ESP8266_RTOS_SDK  v1.4.0
espconn.h
1 /*
2  * ESPRSSIF MIT License
3  *
4  * Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
5  *
6  * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
7  * it is free of charge, to any person obtaining a copy of this software and associated
8  * documentation files (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
11  * to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or
14  * substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef __ESPCONN_H__
26 #define __ESPCONN_H__
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 typedef sint8 err_t;
33 
34 typedef void *espconn_handle;
35 
59 typedef void (* espconn_connect_callback)(void *arg);
60 
83 typedef void (* espconn_reconnect_callback)(void *arg, sint8 err);
84 
85 /* Definitions for error constants. */
86 
87 #define ESPCONN_OK 0
88 #define ESPCONN_MEM -1
89 #define ESPCONN_TIMEOUT -3
90 #define ESPCONN_RTE -4
91 #define ESPCONN_INPROGRESS -5
92 #define ESPCONN_MAXNUM -7
94 #define ESPCONN_ABRT -8
95 #define ESPCONN_RST -9
96 #define ESPCONN_CLSD -10
97 #define ESPCONN_CONN -11
99 #define ESPCONN_ARG -12
100 #define ESPCONN_IF -14
101 #define ESPCONN_ISCONN -15
104 enum espconn_type {
106  ESPCONN_TCP = 0x10,
107  ESPCONN_UDP = 0x20,
108 };
109 
119 };
120 
121 typedef struct _esp_tcp {
124  uint8 local_ip[4];
125  uint8 remote_ip[4];
130 } esp_tcp;
131 
132 typedef struct _esp_udp {
135  uint8 local_ip[4];
136  uint8 remote_ip[4];
137 } esp_udp;
138 
139 typedef struct _remot_info {
142  uint8 remote_ip[4];
143 } remot_info;
144 
146 typedef void (* espconn_recv_callback)(void *arg, char *pdata, unsigned short len);
147 typedef void (* espconn_sent_callback)(void *arg);
148 
150 struct espconn {
153  union {
154  esp_tcp *tcp;
155  esp_udp *udp;
156  } proto;
158  espconn_sent_callback sent_callback;
159  uint8 link_cnt;
160  void *reserve;
161 };
162 
164  ESPCONN_START = 0x00,
167  ESPCONN_COPY = 0x04,
170 };
171 
176 };
177 
178 enum {
179  ESPCONN_IDLE = 0,
180  ESPCONN_CLIENT,
181  ESPCONN_SERVER,
182  ESPCONN_BOTH,
183  ESPCONN_MAX
184 };
185 
195 void espconn_init(void);
196 
214 sint8 espconn_connect(struct espconn *espconn);
215 
229 sint8 espconn_disconnect(struct espconn *espconn);
230 
247 sint8 espconn_delete(struct espconn *espconn);
248 
261 sint8 espconn_accept(struct espconn *espconn);
262 
277 sint8 espconn_create(struct espconn *espconn);
278 
286 uint8 espconn_tcp_get_max_con(void);
287 
298 sint8 espconn_tcp_set_max_con(uint8 num);
299 
311 
323 sint8 espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num);
324 
345 sint8 espconn_regist_time(struct espconn *espconn, uint32 interval, uint8 type_flag);
346 
359 sint8 espconn_get_connection_info(struct espconn *pespconn, remot_info **pcon_info, uint8 typeflags);
360 
373 sint8 espconn_regist_sentcb(struct espconn *espconn, espconn_sent_callback sent_cb);
374 
399 
419 sint8 espconn_send(struct espconn *espconn, uint8 *psent, uint16 length);
420 
442 sint8 espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length);
443 
457 sint16 espconn_sendto(struct espconn *espconn, uint8 *psent, uint16 length);
458 
471 
484 
502 
516 
524 uint32 espconn_port(void);
525 
546 sint8 espconn_set_opt(struct espconn *espconn, uint8 opt);
547 
559 sint8 espconn_clear_opt(struct espconn *espconn, uint8 opt);
560 
580 sint8 espconn_set_keepalive(struct espconn *espconn, uint8 level, void *optarg);
581 
594 sint8 espconn_get_keepalive(struct espconn *espconn, uint8 level, void *optarg);
595 
606 typedef void (*dns_found_callback)(const char *name, ip_addr_t *ipaddr, void *callback_arg);
607 
624 err_t espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t *addr, dns_found_callback found);
625 
638 sint8 espconn_igmp_join(ip_addr_t *host_ip, ip_addr_t *multicast_ip);
639 
652 sint8 espconn_igmp_leave(ip_addr_t *host_ip, ip_addr_t *multicast_ip);
653 
667 sint8 espconn_recv_hold(struct espconn *pespconn);
668 
681 sint8 espconn_recv_unhold(struct espconn *pespconn);
682 
694 void espconn_dns_setserver(char numdns, ip_addr_t *dnsserver);
695 
700 #ifdef __cplusplus
701 }
702 #endif
703 
704 #endif
void(* espconn_recv_callback)(void *arg, char *pdata, unsigned short len)
Definition: espconn.h:146
sint8 espconn_regist_sentcb(struct espconn *espconn, espconn_sent_callback sent_cb)
Register data sent callback which will be called back when data are successfully sent.
void(* espconn_connect_callback)(void *arg)
Connect callback.
Definition: espconn.h:59
Definition: espconn.h:106
espconn_connect_callback connect_callback
Definition: espconn.h:126
Definition: espconn.h:132
sint8 espconn_tcp_set_max_con(uint8 num)
Set the maximum number of how many TCP connection is allowed.
sint8 espconn_recv_unhold(struct espconn *pespconn)
Unblock TCP receiving data (i.e. undo espconn_recv_hold).
void espconn_dns_setserver(char numdns, ip_addr_t *dnsserver)
Set default DNS server. Two DNS server is allowed to be set.
sint8 espconn_create(struct espconn *espconn)
Create UDP transmission.
int local_port
Definition: espconn.h:134
sint8 espconn_regist_recvcb(struct espconn *espconn, espconn_recv_callback recv_cb)
register data receive function which will be called back when data are received.
sint8 espconn_disconnect(struct espconn *espconn)
Disconnect a TCP connection.
Definition: espconn.h:175
sint8 espconn_tcp_get_max_con_allow(struct espconn *espconn)
Get the maximum number of TCP clients which are allowed to connect to ESP8266 TCP server...
sint8 espconn_igmp_join(ip_addr_t *host_ip, ip_addr_t *multicast_ip)
Join a multicast group.
sint8 espconn_regist_disconcb(struct espconn *espconn, espconn_connect_callback discon_cb)
Register disconnection function which will be called back under successful TCP disconnection.
Definition: espconn.h:173
Definition: espconn.h:116
sint8 espconn_regist_reconcb(struct espconn *espconn, espconn_reconnect_callback recon_cb)
Register reconnect callback.
espconn_sent_callback sent_callback
Definition: espconn.h:158
uint8 local_ip[4]
Definition: espconn.h:124
Definition: espconn.h:118
Definition: espconn.h:139
void * reserve
Definition: espconn.h:160
sint16 espconn_sendto(struct espconn *espconn, uint8 *psent, uint16 length)
Send UDP data.
Definition: espconn.h:107
int remote_port
Definition: espconn.h:141
enum espconn_type type
Definition: espconn.h:151
Definition: espconn.h:165
Definition: espconn.h:164
void(* espconn_reconnect_callback)(void *arg, sint8 err)
Reconnect callback.
Definition: espconn.h:83
sint8 espconn_regist_connectcb(struct espconn *espconn, espconn_connect_callback connect_cb)
Register connection function which will be called back under successful TCP connection.
sint8 espconn_send(struct espconn *espconn, uint8 *psent, uint16 length)
Send data through network.
Definition: espconn.h:168
espconn_reconnect_callback reconnect_callback
Definition: espconn.h:127
espconn_option
Definition: espconn.h:163
Definition: espconn.h:114
sint8 espconn_set_opt(struct espconn *espconn, uint8 opt)
Set option of TCP connection.
Definition: espconn.h:169
espconn_type
Definition: espconn.h:104
espconn_recv_callback recv_callback
Definition: espconn.h:157
Definition: espconn.h:121
espconn_connect_callback write_finish_fn
Definition: espconn.h:129
sint8 espconn_accept(struct espconn *espconn)
Creates a TCP server (i.e. accepts connections).
uint8 remote_ip[4]
Definition: espconn.h:142
Definition: espconn.h:105
sint8 espconn_regist_write_finish(struct espconn *espconn, espconn_connect_callback write_finish_fn)
Register a callback which will be called when all sending TCP data is completely write into write-buf...
void(* dns_found_callback)(const char *name, ip_addr_t *ipaddr, void *callback_arg)
Callback which is invoked when a hostname is found.
Definition: espconn.h:606
Definition: espconn.h:174
Definition: espconn.h:115
Definition: espconn.h:112
uint8 remote_ip[4]
Definition: espconn.h:136
sint8 espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length)
Send data through network.
sint8 espconn_regist_time(struct espconn *espconn, uint32 interval, uint8 type_flag)
Register timeout interval of ESP8266 TCP server.
int remote_port
Definition: espconn.h:133
Definition: espconn.h:113
uint8 remote_ip[4]
Definition: espconn.h:125
sint8 espconn_get_keepalive(struct espconn *espconn, uint8 level, void *optarg)
Get configuration of TCP keep alive.
enum espconn_state state
Definition: espconn.h:152
Definition: espconn.h:166
Definition: espconn.h:150
espconn_connect_callback disconnect_callback
Definition: espconn.h:128
sint8 espconn_connect(struct espconn *espconn)
Connect to a TCP server (ESP8266 acting as TCP client).
uint8 espconn_tcp_get_max_con(void)
Get maximum number of how many TCP connections are allowed.
Definition: espconn.h:167
uint8 local_ip[4]
Definition: espconn.h:135
int local_port
Definition: espconn.h:123
Definition: espconn.h:117
sint8 espconn_igmp_leave(ip_addr_t *host_ip, ip_addr_t *multicast_ip)
Leave a multicast group.
uint32 espconn_port(void)
Get an available port for network.
int remote_port
Definition: espconn.h:122
sint8 espconn_clear_opt(struct espconn *espconn, uint8 opt)
Clear option of TCP connection.
void espconn_init(void)
espconn initialization.
sint8 espconn_set_keepalive(struct espconn *espconn, uint8 level, void *optarg)
Set configuration of TCP keep alive.
espconn_state
Definition: espconn.h:111
uint8 link_cnt
Definition: espconn.h:159
sint8 espconn_get_connection_info(struct espconn *pespconn, remot_info **pcon_info, uint8 typeflags)
Get the information about a TCP connection or UDP transmission.
sint8 espconn_recv_hold(struct espconn *pespconn)
Puts in a request to block the TCP receive function.
sint8 espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num)
Set the maximum number of TCP clients allowed to connect to ESP8266 TCP server.
espconn_level
Definition: espconn.h:172
sint8 espconn_delete(struct espconn *espconn)
Delete a transmission.
err_t espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t *addr, dns_found_callback found)
DNS function.
enum espconn_state state
Definition: espconn.h:140