-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.c
963 lines (794 loc) · 23.3 KB
/
test.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
/* Wirepas Oy licensed under Apache License, Version 2.0
*
* See file LICENSE for full license details.
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define LOG_MODULE_NAME "Test"
#define MAX_LOG_LEVEL INFO_LOG_LEVEL
#include "logger.h"
#include "wpc.h"
static int passedTestCount = 0;
static int failedTestCount = 0;
static bool setInitialState(app_role_t role,
app_addr_t id,
net_addr_t network_add,
net_channel_t network_channel,
bool start)
{
WPC_stop_stack();
// Wait for stack to stop
usleep(3 * 1000 * 1000);
if (WPC_set_node_address(id) != APP_RES_OK)
return false;
if (WPC_set_role(role) != APP_RES_OK)
return false;
if (WPC_set_network_address(network_add) != APP_RES_OK)
return false;
if (WPC_set_network_channel(network_channel) != APP_RES_OK)
return false;
if (start && (WPC_start_stack() == APP_RES_OK))
return false;
return true;
}
static bool testFactoryReset()
{
app_res_e res = WPC_stop_stack();
app_addr_t add;
if (res != APP_RES_STACK_ALREADY_STOPPED && res != APP_RES_OK)
{
LOGE("Cannot stop stack\n");
return false;
}
if (WPC_set_node_address(0x123456) != APP_RES_OK)
{
LOGE("Cannot set node address\n");
return false;
}
if (WPC_do_factory_reset() != APP_RES_OK)
{
LOGE("Cannot do factory reset\n");
return false;
}
// Check node address previously set is correctly removed
if (WPC_get_node_address(&add) != APP_RES_ATTRIBUTE_NOT_SET)
{
LOGE("Node address is still set\n");
return false;
}
return true;
}
static bool setAppconfig(uint8_t * config, uint16_t interval, uint8_t size)
{
uint8_t cur_seq, new_seq = 0;
uint16_t cur_interval;
uint8_t cur_config[128];
app_res_e res;
if (size > sizeof(cur_config))
{
LOGE("Specified size (%d) is too big.\n", size);
return false;
}
res = WPC_get_app_config_data(&cur_seq, &cur_interval, cur_config, sizeof(cur_config));
if (res == APP_RES_OK)
{
new_seq = cur_seq + 1;
}
else
{
LOGE("Cannot get current app config seq. Result = 0x%02x.\n", res);
return false;
}
res = WPC_set_app_config_data(new_seq, interval, config, size);
if (res != APP_RES_OK)
{
LOGE("Cannot set new app config with seq = %d. Result = 0x%02x.\n", new_seq, res);
return false;
}
// Read back App config
res = WPC_get_app_config_data(&cur_seq, &cur_interval, cur_config, sizeof(cur_config));
if (res != APP_RES_OK)
{
LOGE("Cannot read back new app config. Result = 0x%02x.\n", res);
return false;
}
if (memcmp(config, cur_config, size) != 0)
{
LOGE("App config set differs from app config read.\n");
return false;
}
return true;
}
static bool dumpCSAPAttributes()
{
uint8_t role;
net_channel_t channel;
app_addr_t node_address;
net_addr_t network;
uint8_t mtu, pdus, scratch_seq, first_channel, last_channel, data_size;
uint16_t firmware[4], api_v, hw_magic, stack_profile;
if (WPC_get_role(&role) == APP_RES_ATTRIBUTE_NOT_SET)
{
LOGW("Role not set\n");
role = 0;
}
if (WPC_get_node_address(&node_address) == APP_RES_ATTRIBUTE_NOT_SET)
{
LOGW("Node address not set\n");
node_address = 0;
}
if (WPC_get_network_address(&network) == APP_RES_ATTRIBUTE_NOT_SET)
{
LOGW("Network address not set\n");
network = 0;
}
if (WPC_get_network_channel(&channel) == APP_RES_ATTRIBUTE_NOT_SET)
{
LOGW("Network channel not set\n");
channel = 0;
}
LOGI("Role is 0x%02x / Add is %d / Network is 0x%06x (channel = %d)\n", role, node_address, network, channel);
WPC_get_mtu(&mtu);
LOGI("MTU size is %d\n", mtu);
WPC_get_pdu_buffer_size(&pdus);
LOGI("PDU size is %d\n", pdus);
WPC_get_scratchpad_sequence(&scratch_seq);
LOGI("Scratchpad seq is %d\n", scratch_seq);
WPC_get_mesh_API_version(&api_v);
LOGI("Mesh API version is %d\n", api_v);
WPC_get_firmware_version(firmware);
LOGI("Firmware version is %d:%d:%d:%d\n", firmware[0], firmware[1], firmware[2], firmware[3]);
WPC_get_channel_limits(&first_channel, &last_channel);
LOGI("Channel Limits are: [%d - %d]\n", first_channel, last_channel);
WPC_get_app_config_data_size(&data_size);
LOGI("Max config data size is %d\n", data_size);
WPC_get_hw_magic(&hw_magic);
LOGI("Radio hardware is %d\n", hw_magic);
WPC_get_stack_profile(&stack_profile);
LOGI("Stack profile is %d\n", stack_profile);
return true;
}
static bool testCipherKey()
{
bool key_set;
if (WPC_is_cipher_key_set(&key_set) != APP_RES_OK)
{
return false;
}
if (key_set)
{
LOGW("Key already set\n");
// return false
}
LOGI("No cipher key\n");
uint8_t key[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
LOGI("Set cipher key\n");
if (WPC_set_cipher_key(key) != APP_RES_OK)
{
LOGE("Cannot set key\n");
return false;
}
LOGI("Check cipher key set\n");
if (WPC_is_cipher_key_set(&key_set) != APP_RES_OK)
{
return false;
}
if (!key_set)
{
LOGE("Key is not set\n");
return false;
}
LOGI("Remove cipher key\n");
if (WPC_remove_cipher_key() != APP_RES_OK)
{
LOGE("Cannot remove key\n");
return false;
}
LOGI("Check cipher key not set\n");
if (WPC_is_cipher_key_set(&key_set) != APP_RES_OK)
{
return false;
}
if (key_set)
{
LOGE("Key is still set\n");
return false;
}
return true;
}
static bool testAuthenticationKey()
{
bool key_set;
if (WPC_is_authentication_key_set(&key_set) != APP_RES_OK)
{
return false;
}
if (key_set)
{
LOGW("Key already set\n");
// return false
}
LOGI("No authentication key\n");
uint8_t key[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
LOGI("Set authentication key\n");
if (WPC_set_authentication_key(key) != APP_RES_OK)
{
LOGE("Cannot set key\n");
return false;
}
LOGI("Check authentication key set\n");
if (WPC_is_authentication_key_set(&key_set) != APP_RES_OK)
{
return false;
}
if (!key_set)
{
LOGE("Key is not set\n");
return false;
}
LOGI("Remove authentication key\n");
if (WPC_remove_authentication_key() != APP_RES_OK)
{
LOGE("Cannot remove key\n");
return false;
}
LOGI("Check authentication key not set\n");
if (WPC_is_authentication_key_set(&key_set) != APP_RES_OK)
{
return false;
}
if (key_set)
{
LOGE("Key is still set\n");
return false;
}
return true;
}
static bool testScratchpadTarget()
{
uint8_t target_seq;
uint16_t target_crc;
uint8_t action;
uint8_t param;
// Read it a first time
if (WPC_read_target_scratchpad(&target_seq, &target_crc, &action, ¶m) != APP_RES_OK)
{
LOGE("Cannot read target scratchpad\n");
return false;
}
LOGI("Target is: %d, 0x%x, %d, %d\n", target_seq, target_crc, action, param);
app_res_e res = WPC_write_target_scratchpad(12, 0x1234, 2, 13);
if (res != APP_RES_OK)
{
LOGE("Cannot write target scratchpad %d\n", res);
return false;
}
LOGI("Write new target\n");
if (WPC_read_target_scratchpad(&target_seq, &target_crc, &action, ¶m))
{
LOGE("Cannot read target back scratchpad\n");
return false;
}
LOGI("Target read back is: %d, 0x%x, %d, %d\n", target_seq, target_crc, action, param);
if (target_seq != 12 || target_crc != 0x1234 || action != 2 || param != 13)
{
LOGE("Wrong read-back value\n");
return false;
}
return true;
}
void onDataSent(uint16_t pduid, uint32_t buffering_delay, uint8_t result)
{
LOGI("Indication received for %d, delay=%d, result=%d\n", pduid, buffering_delay, result);
}
static bool testSendWithCallbacks()
{
// Send 5 message to ourself
uint8_t data[] = "This is a test message #00 with ind\0";
for (int i = 0; i < 2; i++)
{
data[24] = i / 10 + 0x30;
data[25] = i % 10 + 0x30;
if (WPC_send_data(data, sizeof(data), i, APP_ADDR_ANYSINK, APP_QOS_HIGH, 50, 50, onDataSent, 0) !=
APP_RES_OK)
{
return false;
}
}
return true;
}
static bool testSendWithoutCallbacks()
{
// Send 5 message to ourself
uint8_t data[] = "This is a test message #00\0";
for (int i = 2; i < 4; i++)
{
data[24] = i / 10 + 0x30;
data[25] = i % 10 + 0x30;
if (WPC_send_data(data, sizeof(data), i, APP_ADDR_ANYSINK, APP_QOS_HIGH, 50, 50, NULL, 0) != APP_RES_OK)
{
return false;
}
}
return true;
}
static bool testSendWithInitialTime()
{
// Send 5 message to ourself
uint8_t data[] = "This is a test message #00\0";
for (int i = 4; i < 6; i++)
{
data[24] = i / 10 + 0x30;
data[25] = i % 10 + 0x30;
if (WPC_send_data(data, sizeof(data), i, APP_ADDR_ANYSINK, APP_QOS_HIGH, 50, 50, NULL, 300) !=
APP_RES_OK)
{
return false;
}
}
return true;
}
static void onAppConfigDataReceived(uint8_t seq, uint16_t interval, uint8_t * config_p)
{
LOGI("AppConfig received %d, %d, %s\n", seq, interval, config_p);
}
static bool testAppConfigData()
{
uint8_t new_config[128];
uint8_t max_size;
if (WPC_get_app_config_data_size(&max_size) != APP_RES_OK || (max_size > 128))
{
LOGE("Cannot get max app config size of bigger than 128 bytes\n");
return false;
}
for (uint8_t i = 0; i < max_size; i++)
{
new_config[i] = i;
}
if (!setAppconfig(new_config, 30, max_size))
{
LOGE("Cannot setAppConfig\n");
return false;
}
if (WPC_register_for_app_config_data(onAppConfigDataReceived) != APP_RES_OK)
{
LOGE("Cannot register for app_config\n");
return false;
}
return true;
}
static bool testMSAPAttributesStackOff()
{
uint16_t min, max;
// Access cycle
int res = WPC_get_access_cycle_range(&min, &max);
if (res != APP_RES_OK && res != APP_RES_ATTRIBUTE_NOT_SET)
{
LOGE("Cannot get access cycle range\n");
return false;
}
if (res != APP_RES_ATTRIBUTE_NOT_SET)
{
LOGI("Access cycle range is %d - %d\n", min, max);
}
if (WPC_set_access_cycle_range(4000, 4000) != APP_RES_OK)
{
LOGE("Cannot set cycle range\n");
return false;
}
else
{
if (WPC_get_access_cycle_range(&min, &max) != APP_RES_OK)
{
LOGE("Cannot get cycle range\n");
return false;
}
if (min != 4000 || max != 4000)
{
LOGE("Cannot read back access cycle range set\n");
return false;
}
}
LOGI("Cycle range is now %d - %d\n", min, max);
if (WPC_get_access_cycle_limits(&min, &max) != APP_RES_OK)
{
LOGE("Cannot get cycle limits\n");
return false;
}
LOGI("Cycle range limits is %d - %d\n", min, max);
return true;
}
static bool testMSAPAttributesStackOn()
{
app_res_e res;
uint8_t res8;
uint16_t res16;
uint32_t res32;
if (WPC_get_stack_status(&res8) != APP_RES_OK)
{
LOGE("Cannot get stack status\n");
return false;
}
LOGI("Stack status is %d\n", res8);
if (WPC_get_PDU_buffer_usage(&res8) != APP_RES_OK)
{
LOGE("Cannot get PDU buffer usage\n");
return false;
}
LOGI("PDU buffer usage is %d\n", res8);
if (WPC_get_PDU_buffer_capacity(&res8) != APP_RES_OK)
{
LOGE("Cannot get PDU buffer capacity\n");
return false;
}
LOGI("PDU buffer capacity is %d\n", res8);
// Battery remaining setting
res = WPC_get_remaining_energy(&res8);
if (res == APP_RES_INTERNAL_ERROR)
{
// Dualmcu API error code 1 while reading attributes
// means "Unsupported attribute id". That is mapped to
// APP_RES_INTERNAL_ERROR. Not ideal error code to
// indicate that the feature has been depricated or
// not implemented in this particular dualmcu implementation.
LOGI("Energy interface not implemented.\n");
}
else if (res != APP_RES_OK)
{
LOGE("Cannot get remaining battery usage. Result = 0x%02x.\n", res);
return false;
}
else
{
LOGI("Remaining battery is %d\n", res8);
if (WPC_set_remaining_energy(128) != APP_RES_OK)
{
LOGE("Cannot set remaining battery usage\n");
return false;
}
else
{
if (WPC_get_remaining_energy(&res8) != APP_RES_OK)
{
LOGE("Cannot get just set remaining battery usage\n");
return false;
}
if (res8 != 128)
{
LOGE("Expecting 128 but receive %d\n", res8);
return false;
}
}
LOGI("Able to set remaining battery to %d\n", res8);
}
if (WPC_get_autostart(&res8) != APP_RES_OK)
{
LOGE("Cannot get autostart setting\n");
return false;
}
LOGI("Autostart is %d\n", res8);
if (WPC_get_route_count(&res8) != APP_RES_OK)
{
LOGE("Cannot get route count\n");
return false;
}
LOGI("Route count is %d\n", res8);
if (WPC_get_system_time(&res32) != APP_RES_OK)
{
LOGE("Cannot get system time\n");
return false;
}
LOGI("System time is %d\n", res32);
if (WPC_get_current_access_cycle(&res16) != APP_RES_OK)
{
LOGE("Cannot get current access cycle\n");
return false;
}
LOGI("Current Access Cycle is %d\n", res16);
return true;
}
bool testClearScratchpad()
{
app_scratchpad_status_t status;
if (WPC_clear_local_scratchpad() != APP_RES_OK)
{
LOGE("Cannot clear local scratchpad\n");
return false;
}
if (WPC_get_local_scratchpad_status(&status) != APP_RES_OK)
{
LOGE("Cannot get scratchpad status\n");
return false;
}
if (status.scrat_len != 0)
{
LOGE("Scratchpad is not cleared\n");
return false;
}
return true;
}
#define BLOCK_SIZE 128
#define SEQ_NUMBER 50
#define OTAP_UPLOAD_FILE_PATH "otap_files/dummy.otap"
#define OTAP_DOWNLOAD_FILE_PATH "otap_files/downloaded.otap"
bool testUploadScratchpad()
{
FILE * fp;
app_scratchpad_status_t status;
long file_size = 0;
long written = 0;
const char * filename = OTAP_UPLOAD_FILE_PATH;
uint8_t block[BLOCK_SIZE];
fp = fopen(filename, "rb");
if (fp == NULL)
{
LOGE("Cannot open file %s. Please update OTAP_UPLOAD_FILE_PATH to a "
"valid "
"otap image\n",
filename);
return false;
}
/* Get size of binary */
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
if (file_size <= 0)
{
LOGE("Cannot determine file size\n");
return false;
}
LOGI("Uploading otap file of %d bytes\n", file_size);
/* Set cursor to beginning of file */
fseek(fp, 0L, SEEK_SET);
/* Start scratchpad update*/
if (WPC_start_local_scratchpad_update(file_size, SEQ_NUMBER) != APP_RES_OK)
{
LOGE("Cannot start scratchpad update\n");
return false;
}
/* Send scratchpad image block by block */
LOGI("Start sending otap (it may take up to 1 minute)\n");
long remaining = file_size;
while (remaining > 0)
{
uint8_t block_size = (remaining > BLOCK_SIZE) ? BLOCK_SIZE : remaining;
size_t read;
read = fread(block, 1, block_size, fp);
if (read != block_size)
{
LOGE("Error while reading file asked = %d read = %d\n", block_size, read);
break;
}
/* Send the block */
if (WPC_upload_local_block_scratchpad(block_size, block, written) != APP_RES_OK)
{
LOGE("Cannot upload scratchpad block\n");
break;
}
written += block_size;
remaining -= block_size;
}
if (remaining > 0)
{
return false;
}
if (WPC_get_local_scratchpad_status(&status) != APP_RES_OK)
{
LOGE("Cannot get scratchpad status\n");
return false;
}
if (status.scrat_len != file_size)
{
LOGE("Scratchpad is not uploaded correctly (wrong size) %d vs %d\n",
status.scrat_len,
file_size);
return false;
}
if (status.scrat_seq_number != SEQ_NUMBER)
{
LOGE("Wrong seq number after uploading a scratchpad image \n");
return false;
}
return true;
}
bool testDownloadScratchpad()
{
FILE * fp;
uint32_t scratchpad_size;
long read = 0;
const char * filename = OTAP_DOWNLOAD_FILE_PATH;
uint8_t block[BLOCK_SIZE];
fp = fopen(filename, "wb");
if (fp == NULL)
{
LOGE("Cannot open file %s. Please update OTAP_DOWNLOAD_FILE_PATH to a "
"valid path\n",
filename);
return false;
}
/* Get size of scratchpad */
if (WPC_get_scratchpad_size(&scratchpad_size) != APP_RES_OK)
{
LOGE("Cannot read scratchpad size\n");
return false;
}
LOGI("Downloading otap file of %d bytes\n", scratchpad_size);
/* Receive scratchpad image block by block */
LOGI("Start receiving otap (it may take up to 1 minute)\n");
uint32_t remaining = scratchpad_size;
while (remaining > 0)
{
uint8_t block_size = (remaining > BLOCK_SIZE) ? BLOCK_SIZE : remaining;
/* Receive the block */
const app_res_e download_res = WPC_download_local_scratchpad(block_size, block, read);
if (download_res != APP_RES_OK)
{
LOGE("Cannot download scratchpad block\n");
if (download_res == APP_RES_ACCESS_DENIED)
{
LOGE("Access denied when downloading scratchpad block. "
"Make sure the dual MCU application was built with scratchpad reading enabled.\n");
}
break;
}
size_t written = fwrite(block, 1, block_size, fp);
if (written != block_size)
{
LOGE("Error while writing file asked = %d read = %d\n", block_size, written);
break;
}
read += block_size;
remaining -= block_size;
}
fclose(fp);
if (remaining > 0)
{
return false;
}
LOGI("Please compare files %s and %s\n", OTAP_UPLOAD_FILE_PATH, OTAP_DOWNLOAD_FILE_PATH);
return true;
}
static bool scan_done = false;
static void onScanNeighborsDone(uint8_t status)
{
LOGI("Scan neighbors is done res=%d\n", status);
scan_done = true;
}
static bool testScanNeighbors()
{
app_nbors_t neighbors_list;
// Register for end of scan Neighbors
if (WPC_register_for_scan_neighbors_done(onScanNeighborsDone) != APP_RES_OK)
{
LOGE("Cannot register for remote status \n");
return false;
}
// Ask for a scan
if (WPC_start_scan_neighbors() != APP_RES_OK)
{
LOGE("Cannot start scan\n");
return false;
}
LOGI("Wait 5 seconds for scan result\n");
usleep(5 * 1000 * 1000);
// scan_done should be protected but we can assume
// that 5 sec is enough and no race can occur
if (!scan_done)
{
LOGE("Scan is not done\n");
return false;
}
if (WPC_get_neighbors(&neighbors_list) != APP_RES_OK)
{
LOGE("Cannot get neighbors list\n");
return false;
}
LOGI("Get neighbors done and node has %d neighbors\n", neighbors_list.number_of_neighbors);
if (neighbors_list.number_of_neighbors > 0)
{
LOGI("First node: %d, ch=%d, cost=%d, link=%d, type=%d, rssi=%d, "
"tx_power=%d, rx_power=%d, last_update=%d \n",
neighbors_list.nbors[0].add,
neighbors_list.nbors[0].channel,
neighbors_list.nbors[0].cost,
neighbors_list.nbors[0].link_rel,
neighbors_list.nbors[0].nbor_type,
neighbors_list.nbors[0].norm_rssi,
neighbors_list.nbors[0].tx_power,
neighbors_list.nbors[0].rx_power,
neighbors_list.nbors[0].last_update);
}
if (WPC_unregister_from_scan_neighbors_done() != APP_RES_OK)
{
LOGE("Cannot unregister from scan neighbors \n");
return false;
}
return true;
}
static void onStackStatusReceived(uint8_t status)
{
LOGI("Stack status received %d\n", status);
}
static bool testStackStatus()
{
if (WPC_register_for_stack_status(onStackStatusReceived) != APP_RES_OK)
{
return false;
}
return true;
}
// Macro to launch a test and check result
#define RUN_TEST(_test_func_, _expected_result_) \
do \
{ \
LOGI("### Starting test %s\n", #_test_func_); \
if (_test_func_() != _expected_result_) \
{ \
LOGE("### Test is FAIL\n\n"); \
failedTestCount++; \
} \
else \
{ \
LOGI("### Test is PASS\n\n"); \
passedTestCount++; \
} \
} while (0)
int GetPassedTestCount()
{
return passedTestCount;
}
int GetFailedTestCount()
{
return failedTestCount;
}
int Test_runAll()
{
uint8_t status;
RUN_TEST(testFactoryReset, true);
setInitialState(APP_ROLE_SINK, 1234, 0x654321, 5, false);
RUN_TEST(testScratchpadTarget, true);
RUN_TEST(dumpCSAPAttributes, true);
RUN_TEST(testAuthenticationKey, true);
RUN_TEST(testCipherKey, true);
RUN_TEST(testMSAPAttributesStackOff, true);
RUN_TEST(testStackStatus, true);
// Start the stack for following tests
WPC_start_stack();
// Ensure stack is started to avoid wrong test result
while (true)
{
if (WPC_get_stack_status(&status) != APP_RES_OK)
{
LOGI("Cannot read stack state\n");
continue;
}
if (status == 0)
{
break;
}
LOGI("Waiting for stack start\n");
}
RUN_TEST(testSendWithCallbacks, true);
RUN_TEST(testSendWithoutCallbacks, true);
RUN_TEST(testSendWithInitialTime, true);
RUN_TEST(testAppConfigData, true);
RUN_TEST(testMSAPAttributesStackOn, true);
RUN_TEST(testScanNeighbors, true);
return 0;
}
int Test_scratchpad()
{
// Configure node as a sink
setInitialState(APP_ROLE_SINK, 1234, 0x654321, 5, false);
// Set app config
setAppconfig((uint8_t *) "Test scratchpad", 1800, 14);
RUN_TEST(testClearScratchpad, true);
RUN_TEST(testUploadScratchpad, true);
RUN_TEST(testDownloadScratchpad, true);
// Start the stack for following tests
WPC_start_stack();
return 0;
}