-
Notifications
You must be signed in to change notification settings - Fork 83
/
u_cell_sock.c
2149 lines (1975 loc) · 86.1 KB
/
u_cell_sock.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
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Implementation of the sockets API for cellular.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stdio.h" // snprintf()
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // memcpy(), memcmp(), strlen()
#include "limits.h" // UINT16_MAX
#include "u_cfg_sw.h"
#include "u_compiler.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_debug.h"
#include "u_at_client.h"
#include "u_hex_bin_convert.h"
#include "u_sock_errno.h"
#include "u_sock.h"
#include "u_cell_module_type.h"
#include "u_cell_file.h"
#include "u_cell_net.h"
#include "u_cell_private.h"
#include "u_cell_sock.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/** Cross check address sizes.
*/
#if U_SOCK_ADDRESS_STRING_MAX_LENGTH_BYTES < U_CELL_NET_IP_ADDRESS_SIZE
# error U_SOCK_ADDRESS_STRING_MAX_LENGTH_BYTES must be at least as big as U_CELL_NET_IP_ADDRESS_SIZE
#endif
/** The value to use for socket-level options when talking to the
* module (-1 as an int16_t).
*/
#define U_CELL_SOCK_OPT_LEVEL_SOCK_INT16 65535
#ifndef U_CELL_SOCK_DNS_SHOULD_RETRY_MS
/** I have seen DNS queries return ERROR very quickly, likely
* because the module is busy doing something and can't service
* the request. This is the time window within that might
* happen: if it returns at least this quickly with an error
* then it is worth trying again.
*/
# define U_CELL_SOCK_DNS_SHOULD_RETRY_MS 2000
#endif
#ifndef U_CELL_SOCK_SECURE_DELAY_MILLISECONDS
/** I have seen secure socket operations fail if the
* secured socket is used too quickly after security
* has been applied, so wait this long before returning
* after a security profile has been applied.
*/
#define U_CELL_SOCK_SECURE_DELAY_MILLISECONDS 250
#endif
#ifndef U_CELL_SOCK_SARA_R422_DNS_DELAY_MILLISECONDS
/** The gap to leave between being connected to the network
* and performing a DNS look-up for a SARA-R422 module. If
* you do a DNS look-up immediately after connecting then
* SARA-R422 gets a bit upset.
*/
#define U_CELL_SOCK_SARA_R422_DNS_DELAY_MILLISECONDS 500
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** A cellular socket.
*/
typedef struct {
int32_t sockHandle; /**< The handle of the socket instance.
-1 if this socket is not in use. */
uDeviceHandle_t cellHandle; /**< The handle of the cellular instance.
-1 if this socket is not in use. */
uAtClientHandle_t atHandle; /**< The AT client handle for this instance.
NULL if this socket is not in use. */
int32_t sockHandleModule; /**< The handle that the cellular module
uses for the socket instance.
-1 if this socket is not in use. */
uSockProtocol_t protocol; /**< the protocol type, ONLY required to work-around
a peculiarity of LENA-R8. */
volatile int32_t pendingBytes;
void (*pAsyncClosedCallback) (uDeviceHandle_t, int32_t); /**< Set to NULL
if socket is
not in use. */
void (*pDataCallback) (uDeviceHandle_t, int32_t); /**< Set to NULL if
socket is not
in use. */
void (*pClosedCallback) (uDeviceHandle_t, int32_t); /**< Set to NULL
if socket is
not in use. */
bool closedByRemote; /**< Will be set to true if +UUSOCL lands. */
} uCellSockSocket_t;
/** Definition of a URC handler.
*/
typedef struct {
const char *pPrefix;
void (*pHandler) (uAtClientHandle_t, void *);
} uCellSockUrcHandler_t;
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
// Keep track of whether we're initialised or not.
static bool gInitialised = false;
/** The next socket handle to use.
*/
static int32_t gNextSockHandle = 0;
/** The sockets: a nice simple array, nothing fancy.
*/
static uCellSockSocket_t gSockets[U_CELL_SOCK_MAX_NUM_SOCKETS];
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: LIST MANAGEMENT
* -------------------------------------------------------------- */
// Find the entry for the given socket handle.
static uCellSockSocket_t *pFindBySockHandle(int32_t sockHandle)
{
uCellSockSocket_t *pSock = NULL;
for (size_t x = 0; (x < sizeof(gSockets) / sizeof(gSockets[0])) &&
(pSock == NULL); x++) {
if (gSockets[x].sockHandle == sockHandle) {
pSock = &(gSockets[x]);
}
}
return pSock;
}
// Find the entry for the given module socket handle.
//lint -e{818} suppress "could be declared as pointing to const": it is!
static uCellSockSocket_t *pFindBySockHandleModule(const uAtClientHandle_t atHandle,
int32_t sockHandleModule)
{
uCellSockSocket_t *pSock = NULL;
for (size_t x = 0; (x < sizeof(gSockets) / sizeof(gSockets[0])) &&
(pSock == NULL); x++) {
if ((gSockets[x].sockHandle >= 0) &&
(gSockets[x].atHandle == atHandle) &&
(gSockets[x].sockHandleModule == sockHandleModule)) {
pSock = &(gSockets[x]);
}
}
return pSock;
}
// Do AT+USOER, for debug purposes.
static void doUsoer(uAtClientHandle_t atHandle)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+USOER");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+USOER:");
uAtClientResponseStop(atHandle);
uAtClientUnlock(atHandle);
}
// Create a socket entry in the list.
static uCellSockSocket_t *pSockCreate(int32_t sockHandle,
uDeviceHandle_t cellHandle,
uAtClientHandle_t atHandle)
{
uCellSockSocket_t *pSock = NULL;
// Find an empty entry in the list
for (size_t x = 0; (x < sizeof(gSockets) / sizeof(gSockets[0])) &&
(pSock == NULL); x++) {
if (gSockets[x].sockHandle < 0) {
pSock = &(gSockets[x]);
}
}
// Set it up
if (pSock != NULL) {
pSock->sockHandle = sockHandle;
pSock->cellHandle = cellHandle;
pSock->atHandle = atHandle;
pSock->sockHandleModule = -1;
pSock->pendingBytes = 0;
pSock->protocol = 0;
pSock->pAsyncClosedCallback = NULL;
pSock->pDataCallback = NULL;
pSock->pClosedCallback = NULL;
pSock->closedByRemote = false;
}
return pSock;
}
// Free an entry in the list.
static void sockFree(int32_t sockHandle)
{
uCellSockSocket_t *pSock = NULL;
for (size_t x = 0; (x < sizeof(gSockets) / sizeof(gSockets[0])) &&
(pSock == NULL); x++) {
if (gSockets[x].sockHandle == sockHandle) {
pSock = &(gSockets[x]);
pSock->sockHandle = -1;
pSock->cellHandle = NULL;
pSock->atHandle = NULL;
pSock->sockHandleModule = -1;
pSock->pendingBytes = 0;
pSock->protocol = 0;
pSock->pAsyncClosedCallback = NULL;
pSock->pDataCallback = NULL;
pSock->pClosedCallback = NULL;
pSock->closedByRemote = false;
}
}
}
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: URC AND RELATED FUNCTIONS
* -------------------------------------------------------------- */
// Callback trampoline for pending data.
static void dataCallback(const uAtClientHandle_t atHandle,
void *pParameter)
{
//lint -e(507) Suppress size incompatibility: the compiler
// we use for Lint checking is 64 bit so has 8 byte pointers
// and Lint doesn't like them being used to carry 4 byte integers
int32_t sockHandle = U_PTR_TO_INT32(pParameter);
uCellSockSocket_t *pSocket;
(void) atHandle;
if (sockHandle >= 0) {
// Find the entry
pSocket = pFindBySockHandle(sockHandle);
if ((pSocket != NULL) && (pSocket->pDataCallback != NULL)) {
pSocket->pDataCallback(pSocket->cellHandle,
sockHandle);
}
}
}
// Callback trampoline for connection closed.
static void closedCallback(const uAtClientHandle_t atHandle,
void *pParameter)
{
//lint -e(507) Suppress size incompatibility: the compiler
// we use for Lint checking is 64 bit so has 8 byte pointers
// and Lint doesn't like them being used to carry 4 byte integers
int32_t sockHandle = U_PTR_TO_INT32(pParameter);
uCellSockSocket_t *pSocket;
(void) atHandle;
if (sockHandle >= 0) {
// Find the entry
pSocket = pFindBySockHandle(sockHandle);
if (pSocket != NULL) {
if (pSocket->pClosedCallback != NULL) {
pSocket->pClosedCallback(pSocket->cellHandle,
sockHandle);
// Socket is now closed, can lose the callback
pSocket->pClosedCallback = NULL;
}
if (pSocket->pAsyncClosedCallback != NULL) {
pSocket->pAsyncClosedCallback(pSocket->cellHandle,
sockHandle);
// Socket is now closed, lose the
// async closure callback
pSocket->pAsyncClosedCallback = NULL;
}
// Free the entry
sockFree(pSocket->sockHandle);
}
}
}
// Socket Read/Read-From URC.
static void UUSORD_UUSORF_urc(const uAtClientHandle_t atHandle,
void *pUnused)
{
int32_t sockHandleModule;
int32_t dataSizeBytes;
uCellSockSocket_t *pSocket = NULL;
(void) pUnused;
// +UUSORx: <socket>,<length>
sockHandleModule = uAtClientReadInt(atHandle);
dataSizeBytes = uAtClientReadInt(atHandle);
if (sockHandleModule >= 0) {
// Find the entry
pSocket = pFindBySockHandleModule(atHandle,
sockHandleModule);
if (pSocket != NULL) {
// Call the user call-back via the trampoline
if ((dataSizeBytes > 0) &&
(pSocket->pDataCallback != NULL)) {
uAtClientCallback(atHandle,
dataCallback,
U_INT32_TO_PTR(pSocket->sockHandle));
}
pSocket->pendingBytes = dataSizeBytes;
}
}
}
// Callback for Socket Close URC.
static void UUSOCL_urc(const uAtClientHandle_t atHandle,
void *pUnused)
{
int32_t sockHandleModule;
uCellSockSocket_t *pSocket = NULL;
(void) pUnused;
// +UUSOCL: <socket>
sockHandleModule = uAtClientReadInt(atHandle);
if (sockHandleModule >= 0) {
// Find the entry
pSocket = pFindBySockHandleModule(atHandle,
sockHandleModule);
if (pSocket != NULL) {
if (pSocket->pClosedCallback != NULL) {
uAtClientCallback(atHandle,
closedCallback,
U_INT32_TO_PTR(pSocket->sockHandle));
}
pSocket->closedByRemote = true;
}
}
}
/* ----------------------------------------------------------------
* MORE VARIABLES
* -------------------------------------------------------------- */
/** A table of the URC handlers to make set-up easier.
*/
static const uCellSockUrcHandler_t gUrcHandlers[] = {
{"+UUSORD:", UUSORD_UUSORF_urc},
{"+UUSORF:", UUSORD_UUSORF_urc},
{"+UUSOCL:", UUSOCL_urc}
};
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: SOCKET OPTIONS
* -------------------------------------------------------------- */
// Set a socket option that has an integer as a parameter
// returning a (non-negated) value of U_SOCK_Exxx.
static int32_t setOptionInt(const uCellSockSocket_t *pSocket,
int32_t level,
uint32_t option,
const void *pOptionValue,
size_t optionValueLength)
{
int32_t errnoLocal = U_SOCK_EINVAL;
uAtClientHandle_t atHandle = pSocket->atHandle;
if ((pOptionValue != NULL) &&
(optionValueLength >= sizeof(int32_t))) {
if (level == U_SOCK_OPT_LEVEL_SOCK) {
level = U_CELL_SOCK_OPT_LEVEL_SOCK_INT16;
}
// Pass the option transparently through
// to the module which can decide whether
// it likes it or not
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+USOSO=");
uAtClientWriteInt(atHandle, pSocket->sockHandleModule);
uAtClientWriteInt(atHandle, level);
uAtClientWriteInt(atHandle, (int32_t) option);
uAtClientWriteInt(atHandle, *((const int32_t *) pOptionValue));
uAtClientCommandStopReadResponse(atHandle);
if (uAtClientUnlock(atHandle) == 0) {
// All good
errnoLocal = U_SOCK_ENONE;
} else {
// Got an AT interace error, see
// what the module's socket error
// number has to say for debug purposes
doUsoer(atHandle);
}
}
return errnoLocal;
}
// Get a socket option that has an integer as a
// parameter returning a (non-negated) value of
// U_SOCK_Exxx.
static int32_t getOptionInt(const uCellSockSocket_t *pSocket,
int32_t level,
uint32_t option,
void *pOptionValue,
size_t *pOptionValueLength)
{
int32_t errnoLocal = U_SOCK_EINVAL;
uAtClientHandle_t atHandle = pSocket->atHandle;
int32_t x;
if (pOptionValueLength != NULL) {
if (pOptionValue != NULL) {
if (*pOptionValueLength >= sizeof(int32_t)) {
// Get the answer
if (level == U_SOCK_OPT_LEVEL_SOCK) {
level = U_CELL_SOCK_OPT_LEVEL_SOCK_INT16;
}
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+USOGO=");
uAtClientWriteInt(atHandle, pSocket->sockHandleModule);
uAtClientWriteInt(atHandle, level);
uAtClientWriteInt(atHandle, (int32_t) option);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+USOGO:");
x = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
if ((uAtClientUnlock(atHandle) == 0) && (x >= 0)) {
// All good
errnoLocal = U_SOCK_ENONE;
*((int32_t *) pOptionValue) = x;
*pOptionValueLength = sizeof(int32_t);
} else {
// Got an AT interace error, see
// what the module's socket error
// number has to say for debug purposes
doUsoer(atHandle);
}
}
} else {
errnoLocal = U_SOCK_ENONE;
// Caller just wants to know the length required
*pOptionValueLength = sizeof(int32_t);
}
}
return errnoLocal;
}
// Set the linger socket option, returning a
// (non-negated) value of U_SOCK_Exxx.
static int32_t setOptionLinger(const uCellSockSocket_t *pSocket,
const void *pOptionValue,
size_t optionValueLength)
{
int32_t errnoLocal = U_SOCK_EINVAL;
uAtClientHandle_t atHandle = pSocket->atHandle;
int32_t x;
if ((pOptionValue != NULL) &&
(optionValueLength >= sizeof(int32_t))) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+USOSO=");
uAtClientWriteInt(atHandle, pSocket->sockHandleModule);
uAtClientWriteInt(atHandle, U_CELL_SOCK_OPT_LEVEL_SOCK_INT16);
uAtClientWriteInt(atHandle, U_SOCK_OPT_LINGER);
x = ((const uSockLinger_t *) pOptionValue)->onNotOff;
uAtClientWriteInt(atHandle, x);
if (x == 1) {
uAtClientWriteInt(atHandle,
((const uSockLinger_t *) pOptionValue)->lingerSeconds);
}
uAtClientCommandStopReadResponse(atHandle);
if (uAtClientUnlock(atHandle) == 0) {
// All good
errnoLocal = U_SOCK_ENONE;
} else {
// Got an AT interace error, see
// what the module's socket error
// number has to say for debug purposes
doUsoer(atHandle);
}
}
return errnoLocal;
}
// Get the linger socket option, returning a (non-negated)
// value of U_SOCK_Exxx.
static int32_t getOptionLinger(const uCellSockSocket_t *pSocket,
void *pOptionValue,
size_t *pOptionValueLength)
{
int32_t errnoLocal = U_SOCK_EINVAL;
uAtClientHandle_t atHandle = pSocket->atHandle;
int32_t x;
int32_t y = -1;
if (pOptionValueLength != NULL) {
if (pOptionValue != NULL) {
if (*pOptionValueLength >= sizeof(int32_t)) {
// Get the answer
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+USOGO=");
uAtClientWriteInt(atHandle, pSocket->sockHandleModule);
uAtClientWriteInt(atHandle, U_CELL_SOCK_OPT_LEVEL_SOCK_INT16);
uAtClientWriteInt(atHandle, U_SOCK_OPT_LINGER);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+USOGO:");
x = uAtClientReadInt(atHandle);
// Second parameter is only relevant if
// the first is 1
if (x == 1) {
y = uAtClientReadInt(atHandle);
}
uAtClientResponseStop(atHandle);
if (uAtClientUnlock(atHandle) == 0) {
errnoLocal = U_SOCK_EIO;
if (x == 0) {
// All good
errnoLocal = U_SOCK_ENONE;
((uSockLinger_t *) pOptionValue)->onNotOff = x;
*pOptionValueLength = sizeof(uSockLinger_t);
} else if ((x == 1) && (y >= 0)) {
// If x is 1, y must be present
errnoLocal = U_SOCK_ENONE;
((uSockLinger_t *) pOptionValue)->onNotOff = x;
((uSockLinger_t *) pOptionValue)->lingerSeconds = y;
*pOptionValueLength = sizeof(uSockLinger_t);
}
} else {
// Got an AT interace error, see
// what the module's socket error
// number has to say for debug purposes
doUsoer(atHandle);
}
}
} else {
errnoLocal = U_SOCK_ENONE;
// Caller just wants to know the length required
*pOptionValueLength = sizeof(uSockLinger_t);
}
}
return errnoLocal;
}
// Set hex mode on the underlying AT interface on or off.
int32_t setHexMode(uDeviceHandle_t cellHandle, bool hexModeOnNotOff)
{
int32_t errnoLocal = U_SOCK_EINVAL;
uCellPrivateInstance_t *pInstance;
uAtClientHandle_t atHandle;
int32_t state = 0;
if (hexModeOnNotOff) {
state = 1;
}
// Find the instance
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
// Set hex mode
errnoLocal = U_SOCK_EIO;
atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDCONF=");
uAtClientWriteInt(atHandle, 1);
uAtClientWriteInt(atHandle, state);
uAtClientCommandStopReadResponse(atHandle);
if (uAtClientUnlock(atHandle) == 0) {
pInstance->socketsHexMode = (state == 1);
errnoLocal = U_SOCK_ENONE;
}
}
return -errnoLocal;
}
/* ----------------------------------------------------------------
* STATIC FUNCTIONS: MISC
* -------------------------------------------------------------- */
// Do AT+USOCTL for an operation with an integer return value.
static int32_t doUsoctl(uDeviceHandle_t cellHandle, int32_t sockHandle,
int32_t operation)
{
int32_t negErrnoLocallOrValue = -U_SOCK_EINVAL;
uCellPrivateInstance_t *pInstance;
uCellSockSocket_t *pSocket;
uAtClientHandle_t atHandle;
int32_t x;
// Find the instance
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
atHandle = pInstance->atHandle;
// Find the entry
if (sockHandle >= 0) {
pSocket = pFindBySockHandle(sockHandle);
if (pSocket != NULL) {
negErrnoLocallOrValue = -U_SOCK_EIO;
// Do USOCTL 1 to get the last error code
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+USOCTL=");
uAtClientWriteInt(atHandle, pSocket->sockHandleModule);
uAtClientWriteInt(atHandle, operation);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+USOCTL:");
// Skip the first two integers, which
// are just the socket ID and our operation number
// coming back
uAtClientSkipParameters(atHandle, 2);
// Now read the integer we actually want
x = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
if ((uAtClientUnlock(atHandle) == 0) && (x >= 0)) {
negErrnoLocallOrValue = x;
}
}
}
}
return negErrnoLocallOrValue;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: WORKAROUND FOR LINKER ISSUE
* -------------------------------------------------------------- */
void uCellSockPrivateLink()
{
//dummy
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: INIT/DEINIT
* -------------------------------------------------------------- */
// Initialise the cellular sockets layer.
int32_t uCellSockInit()
{
uCellSockSocket_t *pSock = NULL;
if (!gInitialised) {
// Clear the list
for (size_t x = 0; (x < sizeof(gSockets) / sizeof(gSockets[0])); x++) {
pSock = &(gSockets[x]);
pSock->cellHandle = NULL;
pSock->sockHandle = -1;
pSock->sockHandleModule = -1;
pSock->pendingBytes = 0;
pSock->pDataCallback = NULL;
pSock->pClosedCallback = NULL;
}
gInitialised = true;
}
return U_SOCK_ENONE;
}
// Initialise the cellular sockets instance.
int32_t uCellSockInitInstance(uDeviceHandle_t cellHandle)
{
uCellPrivateInstance_t *pInstance;
int32_t errnoLocal = U_SOCK_EINVAL;
if (gInitialised) {
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
errnoLocal = U_SOCK_ENONE;
// Set up the URCs
for (size_t x = 0; (x < sizeof(gUrcHandlers) /
sizeof(gUrcHandlers[0])) &&
(errnoLocal == U_SOCK_ENONE); x++) {
if (uAtClientSetUrcHandler(pInstance->atHandle,
gUrcHandlers[x].pPrefix,
gUrcHandlers[x].pHandler,
NULL) != 0) {
errnoLocal = U_SOCK_ENOMEM;
}
}
}
}
return -errnoLocal;
}
// Deinitialise the cellular sockets layer.
void uCellSockDeinit()
{
if (gInitialised) {
// Nothing to do; URCs are removed by clean-up function
gInitialised = false;
}
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: CREATE/OPEN/CLOSE/CLEAN-UP
* -------------------------------------------------------------- */
// Create a socket.
int32_t uCellSockCreate(uDeviceHandle_t cellHandle,
uSockType_t type,
uSockProtocol_t protocol)
{
int32_t negErrnoLocal = -U_SOCK_EINVAL;
uCellPrivateInstance_t *pInstance;
uAtClientHandle_t atHandle;
uCellSockSocket_t *pSocket;
(void) type;
// Find the instance
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
negErrnoLocal = -U_SOCK_ENOBUFS;
atHandle = pInstance->atHandle;
// Create the entry
pSocket = pSockCreate(gNextSockHandle, cellHandle, atHandle);
gNextSockHandle++;
if (gNextSockHandle < 0) {
gNextSockHandle = 0;
}
if (pSocket != NULL) {
// Create the socket in the cellular module
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+USOCR=");
// Protocol is 6 for TCP or 17 for UDP
uAtClientWriteInt(atHandle, (int32_t) protocol);
// User-specified local port number
if (pInstance->sockNextLocalPort >= 0) {
uAtClientWriteInt(atHandle, pInstance->sockNextLocalPort);
pInstance->sockNextLocalPort = -1;
}
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+USOCR:");
pSocket->sockHandleModule = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
if (uAtClientUnlock(atHandle) == 0) {
// All good
pSocket->protocol = protocol;
negErrnoLocal = pSocket->sockHandle;
} else {
// Free the socket again
sockFree(pSocket->sockHandle);
// See what the module's socket error
// number has to say for debug purposes
doUsoer(atHandle);
}
}
}
return negErrnoLocal;
}
// Connect to a server.
int32_t uCellSockConnect(uDeviceHandle_t cellHandle,
int32_t sockHandle,
const uSockAddress_t *pRemoteAddress)
{
int32_t errnoLocal = U_SOCK_EINVAL;
uCellPrivateInstance_t *pInstance;
uAtClientHandle_t atHandle;
uAtClientDeviceError_t deviceError;
uCellSockSocket_t *pSocket;
char buffer[U_SOCK_ADDRESS_STRING_MAX_LENGTH_BYTES];
char *pRemoteIpAddress;
// Find the instance
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
atHandle = pInstance->atHandle;
// Find the entry
if (sockHandle >= 0) {
pSocket = pFindBySockHandle(sockHandle);
if ((pSocket != NULL) &&
(uSockAddressToString(pRemoteAddress, buffer,
sizeof(buffer)) > 0)) {
pRemoteIpAddress = pUSockDomainRemovePort(buffer);
errnoLocal = U_SOCK_EHOSTUNREACH;
// Connect the socket through the cellular module
// If have seen modules return ERROR to this
// immediately so try a few times
deviceError.type = U_AT_CLIENT_DEVICE_ERROR_TYPE_ERROR;
for (size_t x = 3; (x > 0) &&
(deviceError.type != U_AT_CLIENT_DEVICE_ERROR_TYPE_NO_ERROR);
x--) {
uAtClientLock(atHandle);
// Leave a little longer to connect
uAtClientTimeoutSet(atHandle,
U_CELL_SOCK_CONNECT_TIMEOUT_SECONDS * 1000);
uAtClientCommandStart(atHandle, "AT+USOCO=");
// Write module socket handle
uAtClientWriteInt(atHandle, pSocket->sockHandleModule);
// Write IP address
uAtClientWriteString(atHandle, pRemoteIpAddress, true);
// Write port number
uAtClientWriteInt(atHandle, pRemoteAddress->port);
uAtClientCommandStopReadResponse(atHandle);
uAtClientDeviceErrorGet(atHandle, &deviceError);
if (uAtClientUnlock(atHandle) == 0) {
// All good
errnoLocal = U_SOCK_ENONE;
}
if (deviceError.type != U_AT_CLIENT_DEVICE_ERROR_TYPE_NO_ERROR) {
// Got an AT interace error, see
// what the module's socket error
// number has to say for debug purposes
doUsoer(atHandle);
uPortTaskBlock(1000);
}
}
}
}
}
return -errnoLocal;
}
// Close a socket.
int32_t uCellSockClose(uDeviceHandle_t cellHandle,
int32_t sockHandle,
void (*pCallback) (uDeviceHandle_t,
int32_t))
{
int32_t errnoLocal = U_SOCK_EINVAL;
uCellPrivateInstance_t *pInstance;
uAtClientHandle_t atHandle;
uCellSockSocket_t *pSocket;
uAtClientDeviceError_t deviceError;
int32_t atError = -1;
// Find the instance
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
atHandle = pInstance->atHandle;
// Find the entry
if (sockHandle >= 0) {
pSocket = pFindBySockHandle(sockHandle);
if (pSocket != NULL) {
errnoLocal = U_SOCK_EIO;
// Close the socket through the cellular module
// If have seen modules return ERROR to this
// immediately so try a few times
deviceError.type = U_AT_CLIENT_DEVICE_ERROR_TYPE_ERROR;
for (size_t x = 3; (x > 0) &&
(deviceError.type != U_AT_CLIENT_DEVICE_ERROR_TYPE_NO_ERROR);
x--) {
uAtClientLock(atHandle);
uAtClientTimeoutSet(atHandle,
U_SOCK_CLOSE_TIMEOUT_SECONDS * 1000);
uAtClientCommandStart(atHandle, "AT+USOCL=");
// Write module socket handle
uAtClientWriteInt(atHandle, pSocket->sockHandleModule);
if (!U_CELL_PRIVATE_HAS(pInstance->pModule,
U_CELL_PRIVATE_FEATURE_ASYNC_SOCK_CLOSE)) {
// Asynchronous closure not supported
pCallback = NULL;
}
if (pCallback != NULL) {
// If a callback was given and the module
// supports asynchronous socket closure then
// request it
uAtClientWriteInt(atHandle, 1);
}
uAtClientCommandStopReadResponse(atHandle);
uAtClientDeviceErrorGet(atHandle, &deviceError);
atError = uAtClientUnlock(atHandle);
if (deviceError.type != U_AT_CLIENT_DEVICE_ERROR_TYPE_NO_ERROR) {
uPortTaskBlock(1000);
}
}
if (atError == 0) {
// All good
errnoLocal = U_SOCK_ENONE;
pSocket->pAsyncClosedCallback = pCallback;
if (pCallback == NULL) {
// If no callback was given, or one
// was given and the the module
// doesn't support asynchronous closure,
// call the trampoline from here
uAtClientCallback(atHandle, closedCallback,
U_INT32_TO_PTR(sockHandle));
}
} else {
// Got an AT interace error, see
// what the module's socket error
// number has to say for debug purposes
doUsoer(atHandle);
}
}
}
}
return -errnoLocal;
}
// Clean-up.
void uCellSockCleanup(uDeviceHandle_t cellHandle)
{
uCellPrivateInstance_t *pInstance;
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
// Remove the URCs
for (size_t x = 0; x < sizeof(gUrcHandlers) /
sizeof(gUrcHandlers[0]); x++) {
uAtClientRemoveUrcHandler(pInstance->atHandle,
gUrcHandlers[x].pPrefix);
}
}
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: CONFIGURE
* -------------------------------------------------------------- */
// Set a socket to be blocking or non-blocking.
void uCellSockBlockingSet(uDeviceHandle_t cellHandle,
int32_t sockHandle,
bool isBlocking)
{
(void) cellHandle;
(void) sockHandle;
(void) isBlocking;
// Nothing to do: always non-blocking
}
// Get whether a socket is blocking or not.
bool uCellSockBlockingGet(uDeviceHandle_t cellHandle,
int32_t sockHandle)
{
(void) cellHandle;
(void) sockHandle;
// Always non-blocking.
return false;
}
// Set socket option.
int32_t uCellSockOptionSet(uDeviceHandle_t cellHandle,
int32_t sockHandle,
int32_t level,
uint32_t option,
const void *pOptionValue,
size_t optionValueLength)
{
int32_t errnoLocal = U_SOCK_EINVAL;
uCellPrivateInstance_t *pInstance;
uCellSockSocket_t *pSocket;
// Find the instance
pInstance = pUCellPrivateGetInstance(cellHandle);
if (pInstance != NULL) {
// Find the entry
if (sockHandle >= 0) {
pSocket = pFindBySockHandle(sockHandle);
if (pSocket != NULL) {
if ((optionValueLength == 0) ||
((optionValueLength > 0) && (pOptionValue != NULL))) {
switch (level) {
case U_SOCK_OPT_LEVEL_SOCK: