Skip to content

Commit d3f0e64

Browse files
committed
Merge branch 'task/random_ssid_pop' into 'master'
app_wifi: Changes in SSID and PoP generation for Provisioning See merge request app-frameworks/esp-rainmaker!189
2 parents 3614972 + fd50262 commit d3f0e64

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed

Diff for: CHANGES.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changes
22

3+
## 16-Oct-2020 (app_wifi: Changes in SSID and PoP generation for Provisioning)
4+
5+
The PoP for Wi-Fi provisioning was being fetched from a random 8 character hex string stored in the fctry partition.
6+
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.
7+
PoP is now generated by reading the first 4 bytes of this and converting to 8 character hex string.
8+
Even the SSID now uses the last 3 bytes of this random number as the suffix, instead of last 3 bytes of MAC address.
9+
With this change, it will now be possible to generate the complete Provisioning QR code payload outside the device,
10+
without having to know its MAC address.
11+
312
## 29-Sep-2020 (esp_rmaker_standard_types: Start default names of all standard params with capital letter)
413

514
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.

Diff for: cli/rmaker_tools/rmaker_claim/claim.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def gen_host_csr(private_key, common_name=None):
152152
csr = request.public_bytes(serialization.Encoding.PEM).decode("utf-8")
153153
return csr
154154

155-
def gen_hex_str(octets=4):
155+
def gen_hex_str(octets=64):
156156
"""
157157
Generate random hex string, it is used as PoP
158158
@@ -188,7 +188,7 @@ def save_random_hex_str(dest_filedir, hex_str):
188188
info_file.write(hex_str)
189189

190190
with open(dest_filedir + 'node_info.csv', 'a') as info_file:
191-
info_file.write('random,file,binary,' +
191+
info_file.write('random,file,hex2bin,' +
192192
dest_filedir + 'random.info')
193193
info_file.write('\n')
194194
except Exception as err:

Diff for: components/esp_rainmaker/src/core/esp_rmaker_claim.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
#include "esp_rmaker_claim.h"
4545

4646
static const char *TAG = "esp_claim";
47+
48+
#define ESP_RMAKER_RANDOM_NUMBER_LEN 64
49+
4750
#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM
4851
#include "soc/soc.h"
4952
#include "soc/efuse_reg.h"
@@ -785,8 +788,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
785788
#endif /* CONFIG_ESP_RMAKER_ASSISTED_CLAIM */
786789
esp_err_t __esp_rmaker_claim_init(esp_rmaker_claim_data_t *claim_data)
787790
{
788-
char hexstr[9];
789-
uint32_t my_random;
791+
uint8_t random_bytes[ESP_RMAKER_RANDOM_NUMBER_LEN];
790792
esp_err_t err;
791793

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

820820
/* Store the PoP in the storage */
821-
err = esp_rmaker_storage_set(ESP_RMAKER_CLIENT_RANDOM_NVS_KEY, hexstr, strlen(hexstr));
821+
err = esp_rmaker_storage_set(ESP_RMAKER_CLIENT_RANDOM_NVS_KEY, random_bytes, sizeof(random_bytes));
822822
if (err != ESP_OK) {
823-
ESP_LOGE(TAG, "Failed to store random number to storage");
823+
ESP_LOGE(TAG, "Failed to store random bytes to storage.");
824824
return err;
825825
}
826826
#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM

Diff for: examples/common/app_wifi/app_wifi.c

+37-27
Original file line numberDiff line numberDiff line change
@@ -120,44 +120,55 @@ static void wifi_init_sta()
120120
ESP_ERROR_CHECK(esp_wifi_start());
121121
}
122122

123-
static void get_device_service_name(char *service_name, size_t max)
124-
{
125-
uint8_t eth_mac[6];
126-
const char *ssid_prefix = "PROV_";
127-
esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
128-
snprintf(service_name, max, "%s%02X%02X%02X",
129-
ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
130-
}
131-
132123
/* free the return value after use. */
133-
static char *read_random_bytes_from_nvs()
124+
static esp_err_t read_random_bytes_from_nvs(uint8_t **random_bytes, size_t *len)
134125
{
135126
nvs_handle handle;
136127
esp_err_t err;
137-
size_t required_size = 0;
138-
void *value;
128+
*len = 0;
139129

140130
if ((err = nvs_open_from_partition(CONFIG_ESP_RMAKER_FACTORY_PARTITION_NAME, CREDENTIALS_NAMESPACE,
141131
NVS_READONLY, &handle)) != ESP_OK) {
142132
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);
143-
return NULL;
133+
return ESP_FAIL;
144134
}
145135

146-
if ((err = nvs_get_blob(handle, RANDOM_NVS_KEY, NULL, &required_size)) != ESP_OK) {
147-
ESP_LOGD(TAG, "Failed to read key %s with error %d size %d", RANDOM_NVS_KEY, err, required_size);
136+
if ((err = nvs_get_blob(handle, RANDOM_NVS_KEY, NULL, len)) != ESP_OK) {
137+
ESP_LOGD(TAG, "Error %d. Failed to read key %s.", err, RANDOM_NVS_KEY);
148138
nvs_close(handle);
149-
return NULL;
139+
return ESP_ERR_NOT_FOUND;
150140
}
151141

152-
value = calloc(required_size + 1, 1); /* + 1 for NULL termination */
153-
if (value) {
154-
nvs_get_blob(handle, RANDOM_NVS_KEY, value, &required_size);
142+
*random_bytes = calloc(*len, 1);
143+
if (*random_bytes) {
144+
nvs_get_blob(handle, RANDOM_NVS_KEY, *random_bytes, len);
145+
nvs_close(handle);
146+
return ESP_OK;
155147
}
156-
157148
nvs_close(handle);
158-
return value;
149+
return ESP_ERR_NO_MEM;
150+
}
151+
152+
static esp_err_t get_device_service_name(char *service_name, size_t max)
153+
{
154+
uint8_t *nvs_random;
155+
const char *ssid_prefix = "PROV_";
156+
size_t nvs_random_size = 0;
157+
if ((read_random_bytes_from_nvs(&nvs_random, &nvs_random_size) != ESP_OK) || nvs_random_size < 3) {
158+
uint8_t eth_mac[6];
159+
esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
160+
snprintf(service_name, max, "%s%02x%02x%02x", ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
161+
} else {
162+
snprintf(service_name, max, "%s%02x%02x%02x", ssid_prefix, nvs_random[nvs_random_size - 3],
163+
nvs_random[nvs_random_size - 2], nvs_random[nvs_random_size - 1]);
164+
}
165+
if (nvs_random) {
166+
free(nvs_random);
167+
}
168+
return ESP_OK;
159169
}
160170

171+
161172
static esp_err_t get_device_pop(char *pop, size_t max, app_wifi_pop_type_t pop_type)
162173
{
163174
if (!pop || !max) {
@@ -174,14 +185,13 @@ static esp_err_t get_device_pop(char *pop, size_t max, app_wifi_pop_type_t pop_t
174185
return err;
175186
}
176187
} else if (pop_type == POP_TYPE_RANDOM) {
177-
char *nvs_pop = read_random_bytes_from_nvs();
178-
if (!nvs_pop) {
188+
uint8_t *nvs_random;
189+
size_t nvs_random_size = 0;
190+
if ((read_random_bytes_from_nvs(&nvs_random, &nvs_random_size) != ESP_OK) || nvs_random_size < 4) {
179191
return ESP_ERR_NOT_FOUND;
180192
} else {
181-
strncpy(pop, nvs_pop, max - 1);
182-
pop[max - 1] = 0;
183-
free(nvs_pop);
184-
nvs_pop = NULL;
193+
snprintf(pop, max, "%02x%02x%02x%02x", nvs_random[0], nvs_random[1], nvs_random[2], nvs_random[3]);
194+
free(nvs_random);
185195
return ESP_OK;
186196
}
187197
} else {

0 commit comments

Comments
 (0)