Merge branch 'refactor' into 'master'

Add esp socket between lwip and wifi

See merge request sdk/ESP8266_RTOS_SDK!96
This commit is contained in:
Wu Jian Gang
2018-04-27 17:52:27 +08:00
18 changed files with 1029 additions and 40 deletions

View File

@ -0,0 +1,64 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ESP_AIO_H
#define _ESP_AIO_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* declare
*/
struct esp_aio;
/*
* aio callback function type
*/
typedef int (*esp_aio_cb_t)(struct esp_aio *aio);
/*
* aio data object
*/
typedef struct esp_aio {
int fd; // file description
const char *pbuf; // send/recv data pointer
size_t len; // buffer length by bytes
esp_aio_cb_t cb; // callback function
void *arg; // user callback function private data
int ret; // asynchronous operation result
} esp_aio_t;
/*
* aio data object
*/
typedef struct esp_aio_data {
const char *pbuf; // send/recv data pointer
size_t len; // buffer length by bytes
int status; // lowlevel data status
} esp_aio_data_t;
#ifdef __cplusplus
}
#endif
#endif /* _ESP_AIO_H */

View File

@ -0,0 +1,38 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ESP_MODULE_H
#define _ESP_MODULE_H
#ifdef __cplusplus
extern "C" {
#endif
#define __EXPORT_SYMBOL_NAME(sec) "ksymatab"sec
#define __EXPORT_SYMBOL(sym, sec) \
static const typeof(sym) *__ksymtab_##sym \
__attribute__((used,section("ksymatab"sec))) \
= &sym
#define __DECLARE_SYMBOL_NAME(sec) __EXPORT_SYMBOL_NAME(sec)
#define __DECLARE_SYMBOL(type, sym, sec) \
static const type sym __attribute__((used,section("ksymatab"sec),aligned(sizeof(void *))))
#ifdef __cplusplus
}
#endif
#endif /* _ESP_MODULE_H */

View File

@ -0,0 +1,230 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ESP_SOCKET_H
#define _ESP_SOCKET_H
#include <stdint.h>
#include "esp_aio.h"
#include "esp_module.h"
#include "net/if_packet.h"
#include "net/if_socket.h"
#ifdef __cplusplus
extern "C" {
#endif
/* used for socket link */
#define ESP_SOCKET_METHOD_BASENAME "esp_socket"
#define ESP_SOCKET_METHOD_NAME(sec) __DECLARE_SYMBOL_NAME(ESP_SOCKET_METHOD_BASENAME)
#define ESP_SOCKET_METHOD_DECLARE(sym) __DECLARE_SYMBOL(esp_socket_method_t, sym, ESP_SOCKET_METHOD_BASENAME)
#ifndef socklen_t
typedef uint32_t socklen_t;
#endif
#define esp_socket_va_start(va, arg) va = (va_list)arg
/*
* socket async event
*/
enum esp_socket_event_type {
ESP_SOCKET_CONNECT_EVENT = 0,
ESP_SOCKET_ACCEPT_EVENT = ESP_SOCKET_CONNECT_EVENT,
ESP_SOCKET_RECV_EVENT = 1,
ESP_SOCKET_ERROR_EVENT = 2,
ESP_SOCKET_MAX_EVENT
};
/*
* socket information object
*/
typedef struct esp_socket_info {
int domain;
int type;
int protocol;
} esp_socket_info_t;
/*
* socket method object
*/
typedef struct esp_socket_method {
/*
* socket method name
*/
const char *name;
/*
* @brief open lowlevel socket module
*
* @param info socket information
*
* @return return lowlevel socket index if successfully, otherwise return NULL
* and you can get the real fail reason by "errno"
*/
void* (*open)(esp_socket_info_t *info);
/*
* @brief send a block of data asynchronously and receive result by callback function
*
* @param index lowlevel index
* @param aio asynchronous I/O controlling block
* @param to target address with lowlevel address data format
* @param len target address data length
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int (*aio_sendto)(void *index, esp_aio_t *aio, const struct sockaddr_ll *to, socklen_t len);
/*
* @brief register an event and its callback function to lowlevel socket module
*
* @param index lowlevel index
* @param event asynchronous I/O event
* @param cb callback function
* @param arg callback function parameter
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int (*aio_event)(void *index, unsigned int event, esp_aio_cb_t cb, void *arg);
/*
* @brief free buffer taken from event callback
*
* @param index lowlevel index
* @param pbuf buffer pointer
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int (*free_pbuf)(void *index, void *pbuf);
/*
* @brief send request command to lowlevel socket module and get the result synchronously
*
* @param index lowlevel index
* @param cmd request command
* @param arg start address to variable parameters
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int (*ioctl)(void *index, unsigned int cmd, void *arg);
/*
* @brief close lowlevel socket module
*
* @param index lowlevel index
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int (*close)(void *index);
} esp_socket_method_t;
/*
* @brief create a socket file description
*
* @param domain protocal domain and it must be "AF_PACKET" now
* @param type socket type and it must be "SOCK_RAW" now
* @param protocol target protocol and it must be "ETH_P_ALL" now
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*
* @note using "AF_PACKET", "SOCK_RAW" and "ETH_P_ALL" means that you receive and send full
* ethernet II type message with full message head: " destination mac | source mac | type"
*/
int esp_socket(int domain, int type, int protocol);
/*
* @brief send a block of data asynchronously and receive result by callback function
*
* @param aio asynchronous I/O controlling block
* @param to target address with lowlevel address data format
* @param len target address data length
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int esp_aio_sendto(esp_aio_t *aio, const struct sockaddr_ll *to, socklen_t len);
/*
* @brief register an event and its callback function to target of file description
*
* @param fd file description
* @param event asynchronous I/O event
* @param cb callback function
* @param arg callback function parameter
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int esp_aio_event(int fd, unsigned int event, esp_aio_cb_t cb, void *arg);
/*
* @brief lowlevel socket module upload event and its data
*
* @param index lowlevel index
* @param info socket information
* @param event asynchronous I/O event
* @param aio asynchronous I/O controlling block
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int esp_upload_event(void *index, esp_socket_info_t *info, unsigned int event, esp_aio_data_t *aio_data);
/*
* @brief free buffer taken from event callback
*
* @param fd file description
* @param pbuf buffer pointer
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int esp_free_pbuf(int fd, void *pbuf);
/*
* @brief send request command to target by file description and get the result synchronously
*
* @param fd file description
* @param cmd request command
* @param ... realy command parameters and it must be related to realy object of lowlevel
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int esp_ioctl(int fd, unsigned int cmd, ...);
/*
* @brief close target of file description
*
* @param fd file description
*
* @return return 0 if successfully, otherwise return -1 and you can get the real fail
* reason by "errno"
*/
int esp_close(int fd);
#ifdef __cplusplus
}
#endif
#endif /* _ESP_SOCKET_H */

View File

@ -0,0 +1,41 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _IF_PACKET_H
#define _IF_PACKET_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* socket II type address used by low-level module whose address is base on MAC address
*
* Note: Now it just support 802.3 and 802.11 protocol, so only "sll_addr" works here
*/
struct sockaddr_ll {
unsigned short sll_family; // it must be AF_PACKET
unsigned short sll_protocol; // physics level protocol
int sll_ifindex; // interface index not socket ID
unsigned short sll_hatype; // ARP hardware address type
unsigned char sll_pkttype; // packet type
unsigned char sll_halen; // hardware address length
unsigned char sll_addr[8]; // address data
};
#ifdef __cplusplus
}
#endif
#endif /* _IF_PACKET_H */

View File

@ -0,0 +1,76 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _IF_ETHER_H
#define _IF_ETHER_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* undefine some global macro
*/
#undef ETH_P_ALL
#undef ETH_P_ARP
#undef ETH_P_IP
#undef AF_PACKET
#undef SOCK_RAW
/*
* socket domain
*/
#define AF_PACKET 0
/*
* socket type
*/
#define SOCK_RAW 0
/*
* use 16(2^4) type for socket
*/
#define IF_SOCK_SHIFT 4
#define IF_SOCK_TYPE(d) (d & 0xf)
#define IF_SOCK_DATA(t, s) ((1 << (s + IF_SOCK_SHIFT)) + t)
enum if_sock_type {
IF_SOCK_ETH = 0,
IF_SOCK_WIFI,
IF_SOCK_MAX,
};
#define IF_ETH_DATA(s) IF_SOCK_DATA(IF_SOCK_ETH, s)
#define IF_WIFI_DATA(s) IF_SOCK_DATA(IF_SOCK_WIFI, s)
/*
* socket protocol
*/
#define ETH_P_IP IF_ETH_DATA(0)
#define ETH_P_ARP IF_ETH_DATA(1)
#define ETH_P_ALL ETH_P_IP | ETH_P_ARP
#define WIFI_P_MNG IF_WIFI_DATA(0)
#define WIFI_P_CTL IF_WIFI_DATA(1)
#define WIFI_P_DAT IF_WIFI_DATA(2)
#define WIFI_P_ALL WIFI_P_MNG | WIFI_P_CTL | WIFI_P_DAT
#ifdef __cplusplus
}
#endif
#endif /* _IF_ETHER_H */

View File

@ -0,0 +1,46 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _SOCKIO_H
#define _SOCKIO_H
#ifdef __cplusplus
extern "C" {
#endif
/* espressif specific socket ioctls */
#define SIOESPSTART 0x5500 /* start of espressif specific code */
/* routing table calls. */
#define SIOCADDRT 0x890B /* add routing table entry */
#define SIOCDELRT 0x890C /* delete routing table entry */
#define SIOCRTMSG 0x890D /* call to routing system */
/* socket configuration controls. */
#define SIOCGIFADDR 0x8915 /* get PA address */
#define SIOCSIFADDR 0x8916 /* set PA address */
#define SIOCGIFBRDADDR 0x8919 /* get broadcast PA address */
#define SIOCSIFBRDADDR 0x891a /* set broadcast PA address */
#define SIOCGIFNETMASK 0x891b /* get network PA mask */
#define SIOCSIFNETMASK 0x891c /* set network PA mask */
#define SIOCSIFHWADDR 0x8924 /* set hardware address */
#define SIOCGIFHWADDR 0x8927 /* Get hardware address */
#define SIOCGIFINDEX 0x8933 /* name -> if_index mapping */
#define SIOGIFINDEX SIOCGIFINDEX /* misprint compatibility :-) */
#ifdef __cplusplus
}
#endif
#endif /* _SOCKIO_H */

View File

@ -114,6 +114,12 @@ SECTIONS
*libplatforms.a:(.rodata.* .rodata)
*(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text)
*(.literal.* .text.*)
. = ALIGN(16);
__start_ksymatabesp_socket = .;
*(ksymatabesp_socket)
__stop_ksymatabesp_socket = .;
_irom0_text_end = ABSOLUTE(.);
} >irom0_0_seg :irom0_0_phdr

View File

@ -1,11 +1,11 @@
gwen:
crypto: 5fc5b4f
espnow: 5fc5b4f
core: 5fc5b4f
core: 06675a3
minic: 5fc5b4f
net80211: 5fc5b4f
pp: 5fc5b4f
net80211: 06675a3
pp: 06675a3
pwm: 5fc5b4f
smartconfig:9ec59b5
wpa: 5fc5b4f
wps: 5fc5b4f
wpa: 06675a3
wps: 06675a3

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,333 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/errno.h>
#include "esp_socket.h"
#include "net/sockio.h"
#define CRITICAL_DECLARE(t)
#define CRITICAL_ENTER(t)
#define CRITICAL_EXIT(t)
#ifndef ESP_SOCKET_MAX
#define ESP_SOCKET_MAX 3
#endif
#define SET_ERR(err)
#define CHECK_FD(s) \
if (s >= ESP_SOCKET_MAX \
|| !s_socket[s].info) { \
SET_ERR(EINVAL) \
return -1; \
}
#define CHECK_METHOD(s, io) \
CHECK_FD(s) \
if (!s_socket[s].method \
|| !s_socket[s].method->io) { \
SET_ERR(ESRCH); \
return -1; \
}
#define SOCKET_IO_METHOD(s, io, ...) \
s_socket[s].method->io(s_socket[s].index, ##__VA_ARGS__)
/*
* socket event object
*/
typedef struct esp_socket_event {
esp_aio_cb_t cb;
void *arg;
} esp_socket_event_t;
/*
* socket object
*/
typedef struct esp_socket {
esp_socket_info_t *info;
/*
* lowlevel socket module index
*/
void *index;
/*
* lowlevel socket module method
*/
const esp_socket_method_t *method;
esp_socket_event_t event[ESP_SOCKET_MAX_EVENT];
} esp_socket_t;
static esp_socket_t s_socket[ESP_SOCKET_MAX];
static inline int event_is_used(int s, int e)
{
return s_socket[s].event[e].cb != NULL;
}
static inline int alloc_event(int s, int e)
{
CRITICAL_DECLARE(t);
if (e >= ESP_SOCKET_MAX_EVENT)
return -1;
CRITICAL_ENTER(t);
if (event_is_used(s, e)) {
e = ESP_SOCKET_MAX_EVENT;
}
CRITICAL_EXIT(t);
return e < ESP_SOCKET_MAX_EVENT ? e : -1;
}
static inline void free_event(int s, int e)
{
s_socket[s].event[e].cb = NULL;
}
static inline int alloc_socket(void)
{
int s;
CRITICAL_DECLARE(t);
CRITICAL_ENTER(t);
for (s = 0; s < ESP_SOCKET_MAX; s++) {
if (s_socket[s].info == NULL) {
s_socket[s].info = (void *)1;
break;
}
}
CRITICAL_EXIT(t);
return s < ESP_SOCKET_MAX ? s : -1;
}
static inline void free_socket(int s)
{
int e;
s_socket[s].info = NULL;
for (e = 0; e < ESP_SOCKET_MAX_EVENT; e++) {
free_event(s, e);
}
}
/*
* @brief create a socket file description
*/
int esp_socket(int domain, int type, int protocol)
{
int s;
s = alloc_socket();
if (s < 0) {
SET_ERR(ENOMEM)
return -1;
}
s_socket[s].info = malloc(sizeof(esp_socket_info_t));
if (!s_socket[s].info) {
free_socket(s);
SET_ERR(ENOMEM)
return -1;
}
s_socket[s].info->domain = domain;
s_socket[s].info->type = type;
s_socket[s].info->protocol = protocol;
s_socket[s].index = NULL;
s_socket[s].method = NULL;
return s;
}
/*
* @brief send a block of data asynchronously and receive result by callback function
*/
int esp_aio_sendto(esp_aio_t *aio, const struct sockaddr_ll *to, socklen_t len)
{
int s = aio->fd;
CHECK_METHOD(s, aio_sendto);
return SOCKET_IO_METHOD(s, aio_sendto, aio, to, len);
}
/*
* @brief register a event and its callback function to target of file description
*/
int esp_aio_event(int fd, unsigned int event, esp_aio_cb_t cb, void *arg)
{
int e;
int ret;
int s = fd;
CHECK_METHOD(s, aio_event);
e = alloc_event(s, event);
if (e < 0) {
SET_ERR(ENOMEM)
return -1;
}
ret = SOCKET_IO_METHOD(s, aio_event, event, cb, arg);
if (ret) {
free_event(s, e);
return -1;
}
s_socket[s].event[e].cb = cb;
s_socket[s].event[e].arg = arg;
return 0;
}
/*
* @brief lowlevel socket module upload event and its data
*/
int esp_upload_event(void *index, esp_socket_info_t *info, unsigned int event, esp_aio_data_t *aio_data)
{
int ret;
int s;
if (event >= ESP_SOCKET_MAX_EVENT)
return -EINVAL;
for (s = 0; s < ESP_SOCKET_MAX; s++) {
if (s_socket[s].index == index && event_is_used(s, event)) {
esp_aio_t aio;
aio.fd = s;
aio.cb = s_socket[s].event[event].cb;
aio.arg = s_socket[s].event[event].arg;
aio.pbuf = aio_data->pbuf;
aio.len = aio_data->len;
aio.ret = aio_data->status;
ret = s_socket[s].event[event].cb(&aio);
if (ret)
return ret;
}
}
return 0;
}
/*
* @brief free buffer taken from event callback
*/
int esp_free_pbuf(int fd, void *pbuf)
{
int ret;
int s = fd;
CHECK_METHOD(s, free_pbuf);
ret = SOCKET_IO_METHOD(s, free_pbuf, pbuf);
if (ret)
return -1;
return 0;
}
/*
* @brief map real lowlevel socket object to virtual socket
*/
static int map_socket_ll(int fd, const char *name)
{
int s = fd;
const esp_socket_method_t *p;
extern const esp_socket_method_t __start_ksymatabesp_socket, __stop_ksymatabesp_socket;
for (p = &__start_ksymatabesp_socket; p != &__stop_ksymatabesp_socket; p++) {
if (!strcmp(name, p->name))
break;
}
if (p >= &__stop_ksymatabesp_socket) {
SET_ERR(ENXIO)
return -1;
}
s_socket[s].index = p->open(s_socket[s].info);
if (!s_socket[s].index)
return -1;
s_socket[s].method = p;
return 0;
}
/*
* @brief send requset command to target by file description and get the result synchronously
*/
int esp_ioctl(int fd, unsigned int cmd, ...)
{
int ret;
va_list va;
int s = fd;
va_start(va, cmd);
switch(cmd) {
#if SIOCGIFINDEX != SIOGIFINDEX
case SIOGIFINDEX:
#endif
case SIOCGIFINDEX: {
const char *name;
name = va_arg(va, const char *);
ret = map_socket_ll(fd, name);
break;
}
default: {
int *arg = ((int *)&cmd) + 1;
CHECK_METHOD(s, ioctl);
ret = SOCKET_IO_METHOD(s, ioctl, cmd, arg);
break;
}
}
va_end(va);
return ret;
}
/*
* @brief close target of file description
*/
int esp_close(int fd)
{
int ret;
int s = fd;
CHECK_METHOD(s, close);
ret = SOCKET_IO_METHOD(s, close);
if (ret)
return -1;
free(s_socket[s].info);
free_socket(s);
return 0;
}

View File

@ -37,6 +37,8 @@
#define SOCKETS_MT
//#define SOCKETS_TCP_TRACE
#define LWIP_SUPPORT_CUSTOM_PBUF 1
/*
-----------------------------------------------
---------- Platform specific locking ----------

View File

@ -40,9 +40,11 @@
extern "C" {
#endif
#ifndef LWIP_SUPPORT_CUSTOM_PBUF
/** Currently, the pbuf_custom code is only needed for one specific configuration
* of IP_FRAG */
#define LWIP_SUPPORT_CUSTOM_PBUF (IP_FRAG && !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF)
#endif
/* @todo: We need a mechanism to prevent wasting memory in every pbuf
(TCP vs. UDP, IPv4 vs. IPv6: UDP/IPv4 packets may waste up to 28 bytes) */

View File

@ -18,7 +18,7 @@
#include "esp_libc.h"
#include "esp_wifi.h"
#include "tcpip_adapter.h"
#include "esp_socket.h"
int8_t ieee80211_output_pbuf(uint8_t fd, uint8_t* dataptr, uint16_t datalen);
int8_t wifi_get_netif(uint8_t fd);
@ -54,6 +54,59 @@ static void low_level_init(struct netif* netif)
/* Do whatever else is needed to initialize interface. */
}
/*
* @brief LWIP low-level AI/O sending callback function, it is to free pbuf
*
* @param aio AI/O control block pointer
*
* @return 0 meaning successs
*/
static int low_level_send_cb(esp_aio_t *aio)
{
struct pbuf *pbuf = aio->arg;
pbuf_free(pbuf);
return 0;
}
/*
* @brief transform custom pbuf to LWIP core pbuf, LWIP may use input custom pbuf
* to send ARP data directly
*
* @param pbuf LWIP pbuf pointer
*
* @return LWIP pbuf pointer which it not "PBUF_FLAG_IS_CUSTOM" attribute
*/
static inline struct pbuf *ethernetif_transform_pbuf(struct pbuf *pbuf)
{
struct pbuf *p;
if (!(pbuf->flags & PBUF_FLAG_IS_CUSTOM)) {
/*
* Add ref to pbuf to avoid it to be freed by upper layer.
*/
pbuf_ref(pbuf);
return pbuf;
}
p = pbuf_alloc(PBUF_RAW, pbuf->len, PBUF_RAM);
if (!p)
return NULL;
memcpy(p->payload, pbuf->payload, pbuf->len);
/*
* The input pbuf(named "pbuf") should not be freed, becasue it will be
* freed by upper layer.
*
* The output pbuf(named "p") should not be freed either, becasue it will
* be freed at callback function "low_level_send_cb".
*/
return p;
}
/**
* This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf
@ -72,6 +125,7 @@ static void low_level_init(struct netif* netif)
static int8_t low_level_output(struct netif* netif, struct pbuf* p)
{
esp_aio_t aio;
int8_t err = ERR_OK;
if (netif == NULL) {
@ -83,20 +137,22 @@ static int8_t low_level_output(struct netif* netif, struct pbuf* p)
pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endif
uint8_t* outputbuf = (uint8_t*)os_malloc(p->len + 36);
if (outputbuf == NULL) {
TCPIP_ATAPTER_LOG("ERROR no memory\n");
return ERR_MEM;
}
p = ethernetif_transform_pbuf(p);
if (!p)
return ERR_OK;
outputbuf += 36;
memcpy(outputbuf, p->payload, p->len);
aio.fd = (int)netif->state;
aio.pbuf = p->payload;
aio.len = p->len;
aio.cb = low_level_send_cb;
aio.arg = p;
aio.ret = 0;
if (netif == esp_netif[TCPIP_ADAPTER_IF_STA]) {
err = ieee80211_output_pbuf(TCPIP_ADAPTER_IF_STA, outputbuf, p->len);
} else {
err = ieee80211_output_pbuf(TCPIP_ADAPTER_IF_AP, outputbuf, p->len);
}
/*
* we use "SOCK_RAW" to create socket, so all input/output datas include full ethernet
* header, meaning we should not pass target low-level address here.
*/
err = esp_aio_sendto(&aio, NULL, 0);
if (err == ERR_MEM) {
err = ERR_OK;

View File

@ -21,6 +21,16 @@
#include "esp_misc.h"
#include "tcpip_adapter.h"
#include "dhcpserver/dhcpserver.h"
#include "net/sockio.h"
#include "esp_socket.h"
struct tcpip_adapter_pbuf {
struct pbuf_custom pbuf;
void *base;
struct netif *netif;
};
/* Avoid warning. No header file has include these function */
err_t ethernetif_init(struct netif* netif);
@ -86,6 +96,90 @@ static void tcpip_adapter_station_dhcp_start()
}
}
/*
* @brief LWIP custom pbuf callback function, it is to free custom pbuf
*
* @param p LWIP pbuf pointer
*
* @return none
*/
static void tcpip_adapter_free_pbuf(struct pbuf *p)
{
struct tcpip_adapter_pbuf *pa = (struct tcpip_adapter_pbuf *)p;
int s = (int)pa->netif->state;
esp_free_pbuf(s, pa->base);
os_free(pa);
}
/*
* @brief TCPIP adapter AI/O recieve callback function, it is to recieve input data
* and pass it to LWIP core
*
* @param aio AI/O control block pointer
*
* @return 0 if success or others if failed
*/
static int tcpip_adapter_recv_cb(struct esp_aio *aio)
{
struct pbuf *pbuf = NULL;
struct tcpip_adapter_pbuf *p;
struct netif *netif = (struct netif *)aio->arg;
extern void ethernetif_input(struct netif *netif, struct pbuf *p);
p = os_malloc(sizeof(struct tcpip_adapter_pbuf));
if (!p)
return -ENOMEM;
p->pbuf.custom_free_function = tcpip_adapter_free_pbuf;
p->base = (void *)aio->pbuf;
p->netif = netif;
// PBUF_RAW means payload = (char *)aio->pbuf + offset(=0)
pbuf = pbuf_alloced_custom(PBUF_RAW, aio->len, PBUF_REF, &p->pbuf, (void *)aio->pbuf, aio->len);
if (!pbuf)
return -ENOMEM;
ethernetif_input(netif, pbuf);
return 0;
}
/*
* @brief create a "esp_socket" and bind it to target net card
*
* @param name net card name pointer
* @param netif LWIP net interface pointer
*
* @return 0 if success or others if failed
*/
static int tcpip_adapter_bind_netcard(const char *name, struct netif *netif)
{
int s, ret;
s = esp_socket(AF_PACKET, SOCK_RAW, ETH_P_ALL);
if (s < 0) {
TCPIP_ATAPTER_LOG("create socket of (AF_PACKET, SOCK_RAW, ETH_P_ALL) error\n");
return -1;
}
ret = esp_ioctl(s, SIOCGIFINDEX, name);
if (ret) {
TCPIP_ATAPTER_LOG("bind socket %d to netcard %s error\n", s, name);
esp_close(s);
return -1;
}
ret = esp_aio_event(s, ESP_SOCKET_RECV_EVENT, tcpip_adapter_recv_cb, netif);
if (ret) {
TCPIP_ATAPTER_LOG("socket %d register receive callback function %p error\n", s, tcpip_adapter_recv_cb);
esp_close(s);
return -1;
}
return s;
}
void tcpip_adapter_start(uint8_t netif_index, bool authed)
{
if (!TCPIP_ADAPTER_IF_VALID(netif_index)) {
@ -98,10 +192,20 @@ void tcpip_adapter_start(uint8_t netif_index, bool authed)
if (netif_index == TCPIP_ADAPTER_IF_STA) {
if (authed == 0) {
if (esp_netif[netif_index] == NULL) {
int s;
const char *netcard_name = "sta0";
esp_netif[netif_index] = (struct netif*)os_zalloc(sizeof(*esp_netif[netif_index]));
TCPIP_ATAPTER_LOG("Malloc netif:%d\n", netif_index);
TCPIP_ATAPTER_LOG("Add netif:%d\n", netif_index);
netif_add(esp_netif[netif_index], NULL, NULL, NULL, NULL, ethernetif_init, tcpip_input);
s = tcpip_adapter_bind_netcard(netcard_name, esp_netif[netif_index]);
if (s < 0) {
TCPIP_ATAPTER_LOG("TCPIP adapter bind net card %s error\n", netcard_name);
return ;
}
netif_add(esp_netif[netif_index], NULL, NULL, NULL, (void *)s, ethernetif_init, tcpip_input);
}
} else {
if ((esp_netif[netif_index]->flags & NETIF_FLAG_DHCP) == 0) {
@ -131,10 +235,20 @@ void tcpip_adapter_start(uint8_t netif_index, bool authed)
}
if (esp_netif[netif_index] == NULL) {
int s;
const char *netcard_name = "ap0";
TCPIP_ATAPTER_LOG("Malloc netif:%d\n", netif_index);
esp_netif[netif_index] = (struct netif*)os_zalloc(sizeof(*esp_netif[netif_index]));
s = tcpip_adapter_bind_netcard(netcard_name, esp_netif[netif_index]);
if (s < 0) {
TCPIP_ATAPTER_LOG("TCPIP adapter bind net card %s error\n", netcard_name);
return ;
}
netif_add(esp_netif[netif_index], &esp_ip[TCPIP_ADAPTER_IF_AP].ip,
&esp_ip[TCPIP_ADAPTER_IF_AP].netmask, &esp_ip[TCPIP_ADAPTER_IF_AP].gw, NULL, ethernetif_init, tcpip_input);
&esp_ip[TCPIP_ADAPTER_IF_AP].netmask, &esp_ip[TCPIP_ADAPTER_IF_AP].gw, (void *)s, ethernetif_init, tcpip_input);
}
if (dhcps_flag) {
@ -178,31 +292,12 @@ void tcpip_adapter_stop(uint8_t netif_index)
}
TCPIP_ATAPTER_LOG("stop netif[%d]\n", netif_index);
esp_close((int)esp_netif[netif_index]->state);
netif_remove(esp_netif[netif_index]);
os_free(esp_netif[netif_index]);
esp_netif[netif_index] = NULL;
}
void ieee80211_input(uint8_t netif_index, uint8_t* input, uint16_t len)
{
struct pbuf* pb;
if (!TCPIP_ADAPTER_IF_VALID(netif_index)) {
TCPIP_ATAPTER_LOG("ERROR bad netif index:%d\n", netif_index);
return;
}
pb = pbuf_alloc(PBUF_MAC, len, PBUF_RAM);
if (pb == NULL) {
TCPIP_ATAPTER_LOG("ERROR NO MEMORY\n");
return;
}
memcpy((uint8_t*)(pb->payload), (uint8_t*)input, len);
ethernet_input(pb, esp_netif[netif_index]);
}
bool wifi_set_ip_info(WIFI_INTERFACE netif_index, struct ip_info* if_ip)
{
if (!TCPIP_ADAPTER_IF_VALID((uint8_t)netif_index)) {