You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1988 regels
67 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief WINC BSD compatible Socket Interface.
  6. *
  7. * Copyright (c) 2015 - 2017 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41. #ifndef __SOCKET_H__
  42. #define __SOCKET_H__
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /** \defgroup SocketHeader Socket
  47. * BSD compatible socket interface beftween the host layer and the network
  48. * protocol stacks in the firmware.
  49. * These functions are used by the host application to send or receive
  50. * packets and to do other socket operations.
  51. */
  52. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  53. INCLUDES
  54. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  55. #include "common/include/nm_common.h"
  56. #include "driver/include/m2m_types.h"
  57. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  58. MACROS
  59. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  60. /**
  61. * @defgroup SocketDefines Defines
  62. * @ingroup SocketHeader
  63. */
  64. /** @defgroup IPDefines TCP/IP Defines
  65. * @ingroup SocketDefines
  66. * The following list of macros are used to define constants used throughout the socket layer.
  67. * @{
  68. */
  69. /*
  70. * HOSTNAME_MAX_SIZE is defined here and also in host_drv/socket/include/m2m_socket_host_if.h
  71. * The two definitions must match.
  72. */
  73. #define HOSTNAME_MAX_SIZE 64
  74. /*!<
  75. Maximum allowed size for a host domain name passed to the function gethostbyname @ref gethostbyname.
  76. command value. Used with the setsockopt function.
  77. */
  78. #define SOCKET_BUFFER_MAX_LENGTH 1400
  79. /*!<
  80. Maximum allowed size for a socket data buffer. Used with @ref send socket
  81. function to ensure that the buffer sent is within the allowed range.
  82. */
  83. #define AF_INET 2
  84. /*!<
  85. The AF_INET is the address family used for IPv4. An IPv4 transport address is specified with the @ref sockaddr_in
  86. structure. (It is the only supported type for the current implementation.)
  87. */
  88. #define SOCK_STREAM 1
  89. /*!<
  90. One of the IPv4 supported socket types for reliable connection-oriented stream connection.
  91. Passed to the @ref socket function for the socket creation operation.
  92. */
  93. #define SOCK_DGRAM 2
  94. /*!<
  95. One of the IPv4 supported socket types for unreliable connectionless datagram connection.
  96. Passed to the @ref socket function for the socket creation operation.
  97. */
  98. #define SOCKET_FLAGS_SSL 0x01
  99. /*!<
  100. This flag shall be passed to the socket API for SSL session.
  101. */
  102. #define TCP_SOCK_MAX (7)
  103. /*!<
  104. Maximum number of simultaneous TCP sockets.
  105. */
  106. #define UDP_SOCK_MAX 4
  107. /*!<
  108. Maximum number of simultaneous UDP sockets.
  109. */
  110. #define MAX_SOCKET (TCP_SOCK_MAX + UDP_SOCK_MAX)
  111. /*!<
  112. Maximum number of simultaneous sockets.
  113. */
  114. #define SOL_SOCKET 1
  115. /*!<
  116. Socket option.
  117. Used with the @ref setsockopt function
  118. */
  119. #define SOL_SSL_SOCKET 2
  120. /*!<
  121. SSL Socket option level.
  122. Used with the @ref setsockopt function
  123. */
  124. #define SO_SET_UDP_SEND_CALLBACK 0x00
  125. /*!<
  126. Socket option used by the application to enable/disable
  127. the use of UDP send callbacks.
  128. Used with the @ref setsockopt function.
  129. */
  130. #define IP_ADD_MEMBERSHIP 0x01
  131. /*!<
  132. Set Socket Option Add Membership command value (to join a multicast group).
  133. Used with the @ref setsockopt function.
  134. */
  135. #define IP_DROP_MEMBERSHIP 0x02
  136. /*!<
  137. Set Socket Option Drop Membership command value (to leave a multicast group).
  138. Used with the @ref setsockopt function.
  139. */
  140. //@}
  141. /**
  142. * @defgroup TLSDefines TLS Defines
  143. * @ingroup SocketDefines
  144. */
  145. /** @defgroup SSLSocketOptions TLS Socket Options
  146. * @ingroup TLSDefines
  147. * The following list of macros are used to define SSL Socket options.
  148. * @{
  149. * @sa setsockopt
  150. */
  151. #define SO_SSL_BYPASS_X509_VERIF 0x01
  152. /*!<
  153. Allow an opened SSL socket to bypass the X509 certificate
  154. verification process.
  155. It is highly required NOT to use this socket option in production
  156. software applications. It is supported for debugging and testing
  157. purposes.
  158. The option value should be casted to int type and it is handled
  159. as a boolean flag.
  160. */
  161. #define SO_SSL_SNI 0x02
  162. /*!<
  163. Set the Server Name Indicator (SNI) for an SSL socket. The
  164. SNI is a NULL terminated string containing the server name
  165. associated with the connection. It must not exceed the size
  166. of HOSTNAME_MAX_SIZE.
  167. */
  168. #define SO_SSL_ENABLE_SESSION_CACHING 0x03
  169. /*!<
  170. This option allow the TLS to cache the session information for fast
  171. TLS session establishment in future connections using the
  172. TLS Protocol session resume features.
  173. */
  174. #define SO_SSL_ENABLE_SNI_VALIDATION 0x04
  175. /*!<
  176. Enable SNI validation against the server's certificate subject
  177. common name. If there is no SNI provided (via the SO_SSL_SNI
  178. option), setting this option does nothing.
  179. */
  180. //@}
  181. /** @defgroup LegacySSLCipherSuite Legacy names for TLS Cipher Suite IDs
  182. * @ingroup TLSDefines
  183. * The following list of macros MUST NOT be used. Instead use the new names under SSLCipherSuiteID
  184. * @sa sslSetActiveCipherSuites
  185. * @{
  186. */
  187. #define SSL_ENABLE_RSA_SHA_SUITES 0x01
  188. /*!<
  189. Enable RSA Hmac_SHA based Cipher suites. For example,
  190. TLS_RSA_WITH_AES_128_CBC_SHA
  191. */
  192. #define SSL_ENABLE_RSA_SHA256_SUITES 0x02
  193. /*!<
  194. Enable RSA Hmac_SHA256 based Cipher suites. For example,
  195. TLS_RSA_WITH_AES_128_CBC_SHA256
  196. */
  197. #define SSL_ENABLE_DHE_SHA_SUITES 0x04
  198. /*!<
  199. Enable DHE Hmac_SHA based Cipher suites. For example,
  200. TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  201. */
  202. #define SSL_ENABLE_DHE_SHA256_SUITES 0x08
  203. /*!<
  204. Enable DHE Hmac_SHA256 based Cipher suites. For example,
  205. TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  206. */
  207. #define SSL_ENABLE_RSA_GCM_SUITES 0x10
  208. /*!<
  209. Enable RSA AEAD based Cipher suites. For example,
  210. TLS_RSA_WITH_AES_128_GCM_SHA256
  211. */
  212. #define SSL_ENABLE_DHE_GCM_SUITES 0x20
  213. /*!<
  214. Enable DHE AEAD based Cipher suites. For example,
  215. TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  216. */
  217. #define SSL_ENABLE_ALL_SUITES 0x0000003F
  218. /*!<
  219. Enable all possible supported cipher suites.
  220. */
  221. //@}
  222. /** @defgroup SSLCipherSuiteID TLS Cipher Suite IDs
  223. * @ingroup TLSDefines
  224. * The following list of macros defined the list of supported TLS Cipher suites.
  225. * Each MACRO defines a single Cipher suite.
  226. * @sa m2m_ssl_set_active_ciphersuites
  227. * @{
  228. */
  229. #define SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA NBIT0
  230. #define SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 NBIT1
  231. #define SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA NBIT2
  232. #define SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 NBIT3
  233. #define SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 NBIT4
  234. #define SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256 NBIT5
  235. #define SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA NBIT6
  236. #define SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 NBIT7
  237. #define SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA NBIT8
  238. #define SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256 NBIT9
  239. #define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA NBIT10
  240. #define SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA NBIT11
  241. #define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 NBIT12
  242. #define SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 NBIT13
  243. #define SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256 NBIT14
  244. #define SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 NBIT15
  245. #define SSL_ECC_ONLY_CIPHERS \
  246. (SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  247. /*!<
  248. All ciphers that use ECC crypto only. This execuldes ciphers that use RSA. They use ECDSA instead.
  249. These ciphers are turned off by default at startup.
  250. The application may enable them if it has an ECC math engine (like ATECC508).
  251. */
  252. #define SSL_ECC_ALL_CIPHERS \
  253. (SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 \
  254. | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256 | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 \
  255. | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
  256. /*!<
  257. All supported ECC Ciphers including those ciphers that depend on RSA and ECC.
  258. These ciphers are turned off by default at startup.
  259. The application may enable them if it has an ECC math engine (like ATECC508).
  260. */
  261. #define SSL_NON_ECC_CIPHERS_AES_128 \
  262. (SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA | SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 \
  263. | SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA | SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 \
  264. | SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 | SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256)
  265. /*!<
  266. All supported AES-128 Ciphers (ECC ciphers are not counted). This is the default active group after startup.
  267. */
  268. #define SSL_ECC_CIPHERS_AES_256 (SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA)
  269. /*!<
  270. ECC AES-256 supported ciphers.
  271. */
  272. #define SSL_NON_ECC_CIPHERS_AES_256 \
  273. (SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA | SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 \
  274. | SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA | SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256)
  275. /*!<
  276. AES-256 Ciphers.
  277. This group is disabled by default at startup because the WINC1500 HW Accelerator
  278. supports only AES-128. If the application needs to force AES-256 cipher support,
  279. it could enable them (or any of them) explicitly by calling sslSetActiveCipherSuites.
  280. */
  281. #define SSL_CIPHER_ALL \
  282. (SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA | SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA256 \
  283. | SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA | SSL_CIPHER_DHE_RSA_WITH_AES_128_CBC_SHA256 \
  284. | SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256 | SSL_CIPHER_DHE_RSA_WITH_AES_128_GCM_SHA256 \
  285. | SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA | SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA256 \
  286. | SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA | SSL_CIPHER_DHE_RSA_WITH_AES_256_CBC_SHA256 \
  287. | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 \
  288. | SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256 | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 \
  289. | SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 | SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA)
  290. /*!<
  291. Turn On All TLS Ciphers.
  292. */
  293. //@}
  294. /**************
  295. Socket Errors
  296. **************/
  297. /**@defgroup SocketErrorCode Error Codes
  298. * @ingroup SocketHeader
  299. * The following list of macros are used to define the possible error codes returned as a result of a call to a socket
  300. * function. Errors are listed in numerical order with the error macro name.
  301. * @{
  302. */
  303. #define SOCK_ERR_NO_ERROR 0
  304. /*!<
  305. Successful socket operation
  306. */
  307. #define SOCK_ERR_INVALID_ADDRESS -1
  308. /*!<
  309. Socket address is invalid. The socket operation cannot be completed successfully without specifying a specific
  310. address For example: bind is called without specifying a port number
  311. */
  312. #define SOCK_ERR_ADDR_ALREADY_IN_USE -2
  313. /*!<
  314. Socket operation cannot bind on the given address. With socket operations, only one IP address per socket is
  315. permitted. Any attempt for a new socket to bind with an IP address already bound to another open socket, will return
  316. the following error code. States that bind operation failed.
  317. */
  318. #define SOCK_ERR_MAX_TCP_SOCK -3
  319. /*!<
  320. Exceeded the maximum number of TCP sockets. A maximum number of TCP sockets opened simultaneously is defined through
  321. TCP_SOCK_MAX. It is not permitted to exceed that number at socket creation. Identifies that @ref socket operation
  322. failed.
  323. */
  324. #define SOCK_ERR_MAX_UDP_SOCK -4
  325. /*!<
  326. Exceeded the maximum number of UDP sockets. A maximum number of UDP sockets opened simultaneously is defined through
  327. UDP_SOCK_MAX. It is not permitted to exceed that number at socket creation. Identifies that @ref socket operation
  328. failed
  329. */
  330. #define SOCK_ERR_INVALID_ARG -6
  331. /*!<
  332. An invalid argument is passed to a function.
  333. */
  334. #define SOCK_ERR_MAX_LISTEN_SOCK -7
  335. /*!<
  336. Exceeded the maximum number of TCP passive listening sockets.
  337. Identifies Identifies that @ref listen operation failed.
  338. */
  339. #define SOCK_ERR_INVALID -9
  340. /*!<
  341. The requested socket operation is not valid in the
  342. current socket state.
  343. For example: @ref accept is called on a TCP socket before @ref bind or @ref listen.
  344. */
  345. #define SOCK_ERR_ADDR_IS_REQUIRED -11
  346. /*!<
  347. Destination address is required. Failure to provide the socket address required for the socket operation to be
  348. completed. It is generated as an error to the @ref sendto function when the address required to send the data to is
  349. not known.
  350. */
  351. #define SOCK_ERR_CONN_ABORTED -12
  352. /*!<
  353. The socket is closed by the peer. The local socket is
  354. closed also.
  355. */
  356. #define SOCK_ERR_TIMEOUT -13
  357. /*!<
  358. The socket pending operation has Timedout.
  359. */
  360. #define SOCK_ERR_BUFFER_FULL -14
  361. /*!<
  362. No buffer space available to be used for the requested socket operation.
  363. */
  364. #ifdef _NM_BSP_BIG_END
  365. #define _htonl(m) (m)
  366. #define _htons(A) (A)
  367. #else
  368. #define _htonl(m) \
  369. (uint32)(((uint32)(m << 24)) | ((uint32)((m & 0x0000FF00) << 8)) | ((uint32)((m & 0x00FF0000) >> 8)) \
  370. | ((uint32)(((uint32)m) >> 24)))
  371. /*!<
  372. Convert a 4-byte integer from the host representation to the Network byte order representation.
  373. */
  374. #define _htons(A) (uint16)((((uint16)(A)) << 8) | (((uint16)(A)) >> 8))
  375. /*!<
  376. Convert a 2-byte integer (short) from the host representation to the Network byte order representation.
  377. */
  378. #endif
  379. #define _ntohl _htonl
  380. /*!<
  381. Convert a 4-byte integer from the Network byte order representation to the host representation .
  382. */
  383. #define _ntohs _htons
  384. /*!<
  385. Convert a 2-byte integer from the Network byte order representation to the host representation .
  386. */
  387. //@}
  388. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  389. DATA TYPES
  390. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  391. /** @defgroup SocketEnums DataTypes
  392. * @ingroup SocketHeader
  393. * Specific Enumeration-typedefs used for socket operations
  394. * @{ */
  395. /*!
  396. @typedef \
  397. SOCKET
  398. @brief
  399. Definition for socket handler data type.
  400. Socket ID,used with all socket operations to uniquely identify the socket handler.
  401. Such an ID is uniquely assigned at socket creation when calling @ref socket operation.
  402. */
  403. typedef sint8 SOCKET;
  404. /*!
  405. @struct \
  406. in_addr
  407. @brief
  408. IPv4 address representation.
  409. This structure is used as a placeholder for IPV4 address in other structures.
  410. @see
  411. sockaddr_in
  412. */
  413. typedef struct {
  414. uint32 s_addr;
  415. /*!<
  416. Network Byte Order representation of the IPv4 address. For example,
  417. the address "192.168.0.10" is represented as 0x0A00A8C0.
  418. */
  419. } in_addr;
  420. /*!
  421. @struct \
  422. sockaddr
  423. @brief
  424. Generic socket address structure.
  425. @see
  426. sockaddr_in
  427. */
  428. struct sockaddr {
  429. uint16 sa_family;
  430. /*!<
  431. Socket address family.
  432. */
  433. uint8 sa_data[14];
  434. /*!<
  435. Maximum size of all the different socket address structures.
  436. */
  437. };
  438. /*!
  439. @struct \
  440. sockaddr_in
  441. @brief
  442. Socket address structure for IPV4 addresses. Used to specify socket address information to which to connect to.
  443. Can be cast to @ref sockaddr structure.
  444. */
  445. struct sockaddr_in {
  446. uint16 sin_family;
  447. /*!<
  448. Specifies the address family(AF).
  449. Members of AF_INET address family are IPv4 addresses.
  450. Hence,the only supported value for this is AF_INET.
  451. */
  452. uint16 sin_port;
  453. /*!<
  454. Port number of the socket.
  455. Network sockets are identified by a pair of IP addresses and port number.
  456. It must be set in the Network Byte Order format , @ref _htons (e.g. _htons(80)).
  457. Can NOT have zero value.
  458. */
  459. in_addr sin_addr;
  460. /*!<
  461. IP Address of the socket.
  462. The IP address is of type @ref in_addr structure.
  463. Can be set to "0" to accept any IP address for server operation. non zero otherwise.
  464. */
  465. uint8 sin_zero[8];
  466. /*!<
  467. Padding to make structure the same size as @ref sockaddr.
  468. */
  469. };
  470. //@}
  471. /**@defgroup AsyncCalback Asynchronous Events
  472. * @ingroup SocketEnums
  473. * Specific Enumeration used for asynchronous operations
  474. * @{ */
  475. /*!
  476. @enum \
  477. tenuSocketCallbackMsgType
  478. @brief
  479. Asynchronous APIs, make use of callback functions, in-order to return back the results once the corresponding socket
  480. operation is completed. Hence resuming the normal execution of the application code while the socket operation returns
  481. the results. Callback functions expect event messages to be passed in, in-order to identify the operation they're
  482. returning the results for. The following enum identifies the type of events that are received in the callback function.
  483. Application Use:
  484. In order for application developers to handle the pending events from the network controller through the callback
  485. functions. A function call must be made to the function @ref m2m_wifi_handle_events at least once for each socket
  486. operation.
  487. @see
  488. bind
  489. listen
  490. accept
  491. connect
  492. send
  493. recv
  494. */
  495. typedef enum {
  496. SOCKET_MSG_BIND = 1,
  497. /*!<
  498. Bind socket event.
  499. */
  500. SOCKET_MSG_LISTEN,
  501. /*!<
  502. Listen socket event.
  503. */
  504. SOCKET_MSG_DNS_RESOLVE,
  505. /*!<
  506. DNS Resolution event.
  507. */
  508. SOCKET_MSG_ACCEPT,
  509. /*!<
  510. Accept socket event.
  511. */
  512. SOCKET_MSG_CONNECT,
  513. /*!<
  514. Connect socket event.
  515. */
  516. SOCKET_MSG_RECV,
  517. /*!<
  518. Receive socket event.
  519. */
  520. SOCKET_MSG_SEND,
  521. /*!<
  522. Send socket event.
  523. */
  524. SOCKET_MSG_SENDTO,
  525. /*!<
  526. sendto socket event.
  527. */
  528. SOCKET_MSG_RECVFROM
  529. /*!<
  530. Recvfrom socket event.
  531. */
  532. } tenuSocketCallbackMsgType;
  533. /*!
  534. @struct \
  535. tstrSocketBindMsg
  536. @brief Socket bind status.
  537. An asynchronous call to the @ref bind socket operation, returns information through this structure in response.
  538. This structure together with the event @ref SOCKET_MSG_BIND are passed in parameters to the callback function.
  539. @see
  540. bind
  541. */
  542. typedef struct {
  543. sint8 status;
  544. /*!<
  545. The result of the bind operation.
  546. Holding a value of ZERO for a successful bind or otherwise a negative
  547. error code corresponding to the type of error.
  548. */
  549. } tstrSocketBindMsg;
  550. /*!
  551. @struct \
  552. tstrSocketListenMsg
  553. @brief Socket listen status.
  554. Socket listen information is returned through this structure in response to the asynchronous call to the @ref listen
  555. function. This structure together with the event @ref SOCKET_MSG_LISTEN are passed-in parameters to the callback
  556. function.
  557. @see
  558. listen
  559. */
  560. typedef struct {
  561. sint8 status;
  562. /*!<
  563. Holding a value of ZERO for a successful listen or otherwise a negative
  564. error code corresponding to the type of error.
  565. */
  566. } tstrSocketListenMsg;
  567. /*!
  568. @struct \
  569. tstrSocketAcceptMsg
  570. @brief Socket accept status.
  571. Socket accept information is returned through this structure in response to the asynchronous call to the @ref accept
  572. function. This structure together with the event @ref SOCKET_MSG_ACCEPT are passed-in parameters to the callback
  573. function.
  574. */
  575. typedef struct {
  576. SOCKET sock;
  577. /*!<
  578. On a successful @ref accept operation, the return information is the socket ID for the accepted connection with
  579. the remote peer. Otherwise a negative error code is returned to indicate failure of the accept operation.
  580. */
  581. struct sockaddr_in strAddr;
  582. /*!<
  583. Socket address structure for the remote peer.
  584. */
  585. } tstrSocketAcceptMsg;
  586. /*!
  587. @struct \
  588. tstrSocketConnectMsg
  589. @brief Socket connect status.
  590. Socket connect information is returned through this structure in response to the asynchronous call to the @ref
  591. connect socket function. This structure together with the event @ref SOCKET_MSG_CONNECT are passed-in parameters to the
  592. callback function.
  593. */
  594. typedef struct {
  595. SOCKET sock;
  596. /*!<
  597. Socket ID referring to the socket passed to the connect function call.
  598. */
  599. sint8 s8Error;
  600. /*!<
  601. Connect error code.
  602. Holding a value of ZERO for a successful connect or otherwise a negative
  603. error code corresponding to the type of error.
  604. */
  605. } tstrSocketConnectMsg;
  606. /*!
  607. @struct \
  608. tstrSocketRecvMsg
  609. @brief Socket recv status.
  610. Socket receive information is returned through this structure in response to the asynchronous call to the recv or
  611. recvfrom socket functions. This structure together with the events @ref SOCKET_MSG_RECV or @ref SOCKET_MSG_RECVFROM are
  612. passed-in parameters to the callback function.
  613. @remark
  614. In case the received data from the remote peer is larger than the USER buffer size defined during the asynchronous
  615. call to the @ref recv function, the data is delivered to the user in a number of consecutive chunks according to the
  616. USER Buffer size. a negative or zero buffer size indicates an error with the following code:
  617. @ref SOCK_ERR_NO_ERROR : Socket connection closed
  618. @ref SOCK_ERR_CONN_ABORTED : Socket connection aborted
  619. @SOCK_ERR_TIMEOUT : Socket receive timed out
  620. */
  621. typedef struct {
  622. uint8 *pu8Buffer;
  623. /*!<
  624. Pointer to the USER buffer (passed to @ref recv and @ref recvfrom function) containing the received data chunk.
  625. */
  626. sint16 s16BufferSize;
  627. /*!<
  628. The received data chunk size.
  629. Holds a negative value if there is a receive error or ZERO on success upon reception of close socket message.
  630. */
  631. uint16 u16RemainingSize;
  632. /*!<
  633. The number of bytes remaining in the current @ref recv operation.
  634. */
  635. struct sockaddr_in strRemoteAddr;
  636. /*!<
  637. Socket address structure for the remote peer. It is valid for @ref SOCKET_MSG_RECVFROM event.
  638. */
  639. } tstrSocketRecvMsg;
  640. /*!
  641. @typedef \
  642. tpfAppSocketCb
  643. @brief
  644. The main socket application callback function. Applications register their main socket application
  645. callback through this function by calling @ref registerSocketCallback. In response to events received, the following
  646. callback function is called to handle the corresponding asynchronous function called. Example: @ref bind, @ref
  647. connect,...etc.
  648. @param [in] sock
  649. Socket ID for the callback.
  650. The socket callback function is called whenever a new event is recived in response
  651. to socket operations.
  652. @param [in] u8Msg
  653. Socket event type. Possible values are:
  654. - @ref SOCKET_MSG_BIND
  655. - @ref SOCKET_MSG_LISTEN
  656. - @ref SOCKET_MSG_ACCEPT
  657. - @ref SOCKET_MSG_CONNECT
  658. - @ref SOCKET_MSG_RECV
  659. - @ref SOCKET_MSG_SEND
  660. - @ref SOCKET_MSG_SENDTO
  661. - @ref SOCKET_MSG_RECVFROM
  662. @param [in] pvMsg
  663. Pointer to message structure. Existing types are:
  664. - tstrSocketBindMsg
  665. - tstrSocketListenMsg
  666. - tstrSocketAcceptMsg
  667. - tstrSocketConnectMsg
  668. - tstrSocketRecvMsg
  669. @see
  670. tenuSocketCallbackMsgType
  671. tstrSocketRecvMsg
  672. tstrSocketConnectMsg
  673. tstrSocketAcceptMsg
  674. tstrSocketListenMsg
  675. tstrSocketBindMsg
  676. */
  677. typedef void (*tpfAppSocketCb)(SOCKET sock, uint8 u8Msg, void *pvMsg);
  678. /*!
  679. @typedef \
  680. tpfAppResolveCb
  681. @brief
  682. DNS resolution callback function.
  683. Applications requiring DNS resolution should register their callback through this function by calling @ref
  684. registerSocketCallback. The following callback is triggered in response to asynchronous call to the @ref gethostbyname
  685. function (DNS Resolution callback).
  686. @param [in] pu8DomainName
  687. Domain name of the host.
  688. @param [in] u32ServerIP
  689. Server IPv4 address encoded in NW byte order format. If it is Zero, then the DNS resolution failed.
  690. */
  691. typedef void (*tpfAppResolveCb)(uint8 *pu8DomainName, uint32 u32ServerIP);
  692. /*!
  693. @typedef \
  694. tpfPingCb
  695. @brief PING Callback
  696. The function delivers the ping statistics for the sent ping triggered by calling
  697. m2m_ping_req.
  698. @param [in] u32IPAddr
  699. Destination IP.
  700. @param [in] u32RTT
  701. Round Trip Time.
  702. @param [in] u8ErrorCode
  703. Ping error code. It may be one of:
  704. - PING_ERR_SUCCESS
  705. - PING_ERR_DEST_UNREACH
  706. - PING_ERR_TIMEOUT
  707. */
  708. typedef void (*tpfPingCb)(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode);
  709. /**@}*/
  710. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  711. FUNCTION PROTOTYPES
  712. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  713. /** \defgroup SocketAPI Function
  714. * @ingroup SocketHeader
  715. */
  716. /** @defgroup SocketInitalizationFn socketInit
  717. * @ingroup SocketAPI
  718. * The function performs the necessary initializations for the socket library through the following steps:
  719. - A check made by the global variable gbSocketInit, ensuring that initialization for sockets is performed only once,
  720. in-order to prevent reseting the socket instances already created in the global socket array (gastrSockets).
  721. - Zero initializations to the global socket array (gastrSockets), which holds the list of TCP sockets.
  722. - Registers the socket (Host Interface)hif callback function through the call to the hif_register_cb function.
  723. This facilitates handling all of the socket related functions received through interrupts from the firmware.
  724. */
  725. /**@{*/
  726. /*!
  727. @fn \
  728. NMI_API void socketInit(void);
  729. @param [in] void
  730. @return void
  731. @remarks
  732. This initialization function must be invoked before any socket operation is performed.
  733. No error codes from this initialization function since the socket array is statically allocated based in the maximum
  734. number of sockets @ref MAX_SOCKET based on the systems capability. \section Example This example demonstrates the use of
  735. the socketinit for socket initialization for an mqtt chat application. \code tstrWifiInitParam param; int8_t ret; char
  736. topic[strlen(MAIN_CHAT_TOPIC) + MAIN_CHAT_USER_NAME_SIZE + 1];
  737. //Initialize the board.
  738. system_init();
  739. //Initialize the UART console.
  740. configure_console();
  741. // Initialize the BSP.
  742. nm_bsp_init();
  743. ----------
  744. // Initialize socket interface.
  745. socketInit();
  746. registerSocketCallback(socket_event_handler, socket_resolve_handler);
  747. // Connect to router.
  748. m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID),
  749. MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL);
  750. \endcode
  751. */
  752. NMI_API void socketInit(void);
  753. /*!
  754. @fn \
  755. NMI_API void socketDeinit(void);
  756. @brief Socket Layer De-initialization
  757. The function performs the necessary cleanup for the socket library static data
  758. It must be invoked as the last any socket operation is performed on any active sockets.
  759. */
  760. NMI_API void socketDeinit(void);
  761. /** @} */
  762. /** @defgroup SocketCallbackFn registerSocketCallback
  763. * @ingroup SocketAPI
  764. Register two callback functions one for asynchronous socket events and the other one for DNS callback registering
  765. function. The registered callback functions are used to retrieve information in response to the asynchronous socket
  766. functions called.
  767. */
  768. /**@{*/
  769. /*!
  770. @fn \
  771. NMI_API void registerSocketCallback(tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb);
  772. @param [in] tpfAppSocketCb
  773. Assignment of callback function to the global callback @ref tpfAppSocketCb
  774. gpfAppSocketCb. Delivers socket messages to the host application. In response to the asynchronous function calls, such
  775. as @ref bind
  776. @ref listen @ref accept @ref connect
  777. @param [in] tpfAppResolveCb
  778. Assignment of callback function to the global callback @ref tpfAppResolveCb
  779. gpfAppResolveCb. Used for DNS resolving functionalities. The DNS resolving technique is determined by the application
  780. registering the callback.
  781. NULL is assigned when, DNS resolution is not required.
  782. @return void
  783. @remarks
  784. If any of the socket functionalities is not to be used, NULL is passed in as a parameter.
  785. It must be invoked after socketinit and before other socket layer operations.
  786. \section Example
  787. This example demonstrates the use of the registerSocketCallback to register a socket callback function with DNS
  788. resolution CB set to null for a simple UDP server example. \code tstrWifiInitParam param; int8_t ret; struct sockaddr_in
  789. addr;
  790. // Initialize the board
  791. system_init();
  792. //Initialize the UART console.
  793. configure_console();
  794. // Initialize the BSP.
  795. nm_bsp_init();
  796. // Initialize socket address structure.
  797. addr.sin_family = AF_INET;
  798. addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT);
  799. addr.sin_addr.s_addr = _htonl(MAIN_WIFI_M2M_SERVER_IP);
  800. // Initialize Wi-Fi parameters structure.
  801. memset((uint8_t *)&param, 0, sizeof(tstrWifiInitParam));
  802. // Initialize Wi-Fi driver with data and status callbacks.
  803. param.pfAppWifiCb = wifi_cb;
  804. ret = m2m_wifi_init(&param);
  805. if (M2M_SUCCESS != ret) {
  806. printf("main: m2m_wifi_init call error!(%d)\r\n", ret);
  807. while (1) {
  808. }
  809. }
  810. // Initialize socket module
  811. socketInit();
  812. registerSocketCallback(socket_cb, NULL);
  813. // Connect to router.
  814. m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK,
  815. M2M_WIFI_CH_ALL); \endcode
  816. */
  817. NMI_API void registerSocketCallback(tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb);
  818. /** @} */
  819. /** @defgroup SocketFn socket
  820. * @ingroup SocketAPI
  821. * Synchronous socket allocation function based on the specified socket type. Created sockets are non-blocking and
  822. * their possible types are either TCP or a UDP sockets. The maximum allowed number of TCP sockets is @ref TCP_SOCK_MAX
  823. * sockets while the maximum number of UDP sockets that can be created simultaneously is @ref UDP_SOCK_MAX sockets.
  824. *
  825. */
  826. /**@{*/
  827. /*!
  828. @fn \
  829. NMI_API SOCKET socket(uint16 u16Domain, uint8 u8Type, uint8 u8Flags);
  830. @param [in] u16Domain
  831. Socket family. The only allowed value is AF_INET (IPv4.0) for TCP/UDP sockets.
  832. @param [in] u8Type
  833. Socket type. Allowed values are:
  834. - [SOCK_STREAM](@ref SOCK_STREAM)
  835. - [SOCK_DGRAM](@ref SOCK_DGRAM)
  836. @param [in] u8Flags
  837. Used to specify the socket creation flags. It shall be set to zero for normal TCP/UDP sockets.
  838. It could be @ref SOCKET_FLAGS_SSL if the socket is used for SSL session. The use of the flag
  839. @ref SOCKET_FLAGS_SSL has no meaning in case of UDP sockets.
  840. @pre
  841. The @ref socketInit function must be called once at the beginning of the application to initialize the socket
  842. handler. before any call to the socket function can be made.
  843. @see
  844. connect
  845. bind
  846. listen
  847. accept
  848. recv
  849. recvfrom
  850. send
  851. sendto
  852. close
  853. setsockopt
  854. getsockopt
  855. @return
  856. On successful socket creation, a non-blocking socket type is created and a socket ID is returned
  857. In case of failure the function returns a negative value, identifying one of the socket error codes defined.
  858. For example: @ref SOCK_ERR_INVALID for invalid argument or
  859. @ref SOCK_ERR_MAX_TCP_SOCK if the number of TCP allocated sockets exceeds the number of
  860. available sockets.
  861. @remarks
  862. The socket function must be called a priori to any other related socket functions "e.g. send, recv, close
  863. ..etc" \section Example This example demonstrates the use of the socket function to allocate the socket, returning the
  864. socket handler to be used for other socket operations. Socket creation is dependent on the socket type. \subsection sub1
  865. UDP example
  866. @code
  867. SOCKET UdpServerSocket = -1;
  868. UdpServerSocket = socket(AF_INET, SOCK_DGRAM, 0);
  869. @endcode
  870. \subsection sub2 TCP example
  871. @code
  872. static SOCKET tcp_client_socket = -1;
  873. tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0));
  874. @endcode
  875. \subsection sub3 SSL example
  876. @code
  877. static SOCKET ssl_socket = -1;
  878. ssl_socket = socket(AF_INET, SOCK_STREAM, SOCK_FLAGS_SSL));
  879. @endcode
  880. */
  881. NMI_API SOCKET socket(uint16 u16Domain, uint8 u8Type, uint8 u8Flags);
  882. /** @} */
  883. /** @defgroup BindFn bind
  884. * @ingroup SocketAPI
  885. * Asynchronous bind function associates the provided address and local port to the socket.
  886. * The function can be used with both TCP and UDP sockets it's mandatory to call the @ref bind function before
  887. *starting any UDP or TCP server operation. Upon socket bind completion, the application will receive a @ref
  888. *SOCKET_MSG_BIND message in the socket callback.
  889. */
  890. /**@{*/
  891. /*!
  892. \fn \
  893. NMI_API sint8 bind(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
  894. @param [in] sock
  895. Socket ID, must hold a non negative value.
  896. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  897. argument is passed in.
  898. @param [in] pstrAddr
  899. Pointer to socket address structure "sockaddr_in"
  900. [sockaddr_in](@ref sockaddr_in)
  901. @param [in] u8AddrLen
  902. Size of the given socket address structure in bytes.
  903. @pre
  904. The socket function must be called to allocate a socket before passing the socket ID to the bind function.
  905. @see
  906. socket
  907. connect
  908. listen
  909. accept
  910. recv
  911. recvfrom
  912. send
  913. sendto
  914. @return
  915. The function returns ZERO for successful operations and a negative value otherwise.
  916. The possible error values are:
  917. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR)
  918. Indicating that the operation was successful.
  919. - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG)
  920. Indicating passing invalid arguments such as negative socket ID or NULL socket address structure.
  921. - [SOCK_ERR_INVALID](@ref SOCK_ERR_INVALID)
  922. Indicate socket bind failure.
  923. \section Example
  924. This example demonstrates the call of the bind socket operation after a successful socket operation.
  925. @code
  926. struct sockaddr_in addr;
  927. SOCKET udpServerSocket =-1;
  928. int ret = -1;
  929. if(udpServerSocket == -1)
  930. {
  931. udpServerSocket = socket(AF_INET, SOCK_DGRAM, 0);
  932. if(udpServerSocket >= 0)
  933. {
  934. addr.sin_family = AF_INET;
  935. addr.sin_port = _htons(1234);
  936. addr.sin_addr.s_addr = 0;
  937. ret = bind(udpServerSocket,(struct sockaddr*)&addr,sizeof(addr));
  938. if(ret != 0)
  939. {
  940. printf("Bind Failed. Error code = %d\n",ret);
  941. close(udpServerSocket);
  942. }
  943. }
  944. else
  945. {
  946. printf("UDP Server Socket Creation Failed\n");
  947. return;
  948. }
  949. }
  950. @endcode
  951. */
  952. NMI_API sint8 bind(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
  953. /** @} */
  954. /** @defgroup ListenFn listen
  955. * @ingroup SocketAPI
  956. * After successful socket binding to an IP address and port on the system, start listening on a passive socket for
  957. incoming connections. The socket must be bound on a local port or the listen operation fails. Upon the call to the
  958. asynchronous listen function, response is received through the event [SOCKET_MSG_BIND](@ref SOCKET_MSG_BIND) in the
  959. socket callback. A successful listen means the TCP server operation is active. If a connection is accepted, then the
  960. application socket callback function is notified with the new connected socket through the event @ref
  961. SOCKET_MSG_ACCEPT. Hence there is no need to call the @ref accept function after calling @ref listen.
  962. After a connection is accepted, the user is then required to call the @ref recv to receive any packets transmitted
  963. by the remote host or to receive notification of socket connection termination.
  964. */
  965. /**@{*/
  966. /*!
  967. @fn \
  968. NMI_API sint8 listen(SOCKET sock, uint8 backlog);
  969. @param [in] sock
  970. Socket ID, must hold a non negative value.
  971. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  972. argument is passed in.
  973. @param [in] backlog
  974. Not used by the current implementation.
  975. @pre
  976. The bind function must be called to assign the port number and IP address to the socket before the listen operation.
  977. @see
  978. bind
  979. accept
  980. recv
  981. recvfrom
  982. send
  983. sendto
  984. @return
  985. The function returns ZERO for successful operations and a negative value otherwise.
  986. The possible error values are:
  987. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR)
  988. Indicating that the operation was successful.
  989. - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG)
  990. Indicating passing invalid arguments such as negative socket ID.
  991. - [SOCK_ERR_INVALID](@ref SOCK_ERR_INVALID)
  992. Indicate socket listen failure.
  993. \section Example
  994. This example demonstrates the call of the listen socket operation after a successful socket operation.
  995. @code
  996. static void TCP_Socketcallback(SOCKET sock, uint8 u8Msg, void * pvMsg)
  997. {
  998. int ret =-1;
  999. switch(u8Msg)
  1000. {
  1001. case SOCKET_MSG_BIND:
  1002. {
  1003. tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg*)pvMsg;
  1004. if(pstrBind != NULL)
  1005. {
  1006. if(pstrBind->status == 0)
  1007. {
  1008. ret = listen(sock, 0);
  1009. if(ret <0)
  1010. printf("Listen failure! Error = %d\n",ret);
  1011. }
  1012. else
  1013. {
  1014. M2M_ERR("bind Failure!\n");
  1015. close(sock);
  1016. }
  1017. }
  1018. }
  1019. break;
  1020. case SOCKET_MSG_LISTEN:
  1021. {
  1022. tstrSocketListenMsg *pstrListen = (tstrSocketListenMsg*)pvMsg;
  1023. if(pstrListen != NULL)
  1024. {
  1025. if(pstrListen->status == 0)
  1026. {
  1027. ret = accept(sock,NULL,0);
  1028. }
  1029. else
  1030. {
  1031. M2M_ERR("listen Failure!\n");
  1032. close(sock);
  1033. }
  1034. }
  1035. }
  1036. break;
  1037. case SOCKET_MSG_ACCEPT:
  1038. {
  1039. tstrSocketAcceptMsg *pstrAccept = (tstrSocketAcceptMsg*)pvMsg;
  1040. if(pstrAccept->sock >= 0)
  1041. {
  1042. TcpNotificationSocket = pstrAccept->sock;
  1043. recv(pstrAccept->sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT);
  1044. }
  1045. else
  1046. {
  1047. M2M_ERR("accept failure\n");
  1048. }
  1049. }
  1050. break;
  1051. default:
  1052. break;
  1053. }
  1054. }
  1055. @endcode
  1056. */
  1057. NMI_API sint8 listen(SOCKET sock, uint8 backlog);
  1058. /** @} */
  1059. /** @defgroup AcceptFn accept
  1060. * @ingroup SocketAPI
  1061. * The function has no current implementation. An empty deceleration is used to prevent errors when legacy application
  1062. *code is used. For recent application use, the accept function can be safer as it has no effect and could be safely
  1063. *removed from any application using it.
  1064. */
  1065. /**@{*/
  1066. /*!
  1067. @fn \
  1068. NMI_API sint8 accept(SOCKET sock, struct sockaddr *addr, uint8 *addrlen);
  1069. @param [in] sock
  1070. Socket ID, must hold a non negative value.
  1071. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  1072. argument is passed in.
  1073. @param [in] addr
  1074. Not used in the current implementation.
  1075. @param [in] addrlen
  1076. Not used in the current implementation.
  1077. @return
  1078. The function returns ZERO for successful operations and a negative value otherwise.
  1079. The possible error values are:
  1080. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR)
  1081. Indicating that the operation was successful.
  1082. - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG)
  1083. Indicating passing invalid arguments such as negative socket ID.
  1084. */
  1085. NMI_API sint8 accept(SOCKET sock, struct sockaddr *addr, uint8 *addrlen);
  1086. /** @} */
  1087. /** @defgroup ConnectFn connect
  1088. * @ingroup SocketAPI
  1089. * Establishes a TCP connection with a remote server.
  1090. The asynchronous connect function must be called after receiving a valid socket ID from the @ref socket function.
  1091. The application socket callback function is notified of a successful new socket connection through the event @ref
  1092. SOCKET_MSG_CONNECT. A successful connect means the TCP session is active. The application is then required to make a
  1093. call to the @ref recv to receive any packets transmitted by the remote server, unless the application is interrupted by
  1094. a notification of socket connection termination.
  1095. */
  1096. /**@{*/
  1097. /*!
  1098. @fn \
  1099. NMI_API sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
  1100. @param [in] sock
  1101. Socket ID, must hold a non negative value.
  1102. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  1103. argument is passed in.
  1104. @param [in] pstrAddr
  1105. Address of the remote server.
  1106. @param [in] pstrAddr
  1107. Pointer to socket address structure "sockaddr_in"
  1108. [sockaddr_in](@ref sockaddr_in)
  1109. @param [in] u8AddrLen
  1110. Size of the given socket address structure in bytes.
  1111. Not currently used, implemented for BSD compatibility only.
  1112. @pre
  1113. The socket function must be called to allocate a TCP socket before passing the socket ID to the bind function.
  1114. If the socket is not bound, you do NOT have to call bind before the "connect" function.
  1115. @see
  1116. socket
  1117. recv
  1118. send
  1119. close
  1120. @return
  1121. The function returns ZERO for successful operations and a negative value otherwise.
  1122. The possible error values are:
  1123. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR)
  1124. Indicating that the operation was successful.
  1125. - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG)
  1126. Indicating passing invalid arguments such as negative socket ID or NULL socket address structure.
  1127. - [SOCK_ERR_INVALID](@ref SOCK_ERR_INVALID)
  1128. Indicate socket connect failure.
  1129. \section Example
  1130. The example demonstrates a TCP application, showing how the asynchronous call to the connect function is made through
  1131. the main function and how the callback function handles the @ref SOCKET_MSG_CONNECT event. \subsection sub1 Main
  1132. Function
  1133. @code
  1134. struct sockaddr_in Serv_Addr;
  1135. SOCKET TcpClientSocket =-1;
  1136. int ret = -1
  1137. TcpClientSocket = socket(AF_INET,SOCK_STREAM,0);
  1138. Serv_Addr.sin_family = AF_INET;
  1139. Serv_Addr.sin_port = _htons(1234);
  1140. Serv_Addr.sin_addr.s_addr = inet_addr(SERVER);
  1141. printf("Connected to server via socket %u\n",TcpClientSocket);
  1142. do
  1143. {
  1144. ret = connect(TcpClientSocket,(sockaddr_in*)&Serv_Addr,sizeof(Serv_Addr));
  1145. if(ret != 0)
  1146. {
  1147. printf("Connection Error\n");
  1148. }
  1149. else
  1150. {
  1151. printf("Connection successful.\n");
  1152. break;
  1153. }
  1154. }while(1)
  1155. @endcode
  1156. \subsection sub2 Socket Callback
  1157. @code
  1158. if(u8Msg == SOCKET_MSG_CONNECT)
  1159. {
  1160. tstrSocketConnectMsg *pstrConnect = (tstrSocketConnectMsg*)pvMsg;
  1161. if(pstrConnect->s8Error == 0)
  1162. {
  1163. uint8 acBuffer[GROWL_MSG_SIZE];
  1164. uint16 u16MsgSize;
  1165. printf("Connect success!\n");
  1166. u16MsgSize = FormatMsg(u8ClientID, acBuffer);
  1167. send(sock, acBuffer, u16MsgSize, 0);
  1168. recv(pstrNotification->Socket, (void*)au8Msg,GROWL_DESCRIPTION_MAX_LENGTH, GROWL_RX_TIMEOUT);
  1169. u8Retry = GROWL_CONNECT_RETRY;
  1170. }
  1171. else
  1172. {
  1173. M2M_DBG("Connection Failed, Error: %d\n",pstrConnect->s8Error");
  1174. close(pstrNotification->Socket);
  1175. }
  1176. }
  1177. @endcode
  1178. */
  1179. NMI_API sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen);
  1180. /** @} */
  1181. /** @defgroup ReceiveFn recv
  1182. * @ingroup SocketAPI
  1183. * An asynchronous receive function, used to retrieve data from a TCP stream.
  1184. Before calling the recv function, a successful socket connection status must have been received through any of the
  1185. two socket events [SOCKET_MSG_CONNECT] or [SOCKET_MSG_ACCEPT], from the socket callback. Hence, indicating that the
  1186. socket is already connected to a remote host. The application receives the required data in response to this
  1187. asynchronous call through the reception of the event @ref SOCKET_MSG_RECV in the socket callback.
  1188. Receiving the SOCKET_MSG_RECV message in the callback with zero or negative buffer length indicates the following:
  1189. - SOCK_ERR_NO_ERROR : Socket connection closed
  1190. - SOCK_ERR_CONN_ABORTED : Socket connection aborted
  1191. - SOCK_ERR_TIMEOUT : Socket receive timed out
  1192. The application code is expected to close the socket through the call to the @ref close function upon the appearance
  1193. of the above mentioned errors.
  1194. */
  1195. /**@{*/
  1196. /*!
  1197. @fn \
  1198. NMI_API sint16 recv(SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec);
  1199. @param [in] sock
  1200. Socket ID, must hold a non negative value.
  1201. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  1202. argument is passed in.
  1203. @param [in] pvRecvBuf
  1204. Pointer to a buffer that will hold the received data. The buffer is used
  1205. in the recv callback to deliver the received data to the caller. The buffer must
  1206. be resident in memory (heap or global buffer).
  1207. @param [in] u16BufLen
  1208. The buffer size in bytes.
  1209. @param [in] u32Timeoutmsec
  1210. Timeout for the recv function in milli-seconds. If the value is set to ZERO, the timeout
  1211. will be set to infinite (the recv function waits forever). If the timeout period is
  1212. elapsed with no data received, the socket will get a timeout error.
  1213. @pre
  1214. - The socket function must be called to allocate a TCP socket before passing the socket ID to the recv function.
  1215. - The socket in a connected state is expected to receive data through the socket interface.
  1216. @see
  1217. socket
  1218. connect
  1219. bind
  1220. listen
  1221. recvfrom
  1222. close
  1223. @return
  1224. The function returns ZERO for successful operations and a negative value otherwise.
  1225. The possible error values are:
  1226. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR)
  1227. Indicating that the operation was successful.
  1228. - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG)
  1229. Indicating passing invalid arguments such as negative socket ID or NULL Recieve buffer.
  1230. - [SOCK_ERR_BUFFER_FULL](@ref SOCK_ERR_BUFFER_FULL)
  1231. Indicate socket receive failure.
  1232. \section Example
  1233. The example demonstrates a code snippet for the calling of the recv function in the socket callback upon notification
  1234. of the accept or connect events, and the parsing of the received data when the SOCKET_MSG_RECV event is received.
  1235. @code
  1236. switch(u8Msg)
  1237. {
  1238. case SOCKET_MSG_ACCEPT:
  1239. {
  1240. tstrSocketAcceptMsg *pstrAccept = (tstrSocketAcceptMsg*)pvMsg;
  1241. if(pstrAccept->sock >= 0)
  1242. {
  1243. recv(pstrAccept->sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT);
  1244. }
  1245. else
  1246. {
  1247. M2M_ERR("accept\n");
  1248. }
  1249. }
  1250. break;
  1251. case SOCKET_MSG_RECV:
  1252. {
  1253. tstrSocketRecvMsg *pstrRx = (tstrSocketRecvMsg*)pvMsg;
  1254. if(pstrRx->s16BufferSize > 0)
  1255. {
  1256. recv(sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT);
  1257. }
  1258. else
  1259. {
  1260. printf("Socet recv Error: %d\n",pstrRx->s16BufferSize);
  1261. close(sock);
  1262. }
  1263. }
  1264. break;
  1265. default:
  1266. break;
  1267. }
  1268. }
  1269. @endcode
  1270. */
  1271. NMI_API sint16 recv(SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec);
  1272. /** @} */
  1273. /** @defgroup ReceiveFromSocketFn recvfrom
  1274. * @ingroup SocketAPI
  1275. * Receives data from a UDP Socket.
  1276. *
  1277. * The asynchronous recvfrom function is used to retrieve data from a UDP socket. The socket must already be bound to
  1278. * a local port before a call to the recvfrom function is made (i.e message @ref SOCKET_MSG_BIND is received
  1279. * with successful status in the socket callback).
  1280. *
  1281. * Upon calling the recvfrom function with a successful return code, the application is expected to receive a
  1282. *notification in the socket callback whenever a message is received through the @ref SOCKET_MSG_RECVFROM event.
  1283. *
  1284. * Receiving the SOCKET_MSG_RECVFROM message in the callback with zero, indicates that the socket is closed.
  1285. * Whereby a negative buffer length indicates one of the socket error codes such as socket timeout error
  1286. *@SOCK_ERR_TIMEOUT:
  1287. *
  1288. * The recvfrom callback can also be used to show the IP address of the remote host that sent the frame by
  1289. * using the "strRemoteAddr" element in the @ref tstrSocketRecvMsg structure. (refer to the code example)
  1290. */
  1291. /**@{*/
  1292. /*!
  1293. @fn \
  1294. NMI_API sint16 recvfrom(SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32TimeoutSeconds);
  1295. @param [in] sock
  1296. Socket ID, must hold a non negative value.
  1297. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  1298. argument is passed in.
  1299. @param [in] pvRecvBuf
  1300. Pointer to a buffer that will hold the received data. The buffer shall be used
  1301. in the recv callback to deliver the received data to the caller. The buffer must
  1302. be resident in memory (heap or global buffer).
  1303. @param [in] u16BufLen
  1304. The buffer size in bytes.
  1305. @param [in] u32TimeoutSeconds
  1306. Timeout for the recv function in milli-seconds. If the value is set to ZERO, the timeout
  1307. will be set to infinite (the recv function waits forever).
  1308. @pre
  1309. - The socket function must be called to allocate a UDP socket before passing the socket ID to the recvfrom function.
  1310. - The socket corresponding to the socket ID must be successfully bound to a local port through the call to a @ref
  1311. bind function.
  1312. @see
  1313. socket
  1314. bind
  1315. close
  1316. @return
  1317. The function returns ZERO for successful operations and a negative value otherwise.
  1318. The possible error values are:
  1319. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR)
  1320. Indicating that the operation was successful.
  1321. - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG)
  1322. Indicating passing invalid arguments such as negative socket ID or NULL Receive buffer.
  1323. - [SOCK_ERR_BUFFER_FULL](@ref SOCK_ERR_BUFFER_FULL)
  1324. Indicate socket receive failure.
  1325. \section Example
  1326. The example demonstrates a code snippet for the calling of the recvfrom function in the socket callback upon
  1327. notification of a successful bind event, and the parsing of the received data when the SOCKET_MSG_RECVFROM event is
  1328. received.
  1329. @code
  1330. switch(u8Msg)
  1331. {
  1332. case SOCKET_MSG_BIND:
  1333. {
  1334. tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg*)pvMsg;
  1335. if(pstrBind != NULL)
  1336. {
  1337. if(pstrBind->status == 0)
  1338. {
  1339. recvfrom(sock, gau8SocketTestBuffer, TEST_BUFFER_SIZE, 0);
  1340. }
  1341. else
  1342. {
  1343. M2M_ERR("bind\n");
  1344. }
  1345. }
  1346. }
  1347. break;
  1348. case SOCKET_MSG_RECVFROM:
  1349. {
  1350. tstrSocketRecvMsg *pstrRx = (tstrSocketRecvMsg*)pvMsg;
  1351. if(pstrRx->s16BufferSize > 0)
  1352. {
  1353. //get the remote host address and port number
  1354. uint16 u16port = pstrRx->strRemoteAddr.sin_port;
  1355. uint32 strRemoteHostAddr = pstrRx->strRemoteAddr.sin_addr.s_addr;
  1356. printf("Recieved frame with size = %d.\tHost address=%x, Port number =
  1357. %d\n\n",pstrRx->s16BufferSize,strRemoteHostAddr, u16port);
  1358. ret = recvfrom(sock,gau8SocketTestBuffer,sizeof(gau8SocketTestBuffer),TEST_RECV_TIMEOUT);
  1359. }
  1360. else
  1361. {
  1362. printf("Socet recv Error: %d\n",pstrRx->s16BufferSize);
  1363. ret = close(sock);
  1364. }
  1365. }
  1366. break;
  1367. default:
  1368. break;
  1369. }
  1370. }
  1371. @endcode
  1372. */
  1373. NMI_API sint16 recvfrom(SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec);
  1374. /** @} */
  1375. /** @defgroup SendFn send
  1376. * @ingroup SocketAPI
  1377. * Asynchronous sending function, used to send data on a TCP/UDP socket.
  1378. * Called by the application code when there is outgoing data available required to be sent on a specific socket
  1379. handler.
  1380. * The only difference between this function and the similar @ref sendto function, is the type of socket the data is
  1381. sent on and the parameters passed in.
  1382. * @ref send function is most commonly called for sockets in a connected state.
  1383. * After the data is sent, the socket callback function registered using registerSocketCallback(), is expected to
  1384. receive an event of type
  1385. * @ref SOCKET_MSG_SEND holding information containing the number of data bytes sent.
  1386. */
  1387. /**@{*/
  1388. /*!
  1389. @fn \
  1390. NMI_API sint16 send(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 u16Flags);
  1391. @param [in] sock
  1392. Socket ID, must hold a non negative value.
  1393. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument
  1394. is passed in.
  1395. @param [in] pvSendBuffer
  1396. Pointer to a buffer holding data to be transmitted.
  1397. @param [in] u16SendLength
  1398. The buffer size in bytes.
  1399. @param [in] u16Flags
  1400. Not used in the current implementation.
  1401. @pre
  1402. Sockets must be initialized using socketInit. \n
  1403. For TCP Socket:\n
  1404. Must use a successfully connected Socket (so that the intended recipient address is known ahead of sending the
  1405. data). Hence this function is expected to be called after a successful socket connect operation(in client case or accept
  1406. in the the server case).\n
  1407. For UDP Socket:\n
  1408. UDP sockets most commonly use @ref sendto function, where the destination address is defined. However, in-order
  1409. to send outgoing data using the @ref send function, at least one successful call must be made to the @ref sendto
  1410. function a priori the consecutive calls to the @ref send function, to ensure that the destination address is saved in
  1411. the firmware.
  1412. @see
  1413. socketInit
  1414. recv
  1415. sendto
  1416. socket
  1417. connect
  1418. accept
  1419. sendto
  1420. @warning
  1421. u16SendLength must not exceed @ref SOCKET_BUFFER_MAX_LENGTH. \n
  1422. Use a valid socket identifier through the a prior call to the @ref socket function.
  1423. Must use a valid buffer pointer.
  1424. Successful completion of a call to send() does not guarantee delivery of the message,
  1425. A negative return value indicates only locally-detected errors
  1426. @return
  1427. The function shall return @ref SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the
  1428. error) otherwise.
  1429. */
  1430. NMI_API sint16 send(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 u16Flags);
  1431. /** @} */
  1432. /** @defgroup SendToSocketFn sendto
  1433. * @ingroup SocketAPI
  1434. * Asynchronous sending function, used to send data on a UDP socket.
  1435. * Called by the application code when there is data required to be sent on a UDP socket handler.
  1436. * The application code is expected to receive data from a successful bounded socket node.
  1437. * The only difference between this function and the similar @ref send function, is the type of socket the data is
  1438. * received on. This function works only with UDP sockets. After the data is sent, the socket callback function
  1439. * registered using registerSocketCallback(), is expected to receive an event of type
  1440. * @ref SOCKET_MSG_SENDTO.
  1441. */
  1442. /**@{*/
  1443. /*!
  1444. @fn \
  1445. NMI_API sint16 sendto(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags, struct sockaddr
  1446. *pstrDestAddr, uint8 u8AddrLen);
  1447. @param [in] sock
  1448. Socket ID, must hold a non negative value.
  1449. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  1450. argument is passed in.
  1451. @param [in] pvSendBuffer
  1452. Pointer to a buffer holding data to be transmitted.
  1453. A NULL value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid argument
  1454. is passed in.
  1455. @param [in] u16SendLength
  1456. The buffer size in bytes. It must not exceed @ref SOCKET_BUFFER_MAX_LENGTH.
  1457. @param [in] flags
  1458. Not used in the current implementation
  1459. @param [in] pstrDestAddr
  1460. The destination address.
  1461. @param [in] u8AddrLen
  1462. Destination address length in bytes.
  1463. Not used in the current implementation, only included for BSD compatibility.
  1464. @pre
  1465. Sockets must be initialized using socketInit.
  1466. @see
  1467. socketInit
  1468. recvfrom
  1469. sendto
  1470. socket
  1471. connect
  1472. accept
  1473. send
  1474. @warning
  1475. u16SendLength must not exceed @ref SOCKET_BUFFER_MAX_LENGTH. \n
  1476. Use a valid socket (returned from socket ).
  1477. A valid buffer pointer must be used (not NULL). \n
  1478. Successful completion of a call to sendto() does not guarantee delivery of the message,
  1479. A negative return value indicates only locally-detected errors
  1480. @return
  1481. The function returns @ref SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error)
  1482. otherwise.
  1483. */
  1484. NMI_API sint16 sendto(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags,
  1485. struct sockaddr *pstrDestAddr, uint8 u8AddrLen);
  1486. /** @} */
  1487. /** @defgroup CloseSocketFn close
  1488. * @ingroup SocketAPI
  1489. * Synchronous close function, releases all the socket assigned resources.
  1490. */
  1491. /**@{*/
  1492. /*!
  1493. @fn \
  1494. NMI_API sint8 close(SOCKET sock);
  1495. @param [in] sock
  1496. Socket ID, must hold a non negative value.
  1497. A negative value will return a socket error @ref SOCK_ERR_INVALID_ARG. Indicating that an invalid
  1498. argument is passed in.
  1499. @pre
  1500. Sockets must be initialized through the call of the socketInit function.
  1501. @ref close is called only for valid socket identifiers created through the @ref socket function.
  1502. @warning
  1503. If @ref close is called while there are still pending messages (sent or received ) they will be discarded.
  1504. @see
  1505. socketInit
  1506. socket
  1507. @return
  1508. The function returned @ref SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error)
  1509. otherwise.
  1510. */
  1511. NMI_API sint8 close(SOCKET sock);
  1512. /** @} */
  1513. /** @defgroup InetAddressFn nmi_inet_addr
  1514. * @ingroup SocketAPI
  1515. * Synchronous function which returns a BSD socket compliant Internet Protocol (IPv4) socket address.
  1516. * This IPv4 address in the input string parameter could either be specified as a host name, or as a numeric string
  1517. * representation like n.n.n.n known as the IPv4 dotted-decimal format (i.e. "192.168.10.1"). This function is used
  1518. * whenever an ip address needs to be set in the proper format (i.e. for the @ref tstrM2MIPConfig structure).
  1519. */
  1520. /**@{*/
  1521. /*!
  1522. @fn \
  1523. NMI_API uint32 nmi_inet_addr(char *pcIpAddr);
  1524. @param [in] pcIpAddr
  1525. A null terminated string containing the IP address in IPv4 dotted-decimal address.
  1526. @return
  1527. Unsigned 32-bit integer representing the IP address in Network byte order
  1528. (eg. "192.168.10.1" will be expressed as 0x010AA8C0).
  1529. */
  1530. NMI_API uint32 nmi_inet_addr(char *pcIpAddr);
  1531. /** @} */
  1532. /** @defgroup gethostbynameFn gethostbyname
  1533. * @ingroup SocketAPI
  1534. * Asynchronous DNS resolving function. This function use DNS to resolve a domain name into the corresponding IP
  1535. * address. A call to this function will cause a DNS request to be sent and the response will be delivered to the DNS
  1536. * callback function registered using registerSocketCallback()
  1537. */
  1538. /**@{*/
  1539. /*!
  1540. @fn \
  1541. NMI_API sint8 gethostbyname(uint8 * pcHostName);
  1542. @param [in] pcHostName
  1543. NULL terminated string containing the domain name for the remote host.
  1544. Its size must not exceed [HOSTNAME_MAX_SIZE](@ref HOSTNAME_MAX_SIZE).
  1545. @see
  1546. registerSocketCallback
  1547. @warning
  1548. Successful completion of a call to gethostbyname() does not guarantee success of the DNS request,
  1549. a negative return value indicates only locally-detected errors
  1550. @return
  1551. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR)
  1552. - [SOCK_ERR_INVALID_ARG](@ref SOCK_ERR_INVALID_ARG)
  1553. */
  1554. NMI_API sint8 gethostbyname(uint8 *pcHostName);
  1555. /** @} */
  1556. /** @defgroup sslEnableCertExpirationCheckFn sslEnableCertExpirationCheck
  1557. * @ingroup SocketAPI
  1558. * Configure the behavior of the SSL Library for Certificate Expiry Validation.
  1559. */
  1560. /**@{*/
  1561. /*!
  1562. @fn \
  1563. NMI_API sint8 sslEnableCertExpirationCheck(tenuSslCertExpSettings enuValidationSetting);
  1564. @param [in] enuValidationSetting
  1565. See @ref tenuSslCertExpSettings for details.
  1566. @return
  1567. - [SOCK_ERR_NO_ERROR](@ref SOCK_ERR_NO_ERROR) for successful operation and negative error code otherwise.
  1568. @sa tenuSslCertExpSettings
  1569. */
  1570. NMI_API sint8 sslEnableCertExpirationCheck(tenuSslCertExpSettings enuValidationSetting);
  1571. /** @} */
  1572. /** @defgroup SetSocketOptionFn setsockopt
  1573. * @ingroup SocketAPI
  1574. *The setsockopt() function shall set the option specified by the option_name
  1575. * argument, at the protocol level specified by the level argument, to the value
  1576. * pointed to by the option_value argument for the socket specified by the socket argument.
  1577. *
  1578. * <p>Possible protocol level values supported are @ref SOL_SOCKET and @ref SOL_SSL_SOCKET.
  1579. * Possible options when the protocol level is @ref SOL_SOCKET :</p>
  1580. * <table style="width: 100%">
  1581. * <tr>
  1582. * <td style="height: 22px"><strong>@ref SO_SET_UDP_SEND_CALLBACK</strong></td>
  1583. * <td style="height: 22px">Enable/Disable callback messages for sendto().
  1584. * Since UDP is unreliable by default the user maybe interested (or not) in
  1585. * receiving a message of @ref SOCKET_MSG_SENDTO for each call of sendto().
  1586. * Enabled if option value equals @ref TRUE, disabled otherwise.</td>
  1587. * </tr>
  1588. * <tr>
  1589. * <td><strong>@ref IP_ADD_MEMBERSHIP</strong></td>
  1590. * <td>Valid for UDP sockets. This option is used to receive frames sent to
  1591. * a multicast group. option_value shall be a pointer to Unsigned 32-bit
  1592. * integer containing the multicast IPv4 address. </td>
  1593. * </tr>
  1594. * <tr>
  1595. * <td><strong>@ref IP_DROP_MEMBERSHIP</strong></td>
  1596. * <td>Valid for UDP sockets. This option is used to stop receiving frames
  1597. * sent to a multicast group. option_value shall be a pointer to Unsigned
  1598. * 32-bit integer containing the multicast IPv4 address.</td>
  1599. * </tr>
  1600. * </table>
  1601. * <p>Possible options when the protcol leve&nbsp; is @ref SOL_SSL_SOCKET</p>
  1602. * <table style="width: 100%">
  1603. * <tr>
  1604. * <td style="height: 22px"><strong>
  1605. * @ref SO_SSL_BYPASS_X509_VERIF</strong></td>
  1606. * <td style="height: 22px">Allow an opened SSL socket to bypass the X509
  1607. * certificate verification process. It is highly recommended <strong>NOT</strong> to use
  1608. * this socket option in production software applications. The option is
  1609. * supported for debugging and testing purposes. The option value should be
  1610. * casted to int type and it is handled as a boolean flag.</td>
  1611. * </tr>
  1612. * <tr>
  1613. * <td><strong>@ref SO_SSL_SNI</strong></td>
  1614. * <td>Set the Server Name Indicator (SNI) for an SSL socket. The SNI is a
  1615. * null terminated string containing the server name associated with the
  1616. * connection. It must not exceed the size of @ref HOSTNAME_MAX_SIZE.</td>
  1617. * </tr>
  1618. * <tr>
  1619. * <td><strong>@ref SO_SSL_ENABLE_SESSION_CACHING</strong></td>
  1620. * <td>This option allow the TLS to cache the session information for fast
  1621. * TLS session establishment in future connections using the TLS Protocol
  1622. * session resume features.</td>
  1623. * </tr>
  1624. * </table>
  1625. */
  1626. /**@{*/
  1627. /*!
  1628. @fn \
  1629. NMI_API sint8 setsockopt(SOCKET socket, uint8 u8Level, uint8 option_name,
  1630. const void *option_value, uint16 u16OptionLen);
  1631. @param [in] sock
  1632. Socket handler.
  1633. @param [in] level
  1634. protocol level. See description above.
  1635. @param [in] option_name
  1636. option to be set. See description above.
  1637. @param [in] option_value
  1638. pointer to user provided value.
  1639. @param [in] option_len
  1640. length of the option value in bytes.
  1641. @return
  1642. The function shall return \ref SOCK_ERR_NO_ERROR for successful operation
  1643. and a negative value (indicating the error) otherwise.
  1644. @sa SOL_SOCKET, SOL_SSL_SOCKET, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP
  1645. */
  1646. NMI_API sint8 setsockopt(SOCKET socket, uint8 u8Level, uint8 option_name, const void *option_value,
  1647. uint16 u16OptionLen);
  1648. /** @} */
  1649. /** @defgroup GetSocketOptionsFn getsockopt
  1650. * @ingroup SocketAPI
  1651. * Get socket options retrieves
  1652. * This Function isn't implemented yet but this is the form that will be released later.
  1653. */
  1654. /**@{*/
  1655. /*!
  1656. @fn \
  1657. sint8 getsockopt(SOCKET sock, uint8 u8Level, uint8 u8OptName, const void *pvOptValue, uint8 * pu8OptLen);
  1658. @brief
  1659. @param [in] sock
  1660. Socket Identifie.
  1661. @param [in] u8Level
  1662. The protocol level of the option.
  1663. @param [in] u8OptName
  1664. The u8OptName argument specifies a single option to get.
  1665. @param [out] pvOptValue
  1666. The pvOptValue argument contains pointer to a buffer containing the option value.
  1667. @param [out] pu8OptLen
  1668. Option value buffer length.
  1669. @return
  1670. The function shall return ZERO for successful operation and a negative value otherwise.
  1671. */
  1672. NMI_API sint8 getsockopt(SOCKET sock, uint8 u8Level, uint8 u8OptName, const void *pvOptValue, uint8 *pu8OptLen);
  1673. /** @} */
  1674. /**@}*/
  1675. /** @defgroup PingFn m2m_ping_req
  1676. * @ingroup SocketAPI
  1677. * The function sends ping request to the given IP Address.
  1678. */
  1679. /**@{*/
  1680. /*!
  1681. @fn \
  1682. NMI_API sint8 m2m_ping_req(uint32 u32DstIP, uint8 u8TTL, tpfPingCb fpPingCb);
  1683. @param [in] u32DstIP
  1684. Target Destination IP Address for the ping request. It must be represented in Network byte order.
  1685. The function nmi_inet_addr could be used to translate the dotted decimal notation IP
  1686. to its Network bytes order integer represntative.
  1687. @param [in] u8TTL
  1688. IP TTL value for the ping request. If set to ZERO, the dfault value SHALL be used.
  1689. @param [in] fpPingCb
  1690. Callback will be called to deliver the ping statistics.
  1691. @see nmi_inet_addr
  1692. @return The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  1693. */
  1694. NMI_API sint8 m2m_ping_req(uint32 u32DstIP, uint8 u8TTL, tpfPingCb fpPingCb);
  1695. /**@}*/
  1696. #ifdef __cplusplus
  1697. }
  1698. #endif /* __cplusplus */
  1699. #endif /* __SOCKET_H__ */