-
Notifications
You must be signed in to change notification settings - Fork 129
/
socket.h
2061 lines (1700 loc) · 62.9 KB
/
socket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* \file
*
* \brief WINC BSD compatible Socket Interface.
*
* Copyright (c) 2016-2017 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
#ifndef __SOCKET_H__
#define __SOCKET_H__
#ifdef __cplusplus
extern "C" {
#endif
/** \defgroup SocketHeader Socket
* BSD compatible socket interface beftween the host layer and the network
* protocol stacks in the firmware.
* These functions are used by the host application to send or receive
* packets and to do other socket operations.
*/
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
INCLUDES
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
#include "common/include/nm_common.h"
#include "driver/include/m2m_types.h"
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
MACROS
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/**
* @defgroup SocketDefines Defines
* @ingroup SocketHeader
*/
/** @defgroup IPDefines TCP/IP Defines
* @ingroup SocketDefines
* The following list of macros are used to define constants used throughout the socket layer.
* @{
*/
/*
* HOSTNAME_MAX_SIZE is defined here and also in host_drv/socket/include/m2m_socket_host_if.h
* The two definitions must match.
*/
#define HOSTNAME_MAX_SIZE 64
/*!<
Maximum allowed size for a host domain name passed to the function gethostbyname @ref gethostbyname.
command value. Used with the setsockopt function.
*/
#define SOCKET_BUFFER_MAX_LENGTH 1400
/*!<
Maximum allowed size for a socket data buffer. Used with @ref send socket
function to ensure that the buffer sent is within the allowed range.
*/
#define AF_INET 2
/*!<
The AF_INET is the address family used for IPv4. An IPv4 transport address is specified with the @ref sockaddr_in structure.
(It is the only supported type for the current implementation.)
*/
#define SOCK_STREAM 1
/*!<
One of the IPv4 supported socket types for reliable connection-oriented stream connection.
Passed to the @ref socket function for the socket creation operation.
*/
#define SOCK_DGRAM 2
/*!<
One of the IPv4 supported socket types for unreliable connectionless datagram connection.
Passed to the @ref socket function for the socket creation operation.
*/
#define SOCKET_FLAGS_SSL 0x01
/*!<
This flag shall be passed to the socket API for SSL session.
*/
#define TCP_SOCK_MAX (7)
/*!<
Maximum number of simultaneous TCP sockets.
*/
#define UDP_SOCK_MAX 4
/*!<
Maximum number of simultaneous UDP sockets.
*/
#define MAX_SOCKET (TCP_SOCK_MAX + UDP_SOCK_MAX)
/*!<
Maximum number of Sockets.
*/
#define SOL_SOCKET 1
/*!<
Socket option.
Used with the @ref setsockopt function
*/
#define SOL_SSL_SOCKET 2
/*!<
SSL Socket option level.
Used with the @ref setsockopt function
*/
#define SO_SET_UDP_SEND_CALLBACK 0x00
/*!<
Socket option used by the application to enable/disable
the use of UDP send callbacks.
Used with the @ref setsockopt function.
*/
#define IP_ADD_MEMBERSHIP 0x01
/*!<
Set Socket Option Add Membership command value (to join a multicast group).
Used with the @ref setsockopt function.
*/
#define IP_DROP_MEMBERSHIP 0x02
/*!<
Set Socket Option Drop Membership command value (to leave a multicast group).
Used with the @ref setsockopt function.
*/
//@}
/**
* @defgroup TLSDefines TLS Defines
* @ingroup SocketDefines
*/
/** @defgroup SSLSocketOptions TLS Socket Options
* @ingroup TLSDefines
* The following list of macros are used to define SSL Socket options.
* @{
* @sa setsockopt
*/
#define SO_SSL_BYPASS_X509_VERIF 0x01
/*!<
Allow an opened SSL socket to bypass the X509 certificate
verification process.
It is highly required NOT to use this socket option in production
software applications. It is supported for debugging and testing
purposes.
The option value should be casted to int type and it is handled
as a boolean flag.
*/
#define SO_SSL_SNI 0x02
/*!<
Set the Server Name Indicator (SNI) for an SSL socket. The
SNI is a NULL terminated string containing the server name
associated with the connection. It must not exceed the size
of HOSTNAME_MAX_SIZE.
*/
#define SO_SSL_ENABLE_SESSION_CACHING 0x03
/*!<
This option allow the TLS to cache the session information for fast
TLS session establishment in future connections using the
TLS Protocol session resume features.
*/
#define SO_SSL_ENABLE_SNI_VALIDATION 0x04
/*!<
Enable SNI validation against the server's certificate subject
common name. If there is no SNI provided (via the SO_SSL_SNI
option), setting this option does nothing.
*/
//@}
/** @defgroup LegacySSLCipherSuite Legacy names for TLS Cipher Suite IDs
* @ingroup TLSDefines
* The following list of macros MUST NOT be used. Instead use the new names under SSLCipherSuiteID
* @sa sslSetActiveCipherSuites
* @{
*/
#define SSL_ENABLE_RSA_SHA_SUITES 0x01
/*!<
Enable RSA Hmac_SHA based Cipher suites. For example,
TLS_RSA_WITH_AES_128_CBC_SHA
*/
#define SSL_ENABLE_RSA_SHA256_SUITES 0x02
/*!<
Enable RSA Hmac_SHA256 based Cipher suites. For example,
TLS_RSA_WITH_AES_128_CBC_SHA256
*/
#define SSL_ENABLE_DHE_SHA_SUITES 0x04
/*!<
Enable DHE Hmac_SHA based Cipher suites. For example,
TLS_DHE_RSA_WITH_AES_128_CBC_SHA
*/
#define SSL_ENABLE_DHE_SHA256_SUITES 0x08
/*!<
Enable DHE Hmac_SHA256 based Cipher suites. For example,
TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
*/
#define SSL_ENABLE_RSA_GCM_SUITES 0x10
/*!<
Enable RSA AEAD based Cipher suites. For example,
TLS_RSA_WITH_AES_128_GCM_SHA256
*/
#define SSL_ENABLE_DHE_GCM_SUITES 0x20
/*!<
Enable DHE AEAD based Cipher suites. For example,
TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
*/
#define SSL_ENABLE_ALL_SUITES 0x0000003F
/*!<
Enable all possible supported cipher suites.
*/
//@}
/** @defgroup SSLCipherSuiteID TLS Cipher Suite IDs
* @ingroup TLSDefines
* The following list of macros defined the list of supported TLS Cipher suites.
* Each MACRO defines a single Cipher suite.
* @sa m2m_ssl_set_active_ciphersuites
* @{
*/
#define SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA NBIT0
#define SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 NBIT1
#define SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA NBIT2
#define SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 NBIT3
#define SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 NBIT4
#define SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256 NBIT5
#define SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA NBIT6
#define SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 NBIT7
#define SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA NBIT8
#define SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256 NBIT9
#define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA NBIT10
#define SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA NBIT11
#define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 NBIT12
#define SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 NBIT13
#define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256 NBIT14
#define SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 NBIT15
#define SSL_ECC_ONLY_CIPHERS \
(\
SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 \
)
/*!<
All ciphers that use ECC crypto only. This execuldes ciphers that use RSA. They use ECDSA instead.
These ciphers are turned off by default at startup.
The application may enable them if it has an ECC math engine (like ATECC508).
*/
#define SSL_ECC_ALL_CIPHERS \
(\
SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA | \
SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256 | \
SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 \
)
/*!<
All supported ECC Ciphers including those ciphers that depend on RSA and ECC.
These ciphers are turned off by default at startup.
The application may enable them if it has an ECC math engine (like ATECC508).
*/
#define SSL_NON_ECC_CIPHERS_AES_128 \
(\
SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA | \
SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA | \
SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 | \
SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256 \
)
/*!<
All supported AES-128 Ciphers (ECC ciphers are not counted). This is the default active group after startup.
*/
#define SSL_ECC_CIPHERS_AES_256 \
(\
SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA \
)
/*!<
ECC AES-256 supported ciphers.
*/
#define SSL_NON_ECC_CIPHERS_AES_256 \
(\
SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA | \
SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 | \
SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA | \
SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256 \
)
/*!<
AES-256 Ciphers.
This group is disabled by default at startup because the WINC1500 HW Accelerator
supports only AES-128. If the application needs to force AES-256 cipher support,
it could enable them (or any of them) explicitly by calling sslSetActiveCipherSuites.
*/
#define SSL_CIPHER_ALL \
(\
SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA | \
SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA | \
SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 | \
SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256 | \
SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA | \
SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 | \
SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA | \
SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256 | \
SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA | \
SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256 | \
SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 | \
SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 | \
SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA \
)
/*!<
Turn On All TLS Ciphers.
*/
//@}
/**************
Socket Errors
**************/
/**@defgroup SocketErrorCode Error Codes
* @ingroup SocketHeader
* The following list of macros are used to define the possible error codes returned as a result of a call to a socket function.
* Errors are listed in numerical order with the error macro name.
* @{
*/
#define SOCK_ERR_NO_ERROR 0
/*!<
Successful socket operation
*/
#define SOCK_ERR_INVALID_ADDRESS -1
/*!<
Socket address is invalid. The socket operation cannot be completed successfully without specifying a specific address
For example: bind is called without specifying a port number
*/
#define SOCK_ERR_ADDR_ALREADY_IN_USE -2
/*!<
Socket operation cannot bind on the given address. With socket operations, only one IP address per socket is permitted.
Any attempt for a new socket to bind with an IP address already bound to another open socket,
will return the following error code. States that bind operation failed.
*/
#define SOCK_ERR_MAX_TCP_SOCK -3
/*!<
Exceeded the maximum number of TCP sockets. A maximum number of TCP sockets opened simultaneously is defined through TCP_SOCK_MAX.
It is not permitted to exceed that number at socket creation. Identifies that @ref socket operation failed.
*/
#define SOCK_ERR_MAX_UDP_SOCK -4
/*!<
Exceeded the maximum number of UDP sockets. A maximum number of UDP sockets opened simultaneously is defined through UDP_SOCK_MAX.
It is not permitted to exceed that number at socket creation. Identifies that @ref socket operation failed
*/
#define SOCK_ERR_INVALID_ARG -6
/*!<
An invalid argument is passed to a function.
*/
#define SOCK_ERR_MAX_LISTEN_SOCK -7
/*!<
Exceeded the maximum number of TCP passive listening sockets.
Identifies Identifies that @ref listen operation failed.
*/
#define SOCK_ERR_INVALID -9
/*!<
The requested socket operation is not valid in the
current socket state.
For example: @ref accept is called on a TCP socket before @ref bind or @ref listen.
*/
#define SOCK_ERR_ADDR_IS_REQUIRED -11
/*!<
Destination address is required. Failure to provide the socket address required for the socket operation to be completed.
It is generated as an error to the @ref sendto function when the address required to send the data to is not known.
*/
#define SOCK_ERR_CONN_ABORTED -12
/*!<
The socket is closed by the peer. The local socket is
closed also.
*/
#define SOCK_ERR_TIMEOUT -13
/*!<
The socket pending operation has Timedout.
*/
#define SOCK_ERR_BUFFER_FULL -14
/*!<
No buffer space available to be used for the requested socket operation.
*/
#ifdef _NM_BSP_BIG_END
#define _htonl(m) (m)
#define _htons(A) (A)
#else
#define _htonl(m) \
(uint32)(((uint32)(m << 24)) | ((uint32)((m & 0x0000FF00) << 8)) | ((uint32)((m & 0x00FF0000) >> 8)) | ((uint32)(m >> 24)))
/*!<
Convert a 4-byte integer from the host representation to the Network byte order representation.
*/
#define _htons(A) (uint16)((((uint16) (A)) << 8) | (((uint16) (A)) >> 8))
/*!<
Convert a 2-byte integer (short) from the host representation to the Network byte order representation.
*/
#endif
#define _ntohl _htonl
/*!<
Convert a 4-byte integer from the Network byte order representation to the host representation .
*/
#define _ntohs _htons
/*!<
Convert a 2-byte integer from the Network byte order representation to the host representation .
*/
//@}
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
DATA TYPES
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/** @defgroup SocketEnums DataTypes
* @ingroup SocketHeader
* Specific Enumeration-typedefs used for socket operations
* @{ */
/*!
@typedef \
SOCKET
@brief
Definition for socket handler data type.
Socket ID,used with all socket operations to uniquely identify the socket handler.
Such an ID is uniquely assigned at socket creation when calling @ref socket operation.
*/
typedef sint8 SOCKET;
/*!
@struct \
in_addr
@brief
IPv4 address representation.
This structure is used as a placeholder for IPV4 address in other structures.
@see
sockaddr_in
*/
typedef struct{
uint32 s_addr;
/*!<
Network Byte Order representation of the IPv4 address. For example,
the address "192.168.0.10" is represented as 0x0A00A8C0.
*/
}in_addr;
/*!
@struct \
sockaddr
@brief
Generic socket address structure.
@see
sockaddr_in
*/
struct sockaddr{
uint16 sa_family;
/*!<
Socket address family.
*/
uint8 sa_data[14];
/*!<
Maximum size of all the different socket address structures.
*/
};
/*!
@struct \
sockaddr_in
@brief
Socket address structure for IPV4 addresses. Used to specify socket address information to which to connect to.
Can be cast to @ref sockaddr structure.
*/
struct sockaddr_in{
uint16 sin_family;
/*!<
Specifies the address family(AF).
Members of AF_INET address family are IPv4 addresses.
Hence,the only supported value for this is AF_INET.
*/
uint16 sin_port;
/*!<
Port number of the socket.
Network sockets are identified by a pair of IP addresses and port number.
It must be set in the Network Byte Order format , @ref _htons (e.g. _htons(80)).
Can NOT have zero value.
*/
in_addr sin_addr;
/*!<
IP Address of the socket.
The IP address is of type @ref in_addr structure.
Can be set to "0" to accept any IP address for server operation. non zero otherwise.
*/
uint8 sin_zero[8];
/*!<
Padding to make structure the same size as @ref sockaddr.
*/
};
//@}
/**@defgroup AsyncCalback Asynchronous Events
* @ingroup SocketEnums
* Specific Enumeration used for asynchronous operations
* @{ */
/*!
@enum \
tenuSocketCallbackMsgType
@brief
Asynchronous APIs, make use of callback functions, in-order to return back the results once the corresponding socket operation is completed.
Hence resuming the normal execution of the application code while the socket operation returns the results.
Callback functions expect event messages to be passed in, in-order to identify the operation they're returning the results for.
The following enum identifies the type of events that are received in the callback function.
Application Use:
In order for application developers to handle the pending events from the network controller through the callback functions.
A function call must be made to the function @ref m2m_wifi_handle_events at least once for each socket operation.
@see
bind
listen
accept
connect
send
recv
*/
typedef enum{
SOCKET_MSG_BIND = 1,
/*!<
Bind socket event.
*/
SOCKET_MSG_LISTEN,
/*!<
Listen socket event.
*/
SOCKET_MSG_DNS_RESOLVE,
/*!<
DNS Resolution event.
*/
SOCKET_MSG_ACCEPT,
/*!<
Accept socket event.
*/
SOCKET_MSG_CONNECT,
/*!<
Connect socket event.
*/
SOCKET_MSG_RECV,
/*!<
Receive socket event.
*/
SOCKET_MSG_SEND,
/*!<
Send socket event.
*/
SOCKET_MSG_SENDTO,
/*!<
sendto socket event.
*/
SOCKET_MSG_RECVFROM
/*!<
Recvfrom socket event.
*/
}tenuSocketCallbackMsgType;
/*!
@struct \
tstrSocketBindMsg
@brief Socket bind status.
An asynchronous call to the @ref bind socket operation, returns information through this structure in response.
This structure together with the event @ref SOCKET_MSG_BIND are passed in paramters to the callback function.
@see
bind
*/
typedef struct{
sint8 status;
/*!<
The result of the bind operation.
Holding a value of ZERO for a successful bind or otherwise a negative
error code corresponding to the type of error.
*/
}tstrSocketBindMsg;
/*!
@struct \
tstrSocketListenMsg
@brief Socket listen status.
Socket listen information is returned through this structure in response to the asynchronous call to the @ref listen function.
This structure together with the event @ref SOCKET_MSG_LISTEN are passed-in parameters to the callback function.
@see
listen
*/
typedef struct{
sint8 status;
/*!<
Holding a value of ZERO for a successful listen or otherwise a negative
error code corresponding to the type of error.
*/
}tstrSocketListenMsg;
/*!
@struct \
tstrSocketAcceptMsg
@brief Socket accept status.
Socket accept information is returned through this structure in response to the asynchronous call to the @ref accept function.
This structure together with the event @ref SOCKET_MSG_ACCEPT are passed-in parameters to the callback function.
*/
typedef struct{
SOCKET sock;
/*!<
On a successful @ref accept operation, the return information is the socket ID for the accepted connection with the remote peer.
Otherwise a negative error code is returned to indicate failure of the accept operation.
*/
struct sockaddr_in strAddr;
/*!<
Socket address structure for the remote peer.
*/
}tstrSocketAcceptMsg;
/*!
@struct \
tstrSocketConnectMsg
@brief Socket connect status.
Socket connect information is returned through this structure in response to the asynchronous call to the @ref connect socket function.
This structure together with the event @ref SOCKET_MSG_CONNECT are passed-in parameters to the callback function.
*/
typedef struct{
SOCKET sock;
/*!<
Socket ID referring to the socket passed to the connect function call.
*/
sint8 s8Error;
/*!<
Connect error code.
Holding a value of ZERO for a successful connect or otherwise a negative
error code corresponding to the type of error.
*/
}tstrSocketConnectMsg;
/*!
@struct \
tstrSocketRecvMsg
@brief Socket recv status.
Socket receive information is returned through this structure in response to the asynchronous call to the recv or recvfrom socket functions.
This structure together with the events @ref SOCKET_MSG_RECV or @ref SOCKET_MSG_RECVFROM are passed-in parameters to the callback function.
@remark
In case the received data from the remote peer is larger than the USER buffer size defined during the asynchronous call to the @ref recv function, the data is
delivered to the user in a number of consecutive chunks according to the USER Buffer size.
a negative or zero buffer size indicates an error with the following code:
@ref SOCK_ERR_NO_ERROR : Socket connection closed
@ref SOCK_ERR_CONN_ABORTED : Socket connection aborted
@SOCK_ERR_TIMEOUT : Socket receive timed out
*/
typedef struct{
#ifdef ARDUINO
uint32 pu8Buffer;
#else
uint8 *pu8Buffer;
#endif
/*!<
Pointer to the USER buffer (passed to @ref recv and @ref recvfrom function) containing the received data chunk.
*/
sint16 s16BufferSize;
/*!<
The received data chunk size.
Holds a negative value if there is a receive error or ZERO on success upon reception of close socket message.
*/
uint16 u16RemainingSize;
/*!<
The number of bytes remaining in the current @ref recv operation.
*/
struct sockaddr_in strRemoteAddr;
/*!<
Socket address structure for the remote peer. It is valid for @ref SOCKET_MSG_RECVFROM event.
*/
}tstrSocketRecvMsg;
/*!
@typedef \
tpfAppSocketCb
@brief
The main socket application callback function. Applications register their main socket application callback through this function by calling @ref registerSocketCallback.
In response to events received, the following callback function is called to handle the corresponding asynchronous function called. Example: @ref bind, @ref connect,...etc.
@param [in] sock
Socket ID for the callback.
The socket callback function is called whenever a new event is recived in response
to socket operations.
@param [in] u8Msg
Socket event type. Possible values are:
- @ref SOCKET_MSG_BIND
- @ref SOCKET_MSG_LISTEN
- @ref SOCKET_MSG_ACCEPT
- @ref SOCKET_MSG_CONNECT
- @ref SOCKET_MSG_RECV
- @ref SOCKET_MSG_SEND
- @ref SOCKET_MSG_SENDTO
- @ref SOCKET_MSG_RECVFROM
@param [in] pvMsg
Pointer to message structure. Existing types are:
- tstrSocketBindMsg
- tstrSocketListenMsg
- tstrSocketAcceptMsg
- tstrSocketConnectMsg
- tstrSocketRecvMsg
@see
tenuSocketCallbackMsgType
tstrSocketRecvMsg
tstrSocketConnectMsg
tstrSocketAcceptMsg
tstrSocketListenMsg
tstrSocketBindMsg
*/
typedef void (*tpfAppSocketCb) (SOCKET sock, uint8 u8Msg, void * pvMsg);
/*!
@typedef \
tpfAppResolveCb
@brief
DNS resolution callback function.
Applications requiring DNS resolution should register their callback through this function by calling @ref registerSocketCallback.
The following callback is triggered in response to asynchronous call to the @ref gethostbyname function (DNS Resolution callback).
@param [in] pu8DomainName
Domain name of the host.
@param [in] u32ServerIP
Server IPv4 address encoded in NW byte order format. If it is Zero, then the DNS resolution failed.
*/
typedef void (*tpfAppResolveCb) (uint8* pu8DomainName, uint32 u32ServerIP);
/*!
@typedef \
tpfPingCb
@brief PING Callback
The function delivers the ping statistics for the sent ping triggered by calling
m2m_ping_req.
@param [in] u32IPAddr
Destination IP.
@param [in] u32RTT
Round Trip Time.
@param [in] u8ErrorCode
Ping error code. It may be one of:
- PING_ERR_SUCCESS
- PING_ERR_DEST_UNREACH
- PING_ERR_TIMEOUT
*/
typedef void (*tpfPingCb)(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode);
/**@}*/
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
FUNCTION PROTOTYPES
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/** \defgroup SocketAPI Function
* @ingroup SocketHeader
*/
/** @defgroup SocketInitalizationFn socketInit
* @ingroup SocketAPI
* The function performs the necessary initializations for the socket library through the following steps:
- A check made by the global variable gbSocketInit, ensuring that initialization for sockets is performed only once,
in-order to prevent reseting the socket instances already created in the global socket array (gastrSockets).
- Zero initializations to the global socket array (gastrSockets), which holds the list of TCP sockets.
- Registers the socket (Host Interface)hif callback function through the call to the hif_register_cb function.
This facilitates handling all of the socket related functions received through interrupts from the firmware.
*/
/**@{*/
/*!
@fn \
NMI_API void socketInit(void);
@param [in] void
@return void
@remarks
This initialization function must be invoked before any socket operation is performed.
No error codes from this initialization function since the socket array is statically allocated based in the maximum number of
sockets @ref MAX_SOCKET based on the systems capability.
\section Example
This example demonstrates the use of the socketinit for socket initialization for an mqtt chat application.
\code
tstrWifiInitParam param;
int8_t ret;
char topic[strlen(MAIN_CHAT_TOPIC) + MAIN_CHAT_USER_NAME_SIZE + 1];
//Initialize the board.
system_init();
//Initialize the UART console.
configure_console();
// Initialize the BSP.
nm_bsp_init();
----------
// Initialize socket interface.
socketInit();
registerSocketCallback(socket_event_handler, socket_resolve_handler);
// Connect to router.
m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID),
MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL);
\endcode
*/
NMI_API void socketInit(void);
/*!
@fn \
NMI_API void socketDeinit(void);
@brief Socket Layer De-initialization
The function performs the necessary cleanup for the socket library static data
It must be invoked as the last any socket operation is performed on any active sockets.
*/
NMI_API void socketDeinit(void);
/** @} */
/** @defgroup SocketCallbackFn registerSocketCallback
* @ingroup SocketAPI
Register two callback functions one for asynchronous socket events and the other one for DNS callback registering function.
The registered callback functions are used to retrieve information in response to the asynchronous socket functions called.
*/
/**@{*/
/*!
@fn \
NMI_API void registerSocketCallback(tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb);
@param [in] tpfAppSocketCb
Assignment of callback function to the global callback @ref tpfAppSocketCb gpfAppSocketCb. Delivers
socket messages to the host application. In response to the asynchronous function calls, such as @ref bind
@ref listen @ref accept @ref connect
@param [in] tpfAppResolveCb
Assignment of callback function to the global callback @ref tpfAppResolveCb gpfAppResolveCb.
Used for DNS resolving functionalities. The DNS resolving technique is determined by the application
registering the callback.
NULL is assigned when, DNS resolution is not required.
@return void