-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli_commands.c
1040 lines (867 loc) · 31.6 KB
/
cli_commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cli_commands.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//#include <readline/readline.h>
//#include <readline/history.h>
#include <stack.h>
#include <cli.h>
#include <signal.h>
#include <sys/wait.h>
#include <ZWayLib.h>
#include <ZData.h>
#include <ZPlatform.h>
#include "variant_types.h"
#include "resolver.h"
#include "command_class.h"
//#include "scene_manager.h"
#include "cli_resolver.h"
#include "cli_scene.h"
#include "cli_sensor.h"
#include "cli_service.h"
#include "cli_logger.h"
#include "cli_auth.h"
#include <setjmp.h>
#include "config.h"
#include "command_parser.h"
#include "vty_io.h"
#include "cli_vdev.h"
#include "cli_rest.h"
#include "cli_homebridge.h"
#include "hash.h"
#include "user_manager.h"
#include "socket_io.h"
#include <ctype.h>
DECLARE_LOGGER(CLI)
extern sigjmp_buf jmpbuf;
extern int keep_running;
static char banner_stop_char;
static char* banner;
static int history_size;
// Forward declaration of commands
bool cmd_controller_reset(vty_t* vty, variant_stack_t* params);
bool cmd_controller_inclusion_mode(vty_t* vty, variant_stack_t* params);
bool cmd_controller_exclusion_mode(vty_t* vty, variant_stack_t* params);
bool cmd_controller_reset(vty_t* vty, variant_stack_t* params);
bool cmd_controller_factory_reset(vty_t* vty, variant_stack_t* params);
bool cmd_controller_remove_failed_node(vty_t* vty, variant_stack_t* params);
bool cmd_controller_ping(vty_t* vty, variant_stack_t* params);
bool cmd_show_controller_queue(vty_t* vty, variant_stack_t* params);
bool cmd_list_command_classes(vty_t* vty, variant_stack_t* params);
bool cmd_controller_set_learn_mode(vty_t* vty, variant_stack_t* params);
bool cmd_controller_config_save(vty_t* vty, variant_stack_t* params);
bool cmd_controller_config_restore(vty_t* vty, variant_stack_t* params);
bool cmd_controller_set_suc_node(vty_t* vty, variant_stack_t* params);
bool cmd_help(vty_t* vty, variant_stack_t* params);
bool cmd_set_banner(vty_t* vty, variant_stack_t* params);
bool cmd_show_banner(vty_t* vty, variant_stack_t* params);
bool cmd_clear_banner(vty_t* vty, variant_stack_t* params);
bool cmd_enter_node_by_name(vty_t* vty, const char* node_name);
bool cmd_enter_node(vty_t* vty, variant_stack_t* params);
bool cmd_exit_node(vty_t* vty, variant_stack_t* params);
bool cmd_close_session(vty_t* vty, variant_stack_t* params);
bool cmd_show_running_config(vty_t* vty, variant_stack_t* params);
bool cmd_save_running_config(vty_t* vty, variant_stack_t* params);
bool cmd_copy_running_config(vty_t* vty, variant_stack_t* params);
bool cmd_show_history(vty_t* vty, variant_stack_t* params);
bool cmd_clear_history(vty_t* vty, variant_stack_t* params);
bool cmd_quit(vty_t* vty, variant_stack_t* params);
bool cmd_eval_expression(vty_t* vty, variant_stack_t* params);
bool cmd_set_history(vty_t* vty, variant_stack_t* params);
bool cmd_pager(vty_t* vty, variant_stack_t* params);
bool cmd_line_filter(vty_t* vty, variant_stack_t* params);
void show_command_class_helper(command_class_t* command_class, void* arg);
bool cmd_shutdown(vty_t* vty, variant_stack_t* params);
bool cmd_logout(vty_t* vty, variant_stack_t* params);
cli_command_t root_command_list[] = {
{"controller inclusion start", cmd_controller_inclusion_mode, "Start inclusion mode"},
{"controller inclusion stop", cmd_controller_inclusion_mode, "Stop inclusion mode"},
{"controller exclusion start", cmd_controller_exclusion_mode, "Start exclusion mode"},
{"controller exclusion stop", cmd_controller_exclusion_mode, "Stop exclusion mode"},
{"controller reset", cmd_controller_reset, "Reset zwave controller"},
{"controller factory-reset", cmd_controller_factory_reset, "Reset zwave controller to factory defaults"},
{"controller remove-failed node-id INT", cmd_controller_remove_failed_node, "Remove failed node from the controller"},
{"controller ping node-id INT", cmd_controller_ping, "Ping node and mark as failed if unreachable"},
{"controller learn-mode start|stop|start-nwi", cmd_controller_set_learn_mode, "Start learn mode"},
{"controller save-config", cmd_controller_config_save, "Save controller configuration"},
{"controller restore-config", cmd_controller_config_restore, "Restore controller configuration"},
{"controller suc node-id INT", cmd_controller_set_suc_node, "Set SUC node"},
{"controller suc node-id INT enable-sis", cmd_controller_set_suc_node, "Set SUC and SIS node"},
{"no controller suc node-id INT", cmd_controller_set_suc_node, "Disable SUC node"},
//{"controller learn-mode stop", cmd_controller_set_learn_mode, "Stop learn mode"},
{"show controller queue", cmd_show_controller_queue, "Display the contents of controller job queue"},
{"list command-class", cmd_list_command_classes, "List all supported command classes"},
{"help", cmd_help, "(module) Display help for module"},
{"banner CHAR", cmd_set_banner, "Set new banner"},
{"no banner", cmd_clear_banner, "Clear banner"},
{"show banner", cmd_show_banner, "Show banner"},
{"show running-config", cmd_show_running_config, "Show running configuration"},
{"copy running-config startup-config", cmd_save_running_config, "Save running config into startup config"},
{"copy running-config file WORD", cmd_copy_running_config, "Save running config into custom location"},
{"list history", cmd_show_history, "List command history"},
{"show history", cmd_show_history, "Show command history size"},
{"clear history", cmd_clear_history, "Clear command history"},
{"history INT", cmd_set_history, "Set command history size"},
{"eval LINE", cmd_eval_expression, "Evaluate expression"},
{"more", cmd_pager, "Pager"},
{"grep LINE", cmd_line_filter, "Line filter"},
{"end", cmd_exit_node, "End configuration session"},
{"logout", cmd_logout, "Logout"},
{"exit", cmd_quit, "Exit the session"},
{"quit", cmd_shutdown, "Exit the application"},
{NULL, NULL, NULL}
};
//cli_node_t* current_node;
extern ZWay zway;
vty_t* default_vty;
void cli_set_vty(vty_t* new_vty)
{
//vty_t* vty = new_vty;
vty_set_prompt(new_vty, "(%s)# ", root_node->prompt);
if(banner != NULL)
{
vty_set_banner(new_vty, banner);
}
vty_set_history_size(new_vty, history_size);
new_vty->current_node = root_node;
default_vty = new_vty;
}
void cli_init()
{
cli_node_list = stack_create();
cli_install_node(&root_node, NULL, root_command_list, "", "config");
cli_scene_init(root_node);
cli_resolver_init(root_node);
cli_sensor_init(root_node);
cli_vdev_init(root_node);
cli_service_init(root_node);
cli_logger_init(root_node);
cli_auth_init(root_node);
cli_rest_init(root_node);
cli_homebridge_init(root_node);
//current_node = root_node;
}
void cli_load_config()
{
char config_loc[512] = {0};
snprintf(config_loc, 511, "%s/startup-config", global_config.config_location);
vty_io_data_t* file_data = calloc(1, sizeof(vty_io_data_t));
file_data->desc.file = fopen(config_loc, "r");
if(NULL != file_data->desc.file)
{
vty_t* file_vty = vty_io_create(VTY_FILE, file_data);
vty_set_prompt(file_vty, "(%s)# ", root_node->prompt);
char* str;
cli_set_vty(file_vty);
while((str = vty_read(file_vty)) && !vty_is_error(file_vty))
{
if(vty_is_command_received(file_vty))
{
cli_command_exec(file_vty, str);
}
}
vty_free(file_vty);
}
}
int cli_command_quit(int count, int key)
{
keep_running = 0;
siglongjmp(jmpbuf, 1);
}
void child_cmd_stopped_handler(int sig)
{
pid_t pid = waitpid((pid_t)(-1), 0, 0);
}
void iopair_close_cb(vty_io_data_t* vty_data)
{
fclose(vty_data->desc.io_pair[IN]);
fclose(vty_data->desc.io_pair[OUT]);
}
bool cli_command_exec_custom_node(cli_node_t* node, vty_t* vty, char* line)
{
cmd_tree_node_t* cmd_node;
bool retVal = false;
if(line == 0 || *line == 0 || *line == '\n' || *line == '\r')
{
return false;
}
if(isspace(line[strlen(line) - 1]))
{
line[strlen(line) - 1] = 0;
}
char* pipe_char = rindex(line, '|');
char* pipe_method = NULL;
if(NULL != pipe_char && *(pipe_char-1) != '|')
{
pipe_method = pipe_char+1;
*pipe_char = 0;
}
if(NULL != pipe_method)
{
int pipe_fd[2];
pipe(pipe_fd);
vty_io_data_t* vty_data = calloc(1, sizeof(vty_io_data_t));
vty_data->desc.io_pair[IN] = fdopen(pipe_fd[IN], "r");
vty_data->desc.io_pair[OUT] = fdopen(pipe_fd[OUT], "w");
vty_data->close_cb = iopair_close_cb;
vty_t* vty_pipe = vty_io_create(VTY_STD, vty_data);
vty_pipe->term_width = vty->term_width;
vty_pipe->term_height = vty->term_height;
cli_set_vty(vty_pipe);
pid_t pid = fork();
if(0 == pid)
{
vty_store_vty(vty_pipe, vty);
cli_command_exec_custom_node(root_node, vty_pipe, pipe_method);
exit(0);
}
else
{
// parent
/*struct sigaction sa;
sa.sa_handler = &child_cmd_stopped_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
sigaction(SIGCHLD, &sa, 0);*/
retVal = cli_command_exec_custom_node(node, vty_pipe, line);
// Insert EOF char
char eof_ch = (char)EOF;
//vty_pipe->write_cb(vty_pipe, &eof_ch, 1);
vty_write(vty_pipe, "%c", eof_ch);
int status = 0;
// Wait for pipe to process all input
waitpid((pid_t)(pid), &status, 0);
// Recover
cli_set_vty(vty);
vty_free(vty_pipe);
return retVal;
}
}
variant_stack_t* params;
CmdMatchStatus match_status = cli_get_custom_command(node, line, &cmd_node, ¶ms);
switch(match_status)
{
case CMD_FULL_MATCH:
{
retVal = cmd_node->data->command->func(vty, params);
}
break;
case CMD_PARTIAL_MATCH:
vty_error(vty, "Ambiguous command%s", VTY_NEWLINE(vty));
break;
case CMD_NO_MATCH:
vty_error(vty, "Unknown command%s", VTY_NEWLINE(vty));
break;
}
stack_free(params);
return retVal;
}
bool cli_command_exec(vty_t* vty, char* line)
{
cli_command_exec_custom_node(vty->current_node, vty, line);
}
void cli_command_exec_default(char* line)
{
cli_command_exec(default_vty, line);
}
void cli_commands_handle_connect_event(event_pump_t* pump, int fd, void* context)
{
int session_sock = accept(fd, NULL, NULL);
vty_io_data_t* vty_data = calloc(1, sizeof(vty_io_data_t));
vty_data->desc.socket = session_sock;
vty_t* vty_sock = vty_io_create(VTY_SOCKET, vty_data);
vty_set_pump(vty_sock, pump);
event_dispatcher_register_handler(pump, session_sock, &cli_commands_handle_data_read_event, &vty_nonblock_write_event, (void*)vty_sock);
socket_configure_terminal(vty_sock);
//stack_push_back(client_socket_list, variant_create_ptr(DT_PTR, vty_sock, variant_delete_none));
logger_register_target(vty_sock);
LOG_ADVANCED(CLI, "Remote client connected");
cli_set_vty(vty_sock);
vty_display_banner(vty_sock);
if(user_manager_get_count() > 0)
{
cmd_enter_auth_node(vty_sock);
}
vty_display_prompt(vty_sock);
}
void cli_commands_handle_data_read_event(event_pump_t* pump, int fd, void* context)
{
vty_t* vty_ptr = (vty_t*)context;
char* str = vty_read(vty_ptr);
if(vty_is_command_received(vty_ptr))
{
vty_new_line(vty_ptr);
cli_command_exec(vty_ptr, str);
vty_display_prompt(vty_ptr);
vty_flush(vty_ptr);
}
else if(vty_is_error(vty_ptr))
{
LOG_ERROR(CLI, "Socket error");
logger_unregister_target(vty_ptr);
event_dispatcher_unregister_handler(pump, fd, &vty_free, (void*)vty_ptr);
//vty_free(vty_ptr);
}
if(vty_is_shutdown(vty_ptr))
{
LOG_ADVANCED(CLI, "Remote client disconnected");
logger_unregister_target(vty_ptr);
event_dispatcher_unregister_handler(pump, fd, &vty_free, (void*)vty_ptr);
//vty_free(vty_ptr);
}
}
void cli_commands_handle_http_data_event(event_pump_t* pump, int fd, void* context)
{
vty_t* vty_ptr = (vty_t*)context;
vty_ptr->current_node = root_node;
char* str = vty_read(vty_ptr);
if(vty_is_command_received(vty_ptr))
{
if(!vty_is_error(vty_ptr))
{
// Skip the first 4 chars "GET "
cli_command_exec(vty_ptr, str+4);
}
vty_flush(vty_ptr);
event_dispatcher_unregister_handler(pump, fd, &vty_free, (void*)vty_ptr);
}
}
bool cmd_controller_inclusion_mode(vty_t* vty, variant_stack_t* params)
{
const char* mode = variant_get_string(stack_peek_at(params, 2));
ZWError err;
if(strcmp(mode, "start") == 0)
{
err = zway_controller_add_node_to_network(zway, TRUE);
vty_write(vty, "%% Inclusion started%s", VTY_NEWLINE(vty));
USING_LOGGER(DeviceCallback)
void* event_data;
if(event_dispatcher_wait(DeviceAddedEvent, 260*1000, &event_data))
{
//vty_write(vty, "%% Discovered new device with ID: %d%s", (int)event_data, VTY_NEWLINE(vty));
LOG_INFO(CLI, "Discovered new device with ID: %d", (int)event_data);
zway_controller_add_node_to_network(zway, FALSE);
}
else
{
LOG_INFO(CLI, "No new device discovered", (int)event_data);
}
}
else
{
err = zway_controller_add_node_to_network(zway, FALSE);
vty_write(vty, "%% Inclusion stopped%s", VTY_NEWLINE(vty));
//err = zway_fc_add_node_to_network(zway, FALSE, TRUE, NULL, NULL, NULL);
}
return (err == 0);
}
bool cmd_controller_exclusion_mode(vty_t* vty, variant_stack_t* params)
{
const char* mode = variant_get_string(stack_peek_at(params, 2));
ZWError err;
if(strcmp(mode, "start") == 0)
{
err = zway_controller_remove_node_from_network(zway, TRUE);
vty_write(vty, "%% Exclusion started%s", VTY_NEWLINE(vty));
//err = zway_fc_remove_node_from_network(zway, TRUE, TRUE, NULL, NULL, NULL);
}
else
{
err = zway_controller_remove_node_from_network(zway, FALSE);
vty_write(vty, "%% Exclusion stopped%s", VTY_NEWLINE(vty));
//err = zway_fc_remove_node_from_network(zway, FALSE, FALSE, NULL, NULL, NULL);
}
return (err == 0);
}
bool cmd_controller_set_suc_node(vty_t* vty, variant_stack_t* params)
{
bool enable_suc = true;
bool enable_sis = false;
uint8_t nodeId = 0;
if(strcmp(variant_get_string(stack_peek_at(params, 0)), "no") == 0)
{
enable_suc = false;
nodeId = variant_get_int(stack_peek_at(params, 4));
}
else
{
if(params->count == 5 && strcmp(variant_get_string(stack_peek_at(params, 4)), "enable-sis") == 0)
{
enable_sis = true;
}
nodeId = variant_get_int(stack_peek_at(params, 3));
}
zway_fc_set_suc_node_id(zway, nodeId, enable_suc, enable_sis, NULL, NULL, NULL);
}
bool cmd_controller_set_learn_mode(vty_t* vty, variant_stack_t* params)
{
const char* mode = variant_get_string(stack_peek_at(params, 2));
ZWError err = 0;
if(strcmp(mode, "start") == 0)
{
err = zway_controller_set_learn_mode(zway, 1);
}
else if(strcmp(mode, "start-nwi") == 0)
{
err = zway_controller_set_learn_mode(zway, 2);
}
else if(strcmp(mode, "stop") == 0)
{
err = zway_controller_set_learn_mode(zway, 0);
}
return (err == 0);
}
bool cmd_controller_reset(vty_t* vty, variant_stack_t* params)
{
zway_fc_serial_api_soft_reset(zway,NULL,NULL,NULL);
}
bool cmd_controller_factory_reset(vty_t* vty, variant_stack_t* params)
{
zway_fc_set_default(zway,NULL,NULL,NULL);
}
bool cmd_controller_config_save(vty_t* vty, variant_stack_t* params)
{
ZWBYTE* save_buffer;
size_t buffer_len;
ZWError err = zway_controller_config_save(zway, &save_buffer, &buffer_len);
if(err == 0)
{
char config_loc[512] = {0};
snprintf(config_loc, 511, "%s/controller-backup.tgz", global_config.config_location);
FILE* f = fopen(config_loc, "w");
if(NULL != f)
{
if(1 == fwrite(save_buffer, buffer_len, 1, f))
{
fclose(f);
free(save_buffer);
return true;
}
}
}
return false;
}
bool cmd_controller_config_restore(vty_t* vty, variant_stack_t* params)
{
char config_loc[512] = {0};
snprintf(config_loc, 511, "%s/controller-backup.tgz", global_config.config_location);
byte_buffer_t* buffer = byte_buffer_init(128000);
FILE* f = fopen(config_loc, "r");
if(NULL != f)
{
char buf[512];
int n = fread(buf, 1, 511, f);
while(n == 511)
{
byte_buffer_append(buffer, buf, n);
n = fread(buf, 1, 511, f);
}
byte_buffer_append(buffer, buf, n);
fclose(f);
}
ZWError err = zway_controller_config_restore(zway, byte_buffer_get_read_ptr(buffer), byte_buffer_read_len(buffer), true);
if(err != 0)
{
LOG_ERROR(CLI, "Controller config restore error: %d", err);
}
byte_buffer_free(buffer);
return true;
}
bool cmd_controller_remove_failed_node(vty_t* vty, variant_stack_t* params)
{
ZWBYTE node_id = variant_get_int(stack_peek_at(params, 3));
ZWError err = zway_fc_remove_failed_node(zway, node_id, NULL, NULL, NULL);
}
bool cmd_controller_ping(vty_t* vty, variant_stack_t* params)
{
ZWBYTE node_id = variant_get_int(stack_peek_at(params, 3));
ZWError err = zway_device_send_nop(zway, node_id, NULL, NULL, NULL);
}
bool cmd_show_controller_queue(vty_t* vty, variant_stack_t* params)
{
vty_write(vty, "Status: D - done, W - waiting for wakeup of the device, S - waiting for security session%s", VTY_NEWLINE(vty));
char queuebuf[65000] = {0};
FILE* queue_out = fmemopen(queuebuf, 64999, "w+");
zway_queue_inspect(zway, queue_out);
fflush(queue_out);
vty_write_multiline(vty, queuebuf);
}
bool cmd_list_command_classes(vty_t* vty, variant_stack_t* params)
{
vty_write(vty, "%-15s%-15s%s%s", "Command Id", "Name", "Methods(Args)", VTY_NEWLINE(vty));
command_class_for_each(show_command_class_helper, vty);
}
bool cmd_help(vty_t* vty, variant_stack_t* params)
{
}
bool cmd_set_banner(vty_t* vty, variant_stack_t* params)
{
banner_stop_char = *variant_get_string(stack_peek_at(params, 1));
vty_write(vty, "Enter banner text ending with '%c'%s", banner_stop_char, VTY_NEWLINE(vty));
free(banner);
banner = NULL;
vty_set_multiline(vty, true, banner_stop_char);
do
{
vty_read(vty);
}
while(!vty_is_command_received(vty));
if(NULL != vty->buffer)
{
banner = strdup(vty->buffer);
}
vty_write(vty, VTY_NEWLINE(vty));
}
typedef struct pager_context_t
{
bool keypressed;
char key;
vty_t* vty;
} pager_context_t;
void on_pager_command_event(event_pump_t* pump, int fd, void* context)
{
pager_context_t* ctx = (pager_context_t*)context;
char* str = vty_read(ctx->vty->stored_vty);
ctx->keypressed = true;
ctx->key = *str;
vty_clear_buffer(ctx->vty->stored_vty);
}
bool cmd_pager(vty_t* vty, variant_stack_t* params)
{
int rows = 0;
char* ch;
int n = 0;
bool is_quit = false;
do
{
do
{
if(0 != *ch)
{
memset(ch, 0, 1);
}
n = vty->read_cb(vty);
ch = byte_buffer_get_read_ptr(vty->read_buffer);
while(ch[0] != 0xa && ch[0] != 0xd && ch[0] != (char)EOF)
{
byte_buffer_adjust_read_pos(vty->read_buffer, 1);
vty_write(vty->stored_vty, "%c", *ch);
memset(ch, 0, n);
n = vty->read_cb(vty);
ch = byte_buffer_get_read_ptr(vty->read_buffer);
}
byte_buffer_reset(vty->read_buffer);
vty_write(vty->stored_vty, "%s", VTY_NEWLINE(vty->stored_vty));
}
while(++rows < vty->term_height && ch[0] != (char)EOF && n > 0);
if(ch[0] != (char)EOF && n > 0)
{
rows = 0;
vty_write(vty->stored_vty, "-- more --");
pager_context_t ctx = {.keypressed = false, .key = 0, .vty = vty};
// Run event dispatcher here to catch continue or quit
event_dispatcher_t* wait_dispatcher = event_dispatcher_new();
event_pump_t* socket_pump = wait_dispatcher->get_pump(wait_dispatcher, "SOCKET_PUMP");
int fd = -1;
switch(vty->stored_vty->type)
{
case VTY_SOCKET:
fd = vty->stored_vty->data->desc.socket;
break;
case VTY_STD:
fd = fileno(vty->stored_vty->data->desc.io_pair[IN]);
break;
}
event_dispatcher_register_handler(socket_pump, fd, &on_pager_command_event, &vty_nonblock_write_event, &ctx);
while(!ctx.keypressed)
{
wait_dispatcher->timed_start(wait_dispatcher, 100);
if(ctx.key != 0xa && ctx.key != 0xd && ctx.key != ' ' && ctx.key != 'q' && ctx.key != 'Q')
{
ctx.keypressed = false;
continue;
}
if(ctx.key == 'q' || ctx.key == 'Q')
{
vty_new_line(vty->stored_vty);
is_quit = true;
}
}
event_dispatcher_free(wait_dispatcher);
}
} while(ch[0] != (char)EOF && n > 0 && !is_quit);
//free(ch);
}
bool cmd_line_filter(vty_t* vty, variant_stack_t* params)
{
bool isOk;
char expression[512] = {0};
cli_assemble_line(params, 1, expression, 511);
char buf[512] = {0};
char* ch = NULL;
int n = 0;
do
{
n = vty->read_cb(vty);
ch = byte_buffer_get_read_ptr(vty->read_buffer);
while(ch[0] != 0xa && ch[0] != 0xd && ch[0] != (char)EOF)
{
byte_buffer_adjust_read_pos(vty->read_buffer, n);
strcat(buf, ch);
memset(ch, 0, n);
n = vty->read_cb(vty);
ch = byte_buffer_get_read_ptr(vty->read_buffer);
}
byte_buffer_reset(vty->read_buffer);
if(strstr(buf, expression) != NULL)
{
vty_write(vty->stored_vty, "%s%s", buf, VTY_NEWLINE(vty->stored_vty));
}
memset(buf, 0, 512);
}
while(ch[0] != (char)EOF && n > 0);
//free(ch);
}
bool cmd_show_banner(vty_t* vty, variant_stack_t* params)
{
if(0 != banner_stop_char && NULL != banner)
{
vty_write(vty, "banner %c%s", banner_stop_char, VTY_NEWLINE(vty));
vty_write_multiline(vty, banner);
vty_write(vty, "%c%s", banner_stop_char, VTY_NEWLINE(vty));
//vty_write(vty, "%s%c%s", banner, banner_stop_char, VTY_NEWLINE(vty));
}
else
{
vty_write(vty, "no banner%s", VTY_NEWLINE(vty));
}
}
bool cmd_clear_banner(vty_t* vty, variant_stack_t* params)
{
banner_stop_char = 0;
vty->banner = NULL;
free(banner);
banner = NULL;
}
void cmd_enter_root_node(vty_t* vty)
{
stack_for_each(cli_node_list, cli_node_variant)
{
cli_node_t* node = (cli_node_t*)variant_get_ptr(cli_node_variant);
if(strstr(node->name, "") == node->name)
{
//current_node = node;
vty->current_node = node;
char prompt[256] = {0};
sprintf(prompt, "(%s)# ", vty->current_node->prompt);
vty_set_prompt(vty, prompt);
break;
}
}
}
bool cmd_enter_node(vty_t* vty, variant_stack_t* params)
{
const char* node_name = variant_get_string(stack_peek_at(params, 0));
cmd_enter_node_by_name(vty, node_name);
}
bool cmd_enter_node_by_name(vty_t* vty, const char* node_name)
{
stack_for_each(cli_node_list, cli_node_variant)
{
cli_node_t* node = (cli_node_t*)variant_get_ptr(cli_node_variant);
if(strstr(node->name, node_name) == node->name)
{
char prompt[256] = {0};
strncpy(prompt, vty->prompt, 255);
// vty->prompt = (config-scene)#
// node->prompt = env
// new prompt = (config-scene-env)#
char* prompt_end = strstr(prompt, ")#");
*prompt_end = '\0';
sprintf(prompt_end, "-%s)# ", node->prompt);
//sprintf(prompt, "(%s-%s)# ", current_node->prompt, node->prompt);
vty_set_prompt(vty, prompt);
//current_node = node;
vty->current_node = node;
sprintf(prompt, "(%s)# ", vty->current_node->prompt);
break;
}
}
}
bool cmd_exit_node(vty_t* vty, variant_stack_t* params)
{
if(root_node != vty->current_node)
{
//free(current_node->context);
//current_node->context = 0;
free(vty->current_node->context);
vty->current_node->context = 0;
vty->current_node = vty->current_node->parent;
//current_node = current_node->parent;
char prompt[256] = {0};
if(vty->current_node == root_node)
{
sprintf(prompt, "(%s)# ", vty->current_node->prompt);
}
else
{
strncpy(prompt, vty->prompt, 255);
// vty->prompt = (config-scene-env)#
// current_node->prompt = scene
char* current_prompt_start = strstr(prompt, vty->current_node->prompt);
// move strlen(current prompt) to point to the next "-"
while(*current_prompt_start != '-')
{
current_prompt_start++;
}
// Now append ")# "
*current_prompt_start = '\0';
strcat(current_prompt_start, ")# ");
//sprintf(prompt, "(%s)# ", current_node->prompt);
}
vty_set_prompt(vty, prompt);
}
return true;
}
bool cmd_close_session(vty_t* vty, variant_stack_t* params)
{
}
bool cmd_show_running_config(vty_t* vty, variant_stack_t* params)
{
cli_command_exec(vty, "show banner");
vty_write(vty, "!%s",VTY_NEWLINE(vty));
cli_command_exec(vty, "show user");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show history");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show logging");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show resolver");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show sensor descriptor");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show service");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show virtual-device");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show homebridge");
vty_write(vty, "!%s", VTY_NEWLINE(vty));
cli_command_exec(vty, "show scene");
}
bool cmd_save_running_config(vty_t* vty, variant_stack_t* params)
{
char config_loc[512] = {0};
snprintf(config_loc, 511, "%s/startup-config", global_config.config_location);
char backup_loc[512] = {0};
snprintf(backup_loc, 511, "%s/startup-config.backup", global_config.config_location);
// First rename current startup config to backup
rename(config_loc, backup_loc);
vty_io_data_t* file_vty_data = malloc(sizeof(vty_io_data_t));
file_vty_data->desc.file = fopen(config_loc, "w");
vty_t* file_vty = vty_io_create(VTY_FILE, file_vty_data);
vty_set_banner(file_vty, vty->banner);
vty_set_history_size(file_vty, vty->history_size);
file_vty->current_node = vty->current_node;
cmd_show_running_config(file_vty, NULL);
vty_free(file_vty);
}
bool cmd_copy_running_config(vty_t* vty, variant_stack_t* params)
{
const char* dest_filename = variant_get_string(stack_peek_at(params, 3));
char config_loc[512] = {0};
snprintf(config_loc, 511, "%s/%s", global_config.config_location, dest_filename);
vty_io_data_t file_vty_data = {
.desc.file = fopen(config_loc, "w")
};
vty_t* file_vty = vty_create(VTY_FILE, &file_vty_data);
cmd_show_running_config(file_vty, NULL);
vty_free(file_vty);
}
bool cmd_show_history(vty_t* vty, variant_stack_t* params)
{
const char* mode = variant_get_string(stack_peek_at(params, 0));
if(strcmp(mode, "list") == 0)
{
vty_show_history(vty);
}
else if(strcmp(mode, "show") == 0)
{
vty_write(vty, "history %d%s", vty->history_size, VTY_NEWLINE(vty));
}
}
bool cmd_clear_history(vty_t* vty, variant_stack_t* params)
{
vty_clear_history(vty);
}
bool cmd_set_history(vty_t* vty, variant_stack_t* params)
{
history_size = variant_get_int(stack_peek_at(params, 1));
vty_set_history_size(vty, history_size);
}
bool cmd_logout(vty_t* vty, variant_stack_t* params)
{
cli_node_t* auth_node = cli_find_node("auth-user");
if(auth_node != NULL)
{
char prompt[256] = {0};
strncpy(prompt, auth_node->prompt, 255);
vty_set_prompt(vty, prompt);
vty_set_history_enabled(vty, false);
vty->current_node = auth_node;
}
}
bool cmd_quit(vty_t* vty, variant_stack_t* params)
{
//cli_command_quit(0, 0);
vty_shutdown(vty);
}
bool cmd_shutdown(vty_t* vty, variant_stack_t* params)
{
kill(getpid(), SIGHUP);
}
bool cmd_eval_expression(vty_t* vty, variant_stack_t* params)
{
bool isOk;
char expression[512] = {0};
cli_assemble_line(params, 1, expression, 511);
variant_stack_t* compiled_value = command_parser_compile_expression(expression, &isOk);
bool retVal = false;
if(isOk)
{
variant_t* result = command_parser_execute_expression(compiled_value);
if(NULL != result)
{
char* str_result;
if(variant_to_string(result, &str_result))
{