update example to use host claim

This commit is contained in:
WanqQixiang
2024-09-11 20:11:37 +08:00
parent b13c7a2d56
commit 8cd67f723b
14 changed files with 325 additions and 282 deletions
-10
View File
@@ -6,30 +6,20 @@ if(NOT DEFINED ENV{ESP_MATTER_PATH})
message(FATAL_ERROR "Please set ESP_MATTER_PATH to the path of esp-matter repo")
endif(NOT DEFINED ENV{ESP_MATTER_PATH})
if(NOT DEFINED ENV{RMAKER_PATH})
message(FATAL_ERROR "Please set RMAKER_PATH to the path of esp-rainmaker repo")
endif(NOT DEFINED ENV{RMAKER_PATH})
set(PROJECT_VER "1.0")
set(PROJECT_VER_NUMBER 1)
set(ESP_MATTER_PATH $ENV{ESP_MATTER_PATH})
set(MATTER_SDK_PATH ${ESP_MATTER_PATH}/connectedhomeip/connectedhomeip)
set(RMAKER_PATH $ENV{RMAKER_PATH})
# This should be done before using the IDF_TARGET variable.
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
include(${ESP_MATTER_PATH}/examples/common/cmake_common/components_include.cmake)
idf_build_set_property(RAINMAKER_ENABLED 1)
set(EXTRA_COMPONENT_DIRS
"${ESP_MATTER_PATH}/examples/common"
"${MATTER_SDK_PATH}/config/esp32/components"
"${ESP_MATTER_PATH}/components"
"${RMAKER_PATH}/examples/common/qrcode"
"${RMAKER_PATH}/components/esp_rainmaker"
${extra_components_dirs_append})
project(light-wifi-prov)
+112 -10
View File
@@ -6,27 +6,61 @@ See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/de
## 1. Prerequisites
- ESP32-S3-DevKitM
- ESP32-S3-DevKitM / ESP32-C3-DevKitM
- chip-tool (For Matter commissioning)
- Android RainMaker APP of version 3.4.1 or iOS RainMaker APP of version 3.2.0 (For RainMaker Provisioning)
- [ESP-IDF](https://github.com/espressif/esp-idf) on commit 1022b2b447d364d2f2600045c75d81dd203dd15e
- [ESP-RainMaker](https://github.com/espressif/esp-rainmaker) on commit 90a8d9edf9802ca23b14ff87fe3bbf826a1df228
- [ESP-IDF](https://github.com/espressif/esp-idf) on tag v5.2.2
- [ESP RainMaker](https://github.com/espressif/esp-rainmaker/tree/fd781295) on commit fd78129500aa1a8b2eee9e0dcc0720d7b14cd3dc
- [ESP Secure Cert Manager](https://github.com/espressif/esp_secure_cert_mgr)
## 2. Additional Environment Setup
To build this example, you need to clone ESP-RainMaker repository and export the path as RMAKER_PATH:
### 2.1 Claiming device certificates
This example will use [host driven claiming](https://rainmaker.espressif.com/docs/claiming/#host-driven-claiming) via the RainMaker CLI.
Make sure your device is connected to the host machine, login into the CLI and execute this:
```
$ cd $RMAKER_PATH/cli
$ ./rainmaker.py claim --matter <port>
```
The CLI will fetch the device certificates and flash them into the secure cert partition of your device. The certificates will be used for both the Matter device attestation verification and RainMaker MQTT connection.
### 2.2 Generating the factory nvs binary
The factory nvs (fctry partition) needs to be generated using the mfg_tool of esp-matter. It is released on pypi as [esp-matter-mfg-tool](https://pypi.org/project/esp-matter-mfg-tool) and can be installed by running `pip install esp-matter-mfg-tool`
```
git clone https://github.com/espressif/esp-rainmaker.git <rainmaker_path>
cd <rainmaker_path>
git checkout 90a8d9edf9802ca23b14ff87fe3bbf826a1df228
git submodule update --recursive --init
export RMAKER_PATH=<rainmaker_path>
$ export ESP_SECURE_CERT_PATH=/path/to/esp_secure_cert_mgr
$ esp-matter-mfg-tool -v 0x131B -p 0x2 -cd $RMAKER_PATH/examples/matter/mfg/cd_131B_0002.der --csv $RMAKER_PATH/examples/matter/mfg/keys.csv --mcsv $RMAKER_PATH/examples/matter/mfg/master.csv
```
This not only generates the factory nvs binary required for matter, but also embeds the RainMaker MQTT Host url into it via the master.csv file. Optionally, you can embed the MQTT host into the firmware itself by using idf.py menuconfig -> ESP RainMaker Config -> ESP_RMAKER_READ_MQTT_HOST_FROM_CONFIG and then skipping the --csv and --mcsv options to mfg_tool
The factory binary generated above should be flashed onto the fctry partition (default : 0x3e0000 for ESP32-S3 and ESP32-C3. Do check your partition table for exact address).
```
$ esptool.py write_flash 0x3e0000 out/131b_2/<node-id>/<node-id>-partition.bin
```
### 2.3 Build the example
Once the environment and required files are set up, we can now proceed to build and flash the example
```
$ idf.py set-target <target>
$ idf.py build
$ idf.py flash monitor
```
## 3. Post Commissioning Setup
No additional setup is required for Matter Commissioning. See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#commissioning-and-control) for more information about Matter commissioning with chip-tool
See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#commissioning-and-control) for the information about Matter commissioning with chip-tool. Note that you need to specific the PAA path to the `paa_cert` under the directory of this example when you use the chip-tool to commisioning the device.
```
$ ./chip-tool pairing ble-wifi <node-id> <ssid> <passcode> <setup-pincode> <discriminator> --paa-trust-store-path <PAA-path>
```
**Note**: The `setup-picode` and `discriminator` is generated with the factory partition binary. You can find it in the `out/131b_2/<node-id>/<node-id>-onb_codes.csv`.
## 4. External platform
@@ -39,3 +73,71 @@ There are also some WiFi stack initialization changes to avoid duplicated Wi-Fi
## 5. RainMaker Provisioning
After you flash the example to the DevKit board, the console will print the QR code for RainMaker Provisioning. After you scan the QR code with the RainMaker Phone App, the RainMaker provisioning will be done and the device will be added to your RainMaker home.
## 6. Manufacturing Considerations
This step is only suggested for Privately deployed Production and not required for test set up.
### 6.1 RainMaker MQTT Host
Find your private deployment's mqtt hostname (if applicable) by sending a GET request at `https://<Rainmaker-API-endpoint>/mqtt_host`. You should replace the mqtt host in master.csv (As described in the section above) with this to generate the factory nvs binary.
### 6.2 Matter VID/PID
For production devices which may have a different matter vid and pid, please set the values of DEVICE_VENDOR_ID and DEVICE_PRODUCT_ID by using `idf.py menuconfig` -> `Component config` -> `CHIP Device Layer` -> `Device Identification Options`. These same should also be used in the `mfg_tool`.
**Note**: The CD used by `mfg_tool` should also be changed when VID/PID is changed. Please refer to [this](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/certification.html#certification-declaration) about how to generate a test CD file.
### 6.3 Matter DAC
For public RainMaker, some test DACs are provided via claiming. For private deployments, test DACs can be generated using `mfg_tool`.
```
export ESP_SECURE_CERT_PATH=/path/to/esp_secure_cert_mgr
esp-matter-mfg-tool -v <vendor-id> -p <product-id> --pai -k <pai-key> -c <pai-cert> -cd <cert-dclrn> --csv /path/to/keys.csv --mcsv /path/to/master.csv
```
Samples of keys.csv and master.csv can be found in $RMAKER_PATH/examples/matter/mfg/.
For testing, you can use the test VID, PID, PAI and CD as shown below.
```
$ esp-matter-mfg-tool --dac-in-secure-cert -v 0xFFF2 -p 0x8001 --pai -k $ESP_MATTER_PATH/connectedhomeip/connectedhomeip/credentials/test/attestation/Chip-Test-PAI-FFF2-8001-Key.pem -c $ESP_MATTER_PATH/connectedhomeip/connectedhomeip/credentials/test/attestation/Chip-Test-PAI-FFF2-8001-Cert.pem -cd $ESP_MATTER_PATH/connectedhomeip/connectedhomeip/credentials/test/certification-declaration/Chip-Test-CD-FFF2-8001.der --csv $RMAKER_PATH/examples/matter/mfg/keys.csv --mcsv $RMAKER_PATH/examples/matter/mfg/master.csv
```
Note the path where the files are generated after running the above command since it will be required later.
### 6.4 Configure your app
Open the project configuration menu using
```
idf.py menuconfig
```
In the configuration menu, set the following additional configuration to use custom factory partition and different values for Data and Device Info Providers.
1. Enable ESP32 Factory Data Provider [Component config → CHIP Device Layer → Commissioning options → Use ESP32 Factory Data Provider]
Enable config option CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER to use ESP32 specific implementation of CommissionableDataProvider and DeviceAttestationCredentialsProvider.
2. Enable ESP32 Device Instance Info Provider [Component config → CHIP Device Layer → Commissioning options → Use ESP32 Device Instance Info Provider]
Enable config option ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER to get device instance info from factory partition.
3. Enable Attestation - Secure Cert [ Component config → ESP Matter → DAC Provider options → Attestation - Secure Cert]
Enable config option CONFIG_FACTORY_PARTITION_DAC_PROVIDER to use DAC certificates from the secure_cert partition during Attestation.
4. Set chip-factory namespace partition label [Component config → CHIP Device Layer → Matter Manufacturing Options → chip-factory namespace partition label]
Set config option CHIP_FACTORY_NAMESPACE_PARTITION_LABEL to choose the label of the partition to store key-values in the "chip-factory" namespace. The default chosen partition label is nvs, change it to fctry.
Connect your esp32 device to your computer. Enter the below command to flash certificates and factory partition
```
$ esptool.py write_flash 0xd000 /out/<vendor-id>_<product-id>/<node-id>/<node-id>_esp_secure_cert.bin 0x3e0000 ./out/<vendor-id>_<product-id>/<node-id>/<node-id>-partition.bin
```
The csv file generate at `/out/<vendor-id>_<product-id>/cn_dacs-<date>-<time>.csv` should be registered to your private RainMaker deployment (if applicable) using the steps mentioned here.
> **In production use case, the DACs will be pre-provisioned in the modules and a csv file will be provided by the Espressif factory directly. Optionally, even the fctry partitions can be pre programmed. If not, use the mfg_tool to generate these nvs binaries**
+6 -21
View File
@@ -7,7 +7,7 @@
*/
#include "esp_matter_attribute_utils.h"
#include "esp_matter_core.h"
#include "esp_rmaker_user_mapping.h"
#include <esp_err.h>
#include <esp_log.h>
#include <nvs_flash.h>
@@ -15,7 +15,6 @@
#include <esp_matter.h>
#include <esp_matter_console.h>
#include <esp_matter_ota.h>
#include <esp_matter_rainmaker.h>
#include <esp_rmaker_console.h>
#include <esp_rmaker_core.h>
@@ -255,8 +254,6 @@ extern "C" void app_main()
attribute::get(color_control_cluster, ColorControl::Attributes::ColorTemperatureMireds::Id);
attribute::set_deferred_persistence(color_temp_attribute);
esp_matter::rainmaker::init();
err = esp_event_loop_create_default();
if (err != ESP_OK) {
@@ -264,16 +261,6 @@ extern "C" void app_main()
return;
}
app_wifi_init();
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
/* Set OpenThread platform config */
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
set_openthread_platform_config(&config);
#endif
esp_rmaker_config_t rainmaker_cfg = {
.enable_time_sync = false,
};
@@ -283,7 +270,7 @@ extern "C" void app_main()
vTaskDelay(5000 / portTICK_PERIOD_MS);
abort();
}
esp_matter::rainmaker::start();
light_device = esp_rmaker_lightbulb_device_create("Light", light_handle, DEFAULT_POWER);
esp_rmaker_device_add_bulk_cb(light_device, bulk_write_cb, NULL);
@@ -301,11 +288,9 @@ extern "C" void app_main()
esp_rmaker_schedule_enable();
esp_rmaker_start();
err = app_wifi_set_custom_mfg_data(MGF_DATA_DEVICE_TYPE_LIGHT, MFG_DATA_DEVICE_SUBTYPE_LIGHT);
err = app_wifi_start(POP_TYPE_RANDOM);
err = app_wifi_start(POP_TYPE_MAC);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Could not start Wifi. Aborting!!!");
abort();
@@ -322,13 +307,13 @@ extern "C" void app_main()
err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len);
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err));
#endif // CONFIG_ENABLE_ENCRYPTED_OTA
if (esp_rmaker_user_node_mapping_get_state() == ESP_RMAKER_USER_MAPPING_DONE) {
chip::DeviceLayer::Internal::BLEMgr().Shutdown();
}
#if CONFIG_ENABLE_CHIP_SHELL
esp_matter::console::diagnostics_register_commands();
esp_matter::console::wifi_register_commands();
#if CONFIG_OPENTHREAD_CLI
esp_matter::console::otcli_register_commands();
#endif
esp_matter::console::init();
#endif
}
+132 -122
View File
@@ -5,29 +5,29 @@
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <esp_wifi.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_idf_version.h>
#include <inttypes.h>
#include <esp_log.h>
#include <esp_rmaker_utils.h>
#include <esp_wifi.h>
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
#include <string.h>
#include <esp_netif.h>
#include <wifi_provisioning/manager.h>
#include <wifi_prov_scheme_matter_ble.h>
#include <wifi_provisioning/manager.h>
#include <qrcode.h>
#include <app_wifi.h>
#include <esp_rmaker_core.h>
#include <esp_timer.h>
#include <nvs.h>
#include <nvs_flash.h>
#include <esp_timer.h>
#include "app_wifi.h"
#include <platform/PlatformManager.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/ESP32_custom/PlatformManagerImpl.h>
#include <platform/PlatformManager.h>
#include <qrcode.h>
ESP_EVENT_DEFINE_BASE(APP_WIFI_EVENT);
@@ -35,32 +35,31 @@ static const char *TAG = "app_wifi";
#define PROV_QR_VERSION "v1"
#define PROV_TRANSPORT_BLE "ble"
#define QRCODE_BASE_URL "https://rainmaker.espressif.com/qrcode.html"
#define PROV_TRANSPORT_BLE "ble"
#define QRCODE_BASE_URL "https://rainmaker.espressif.com/qrcode.html"
#define CREDENTIALS_NAMESPACE "rmaker_creds"
#define RANDOM_NVS_KEY "random"
#define CREDENTIALS_NAMESPACE "rmaker_creds"
#define RANDOM_NVS_KEY "random"
#define POP_STR_SIZE 9
#define POP_STR_SIZE 9
static esp_timer_handle_t prov_stop_timer;
/* Timeout period in minutes */
#define APP_WIFI_PROV_TIMEOUT_PERIOD 30
#define APP_WIFI_PROV_TIMEOUT_PERIOD 30
/* Autofetch period in micro-seconds */
static uint64_t prov_timeout_period = (APP_WIFI_PROV_TIMEOUT_PERIOD * 60 * 1000000LL);
#define APP_PROV_STOP_ON_CREDS_MISMATCH
#define ESP_RAINMAKER_GITHUB_EXAMPLES_PATH "https://github.com/espressif/esp-rainmaker/blob/master/examples"
#define ESP_RAINMAKER_INTRO_LINK "https://rainmaker.espressif.com"
#define ESP_RMAKER_PHONE_APP_LINK "http://bit.ly/esp-rmaker"
char esp_rainmaker_ascii_art[] = \
" ______ _____ _____ _____ _____ _ _ __ __ _ ________ _____\n"\
" | ____|/ ____| __ \\ | __ \\ /\\ |_ _| \\ | | \\/ | /\\ | |/ / ____| __ \\\n"\
" | |__ | (___ | |__) | | |__) | / \\ | | | \\| | \\ / | / \\ | ' /| |__ | |__) |\n"\
" | __| \\___ \\| ___/ | _ / / /\\ \\ | | | . ` | |\\/| | / /\\ \\ | < | __| | _ /\n"\
" | |____ ____) | | | | \\ \\ / ____ \\ _| |_| |\\ | | | |/ ____ \\| . \\| |____| | \\ \\\n"\
" |______|_____/|_| |_| \\_\\/_/ \\_\\_____|_| \\_|_| |_/_/ \\_\\_|\\_\\______|_| \\_\\\n";
#define ESP_RAINMAKER_GITHUB_EXAMPLES_PATH "https://github.com/espressif/esp-rainmaker/blob/master/examples"
#define ESP_RAINMAKER_INTRO_LINK "https://rainmaker.espressif.com"
#define ESP_RMAKER_PHONE_APP_LINK "http://bit.ly/esp-rmaker"
char esp_rainmaker_ascii_art[] =
" ______ _____ _____ _____ _____ _ _ __ __ _ ________ _____\n"
" | ____|/ ____| __ \\ | __ \\ /\\ |_ _| \\ | | \\/ | /\\ | |/ / ____| __ \\\n"
" | |__ | (___ | |__) | | |__) | / \\ | | | \\| | \\ / | / \\ | ' /| |__ | |__) |\n"
" | __| \\___ \\| ___/ | _ / / /\\ \\ | | | . ` | |\\/| | / /\\ \\ | < | __| | _ /\n"
" | |____ ____) | | | | \\ \\ / ____ \\ _| |_| |\\ | | | |/ ____ \\| . \\| |____| | \\ \\\n"
" |______|_____/|_| |_| \\_\\/_/ \\_\\_____|_| \\_|_| |_/_/ \\_\\_|\\_\\______|_| \\_\\\n";
static uint8_t *custom_mfg_data = NULL;
static size_t custom_mfg_data_len = 0;
@@ -83,6 +82,14 @@ esp_err_t app_wifi_set_custom_mfg_data(uint16_t device_type, uint8_t device_subt
return ESP_OK;
}
static esp_err_t qrcode_display(const char *text)
{
#define MAX_QRCODE_VERSION 5
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
cfg.max_qrcode_version = MAX_QRCODE_VERSION;
return esp_qrcode_generate(&cfg, text);
}
static void app_wifi_print_qr(const char *name, const char *pop, const char *transport)
{
if (!name || !transport) {
@@ -91,91 +98,96 @@ static void app_wifi_print_qr(const char *name, const char *pop, const char *tra
}
char payload[150];
if (pop) {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"pop\":\"%s\",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, pop, transport);
snprintf(payload, sizeof(payload),
"{\"ver\":\"%s\",\"name\":\"%s\""
",\"pop\":\"%s\",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, pop, transport);
} else {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, transport);
snprintf(payload, sizeof(payload),
"{\"ver\":\"%s\",\"name\":\"%s\""
",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, transport);
}
ESP_LOGI(TAG, "Scan this QR code from the ESP RainMaker phone app for Provisioning.");
qrcode_display(payload);
ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL, payload);
ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL,
payload);
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_QR_DISPLAY, payload, strlen(payload) + 1, portMAX_DELAY);
}
/* Event handler for catching system events */
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
static int retries = 0;
static int failed_cnt = 0;
if (event_base == WIFI_PROV_EVENT) {
switch (event_id) {
case WIFI_PROV_START:
ESP_LOGI(TAG, "Provisioning started");
break;
case WIFI_PROV_CRED_RECV: {
wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
ESP_LOGI(TAG, "Received Wi-Fi credentials"
"\n\tSSID : %s\n\tPassword : %s",
(const char *) wifi_sta_cfg->ssid,
(const char *) wifi_sta_cfg->password);
break;
}
case WIFI_PROV_CRED_FAIL: {
wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s"
"\n\tPlease reset to factory and retry provisioning",
(*reason == WIFI_PROV_STA_AUTH_ERROR) ?
"Wi-Fi station authentication failed" : "Wi-Fi access-point not found");
retries++;
if (retries >= 5) {
ESP_LOGI(TAG, "Failed to connect with provisioned AP, reseting provisioned credentials");
wifi_prov_mgr_reset_sm_state_on_failure();
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_RESTART, NULL, 0, portMAX_DELAY);
ESP_LOGW(TAG, "Failed to connect with provisioned AP, please reset to provisioning manually");
retries = 0;
}
break;
}
case WIFI_PROV_CRED_SUCCESS:
ESP_LOGI(TAG, "Provisioning successful");
case WIFI_PROV_START:
ESP_LOGI(TAG, "Provisioning started");
break;
case WIFI_PROV_CRED_RECV: {
wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
ESP_LOGI(TAG,
"Received Wi-Fi credentials"
"\n\tSSID : %s\n\tPassword : %s",
(const char *)wifi_sta_cfg->ssid, (const char *)wifi_sta_cfg->password);
break;
}
case WIFI_PROV_CRED_FAIL: {
wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
ESP_LOGE(TAG,
"Provisioning failed!\n\tReason : %s"
"\n\tPlease reset to factory and retry provisioning",
(*reason == WIFI_PROV_STA_AUTH_ERROR) ? "Wi-Fi station authentication failed"
: "Wi-Fi access-point not found");
retries++;
if (retries >= 5) {
ESP_LOGI(TAG, "Failed to connect with provisioned AP, reseting provisioned credentials");
wifi_prov_mgr_reset_sm_state_on_failure();
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_RESTART, NULL, 0, portMAX_DELAY);
ESP_LOGW(TAG, "Failed to connect with provisioned AP, please reset to provisioning manually");
retries = 0;
break;
case WIFI_PROV_END:
if (prov_stop_timer) {
esp_timer_stop(prov_stop_timer);
esp_timer_delete(prov_stop_timer);
prov_stop_timer = NULL;
}
/* De-initialize manager once provisioning is finished */
wifi_prov_mgr_deinit();
break;
default:
break;
}
break;
}
case WIFI_PROV_CRED_SUCCESS:
ESP_LOGI(TAG, "Provisioning successful");
retries = 0;
break;
case WIFI_PROV_END:
if (prov_stop_timer) {
esp_timer_stop(prov_stop_timer);
esp_timer_delete(prov_stop_timer);
prov_stop_timer = NULL;
}
/* De-initialize manager once provisioning is finished */
wifi_prov_mgr_deinit();
break;
default:
break;
}
} else if (event_base == PROTOCOMM_SECURITY_SESSION_EVENT) {
switch (event_id) {
case PROTOCOMM_SECURITY_SESSION_SETUP_OK:
ESP_LOGI(TAG, "Secured session established!");
chip::DeviceLayer::PlatformManagerImpl::DisableESPEventDispatch();
break;
case PROTOCOMM_SECURITY_SESSION_INVALID_SECURITY_PARAMS:
/* fall-through */
case PROTOCOMM_SECURITY_SESSION_CREDENTIALS_MISMATCH:
ESP_LOGE(TAG, "Received incorrect PoP or invalid security params! event: %d", (int) event_id);
if (++failed_cnt >= 5) {
/* stop provisioning for security reasons */
wifi_prov_mgr_stop_provisioning();
ESP_LOGW(TAG, "Max PoP attempts reached! Provisioning disabled for security reasons. Please reboot device to restart provisioning");
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_CRED_MISMATCH, NULL, 0, portMAX_DELAY);
}
break;
default:
break;
case PROTOCOMM_SECURITY_SESSION_SETUP_OK:
ESP_LOGI(TAG, "Secured session established!");
esp_rmaker_start();
break;
case PROTOCOMM_SECURITY_SESSION_INVALID_SECURITY_PARAMS:
/* fall-through */
case PROTOCOMM_SECURITY_SESSION_CREDENTIALS_MISMATCH:
ESP_LOGE(TAG, "Received incorrect PoP or invalid security params! event: %d", (int)event_id);
if (++failed_cnt >= 5) {
/* stop provisioning for security reasons */
wifi_prov_mgr_stop_provisioning();
ESP_LOGW(TAG,
"Max PoP attempts reached! Provisioning disabled for security reasons. Please reboot device "
"to restart provisioning");
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_CRED_MISMATCH, NULL, 0, portMAX_DELAY);
}
break;
default:
break;
}
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
ESP_LOGI(TAG, "Disconnected. Connecting to the AP again...");
@@ -190,9 +202,10 @@ static esp_err_t read_random_bytes_from_nvs(uint8_t **random_bytes, size_t *len)
esp_err_t err;
*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);
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 ESP_FAIL;
}
@@ -244,7 +257,7 @@ static esp_err_t get_device_service_name(char *service_name, size_t max)
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]);
nvs_random[nvs_random_size - 2], nvs_random[nvs_random_size - 1]);
}
if (nvs_random) {
free(nvs_random);
@@ -252,7 +265,6 @@ static esp_err_t get_device_service_name(char *service_name, size_t max)
return ESP_OK;
}
static char *get_device_pop(app_wifi_pop_type_t pop_type)
{
if (pop_type == POP_TYPE_NONE) {
@@ -304,34 +316,32 @@ void app_wifi_init(void)
{
wifi_init_config_t cfg;
esp_err_t err = esp_netif_init();
if (err != ESP_OK)
{
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize esp_netif");
return;
}
// Lets not create a default station interface if already present
if (!esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"))
{
if (!esp_netif_create_default_wifi_sta())
{
if (!esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")) {
if (!esp_netif_create_default_wifi_sta()) {
ESP_LOGE(TAG, "Failed to create the WiFi STA netif");
return;;
return;
;
}
}
// Initialize the ESP WiFi layer.
cfg = WIFI_INIT_CONFIG_DEFAULT();
err = esp_wifi_init(&cfg);
if (err != ESP_OK)
{
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize esp_wifi");
return;
}
/* Register our event handler for Wi-Fi, IP and Provisioning related events */
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
#ifdef APP_PROV_STOP_ON_CREDS_MISMATCH
ESP_ERROR_CHECK(esp_event_handler_register(PROTOCOMM_SECURITY_SESSION_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(
esp_event_handler_register(PROTOCOMM_SECURITY_SESSION_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
#endif
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
@@ -349,16 +359,13 @@ esp_err_t app_wifi_start_timer(void)
if (prov_timeout_period == 0) {
return ESP_OK;
}
esp_timer_create_args_t prov_stop_timer_conf = {
.callback = app_wifi_prov_stop,
.arg = NULL,
.dispatch_method = ESP_TIMER_TASK,
.name = "app_wifi_prov_stop_tm"
};
esp_timer_create_args_t prov_stop_timer_conf = {.callback = app_wifi_prov_stop,
.arg = NULL,
.dispatch_method = ESP_TIMER_TASK,
.name = "app_wifi_prov_stop_tm"};
if (esp_timer_create(&prov_stop_timer_conf, &prov_stop_timer) == ESP_OK) {
esp_timer_start_once(prov_stop_timer, prov_timeout_period);
ESP_LOGI(TAG, "Provisioning will auto stop after %d minute(s).",
APP_WIFI_PROV_TIMEOUT_PERIOD);
ESP_LOGI(TAG, "Provisioning will auto stop after %d minute(s).", APP_WIFI_PROV_TIMEOUT_PERIOD);
return ESP_OK;
} else {
ESP_LOGE(TAG, "Failed to create Provisioning auto stop timer.");
@@ -437,8 +444,7 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
uint8_t custom_service_uuid[] = {
/* This is a random uuid. This can be modified if you want to change the BLE uuid. */
/* 12th and 13th bit will be replaced by internal bits. */
0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02,
0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02,
};
esp_err_t err = wifi_prov_scheme_matter_ble_set_service_uuid(custom_service_uuid, sizeof(custom_service_uuid));
if (err != ESP_OK) {
@@ -467,6 +473,10 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
/* We don't need the manager as device is already provisioned,
* so let's release it's resources */
wifi_prov_mgr_deinit();
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
esp_wifi_connect();
esp_rmaker_start();
}
if (custom_mfg_data) {
free(custom_mfg_data);
@@ -1,8 +1,7 @@
dependencies:
espressif/cmake_utilities:
version: 0.*
rules: # will add "optional_component" only when all if clauses are True
- if: "idf_version >=5.0"
- if: "target in [esp32c2]"
esp_bsp_generic:
version: "^1.1.0"
espressif/esp_rainmaker:
version: "1.3.0"
espressif/qrcode:
version: "^0.1.0~2"
@@ -1,3 +1,11 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <platform/CHIPDeviceConfig.h>
#include <ble/CHIPBleServiceData.h>
@@ -1,3 +1,11 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <esp_err.h>
@@ -1,3 +1,11 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "wifi_prov_scheme_matter_ble.h"
#include "protocomm_matter_ble.h"
@@ -1,3 +1,11 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <protocomm.h>
+3 -3
View File
@@ -5,6 +5,6 @@ nvs, data, nvs, 0x10000, 0xC000,
nvs_keys, data, nvs_keys,, 0x1000, encrypted
otadata, data, ota, , 0x2000
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, 0x20000, 1900K,
ota_1, app, ota_1, , 1900K,
fctry, data, nvs, , 0x6000
ota_0, app, ota_0, 0x20000, 0x1E0000,
ota_1, app, ota_1, , 0x1E0000,
fctry, data, nvs, 0x3E0000, 0x6000
1 # Name, Type, SubType, Offset, Size, Flags
5 nvs_keys, data, nvs_keys,, 0x1000, encrypted
6 otadata, data, ota, , 0x2000
7 phy_init, data, phy, , 0x1000,
8 ota_0, app, ota_0, 0x20000, 1900K, ota_0, app, ota_0, 0x20000, 0x1E0000,
9 ota_1, app, ota_1, , 1900K, ota_1, app, ota_1, , 0x1E0000,
10 fctry, data, nvs, , 0x6000 fctry, data, nvs, 0x3E0000, 0x6000
+29 -8
View File
@@ -10,6 +10,10 @@ CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
# Enable Extended advertisement of NIMBLE
CONFIG_BT_NIMBLE_EXT_ADV=y
CONFIG_BT_NIMBLE_MAX_EXT_ADV_INSTANCES=2
#disable BT connection reattempt
CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT=n
@@ -45,11 +49,28 @@ CONFIG_MBEDTLS_HKDF_C=y
# unique local addresses for fabrics(MAX_FABRIC), a link local address(1)
CONFIG_LWIP_IPV6_NUM_ADDRESSES=6
# ESP32-DevKitC Settings
# Buttons
CONFIG_BSP_BUTTONS_NUM=1
CONFIG_BSP_BUTTON_1_TYPE_GPIO=y
CONFIG_BSP_BUTTON_1_GPIO=0
CONFIG_BSP_BUTTON_1_LEVEL=0
# LEDs
CONFIG_BSP_LEDS_NUM=0
# mbedtls
CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
CONFIG_MBEDTLS_DYNAMIC_FREE_PEER_CERT=y
CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN=y
# Fix for Timer Overflows
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120
# External Platform
CONFIG_CHIP_ENABLE_EXTERNAL_PLATFORM=y
CONFIG_CHIP_EXTERNAL_PLATFORM_DIR="../../../../examples/common/secondary_ble_adv/platform/ESP32_custom"
CONFIG_CHIP_EXTERNAL_PLATFORM_INCLUDE_DIR="../../../../examples/common/secondary_ble_adv"
# Additional configurations
CONFIG_ESP_RMAKER_USER_ID_CHECK=y
CONFIG_ESP_RMAKER_NO_CLAIM=y
CONFIG_ESP_RMAKER_USE_ESP_SECURE_CERT_MGR=y
CONFIG_ESP_RMAKER_READ_NODE_ID_FROM_CERT_CN=y
CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL="fctry"
CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER=y
CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER=y
CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER=y
CONFIG_SEC_CERT_DAC_PROVIDER=y
@@ -1,59 +1,14 @@
#enable BT
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
# Enable Extended advertisement of NIMBLE
CONFIG_BT_NIMBLE_EXT_ADV=y
CONFIG_BT_NIMBLE_MAX_EXT_ADV_INSTANCES=2
#disable BT connection reattempt
CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT=n
#enable lwip ipv6 autoconfig
CONFIG_LWIP_IPV6_AUTOCONFIG=y
# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_OFFSET=0xC000
# Enable chip shell
CONFIG_ENABLE_CHIP_SHELL=y
#enable lwIP route hooks
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y
# Button
CONFIG_BUTTON_PERIOD_TIME_MS=20
CONFIG_BUTTON_LONG_PRESS_TIME_MS=5000
# Enable HKDF in mbedtls
CONFIG_MBEDTLS_HKDF_C=y
# ESP32-S3-DevKitC-1 Settings
# ESP32-C3-DevKitC-02 Settings
# Buttons
CONFIG_BSP_BUTTONS_NUM=1
CONFIG_BSP_BUTTON_1_TYPE_GPIO=y
CONFIG_BSP_BUTTON_1_GPIO=0
CONFIG_BSP_BUTTON_1_GPIO=9
CONFIG_BSP_BUTTON_1_LEVEL=0
# mbedtls
CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
CONFIG_MBEDTLS_DYNAMIC_FREE_PEER_CERT=y
CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN=y
# For additional security on reset to factory
CONFIG_ESP_RMAKER_USER_ID_CHECK=y
# Fix for Timer Overflows
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120
# External Platform
CONFIG_CHIP_ENABLE_EXTERNAL_PLATFORM=y
CONFIG_CHIP_EXTERNAL_PLATFORM_DIR="../../../../examples/common/secondary_ble_adv/platform/ESP32_custom"
CONFIG_CHIP_EXTERNAL_PLATFORM_INCLUDE_DIR="../../../../examples/common/secondary_ble_adv"
# LEDs
CONFIG_BSP_LEDS_NUM=1
CONFIG_BSP_LED_TYPE_RGB=y
CONFIG_BSP_LED_RGB_GPIO=8
CONFIG_BSP_LED_RGB_BACKEND_RMT=y
# Memory Optimization
CONFIG_ESP_WIFI_IRAM_OPT=n
@@ -66,6 +21,5 @@ CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE=256
CONFIG_MAX_EXCHANGE_CONTEXTS=5
CONFIG_MAX_BINDINGS=2
CONFIG_MAX_PEER_NODES=8
CONFIG_DISABLE_IPV4=y
CONFIG_NUM_TCP_ENDPOINTS=1
CONFIG_NUM_UDP_ENDPOINTS=6
@@ -1,56 +1,6 @@
#enable BT
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
# Enable Extended advertisement of NIMBLE
CONFIG_BT_NIMBLE_EXT_ADV=y
CONFIG_BT_NIMBLE_MAX_EXT_ADV_INSTANCES=2
#disable BT connection reattempt
CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT=n
#enable lwip ipv6 autoconfig
CONFIG_LWIP_IPV6_AUTOCONFIG=y
# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_OFFSET=0xC000
# Enable chip shell
CONFIG_ENABLE_CHIP_SHELL=y
#enable lwIP route hooks
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y
# Button
CONFIG_BUTTON_PERIOD_TIME_MS=20
CONFIG_BUTTON_LONG_PRESS_TIME_MS=5000
# Enable HKDF in mbedtls
CONFIG_MBEDTLS_HKDF_C=y
# ESP32-S3-DevKitC-1 Settings
# Buttons
CONFIG_BSP_BUTTONS_NUM=1
CONFIG_BSP_BUTTON_1_TYPE_GPIO=y
CONFIG_BSP_BUTTON_1_GPIO=0
CONFIG_BSP_BUTTON_1_LEVEL=0
# mbedtls
CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
CONFIG_MBEDTLS_DYNAMIC_FREE_PEER_CERT=y
CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN=y
# For additional security on reset to factory
CONFIG_ESP_RMAKER_USER_ID_CHECK=y
# Fix for Timer Overflows
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120
# External Platform
CONFIG_CHIP_ENABLE_EXTERNAL_PLATFORM=y
CONFIG_CHIP_EXTERNAL_PLATFORM_DIR="../../../../examples/common/secondary_ble_adv/platform/ESP32_custom"
CONFIG_CHIP_EXTERNAL_PLATFORM_INCLUDE_DIR="../../../../examples/common/secondary_ble_adv"