-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathzpc_stdin_command_handling.cpp
More file actions
984 lines (887 loc) · 33.9 KB
/
zpc_stdin_command_handling.cpp
File metadata and controls
984 lines (887 loc) · 33.9 KB
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
/******************************************************************************
* # License
* <b>Copyright 2021 Silicon Laboratories Inc. www.silabs.com</b>
******************************************************************************
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
*****************************************************************************/
#include "zpc_stdin_command_handling.h"
// Unify components
#include "sl_log.h"
#include "uic_mqtt.h"
#include "uic_stdin.hpp"
#include "ota_time.h"
// Other component includes
#include "zwave_controller.h"
#include "zwave_controller_keyset.h"
#include "zwave_controller_utils.h"
#include "zwave_tx.h"
#include "zwave_tx_groups.h"
#include "attribute_store.h"
#include "zpc_attribute_store_network_helper.h"
#include "zwave_unid.h"
#include "zwave_utils.h"
#include "zwave_tx_scheme_selector.h"
#include "zwave_command_class_supervision.h"
#include "zwave_command_handler.h"
#include "zwave_command_class_association_send.h"
#include "zwave_command_class_firmware_update.h"
#include "zpc_converters.h"
#include "ucl_mqtt_node_interview.h"
#include "attribute_store_configuration.h"
#include "attribute_store_type_registration.h"
#include "attribute.hpp"
#include "zwave_s2_keystore.h"
#include "zwave_s2_nonce_management.h"
#include "zwapi_protocol_controller.h"
// Contiki includes
#include "clock.h" // For CLOCK_CONF_SECOND
// C++ stdlib includes
#include <cstdio>
#include <cstring>
#include <sstream>
#include <map>
#include <string>
#include <vector>
#include <array>
#include <stdexcept>
#include <float.h>
#include <unistd.h>
// Type aliasing
using attribute_store_get_string_function_t
= sl_status_t (*)(attribute_store_node_t, char *, size_t);
/// File descripter for output stream
static int out_stream;
/// The DSK reported by the node during the S2 inclusion
static uint8_t node_reported_dsk[16] = {0};
// Private handler functions
static sl_status_t handle_zwave_set_default(const handle_args_t &arg);
static sl_status_t handle_zwave_log_security_keys(const handle_args_t &arg);
static sl_status_t
handle_zwave_save_security_keys_to_file(const handle_args_t &arg);
static sl_status_t handle_zwave_tx(const handle_args_t &arg);
static sl_status_t handle_zwave_tx_association(const handle_args_t &arg);
static sl_status_t handle_zwave_tx_multi(const handle_args_t &arg);
static sl_status_t handle_zwave_tx_log(const handle_args_t &arg);
static sl_status_t
handle_zwave_command_handler_dispatch(const handle_args_t &arg);
static sl_status_t handle_remove_zwave_node(const handle_args_t &arg);
static sl_status_t handle_attribute_store_log_network(const handle_args_t &arg);
static sl_status_t handle_zwave_remove_failed(const handle_args_t &arg);
static sl_status_t handle_add_zwave_node(const handle_args_t &arg);
static sl_status_t handle_add_zwave_node_abort(const handle_args_t &arg);
static sl_status_t handle_grant_keys(const handle_args_t &arg);
static sl_status_t handle_accept_dsk(const handle_args_t &arg);
static sl_status_t handle_zwave_wake_up(const handle_args_t &arg);
static sl_status_t handle_zwave_learn_mode(const handle_args_t &arg);
static sl_status_t handle_zwave_initiate_interview(const handle_args_t &arg);
static sl_status_t handle_cc_versions_log(const handle_args_t &arg);
static sl_status_t
handle_zwave_initiate_firmware_update(const handle_args_t &arg);
static sl_status_t handle_zwave_abort_firmware_update(const handle_args_t &arg);
static sl_status_t handle_zwave_reset_span(const handle_args_t &arg);
static sl_status_t handle_zwave_reset_mpan(const handle_args_t &arg);
static sl_status_t handle_zwave_home_id(const handle_args_t &arg);
static sl_status_t handle_enable_nls(const handle_args_t &arg);
static sl_status_t handle_get_nls_state(const handle_args_t &arg);
static sl_status_t handle_set_priority_route(const handle_args_t &arg);
/// Map that holds all the commands (used for printing help)
///
/// Entries are {<command>, {<help message>, <handler_func>}},
/// <command>: The command written on the command interface (e.g. "help")
/// <help_message>: Help message printed to output when "help" is executed
/// <handler_func>: callback, that is called whenever the command is receied
const std::map<std::string, std::pair<std::string, handler_func>> commands = {
{"zwave_set_default", {"Reset Z-Wave network", &handle_zwave_set_default}},
{"zwave_log_security_keys",
{"Log Z-Wave network security keys", &handle_zwave_log_security_keys}},
{"zwave_save_security_keys_to_file",
{"Save Z-Wave network security keys to a file",
&handle_zwave_save_security_keys_to_file}},
{"zwave_remove_failed",
{COLOR_START "<NodeID>" COLOR_END " :Remove a failing Z-Wave node.",
&handle_zwave_remove_failed}},
{"zwave_home_id", {"Print Z-Wave Home ID", &handle_zwave_home_id}},
{"zwave_reset_span",
{COLOR_START "<NodeID>" COLOR_END " :Reset SPAN for the node id.",
&handle_zwave_reset_span}},
{"zwave_reset_mpan",
{COLOR_START "<Group ID><NodeID>" COLOR_END " :Reset MPAN for the Group id "
"and node id. Only" COLOR_START "< Group ID >" COLOR_END
"is also accepted,"
" then ZPC node id will be used",
&handle_zwave_reset_mpan}},
{"zwave_tx",
{COLOR_START
"\n\r1. <NodeID>,<Hexadecimal stream payload>" COLOR_END
" :Send Z-Wave payload to a given NodeID." COLOR_START
"\n\r2. <NodeID>,<Dst End Point>,<Src End Point>,<Hex payload>" COLOR_END
" :Send Z-Wave payload to a given NodeID, using Multi Channel "
"encapsulation.",
&handle_zwave_tx}},
{"zwave_tx_association",
{COLOR_START
"<Group ID>,<Hexadecimal stream payload>" COLOR_END
" :Sends Z-Wave payload to all destinations in an Association Group ID.",
&handle_zwave_tx_association}},
{"zwave_tx_multi",
{COLOR_START "\n\r <NodeID>,<NodeID>,...,<NodeID>,<Hexadecimal stream "
"payload>" COLOR_END
" :Send Z-Wave payload to a list of NodeID. The last argument "
"will be the hexadecimal payload.",
&handle_zwave_tx_multi}},
{"zwave_tx_log",
{COLOR_START
"<1|0>" COLOR_END
" :Logs the content of the TX queue. Use 1 to print frame contents. "
"0 to print the frames only. Default option is 0.",
&handle_zwave_tx_log}},
{"zwave_command_handler_dispatch",
{COLOR_START "<payload>" COLOR_END
" :Injects a payload to the local Z-Wave dispatch handler.",
&handle_zwave_command_handler_dispatch}},
{"zwave_remove_node",
{" :Remove a Z-Wave node from the network", &handle_remove_zwave_node}},
{"attribute_store_log_network",
{COLOR_START
"<NodeID>" COLOR_END
" :Logs the attribute store up to 3 levels and then just displays"
" the number of children nodes "
"The entire attribute store is dumped if no NodeID is provided",
&handle_attribute_store_log_network}},
{"zwave_network_management_abort",
{" :Abort an ongoing zwave network management operation if possible",
&handle_add_zwave_node_abort}},
{"zwave_add_node",
{" :Add a Z-Wave node to the network", &handle_add_zwave_node}},
{"zwave_set_priority_route",
{" :Set the priority route to a destination node",
&handle_set_priority_route}},
{"zwave_grant_keys",
{COLOR_START
"<1/0 to accept/reject the requested keys>,<Set of keys to"
" grant in [Hexadecimal]>,<1 if csa requested,"
"if not no need to insert this value>" COLOR_END
" :Accept security bootstraping and grant the requested keys. For e.g. "
"zwave_grant_keys 1,87,1 for accepting all keys with csa",
&handle_grant_keys}},
{"zwave_accept_dsk",
{" :Verify the DSK and insert the missing part. Usage: zwave_accept_dsk "
"<insert the first two byte of the DSK in [decimal]>",
&handle_accept_dsk}},
{"zwave_wake_up",
{COLOR_START "<NodeID>" COLOR_END
" :Attempts to fast track a Wake Up from a Wake "
"Up Node.",
&handle_zwave_wake_up}},
{"zwave_learn_mode",
{COLOR_START
"<Learn Mode Intent>" COLOR_END
" :Selects the Learn Mode intent. The following values are available:"
" - 'inclusion': Direct range Inclusion"
" - 'exclusion': Direct range Exclusion"
" - 'nwi': Network-Wide Inclusion"
" - 'nwe': Network-Wide Exclusion"
" - 'stop': Stop Ongoing Learn Mode operation"
" For e.g. zwave_learn_mode inclusion (It will trigger Learn Mode with "
"direct range communication.)",
&handle_zwave_learn_mode}},
{"zwave_initiate_interview",
{COLOR_START
"\n\r1. <NodeID>" COLOR_END
" :Initiates a full interview for the given NodeID." COLOR_START
"\n\r2. <NodeID>,<End Point ID>" COLOR_END
" :Initiates a full interview for a given Endpoint under a NodeID",
&handle_zwave_initiate_interview}},
{"zwave_initiate_firmware_update",
{COLOR_START "<NodeID>,<filename>" COLOR_END
"Initiates a firmware update of <NodeID> Endpoint 0, "
"firmware 0, using the "
"specified file name for binary data. ",
&handle_zwave_initiate_firmware_update}},
{"zwave_abort_firmware_update",
{COLOR_START "<NodeID>" COLOR_END
"Ensures that any firmware update of <NodeID> is stopped.",
&handle_zwave_abort_firmware_update}},
{"zwave_cc_versions_log",
{"Print the CC version table", &handle_cc_versions_log}},
{"zwave_enable_nls",
{COLOR_START "<NodeID>" COLOR_END
"Enable NLS on a given node.",
&handle_enable_nls}},
{"zwave_get_nls_state",
{COLOR_START "<NodeID>" COLOR_END
"Print the NLS state of the given nodeID",
&handle_get_nls_state}},
};
// Pre declaration of setup function
sl_status_t zpc_stdin_command_handling_init();
// Adding zpc commands to the Unify CLI
void zpc_setup_cli()
{
uic_stdin_add_commands(commands);
uic_stdin_set_prompt_string("ZPC>");
zpc_stdin_command_handling_init();
}
// Internal ZPC commands
static sl_status_t handle_zwave_set_default(const handle_args_t &arg)
{
zwave_controller_reset();
return SL_STATUS_OK;
}
static sl_status_t handle_zwave_log_security_keys(const handle_args_t &arg)
{
zwave_s2_log_security_keys(SL_LOG_INFO);
return SL_STATUS_OK;
}
static sl_status_t
handle_zwave_save_security_keys_to_file(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args: <File name>"
"For example zwave_save_security_keys_to_file keys.txt\n");
return SL_STATUS_FAIL;
}
const char *key_file_name = static_cast<const char *>(arg[1].c_str());
zwave_s2_save_security_keys(key_file_name);
return SL_STATUS_OK;
}
static sl_status_t handle_zwave_reset_mpan(const handle_args_t &arg)
{
uint8_t group_id = 0;
zwave_node_id_t node_id = zwave_network_management_get_node_id();
if ((arg.size() < 2) || (arg.size() > 3)) {
dprintf(out_stream,
"Invalid number of arguments, least expected args: <GroupID>"
" Or <GroupID>,<NodeID>\n"
"For example to reset MPAN of group id 2, zwave_reset_span 2 or\n"
" to reset MPAN of group 2 for owner id 2, zwave_reset_span 2,1\n");
return SL_STATUS_FAIL;
}
try {
group_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s: Invalid argument: %s\n", arg[0].c_str(), e.what());
return SL_STATUS_FAIL;
}
if (arg.size() == 3) {
try {
node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[2].c_str(), nullptr, 10));
} catch (const std::invalid_argument &e) {
dprintf(out_stream,
"%s: Invalid argument: %s\n",
arg[0].c_str(),
e.what());
return SL_STATUS_FAIL;
}
}
zwave_s2_reset_mpan(node_id, group_id);
return SL_STATUS_OK;
}
static sl_status_t handle_zwave_home_id(const handle_args_t &arg)
{
dprintf(out_stream,
"Z-Wave Home ID: %08X\n",
zwave_network_management_get_home_id());
return SL_STATUS_OK;
}
static sl_status_t handle_zwave_reset_span(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args: <NodeID>"
"For example zwave_reset_span 02\n");
return SL_STATUS_FAIL;
}
try {
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
zwave_s2_reset_span(node_id);
return SL_STATUS_OK;
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s: Invalid argument: %s\n", arg[0].c_str(), e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_remove_failed(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args: <NodeID>"
"For example zwave_remove_failed 02\n");
return SL_STATUS_FAIL;
}
try {
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
return zwave_network_management_remove_failed(node_id);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s: Invalid argument: %s\n", arg[0].c_str(), e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_tx(const handle_args_t &arg)
{
if (arg.size() != 3 && arg.size() != 5) {
dprintf(out_stream,
"Invalid number of arguments. You have the choice between:\n"
"1. <Dst NodeID>,<Hexpayload>. For example zwave_tx 02,2501FF\n"
"2. <Dst NodeID>,<Dst End Point>,<Src End Point>,<Hex payload>. "
"For example zwave_tx 02,03,00,2501FF\n");
return SL_STATUS_FAIL;
}
std::array<uint8_t, ZWAVE_MAX_FRAME_SIZE> payload;
zwave_tx_options_t tx_options = {};
zwave_controller_connection_info_t connection_info;
zwave_endpoint_id_t local_endpoint_id = 0;
zwave_endpoint_id_t remote_endpoint_id = 0;
uint8_t payload_index = 2;
if (arg.size() == 5) {
payload_index = 4;
}
try {
// Get the data from the input args
connection_info.remote.node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
if (arg.size() == 5) {
local_endpoint_id = static_cast<zwave_endpoint_id_t>(
std::stoi(arg[3].c_str(), nullptr, 10));
remote_endpoint_id = static_cast<zwave_endpoint_id_t>(
std::stoi(arg[2].c_str(), nullptr, 10));
}
for (std::size_t i = 0; i < (arg[payload_index].length()); i += 2) {
payload.at(i / 2) = static_cast<uint8_t>(
std::stoi(arg[payload_index].substr(i, 2), nullptr, 16));
}
// Prepare the Connection Info data for remote node
connection_info.local.endpoint_id = local_endpoint_id;
zwave_tx_scheme_get_node_connection_info(connection_info.remote.node_id,
remote_endpoint_id,
&connection_info);
// Some pre-defined Z-Wave TX options.
uint8_t number_of_expected_responses = 1;
uint32_t discard_timeout_ms = 5 * CLOCK_CONF_SECOND;
zwave_tx_scheme_get_node_tx_options(ZWAVE_TX_QOS_MIN_PRIORITY,
number_of_expected_responses,
discard_timeout_ms,
&tx_options);
return zwave_tx_send_data(&connection_info,
(uint16_t)(arg[payload_index].length() / 2),
payload.data(),
&tx_options,
nullptr,
nullptr,
nullptr);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_tx_association(const handle_args_t &arg)
{
if (arg.size() != 3) {
dprintf(
out_stream,
"Invalid number of arguments. Expected:\n"
"1. <Group ID>,<Hexpayload>. For example zwave_tx_association 01,5A01\n");
return SL_STATUS_FAIL;
}
std::array<uint8_t, ZWAVE_MAX_FRAME_SIZE> payload;
zwave_tx_options_t tx_options = {};
uint8_t payload_index = 2;
association_group_id_t group_id = 0;
try {
// Get the data from the input args
group_id = static_cast<association_group_id_t>(
std::stoi(arg[1].c_str(), nullptr, 10));
for (std::size_t i = 0; i < (arg[payload_index].length()); i += 2) {
payload.at(i / 2) = static_cast<uint8_t>(
std::stoi(arg[payload_index].substr(i, 2), nullptr, 16));
}
// Some pre-defined Z-Wave TX options.
uint8_t number_of_expected_responses = 0;
uint32_t discard_timeout_ms = 5 * CLOCK_CONF_SECOND;
zwave_tx_scheme_get_node_tx_options(ZWAVE_TX_QOS_MIN_PRIORITY,
number_of_expected_responses,
discard_timeout_ms,
&tx_options);
return zwave_association_send_to_group(
group_id,
(uint16_t)(arg[payload_index].length() / 2),
payload.data(),
true,
&tx_options,
0,
nullptr,
nullptr);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_tx_multi(const handle_args_t &arg)
{
if (arg.size() < 3) {
dprintf(out_stream,
"Invalid number of arguments. We need at least 2 (1 NodeID / 1 Hex "
"payload)"
"For example zwave_tx_multi 02,03,2501FF\n");
return SL_STATUS_FAIL;
}
std::array<uint8_t, ZWAVE_MAX_FRAME_SIZE> payload;
zwave_tx_options_t tx_options = {};
zwave_controller_connection_info_t connection_info;
zwave_nodemask_t node_mask = {};
memset(node_mask, 0, sizeof(zwave_nodemask_t));
uint8_t arg_index = 1;
try {
zwave_node_id_t current_node = 0;
while (arg_index < arg.size() - 1) {
current_node = static_cast<zwave_node_id_t>(
std::stoi(arg[arg_index].c_str(), nullptr, 10));
ZW_ADD_NODE_TO_MASK(current_node, node_mask);
arg_index += 1;
}
for (std::size_t i = 0; i < (arg[arg_index].length()); i += 2) {
payload.at(i / 2) = static_cast<uint8_t>(
std::stoi(arg[arg_index].substr(i, 2), nullptr, 16));
}
// Ask a Group ID to the Z-Wave TX Group manager
zwave_multicast_group_id_t group_id = 0;
if (SL_STATUS_OK != zwave_tx_assign_group(node_mask, &group_id)) {
return SL_STATUS_FAIL;
}
// Prepare the Connection Info data for remote nodes
connection_info.local.endpoint_id = 0;
// Take any node in the group for the connection_info, since
// they all have the same security level / protocol.
zwave_tx_scheme_get_node_connection_info(current_node, 0, &connection_info);
connection_info.remote.is_multicast = true;
connection_info.remote.multicast_group = group_id;
// Some pre-defined Z-Wave TX options.
uint8_t number_of_expected_responses = 0;
uint32_t discard_timeout_ms = 5 * CLOCK_CONF_SECOND;
zwave_tx_scheme_get_node_tx_options(ZWAVE_TX_QOS_MIN_PRIORITY,
number_of_expected_responses,
discard_timeout_ms,
&tx_options);
// We want the TX queue to do follow up for us.
tx_options.send_follow_ups = true;
return zwave_command_class_supervision_send_data(
&connection_info,
(uint16_t)(arg[arg_index].length() / 2),
payload.data(),
&tx_options,
nullptr,
nullptr,
nullptr);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_tx_log(const handle_args_t &arg)
{
zwave_tx_log_queue(true);
return SL_STATUS_OK;
}
static sl_status_t
handle_zwave_command_handler_dispatch(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(
out_stream,
"Invalid number of arguments: Payload needs to be specified.\n"
" Example: zwave_command_handler_dispatch 87010003500308500403500506\n");
return SL_STATUS_FAIL;
}
std::array<uint8_t, ZWAVE_MAX_FRAME_SIZE> frame_data;
try {
for (std::size_t i = 0; i < (arg[1].length()); i += 2) {
frame_data.at(i / 2)
= static_cast<uint8_t>(std::stoi(arg[1].substr(i, 2), nullptr, 16));
}
zwave_controller_connection_info_t connection_info = {{{0}}};
connection_info.remote.node_id = zwave_network_management_get_node_id();
connection_info.encapsulation = zpc_highest_security_class();
return zwave_command_handler_dispatch(&connection_info,
frame_data.data(),
(uint16_t)(arg[1].length() / 2));
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s: Invalid argument: %s\n", arg[0].c_str(), e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_remove_zwave_node(const handle_args_t &arg)
{
return zwave_network_management_remove_node();
}
static sl_status_t handle_zwave_wake_up(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args: <NodeID>"
"For example zwave_wake_up 23\n");
return SL_STATUS_FAIL;
}
try {
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
return zwave_command_class_supervision_wake_on_demand(node_id);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s: Invalid argument: %s\n", arg[1].c_str(), e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_learn_mode(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args: <Learn Mode Intent>\n"
"Supported modes nwi, nwe or stop\n"
"For example zwave_learn_mode nwi\n");
return SL_STATUS_FAIL;
}
std::string learn_mode_intent;
learn_mode_intent.assign(arg[1].c_str());
if (learn_mode_intent.compare("nwi") == 0) {
dprintf(out_stream, "Starting NWI Learn Mode Operation.\n");
return zwave_network_management_learn_mode(
ZWAVE_NETWORK_MANAGEMENT_LEARN_NWI);
} else if (learn_mode_intent.compare("nwe") == 0) {
dprintf(out_stream, "Starting NWE Learn Mode Operation.\n");
return zwave_network_management_learn_mode(
ZWAVE_NETWORK_MANAGEMENT_LEARN_NWE);
} else if (learn_mode_intent.compare("stop") == 0) {
dprintf(out_stream, "Stopping Learn Mode Operation.\n");
return zwave_network_management_learn_mode(
ZWAVE_NETWORK_MANAGEMENT_LEARN_NONE);
} else {
dprintf(out_stream, "Unknown learn mode intent!\n");
}
return SL_STATUS_OK;
}
static sl_status_t handle_set_priority_route(const handle_args_t &arg)
{
zwave_node_id_t dst_node_id = 0;
uint8_t route[PRIORITY_ROUTE_SIZE] = {0};
const int max_node_id = 0xE8;
if (arg.size() != 7) {
dprintf(out_stream,
"Invalid number of arguments, expected args:"
"<Dst NodeID>,<Hop 1>,<Hop 2>,<Hop 3>,<Hop 4>,<Speed> "
"all values in hexadecimal\n");
return SL_STATUS_FAIL;
}
try {
dst_node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 16));
for (size_t i = 0; i < 5; ++i) {
route[i]
= static_cast<uint8_t>(std::stoi(arg[i + 2].c_str(), nullptr, 16));
}
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "Invalid argument: %s\n", e.what());
return SL_STATUS_FAIL;
}
if (dst_node_id <= 0 || dst_node_id > max_node_id) {
dprintf(out_stream, "Invalid destination node id\n");
return SL_STATUS_FAIL;
}
bool previous_hop_was_direct = false;
for (size_t i = 0; i < 4; ++i) {
if (route[i] < 0 || route[i] > max_node_id) {
dprintf(out_stream, "Invalid hop node id\n");
return SL_STATUS_FAIL;
}
if (previous_hop_was_direct && route[i] != 0) {
dprintf(out_stream, "Invalid route. Cannot set a node id after a zero\n");
return SL_STATUS_FAIL;
}
if (0 == route[i]) {
previous_hop_was_direct = true;
}
}
if (route[4] <= 0 || route[4] > 3) {
dprintf(out_stream, "Invalid Route Speed.\n");
return SL_STATUS_FAIL;
}
return zwave_network_management_set_priority_route(dst_node_id, route);
}
static sl_status_t handle_add_zwave_node(const handle_args_t &arg)
{
return zwave_network_management_add_node();
}
static sl_status_t handle_add_zwave_node_abort(const handle_args_t &arg)
{
return zwave_network_management_abort();
}
static sl_status_t handle_grant_keys(const handle_args_t &arg)
{
uint8_t accept;
uint8_t csa = 0x00;
uint8_t keys;
if (arg.size() < 3 || arg.size() > 4) {
dprintf(out_stream,
"Invalid number of arguments, expected args:"
"<1/0 to accept/reject the requested keys>,<Set of keys to grant "
"in [Hexadecimal]>,<1 if csa requested,"
"if not no need to insert this value>\n");
return SL_STATUS_FAIL;
}
try {
accept = static_cast<uint8_t>(std::stoi(arg[1].c_str(), nullptr, 10));
keys = static_cast<uint8_t>(std::stoi(arg[2].c_str(), nullptr, 16));
if (arg.size() == 4) {
csa = static_cast<uint8_t>(std::stoi(arg[3].c_str(), nullptr, 10));
}
return zwave_network_management_keys_set((bool)accept, (bool)csa, keys);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_accept_dsk(const handle_args_t &arg)
{
uint16_t dsk;
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args:"
"zwave_accept_dsk <the first two byte of the DSK in [decimal]>\n");
return SL_STATUS_FAIL;
}
try {
dsk = std::stoi(arg[1].c_str());
node_reported_dsk[1] = (dsk)&0xff;
node_reported_dsk[0] = (dsk >> 8) & 0xff;
return zwave_network_management_dsk_set(node_reported_dsk);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_attribute_store_log_network(const handle_args_t &arg)
{
try {
if (arg.size() == 2) {
// Parse the NodeID and get its node.
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
unid_t unid;
zwave_unid_from_node_id(node_id, unid);
attribute_store_node_t node_to_print
= attribute_store_network_helper_get_node_id_node(unid);
return unify_stdin_attribute_store_log_node(node_to_print, true);
} else {
return unify_stdin_attribute_store_log_node(attribute_store_get_root(),
true);
}
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_initiate_interview(const handle_args_t &arg)
{
if (arg.size() <= 1) {
dprintf(out_stream,
"Error: this command requires at least 1 argument (NodeID)\n");
return SL_STATUS_FAIL;
}
try {
// For sure we have a NodeID parameter:
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
unid_t node_unid;
zwave_unid_from_node_id(node_id, node_unid);
if (arg.size() == 2) {
return ucl_mqtt_initiate_node_interview(node_unid);
} else {
zwave_endpoint_id_t endpoint_id = static_cast<zwave_endpoint_id_t>(
std::stoi(arg[2].c_str(), nullptr, 10));
return ucl_mqtt_initiate_endpoint_interview(node_unid, endpoint_id);
}
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t
handle_zwave_initiate_firmware_update(const handle_args_t &arg)
{
if (arg.size() <= 2) {
dprintf(out_stream,
"Error: this command requires at least 2 arguments "
"(NodeID and file name)\n");
return SL_STATUS_FAIL;
}
try {
// Parse the NodeID
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
// Wait 1 second before starting
unsigned long apply_after = get_current_utc_current_time() + 2;
return command_class_firmware_update_initiate_firmware_update(
node_id,
0, // Endpoint ID 0
0, // Firmware ID 0
apply_after,
arg[2].c_str());
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_zwave_abort_firmware_update(const handle_args_t &arg)
{
if (arg.size() <= 1) {
dprintf(out_stream,
"Error: this command requires at least 1 argument (NodeID)\n");
return SL_STATUS_FAIL;
}
try {
// Parse the NodeID
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
command_class_firmware_update_abort_ongoing_firmware_operation(node_id, 0);
return SL_STATUS_OK;
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s\n", e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t
handle_cc_versions_log([[maybe_unused]] const handle_args_t &arg)
{
zwave_command_handler_print_info(out_stream);
return SL_STATUS_OK;
}
// Notifying the user to choice which keys to grant --- during S2 inclusion
static void on_keys_report(bool csa, zwave_keyset_t keys)
{
dprintf(out_stream, "Requested Key Set : 0x%x\n", keys);
dprintf(out_stream, "Requested Key Classes: \n");
if (keys & ZWAVE_CONTROLLER_S2_ACCESS_KEY) {
dprintf(out_stream, " S2_ACCESS \n");
}
if (keys & ZWAVE_CONTROLLER_S2_AUTHENTICATED_KEY) {
dprintf(out_stream, " S2_AUTHENTICATED \n");
}
if (keys & ZWAVE_CONTROLLER_S2_UNAUTHENTICATED_KEY) {
dprintf(out_stream, " S2_UNAUTHENTICATED \n");
}
if (keys & ZWAVE_CONTROLLER_S0_KEY) {
dprintf(out_stream, " SECURITY_0 \n");
}
if (csa) {
dprintf(out_stream, "Client Side Authentication is requested \n");
} else {
dprintf(out_stream, "Client Side Authentication is not requested \n");
}
dprintf(out_stream,
"Please accept the security bootstrapping and select which requested "
"keys to grant\n");
dprintf(out_stream,
"Usage: zwave_grant_keys <1/0 to accept/reject the requested "
"keys>,<Set of keys to grant in Hexadecimal>,<1 if csa requested>\n");
dprintf(out_stream,
"zwave_grant_keys 1, 87 (to grant all keys and accept "
"the security bootstrapping)\n");
}
// Requesting a user to insert the missing part of the DSK if the inclusion
// needs authentication. If the inclusion is unauthenticated, ZPC does not
// notify the user about the received DSK, rather the ZPC will accept the received DSK.
static void
on_dsk_report(uint8_t input_length, zwave_dsk_t dsk, zwave_keyset_t keys)
{
if ((keys & ZWAVE_CONTROLLER_S2_ACCESS_KEY)
|| (keys & ZWAVE_CONTROLLER_S2_AUTHENTICATED_KEY)) {
char dsk_string[DSK_STR_LEN];
zpc_converters_dsk_to_str(dsk, dsk_string, DSK_STR_LEN);
dprintf(out_stream, "Received DSK: %s \n", dsk_string);
dprintf(
out_stream,
"Please verify the DSK and insert the missing part (first 5 digits)\n");
dprintf(out_stream,
"Usage: zwave_accept_dsk "
"<insert the first two byte of the DSK in [decimal]>\n");
memcpy(node_reported_dsk, dsk, sizeof(zwave_dsk_t));
}
}
static sl_status_t handle_enable_nls(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args: <NodeID>"
"For example zwave_enable_nls 02\n");
return SL_STATUS_FAIL;
}
try {
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
return zwave_store_nls_state(node_id, true, DESIRED_ATTRIBUTE);
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s: Invalid argument: %s\n", arg[0].c_str(), e.what());
return SL_STATUS_FAIL;
}
}
static sl_status_t handle_get_nls_state(const handle_args_t &arg)
{
if (arg.size() != 2) {
dprintf(out_stream,
"Invalid number of arguments, expected args: <NodeID>"
"For example zwave_get_nls_state 02\n");
return SL_STATUS_FAIL;
}
try {
zwave_node_id_t node_id
= static_cast<zwave_node_id_t>(std::stoi(arg[1].c_str(), nullptr, 10));
uint8_t nls_state = 0;
uint8_t nls_support = 0;
sl_status_t status = zwapi_get_node_nls(node_id, &nls_state, &nls_support);
if (SL_STATUS_OK == status)
{
status = zwave_store_nls_state(node_id, nls_state, REPORTED_ATTRIBUTE) || zwave_store_nls_support(node_id, nls_support, REPORTED_ATTRIBUTE);
if (SL_STATUS_OK != status) {
dprintf(out_stream, "Unable to store NLS state for Node ID: %d\n", node_id);
return SL_STATUS_FAIL;
}
dprintf(out_stream, "Node ID %d, NLS Support: %d, NLS state: %d\n", node_id, nls_support, nls_state);
return SL_STATUS_OK;
}
else
{
dprintf(out_stream,
"Unable to read NLS state for Node ID: %d\n",
node_id);
return SL_STATUS_FAIL;
}
} catch (const std::invalid_argument &e) {
dprintf(out_stream, "%s: Invalid argument: %s\n", arg[0].c_str(), e.what());
return SL_STATUS_FAIL;
}
}
static zwave_controller_callbacks_t stdin_zwave_controller_cb = {
.on_keys_report = on_keys_report,
.on_dsk_report = on_dsk_report,
};
// For test functionality
zwave_controller_callbacks_t get_zpc_stdin_callbacks()
{
return stdin_zwave_controller_cb;
}
sl_status_t zpc_stdin_command_handling_init()
{
out_stream = uic_stdin_get_output_fd();
// Register zwave_controller_callbacks
zwave_controller_register_callbacks(&stdin_zwave_controller_cb);
return SL_STATUS_OK;
}