Changes in uspace/srv/net/nil/nildummy/nildummy.c [21580dd:aadf01e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/nil/nildummy/nildummy.c
r21580dd raadf01e 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 ) 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 ){ 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){ 147 149 ERROR_DECLARE; 148 150 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);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); 153 155 nildummy_globals.net_phone = net_phone; 154 156 nildummy_globals.proto.phone = 0; 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){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){ 162 164 ERROR_DECLARE; 163 165 164 nildummy_device_ref 165 int 166 167 fibril_rwlock_write_lock( & nildummy_globals.devices_lock);166 nildummy_device_ref device; 167 int index; 168 169 fibril_rwlock_write_lock(&nildummy_globals.devices_lock); 168 170 // an existing device? 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);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); 174 176 return EEXIST; 175 177 }else{ 176 178 // update mtu 177 if( mtu > 0){179 if(mtu > 0){ 178 180 device->mtu = mtu; 179 181 }else{ 180 182 device->mtu = NET_DEFAULT_MTU; 181 183 } 182 printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu);183 fibril_rwlock_write_unlock( & nildummy_globals.devices_lock);184 printf("Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu); 185 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 184 186 // notify the upper layer module 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);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); 188 190 } 189 fibril_rwlock_read_unlock( & nildummy_globals.protos_lock);191 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock); 190 192 return EOK; 191 193 } 192 194 }else{ 193 195 // create a new device 194 device = ( nildummy_device_ref ) malloc( sizeof( nildummy_device_t )); 195 if( ! device ) return ENOMEM; 196 device = (nildummy_device_ref) malloc(sizeof(nildummy_device_t)); 197 if(! device){ 198 return ENOMEM; 199 } 196 200 device->device_id = device_id; 197 201 device->service = service; 198 if( mtu > 0){202 if(mtu > 0){ 199 203 device->mtu = mtu; 200 204 }else{ … … 202 206 } 203 207 // bind the device driver 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);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); 208 212 return device->phone; 209 213 } 210 214 // get hardware address 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);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); 214 218 return ERROR_CODE; 215 219 } 216 220 // add to the cache 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);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); 223 227 return index; 224 228 } 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 ); 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); 239 245 return ENOENT; 240 246 } 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 ); 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); 254 262 return ENOENT; 255 263 } 256 * 257 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock);258 * 259 * 260 * 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 266 267 fibril_rwlock_read_lock( & nildummy_globals.protos_lock);268 if( nildummy_globals.proto.phone){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 next; 274 275 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 276 if(nildummy_globals.proto.phone){ 269 277 do{ 270 next = pq_detach( packet);271 il_received_msg( nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service);278 next = pq_detach(packet); 279 il_received_msg(nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service); 272 280 packet = next; 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);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); 281 289 nildummy_globals.proto.service = service; 282 290 nildummy_globals.proto.phone = phone; 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 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);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 device; 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); 295 303 return ENOENT; 296 304 } 297 305 // send packet queue 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){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){ 306 314 ERROR_DECLARE; 307 315 308 measured_string_ref 309 packet_t 310 311 // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST);312 * 313 switch( IPC_GET_METHOD( * call)){316 measured_string_ref address; 317 packet_t packet; 318 319 // printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NIL_FIRST); 320 *answer_count = 0; 321 switch(IPC_GET_METHOD(*call)){ 314 322 case IPC_M_PHONE_HUNGUP: 315 323 return EOK; 316 324 case NET_NIL_DEVICE: 317 return nildummy_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call));325 return nildummy_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), IPC_GET_MTU(call)); 318 326 case NET_NIL_SEND: 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));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)); 321 329 case NET_NIL_PACKET_SPACE: 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 * 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; 324 332 return EOK; 325 333 case NET_NIL_ADDR: 326 ERROR_PROPAGATE( nildummy_addr_message( IPC_GET_DEVICE( call ), & address));327 return measured_strings_reply( address, 1);334 ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), &address)); 335 return measured_strings_reply(address, 1); 328 336 case IPC_M_CONNECT_TO_ME: 329 return nildummy_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call));337 return nildummy_register_message(NIL_GET_PROTO(call), IPC_GET_PHONE(call)); 330 338 } 331 339 return ENOTSUP; 332 340 } 333 341 334 void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall){342 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){ 335 343 ERROR_DECLARE; 336 344 337 packet_t 338 339 while( true){340 // printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST);341 switch( IPC_GET_METHOD( * icall)){345 packet_t packet; 346 347 while(true){ 348 // printf("message %d - %d\n", IPC_GET_METHOD(*icall), NET_NIL_FIRST); 349 switch(IPC_GET_METHOD(*icall)){ 342 350 case NET_NIL_DEVICE_STATE: 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);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); 345 353 break; 346 354 case NET_NIL_RECEIVED: 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);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); 349 357 } 350 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE);358 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE); 351 359 break; 352 360 default: 353 ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP);354 } 355 iid = async_get_call( icall);361 ipc_answer_0(iid, (ipcarg_t) ENOTSUP); 362 } 363 iid = async_get_call(icall); 356 364 } 357 365 }
Note:
See TracChangeset
for help on using the changeset viewer.