forked from espressif/arduino-esp32
Add support of unified provisioning to Arduino
1. WiFiProv.ino sketch is added that allows arduino users to do provisioning via SoftAP or BLE. WiFi.beginProvision( ) API is designed for provisioning in Arduino. 2. In WiFiProv.h provisioning class is defined. 3. WiFiProv.cpp contains implementation for provisioning class. 4. README.md file is added which contains detail information for working.
This commit is contained in:
@ -37,8 +37,9 @@
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiServer.h"
|
||||
#include "WiFiUdp.h"
|
||||
#include "WiFiProv.h"
|
||||
|
||||
class WiFiClass : public WiFiGenericClass, public WiFiSTAClass, public WiFiScanClass, public WiFiAPClass
|
||||
class WiFiClass : public WiFiGenericClass, public WiFiSTAClass, public WiFiScanClass, public WiFiAPClass, public WiFiProvClass
|
||||
{
|
||||
public:
|
||||
using WiFiGenericClass::channel;
|
||||
@ -54,8 +55,7 @@ public:
|
||||
using WiFiScanClass::BSSID;
|
||||
using WiFiScanClass::BSSIDstr;
|
||||
using WiFiScanClass::channel;
|
||||
|
||||
public:
|
||||
public:
|
||||
void printDiag(Print& dest);
|
||||
friend class WiFiClient;
|
||||
friend class WiFiServer;
|
||||
|
@ -42,7 +42,6 @@ extern "C" {
|
||||
#include "lwip/dns.h"
|
||||
#include "esp_ipc.h"
|
||||
|
||||
|
||||
} //extern "C"
|
||||
|
||||
#include "esp32-hal-log.h"
|
||||
@ -53,20 +52,42 @@ static xQueueHandle _network_event_queue;
|
||||
static TaskHandle_t _network_event_task_handle = NULL;
|
||||
static EventGroupHandle_t _network_event_group = NULL;
|
||||
|
||||
esp_err_t postToSysQueue(system_prov_event_t *data)
|
||||
{
|
||||
if (xQueueSend(_network_event_queue, &data, portMAX_DELAY) != pdPASS) {
|
||||
log_w("Network Event Queue Send Failed!");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void _network_event_task(void * arg){
|
||||
system_event_t event;
|
||||
system_prov_event_t *data;
|
||||
for (;;) {
|
||||
if(xQueueReceive(_network_event_queue, &event, portMAX_DELAY) == pdTRUE){
|
||||
WiFiGenericClass::_eventCallback(arg, &event);
|
||||
}
|
||||
if(xQueueReceive(_network_event_queue, &data, portMAX_DELAY) == pdTRUE){
|
||||
if(data->prov_event != NULL){
|
||||
WiFiGenericClass::_eventCallback(arg, data->sys_event, data->prov_event);
|
||||
free(data->sys_event);
|
||||
free(data->prov_event);
|
||||
} else {
|
||||
WiFiGenericClass::_eventCallback(arg, data->sys_event, NULL);
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
_network_event_task_handle = NULL;
|
||||
}
|
||||
|
||||
static esp_err_t _network_event_cb(void *arg, system_event_t *event){
|
||||
if (xQueueSend(_network_event_queue, event, portMAX_DELAY) != pdPASS) {
|
||||
log_w("Network Event Queue Send Failed!");
|
||||
static esp_err_t _network_event_cb(void *arg, system_event_t *event){
|
||||
system_prov_event_t *sys_prov_data = (system_prov_event_t *)malloc(sizeof(system_prov_event_t));
|
||||
if(sys_prov_data == NULL) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
sys_prov_data->sys_event = event;
|
||||
sys_prov_data->prov_event = NULL;
|
||||
if (postToSysQueue(sys_prov_data) != ESP_OK){
|
||||
free(sys_prov_data);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
@ -82,7 +103,7 @@ static bool _start_network_event_task(){
|
||||
xEventGroupSetBits(_network_event_group, WIFI_DNS_IDLE_BIT);
|
||||
}
|
||||
if(!_network_event_queue){
|
||||
_network_event_queue = xQueueCreate(32, sizeof(system_event_t));
|
||||
_network_event_queue = xQueueCreate(32, sizeof(system_prov_event_t));
|
||||
if(!_network_event_queue){
|
||||
log_e("Network Event Queue Create Failed!");
|
||||
return false;
|
||||
@ -144,7 +165,7 @@ static bool espWiFiStart(){
|
||||
_esp_wifi_started = true;
|
||||
system_event_t event;
|
||||
event.event_id = SYSTEM_EVENT_WIFI_READY;
|
||||
WiFiGenericClass::_eventCallback(nullptr, &event);
|
||||
WiFiGenericClass::_eventCallback(nullptr, &event, NULL);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -173,9 +194,10 @@ typedef struct WiFiEventCbList {
|
||||
WiFiEventCb cb;
|
||||
WiFiEventFuncCb fcb;
|
||||
WiFiEventSysCb scb;
|
||||
WiFiProvEventCb provcb;
|
||||
system_event_id_t event;
|
||||
|
||||
WiFiEventCbList() : id(current_id++), cb(NULL), fcb(NULL), scb(NULL), event(SYSTEM_EVENT_WIFI_READY) {}
|
||||
WiFiEventCbList() : id(current_id++), cb(NULL), fcb(NULL), scb(NULL), provcb(NULL), event(SYSTEM_EVENT_WIFI_READY) {}
|
||||
} WiFiEventCbList_t;
|
||||
wifi_event_id_t WiFiEventCbList::current_id = 1;
|
||||
|
||||
@ -230,6 +252,20 @@ int WiFiGenericClass::waitStatusBits(int bits, uint32_t timeout_ms){
|
||||
* @param cbEvent WiFiEventCb
|
||||
* @param event optional filter (WIFI_EVENT_MAX is all events)
|
||||
*/
|
||||
wifi_event_id_t WiFiGenericClass::onEvent(WiFiProvEventCb cbEvent, system_event_id_t event)
|
||||
{
|
||||
if(!cbEvent){
|
||||
return 0;
|
||||
}
|
||||
WiFiEventCbList_t newEventHandler;
|
||||
newEventHandler.cb = NULL;
|
||||
newEventHandler.fcb = NULL;
|
||||
newEventHandler.scb = NULL;
|
||||
newEventHandler.provcb = cbEvent;
|
||||
newEventHandler.event = event;
|
||||
cbEventList.push_back(newEventHandler);
|
||||
return newEventHandler.id;
|
||||
}
|
||||
wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event)
|
||||
{
|
||||
if(!cbEvent) {
|
||||
@ -239,6 +275,7 @@ wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t
|
||||
newEventHandler.cb = cbEvent;
|
||||
newEventHandler.fcb = NULL;
|
||||
newEventHandler.scb = NULL;
|
||||
newEventHandler.provcb = NULL;
|
||||
newEventHandler.event = event;
|
||||
cbEventList.push_back(newEventHandler);
|
||||
return newEventHandler.id;
|
||||
@ -253,6 +290,7 @@ wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventFuncCb cbEvent, system_event_
|
||||
newEventHandler.cb = NULL;
|
||||
newEventHandler.fcb = cbEvent;
|
||||
newEventHandler.scb = NULL;
|
||||
newEventHandler.provcb = NULL;
|
||||
newEventHandler.event = event;
|
||||
cbEventList.push_back(newEventHandler);
|
||||
return newEventHandler.id;
|
||||
@ -267,6 +305,7 @@ wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_i
|
||||
newEventHandler.cb = NULL;
|
||||
newEventHandler.fcb = NULL;
|
||||
newEventHandler.scb = cbEvent;
|
||||
newEventHandler.provcb = NULL;
|
||||
newEventHandler.event = event;
|
||||
cbEventList.push_back(newEventHandler);
|
||||
return newEventHandler.id;
|
||||
@ -326,8 +365,11 @@ const char * system_event_names[] = { "WIFI_READY", "SCAN_DONE", "STA_START", "S
|
||||
const char * system_event_reasons[] = { "UNSPECIFIED", "AUTH_EXPIRE", "AUTH_LEAVE", "ASSOC_EXPIRE", "ASSOC_TOOMANY", "NOT_AUTHED", "NOT_ASSOCED", "ASSOC_LEAVE", "ASSOC_NOT_AUTHED", "DISASSOC_PWRCAP_BAD", "DISASSOC_SUPCHAN_BAD", "UNSPECIFIED", "IE_INVALID", "MIC_FAILURE", "4WAY_HANDSHAKE_TIMEOUT", "GROUP_KEY_UPDATE_TIMEOUT", "IE_IN_4WAY_DIFFERS", "GROUP_CIPHER_INVALID", "PAIRWISE_CIPHER_INVALID", "AKMP_INVALID", "UNSUPP_RSN_IE_VERSION", "INVALID_RSN_IE_CAP", "802_1X_AUTH_FAILED", "CIPHER_SUITE_REJECTED", "BEACON_TIMEOUT", "NO_AP_FOUND", "AUTH_FAIL", "ASSOC_FAIL", "HANDSHAKE_TIMEOUT", "CONNECTION_FAIL" };
|
||||
#define reason2str(r) ((r>176)?system_event_reasons[r-176]:system_event_reasons[r-1])
|
||||
#endif
|
||||
esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
|
||||
esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event, wifi_prov_event_t *prov_event)
|
||||
{
|
||||
if(WiFi.isProvEnabled()) {
|
||||
wifi_prov_mgr_event_handler(arg,event);
|
||||
}
|
||||
if(event->event_id < 26) {
|
||||
log_d("Event: %d - %s", event->event_id, system_event_names[event->event_id]);
|
||||
}
|
||||
@ -422,7 +464,7 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
|
||||
setStatusBits(ETH_CONNECTED_BIT | ETH_HAS_IP6_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
||||
WiFiEventCbList_t entry = cbEventList[i];
|
||||
if(entry.cb || entry.fcb || entry.scb) {
|
||||
@ -436,6 +478,10 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(entry.provcb) {
|
||||
entry.provcb(event,prov_event);
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
@ -27,10 +27,25 @@
|
||||
#include <esp_event_loop.h>
|
||||
#include <functional>
|
||||
#include "WiFiType.h"
|
||||
#include "IPAddress.h"
|
||||
#include <wifi_provisioning/manager.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
wifi_prov_cb_event_t event;
|
||||
void *event_data;
|
||||
}wifi_prov_event_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
wifi_prov_event_t *prov_event;
|
||||
system_event_t *sys_event;
|
||||
}system_prov_event_t;
|
||||
|
||||
typedef void (*WiFiEventCb)(system_event_id_t event);
|
||||
typedef std::function<void(system_event_id_t event, system_event_info_t info)> WiFiEventFuncCb;
|
||||
typedef void (*WiFiEventSysCb)(system_event_t *event);
|
||||
typedef void (*WiFiProvEventCb)(system_event_t *sys_event, wifi_prov_event_t *prov_event);
|
||||
|
||||
typedef size_t wifi_event_id_t;
|
||||
|
||||
@ -73,6 +88,7 @@ class WiFiGenericClass
|
||||
wifi_event_id_t onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
wifi_event_id_t onEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
wifi_event_id_t onEvent(WiFiProvEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
void removeEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
void removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
void removeEvent(wifi_event_id_t id);
|
||||
@ -97,7 +113,7 @@ class WiFiGenericClass
|
||||
bool setTxPower(wifi_power_t power);
|
||||
wifi_power_t getTxPower();
|
||||
|
||||
static esp_err_t _eventCallback(void *arg, system_event_t *event);
|
||||
static esp_err_t _eventCallback(void *arg, system_event_t *event, wifi_prov_event_t *prov_event);
|
||||
|
||||
protected:
|
||||
static bool _persistent;
|
||||
|
157
libraries/WiFi/src/WiFiProv.cpp
Normal file
157
libraries/WiFi/src/WiFiProv.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
WiFiProv.cpp - WiFiProv class for provisioning
|
||||
All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event_loop.h>
|
||||
#include <esp32-hal.h>
|
||||
|
||||
#include <nvs_flash.h>
|
||||
#include <wifi_provisioning/scheme_ble.h>
|
||||
#include <wifi_provisioning/scheme_softap.h>
|
||||
#include <wifi_provisioning/manager.h>
|
||||
#undef IPADDR_NONE
|
||||
#include "WiFi.h"
|
||||
|
||||
extern esp_err_t postToSysQueue(system_prov_event_t *);
|
||||
static const uint8_t custom_service_uuid[16] = { 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
|
||||
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02, };
|
||||
|
||||
#define SERV_NAME_PREFIX_BLE "BLE_"
|
||||
#define SERV_NAME_PREFIX_WIFI "WIFI_"
|
||||
|
||||
bool WiFiProvClass::prov_enable = true;
|
||||
|
||||
bool WiFiProvClass::isProvEnabled()
|
||||
{
|
||||
return prov_enable;
|
||||
}
|
||||
|
||||
static void prov_event_handler(void *user_data, wifi_prov_cb_event_t event, void *event_data)
|
||||
{
|
||||
if (!event) {
|
||||
return;
|
||||
}
|
||||
|
||||
system_prov_event_t *sys_prov = (system_prov_event_t *)malloc(sizeof(system_prov_event_t));
|
||||
if(sys_prov == NULL) {
|
||||
log_e("Malloc Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
sys_prov->prov_event = (wifi_prov_event_t *)malloc(sizeof(wifi_prov_event_t));
|
||||
if(sys_prov->prov_event == NULL) {
|
||||
log_e("Malloc Failed");
|
||||
free(sys_prov);
|
||||
return;
|
||||
}
|
||||
|
||||
sys_prov->sys_event = (system_event_t *)malloc(sizeof(system_event_t));
|
||||
if(sys_prov->sys_event == NULL) {
|
||||
log_e("Malloc Failed");
|
||||
free(sys_prov->prov_event);
|
||||
free(sys_prov);
|
||||
return;
|
||||
}
|
||||
|
||||
sys_prov->prov_event->event = event;
|
||||
sys_prov->prov_event->event_data = event_data;
|
||||
sys_prov->sys_event->event_id = SYSTEM_EVENT_MAX;
|
||||
esp_err_t check = postToSysQueue(sys_prov);
|
||||
if(check == ESP_FAIL) {
|
||||
log_e("Provisioning event not send to queue");
|
||||
free(sys_prov->sys_event);
|
||||
free(sys_prov->prov_event);
|
||||
free(sys_prov);
|
||||
}
|
||||
}
|
||||
|
||||
static void get_device_service_name(scheme prov_scheme, char *service_name, size_t max)
|
||||
{
|
||||
uint8_t eth_mac[6];
|
||||
WiFi.macAddress(eth_mac);
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
snprintf(service_name, max, "%s%02X%02X%02X",SERV_NAME_PREFIX_BLE, eth_mac[3], eth_mac[4], eth_mac[5]);
|
||||
} else {
|
||||
snprintf(service_name, max, "%s%02X%02X%02X",SERV_NAME_PREFIX_WIFI, eth_mac[3], eth_mac[4], eth_mac[5]);
|
||||
}
|
||||
}
|
||||
|
||||
void WiFiProvClass :: beginProvision(scheme prov_scheme, wifi_prov_event_handler_t scheme_event_handler, wifi_prov_security_t security, const char * pop, const char *service_name, const char *service_key, uint8_t * uuid)
|
||||
{
|
||||
prov_enable = true;
|
||||
bool provisioned = false;
|
||||
wifi_prov_mgr_config_t config;
|
||||
config.scheme_event_handler = scheme_event_handler;
|
||||
config.app_event_handler = {
|
||||
.event_cb = prov_event_handler,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
config.scheme = wifi_prov_scheme_ble;
|
||||
} else {
|
||||
config.scheme = wifi_prov_scheme_softap;
|
||||
}
|
||||
|
||||
wifi_prov_mgr_init(config);
|
||||
WiFi.mode(WIFI_MODE_AP);
|
||||
wifi_prov_mgr_is_provisioned(&provisioned);
|
||||
if(provisioned == false) {
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
service_key = NULL;
|
||||
if(uuid == NULL) {
|
||||
uuid=(uint8_t *)custom_service_uuid;
|
||||
}
|
||||
wifi_prov_scheme_ble_set_service_uuid(uuid);
|
||||
}
|
||||
|
||||
if(service_name == NULL) {
|
||||
char service_name_temp[12];
|
||||
get_device_service_name(prov_scheme,service_name_temp,sizeof(service_name_temp));
|
||||
service_name = (const char *)service_name_temp;
|
||||
}
|
||||
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
log_i("Starting AP using BLE\n service_name : %s\n pop : %s",service_name,pop);
|
||||
|
||||
} else {
|
||||
if(service_key == NULL) {
|
||||
log_i("Starting AP using SOFTAP\n service_name : %s\n pop : %s",service_name,pop);
|
||||
} else {
|
||||
log_i("Starting AP using SOFTAP\n service_name : %s\n password : %s\n pop : %s",service_name,service_key,pop);
|
||||
}
|
||||
}
|
||||
|
||||
wifi_prov_mgr_start_provisioning(security,pop,service_name,service_key);
|
||||
|
||||
} else {
|
||||
wifi_prov_mgr_deinit();
|
||||
WiFi.mode(WIFI_MODE_STA);
|
||||
log_i("Aleardy Provisioned, starting Wi-Fi STA");
|
||||
log_i("CONNECTING ACCESS POINT CREDENTIALS : ");
|
||||
log_i("SSID : %s\n",WiFi.SSID().c_str());
|
||||
}
|
||||
}
|
||||
|
55
libraries/WiFi/src/WiFiProv.h
Normal file
55
libraries/WiFi/src/WiFiProv.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
WiFiProv.h - Base class for provisioning support
|
||||
All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "wifi_provisioning/manager.h"
|
||||
#include "wifi_provisioning/scheme_ble.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "SimpleBLE.h"
|
||||
//Select the scheme using which you want to provision
|
||||
enum scheme
|
||||
{
|
||||
WIFI_PROV_SCHEME_BLE,
|
||||
WIFI_PROV_SCHEME_SOFTAP,
|
||||
WIFI_PROV_SCHEME_CONSOLE
|
||||
};
|
||||
//Provisioning class
|
||||
class WiFiProvClass
|
||||
{
|
||||
protected:
|
||||
static bool prov_enable;
|
||||
|
||||
public:
|
||||
WiFiProvClass() {
|
||||
prov_enable = false;
|
||||
}
|
||||
|
||||
bool isProvEnabled();
|
||||
|
||||
void beginProvision(scheme prov_scheme = WIFI_PROV_SCHEME_SOFTAP, wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE, wifi_prov_security_t security = WIFI_PROV_SECURITY_1, const char * pop = "abcd1234", const char * service_name = NULL, const char * service_key = NULL, uint8_t *uuid = NULL);
|
||||
};
|
||||
|
||||
/*
|
||||
Event Handler for BLE
|
||||
- WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
|
||||
- WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE
|
||||
- WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BT
|
||||
Event Handler for SOFTAP
|
||||
- WIFI_PROV_EVENT_HANDLER_NONE
|
||||
*/
|
Reference in New Issue
Block a user