Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/netif/lo/lo.c

    rfe8dfa6 r774e6d1a  
    4848#include <packet_client.h>
    4949#include <net/device.h>
     50#include <nil_interface.h>
    5051#include <netif_skel.h>
    51 #include <nil_remote.h>
     52
     53/** Default hardware address. */
     54#define DEFAULT_ADDR  0
    5255
    5356/** Default address length. */
     
    5760#define NAME  "lo"
    5861
    59 static uint8_t default_addr[DEFAULT_ADDR_LEN] =
    60     {0, 0, 0, 0, 0, 0};
     62/** Network interface global data. */
     63netif_globals_t netif_globals;
    6164
    6265int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
     
    7174                return EBADMEM;
    7275       
    73         address->value = default_addr;
     76        uint8_t *addr = (uint8_t *) malloc(DEFAULT_ADDR_LEN);
     77        memset(addr, DEFAULT_ADDR, DEFAULT_ADDR_LEN);
     78       
     79        address->value = addr;
    7480        address->length = DEFAULT_ADDR_LEN;
    7581       
     
    7985int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
    8086{
     87        netif_device_t *device;
     88        int rc;
     89
    8190        if (!stats)
    8291                return EBADMEM;
    83        
    84         netif_device_t *device;
    85         int rc = find_device(device_id, &device);
     92
     93        rc = find_device(device_id, &device);
    8694        if (rc != EOK)
    8795                return rc;
    88        
     96
    8997        memcpy(stats, (device_stats_t *) device->specific,
    9098            sizeof(device_stats_t));
    91        
    92         return EOK;
    93 }
    94 
    95 /** Change the loopback state.
    96  *
    97  * @param[in] device The device structure.
    98  * @param[in] state  The new device state.
    99  *
    100  * @return New state if changed.
    101  * @return EOK otherwise.
    102  *
    103  */
    104 static void change_state_message(netif_device_t *device, device_state_t state)
     99
     100        return EOK;
     101}
     102
     103/** Changes the loopback state.
     104 *
     105 * @param[in] device    The device structure.
     106 * @param[in] state     The new device state.
     107 * @return              The new state if changed.
     108 * @return              EOK otherwise.
     109 */
     110static int change_state_message(netif_device_t *device, device_state_t state)
    105111{
    106112        if (device->state != state) {
    107113                device->state = state;
    108114               
    109                 const char *desc;
    110                 switch (state) {
    111                 case NETIF_ACTIVE:
    112                         desc = "active";
    113                         break;
    114                 case NETIF_STOPPED:
    115                         desc = "stopped";
    116                         break;
    117                 default:
    118                         desc = "unknown";
    119                 }
     115                printf("%s: State changed to %s\n", NAME,
     116                    (state == NETIF_ACTIVE) ? "active" : "stopped");
    120117               
    121                 printf("%s: State changed to %s\n", NAME, desc);
    122         }
    123 }
    124 
    125 /** Create and return the loopback network interface structure.
    126  *
    127  * @param[in]  device_id New devce identifier.
    128  * @param[out] device    Device structure.
    129  *
    130  * @return EOK on success.
    131  * @return EXDEV if one loopback network interface already exists.
    132  * @return ENOMEM if there is not enough memory left.
    133  *
    134  */
    135 static int lo_create(device_id_t device_id, netif_device_t **device)
    136 {
     118                return state;
     119        }
     120       
     121        return EOK;
     122}
     123
     124/** Creates and returns the loopback network interface structure.
     125 *
     126 * @param[in] device_id The new devce identifier.
     127 * @param[out] device   The device structure.
     128 * @return              EOK on success.
     129 * @return              EXDEV if one loopback network interface already exists.
     130 * @return              ENOMEM if there is not enough memory left.
     131 */
     132static int create(device_id_t device_id, netif_device_t **device)
     133{
     134        int index;
     135
    137136        if (netif_device_map_count(&netif_globals.device_map) > 0)
    138137                return EXDEV;
    139        
     138
    140139        *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    141140        if (!*device)
    142141                return ENOMEM;
    143        
     142
    144143        (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
    145144        if (!(*device)->specific) {
     
    147146                return ENOMEM;
    148147        }
    149        
     148
    150149        null_device_stats((device_stats_t *) (*device)->specific);
    151150        (*device)->device_id = device_id;
    152151        (*device)->nil_phone = -1;
    153152        (*device)->state = NETIF_STOPPED;
    154         int index = netif_device_map_add(&netif_globals.device_map,
     153        index = netif_device_map_add(&netif_globals.device_map,
    155154            (*device)->device_id, *device);
    156        
     155
    157156        if (index < 0) {
    158157                free(*device);
     
    168167{
    169168        sysarg_t phonehash;
     169
    170170        return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, &phonehash);
    171171}
     
    173173int netif_probe_message(device_id_t device_id, int irq, void *io)
    174174{
     175        netif_device_t *device;
     176        int rc;
     177
    175178        /* Create a new device */
    176         netif_device_t *device;
    177         int rc = lo_create(device_id, &device);
     179        rc = create(device_id, &device);
    178180        if (rc != EOK)
    179181                return rc;
    180        
     182
     183        /* Print the settings */
    181184        printf("%s: Device created (id: %d)\n", NAME, device->device_id);
     185
    182186        return EOK;
    183187}
     
    186190{
    187191        netif_device_t *device;
    188         int rc = find_device(device_id, &device);
     192        size_t length;
     193        packet_t *next;
     194        int phone;
     195        int rc;
     196
     197        rc = find_device(device_id, &device);
    189198        if (rc != EOK)
    190199                return EOK;
    191        
     200
    192201        if (device->state != NETIF_ACTIVE) {
    193202                netif_pq_release(packet_get_id(packet));
    194203                return EFORWARD;
    195204        }
    196        
    197         packet_t *next = packet;
     205
     206        next = packet;
    198207        do {
    199208                ((device_stats_t *) device->specific)->send_packets++;
    200209                ((device_stats_t *) device->specific)->receive_packets++;
    201                 size_t length = packet_get_data_length(next);
     210                length = packet_get_data_length(next);
    202211                ((device_stats_t *) device->specific)->send_bytes += length;
    203212                ((device_stats_t *) device->specific)->receive_bytes += length;
    204213                next = pq_next(next);
    205         } while (next);
    206        
    207         int phone = device->nil_phone;
     214        } while(next);
     215
     216        phone = device->nil_phone;
    208217        fibril_rwlock_write_unlock(&netif_globals.lock);
    209        
    210218        nil_received_msg(phone, device_id, packet, sender);
    211        
    212219        fibril_rwlock_write_lock(&netif_globals.lock);
     220       
    213221        return EOK;
    214222}
     
    216224int netif_start_message(netif_device_t *device)
    217225{
    218         change_state_message(device, NETIF_ACTIVE);
    219         return device->state;
     226        return change_state_message(device, NETIF_ACTIVE);
    220227}
    221228
    222229int netif_stop_message(netif_device_t *device)
    223230{
    224         change_state_message(device, NETIF_STOPPED);
    225         return device->state;
     231        return change_state_message(device, NETIF_STOPPED);
    226232}
    227233
Note: See TracChangeset for help on using the changeset viewer.