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:
sweetymhaiske
2020-04-16 02:07:55 +05:30
committed by GitHub
parent 5508689ea3
commit 2c9b648502
8 changed files with 490 additions and 17 deletions

View File

@ -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;
}