provisioning example, waitForProvisioning()

This commit is contained in:
Manuel Bleichenbacher
2018-07-23 14:30:03 +02:00
parent b179ce6884
commit 3e084a4ed9
6 changed files with 142 additions and 12 deletions

View File

@ -87,6 +87,21 @@ void TheThingsNetwork::startProvisioningTask()
#endif
}
void TheThingsNetwork::waitForProvisioning()
{
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
if (isProvisioned())
{
ESP_LOGI(TAG, "Device is already provisioned");
return;
}
while (!provisioning_have_keys())
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Device successfully provisioned");
#endif
}
bool TheThingsNetwork::join(const char *devEui, const char *appEui, const char *appKey)
{

View File

@ -245,32 +245,38 @@ bool provisioning_have_keys()
bool provisioning_decode_keys(const char *dev_eui, const char *app_eui, const char *app_key)
{
have_keys = false;
uint8_t buf_dev_eui[8];
uint8_t buf_app_eui[8];
uint8_t buf_app_key[16];
if (strlen(dev_eui) != 16 || !hex_str_to_bin(dev_eui, global_dev_eui, 8))
if (strlen(dev_eui) != 16 || !hex_str_to_bin(dev_eui, buf_dev_eui, 8))
{
ESP_LOGW(TAG, "Invalid device EUI: %s", dev_eui);
return false;
}
swap_bytes(global_dev_eui, 8);
swap_bytes(buf_dev_eui, 8);
if (strlen(app_eui) != 16 || !hex_str_to_bin(app_eui, global_app_eui, 8))
if (strlen(app_eui) != 16 || !hex_str_to_bin(app_eui, buf_app_eui, 8))
{
ESP_LOGW(TAG, "Invalid application EUI: %s", app_eui);
return false;
}
swap_bytes(global_app_eui, 8);
swap_bytes(buf_app_eui, 8);
if (strlen(app_key) != 32 || !hex_str_to_bin(app_key, global_app_key, 16))
if (strlen(app_key) != 32 || !hex_str_to_bin(app_key, buf_app_key, 16))
{
ESP_LOGW(TAG, "Invalid application key: %s", app_key);
return false;
}
have_keys = true;
return true;
memcpy(global_dev_eui, buf_dev_eui, sizeof(global_dev_eui));
memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui));
memcpy(global_app_key, buf_app_key, sizeof(global_app_key));
have_keys = provisioning_save_keys();
return have_keys;
}
@ -313,7 +319,9 @@ done:
bool provisioning_restore_keys(bool silent)
{
have_keys = false;
uint8_t buf_dev_eui[8];
uint8_t buf_app_eui[8];
uint8_t buf_app_key[16];
nvs_handle handle = 0;
esp_err_t res = nvs_open(NVS_FLASH_PARTITION, NVS_READONLY, &handle);
@ -328,15 +336,18 @@ bool provisioning_restore_keys(bool silent)
if (res != ESP_OK)
goto done;
if (!read_nvs_value(handle, NVS_FLASH_KEY_DEV_EUI, global_dev_eui, sizeof(global_dev_eui), silent))
if (!read_nvs_value(handle, NVS_FLASH_KEY_DEV_EUI, buf_dev_eui, sizeof(global_dev_eui), silent))
goto done;
if (!read_nvs_value(handle, NVS_FLASH_KEY_APP_EUI, global_app_eui, sizeof(global_app_eui), silent))
if (!read_nvs_value(handle, NVS_FLASH_KEY_APP_EUI, buf_app_eui, sizeof(global_app_eui), silent))
goto done;
if (!read_nvs_value(handle, NVS_FLASH_KEY_APP_KEY, global_app_key, sizeof(global_app_key), silent))
if (!read_nvs_value(handle, NVS_FLASH_KEY_APP_KEY, buf_app_key, sizeof(global_app_key), silent))
goto done;
memcpy(global_dev_eui, buf_dev_eui, sizeof(global_dev_eui));
memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui));
memcpy(global_app_key, buf_app_key, sizeof(global_app_key));
have_keys = true;
ESP_LOGI(TAG, "Dev and app EUI and app key have been restored from NVS storage");