7 // =============================================================================
11 // helper function to analyse bitrate speed specifications
12 static int bitrate_analyse (int nb, const char **com, int *ports) {
18 s=parseBitrate(com[i+1]);
19 if ( strcmp(com[i], "inout")==0 ) {
22 } else if ( strcmp(com[i], "in")==0 ) {
24 } else if ( strcmp(com[i], "out")==0 ) {
38 static bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga) {
40 int i, k=0, defs[]={12, 12}, p, *ports=NULL;
41 const struct swi_attr *sa;
46 printf("Usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n");
47 printf("SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n");
52 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
53 printf("must be logged\n");
58 ports=malloc(2*sa->ports*sizeof(int));
60 // get defaults if present
61 if ( strcmp(com[k], "all")==0 ) {
63 k+=bitrate_analyse(nb-k, &com[k], defs);
67 for (i=0; i<sa->ports; ++i) {
68 memcpy(&ports[2*i], defs, sizeof(defs));
71 // get ports specifics
73 p=strtol(com[k++], NULL, 0)-1;
74 if ( p>=0 && p<sa->ports ) {
75 k+=bitrate_analyse(nb-k, &com[k], &ports[2*p]);
79 // send it to the switch
80 i=ngadmin_setBitrateLimits(nga, ports);
93 static bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
95 int i, ret=true, *ports=NULL;
96 const struct swi_attr *sa;
99 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
100 printf("must be logged\n");
106 ports=malloc(2*sa->ports*sizeof(int));
107 if ( (i=ngadmin_getBitrateLimits(nga, ports))!=ERR_OK ) {
113 for (i=0; i<sa->ports; ++i) {
114 printf("port %i: in %s, out %s\n", i+1, bitrates[ports[2*i+0]], bitrates[ports[2*i+1]]);
126 // =============================================================================
130 static bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
132 const struct swi_attr *sa;
136 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
137 printf("must be logged\n");
153 static bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) {
155 const struct swi_attr *sa;
160 printf("Usage: firmware upgrade <file>\n");
164 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
165 printf("must be logged\n");
170 printf("not implemented yet\n");
181 // =============================================================================
185 static bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
188 const struct swi_attr *sa;
191 sa=ngadmin_getSwitchTab(nga, &n);
192 displaySwitchTab(sa, n);
201 // =============================================================================
205 static bool do_login (int nb, const char **com, struct ngadmin *nga) {
211 printf("Usage: login <num>\n");
216 i=strtol(com[0], NULL, 0);
217 i=ngadmin_login(nga, i);
227 // =============================================================================
231 static bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
233 const struct swi_attr *sa;
236 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
237 printf("must be logged\n");
250 static bool do_name_set (int nb, const char **com, struct ngadmin *nga) {
253 const struct swi_attr *sa;
257 printf("Usage: name set <value>\n");
261 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
262 printf("must be logged\n");
266 i=ngadmin_setName(nga, com[0]);
276 static bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
279 const struct swi_attr *sa;
282 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
283 printf("must be logged\n");
287 i=ngadmin_setName(nga, NULL);
297 // =============================================================================
301 static bool do_password_change (int nb, const char **com, struct ngadmin *nga) {
304 const struct swi_attr *sa;
308 printf("Usage: password set <value>\n");
312 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
313 printf("must be logged\n");
317 i=ngadmin_changePassword(nga, com[0]);
327 static bool do_password_set (int nb, const char **com, struct ngadmin *nga) {
333 printf("Usage: password set <value>\n");
337 i=ngadmin_setPassword(nga, com[0]);
347 // =============================================================================
351 static bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
354 const struct swi_attr *sa;
355 unsigned char *ports=NULL;
359 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
360 printf("must be logged\n");
366 ports=malloc(sa->ports*sizeof(unsigned char));
367 if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
373 for (i=0; i<sa->ports; i++) {
374 printf("port %i: ", i+1);
375 switch ( ports[i] ) {
376 case 0: printf("down"); break;
377 case SPEED_10: printf("up, 10M"); break;
378 case SPEED_100: printf("up, 100M"); break;
379 case SPEED_1000: printf("up, 1000M"); break;
380 default: printf("unknown (%i)", ports[i]);
395 static bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
400 if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
401 printf("must be logged\n");
405 i=ngadmin_resetPortsStatistics(nga);
415 static bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
418 const struct swi_attr *sa;
420 struct port_stats *ps=NULL;
423 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
424 printf("must be logged\n");
429 ps=calloc(sa->ports, sizeof(struct port_stats));
430 if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
436 printf("Port\tReceived\tSent\tCRC errors\n");
437 for (i=0; i<sa->ports; ++i) {
438 printf("% 4i%12llu%12llu%14llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc);
451 // =============================================================================
455 static bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
465 // =============================================================================
469 static bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
472 const struct swi_attr *sa;
475 if ( (i=ngadmin_scan(nga))<0 ) {
480 sa=ngadmin_getSwitchTab(nga, &nb);
481 displaySwitchTab(sa, nb);
490 // =============================================================================
494 static bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
497 const struct swi_attr *sa;
500 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
501 printf("must be logged\n");
505 i=ngadmin_setStormFilterState(nga, 1);
515 static bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
518 const struct swi_attr *sa;
521 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
522 printf("must be logged\n");
526 i=ngadmin_setStormFilterState(nga, 0);
536 static bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) {
538 int i, d=BITRATE_UNSPEC, p, *ports=NULL;
539 const struct swi_attr *sa;
544 printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
549 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
550 printf("must be logged\n");
556 ports=malloc(sa->ports*sizeof(int));
558 if ( strcmp(com[0], "all")==0 ) {
559 d=parseBitrate(com[1]);
564 for (i=0; i<sa->ports; ++i) {
568 for (i=0; i<nb; i+=2) {
569 if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
570 ports[p-1]=parseBitrate(com[i+1]);
574 i=ngadmin_setStormFilterValues(nga, ports);
587 static bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
589 int i, s, ret=true, *ports=NULL;
590 const struct swi_attr *sa;
593 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
594 printf("must be logged\n");
599 if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
607 printf("storm filter is disabled\n");
611 printf("storm filter is enabled\n");
613 ports=malloc(sa->ports*sizeof(int));
614 if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
620 for (i=0; i<sa->ports; ++i) {
621 printf("port %i: %s\n", i+1, bitrates[ports[i]]);
633 // =============================================================================
637 static void display_node (const struct TreeNode *tn, int depth) {
640 const struct TreeNode *s;
643 for (i=0; i<depth; ++i) {
649 if ( tn->sub==NULL ) return;
651 for (s=tn->sub; s->name!=NULL; ++s) {
652 display_node(s, depth+1);
659 static bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
662 display_node(&coms, 0);
671 // =============================================================================
677 COM_TERM(set, do_bitrate_set, true)
678 COM_TERM(show, do_bitrate_show, false)
681 COM_TERM(cabletest, NULL, true)
683 COM_TERM(defaults, NULL, false)
686 COM_TERM(show, do_firmware_show, false)
687 COM_TERM(upgrade, do_firmware_upgrade, true)
690 COM_TERM(list, do_list, false)
692 COM_TERM(login, do_login, true)
695 COM_TERM(enable, NULL, false)
696 COM_TERM(disable, NULL, false)
697 COM_TERM(set, NULL, true)
698 COM_TERM(show, NULL, false)
702 COM_TERM(show, do_name_show, false)
703 COM_TERM(set, do_name_set, true)
704 COM_TERM(clear, do_name_clear, false)
708 COM_TERM(show, NULL, false)
709 COM_TERM(set, NULL, true)
713 COM_TERM(change, do_password_change, true)
714 COM_TERM(set, do_password_set, true)
718 COM_TERM(state, do_ports_state, false)
719 COM_START(statistics)
720 COM_TERM(reset, do_ports_statistics_reset, false)
721 COM_TERM(show, do_ports_statistics_show, false)
726 COM_TERM(mode, NULL, true)
727 COM_TERM(set, NULL, true)
728 COM_TERM(show, NULL, false)
731 COM_TERM(quit, do_quit, false)
733 COM_TERM(restart, NULL, false)
735 COM_TERM(scan, do_scan, false)
737 COM_START(stormfilter)
738 COM_TERM(enable, do_stormfilter_enable, false)
739 COM_TERM(disable, do_stormfilter_disable, false)
740 COM_TERM(set, do_stormfilter_set, true)
741 COM_TERM(show, do_stormfilter_show, false)
744 COM_TERM(tree, do_tree, false)
747 COM_TERM(show, NULL, false)
748 COM_TERM(mode, NULL, true)