Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/nil/nildummy/nildummy.c

    raadf01e r21580dd  
    7777 *  @param[in,out] icall The message parameters.
    7878 */
    79 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall);
     79void    nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall );
    8080
    8181/** Registers new device or updates the MTU of an existing one.
     
    9090 *  @returns Other error codes as defined for the netif_get_addr_req() function.
    9191 */
    92 int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu);
     92int     nildummy_device_message( device_id_t device_id, services_t service, size_t mtu );
    9393
    9494/** Returns the device packet dimensions for sending.
     
    102102 *  @returns ENOENT if there is no such device.
    103103 */
    104 int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix);
     104int     nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
    105105
    106106/** Registers receiving module service.
     
    112112 *  @returns ENOMEM if there is not enough memory left.
    113113 */
    114 int nildummy_register_message(services_t service, int phone);
     114int     nildummy_register_message( services_t service, int phone );
    115115
    116116/** Sends the packet queue.
     
    122122 *  @returns EINVAL if the service parameter is not known.
    123123 */
    124 int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender);
     124int     nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender );
    125125
    126126/** Returns the device hardware address.
     
    131131 *  @returns ENOENT if there no such device.
    132132 */
    133 int nildummy_addr_message(device_id_t device_id, measured_string_ref * address);
     133int     nildummy_addr_message( device_id_t device_id, measured_string_ref * address );
    134134
    135135/*@}*/
    136136
    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){
     137DEVICE_MAP_IMPLEMENT( nildummy_devices, nildummy_device_t )
     138
     139int     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
     146int nil_initialize( int net_phone ){
    149147        ERROR_DECLARE;
    150148
    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 );
    155153        nildummy_globals.net_phone = net_phone;
    156154        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
     161int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu ){
    164162        ERROR_DECLARE;
    165163
    166         nildummy_device_ref device;
    167         int index;
    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 );
    170168        // 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 );
    176174                        return EEXIST;
    177175                }else{
    178176                        // update mtu
    179                         if(mtu > 0){
     177                        if( mtu > 0 ){
    180178                                device->mtu = mtu;
    181179                        }else{
    182180                                device->mtu = NET_DEFAULT_MTU;
    183181                        }
    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 );
    186184                        // 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 );
    190188                        }
    191                         fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
     189                        fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
    192190                        return EOK;
    193191                }
    194192        }else{
    195193                // 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;
    200196                device->device_id = device_id;
    201197                device->service = service;
    202                 if(mtu > 0){
     198                if( mtu > 0 ){
    203199                        device->mtu = mtu;
    204200                }else{
     
    206202                }
    207203                // 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 );
    212208                        return device->phone;
    213209                }
    214210                // 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 );
    218214                        return ERROR_CODE;
    219215                }
    220216                // 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 );
    227223                        return index;
    228224                }
    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
     231int 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 );
    245239                return ENOENT;
    246240        }
    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
     246int 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 );
    262254                return ENOENT;
    263255        }
    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){
     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
     264int 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 ){
    277269                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 );
    280272                        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
     279int nildummy_register_message( services_t service, int phone ){
     280        fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
    289281        nildummy_globals.proto.service = service;
    290282        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 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);
     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
     288int 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 );
    303295                return ENOENT;
    304296        }
    305297        // 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
     305int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
    314306        ERROR_DECLARE;
    315307
    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)){
     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 )){
    322314                case IPC_M_PHONE_HUNGUP:
    323315                        return EOK;
    324316                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 ));
    326318                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 ));
    329321                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;
    332324                        return EOK;
    333325                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 );
    336328                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 ));
    338330        }
    339331        return ENOTSUP;
    340332}
    341333
    342 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){
     334void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall ){
    343335        ERROR_DECLARE;
    344336
    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)){
     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 )){
    350342                        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 );
    353345                                break;
    354346                        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 );
    357349                                }
    358                                 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
     350                                ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
    359351                                break;
    360352                        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 );
    364356        }
    365357}
Note: See TracChangeset for help on using the changeset viewer.