-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcellular_pktio.c
1543 lines (1310 loc) · 57.6 KB
/
cellular_pktio.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
/*
* FreeRTOS-Cellular-Interface <DEVELOPMENT BRANCH>
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*/
/**
* @brief FreeRTOS Cellular Library common packet I/O functions to assemble packet from comm interface.
*/
#ifndef CELLULAR_DO_NOT_USE_CUSTOM_CONFIG
/* Include custom config file before other headers. */
#include "cellular_config.h"
#endif
#include "cellular_config_defaults.h"
/* Standard includes. */
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "cellular_platform.h"
#include "cellular_types.h"
#include "cellular_internal.h"
#include "cellular_pktio_internal.h"
#include "cellular_common_internal.h"
/*-----------------------------------------------------------*/
#define PKTIO_EVT_MASK_STARTED ( 0x0001UL )
#define PKTIO_EVT_MASK_ABORT ( 0x0002UL )
#define PKTIO_EVT_MASK_ABORTED ( 0x0004UL )
#define PKTIO_EVT_MASK_RX_DATA ( 0x0008UL )
#define PKTIO_EVT_MASK_ALL_EVENTS \
( PKTIO_EVT_MASK_STARTED \
| PKTIO_EVT_MASK_ABORT \
| PKTIO_EVT_MASK_ABORTED \
| PKTIO_EVT_MASK_RX_DATA )
#define FREE_AT_RESPONSE_AND_SET_NULL( pResp ) { ( _Cellular_AtResponseFree( ( pResp ) ) ); ( ( pResp ) = NULL ); }
#define PKTIO_SHUTDOWN_WAIT_INTERVAL_MS ( 10U )
#ifdef CELLULAR_DO_NOT_USE_CUSTOM_CONFIG
#define LOOP_FOREVER() true
#endif
/*-----------------------------------------------------------*/
static void _saveData( char * pLine,
CellularATCommandResponse_t * pResp,
uint32_t dataLen );
static void _saveRawData( char * pLine,
CellularATCommandResponse_t * pResp,
uint32_t dataLen );
static void _saveATData( char * pLine,
CellularATCommandResponse_t * pResp );
static CellularPktStatus_t _processIntermediateResponse( char * pLine,
CellularATCommandResponse_t * pResp,
CellularATCommandType_t atType );
static CellularATCommandResponse_t * _Cellular_AtResponseNew( void );
static void _Cellular_AtResponseFree( CellularATCommandResponse_t * pResp );
static CellularPktStatus_t _Cellular_ProcessLine( CellularContext_t * pContext,
char * pLine,
CellularATCommandResponse_t * pResp,
CellularATCommandType_t atType,
const char * pRespPrefix );
static bool _checkUrcTokenWoPrefix( const CellularContext_t * pContext,
const char * pLine );
static _atRespType_t _getMsgType( CellularContext_t * pContext,
const char * pLine,
const char * pRespPrefix );
static CellularCommInterfaceError_t _Cellular_PktRxCallBack( void * pUserData,
CellularCommInterfaceHandle_t commInterfaceHandle );
static char * _handleLeftoverBuffer( CellularContext_t * pContext );
static char * _Cellular_ReadLine( CellularContext_t * pContext,
uint32_t * pBytesRead,
const CellularATCommandResponse_t * pAtResp );
static CellularPktStatus_t _handleData( char * pStartOfData,
CellularContext_t * pContext,
CellularATCommandResponse_t * pAtResp,
char ** ppLine,
uint32_t bytesRead,
uint32_t * pBytesLeft );
static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext,
CellularATCommandResponse_t ** ppAtResp,
char * pLine );
static void _handleAllReceived( CellularContext_t * pContext,
CellularATCommandResponse_t ** ppAtResp,
char * pData,
uint32_t bytesInBuffer );
static uint32_t _handleRxDataEvent( CellularContext_t * pContext );
static void _pktioReadThread( void * pUserData );
static void _PktioInitProcessReadThreadStatus( CellularContext_t * pContext );
static bool _getNextLine( CellularContext_t * pContext,
char ** ppLine,
uint32_t * pBytesRead,
uint32_t currentLineLength,
CellularPktStatus_t pktStatus );
static bool _handleCallbackResult( CellularContext_t * pContext,
CellularPktStatus_t pktStatus,
char * pLine,
uint32_t * pBytesRead );
static bool _preprocessInputBuffer( CellularContext_t * pContext,
char ** pLine,
uint32_t * pBytesRead );
static CellularPktStatus_t _setPrefixByAtCommandType( CellularContext_t * pContext,
CellularATCommandType_t atType,
const char * pAtRspPrefix );
/*-----------------------------------------------------------*/
static uint32_t _convertCharPtrDistance( const char * pEndPtr,
const char * pStartPtr )
{
int32_t ptrDistance = ( int32_t ) ( pEndPtr - pStartPtr );
return ( uint32_t ) ptrDistance;
}
/*-----------------------------------------------------------*/
static void _saveData( char * pLine,
CellularATCommandResponse_t * pResp,
uint32_t dataLen )
{
CellularATCommandLine_t * pNew = NULL, * pTemp = NULL;
( void ) dataLen;
LogDebug( ( "_saveData : Save data %p with length %u", pLine, ( unsigned int ) dataLen ) );
pNew = ( CellularATCommandLine_t * ) Platform_Malloc( sizeof( CellularATCommandLine_t ) );
CELLULAR_CONFIG_ASSERT( ( pNew != NULL ) );
/* Reuse the pktio buffer instead of allocate. */
pNew->pLine = pLine;
pNew->pNext = NULL;
if( pResp->pItm == NULL )
{
pResp->pItm = pNew;
}
else
{
pTemp = pResp->pItm;
while( pTemp->pNext != NULL )
{
pTemp = pTemp->pNext;
}
pTemp->pNext = pNew;
}
}
/*-----------------------------------------------------------*/
static void _saveRawData( char * pLine,
CellularATCommandResponse_t * pResp,
uint32_t dataLen )
{
LogDebug( ( "Save [%p] %u data to pResp", pLine, ( unsigned int ) dataLen ) );
_saveData( pLine, pResp, dataLen );
}
/*-----------------------------------------------------------*/
static void _saveATData( char * pLine,
CellularATCommandResponse_t * pResp )
{
LogDebug( ( "Save [%s] %u AT data to pResp", pLine, ( unsigned int ) strlen( pLine ) ) );
_saveData( pLine, pResp, ( uint32_t ) ( strlen( pLine ) + 1U ) );
}
/*-----------------------------------------------------------*/
static CellularPktStatus_t _processIntermediateResponse( char * pLine,
CellularATCommandResponse_t * pResp,
CellularATCommandType_t atType )
{
CellularPktStatus_t pkStatus = CELLULAR_PKT_STATUS_PENDING_DATA;
switch( atType )
{
case CELLULAR_AT_WO_PREFIX:
if( pResp->pItm == NULL )
{
_saveATData( pLine, pResp );
}
else
{
/* We already have an intermediate response. */
pkStatus = CELLULAR_PKT_STATUS_INVALID_DATA;
LogError( ( "CELLULAR_AT_WO_PREFIX process intermediate response ERROR: %s, status: %d, previous line %s",
pLine, pkStatus, pResp->pItm->pLine ) );
}
break;
case CELLULAR_AT_WITH_PREFIX:
if( pResp->pItm == NULL )
{
/* The removed code which demonstrate the existence of the prefix has been done in
* function _getMsgType(), so the failure condition here won't be touched.
*/
_saveATData( pLine, pResp );
}
else
{
/* We already have an intermediate response. */
pkStatus = CELLULAR_PKT_STATUS_INVALID_DATA;
LogError( ( "CELLULAR_AT_WITH_PREFIX process intermediate response ERROR: %s, status: %d, previous line %s",
pLine, pkStatus, pResp->pItm->pLine ) );
}
break;
case CELLULAR_AT_MULTI_WITH_PREFIX:
/* The removed code which demonstrate the existence of the prefix has been done in
* function _getMsgType(), so the failure condition here won't be touched.
*/
_saveATData( pLine, pResp );
break;
case CELLULAR_AT_MULTI_WO_PREFIX:
_saveATData( pLine, pResp );
break;
case CELLULAR_AT_MULTI_DATA_WO_PREFIX:
_saveATData( pLine, pResp );
pkStatus = CELLULAR_PKT_STATUS_PENDING_BUFFER;
break;
case CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE:
case CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE:
/* Save the line in the response. */
_saveATData( pLine, pResp );
/* Returns CELLULAR_PKT_STATUS_OK to indicate that the response of the
* command is received. No success result code is expected. Set the response
* status to true here. */
pkStatus = CELLULAR_PKT_STATUS_OK;
pResp->status = true;
break;
default:
/* Unexpected message received when sending the AT command. */
LogInfo( ( "Undefind message received %s when sending AT command type %d.",
pLine, atType ) );
pkStatus = CELLULAR_PKT_STATUS_INVALID_DATA;
break;
}
return pkStatus;
}
/*-----------------------------------------------------------*/
static CellularATCommandResponse_t * _Cellular_AtResponseNew( void )
{
CellularATCommandResponse_t * pNew = NULL;
pNew = ( CellularATCommandResponse_t * ) Platform_Malloc( sizeof( CellularATCommandResponse_t ) );
CELLULAR_CONFIG_ASSERT( ( pNew != NULL ) );
( void ) memset( ( void * ) pNew, 0, sizeof( CellularATCommandResponse_t ) );
return pNew;
}
/*-----------------------------------------------------------*/
/**
* Returns a pointer to the end of the next line
* special-cases the "> " SMS prompt.
*
* Returns NULL if there is no complete line.
*/
static void _Cellular_AtResponseFree( CellularATCommandResponse_t * pResp )
{
CellularATCommandLine_t * pCurrLine = NULL;
CellularATCommandLine_t * pToFree = NULL;
if( pResp != NULL )
{
pCurrLine = pResp->pItm;
while( pCurrLine != NULL )
{
pToFree = pCurrLine;
pCurrLine = pCurrLine->pNext;
/* Reuse the packet io buffer. No need to free pToFree->pLine here. */
Platform_Free( pToFree );
}
Platform_Free( pResp );
}
}
/*-----------------------------------------------------------*/
static CellularPktStatus_t _Cellular_ProcessLine( CellularContext_t * pContext,
char * pLine,
CellularATCommandResponse_t * pResp,
CellularATCommandType_t atType,
const char * pRespPrefix )
{
CellularPktStatus_t pkStatus = CELLULAR_PKT_STATUS_FAILURE;
bool result = false;
const char * const * pTokenSuccessTable = NULL;
const char * const * pTokenErrorTable = NULL;
const char * const * pTokenExtraTable = NULL;
uint32_t tokenSuccessTableSize = 0;
uint32_t tokenErrorTableSize = 0;
uint32_t tokenExtraTableSize = 0;
/* This variable is used in warning message. */
( void ) pRespPrefix;
/* Lock the response mutex when processing the input line. */
PlatformMutex_Lock( &( pContext->PktRespMutex ) );
if( ( pContext->tokenTable.pCellularSrcTokenErrorTable != NULL ) &&
( pContext->tokenTable.pCellularSrcTokenSuccessTable != NULL ) )
{
pTokenSuccessTable = pContext->tokenTable.pCellularSrcTokenSuccessTable;
tokenSuccessTableSize = pContext->tokenTable.cellularSrcTokenSuccessTableSize;
pTokenErrorTable = pContext->tokenTable.pCellularSrcTokenErrorTable;
tokenErrorTableSize = pContext->tokenTable.cellularSrcTokenErrorTableSize;
pTokenExtraTable = pContext->tokenTable.pCellularSrcExtraTokenSuccessTable;
tokenExtraTableSize = pContext->tokenTable.cellularSrcExtraTokenSuccessTableSize;
/* pResp has been checked while allocating memory, so we don't
* need to demonstrate it here.
*/
( void ) Cellular_ATcheckErrorCode( pLine, pTokenExtraTable,
tokenExtraTableSize, &result );
if( result == true )
{
pResp->status = true;
pkStatus = CELLULAR_PKT_STATUS_OK;
LogDebug( ( "Final AT response is SUCCESS [%s] in extra table", pLine ) );
}
else
{
( void ) Cellular_ATcheckErrorCode( pLine, pTokenSuccessTable,
tokenSuccessTableSize, &result );
if( result == true )
{
pResp->status = true;
pkStatus = CELLULAR_PKT_STATUS_OK;
LogDebug( ( "Final AT response is SUCCESS [%s]", pLine ) );
}
}
if( result != true )
{
( void ) Cellular_ATcheckErrorCode( pLine, pTokenErrorTable,
tokenErrorTableSize, &result );
if( result == true )
{
pResp->status = false;
pkStatus = CELLULAR_PKT_STATUS_OK;
}
}
if( result != true )
{
pkStatus = _processIntermediateResponse( pLine, pResp, atType );
}
}
if( ( result == true ) && ( pResp->status == false ) )
{
LogWarn( ( "Modem return ERROR: line %s, cmd : %s, respPrefix %s",
pLine,
( pContext->pCurrentCmd != NULL ? pContext->pCurrentCmd : "NULL" ),
( pRespPrefix != NULL ? pRespPrefix : "NULL" ) ) );
}
PlatformMutex_Unlock( &( pContext->PktRespMutex ) );
return pkStatus;
}
/*-----------------------------------------------------------*/
static bool _checkUrcTokenWoPrefix( const CellularContext_t * pContext,
const char * pLine )
{
bool ret = false;
uint32_t i = 0;
uint32_t urcTokenTableSize = pContext->tokenTable.cellularUrcTokenWoPrefixTableSize;
const char * const * const pUrcTokenTable = pContext->tokenTable.pCellularUrcTokenWoPrefixTable;
if( ( pUrcTokenTable == NULL ) || ( urcTokenTableSize == 0U ) )
{
ret = false;
}
else
{
for( i = 0; i < urcTokenTableSize; i++ )
{
if( strcmp( pLine, pUrcTokenTable[ i ] ) == 0 )
{
ret = true;
break;
}
}
}
return ret;
}
/*-----------------------------------------------------------*/
static _atRespType_t _getMsgType( CellularContext_t * pContext,
const char * pLine,
const char * pRespPrefix )
{
_atRespType_t atRespType = AT_UNDEFINED;
CellularATError_t atStatus = CELLULAR_AT_SUCCESS;
bool inputWithPrefix = false;
bool inputWithSrcPrefix = false;
/* Lock the response mutex when deciding message type. */
PlatformMutex_Lock( &( pContext->PktRespMutex ) );
if( _checkUrcTokenWoPrefix( pContext, pLine ) == true )
{
atRespType = AT_UNSOLICITED;
}
else
{
/* Check if prefix exist in pLine. */
( void ) Cellular_ATIsPrefixPresent( pLine, &inputWithPrefix );
if( ( inputWithPrefix == true ) && ( pRespPrefix != NULL ) )
{
/* Check if this line contains prefix expected in AT command response. */
atStatus = Cellular_ATStrStartWith( pLine, pRespPrefix, &inputWithSrcPrefix );
}
}
if( ( atStatus == CELLULAR_AT_SUCCESS ) && ( atRespType == AT_UNDEFINED ) )
{
if( inputWithPrefix == true )
{
if( ( pContext->PktioAtCmdType != CELLULAR_AT_NO_COMMAND ) && ( inputWithSrcPrefix == true ) )
{
/* Celluar interface is sending AT command and this line contains
* expected prefix in the response. Return AT_SOLICITED here. */
atRespType = AT_SOLICITED;
}
else
{
/* Lines with prefix are considered AT_UNSOLICITED unless the prefix
* is expected in AT command response. */
atRespType = AT_UNSOLICITED;
}
}
else
{
if( pContext->PktioAtCmdType != CELLULAR_AT_NO_COMMAND )
{
/* Cellular interface is waiting for AT command response from
* cellular modem. The token without prefix can be success or error
* token to indicate the AT command status. Return AT_SOLICITED
* here and this line will be parsed in _Cellular_ProcessLine later. */
atRespType = AT_SOLICITED;
}
else
{
/* This line doesn't contain any prefix and cellular interface is
* not sending AT command. Therefore, this line is unexpected.
* Return AT_UNDEFINED here. */
atRespType = AT_UNDEFINED;
}
}
}
PlatformMutex_Unlock( &( pContext->PktRespMutex ) );
return atRespType;
}
/*-----------------------------------------------------------*/
static CellularCommInterfaceError_t _Cellular_PktRxCallBack( void * pUserData,
CellularCommInterfaceHandle_t commInterfaceHandle )
{
const CellularContext_t * pContext = ( CellularContext_t * ) pUserData;
PlatformBaseType_t xHigherPriorityTaskWoken = platformFALSE, xResult = platformFALSE;
CellularCommInterfaceError_t retComm = IOT_COMM_INTERFACE_SUCCESS;
( void ) commInterfaceHandle; /* Comm if is not used in this function. */
/* The context of this function is a ISR. */
if( pContext->pPktioCommEvent == NULL )
{
retComm = IOT_COMM_INTERFACE_BAD_PARAMETER;
}
else
{
xResult = PlatformEventGroup_SetBitsFromISR( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( PlatformEventBits_t ) PKTIO_EVT_MASK_RX_DATA,
&xHigherPriorityTaskWoken );
if( xResult == platformPASS )
{
if( xHigherPriorityTaskWoken == platformTRUE )
{
retComm = IOT_COMM_INTERFACE_SUCCESS;
}
else
{
retComm = IOT_COMM_INTERFACE_BUSY;
}
}
else
{
retComm = IOT_COMM_INTERFACE_FAILURE;
}
}
return retComm;
}
/*-----------------------------------------------------------*/
static char * _handleLeftoverBuffer( CellularContext_t * pContext )
{
char * pRead = NULL; /* Pointer to first empty space in pContext->pktioReadBuf. */
/* Move the leftover data or AT command response to the start of buffer.
* Set the pRead pointer to the empty buffer space. */
LogDebug( ( "moved the partial line/data from %p to %p %u",
pContext->pPktioReadPtr, pContext->pktioReadBuf, ( unsigned int ) pContext->partialDataRcvdLen ) );
( void ) memmove( pContext->pktioReadBuf, pContext->pPktioReadPtr, pContext->partialDataRcvdLen );
pContext->pktioReadBuf[ pContext->partialDataRcvdLen ] = '\0';
pRead = &( pContext->pktioReadBuf[ pContext->partialDataRcvdLen ] );
pContext->pPktioReadPtr = pContext->pktioReadBuf;
return pRead;
}
/*-----------------------------------------------------------*/
/* pBytesRead : bytes read from comm interface. */
/* partialData : leftover bytes in the pktioreadbuf. Not enough to be a command. */
static char * _Cellular_ReadLine( CellularContext_t * pContext,
uint32_t * pBytesRead,
const CellularATCommandResponse_t * pAtResp )
{
char * pAtBuf = NULL; /* The returned start of data. */
char * pRead = NULL; /* pRead is the first empty ptr in the Buffer for comm intf to read. */
uint32_t bytesRead = 0;
uint32_t partialDataRead = pContext->partialDataRcvdLen;
int32_t bufferEmptyLength = ( int32_t ) PKTIO_READ_BUFFER_SIZE;
pAtBuf = pContext->pktioReadBuf;
pRead = pContext->pktioReadBuf;
/* pContext->pPktioReadPtr is valid data start pointer.
* pContext->partialDataRcvdLen is the valid data length need to be handled.
* if pContext->pPktioReadPtr is NULL, valid data start from pContext->pktioReadBuf.
* pAtResp equals NULL indicate that no data is buffered in AT command response and
* data before pPktioReadPtr is invalid data can be recycled. */
if( ( pContext->pPktioReadPtr != NULL ) && ( pContext->pPktioReadPtr != pContext->pktioReadBuf ) &&
( pContext->partialDataRcvdLen != 0U ) && ( pAtResp == NULL ) )
{
pRead = _handleLeftoverBuffer( pContext );
bufferEmptyLength = ( ( int32_t ) PKTIO_READ_BUFFER_SIZE - ( int32_t ) pContext->partialDataRcvdLen );
}
else
{
if( pContext->pPktioReadPtr != NULL )
{
/* There are still valid data before pPktioReadPtr. */
pRead = &( pContext->pPktioReadPtr[ pContext->partialDataRcvdLen ] );
pAtBuf = pContext->pPktioReadPtr;
bufferEmptyLength = ( ( int32_t ) PKTIO_READ_BUFFER_SIZE -
( int32_t ) pContext->partialDataRcvdLen - ( int32_t ) _convertCharPtrDistance( pContext->pPktioReadPtr, pContext->pktioReadBuf ) );
}
else
{
/* There are valid data need to be handled with length pContext->partialDataRcvdLen. */
pRead = &( pContext->pktioReadBuf[ pContext->partialDataRcvdLen ] );
pAtBuf = pContext->pktioReadBuf;
bufferEmptyLength = ( ( int32_t ) PKTIO_READ_BUFFER_SIZE - ( int32_t ) pContext->partialDataRcvdLen );
}
}
if( bufferEmptyLength > 0 )
{
( void ) pContext->pCommIntf->recv( pContext->hPktioCommIntf, ( uint8_t * ) pRead,
( uint32_t ) bufferEmptyLength,
CELLULAR_COMM_IF_RECV_TIMEOUT_MS, &bytesRead );
if( bytesRead > 0U )
{
/* Add a NULL after the bytesRead. This is required for further processing. */
pRead[ bytesRead ] = '\0';
LogDebug( ( "AT Read %u bytes, data[%p]", ( unsigned int ) bytesRead, pRead ) );
/* Set the pBytesRead only when actual bytes read from comm interface. */
*pBytesRead = bytesRead + partialDataRead;
/* Clean the partial data and read pointer. */
pContext->partialDataRcvdLen = 0;
}
else
{
pAtBuf = NULL;
*pBytesRead = 0U;
}
}
else
{
LogError( ( "No empty space from comm if to handle incoming data, reset all parameter for next incoming data." ) );
*pBytesRead = 0;
pContext->partialDataRcvdLen = 0;
pContext->pPktioReadPtr = NULL;
}
return pAtBuf;
}
/*-----------------------------------------------------------*/
static CellularPktStatus_t _handleData( char * pStartOfData,
CellularContext_t * pContext,
CellularATCommandResponse_t * pAtResp,
char ** ppLine,
uint32_t bytesRead,
uint32_t * pBytesLeft )
{
/* Calculate the size of data received so far. */
uint32_t bytesBeforeData = _convertCharPtrDistance( pStartOfData, *ppLine );
CellularPktStatus_t pkStatus = CELLULAR_PKT_STATUS_OK;
uint32_t bytesDataAndLeft = 0;
/* Bytes before pStartOfData is not data, skip the bytes received. */
/* bytesRead = bytesBeforeData( data prefix ) + bytesData + bytesLeft( other AT command response ). */
bytesDataAndLeft = bytesRead - bytesBeforeData;
if( bytesDataAndLeft >= pContext->dataLength )
{
/* Add data to the response linked list. */
_saveRawData( pStartOfData, pAtResp, pContext->dataLength );
/* Advance pLine to a point after data. */
*ppLine = &( pStartOfData[ pContext->dataLength ] );
/* There are more bytes after the data. */
*pBytesLeft = ( bytesDataAndLeft - pContext->dataLength );
LogDebug( ( "_handleData : read buffer buffer %p start %p prefix %d left %d, read total %u",
pContext->pktioReadBuf,
pStartOfData,
( unsigned int ) bytesBeforeData,
( unsigned int ) *pBytesLeft,
( unsigned int ) bytesRead ) );
/* reset the data related variables. */
pContext->dataLength = 0U;
/* Set the pPktioReadPtr to indicate data already handled. */
pContext->pPktioReadPtr = *ppLine;
pContext->partialDataRcvdLen = *pBytesLeft;
}
else
{
/* The data received is partial. Store the start of data in read pointer. */
pContext->pPktioReadPtr = pStartOfData;
pContext->partialDataRcvdLen = bytesDataAndLeft;
pkStatus = CELLULAR_PKT_STATUS_PENDING_BUFFER;
}
return pkStatus;
}
/*-----------------------------------------------------------*/
static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext,
CellularATCommandResponse_t ** ppAtResp,
char * pLine )
{
CellularPktStatus_t pkStatus = CELLULAR_PKT_STATUS_OK;
if( pContext->recvdMsgType == AT_UNSOLICITED )
{
if( pContext->pPktioHandlepktCB != NULL )
{
( void ) pContext->pPktioHandlepktCB( pContext, AT_UNSOLICITED, pLine );
}
}
else if( pContext->recvdMsgType == AT_SOLICITED )
{
if( *ppAtResp == NULL )
{
*ppAtResp = _Cellular_AtResponseNew();
LogDebug( ( "Allocate at response %p", ( void * ) *ppAtResp ) );
}
LogDebug( ( "AT solicited Resp[%s]", pLine ) );
/* Process Line will store the Line data in AT response. */
pkStatus = _Cellular_ProcessLine( pContext, pLine, *ppAtResp, pContext->PktioAtCmdType, pContext->pRespPrefix );
if( pkStatus == CELLULAR_PKT_STATUS_OK )
{
/* Reset the command type. Further response from cellular modem won't be
* regarded as AT_SOLICITED response. */
PlatformMutex_Lock( &( pContext->PktRespMutex ) );
pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND;
pContext->pRespPrefix = NULL;
PlatformMutex_Unlock( &( pContext->PktRespMutex ) );
/* This command is completed. Call the user callback to parse the result. */
if( pContext->pPktioHandlepktCB != NULL )
{
( void ) pContext->pPktioHandlepktCB( pContext, AT_SOLICITED, *ppAtResp );
}
FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp );
}
else if( pkStatus == CELLULAR_PKT_STATUS_PENDING_BUFFER )
{
/* This command expects raw data to be appended to buffer. Check data
* prefix first then store the data if this command has data response. */
}
else if( pkStatus == CELLULAR_PKT_STATUS_PENDING_DATA )
{
/* The command expects more response line. */
}
else
{
/* A unexpected message received when sending the AT command.Try to
* handle it with undefined response callback. */
pContext->recvdMsgType = AT_UNDEFINED;
}
}
else
{
/* This is AT_UNDEFINED when not sending the AT command. */
}
if( pContext->recvdMsgType == AT_UNDEFINED )
{
/* Pktio receives AT_UNDEFINED response from modem. This could be module specific
* response. Call the packet handler callback to handle this message. */
if( pContext->pPktioHandlepktCB != NULL )
{
pkStatus = pContext->pPktioHandlepktCB( pContext, AT_UNDEFINED, pLine );
}
if( pkStatus != CELLULAR_PKT_STATUS_OK )
{
LogError( ( "recvdMsgType is AT_UNDEFINED for Message: %s, cmd %s",
pLine,
( pContext->pCurrentCmd != NULL ? pContext->pCurrentCmd : "NULL" ) ) );
/* Reset the command type. */
PlatformMutex_Lock( &( pContext->PktRespMutex ) );
pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND;
pContext->pRespPrefix = NULL;
PlatformMutex_Unlock( &( pContext->PktRespMutex ) );
/* Clean the read buffer and read pointer. */
( void ) memset( pContext->pktioReadBuf, 0, PKTIO_READ_BUFFER_SIZE + 1U );
pContext->pPktioReadPtr = NULL;
pContext->partialDataRcvdLen = 0;
FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp );
/* Return invalid data error code. */
pkStatus = CELLULAR_PKT_STATUS_INVALID_DATA;
}
else
{
/* The undefined response callback handle this message without problem. */
}
}
return pkStatus;
}
/*-----------------------------------------------------------*/
static bool _findLineInStream( CellularContext_t * pContext,
char * pLine,
uint32_t bytesRead,
uint32_t * pLineLength )
{
bool keepProcess = true;
char * pTempLine = pLine;
uint32_t i = 0;
/* Handle the complete line here. GetMsgType needs a complete Line or longer then maximum prefix line. */
for( i = 0; i < bytesRead; i++ )
{
if( ( pTempLine[ i ] == '\0' ) || ( pTempLine[ i ] == '\r' ) || ( pTempLine[ i ] == '\n' ) )
{
break;
}
}
/* A complete Line is found. */
if( i < bytesRead )
{
pTempLine[ i ] = '\0';
*pLineLength = i;
}
else
{
LogDebug( ( "%p is not a complete line", pTempLine ) );
pContext->pPktioReadPtr = pTempLine;
pContext->partialDataRcvdLen = bytesRead;
keepProcess = false;
}
return keepProcess;
}
/*-----------------------------------------------------------*/
static bool _handleCallbackResult( CellularContext_t * pContext,
CellularPktStatus_t pktStatus,
char * pLine,
uint32_t * pBytesRead )
{
bool keepProcess;
if( pktStatus == CELLULAR_PKT_STATUS_PREFIX_MISMATCH )
{
/* Input buffer is not handled in the callback. pktio should keep processing
* the input buffer. */
keepProcess = true;
}
else if( pktStatus == CELLULAR_PKT_STATUS_SIZE_MISMATCH )
{
/* Input buffer is handled in the callback. The callback expects to be called
* again with more data received. pktio won't keep process this input buffer. */
pContext->pPktioReadPtr = pLine;
pContext->partialDataRcvdLen = *pBytesRead;
keepProcess = false;
}
else if( pktStatus != CELLULAR_PKT_STATUS_OK )
{
/* Modem returns unexpected response. */
LogError( ( "Input buffer callback returns error %d. Clean the read buffer.", pktStatus ) );
/* Clean the read buffer and read pointer. */
( void ) memset( pContext->pktioReadBuf, 0, PKTIO_READ_BUFFER_SIZE + 1U );
pContext->pPktioReadPtr = NULL;
pContext->partialDataRcvdLen = 0;
keepProcess = false;
}
else
{
/* Callback function returns CELLULAR_PKT_STATUS_OK. pktio can keep processing
* the input buffer. */
keepProcess = true;
}
return keepProcess;
}
/*-----------------------------------------------------------*/
static bool _preprocessInputBuffer( CellularContext_t * pContext,
char ** pLine,
uint32_t * pBytesRead )
{
char * pTempLine = *pLine;
bool keepProcess = true;
uint32_t bufferLength = 0;
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
if( pContext->inputBufferCallback != NULL )
{
PlatformMutex_Lock( &( pContext->PktRespMutex ) );
pktStatus = pContext->inputBufferCallback( pContext->pInputBufferCallbackContext,
pTempLine,
*pBytesRead,
&bufferLength );
PlatformMutex_Unlock( &( pContext->PktRespMutex ) );
if( pktStatus == CELLULAR_PKT_STATUS_OK )
{
/* Handle the callback result is CELLULAR_PKT_STATUS_OK in this function.
* Check the bufferLength returned by callback function here. */
if( bufferLength > *pBytesRead )
{
/* The input buffer callback returns incorrect buffer length. */
LogError( ( "Input buffer callback returns bufferLength %u. Modem returns length %u. Clean the read buffer.",
( unsigned int ) bufferLength, ( unsigned int ) *pBytesRead ) );
/* Clean the read buffer and read pointer. */
( void ) memset( pContext->pktioReadBuf, 0, PKTIO_READ_BUFFER_SIZE + 1U );
pContext->pPktioReadPtr = NULL;
pContext->partialDataRcvdLen = 0;
keepProcess = false;
}
else
{
/* The input buffer is handled in the callback successfully. Move
* the read pointer forward. pktio will keep processing the line
* after. */
pTempLine = &( pTempLine[ bufferLength ] );
*pLine = pTempLine;
pContext->pPktioReadPtr = *pLine;
/* Calculate remain bytes in the buffer. */
*pBytesRead = *pBytesRead - bufferLength;
}
}
else
{
keepProcess = _handleCallbackResult( pContext, pktStatus, pTempLine, pBytesRead );
}
}
return keepProcess;
}
/*-----------------------------------------------------------*/
static bool _preprocessLine( CellularContext_t * pContext,
char * pLine,
uint32_t * pBytesRead,
char ** ppStartOfData )
{
char * pTempLine = pLine;
bool keepProcess = true;
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularATCommandDataPrefixCallback_t pktDataPrefixCB = NULL;
void * pDataPrefixCBContext = NULL;
CellularATCommandDataSendPrefixCallback_t pktDataSendPrefixCB = NULL;
void * pDataSendPrefixCBContext = NULL;
/* Acquire the response lock to keep consistency. */
PlatformMutex_Lock( &( pContext->PktRespMutex ) );
pktDataPrefixCB = pContext->pktDataPrefixCB;
pDataPrefixCBContext = pContext->pDataPrefixCBContext;
pktDataSendPrefixCB = pContext->pktDataSendPrefixCB;
pDataSendPrefixCBContext = pContext->pDataSendPrefixCBContext;
PlatformMutex_Unlock( &( pContext->PktRespMutex ) );
/* The line only has change line. */
if( *pBytesRead <= 0U )
{
pContext->pPktioReadPtr = pTempLine;
pContext->partialDataRcvdLen = 0;
keepProcess = false;
}
else
{
if( pktDataSendPrefixCB != NULL )
{
/* Check if the AT command response is the data send prefix.
* Data send prefix is an SRC success token for data send AT command.
* It is used to indicate modem can receive data now. */
/* This function may fix the data stream if the data send prefix is not a line. */
pktStatus = pktDataSendPrefixCB( pDataSendPrefixCBContext, pTempLine, pBytesRead );