Changes in uspace/app/nic/nic.c [278fede:6bcecc2] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nic/nic.c

    r278fede r6bcecc2  
    4545
    4646typedef struct {
    47         nic_device_info_t device_info;
    4847        nic_address_t address;
    4948        nic_cable_state_t link_state;
    50         nic_channel_mode_t duplex;
    51         int speed;
    5249} nic_info_t;
    5350
     
    5552{
    5653        printf("syntax:\n");
    57         printf("\t" NAME " [<index> <cmd> [<args...>]]\n");
    58         printf("\t<index> is NIC index number reported by the tool\n");
    59         printf("\t<cmd> is:\n");
    60         printf("\taddr <mac_address> - set MAC address\n");
    61         printf("\tspeed <10|100|1000> - set NIC speed\n");
    62         printf("\tduplex <half|full> - set duplex mode\n");
    63 }
    64 
    65 static async_sess_t *get_nic_by_index(size_t i)
    66 {
    67         int rc;
    68         size_t count;
    69         char *svc_name;
    70         category_id_t nic_cat;
    71         service_id_t *nics = NULL;
    72         async_sess_t *sess;
    73 
    74         rc = loc_category_get_id("nic", &nic_cat, 0);
    75         if (rc != EOK) {
    76                 printf("Error resolving category 'nic'.\n");
    77                 goto error;
    78         }
    79 
    80         rc = loc_category_get_svcs(nic_cat, &nics, &count);
    81         if (rc != EOK) {
    82                 printf("Error getting list of NICs.\n");
    83                 goto error;
    84         }
    85 
    86         rc = loc_service_get_name(nics[i], &svc_name);
    87         if (rc != EOK) {
    88                 printf("Error getting service name.\n");
    89                 goto error;
    90         }
    91 
    92         printf("Using device: %s\n", svc_name);
    93 
    94         sess = loc_service_connect(EXCHANGE_SERIALIZE, nics[i], 0);
    95         if (sess == NULL) {
    96                 printf("Error connecting to service.\n");
    97                 goto error;
    98         }
    99 
    100         return sess;
    101 error:
    102         return NULL;
     54        printf("\t" NAME "\n");
    10355}
    10456
     
    10759{
    10860        async_sess_t *sess;
    109         nic_role_t role;
    11061        int rc;
    11162
    11263        sess = loc_service_connect(EXCHANGE_SERIALIZE, svc_id, 0);
    11364        if (sess == NULL) {
    114                 printf("Error connecting to service.\n");
     65                printf("Error connecting '%s'.\n", svc_name);
     66                rc = EIO;
    11567                goto error;
    11668        }
     
    12375        }
    12476
    125         rc = nic_get_device_info(sess, &info->device_info);
    126         if (rc != EOK) {
    127                 printf("Error getting NIC device info.\n");
    128                 rc = EIO;
    129                 goto error;
    130         }
    131 
    13277        rc = nic_get_cable_state(sess, &info->link_state);
    13378        if (rc != EOK) {
     
    13681                goto error;
    13782        }
    138 
    139         rc = nic_get_operation_mode(sess, &info->speed, &info->duplex, &role);
    140         if (rc != EOK) {
    141                 printf("Error getting NIC speed and duplex mode.\n");
    142                 rc = EIO;
    143                 goto error;
    144         }
    145 
    14683
    14784        return EOK;
     
    15693        case NIC_CS_PLUGGED: return "up";
    15794        case NIC_CS_UNPLUGGED: return "down";
    158         default: assert(false); return NULL;
    159         }
    160 }
    161 
    162 static const char *nic_duplex_mode_str(nic_channel_mode_t mode)
    163 {
    164         switch (mode) {
    165         case NIC_CM_FULL_DUPLEX: return "full-duplex";
    166         case NIC_CM_HALF_DUPLEX: return "half-duplex";
    167         case NIC_CM_SIMPLEX: return "simplex";
    16895        default: assert(false); return NULL;
    16996        }
     
    206133        }
    207134
    208         printf("[Service Name]\n");
     135        printf("[Address] [Link State] [Service Name]\n");
    209136        for (i = 0; i < count; i++) {
    210137                rc = loc_service_get_name(nics[i], &svc_name);
     
    225152                }
    226153
    227                 printf("%d: %s\n", i, svc_name);
    228                 printf("\tMAC address: %s\n", addr_str);
    229                 printf("\tVendor name: %s\n",
    230                     nic_info.device_info.vendor_name);
    231                 printf("\tModel name: %s\n",
    232                     nic_info.device_info.model_name);
    233                 printf("\tLink state: %s\n",
    234                     nic_link_state_str(nic_info.link_state));
    235 
    236                 if (nic_info.link_state == NIC_CS_PLUGGED) {
    237                         printf("\tSpeed: %dMbps %s\n", nic_info.speed,
    238                             nic_duplex_mode_str(nic_info.duplex));
    239                 }
     154                printf("%s %s %s\n", addr_str,
     155                    nic_link_state_str(nic_info.link_state), svc_name);
    240156
    241157                free(svc_name);
     
    249165}
    250166
    251 static int nic_set_speed(int i, char *str)
    252 {
    253         async_sess_t *sess;
    254         uint32_t speed;
    255         int oldspeed;
    256         nic_channel_mode_t oldduplex;
    257         nic_role_t oldrole;
    258         int rc;
    259 
    260         rc = str_uint32_t(str, NULL, 10, false, &speed);
    261         if (rc != EOK) {
    262                 printf("Speed must be a numeric value.\n");
    263                 return rc;
    264         }
    265 
    266         if (speed != 10 && speed != 100 && speed != 1000) {
    267                 printf("Speed must be one of: 10, 100, 1000.\n");
    268                 return EINVAL;
    269         }
    270 
    271         sess = get_nic_by_index(i);
    272         if (sess == NULL) {
    273                 printf("Specified NIC doesn't exist or cannot connect to it.\n");
    274                 return EINVAL;
    275         }
    276 
    277         rc = nic_get_operation_mode(sess, &oldspeed, &oldduplex, &oldrole);
    278         if (rc != EOK) {
    279                 printf("Error getting NIC speed and duplex mode.\n");
    280                 return EIO;
    281         }
    282 
    283         return nic_set_operation_mode(sess, speed, oldduplex, oldrole);
    284 }
    285 
    286 static int nic_set_duplex(int i, char *str)
    287 {
    288         async_sess_t *sess;
    289         int oldspeed;
    290         nic_channel_mode_t duplex = NIC_CM_UNKNOWN;
    291         nic_channel_mode_t oldduplex;
    292         nic_role_t oldrole;
    293         int rc;
    294 
    295         if (!str_cmp(str, "half"))
    296                 duplex = NIC_CM_HALF_DUPLEX;
    297 
    298         if (!str_cmp(str, "full"))
    299                 duplex = NIC_CM_FULL_DUPLEX;
    300 
    301         if (!str_cmp(str, "simplex"))
    302                 duplex = NIC_CM_SIMPLEX;
    303 
    304         if (duplex == NIC_CM_UNKNOWN) {
    305                 printf("Invalid duplex specification.\n");
    306                 return EINVAL;
    307         }
    308 
    309         sess = get_nic_by_index(i);
    310         if (sess == NULL) {
    311                 printf("Specified NIC doesn't exist or cannot connect to it.\n");
    312                 return EINVAL;
    313         }
    314 
    315         rc = nic_get_operation_mode(sess, &oldspeed, &oldduplex, &oldrole);
    316         if (rc != EOK) {
    317                 printf("Error getting NIC speed and duplex mode.\n");
    318                 return EIO;
    319         }
    320 
    321         return nic_set_operation_mode(sess, oldspeed, duplex, oldrole);
    322 }
    323 
    324 static int nic_set_addr(int i, char *str)
    325 {
    326         async_sess_t *sess;
    327         nic_address_t addr;
    328         int rc, idx;
    329 
    330         sess = get_nic_by_index(i);
    331         if (sess == NULL) {
    332                 printf("Specified NIC doesn't exist or cannot connect to it.\n");
    333                 return EINVAL;
    334         }
    335 
    336         if (str_size(str) != 17) {
    337                 printf("Invalid MAC address specified");
    338                 return EINVAL;
    339         }
    340 
    341         for (idx = 0; idx < 6; idx++) {
    342                 rc = str_uint8_t(&str[idx * 3], NULL, 16, false, &addr.address[idx]);
    343                 if (rc != EOK) {
    344                         printf("Invalid MAC address specified");
    345                         return EINVAL;
    346                 }
    347         }
    348 
    349         return nic_set_address(sess, &addr);
    350 }
    351 
    352167int main(int argc, char *argv[])
    353168{
    354169        int rc;
    355         uint32_t index;
    356170
    357171        if (argc == 1) {
     
    359173                if (rc != EOK)
    360174                        return 1;
    361         } else if (argc >= 3) {
    362                 rc = str_uint32_t(argv[1], NULL, 10, false, &index);
    363                 if (rc != EOK) {
    364                         printf(NAME ": Invalid argument.\n");
    365                         print_syntax();
    366                         return 1;
    367                 }
    368 
    369                 if (!str_cmp(argv[2], "addr"))
    370                         return nic_set_addr(index, argv[3]);
    371 
    372                 if (!str_cmp(argv[2], "speed"))
    373                         return nic_set_speed(index, argv[3]);
    374 
    375                 if (!str_cmp(argv[2], "duplex"))
    376                         return nic_set_duplex(index, argv[3]);
    377 
    378175        } else {
    379176                printf(NAME ": Invalid argument.\n");
Note: See TracChangeset for help on using the changeset viewer.