Skip to content

Commit

Permalink
concept of providing more data than wifi creds
Browse files Browse the repository at this point in the history
  • Loading branch information
qqq authored and eug-ev committed Sep 3, 2024
1 parent e0991fa commit afe24c5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/wifi_provisioning/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS include
PRIV_INCLUDE_DIRS src proto-c
REQUIRES lwip protocomm
REQUIRES lwip protocomm mbedtls nvs_flash
PRIV_REQUIRES protobuf-c bt json esp_timer esp_wifi)
61 changes: 58 additions & 3 deletions components/wifi_provisioning/src/wifi_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <esp_err.h>
#include <esp_log.h>

#include "mbedtls/base64.h"
#include "nvs.h"

#include "wifi_constants.pb-c.h"
#include "wifi_config.pb-c.h"

Expand Down Expand Up @@ -133,6 +136,54 @@ static esp_err_t cmd_get_status_handler(WiFiConfigPayload *req,
return ESP_OK;
}

static int PrepareForDataAndReturnDataLen(ProtobufCBinaryData* passData)
{
assert(passData->len >= 8);
const char check [] = "!@CDATA_";

if ((uint64_t)*passData->data != (uint64_t)*check)
{
return -1;
}
assert(passData->len >= 16);

uint32_t customDataLength;
size_t bytesWritten;
mbedtls_base64_decode((unsigned char*)&customDataLength, sizeof(customDataLength), &bytesWritten, passData->data + sizeof(uint64_t), 8);
ESP_LOGI(TAG, "GetPassCustomDataIndex size = %"PRIu32"", customDataLength);
passData->data = passData->data + 16;
passData->len -= 16;
return customDataLength;
}

static void ExtractCustomDataFromPass(ProtobufCBinaryData* passData)
{
int dataLen = PrepareForDataAndReturnDataLen(passData);
if (dataLen < 0)
return;

nvs_handle_t nvsHandle = 0;
esp_err_t err;

err = nvs_open("prov_custom", NVS_READWRITE, &nvsHandle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error opening NVS");
goto exit;
}
nvs_set_blob(nvsHandle, "data", passData->data, dataLen);

err = nvs_commit(nvsHandle);

exit:
if (nvsHandle != 0)
{
nvs_close(nvsHandle);
}

passData->data = passData->data + dataLen;
passData->len -= dataLen;
}

static esp_err_t cmd_set_config_handler(WiFiConfigPayload *req,
WiFiConfigPayload *resp, void *priv_data)
{
Expand All @@ -153,6 +204,10 @@ static esp_err_t cmd_set_config_handler(WiFiConfigPayload *req,
wifi_prov_config_set_data_t req_data;
memset(&req_data, 0, sizeof(req_data));


ProtobufCBinaryData passData = req->cmd_set_config->passphrase;
ExtractCustomDataFromPass(&passData);

/* Check arguments provided in protobuf packet:
* - SSID / Passphrase string length must be within the standard limits
* - BSSID must either be NULL or have length equal to that imposed by the standard
Expand All @@ -164,16 +219,16 @@ static esp_err_t cmd_set_config_handler(WiFiConfigPayload *req,
ESP_LOGD(TAG, "Received invalid BSSID");
} else if (req->cmd_set_config->ssid.len >= sizeof(req_data.ssid)) {
ESP_LOGD(TAG, "Received invalid SSID");
} else if (req->cmd_set_config->passphrase.len >= sizeof(req_data.password)) {
} else if (passData.len >= sizeof(req_data.password)) {
ESP_LOGD(TAG, "Received invalid Passphrase");
} else {
/* The received SSID and Passphrase are not NULL terminated so
* we memcpy over zeroed out arrays. Above length checks ensure
* that there is atleast 1 extra byte for null termination */
memcpy(req_data.ssid, req->cmd_set_config->ssid.data,
req->cmd_set_config->ssid.len);
memcpy(req_data.password, req->cmd_set_config->passphrase.data,
req->cmd_set_config->passphrase.len);
memcpy(req_data.password, passData.data,
passData.len);
memcpy(req_data.bssid, req->cmd_set_config->bssid.data,
req->cmd_set_config->bssid.len);
req_data.channel = req->cmd_set_config->channel;
Expand Down

0 comments on commit afe24c5

Please sign in to comment.