Changes in uspace/srv/net/nil/nildummy/nildummy.c [aadf01e:21580dd] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/nil/nildummy/nildummy.c
raadf01e r21580dd 77 77 * @param[in,out] icall The message parameters. 78 78 */ 79 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall);79 void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall ); 80 80 81 81 /** Registers new device or updates the MTU of an existing one. … … 90 90 * @returns Other error codes as defined for the netif_get_addr_req() function. 91 91 */ 92 int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu);92 int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu ); 93 93 94 94 /** Returns the device packet dimensions for sending. … … 102 102 * @returns ENOENT if there is no such device. 103 103 */ 104 int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix);104 int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); 105 105 106 106 /** Registers receiving module service. … … 112 112 * @returns ENOMEM if there is not enough memory left. 113 113 */ 114 int nildummy_register_message(services_t service, int phone);114 int nildummy_register_message( services_t service, int phone ); 115 115 116 116 /** Sends the packet queue. … … 122 122 * @returns EINVAL if the service parameter is not known. 123 123 */ 124 int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender);124 int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender ); 125 125 126 126 /** Returns the device hardware address. … … 131 131 * @returns ENOENT if there no such device. 132 132 */ 133 int nildummy_addr_message(device_id_t device_id, measured_string_ref * address);133 int nildummy_addr_message( device_id_t device_id, measured_string_ref * address ); 134 134 135 135 /*@}*/ 136 136 137 DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t) 138 139 int nil_device_state_msg(int nil_phone, device_id_t device_id, int state){ 140 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 141 if(nildummy_globals.proto.phone){ 142 il_device_state_msg(nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service); 143 } 144 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock); 145 return EOK; 146 } 147 148 int nil_initialize(int net_phone){ 137 DEVICE_MAP_IMPLEMENT( nildummy_devices, nildummy_device_t ) 138 139 int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){ 140 fibril_rwlock_read_lock( & nildummy_globals.protos_lock ); 141 if( nildummy_globals.proto.phone ) il_device_state_msg( nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service ); 142 fibril_rwlock_read_unlock( & nildummy_globals.protos_lock ); 143 return EOK; 144 } 145 146 int nil_initialize( int net_phone ){ 149 147 ERROR_DECLARE; 150 148 151 fibril_rwlock_initialize( &nildummy_globals.devices_lock);152 fibril_rwlock_initialize( &nildummy_globals.protos_lock);153 fibril_rwlock_write_lock( &nildummy_globals.devices_lock);154 fibril_rwlock_write_lock( &nildummy_globals.protos_lock);149 fibril_rwlock_initialize( & nildummy_globals.devices_lock ); 150 fibril_rwlock_initialize( & nildummy_globals.protos_lock ); 151 fibril_rwlock_write_lock( & nildummy_globals.devices_lock ); 152 fibril_rwlock_write_lock( & nildummy_globals.protos_lock ); 155 153 nildummy_globals.net_phone = net_phone; 156 154 nildummy_globals.proto.phone = 0; 157 ERROR_PROPAGATE( nildummy_devices_initialize(&nildummy_globals.devices));158 fibril_rwlock_write_unlock( &nildummy_globals.protos_lock);159 fibril_rwlock_write_unlock( &nildummy_globals.devices_lock);160 return EOK; 161 } 162 163 int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu){155 ERROR_PROPAGATE( nildummy_devices_initialize( & nildummy_globals.devices )); 156 fibril_rwlock_write_unlock( & nildummy_globals.protos_lock ); 157 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock ); 158 return EOK; 159 } 160 161 int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu ){ 164 162 ERROR_DECLARE; 165 163 166 nildummy_device_ref 167 int 168 169 fibril_rwlock_write_lock( &nildummy_globals.devices_lock);164 nildummy_device_ref device; 165 int index; 166 167 fibril_rwlock_write_lock( & nildummy_globals.devices_lock ); 170 168 // an existing device? 171 device = nildummy_devices_find( &nildummy_globals.devices, device_id);172 if( device){173 if( device->service != service){174 printf( "Device %d already exists\n", device->device_id);175 fibril_rwlock_write_unlock( &nildummy_globals.devices_lock);169 device = nildummy_devices_find( & nildummy_globals.devices, device_id ); 170 if( device ){ 171 if( device->service != service ){ 172 printf( "Device %d already exists\n", device->device_id ); 173 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock ); 176 174 return EEXIST; 177 175 }else{ 178 176 // update mtu 179 if( mtu > 0){177 if( mtu > 0 ){ 180 178 device->mtu = mtu; 181 179 }else{ 182 180 device->mtu = NET_DEFAULT_MTU; 183 181 } 184 printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu);185 fibril_rwlock_write_unlock( &nildummy_globals.devices_lock);182 printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu ); 183 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock ); 186 184 // notify the upper layer module 187 fibril_rwlock_read_lock( &nildummy_globals.protos_lock);188 if( nildummy_globals.proto.phone){189 il_mtu_changed_msg( nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service);185 fibril_rwlock_read_lock( & nildummy_globals.protos_lock ); 186 if( nildummy_globals.proto.phone ){ 187 il_mtu_changed_msg( nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service ); 190 188 } 191 fibril_rwlock_read_unlock( &nildummy_globals.protos_lock);189 fibril_rwlock_read_unlock( & nildummy_globals.protos_lock ); 192 190 return EOK; 193 191 } 194 192 }else{ 195 193 // create a new device 196 device = (nildummy_device_ref) malloc(sizeof(nildummy_device_t)); 197 if(! device){ 198 return ENOMEM; 199 } 194 device = ( nildummy_device_ref ) malloc( sizeof( nildummy_device_t )); 195 if( ! device ) return ENOMEM; 200 196 device->device_id = device_id; 201 197 device->service = service; 202 if( mtu > 0){198 if( mtu > 0 ){ 203 199 device->mtu = mtu; 204 200 }else{ … … 206 202 } 207 203 // bind the device driver 208 device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver);209 if( device->phone < 0){210 fibril_rwlock_write_unlock( &nildummy_globals.devices_lock);211 free( device);204 device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver ); 205 if( device->phone < 0 ){ 206 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock ); 207 free( device ); 212 208 return device->phone; 213 209 } 214 210 // get hardware address 215 if( ERROR_OCCURRED(netif_get_addr_req(device->phone, device->device_id, &device->addr, &device->addr_data))){216 fibril_rwlock_write_unlock( &nildummy_globals.devices_lock);217 free( device);211 if( ERROR_OCCURRED( netif_get_addr_req( device->phone, device->device_id, & device->addr, & device->addr_data ))){ 212 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock ); 213 free( device ); 218 214 return ERROR_CODE; 219 215 } 220 216 // add to the cache 221 index = nildummy_devices_add( &nildummy_globals.devices, device->device_id, device);222 if( index < 0){223 fibril_rwlock_write_unlock( &nildummy_globals.devices_lock);224 free( device->addr);225 free( device->addr_data);226 free( device);217 index = nildummy_devices_add( & nildummy_globals.devices, device->device_id, device ); 218 if( index < 0 ){ 219 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock ); 220 free( device->addr ); 221 free( device->addr_data ); 222 free( device ); 227 223 return index; 228 224 } 229 printf("New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu); 230 } 231 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 232 return EOK; 233 } 234 235 int nildummy_addr_message(device_id_t device_id, measured_string_ref * address){ 236 nildummy_device_ref device; 237 238 if(! address){ 239 return EBADMEM; 240 } 241 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 242 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 243 if(! device){ 244 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 225 printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu ); 226 } 227 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock ); 228 return EOK; 229 } 230 231 int nildummy_addr_message( device_id_t device_id, measured_string_ref * address ){ 232 nildummy_device_ref device; 233 234 if( ! address ) return EBADMEM; 235 fibril_rwlock_read_lock( & nildummy_globals.devices_lock ); 236 device = nildummy_devices_find( & nildummy_globals.devices, device_id ); 237 if( ! device ){ 238 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock ); 245 239 return ENOENT; 246 240 } 247 *address = device->addr; 248 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 249 return (*address) ? EOK : ENOENT; 250 } 251 252 int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix){ 253 nildummy_device_ref device; 254 255 if(!(addr_len && prefix && content && suffix)){ 256 return EBADMEM; 257 } 258 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 259 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 260 if(! device){ 261 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 241 * address = device->addr; 242 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock ); 243 return ( * address ) ? EOK : ENOENT; 244 } 245 246 int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ 247 nildummy_device_ref device; 248 249 if( !( addr_len && prefix && content && suffix )) return EBADMEM; 250 fibril_rwlock_read_lock( & nildummy_globals.devices_lock ); 251 device = nildummy_devices_find( & nildummy_globals.devices, device_id ); 252 if( ! device ){ 253 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock ); 262 254 return ENOENT; 263 255 } 264 * content = device->mtu;265 fibril_rwlock_read_unlock( &nildummy_globals.devices_lock);266 * addr_len = 0;267 * prefix = 0;268 * suffix = 0;269 return EOK; 270 } 271 272 int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target){273 packet_t 274 275 fibril_rwlock_read_lock( &nildummy_globals.protos_lock);276 if( nildummy_globals.proto.phone){256 * content = device->mtu; 257 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock ); 258 * addr_len = 0; 259 * prefix = 0; 260 * suffix = 0; 261 return EOK; 262 } 263 264 int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){ 265 packet_t next; 266 267 fibril_rwlock_read_lock( & nildummy_globals.protos_lock ); 268 if( nildummy_globals.proto.phone ){ 277 269 do{ 278 next = pq_detach( packet);279 il_received_msg( nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service);270 next = pq_detach( packet ); 271 il_received_msg( nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service ); 280 272 packet = next; 281 }while( packet);282 } 283 fibril_rwlock_read_unlock( &nildummy_globals.protos_lock);284 return EOK; 285 } 286 287 int nildummy_register_message( services_t service, int phone){288 fibril_rwlock_write_lock( &nildummy_globals.protos_lock);273 }while( packet ); 274 } 275 fibril_rwlock_read_unlock( & nildummy_globals.protos_lock ); 276 return EOK; 277 } 278 279 int nildummy_register_message( services_t service, int phone ){ 280 fibril_rwlock_write_lock( & nildummy_globals.protos_lock ); 289 281 nildummy_globals.proto.service = service; 290 282 nildummy_globals.proto.phone = phone; 291 printf( "New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone);292 fibril_rwlock_write_unlock( &nildummy_globals.protos_lock);293 return EOK; 294 } 295 296 int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender){297 nildummy_device_ref 298 299 fibril_rwlock_read_lock( &nildummy_globals.devices_lock);300 device = nildummy_devices_find( &nildummy_globals.devices, device_id);301 if( ! device){302 fibril_rwlock_read_unlock( &nildummy_globals.devices_lock);283 printf( "New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone ); 284 fibril_rwlock_write_unlock( & nildummy_globals.protos_lock ); 285 return EOK; 286 } 287 288 int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender ){ 289 nildummy_device_ref device; 290 291 fibril_rwlock_read_lock( & nildummy_globals.devices_lock ); 292 device = nildummy_devices_find( & nildummy_globals.devices, device_id ); 293 if( ! device ){ 294 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock ); 303 295 return ENOENT; 304 296 } 305 297 // send packet queue 306 if( packet){307 netif_send_msg( device->phone, device_id, packet, SERVICE_NILDUMMY);308 } 309 fibril_rwlock_read_unlock( &nildummy_globals.devices_lock);310 return EOK; 311 } 312 313 int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){298 if( packet ){ 299 netif_send_msg( device->phone, device_id, packet, SERVICE_NILDUMMY ); 300 } 301 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock ); 302 return EOK; 303 } 304 305 int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ 314 306 ERROR_DECLARE; 315 307 316 measured_string_ref 317 packet_t 318 319 // printf( "message %d - %d\n", IPC_GET_METHOD(*call), NET_NIL_FIRST);320 * answer_count = 0;321 switch( IPC_GET_METHOD(*call)){308 measured_string_ref address; 309 packet_t packet; 310 311 // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST ); 312 * answer_count = 0; 313 switch( IPC_GET_METHOD( * call )){ 322 314 case IPC_M_PHONE_HUNGUP: 323 315 return EOK; 324 316 case NET_NIL_DEVICE: 325 return nildummy_device_message( IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), IPC_GET_MTU(call));317 return nildummy_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call )); 326 318 case NET_NIL_SEND: 327 ERROR_PROPAGATE( packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(call)));328 return nildummy_send_message( IPC_GET_DEVICE(call), packet, IPC_GET_SERVICE(call));319 ERROR_PROPAGATE( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( call ))); 320 return nildummy_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); 329 321 case NET_NIL_PACKET_SPACE: 330 ERROR_PROPAGATE( nildummy_packet_space_message(IPC_GET_DEVICE(call), IPC_SET_ADDR(answer), IPC_SET_PREFIX(answer), IPC_SET_CONTENT(answer), IPC_SET_SUFFIX(answer)));331 * answer_count = 4;322 ERROR_PROPAGATE( nildummy_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer ))); 323 * answer_count = 4; 332 324 return EOK; 333 325 case NET_NIL_ADDR: 334 ERROR_PROPAGATE( nildummy_addr_message(IPC_GET_DEVICE(call), &address));335 return measured_strings_reply( address, 1);326 ERROR_PROPAGATE( nildummy_addr_message( IPC_GET_DEVICE( call ), & address )); 327 return measured_strings_reply( address, 1 ); 336 328 case IPC_M_CONNECT_TO_ME: 337 return nildummy_register_message( NIL_GET_PROTO(call), IPC_GET_PHONE(call));329 return nildummy_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call )); 338 330 } 339 331 return ENOTSUP; 340 332 } 341 333 342 void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall){334 void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall ){ 343 335 ERROR_DECLARE; 344 336 345 packet_t 346 347 while( true){348 // printf( "message %d - %d\n", IPC_GET_METHOD(*icall), NET_NIL_FIRST);349 switch( IPC_GET_METHOD(*icall)){337 packet_t packet; 338 339 while( true ){ 340 // printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST ); 341 switch( IPC_GET_METHOD( * icall )){ 350 342 case NET_NIL_DEVICE_STATE: 351 ERROR_CODE = nil_device_state_msg( 0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));352 ipc_answer_0( iid, (ipcarg_t) ERROR_CODE);343 ERROR_CODE = nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall )); 344 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE ); 353 345 break; 354 346 case NET_NIL_RECEIVED: 355 if( ! ERROR_OCCURRED(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){356 ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE(icall), packet, 0);347 if( ! ERROR_OCCURRED( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){ 348 ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 ); 357 349 } 358 ipc_answer_0( iid, (ipcarg_t) ERROR_CODE);350 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE ); 359 351 break; 360 352 default: 361 ipc_answer_0( iid, (ipcarg_t) ENOTSUP);362 } 363 iid = async_get_call( icall);353 ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP ); 354 } 355 iid = async_get_call( icall ); 364 356 } 365 357 }
Note:
See TracChangeset
for help on using the changeset viewer.