Changeset f2f4c00 in mainline for uspace/app/nic/nic.c
- Timestamp:
- 2014-08-03T15:54:44Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8e77b12f
- Parents:
- 251d4dd (diff), 1f1fa64 (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nic/nic.c
r251d4dd rf2f4c00 45 45 46 46 typedef struct { 47 nic_device_info_t device_info; 47 48 nic_address_t address; 48 49 nic_cable_state_t link_state; 50 nic_channel_mode_t duplex; 51 int speed; 49 52 } nic_info_t; 50 53 … … 52 55 { 53 56 printf("syntax:\n"); 54 printf("\t" NAME "\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|simplex> - set duplex mode\n"); 63 printf("\tauto - enable autonegotiation\n"); 64 } 65 66 static async_sess_t *get_nic_by_index(size_t i) 67 { 68 int rc; 69 size_t count; 70 char *svc_name; 71 category_id_t nic_cat; 72 service_id_t *nics = NULL; 73 async_sess_t *sess; 74 75 rc = loc_category_get_id("nic", &nic_cat, 0); 76 if (rc != EOK) { 77 printf("Error resolving category 'nic'.\n"); 78 goto error; 79 } 80 81 rc = loc_category_get_svcs(nic_cat, &nics, &count); 82 if (rc != EOK) { 83 printf("Error getting list of NICs.\n"); 84 goto error; 85 } 86 87 rc = loc_service_get_name(nics[i], &svc_name); 88 if (rc != EOK) { 89 printf("Error getting service name.\n"); 90 goto error; 91 } 92 93 printf("Using device: %s\n", svc_name); 94 95 sess = loc_service_connect(EXCHANGE_SERIALIZE, nics[i], 0); 96 if (sess == NULL) { 97 printf("Error connecting to service.\n"); 98 goto error; 99 } 100 101 return sess; 102 error: 103 return NULL; 55 104 } 56 105 … … 59 108 { 60 109 async_sess_t *sess; 110 nic_role_t role; 61 111 int rc; 62 112 63 113 sess = loc_service_connect(EXCHANGE_SERIALIZE, svc_id, 0); 64 114 if (sess == NULL) { 65 printf("Error connecting '%s'.\n", svc_name); 66 rc = EIO; 115 printf("Error connecting to service.\n"); 67 116 goto error; 68 117 } … … 75 124 } 76 125 126 rc = nic_get_device_info(sess, &info->device_info); 127 if (rc != EOK) { 128 printf("Error getting NIC device info.\n"); 129 rc = EIO; 130 goto error; 131 } 132 77 133 rc = nic_get_cable_state(sess, &info->link_state); 78 134 if (rc != EOK) { … … 81 137 goto error; 82 138 } 139 140 rc = nic_get_operation_mode(sess, &info->speed, &info->duplex, &role); 141 if (rc != EOK) { 142 printf("Error getting NIC speed and duplex mode.\n"); 143 rc = EIO; 144 goto error; 145 } 146 83 147 84 148 return EOK; … … 93 157 case NIC_CS_PLUGGED: return "up"; 94 158 case NIC_CS_UNPLUGGED: return "down"; 159 default: assert(false); return NULL; 160 } 161 } 162 163 static const char *nic_duplex_mode_str(nic_channel_mode_t mode) 164 { 165 switch (mode) { 166 case NIC_CM_FULL_DUPLEX: return "full-duplex"; 167 case NIC_CM_HALF_DUPLEX: return "half-duplex"; 168 case NIC_CM_SIMPLEX: return "simplex"; 95 169 default: assert(false); return NULL; 96 170 } … … 133 207 } 134 208 135 printf("[ Address] [Link State][Service Name]\n");209 printf("[Index]: [Service Name]\n"); 136 210 for (i = 0; i < count; i++) { 137 211 rc = loc_service_get_name(nics[i], &svc_name); … … 152 226 } 153 227 154 printf("%s %s %s\n", addr_str, 155 nic_link_state_str(nic_info.link_state), svc_name); 228 printf("%zu: %s\n", i, svc_name); 229 printf("\tMAC address: %s\n", addr_str); 230 printf("\tVendor name: %s\n", 231 nic_info.device_info.vendor_name); 232 printf("\tModel name: %s\n", 233 nic_info.device_info.model_name); 234 printf("\tLink state: %s\n", 235 nic_link_state_str(nic_info.link_state)); 236 237 if (nic_info.link_state == NIC_CS_PLUGGED) { 238 printf("\tSpeed: %dMbps %s\n", nic_info.speed, 239 nic_duplex_mode_str(nic_info.duplex)); 240 } 156 241 157 242 free(svc_name); … … 165 250 } 166 251 252 static int nic_set_speed(int i, char *str) 253 { 254 async_sess_t *sess; 255 uint32_t speed; 256 int oldspeed; 257 nic_channel_mode_t oldduplex; 258 nic_role_t oldrole; 259 int rc; 260 261 rc = str_uint32_t(str, NULL, 10, false, &speed); 262 if (rc != EOK) { 263 printf("Speed must be a numeric value.\n"); 264 return rc; 265 } 266 267 if (speed != 10 && speed != 100 && speed != 1000) { 268 printf("Speed must be one of: 10, 100, 1000.\n"); 269 return EINVAL; 270 } 271 272 sess = get_nic_by_index(i); 273 if (sess == NULL) { 274 printf("Specified NIC doesn't exist or cannot connect to it.\n"); 275 return EINVAL; 276 } 277 278 rc = nic_get_operation_mode(sess, &oldspeed, &oldduplex, &oldrole); 279 if (rc != EOK) { 280 printf("Error getting NIC speed and duplex mode.\n"); 281 return EIO; 282 } 283 284 return nic_set_operation_mode(sess, speed, oldduplex, oldrole); 285 } 286 287 static int nic_set_duplex(int i, char *str) 288 { 289 async_sess_t *sess; 290 int oldspeed; 291 nic_channel_mode_t duplex = NIC_CM_UNKNOWN; 292 nic_channel_mode_t oldduplex; 293 nic_role_t oldrole; 294 int rc; 295 296 if (!str_cmp(str, "half")) 297 duplex = NIC_CM_HALF_DUPLEX; 298 299 if (!str_cmp(str, "full")) 300 duplex = NIC_CM_FULL_DUPLEX; 301 302 if (!str_cmp(str, "simplex")) 303 duplex = NIC_CM_SIMPLEX; 304 305 if (duplex == NIC_CM_UNKNOWN) { 306 printf("Invalid duplex specification.\n"); 307 return EINVAL; 308 } 309 310 sess = get_nic_by_index(i); 311 if (sess == NULL) { 312 printf("Specified NIC doesn't exist or cannot connect to it.\n"); 313 return EINVAL; 314 } 315 316 rc = nic_get_operation_mode(sess, &oldspeed, &oldduplex, &oldrole); 317 if (rc != EOK) { 318 printf("Error getting NIC speed and duplex mode.\n"); 319 return EIO; 320 } 321 322 return nic_set_operation_mode(sess, oldspeed, duplex, oldrole); 323 } 324 325 static int nic_set_autoneg(int i) 326 { 327 async_sess_t *sess; 328 int rc; 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 rc = nic_autoneg_restart(sess); 337 if (rc != EOK) { 338 printf("Error restarting NIC autonegotiation.\n"); 339 return EIO; 340 } 341 342 return EOK; 343 } 344 345 static int nic_set_addr(int i, char *str) 346 { 347 async_sess_t *sess; 348 nic_address_t addr; 349 int rc, idx; 350 351 sess = get_nic_by_index(i); 352 if (sess == NULL) { 353 printf("Specified NIC doesn't exist or cannot connect to it.\n"); 354 return EINVAL; 355 } 356 357 if (str_size(str) != 17) { 358 printf("Invalid MAC address specified"); 359 return EINVAL; 360 } 361 362 for (idx = 0; idx < 6; idx++) { 363 rc = str_uint8_t(&str[idx * 3], NULL, 16, false, &addr.address[idx]); 364 if (rc != EOK) { 365 printf("Invalid MAC address specified"); 366 return EINVAL; 367 } 368 } 369 370 return nic_set_address(sess, &addr); 371 } 372 167 373 int main(int argc, char *argv[]) 168 374 { 169 375 int rc; 376 uint32_t index; 170 377 171 378 if (argc == 1) { … … 173 380 if (rc != EOK) 174 381 return 1; 382 } else if (argc >= 3) { 383 rc = str_uint32_t(argv[1], NULL, 10, false, &index); 384 if (rc != EOK) { 385 printf(NAME ": Invalid argument.\n"); 386 print_syntax(); 387 return 1; 388 } 389 390 if (!str_cmp(argv[2], "addr")) 391 return nic_set_addr(index, argv[3]); 392 393 if (!str_cmp(argv[2], "speed")) 394 return nic_set_speed(index, argv[3]); 395 396 if (!str_cmp(argv[2], "duplex")) 397 return nic_set_duplex(index, argv[3]); 398 399 if (!str_cmp(argv[2], "auto")) 400 return nic_set_autoneg(index); 401 175 402 } else { 176 403 printf(NAME ": Invalid argument.\n");
Note:
See TracChangeset
for help on using the changeset viewer.