2016-08-17 15:08:22 +00:00
|
|
|
// Copyright 2015-2016 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.
|
2016-09-02 10:18:22 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tcpip_adapter.h"
|
|
|
|
|
|
|
|
#if CONFIG_TCPIP_LWIP
|
|
|
|
|
|
|
|
#include "lwip/inet.h"
|
|
|
|
#include "lwip/tcpip.h"
|
|
|
|
#include "lwip/dhcp.h"
|
|
|
|
#include "lwip/ip_addr.h"
|
2016-11-10 03:24:31 +00:00
|
|
|
#include "lwip/ip6_addr.h"
|
|
|
|
#include "lwip/nd6.h"
|
2017-03-29 03:07:41 +00:00
|
|
|
#include "lwip/priv/tcpip_priv.h"
|
2016-12-13 01:52:29 +00:00
|
|
|
#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
|
|
|
|
#include "lwip/dns.h"
|
|
|
|
#endif
|
2016-08-17 15:08:22 +00:00
|
|
|
#include "netif/wlanif.h"
|
2016-11-08 09:45:17 +00:00
|
|
|
#include "netif/ethernetif.h"
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
#include "apps/dhcpserver.h"
|
|
|
|
|
2016-08-19 04:08:45 +00:00
|
|
|
#include "esp_event.h"
|
2017-03-29 03:07:41 +00:00
|
|
|
#include "esp_log.h"
|
2016-08-19 04:08:45 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
static struct netif *esp_netif[TCPIP_ADAPTER_IF_MAX];
|
2016-09-06 05:02:02 +00:00
|
|
|
static tcpip_adapter_ip_info_t esp_ip[TCPIP_ADAPTER_IF_MAX];
|
2016-11-10 03:24:31 +00:00
|
|
|
static tcpip_adapter_ip6_info_t esp_ip6[TCPIP_ADAPTER_IF_MAX];
|
2017-08-08 04:48:14 +00:00
|
|
|
static netif_init_fn esp_netif_init_fn[TCPIP_ADAPTER_IF_MAX];
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
static tcpip_adapter_dhcp_status_t dhcps_status = TCPIP_ADAPTER_DHCP_INIT;
|
2016-11-08 09:45:17 +00:00
|
|
|
static tcpip_adapter_dhcp_status_t dhcpc_status[TCPIP_ADAPTER_IF_MAX] = {TCPIP_ADAPTER_DHCP_INIT};
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_start_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_stop_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_up_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_down_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_set_ip_info_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_create_ip6_linklocal_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_dhcps_start_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_dhcps_stop_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_dhcpc_start_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_dhcpc_stop_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static esp_err_t tcpip_adapter_set_hostname_api(tcpip_adapter_api_msg_t * msg);
|
|
|
|
static sys_sem_t api_sync_sem = NULL;
|
|
|
|
extern sys_thread_t g_lwip_task;
|
|
|
|
|
|
|
|
#define TAG "tcpip_adapter"
|
|
|
|
|
|
|
|
static void tcpip_adapter_api_cb(void* api_msg)
|
|
|
|
{
|
|
|
|
tcpip_adapter_api_msg_t *msg = (tcpip_adapter_api_msg_t*)api_msg;
|
|
|
|
|
|
|
|
if (!msg || !msg->api_fn) {
|
|
|
|
ESP_LOGD(TAG, "null msg/api_fn");
|
|
|
|
return;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
msg->ret = msg->api_fn(msg);
|
|
|
|
ESP_LOGD(TAG, "call api in lwip: ret=0x%x, give sem", msg->ret);
|
|
|
|
sys_sem_signal(&api_sync_sem);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
void tcpip_adapter_init(void)
|
|
|
|
{
|
|
|
|
static bool tcpip_inited = false;
|
2017-03-29 03:07:41 +00:00
|
|
|
int ret;
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
if (tcpip_inited == false) {
|
|
|
|
tcpip_inited = true;
|
|
|
|
|
|
|
|
tcpip_init(NULL, NULL);
|
|
|
|
|
2017-06-30 08:49:42 +00:00
|
|
|
memset(esp_ip, 0, sizeof(tcpip_adapter_ip_info_t)*TCPIP_ADAPTER_IF_MAX);
|
2016-08-17 15:08:22 +00:00
|
|
|
IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].ip, 192, 168 , 4, 1);
|
|
|
|
IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].gw, 192, 168 , 4, 1);
|
|
|
|
IP4_ADDR(&esp_ip[TCPIP_ADAPTER_IF_AP].netmask, 255, 255 , 255, 0);
|
2017-03-29 03:07:41 +00:00
|
|
|
ret = sys_sem_new(&api_sync_sem, 0);
|
|
|
|
if (ERR_OK != ret) {
|
|
|
|
ESP_LOGD(TAG, "tcpip adatper api sync sem init fail");
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:48:14 +00:00
|
|
|
static inline netif_init_fn tcpip_if_to_netif_init_fn(tcpip_adapter_if_t tcpip_if)
|
2017-02-24 03:46:23 +00:00
|
|
|
{
|
2017-08-08 04:48:14 +00:00
|
|
|
if (tcpip_if < TCPIP_ADAPTER_IF_MAX)
|
|
|
|
return esp_netif_init_fn[tcpip_if];
|
|
|
|
else
|
|
|
|
return NULL;
|
2017-02-24 03:46:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static int tcpip_adapter_ipc_check(tcpip_adapter_api_msg_t *msg)
|
|
|
|
{
|
|
|
|
#if TCPIP_ADAPTER_TRHEAD_SAFE
|
|
|
|
xTaskHandle local_task = xTaskGetCurrentTaskHandle();
|
|
|
|
|
|
|
|
if (local_task == g_lwip_task) {
|
|
|
|
return TCPIP_ADAPTER_IPC_LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpip_send_api_msg((tcpip_callback_fn)tcpip_adapter_api_cb, msg, &api_sync_sem);
|
|
|
|
|
|
|
|
return TCPIP_ADAPTER_IPC_REMOTE;
|
|
|
|
#else
|
|
|
|
return TCPIP_ADAPTER_IPC_LOCAL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
2017-02-24 03:46:23 +00:00
|
|
|
netif_init_fn netif_init;
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, mac, ip_info, 0, tcpip_adapter_start_api);
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL || ip_info == NULL) {
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:49:42 +00:00
|
|
|
if (esp_netif[tcpip_if] == NULL || !netif_is_up(esp_netif[tcpip_if])) {
|
|
|
|
if (esp_netif[tcpip_if] == NULL) {
|
|
|
|
esp_netif[tcpip_if] = calloc(1, sizeof(*esp_netif[tcpip_if]));
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
if (esp_netif[tcpip_if] == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
memcpy(esp_netif[tcpip_if]->hwaddr, mac, NETIF_MAX_HWADDR_LEN);
|
2017-02-24 03:46:23 +00:00
|
|
|
|
|
|
|
netif_init = tcpip_if_to_netif_init_fn(tcpip_if);
|
|
|
|
assert(netif_init != NULL);
|
|
|
|
netif_add(esp_netif[tcpip_if], &ip_info->ip, &ip_info->netmask, &ip_info->gw, NULL, netif_init, tcpip_input);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
|
|
|
|
netif_set_up(esp_netif[tcpip_if]);
|
|
|
|
|
|
|
|
if (dhcps_status == TCPIP_ADAPTER_DHCP_INIT) {
|
2016-09-06 05:02:02 +00:00
|
|
|
dhcps_start(esp_netif[tcpip_if], ip_info->ip);
|
2016-08-31 09:02:36 +00:00
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server start:(ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR ")",
|
2016-11-08 09:45:17 +00:00
|
|
|
IP2STR(&ip_info->ip), IP2STR(&ip_info->netmask), IP2STR(&ip_info->gw));
|
2016-08-31 09:02:36 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
dhcps_status = TCPIP_ADAPTER_DHCP_STARTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if ap is on, choose ap as default if */
|
2017-06-30 08:49:42 +00:00
|
|
|
if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_AP])) {
|
2016-08-17 15:08:22 +00:00
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_AP]);
|
2017-06-30 08:49:42 +00:00
|
|
|
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) {
|
2016-08-17 15:08:22 +00:00
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]);
|
2017-06-30 08:49:42 +00:00
|
|
|
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) {
|
2016-11-08 09:45:17 +00:00
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:48:14 +00:00
|
|
|
esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
|
|
|
|
{
|
|
|
|
esp_netif_init_fn[TCPIP_ADAPTER_IF_ETH] = ethernetif_init;
|
|
|
|
return tcpip_adapter_start(TCPIP_ADAPTER_IF_ETH, mac, ip_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
|
|
|
|
{
|
|
|
|
esp_netif_init_fn[TCPIP_ADAPTER_IF_STA] = wlanif_init_sta;
|
|
|
|
return tcpip_adapter_start(TCPIP_ADAPTER_IF_STA, mac, ip_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
|
|
|
|
{
|
|
|
|
esp_netif_init_fn[TCPIP_ADAPTER_IF_AP] = wlanif_init_ap;
|
|
|
|
return tcpip_adapter_start(TCPIP_ADAPTER_IF_AP, mac, ip_info);
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_start_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_start(msg->tcpip_if, msg->mac, msg->ip_info);
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if)
|
|
|
|
{
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_stop_api);
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (esp_netif[tcpip_if] == NULL) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:49:42 +00:00
|
|
|
if (!netif_is_up(esp_netif[tcpip_if])) {
|
|
|
|
netif_remove(esp_netif[tcpip_if]);
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
|
|
|
|
dhcps_stop(esp_netif[tcpip_if]); // TODO: dhcps checks status by its self
|
2016-11-08 09:45:17 +00:00
|
|
|
if (TCPIP_ADAPTER_DHCP_STOPPED != dhcps_status) {
|
2016-10-25 01:26:10 +00:00
|
|
|
dhcps_status = TCPIP_ADAPTER_DHCP_INIT;
|
|
|
|
}
|
2016-11-08 09:45:17 +00:00
|
|
|
} else if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) {
|
2016-09-05 12:24:47 +00:00
|
|
|
dhcp_release(esp_netif[tcpip_if]);
|
2016-08-17 15:08:22 +00:00
|
|
|
dhcp_stop(esp_netif[tcpip_if]);
|
2016-09-05 12:24:47 +00:00
|
|
|
dhcp_cleanup(esp_netif[tcpip_if]);
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT;
|
2016-09-05 12:24:47 +00:00
|
|
|
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].ip);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].gw);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].netmask);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 08:49:42 +00:00
|
|
|
netif_set_down(esp_netif[tcpip_if]);
|
2016-08-17 15:08:22 +00:00
|
|
|
netif_remove(esp_netif[tcpip_if]);
|
|
|
|
|
|
|
|
/* in ap + sta mode, if stop ap, choose sta as default if */
|
2017-06-30 08:49:42 +00:00
|
|
|
if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
|
|
|
|
if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) {
|
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]);
|
|
|
|
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) {
|
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]);
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_stop_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
msg->ret = tcpip_adapter_stop(msg->tcpip_if);
|
|
|
|
return msg->ret;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if)
|
|
|
|
{
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_up_api);
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH ) {
|
|
|
|
if (esp_netif[tcpip_if] == NULL) {
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
|
|
|
|
2016-08-31 08:34:48 +00:00
|
|
|
/* use last obtained ip, or static ip */
|
|
|
|
netif_set_addr(esp_netif[tcpip_if], &esp_ip[tcpip_if].ip, &esp_ip[tcpip_if].netmask, &esp_ip[tcpip_if].gw);
|
2016-08-17 15:08:22 +00:00
|
|
|
netif_set_up(esp_netif[tcpip_if]);
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:49:42 +00:00
|
|
|
if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_AP])) {
|
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_AP]);
|
|
|
|
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_STA])) {
|
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_STA]);
|
|
|
|
} else if (netif_is_up(esp_netif[TCPIP_ADAPTER_IF_ETH])) {
|
|
|
|
netif_set_default(esp_netif[TCPIP_ADAPTER_IF_ETH]);
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_up_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
msg->ret = tcpip_adapter_up(msg->tcpip_if);
|
|
|
|
return msg->ret;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if)
|
|
|
|
{
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_down_api);
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH ) {
|
2016-08-26 04:03:52 +00:00
|
|
|
if (esp_netif[tcpip_if] == NULL) {
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if (dhcpc_status[tcpip_if] == TCPIP_ADAPTER_DHCP_STARTED) {
|
2016-08-17 15:08:22 +00:00
|
|
|
dhcp_stop(esp_netif[tcpip_if]);
|
2016-08-26 03:52:46 +00:00
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT;
|
2016-09-02 12:27:22 +00:00
|
|
|
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].ip);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].gw);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].netmask);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
2016-09-05 12:24:47 +00:00
|
|
|
|
2016-09-22 03:49:37 +00:00
|
|
|
/* Modify ip address to trigger tcp/udp pcb cleanup */
|
|
|
|
netif_set_addr(esp_netif[tcpip_if], IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY);
|
|
|
|
|
2016-08-31 08:34:48 +00:00
|
|
|
netif_set_down(esp_netif[tcpip_if]);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_down_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_down(msg->tcpip_if);
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info)
|
2016-08-31 09:02:36 +00:00
|
|
|
{
|
|
|
|
struct netif *p_netif;
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL) {
|
2016-08-31 09:02:36 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
|
|
|
|
|
|
|
if (p_netif != NULL && netif_is_up(p_netif)) {
|
2016-09-06 05:02:02 +00:00
|
|
|
ip4_addr_set(&ip_info->ip, ip_2_ip4(&p_netif->ip_addr));
|
|
|
|
ip4_addr_set(&ip_info->netmask, ip_2_ip4(&p_netif->netmask));
|
|
|
|
ip4_addr_set(&ip_info->gw, ip_2_ip4(&p_netif->gw));
|
2016-08-31 09:02:36 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
ip4_addr_copy(ip_info->ip, esp_ip[tcpip_if].ip);
|
|
|
|
ip4_addr_copy(ip_info->gw, esp_ip[tcpip_if].gw);
|
|
|
|
ip4_addr_copy(ip_info->netmask, esp_ip[tcpip_if].netmask);
|
2016-08-31 09:02:36 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
struct netif *p_netif;
|
2016-08-30 15:58:55 +00:00
|
|
|
tcpip_adapter_dhcp_status_t status;
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, ip_info, 0, tcpip_adapter_set_ip_info_api);
|
|
|
|
|
2016-09-05 12:24:47 +00:00
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || ip_info == NULL ||
|
2016-12-14 19:30:40 +00:00
|
|
|
ip4_addr_isany_val(ip_info->ip) || ip4_addr_isany_val(ip_info->netmask)) {
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
2016-08-30 15:58:55 +00:00
|
|
|
if (tcpip_if == TCPIP_ADAPTER_IF_AP) {
|
|
|
|
tcpip_adapter_dhcps_get_status(tcpip_if, &status);
|
|
|
|
|
2016-09-01 07:11:23 +00:00
|
|
|
if (status != TCPIP_ADAPTER_DHCP_STOPPED) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED;
|
2016-08-30 15:58:55 +00:00
|
|
|
}
|
2016-11-08 09:45:17 +00:00
|
|
|
} else if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH ) {
|
2016-08-30 15:58:55 +00:00
|
|
|
tcpip_adapter_dhcpc_get_status(tcpip_if, &status);
|
|
|
|
|
2016-09-01 07:11:23 +00:00
|
|
|
if (status != TCPIP_ADAPTER_DHCP_STOPPED) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED;
|
2016-08-30 15:58:55 +00:00
|
|
|
}
|
2016-12-13 01:52:29 +00:00
|
|
|
#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
|
|
|
|
u8_t numdns = 0;
|
|
|
|
for (numdns = 0; numdns < DNS_MAX_SERVERS; numdns ++) {
|
|
|
|
dns_setserver(numdns, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-30 15:58:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
ip4_addr_copy(esp_ip[tcpip_if].ip, ip_info->ip);
|
|
|
|
ip4_addr_copy(esp_ip[tcpip_if].gw, ip_info->gw);
|
|
|
|
ip4_addr_copy(esp_ip[tcpip_if].netmask, ip_info->netmask);
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
|
|
|
|
|
|
|
if (p_netif != NULL && netif_is_up(p_netif)) {
|
2016-09-06 05:02:02 +00:00
|
|
|
netif_set_addr(p_netif, &ip_info->ip, &ip_info->netmask, &ip_info->gw);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_set_ip_info_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_set_ip_info(msg->tcpip_if, msg->ip_info);
|
|
|
|
}
|
|
|
|
|
2016-11-10 03:24:31 +00:00
|
|
|
static void tcpip_adapter_nd6_cb(struct netif *p_netif, uint8_t ip_idex)
|
|
|
|
{
|
|
|
|
tcpip_adapter_ip6_info_t *ip6_info;
|
|
|
|
|
|
|
|
if (!p_netif) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "null p_netif=%p", p_netif);
|
2016-11-10 03:24:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_netif == esp_netif[TCPIP_ADAPTER_IF_STA]) {
|
|
|
|
ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_STA];
|
2016-11-08 09:45:17 +00:00
|
|
|
} else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_AP]) {
|
|
|
|
ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_AP];
|
2016-11-10 03:24:31 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
system_event_t evt;
|
|
|
|
|
|
|
|
ip6_addr_set(&ip6_info->ip, ip_2_ip6(&p_netif->ip6_addr[ip_idex]));
|
|
|
|
|
|
|
|
//notify event
|
|
|
|
evt.event_id = SYSTEM_EVENT_AP_STA_GOT_IP6;
|
|
|
|
memcpy(&evt.event_info.got_ip6.ip6_info, ip6_info, sizeof(tcpip_adapter_ip6_info_t));
|
|
|
|
esp_event_send(&evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if)
|
|
|
|
{
|
|
|
|
struct netif *p_netif;
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_create_ip6_linklocal_api);
|
|
|
|
|
2016-11-10 03:24:31 +00:00
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
2016-11-08 09:45:17 +00:00
|
|
|
if (p_netif != NULL && netif_is_up(p_netif)) {
|
2016-11-10 03:24:31 +00:00
|
|
|
netif_create_ip6_linklocal_address(p_netif, 1);
|
|
|
|
nd6_set_cb(p_netif, tcpip_adapter_nd6_cb);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
} else {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_create_ip6_linklocal_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_create_ip6_linklocal(msg->tcpip_if);
|
|
|
|
}
|
|
|
|
|
2016-11-10 03:24:31 +00:00
|
|
|
esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6)
|
|
|
|
{
|
|
|
|
struct netif *p_netif;
|
|
|
|
|
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || if_ip6 == NULL) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
|
|
|
if (p_netif != NULL && netif_is_up(p_netif) && ip6_addr_ispreferred(netif_ip6_addr_state(p_netif, 0))) {
|
|
|
|
memcpy(if_ip6, &p_netif->ip6_addr[0], sizeof(ip6_addr_t));
|
|
|
|
} else {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
#if 0
|
|
|
|
esp_err_t tcpip_adapter_get_mac(tcpip_adapter_if_t tcpip_if, uint8_t mac[6])
|
|
|
|
{
|
|
|
|
struct netif *p_netif;
|
|
|
|
|
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
|
|
|
|
|
|
|
if (p_netif != NULL) {
|
|
|
|
memcpy(mac, p_netif->hwaddr, NETIF_MAX_HWADDR_LEN);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t tcpip_adapter_set_mac(tcpip_adapter_if_t tcpip_if, uint8_t mac[6])
|
|
|
|
{
|
|
|
|
struct netif *p_netif;
|
|
|
|
|
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || mac == NULL) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
|
|
|
|
|
|
|
if (p_netif != NULL) {
|
|
|
|
memcpy(p_netif->hwaddr, mac, NETIF_MAX_HWADDR_LEN);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode_t opt_op, tcpip_adapter_option_id_t opt_id, void *opt_val, uint32_t opt_len)
|
2016-08-26 06:15:14 +00:00
|
|
|
{
|
2016-08-26 10:16:29 +00:00
|
|
|
void *opt_info = dhcps_option_info(opt_id, opt_len);
|
|
|
|
|
|
|
|
if (opt_info == NULL || opt_val == NULL) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt_op == TCPIP_ADAPTER_OP_GET) {
|
2016-09-01 07:11:23 +00:00
|
|
|
if (dhcps_status == TCPIP_ADAPTER_DHCP_STOPPED) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED;
|
2016-08-26 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (opt_id) {
|
2016-11-08 09:45:17 +00:00
|
|
|
case IP_ADDRESS_LEASE_TIME: {
|
|
|
|
*(uint32_t *)opt_val = *(uint32_t *)opt_info;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REQUESTED_IP_ADDRESS: {
|
|
|
|
memcpy(opt_val, opt_info, opt_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ROUTER_SOLICITATION_ADDRESS: {
|
|
|
|
*(uint8_t *)opt_val = (*(uint8_t *)opt_info) & OFFER_ROUTER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-26 10:16:29 +00:00
|
|
|
}
|
|
|
|
} else if (opt_op == TCPIP_ADAPTER_OP_SET) {
|
|
|
|
if (dhcps_status == TCPIP_ADAPTER_DHCP_STARTED) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opt_id) {
|
2016-11-08 09:45:17 +00:00
|
|
|
case IP_ADDRESS_LEASE_TIME: {
|
|
|
|
if (*(uint32_t *)opt_val != 0) {
|
|
|
|
*(uint32_t *)opt_info = *(uint32_t *)opt_val;
|
|
|
|
} else {
|
|
|
|
*(uint32_t *)opt_info = DHCPS_LEASE_TIME_DEF;
|
2016-08-26 10:16:29 +00:00
|
|
|
}
|
2016-11-08 09:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REQUESTED_IP_ADDRESS: {
|
|
|
|
tcpip_adapter_ip_info_t info;
|
|
|
|
uint32_t softap_ip = 0;
|
|
|
|
uint32_t start_ip = 0;
|
|
|
|
uint32_t end_ip = 0;
|
|
|
|
dhcps_lease_t *poll = opt_val;
|
|
|
|
|
|
|
|
if (poll->enable) {
|
|
|
|
memset(&info, 0x00, sizeof(tcpip_adapter_ip_info_t));
|
|
|
|
tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &info);
|
|
|
|
softap_ip = htonl(info.ip.addr);
|
|
|
|
start_ip = htonl(poll->start_ip.addr);
|
|
|
|
end_ip = htonl(poll->end_ip.addr);
|
|
|
|
|
|
|
|
/*config ip information can't contain local ip*/
|
|
|
|
if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*config ip information must be in the same segment as the local ip*/
|
|
|
|
softap_ip >>= 8;
|
|
|
|
if ((start_ip >> 8 != softap_ip)
|
2016-11-30 07:24:49 +00:00
|
|
|
|| (end_ip >> 8 != softap_ip)) {
|
2016-11-08 09:45:17 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
2016-11-30 07:24:49 +00:00
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if (end_ip - start_ip > DHCPS_MAX_LEASE) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
2016-08-26 10:16:29 +00:00
|
|
|
}
|
2016-11-08 09:45:17 +00:00
|
|
|
|
|
|
|
memcpy(opt_info, opt_val, opt_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ROUTER_SOLICITATION_ADDRESS: {
|
|
|
|
*(uint8_t *)opt_info = (*(uint8_t *)opt_val) & OFFER_ROUTER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-26 10:16:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
2016-08-26 06:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 09:02:36 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status)
|
|
|
|
{
|
|
|
|
*status = dhcps_status;
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if)
|
|
|
|
{
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcps_start_api);
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
/* only support ap now */
|
|
|
|
if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server invalid if=%d", tcpip_if);
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dhcps_status != TCPIP_ADAPTER_DHCP_STARTED) {
|
|
|
|
struct netif *p_netif = esp_netif[tcpip_if];
|
|
|
|
|
|
|
|
if (p_netif != NULL && netif_is_up(p_netif)) {
|
2016-09-06 05:02:02 +00:00
|
|
|
tcpip_adapter_ip_info_t default_ip;
|
2016-11-08 09:45:17 +00:00
|
|
|
tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &default_ip);
|
2016-09-06 05:02:02 +00:00
|
|
|
dhcps_start(p_netif, default_ip.ip);
|
2016-08-17 15:08:22 +00:00
|
|
|
dhcps_status = TCPIP_ADAPTER_DHCP_STARTED;
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server start successfully");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
} else {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server re init");
|
2016-08-17 15:08:22 +00:00
|
|
|
dhcps_status = TCPIP_ADAPTER_DHCP_INIT;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server already start");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_dhcps_start_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_dhcps_start(msg->tcpip_if);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if)
|
|
|
|
{
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcps_stop_api);
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
/* only support ap now */
|
|
|
|
if (tcpip_if != TCPIP_ADAPTER_IF_AP || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server invalid if=%d", tcpip_if);
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dhcps_status == TCPIP_ADAPTER_DHCP_STARTED) {
|
|
|
|
struct netif *p_netif = esp_netif[tcpip_if];
|
|
|
|
|
|
|
|
if (p_netif != NULL) {
|
|
|
|
dhcps_stop(p_netif);
|
|
|
|
} else {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server if not ready");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
2016-09-01 07:11:23 +00:00
|
|
|
} else if (dhcps_status == TCPIP_ADAPTER_DHCP_STOPPED) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server already stoped");
|
2016-09-01 07:11:23 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp server stop successfully");
|
2016-09-01 07:11:23 +00:00
|
|
|
dhcps_status = TCPIP_ADAPTER_DHCP_STOPPED;
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_dhcps_stop_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_dhcps_stop(msg->tcpip_if);
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode_t opt_op, tcpip_adapter_option_id_t opt_id, void *opt_val, uint32_t opt_len)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
2016-08-31 09:02:36 +00:00
|
|
|
// TODO: when dhcp request timeout,change the retry count
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
static void tcpip_adapter_dhcpc_cb(struct netif *netif)
|
2016-09-02 12:27:22 +00:00
|
|
|
{
|
|
|
|
if (!netif) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "null netif=%p", netif);
|
2016-09-02 12:27:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if (netif != esp_netif[TCPIP_ADAPTER_IF_STA] && netif != esp_netif[TCPIP_ADAPTER_IF_ETH]) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "err netif=%p", netif);
|
2016-11-08 09:45:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:27:22 +00:00
|
|
|
if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY) ) {
|
2017-01-10 13:54:53 +00:00
|
|
|
tcpip_adapter_ip_info_t *ip_info = NULL;
|
|
|
|
if( netif == esp_netif[TCPIP_ADAPTER_IF_STA] ) {
|
|
|
|
ip_info = &esp_ip[TCPIP_ADAPTER_IF_STA];
|
|
|
|
} else if(netif == esp_netif[TCPIP_ADAPTER_IF_ETH] ) {
|
|
|
|
ip_info = &esp_ip[TCPIP_ADAPTER_IF_ETH];
|
|
|
|
}
|
2016-09-05 12:24:47 +00:00
|
|
|
|
2016-09-02 12:27:22 +00:00
|
|
|
//check whether IP is changed
|
|
|
|
if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), &ip_info->ip) ||
|
2016-11-08 09:45:17 +00:00
|
|
|
!ip4_addr_cmp(ip_2_ip4(&netif->netmask), &ip_info->netmask) ||
|
|
|
|
!ip4_addr_cmp(ip_2_ip4(&netif->gw), &ip_info->gw) ) {
|
2016-09-05 12:24:47 +00:00
|
|
|
system_event_t evt;
|
2016-09-02 12:27:22 +00:00
|
|
|
|
|
|
|
ip4_addr_set(&ip_info->ip, ip_2_ip4(&netif->ip_addr));
|
|
|
|
ip4_addr_set(&ip_info->netmask, ip_2_ip4(&netif->netmask));
|
|
|
|
ip4_addr_set(&ip_info->gw, ip_2_ip4(&netif->gw));
|
|
|
|
|
|
|
|
//notify event
|
2016-11-08 09:45:17 +00:00
|
|
|
if (netif == esp_netif[TCPIP_ADAPTER_IF_STA]) {
|
|
|
|
evt.event_id = SYSTEM_EVENT_STA_GOT_IP;
|
|
|
|
} else if (netif == esp_netif[TCPIP_ADAPTER_IF_ETH]) {
|
|
|
|
evt.event_id = SYSTEM_EVENT_ETH_GOT_IP;
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
memcpy(&evt.event_info.got_ip.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t));
|
2016-09-02 12:27:22 +00:00
|
|
|
|
|
|
|
esp_event_send(&evt);
|
|
|
|
} else {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "ip unchanged");
|
2016-09-02 12:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-31 09:02:36 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status)
|
2016-08-26 06:15:14 +00:00
|
|
|
{
|
2016-11-08 09:45:17 +00:00
|
|
|
*status = dhcpc_status[tcpip_if];
|
2016-08-31 09:02:36 +00:00
|
|
|
|
2016-08-26 10:16:29 +00:00
|
|
|
return ESP_OK;
|
2016-08-26 06:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if)
|
|
|
|
{
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcpc_start_api);
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
/* only support sta now, need to support ethernet */
|
2016-11-08 09:45:17 +00:00
|
|
|
if ((tcpip_if != TCPIP_ADAPTER_IF_STA && tcpip_if != TCPIP_ADAPTER_IF_ETH) || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client invalid if=%d", tcpip_if);
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if (dhcpc_status[tcpip_if] != TCPIP_ADAPTER_DHCP_STARTED) {
|
2016-08-17 15:08:22 +00:00
|
|
|
struct netif *p_netif = esp_netif[tcpip_if];
|
|
|
|
|
2016-09-05 12:24:47 +00:00
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].ip);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].gw);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].netmask);
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
if (p_netif != NULL) {
|
|
|
|
if (netif_is_up(p_netif)) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client init ip/mask/gw to all-0");
|
2016-08-30 15:58:55 +00:00
|
|
|
ip_addr_set_zero(&p_netif->ip_addr);
|
2016-08-17 15:08:22 +00:00
|
|
|
ip_addr_set_zero(&p_netif->netmask);
|
|
|
|
ip_addr_set_zero(&p_netif->gw);
|
2016-08-31 08:34:48 +00:00
|
|
|
} else {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client re init");
|
2016-11-08 09:45:17 +00:00
|
|
|
dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT;
|
2016-08-31 08:34:48 +00:00
|
|
|
return ESP_OK;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dhcp_start(p_netif) != ERR_OK) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client start failed");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED;
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:27:22 +00:00
|
|
|
dhcp_set_cb(p_netif, tcpip_adapter_dhcpc_cb);
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client start successfully");
|
2016-11-08 09:45:17 +00:00
|
|
|
dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_STARTED;
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
} else {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client re init");
|
2016-11-08 09:45:17 +00:00
|
|
|
dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_INIT;
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client already started");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_dhcpc_start_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_dhcpc_start(msg->tcpip_if);
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:47:33 +00:00
|
|
|
esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, 0, tcpip_adapter_dhcpc_stop_api);
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
/* only support sta now, need to support ethernet */
|
|
|
|
if (tcpip_if != TCPIP_ADAPTER_IF_STA || tcpip_if >= TCPIP_ADAPTER_IF_MAX) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client invalid if=%d", tcpip_if);
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if (dhcpc_status[tcpip_if] == TCPIP_ADAPTER_DHCP_STARTED) {
|
2016-08-17 15:08:22 +00:00
|
|
|
struct netif *p_netif = esp_netif[tcpip_if];
|
|
|
|
|
|
|
|
if (p_netif != NULL) {
|
|
|
|
dhcp_stop(p_netif);
|
2016-09-05 12:24:47 +00:00
|
|
|
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].ip);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].gw);
|
|
|
|
ip4_addr_set_zero(&esp_ip[tcpip_if].netmask);
|
2016-08-17 15:08:22 +00:00
|
|
|
} else {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client if not ready");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
}
|
2016-11-08 09:45:17 +00:00
|
|
|
} else if (dhcpc_status[tcpip_if] == TCPIP_ADAPTER_DHCP_STOPPED) {
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client already stoped");
|
2016-09-01 07:11:23 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
ESP_LOGD(TAG, "dhcp client stop successfully");
|
2016-11-08 09:45:17 +00:00
|
|
|
dhcpc_status[tcpip_if] = TCPIP_ADAPTER_DHCP_STOPPED;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_dhcpc_stop_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_dhcpc_stop(msg->tcpip_if);
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb)
|
|
|
|
{
|
|
|
|
ethernetif_input(esp_netif[TCPIP_ADAPTER_IF_ETH], buffer, len);
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
wlanif_input(esp_netif[TCPIP_ADAPTER_IF_STA], buffer, len, eb);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
wlanif_input(esp_netif[TCPIP_ADAPTER_IF_AP], buffer, len, eb);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
bool tcpip_dep_output(wifi_interface_t wifi_if, void *buffer, uint16_t len)
|
|
|
|
{
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
esp_interface_t tcpip_adapter_get_esp_if(void *dev)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
struct netif *p_netif = (struct netif *)dev;
|
|
|
|
|
|
|
|
if (p_netif == esp_netif[TCPIP_ADAPTER_IF_STA]) {
|
2016-11-08 09:45:17 +00:00
|
|
|
return ESP_IF_WIFI_STA;
|
2016-08-17 15:08:22 +00:00
|
|
|
} else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_AP]) {
|
2016-11-08 09:45:17 +00:00
|
|
|
return ESP_IF_WIFI_AP;
|
|
|
|
} else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_ETH]) {
|
|
|
|
return ESP_IF_ETH;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
return ESP_IF_MAX;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
2016-08-23 03:22:58 +00:00
|
|
|
|
2016-10-26 10:16:40 +00:00
|
|
|
esp_err_t tcpip_adapter_get_sta_list(wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list)
|
2016-08-23 03:22:58 +00:00
|
|
|
{
|
2016-10-26 10:16:40 +00:00
|
|
|
int i;
|
2016-08-23 03:22:58 +00:00
|
|
|
|
2016-11-08 09:45:17 +00:00
|
|
|
if ((wifi_sta_list == NULL) || (tcpip_sta_list == NULL)) {
|
2016-08-23 03:22:58 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
2016-11-08 09:45:17 +00:00
|
|
|
}
|
2016-08-23 03:22:58 +00:00
|
|
|
|
2016-10-26 10:16:40 +00:00
|
|
|
memset(tcpip_sta_list, 0, sizeof(tcpip_adapter_sta_list_t));
|
2016-10-28 08:53:49 +00:00
|
|
|
tcpip_sta_list->num = wifi_sta_list->num;
|
2016-11-08 09:45:17 +00:00
|
|
|
for (i = 0; i < wifi_sta_list->num; i++) {
|
2016-10-26 10:16:40 +00:00
|
|
|
memcpy(tcpip_sta_list->sta[i].mac, wifi_sta_list->sta[i].mac, 6);
|
|
|
|
dhcp_search_ip_on_mac(tcpip_sta_list->sta[i].mac, &tcpip_sta_list->sta[i].ip);
|
2016-08-23 03:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-03 05:30:32 +00:00
|
|
|
esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname)
|
|
|
|
{
|
2016-12-07 07:37:59 +00:00
|
|
|
#if LWIP_NETIF_HOSTNAME
|
2017-03-29 03:07:41 +00:00
|
|
|
TCPIP_ADAPTER_IPC_CALL(tcpip_if, 0, 0, hostname, tcpip_adapter_set_hostname_api);
|
2016-11-03 05:30:32 +00:00
|
|
|
struct netif *p_netif;
|
2017-02-17 10:02:00 +00:00
|
|
|
static char hostinfo[TCPIP_ADAPTER_IF_MAX][TCPIP_HOSTNAME_MAX_SIZE + 1];
|
2016-11-03 05:30:32 +00:00
|
|
|
|
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
2016-12-07 07:37:59 +00:00
|
|
|
if (strlen(hostname) > TCPIP_HOSTNAME_MAX_SIZE) {
|
2016-11-03 05:30:32 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
|
|
|
if (p_netif != NULL) {
|
2016-12-07 07:37:59 +00:00
|
|
|
memset(hostinfo[tcpip_if], 0, sizeof(hostinfo[tcpip_if]));
|
2017-03-02 03:50:58 +00:00
|
|
|
strlcpy(hostinfo[tcpip_if], hostname, sizeof(hostinfo[tcpip_if]));
|
2016-12-07 07:37:59 +00:00
|
|
|
p_netif->hostname = hostinfo[tcpip_if];
|
|
|
|
return ESP_OK;
|
2016-11-03 05:30:32 +00:00
|
|
|
} else {
|
2016-12-07 07:37:59 +00:00
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
2016-11-03 05:30:32 +00:00
|
|
|
}
|
2016-12-07 07:37:59 +00:00
|
|
|
#else
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
#endif
|
2016-11-03 05:30:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 03:07:41 +00:00
|
|
|
static esp_err_t tcpip_adapter_set_hostname_api(tcpip_adapter_api_msg_t * msg)
|
|
|
|
{
|
|
|
|
return tcpip_adapter_set_hostname(msg->tcpip_if, msg->hostname);
|
|
|
|
}
|
|
|
|
|
2016-11-30 08:44:31 +00:00
|
|
|
esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname)
|
|
|
|
{
|
2016-12-07 07:37:59 +00:00
|
|
|
#if LWIP_NETIF_HOSTNAME
|
2016-11-30 08:44:31 +00:00
|
|
|
struct netif *p_netif = NULL;
|
|
|
|
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_netif = esp_netif[tcpip_if];
|
|
|
|
if (p_netif != NULL) {
|
|
|
|
*hostname = p_netif->hostname;
|
|
|
|
return ESP_OK;
|
|
|
|
} else {
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
|
|
|
}
|
2016-12-07 07:37:59 +00:00
|
|
|
#else
|
|
|
|
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
|
|
|
|
#endif
|
2016-11-30 08:44:31 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 03:46:23 +00:00
|
|
|
#endif /* CONFIG_TCPIP_LWIP */
|