Compare commits

..

No commits in common. "ff51b9ef234532b51809374d2d412bf097b662a0" and "8403ff349eb90555546d55d587774717ac872250" have entirely different histories.

10 changed files with 30 additions and 663 deletions

View File

@ -1,4 +1,9 @@
idf_component_register(SRCS "eis4.c" "eis.c" "echem.c" "protocol.c" "wifi_transport.c" "temp.c" "refs.c"
"wifi_cfg.c" "ble_prov.c"
INCLUDE_DIRS "."
REQUIRES ad5941 ad5941_port nvs_flash esp_wifi esp_netif esp_event esp_timer bt)
REQUIRES ad5941 ad5941_port nvs_flash esp_wifi esp_netif esp_event esp_timer)
if(DEFINED ENV{WIFI_SSID})
target_compile_definitions(${COMPONENT_LIB} PRIVATE
STA_SSID="$ENV{WIFI_SSID}"
STA_PASS="$ENV{WIFI_PASS}")
endif()

View File

@ -1,391 +0,0 @@
#include "ble_prov.h"
#include "wifi_cfg.h"
#include "protocol.h"
#include <string.h>
#include <stdio.h>
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/ble_uuid.h"
#include "host/util/util.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
#include "esp_mac.h"
/* wifi_transport provides these */
extern esp_err_t wifi_transport_reconnect_sta(const char *ssid, const char *pass);
extern void wifi_transport_get_sta_state(uint8_t *state, uint32_t *ip);
#define DEVICE_NAME "EIS4"
#define FW_VERSION "4.0"
#define RSP_BUF_MAX 128
/* EIS4 provisioning service UUID: 4e455334-a001-4801-b947-001122334455 */
static const ble_uuid128_t svc_uuid =
BLE_UUID128_INIT(0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0x47, 0xb9,
0x01, 0x48, 0x01, 0xa0, 0x34, 0x53, 0x45, 0x4e);
/* EIS4 provisioning characteristic UUID: 4e455334-a002-4801-b947-001122334455 */
static const ble_uuid128_t chr_uuid =
BLE_UUID128_INIT(0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0x47, 0xb9,
0x01, 0x48, 0x02, 0xa0, 0x34, 0x53, 0x45, 0x4e);
static bool s_ble_active;
static uint16_t s_chr_handle;
static uint8_t s_rsp_buf[RSP_BUF_MAX];
static uint16_t s_rsp_len;
static uint8_t s_own_addr_type;
static void start_advertise(void);
/* -- response builders -- */
static void build_wifi_cfg_rsp(void)
{
char ssid[WIFI_CFG_SSID_MAX], pass[WIFI_CFG_PASS_MAX];
uint8_t *p = s_rsp_buf;
*p++ = 0xF0;
*p++ = 0x7D;
*p++ = RSP_WIFI_CFG;
/* STA SSID */
if (wifi_cfg_get_sta(ssid, sizeof(ssid), pass, sizeof(pass)) == ESP_OK) {
uint8_t slen = (uint8_t)strlen(ssid);
*p++ = slen;
memcpy(p, ssid, slen); p += slen;
/* STA status */
uint8_t state; uint32_t ip;
wifi_transport_get_sta_state(&state, &ip);
*p++ = state;
} else {
*p++ = 0;
*p++ = WIFI_STATE_DISCONNECTED;
}
/* AP SSID */
wifi_cfg_get_ap(ssid, sizeof(ssid), pass, sizeof(pass));
{
uint8_t slen = (uint8_t)strlen(ssid);
*p++ = slen;
memcpy(p, ssid, slen); p += slen;
}
*p++ = 0xF7;
s_rsp_len = (uint16_t)(p - s_rsp_buf);
}
static void build_set_result(uint8_t ok)
{
uint8_t *p = s_rsp_buf;
*p++ = 0xF0;
*p++ = 0x7D;
*p++ = RSP_SET_WIFI_RESULT;
*p++ = ok ? 0 : 1;
*p++ = 0xF7;
s_rsp_len = (uint16_t)(p - s_rsp_buf);
}
static void build_device_info_rsp(void)
{
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_STA);
uint8_t *p = s_rsp_buf;
*p++ = 0xF0;
*p++ = 0x7D;
*p++ = RSP_DEVICE_INFO;
/* version */
uint8_t vlen = (uint8_t)strlen(FW_VERSION);
*p++ = vlen;
memcpy(p, FW_VERSION, vlen); p += vlen;
/* name */
uint8_t nlen = (uint8_t)strlen(DEVICE_NAME);
*p++ = nlen;
memcpy(p, DEVICE_NAME, nlen); p += nlen;
/* MAC (6 bytes, each masked to 7-bit halves) */
for (int i = 0; i < 6; i++) {
*p++ = mac[i] & 0x7F;
*p++ = (mac[i] >> 7) & 0x01;
}
*p++ = 0xF7;
s_rsp_len = (uint16_t)(p - s_rsp_buf);
}
static void build_wifi_status_rsp(void)
{
uint8_t state;
uint32_t ip;
wifi_transport_get_sta_state(&state, &ip);
uint8_t *p = s_rsp_buf;
*p++ = 0xF0;
*p++ = 0x7D;
*p++ = RSP_WIFI_STATUS;
*p++ = state;
if (state == WIFI_STATE_CONNECTED) {
*p++ = (ip >> 0) & 0x7F;
*p++ = (ip >> 7) & 0x7F;
*p++ = (ip >> 14) & 0x7F;
*p++ = (ip >> 21) & 0x7F;
*p++ = (ip >> 28) & 0x0F;
}
*p++ = 0xF7;
s_rsp_len = (uint16_t)(p - s_rsp_buf);
}
/* -- command processing -- */
static void process_command(const uint8_t *data, uint16_t len)
{
if (len < 4 || data[0] != 0xF0 || data[1] != 0x7D)
return;
uint16_t end = 0;
for (uint16_t i = 2; i < len; i++) {
if (data[i] == 0xF7) { end = i; break; }
}
if (!end) return;
uint8_t cmd = data[2];
switch (cmd) {
case CMD_GET_WIFI_CFG:
build_wifi_cfg_rsp();
break;
case CMD_SET_WIFI_STA: {
if (end < 6) { build_set_result(0); break; }
uint8_t ssid_len = data[3];
if (3 + 1 + ssid_len + 1 >= end) { build_set_result(0); break; }
char ssid[WIFI_CFG_SSID_MAX] = {0};
memcpy(ssid, &data[4], ssid_len);
uint8_t pass_len = data[4 + ssid_len];
char pass[WIFI_CFG_PASS_MAX] = {0};
if (pass_len > 0 && (4 + ssid_len + 1 + pass_len) <= end)
memcpy(pass, &data[5 + ssid_len], pass_len);
esp_err_t err = wifi_cfg_set_sta(ssid, pass);
if (err == ESP_OK)
err = wifi_transport_reconnect_sta(ssid, pass);
build_set_result(err == ESP_OK);
break;
}
case CMD_SET_WIFI_AP: {
if (end < 6) { build_set_result(0); break; }
uint8_t ssid_len = data[3];
if (3 + 1 + ssid_len + 1 >= end) { build_set_result(0); break; }
char ssid[WIFI_CFG_SSID_MAX] = {0};
memcpy(ssid, &data[4], ssid_len);
uint8_t pass_len = data[4 + ssid_len];
char pass[WIFI_CFG_PASS_MAX] = {0};
if (pass_len > 0 && (4 + ssid_len + 1 + pass_len) <= end)
memcpy(pass, &data[5 + ssid_len], pass_len);
esp_err_t err = wifi_cfg_set_ap(ssid, pass);
build_set_result(err == ESP_OK);
break;
}
case CMD_GET_DEVICE_INFO:
build_device_info_rsp();
break;
case CMD_WIFI_STATUS:
build_wifi_status_rsp();
break;
default:
return;
}
}
/* -- GATT -- */
static int chr_access_cb(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg)
{
(void)conn_handle;
(void)attr_handle;
(void)arg;
switch (ctxt->op) {
case BLE_GATT_ACCESS_OP_READ_CHR:
if (s_rsp_len > 0)
os_mbuf_append(ctxt->om, s_rsp_buf, s_rsp_len);
return 0;
case BLE_GATT_ACCESS_OP_WRITE_CHR: {
uint8_t buf[RSP_BUF_MAX];
uint16_t out_len = 0;
uint16_t om_len = OS_MBUF_PKTLEN(ctxt->om);
if (om_len > sizeof(buf)) om_len = sizeof(buf);
ble_hs_mbuf_to_flat(ctxt->om, buf, sizeof(buf), &out_len);
process_command(buf, out_len);
return 0;
}
default:
return BLE_ATT_ERR_UNLIKELY;
}
}
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
{
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &svc_uuid.u,
.characteristics = (struct ble_gatt_chr_def[]) {
{
.uuid = &chr_uuid.u,
.access_cb = chr_access_cb,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
.val_handle = &s_chr_handle,
},
{ 0 },
},
},
{ 0 },
};
static int gatt_svr_init(void)
{
ble_svc_gap_init();
ble_svc_gatt_init();
int rc = ble_gatts_count_cfg(gatt_svr_svcs);
if (rc != 0) return rc;
return ble_gatts_add_svcs(gatt_svr_svcs);
}
/* -- GAP -- */
static int gap_event_cb(struct ble_gap_event *event, void *arg)
{
(void)arg;
switch (event->type) {
case BLE_GAP_EVENT_CONNECT:
if (event->connect.status != 0)
start_advertise();
return 0;
case BLE_GAP_EVENT_DISCONNECT:
if (s_ble_active)
start_advertise();
return 0;
case BLE_GAP_EVENT_ADV_COMPLETE:
if (s_ble_active)
start_advertise();
return 0;
default:
return 0;
}
}
static void start_advertise(void)
{
struct ble_hs_adv_fields fields = {0};
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
fields.tx_pwr_lvl_is_present = 1;
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
const char *name = ble_svc_gap_device_name();
fields.name = (uint8_t *)name;
fields.name_len = strlen(name);
fields.name_is_complete = 1;
ble_gap_adv_set_fields(&fields);
struct ble_gap_adv_params params = {0};
params.conn_mode = BLE_GAP_CONN_MODE_UND;
params.disc_mode = BLE_GAP_DISC_MODE_GEN;
ble_gap_adv_start(s_own_addr_type, NULL, BLE_HS_FOREVER,
&params, gap_event_cb, NULL);
}
static void on_sync(void)
{
ble_hs_util_ensure_addr(0);
ble_hs_id_infer_auto(0, &s_own_addr_type);
start_advertise();
printf("BLE: advertising\n");
}
static void on_reset(int reason)
{
printf("BLE: host reset, reason=%d\n", reason);
}
static void ble_host_task(void *param)
{
(void)param;
nimble_port_run();
nimble_port_freertos_deinit();
}
/* -- public API -- */
esp_err_t ble_prov_start(void)
{
if (s_ble_active)
return ESP_OK;
esp_err_t err = nimble_port_init();
if (err != ESP_OK) {
printf("BLE: nimble_port_init failed: %d\n", (int)err);
return err;
}
ble_hs_cfg.reset_cb = on_reset;
ble_hs_cfg.sync_cb = on_sync;
ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO;
int rc = gatt_svr_init();
if (rc != 0) {
printf("BLE: GATT init failed: %d\n", rc);
nimble_port_deinit();
return ESP_FAIL;
}
ble_svc_gap_device_name_set(DEVICE_NAME);
nimble_port_freertos_init(ble_host_task);
s_ble_active = true;
s_rsp_len = 0;
printf("BLE: provisioning started\n");
return ESP_OK;
}
esp_err_t ble_prov_stop(void)
{
if (!s_ble_active)
return ESP_OK;
s_ble_active = false;
if (ble_gap_adv_active())
ble_gap_adv_stop();
nimble_port_stop();
nimble_port_deinit();
printf("BLE: provisioning stopped\n");
return ESP_OK;
}
bool ble_prov_is_active(void)
{
return s_ble_active;
}

View File

@ -1,11 +0,0 @@
#ifndef BLE_PROV_H
#define BLE_PROV_H
#include "esp_err.h"
#include <stdbool.h>
esp_err_t ble_prov_start(void);
esp_err_t ble_prov_stop(void);
bool ble_prov_is_active(void);
#endif

View File

@ -5,7 +5,6 @@
#include "echem.h"
#include "protocol.h"
#include "wifi_transport.h"
#include "wifi_cfg.h"
#include "temp.h"
#include "refs.h"
#include "freertos/FreeRTOS.h"
@ -65,8 +64,6 @@ void app_main(void)
eis_load_ph_cal();
temp_init();
wifi_cfg_init();
esp_netif_init();
esp_event_loop_create_default();

View File

@ -38,13 +38,6 @@
#define CMD_SESSION_RENAME 0x43
#define CMD_HEARTBEAT 0x44
/* Provisioning commands (0x6x, over BLE) */
#define CMD_GET_WIFI_CFG 0x60
#define CMD_SET_WIFI_STA 0x61
#define CMD_SET_WIFI_AP 0x62
#define CMD_GET_DEVICE_INFO 0x63
#define CMD_WIFI_STATUS 0x64
/* Responses: Firmware -> Client (0x0x, 0x2x) */
#define RSP_SWEEP_START 0x01
#define RSP_DATA_POINT 0x02
@ -78,17 +71,6 @@
#define RSP_SESSION_RENAMED 0x43
#define RSP_CLIENT_LIST 0x44
/* Provisioning responses (0x6x, over BLE) */
#define RSP_WIFI_CFG 0x60
#define RSP_SET_WIFI_RESULT 0x61
#define RSP_DEVICE_INFO 0x63
#define RSP_WIFI_STATUS 0x64
/* WiFi STA state values (for RSP_WIFI_STATUS) */
#define WIFI_STATE_DISCONNECTED 0
#define WIFI_STATE_CONNECTING 1
#define WIFI_STATE_CONNECTED 2
/* Session limits */
#define MAX_SESSIONS 8
#define MAX_SESSION_NAME 32

View File

@ -1,117 +0,0 @@
#include "wifi_cfg.h"
#include <string.h>
#include <stdio.h>
#include "nvs.h"
#define NVS_NS "wifi_prov"
#define KEY_STA_SSID "sta_ssid"
#define KEY_STA_PASS "sta_pass"
#define KEY_AP_SSID "ap_ssid"
#define KEY_AP_PASS "ap_pass"
#define DEFAULT_AP_SSID "EIS4"
#define DEFAULT_AP_PASS "eis4data"
static char sta_ssid[WIFI_CFG_SSID_MAX];
static char sta_pass[WIFI_CFG_PASS_MAX];
static char ap_ssid[WIFI_CFG_SSID_MAX];
static char ap_pass[WIFI_CFG_PASS_MAX];
esp_err_t wifi_cfg_init(void)
{
memset(sta_ssid, 0, sizeof(sta_ssid));
memset(sta_pass, 0, sizeof(sta_pass));
strncpy(ap_ssid, DEFAULT_AP_SSID, sizeof(ap_ssid) - 1);
strncpy(ap_pass, DEFAULT_AP_PASS, sizeof(ap_pass) - 1);
nvs_handle_t h;
esp_err_t err = nvs_open(NVS_NS, NVS_READONLY, &h);
if (err == ESP_ERR_NVS_NOT_FOUND)
return ESP_OK;
if (err != ESP_OK)
return err;
size_t len;
len = sizeof(sta_ssid);
nvs_get_str(h, KEY_STA_SSID, sta_ssid, &len);
len = sizeof(sta_pass);
nvs_get_str(h, KEY_STA_PASS, sta_pass, &len);
len = sizeof(ap_ssid);
if (nvs_get_str(h, KEY_AP_SSID, ap_ssid, &len) != ESP_OK) {
strncpy(ap_ssid, DEFAULT_AP_SSID, sizeof(ap_ssid) - 1);
}
len = sizeof(ap_pass);
if (nvs_get_str(h, KEY_AP_PASS, ap_pass, &len) != ESP_OK) {
strncpy(ap_pass, DEFAULT_AP_PASS, sizeof(ap_pass) - 1);
}
nvs_close(h);
if (sta_ssid[0])
printf("WiFi cfg: STA \"%s\"\n", sta_ssid);
printf("WiFi cfg: AP \"%s\"\n", ap_ssid);
return ESP_OK;
}
bool wifi_cfg_has_sta(void)
{
return sta_ssid[0] != '\0';
}
esp_err_t wifi_cfg_get_sta(char *ssid, size_t ssid_sz, char *pass, size_t pass_sz)
{
if (!sta_ssid[0])
return ESP_ERR_NOT_FOUND;
strncpy(ssid, sta_ssid, ssid_sz - 1);
ssid[ssid_sz - 1] = '\0';
strncpy(pass, sta_pass, pass_sz - 1);
pass[pass_sz - 1] = '\0';
return ESP_OK;
}
esp_err_t wifi_cfg_set_sta(const char *ssid, const char *pass)
{
nvs_handle_t h;
esp_err_t err = nvs_open(NVS_NS, NVS_READWRITE, &h);
if (err != ESP_OK) return err;
nvs_set_str(h, KEY_STA_SSID, ssid);
nvs_set_str(h, KEY_STA_PASS, pass);
nvs_commit(h);
nvs_close(h);
strncpy(sta_ssid, ssid, sizeof(sta_ssid) - 1);
sta_ssid[sizeof(sta_ssid) - 1] = '\0';
strncpy(sta_pass, pass, sizeof(sta_pass) - 1);
sta_pass[sizeof(sta_pass) - 1] = '\0';
return ESP_OK;
}
esp_err_t wifi_cfg_get_ap(char *ssid, size_t ssid_sz, char *pass, size_t pass_sz)
{
strncpy(ssid, ap_ssid, ssid_sz - 1);
ssid[ssid_sz - 1] = '\0';
strncpy(pass, ap_pass, pass_sz - 1);
pass[pass_sz - 1] = '\0';
return ESP_OK;
}
esp_err_t wifi_cfg_set_ap(const char *ssid, const char *pass)
{
nvs_handle_t h;
esp_err_t err = nvs_open(NVS_NS, NVS_READWRITE, &h);
if (err != ESP_OK) return err;
nvs_set_str(h, KEY_AP_SSID, ssid);
nvs_set_str(h, KEY_AP_PASS, pass);
nvs_commit(h);
nvs_close(h);
strncpy(ap_ssid, ssid, sizeof(ap_ssid) - 1);
ap_ssid[sizeof(ap_ssid) - 1] = '\0';
strncpy(ap_pass, pass, sizeof(ap_pass) - 1);
ap_pass[sizeof(ap_pass) - 1] = '\0';
return ESP_OK;
}

View File

@ -1,18 +0,0 @@
#ifndef WIFI_CFG_H
#define WIFI_CFG_H
#include <stdbool.h>
#include <stddef.h>
#include "esp_err.h"
#define WIFI_CFG_SSID_MAX 33
#define WIFI_CFG_PASS_MAX 65
esp_err_t wifi_cfg_init(void);
bool wifi_cfg_has_sta(void);
esp_err_t wifi_cfg_get_sta(char *ssid, size_t ssid_sz, char *pass, size_t pass_sz);
esp_err_t wifi_cfg_set_sta(const char *ssid, const char *pass);
esp_err_t wifi_cfg_get_ap(char *ssid, size_t ssid_sz, char *pass, size_t pass_sz);
esp_err_t wifi_cfg_set_ap(const char *ssid, const char *pass);
#endif

View File

@ -1,6 +1,4 @@
#include "wifi_transport.h"
#include "wifi_cfg.h"
#include "ble_prov.h"
#include "protocol.h"
#include <string.h>
#include <stdio.h>
@ -8,13 +6,14 @@
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_wifi_ap_get_sta_list.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "lwip/sockets.h"
#define WIFI_SSID "EIS4"
#define WIFI_PASS "eis4data"
#define WIFI_CHANNEL 1
#define WIFI_MAX_CONN 10
@ -25,13 +24,8 @@
#define REAP_WINDOW_MS 200
#define REAP_INTERVAL_MS 5000
#define STA_RETRY_MAX 5
#define RECONNECT_BIT_OK BIT0
#define RECONNECT_BIT_FAIL BIT1
static int udp_sock = -1;
static esp_netif_t *ap_netif;
static esp_netif_t *sta_netif;
static struct {
struct sockaddr_in addr;
@ -43,11 +37,6 @@ static struct {
static int client_count;
static SemaphoreHandle_t client_mutex;
static uint8_t s_sta_state = WIFI_STATE_DISCONNECTED;
static uint32_t s_sta_ip;
static int s_sta_retries;
static EventGroupHandle_t s_reconnect_eg;
static void client_touch(const struct sockaddr_in *addr)
{
xSemaphoreTake(client_mutex, portMAX_DELAY);
@ -320,81 +309,30 @@ static void wifi_event_handler(void *arg, esp_event_base_t base,
}
}
#ifndef STA_SSID
#define STA_SSID ""
#endif
#ifndef STA_PASS
#define STA_PASS ""
#endif
static void sta_event_handler(void *arg, esp_event_base_t base,
int32_t id, void *data)
{
(void)arg;
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
s_sta_state = WIFI_STATE_DISCONNECTED;
s_sta_ip = 0;
s_sta_retries++;
printf("WiFi: STA disconnected (retry %d/%d)\n", s_sta_retries, STA_RETRY_MAX);
if (s_sta_retries >= STA_RETRY_MAX) {
if (s_reconnect_eg)
xEventGroupSetBits(s_reconnect_eg, RECONNECT_BIT_FAIL);
if (!ble_prov_is_active()) {
printf("WiFi: STA retries exhausted, starting BLE provisioning\n");
ble_prov_start();
}
}
s_sta_state = WIFI_STATE_CONNECTING;
printf("WiFi: STA disconnected, reconnecting...\n");
esp_wifi_connect();
} else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *evt = (ip_event_got_ip_t *)data;
s_sta_state = WIFI_STATE_CONNECTED;
s_sta_ip = evt->ip_info.ip.addr;
s_sta_retries = 0;
printf("WiFi: STA connected, IP " IPSTR "\n", IP2STR(&evt->ip_info.ip));
if (s_reconnect_eg)
xEventGroupSetBits(s_reconnect_eg, RECONNECT_BIT_OK);
if (ble_prov_is_active())
ble_prov_stop();
}
}
void wifi_transport_get_sta_state(uint8_t *state, uint32_t *ip)
{
*state = s_sta_state;
*ip = s_sta_ip;
}
esp_err_t wifi_transport_reconnect_sta(const char *ssid, const char *pass)
{
s_sta_retries = 0;
s_sta_state = WIFI_STATE_CONNECTING;
esp_wifi_disconnect();
wifi_config_t sta_cfg = {0};
strncpy((char *)sta_cfg.sta.ssid, ssid, sizeof(sta_cfg.sta.ssid) - 1);
strncpy((char *)sta_cfg.sta.password, pass, sizeof(sta_cfg.sta.password) - 1);
sta_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
esp_wifi_set_config(WIFI_IF_STA, &sta_cfg);
if (!s_reconnect_eg)
s_reconnect_eg = xEventGroupCreate();
xEventGroupClearBits(s_reconnect_eg, RECONNECT_BIT_OK | RECONNECT_BIT_FAIL);
esp_wifi_connect();
printf("WiFi: STA reconnecting to \"%s\"\n", ssid);
EventBits_t bits = xEventGroupWaitBits(s_reconnect_eg,
RECONNECT_BIT_OK | RECONNECT_BIT_FAIL,
pdTRUE, pdFALSE, pdMS_TO_TICKS(15000));
if (bits & RECONNECT_BIT_OK)
return ESP_OK;
return ESP_FAIL;
}
static int wifi_ap_init(void)
{
ap_netif = esp_netif_create_default_wifi_ap();
sta_netif = esp_netif_create_default_wifi_sta();
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t err = esp_wifi_init(&wifi_cfg);
@ -408,31 +346,24 @@ static int wifi_ap_init(void)
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
sta_event_handler, NULL, &inst);
char ap_ssid[WIFI_CFG_SSID_MAX], ap_pass[WIFI_CFG_PASS_MAX];
wifi_cfg_get_ap(ap_ssid, sizeof(ap_ssid), ap_pass, sizeof(ap_pass));
wifi_config_t ap_cfg = {
.ap = {
.ssid = WIFI_SSID,
.ssid_len = sizeof(WIFI_SSID) - 1,
.channel = WIFI_CHANNEL,
.password = WIFI_PASS,
.max_connection = WIFI_MAX_CONN,
.authmode = WIFI_AUTH_WPA2_PSK,
},
};
strncpy((char *)ap_cfg.ap.ssid, ap_ssid, sizeof(ap_cfg.ap.ssid) - 1);
ap_cfg.ap.ssid_len = strlen(ap_ssid);
strncpy((char *)ap_cfg.ap.password, ap_pass, sizeof(ap_cfg.ap.password) - 1);
esp_wifi_set_mode(WIFI_MODE_APSTA);
esp_wifi_set_config(WIFI_IF_AP, &ap_cfg);
char sta_ssid[WIFI_CFG_SSID_MAX], sta_pass[WIFI_CFG_PASS_MAX];
bool has_sta = (wifi_cfg_get_sta(sta_ssid, sizeof(sta_ssid),
sta_pass, sizeof(sta_pass)) == ESP_OK);
if (has_sta) {
if (strlen(STA_SSID) > 0) {
wifi_config_t sta_cfg = {0};
strncpy((char *)sta_cfg.sta.ssid, sta_ssid, sizeof(sta_cfg.sta.ssid) - 1);
strncpy((char *)sta_cfg.sta.password, sta_pass, sizeof(sta_cfg.sta.password) - 1);
strncpy((char *)sta_cfg.sta.ssid, STA_SSID, sizeof(sta_cfg.sta.ssid) - 1);
strncpy((char *)sta_cfg.sta.password, STA_PASS, sizeof(sta_cfg.sta.password) - 1);
sta_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
esp_wifi_set_config(WIFI_IF_STA, &sta_cfg);
}
@ -443,16 +374,12 @@ static int wifi_ap_init(void)
esp_wifi_set_ps(WIFI_PS_NONE);
esp_wifi_set_inactive_time(WIFI_IF_AP, 120);
if (has_sta) {
s_sta_state = WIFI_STATE_CONNECTING;
if (strlen(STA_SSID) > 0) {
esp_wifi_connect();
printf("WiFi: STA connecting to \"%s\"\n", sta_ssid);
} else {
printf("WiFi: no STA credentials, starting BLE provisioning\n");
ble_prov_start();
printf("WiFi: STA connecting to \"%s\"\n", STA_SSID);
}
printf("WiFi: AP \"%s\" on channel %d\n", ap_ssid, WIFI_CHANNEL);
printf("WiFi: AP \"%s\" on channel %d\n", WIFI_SSID, WIFI_CHANNEL);
return 0;
}
@ -480,7 +407,6 @@ static int udp_init(void)
int wifi_transport_init(void)
{
client_mutex = xSemaphoreCreateMutex();
s_reconnect_eg = xEventGroupCreate();
int rc = wifi_ap_init();
if (rc) {

View File

@ -2,12 +2,9 @@
#define WIFI_TRANSPORT_H
#include <stdint.h>
#include "esp_err.h"
int wifi_transport_init(void);
int wifi_send_sysex(const uint8_t *sysex, uint16_t len);
int wifi_get_client_count(void);
esp_err_t wifi_transport_reconnect_sta(const char *ssid, const char *pass);
void wifi_transport_get_sta_state(uint8_t *state, uint32_t *ip);
#endif

View File

@ -1,6 +1,3 @@
CONFIG_IDF_TARGET="esp32s3"
CONFIG_LWIP_DHCPS_MAX_STATION_NUM=12
CONFIG_ESP_WIFI_SLP_IRAM_OPT=n
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_ESP_WIFI_IRAM_OPT=n