espconn.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #ifndef __ESPCONN_H__
  2. #define __ESPCONN_H__
  3. #include "lwip/ip_addr.h"
  4. typedef sint8 err_t;
  5. typedef void *espconn_handle;
  6. typedef void (* espconn_connect_callback)(void *arg);
  7. typedef void (* espconn_reconnect_callback)(void *arg, sint8 err);
  8. /* Definitions for error constants. */
  9. #define ESPCONN_OK 0 /* No error, everything OK. */
  10. #define ESPCONN_MEM -1 /* Out of memory error. */
  11. #define ESPCONN_TIMEOUT -3 /* Timeout. */
  12. #define ESPCONN_RTE -4 /* Routing problem. */
  13. #define ESPCONN_INPROGRESS -5 /* Operation in progress */
  14. #define ESPCONN_ABRT -8 /* Connection aborted. */
  15. #define ESPCONN_RST -9 /* Connection reset. */
  16. #define ESPCONN_CLSD -10 /* Connection closed. */
  17. #define ESPCONN_CONN -11 /* Not connected. */
  18. #define ESPCONN_ARG -12 /* Illegal argument. */
  19. #define ESPCONN_ISCONN -15 /* Already connected. */
  20. /** Protocol family and type of the espconn */
  21. enum espconn_type {
  22. ESPCONN_INVALID = 0,
  23. /* ESPCONN_TCP Group */
  24. ESPCONN_TCP = 0x10,
  25. /* ESPCONN_UDP Group */
  26. ESPCONN_UDP = 0x20,
  27. };
  28. /** Current state of the espconn. Non-TCP espconn are always in state ESPCONN_NONE! */
  29. enum espconn_state {
  30. ESPCONN_NONE,
  31. ESPCONN_WAIT,
  32. ESPCONN_LISTEN,
  33. ESPCONN_CONNECT,
  34. ESPCONN_WRITE,
  35. ESPCONN_READ,
  36. ESPCONN_CLOSE
  37. };
  38. typedef struct _esp_tcp {
  39. int remote_port;
  40. int local_port;
  41. uint8 local_ip[4];
  42. uint8 remote_ip[4];
  43. espconn_connect_callback connect_callback;
  44. espconn_reconnect_callback reconnect_callback;
  45. espconn_connect_callback disconnect_callback;
  46. espconn_connect_callback write_finish_fn;
  47. } esp_tcp;
  48. typedef struct _esp_udp {
  49. int remote_port;
  50. int local_port;
  51. uint8 local_ip[4];
  52. uint8 remote_ip[4];
  53. } esp_udp;
  54. typedef struct _remot_info{
  55. enum espconn_state state;
  56. int remote_port;
  57. uint8 remote_ip[4];
  58. }remot_info;
  59. /** A callback prototype to inform about events for a espconn */
  60. typedef void (* espconn_recv_callback)(void *arg, char *pdata, unsigned short len);
  61. typedef void (* espconn_sent_callback)(void *arg);
  62. /** A espconn descriptor */
  63. struct espconn {
  64. /** type of the espconn (TCP, UDP) */
  65. enum espconn_type type;
  66. /** current state of the espconn */
  67. enum espconn_state state;
  68. union {
  69. esp_tcp *tcp;
  70. esp_udp *udp;
  71. } proto;
  72. /** A callback function that is informed about events for this espconn */
  73. espconn_recv_callback recv_callback;
  74. espconn_sent_callback sent_callback;
  75. uint8 link_cnt;
  76. void *reverse;
  77. };
  78. enum espconn_option{
  79. ESPCONN_START = 0x00,
  80. ESPCONN_REUSEADDR = 0x01,
  81. ESPCONN_NODELAY = 0x02,
  82. ESPCONN_COPY = 0x04,
  83. ESPCONN_END
  84. };
  85. /******************************************************************************
  86. * FunctionName : espconn_connect
  87. * Description : The function given as the connect
  88. * Parameters : espconn -- the espconn used to listen the connection
  89. * Returns : none
  90. *******************************************************************************/
  91. sint8 espconn_connect(struct espconn *espconn);
  92. /******************************************************************************
  93. * FunctionName : espconn_disconnect
  94. * Description : disconnect with host
  95. * Parameters : espconn -- the espconn used to disconnect the connection
  96. * Returns : none
  97. *******************************************************************************/
  98. sint8 espconn_disconnect(struct espconn *espconn);
  99. /******************************************************************************
  100. * FunctionName : espconn_delete
  101. * Description : disconnect with host
  102. * Parameters : espconn -- the espconn used to disconnect the connection
  103. * Returns : none
  104. *******************************************************************************/
  105. sint8 espconn_delete(struct espconn *espconn);
  106. /******************************************************************************
  107. * FunctionName : espconn_accept
  108. * Description : The function given as the listen
  109. * Parameters : espconn -- the espconn used to listen the connection
  110. * Returns : none
  111. *******************************************************************************/
  112. sint8 espconn_accept(struct espconn *espconn);
  113. /******************************************************************************
  114. * FunctionName : espconn_create
  115. * Description : sent data for client or server
  116. * Parameters : espconn -- espconn to the data transmission
  117. * Returns : result
  118. *******************************************************************************/
  119. sint8 espconn_create(struct espconn *espconn);
  120. /******************************************************************************
  121. * FunctionName : espconn_tcp_get_max_con
  122. * Description : get the number of simulatenously active TCP connections
  123. * Parameters : none
  124. * Returns : none
  125. *******************************************************************************/
  126. uint8 espconn_tcp_get_max_con(void);
  127. /******************************************************************************
  128. * FunctionName : espconn_tcp_set_max_con
  129. * Description : set the number of simulatenously active TCP connections
  130. * Parameters : num -- total number
  131. * Returns : none
  132. *******************************************************************************/
  133. sint8 espconn_tcp_set_max_con(uint8 num);
  134. /******************************************************************************
  135. * FunctionName : espconn_tcp_get_max_con_allow
  136. * Description : get the count of simulatenously active connections on the server
  137. * Parameters : espconn -- espconn to get the count
  138. * Returns : result
  139. *******************************************************************************/
  140. sint8 espconn_tcp_get_max_con_allow(struct espconn *espconn);
  141. /******************************************************************************
  142. * FunctionName : espconn_tcp_set_max_con_allow
  143. * Description : set the count of simulatenously active connections on the server
  144. * Parameters : espconn -- espconn to set the count
  145. * num -- support the connection number
  146. * Returns : result
  147. *******************************************************************************/
  148. sint8 espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num);
  149. /******************************************************************************
  150. * FunctionName : espconn_regist_time
  151. * Description : used to specify the time that should be called when don't recv data
  152. * Parameters : espconn -- the espconn used to the connection
  153. * interval -- the timer when don't recv data
  154. * Returns : none
  155. *******************************************************************************/
  156. sint8 espconn_regist_time(struct espconn *espconn, uint32 interval, uint8 type_flag);
  157. /******************************************************************************
  158. * FunctionName : espconn_get_connection_info
  159. * Description : used to specify the function that should be called when disconnect
  160. * Parameters : espconn -- espconn to set the err callback
  161. * discon_cb -- err callback function to call when err
  162. * Returns : none
  163. *******************************************************************************/
  164. sint8 espconn_get_connection_info(struct espconn *pespconn, remot_info **pcon_info, uint8 typeflags);
  165. /******************************************************************************
  166. * FunctionName : espconn_regist_sentcb
  167. * Description : Used to specify the function that should be called when data
  168. * has been successfully delivered to the remote host.
  169. * Parameters : struct espconn *espconn -- espconn to set the sent callback
  170. * espconn_sent_callback sent_cb -- sent callback function to
  171. * call for this espconn when data is successfully sent
  172. * Returns : none
  173. *******************************************************************************/
  174. sint8 espconn_regist_sentcb(struct espconn *espconn, espconn_sent_callback sent_cb);
  175. /******************************************************************************
  176. * FunctionName : espconn_regist_sentcb
  177. * Description : Used to specify the function that should be called when data
  178. * has been successfully delivered to the remote host.
  179. * Parameters : espconn -- espconn to set the sent callback
  180. * sent_cb -- sent callback function to call for this espconn
  181. * when data is successfully sent
  182. * Returns : none
  183. *******************************************************************************/
  184. sint8 espconn_regist_write_finish(struct espconn *espconn, espconn_connect_callback write_finish_fn);
  185. /******************************************************************************
  186. * FunctionName : espconn_sent
  187. * Description : sent data for client or server
  188. * Parameters : espconn -- espconn to set for client or server
  189. * psent -- data to send
  190. * length -- length of data to send
  191. * Returns : none
  192. *******************************************************************************/
  193. sint8 espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length);
  194. /******************************************************************************
  195. * FunctionName : espconn_regist_connectcb
  196. * Description : used to specify the function that should be called when
  197. * connects to host.
  198. * Parameters : espconn -- espconn to set the connect callback
  199. * connect_cb -- connected callback function to call when connected
  200. * Returns : none
  201. *******************************************************************************/
  202. sint8 espconn_regist_connectcb(struct espconn *espconn, espconn_connect_callback connect_cb);
  203. /******************************************************************************
  204. * FunctionName : espconn_regist_recvcb
  205. * Description : used to specify the function that should be called when recv
  206. * data from host.
  207. * Parameters : espconn -- espconn to set the recv callback
  208. * recv_cb -- recv callback function to call when recv data
  209. * Returns : none
  210. *******************************************************************************/
  211. sint8 espconn_regist_recvcb(struct espconn *espconn, espconn_recv_callback recv_cb);
  212. /******************************************************************************
  213. * FunctionName : espconn_regist_reconcb
  214. * Description : used to specify the function that should be called when connection
  215. * because of err disconnect.
  216. * Parameters : espconn -- espconn to set the err callback
  217. * recon_cb -- err callback function to call when err
  218. * Returns : none
  219. *******************************************************************************/
  220. sint8 espconn_regist_reconcb(struct espconn *espconn, espconn_reconnect_callback recon_cb);
  221. /******************************************************************************
  222. * FunctionName : espconn_regist_disconcb
  223. * Description : used to specify the function that should be called when disconnect
  224. * Parameters : espconn -- espconn to set the err callback
  225. * discon_cb -- err callback function to call when err
  226. * Returns : none
  227. *******************************************************************************/
  228. sint8 espconn_regist_disconcb(struct espconn *espconn, espconn_connect_callback discon_cb);
  229. /******************************************************************************
  230. * FunctionName : espconn_port
  231. * Description : access port value for client so that we don't end up bouncing
  232. * all connections at the same time .
  233. * Parameters : none
  234. * Returns : access port value
  235. *******************************************************************************/
  236. uint32 espconn_port(void);
  237. /******************************************************************************
  238. * FunctionName : espconn_set_opt
  239. * Description : access port value for client so that we don't end up bouncing
  240. * all connections at the same time .
  241. * Parameters : none
  242. * Returns : access port value
  243. *******************************************************************************/
  244. sint8 espconn_set_opt(struct espconn *espconn, uint8 opt);
  245. /******************************************************************************
  246. * TypedefName : dns_found_callback
  247. * Description : Callback which is invoked when a hostname is found.
  248. * Parameters : name -- pointer to the name that was looked up.
  249. * ipaddr -- pointer to an ip_addr_t containing the IP address of
  250. * the hostname, or NULL if the name could not be found (or on any
  251. * other error).
  252. * callback_arg -- a user-specified callback argument passed to
  253. * dns_gethostbyname
  254. *******************************************************************************/
  255. typedef void (*dns_found_callback)(const char *name, ip_addr_t *ipaddr, void *callback_arg);
  256. /******************************************************************************
  257. * FunctionName : espconn_gethostbyname
  258. * Description : Resolve a hostname (string) into an IP address.
  259. * Parameters : pespconn -- espconn to resolve a hostname
  260. * hostname -- the hostname that is to be queried
  261. * addr -- pointer to a ip_addr_t where to store the address if
  262. * it is already cached in the dns_table (only valid if ESPCONN_OK
  263. * is returned!)
  264. * found -- a callback function to be called on success, failure
  265. * or timeout (only if ERR_INPROGRESS is returned!)
  266. * Returns : err_t return code
  267. * - ESPCONN_OK if hostname is a valid IP address string or the host
  268. * name is already in the local names table.
  269. * - ESPCONN_INPROGRESS enqueue a request to be sent to the DNS server
  270. * for resolution if no errors are present.
  271. * - ESPCONN_ARG: dns client not initialized or invalid hostname
  272. *******************************************************************************/
  273. err_t espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t *addr, dns_found_callback found);
  274. /******************************************************************************
  275. * FunctionName : espconn_encry_connect
  276. * Description : The function given as connection
  277. * Parameters : espconn -- the espconn used to connect with the host
  278. * Returns : none
  279. *******************************************************************************/
  280. sint8 espconn_secure_connect(struct espconn *espconn);
  281. /******************************************************************************
  282. * FunctionName : espconn_encry_disconnect
  283. * Description : The function given as the disconnection
  284. * Parameters : espconn -- the espconn used to disconnect with the host
  285. * Returns : none
  286. *******************************************************************************/
  287. sint8 espconn_secure_disconnect(struct espconn *espconn);
  288. /******************************************************************************
  289. * FunctionName : espconn_encry_sent
  290. * Description : sent data for client or server
  291. * Parameters : espconn -- espconn to set for client or server
  292. * psent -- data to send
  293. * length -- length of data to send
  294. * Returns : none
  295. *******************************************************************************/
  296. sint8 espconn_secure_sent(struct espconn *espconn, uint8 *psent, uint16 length);
  297. /******************************************************************************
  298. * FunctionName : espconn_secure_accept
  299. * Description : The function given as the listen
  300. * Parameters : espconn -- the espconn used to listen the connection
  301. * Returns : none
  302. *******************************************************************************/
  303. sint8 espconn_secure_accept(struct espconn *espconn);
  304. /******************************************************************************
  305. * FunctionName : espconn_igmp_join
  306. * Description : join a multicast group
  307. * Parameters : host_ip -- the ip address of udp server
  308. * multicast_ip -- multicast ip given by user
  309. * Returns : none
  310. *******************************************************************************/
  311. sint8 espconn_igmp_join(ip_addr_t *host_ip, ip_addr_t *multicast_ip);
  312. /******************************************************************************
  313. * FunctionName : espconn_igmp_leave
  314. * Description : leave a multicast group
  315. * Parameters : host_ip -- the ip address of udp server
  316. * multicast_ip -- multicast ip given by user
  317. * Returns : none
  318. *******************************************************************************/
  319. sint8 espconn_igmp_leave(ip_addr_t *host_ip, ip_addr_t *multicast_ip);
  320. /******************************************************************************
  321. * FunctionName : espconn_recv_hold
  322. * Description : hold tcp receive
  323. * Parameters : espconn -- espconn to hold
  324. * Returns : none
  325. *******************************************************************************/
  326. sint8 espconn_recv_hold(struct espconn *pespconn);
  327. /******************************************************************************
  328. * FunctionName : espconn_recv_unhold
  329. * Description : unhold tcp receive
  330. * Parameters : espconn -- espconn to unhold
  331. * Returns : none
  332. *******************************************************************************/
  333. sint8 espconn_recv_unhold(struct espconn *pespconn);
  334. #endif