-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathm2m_wifi.c
1496 lines (1396 loc) · 45.2 KB
/
m2m_wifi.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
/**
*
* \file
*
* \brief This module contains M2M Wi-Fi APIs implementation.
*
* Copyright (c) 2016-2017 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
#include "driver/include/m2m_wifi.h"
#include "driver/source/m2m_hif.h"
#include "driver/source/nmasic.h"
static volatile uint8 gu8ChNum;
static volatile uint8 gu8scanInProgress = 0;
static tpfAppWifiCb gpfAppWifiCb = NULL;
#ifdef ETH_MODE
static tpfAppEthCb gpfAppEthCb = NULL;
static uint8* gau8ethRcvBuf=NULL;
static uint16 gu16ethRcvBufSize ;
#endif
//#define CONF_MGMT
#ifdef CONF_MGMT
static tpfAppMonCb gpfAppMonCb = NULL;
static struct _tstrMgmtCtrl
{
uint8* pu8Buf;
uint16 u16Offset;
uint16 u16Sz;
}
gstrMgmtCtrl = {NULL, 0 , 0};
#endif
/**
* @fn m2m_wifi_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr, uint8 grp)
* @brief WiFi call back function
* @param [in] u8OpCode
* HIF Opcode type.
* @param [in] u16DataSize
* HIF data length.
* @param [in] u32Addr
* HIF address.
* @param [in] grp
* HIF group type.
* @author
* @date
* @version 1.0
*/
static void m2m_wifi_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr)
{
#ifdef ARDUINO
(void)u16DataSize; // Silence "unused" warning
#endif
uint8 rx_buf[8];
if (u8OpCode == M2M_WIFI_RESP_CON_STATE_CHANGED)
{
tstrM2mWifiStateChanged strState;
if (hif_receive(u32Addr, (uint8*) &strState,sizeof(tstrM2mWifiStateChanged), 0) == M2M_SUCCESS)
{
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_CON_STATE_CHANGED, &strState);
}
}
else if (u8OpCode == M2M_WIFI_RESP_GET_SYS_TIME)
{
tstrSystemTime strSysTime;
if (hif_receive(u32Addr, (uint8*) &strSysTime,sizeof(tstrSystemTime), 0) == M2M_SUCCESS)
{
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_GET_SYS_TIME, &strSysTime);
}
}
else if(u8OpCode == M2M_WIFI_RESP_CONN_INFO)
{
tstrM2MConnInfo strConnInfo;
if(hif_receive(u32Addr, (uint8*)&strConnInfo, sizeof(tstrM2MConnInfo), 1) == M2M_SUCCESS)
{
if(gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_CONN_INFO, &strConnInfo);
}
}
else if (u8OpCode == M2M_WIFI_RESP_MEMORY_RECOVER)
{
#if 0
if (hif_receive(u32Addr, rx_buf, 4, 1) == M2M_SUCCESS)
{
tstrM2mWifiStateChanged strState;
m2m_memcpy((uint8*) &strState, rx_buf,sizeof(tstrM2mWifiStateChanged));
if (app_wifi_recover_cb)
app_wifi_recover_cb(strState.u8CurrState);
}
#endif
}
else if (u8OpCode == M2M_WIFI_REQ_DHCP_CONF)
{
tstrM2MIPConfig strIpConfig;
#ifdef ARDUINO
extern uint32 nmdrv_firm_ver;
uint16 rxSize = sizeof(tstrM2MIPConfig);
if (nmdrv_firm_ver < M2M_MAKE_VERSION(19, 5, 0)) {
// for backwards compatibility with firmware 19.4.4 and older,
// the old tstrM2MIPConfig does not contain the u32DhcpLeaseTime field
rxSize -= sizeof(strIpConfig.u32DhcpLeaseTime);
}
if (hif_receive(u32Addr, (uint8 *)&strIpConfig, rxSize, 0) == M2M_SUCCESS)
#else
if (hif_receive(u32Addr, (uint8 *)&strIpConfig, sizeof(tstrM2MIPConfig), 0) == M2M_SUCCESS)
#endif
{
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_REQ_DHCP_CONF, (uint8 *)&strIpConfig);
}
}
else if (u8OpCode == M2M_WIFI_REQ_WPS)
{
tstrM2MWPSInfo strWps;
m2m_memset((uint8*)&strWps,0,sizeof(tstrM2MWPSInfo));
if(hif_receive(u32Addr, (uint8*)&strWps, sizeof(tstrM2MWPSInfo), 0) == M2M_SUCCESS)
{
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_REQ_WPS, &strWps);
}
}
else if (u8OpCode == M2M_WIFI_RESP_IP_CONFLICT)
{
uint32 u32ConflictedIP;
if(hif_receive(u32Addr, (uint8 *)&u32ConflictedIP, sizeof(u32ConflictedIP), 0) == M2M_SUCCESS)
{
M2M_INFO("Conflicted IP \" %u.%u.%u.%u \" \n",
BYTE_0(u32ConflictedIP),BYTE_1(u32ConflictedIP),BYTE_2(u32ConflictedIP),BYTE_3(u32ConflictedIP));
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_IP_CONFLICT, NULL);
}
}
else if (u8OpCode == M2M_WIFI_RESP_SCAN_DONE)
{
tstrM2mScanDone strState;
gu8scanInProgress = 0;
if(hif_receive(u32Addr, (uint8*)&strState, sizeof(tstrM2mScanDone), 0) == M2M_SUCCESS)
{
gu8ChNum = strState.u8NumofCh;
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_SCAN_DONE, &strState);
}
}
else if (u8OpCode == M2M_WIFI_RESP_SCAN_RESULT)
{
tstrM2mWifiscanResult strScanResult;
if(hif_receive(u32Addr, (uint8*)&strScanResult, sizeof(tstrM2mWifiscanResult), 0) == M2M_SUCCESS)
{
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_SCAN_RESULT, &strScanResult);
}
}
else if (u8OpCode == M2M_WIFI_RESP_CURRENT_RSSI)
{
if (hif_receive(u32Addr, rx_buf, 4, 0) == M2M_SUCCESS)
{
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_CURRENT_RSSI, rx_buf);
}
}
else if (u8OpCode == M2M_WIFI_RESP_CLIENT_INFO)
{
if (hif_receive(u32Addr, rx_buf, 4, 0) == M2M_SUCCESS)
{
if (gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_CLIENT_INFO, rx_buf);
}
}
else if(u8OpCode == M2M_WIFI_RESP_PROVISION_INFO)
{
tstrM2MProvisionInfo strProvInfo;
if(hif_receive(u32Addr, (uint8*)&strProvInfo, sizeof(tstrM2MProvisionInfo), 1) == M2M_SUCCESS)
{
if(gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_PROVISION_INFO, &strProvInfo);
}
}
else if(u8OpCode == M2M_WIFI_RESP_DEFAULT_CONNECT)
{
tstrM2MDefaultConnResp strResp;
if(hif_receive(u32Addr, (uint8*)&strResp, sizeof(tstrM2MDefaultConnResp), 1) == M2M_SUCCESS)
{
if(gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_DEFAULT_CONNECT, &strResp);
}
}
else if(u8OpCode == M2M_WIFI_RESP_GET_PRNG)
{
tstrPrng strPrng;
if(hif_receive(u32Addr, (uint8*)&strPrng,sizeof(tstrPrng), 0) == M2M_SUCCESS)
{
if(hif_receive(u32Addr + sizeof(tstrPrng),strPrng.pu8RngBuff,strPrng.u16PrngSize, 1) == M2M_SUCCESS)
{
if(gpfAppWifiCb)
gpfAppWifiCb(M2M_WIFI_RESP_GET_PRNG,&strPrng);
}
}
}
#ifdef ETH_MODE
else if(u8OpCode == M2M_WIFI_RESP_ETHERNET_RX_PACKET)
{
uint8 u8SetRxDone;
tstrM2mIpRsvdPkt strM2mRsvd;
if(hif_receive(u32Addr, (uint8*)&strM2mRsvd, sizeof(tstrM2mIpRsvdPkt), 0) == M2M_SUCCESS)
{
tstrM2mIpCtrlBuf strM2mIpCtrlBuf;
uint16 u16Offset = strM2mRsvd.u16PktOffset;
strM2mIpCtrlBuf.u16RemainigDataSize = strM2mRsvd.u16PktSz;
if((gpfAppEthCb) && (gau8ethRcvBuf) && (gu16ethRcvBufSize > 0))
{
do
{
u8SetRxDone = 1;
if(strM2mIpCtrlBuf.u16RemainigDataSize > gu16ethRcvBufSize)
{
u8SetRxDone = 0;
strM2mIpCtrlBuf.u16DataSize = gu16ethRcvBufSize;
}
else
{
strM2mIpCtrlBuf.u16DataSize = strM2mIpCtrlBuf.u16RemainigDataSize;
}
if(hif_receive(u32Addr + u16Offset, gau8ethRcvBuf, strM2mIpCtrlBuf.u16DataSize, u8SetRxDone) == M2M_SUCCESS)
{
strM2mIpCtrlBuf.u16RemainigDataSize -= strM2mIpCtrlBuf.u16DataSize;
u16Offset += strM2mIpCtrlBuf.u16DataSize;
gpfAppEthCb(M2M_WIFI_RESP_ETHERNET_RX_PACKET, gau8ethRcvBuf, &(strM2mIpCtrlBuf));
}
else
{
break;
}
}while (strM2mIpCtrlBuf.u16RemainigDataSize > 0);
}
}
}
#endif /* ETH_MODE */
#ifdef CONF_MGMT
else if(u8OpCode == M2M_WIFI_RESP_WIFI_RX_PACKET)
{
tstrM2MWifiRxPacketInfo strRxPacketInfo;
if(u16DataSize >= sizeof(tstrM2MWifiRxPacketInfo)) {
if(hif_receive(u32Addr, (uint8*)&strRxPacketInfo, sizeof(tstrM2MWifiRxPacketInfo), 0) == M2M_SUCCESS)
{
u16DataSize -= sizeof(tstrM2MWifiRxPacketInfo);
if(u16DataSize > 0 && gstrMgmtCtrl.pu8Buf != NULL)
{
if(u16DataSize > (gstrMgmtCtrl.u16Sz + gstrMgmtCtrl.u16Offset))
{
u16DataSize = gstrMgmtCtrl.u16Sz;
}
u32Addr += sizeof(tstrM2MWifiRxPacketInfo) + gstrMgmtCtrl.u16Offset;
if(hif_receive(u32Addr , gstrMgmtCtrl.pu8Buf, u16DataSize, 1) != M2M_SUCCESS) return;
}
if(gpfAppMonCb)
gpfAppMonCb(&strRxPacketInfo, gstrMgmtCtrl.pu8Buf,u16DataSize);
}
} else {
M2M_ERR("Incorrect mon data size %u\n", u16DataSize);
}
}
#endif
else
{
M2M_ERR("REQ Not defined %d\n",u8OpCode);
}
}
sint8 m2m_wifi_download_mode()
{
sint8 ret = M2M_SUCCESS;
/* Apply device specific initialization. */
ret = nm_drv_init_download_mode();
if(ret != M2M_SUCCESS) goto _EXIT0;
enable_interrupts();
_EXIT0:
return ret;
}
static sint8 m2m_validate_ap_parameters(CONST tstrM2MAPConfig* pstrM2MAPConfig)
{
sint8 s8Ret = M2M_SUCCESS;
/* Check for incoming pointer */
if(pstrM2MAPConfig == NULL)
{
M2M_ERR("INVALID POINTER\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
/* Check for SSID */
if((m2m_strlen((uint8 *)pstrM2MAPConfig->au8SSID) <= 0) || (m2m_strlen((uint8 *)pstrM2MAPConfig->au8SSID) >= M2M_MAX_SSID_LEN))
{
M2M_ERR("INVALID SSID\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
/* Check for Channel */
if(pstrM2MAPConfig->u8ListenChannel > M2M_WIFI_CH_14 || pstrM2MAPConfig->u8ListenChannel < M2M_WIFI_CH_1)
{
M2M_ERR("INVALID CH\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
/* Check for DHCP Server IP address */
if(!(pstrM2MAPConfig->au8DHCPServerIP[0] || pstrM2MAPConfig->au8DHCPServerIP[1]))
{
if(!(pstrM2MAPConfig->au8DHCPServerIP[2]))
{
M2M_ERR("INVALID DHCP SERVER IP\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
}
/* Check for Security */
if(pstrM2MAPConfig->u8SecType == M2M_WIFI_SEC_OPEN)
{
goto ERR1;
}
else if(pstrM2MAPConfig->u8SecType == M2M_WIFI_SEC_WEP)
{
/* Check for WEP Key index */
if((pstrM2MAPConfig->u8KeyIndx <= 0) || (pstrM2MAPConfig->u8KeyIndx > WEP_KEY_MAX_INDEX))
{
M2M_ERR("INVALID KEY INDEX\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
/* Check for WEP Key size */
if( (pstrM2MAPConfig->u8KeySz != WEP_40_KEY_STRING_SIZE) &&
(pstrM2MAPConfig->u8KeySz != WEP_104_KEY_STRING_SIZE)
)
{
M2M_ERR("INVALID KEY STRING SIZE\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
if((m2m_strlen((uint8 *)pstrM2MAPConfig->au8WepKey) <= 0) || (m2m_strlen((uint8 *)pstrM2MAPConfig->au8WepKey) > WEP_104_KEY_STRING_SIZE))
{
M2M_ERR("INVALID KEY SIZE\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
}
else if(pstrM2MAPConfig->u8SecType == M2M_WIFI_SEC_WPA_PSK)
{
/* Check for WPA Key size */
if( ((pstrM2MAPConfig->u8KeySz + 1) < M2M_MIN_PSK_LEN) || ((pstrM2MAPConfig->u8KeySz + 1) > M2M_MAX_PSK_LEN))
{
M2M_ERR("INVALID WPA KEY SIZE\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
}
else
{
M2M_ERR("INVALID AUTHENTICATION MODE\n");
s8Ret = M2M_ERR_FAIL;
goto ERR1;
}
ERR1:
return s8Ret;
}
static sint8 m2m_validate_scan_options(tstrM2MScanOption* ptstrM2MScanOption)
{
sint8 s8Ret = M2M_SUCCESS;
/* Check for incoming pointer */
if(ptstrM2MScanOption == NULL)
{
M2M_ERR("INVALID POINTER\n");
s8Ret = M2M_ERR_FAIL;
goto ERR;
}
/* Check for valid No of slots */
if(ptstrM2MScanOption->u8NumOfSlot == 0)
{
M2M_ERR("INVALID No of scan slots! %d\n",ptstrM2MScanOption->u8NumOfSlot);
s8Ret = M2M_ERR_FAIL;
goto ERR;
}
/* Check for valid time of slots */
if(ptstrM2MScanOption->u8SlotTime < 10 || ptstrM2MScanOption->u8SlotTime > 250)
{
M2M_ERR("INVALID scan slot time! %d\n",ptstrM2MScanOption->u8SlotTime);
s8Ret = M2M_ERR_FAIL;
goto ERR;
}
/* Check for valid No of probe requests per slot */
if((ptstrM2MScanOption->u8ProbesPerSlot == 0)||(ptstrM2MScanOption->u8ProbesPerSlot > M2M_SCAN_DEFAULT_NUM_PROBE))
{
M2M_ERR("INVALID No of probe requests per scan slot %d\n",ptstrM2MScanOption->u8ProbesPerSlot);
s8Ret = M2M_ERR_FAIL;
goto ERR;
}
/* Check for valid RSSI threshold */
if((ptstrM2MScanOption->s8RssiThresh < -99) || (ptstrM2MScanOption->s8RssiThresh >= 0))
{
M2M_ERR("INVALID RSSI threshold %d \n",ptstrM2MScanOption->s8RssiThresh);
s8Ret = M2M_ERR_FAIL;
}
ERR:
return s8Ret;
}
sint8 m2m_wifi_send_crl(tstrTlsCrlInfo* pCRL)
{
sint8 s8Ret = M2M_ERR_FAIL;
s8Ret = hif_send(M2M_REQ_GROUP_SSL, M2M_SSL_IND_CRL|M2M_REQ_DATA_PKT, NULL, 0, (uint8*)pCRL, sizeof(tstrTlsCrlInfo), 0);
return s8Ret;
}
sint8 m2m_wifi_init(tstrWifiInitParam * param)
{
tstrM2mRev strtmp;
sint8 ret = M2M_SUCCESS;
uint8 u8WifiMode = M2M_WIFI_MODE_NORMAL;
if(param == NULL) {
ret = M2M_ERR_FAIL;
goto _EXIT0;
}
gpfAppWifiCb = param->pfAppWifiCb;
#ifdef ETH_MODE
gpfAppEthCb = param->strEthInitParam.pfAppEthCb;
gau8ethRcvBuf = param->strEthInitParam.au8ethRcvBuf;
gu16ethRcvBufSize = param->strEthInitParam.u16ethRcvBufSize;
u8WifiMode = param->strEthInitParam.u8EthernetEnable;
#endif /* ETH_MODE */
#ifdef CONF_MGMT
gpfAppMonCb = param->pfAppMonCb;
#endif
gu8scanInProgress = 0;
/* Apply device specific initialization. */
ret = nm_drv_init(&u8WifiMode);
if(ret != M2M_SUCCESS) goto _EXIT0;
/* Initialize host interface module */
ret = hif_init(NULL);
if(ret != M2M_SUCCESS) goto _EXIT1;
hif_register_cb(M2M_REQ_GROUP_WIFI,m2m_wifi_cb);
ret = nm_get_firmware_full_info(&strtmp);
#ifdef ARDUINO
if (M2M_ERR_FAIL == ret)
{
// for compatibility with firmware version 19.3.0
ret = nm_get_firmware_info(&strtmp);
}
#endif
M2M_INFO("Firmware ver : %u.%u.%u Svnrev %u\n", strtmp.u8FirmwareMajor, strtmp.u8FirmwareMinor, strtmp.u8FirmwarePatch,strtmp.u16FirmwareSvnNum);
M2M_INFO("Firmware Build %s Time %s\n",strtmp.BuildDate,strtmp.BuildTime);
M2M_INFO("Firmware Min driver ver : %u.%u.%u\n", strtmp.u8DriverMajor, strtmp.u8DriverMinor, strtmp.u8DriverPatch);
M2M_INFO("Driver ver: %u.%u.%u\n", M2M_RELEASE_VERSION_MAJOR_NO, M2M_RELEASE_VERSION_MINOR_NO, M2M_RELEASE_VERSION_PATCH_NO);
M2M_INFO("Driver built at %s\t%s\n",__DATE__,__TIME__);
if(M2M_ERR_FW_VER_MISMATCH == ret)
{
M2M_ERR("Mismatch Firmawre Version\n");
}
goto _EXIT0;
_EXIT1:
nm_drv_deinit(NULL);
_EXIT0:
return ret;
}
sint8 m2m_wifi_deinit(void * arg)
{
#ifdef ARDUINO
(void)arg; // Silence "unused" warning
#endif
hif_deinit(NULL);
nm_drv_deinit(NULL);
return M2M_SUCCESS;
}
sint8 m2m_wifi_handle_events(void * arg)
{
#ifdef ARDUINO
(void)arg; // Silence "unused" warning
#endif
return hif_handle_isr();
}
sint8 m2m_wifi_default_connect(void)
{
return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DEFAULT_CONNECT, NULL, 0,NULL, 0,0);
}
sint8 m2m_wifi_connect(char *pcSsid, uint8 u8SsidLen, uint8 u8SecType, void *pvAuthInfo, uint16 u16Ch)
{
return m2m_wifi_connect_sc(pcSsid, u8SsidLen, u8SecType, pvAuthInfo, u16Ch,0);
}
sint8 m2m_wifi_connect_sc(char *pcSsid, uint8 u8SsidLen, uint8 u8SecType, void *pvAuthInfo, uint16 u16Ch, uint8 u8NoSaveCred)
{
sint8 ret = M2M_SUCCESS;
tstrM2mWifiConnect strConnect;
tstrM2MWifiSecInfo *pstrAuthInfo;
if(u8SecType != M2M_WIFI_SEC_OPEN)
{
if(pvAuthInfo == NULL)
{
M2M_ERR("Key is not valid\n");
ret = M2M_ERR_FAIL;
goto ERR1;
}
if((u8SecType == M2M_WIFI_SEC_WPA_PSK) && (m2m_strlen(pvAuthInfo) == (M2M_MAX_PSK_LEN-1)))
{
uint8 i = 0;
uint8* pu8Psk = (uint8*)pvAuthInfo;
while(i < (M2M_MAX_PSK_LEN-1))
{
if(pu8Psk[i]<'0' || (pu8Psk[i]>'9' && pu8Psk[i] < 'A')|| (pu8Psk[i]>'F' && pu8Psk[i] < 'a') || pu8Psk[i] > 'f')
{
M2M_ERR("Invalid Key\n");
ret = M2M_ERR_FAIL;
goto ERR1;
}
i++;
}
}
}
if((u8SsidLen<=0)||(u8SsidLen>=M2M_MAX_SSID_LEN))
{
M2M_ERR("SSID LEN INVALID\n");
ret = M2M_ERR_FAIL;
goto ERR1;
}
if(u16Ch < M2M_WIFI_CH_1|| u16Ch > M2M_WIFI_CH_14)
{
if(u16Ch!=M2M_WIFI_CH_ALL)
{
M2M_ERR("CH INVALID\n");
ret = M2M_ERR_FAIL;
goto ERR1;
}
}
m2m_memcpy(strConnect.au8SSID, (uint8*)pcSsid, u8SsidLen);
strConnect.au8SSID[u8SsidLen] = 0;
strConnect.u16Ch = NM_BSP_B_L_16(u16Ch);
/* Credentials will be Not be saved if u8NoSaveCred is set */
strConnect.u8NoSaveCred = u8NoSaveCred ? 1:0;
pstrAuthInfo = &strConnect.strSec;
pstrAuthInfo->u8SecType = u8SecType;
if(u8SecType == M2M_WIFI_SEC_WEP)
{
tstrM2mWifiWepParams * pstrWepParams = (tstrM2mWifiWepParams*)pvAuthInfo;
tstrM2mWifiWepParams *pstrWep = &pstrAuthInfo->uniAuth.strWepInfo;
#ifdef ARDUINO
pstrWep->u8KeyIndx =pstrWepParams->u8KeyIndx;
#else
pstrWep->u8KeyIndx =pstrWepParams->u8KeyIndx-1;
#endif
if(pstrWep->u8KeyIndx >= WEP_KEY_MAX_INDEX)
{
M2M_ERR("Invalid Wep key index %d\n", pstrWep->u8KeyIndx);
ret = M2M_ERR_FAIL;
goto ERR1;
}
#ifdef ARDUINO
pstrWep->u8KeySz = pstrWepParams->u8KeySz;
#else
pstrWep->u8KeySz = pstrWepParams->u8KeySz-1;
#endif
if ((pstrWep->u8KeySz != WEP_40_KEY_STRING_SIZE)&& (pstrWep->u8KeySz != WEP_104_KEY_STRING_SIZE))
{
M2M_ERR("Invalid Wep key length %d\n", pstrWep->u8KeySz);
ret = M2M_ERR_FAIL;
goto ERR1;
}
m2m_memcpy((uint8*)pstrWep->au8WepKey,(uint8*)pstrWepParams->au8WepKey, pstrWepParams->u8KeySz);
pstrWep->au8WepKey[pstrWepParams->u8KeySz] = 0;
}
else if(u8SecType == M2M_WIFI_SEC_WPA_PSK)
{
uint16 u16KeyLen = m2m_strlen((uint8*)pvAuthInfo);
if((u16KeyLen <= 0)||(u16KeyLen >= M2M_MAX_PSK_LEN))
{
M2M_ERR("Incorrect PSK key length\n");
ret = M2M_ERR_FAIL;
goto ERR1;
}
m2m_memcpy(pstrAuthInfo->uniAuth.au8PSK, (uint8*)pvAuthInfo, u16KeyLen + 1);
}
else if(u8SecType == M2M_WIFI_SEC_802_1X)
{
m2m_memcpy((uint8*)&pstrAuthInfo->uniAuth.strCred1x, (uint8*)pvAuthInfo, sizeof(tstr1xAuthCredentials));
}
else if(u8SecType == M2M_WIFI_SEC_OPEN)
{
}
else
{
M2M_ERR("undefined sec type\n");
ret = M2M_ERR_FAIL;
goto ERR1;
}
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CONNECT, (uint8*)&strConnect, sizeof(tstrM2mWifiConnect),NULL, 0,0);
ERR1:
return ret;
}
sint8 m2m_wifi_disconnect(void)
{
return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISCONNECT, NULL, 0, NULL, 0,0);
}
sint8 m2m_wifi_set_mac_address(uint8 au8MacAddress[6])
{
tstrM2mSetMacAddress strTmp;
m2m_memcpy((uint8*) strTmp.au8Mac, (uint8*) au8MacAddress, 6);
return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_MAC_ADDRESS,
(uint8*) &strTmp, sizeof(tstrM2mSetMacAddress), NULL, 0,0);
}
sint8 m2m_wifi_set_static_ip(tstrM2MIPConfig * pstrStaticIPConf)
{
pstrStaticIPConf->u32DNS = NM_BSP_B_L_32(pstrStaticIPConf->u32DNS);
pstrStaticIPConf->u32Gateway = NM_BSP_B_L_32(pstrStaticIPConf->u32Gateway);
pstrStaticIPConf->u32StaticIP = NM_BSP_B_L_32(
pstrStaticIPConf->u32StaticIP);
pstrStaticIPConf->u32SubnetMask = NM_BSP_B_L_32(
pstrStaticIPConf->u32SubnetMask);
return hif_send(M2M_REQ_GROUP_IP, M2M_IP_REQ_STATIC_IP_CONF,
(uint8*) pstrStaticIPConf, sizeof(tstrM2MIPConfig), NULL, 0,0);
}
sint8 m2m_wifi_request_dhcp_client(void)
{
/*legacy API should be removed */
return 0;
}
sint8 m2m_wifi_request_dhcp_server(uint8* addr)
{
#ifdef ARDUINO
(void)addr; // Silence "unused" warning
#endif
/*legacy API should be removed */
return 0;
}
/*!
@fn NMI_API sint8 m2m_wifi_set_lsn_int(tstrM2mLsnInt * pstrM2mLsnInt);
@brief Set the Wi-Fi listen interval for power save operation. It is represented in units
of AP Beacon periods.
@param [in] pstrM2mLsnInt
Structure holding the listen interval configurations.
@return The function SHALL return 0 for success and a negative value otherwise.
@sa tstrM2mLsnInt , m2m_wifi_set_sleep_mode
@pre m2m_wifi_set_sleep_mode shall be called first
@warning The Function called once after initialization.
*/
sint8 m2m_wifi_enable_dhcp(uint8 u8DhcpEn )
{
uint8 u8Req;
u8Req = u8DhcpEn ? M2M_IP_REQ_ENABLE_DHCP : M2M_IP_REQ_DISABLE_DHCP;
return hif_send(M2M_REQ_GROUP_IP, u8Req, NULL, 0, NULL, 0, 0);
}
sint8 m2m_wifi_set_lsn_int(tstrM2mLsnInt* pstrM2mLsnInt)
{
return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_LSN_INT, (uint8*)pstrM2mLsnInt, sizeof(tstrM2mLsnInt), NULL, 0, 0);
}
sint8 m2m_wifi_set_cust_InfoElement(uint8* pau8M2mCustInfoElement)
{
sint8 ret = M2M_ERR_FAIL;
if(pau8M2mCustInfoElement != NULL)
{
if((pau8M2mCustInfoElement[0] + 1) < M2M_CUST_IE_LEN_MAX)
{
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CUST_INFO_ELEMENT|M2M_REQ_DATA_PKT, (uint8*)pau8M2mCustInfoElement, pau8M2mCustInfoElement[0]+1, NULL, 0, 0);
}
}
return ret;
}
sint8 m2m_wifi_set_scan_options(tstrM2MScanOption* ptstrM2MScanOption)
{
sint8 s8Ret = M2M_ERR_FAIL;
if(m2m_validate_scan_options (ptstrM2MScanOption) == M2M_SUCCESS)
{
s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_SCAN_OPTION, (uint8*)ptstrM2MScanOption, sizeof(tstrM2MScanOption),NULL, 0,0);
}
return s8Ret;
}
sint8 m2m_wifi_set_scan_region(uint16 ScanRegion)
{
sint8 s8Ret = M2M_ERR_FAIL;
tstrM2MScanRegion strScanRegion;
strScanRegion.u16ScanRegion = ScanRegion;
s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_SCAN_REGION, (uint8*)&strScanRegion, sizeof(tstrM2MScanRegion),NULL, 0,0);
return s8Ret;
}
sint8 m2m_wifi_request_scan(uint8 ch)
{
sint8 s8Ret = M2M_SUCCESS;
if(!gu8scanInProgress)
{
if(((ch >= M2M_WIFI_CH_1) && (ch <= M2M_WIFI_CH_14)) || (ch == M2M_WIFI_CH_ALL))
{
tstrM2MScan strtmp;
strtmp.u8ChNum = ch;
s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SCAN, (uint8*)&strtmp, sizeof(tstrM2MScan),NULL, 0,0);
if(s8Ret == M2M_SUCCESS)
{
gu8scanInProgress = 1;
}
}
else
{
s8Ret = M2M_ERR_INVALID_ARG;
}
}
else
{
s8Ret = M2M_ERR_SCAN_IN_PROGRESS;
}
return s8Ret;
}
sint8 m2m_wifi_request_scan_passive(uint8 ch, uint16 scan_time)
{
sint8 s8Ret = M2M_SUCCESS;
if(!gu8scanInProgress)
{
if(((ch >= M2M_WIFI_CH_1) && (ch <= M2M_WIFI_CH_14)) || (ch == M2M_WIFI_CH_ALL))
{
tstrM2MScan strtmp;
strtmp.u8ChNum = ch;
strtmp.u16PassiveScanTime = scan_time;
s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_PASSIVE_SCAN, (uint8*)&strtmp, sizeof(tstrM2MScan),NULL, 0,0);
if(s8Ret == M2M_SUCCESS)
{
gu8scanInProgress = 1;
}
}
else
{
s8Ret = M2M_ERR_INVALID_ARG;
}
}
else
{
s8Ret = M2M_ERR_SCAN_IN_PROGRESS;
}
return s8Ret;
}
sint8 m2m_wifi_request_scan_ssid_list(uint8 ch,uint8 * u8Ssidlist)
{
sint8 s8Ret = M2M_ERR_INVALID_ARG;
if(!gu8scanInProgress)
{
if((((ch >= M2M_WIFI_CH_1) && (ch <= M2M_WIFI_CH_14)) || (ch == M2M_WIFI_CH_ALL))&&(u8Ssidlist != NULL))
{
tstrM2MScan strtmp;
uint16 u16Lsize = 0;
uint8 u8Apnum = u8Ssidlist[u16Lsize];
if(u8Apnum <= MAX_HIDDEN_SITES)
{
u16Lsize++;
while(u8Apnum)
{
if(u8Ssidlist[u16Lsize] >= M2M_MAX_SSID_LEN){
goto EXIT;
}else {
u16Lsize += u8Ssidlist[u16Lsize] + 1;
u8Apnum--;
}
}
strtmp.u8ChNum = ch;
s8Ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SCAN_SSID_LIST|M2M_REQ_DATA_PKT, (uint8*)&strtmp, sizeof(tstrM2MScan),u8Ssidlist, u16Lsize,sizeof(tstrM2MScan));
if(s8Ret == M2M_SUCCESS)
{
gu8scanInProgress = 1;
}
}
}
}
else
{
s8Ret = M2M_ERR_SCAN_IN_PROGRESS;
}
EXIT:
return s8Ret;
}
sint8 m2m_wifi_wps(uint8 u8TriggerType,const char *pcPinNumber)
{
tstrM2MWPSConnect strtmp;
/* Stop Scan if it is ongoing.
*/
gu8scanInProgress = 0;
strtmp.u8TriggerType = u8TriggerType;
/*If WPS is using PIN METHOD*/
if (u8TriggerType == WPS_PIN_TRIGGER)
m2m_memcpy ((uint8*)strtmp.acPinNumber,(uint8*) pcPinNumber,8);
return hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_WPS, (uint8*)&strtmp,sizeof(tstrM2MWPSConnect), NULL, 0,0);
}
sint8 m2m_wifi_wps_disable(void)
{
sint8 ret = M2M_SUCCESS;
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISABLE_WPS, NULL,0, NULL, 0, 0);
return ret;
}
/*!
@fn NMI_API sint8 m2m_wifi_req_client_ctrl(uint8 cmd);
@brief Send a command to the PS Client (An WINC1500 board running the ps_firmware),
if the PS client send any commands it will be received in wifi_cb M2M_WIFI_RESP_CLIENT_INFO
@param [in] cmd
Control command sent from PS Server to PS Client (command values defined by the application)
@return The function SHALL return M2M_SUCCESE for success and a negative value otherwise.
@sa m2m_wifi_req_server_init, M2M_WIFI_RESP_CLIENT_INFO
@pre m2m_wifi_req_server_init should be called first
@warning
*/
sint8 m2m_wifi_req_client_ctrl(uint8 u8Cmd)
{
sint8 ret = M2M_SUCCESS;
#ifdef _PS_SERVER_
tstrM2Mservercmd strCmd;
strCmd.u8cmd = u8Cmd;
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_CLIENT_CTRL, (uint8*)&strCmd, sizeof(tstrM2Mservercmd), NULL, 0, 0);
#else
#ifdef ARDUINO
(void)u8Cmd; // Silence "unused" warning
#endif
M2M_ERR("_PS_SERVER_ is not defined\n");
#endif
return ret;
}
/*!
@fn NMI_API sint8 m2m_wifi_req_server_init(uint8 ch);
@brief Initialize the PS Server, The WINC1500 support Non secure communication with another WINC1500,
(SERVER/CLIENT) through one byte command (probe request and probe response) without any connection setup
@param [in] ch
Server listening channel
@return The function SHALL return M2M_SUCCESE for success and a negative value otherwise
@sa m2m_wifi_req_client_ctrl
@warning The server mode can't be used with any other modes (STA/P2P/AP)
*/
sint8 m2m_wifi_req_server_init(uint8 ch)
{
sint8 ret = M2M_SUCCESS;
#ifdef _PS_SERVER_
tstrM2mServerInit strServer;
strServer.u8Channel = ch;
ret = hif_send(M2M_REQ_GROUP_WIFI,M2M_WIFI_REQ_SERVER_INIT, (uint8*)&strServer, sizeof(tstrM2mServerInit), NULL, 0, 0);
#else
#ifdef ARDUINO
(void)ch; // Silence "unused" warning
#endif
M2M_ERR("_PS_SERVER_ is not defined\n");
#endif
return ret;
}
sint8 m2m_wifi_p2p(uint8 u8Channel)
{
sint8 ret = M2M_SUCCESS;
if((u8Channel == M2M_WIFI_CH_1) || (u8Channel == M2M_WIFI_CH_6) || (u8Channel == M2M_WIFI_CH_11))
{
tstrM2MP2PConnect strtmp;
strtmp.u8ListenChannel = u8Channel;
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_ENABLE_P2P, (uint8*)&strtmp, sizeof(tstrM2MP2PConnect), NULL, 0,0);
}
else
{
M2M_ERR("Listen channel should only be M2M_WIFI_CH_1/6/11 \n");
ret = M2M_ERR_FAIL;
}
return ret;
}
sint8 m2m_wifi_p2p_disconnect(void)
{
sint8 ret = M2M_SUCCESS;
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISABLE_P2P, NULL, 0, NULL, 0, 0);
return ret;
}
sint8 m2m_wifi_enable_ap(CONST tstrM2MAPConfig* pstrM2MAPConfig)
{
sint8 ret = M2M_ERR_FAIL;
if(M2M_SUCCESS == m2m_validate_ap_parameters(pstrM2MAPConfig))
{
#ifdef ARDUINO
extern uint32 nmdrv_firm_ver;
uint16 txSize = sizeof(tstrM2MAPConfig);
if (nmdrv_firm_ver < M2M_MAKE_VERSION(19, 5, 0)) {
// for backwards compat with firmwware 19.4.x and older
// (listen channel is 0 based, there is no au8Key field)
((tstrM2MAPConfig*)pstrM2MAPConfig)->u8ListenChannel--;
txSize -= sizeof(pstrM2MAPConfig->au8Key) + 1;
}
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_ENABLE_AP, (uint8 *)pstrM2MAPConfig, txSize, NULL, 0, 0);
#else
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_ENABLE_AP, (uint8 *)pstrM2MAPConfig, sizeof(tstrM2MAPConfig), NULL, 0, 0);
#endif
}
return ret;
}
sint8 m2m_wifi_set_gains(tstrM2mWifiGainsParams* pstrM2mGain)
{
sint8 ret = M2M_ERR_FAIL;
if(pstrM2mGain != NULL)
{
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_SET_GAINS, (uint8 *)pstrM2mGain, sizeof(tstrM2mWifiGainsParams), NULL, 0, 0);
}
return ret;
}
sint8 m2m_wifi_disable_ap(void)
{
sint8 ret = M2M_SUCCESS;
ret = hif_send(M2M_REQ_GROUP_WIFI, M2M_WIFI_REQ_DISABLE_AP, NULL, 0, NULL, 0, 0);
return ret;
}
/*!
@fn NMI_API sint8 m2m_wifi_req_curr_rssi(void);
@brief Request the current RSSI for the current connected AP,
the response received in wifi_cb M2M_WIFI_RESP_CURRENT_RSSI
@sa M2M_WIFI_RESP_CURRENT_RSSI
@return The function shall return M2M_SUCCESS for success and a negative value otherwise.