wm_socket2.1.3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @file wm_socket2.1.3.h
  3. *
  4. * @brief socket2.1.3 apis
  5. *
  6. * @author winnermicro
  7. *
  8. * @copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. */
  10. #ifndef WM_SOCKET2_1_3_H
  11. #define WM_SOCKET2_1_3_H
  12. #include "wm_type_def.h"
  13. #include "wm_netif.h"
  14. #include "lwip/err.h"
  15. #include "lwip/pbuf.h"
  16. //socket state defination
  17. #define NETCONN_STATE_NONE 0
  18. #define NETCONN_STATE_WAITING 1
  19. #define NETCONN_STATE_CONNECTED 2
  20. #define NETCONN_STATE_CLOSED 3
  21. //socket event defination
  22. #define NET_EVENT_TCP_JOINED 0
  23. #define NET_EVENT_TCP_DISCONNECT 1
  24. #define NET_EVENT_TCP_CONNECTED 2
  25. #define NET_EVENT_TCP_CONNECT_FAILED 3
  26. #define NET_EVENT_UDP_START 4
  27. #define NET_EVENT_UDP_START_FAILED 5
  28. #define TLS_MAX_SOCKET_NUM 4
  29. #define TLS_MAX_NETCONN_NUM 20
  30. /**
  31. * @brief This Function prototype for tcp error callback functions. Called when
  32. * receives a RST or is unexpectedly closed for any other reason.
  33. * The corresponding socket is already freed when this callback is called!
  34. *
  35. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  36. *
  37. * @param[in] err Error code to indicate why the socket has been closed
  38. * ERR_ABRT: aborted through returning ERR_ABRT from within others
  39. * callback functions
  40. * ERR_RST: the connection was reset by the remote host
  41. */
  42. typedef void (*socket_err_fn)(u8 skt_num, err_t err);
  43. /**
  44. * @brief This Function prototype for socket receive callback functions. Called when data has
  45. * been received.
  46. *
  47. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  48. *
  49. * @param[in] p The received data (or NULL when the connection has been closed!)
  50. *
  51. * @param[in] err An error code if there has been an error receiving, always be ERR_OK
  52. * when cs mode is udp.
  53. *
  54. * @retval The return value is only valid for tcp receive, for upd it means nothing.
  55. * ERR_OK: Return this value after handling the received data.
  56. * ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
  57. * callback function!
  58. */
  59. typedef err_t (*socket_recv_fn)(u8 skt_num, struct pbuf *p, err_t err);
  60. /**
  61. * @brief This Function prototype for socket srce ip callback functions. Called when data has
  62. * been received.
  63. *
  64. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  65. *
  66. * @param[in] datalen The received data length
  67. *
  68. * @param[in] ipsrc source ip addr
  69. *
  70. * @param[in] port source port
  71. *
  72. * @param[in] err An error code if there has been an error receiving, always be ERR_OK
  73. * when cs mode is udp.
  74. *
  75. * @retval The return value is only valid for UDP receive, for udp it means nothing.
  76. * ERR_OK: Return this value after handling the received data.
  77. * ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
  78. * callback function!
  79. */
  80. typedef err_t (*socket_recv_ip_rpt_fn)(u8 skt_num, u16 datalen, u8 *ipsrc, u16 port, err_t err);
  81. /**
  82. * @brief This Function prototype for tcp connected callback functions. Called when
  83. * connected to the remote side.
  84. *
  85. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  86. *
  87. * @param[in] err An unused error code, always ERR_OK currently.
  88. *
  89. * @retval ERR_OK: Return this value after handling your logic.
  90. * @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
  91. * callback function!
  92. */
  93. typedef err_t (*socket_connected_fn)(u8 skt_num, err_t err);
  94. /**
  95. * @brief This Function prototype for tcp poll callback functions. Called periodically.
  96. *
  97. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  98. *
  99. * @retval ERR_OK: Try to do something periodically.
  100. * @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
  101. * callback function!
  102. */
  103. typedef err_t (*socket_poll_fn)(u8 skt_num);
  104. /**
  105. * @brief This Function prototype for tcp accept callback functions. Called when a new
  106. * connection can be accepted on a listening tcp.
  107. *
  108. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  109. *
  110. * @param[in] err An error code if there has been an error accepting.
  111. *
  112. * @retval ERR_OK: Return this value after handling your logic.
  113. * @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
  114. * callback function!
  115. */
  116. typedef err_t (*socket_accept_fn)(u8 skt_num, err_t err);
  117. /**
  118. * @brief This Function prototype for socket state changed callback functions. Called when socket
  119. * the sockte's state changed.
  120. *
  121. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  122. *
  123. * @param[in] event Is the event number, see socket event defination.
  124. *
  125. * @param[in] state Is the socket state, see socket state defination.
  126. */
  127. typedef void(*socket_state_changed_fn)(u8 skt_num, u8 event, u8 state);
  128. enum tls_socket_protocol{
  129. SOCKET_PROTO_TCP, /* TCP Protocol */
  130. SOCKET_PROTO_UDP, /* UDP Protocol */
  131. };
  132. enum tls_socket_cs_mode{
  133. SOCKET_CS_MODE_CLIENT, /* Client mode */
  134. SOCKET_CS_MODE_SERVER, /* Server mode */
  135. };
  136. struct tls_socket_desc {
  137. enum tls_socket_cs_mode cs_mode; /* Server mode or Client mode, Only for tcp protocol is valid */
  138. enum tls_socket_protocol protocol; /* TCP Protocol or UDP Protocol */
  139. ip_addr_t ip_addr; /* Remote ip address, for tcp client mode is remote server's ip address; for tcp server mode can be any address. */
  140. /* for udp is remote server's ip address */
  141. u16 port; /* port, for tcp client mode is remote server's port; for tcp server mode is local listen port .
  142. for udp is remote server's port */
  143. u16 localport; /* local port, for udp and tcp client is local listen port, for tcp server means nothing, tcp server always listen at port */
  144. char host_name[32]; /* remote host name, not support for now */
  145. u8 host_len; /* the length of host name */
  146. u32 timeout; /* poll timeout, not implemented for now */
  147. socket_err_fn errf; /* a pointer to socket_err_fn */
  148. socket_recv_fn recvf; /* a pointer to socket_recv_fn */
  149. socket_connected_fn connf; /* a pointer to socket_connected_fn */
  150. socket_poll_fn pollf; /* a pointer to socket_poll_fn */
  151. socket_accept_fn acceptf; /* a pointer to socket_accept_fn */
  152. socket_state_changed_fn state_changed; /* a pointer to socket_state_changed_fn */
  153. socket_recv_ip_rpt_fn recvwithipf; /*recv skt info report*/
  154. };
  155. /**
  156. * @brief This function is called by your application code to create a socket.
  157. *
  158. * @param[in] skd Is a pointer to an tls_socket_desc.
  159. *
  160. * @retval ERR_OK If create socket successfully.
  161. * negative number If an error was detected.
  162. */
  163. int tls_socket_create(struct tls_socket_desc * skd);
  164. /**
  165. * @brief This function is called by your application code to send data by the socket.
  166. *
  167. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  168. *
  169. * @param[in] pdata Is a pointer to the data which need to be send by the socket.
  170. *
  171. * @param[in] len The data's length.
  172. *
  173. * @retval ERR_OK If send data successfully.
  174. * negative number If an error was detected.
  175. */
  176. int tls_socket_send(u8 skt_num, void *pdata, u16 len);
  177. /**
  178. * @brief This function is called by your application code to close the socket, and the related resources would be released.
  179. *
  180. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  181. *
  182. * @retval ERR_OK If close socket successfully.
  183. * negative number If an error was detected.
  184. */
  185. int tls_socket_close(u8 skt_num);
  186. struct tls_skt_status_ext_t {
  187. u8 socket;
  188. u8 status;
  189. enum tls_socket_protocol protocol;
  190. u8 host_ipaddr[4];
  191. u16 remote_port;
  192. u16 local_port;
  193. };
  194. struct tls_skt_status_t {
  195. u32 socket_cnt;
  196. struct tls_skt_status_ext_t skts_ext[1];
  197. };
  198. /**
  199. * @brief This function is called by your application code to get the socket status of specified socket num.
  200. *
  201. * @param[in] skt_num Is the socket number that returned by tls_socket_create function.
  202. *
  203. * @param[in] buf Is a pointer to the data contains the socket status, if the socket is server, also contains it's client's status.
  204. *
  205. * @param[in] len The buf's length. At least, the len should be bigger than sizeof(struct tls_skt_status_t).
  206. *
  207. * @retval ERR_OK If send data successfully.
  208. * negative number If an error was detected.
  209. */
  210. int tls_socket_get_status(u8 skt_num, u8 *buf, u32 bufsize);
  211. /**
  212. * @brief This function is called by your application code to send data by udp socket.
  213. *
  214. * @param[in] localport This function will search all created sockets, if there is a socket whose localport equals this value and it's protocol is udp,
  215. * then send the data by this socket, otherwise, nothing to send.
  216. *
  217. * @param[in] ip_addr Is the remote ip address.
  218. *
  219. * @param[in] port Is the remote port which upd send to.
  220. *
  221. * @param[in] pdata Is a pointer to the data which need to be send by the socket.
  222. *
  223. * @param[in] len The data's length.
  224. * @retval ERR_OK If send data successfully.
  225. * negative number If an error was detected.
  226. */
  227. int tls_socket_udp_sendto(u16 localport, u8 *ip_addr, u16 port, void *pdata, u16 len);
  228. #endif