|
|
@@ -1,5 +1,6 @@
|
|
|
#include "../pikascript-lib/PikaStdDevice/pika_hal.h"
|
|
|
#include "esp_event.h"
|
|
|
+#include "esp_mac.h"
|
|
|
#include "esp_netif.h"
|
|
|
#include "esp_wifi.h"
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
@@ -14,13 +15,27 @@ static volatile PIKA_HAL_WIFI_STATUS wifi_sta_disconn_reason =
|
|
|
PIKA_HAL_WIFI_STATUS_IDLE;
|
|
|
static EventGroupHandle_t wifi_event_group;
|
|
|
static esp_netif_t* sta_netif = NULL;
|
|
|
+static esp_netif_t* ap_netif = NULL;
|
|
|
+
|
|
|
+uint32_t _ip_str2u32(char* ip_str) {
|
|
|
+ uint32_t ip = 0;
|
|
|
+ uint8_t* ip_u8 = (uint8_t*)&ip;
|
|
|
+ char* p = ip_str;
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ ip_u8[i] = atoi(p);
|
|
|
+ p = strchr(p, '.');
|
|
|
+ if (p == NULL) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ p++;
|
|
|
+ }
|
|
|
+ return ip;
|
|
|
+}
|
|
|
|
|
|
static void event_handler(void* event_handler_arg,
|
|
|
esp_event_base_t event_base,
|
|
|
int32_t event_id,
|
|
|
void* event_data) {
|
|
|
- pika_dev* dev = (pika_dev*)event_handler_arg;
|
|
|
- pika_hal_WIFI_config* cfg = (pika_hal_WIFI_config*)dev->ioctl_config;
|
|
|
pika_debug("event_handler: %p %d", event_base, event_id);
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
|
|
pika_debug("event_handler: WIFI_EVENT_STA_START");
|
|
|
@@ -30,6 +45,8 @@ static void event_handler(void* event_handler_arg,
|
|
|
pika_debug("event_handler: WIFI_EVENT_STA_START: connect");
|
|
|
esp_wifi_connect();
|
|
|
}
|
|
|
+ } else if (event_id == WIFI_EVENT_AP_START) {
|
|
|
+ pika_debug("event_handler: WIFI_EVENT_AP_START");
|
|
|
} else if (event_base == WIFI_EVENT &&
|
|
|
event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
|
wifi_sta_connected = PIKA_FALSE;
|
|
|
@@ -41,15 +58,18 @@ static void event_handler(void* event_handler_arg,
|
|
|
}
|
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
|
pika_debug("event_handler: IP_EVENT_STA_GOT_IP");
|
|
|
- ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
|
|
|
- sprintf(cfg->ip, IPSTR, IP2STR(&event->ip_info.ip));
|
|
|
- sprintf(cfg->gateway, IPSTR, IP2STR(&event->ip_info.gw));
|
|
|
- sprintf(cfg->netmask, IPSTR, IP2STR(&event->ip_info.netmask));
|
|
|
- esp_netif_dns_info_t dns_info;
|
|
|
- esp_netif_get_dns_info(sta_netif, ESP_NETIF_DNS_MAIN, &dns_info);
|
|
|
- sprintf(cfg->dns, IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
|
|
|
wifi_sta_connected = PIKA_TRUE;
|
|
|
wifi_sta_disconn_reason = PIKA_HAL_WIFI_STATUS_GOT_IP;
|
|
|
+ } else if (event_id == WIFI_EVENT_AP_STACONNECTED) {
|
|
|
+ wifi_event_ap_staconnected_t* event =
|
|
|
+ (wifi_event_ap_staconnected_t*)event_data;
|
|
|
+ pika_debug("station " MACSTR " join, AID=%d", MAC2STR(event->mac),
|
|
|
+ event->aid);
|
|
|
+ } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
|
|
+ wifi_event_ap_stadisconnected_t* event =
|
|
|
+ (wifi_event_ap_stadisconnected_t*)event_data;
|
|
|
+ pika_debug("station " MACSTR " leave, AID=%d", MAC2STR(event->mac),
|
|
|
+ event->aid);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -65,7 +85,6 @@ int pika_hal_platform_WIFI_open(pika_dev* dev, char* name) {
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
wifi_event_group = xEventGroupCreate();
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
- sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -75,80 +94,73 @@ int pika_hal_platform_WIFI_close(pika_dev* dev) {
|
|
|
|
|
|
int pika_hal_platform_WIFI_ioctl_config(pika_dev* dev,
|
|
|
pika_hal_WIFI_config* cfg) {
|
|
|
+ if (!wifi_started) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (cfg->mode == PIKA_HAL_WIFI_MODE_STA) {
|
|
|
+ pika_platform_printf("Error: STA mode not support config\r\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (cfg->mode == PIKA_HAL_WIFI_MODE_AP) {
|
|
|
+ wifi_config_t esp_wifi_cfg = {0};
|
|
|
+ // ESP_ERROR_CHECK(esp_wifi_get_config(WIFI_IF_AP, &esp_wifi_cfg));
|
|
|
+ strcpy((char*)esp_wifi_cfg.ap.ssid, cfg->ap_ssid);
|
|
|
+ strcpy((char*)esp_wifi_cfg.ap.password, cfg->ap_password);
|
|
|
+ esp_wifi_cfg.ap.ssid_len = strlen(cfg->ap_ssid);
|
|
|
+ esp_wifi_cfg.ap.channel = cfg->channel;
|
|
|
+ esp_wifi_cfg.ap.authmode = WIFI_AUTH_WPA2_PSK;
|
|
|
+ esp_wifi_cfg.ap.max_connection = cfg->max_connection - 1;
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &esp_wifi_cfg));
|
|
|
+ }
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int pika_hal_platform_WIFI_ioctl_enable(pika_dev* dev) {
|
|
|
- /* connect here */
|
|
|
- pika_debug("pika_hal_platform_WIFI_ioctl_enable");
|
|
|
- wifi_config_t wifi_sta_config = {0};
|
|
|
+ wifi_mode_t mode;
|
|
|
pika_hal_WIFI_config* cfg = (pika_hal_WIFI_config*)dev->ioctl_config;
|
|
|
- strcpy((char*)wifi_sta_config.sta.ssid, cfg->ssid);
|
|
|
- strcpy((char*)wifi_sta_config.sta.password, cfg->password);
|
|
|
- if (cfg->bssid[0] != '\0') {
|
|
|
- wifi_sta_config.sta.bssid_set = 1;
|
|
|
- strcpy((char*)wifi_sta_config.sta.bssid, cfg->bssid);
|
|
|
+ if (cfg->mode == PIKA_HAL_WIFI_MODE_STA) {
|
|
|
+ mode = WIFI_MODE_STA;
|
|
|
+ } else if (cfg->mode == PIKA_HAL_WIFI_MODE_AP) {
|
|
|
+ mode = WIFI_MODE_AP;
|
|
|
} else {
|
|
|
- wifi_sta_config.sta.bssid_set = 0;
|
|
|
+ mode = WIFI_MODE_APSTA;
|
|
|
+ }
|
|
|
+ if (wifi_started) {
|
|
|
+ esp_wifi_set_mode(mode);
|
|
|
+ } else {
|
|
|
+ pika_debug("regist event handler");
|
|
|
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
+ pika_debug("init wifi");
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
+
|
|
|
+ ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
|
|
+ &event_handler, dev));
|
|
|
+ ESP_ERROR_CHECK(esp_event_handler_register(
|
|
|
+ IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, dev));
|
|
|
+ pika_debug("set mode: %d", mode);
|
|
|
+ esp_wifi_set_mode(mode);
|
|
|
+ sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
+ ap_netif = esp_netif_create_default_wifi_ap();
|
|
|
+ pika_debug("start wifi");
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
+ wifi_started = PIKA_TRUE;
|
|
|
}
|
|
|
- wifi_sta_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
|
|
- wifi_sta_config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;
|
|
|
- ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config));
|
|
|
- pika_debug("connecting to %s", cfg->ssid);
|
|
|
- wifi_sta_connect_requested = PIKA_TRUE;
|
|
|
- ESP_ERROR_CHECK(esp_wifi_connect());
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int pika_hal_platform_WIFI_ioctl_disable(pika_dev* dev) {
|
|
|
- /* disconenct here */
|
|
|
+ if (wifi_started) {
|
|
|
+ esp_wifi_stop();
|
|
|
+ esp_wifi_deinit();
|
|
|
+ wifi_started = PIKA_FALSE;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
int pika_hal_platform_WIFI_ioctl_others(pika_dev* dev,
|
|
|
PIKA_HAL_IOCTL_CMD cmd,
|
|
|
void* arg) {
|
|
|
- if (cmd == PIKA_HAL_IOCTL_WIFI_SET_ACTIVE) {
|
|
|
- wifi_mode_t mode;
|
|
|
- pika_hal_WIFI_config* cfg = (pika_hal_WIFI_config*)dev->ioctl_config;
|
|
|
- int active = *(int*)arg;
|
|
|
- pika_debug("set wifi active: %d", active);
|
|
|
- if (active) {
|
|
|
- if (cfg->mode == PIKA_HAL_WIFI_MODE_STA) {
|
|
|
- mode = WIFI_MODE_STA;
|
|
|
- } else if (cfg->mode == PIKA_HAL_WIFI_MODE_AP) {
|
|
|
- mode = WIFI_MODE_AP;
|
|
|
- } else {
|
|
|
- mode = WIFI_MODE_APSTA;
|
|
|
- }
|
|
|
- if (wifi_started) {
|
|
|
- esp_wifi_set_mode(mode);
|
|
|
- } else {
|
|
|
- pika_debug("regist event handler");
|
|
|
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
- pika_debug("init wifi");
|
|
|
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
-
|
|
|
- ESP_ERROR_CHECK(esp_event_handler_register(
|
|
|
- WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, dev));
|
|
|
- ESP_ERROR_CHECK(esp_event_handler_register(
|
|
|
- IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, dev));
|
|
|
-
|
|
|
- pika_debug("set mode: %d", mode);
|
|
|
- esp_wifi_set_mode(mode);
|
|
|
- pika_debug("start wifi");
|
|
|
- ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
- wifi_started = PIKA_TRUE;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (wifi_started) {
|
|
|
- esp_wifi_stop();
|
|
|
- esp_wifi_deinit();
|
|
|
- wifi_started = PIKA_FALSE;
|
|
|
- }
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
if (cmd == PIKA_HAL_IOCTL_WIFI_GET_STATUS) {
|
|
|
if (wifi_sta_connect_requested) {
|
|
|
if (wifi_sta_connected) {
|
|
|
@@ -215,5 +227,80 @@ int pika_hal_platform_WIFI_ioctl_others(pika_dev* dev,
|
|
|
*(pika_hal_WIFI_scan_result**)arg = result;
|
|
|
return 0;
|
|
|
}
|
|
|
+ if (cmd == PIKA_HAL_IOCTL_WIFI_CONNECT) {
|
|
|
+ pika_debug("ioctl connect");
|
|
|
+ wifi_config_t esp_wifi_cfg = {0};
|
|
|
+ pika_hal_WIFI_connect_config* conncfg =
|
|
|
+ (pika_hal_WIFI_connect_config*)arg;
|
|
|
+ // ESP_ERROR_CHECK(esp_wifi_get_config(WIFI_IF_STA, &esp_wifi_cfg));
|
|
|
+ strcpy((char*)esp_wifi_cfg.sta.ssid, conncfg->ssid);
|
|
|
+ strcpy((char*)esp_wifi_cfg.sta.password, conncfg->password);
|
|
|
+ if (conncfg->bssid[0] != '\0') {
|
|
|
+ esp_wifi_cfg.sta.bssid_set = 1;
|
|
|
+ strcpy((char*)esp_wifi_cfg.sta.bssid, conncfg->bssid);
|
|
|
+ } else {
|
|
|
+ esp_wifi_cfg.sta.bssid_set = 0;
|
|
|
+ }
|
|
|
+ esp_wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
|
|
+ esp_wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &esp_wifi_cfg));
|
|
|
+ pika_debug("connecting to %s, password:%s, bssid_set:%d", conncfg->ssid,
|
|
|
+ conncfg->password, esp_wifi_cfg.sta.bssid_set);
|
|
|
+ wifi_sta_connect_requested = PIKA_TRUE;
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_connect());
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (cmd == PIKA_HAL_IOCTL_WIFI_GET_IFCONFIG) {
|
|
|
+ pika_hal_WIFI_config* cfg = (pika_hal_WIFI_config*)dev->ioctl_config;
|
|
|
+ esp_netif_t* netif = NULL;
|
|
|
+ if (cfg->mode == PIKA_HAL_WIFI_MODE_STA) {
|
|
|
+ netif = sta_netif;
|
|
|
+ } else if (cfg->mode == PIKA_HAL_WIFI_MODE_AP) {
|
|
|
+ netif = ap_netif;
|
|
|
+ }
|
|
|
+ esp_netif_ip_info_t ip_info;
|
|
|
+ esp_netif_dns_info_t dns_info;
|
|
|
+ esp_netif_get_ip_info(netif, &ip_info);
|
|
|
+ esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info);
|
|
|
+ pika_hal_WIFI_ifconfig* hal_if = (pika_hal_WIFI_ifconfig*)arg;
|
|
|
+ sprintf(hal_if->ip, IPSTR, IP2STR(&ip_info.ip));
|
|
|
+ sprintf(hal_if->netmask, IPSTR, IP2STR(&ip_info.netmask));
|
|
|
+ sprintf(hal_if->gateway, IPSTR, IP2STR(&ip_info.gw));
|
|
|
+ sprintf(hal_if->dns, IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (cmd == PIKA_HAL_IOCTL_WIFI_SET_IFCONFIG) {
|
|
|
+ pika_hal_WIFI_config* cfg = (pika_hal_WIFI_config*)dev->ioctl_config;
|
|
|
+ esp_netif_t* netif = NULL;
|
|
|
+ if (cfg->mode == PIKA_HAL_WIFI_MODE_STA) {
|
|
|
+ netif = sta_netif;
|
|
|
+ } else if (cfg->mode == PIKA_HAL_WIFI_MODE_AP) {
|
|
|
+ netif = ap_netif;
|
|
|
+ }
|
|
|
+ pika_hal_WIFI_ifconfig* hal_if = (pika_hal_WIFI_ifconfig*)arg;
|
|
|
+ esp_netif_ip_info_t ip_info;
|
|
|
+ esp_netif_dns_info_t dns_info;
|
|
|
+ ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip_info));
|
|
|
+ ESP_ERROR_CHECK(
|
|
|
+ esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info));
|
|
|
+ ip_info.ip.addr = _ip_str2u32(hal_if->ip);
|
|
|
+ ip_info.netmask.addr = _ip_str2u32(hal_if->netmask);
|
|
|
+ ip_info.gw.addr = _ip_str2u32(hal_if->gateway);
|
|
|
+ dns_info.ip.u_addr.ip4.addr = _ip_str2u32(hal_if->dns);
|
|
|
+ if (cfg->mode == PIKA_HAL_WIFI_MODE_STA) {
|
|
|
+ esp_netif_dhcpc_stop(netif);
|
|
|
+ } else if (cfg->mode == PIKA_HAL_WIFI_MODE_AP) {
|
|
|
+ esp_netif_dhcps_stop(netif);
|
|
|
+ }
|
|
|
+ ESP_ERROR_CHECK(esp_netif_set_ip_info(netif, &ip_info));
|
|
|
+ if (dns_info.ip.u_addr.ip4.addr != 0) {
|
|
|
+ ESP_ERROR_CHECK(
|
|
|
+ esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info));
|
|
|
+ }
|
|
|
+ if (cfg->mode == PIKA_HAL_WIFI_MODE_AP) {
|
|
|
+ esp_netif_dhcps_start(netif);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
return -1;
|
|
|
}
|