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-13 07:57:11 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-05 03:05:07 +00:00
|
|
|
#include <string.h>
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_event.h"
|
2016-09-25 17:17:32 +00:00
|
|
|
#include "esp_event_loop.h"
|
2016-09-01 06:09:21 +00:00
|
|
|
#include "esp_task.h"
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
|
|
|
|
#include "tcpip_adapter.h"
|
2016-09-23 06:46:39 +00:00
|
|
|
#include "esp_log.h"
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2016-09-25 17:17:32 +00:00
|
|
|
const char* TAG = "event";
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
#define WIFI_API_CALL_CHECK(info, api_call, ret) \
|
|
|
|
do{\
|
|
|
|
esp_err_t __err = (api_call);\
|
|
|
|
if ((ret) != __err) {\
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGE(TAG, "%s %d %s ret=%d", __FUNCTION__, __LINE__, (info), __err);\
|
2016-08-17 15:08:22 +00:00
|
|
|
return __err;\
|
|
|
|
}\
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
typedef esp_err_t (*system_event_handle_fn_t)(system_event_t *e);
|
2016-09-13 07:57:11 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
typedef struct {
|
|
|
|
system_event_id_t event_id;
|
|
|
|
system_event_handle_fn_t event_handle;
|
|
|
|
} system_event_handle_t;
|
|
|
|
|
|
|
|
|
|
|
|
static esp_err_t system_event_ap_start_handle_default(system_event_t *event);
|
|
|
|
static esp_err_t system_event_ap_stop_handle_default(system_event_t *event);
|
|
|
|
static esp_err_t system_event_sta_start_handle_default(system_event_t *event);
|
|
|
|
static esp_err_t system_event_sta_stop_handle_default(system_event_t *event);
|
|
|
|
static esp_err_t system_event_sta_connected_handle_default(system_event_t *event);
|
|
|
|
static esp_err_t system_event_sta_disconnected_handle_default(system_event_t *event);
|
2016-09-13 07:57:11 +00:00
|
|
|
static esp_err_t system_event_sta_got_ip_default(system_event_t *event);
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
static system_event_handle_t g_system_event_handle_table[] = {
|
|
|
|
{SYSTEM_EVENT_WIFI_READY, NULL},
|
|
|
|
{SYSTEM_EVENT_SCAN_DONE, NULL},
|
|
|
|
{SYSTEM_EVENT_STA_START, system_event_sta_start_handle_default},
|
|
|
|
{SYSTEM_EVENT_STA_STOP, system_event_sta_stop_handle_default},
|
|
|
|
{SYSTEM_EVENT_STA_CONNECTED, system_event_sta_connected_handle_default},
|
|
|
|
{SYSTEM_EVENT_STA_DISCONNECTED, system_event_sta_disconnected_handle_default},
|
|
|
|
{SYSTEM_EVENT_STA_AUTHMODE_CHANGE, NULL},
|
2016-09-23 06:46:39 +00:00
|
|
|
{SYSTEM_EVENT_STA_GOT_IP, system_event_sta_got_ip_default},
|
2016-08-17 15:08:22 +00:00
|
|
|
{SYSTEM_EVENT_AP_START, system_event_ap_start_handle_default},
|
|
|
|
{SYSTEM_EVENT_AP_STOP, system_event_ap_stop_handle_default},
|
|
|
|
{SYSTEM_EVENT_AP_STACONNECTED, NULL},
|
|
|
|
{SYSTEM_EVENT_AP_STADISCONNECTED, NULL},
|
|
|
|
{SYSTEM_EVENT_AP_PROBEREQRECVED, NULL},
|
|
|
|
{SYSTEM_EVENT_MAX, NULL},
|
|
|
|
};
|
|
|
|
|
2016-09-13 07:57:11 +00:00
|
|
|
static esp_err_t system_event_sta_got_ip_default(system_event_t *event)
|
2016-08-30 06:04:12 +00:00
|
|
|
{
|
|
|
|
extern esp_err_t esp_wifi_set_sta_ip(void);
|
|
|
|
WIFI_API_CALL_CHECK("esp_wifi_set_sta_ip", esp_wifi_set_sta_ip(), ESP_OK);
|
2016-09-05 03:05:07 +00:00
|
|
|
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGI(TAG, "ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR,
|
2016-09-13 07:57:11 +00:00
|
|
|
IP2STR(&event->event_info.got_ip.ip_info.ip),
|
|
|
|
IP2STR(&event->event_info.got_ip.ip_info.netmask),
|
|
|
|
IP2STR(&event->event_info.got_ip.ip_info.gw));
|
2016-09-05 03:05:07 +00:00
|
|
|
|
2016-08-30 06:04:12 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t system_event_ap_start_handle_default(system_event_t *event)
|
|
|
|
{
|
2016-09-06 05:02:02 +00:00
|
|
|
tcpip_adapter_ip_info_t ap_ip;
|
2016-08-17 15:08:22 +00:00
|
|
|
uint8_t ap_mac[6];
|
|
|
|
|
|
|
|
WIFI_API_CALL_CHECK("esp_wifi_reg_rxcb", esp_wifi_reg_rxcb(WIFI_IF_AP, (wifi_rxcb_t)tcpip_adapter_ap_input), ESP_OK);
|
|
|
|
WIFI_API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(WIFI_IF_AP, ap_mac), ESP_OK);
|
|
|
|
|
|
|
|
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ap_ip);
|
|
|
|
tcpip_adapter_start(TCPIP_ADAPTER_IF_AP, ap_mac, &ap_ip);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t system_event_ap_stop_handle_default(system_event_t *event)
|
|
|
|
{
|
|
|
|
WIFI_API_CALL_CHECK("esp_wifi_reg_rxcb", esp_wifi_reg_rxcb(WIFI_IF_AP, NULL), ESP_OK);
|
|
|
|
|
|
|
|
tcpip_adapter_stop(TCPIP_ADAPTER_IF_AP);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t system_event_sta_start_handle_default(system_event_t *event)
|
|
|
|
{
|
2016-09-06 05:02:02 +00:00
|
|
|
tcpip_adapter_ip_info_t sta_ip;
|
2016-08-17 15:08:22 +00:00
|
|
|
uint8_t sta_mac[6];
|
|
|
|
|
|
|
|
WIFI_API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(WIFI_IF_STA, sta_mac), ESP_OK);
|
|
|
|
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip);
|
|
|
|
tcpip_adapter_start(TCPIP_ADAPTER_IF_STA, sta_mac, &sta_ip);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t system_event_sta_stop_handle_default(system_event_t *event)
|
|
|
|
{
|
|
|
|
tcpip_adapter_stop(TCPIP_ADAPTER_IF_STA);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t system_event_sta_connected_handle_default(system_event_t *event)
|
|
|
|
{
|
2016-08-26 03:52:46 +00:00
|
|
|
tcpip_adapter_dhcp_status_t status;
|
2016-09-13 07:57:11 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
WIFI_API_CALL_CHECK("esp_wifi_reg_rxcb", esp_wifi_reg_rxcb(WIFI_IF_STA, (wifi_rxcb_t)tcpip_adapter_sta_input), ESP_OK);
|
|
|
|
|
|
|
|
tcpip_adapter_up(TCPIP_ADAPTER_IF_STA);
|
2016-08-26 03:52:46 +00:00
|
|
|
|
|
|
|
tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &status);
|
|
|
|
|
|
|
|
if (status == TCPIP_ADAPTER_DHCP_INIT) {
|
|
|
|
tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
|
2016-09-05 03:05:07 +00:00
|
|
|
} else if (status == TCPIP_ADAPTER_DHCP_STOPPED) {
|
2016-09-06 05:02:02 +00:00
|
|
|
tcpip_adapter_ip_info_t sta_ip;
|
2016-09-05 03:05:07 +00:00
|
|
|
|
2016-09-06 05:02:02 +00:00
|
|
|
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip);
|
2016-09-05 03:05:07 +00:00
|
|
|
|
2016-09-05 12:14:58 +00:00
|
|
|
if (!(ip4_addr_isany_val(sta_ip.ip) || ip4_addr_isany_val(sta_ip.netmask) || ip4_addr_isany_val(sta_ip.gw))) {
|
|
|
|
system_event_t evt;
|
2016-09-05 03:05:07 +00:00
|
|
|
|
2016-09-05 12:14:58 +00:00
|
|
|
//notify event
|
2016-09-12 08:43:32 +00:00
|
|
|
evt.event_id = SYSTEM_EVENT_STA_GOT_IP;
|
2016-09-05 12:14:58 +00:00
|
|
|
memcpy(&evt.event_info.got_ip.ip_info, &sta_ip, sizeof(tcpip_adapter_ip_info_t));
|
|
|
|
|
|
|
|
esp_event_send(&evt);
|
|
|
|
} else {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGE(TAG, "invalid static ip");
|
2016-09-05 12:14:58 +00:00
|
|
|
}
|
2016-08-26 03:52:46 +00:00
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t system_event_sta_disconnected_handle_default(system_event_t *event)
|
|
|
|
{
|
|
|
|
tcpip_adapter_down(TCPIP_ADAPTER_IF_STA);
|
|
|
|
WIFI_API_CALL_CHECK("esp_wifi_reg_rxcb", esp_wifi_reg_rxcb(WIFI_IF_STA, NULL), ESP_OK);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t esp_system_event_debug(system_event_t *event)
|
|
|
|
{
|
|
|
|
if (event == NULL) {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGE(TAG, "event is null!");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event->event_id) {
|
2016-09-13 07:57:11 +00:00
|
|
|
case SYSTEM_EVENT_WIFI_READY: {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_WIFI_READY");
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_SCAN_DONE: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_sta_scan_done_t *scan_done = &event->event_info.scan_done;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_SCAN_DONE, status:%d, number:%d", scan_done->status, scan_done->number);
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_STA_START: {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_STA_START");
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_STA_STOP: {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_STA_STOP");
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_STA_CONNECTED: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_sta_connected_t *connected = &event->event_info.connected;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_STA_CONNECTED, ssid:%s, ssid_len:%d, bssid:%02x:%02x:%02x:%02x:%02x:%02x, channel:%d, authmode:%d", \
|
2016-09-13 07:57:11 +00:00
|
|
|
connected->ssid, connected->ssid_len, connected->bssid[0], connected->bssid[0], connected->bssid[1], \
|
|
|
|
connected->bssid[3], connected->bssid[4], connected->bssid[5], connected->channel, connected->authmode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_STA_DISCONNECTED: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_STA_DISCONNECTED, ssid:%s, ssid_len:%d, bssid:%02x:%02x:%02x:%02x:%02x:%02x, reason:%d", \
|
2016-09-13 07:57:11 +00:00
|
|
|
disconnected->ssid, disconnected->ssid_len, disconnected->bssid[0], disconnected->bssid[0], disconnected->bssid[1], \
|
|
|
|
disconnected->bssid[3], disconnected->bssid[4], disconnected->bssid[5], disconnected->reason);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_sta_authmode_change_t *auth_change = &event->event_info.auth_change;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_STA_AUTHMODE_CHNAGE, old_mode:%d, new_mode:%d", auth_change->old_mode, auth_change->new_mode);
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_STA_GOT_IP: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_sta_got_ip_t *got_ip = &event->event_info.got_ip;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_STA_GOTIP, ip:" IPSTR ", mask:" IPSTR ", gw:" IPSTR,
|
|
|
|
IP2STR(&got_ip->ip_info.ip),
|
|
|
|
IP2STR(&got_ip->ip_info.netmask),
|
|
|
|
IP2STR(&got_ip->ip_info.gw));
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_AP_START: {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_AP_START");
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_AP_STOP: {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STOP");
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_AP_STACONNECTED: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_ap_staconnected_t *staconnected = &event->event_info.sta_connected;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STACONNECTED, mac:%02x:%02x:%02x:%02x:%02x:%02x, aid:%d", \
|
2016-09-13 07:57:11 +00:00
|
|
|
staconnected->mac[0], staconnected->mac[0], staconnected->mac[1], \
|
|
|
|
staconnected->mac[3], staconnected->mac[4], staconnected->mac[5], staconnected->aid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_AP_STADISCONNECTED: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_ap_stadisconnected_t *stadisconnected = &event->event_info.sta_disconnected;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STADISCONNECTED, mac:%02x:%02x:%02x:%02x:%02x:%02x, aid:%d", \
|
2016-09-13 07:57:11 +00:00
|
|
|
stadisconnected->mac[0], stadisconnected->mac[0], stadisconnected->mac[1], \
|
|
|
|
stadisconnected->mac[3], stadisconnected->mac[4], stadisconnected->mac[5], stadisconnected->aid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYSTEM_EVENT_AP_PROBEREQRECVED: {
|
2016-09-23 06:46:39 +00:00
|
|
|
system_event_ap_probe_req_rx_t *ap_probereqrecved = &event->event_info.ap_probereqrecved;
|
|
|
|
ESP_LOGD(TAG, "SYSTEM_EVENT_AP_PROBEREQRECVED, rssi:%d, mac:%02x:%02x:%02x:%02x:%02x:%02x", \
|
2016-09-13 07:57:11 +00:00
|
|
|
ap_probereqrecved->rssi, ap_probereqrecved->mac[0], ap_probereqrecved->mac[0], ap_probereqrecved->mac[1], \
|
|
|
|
ap_probereqrecved->mac[3], ap_probereqrecved->mac[4], ap_probereqrecved->mac[5]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGW(TAG, "no such kind of event!");
|
2016-09-13 07:57:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-25 17:17:32 +00:00
|
|
|
esp_err_t esp_event_process_default(system_event_t *event)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
if (event == NULL) {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGE(TAG, "Error: event is null!");
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_system_event_debug(event);
|
2016-09-13 07:57:11 +00:00
|
|
|
if ((event->event_id < SYSTEM_EVENT_MAX) && (event->event_id == g_system_event_handle_table[event->event_id].event_id)) {
|
|
|
|
if (g_system_event_handle_table[event->event_id].event_handle) {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGV(TAG, "enter default callback");
|
2016-08-17 15:08:22 +00:00
|
|
|
g_system_event_handle_table[event->event_id].event_handle(event);
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGV(TAG, "exit default callback");
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-23 06:46:39 +00:00
|
|
|
ESP_LOGE(TAG, "mismatch or invalid event, id=%d", event->event_id);
|
2016-09-13 07:57:11 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|