forked from SiliconLabsSoftware/matter_sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1639 lines (1447 loc) · 50.5 KB
/
main.cpp
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
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// #include "FreeRTOS.h"
// #include "task.h"
#include <lib/shell/Engine.h>
#include <app/server/OnboardingCodesUtil.h>
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/SetupPayload.h>
#include <lib/core/CHIPCore.h>
#include <lib/support/Base64.h>
#include <lib/support/CHIPArgParser.hpp>
#include <lib/support/CodeUtils.h>
// #include <lib/support/RandUtils.h> //==> rm from TE7.5
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/server/Dnssd.h>
#include <app/server/Server.h>
#include <app/util/af-types.h>
#include <app/util/attribute-storage.h>
#include <app/util/attribute-table.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <static-supported-modes-manager.h>
#include <static-supported-temperature-levels.h>
#include <app/InteractionModelEngine.h>
#include <ChipShellCollection.h>
// cr++
#if (defined(CONFIG_CHIP_MW320_REAL_FACTORY_DATA) && (CONFIG_CHIP_MW320_REAL_FACTORY_DATA == 1))
#include "FactoryDataProvider.h"
#else
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#endif // if CONFIG_CHIP_MW320_REAL_FACTORY_DATA
// cr--
// ota++
#include "app/clusters/ota-requestor/BDXDownloader.h"
#include "app/clusters/ota-requestor/DefaultOTARequestor.h"
#include "app/clusters/ota-requestor/DefaultOTARequestorDriver.h"
#include "app/clusters/ota-requestor/DefaultOTARequestorStorage.h"
// #include <app/clusters/ota-requestor/DefaultOTARequestorUserConsent.h>
#include "platform/nxp/mw320/OTAImageProcessorImpl.h"
// #include "app/clusters/ota-requestor/OTARequestorDriver.h"
// for ota module test
#include "mw320_ota.h"
// ota--
#include "app/clusters/bindings/BindingManager.h"
#include "binding-handler.h"
/* platform specific */
#include "board.h"
#include "clock_config.h"
#include "fsl_debug_console.h"
#include "fsl_gpio.h"
#include "pin_mux.h"
#include <wm_os.h>
extern "C" {
#include "boot_flags.h"
#include "cli.h"
#include "dhcp-server.h"
#include "iperf.h"
#include "mflash_drv.h"
#include "network_flash_storage.h"
#include "partition.h"
#include "ping.h"
#include "wlan.h"
#include "wm_net.h"
}
#include "fsl_aes.h"
#include "lpm.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define APP_AES AES
#define CONNECTION_INFO_FILENAME "connection_info.dat"
#define SSID_FNAME "ssid_fname"
#define PSK_FNAME "psk_fname"
#define VERSION_STR "mw320-2.9.10-005"
enum
{
MCUXPRESSO_WIFI_CLI,
MATTER_SHELL,
MAX_SELECTION,
};
static int Matter_Selection = MAX_SELECTION;
#define RUN_RST_LT_DELAY 10
static const char TAG[] = "mw320";
/*******************************************************************************
* Variables
******************************************************************************/
static SemaphoreHandle_t aesLock;
static struct wlan_network sta_network;
static struct wlan_network uap_network;
chip::app::Clusters::TemperatureControl::AppSupportedTemperatureLevelsDelegate sAppSupportedTemperatureLevelsDelegate;
chip::app::Clusters::ModeSelect::StaticSupportedModesManager sStaticSupportedModesManager;
const int TASK_MAIN_PRIO = OS_PRIO_3;
const int TASK_MAIN_STACK_SIZE = 800;
portSTACK_TYPE * task_main_stack = NULL;
TaskHandle_t task_main_task_handler;
uint8_t * __FACTORY_DATA_START;
uint32_t __FACTORY_DATA_SIZE;
#if CHIP_ENABLE_OPENTHREAD
extern "C" {
#include <openthread/platform/platform-softdevice.h>
}
#endif // CHIP_ENABLE_OPENTHREAD
using namespace chip;
using namespace chip::Credentials;
using namespace ::chip::app;
using namespace chip::Shell;
using namespace chip::DeviceLayer;
// ota ++
using chip::BDXDownloader;
using chip::DefaultOTARequestor;
using chip::OTADownloader;
using chip::OTAImageProcessorImpl;
using chip::OTAImageProgress;
DefaultOTARequestor gRequestorCore;
DefaultOTARequestorStorage gRequestorStorage;
chip::DeviceLayer::DefaultOTARequestorDriver gRequestorUser;
BDXDownloader gDownloader;
OTAImageProcessorImpl gImageProcessor;
// chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider;
// static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kGranted;
void InitOTARequestor(void)
{
// Initialize and interconnect the Requestor and Image Processor objects -- START
SetRequestorInstance(&gRequestorCore);
gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
// Set server instance used for session establishment
gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader);
// WARNING: this is probably not realistic to know such details of the image or to even have an OTADownloader instantiated at
// the beginning of program execution. We're using hardcoded values here for now since this is a reference application.
// TODO: instatiate and initialize these values when QueryImageResponse tells us an image is available
// TODO: add API for OTARequestor to pass QueryImageResponse info to the application to use for OTADownloader init
// OTAImageProcessor ipParams;
// ipParams.imageFile = "dnld_img.txt"_span;
// gImageProcessor.SetOTAImageProcessorParams(ipParams);
gImageProcessor.SetOTADownloader(&gDownloader);
// Connect the Downloader and Image Processor objects
gDownloader.SetImageProcessorDelegate(&gImageProcessor);
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);
/*
if (gUserConsentState != chip::ota::UserConsentState::kUnknown)
{
gUserConsentProvider.SetUserConsentState(gUserConsentState);
gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider);
}
*/
// Initialize and interconnect the Requestor and Image Processor objects -- END
}
const char * mw320_get_verstr(void)
{
return VERSION_STR;
}
void save_network(char * ssid, char * pwd);
void save_network(char * ssid, char * pwd)
{
int ret;
ret = save_wifi_network((char *) SSID_FNAME, (uint8_t *) ssid, strlen(ssid) + 1);
if (ret != WM_SUCCESS)
{
PRINTF("Error: write ssid to flash failed\r\n");
}
ret = save_wifi_network((char *) PSK_FNAME, (uint8_t *) pwd, strlen(pwd) + 1);
if (ret != WM_SUCCESS)
{
PRINTF("Error: write psk to flash failed\r\n");
}
return;
}
// ota --
namespace {
static void rst_args_lt(System::Layer * aSystemLayer, void * aAppState);
}
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
volatile int g_ButtonPress = 0;
bool need2sync_sw_attr = false;
void sw2_handle(bool frm_clk)
{
static uint8_t click_cnt = 0;
static uint8_t run_times = 0;
if (frm_clk == true)
{
// Called while user clicks the button
click_cnt++;
PRINTF(" (%d times) \r\n", click_cnt);
return;
}
// Called regularlly from a thread every 500ms
run_times++;
if (click_cnt > 4)
{
// More than 4 clicks within the last second => erase the saved parameters
PRINTF("--> enough clicks (%d times) => resetting the saved parameters \r\n", click_cnt);
::erase_all_params();
DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(RUN_RST_LT_DELAY), rst_args_lt, nullptr);
click_cnt = 0;
}
if (run_times >= 2)
{
// Called twice with gap==500ms
click_cnt = 0;
run_times = 0;
}
return;
}
void GPIO_IRQHandler(void)
{
uint32_t intrval = GPIO_PortGetInterruptFlags(GPIO, GPIO_PORT(BOARD_SW1_GPIO_PIN));
// Clear the interrupt
GPIO_PortClearInterruptFlags(GPIO, GPIO_PORT(BOARD_SW1_GPIO_PIN), intrval);
// Check which sw tiggers the interrupt
if (intrval & 1UL << GPIO_PORT_PIN(BOARD_SW1_GPIO_PIN))
{
PRINTF("SW_1 click => do switch handler\r\n");
/* Change state of button. */
g_ButtonPress++;
need2sync_sw_attr = true;
}
else if (intrval & 1UL << GPIO_PORT_PIN(BOARD_SW2_GPIO_PIN))
{
PRINTF("SW_2 click \r\n");
sw2_handle(true);
}
SDK_ISR_EXIT_BARRIER;
}
#if defined(__cplusplus)
}
#endif /* __cplusplus */
/*
Protocols::InteractionModel::Status emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint16_t manufacturerCode,
uint8_t * buffer, uint16_t maxReadLength, int32_t index)
{
PRINTF("====> emberAfExternalAttributeReadCallback\r\n");
if(clusterId == Clusters::Switch::Id) {
*buffer = g_ButtonPress;
}
return Protocols::InteractionModel::Status::Success;
}
*/
namespace {
typedef enum
{
chip_srv_all,
dns_srv,
srv_type_max
} srv_type_t;
typedef enum
{
led_yellow,
led_amber,
led_max
} led_id_t;
static void run_chip_srv(System::Layer * aSystemLayer, void * aAppState);
static void run_dnssrv(System::Layer * aSystemLayer, void * aAppState);
static void run_update_chipsrv(srv_type_t srv_type);
static void led_on_off(led_id_t lt_id, bool is_on);
bool is_connected = false;
/*******************************************************************************
* Prototypes
******************************************************************************/
static void load_network(char * ssid, char * pwd);
/*
static void saveProfile(int argc, char **argv);
static void loadProfile(int argc, char **argv);
static void resetProfile(int argc, char **argv);
static void wlanIeeePowerSave(int argc, char **argv);
static void wlanDeepSleep(int argc, char **argv);
static void mcuPowerMode(int argc, char **argv);
static struct cli_command saveload[] = {
{"save-profile", "<profile_name>", saveProfile},
{"load-profile", NULL, loadProfile},
{"reset-profile", NULL, resetProfile},
};
static struct cli_command wlanPower[] = {
{"wlan-ieee-power-save", "<on/off> <wakeup condition>", wlanIeeePowerSave},
{"wlan-deepsleep", "<on/off>", wlanDeepSleep},
};
static struct cli_command mcuPower[] = {
{"mcu-power-mode", "<pm0/pm1/pm2/pm4> [<pm2_io_exclude_mask>]", mcuPowerMode},
};
*/
TaskHandle_t sShellTaskHandle;
/*******************************************************************************
* Code
******************************************************************************/
static status_t APP_AES_Lock(void)
{
if (pdTRUE == xSemaphoreTakeRecursive(aesLock, portMAX_DELAY))
{
return kStatus_Success;
}
else
{
return kStatus_Fail;
}
}
static void APP_AES_Unlock(void)
{
xSemaphoreGiveRecursive(aesLock);
}
static void load_network(char * ssid, char * pwd)
{
int ret;
unsigned char ssid_buf[IEEEtypes_SSID_SIZE + 1];
unsigned char psk_buf[WLAN_PSK_MAX_LENGTH];
uint32_t len;
len = IEEEtypes_SSID_SIZE + 1;
ret = get_saved_wifi_network((char *) SSID_FNAME, ssid_buf, &len);
if (ret != WM_SUCCESS)
{
PRINTF("Error: Read saved SSID\r\n");
strcpy(ssid, "");
}
else
{
PRINTF("saved_ssid: [%s]\r\n", ssid_buf);
strcpy(ssid, (const char *) ssid_buf);
}
len = WLAN_PSK_MAX_LENGTH;
ret = get_saved_wifi_network((char *) PSK_FNAME, psk_buf, &len);
if (ret != WM_SUCCESS)
{
PRINTF("Error: Read saved PSK\r\n");
strcpy(pwd, "");
}
else
{
PRINTF("saved_psk: [%s]\r\n", psk_buf);
strcpy(pwd, (const char *) psk_buf);
}
}
/*
static void saveProfile(int argc, char **argv)
{
int ret;
struct wlan_network network;
if (argc < 2)
{
PRINTF("Usage: %s <profile_name>\r\n", argv[0]);
PRINTF("Error: specify network to save\r\n");
return;
}
ret = wlan_get_network_byname(argv[1], &network);
if (ret != WM_SUCCESS)
{
PRINTF("Error: network not found\r\n");
}
else
{
ret = save_wifi_network((char *)CONNECTION_INFO_FILENAME, (uint8_t *)&network, sizeof(network));
if (ret != WM_SUCCESS)
{
PRINTF("Error: write network to flash failed\r\n");
}
}
}
static void loadProfile(int argc, char **argv)
{
int ret;
struct wlan_network network;
uint32_t len = sizeof(network);
ret = get_saved_wifi_network((char *)CONNECTION_INFO_FILENAME, (uint8_t *)&network, &len);
if (ret != WM_SUCCESS || len != sizeof(network))
{
PRINTF("Error: No network saved\r\n");
}
else
{
ret = wlan_add_network(&network);
if (ret != WM_SUCCESS)
{
PRINTF("Error: network data corrupted or network already added\r\n");
}
}
}
static void resetProfile(int argc, char **argv)
{
int ret;
ret = reset_saved_wifi_network((char *)CONNECTION_INFO_FILENAME);
if (ret != WM_SUCCESS)
{
PRINTF("Error: Reset profile failed\r\n");
}
}
static void wlanIeeePowerSave(int argc, char **argv)
{
bool on = false;
bool off = false;
uint32_t cond;
int ret;
if (argc >= 2)
{
on = (strcmp(argv[1], "on") == 0);
off = (strcmp(argv[1], "off") == 0);
}
if ((argc < 2) || (!on && !off) || (on && argc < 3))
{
PRINTF("Usage: %s <on/off> [<wakeup condition>]\r\n", argv[0]);
PRINTF(" wakeup condictions needed by \"on\" command:\r\n");
PRINTF(" bit0=1: broadcast data\r\n");
PRINTF(" bit1=1: unicast data\r\n");
PRINTF(" bit2=1: mac events\r\n");
PRINTF(" bit3=1: multicast data\r\n");
PRINTF(" bit4=1: arp broadcast data\r\n");
PRINTF(" bit6=1: management frame\r\n");
return;
}
if (on)
{
cond = strtoul(argv[2], NULL, 0);
ret = wlan_ieeeps_on(cond);
}
else
{
ret = wlan_ieeeps_off();
}
if (ret != WM_SUCCESS)
{
PRINTF("Cannot request IEEE power save mode change!\r\n");
}
else
{
PRINTF("IEEE power save mode change requested!\r\n");
}
}
static void wlanDeepSleep(int argc, char **argv)
{
bool on;
int ret;
if ((argc < 2) || ((strcmp(argv[1], "on") != 0) && (strcmp(argv[1], "off") != 0)))
{
PRINTF("Usage: %s <on/off>\r\n", argv[0]);
PRINTF("Error: specify deep sleep on or off.\r\n");
return;
}
on = (strcmp(argv[1], "on") == 0);
if (on)
{
ret = wlan_deepsleepps_on();
}
else
{
ret = wlan_deepsleepps_off();
}
if (ret != WM_SUCCESS)
{
PRINTF("Cannot request deep sleep mode change!\r\n");
}
else
{
PRINTF("Deep sleep mode change requested!\r\n");
}
}
static void mcuPowerMode(int argc, char **argv)
{
uint32_t excludeIo = 0U;
if ((argc < 2) || (strlen(argv[1]) != 3) || (argv[1][0] != 'p') || (argv[1][1] != 'm') || (argv[1][2] < '0') ||
(argv[1][2] > '4') || (argv[1][2] == '3'))
{
PRINTF("Usage: %s <pm0/pm1/pm2/pm4> [<pm2_io_exclude_mask>]\r\n", argv[0]);
PRINTF(" pm2_io_exclude_mask: bitmask of io domains to keep on in PM2.\r\n");
PRINTF(" e.g. 0x5 means VDDIO0 and VDDIO2 will not be powered off in PM2\r\n");
PRINTF("Error: specify power mode to enter.\r\n");
return;
}
if (argv[1][2] - '0' == 2U)
{
if (argc < 3)
{
PRINTF("Error: PM2 need 3rd parameter.\r\n");
return;
}
else
{
excludeIo = strtoul(argv[2], NULL, 0);
}
}
LPM_SetPowerMode(argv[1][2] - '0', excludeIo);
}
*/
static void mcuInitPower(void)
{
lpm_config_t config = {
/* System PM2/PM3 less than 50 ms will be skipped. */
.threshold = 50U,
/* SFLL config and RC32M setup takes approx 14 ms. */
.latency = 15U,
.enableWakeupPin0 = true,
.enableWakeupPin1 = true,
.handler = NULL,
};
LPM_Init(&config);
}
/* Callback Function passed to WLAN Connection Manager. The callback function
* gets called when there are WLAN Events that need to be handled by the
* application.
*/
int wlan_event_callback(enum wlan_event_reason reason, void * data)
{
int ret;
struct wlan_ip_config addr;
char ip[16];
static int auth_fail = 0;
// PRINTF("[%s] WLAN: received event %d\r\n", __FUNCTION__, reason);
switch (reason)
{
case WLAN_REASON_INITIALIZED:
// PRINTF("app_cb: WLAN initialized\r\n");
#ifdef MCUXPRESSO_WIFI_CLI
ret = wlan_basic_cli_init();
if (ret != WM_SUCCESS)
{
PRINTF("Failed to initialize BASIC WLAN CLIs\r\n");
return 0;
}
ret = wlan_cli_init();
if (ret != WM_SUCCESS)
{
PRINTF("Failed to initialize WLAN CLIs\r\n");
return 0;
}
PRINTF("WLAN CLIs are initialized\r\n");
ret = ping_cli_init();
if (ret != WM_SUCCESS)
{
PRINTF("Failed to initialize PING CLI\r\n");
return 0;
}
ret = iperf_cli_init();
if (ret != WM_SUCCESS)
{
PRINTF("Failed to initialize IPERF CLI\r\n");
return 0;
}
#endif
ret = dhcpd_cli_init();
if (ret != WM_SUCCESS)
{
// PRINTF("Failed to initialize DHCP Server CLI\r\n");
return 0;
}
#ifdef MCUXPRESSO_WIFI_CLI
if (cli_register_commands(saveload, sizeof(saveload) / sizeof(struct cli_command)))
{
return -WM_FAIL;
}
if (cli_register_commands(wlanPower, sizeof(wlanPower) / sizeof(struct cli_command)))
{
return -WM_FAIL;
}
if (cli_register_commands(mcuPower, sizeof(mcuPower) / sizeof(struct cli_command)))
{
return -WM_FAIL;
}
PRINTF("CLIs Available:\r\n");
if (Matter_Selection == MCUXPRESSO_WIFI_CLI)
help_command(0, NULL);
#endif
break;
case WLAN_REASON_INITIALIZATION_FAILED:
// PRINTF("app_cb: WLAN: initialization failed\r\n");
break;
case WLAN_REASON_SUCCESS:
// PRINTF("app_cb: WLAN: connected to network\r\n");
ret = wlan_get_address(&addr);
if (ret != WM_SUCCESS)
{
// PRINTF("failed to get IP address\r\n");
return 0;
}
net_inet_ntoa(addr.ipv4.address, ip);
ret = wlan_get_current_network(&sta_network);
if (ret != WM_SUCCESS)
{
// PRINTF("Failed to get External AP network\r\n");
return 0;
}
PRINTF("Connected to following BSS:\r\n");
PRINTF("SSID = [%s], IP = [%s]\r\n", sta_network.ssid, ip);
save_network(sta_network.ssid, sta_network.security.psk);
#ifdef CONFIG_IPV6
{
int i;
(void) PRINTF("\r\n\tIPv6 Addresses\r\n");
for (i = 0; i < MAX_IPV6_ADDRESSES; i++)
{
if (sta_network.ip.ipv6[i].addr_state != IP6_ADDR_INVALID)
{
(void) PRINTF("\t%-13s:\t%s (%s)\r\n", ipv6_addr_type_to_desc(&(sta_network.ip.ipv6[i])),
inet6_ntoa(sta_network.ip.ipv6[i].address),
ipv6_addr_state_to_desc(sta_network.ip.ipv6[i].addr_state));
}
}
(void) PRINTF("\r\n");
}
#endif
auth_fail = 0;
is_connected = true;
run_update_chipsrv(dns_srv);
if (is_uap_started())
{
wlan_get_current_uap_network(&uap_network);
ret = wlan_stop_network(uap_network.name);
/* if (ret != WM_SUCCESS)
PRINTF("Error: unable to stop network\r\n");
else
PRINTF("stop uAP, SSID = [%s]\r\n", uap_network.ssid);
*/
}
break;
case WLAN_REASON_CONNECT_FAILED:
// PRINTF("app_cb: WLAN: connect failed\r\n");
break;
case WLAN_REASON_NETWORK_NOT_FOUND:
// PRINTF("app_cb: WLAN: network not found\r\n");
break;
case WLAN_REASON_NETWORK_AUTH_FAILED:
// PRINTF("app_cb: WLAN: network authentication failed\r\n");
auth_fail++;
if (auth_fail >= 3)
{
// PRINTF("Authentication Failed. Disconnecting ... \r\n");
wlan_disconnect();
auth_fail = 0;
}
break;
case WLAN_REASON_ADDRESS_SUCCESS:
// PRINTF("network mgr: DHCP new lease\r\n");
break;
case WLAN_REASON_ADDRESS_FAILED:
// PRINTF("app_cb: failed to obtain an IP address\r\n");
break;
case WLAN_REASON_USER_DISCONNECT:
// PRINTF("app_cb: disconnected\r\n");
auth_fail = 0;
break;
case WLAN_REASON_LINK_LOST:
is_connected = false;
run_update_chipsrv(dns_srv);
// PRINTF("app_cb: WLAN: link lost\r\n");
break;
case WLAN_REASON_CHAN_SWITCH:
// PRINTF("app_cb: WLAN: channel switch\r\n");
break;
case WLAN_REASON_UAP_SUCCESS:
// PRINTF("app_cb: WLAN: UAP Started\r\n");
ret = wlan_get_current_uap_network(&uap_network);
if (ret != WM_SUCCESS)
{
PRINTF("Failed to get Soft AP network\r\n");
return 0;
}
// PRINTF("Soft AP \"%s\" started successfully\r\n", uap_network.ssid);
if (dhcp_server_start(net_get_uap_handle()))
PRINTF("Error in starting dhcp server\r\n");
// PRINTF("DHCP Server started successfully\r\n");
break;
case WLAN_REASON_UAP_CLIENT_ASSOC:
PRINTF("app_cb: WLAN: UAP a Client Associated\r\n");
// PRINTF("Client => ");
// print_mac((const char *)data);
// PRINTF("Associated with Soft AP\r\n");
break;
case WLAN_REASON_UAP_CLIENT_DISSOC:
// PRINTF("app_cb: WLAN: UAP a Client Dissociated\r\n");
// PRINTF("Client => ");
// print_mac((const char *)data);
// PRINTF("Dis-Associated from Soft AP\r\n");
break;
case WLAN_REASON_UAP_STOPPED:
// PRINTF("app_cb: WLAN: UAP Stopped\r\n");
// PRINTF("Soft AP \"%s\" stopped successfully\r\n", uap_network.ssid);
dhcp_server_stop();
// PRINTF("DHCP Server stopped successfully\r\n");
break;
case WLAN_REASON_PS_ENTER:
// PRINTF("app_cb: WLAN: PS_ENTER\r\n");
break;
case WLAN_REASON_PS_EXIT:
// PRINTF("app_cb: WLAN: PS EXIT\r\n");
break;
default:
PRINTF("app_cb: WLAN: Unknown Event: %d\r\n", reason);
}
return 0;
}
#if 0
char profile[8] = "mw320";
char ssid[32] = "matter_mw320";
char psk[64] = "12345678";
char network_ip[15] = "192.168.2.1";
char network_netmask[15] = "255.255.255.0";
const uint8_t kOptionalDefaultStringTag1 = 1;
const uint8_t kOptionalDefaultStringTag2 = 2;
const uint8_t kOptionalDefaultStringTag3 = 3;
std::string createSetupPayload()
{
CHIP_ERROR err = CHIP_NO_ERROR;
std::string result;
std::string kOptionalDefaultStringValue1 = "IP:";
std::string kOptionalDefaultStringValue2 = "SSID:";
std::string kOptionalDefaultStringValue3 = "Key:";
uint16_t discriminator;
kOptionalDefaultStringValue1.append( network_ip, sizeof(network_ip) );
kOptionalDefaultStringValue2.append( ssid, sizeof(ssid) );
kOptionalDefaultStringValue3.append( psk, sizeof(psk) );
err = ConfigurationMgr().GetSetupDiscriminator(discriminator);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't get discriminator: %s\r\n", __FUNCTION__, ErrorStr(err));
return result;
}
uint32_t setupPINCode;
err = ConfigurationMgr().GetSetupPinCode(setupPINCode);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't get setupPINCode: %s\r\n", __FUNCTION__, ErrorStr(err));
return result;
}
uint16_t vendorId;
err = ConfigurationMgr().GetVendorId(vendorId);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't get vendorId: %s\r\n", __FUNCTION__, ErrorStr(err));
return result;
}
uint16_t productId;
err = ConfigurationMgr().GetProductId(productId);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't get productId: %s\r\n", __FUNCTION__, ErrorStr(err));
return result;
}
SetupPayload payload;
payload.version = 0;
payload.discriminator = discriminator;
payload.setUpPINCode = setupPINCode;
payload.rendezvousInformation.SetValue(chip::RendezvousInformationFlag::kBLE);
payload.vendorID = vendorId;
payload.productID = productId;
err = payload.addOptionalVendorData(kOptionalDefaultStringTag1, kOptionalDefaultStringValue1);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't add payload Vnedor string %d \r\n", __FUNCTION__, kOptionalDefaultStringTag1);
}
err = payload.addOptionalVendorData(kOptionalDefaultStringTag2, kOptionalDefaultStringValue2);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't add payload Vnedor string %d \r\n", __FUNCTION__, kOptionalDefaultStringTag2);
}
err = payload.addOptionalVendorData(kOptionalDefaultStringTag3, kOptionalDefaultStringValue3);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't add payload Vnedor string %d \r\n", __FUNCTION__, kOptionalDefaultStringTag3);
}
QRCodeSetupPayloadGenerator generator(payload);
size_t tlvDataLen = sizeof(kOptionalDefaultStringValue1)+sizeof(kOptionalDefaultStringValue2)+sizeof(kOptionalDefaultStringValue3);
uint8_t tlvDataStart[tlvDataLen];
err = generator.payloadBase38Representation(result, tlvDataStart, tlvDataLen);
if (err != CHIP_NO_ERROR)
{
PRINTF("[%s]: Couldn't get payload string %d \r\n", __FUNCTION__, err);
}
return result;
}
#endif // 0
#if 0
void demo_init(void)
{
struct wlan_network network;
int ret = 0;
// add uAP profile
memset(&network, 0, sizeof(struct wlan_network));
memcpy(network.name, profile, strlen(profile));
memcpy(network.ssid, ssid, strlen(ssid));
network.channel = 1;
network.ip.ipv4.address = net_inet_aton(network_ip);
network.ip.ipv4.gw = net_inet_aton(network_ip);
network.ip.ipv4.netmask = net_inet_aton(network_netmask);
network.ip.ipv4.addr_type = ADDR_TYPE_STATIC;
network.security.psk_len = strlen(psk);
strcpy(network.security.psk, psk);
network.security.type = WLAN_SECURITY_WPA2;
network.role = WLAN_BSS_ROLE_UAP;
ret = wlan_add_network(&network);
switch (ret)
{
case WM_SUCCESS:
PRINTF("Added \"%s\"\r\n", network.name);
break;
case -WM_E_INVAL:
PRINTF("Error: network already exists or invalid arguments\r\n");
break;
case -WM_E_NOMEM:
PRINTF("Error: network list is full\r\n");
break;
case WLAN_ERROR_STATE:
PRINTF("Error: can't add networks in this state\r\n");
break;
default:
PRINTF(
"Error: unable to add network for unknown"
" reason\r\n");
break;
}
// start uAP
ret = wlan_start_network(profile);
if (ret != WM_SUCCESS)
PRINTF("Error: unable to start network\r\n");
else
PRINTF("start uAP ssid: %s\r\n", network.ssid);
}
#endif // 0
void task_main(void * param)
{
#if 0
int32_t result = 0;
flash_desc_t fl;
struct partition_entry *p, *f1, *f2;
short history = 0;
uint32_t *wififw;
#ifdef CONFIG_USE_PSM
struct partition_entry *psm;
#endif
mcuInitPower();
boot_init();
mflash_drv_init();
PRINTF("[%s]: Initialize CLI\r\n", __FUNCTION__);
result = cli_init();
if (WM_SUCCESS != result)
{
assert(false);
}
PRINTF("[%s]: Initialize WLAN Driver\r\n", __FUNCTION__);
result = part_init();
if (WM_SUCCESS != result)
{
assert(false);
}
#ifdef CONFIG_USE_PSM
psm = part_get_layout_by_id(FC_COMP_PSM, NULL);
part_to_flash_desc(psm, &fl);
#else
fl.fl_dev = 0U;
fl.fl_start = MFLASH_FILE_BASEADDR;
fl.fl_size = MFLASH_FILE_SIZE;
#endif
init_flash_storage((char *)CONNECTION_INFO_FILENAME, &fl);
f1 = part_get_layout_by_id(FC_COMP_WLAN_FW, &history);
f2 = part_get_layout_by_id(FC_COMP_WLAN_FW, &history);
if (f1 && f2)
{
p = part_get_active_partition(f1, f2);
}
else if (!f1 && f2)
{
p = f2;
}