feat(espconn): Add espconn source code

internal: b36d3ed7
This commit is contained in:
Espressif Systems
2017-08-25 20:13:17 +08:00
parent 7fbd498a3c
commit 854acab56c
12 changed files with 5170 additions and 2 deletions

View File

@ -1,7 +1,6 @@
gwen:
espconn: 730c7f0
espnow: 1aafc07
main: 730c7f0
main: 4e7f435
mesh: 1aafc07
net80211: 730c7f0
pp: 1474356
@ -9,6 +8,7 @@ gwen:
wps: 1aafc07
gitlab:
espconn: 3a998034
lwip: 2235ad17
driver: 7bee5263
mbedtls: 1ac9f1f4

757
include/espconn/espconn.h Executable file
View File

@ -0,0 +1,757 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ESPCONN_H__
#define __ESPCONN_H__
#include "lwip/dns.h"
#include "arch/sys_arch.h"
#include "esp_common.h"
#include "espconn/espconn_buf.h"
#if 0
#define espconn_printf(fmt, args...) os_printf(fmt,## args)
#else
#define espconn_printf(fmt, args...)
#endif
#if !NO_SYS
#define ESPCONN_API_MUTEX_TAKE() taskENTER_CRITICAL() //vTaskSuspendAll()//taskENTER_CRITICAL()//API_MUTEX_TAKE()
#define ESPCONN_API_MUTEX_GIVE() taskEXIT_CRITICAL() //xTaskResumeAll()//taskEXIT_CRITICAL()//API_MUTEX_GIVE()
#else
#define ESPCONN_API_MUTEX_TAKE()
#define ESPCONN_API_MUTEX_GIVE()
#endif
typedef void* espconn_handle;
typedef void (* espconn_connect_callback)(void* arg);
typedef void (* espconn_reconnect_callback)(void* arg, sint8 err);
/* Definitions for error constants. */
#define ESPCONN_OK 0 /* No error, everything OK. */
#define ESPCONN_MEM -1 /* Out of memory error. */
#define ESPCONN_TIMEOUT -3 /* Timeout. */
#define ESPCONN_RTE -4 /* Routing problem. */
#define ESPCONN_INPROGRESS -5 /* Operation in progress */
#define ESPCONN_MAXNUM -7 /* Total number exceeds the set maximum*/
#define ESPCONN_ABRT -8 /* Connection aborted. */
#define ESPCONN_RST -9 /* Connection reset. */
#define ESPCONN_CLSD -10 /* Connection closed. */
#define ESPCONN_CONN -11 /* Not connected. */
#define ESPCONN_ARG -12 /* Illegal argument. */
#define ESPCONN_IF -14 /* Low_level error */
#define ESPCONN_ISCONN -15 /* Already connected. */
#define ESPCONN_TIME -16 /* Sync Time error */
#define ESPCONN_NODATA -17 /* No data can be read */
#define ESPCONN_HANDSHAKE -28 /* ssl handshake failed */
#define ESPCONN_RESP_TIMEOUT -29 /* ssl handshake no response*/
#define ESPCONN_PROTO_MSG -61 /* ssl application invalid */
#define ESPCONN_SSL 0x01
#define ESPCONN_NORM 0x00
#define ESPCONN_STA 0x01
#define ESPCONN_AP 0x02
#define ESPCONN_AP_STA 0x03
#define STA_NETIF 0x00
#define AP_NETIF 0x01
/** Protocol family and type of the espconn */
enum espconn_type {
ESPCONN_INVALID = 0,
/* ESPCONN_TCP Group */
ESPCONN_TCP = 0x10,
/* ESPCONN_UDP Group */
ESPCONN_UDP = 0x20,
};
/** Current state of the espconn. Non-TCP espconn are always in state ESPCONN_NONE! */
enum espconn_state {
ESPCONN_NONE,
ESPCONN_WAIT,
ESPCONN_LISTEN,
ESPCONN_CONNECT,
ESPCONN_WRITE,
ESPCONN_READ,
ESPCONN_CLOSE
};
typedef struct _esp_tcp {
int remote_port;
int local_port;
uint8 local_ip[4];
uint8 remote_ip[4];
espconn_connect_callback connect_callback;
espconn_reconnect_callback reconnect_callback;
espconn_connect_callback disconnect_callback;
espconn_connect_callback write_finish_fn;
} esp_tcp;
typedef struct _esp_udp {
int remote_port;
int local_port;
uint8 local_ip[4];
uint8 remote_ip[4];
} esp_udp;
typedef struct _remot_info {
enum espconn_state state;
int remote_port;
uint8 remote_ip[4];
} remot_info;
/** A callback prototype to inform about events for a espconn */
typedef void (* espconn_recv_callback)(void* arg, char* pdata, unsigned short len);
typedef void (* espconn_sent_callback)(void* arg);
/** A espconn descriptor */
struct espconn {
/** type of the espconn (TCP, UDP) */
enum espconn_type type;
/** current state of the espconn */
enum espconn_state state;
union {
esp_tcp* tcp;
esp_udp* udp;
} proto;
/** A callback function that is informed about events for this espconn */
espconn_recv_callback recv_callback;
espconn_sent_callback sent_callback;
uint8 link_cnt;
void* reverse;
};
enum espconn_option {
ESPCONN_START = 0x00,
ESPCONN_REUSEADDR = 0x01,
ESPCONN_NODELAY = 0x02,
ESPCONN_COPY = 0x04,
ESPCONN_KEEPALIVE = 0x08,
ESPCONN_END
};
enum espconn_level {
ESPCONN_KEEPIDLE,
ESPCONN_KEEPINTVL,
ESPCONN_KEEPCNT
};
enum espconn_mode {
ESPCONN_NOMODE,
ESPCONN_TCPSERVER_MODE,
ESPCONN_TCPCLIENT_MODE,
ESPCONN_UDP_MODE,
ESPCONN_NUM_MODE
};
struct espconn_packet {
uint16 sent_length; /* sent length successful*/
uint16 snd_buf_size; /* Available buffer size for sending */
uint16 snd_queuelen; /* Available buffer space for sending */
uint16 total_queuelen; /* total Available buffer space for sending */
uint32 packseqno; /* seqno to be sent */
uint32 packseq_nxt; /* seqno expected */
uint32 packnum;
};
typedef struct _espconn_buf {
uint8* payload;
uint8* punsent;
uint16 unsent;
uint16 len;
uint16 tot_len;
struct _espconn_buf* pnext;
} espconn_buf;
typedef struct _comon_pkt {
void* pcb;
int remote_port;
uint8 remote_ip[4];
uint32 local_port;
uint32 local_ip;
espconn_buf* pbuf;
espconn_buf* ptail;
uint8* ptrbuf;
uint16 cntr;
sint8 err;
uint32 timeout;
uint32 recv_check;
uint8 pbuf_num;
struct espconn_packet packet_info;
bool write_flag;
enum espconn_option espconn_opt;
} comon_pkt;
typedef struct _espconn_msg {
struct espconn* pespconn;
comon_pkt pcommon;
uint8 count_opt;
uint8 espconn_mode;
uint8 sig_type;
sint16 hs_status; //the status of the handshake
void* preverse;
void* pssl;
struct _espconn_msg* pnext;
//***********Code for WIFI_BLOCK from upper**************
uint8 recv_hold_flag;
uint16 recv_holded_buf_Len;
//*******************************************************
ringbuf* readbuf;
} espconn_msg;
#ifndef _MDNS_INFO
#define _MDNS_INFO
struct mdns_info {
char* host_name;
char* server_name;
uint16 server_port;
unsigned long ipAddr;
char* txt_data[10];
};
#endif
#define linkMax 15
#define espconn_delay_disabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_NODELAY) != 0)
#define espconn_delay_enabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_NODELAY) == 0)
#define espconn_reuse_disabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_REUSEADDR) != 0)
#define espconn_copy_disabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_COPY) != 0)
#define espconn_copy_enabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_COPY) == 0)
#define espconn_keepalive_disabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_KEEPALIVE) != 0)
#define espconn_keepalive_enabled(espconn) (((espconn)->pcommon.espconn_opt & ESPCONN_KEEPALIVE) == 0)
#define espconn_TaskPrio 26
#define espconn_TaskQueueLen 15
enum espconn_sig {
SIG_ESPCONN_NONE,
SIG_ESPCONN_ERRER,
SIG_ESPCONN_LISTEN,
SIG_ESPCONN_CONNECT,
SIG_ESPCONN_WRITE,
SIG_ESPCONN_SEND,
SIG_ESPCONN_READ,
SIG_ESPCONN_CLOSE
};
#if !NO_SYS
/**
* ESPCONN_THREAD_NAME: The name assigned to the main espconn thread.
*/
#ifndef ESPCONN_THREAD_NAME
#define ESPCONN_THREAD_NAME "espconn_thread"
#endif
/**
* ESPCONN_THREAD_STACKSIZE: The stack size used by the main espconn thread.
* The stack size value itself is platform-dependent, but is passed to
* sys_thread_new() when the thread is created.
*/
#define ESPCONN_THREAD_STACKSIZE 512 //not ok:384
/**
* ESPCONN_THREAD_PRIO: The priority assigned to the main espconn thread.
* The priority value itself is platform-dependent, but is passed to
* sys_thread_new() when the thread is created.
*/
#define ESPCONN_THREAD_PRIO (configMAX_PRIORITIES-7)
//#define ESPCONN_THREAD_PRIO (configMAX_PRIORITIES-1)
/**
* ESPCONN_MBOX_SIZE: The mailbox size for the tcpip thread messages
* The queue size value itself is platform-dependent, but is passed to
* sys_mbox_new() when tcpip_init is called.
*/
#define ESPCONN_MBOX_SIZE 16
#define ESPCONN_MAX_DELAY portMAX_DELAY
/**
* ESPCONN_LOCKING:
*/
#ifndef ESPCONN_LOCKING
#define ESPCONN_LOCKING 0
#endif
/** The global semaphore to lock the task. */
#if ESPCONN_LOCKING
extern sys_mutex_t lock_espconn_task;
#define LOCK_ESPCONN_TASK() sys_mutex_lock(&lock_espconn_task)
#define UNLOCK_ESPCONN_TASK() sys_mutex_unlock(&lock_espconn_task)
#else
#define LOCK_ESPCONN_TASK()
#define UNLOCK_ESPCONN_TASK()
#endif
#else
#endif
/******************************************************************************
* FunctionName : espconn_copy_partial
* Description : reconnect with host
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
void espconn_copy_partial(struct espconn* pesp_dest, struct espconn* pesp_source);
/******************************************************************************
* FunctionName : espconn_copy_partial
* Description : insert the node to the active connection list
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
void espconn_list_creat(espconn_msg** phead, espconn_msg* pinsert);
/******************************************************************************
* FunctionName : espconn_list_delete
* Description : remove the node from the active connection list
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
void espconn_list_delete(espconn_msg** phead, espconn_msg* pdelete);
/******************************************************************************
* FunctionName : espconn_find_connection
* Description : Initialize the server: set up a listening PCB and bind it to
* the defined port
* Parameters : espconn -- the espconn used to build server
* Returns : none
*******************************************************************************/
bool espconn_find_connection(struct espconn* pespconn, espconn_msg** pnode);
/******************************************************************************
* FunctionName : espconn_get_connection_info
* Description : used to specify the function that should be called when disconnect
* Parameters : espconn -- espconn to set the err callback
* discon_cb -- err callback function to call when err
* Returns : none
*******************************************************************************/
sint8 espconn_get_connection_info(struct espconn* pespconn, remot_info** pcon_info, uint8 typeflags);
/******************************************************************************
* FunctionName : espconn_get_packet_info
* Description : get the packet info with host
* Parameters : espconn -- the espconn used to disconnect the connection
* infoarg -- the packet info
* Returns : the errur code
*******************************************************************************/
sint8 espconn_get_packet_info(struct espconn* espconn, struct espconn_packet* infoarg);
/******************************************************************************
* FunctionName : espconn_connect
* Description : The function given as the connect
* Parameters : espconn -- the espconn used to listen the connection
* Returns : none
*******************************************************************************/
extern sint8 espconn_connect(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_disconnect
* Description : disconnect with host
* Parameters : espconn -- the espconn used to disconnect the connection
* Returns : none
*******************************************************************************/
extern sint8 espconn_disconnect(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_delete
* Description : disconnect with host
* Parameters : espconn -- the espconn used to disconnect the connection
* Returns : none
*******************************************************************************/
extern sint8 espconn_delete(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_accept
* Description : The function given as the listen
* Parameters : espconn -- the espconn used to listen the connection
* Returns : none
*******************************************************************************/
extern sint8 espconn_accept(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_create
* Description : sent data for client or server
* Parameters : espconn -- espconn to the data transmission
* Returns : result
*******************************************************************************/
extern sint8 espconn_create(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_tcp_get_wnd
* Description : get the window size of simulatenously active TCP connections
* Parameters : none
* Returns : the number of TCP_MSS active TCP connections
*******************************************************************************/
extern uint8 espconn_tcp_get_wnd(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con
* Description : set the window size simulatenously active TCP connections
* Parameters : num -- the number of TCP_MSS
* Returns : ESPCONN_ARG -- Illegal argument
* ESPCONN_OK -- No error
*******************************************************************************/
extern sint8 espconn_tcp_set_wnd(uint8 num);
/******************************************************************************
* FunctionName : espconn_tcp_get_max_con
* Description : get the number of simulatenously active TCP connections
* Parameters : none
* Returns : none
*******************************************************************************/
extern uint8 espconn_tcp_get_max_con(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con
* Description : set the number of simulatenously active TCP connections
* Parameters : num -- total number
* Returns : none
*******************************************************************************/
extern sint8 espconn_tcp_set_max_con(uint8 num);
/******************************************************************************
* FunctionName : espconn_tcp_get_max_retran
* Description : get the Maximum number of retransmissions of data active TCP connections
* Parameters : none
* Returns : the Maximum number of retransmissions
*******************************************************************************/
extern uint8 espconn_tcp_get_max_retran(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_retran
* Description : set the Maximum number of retransmissions of data active TCP connections
* Parameters : num -- the Maximum number of retransmissions
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_set_max_retran(uint8 num);
/******************************************************************************
* FunctionName : espconn_tcp_get_max_syn
* Description : get the Maximum number of retransmissions of SYN segments
* Parameters : none
* Returns : the Maximum number of retransmissions
*******************************************************************************/
extern uint8 espconn_tcp_get_max_syn(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_syn
* Description : set the Maximum number of retransmissions of SYN segments
* Parameters : num -- the Maximum number of retransmissions
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_set_max_syn(uint8 num);
/******************************************************************************
* FunctionName : espconn_tcp_get_max_con_allow
* Description : get the count of simulatenously active connections on the server
* Parameters : espconn -- espconn to get the count
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_get_max_con_allow(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con_allow
* Description : set the count of simulatenously active connections on the server
* Parameters : espconn -- espconn to set the count
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_set_max_con_allow(struct espconn* espconn, uint8 num);
/******************************************************************************
* FunctionName : espconn_tcp_set_buf_count
* Description : set the total number of espconn_buf on the unsent lists
* Parameters : espconn -- espconn to set the count
* num -- the total number of espconn_buf
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_set_buf_count(struct espconn* espconn, uint8 num);
/******************************************************************************
* FunctionName : espconn_regist_time
* Description : used to specify the time that should be called when don't recv data
* Parameters : espconn -- the espconn used to the connection
* interval -- the timer when don't recv data
* Returns : none
*******************************************************************************/
extern sint8 espconn_regist_time(struct espconn* espconn, uint32 interval, uint8 type_flag);
/******************************************************************************
* FunctionName : espconn_regist_sentcb
* Description : Used to specify the function that should be called when data
* has been successfully delivered to the remote host.
* Parameters : struct espconn *espconn -- espconn to set the sent callback
* espconn_sent_callback sent_cb -- sent callback function to
* call for this espconn when data is successfully sent
* Returns : none
*******************************************************************************/
extern sint8 espconn_regist_sentcb(struct espconn* espconn, espconn_sent_callback sent_cb);
/******************************************************************************
* FunctionName : espconn_regist_sentcb
* Description : Used to specify the function that should be called when data
* has been successfully delivered to the remote host.
* Parameters : espconn -- espconn to set the sent callback
* sent_cb -- sent callback function to call for this espconn
* when data is successfully sent
* Returns : none
*******************************************************************************/
extern sint8 espconn_regist_write_finish(struct espconn* espconn, espconn_connect_callback write_finish_fn);
/******************************************************************************
* FunctionName : espconn_sent
* Description : sent data for client or server
* Parameters : espconn -- espconn to set for client or server
* psent -- data to send
* length -- length of data to send
* Returns : none
*******************************************************************************/
extern sint8 espconn_sent(struct espconn* espconn, uint8* psent, uint16 length);
/******************************************************************************
* FunctionName : espconn_regist_connectcb
* Description : used to specify the function that should be called when
* connects to host.
* Parameters : espconn -- espconn to set the connect callback
* connect_cb -- connected callback function to call when connected
* Returns : none
*******************************************************************************/
extern sint8 espconn_regist_connectcb(struct espconn* espconn, espconn_connect_callback connect_cb);
/******************************************************************************
* FunctionName : espconn_regist_recvcb
* Description : used to specify the function that should be called when recv
* data from host.
* Parameters : espconn -- espconn to set the recv callback
* recv_cb -- recv callback function to call when recv data
* Returns : none
*******************************************************************************/
extern sint8 espconn_regist_recvcb(struct espconn* espconn, espconn_recv_callback recv_cb);
/******************************************************************************
* FunctionName : espconn_regist_reconcb
* Description : used to specify the function that should be called when connection
* because of err disconnect.
* Parameters : espconn -- espconn to set the err callback
* recon_cb -- err callback function to call when err
* Returns : none
*******************************************************************************/
extern sint8 espconn_regist_reconcb(struct espconn* espconn, espconn_reconnect_callback recon_cb);
/******************************************************************************
* FunctionName : espconn_regist_disconcb
* Description : used to specify the function that should be called when disconnect
* Parameters : espconn -- espconn to set the err callback
* discon_cb -- err callback function to call when err
* Returns : none
*******************************************************************************/
extern sint8 espconn_regist_disconcb(struct espconn* espconn, espconn_connect_callback discon_cb);
/******************************************************************************
* FunctionName : espconn_port
* Description : access port value for client so that we don't end up bouncing
* all connections at the same time .
* Parameters : none
* Returns : access port value
*******************************************************************************/
extern uint32 espconn_port(void);
/******************************************************************************
* FunctionName : espconn_set_opt
* Description : access port value for client so that we don't end up bouncing
* all connections at the same time .
* Parameters : none
* Returns : access port value
*******************************************************************************/
extern sint8 espconn_set_opt(struct espconn* espconn, uint8 opt);
/******************************************************************************
* FunctionName : espconn_set_keepalive
* Description : access level value for connection so that we set the value for
* keep alive
* Parameters : espconn -- the espconn used to set the connection
* level -- the connection's level
* value -- the value of time(s)
* Returns : access port value
*******************************************************************************/
extern sint8 espconn_set_keepalive(struct espconn* espconn, uint8 level, void* optarg);
/******************************************************************************
* FunctionName : espconn_get_keepalive
* Description : access level value for connection so that we get the value for
* keep alive
* Parameters : espconn -- the espconn used to get the connection
* level -- the connection's level
* Returns : access keep alive value
*******************************************************************************/
extern sint8 espconn_get_keepalive(struct espconn* espconn, uint8 level, void* optarg);
/******************************************************************************
* FunctionName : espconn_gethostbyname
* Description : Resolve a hostname (string) into an IP address.
* Parameters : pespconn -- espconn to resolve a hostname
* hostname -- the hostname that is to be queried
* addr -- pointer to a ip_addr_t where to store the address if
* it is already cached in the dns_table (only valid if
* ESPCONN_OK is returned!)
* found -- a callback function to be called on success, failure
* or timeout (only if ERR_INPROGRESS is returned!)
* Returns : err_t return code
* - ESPCONN_OK if hostname is a valid IP address string or the host
* name is already in the local names table.
* - ESPCONN_INPROGRESS enqueue a request to be sent to the DNS server
* for resolution if no errors are present.
* - ESPCONN_ARG: dns client not initialized or invalid hostname
*******************************************************************************/
extern err_t espconn_gethostbyname(struct espconn* pespconn, const char* name, ip_addr_t* addr, dns_found_callback found);
/******************************************************************************
* FunctionName : espconn_igmp_join
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
extern sint8 espconn_igmp_join(ip_addr_t* host_ip, ip_addr_t* multicast_ip);
/******************************************************************************
* FunctionName : espconn_igmp_leave
* Description : leave a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
extern sint8 espconn_igmp_leave(ip_addr_t* host_ip, ip_addr_t* multicast_ip);
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : register a device with mdns
* Parameters : ipAddr -- the ip address of device
* hostname -- the hostname of device
* Returns : none
*******************************************************************************/
extern void espconn_mdns_init(struct mdns_info* info);
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : close mdns socket
* Parameters : void
* Returns : none
*******************************************************************************/
extern void espconn_mdns_close(void);
/******************************************************************************
* FunctionName : mdns_server_register
* Description : register a server and join a multicast group
* Parameters : none
* Returns : none
*******************************************************************************/
extern void espconn_mdns_server_register(void);
/******************************************************************************
* FunctionName : mdns_server_register
* Description : unregister server and leave multicast group
* Parameters : none
* Returns : none
*******************************************************************************/
extern void espconn_mdns_server_unregister(void);
/******************************************************************************
* FunctionName : espconn_mdns_get_servername
* Description : get server name
* Parameters : none
* Returns : server name
*******************************************************************************/
extern char* espconn_mdns_get_servername(void);
/******************************************************************************
* FunctionName : espconn_mdns_get_servername
* Description : set server name
* Parameters : server name
* Returns : none
*******************************************************************************/
extern void espconn_mdns_set_servername(const char* name);
/******************************************************************************
* FunctionName : espconn_mdns_set_hostname
* Description : set host name
* Parameters : host name
* Returns : none
*******************************************************************************/
extern void espconn_mdns_set_hostname(char* name);
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : get host name
* Parameters : void
* Returns : hostname
*******************************************************************************/
extern char* espconn_mdns_get_hostname(void);
/******************************************************************************
* FunctionName : espconn_mdns_disable
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
extern void espconn_mdns_disable(void);
/******************************************************************************
* FunctionName : espconn_mdns_enable
* Description : enable mdns
* Parameters : void
* Returns : none
*******************************************************************************/
extern void espconn_mdns_enable(void);
/******************************************************************************
* FunctionName : espconn_dns_setserver
* Description : Initialize one of the DNS servers.
* Parameters : numdns -- the index of the DNS server to set must
* be < DNS_MAX_SERVERS = 2
* dnsserver -- IP address of the DNS server to set
* Returns : none
*******************************************************************************/
extern void espconn_dns_setserver(u8_t numdns, ip_addr_t* dnsserver);
#endif

68
include/espconn/espconn_buf.h Executable file
View File

@ -0,0 +1,68 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _ESPCONN_BUF_H_
#define _ESPCONN_BUF_H_
#include "c_types.h"
typedef struct ringbuf_t {
uint8_t* buf;
uint8_t* head, *tail;
size_t size;
} ringbuf, *ringbuf_t;
ringbuf_t ringbuf_new(size_t capacity);
size_t ringbuf_buffer_size(const struct ringbuf_t* rb);
void ringbuf_reset(ringbuf_t rb);
void ringbuf_free(ringbuf_t* rb);
size_t ringbuf_capacity(const struct ringbuf_t* rb);
size_t ringbuf_bytes_free(const struct ringbuf_t* rb);
size_t ringbuf_bytes_used(const struct ringbuf_t* rb);
int ringbuf_is_full(const struct ringbuf_t* rb);
int ringbuf_is_empty(const struct ringbuf_t* rb);
const void* ringbuf_tail(const struct ringbuf_t* rb);
const void* ringbuf_head(const struct ringbuf_t* rb);
static uint8_t* ringbuf_nextp(ringbuf_t rb, const uint8_t* p);
size_t ringbuf_findchr(const struct ringbuf_t* rb, int c, size_t offset);
size_t ringbuf_memset(ringbuf_t dst, int c, size_t len);
void* ringbuf_memcpy_into(ringbuf_t dst, const void* src, size_t count);
void* ringbuf_memcpy_from(void* dst, ringbuf_t src, size_t count);
#endif /* RINGBUF_H_ */

80
include/espconn/espconn_tcp.h Executable file
View File

@ -0,0 +1,80 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ESPCONN_TCP_H__
#define __ESPCONN_TCP_H__
#ifndef ESPCONN_TCP_DEBUG
#define ESPCONN_TCP_DEBUG LWIP_DBG_OFF
#endif
#include "espconn/espconn.h"
#ifndef ESPCONN_TCP_TIMER
#define ESPCONN_TCP_TIMER 40
#endif
#define espconn_keepalive_enable(pcb) ((pcb)->so_options |= SOF_KEEPALIVE)
#define espconn_keepalive_disable(pcb) ((pcb)->so_options &= ~SOF_KEEPALIVE)
/******************************************************************************
* FunctionName : espconn_kill_oldest_pcb
* Description : A oldest incoming connection has been killed.
* Parameters : none
* Returns : none
*******************************************************************************/
extern void espconn_kill_oldest_pcb(void);
/******************************************************************************
* FunctionName : espconn_tcp_disconnect
* Description : A new incoming connection has been disconnected.
* Parameters : espconn -- the espconn used to disconnect with host
* Returns : none
*******************************************************************************/
extern void espconn_tcp_disconnect(espconn_msg* pdiscon, u8 type);
/******************************************************************************
* FunctionName : espconn_tcp_client
* Description : Initialize the client: set up a connect PCB and bind it to
* the defined port
* Parameters : espconn -- the espconn used to build client
* Returns : none
*******************************************************************************/
extern sint8 espconn_tcp_client(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_tcp_server
* Description : Initialize the server: set up a listening PCB and bind it to
* the defined port
* Parameters : espconn -- the espconn used to build server
* Returns : none
*******************************************************************************/
extern sint8 espconn_tcp_server(struct espconn* espconn);
#endif /* __CLIENT_TCP_H__ */

88
include/espconn/espconn_udp.h Executable file
View File

@ -0,0 +1,88 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ESPCONN_UDP_H__
#define __ESPCONN_UDP_H__
#ifndef ESPCONN_UDP_DEBUG
#define ESPCONN_UDP_DEBUG LWIP_DBG_OFF
#endif
#include "espconn/espconn.h"
/******************************************************************************
* FunctionName : espconn_udp_client
* Description : Initialize the client: set up a PCB and bind it to the port
* Parameters : pespconn -- the espconn used to build client
* Returns : none
*******************************************************************************/
extern sint8 espconn_udp_client(struct espconn* pespconn);
/******************************************************************************
* FunctionName : espconn_udp_disconnect
* Description : A new incoming connection has been disconnected.
* Parameters : espconn -- the espconn used to disconnect with host
* Returns : none
*******************************************************************************/
extern void espconn_udp_disconnect(espconn_msg* pdiscon);
/******************************************************************************
* FunctionName : espconn_udp_server
* Description : Initialize the server: set up a PCB and bind it to the port
* Parameters : pespconn -- the espconn used to build server
* Returns : none
*******************************************************************************/
extern sint8 espconn_udp_server(struct espconn* espconn);
/******************************************************************************
* FunctionName : espconn_udp_sent
* Description : sent data for client or server
* Parameters : void *arg -- client or server to send
* uint8* psent -- Data to send
* uint16 length -- Length of data to send
* Returns : none
*******************************************************************************/
extern err_t espconn_udp_sent(void* arg, uint8* psent, uint16 length);
/******************************************************************************
* FunctionName : espconn_udp_sendto
* Description : sent data for UDP
* Parameters : void *arg -- UDP to send
* uint8* psent -- Data to send
* uint16 length -- Length of data to send
* Returns : return espconn error code.
* - ESPCONN_OK. Successful. No error occured.
* - ESPCONN_MEM. Out of memory.
* - ESPCONN_RTE. Could not find route to destination address.
* - More errors could be returned by lower protocol layers.
*******************************************************************************/
extern err_t espconn_udp_sendto(void* arg, uint8* psent, uint16 length);
#endif /* __ESPCONN_UDP_H__ */

Binary file not shown.

Binary file not shown.

47
third_party/espconn/Makefile vendored Executable file
View File

@ -0,0 +1,47 @@
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
ifndef PDIR
GEN_LIBS = libespconn.a
endif
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#DEFINES +=
CCFLAGS += -ffunction-sections -fdata-sections
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile

1606
third_party/espconn/espconn.c vendored Executable file

File diff suppressed because it is too large Load Diff

229
third_party/espconn/espconn_buf.c vendored Executable file
View File

@ -0,0 +1,229 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <string.h>
#include "lwip/def.h"
#include "esp_libc.h"
#include "espconn/espconn_buf.h"
#if (!defined(lwIP_unlikely))
#define lwIP_unlikely(Expression) !!(Expression)
#endif
#define lwIP_ASSERT(Expression) do{if(!(Expression)) {os_printf("%s %d\n", __func__, __LINE__);return;}}while(0)
ringbuf_t ringbuf_new(size_t capacity)
{
ringbuf_t rb = (ringbuf_t)os_zalloc(sizeof(struct ringbuf_t));
if (rb) {
rb->size = capacity + 1;
rb->buf = (uint8*)os_zalloc(rb->size);
if (rb->buf) {
ringbuf_reset(rb);
} else {
os_free(rb);
return NULL;
}
}
return rb;
}
size_t ringbuf_buffer_size(const struct ringbuf_t* rb)
{
return rb->size;
}
void ringbuf_reset(ringbuf_t rb)
{
rb ->head = rb->tail = rb->buf;
}
void ringbuf_free(ringbuf_t* rb)
{
lwIP_ASSERT(rb && *rb);
os_free((*rb)->buf);
os_free(*rb);
*rb = NULL;
}
size_t ringbuf_capacity(const struct ringbuf_t* rb)
{
return ringbuf_buffer_size(rb) - 1;
}
static const uint8_t* ringbuf_end(const struct ringbuf_t* rb)
{
return rb->buf + ringbuf_buffer_size(rb);
}
size_t ringbuf_bytes_free(const struct ringbuf_t* rb)
{
if (rb->head >= rb->tail) {
return ringbuf_capacity(rb) - (rb->head - rb->tail);
} else {
return rb->tail - rb->head - 1;
}
}
size_t ringbuf_bytes_used(const struct ringbuf_t* rb)
{
return ringbuf_capacity(rb) - ringbuf_bytes_free(rb);
}
int ringbuf_is_full(const struct ringbuf_t* rb)
{
return ringbuf_bytes_free(rb) == 0;
}
int ringbuf_is_empty(const struct ringbuf_t* rb)
{
return ringbuf_bytes_free(rb) == ringbuf_capacity(rb);
}
const void* ringbuf_tail(const struct ringbuf_t* rb)
{
return rb->tail;
}
const void* ringbuf_head(const struct ringbuf_t* rb)
{
return rb->head;
}
static uint8_t* ringbuf_nextp(ringbuf_t rb, const uint8_t* p)
{
lwIP_ASSERT((p >= rb->buf) && (p < ringbuf_end(rb)));
return rb->buf + ((++p - rb->buf) % ringbuf_buffer_size(rb));
}
size_t ringbuf_findchr(const struct ringbuf_t* rb, int c, size_t offset)
{
const uint8_t* bufend = ringbuf_end(rb);
size_t bytes_used = ringbuf_bytes_used(rb);
if (offset >= bytes_used) {
return bytes_used;
}
const uint8_t* start = rb ->buf + (((rb->tail - rb->buf) + offset) % ringbuf_buffer_size(rb));
lwIP_ASSERT(bufend > start);
size_t n = LWIP_MIN(bufend - start, bytes_used - offset);
const uint8_t* found = (const uint8_t*)memchr(start, c, n);
if (found) {
return offset + (found - start);
} else {
return ringbuf_findchr(rb, c, offset + n);
}
}
size_t ringbuf_memset(ringbuf_t dst, int c, size_t len)
{
const uint8_t* bufend = ringbuf_end(dst);
size_t nwritten = 0;
size_t count = LWIP_MIN(len, ringbuf_buffer_size(dst));
int overflow = count > ringbuf_bytes_free(dst);
while (nwritten != count) {
lwIP_ASSERT(bufend > dst->head);
size_t n = LWIP_MIN(bufend - dst->head, count - nwritten);
os_memset(dst->head, c, n);
dst->head += n;
nwritten += n;
if (dst->head == bufend) {
dst->head = dst->buf;
}
}
if (overflow) {
dst->tail = ringbuf_nextp(dst, dst->head);
lwIP_ASSERT(ringbuf_is_full(dst));
}
return nwritten;
}
void* ringbuf_memcpy_into(ringbuf_t dst, const void* src, size_t count)
{
const uint8_t* u8src = src;
const uint8_t* bufend = ringbuf_end(dst);
int overflow = count > ringbuf_bytes_free(dst);
size_t nread = 0;
while (nread != count) {
lwIP_ASSERT(bufend > dst->head);
size_t n = LWIP_MIN(bufend - dst->head, count - nread);
memcpy(dst->head, u8src + nread, n);
dst->head += n;
nread += n;
if (dst->head == bufend) {
dst->head = dst->buf;
}
}
if (overflow) {
dst->tail = ringbuf_nextp(dst, dst->head);
lwIP_ASSERT(ringbuf_is_full(dst));
}
return dst->head;
}
void* ringbuf_memcpy_from(void* dst, ringbuf_t src, size_t count)
{
size_t bytes_used = ringbuf_bytes_used(src);
if (count > bytes_used) {
return NULL;
}
const uint8_t* u8dst = dst;
const uint8_t* bufend = ringbuf_end(src);
size_t nwritten = 0;
while (nwritten != count) {
lwIP_ASSERT(bufend > src->tail);
size_t n = LWIP_MIN(bufend - src->tail, count - nwritten);
memcpy((uint8_t*)u8dst + nwritten, src->tail, n);
src->tail += n;
nwritten += n;
if (src->tail == bufend) {
src->tail = src->buf;
}
}
lwIP_ASSERT(count + ringbuf_bytes_used(src) == bytes_used);
return src->tail;
}

1816
third_party/espconn/espconn_tcp.c vendored Executable file

File diff suppressed because it is too large Load Diff

477
third_party/espconn/espconn_udp.c vendored Executable file
View File

@ -0,0 +1,477 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "lwip/inet.h"
#include "lwip/err.h"
#include "lwip/pbuf.h"
#include "lwip/mem.h"
#include "lwip/tcp_impl.h"
#include "lwip/udp.h"
#include "esp_common.h"
#include "espconn/espconn_udp.h"
#ifdef MEMLEAK_DEBUG
static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
#endif
#define IP_MAX_MTU 1500
extern espconn_msg* plink_active;
uint8 default_interface;
enum send_opt {
ESPCONN_SENDTO,
ESPCONN_SEND
};
static void ICACHE_FLASH_ATTR espconn_data_sentcb(struct espconn* pespconn)
{
if (pespconn == NULL) {
return;
}
if (pespconn->sent_callback != NULL) {
pespconn->sent_callback(pespconn);
}
}
static void ICACHE_FLASH_ATTR espconn_data_sent(void* arg, enum send_opt opt)
{
espconn_msg* psent = arg;
if (psent == NULL) {
return;
}
if (psent->pcommon.cntr == 0) {
psent->pespconn->state = ESPCONN_CONNECT;
if (psent->pcommon.err == 0) {
espconn_data_sentcb(psent->pespconn);
}
} else {
if (opt == ESPCONN_SEND) {
espconn_udp_sent(arg, psent->pcommon.ptrbuf, psent->pcommon.cntr);
} else {
espconn_udp_sendto(arg, psent->pcommon.ptrbuf, psent->pcommon.cntr);
}
}
}
/******************************************************************************
* FunctionName : espconn_udp_sent
* Description : sent data for client or server
* Parameters : void *arg -- client or server to send
* uint8* psent -- Data to send
* uint16 length -- Length of data to send
* Returns : return espconn error code.
* - ESPCONN_OK. Successful. No error occured.
* - ESPCONN_MEM. Out of memory.
* - ESPCONN_RTE. Could not find route to destination address.
* - More errors could be returned by lower protocol layers.
*******************************************************************************/
err_t ICACHE_FLASH_ATTR
espconn_udp_sent(void* arg, uint8* psent, uint16 length)
{
espconn_msg* pudp_sent = arg;
struct udp_pcb* upcb = pudp_sent->pcommon.pcb;
struct pbuf* p, *q , *p_temp;
u8_t* data = NULL;
u16_t cnt = 0;
u16_t datalen = 0;
u16_t i = 0;
err_t err;
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d %p\n", __LINE__, length, upcb));
if (pudp_sent == NULL || upcb == NULL || psent == NULL || length == 0) {
return ESPCONN_ARG;
}
if ((IP_MAX_MTU - 20 - 8) < length) {
datalen = IP_MAX_MTU - 20 - 8;
} else {
datalen = length;
}
p = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM);
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p));
if (p != NULL) {
q = p;
while (q != NULL) {
data = (u8_t*)q->payload;
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, data));
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t*) psent)[cnt++];
}
q = q->next;
}
} else {
return ESPCONN_MEM;
}
upcb->remote_port = pudp_sent->pespconn->proto.udp->remote_port;
IP4_ADDR(&(upcb->remote_ip.ip4), pudp_sent->pespconn->proto.udp->remote_ip[0],
pudp_sent->pespconn->proto.udp->remote_ip[1],
pudp_sent->pespconn->proto.udp->remote_ip[2],
pudp_sent->pespconn->proto.udp->remote_ip[3]);
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %x %d\n", __LINE__, upcb->remote_ip, upcb->remote_port));
struct netif* sta_netif = (struct netif*)eagle_lwip_getif(0x00);
struct netif* ap_netif = (struct netif*)eagle_lwip_getif(0x01);
if (wifi_get_opmode() == ESPCONN_AP_STA && default_interface == ESPCONN_AP_STA && sta_netif != NULL && ap_netif != NULL) {
if (netif_is_up(sta_netif) && netif_is_up(ap_netif) && \
ip_addr_isbroadcast(&(upcb->remote_ip.ip4), sta_netif) && \
ip_addr_isbroadcast(&(upcb->remote_ip.ip4), ap_netif)) {
p_temp = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM);
if (pbuf_copy(p_temp, p) != ERR_OK) {
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent: copying to new pbuf failed\n"));
return ESPCONN_ARG;
}
netif_set_default(sta_netif);
err = udp_send(upcb, p_temp);
pbuf_free(p_temp);
netif_set_default(ap_netif);
}
}
err = udp_send(upcb, p);
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d\n", __LINE__, err));
if (p->ref != 0) {
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p));
pbuf_free(p);
pudp_sent->pcommon.ptrbuf = psent + datalen;
pudp_sent->pcommon.cntr = length - datalen;
pudp_sent->pcommon.err = err;
espconn_data_sent(pudp_sent, ESPCONN_SEND);
if (err > 0) {
return ESPCONN_IF;
}
return err;
} else {
pbuf_free(p);
return ESPCONN_RTE;
}
}
/******************************************************************************
* FunctionName : espconn_udp_sendto
* Description : sent data for UDP
* Parameters : void *arg -- UDP to send
* uint8* psent -- Data to send
* uint16 length -- Length of data to send
* Returns : return espconn error code.
* - ESPCONN_OK. Successful. No error occured.
* - ESPCONN_MEM. Out of memory.
* - ESPCONN_RTE. Could not find route to destination address.
* - More errors could be returned by lower protocol layers.
*******************************************************************************/
err_t ICACHE_FLASH_ATTR
espconn_udp_sendto(void* arg, uint8* psent, uint16 length)
{
espconn_msg* pudp_sent = arg;
struct udp_pcb* upcb = pudp_sent->pcommon.pcb;
struct espconn* pespconn = pudp_sent->pespconn;
struct pbuf* p, *q , *p_temp;
struct ip_addr dst_ip;
u16_t dst_port;
u8_t* data = NULL;
u16_t cnt = 0;
u16_t datalen = 0;
u16_t i = 0;
err_t err;
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d %p\n", __LINE__, length, upcb));
if (pudp_sent == NULL || upcb == NULL || psent == NULL || length == 0) {
return ESPCONN_ARG;
}
if ((IP_MAX_MTU - 20 - 8) < length) {
datalen = IP_MAX_MTU - 20 - 8;
} else {
datalen = length;
}
p = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM);
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p));
if (p != NULL) {
q = p;
while (q != NULL) {
data = (u8_t*)q->payload;
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, data));
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t*) psent)[cnt++];
}
q = q->next;
}
} else {
return ESPCONN_MEM;
}
dst_port = pespconn->proto.udp->remote_port;
IP4_ADDR(&dst_ip, pespconn->proto.udp->remote_ip[0],
pespconn->proto.udp->remote_ip[1], pespconn->proto.udp->remote_ip[2],
pespconn->proto.udp->remote_ip[3]);
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %x %d\n", __LINE__, upcb->remote_ip, upcb->remote_port));
#if LWIP_IPV6 || LWIP_IGMP
ipX_addr_t* dst_ip_route = ip_2_ipX(&dst_ip);
if (ipX_addr_ismulticast(PCB_ISIPV6(upcb), dst_ip_route)) {
/* For multicast, find a netif based on source address. */
#if LWIP_IPV6
if (PCB_ISIPV6(upcb)) {
dst_ip_route = &upcb->local_ip;
} else
#endif /* LWIP_IPV6 */
{
#if LWIP_IGMP
upcb->multicast_ip.addr = dst_ip.addr;
#endif /* LWIP_IGMP */
}
}
#endif /* LWIP_IPV6 || LWIP_IGMP */
struct netif* sta_netif = (struct netif*)eagle_lwip_getif(0x00);
struct netif* ap_netif = (struct netif*)eagle_lwip_getif(0x01);
if (wifi_get_opmode() == ESPCONN_AP_STA && default_interface == ESPCONN_AP_STA && sta_netif != NULL && ap_netif != NULL) {
if (netif_is_up(sta_netif) && netif_is_up(ap_netif) && \
ip_addr_isbroadcast(&(upcb->remote_ip.ip4), sta_netif) && \
ip_addr_isbroadcast(&(upcb->remote_ip.ip4), ap_netif)) {
p_temp = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM);
if (pbuf_copy(p_temp, p) != ERR_OK) {
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sendto: copying to new pbuf failed\n"));
return ESPCONN_ARG;
}
netif_set_default(sta_netif);
err = udp_sendto(upcb, p_temp, &dst_ip, dst_port);
pbuf_free(p_temp);
netif_set_default(ap_netif);
}
}
err = udp_sendto(upcb, p, &dst_ip, dst_port);
if (p->ref != 0) {
pbuf_free(p);
pudp_sent->pcommon.ptrbuf = psent + datalen;
pudp_sent->pcommon.cntr = length - datalen;
pudp_sent->pcommon.err = err;
espconn_data_sent(pudp_sent, ESPCONN_SENDTO);
if (err > 0) {
return ESPCONN_IF;
}
return err;
} else {
pbuf_free(p);
return ESPCONN_RTE;
}
}
/******************************************************************************
* FunctionName : espconn_udp_server_recv
* Description : This callback will be called when receiving a datagram.
* Parameters : arg -- user supplied argument
* upcb -- the udp_pcb which received data
* p -- the packet buffer that was received
* addr -- the remote IP address from which the packet was received
* port -- the remote port from which the packet was received
* Returns : none
*******************************************************************************/
static void ICACHE_FLASH_ATTR
espconn_udp_recv(void* arg, struct udp_pcb* upcb, struct pbuf* p,
struct ip_addr* addr, u16_t port)
{
espconn_msg* precv = arg;
u8_t* pdata = NULL;
u16_t length = 0;
struct ip_info ipconfig;
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_server_recv %d %p\n", __LINE__, upcb));
//ESPCONN_API_MUTEX_TAKE();
precv->pcommon.remote_ip[0] = ip4_addr1_16(addr);
precv->pcommon.remote_ip[1] = ip4_addr2_16(addr);
precv->pcommon.remote_ip[2] = ip4_addr3_16(addr);
precv->pcommon.remote_ip[3] = ip4_addr4_16(addr);
precv->pcommon.remote_port = port;
precv->pcommon.pcb = upcb;
if (wifi_get_opmode() != 1) {
wifi_get_ip_info(1, &ipconfig);
if (!ip_addr_netcmp(addr, &ipconfig.ip, &ipconfig.netmask)) {
wifi_get_ip_info(0, &ipconfig);
}
} else {
wifi_get_ip_info(0, &ipconfig);
}
precv->pespconn->proto.udp->local_ip[0] = ip4_addr1_16(&ipconfig.ip);
precv->pespconn->proto.udp->local_ip[1] = ip4_addr2_16(&ipconfig.ip);
precv->pespconn->proto.udp->local_ip[2] = ip4_addr3_16(&ipconfig.ip);
precv->pespconn->proto.udp->local_ip[3] = ip4_addr4_16(&ipconfig.ip);
if (p != NULL) {
pdata = (u8_t*)os_zalloc(p ->tot_len + 1);
length = pbuf_copy_partial(p, pdata, p ->tot_len, 0);
precv->pcommon.pcb = upcb;
pbuf_free(p);
if (length != 0) {
if (precv->pespconn->recv_callback != NULL) {
precv->pespconn->recv_callback(precv->pespconn, pdata, length);
}
}
os_free(pdata);
//ESPCONN_API_MUTEX_GIVE();
} else {
//ESPCONN_API_MUTEX_GIVE();
return;
}
}
/******************************************************************************
* FunctionName : espconn_udp_disconnect
* Description : A new incoming connection has been disconnected.
* Parameters : espconn -- the espconn used to disconnect with host
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR espconn_udp_disconnect(espconn_msg* pdiscon)
{
if (pdiscon == NULL) {
return;
}
struct udp_pcb* upcb = pdiscon->pcommon.pcb;
udp_disconnect(upcb);
udp_remove(upcb);
espconn_list_delete(&plink_active, pdiscon);
os_free(pdiscon);
pdiscon = NULL;
}
/******************************************************************************
* FunctionName : espconn_udp_server
* Description : Initialize the server: set up a PCB and bind it to the port
* Parameters : pespconn -- the espconn used to build server
* Returns : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_udp_server(struct espconn* pespconn)
{
struct udp_pcb* upcb = NULL;
espconn_msg* pserver = NULL;
upcb = udp_new();
if (upcb == NULL) {
return ESPCONN_MEM;
} else {
pserver = (espconn_msg*)os_zalloc(sizeof(espconn_msg));
if (pserver == NULL) {
udp_remove(upcb);
return ESPCONN_MEM;
}
pserver->pcommon.pcb = upcb;
pserver->pespconn = pespconn;
espconn_list_creat(&plink_active, pserver);
udp_bind(upcb, IP_ADDR_ANY, pserver->pespconn->proto.udp->local_port);
udp_recv(upcb, espconn_udp_recv, (void*)pserver);
return ESPCONN_OK;
}
}
/******************************************************************************
* FunctionName : espconn_igmp_leave
* Description : leave a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_igmp_leave(ip_addr_t* host_ip, ip_addr_t* multicast_ip)
{
#if LWIP_IGMP
if (igmp_leavegroup(host_ip, multicast_ip) != ERR_OK) {
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("udp_leave_multigrup failed!\n"));
return -1;
};
#endif
return ESPCONN_OK;
}
/******************************************************************************
* FunctionName : espconn_igmp_join
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_igmp_join(ip_addr_t* host_ip, ip_addr_t* multicast_ip)
{
#if LWIP_IGMP
if (igmp_joingroup(host_ip, multicast_ip) != ERR_OK) {
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("udp_join_multigrup failed!\n"));
return -1;
};
#endif
/* join to any IP address at the port */
return ESPCONN_OK;
}