Skip to content

Commit

Permalink
Merge branch 'task/random_ssid_pop' into 'master'
Browse files Browse the repository at this point in the history
app_wifi: Changes in SSID and PoP generation for Provisioning

See merge request app-frameworks/esp-rainmaker!189
  • Loading branch information
shahpiyushv committed Oct 16, 2020
2 parents 3614972 + fd50262 commit d3f0e64
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changes

## 16-Oct-2020 (app_wifi: Changes in SSID and PoP generation for Provisioning)

The PoP for Wi-Fi provisioning was being fetched from a random 8 character hex string stored in the fctry partition.
In this commit, the random 8 character hex string has been replaced by 64 byte random number, which can be used for other purposes as well.
PoP is now generated by reading the first 4 bytes of this and converting to 8 character hex string.
Even the SSID now uses the last 3 bytes of this random number as the suffix, instead of last 3 bytes of MAC address.
With this change, it will now be possible to generate the complete Provisioning QR code payload outside the device,
without having to know its MAC address.

## 29-Sep-2020 (esp_rmaker_standard_types: Start default names of all standard params with capital letter)

Default parameter names like name, power, etc. have been changed to Name, Power, etc. respectively, so that they look better in the phone app UIs.
Expand Down
4 changes: 2 additions & 2 deletions cli/rmaker_tools/rmaker_claim/claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def gen_host_csr(private_key, common_name=None):
csr = request.public_bytes(serialization.Encoding.PEM).decode("utf-8")
return csr

def gen_hex_str(octets=4):
def gen_hex_str(octets=64):
"""
Generate random hex string, it is used as PoP
Expand Down Expand Up @@ -188,7 +188,7 @@ def save_random_hex_str(dest_filedir, hex_str):
info_file.write(hex_str)

with open(dest_filedir + 'node_info.csv', 'a') as info_file:
info_file.write('random,file,binary,' +
info_file.write('random,file,hex2bin,' +
dest_filedir + 'random.info')
info_file.write('\n')
except Exception as err:
Expand Down
16 changes: 8 additions & 8 deletions components/esp_rainmaker/src/core/esp_rmaker_claim.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
#include "esp_rmaker_claim.h"

static const char *TAG = "esp_claim";

#define ESP_RMAKER_RANDOM_NUMBER_LEN 64

#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM
#include "soc/soc.h"
#include "soc/efuse_reg.h"
Expand Down Expand Up @@ -785,8 +788,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
#endif /* CONFIG_ESP_RMAKER_ASSISTED_CLAIM */
esp_err_t __esp_rmaker_claim_init(esp_rmaker_claim_data_t *claim_data)
{
char hexstr[9];
uint32_t my_random;
uint8_t random_bytes[ESP_RMAKER_RANDOM_NUMBER_LEN];
esp_err_t err;

char *key = esp_rmaker_get_client_key();
Expand All @@ -812,15 +814,13 @@ esp_err_t __esp_rmaker_claim_init(esp_rmaker_claim_data_t *claim_data)
return err;
}
}
/* Generate random hex string */
memset(hexstr, 0, sizeof(hexstr));
esp_fill_random(&my_random, sizeof(my_random));
snprintf(hexstr, sizeof(hexstr), "%08x", my_random);
/* Generate random bytes for general purpose use */
esp_fill_random(&random_bytes, sizeof(random_bytes));

/* Store the PoP in the storage */
err = esp_rmaker_storage_set(ESP_RMAKER_CLIENT_RANDOM_NVS_KEY, hexstr, strlen(hexstr));
err = esp_rmaker_storage_set(ESP_RMAKER_CLIENT_RANDOM_NVS_KEY, random_bytes, sizeof(random_bytes));
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to store random number to storage");
ESP_LOGE(TAG, "Failed to store random bytes to storage.");
return err;
}
#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM
Expand Down
64 changes: 37 additions & 27 deletions examples/common/app_wifi/app_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,44 +120,55 @@ static void wifi_init_sta()
ESP_ERROR_CHECK(esp_wifi_start());
}

static void get_device_service_name(char *service_name, size_t max)
{
uint8_t eth_mac[6];
const char *ssid_prefix = "PROV_";
esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
snprintf(service_name, max, "%s%02X%02X%02X",
ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
}

/* free the return value after use. */
static char *read_random_bytes_from_nvs()
static esp_err_t read_random_bytes_from_nvs(uint8_t **random_bytes, size_t *len)
{
nvs_handle handle;
esp_err_t err;
size_t required_size = 0;
void *value;
*len = 0;

if ((err = nvs_open_from_partition(CONFIG_ESP_RMAKER_FACTORY_PARTITION_NAME, CREDENTIALS_NAMESPACE,
NVS_READONLY, &handle)) != ESP_OK) {
ESP_LOGD(TAG, "NVS open for %s %s %s failed with error %d", CONFIG_ESP_RMAKER_FACTORY_PARTITION_NAME, CREDENTIALS_NAMESPACE, RANDOM_NVS_KEY, err);
return NULL;
return ESP_FAIL;
}

if ((err = nvs_get_blob(handle, RANDOM_NVS_KEY, NULL, &required_size)) != ESP_OK) {
ESP_LOGD(TAG, "Failed to read key %s with error %d size %d", RANDOM_NVS_KEY, err, required_size);
if ((err = nvs_get_blob(handle, RANDOM_NVS_KEY, NULL, len)) != ESP_OK) {
ESP_LOGD(TAG, "Error %d. Failed to read key %s.", err, RANDOM_NVS_KEY);
nvs_close(handle);
return NULL;
return ESP_ERR_NOT_FOUND;
}

value = calloc(required_size + 1, 1); /* + 1 for NULL termination */
if (value) {
nvs_get_blob(handle, RANDOM_NVS_KEY, value, &required_size);
*random_bytes = calloc(*len, 1);
if (*random_bytes) {
nvs_get_blob(handle, RANDOM_NVS_KEY, *random_bytes, len);
nvs_close(handle);
return ESP_OK;
}

nvs_close(handle);
return value;
return ESP_ERR_NO_MEM;
}

static esp_err_t get_device_service_name(char *service_name, size_t max)
{
uint8_t *nvs_random;
const char *ssid_prefix = "PROV_";
size_t nvs_random_size = 0;
if ((read_random_bytes_from_nvs(&nvs_random, &nvs_random_size) != ESP_OK) || nvs_random_size < 3) {
uint8_t eth_mac[6];
esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
snprintf(service_name, max, "%s%02x%02x%02x", ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
} else {
snprintf(service_name, max, "%s%02x%02x%02x", ssid_prefix, nvs_random[nvs_random_size - 3],
nvs_random[nvs_random_size - 2], nvs_random[nvs_random_size - 1]);
}
if (nvs_random) {
free(nvs_random);
}
return ESP_OK;
}


static esp_err_t get_device_pop(char *pop, size_t max, app_wifi_pop_type_t pop_type)
{
if (!pop || !max) {
Expand All @@ -174,14 +185,13 @@ static esp_err_t get_device_pop(char *pop, size_t max, app_wifi_pop_type_t pop_t
return err;
}
} else if (pop_type == POP_TYPE_RANDOM) {
char *nvs_pop = read_random_bytes_from_nvs();
if (!nvs_pop) {
uint8_t *nvs_random;
size_t nvs_random_size = 0;
if ((read_random_bytes_from_nvs(&nvs_random, &nvs_random_size) != ESP_OK) || nvs_random_size < 4) {
return ESP_ERR_NOT_FOUND;
} else {
strncpy(pop, nvs_pop, max - 1);
pop[max - 1] = 0;
free(nvs_pop);
nvs_pop = NULL;
snprintf(pop, max, "%02x%02x%02x%02x", nvs_random[0], nvs_random[1], nvs_random[2], nvs_random[3]);
free(nvs_random);
return ESP_OK;
}
} else {
Expand Down

0 comments on commit d3f0e64

Please sign in to comment.