Ignore:
File:
1 edited

Legend:

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

    rffa2c8ef r228e490  
    4141#include <stdio.h>
    4242#include <str.h>
    43 #include <ipc/nil.h>
     43#include <ipc/ipc.h>
    4444#include <ipc/net.h>
    4545#include <ipc/services.h>
     
    4747#include <net/modules.h>
    4848#include <net/device.h>
    49 #include <il_remote.h>
     49#include <netif_interface.h>
     50#include <nil_interface.h>
     51#include <il_interface.h>
    5052#include <adt/measured_strings.h>
    5153#include <net/packet.h>
    5254#include <packet_remote.h>
    53 #include <netif_remote.h>
    54 #include <nil_skel.h>
     55#include <nil_local.h>
    5556
    5657#include "nildummy.h"
     
    8081int nil_initialize(int net_phone)
    8182{
     83        int rc;
     84       
    8285        fibril_rwlock_initialize(&nildummy_globals.devices_lock);
    8386        fibril_rwlock_initialize(&nildummy_globals.protos_lock);
     
    8790        nildummy_globals.net_phone = net_phone;
    8891        nildummy_globals.proto.phone = 0;
    89         int rc = nildummy_devices_initialize(&nildummy_globals.devices);
     92        rc = nildummy_devices_initialize(&nildummy_globals.devices);
    9093       
    9194        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
     
    9598}
    9699
    97 /** Process IPC messages from the registered device driver modules
    98  *
    99  * @param[in]     iid   Message identifier.
    100  * @param[in,out] icall Message parameters.
    101  *
     100/** Process IPC messages from the registered device driver modules in an
     101 * infinite loop.
     102 *
     103 * @param[in] iid       The message identifier.
     104 * @param[in,out]       icall The message parameters.
    102105 */
    103106static void nildummy_receiver(ipc_callid_t iid, ipc_call_t *icall)
     
    105108        packet_t *packet;
    106109        int rc;
    107        
     110
    108111        while (true) {
    109112                switch (IPC_GET_IMETHOD(*icall)) {
    110113                case NET_NIL_DEVICE_STATE:
    111114                        rc = nil_device_state_msg_local(0,
    112                             IPC_GET_DEVICE(*icall), IPC_GET_STATE(*icall));
    113                         async_answer_0(iid, (sysarg_t) rc);
     115                            IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));
     116                        ipc_answer_0(iid, (sysarg_t) rc);
    114117                        break;
    115118               
    116119                case NET_NIL_RECEIVED:
    117120                        rc = packet_translate_remote(nildummy_globals.net_phone,
    118                             &packet, IPC_GET_PACKET(*icall));
    119                         if (rc == EOK)
     121                            &packet, IPC_GET_PACKET(icall));
     122                        if (rc == EOK) {
    120123                                rc = nil_received_msg_local(0,
    121                                     IPC_GET_DEVICE(*icall), packet, 0);
    122                        
    123                         async_answer_0(iid, (sysarg_t) rc);
     124                                    IPC_GET_DEVICE(icall), packet, 0);
     125                        }
     126                        ipc_answer_0(iid, (sysarg_t) rc);
    124127                        break;
    125128               
    126129                default:
    127                         async_answer_0(iid, (sysarg_t) ENOTSUP);
     130                        ipc_answer_0(iid, (sysarg_t) ENOTSUP);
    128131                }
    129132               
     
    136139 * Determine the device local hardware address.
    137140 *
    138  * @param[in] device_id New device identifier.
    139  * @param[in] service   Device driver service.
    140  * @param[in] mtu       Device maximum transmission unit.
    141  *
    142  * @return EOK on success.
    143  * @return EEXIST if the device with the different service exists.
    144  * @return ENOMEM if there is not enough memory left.
    145  * @return Other error codes as defined for the
    146  *         netif_bind_service() function.
    147  * @return Other error codes as defined for the
    148  *         netif_get_addr_req() function.
    149  *
     141 * @param[in] device_id The new device identifier.
     142 * @param[in] service   The device driver service.
     143 * @param[in] mtu       The device maximum transmission unit.
     144 * @return              EOK on success.
     145 * @return              EEXIST if the device with the different service exists.
     146 * @return              ENOMEM if there is not enough memory left.
     147 * @return              Other error codes as defined for the
     148 *                      netif_bind_service() function.
     149 * @return              Other error codes as defined for the
     150 *                      netif_get_addr_req() function.
    150151 */
    151152static int nildummy_device_message(device_id_t device_id, services_t service,
    152153    size_t mtu)
    153154{
     155        nildummy_device_t *device;
     156        int index;
     157        int rc;
     158
    154159        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
    155        
     160
    156161        /* An existing device? */
    157         nildummy_device_t *device =
    158             nildummy_devices_find(&nildummy_globals.devices, device_id);
     162        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
    159163        if (device) {
    160164                if (device->service != service) {
     
    209213       
    210214        /* Get hardware address */
    211         int rc = netif_get_addr_req(device->phone, device->device_id,
    212             &device->addr, &device->addr_data);
     215        rc = netif_get_addr_req(device->phone, device->device_id, &device->addr,
     216            &device->addr_data);
    213217        if (rc != EOK) {
    214218                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     
    218222       
    219223        /* Add to the cache */
    220         int index = nildummy_devices_add(&nildummy_globals.devices,
     224        index = nildummy_devices_add(&nildummy_globals.devices,
    221225            device->device_id, device);
    222226        if (index < 0) {
     
    236240/** Return the device hardware address.
    237241 *
    238  * @param[in]  device_id Device identifier.
    239  * @param[out] address   Device hardware address.
    240  *
    241  * @return EOK on success.
    242  * @return EBADMEM if the address parameter is NULL.
    243  * @return ENOENT if there no such device.
     242 * @param[in] device_id The device identifier.
     243 * @param[out] address  The device hardware address.
     244 * @return               EOK on success.
     245 * @return              EBADMEM if the address parameter is NULL.
     246 * @return              ENOENT if there no such device.
    244247 *
    245248 */
     
    247250    measured_string_t **address)
    248251{
     252        nildummy_device_t *device;
     253
    249254        if (!address)
    250255                return EBADMEM;
    251256       
    252257        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
    253        
    254         nildummy_device_t *device =
    255             nildummy_devices_find(&nildummy_globals.devices, device_id);
     258        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
    256259        if (!device) {
    257260                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    258261                return ENOENT;
    259262        }
    260        
    261263        *address = device->addr;
    262        
    263264        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    264265       
     
    268269/** Return the device packet dimensions for sending.
    269270 *
    270  * @param[in]  device_id Device identifier.
    271  * @param[out] addr_len  Minimum reserved address length.
    272  * @param[out] prefix    Minimum reserved prefix size.
    273  * @param[out] content   Maximum content size.
    274  * @param[out] suffix    Minimum reserved suffix size.
    275  *
    276  * @return EOK on success.
    277  * @return EBADMEM if either one of the parameters is NULL.
    278  * @return ENOENT if there is no such device.
     271 * @param[in] device_id The device identifier.
     272 * @param[out] addr_len The minimum reserved address length.
     273 * @param[out] prefix   The minimum reserved prefix size.
     274 * @param[out] content  The maximum content size.
     275 * @param[out] suffix   The minimum reserved suffix size.
     276 * @return              EOK on success.
     277 * @return              EBADMEM if either one of the parameters is NULL.
     278 * @return              ENOENT if there is no such device.
    279279 *
    280280 */
     
    282282    size_t *prefix, size_t *content, size_t *suffix)
    283283{
    284         if ((!addr_len) || (!prefix) || (!content) || (!suffix))
     284        nildummy_device_t *device;
     285
     286        if (!addr_len || !prefix || !content || !suffix)
    285287                return EBADMEM;
    286288       
    287289        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
    288        
    289         nildummy_device_t *device =
    290             nildummy_devices_find(&nildummy_globals.devices, device_id);
     290        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
    291291        if (!device) {
    292292                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    293293                return ENOENT;
    294294        }
    295        
     295
    296296        *content = device->mtu;
    297        
    298297        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    299298       
     
    307306    packet_t *packet, services_t target)
    308307{
     308        packet_t *next;
     309
    309310        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
    310        
    311311        if (nildummy_globals.proto.phone) {
    312312                do {
    313                         packet_t *next = pq_detach(packet);
     313                        next = pq_detach(packet);
    314314                        il_received_msg(nildummy_globals.proto.phone, device_id,
    315315                            packet, nildummy_globals.proto.service);
    316316                        packet = next;
    317                 } while (packet);
    318         }
    319        
     317                } while(packet);
     318        }
    320319        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
    321320       
     
    327326 * Pass received packets for this service.
    328327 *
    329  * @param[in] service Module service.
    330  * @param[in] phone   Service phone.
    331  *
    332  * @return EOK on success.
    333  * @return ENOENT if the service is not known.
    334  * @return ENOMEM if there is not enough memory left.
    335  *
     328 * @param[in] service   The module service.
     329 * @param[in] phone     The service phone.
     330 * @return              EOK on success.
     331 * @return              ENOENT if the service is not known.
     332 * @return              ENOMEM if there is not enough memory left.
    336333 */
    337334static int nildummy_register_message(services_t service, int phone)
     
    350347/** Send the packet queue.
    351348 *
    352  * @param[in] device_id Device identifier.
    353  * @param[in] packet    Packet queue.
    354  * @param[in] sender    Sending module service.
    355  *
    356  * @return EOK on success.
    357  * @return ENOENT if there no such device.
    358  * @return EINVAL if the service parameter is not known.
    359  *
     349 * @param[in] device_id The device identifier.
     350 * @param[in] packet    The packet queue.
     351 * @param[in] sender    The sending module service.
     352 * @return              EOK on success.
     353 * @return              ENOENT if there no such device.
     354 * @return              EINVAL if the service parameter is not known.
    360355 */
    361356static int nildummy_send_message(device_id_t device_id, packet_t *packet,
    362357    services_t sender)
    363358{
     359        nildummy_device_t *device;
     360
    364361        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
    365        
    366         nildummy_device_t *device =
    367             nildummy_devices_find(&nildummy_globals.devices, device_id);
     362        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
    368363        if (!device) {
    369364                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    370365                return ENOENT;
    371366        }
    372        
     367
    373368        /* Send packet queue */
    374369        if (packet)
    375370                netif_send_msg(device->phone, device_id, packet,
    376371                    SERVICE_NILDUMMY);
    377        
    378372        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    379        
    380         return EOK;
    381 }
    382 
    383 int nil_module_message(ipc_callid_t callid, ipc_call_t *call,
    384     ipc_call_t *answer, size_t *answer_count)
     373        return EOK;
     374}
     375
     376int nil_message_standalone(const char *name, ipc_callid_t callid,
     377    ipc_call_t *call, ipc_call_t *answer, int *answer_count)
    385378{
    386379        measured_string_t *address;
     
    398391       
    399392        case NET_NIL_DEVICE:
    400                 return nildummy_device_message(IPC_GET_DEVICE(*call),
    401                     IPC_GET_SERVICE(*call), IPC_GET_MTU(*call));
     393                return nildummy_device_message(IPC_GET_DEVICE(call),
     394                    IPC_GET_SERVICE(call), IPC_GET_MTU(call));
    402395       
    403396        case NET_NIL_SEND:
    404397                rc = packet_translate_remote(nildummy_globals.net_phone,
    405                     &packet, IPC_GET_PACKET(*call));
     398                    &packet, IPC_GET_PACKET(call));
    406399                if (rc != EOK)
    407400                        return rc;
    408                 return nildummy_send_message(IPC_GET_DEVICE(*call), packet,
    409                     IPC_GET_SERVICE(*call));
     401                return nildummy_send_message(IPC_GET_DEVICE(call), packet,
     402                    IPC_GET_SERVICE(call));
    410403       
    411404        case NET_NIL_PACKET_SPACE:
    412                 rc = nildummy_packet_space_message(IPC_GET_DEVICE(*call),
     405                rc = nildummy_packet_space_message(IPC_GET_DEVICE(call),
    413406                    &addrlen, &prefix, &content, &suffix);
    414407                if (rc != EOK)
    415408                        return rc;
    416                 IPC_SET_ADDR(*answer, addrlen);
    417                 IPC_SET_PREFIX(*answer, prefix);
    418                 IPC_SET_CONTENT(*answer, content);
    419                 IPC_SET_SUFFIX(*answer, suffix);
     409                IPC_SET_ADDR(answer, addrlen);
     410                IPC_SET_PREFIX(answer, prefix);
     411                IPC_SET_CONTENT(answer, content);
     412                IPC_SET_SUFFIX(answer, suffix);
    420413                *answer_count = 4;
    421414                return EOK;
    422415       
    423416        case NET_NIL_ADDR:
    424                 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address);
     417                rc = nildummy_addr_message(IPC_GET_DEVICE(call), &address);
    425418                if (rc != EOK)
    426419                        return rc;
     
    428421       
    429422        case NET_NIL_BROADCAST_ADDR:
    430                 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address);
     423                rc = nildummy_addr_message(IPC_GET_DEVICE(call), &address);
    431424                if (rc != EOK)
    432425                        return rc;
     
    434427       
    435428        case IPC_M_CONNECT_TO_ME:
    436                 return nildummy_register_message(NIL_GET_PROTO(*call),
    437                     IPC_GET_PHONE(*call));
     429                return nildummy_register_message(NIL_GET_PROTO(call),
     430                    IPC_GET_PHONE(call));
    438431        }
    439432       
     
    441434}
    442435
     436/** Default thread for new connections.
     437 *
     438 * @param[in] iid       The initial message identifier.
     439 * @param[in] icall     The initial message call structure.
     440 */
     441static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall)
     442{
     443        /*
     444         * Accept the connection
     445         *  - Answer the first IPC_M_CONNECT_ME_TO call.
     446         */
     447        ipc_answer_0(iid, EOK);
     448       
     449        while (true) {
     450                ipc_call_t answer;
     451                int answer_count;
     452               
     453                /* Clear the answer structure */
     454                refresh_answer(&answer, &answer_count);
     455               
     456                /* Fetch the next message */
     457                ipc_call_t call;
     458                ipc_callid_t callid = async_get_call(&call);
     459               
     460                /* Process the message */
     461                int res = nil_module_message_standalone(NAME, callid, &call,
     462                    &answer, &answer_count);
     463               
     464                /*
     465                 * End if told to either by the message or the processing
     466                 * result.
     467                 */
     468                if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||
     469                    (res == EHANGUP))
     470                        return;
     471               
     472                /* Answer the message */
     473                answer_call(callid, res, &answer, answer_count);
     474        }
     475}
     476
    443477int main(int argc, char *argv[])
    444478{
     479        int rc;
     480       
    445481        /* Start the module */
    446         return nil_module_start(SERVICE_NILDUMMY);
     482        rc = nil_module_start_standalone(nil_client_connection);
     483        return rc;
    447484}
    448485
Note: See TracChangeset for help on using the changeset viewer.