2021-03-11 11:11:53 +01:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2020-04-15 22:37:55 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <esp_err.h>
|
|
|
|
#include <esp_wifi.h>
|
2021-04-05 13:23:58 +02:00
|
|
|
#include <esp_event.h>
|
2020-04-15 22:37:55 +02:00
|
|
|
#include <esp32-hal.h>
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
#include <nvs_flash.h>
|
|
|
|
#if CONFIG_BLUEDROID_ENABLED
|
|
|
|
#include "wifi_provisioning/scheme_ble.h"
|
|
|
|
#endif
|
2020-04-15 22:37:55 +02:00
|
|
|
#include <wifi_provisioning/scheme_softap.h>
|
|
|
|
#include <wifi_provisioning/manager.h>
|
|
|
|
#undef IPADDR_NONE
|
2020-11-18 23:12:16 +01:00
|
|
|
#include "WiFiProv.h"
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2020-11-18 23:12:16 +01:00
|
|
|
#include "SimpleBLE.h"
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2020-04-15 22:37:55 +02:00
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
bool wifiLowLevelInit(bool persistent);
|
2020-11-10 19:51:10 +01:00
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2020-04-15 22:37:55 +02:00
|
|
|
static const uint8_t custom_service_uuid[16] = { 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
|
|
|
|
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02, };
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2020-04-15 22:37:55 +02:00
|
|
|
|
2020-08-25 10:03:06 +02:00
|
|
|
#define SERV_NAME_PREFIX_PROV "PROV_"
|
2020-04-15 22:37:55 +02:00
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
static void get_device_service_name(prov_scheme_t prov_scheme, char *service_name, size_t max)
|
2020-08-25 10:03:06 +02:00
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
uint8_t eth_mac[6] = {0,0,0,0,0,0};
|
|
|
|
if(esp_wifi_get_mac((wifi_interface_t)WIFI_IF_STA, eth_mac) != ESP_OK){
|
|
|
|
log_e("esp_wifi_get_mac failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32 && defined(CONFIG_BLUEDROID_ENABLED)
|
|
|
|
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
|
|
|
snprintf(service_name, max, "%s%02X%02X%02X",SERV_NAME_PREFIX_PROV, eth_mac[3], eth_mac[4], eth_mac[5]);
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
snprintf(service_name, max, "%s%02X%02X%02X",SERV_NAME_PREFIX_PROV, eth_mac[3], eth_mac[4], eth_mac[5]);
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32 && defined(CONFIG_BLUEDROID_ENABLED)
|
|
|
|
}
|
|
|
|
#endif
|
2020-08-25 10:03:06 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
static esp_err_t custom_prov_data_handler(uint32_t session_id, const uint8_t *inbuf, ssize_t inlen, uint8_t **outbuf, ssize_t *outlen, void *priv_data){
|
|
|
|
if (inbuf) {
|
|
|
|
log_d("Received data: %.*s", inlen, (char *)inbuf);
|
|
|
|
}
|
|
|
|
*outbuf = NULL;
|
|
|
|
*outlen = 0;
|
|
|
|
return ESP_OK;
|
2020-08-25 10:03:06 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
void WiFiProvClass :: beginProvision(prov_scheme_t prov_scheme, scheme_handler_t scheme_handler, wifi_prov_security_t security, const char * pop, const char *service_name, const char *service_key, uint8_t * uuid)
|
2020-04-15 22:37:55 +02:00
|
|
|
{
|
2021-04-05 13:23:58 +02:00
|
|
|
bool provisioned = false;
|
|
|
|
static char service_name_temp[32];
|
2020-04-15 22:37:55 +02:00
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
wifi_prov_mgr_config_t config;
|
|
|
|
#if CONFIG_BLUEDROID_ENABLED
|
|
|
|
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
|
|
|
config.scheme = wifi_prov_scheme_ble;
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
config.scheme = wifi_prov_scheme_softap;
|
|
|
|
#if CONFIG_BLUEDROID_ENABLED
|
|
|
|
}
|
2020-04-15 22:37:55 +02:00
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_NONE){
|
|
|
|
#endif
|
|
|
|
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE;
|
|
|
|
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
|
|
|
#if CONFIG_BLUEDROID_ENABLED
|
|
|
|
} else if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_FREE_BTDM){
|
|
|
|
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM;
|
|
|
|
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
|
|
|
} else if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_FREE_BT){
|
|
|
|
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BT;
|
|
|
|
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
|
|
|
} else if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_FREE_BLE){
|
|
|
|
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE;
|
|
|
|
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
|
|
|
} else {
|
|
|
|
log_e("Unknown scheme handler!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
config.app_event_handler.event_cb = NULL;
|
|
|
|
config.app_event_handler.user_data = NULL;
|
|
|
|
wifiLowLevelInit(true);
|
|
|
|
if(wifi_prov_mgr_init(config) != ESP_OK){
|
|
|
|
log_e("wifi_prov_mgr_init failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(wifi_prov_mgr_is_provisioned(&provisioned) != ESP_OK){
|
|
|
|
log_e("wifi_prov_mgr_is_provisioned failed!");
|
|
|
|
wifi_prov_mgr_deinit();
|
|
|
|
return;
|
|
|
|
}
|
2020-04-15 22:37:55 +02:00
|
|
|
if(provisioned == false) {
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_BLUEDROID_ENABLED
|
2020-04-15 22:37:55 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2020-04-15 22:37:55 +02:00
|
|
|
|
|
|
|
if(service_name == NULL) {
|
2021-04-05 13:23:58 +02:00
|
|
|
get_device_service_name(prov_scheme, service_name_temp, 32);
|
2020-04-15 22:37:55 +02:00
|
|
|
service_name = (const char *)service_name_temp;
|
|
|
|
}
|
|
|
|
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_BLUEDROID_ENABLED
|
2020-04-15 22:37:55 +02:00
|
|
|
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
2021-04-05 13:23:58 +02:00
|
|
|
log_i("Starting AP using BLE. service_name : %s, pop : %s",service_name,pop);
|
2020-04-15 22:37:55 +02:00
|
|
|
} else {
|
2021-04-05 13:23:58 +02:00
|
|
|
#endif
|
2020-04-15 22:37:55 +02:00
|
|
|
if(service_key == NULL) {
|
2021-04-05 13:23:58 +02:00
|
|
|
log_i("Starting provisioning AP using SOFTAP. service_name : %s, pop : %s",service_name,pop);
|
|
|
|
} else {
|
|
|
|
log_i("Starting provisioning AP using SOFTAP. service_name : %s, password : %s, pop : %s",service_name,service_key,pop);
|
2020-04-15 22:37:55 +02:00
|
|
|
}
|
2021-04-05 13:23:58 +02:00
|
|
|
#if CONFIG_BLUEDROID_ENABLED
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key) != ESP_OK){
|
|
|
|
log_e("wifi_prov_mgr_start_provisioning failed!");
|
|
|
|
return;
|
2020-04-15 22:37:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-05 13:23:58 +02:00
|
|
|
log_i("Already Provisioned");
|
2020-04-21 13:39:26 +02:00
|
|
|
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
|
2021-04-05 13:23:58 +02:00
|
|
|
static wifi_config_t conf;
|
|
|
|
esp_wifi_get_config((wifi_interface_t)WIFI_IF_STA,&conf);
|
|
|
|
log_i("Attempting connect to AP: %s\n",conf.sta.ssid);
|
2020-04-21 13:39:26 +02:00
|
|
|
#endif
|
2021-04-05 13:23:58 +02:00
|
|
|
esp_wifi_start();
|
|
|
|
wifi_prov_mgr_deinit();
|
|
|
|
WiFi.begin();
|
2020-04-15 22:37:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 13:23:58 +02:00
|
|
|
|
2020-11-18 23:12:16 +01:00
|
|
|
WiFiProvClass WiFiProv;
|