Skip to content

Commit

Permalink
wifi_prov: Add mfg data for improved device discovery
Browse files Browse the repository at this point in the history
For BLE provisioning, adding some metadata in BLE advertisement can help
apps in filtering the scanned device list better, instead of relying
just on the prefix. It can also help show the device type even before
provisioning.

Metadata has been added in led_light and switch examples only, but can be
added in any other project too, as per the apps' requirement.
  • Loading branch information
sanketwadekar authored and shahpiyushv committed Aug 18, 2023
1 parent 269cf81 commit d789c71
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/common/app_wifi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(priv_req wifi_provisioning qrcode nvs_flash esp_event)
set(priv_req wifi_provisioning qrcode nvs_flash esp_event rmaker_common)

idf_component_register(SRCS "app_wifi.c"
INCLUDE_DIRS "."
Expand Down
38 changes: 38 additions & 0 deletions examples/common/app_wifi/app_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <esp_event.h>
#include <esp_log.h>
#include <esp_idf_version.h>
#include <inttypes.h>
#include <esp_rmaker_utils.h>
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0)
// Features supported in 4.1+
#define ESP_NETIF_SUPPORTED
Expand Down Expand Up @@ -100,6 +102,28 @@ static void intro_print(bool provisioned)

#endif /* !APP_WIFI_SHOW_DEMO_INTRO_TEXT */

static uint8_t *custom_mfg_data = NULL;
static size_t custom_mfg_data_len = 0;

esp_err_t app_wifi_set_custom_mfg_data(uint16_t device_type, uint8_t device_subtype)
{
int8_t mfg_data[] = {MFG_DATA_HEADER, MGF_DATA_APP_ID, MFG_DATA_VERSION, MFG_DATA_CUSTOMER_ID};
size_t mfg_data_len = sizeof(mfg_data) + 4; // 4 bytes of device type, subtype, and extra-code
custom_mfg_data = (uint8_t *)MEM_ALLOC_EXTRAM(mfg_data_len);
if (custom_mfg_data == NULL) {
ESP_LOGE(TAG, "Failed to allocate memory to custom mfg data");
return ESP_ERR_NO_MEM;
}
memcpy(custom_mfg_data, mfg_data, sizeof(mfg_data));
custom_mfg_data[8] = 0xff & (device_type >> 8);
custom_mfg_data[9] = 0xff & device_type;
custom_mfg_data[10] = device_subtype;
custom_mfg_data[11] = 0;
custom_mfg_data_len = mfg_data_len;
ESP_LOG_BUFFER_HEXDUMP("tag", custom_mfg_data, mfg_data_len, 3);
return ESP_OK;
}

static void app_wifi_print_qr(const char *name, const char *pop, const char *transport)
{
if (!name || !transport) {
Expand Down Expand Up @@ -449,6 +473,15 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
ESP_LOGE(TAG, "wifi_prov_scheme_ble_set_service_uuid failed %d", err);
return err;
}
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
if (custom_mfg_data) {
err = wifi_prov_scheme_ble_set_mfg_data(custom_mfg_data, custom_mfg_data_len);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set mfg data, err=0x%x", err);
return err;
}
}
#endif
#endif /* CONFIG_APP_WIFI_PROV_TRANSPORT_BLE */

/* Start provisioning service */
Expand All @@ -475,6 +508,11 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
/* Start Wi-Fi station */
wifi_init_sta();
}
if (custom_mfg_data) {
free(custom_mfg_data);
custom_mfg_data = NULL;
custom_mfg_data_len = 0;
}
/* Wait for Wi-Fi connection */
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY);
return ESP_OK;
Expand Down
14 changes: 14 additions & 0 deletions examples/common/app_wifi/app_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
extern "C" {
#endif

#define MFG_DATA_HEADER 0xe5, 0x02
#define MGF_DATA_APP_ID 'N', 'o', 'v'
#define MFG_DATA_VERSION 'a'
#define MFG_DATA_CUSTOMER_ID 0x00, 0x01

#define MGF_DATA_DEVICE_TYPE_LIGHT 0x0005
#define MGF_DATA_DEVICE_TYPE_SWITCH 0x0080

#define MFG_DATA_DEVICE_SUBTYPE_SWITCH 0x01
#define MFG_DATA_DEVICE_SUBTYPE_LIGHT 0x01

#define MFG_DATA_DEVICE_EXTRA_CODE 0x00

/** ESP RainMaker Event Base */
ESP_EVENT_DECLARE_BASE(APP_WIFI_EVENT);

Expand Down Expand Up @@ -40,6 +53,7 @@ typedef enum {

void app_wifi_init();
esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type);
esp_err_t app_wifi_set_custom_mfg_data(uint16_t device_type, uint8_t device_subtype);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions examples/led_light/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ void app_main()
/* Start the ESP RainMaker Agent */
esp_rmaker_start();

err = app_wifi_set_custom_mfg_data(MGF_DATA_DEVICE_TYPE_LIGHT, MFG_DATA_DEVICE_SUBTYPE_LIGHT);
/* Start the Wi-Fi.
* If the node is provisioned, it will start connection attempts,
* else, it will start Wi-Fi provisioning. The function will return
Expand Down
1 change: 1 addition & 0 deletions examples/switch/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ void app_main()
/* Start the ESP RainMaker Agent */
esp_rmaker_start();

err = app_wifi_set_custom_mfg_data(MGF_DATA_DEVICE_TYPE_SWITCH, MFG_DATA_DEVICE_SUBTYPE_SWITCH);
/* Start the Wi-Fi.
* If the node is provisioned, it will start connection attempts,
* else, it will start Wi-Fi provisioning. The function will return
Expand Down

0 comments on commit d789c71

Please sign in to comment.