Changes in uspace/app/nic/nic.c [278fede:6bcecc2] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nic/nic.c
r278fede r6bcecc2 45 45 46 46 typedef struct { 47 nic_device_info_t device_info;48 47 nic_address_t address; 49 48 nic_cable_state_t link_state; 50 nic_channel_mode_t duplex;51 int speed;52 49 } nic_info_t; 53 50 … … 55 52 { 56 53 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"); 103 55 } 104 56 … … 107 59 { 108 60 async_sess_t *sess; 109 nic_role_t role;110 61 int rc; 111 62 112 63 sess = loc_service_connect(EXCHANGE_SERIALIZE, svc_id, 0); 113 64 if (sess == NULL) { 114 printf("Error connecting to service.\n"); 65 printf("Error connecting '%s'.\n", svc_name); 66 rc = EIO; 115 67 goto error; 116 68 } … … 123 75 } 124 76 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 132 77 rc = nic_get_cable_state(sess, &info->link_state); 133 78 if (rc != EOK) { … … 136 81 goto error; 137 82 } 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 146 83 147 84 return EOK; … … 156 93 case NIC_CS_PLUGGED: return "up"; 157 94 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";168 95 default: assert(false); return NULL; 169 96 } … … 206 133 } 207 134 208 printf("[ Service Name]\n");135 printf("[Address] [Link State] [Service Name]\n"); 209 136 for (i = 0; i < count; i++) { 210 137 rc = loc_service_get_name(nics[i], &svc_name); … … 225 152 } 226 153 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); 240 156 241 157 free(svc_name); … … 249 165 } 250 166 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 352 167 int main(int argc, char *argv[]) 353 168 { 354 169 int rc; 355 uint32_t index;356 170 357 171 if (argc == 1) { … … 359 173 if (rc != EOK) 360 174 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 378 175 } else { 379 176 printf(NAME ": Invalid argument.\n");
Note:
See TracChangeset
for help on using the changeset viewer.