This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathdriver.c
1843 lines (1485 loc) · 62.4 KB
/
driver.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
/*
* driver.c - CC31xx/CC32xx Host Driver Implementation
*
* Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 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.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS 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.
*
*/
/*****************************************************************************/
/* Include files */
/*****************************************************************************/
#include "simplelink.h"
#include "protocol.h"
#include "driver.h"
#include "flowcont.h"
/*****************************************************************************/
/* Macro declarations */
/*****************************************************************************/
#define _SL_PENDING_RX_MSG(pDriverCB) (RxIrqCnt != (pDriverCB)->RxDoneCnt)
/* 2 LSB of the N2H_SYNC_PATTERN are for sequence number
only in SPI interface
support backward sync pattern */
#define N2H_SYNC_PATTERN_SEQ_NUM_BITS ((_u32)0x00000003) /* Bits 0..1 - use the 2 LBS for seq num */
#define N2H_SYNC_PATTERN_SEQ_NUM_EXISTS ((_u32)0x00000004) /* Bit 2 - sign that sequence number exists in the sync pattern */
#define N2H_SYNC_PATTERN_MASK ((_u32)0xFFFFFFF8) /* Bits 3..31 - constant SYNC PATTERN */
#define N2H_SYNC_SPI_BUGS_MASK ((_u32)0x7FFF7F7F) /* Bits 7,15,31 - ignore the SPI (8,16,32 bites bus) error bits */
#define BUF_SYNC_SPIM(pBuf) ((*(_u32 *)(pBuf)) & N2H_SYNC_SPI_BUGS_MASK)
_u8 _SlDrvProtectAsyncRespSetting(_u8 *pAsyncRsp, _u8 ActionID, _u8 SocketID);
#define N2H_SYNC_SPIM (N2H_SYNC_PATTERN & N2H_SYNC_SPI_BUGS_MASK)
#define N2H_SYNC_SPIM_WITH_SEQ(TxSeqNum) ((N2H_SYNC_SPIM & N2H_SYNC_PATTERN_MASK) | N2H_SYNC_PATTERN_SEQ_NUM_EXISTS | ((TxSeqNum) & (N2H_SYNC_PATTERN_SEQ_NUM_BITS)))
#define MATCH_WOUT_SEQ_NUM(pBuf) ( BUF_SYNC_SPIM(pBuf) == N2H_SYNC_SPIM )
#define MATCH_WITH_SEQ_NUM(pBuf, TxSeqNum) ( BUF_SYNC_SPIM(pBuf) == (N2H_SYNC_SPIM_WITH_SEQ(TxSeqNum)) )
#define N2H_SYNC_PATTERN_MATCH(pBuf, TxSeqNum) \
( \
( (*((_u32 *)pBuf) & N2H_SYNC_PATTERN_SEQ_NUM_EXISTS) && ( MATCH_WITH_SEQ_NUM(pBuf, TxSeqNum) ) ) || \
( !(*((_u32 *)pBuf) & N2H_SYNC_PATTERN_SEQ_NUM_EXISTS) && ( MATCH_WOUT_SEQ_NUM(pBuf ) ) ) \
)
#define OPCODE(_ptr) (((_SlResponseHeader_t *)(_ptr))->GenHeader.Opcode)
#define RSP_PAYLOAD_LEN(_ptr) (((_SlResponseHeader_t *)(_ptr))->GenHeader.Len - _SL_RESP_SPEC_HDR_SIZE)
#define SD(_ptr) (((_SocketAddrResponse_u *)(_ptr))->IpV4.sd)
/* Actual size of Recv/Recvfrom response data */
#define ACT_DATA_SIZE(_ptr) (((_SocketAddrResponse_u *)(_ptr))->IpV4.statusOrLen)
/* General Events handling*/
#if defined (EXT_LIB_REGISTERED_GENERAL_EVENTS)
typedef _SlEventPropogationStatus_e (*general_callback) (SlDeviceEvent_t *);
static const general_callback general_callbacks[] =
{
#ifdef SlExtLib1GeneralEventHandler
SlExtLib1GeneralEventHandler,
#endif
#ifdef SlExtLib2GeneralEventHandler
SlExtLib2GeneralEventHandler,
#endif
#ifdef SlExtLib3GeneralEventHandler
SlExtLib3GeneralEventHandler,
#endif
#ifdef SlExtLib4GeneralEventHandler
SlExtLib4GeneralEventHandler,
#endif
#ifdef SlExtLib5GeneralEventHandler
SlExtLib5GeneralEventHandler,
#endif
};
#undef _SlDrvHandleGeneralEvents
/********************************************************************
_SlDrvHandleGeneralEvents
Iterates through all the general(device) event handlers which are
registered by the external libs/user application.
*********************************************************************/
void _SlDrvHandleGeneralEvents(SlDeviceEvent_t *slGeneralEvent)
{
_u8 i;
/* Iterate over all the extenal libs handlers */
for ( i = 0 ; i < sizeof(general_callbacks)/sizeof(general_callbacks[0]) ; i++ )
{
if (EVENT_PROPAGATION_BLOCK == general_callbacks[i](slGeneralEvent) )
{
/* exit immediately and do not call the user specific handler as well */
return;
}
}
/* At last call the Application specific handler if registered */
#ifdef sl_GeneralEvtHdlr
sl_GeneralEvtHdlr(slGeneralEvent);
#endif
}
#endif
/* WLAN Events handling*/
#if defined (EXT_LIB_REGISTERED_WLAN_EVENTS)
typedef _SlEventPropogationStatus_e (*wlan_callback) (SlWlanEvent_t *);
static wlan_callback wlan_callbacks[] =
{
#ifdef SlExtLib1WlanEventHandler
SlExtLib1WlanEventHandler,
#endif
#ifdef SlExtLib2WlanEventHandler
SlExtLib2WlanEventHandler,
#endif
#ifdef SlExtLib3WlanEventHandler
SlExtLib3WlanEventHandler,
#endif
#ifdef SlExtLib4WlanEventHandler
SlExtLib4WlanEventHandler,
#endif
#ifdef SlExtLib5WlanEventHandler
SlExtLib5WlanEventHandler,
#endif
};
#undef _SlDrvHandleWlanEvents
/***********************************************************
_SlDrvHandleWlanEvents
Iterates through all the wlan event handlers which are
registered by the external libs/user application.
************************************************************/
void _SlDrvHandleWlanEvents(SlWlanEvent_t *slWlanEvent)
{
_u8 i;
/* Iterate over all the extenal libs handlers */
for ( i = 0 ; i < sizeof(wlan_callbacks)/sizeof(wlan_callbacks[0]) ; i++ )
{
if ( EVENT_PROPAGATION_BLOCK == wlan_callbacks[i](slWlanEvent) )
{
/* exit immediately and do not call the user specific handler as well */
return;
}
}
/* At last call the Application specific handler if registered */
#ifdef sl_WlanEvtHdlr
sl_WlanEvtHdlr(slWlanEvent);
#endif
}
#endif
/* NetApp Events handling */
#if defined (EXT_LIB_REGISTERED_NETAPP_EVENTS)
typedef _SlEventPropogationStatus_e (*netApp_callback) (SlNetAppEvent_t *);
static const netApp_callback netApp_callbacks[] =
{
#ifdef SlExtLib1NetAppEventHandler
SlExtLib1NetAppEventHandler,
#endif
#ifdef SlExtLib2NetAppEventHandler
SlExtLib2NetAppEventHandler,
#endif
#ifdef SlExtLib3NetAppEventHandler
SlExtLib3NetAppEventHandler,
#endif
#ifdef SlExtLib4NetAppEventHandler
SlExtLib4NetAppEventHandler,
#endif
#ifdef SlExtLib5NetAppEventHandler
SlExtLib5NetAppEventHandler,
#endif
};
#undef _SlDrvHandleNetAppEvents
/************************************************************
_SlDrvHandleNetAppEvents
Iterates through all the net app event handlers which are
registered by the external libs/user application.
************************************************************/
void _SlDrvHandleNetAppEvents(SlNetAppEvent_t *slNetAppEvent)
{
_u8 i;
/* Iterate over all the extenal libs handlers */
for ( i = 0 ; i < sizeof(netApp_callbacks)/sizeof(netApp_callbacks[0]) ; i++ )
{
if (EVENT_PROPAGATION_BLOCK == netApp_callbacks[i](slNetAppEvent) )
{
/* exit immediately and do not call the user specific handler as well */
return;
}
}
/* At last call the Application specific handler if registered */
#ifdef sl_NetAppEvtHdlr
sl_NetAppEvtHdlr(slNetAppEvent);
#endif
}
#endif
/* Http Server Events handling */
#if defined (EXT_LIB_REGISTERED_HTTP_SERVER_EVENTS)
typedef _SlEventPropogationStatus_e (*httpServer_callback) (SlHttpServerEvent_t*, SlHttpServerResponse_t*);
static const httpServer_callback httpServer_callbacks[] =
{
#ifdef SlExtLib1HttpServerEventHandler
SlExtLib1HttpServerEventHandler,
#endif
#ifdef SlExtLib2HttpServerEventHandler
SlExtLib2HttpServerEventHandler,
#endif
#ifdef SlExtLib3HttpServerEventHandler
SlExtLib3HttpServerEventHandler,
#endif
#ifdef SlExtLib4HttpServerEventHandler
SlExtLib4HttpServerEventHandler,
#endif
#ifdef SlExtLib5HttpServerEventHandler
SlExtLib5HttpServerEventHandler,
#endif
};
#undef _SlDrvHandleHttpServerEvents
/*******************************************************************
_SlDrvHandleHttpServerEvents
Iterates through all the http server event handlers which are
registered by the external libs/user application.
********************************************************************/
void _SlDrvHandleHttpServerEvents(SlHttpServerEvent_t *slHttpServerEvent, SlHttpServerResponse_t *slHttpServerResponse)
{
_u8 i;
/* Iterate over all the external libs handlers */
for ( i = 0 ; i < sizeof(httpServer_callbacks)/sizeof(httpServer_callbacks[0]) ; i++ )
{
if ( EVENT_PROPAGATION_BLOCK == httpServer_callbacks[i](slHttpServerEvent, slHttpServerResponse) )
{
/* exit immediately and do not call the user specific handler as well */
return;
}
}
/* At last call the Application specific handler if registered */
#ifdef sl_HttpServerCallback
sl_HttpServerCallback(slHttpServerEvent, slHttpServerResponse);
#endif
}
#endif
/* Socket Events */
#if defined (EXT_LIB_REGISTERED_SOCK_EVENTS)
typedef _SlEventPropogationStatus_e (*sock_callback) (SlSockEvent_t *);
static const sock_callback sock_callbacks[] =
{
#ifdef SlExtLib1SockEventHandler
SlExtLib1SockEventHandler,
#endif
#ifdef SlExtLib2SockEventHandler
SlExtLib2SockEventHandler,
#endif
#ifdef SlExtLib3SockEventHandler
SlExtLib3SockEventHandler,
#endif
#ifdef SlExtLib4SockEventHandler
SlExtLib4SockEventHandler,
#endif
#ifdef SlExtLib5SockEventHandler
SlExtLib5SockEventHandler,
#endif
};
/*************************************************************
_SlDrvHandleSockEvents
Iterates through all the socket event handlers which are
registered by the external libs/user application.
**************************************************************/
void _SlDrvHandleSockEvents(SlSockEvent_t *slSockEvent)
{
_u8 i;
/* Iterate over all the external libs handlers */
for ( i = 0 ; i < sizeof(sock_callbacks)/sizeof(sock_callbacks[0]) ; i++ )
{
if ( EVENT_PROPAGATION_BLOCK == sock_callbacks[i](slSockEvent) )
{
/* exit immediately and do not call the user specific handler as well */
return;
}
}
/* At last call the Application specific handler if registered */
#ifdef sl_SockEvtHdlr
sl_SockEvtHdlr(slSockEvent);
#endif
}
#endif
#if (SL_MEMORY_MGMT != SL_MEMORY_MGMT_DYNAMIC)
typedef struct
{
_u32 Align;
_SlDriverCb_t DriverCB;
_u8 AsyncRespBuf[SL_ASYNC_MAX_MSG_LEN];
}_SlStatMem_t;
_SlStatMem_t g_StatMem;
#endif
_u8 _SlDrvProtectAsyncRespSetting(_u8 *pAsyncRsp, _u8 ActionID, _u8 SocketID)
{
_u8 ObjIdx;
/* Use Obj to issue the command, if not available try later */
ObjIdx = _SlDrvWaitForPoolObj(ActionID, SocketID);
if (MAX_CONCURRENT_ACTIONS != ObjIdx)
{
_SlDrvProtectionObjLockWaitForever();
g_pCB->ObjPool[ObjIdx].pRespArgs = pAsyncRsp;
_SlDrvProtectionObjUnLock();
}
return ObjIdx;
}
/*****************************************************************************/
/* Variables */
/*****************************************************************************/
const _SlSyncPattern_t g_H2NSyncPattern = H2N_SYNC_PATTERN;
const _SlSyncPattern_t g_H2NCnysPattern = H2N_CNYS_PATTERN;
_volatile _u8 RxIrqCnt;
#ifndef SL_TINY_EXT
const _SlActionLookup_t _SlActionLookupTable[] =
{
{ACCEPT_ID, SL_OPCODE_SOCKET_ACCEPTASYNCRESPONSE, (_SlSpawnEntryFunc_t)_sl_HandleAsync_Accept},
{CONNECT_ID, SL_OPCODE_SOCKET_CONNECTASYNCRESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_Connect},
{SELECT_ID, SL_OPCODE_SOCKET_SELECTASYNCRESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_Select},
{GETHOSYBYNAME_ID, SL_OPCODE_NETAPP_DNSGETHOSTBYNAMEASYNCRESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_DnsGetHostByName},
{GETHOSYBYSERVICE_ID, SL_OPCODE_NETAPP_MDNSGETHOSTBYSERVICEASYNCRESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_DnsGetHostByService},
{PING_ID, SL_OPCODE_NETAPP_PINGREPORTREQUESTRESPONSE, (_SlSpawnEntryFunc_t)_sl_HandleAsync_PingResponse},
{START_STOP_ID, SL_OPCODE_DEVICE_STOP_ASYNC_RESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_Stop}
};
#else
const _SlActionLookup_t _SlActionLookupTable[] =
{
{CONNECT_ID, SL_OPCODE_SOCKET_CONNECTASYNCRESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_Connect},
{GETHOSYBYNAME_ID, SL_OPCODE_NETAPP_DNSGETHOSTBYNAMEASYNCRESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_DnsGetHostByName},
{START_STOP_ID, SL_OPCODE_DEVICE_STOP_ASYNC_RESPONSE,(_SlSpawnEntryFunc_t)_sl_HandleAsync_Stop}
};
#endif
typedef struct
{
_u16 opcode;
_u8 event;
} OpcodeKeyVal_t;
/* The table translates opcode to user's event type */
const OpcodeKeyVal_t OpcodeTranslateTable[] =
{
{SL_OPCODE_WLAN_SMART_CONFIG_START_ASYNC_RESPONSE, SL_WLAN_SMART_CONFIG_COMPLETE_EVENT},
{SL_OPCODE_WLAN_SMART_CONFIG_STOP_ASYNC_RESPONSE,SL_WLAN_SMART_CONFIG_STOP_EVENT},
{SL_OPCODE_WLAN_STA_CONNECTED, SL_WLAN_STA_CONNECTED_EVENT},
{SL_OPCODE_WLAN_STA_DISCONNECTED,SL_WLAN_STA_DISCONNECTED_EVENT},
{SL_OPCODE_WLAN_P2P_DEV_FOUND,SL_WLAN_P2P_DEV_FOUND_EVENT},
{SL_OPCODE_WLAN_P2P_NEG_REQ_RECEIVED, SL_WLAN_P2P_NEG_REQ_RECEIVED_EVENT},
{SL_OPCODE_WLAN_CONNECTION_FAILED, SL_WLAN_CONNECTION_FAILED_EVENT},
{SL_OPCODE_WLAN_WLANASYNCCONNECTEDRESPONSE, SL_WLAN_CONNECT_EVENT},
{SL_OPCODE_WLAN_WLANASYNCDISCONNECTEDRESPONSE, SL_WLAN_DISCONNECT_EVENT},
{SL_OPCODE_NETAPP_IPACQUIRED, SL_NETAPP_IPV4_IPACQUIRED_EVENT},
{SL_OPCODE_NETAPP_IPACQUIRED_V6, SL_NETAPP_IPV6_IPACQUIRED_EVENT},
{SL_OPCODE_NETAPP_IP_LEASED, SL_NETAPP_IP_LEASED_EVENT},
{SL_OPCODE_NETAPP_IP_RELEASED, SL_NETAPP_IP_RELEASED_EVENT},
{SL_OPCODE_SOCKET_TXFAILEDASYNCRESPONSE, SL_SOCKET_TX_FAILED_EVENT},
{SL_OPCODE_SOCKET_SOCKETASYNCEVENT, SL_SOCKET_ASYNC_EVENT}
};
_SlDriverCb_t* g_pCB = NULL;
P_SL_DEV_PING_CALLBACK pPingCallBackFunc = NULL;
_u8 gFirstCmdMode = 0;
/*****************************************************************************/
/* Function prototypes */
/*****************************************************************************/
_SlReturnVal_t _SlDrvMsgRead(void);
_SlReturnVal_t _SlDrvMsgWrite(_SlCmdCtrl_t *pCmdCtrl,_SlCmdExt_t *pCmdExt, _u8 *pTxRxDescBuff);
_SlReturnVal_t _SlDrvMsgReadCmdCtx(void);
_SlReturnVal_t _SlDrvMsgReadSpawnCtx(void *pValue);
void _SlDrvClassifyRxMsg(_SlOpcode_t Opcode );
_SlReturnVal_t _SlDrvRxHdrRead(_u8 *pBuf, _u8 *pAlignSize);
void _SlDrvShiftDWord(_u8 *pBuf);
void _SlDrvDriverCBInit(void);
void _SlAsyncEventGenericHandler(void);
_u8 _SlDrvWaitForPoolObj(_u8 ActionID, _u8 SocketID);
void _SlDrvReleasePoolObj(_u8 pObj);
void _SlRemoveFromList(_u8* ListIndex, _u8 ItemIndex);
_SlReturnVal_t _SlFindAndSetActiveObj(_SlOpcode_t Opcode, _u8 Sd);
/*****************************************************************************/
/* Internal functions */
/*****************************************************************************/
/*****************************************************************************
_SlDrvDriverCBInit - init Driver Control Block
*****************************************************************************/
void _SlDrvDriverCBInit(void)
{
_u8 Idx =0;
#if (SL_MEMORY_MGMT == SL_MEMORY_MGMT_DYNAMIC)
g_pCB = sl_Malloc(sizeof(_SlDriverCb_t));
#else
g_pCB = &(g_StatMem.DriverCB);
#endif
MALLOC_OK_CHECK(g_pCB);
_SlDrvMemZero(g_pCB, sizeof(_SlDriverCb_t));
RxIrqCnt = 0;
OSI_RET_OK_CHECK( sl_SyncObjCreate(&g_pCB->CmdSyncObj, "CmdSyncObj") );
sl_SyncObjClear(&g_pCB->CmdSyncObj);
OSI_RET_OK_CHECK( sl_LockObjCreate(&g_pCB->GlobalLockObj, "GlobalLockObj") );
OSI_RET_OK_CHECK( sl_LockObjCreate(&g_pCB->ProtectionLockObj, "ProtectionLockObj") );
/* Init Drv object */
_SlDrvMemZero(&g_pCB->ObjPool[0], MAX_CONCURRENT_ACTIONS*sizeof(_SlPoolObj_t));
/* place all Obj in the free list*/
g_pCB->FreePoolIdx = 0;
for (Idx = 0 ; Idx < MAX_CONCURRENT_ACTIONS ; Idx++)
{
g_pCB->ObjPool[Idx].NextIndex = Idx + 1;
g_pCB->ObjPool[Idx].AdditionalData = SL_MAX_SOCKETS;
OSI_RET_OK_CHECK( sl_SyncObjCreate(&g_pCB->ObjPool[Idx].SyncObj, "SyncObj"));
sl_SyncObjClear(&g_pCB->ObjPool[Idx].SyncObj);
}
g_pCB->ActivePoolIdx = MAX_CONCURRENT_ACTIONS;
g_pCB->PendingPoolIdx = MAX_CONCURRENT_ACTIONS;
/* Flow control init */
g_pCB->FlowContCB.TxPoolCnt = FLOW_CONT_MIN;
OSI_RET_OK_CHECK(sl_LockObjCreate(&g_pCB->FlowContCB.TxLockObj, "TxLockObj"));
OSI_RET_OK_CHECK(sl_SyncObjCreate(&g_pCB->FlowContCB.TxSyncObj, "TxSyncObj"));
gFirstCmdMode = 0;
}
/*****************************************************************************
_SlDrvDriverCBDeinit - De init Driver Control Block
*****************************************************************************/
void _SlDrvDriverCBDeinit()
{
_u8 Idx =0;
/* Flow control de-init */
g_pCB->FlowContCB.TxPoolCnt = 0;
OSI_RET_OK_CHECK(sl_LockObjDelete(&g_pCB->FlowContCB.TxLockObj));
OSI_RET_OK_CHECK(sl_SyncObjDelete(&g_pCB->FlowContCB.TxSyncObj));
OSI_RET_OK_CHECK( sl_SyncObjDelete(&g_pCB->CmdSyncObj) );
OSI_RET_OK_CHECK( sl_LockObjDelete(&g_pCB->GlobalLockObj) );
OSI_RET_OK_CHECK( sl_LockObjDelete(&g_pCB->ProtectionLockObj) );
#ifndef SL_TINY_EXT
for (Idx = 0; Idx < MAX_CONCURRENT_ACTIONS; Idx++)
#endif
{
OSI_RET_OK_CHECK( sl_SyncObjDelete(&g_pCB->ObjPool[Idx].SyncObj) );
}
g_pCB->FreePoolIdx = 0;
g_pCB->PendingPoolIdx = MAX_CONCURRENT_ACTIONS;
g_pCB->ActivePoolIdx = MAX_CONCURRENT_ACTIONS;
#if (SL_MEMORY_MGMT == SL_MEMORY_MGMT_DYNAMIC)
sl_Free(g_pCB);
#else
g_pCB = NULL;
#endif
g_pCB = NULL;
}
/*****************************************************************************
_SlDrvRxIrqHandler - Interrupt handler
*****************************************************************************/
void _SlDrvRxIrqHandler(void *pValue)
{
sl_IfMaskIntHdlr();
RxIrqCnt++;
if (TRUE == g_pCB->IsCmdRespWaited)
{
OSI_RET_OK_CHECK( sl_SyncObjSignalFromIRQ(&g_pCB->CmdSyncObj) );
}
else
{
sl_Spawn((_SlSpawnEntryFunc_t)_SlDrvMsgReadSpawnCtx, NULL, 0);
}
}
/*****************************************************************************
_SlDrvCmdOp
*****************************************************************************/
_SlReturnVal_t _SlDrvCmdOp(
_SlCmdCtrl_t *pCmdCtrl ,
void *pTxRxDescBuff ,
_SlCmdExt_t *pCmdExt)
{
_SlReturnVal_t RetVal;
_SlDrvObjLockWaitForever(&g_pCB->GlobalLockObj);
g_pCB->IsCmdRespWaited = TRUE;
SL_TRACE0(DBG_MSG, MSG_312, "_SlDrvCmdOp: call _SlDrvMsgWrite");
/* send the message */
RetVal = _SlDrvMsgWrite(pCmdCtrl, pCmdExt, pTxRxDescBuff);
if(SL_OS_RET_CODE_OK == RetVal)
{
#ifndef SL_IF_TYPE_UART
/* Waiting for SPI to stabilize after first command */
if( 0 == gFirstCmdMode )
{
volatile _u32 CountVal = 0;
gFirstCmdMode = 1;
CountVal = CPU_FREQ_IN_MHZ*USEC_DELAY;
while( CountVal-- );
}
#endif
/* wait for respond */
RetVal = _SlDrvMsgReadCmdCtx(); /* will free global lock */
SL_TRACE0(DBG_MSG, MSG_314, "_SlDrvCmdOp: exited _SlDrvMsgReadCmdCtx");
}
else
{
_SlDrvObjUnLock(&g_pCB->GlobalLockObj);
}
return RetVal;
}
/*****************************************************************************
_SlDrvDataReadOp
*****************************************************************************/
_SlReturnVal_t _SlDrvDataReadOp(
_SlSd_t Sd,
_SlCmdCtrl_t *pCmdCtrl ,
void *pTxRxDescBuff ,
_SlCmdExt_t *pCmdExt)
{
_SlReturnVal_t RetVal;
_u8 ObjIdx = MAX_CONCURRENT_ACTIONS;
_SlArgsData_t pArgsData;
/* Validate input arguments */
VERIFY_PROTOCOL(NULL != pCmdExt->pRxPayload);
/* If zero bytes is requested, return error. */
/* This allows us not to fill remote socket's IP address in return arguments */
VERIFY_PROTOCOL(0 != pCmdExt->RxPayloadLen);
/* Validate socket */
if((Sd & BSD_SOCKET_ID_MASK) >= SL_MAX_SOCKETS)
{
return SL_EBADF;
}
/*Use Obj to issue the command, if not available try later*/
ObjIdx = (_u8)_SlDrvWaitForPoolObj(RECV_ID, Sd & BSD_SOCKET_ID_MASK);
if (MAX_CONCURRENT_ACTIONS == ObjIdx)
{
return SL_POOL_IS_EMPTY;
}
_SlDrvProtectionObjLockWaitForever();
pArgsData.pData = pCmdExt->pRxPayload;
pArgsData.pArgs = (_u8 *)pTxRxDescBuff;
g_pCB->ObjPool[ObjIdx].pRespArgs = (_u8 *)&pArgsData;
_SlDrvProtectionObjUnLock();
/* Do Flow Control check/update for DataWrite operation */
_SlDrvObjLockWaitForever(&g_pCB->FlowContCB.TxLockObj);
/* Clear SyncObj for the case it was signalled before TxPoolCnt */
/* dropped below '1' (last Data buffer was taken) */
/* OSI_RET_OK_CHECK( sl_SyncObjClear(&g_pCB->FlowContCB.TxSyncObj) ); */
sl_SyncObjClear(&g_pCB->FlowContCB.TxSyncObj);
if(g_pCB->FlowContCB.TxPoolCnt <= FLOW_CONT_MIN)
{
/* If TxPoolCnt was increased by other thread at this moment,
TxSyncObj won't wait here */
_SlDrvSyncObjWaitForever(&g_pCB->FlowContCB.TxSyncObj);
}
_SlDrvObjLockWaitForever(&g_pCB->GlobalLockObj);
VERIFY_PROTOCOL(g_pCB->FlowContCB.TxPoolCnt > FLOW_CONT_MIN);
g_pCB->FlowContCB.TxPoolCnt--;
_SlDrvObjUnLock(&g_pCB->FlowContCB.TxLockObj);
/* send the message */
RetVal = _SlDrvMsgWrite(pCmdCtrl, pCmdExt, (_u8 *)pTxRxDescBuff);
_SlDrvObjUnLock(&g_pCB->GlobalLockObj);
if(SL_OS_RET_CODE_OK == RetVal)
{
/* Wait for response message. Will be signaled by _SlDrvMsgRead. */
_SlDrvSyncObjWaitForever(&g_pCB->ObjPool[ObjIdx].SyncObj);
}
_SlDrvReleasePoolObj(ObjIdx);
return RetVal;
}
/* ******************************************************************************/
/* _SlDrvDataWriteOp */
/* ******************************************************************************/
_SlReturnVal_t _SlDrvDataWriteOp(
_SlSd_t Sd,
_SlCmdCtrl_t *pCmdCtrl ,
void *pTxRxDescBuff ,
_SlCmdExt_t *pCmdExt)
{
_SlReturnVal_t RetVal = SL_EAGAIN; /* initiated as SL_EAGAIN for the non blocking mode */
while( 1 )
{
/* Do Flow Control check/update for DataWrite operation */
_SlDrvObjLockWaitForever(&g_pCB->FlowContCB.TxLockObj);
/* Clear SyncObj for the case it was signalled before TxPoolCnt */
/* dropped below '1' (last Data buffer was taken) */
/* OSI_RET_OK_CHECK( sl_SyncObjClear(&g_pCB->FlowContCB.TxSyncObj) ); */
sl_SyncObjClear(&g_pCB->FlowContCB.TxSyncObj);
/* we have indication that the last send has failed - socket is no longer valid for operations */
if(g_pCB->SocketTXFailure & (1<<(Sd & BSD_SOCKET_ID_MASK)))
{
_SlDrvObjUnLock(&g_pCB->FlowContCB.TxLockObj);
return SL_SOC_ERROR;
}
if(g_pCB->FlowContCB.TxPoolCnt <= FLOW_CONT_MIN + 1)
{
/* we have indication that this socket is set as blocking and we try to */
/* unblock it - return an error */
if( g_pCB->SocketNonBlocking & (1<< (Sd & BSD_SOCKET_ID_MASK)))
{
_SlDrvObjUnLock(&g_pCB->FlowContCB.TxLockObj);
return RetVal;
}
/* If TxPoolCnt was increased by other thread at this moment, */
/* TxSyncObj won't wait here */
_SlDrvSyncObjWaitForever(&g_pCB->FlowContCB.TxSyncObj);
}
if(g_pCB->FlowContCB.TxPoolCnt > FLOW_CONT_MIN + 1 )
{
break;
}
else
{
_SlDrvObjUnLock(&g_pCB->FlowContCB.TxLockObj);
}
}
_SlDrvObjLockWaitForever(&g_pCB->GlobalLockObj);
VERIFY_PROTOCOL(g_pCB->FlowContCB.TxPoolCnt > FLOW_CONT_MIN + 1 );
g_pCB->FlowContCB.TxPoolCnt--;
_SlDrvObjUnLock(&g_pCB->FlowContCB.TxLockObj);
/* send the message */
RetVal = _SlDrvMsgWrite(pCmdCtrl, pCmdExt, pTxRxDescBuff);
_SlDrvObjUnLock(&g_pCB->GlobalLockObj);
return RetVal;
}
/* ******************************************************************************/
/* _SlDrvMsgWrite */
/* ******************************************************************************/
_SlReturnVal_t _SlDrvMsgWrite(_SlCmdCtrl_t *pCmdCtrl,_SlCmdExt_t *pCmdExt, _u8 *pTxRxDescBuff)
{
_u8 sendRxPayload = FALSE;
VERIFY_PROTOCOL(NULL != pCmdCtrl);
g_pCB->FunctionParams.pCmdCtrl = pCmdCtrl;
g_pCB->FunctionParams.pTxRxDescBuff = pTxRxDescBuff;
g_pCB->FunctionParams.pCmdExt = pCmdExt;
g_pCB->TempProtocolHeader.Opcode = pCmdCtrl->Opcode;
g_pCB->TempProtocolHeader.Len = _SL_PROTOCOL_CALC_LEN(pCmdCtrl, pCmdExt);
if (pCmdExt && pCmdExt->RxPayloadLen < 0 && pCmdExt->TxPayloadLen)
{
pCmdExt->RxPayloadLen = pCmdExt->RxPayloadLen * (-1); /* change sign */
sendRxPayload = TRUE;
g_pCB->TempProtocolHeader.Len = g_pCB->TempProtocolHeader.Len + pCmdExt->RxPayloadLen;
}
#ifdef SL_START_WRITE_STAT
sl_IfStartWriteSequence(g_pCB->FD);
#endif
#ifdef SL_IF_TYPE_UART
/* Write long sync pattern */
NWP_IF_WRITE_CHECK(g_pCB->FD, (_u8 *)&g_H2NSyncPattern.Long, 2*SYNC_PATTERN_LEN);
#else
/* Write short sync pattern */
NWP_IF_WRITE_CHECK(g_pCB->FD, (_u8 *)&g_H2NSyncPattern.Short, SYNC_PATTERN_LEN);
#endif
/* Header */
NWP_IF_WRITE_CHECK(g_pCB->FD, (_u8 *)&g_pCB->TempProtocolHeader, _SL_CMD_HDR_SIZE);
/* Descriptors */
if (pTxRxDescBuff && pCmdCtrl->TxDescLen > 0)
{
NWP_IF_WRITE_CHECK(g_pCB->FD, pTxRxDescBuff,
_SL_PROTOCOL_ALIGN_SIZE(pCmdCtrl->TxDescLen));
}
/* A special mode where Rx payload and Rx length are used as Tx as well */
/* This mode requires no Rx payload on the response and currently used by fs_Close and sl_Send on */
/* transceiver mode */
if (sendRxPayload == TRUE )
{
NWP_IF_WRITE_CHECK(g_pCB->FD, pCmdExt->pRxPayload,
_SL_PROTOCOL_ALIGN_SIZE(pCmdExt->RxPayloadLen));
}
/* Payload */
if (pCmdExt && pCmdExt->TxPayloadLen > 0)
{
/* If the message has payload, it is mandatory that the message's arguments are protocol aligned. */
/* Otherwise the aligning of arguments will create a gap between arguments and payload. */
VERIFY_PROTOCOL(_SL_IS_PROTOCOL_ALIGNED_SIZE(pCmdCtrl->TxDescLen));
NWP_IF_WRITE_CHECK(g_pCB->FD, pCmdExt->pTxPayload,
_SL_PROTOCOL_ALIGN_SIZE(pCmdExt->TxPayloadLen));
}
_SL_DBG_CNT_INC(MsgCnt.Write);
#ifdef SL_START_WRITE_STAT
sl_IfEndWriteSequence(g_pCB->FD);
#endif
return SL_OS_RET_CODE_OK;
}
/* ******************************************************************************/
/* _SlDrvMsgRead */
/* ******************************************************************************/
_SlReturnVal_t _SlDrvMsgRead(void)
{
/* alignment for small memory models */
union
{
_u8 TempBuf[_SL_RESP_HDR_SIZE];
_u32 DummyBuf[2];
} uBuf;
_u8 TailBuffer[4];
_u16 LengthToCopy;
_u16 AlignedLengthRecv;
_u8 AlignSize;
_u8 *pAsyncBuf = NULL;
_u16 OpCode;
_u16 RespPayloadLen;
_u8 sd = SL_MAX_SOCKETS;
_SlRxMsgClass_e RxMsgClass;
/* save params in global CB */
g_pCB->FunctionParams.AsyncExt.pAsyncBuf = NULL;
g_pCB->FunctionParams.AsyncExt.AsyncEvtHandler= NULL;
VERIFY_RET_OK(_SlDrvRxHdrRead((_u8*)(uBuf.TempBuf), &AlignSize));
OpCode = OPCODE(uBuf.TempBuf);
RespPayloadLen = RSP_PAYLOAD_LEN(uBuf.TempBuf);
/* 'Init Compelete' message bears no valid FlowControl info */
if(SL_OPCODE_DEVICE_INITCOMPLETE != OpCode)
{
g_pCB->FlowContCB.TxPoolCnt = ((_SlResponseHeader_t *)uBuf.TempBuf)->TxPoolCnt;
g_pCB->SocketNonBlocking = ((_SlResponseHeader_t *)uBuf.TempBuf)->SocketNonBlocking;
g_pCB->SocketTXFailure = ((_SlResponseHeader_t *)uBuf.TempBuf)->SocketTXFailure;
if(g_pCB->FlowContCB.TxPoolCnt > FLOW_CONT_MIN)
{
_SlDrvSyncObjSignal(&g_pCB->FlowContCB.TxSyncObj);
}
}
/* Find the RX messaage class and set its async event handler */
_SlDrvClassifyRxMsg(OpCode);
RxMsgClass = g_pCB->FunctionParams.AsyncExt.RxMsgClass;
switch(RxMsgClass)
{
case ASYNC_EVT_CLASS:
VERIFY_PROTOCOL(NULL == pAsyncBuf);
#if (SL_MEMORY_MGMT == SL_MEMORY_MGMT_DYNAMIC)
g_pCB->FunctionParams.AsyncExt.pAsyncBuf = sl_Malloc(SL_ASYNC_MAX_MSG_LEN);
#else
g_pCB->FunctionParams.AsyncExt.pAsyncBuf = g_StatMem.AsyncRespBuf;
#endif
/* set the local pointer to the allocated one */
pAsyncBuf = g_pCB->FunctionParams.AsyncExt.pAsyncBuf;
/* clear the async buffer */
_SlDrvMemZero(pAsyncBuf, SL_ASYNC_MAX_MSG_LEN);
MALLOC_OK_CHECK(pAsyncBuf);
sl_Memcpy(pAsyncBuf, uBuf.TempBuf, _SL_RESP_HDR_SIZE);
if (_SL_PROTOCOL_ALIGN_SIZE(RespPayloadLen) <= SL_ASYNC_MAX_PAYLOAD_LEN)
{
AlignedLengthRecv = _SL_PROTOCOL_ALIGN_SIZE(RespPayloadLen);
}
else
{
AlignedLengthRecv = _SL_PROTOCOL_ALIGN_SIZE(SL_ASYNC_MAX_PAYLOAD_LEN);
}
if (RespPayloadLen > 0)
{
NWP_IF_READ_CHECK(g_pCB->FD,
pAsyncBuf + _SL_RESP_HDR_SIZE,
AlignedLengthRecv);
}
/* In case ASYNC RX buffer length is smaller then the received data length, dump the rest */
if ((_SL_PROTOCOL_ALIGN_SIZE(RespPayloadLen) > SL_ASYNC_MAX_PAYLOAD_LEN))
{
AlignedLengthRecv = _SL_PROTOCOL_ALIGN_SIZE(RespPayloadLen) - SL_ASYNC_MAX_PAYLOAD_LEN;
while (AlignedLengthRecv > 0)
{
NWP_IF_READ_CHECK(g_pCB->FD,TailBuffer,4);
AlignedLengthRecv = AlignedLengthRecv - 4;
}
}
_SlDrvProtectionObjLockWaitForever();
if (
#ifndef SL_TINY_EXT
(SL_OPCODE_SOCKET_ACCEPTASYNCRESPONSE == OpCode) ||
(SL_OPCODE_SOCKET_ACCEPTASYNCRESPONSE_V6 == OpCode) ||
#endif
(SL_OPCODE_SOCKET_CONNECTASYNCRESPONSE == OpCode)
)
{
/* go over the active list if exist to find obj waiting for this Async event */
sd = ((((_SocketResponse_t *)(pAsyncBuf + _SL_RESP_HDR_SIZE))->sd) & BSD_SOCKET_ID_MASK);
}
_SlFindAndSetActiveObj(OpCode, sd);
_SlDrvProtectionObjUnLock();
break;
case RECV_RESP_CLASS:
{
_u8 ExpArgSize; /* Expected size of Recv/Recvfrom arguments */
switch(OpCode)
{
case SL_OPCODE_SOCKET_RECVFROMASYNCRESPONSE:
ExpArgSize = RECVFROM_IPV4_ARGS_SIZE;
break;
#ifndef SL_TINY_EXT
case SL_OPCODE_SOCKET_RECVFROMASYNCRESPONSE_V6:
ExpArgSize = RECVFROM_IPV6_ARGS_SIZE;
break;
#endif
default:
/* SL_OPCODE_SOCKET_RECVASYNCRESPONSE: */
ExpArgSize = RECV_ARGS_SIZE;
}
/* Read first 4 bytes of Recv/Recvfrom response to get SocketId and actual */
/* response data length */
NWP_IF_READ_CHECK(g_pCB->FD, &uBuf.TempBuf[4], RECV_ARGS_SIZE);