Ignore:
File:
1 edited

Legend:

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

    r849ed54 r14f1db0  
    2828
    2929/** @addtogroup nildummy
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Dummy network interface layer module implementation.
    35  *  @see nildummy.h
     34 * Dummy network interface layer module implementation.
     35 * @see nildummy.h
    3636 */
    3737
     
    4141#include <stdio.h>
    4242#include <str.h>
    43 
    4443#include <ipc/ipc.h>
    4544#include <ipc/services.h>
     
    5453#include <adt/measured_strings.h>
    5554#include <packet/packet.h>
    56 #include <nil_module.h>
     55#include <packet_remote.h>
     56#include <nil_local.h>
    5757
    5858#include "nildummy.h"
    5959
    6060/** The module name.
    61  */
    62 #define NAME    "Dummy nil protocol"
     61 *
     62 */
     63#define NAME  "nildummy"
    6364
    6465/** Default maximum transmission unit.
    65  */
    66 #define NET_DEFAULT_MTU 1500
     66 *
     67 */
     68#define NET_DEFAULT_MTU  1500
    6769
    6870/** Network interface layer module global data.
    69  */
    70 nildummy_globals_t      nildummy_globals;
    71 
    72 /** @name Message processing functions
    73  */
    74 /*@{*/
    75 
    76 /** Processes IPC messages from the registered device driver modules in an infinite loop.
    77  *  @param[in] iid The message identifier.
    78  *  @param[in,out] icall The message parameters.
    79  */
    80 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall);
    81 
    82 /** Registers new device or updates the MTU of an existing one.
    83  *  Determines the device local hardware address.
    84  *  @param[in] device_id The new device identifier.
    85  *  @param[in] service The device driver service.
    86  *  @param[in] mtu The device maximum transmission unit.
    87  *  @returns EOK on success.
    88  *  @returns EEXIST if the device with the different service exists.
    89  *  @returns ENOMEM if there is not enough memory left.
    90  *  @returns Other error codes as defined for the netif_bind_service() function.
    91  *  @returns Other error codes as defined for the netif_get_addr_req() function.
    92  */
    93 int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu);
    94 
    95 /** Returns the device packet dimensions for sending.
    96  *  @param[in] device_id The device identifier.
    97  *  @param[out] addr_len The minimum reserved address length.
    98  *  @param[out] prefix The minimum reserved prefix size.
    99  *  @param[out] content The maximum content size.
    100  *  @param[out] suffix The minimum reserved suffix size.
    101  *  @returns EOK on success.
    102  *  @returns EBADMEM if either one of the parameters is NULL.
    103  *  @returns ENOENT if there is no such device.
    104  */
    105 int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix);
    106 
    107 /** Registers receiving module service.
    108  *  Passes received packets for this service.
    109  *  @param[in] service The module service.
    110  *  @param[in] phone The service phone.
    111  *  @returns EOK on success.
    112  *  @returns ENOENT if the service is not known.
    113  *  @returns ENOMEM if there is not enough memory left.
    114  */
    115 int nildummy_register_message(services_t service, int phone);
    116 
    117 /** Sends the packet queue.
    118  *  @param[in] device_id The device identifier.
    119  *  @param[in] packet The packet queue.
    120  *  @param[in] sender The sending module service.
    121  *  @returns EOK on success.
    122  *  @returns ENOENT if there no such device.
    123  *  @returns EINVAL if the service parameter is not known.
    124  */
    125 int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender);
    126 
    127 /** Returns the device hardware address.
    128  *  @param[in] device_id The device identifier.
    129  *  @param[out] address The device hardware address.
    130  *  @returns EOK on success.
    131  *  @returns EBADMEM if the address parameter is NULL.
    132  *  @returns ENOENT if there no such device.
    133  */
    134 int nildummy_addr_message(device_id_t device_id, measured_string_ref * address);
    135 
    136 /*@}*/
    137 
    138 DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t)
    139 
    140 int nil_device_state_msg(int nil_phone, device_id_t device_id, int state){
     71 *
     72 */
     73nildummy_globals_t nildummy_globals;
     74
     75DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t);
     76
     77int nil_device_state_msg_local(int nil_phone, device_id_t device_id, int state)
     78{
    14179        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
    142         if(nildummy_globals.proto.phone){
    143                 il_device_state_msg(nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service);
    144         }
     80       
     81        if (nildummy_globals.proto.phone)
     82                il_device_state_msg(nildummy_globals.proto.phone, device_id, state,
     83                    nildummy_globals.proto.service);
     84       
    14585        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
    146         return EOK;
    147 }
    148 
    149 int nil_initialize(int net_phone){
     86       
     87        return EOK;
     88}
     89
     90int nil_initialize(int net_phone)
     91{
    15092        ERROR_DECLARE;
    151 
     93       
    15294        fibril_rwlock_initialize(&nildummy_globals.devices_lock);
    15395        fibril_rwlock_initialize(&nildummy_globals.protos_lock);
    15496        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
    15597        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
     98       
    15699        nildummy_globals.net_phone = net_phone;
    157100        nildummy_globals.proto.phone = 0;
    158101        ERROR_PROPAGATE(nildummy_devices_initialize(&nildummy_globals.devices));
     102       
    159103        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
    160104        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    161         return EOK;
    162 }
    163 
    164 int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu){
     105       
     106        return EOK;
     107}
     108
     109/** Process IPC messages from the registered device driver modules in an infinite loop.
     110 *
     111 * @param[in]     iid   The message identifier.
     112 * @param[in,out] icall The message parameters.
     113 *
     114 */
     115static void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){
     116        ERROR_DECLARE;
     117
     118        packet_t packet;
     119
     120        while(true){
     121                switch(IPC_GET_METHOD(*icall)){
     122                        case NET_NIL_DEVICE_STATE:
     123                                ERROR_CODE = nil_device_state_msg_local(0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));
     124                                ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
     125                                break;
     126                        case NET_NIL_RECEIVED:
     127                                if(! ERROR_OCCURRED(packet_translate_remote(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){
     128                                        ERROR_CODE = nil_received_msg_local(0, IPC_GET_DEVICE(icall), packet, 0);
     129                                }
     130                                ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
     131                                break;
     132                        default:
     133                                ipc_answer_0(iid, (ipcarg_t) ENOTSUP);
     134                }
     135                iid = async_get_call(icall);
     136        }
     137}
     138
     139/** Register new device or updates the MTU of an existing one.
     140 *
     141 * Determine the device local hardware address.
     142 *
     143 * @param[in] device_id The new device identifier.
     144 * @param[in] service   The device driver service.
     145 * @param[in] mtu       The device maximum transmission unit.
     146 *
     147 * @returns EOK on success.
     148 * @returns EEXIST if the device with the different service exists.
     149 * @returns ENOMEM if there is not enough memory left.
     150 * @returns Other error codes as defined for the netif_bind_service() function.
     151 * @returns Other error codes as defined for the netif_get_addr_req() function.
     152 *
     153 */
     154static int nildummy_device_message(device_id_t device_id, services_t service,
     155    size_t mtu)
     156{
    165157        ERROR_DECLARE;
    166158
     
    228220                        return index;
    229221                }
    230                 printf("New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu);
     222                printf("%s: Device registered (id: %d, service: %d, mtu: %d)\n",
     223                    NAME, device->device_id, device->service, device->mtu);
    231224        }
    232225        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     
    234227}
    235228
    236 int nildummy_addr_message(device_id_t device_id, measured_string_ref * address){
     229/** Return the device hardware address.
     230 *
     231 * @param[in]  device_id The device identifier.
     232 * @param[out] address   The device hardware address.
     233 *
     234 * @return EOK on success.
     235 * @return EBADMEM if the address parameter is NULL.
     236 * @return ENOENT if there no such device.
     237 *
     238 */
     239static int nildummy_addr_message(device_id_t device_id,
     240    measured_string_ref *address)
     241{
    237242        nildummy_device_ref device;
    238243
     
    251256}
    252257
    253 int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix){
     258/** Return the device packet dimensions for sending.
     259 *
     260 * @param[in]  device_id The device identifier.
     261 * @param[out] addr_len  The minimum reserved address length.
     262 * @param[out] prefix    The minimum reserved prefix size.
     263 * @param[out] content   The maximum content size.
     264 * @param[out] suffix    The minimum reserved suffix size.
     265 *
     266 * @return EOK on success.
     267 * @return EBADMEM if either one of the parameters is NULL.
     268 * @return ENOENT if there is no such device.
     269 *
     270 */
     271static int nildummy_packet_space_message(device_id_t device_id,
     272    size_t *addr_len, size_t *prefix, size_t *content, size_t *suffix)
     273{
    254274        nildummy_device_ref device;
    255275
     
    271291}
    272292
    273 int nil_received_msg(int nil_phone, device_id_t device_id, packet_t packet, services_t target){
     293int nil_received_msg_local(int nil_phone, device_id_t device_id, packet_t packet, services_t target){
    274294        packet_t next;
    275295
     
    286306}
    287307
    288 int nildummy_register_message(services_t service, int phone){
     308/** Register receiving module service.
     309 *
     310 * Pass received packets for this service.
     311 *
     312 * @param[in] service The module service.
     313 * @param[in] phone   The service phone.
     314 *
     315 * @return EOK on success.
     316 * @return ENOENT if the service is not known.
     317 * @return ENOMEM if there is not enough memory left.
     318 *
     319 */
     320static int nildummy_register_message(services_t service, int phone)
     321{
    289322        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
    290323        nildummy_globals.proto.service = service;
    291324        nildummy_globals.proto.phone = phone;
    292         printf("New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone);
     325       
     326        printf("%s: Protocol registered (service: %d, phone: %d)\n",
     327            NAME, nildummy_globals.proto.service, nildummy_globals.proto.phone);
     328       
    293329        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
    294330        return EOK;
    295331}
    296332
    297 int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender){
     333/** Send the packet queue.
     334 *
     335 * @param[in] device_id The device identifier.
     336 * @param[in] packet    The packet queue.
     337 * @param[in] sender    The sending module service.
     338 *
     339 * @return EOK on success.
     340 * @return ENOENT if there no such device.
     341 * @return EINVAL if the service parameter is not known.
     342 *
     343 */
     344static int nildummy_send_message(device_id_t device_id, packet_t packet,
     345    services_t sender)
     346{
    298347        nildummy_device_ref device;
    299348
     
    312361}
    313362
    314 int nil_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     363int nil_message_standalone(const char *name, ipc_callid_t callid, ipc_call_t *call,
     364    ipc_call_t *answer, int *answer_count)
     365{
    315366        ERROR_DECLARE;
    316 
     367       
    317368        measured_string_ref address;
    318369        packet_t packet;
     
    321372        size_t suffix;
    322373        size_t content;
    323 
    324 //      printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NIL_FIRST);
     374       
    325375        *answer_count = 0;
    326         switch(IPC_GET_METHOD(*call)){
     376        switch (IPC_GET_METHOD(*call)) {
    327377                case IPC_M_PHONE_HUNGUP:
    328378                        return EOK;
    329379                case NET_NIL_DEVICE:
    330                         return nildummy_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), IPC_GET_MTU(call));
     380                        return nildummy_device_message(IPC_GET_DEVICE(call),
     381                            IPC_GET_SERVICE(call), IPC_GET_MTU(call));
    331382                case NET_NIL_SEND:
    332                         ERROR_PROPAGATE(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(call)));
    333                         return nildummy_send_message(IPC_GET_DEVICE(call), packet, IPC_GET_SERVICE(call));
     383                        ERROR_PROPAGATE(packet_translate_remote(nildummy_globals.net_phone,
     384                            &packet, IPC_GET_PACKET(call)));
     385                        return nildummy_send_message(IPC_GET_DEVICE(call), packet,
     386                            IPC_GET_SERVICE(call));
    334387                case NET_NIL_PACKET_SPACE:
    335                         ERROR_PROPAGATE(nildummy_packet_space_message(IPC_GET_DEVICE(call), &addrlen, &prefix, &content, &suffix));
     388                        ERROR_PROPAGATE(nildummy_packet_space_message(IPC_GET_DEVICE(call),
     389                            &addrlen, &prefix, &content, &suffix));
    336390                        IPC_SET_ADDR(answer, addrlen);
    337391                        IPC_SET_PREFIX(answer, prefix);
     
    341395                        return EOK;
    342396                case NET_NIL_ADDR:
    343                         ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), &address));
     397                        ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call),
     398                            &address));
     399                        return measured_strings_reply(address, 1);
     400                case NET_NIL_BROADCAST_ADDR:
     401                        ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call),
     402                            &address));
    344403                        return measured_strings_reply(address, 1);
    345404                case IPC_M_CONNECT_TO_ME:
    346                         return nildummy_register_message(NIL_GET_PROTO(call), IPC_GET_PHONE(call));
    347         }
     405                        return nildummy_register_message(NIL_GET_PROTO(call),
     406                            IPC_GET_PHONE(call));
     407        }
     408       
    348409        return ENOTSUP;
    349410}
    350411
    351 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){
    352         ERROR_DECLARE;
    353 
    354         packet_t packet;
    355 
    356         while(true){
    357 //              printf("message %d - %d\n", IPC_GET_METHOD(*icall), NET_NIL_FIRST);
    358                 switch(IPC_GET_METHOD(*icall)){
    359                         case NET_NIL_DEVICE_STATE:
    360                                 ERROR_CODE = nil_device_state_msg(0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));
    361                                 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
    362                                 break;
    363                         case NET_NIL_RECEIVED:
    364                                 if(! ERROR_OCCURRED(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){
    365                                         ERROR_CODE = nil_received_msg(0, IPC_GET_DEVICE(icall), packet, 0);
    366                                 }
    367                                 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
    368                                 break;
    369                         default:
    370                                 ipc_answer_0(iid, (ipcarg_t) ENOTSUP);
    371                 }
    372                 iid = async_get_call(icall);
    373         }
    374 }
    375 
    376 #ifdef CONFIG_NETWORKING_modular
    377 
    378 #include <nil_standalone.h>
     412#ifndef CONFIG_NETIF_NIL_BUNDLE
    379413
    380414/** Default thread for new connections.
    381415 *
    382  *  @param[in] iid The initial message identifier.
    383  *  @param[in] icall The initial message call structure.
    384  *
    385  */
    386 static void nil_client_connection(ipc_callid_t iid, ipc_call_t * icall)
     416 * @param[in] iid  The initial message identifier.
     417 * @param[in] icall The initial message call structure.
     418 *
     419 */
     420static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall)
    387421{
    388422        /*
     
    404438               
    405439                /* Process the message */
    406                 int res = nil_module_message(callid, &call, &answer, &answer_count);
     440                int res = nil_module_message_standalone(NAME, callid, &call, &answer,
     441                    &answer_count);
    407442               
    408443                /* End if said to either by the message or the processing result */
     
    415450}
    416451
    417 /** Starts the module.
    418  *
    419  *  @param argc The count of the command line arguments. Ignored parameter.
    420  *  @param argv The command line parameters. Ignored parameter.
    421  *
    422  *  @returns EOK on success.
    423  *  @returns Other error codes as defined for each specific module start function.
    424  *
    425  */
    426452int main(int argc, char *argv[])
    427453{
    428454        ERROR_DECLARE;
    429455       
    430         /* Print the module label */
    431         printf("Task %d - %s\n", task_get_id(), NAME);
    432        
    433456        /* Start the module */
    434         if (ERROR_OCCURRED(nil_module_start(nil_client_connection))) {
    435                 printf(" - ERROR %i\n", ERROR_CODE);
     457        if (ERROR_OCCURRED(nil_module_start_standalone(nil_client_connection)))
    436458                return ERROR_CODE;
    437         }
    438        
    439         return EOK;
    440 }
    441 
    442 #endif /* CONFIG_NETWORKING_modular */
     459       
     460        return EOK;
     461}
     462
     463#endif /* CONFIG_NETIF_NIL_BUNDLE */
    443464
    444465/** @}
Note: See TracChangeset for help on using the changeset viewer.