MeterLogger
Macros | Functions
espconn_tcp.h File Reference
#include "lwip/app/espconn.h"
Include dependency graph for espconn_tcp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ESPCONN_TCP_DEBUG   LWIP_DBG_OFF
 
#define ESPCONN_TCP_TIMER   40
 
#define espconn_keepalive_enable(pcb)   ((pcb)->so_options |= SOF_KEEPALIVE)
 
#define espconn_keepalive_disable(pcb)   ((pcb)->so_options &= ~SOF_KEEPALIVE)
 

Functions

void espconn_kill_oldest_pcb (void)
 
void espconn_tcp_disconnect (espconn_msg *pdiscon, u8 type)
 
sint8 espconn_tcp_client (struct espconn *espconn)
 
sint8 espconn_tcp_server (struct espconn *espconn)
 

Macro Definition Documentation

◆ espconn_keepalive_disable

#define espconn_keepalive_disable (   pcb)    ((pcb)->so_options &= ~SOF_KEEPALIVE)

Definition at line 14 of file espconn_tcp.h.

Referenced by espconn_clear_opt().

◆ espconn_keepalive_enable

#define espconn_keepalive_enable (   pcb)    ((pcb)->so_options |= SOF_KEEPALIVE)

Definition at line 13 of file espconn_tcp.h.

Referenced by espconn_client_connect(), espconn_set_opt(), and espconn_tcp_accept().

◆ ESPCONN_TCP_DEBUG

#define ESPCONN_TCP_DEBUG   LWIP_DBG_OFF

Definition at line 5 of file espconn_tcp.h.

◆ ESPCONN_TCP_TIMER

#define ESPCONN_TCP_TIMER   40

Definition at line 10 of file espconn_tcp.h.

Function Documentation

◆ espconn_kill_oldest_pcb()

void espconn_kill_oldest_pcb ( void  )

Definition at line 115 of file espconn_tcp.c.

References espconn_kill_oldest(), MEMP_NUM_TCP_PCB, NULL, and tcp_pcb_lists.

Referenced by espconn_tcp_disconnect_successful(), and espconn_tcp_reconnect().

116 {
117  struct tcp_pcb *cpcb = NULL;
118  uint8 i = 0;
119  uint8 num_tcp_fin = 0;
120  for(i = 2; i < 4; i ++){
121  for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
122  if (cpcb->state == TIME_WAIT){
123  num_tcp_fin ++;
124  if (num_tcp_fin == MEMP_NUM_TCP_PCB)
125  break;
126  }
127 
128  if (cpcb->state == FIN_WAIT_1 || cpcb->state == FIN_WAIT_2 || cpcb->state == LAST_ACK){
129  num_tcp_fin++;
130  if (num_tcp_fin == MEMP_NUM_TCP_PCB)
131  break;
132  }
133  }
134 
135  if (num_tcp_fin == MEMP_NUM_TCP_PCB){
136  num_tcp_fin = 0;
138  } else if (cpcb == NULL){
139  num_tcp_fin = 0;
140  }
141  }
142 }
#define NULL
Definition: def.h:47
#define MEMP_NUM_TCP_PCB
Definition: opt.h:251
struct tcp_pcb **const tcp_pcb_lists[]
unsigned char uint8
Definition: c_types.h:45
static void ICACHE_FLASH_ATTR espconn_kill_oldest(void)
Definition: espconn_tcp.c:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ espconn_tcp_client()

sint8 espconn_tcp_client ( struct espconn espconn)

Definition at line 878 of file espconn_tcp.c.

References ERR_OK, ERR_RTE, ERR_USE, espconn_client_connect(), espconn_client_err(), espconn_kill_pcb(), espconn_list_creat(), espconn_list_delete(), ESPCONN_MEM, ESPCONN_RTE, ESPCONN_WAIT, ICACHE_FLASH_ATTR, IP4_ADDR, IP_ADDR_ANY, memp_free(), NULL, os_free, os_zalloc, espconn::proto, _esp_tcp::remote_ip, espconn::state, and espconn::tcp.

Referenced by espconn_connect().

879 {
880  struct tcp_pcb *pcb = NULL;
881  struct ip_addr ipaddr;
882  espconn_msg *pclient = NULL;
883 
884  /*Creates a new client control message*/
885  pclient = (espconn_msg *)os_zalloc(sizeof(espconn_msg));
886  if (pclient == NULL){
887  return ESPCONN_MEM;
888  }
889 
890  /*Set an IP address given for Little-endian.*/
891  IP4_ADDR(&ipaddr, espconn->proto.tcp->remote_ip[0],
892  espconn->proto.tcp->remote_ip[1],
893  espconn->proto.tcp->remote_ip[2],
894  espconn->proto.tcp->remote_ip[3]);
895 
896  /*Creates a new TCP protocol control block*/
897  pcb = tcp_new();
898 
899  if (pcb == NULL) {
900  /*to prevent memory leaks, ensure that each allocated is deleted*/
901  os_free(pclient);
902  pclient = NULL;
903  return ESPCONN_MEM;
904  } else {
905 
906  /*insert the node to the active connection list*/
907  espconn_list_creat(&plink_active, pclient);
908  tcp_arg(pcb, (void *)pclient);
909  tcp_err(pcb, espconn_client_err);
910  pclient->preverse = NULL;
911  pclient->pespconn = espconn;
912  pclient->pespconn->state = ESPCONN_WAIT;
913  pclient->pcommon.pcb = pcb;
914  tcp_bind(pcb, IP_ADDR_ANY, pclient->pespconn->proto.tcp->local_port);
915 #if 0
916  pclient->pcommon.err = tcp_bind(pcb, IP_ADDR_ANY, pclient->pespconn->proto.tcp->local_port);
917  if (pclient->pcommon.err != ERR_OK){
918  /*remove the node from the client's active connection list*/
920  memp_free(MEMP_TCP_PCB, pcb);
921  os_free(pclient);
922  pclient = NULL;
923  return ERR_USE;
924  }
925 #endif
926  /*Establish the connection*/
927  pclient->pcommon.err = tcp_connect(pcb, &ipaddr,
928  pclient->pespconn->proto.tcp->remote_port, espconn_client_connect);
929  if (pclient->pcommon.err == ERR_RTE){
930  /*remove the node from the client's active connection list*/
932  espconn_kill_pcb(pcb->local_port);
933  os_free(pclient);
934  pclient = NULL;
935  return ESPCONN_RTE;
936  }
937  return pclient->pcommon.err;
938  }
939 }
void memp_free(memp_t type, void *mem) ICACHE_FLASH_ATTR
Definition: memp.c:438
#define ERR_USE
Definition: err.h:70
esp_tcp * tcp
Definition: espconn.h:105
void espconn_list_delete(espconn_msg **phead, espconn_msg *pdelete)
Definition: espconn.c:104
#define NULL
Definition: def.h:47
uint8 remote_ip[4]
Definition: espconn.h:74
static err_t ICACHE_FLASH_ATTR espconn_client_connect(void *arg, struct tcp_pcb *tpcb, err_t err)
Definition: espconn_tcp.c:826
void ICACHE_FLASH_ATTR espconn_kill_pcb(u16_t port)
Definition: espconn_tcp.c:150
#define os_zalloc(s)
Definition: mem.h:44
void espconn_list_creat(espconn_msg **phead, espconn_msg *pinsert)
Definition: espconn.c:76
#define ESPCONN_RTE
Definition: espconn.h:23
#define ERR_RTE
Definition: err.h:56
#define ERR_OK
Definition: err.h:52
#define IP_ADDR_ANY
Definition: ip_addr.h:92
static void ICACHE_FLASH_ATTR espconn_client_err(void *arg, err_t err)
Definition: espconn_tcp.c:761
#define os_free(s)
Definition: mem.h:40
union espconn::@1 proto
enum espconn_state state
Definition: espconn.h:103
espconn_msg * plink_active
Definition: espconn.c:32
#define IP4_ADDR(ipaddr, a, b, c, d)
Definition: ip_addr.h:139
#define ESPCONN_MEM
Definition: espconn.h:21
Here is the call graph for this function:
Here is the caller graph for this function:

◆ espconn_tcp_disconnect()

void espconn_tcp_disconnect ( espconn_msg pdiscon,
u8  type 
)

Definition at line 498 of file espconn_tcp.c.

References espconn_client_close(), espconn_printf, espconn_server_close(), ICACHE_FLASH_ATTR, NULL, _comon_pkt::pcb, _espconn_msg::pcommon, and _espconn_msg::preverse.

Referenced by espconn_abort(), and espconn_disconnect().

499 {
500  if (pdiscon != NULL){
501  /*disconnect with the host by send the FIN frame*/
502  if (pdiscon->preverse != NULL)
503  espconn_server_close(pdiscon, pdiscon->pcommon.pcb,type);
504  else
505  espconn_client_close(pdiscon, pdiscon->pcommon.pcb,type);
506  } else{
507  espconn_printf("espconn_tcp_disconnect err.\n");
508  }
509 }
static void espconn_client_close(void *arg, struct tcp_pcb *pcb, u8 type)
Definition: espconn_tcp.c:520
#define NULL
Definition: def.h:47
void * pcb
Definition: espconn.h:150
void * preverse
Definition: espconn.h:173
comon_pkt pcommon
Definition: espconn.h:170
static void espconn_server_close(void *arg, struct tcp_pcb *pcb, u8 type)
Definition: espconn_tcp.c:950
#define espconn_printf(fmt, args...)
Definition: espconn.h:10
Here is the call graph for this function:
Here is the caller graph for this function:

◆ espconn_tcp_server()

sint8 espconn_tcp_server ( struct espconn espconn)

Definition at line 1281 of file espconn_tcp.c.

References _espconn_msg::count_opt, espconn_list_creat(), ESPCONN_LISTEN, ESPCONN_MEM, ESPCONN_OK, espconn_tcp_accept(), IP_ADDR_ANY, _esp_tcp::local_port, memp_free(), MEMP_NUM_TCP_PCB, NULL, os_free, os_zalloc, _espconn_msg::pcommon, _espconn_msg::pespconn, _espconn_msg::preverse, espconn::proto, espconn::state, espconn::tcp, and _comon_pkt::timeout.

Referenced by espconn_accept().

1282 {
1283  struct tcp_pcb *pcb = NULL;
1284  espconn_msg *pserver = NULL;
1285 
1286  /*Creates a new server control message*/
1287  pserver = (espconn_msg *)os_zalloc(sizeof(espconn_msg));
1288  if (pserver == NULL){
1289  return ESPCONN_MEM;
1290  }
1291 
1292  /*Creates a new TCP protocol control block*/
1293  pcb = tcp_new();
1294  if (pcb == NULL) {
1295  /*to prevent memory leaks, ensure that each allocated is deleted*/
1296  os_free(pserver);
1297  pserver = NULL;
1298  return ESPCONN_MEM;
1299  } else {
1300  struct tcp_pcb *lpcb = NULL;
1301  /*Binds the connection to a local port number and any IP address*/
1302  tcp_bind(pcb, IP_ADDR_ANY, espconn->proto.tcp->local_port);
1303  lpcb = pcb;
1304  /*malloc and set the state of the connection to be LISTEN*/
1305  pcb = tcp_listen(pcb);
1306  if (pcb != NULL) {
1307  /*insert the node to the active connection list*/
1308  espconn_list_creat(&pserver_list, pserver);
1309  pserver->preverse = pcb;
1310  pserver->pespconn = espconn;
1311  pserver->count_opt = MEMP_NUM_TCP_PCB;
1312  pserver->pcommon.timeout = 0x0a;
1313  espconn ->state = ESPCONN_LISTEN;
1314  /*set the specify argument that should be passed callback function*/
1315  tcp_arg(pcb, (void *)espconn);
1316  /*accept callback function to call for this control block*/
1317  tcp_accept(pcb, espconn_tcp_accept);
1318  return ESPCONN_OK;
1319  } else {
1320  /*to prevent memory leaks, ensure that each allocated is deleted*/
1321  memp_free(MEMP_TCP_PCB,lpcb);
1322  os_free(pserver);
1323  pserver = NULL;
1324  return ESPCONN_MEM;
1325  }
1326  }
1327 }
void memp_free(memp_t type, void *mem) ICACHE_FLASH_ATTR
Definition: memp.c:438
esp_tcp * tcp
Definition: espconn.h:105
#define NULL
Definition: def.h:47
int local_port
Definition: espconn.h:72
espconn_msg * pserver_list
Definition: espconn.c:33
uint32 timeout
Definition: espconn.h:160
#define ESPCONN_OK
Definition: espconn.h:20
#define MEMP_NUM_TCP_PCB
Definition: opt.h:251
void * preverse
Definition: espconn.h:173
#define os_zalloc(s)
Definition: mem.h:44
static err_t ICACHE_FLASH_ATTR espconn_tcp_accept(void *arg, struct tcp_pcb *pcb, err_t err)
Definition: espconn_tcp.c:1199
void espconn_list_creat(espconn_msg **phead, espconn_msg *pinsert)
Definition: espconn.c:76
comon_pkt pcommon
Definition: espconn.h:170
#define IP_ADDR_ANY
Definition: ip_addr.h:92
#define os_free(s)
Definition: mem.h:40
union espconn::@1 proto
enum espconn_state state
Definition: espconn.h:103
struct espconn * pespconn
Definition: espconn.h:169
uint8 count_opt
Definition: espconn.h:171
#define ESPCONN_MEM
Definition: espconn.h:21
Here is the call graph for this function:
Here is the caller graph for this function: