Changeset 0a9ea4a in mainline for uspace/srv/net/netif/lo/lo.c


Ignore:
Timestamp:
2011-01-14T14:28:57Z (13 years ago)
Author:
Lubos Slovak <lubos.slovak@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1d7a74e, 6336b6e
Parents:
45019865 (diff), 2f60e57d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge development into lelian/hidd

File:
1 edited

Legend:

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

    r45019865 r0a9ea4a  
    4848#include <packet_client.h>
    4949#include <net/device.h>
    50 #include <nil_interface.h>
    5150#include <netif_skel.h>
    52 
    53 /** Default hardware address. */
    54 #define DEFAULT_ADDR  0
     51#include <nil_remote.h>
    5552
    5653/** Default address length. */
     
    6057#define NAME  "lo"
    6158
    62 /** Network interface global data. */
    63 netif_globals_t netif_globals;
     59static uint8_t default_addr[DEFAULT_ADDR_LEN] =
     60    {0, 0, 0, 0, 0, 0};
    6461
    6562int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
     
    7471                return EBADMEM;
    7572       
    76         uint8_t *addr = (uint8_t *) malloc(DEFAULT_ADDR_LEN);
    77         memset(addr, DEFAULT_ADDR, DEFAULT_ADDR_LEN);
    78        
    79         address->value = addr;
     73        address->value = default_addr;
    8074        address->length = DEFAULT_ADDR_LEN;
    8175       
     
    8579int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
    8680{
    87         netif_device_t *device;
    88         int rc;
    89 
    9081        if (!stats)
    9182                return EBADMEM;
    92 
    93         rc = find_device(device_id, &device);
     83       
     84        netif_device_t *device;
     85        int rc = find_device(device_id, &device);
    9486        if (rc != EOK)
    9587                return rc;
    96 
     88       
    9789        memcpy(stats, (device_stats_t *) device->specific,
    9890            sizeof(device_stats_t));
    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  */
    110 static int change_state_message(netif_device_t *device, device_state_t state)
     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 */
     104static void change_state_message(netif_device_t *device, device_state_t state)
    111105{
    112106        if (device->state != state) {
    113107                device->state = state;
    114108               
    115                 printf("%s: State changed to %s\n", NAME,
    116                     (state == NETIF_ACTIVE) ? "active" : "stopped");
     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                }
    117120               
    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  */
    132 static int create(device_id_t device_id, netif_device_t **device)
    133 {
    134         int index;
    135 
     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 */
     135static int lo_create(device_id_t device_id, netif_device_t **device)
     136{
    136137        if (netif_device_map_count(&netif_globals.device_map) > 0)
    137138                return EXDEV;
    138 
     139       
    139140        *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    140141        if (!*device)
    141142                return ENOMEM;
    142 
     143       
    143144        (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
    144145        if (!(*device)->specific) {
     
    146147                return ENOMEM;
    147148        }
    148 
     149       
    149150        null_device_stats((device_stats_t *) (*device)->specific);
    150151        (*device)->device_id = device_id;
    151152        (*device)->nil_phone = -1;
    152153        (*device)->state = NETIF_STOPPED;
    153         index = netif_device_map_add(&netif_globals.device_map,
     154        int index = netif_device_map_add(&netif_globals.device_map,
    154155            (*device)->device_id, *device);
    155 
     156       
    156157        if (index < 0) {
    157158                free(*device);
     
    167168{
    168169        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        /* Create a new device */
    175176        netif_device_t *device;
    176         int rc;
    177 
    178         /* Create a new device */
    179         rc = create(device_id, &device);
     177        int rc = lo_create(device_id, &device);
    180178        if (rc != EOK)
    181179                return rc;
    182 
    183         /* Print the settings */
     180       
    184181        printf("%s: Device created (id: %d)\n", NAME, device->device_id);
    185 
    186182        return EOK;
    187183}
     
    190186{
    191187        netif_device_t *device;
    192         size_t length;
    193         packet_t *next;
    194         int phone;
    195         int rc;
    196 
    197         rc = find_device(device_id, &device);
     188        int rc = find_device(device_id, &device);
    198189        if (rc != EOK)
    199190                return EOK;
    200 
     191       
    201192        if (device->state != NETIF_ACTIVE) {
    202193                netif_pq_release(packet_get_id(packet));
    203194                return EFORWARD;
    204195        }
    205 
    206         next = packet;
     196       
     197        packet_t *next = packet;
    207198        do {
    208199                ((device_stats_t *) device->specific)->send_packets++;
    209200                ((device_stats_t *) device->specific)->receive_packets++;
    210                 length = packet_get_data_length(next);
     201                size_t length = packet_get_data_length(next);
    211202                ((device_stats_t *) device->specific)->send_bytes += length;
    212203                ((device_stats_t *) device->specific)->receive_bytes += length;
    213204                next = pq_next(next);
    214         } while(next);
    215 
    216         phone = device->nil_phone;
     205        } while (next);
     206       
     207        int phone = device->nil_phone;
    217208        fibril_rwlock_write_unlock(&netif_globals.lock);
     209       
    218210        nil_received_msg(phone, device_id, packet, sender);
     211       
    219212        fibril_rwlock_write_lock(&netif_globals.lock);
    220        
    221213        return EOK;
    222214}
     
    224216int netif_start_message(netif_device_t *device)
    225217{
    226         return change_state_message(device, NETIF_ACTIVE);
     218        change_state_message(device, NETIF_ACTIVE);
     219        return device->state;
    227220}
    228221
    229222int netif_stop_message(netif_device_t *device)
    230223{
    231         return change_state_message(device, NETIF_STOPPED);
     224        change_state_message(device, NETIF_STOPPED);
     225        return device->state;
    232226}
    233227
Note: See TracChangeset for help on using the changeset viewer.