From 24bd75955fd4b36d749e12a58d44ad937ed04f49 Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Wed, 7 Aug 2019 18:51:35 +0800 Subject: [PATCH 001/146] newlib: Fix UT - test time adjustment happens linearly Obtaining accurate and adjustable time occurs closer to each other. --- components/newlib/test/test_time.c | 81 ++++++++++++++++-------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/components/newlib/test/test_time.c b/components/newlib/test/test_time.c index 92f1bf8999..a5abc3bd9c 100644 --- a/components/newlib/test/test_time.c +++ b/components/newlib/test/test_time.c @@ -223,16 +223,26 @@ static void get_time_task(void *pvParameters) static void start_measure(int64_t* sys_time, int64_t* real_time) { struct timeval tv_time; - *real_time = esp_timer_get_time(); - gettimeofday(&tv_time, NULL); + int64_t t1, t2; + do { + t1 = esp_timer_get_time(); + gettimeofday(&tv_time, NULL); + t2 = esp_timer_get_time(); + } while (t2 - t1 > 40); + *real_time = t2; *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec; } static void end_measure(int64_t* sys_time, int64_t* real_time) { struct timeval tv_time; - gettimeofday(&tv_time, NULL); - *real_time = esp_timer_get_time(); + int64_t t1, t2; + do { + t1 = esp_timer_get_time(); + gettimeofday(&tv_time, NULL); + t2 = esp_timer_get_time(); + } while (t2 - t1 > 40); + *real_time = t2; *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec; } @@ -253,51 +263,55 @@ static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real static void measure_time_task(void *pvParameters) { - struct timeval tv_time; - int64_t real_time_us[2]; - int64_t sys_time_us[2]; - int64_t delay_us = 2 * 1000000; // 2 sec xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters; + int64_t main_real_time_us[2]; + int64_t main_sys_time_us[2]; + struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0}; + TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL)); + struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000}; + adjtime(&delta, NULL); gettimeofday(&tv_time, NULL); - start_measure(&sys_time_us[0], &real_time_us[0]); - // although exit flag is set in another task, checking (exit_flag == false) is safe - while (exit_flag == false) { - ets_delay_us(delay_us); + start_measure(&main_sys_time_us[0], &main_real_time_us[0]); - end_measure(&sys_time_us[1], &real_time_us[1]); - result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us); + { + int64_t real_time_us[2]; + int64_t sys_time_us[2]; + int64_t delay_us = 2 * 1000000; // 2 sec + start_measure(&sys_time_us[0], &real_time_us[0]); + // although exit flag is set in another task, checking (exit_flag == false) is safe + while (exit_flag == false) { + ets_delay_us(delay_us); - sys_time_us[0] = sys_time_us[1]; - real_time_us[0] = real_time_us[1]; + end_measure(&sys_time_us[1], &real_time_us[1]); + result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us); + + sys_time_us[0] = sys_time_us[1]; + real_time_us[0] = real_time_us[1]; + } } + + end_measure(&main_sys_time_us[1], &main_real_time_us[1]); + result_adjtime_correction_us[0] = calc_correction("main", main_sys_time_us, main_real_time_us); + int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1]; + printf("\nresult of adjtime correction: %lli us, %lli us. delta = %lli us\n", result_adjtime_correction_us[0], result_adjtime_correction_us[1], delta_us); + TEST_ASSERT_INT_WITHIN(100, 0, delta_us); + xSemaphoreGive(*sema); vTaskDelete(NULL); } TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]") { - int64_t real_time_us[2]; - int64_t sys_time_us[2]; - exit_flag = false; - struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0}; - TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL)); - - struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000}; - adjtime(&delta, NULL); - gettimeofday(&tv_time, NULL); - xSemaphoreHandle exit_sema[2]; for (int i = 0; i < 2; ++i) { exit_sema[i] = xSemaphoreCreateBinary(); result_adjtime_correction_us[i] = 0; } - start_measure(&sys_time_us[0], &real_time_us[0]); - - xTaskCreatePinnedToCore(get_time_task, "get_time_task", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0); - xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1); + xTaskCreatePinnedToCore(get_time_task, "get_time_task", 4096, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0); + xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 4096, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1); printf("start waiting for 30 seconds\n"); vTaskDelay(30000 / portTICK_PERIOD_MS); @@ -311,13 +325,6 @@ TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]") } } - end_measure(&sys_time_us[1], &real_time_us[1]); - result_adjtime_correction_us[0] = calc_correction("main", sys_time_us, real_time_us); - - int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1]; - printf("\nresult of adjtime correction: %lli us, %lli us. delta = %lli us\n", result_adjtime_correction_us[0], result_adjtime_correction_us[1], delta_us); - TEST_ASSERT_INT_WITHIN(100, 0, delta_us); - for (int i = 0; i < 2; ++i) { vSemaphoreDelete(exit_sema[i]); } From c082d130754caf725a38a01041891e84322e7a7a Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Mon, 22 Apr 2019 14:09:24 +0800 Subject: [PATCH 002/146] uart/driver: Add module reset before enabling This commit prevents infinite restarts caused due to an interrupt flag was left uncleared. Closes: https://github.com/espressif/esp-idf/issues/1981 Closes: IDF-188 --- components/driver/uart.c | 44 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/components/driver/uart.c b/components/driver/uart.c index 3367d1d04a..253475a998 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -665,20 +665,35 @@ esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num) return ESP_OK; } +static periph_module_t get_periph_module(uart_port_t uart_num) +{ + periph_module_t periph_module = PERIPH_UART0_MODULE; + if (uart_num == UART_NUM_0) { + periph_module = PERIPH_UART0_MODULE; + } else if (uart_num == UART_NUM_1) { + periph_module = PERIPH_UART1_MODULE; + } +#if SOC_UART_NUM > 2 + else if (uart_num == UART_NUM_2) { + periph_module = PERIPH_UART2_MODULE; + } +#endif + else { + assert(0 && "uart_num error"); + } + return periph_module; +} + esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config) { esp_err_t r; UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_config), "param null", ESP_FAIL); - if(uart_num == UART_NUM_0) { - periph_module_enable(PERIPH_UART0_MODULE); - } else if(uart_num == UART_NUM_1) { - periph_module_enable(PERIPH_UART1_MODULE); -#if UART_NUM > 2 - } else if(uart_num == UART_NUM_2) { - periph_module_enable(PERIPH_UART2_MODULE); -#endif + periph_module_t periph_module = get_periph_module(uart_num); + if (uart_num != CONFIG_ESP_CONSOLE_UART_NUM) { + periph_module_reset(periph_module); } + periph_module_enable(periph_module); r = uart_set_hw_flow_ctrl(uart_num, uart_config->flow_ctrl, uart_config->rx_flow_ctrl_thresh); if (r != ESP_OK) return r; @@ -1463,16 +1478,9 @@ esp_err_t uart_driver_delete(uart_port_t uart_num) free(p_uart_obj[uart_num]); p_uart_obj[uart_num] = NULL; - if (uart_num != CONFIG_ESP_CONSOLE_UART_NUM ) { - if(uart_num == UART_NUM_0) { - periph_module_disable(PERIPH_UART0_MODULE); - } else if(uart_num == UART_NUM_1) { - periph_module_disable(PERIPH_UART1_MODULE); -#if UART_NUM > 2 - } else if(uart_num == UART_NUM_2) { - periph_module_disable(PERIPH_UART2_MODULE); -#endif - } + if (uart_num != CONFIG_ESP_CONSOLE_UART_NUM) { + periph_module_t periph_module = get_periph_module(uart_num); + periph_module_disable(periph_module); } return ESP_OK; } From d064cd485c0011bbc1edbc83133a1d41461f1f60 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Mon, 13 May 2019 19:45:18 +0800 Subject: [PATCH 003/146] can/driver: Add module reset before enabling --- components/driver/can.c | 1 + 1 file changed, 1 insertion(+) diff --git a/components/driver/can.c b/components/driver/can.c index ec843e4920..933994b058 100644 --- a/components/driver/can.c +++ b/components/driver/can.c @@ -677,6 +677,7 @@ esp_err_t can_driver_install(const can_general_config_t *g_config, const can_tim ret = ESP_ERR_INVALID_STATE; goto err; } + periph_module_reset(PERIPH_CAN_MODULE); periph_module_enable(PERIPH_CAN_MODULE); //Enable APB CLK to CAN peripheral configASSERT(can_enter_reset_mode() == ESP_OK); //Must enter reset mode to write to config registers can_config_pelican(); //Use PeliCAN addresses From 437228e9477608cdb9dc5ad973bff909fe1329d9 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Mon, 13 May 2019 19:56:41 +0800 Subject: [PATCH 004/146] i2s/driver: Add module reset before enabling --- components/driver/i2s.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/components/driver/i2s.c b/components/driver/i2s.c index fe90c9c78b..10a38b15ba 100644 --- a/components/driver/i2s.c +++ b/components/driver/i2s.c @@ -886,12 +886,6 @@ static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_co I2S_CHECK(!((i2s_config->mode & I2S_MODE_DAC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S DAC built-in only support on I2S0", ESP_ERR_INVALID_ARG); I2S_CHECK(!((i2s_config->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG); - if (i2s_num == I2S_NUM_1) { - periph_module_enable(PERIPH_I2S1_MODULE); - } else { - periph_module_enable(PERIPH_I2S0_MODULE); - } - if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) { //in ADC built-in mode, we need to call i2s_set_adc_mode to //initialize the specific ADC channel. @@ -1099,8 +1093,10 @@ esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, //To make sure hardware is enabled before any hardware register operations. if (i2s_num == I2S_NUM_1) { + periph_module_reset(PERIPH_I2S1_MODULE); periph_module_enable(PERIPH_I2S1_MODULE); } else { + periph_module_reset(PERIPH_I2S0_MODULE); periph_module_enable(PERIPH_I2S0_MODULE); } From 5c560e0cd1abb33aa6f7b5eb18843f4c3e5e5c11 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Mon, 13 May 2019 20:39:16 +0800 Subject: [PATCH 005/146] sdmmc_host/driver: Add module reset before enabling --- components/driver/sdmmc_host.c | 1 + 1 file changed, 1 insertion(+) diff --git a/components/driver/sdmmc_host.c b/components/driver/sdmmc_host.c index c1361829bc..bab540175a 100644 --- a/components/driver/sdmmc_host.c +++ b/components/driver/sdmmc_host.c @@ -222,6 +222,7 @@ esp_err_t sdmmc_host_init(void) return ESP_ERR_INVALID_STATE; } + periph_module_reset(PERIPH_SDMMC_MODULE); periph_module_enable(PERIPH_SDMMC_MODULE); // Enable clock to peripheral. Use smallest divider first. From a418b603d0b0b91699d80a144e8e00726d5adb89 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Tue, 14 May 2019 20:01:35 +0800 Subject: [PATCH 006/146] pcnt/driver: Add module reset before enabling --- components/driver/pcnt.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/driver/pcnt.c b/components/driver/pcnt.c index 249a7f8288..32ceb23483 100644 --- a/components/driver/pcnt.c +++ b/components/driver/pcnt.c @@ -58,6 +58,11 @@ esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config) PCNT_CHECK((pcnt_config->pos_mode < PCNT_COUNT_MAX) && (pcnt_config->neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG); PCNT_CHECK((pcnt_config->hctrl_mode < PCNT_MODE_MAX) && (pcnt_config->lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG); /*Enalbe hardware module*/ + static bool pcnt_enable = false; + if (pcnt_enable == false) { + periph_module_reset(PERIPH_PCNT_MODULE); + pcnt_enable = true; + } periph_module_enable(PERIPH_PCNT_MODULE); /*Set counter range*/ pcnt_set_event_value(unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim); From 9a669bfbb913a8e4df927330c9b0aeb642088ba5 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Tue, 14 May 2019 20:02:01 +0800 Subject: [PATCH 007/146] rmt/driver: Add module reset before enabling --- components/driver/rmt.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/driver/rmt.c b/components/driver/rmt.c index 38380da30a..966600a956 100644 --- a/components/driver/rmt.c +++ b/components/driver/rmt.c @@ -424,6 +424,11 @@ esp_err_t rmt_config(const rmt_config_t* rmt_param) RMT_CHECK((!carrier_en || carrier_freq_hz > 0), "RMT carrier frequency can't be zero", ESP_ERR_INVALID_ARG); } + static bool rmt_enable = false; + if (rmt_enable == false) { + periph_module_reset(PERIPH_RMT_MODULE); + rmt_enable = true; + } periph_module_enable(PERIPH_RMT_MODULE); RMT.conf_ch[channel].conf0.div_cnt = clk_div; From 5b9e38fe4e43a8ac60bcd7825c95fe35f3a97470 Mon Sep 17 00:00:00 2001 From: Sergei Silnov Date: Tue, 13 Aug 2019 11:35:51 +0200 Subject: [PATCH 008/146] idf.py: Fix PropertyDict implementation --- tools/idf.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/idf.py b/tools/idf.py index 1083d1d026..a8fe6089df 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -524,9 +524,20 @@ def get_default_serial_port(): class PropertyDict(dict): - def __init__(self, *args, **kwargs): - super(PropertyDict, self).__init__(*args, **kwargs) - self.__dict__ = self + def __getattr__(self, name): + if name in self: + return self[name] + else: + raise AttributeError("'PropertyDict' object has no attribute '%s'" % name) + + def __setattr__(self, name, value): + self[name] = value + + def __delattr__(self, name): + if name in self: + del self[name] + else: + raise AttributeError("'PropertyDict' object has no attribute '%s'" % name) def init_cli(): From f72dc5b2715f84aeb0fe59603b344e19d2c82e47 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 14 Aug 2019 10:03:27 +1000 Subject: [PATCH 009/146] Update cJSON to v1.7.12 Fixes potential DoS in cJSON_Minify, see https://github.com/DaveGamble/cJSON/issues/354 --- components/json/cJSON | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/json/cJSON b/components/json/cJSON index 93688cbe72..3c8935676a 160000 --- a/components/json/cJSON +++ b/components/json/cJSON @@ -1 +1 @@ -Subproject commit 93688cbe72b190300d1be6b98e86b772df9b9ead +Subproject commit 3c8935676a97c7c97bf006db8312875b4f292f6c From 88a69823cfb23359a0bbb116ea71355c59756739 Mon Sep 17 00:00:00 2001 From: boarchuz <46267286+boarchuz@users.noreply.github.com> Date: Mon, 3 Jun 2019 19:32:38 +1000 Subject: [PATCH 010/146] ulp: Expand ULP macro functionality Merges https://github.com/espressif/esp-idf/pull/3580 --- components/ulp/include/esp32/ulp.h | 233 +++++++++++++++++++++++++++-- components/ulp/ulp_macro.c | 87 +++++++---- 2 files changed, 277 insertions(+), 43 deletions(-) diff --git a/components/ulp/include/esp32/ulp.h b/components/ulp/include/esp32/ulp.h index c9ca511017..d479913f96 100644 --- a/components/ulp/include/esp32/ulp.h +++ b/components/ulp/include/esp32/ulp.h @@ -46,20 +46,22 @@ extern "C" { * @{ */ -#define OPCODE_WR_REG 1 /*!< Instruction: write peripheral register (RTC_CNTL/RTC_IO/SARADC) (not implemented yet) */ +#define OPCODE_WR_REG 1 /*!< Instruction: write peripheral register (RTC_CNTL/RTC_IO/SARADC) */ -#define OPCODE_RD_REG 2 /*!< Instruction: read peripheral register (RTC_CNTL/RTC_IO/SARADC) (not implemented yet) */ +#define OPCODE_RD_REG 2 /*!< Instruction: read peripheral register (RTC_CNTL/RTC_IO/SARADC) */ #define RD_REG_PERIPH_RTC_CNTL 0 /*!< Identifier of RTC_CNTL peripheral for RD_REG and WR_REG instructions */ #define RD_REG_PERIPH_RTC_IO 1 /*!< Identifier of RTC_IO peripheral for RD_REG and WR_REG instructions */ #define RD_REG_PERIPH_SENS 2 /*!< Identifier of SARADC peripheral for RD_REG and WR_REG instructions */ #define RD_REG_PERIPH_RTC_I2C 3 /*!< Identifier of RTC_I2C peripheral for RD_REG and WR_REG instructions */ -#define OPCODE_I2C 3 /*!< Instruction: read/write I2C (not implemented yet) */ +#define OPCODE_I2C 3 /*!< Instruction: read/write I2C */ +#define SUB_OPCODE_I2C_RD 0 /*!< I2C read */ +#define SUB_OPCODE_I2C_WR 1 /*!< I2C write */ #define OPCODE_DELAY 4 /*!< Instruction: delay (nop) for a given number of cycles */ -#define OPCODE_ADC 5 /*!< Instruction: SAR ADC measurement (not implemented yet) */ +#define OPCODE_ADC 5 /*!< Instruction: SAR ADC measurement */ #define OPCODE_ST 6 /*!< Instruction: store indirect to RTC memory */ #define SUB_OPCODE_ST 4 /*!< Store 32 bits, 16 MSBs contain PC, 16 LSBs contain value from source register */ @@ -67,7 +69,7 @@ extern "C" { #define OPCODE_ALU 7 /*!< Arithmetic instructions */ #define SUB_OPCODE_ALU_REG 0 /*!< Arithmetic instruction, both source values are in register */ #define SUB_OPCODE_ALU_IMM 1 /*!< Arithmetic instruction, one source value is an immediate */ -#define SUB_OPCODE_ALU_CNT 2 /*!< Arithmetic instruction between counter register and an immediate (not implemented yet)*/ +#define SUB_OPCODE_ALU_CNT 2 /*!< Arithmetic instruction, stage counter and an immediate */ #define ALU_SEL_ADD 0 /*!< Addition */ #define ALU_SEL_SUB 1 /*!< Subtraction */ #define ALU_SEL_AND 2 /*!< Logical AND */ @@ -75,21 +77,29 @@ extern "C" { #define ALU_SEL_MOV 4 /*!< Copy value (immediate to destination register or source register to destination register */ #define ALU_SEL_LSH 5 /*!< Shift left by given number of bits */ #define ALU_SEL_RSH 6 /*!< Shift right by given number of bits */ +#define ALU_SEL_SINC 0 /*!< Increment the stage counter */ +#define ALU_SEL_SDEC 1 /*!< Decrement the stage counter */ +#define ALU_SEL_SRST 2 /*!< Reset the stage counter */ #define OPCODE_BRANCH 8 /*!< Branch instructions */ #define SUB_OPCODE_BX 0 /*!< Branch to absolute PC (immediate or in register) */ +#define SUB_OPCODE_BR 1 /*!< Branch to relative PC, conditional on R0 */ +#define SUB_OPCODE_BS 2 /*!< Branch to relative PC, conditional on the stage counter */ #define BX_JUMP_TYPE_DIRECT 0 /*!< Unconditional jump */ #define BX_JUMP_TYPE_ZERO 1 /*!< Branch if last ALU result is zero */ #define BX_JUMP_TYPE_OVF 2 /*!< Branch if last ALU operation caused and overflow */ #define SUB_OPCODE_B 1 /*!< Branch to a relative offset */ #define B_CMP_L 0 /*!< Branch if R0 is less than an immediate */ #define B_CMP_GE 1 /*!< Branch if R0 is greater than or equal to an immediate */ +#define JUMPS_LT 0 /*!< Branch if the stage counter < */ +#define JUMPS_GE 1 /*!< Branch if the stage counter >= */ +#define JUMPS_LE 2 /*!< Branch if the stage counter <= */ #define OPCODE_END 9 /*!< Stop executing the program */ #define SUB_OPCODE_END 0 /*!< Stop executing the program and optionally wake up the chip */ #define SUB_OPCODE_SLEEP 1 /*!< Stop executing the program and run it again after selected interval */ -#define OPCODE_TSENS 10 /*!< Instruction: temperature sensor measurement (not implemented yet) */ +#define OPCODE_TSENS 10 /*!< Instruction: temperature sensor measurement */ #define OPCODE_HALT 11 /*!< Halt the coprocessor */ @@ -98,6 +108,7 @@ extern "C" { #define OPCODE_MACRO 15 /*!< Not a real opcode. Used to identify labels and branches in the program */ #define SUB_OPCODE_MACRO_LABEL 0 /*!< Label macro */ #define SUB_OPCODE_MACRO_BRANCH 1 /*!< Branch macro */ +#define SUB_OPCODE_MACRO_LABELPC 2 /*!< Label pointer macro */ /**@}*/ /**@{*/ @@ -173,7 +184,17 @@ typedef union { uint32_t sign : 1; /*!< Sign of target PC offset: 0: positive, 1: negative */ uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_B) */ uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */ - } b; /*!< Format of BRANCH instruction (relative address) */ + } b; /*!< Format of BRANCH instruction (relative address, conditional on R0) */ + + struct { + uint32_t imm : 8; /*!< Immediate value to compare against */ + uint32_t unused : 7; /*!< Unused */ + uint32_t cmp : 2; /*!< Comparison to perform: JUMPS_LT, JUMPS_GE or JUMPS_LE */ + uint32_t offset : 7; /*!< Absolute value of target PC offset w.r.t. current PC, expressed in words */ + uint32_t sign : 1; /*!< Sign of target PC offset: 0: positive, 1: negative */ + uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_BS) */ + uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */ + } bs; /*!< Format of BRANCH instruction (relative address, conditional on the stage counter) */ struct { uint32_t dreg : 2; /*!< Destination register */ @@ -185,6 +206,15 @@ typedef union { uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */ } alu_reg; /*!< Format of ALU instruction (both sources are registers) */ + struct { + uint32_t unused1 : 4; /*!< Unused */ + uint32_t imm : 8; /*!< Immediate value of operand */ + uint32_t unused2 : 9; /*!< Unused */ + uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_Sxxx */ + uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_CNT) */ + uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */ + } alu_reg_s; /*!< Format of ALU instruction (stage counter and an immediate) */ + struct { uint32_t dreg : 2; /*!< Destination register */ uint32_t sreg : 2; /*!< Register with operand A */ @@ -232,10 +262,10 @@ typedef union { struct { uint32_t i2c_addr : 8; /*!< I2C slave address */ - uint32_t data : 8; /*!< Data to read or write */ - uint32_t low_bits : 3; /*!< TBD */ - uint32_t high_bits : 3; /*!< TBD */ - uint32_t i2c_sel : 4; /*!< TBD, select reg_i2c_slave_address[7:0] */ + uint32_t data : 8; /*!< 8 bits of data for write operation */ + uint32_t low_bits : 3; /*!< number of low bits to mask for write operation */ + uint32_t high_bits : 3; /*!< number of high bits to mask for write operation */ + uint32_t i2c_sel : 4; /*!< index of slave address register [7:0] */ uint32_t unused : 1; /*!< Unused */ uint32_t rw : 1; /*!< Write (1) or read (0) */ uint32_t opcode : 4; /*!< Opcode (OPCODE_I2C) */ @@ -256,11 +286,12 @@ typedef union { } sleep; /*!< Format of END instruction with sleep */ struct { + uint32_t dreg : 2; /*!< Destination register (for SUB_OPCODE_MACRO_LABELPC) > */ uint32_t label : 16; /*!< Label number */ - uint32_t unused : 8; /*!< Unused */ - uint32_t sub_opcode : 4; /*!< SUB_OPCODE_MACRO_LABEL or SUB_OPCODE_MACRO_BRANCH */ + uint32_t unused : 6; /*!< Unused */ + uint32_t sub_opcode : 4; /*!< SUB_OPCODE_MACRO_LABEL or SUB_OPCODE_MACRO_BRANCH or SUB_OPCODE_MACRO_LABELPC */ uint32_t opcode: 4; /*!< Opcode (OPCODE_MACRO) */ - } macro; /*!< Format of tokens used by LABEL and BRANCH macros */ + } macro; /*!< Format of tokens used by MACROs */ } ulp_insn_t; @@ -763,6 +794,7 @@ static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) { * below. */ #define M_LABEL(label_num) { .macro = { \ + .dreg = 0, \ .label = label_num, \ .unused = 0, \ .sub_opcode = SUB_OPCODE_MACRO_LABEL, \ @@ -772,11 +804,35 @@ static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) { * Token macro used by M_B and M_BX macros. Not to be used directly. */ #define M_BRANCH(label_num) { .macro = { \ + .dreg = 0, \ .label = label_num, \ .unused = 0, \ .sub_opcode = SUB_OPCODE_MACRO_BRANCH, \ .opcode = OPCODE_MACRO } } +/** + * Token macro used by M_MOVL macro. Not to be used directly. + */ +#define M_LABELPC(label_num) { .macro = { \ + .dreg = 0, \ + .label = label_num, \ + .unused = 0, \ + .sub_opcode = SUB_OPCODE_MACRO_LABELPC, \ + .opcode = OPCODE_MACRO } } + +/** + * Macro: Move the program counter at the given label into the register. + * This address can then be used with I_BXR, I_BXZR, I_BXFR, etc. + * + * This macro generates two ulp_insn_t values separated by a comma, and should + * be used when defining contents of ulp_insn_t arrays. First value is not a + * real instruction; it is a token which is removed by ulp_process_macros_and_load + * function. + */ +#define M_MOVL(reg_dest, label_num) \ + M_LABELPC(label_num), \ + I_MOVI(reg_dest, 0) + /** * Macro: branch to label label_num if R0 is less than immediate value. * @@ -837,7 +893,154 @@ static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) { M_BRANCH(label_num), \ I_BXFI(0) +/** + * Increment the stage counter by immediate value + */ +#define I_STAGE_INC(imm_) { .alu_reg_s = { \ + .unused1 = 0, \ + .imm = imm_, \ + .unused2 = 0, \ + .sel = ALU_SEL_SINC, \ + .sub_opcode = SUB_OPCODE_ALU_CNT, \ + .opcode = OPCODE_ALU } } +/** + * Decrement the stage counter by immediate value + */ +#define I_STAGE_DEC(imm_) { .alu_reg_s = { \ + .unused1 = 0, \ + .imm = imm_, \ + .unused2 = 0, \ + .sel = ALU_SEL_SDEC, \ + .sub_opcode = SUB_OPCODE_ALU_CNT, \ + .opcode = OPCODE_ALU } } + +/** + * Reset the stage counter + */ +#define I_STAGE_RST() { .alu_reg_s = { \ + .unused1 = 0, \ + .imm = 0, \ + .unused2 = 0, \ + .sel = ALU_SEL_SRST, \ + .sub_opcode = SUB_OPCODE_ALU_CNT, \ + .opcode = OPCODE_ALU } } + +/** + * Macro: branch to label if the stage counter is less than immediate value + * + * This macro generates two ulp_insn_t values separated by a comma, and should + * be used when defining contents of ulp_insn_t arrays. First value is not a + * real instruction; it is a token which is removed by ulp_process_macros_and_load + * function. + */ +#define M_BSLT(label_num, imm_value) \ + M_BRANCH(label_num), \ + I_JUMPS(0, imm_value, JUMPS_LT) + +/** + * Macro: branch to label if the stage counter is greater than or equal to immediate value + * + * This macro generates two ulp_insn_t values separated by a comma, and should + * be used when defining contents of ulp_insn_t arrays. First value is not a + * real instruction; it is a token which is removed by ulp_process_macros_and_load + * function. + */ +#define M_BSGE(label_num, imm_value) \ + M_BRANCH(label_num), \ + I_JUMPS(0, imm_value, JUMPS_GE) + +/** + * Macro: branch to label if the stage counter is less than or equal to immediate value + * + * This macro generates two ulp_insn_t values separated by a comma, and should + * be used when defining contents of ulp_insn_t arrays. First value is not a + * real instruction; it is a token which is removed by ulp_process_macros_and_load + * function. + */ +#define M_BSLE(label_num, imm_value) \ + M_BRANCH(label_num), \ + I_JUMPS(0, imm_value, JUMPS_LE) + +/** + * Macro: branch to label if the stage counter is equal to immediate value. + * Implemented using two JUMPS instructions: + * JUMPS next, imm_value, LT + * JUMPS label_num, imm_value, LE + * + * This macro generates three ulp_insn_t values separated by commas, and should + * be used when defining contents of ulp_insn_t arrays. Second value is not a + * real instruction; it is a token which is removed by ulp_process_macros_and_load + * function. + */ +#define M_BSEQ(label_num, imm_value) \ + I_JUMPS(2, imm_value, JUMPS_LT), \ + M_BRANCH(label_num), \ + I_JUMPS(0, imm_value, JUMPS_LE) + +/** + * Macro: branch to label if the stage counter is greater than immediate value. + * Implemented using two instructions: + * JUMPS next, imm_value, LE + * JUMPS label_num, imm_value, GE + * + * This macro generates three ulp_insn_t values separated by commas, and should + * be used when defining contents of ulp_insn_t arrays. Second value is not a + * real instruction; it is a token which is removed by ulp_process_macros_and_load + * function. + */ +#define M_BSGT(label_num, imm_value) \ + I_JUMPS(2, imm_value, JUMPS_LE), \ + M_BRANCH(label_num), \ + I_JUMPS(0, imm_value, JUMPS_GE) + +/** + * Branch relative if (stage counter [comp_type] [imm_value]) evaluates to true. + * + * pc_offset is expressed in words, and can be from -127 to 127 + * imm_value is an 8-bit value to compare the stage counter against + * comp_type is the type of comparison to perform: JUMPS_LT (<), JUMPS_GE (>=) or JUMPS_LE (<=) + */ +#define I_JUMPS(pc_offset, imm_value, comp_type) { .bs = { \ + .imm = imm_value, \ + .unused = 0, \ + .cmp = comp_type, \ + .offset = abs(pc_offset), \ + .sign = (pc_offset >= 0) ? 0 : 1, \ + .sub_opcode = SUB_OPCODE_BS, \ + .opcode = OPCODE_BRANCH } } + +/** + * Perform an I2C transaction with a slave device. + * I_I2C_READ and I_I2C_WRITE are provided for convenience, instead of using this directly. + * + * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel. + * For read operations, 8 bits of read result is stored into R0 register. + * For write operations, bits outside range [high_bit:low_bit] of val are masked. + */ +#define I_I2C_RW(sub_addr, val, low_bit, high_bit, slave_sel, rw_bit) { .i2c = {\ + .i2c_addr = sub_addr, \ + .data = val, \ + .low_bits = low_bit, \ + .high_bits = high_bit, \ + .i2c_sel = slave_sel, \ + .unused = 0, \ + .rw = rw_bit, \ + .opcode = OPCODE_I2C } } + +/** + * Read a byte from the sub address of an I2C slave, and store the result in R0. + * + * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel. + */ +#define I_I2C_READ(slave_sel, sub_addr) I_I2C_RW(sub_addr, 0, 0, 0, slave_sel, SUB_OPCODE_I2C_RD) + +/** + * Write a byte to the sub address of an I2C slave. + * + * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel. + */ +#define I_I2C_WRITE(slave_sel, sub_addr, val) I_I2C_RW(sub_addr, val, 0, 7, slave_sel, SUB_OPCODE_I2C_WR) #define RTC_SLOW_MEM ((uint32_t*) 0x50000000) /*!< RTC slow memory, 8k size */ @@ -917,4 +1120,4 @@ esp_err_t ulp_set_wakeup_period(size_t period_index, uint32_t period_us); #ifdef __cplusplus } -#endif +#endif \ No newline at end of file diff --git a/components/ulp/ulp_macro.c b/components/ulp/ulp_macro.c index c9b6e6dcbd..619094ae53 100644 --- a/components/ulp/ulp_macro.c +++ b/components/ulp/ulp_macro.c @@ -38,6 +38,7 @@ typedef struct { #define RELOC_TYPE_LABEL 0 #define RELOC_TYPE_BRANCH 1 +#define RELOC_TYPE_LABELPC 2 /* This record means: there is a label at address * insn_addr, with number label_num. @@ -58,6 +59,16 @@ typedef struct { .unused = 0, \ .type = RELOC_TYPE_BRANCH } +/* This record means: there is a move instruction at insn_addr, + * imm needs to be changed to the program counter of the instruction + * at label label_num. + */ +#define RELOC_INFO_LABELPC(label_num, insn_addr) (reloc_info_t) { \ + .label = label_num, \ + .addr = insn_addr, \ + .unused = 0, \ + .type = RELOC_TYPE_LABELPC } + /* Comparison function used to sort the relocations array */ static int reloc_sort_func(const void* p_lhs, const void* p_rhs) { @@ -110,45 +121,61 @@ static int reloc_sort_func(const void* p_lhs, const void* p_rhs) * For each label number, label entry comes first * because the array was sorted at the previous step. * Label address is recorded, and all subsequent - * "branch" entries which point to the same label number - * are processed. For each branch entry, correct offset - * or absolute address is calculated, depending on branch - * type, and written into the appropriate field of - * the instruction. + * entries which point to the same label number + * are processed. For each entry, correct offset + * or absolute address is calculated, depending on + * type and subtype, and written into the appropriate + * field of the instruction. * */ static esp_err_t do_single_reloc(ulp_insn_t* program, uint32_t load_addr, - reloc_info_t label_info, reloc_info_t branch_info) + reloc_info_t label_info, reloc_info_t the_reloc) { - size_t insn_offset = branch_info.addr - load_addr; + size_t insn_offset = the_reloc.addr - load_addr; ulp_insn_t* insn = &program[insn_offset]; - // B and BX have the same layout of opcode/sub_opcode fields, - // and share the same opcode - assert(insn->b.opcode == OPCODE_BRANCH - && "branch macro was applied to a non-branch instruction"); - switch (insn->b.sub_opcode) { - case SUB_OPCODE_B: { - int32_t offset = ((int32_t) label_info.addr) - ((int32_t) branch_info.addr); - uint32_t abs_offset = abs(offset); - uint32_t sign = (offset >= 0) ? 0 : 1; - if (abs_offset > 127) { - ESP_LOGW(TAG, "target out of range: branch from %x to %x", - branch_info.addr, label_info.addr); - return ESP_ERR_ULP_BRANCH_OUT_OF_RANGE; + + switch (the_reloc.type) { + case RELOC_TYPE_BRANCH: { + // B, BS and BX have the same layout of opcode/sub_opcode fields, + // and share the same opcode. B and BS also have the same layout of + // offset and sign fields. + assert(insn->b.opcode == OPCODE_BRANCH + && "branch macro was applied to a non-branch instruction"); + switch (insn->b.sub_opcode) { + case SUB_OPCODE_B: + case SUB_OPCODE_BS:{ + int32_t offset = ((int32_t) label_info.addr) - ((int32_t) the_reloc.addr); + uint32_t abs_offset = abs(offset); + uint32_t sign = (offset >= 0) ? 0 : 1; + if (abs_offset > 127) { + ESP_LOGW(TAG, "target out of range: branch from %x to %x", + the_reloc.addr, label_info.addr); + return ESP_ERR_ULP_BRANCH_OUT_OF_RANGE; + } + insn->b.offset = abs_offset; //== insn->bs.offset = abs_offset; + insn->b.sign = sign; //== insn->bs.sign = sign; + break; + } + case SUB_OPCODE_BX:{ + assert(insn->bx.reg == 0 && + "relocation applied to a jump with offset in register"); + insn->bx.addr = label_info.addr; + break; + } + default: + assert(false && "unexpected branch sub-opcode"); } - insn->b.offset = abs_offset; - insn->b.sign = sign; break; } - case SUB_OPCODE_BX: { - assert(insn->bx.reg == 0 && - "relocation applied to a jump with offset in register"); - insn->bx.addr = label_info.addr; + case RELOC_TYPE_LABELPC: { + assert((insn->alu_imm.opcode == OPCODE_ALU && insn->alu_imm.sub_opcode == SUB_OPCODE_ALU_IMM && insn->alu_imm.sel == ALU_SEL_MOV) + && "pc macro was applied to an incompatible instruction"); + insn->alu_imm.imm = label_info.addr; break; } default: - assert(false && "unexpected sub-opcode"); + assert(false && "unknown reloc type"); } return ESP_OK; } @@ -199,7 +226,7 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog while (read_ptr < end) { ulp_insn_t r_insn = *read_ptr; if (r_insn.macro.opcode == OPCODE_MACRO) { - switch(r_insn.macro.sub_opcode) { + switch (r_insn.macro.sub_opcode) { case SUB_OPCODE_MACRO_LABEL: *cur_reloc = RELOC_INFO_LABEL(r_insn.macro.label, cur_insn_addr); @@ -208,6 +235,10 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog *cur_reloc = RELOC_INFO_BRANCH(r_insn.macro.label, cur_insn_addr); break; + case SUB_OPCODE_MACRO_LABELPC: + *cur_reloc = RELOC_INFO_LABELPC(r_insn.macro.label, + cur_insn_addr); + break; default: assert(0 && "invalid sub_opcode for macro insn"); } From 68b3677daa00610f650b6059cb9d81996c2311b6 Mon Sep 17 00:00:00 2001 From: boarchuz <46267286+boarchuz@users.noreply.github.com> Date: Thu, 13 Jun 2019 18:20:31 +1000 Subject: [PATCH 011/146] ulp: Correct misleading i2c write mask comments Merges https://github.com/espressif/esp-idf/pull/3580 --- components/ulp/include/esp32/ulp.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/ulp/include/esp32/ulp.h b/components/ulp/include/esp32/ulp.h index d479913f96..d273565557 100644 --- a/components/ulp/include/esp32/ulp.h +++ b/components/ulp/include/esp32/ulp.h @@ -263,8 +263,8 @@ typedef union { struct { uint32_t i2c_addr : 8; /*!< I2C slave address */ uint32_t data : 8; /*!< 8 bits of data for write operation */ - uint32_t low_bits : 3; /*!< number of low bits to mask for write operation */ - uint32_t high_bits : 3; /*!< number of high bits to mask for write operation */ + uint32_t low_bits : 3; /*!< low bit of range for write operation (lower bits are masked) */ + uint32_t high_bits : 3; /*!< high bit of range for write operation (higher bits are masked) */ uint32_t i2c_sel : 4; /*!< index of slave address register [7:0] */ uint32_t unused : 1; /*!< Unused */ uint32_t rw : 1; /*!< Write (1) or read (0) */ @@ -1016,7 +1016,7 @@ static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) { * * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel. * For read operations, 8 bits of read result is stored into R0 register. - * For write operations, bits outside range [high_bit:low_bit] of val are masked. + * For write operations, bits outside of val[high_bit:low_bit] are masked. */ #define I_I2C_RW(sub_addr, val, low_bit, high_bit, slave_sel, rw_bit) { .i2c = {\ .i2c_addr = sub_addr, \ From 28ca2d72b879c2bdb2cf7d50d02136f293eb0b88 Mon Sep 17 00:00:00 2001 From: boarchuz <46267286+boarchuz@users.noreply.github.com> Date: Fri, 14 Jun 2019 06:03:56 +1000 Subject: [PATCH 012/146] ulp: Correct misleading corrections of i2c comments Merges https://github.com/espressif/esp-idf/pull/3580 --- components/ulp/include/esp32/ulp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/ulp/include/esp32/ulp.h b/components/ulp/include/esp32/ulp.h index d273565557..b045ccc3d9 100644 --- a/components/ulp/include/esp32/ulp.h +++ b/components/ulp/include/esp32/ulp.h @@ -1016,7 +1016,7 @@ static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) { * * Slave address (in 7-bit format) has to be set in advance into SENS_I2C_SLAVE_ADDRx register field, where x == slave_sel. * For read operations, 8 bits of read result is stored into R0 register. - * For write operations, bits outside of val[high_bit:low_bit] are masked. + * For write operations, val will be written to sub_addr at [high_bit:low_bit]. Bits outside of this range are masked. */ #define I_I2C_RW(sub_addr, val, low_bit, high_bit, slave_sel, rw_bit) { .i2c = {\ .i2c_addr = sub_addr, \ From ccc95191ea8fbe5ef6d37598dc45ffd2a935d413 Mon Sep 17 00:00:00 2001 From: Saket Dandawate Date: Wed, 10 Jul 2019 07:37:37 -0700 Subject: [PATCH 013/146] ulp: Add aditional uint32_t object to `ulp_insn_t` Used to get the encoded instruction from bit-field structs. Merges https://github.com/espressif/esp-idf/pull/3759 --- components/ulp/include/esp32/ulp.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/ulp/include/esp32/ulp.h b/components/ulp/include/esp32/ulp.h index b045ccc3d9..d55a8c6c7c 100644 --- a/components/ulp/include/esp32/ulp.h +++ b/components/ulp/include/esp32/ulp.h @@ -293,6 +293,8 @@ typedef union { uint32_t opcode: 4; /*!< Opcode (OPCODE_MACRO) */ } macro; /*!< Format of tokens used by MACROs */ + uint32_t instruction; /*!< Encoded instruction for ULP coprocessor */ + } ulp_insn_t; _Static_assert(sizeof(ulp_insn_t) == 4, "ULP coprocessor instruction size should be 4 bytes"); From 727ffbe90863dc1cf69ff7f2abc6a568df35168e Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 16 Aug 2019 13:00:21 +1000 Subject: [PATCH 014/146] esp_timer: Add parameter NULL checks to public API Return ESP_ERR_INVALID_ARG if the handle is NULL, instead of crashing. As reported via forum https://esp32.com/viewtopic.php?f=13&t=11721&p=47926#p47926 --- components/esp_common/src/esp_timer.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/components/esp_common/src/esp_timer.c b/components/esp_common/src/esp_timer.c index 034f77fcca..718b8100d1 100644 --- a/components/esp_common/src/esp_timer.c +++ b/components/esp_common/src/esp_timer.c @@ -104,7 +104,7 @@ esp_err_t esp_timer_create(const esp_timer_create_args_t* args, if (!is_initialized()) { return ESP_ERR_INVALID_STATE; } - if (args->callback == NULL) { + if (args == NULL || args->callback == NULL || out_handle == NULL) { return ESP_ERR_INVALID_ARG; } esp_timer_handle_t result = (esp_timer_handle_t) calloc(1, sizeof(*result)); @@ -123,6 +123,9 @@ esp_err_t esp_timer_create(const esp_timer_create_args_t* args, esp_err_t IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us) { + if (timer == NULL) { + return ESP_ERR_INVALID_ARG; + } if (!is_initialized() || timer_armed(timer)) { return ESP_ERR_INVALID_STATE; } @@ -136,6 +139,9 @@ esp_err_t IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t time esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us) { + if (timer == NULL) { + return ESP_ERR_INVALID_ARG; + } if (!is_initialized() || timer_armed(timer)) { return ESP_ERR_INVALID_STATE; } @@ -150,6 +156,9 @@ esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t esp_err_t IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer) { + if (timer == NULL) { + return ESP_ERR_INVALID_ARG; + } if (!is_initialized() || !timer_armed(timer)) { return ESP_ERR_INVALID_STATE; } From 2116968d783ff05e3406138713b4e449a2bafa6c Mon Sep 17 00:00:00 2001 From: chenyudong Date: Tue, 13 Aug 2019 15:58:43 +0800 Subject: [PATCH 015/146] test: fix test case MESH_EST_2403 --- .../idf_test/integration_test/TC_IT_MESH_EST.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/idf_test/integration_test/TC_IT_MESH_EST.yml b/components/idf_test/integration_test/TC_IT_MESH_EST.yml index 5422ea7de7..8e8d08bde5 100644 --- a/components/idf_test/integration_test/TC_IT_MESH_EST.yml +++ b/components/idf_test/integration_test/TC_IT_MESH_EST.yml @@ -4224,10 +4224,10 @@ test cases: - - SSC SSC1 reboot - - P SSC1 C !!!ready!!! - P SSC2 C MESH_EVENT_DISCONNECTE - - - SSC SSC1 meshset -O -o 0 -n 1 -t 1 - - - P SSC1 C +MESHSET:SELF_ORG,OK - - - SSC SSC1 meshset -T -o 1 - - - P SSC1 C MESHGET:TYPE,OK,0 + - - SSC SSC2 meshset -O -o 0 -n 1 -t 1 + - - P SSC2 C +MESHSET:SELF_ORG,OK + - - SSC SSC2 meshset -T -o 1 + - - P SSC2 C MESHGET:TYPE,OK,0 expected result: |- 1. succeed 2. succeed @@ -4235,7 +4235,7 @@ test cases: steps: |- 1. dut1 set AP, dut2 start mesh and connected with dut1 2. reboot dut1, dut2 disconnected with dut1 - 3. set self_organized(1,1) and check dut2 IDLE + 3. set dut2 self_organized(1,1) and check it IDLE initial condition: MESH_DEINIT_STA test environment: SSC_T2_MESH1 summary: set self_organized (1,1) to give up root state From ded0ac352d90cda800bd54ec5c71635ffe38229a Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Fri, 16 Aug 2019 17:46:50 +0800 Subject: [PATCH 016/146] cmake: set build dir for mconf-idf Fixes an issue where if idf_build_process is called in a CMake subdirectory, menuconfig looks for the mconf-idf binary in the wrong place (in the subdirectory build dir instead of root binary dir). --- tools/cmake/kconfig.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cmake/kconfig.cmake b/tools/cmake/kconfig.cmake index d1d34234d4..872e0d44db 100644 --- a/tools/cmake/kconfig.cmake +++ b/tools/cmake/kconfig.cmake @@ -47,7 +47,7 @@ function(__kconfig_init) externalproject_add(mconf-idf SOURCE_DIR ${src_path} CONFIGURE_COMMAND "" - BINARY_DIR "kconfig_bin" + BINARY_DIR "${CMAKE_BINARY_DIR}/kconfig_bin" BUILD_COMMAND rm -f ${src_path}/zconf.lex.c ${src_path}/zconf.hash.c COMMAND make -f ${src_path}/Makefile mconf-idf BUILD_BYPRODUCTS ${MCONF} From 38ae31ebc4e34e764d46bf6f110f0d14c716d5b0 Mon Sep 17 00:00:00 2001 From: baohongde Date: Wed, 14 Aug 2019 21:24:24 +0800 Subject: [PATCH 017/146] components/bt: Fix linking fail with toolchain 8.2.0 --- .../api/include/api/esp_gap_bt_api.h | 10 ++-- .../bluedroid/common/include/common/bt_defs.h | 59 +------------------ .../common/include/common/bt_trace.h | 2 +- .../decoder/include/oi_codec_sbc_private.h | 16 ++--- .../external/sbc/decoder/srce/dequant.c | 2 +- .../external/sbc/decoder/srce/framing.c | 10 ++-- .../sbc/decoder/srce/synthesis-dct8.c | 2 +- .../external/sbc/decoder/srce/synthesis-sbc.c | 2 +- 8 files changed, 23 insertions(+), 80 deletions(-) diff --git a/components/bt/host/bluedroid/api/include/api/esp_gap_bt_api.h b/components/bt/host/bluedroid/api/include/api/esp_gap_bt_api.h index 076f59caf7..9e9ab235db 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gap_bt_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gap_bt_api.h @@ -326,7 +326,7 @@ typedef void (* esp_bt_gap_cb_t)(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_para * @param[in] cod: Class of Device * @return major service bits */ -inline uint32_t esp_bt_gap_get_cod_srvc(uint32_t cod) +static inline uint32_t esp_bt_gap_get_cod_srvc(uint32_t cod) { return (cod & ESP_BT_COD_SRVC_BIT_MASK) >> ESP_BT_COD_SRVC_BIT_OFFSET; } @@ -336,7 +336,7 @@ inline uint32_t esp_bt_gap_get_cod_srvc(uint32_t cod) * @param[in] cod: Class of Device * @return major device bits */ -inline uint32_t esp_bt_gap_get_cod_major_dev(uint32_t cod) +static inline uint32_t esp_bt_gap_get_cod_major_dev(uint32_t cod) { return (cod & ESP_BT_COD_MAJOR_DEV_BIT_MASK) >> ESP_BT_COD_MAJOR_DEV_BIT_OFFSET; } @@ -346,7 +346,7 @@ inline uint32_t esp_bt_gap_get_cod_major_dev(uint32_t cod) * @param[in] cod: Class of Device * @return minor service bits */ -inline uint32_t esp_bt_gap_get_cod_minor_dev(uint32_t cod) +static inline uint32_t esp_bt_gap_get_cod_minor_dev(uint32_t cod) { return (cod & ESP_BT_COD_MINOR_DEV_BIT_MASK) >> ESP_BT_COD_MINOR_DEV_BIT_OFFSET; } @@ -356,7 +356,7 @@ inline uint32_t esp_bt_gap_get_cod_minor_dev(uint32_t cod) * @param[in] cod: Class of Device * @return format type */ -inline uint32_t esp_bt_gap_get_cod_format_type(uint32_t cod) +static inline uint32_t esp_bt_gap_get_cod_format_type(uint32_t cod) { return (cod & ESP_BT_COD_FORMAT_TYPE_BIT_MASK); } @@ -368,7 +368,7 @@ inline uint32_t esp_bt_gap_get_cod_format_type(uint32_t cod) * - true if cod is valid * - false otherise */ -inline bool esp_bt_gap_is_valid_cod(uint32_t cod) +static inline bool esp_bt_gap_is_valid_cod(uint32_t cod) { if (esp_bt_gap_get_cod_format_type(cod) == ESP_BT_COD_FORMAT_TYPE_1 && esp_bt_gap_get_cod_srvc(cod) != ESP_BT_COD_SRVC_NONE) { diff --git a/components/bt/host/bluedroid/common/include/common/bt_defs.h b/components/bt/host/bluedroid/common/include/common/bt_defs.h index 7421e7b147..1ff02ea1f3 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_defs.h +++ b/components/bt/host/bluedroid/common/include/common/bt_defs.h @@ -21,6 +21,7 @@ #include #include +#include #include "bt_common.h" #include "common/bt_target.h" @@ -69,62 +70,4 @@ typedef struct { #define CPU_LITTLE_ENDIAN #endif -inline uint16_t swap_byte_16(uint16_t x) -{ - return (((x & 0x00ffU) << 8) | - ((x & 0xff00U) >> 8)); -} - -inline uint32_t swap_byte_32(uint32_t x) -{ - return (((x & 0x000000ffUL) << 24) | - ((x & 0x0000ff00UL) << 8) | - ((x & 0x00ff0000UL) >> 8) | - ((x & 0xff000000UL) >> 24)); -} - -#ifndef ntohs -inline uint16_t ntohs(uint16_t x) -{ -#ifdef CPU_LITTLE_ENDIAN - return swap_byte_16(x); -#else - return x; -#endif -} -#endif /* #ifndef ntohs */ - -#ifndef htons -inline uint16_t htons(uint16_t x) -{ -#ifdef CPU_LITTLE_ENDIAN - return swap_byte_16(x); -#else - return x; -#endif -} -#endif /* #ifndef htons */ - -#ifndef ntohl -inline uint32_t ntohl(uint32_t x) -{ -#ifdef CPU_LITTLE_ENDIAN - return swap_byte_32(x); -#else - return x; -#endif -} -#endif /* #ifndef ntohl*/ - -#ifndef htonl -inline uint32_t htonl(uint32_t x) -{ -#ifdef CPU_LITTLE_ENDIAN - return swap_byte_32(x); -#else - return x; -#endif -} -#endif /* #ifndef htonl*/ - #endif /* _BT_DEFS_H_ */ diff --git a/components/bt/host/bluedroid/common/include/common/bt_trace.h b/components/bt/host/bluedroid/common/include/common/bt_trace.h index 09412ca7f3..780894887e 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_trace.h +++ b/components/bt/host/bluedroid/common/include/common/bt_trace.h @@ -25,7 +25,7 @@ #include "stack/bt_types.h" #include "bt_common.h" -inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t len) +static inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t len) { uint16_t i; diff --git a/components/bt/host/bluedroid/external/sbc/decoder/include/oi_codec_sbc_private.h b/components/bt/host/bluedroid/external/sbc/decoder/include/oi_codec_sbc_private.h index 092d26f9af..5682672aab 100644 --- a/components/bt/host/bluedroid/external/sbc/decoder/include/oi_codec_sbc_private.h +++ b/components/bt/host/bluedroid/external/sbc/decoder/include/oi_codec_sbc_private.h @@ -143,11 +143,11 @@ OI_INT adjustToFitBitpool(const OI_UINT bitpool, OI_UINT bitcount, OI_UINT *excess); -INLINE OI_INT allocAdjustedBits(OI_UINT8 *dest, +OI_INT allocAdjustedBits(OI_UINT8 *dest, OI_INT bits, OI_INT excess); -INLINE OI_INT allocExcessBits(OI_UINT8 *dest, +OI_INT allocExcessBits(OI_UINT8 *dest, OI_INT excess); PRIVATE OI_UINT32 internal_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame); @@ -165,7 +165,7 @@ PRIVATE OI_STATUS internal_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_INT16 *pcmData, OI_UINT32 *pcmBytes); -INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context, +OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_UINT32 *decoderData, OI_UINT32 decoderDataBytes, OI_BYTE maxChannels, @@ -173,7 +173,7 @@ INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BOOL enhanced, OI_BOOL msbc_enable); -INLINE OI_UINT16 OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO *frame, OI_UINT *headerLen_); +OI_UINT16 OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO *frame, OI_UINT *headerLen_); PRIVATE OI_UINT32 OI_SBC_MaxBitpool(OI_CODEC_SBC_FRAME_INFO *frame); @@ -185,13 +185,13 @@ PRIVATE void shift_buffer(SBC_BUFFER_T *dest, SBC_BUFFER_T *src, OI_UINT wordCou PRIVATE void cosineModulateSynth4(SBC_BUFFER_T *RESTRICT out, OI_INT32 const *RESTRICT in); PRIVATE void SynthWindow40_int32_int32_symmetry_with_sum(OI_INT16 *pcm, SBC_BUFFER_T buffer[80], OI_UINT strideShift); -INLINE void dct3_4(OI_INT32 *RESTRICT out, OI_INT32 const *RESTRICT in); +void dct3_4(OI_INT32 *RESTRICT out, OI_INT32 const *RESTRICT in); PRIVATE void analyze4_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 40], OI_INT16 *pcm, OI_UINT strideShift, OI_INT32 subband[4]); -INLINE void dct3_8(OI_INT32 *RESTRICT out, OI_INT32 const *RESTRICT in); +void dct3_8(OI_INT32 *RESTRICT out, OI_INT32 const *RESTRICT in); PRIVATE void analyze8_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 80], OI_INT16 *pcm, @@ -207,12 +207,12 @@ PRIVATE void analyze8_enhanced_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 11 /* Decoder functions */ -INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data); +void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data); PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *b, OI_BITSTREAM *bs); PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *ob); PRIVATE void OI_SBC_ReadSamplesJoint(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *global_bs); PRIVATE void OI_SBC_SynthFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_INT16 *pcm, OI_UINT start_block, OI_UINT nrof_blocks); -INLINE OI_INT32 OI_SBC_Dequant(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits); +OI_INT32 OI_SBC_Dequant(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits); PRIVATE OI_BOOL OI_SBC_ExamineCommandPacket(OI_CODEC_SBC_DECODER_CONTEXT *context, const OI_BYTE *data, OI_UINT32 len); PRIVATE void OI_SBC_GenerateTestSignal(OI_INT16 pcmData[][2], OI_UINT32 sampleCount); diff --git a/components/bt/host/bluedroid/external/sbc/decoder/srce/dequant.c b/components/bt/host/bluedroid/external/sbc/decoder/srce/dequant.c index 5c529e0b12..1d26596016 100644 --- a/components/bt/host/bluedroid/external/sbc/decoder/srce/dequant.c +++ b/components/bt/host/bluedroid/external/sbc/decoder/srce/dequant.c @@ -125,7 +125,7 @@ const OI_UINT32 dequant_long_unscaled[17]; #include -INLINE float dequant_float(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits) +static INLINE float dequant_float(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits) { float result = (1 << (scale_factor + 1)) * ((raw * 2.0f + 1.0f) / ((1 << bits) - 1.0f) - 1.0f); diff --git a/components/bt/host/bluedroid/external/sbc/decoder/srce/framing.c b/components/bt/host/bluedroid/external/sbc/decoder/srce/framing.c index f819ae9aa0..9c4eb3a4dc 100644 --- a/components/bt/host/bluedroid/external/sbc/decoder/srce/framing.c +++ b/components/bt/host/bluedroid/external/sbc/decoder/srce/framing.c @@ -110,7 +110,7 @@ const OI_UINT32 dequant_long_unscaled[17] = { #endif #ifdef USE_WIDE_CRC -INLINE OI_CHAR crc_iterate(OI_UINT8 oldcrc, OI_UINT8 next) +static INLINE OI_CHAR crc_iterate(OI_UINT8 oldcrc, OI_UINT8 next) { OI_UINT crc; OI_UINT idx; @@ -125,7 +125,7 @@ INLINE OI_CHAR crc_iterate(OI_UINT8 oldcrc, OI_UINT8 next) return crc; } -INLINE OI_CHAR crc_iterate_top4(OI_UINT8 oldcrc, OI_UINT8 next) +static INLINE OI_CHAR crc_iterate_top4(OI_UINT8 oldcrc, OI_UINT8 next) { OI_UINT crc; OI_UINT idx; @@ -142,13 +142,13 @@ INLINE OI_CHAR crc_iterate_top4(OI_UINT8 oldcrc, OI_UINT8 next) #else // USE_WIDE_CRC -INLINE OI_UINT8 crc_iterate_top4(OI_UINT8 oldcrc, OI_UINT8 next) +static INLINE OI_UINT8 crc_iterate_top4(OI_UINT8 oldcrc, OI_UINT8 next) { return (oldcrc << 4) ^ crc8_narrow[(oldcrc ^ next) >> 4]; } #ifdef USE_NIBBLEWISE_CRC -INLINE OI_UINT8 crc_iterate(OI_UINT8 crc, OI_UINT8 next) +static INLINE OI_UINT8 crc_iterate(OI_UINT8 crc, OI_UINT8 next) { crc = (crc << 4) ^ crc8_narrow[(crc ^ next) >> 4]; crc = (crc << 4) ^ crc8_narrow[((crc >> 4)^next) & 0xf]; @@ -157,7 +157,7 @@ INLINE OI_UINT8 crc_iterate(OI_UINT8 crc, OI_UINT8 next) } #else // USE_NIBBLEWISE_CRC -INLINE OI_UINT8 crc_iterate(OI_UINT8 crc, OI_UINT8 next) +static INLINE OI_UINT8 crc_iterate(OI_UINT8 crc, OI_UINT8 next) { return crc8_narrow[crc ^ next]; } diff --git a/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-dct8.c b/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-dct8.c index 1a12774e71..504e3c3bff 100644 --- a/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-dct8.c +++ b/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-dct8.c @@ -62,7 +62,7 @@ * @return A signed 32-bit value corresponding to the 32 most significant bits * of the 64-bit product of u and v. */ -INLINE OI_INT32 default_mul_32s_32s_hi(OI_INT32 u, OI_INT32 v) +static INLINE OI_INT32 default_mul_32s_32s_hi(OI_INT32 u, OI_INT32 v) { OI_UINT32 u0, v0; OI_INT32 u1, v1, w1, w2, t; diff --git a/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-sbc.c b/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-sbc.c index 158ba6fbf5..cff4863262 100644 --- a/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-sbc.c +++ b/components/bt/host/bluedroid/external/sbc/decoder/srce/synthesis-sbc.c @@ -215,7 +215,7 @@ const OI_INT32 dec_window_4[21] = { * @return A signed 32-bit value corresponding to the 32 most significant bits * of the 48-bit product of u and v. */ -INLINE OI_INT32 default_mul_16s_32s_hi(OI_INT16 u, OI_INT32 v) +static INLINE OI_INT32 default_mul_16s_32s_hi(OI_INT16 u, OI_INT32 v) { OI_UINT16 v0; OI_INT16 v1; From 026ec388a6128331a600ab3f59455de84b53339e Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Wed, 14 Aug 2019 21:19:06 +0800 Subject: [PATCH 018/146] idf_py: support new command set-target Instead of using -DIDF_TARGET, this command is more intuitive: 1. Can limit the choice of targets 2. Easy to understand this is a destructive command 3. Easy to remember, and have an entry in the --help menu --- tools/ci/test_build_system_cmake.sh | 9 ++++ tools/idf.py | 68 ++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/tools/ci/test_build_system_cmake.sh b/tools/ci/test_build_system_cmake.sh index bbe299b92f..47e1ae23ee 100755 --- a/tools/ci/test_build_system_cmake.sh +++ b/tools/ci/test_build_system_cmake.sh @@ -306,6 +306,15 @@ function run_tests() grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured correctly using idf.py reconfigure -D" grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py reconfigure -D" + # TODO Change the real target to other value than esp32 when we have + real_target=esp32 + print_status "Can set target using idf.py set-target" + clean_build_dir + rm sdkconfig + idf.py set-target esp32 || failure "Failed to set target via idf.py set-target" + grep "CONFIG_IDF_TARGET=\"${real_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target" + grep "IDF_TARGET:STRING=${real_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target" + # Clean up modifications for the fake target mv CMakeLists.txt.bak CMakeLists.txt rm -rf components diff --git a/tools/idf.py b/tools/idf.py index 1083d1d026..6d4e5fe43a 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -81,6 +81,8 @@ GENERATORS = [ GENERATOR_CMDS = dict((a[0], a[1]) for a in GENERATORS) GENERATOR_VERBOSE = dict((a[0], a[3]) for a in GENERATORS) +SUPPORTED_TARGETS = ["esp32"] + def _run_tool(tool_name, args, cwd): def quote_arg(arg): @@ -220,7 +222,6 @@ def _ensure_build_directory(args, always_run_cmake=False): os.makedirs(build_dir) cache_path = os.path.join(build_dir, "CMakeCache.txt") - args.define_cache_entry = list(args.define_cache_entry) args.define_cache_entry.append("CCACHE_ENABLE=%d" % args.ccache) if always_run_cmake or _new_cmakecache_entries(cache_path, args.define_cache_entry): @@ -406,6 +407,17 @@ def clean(action, ctx, args): build_target("clean", ctx, args) +def set_target(action, ctx, args, idf_target): + args.define_cache_entry.append("IDF_TARGET=" + idf_target) + sdkconfig_path = os.path.join(args.project_dir, 'sdkconfig') + sdkconfig_old = sdkconfig_path + ".old" + if os.path.exists(sdkconfig_old): + os.remove(sdkconfig_old) + if os.path.exists(sdkconfig_path): + os.rename(sdkconfig_path, sdkconfig_old) + print("Set Target to: %s, new sdkconfig created. Existing sdkconfig renamed to sdkconfig.old." % idf_target) + + def reconfigure(action, ctx, args): _ensure_build_directory(args, True) @@ -828,24 +840,21 @@ def init_cli(): for action_callback in ctx.command.global_action_callbacks: action_callback(ctx, global_args, tasks) - # very simple dependency management - completed_tasks = set() - if not tasks: print(ctx.get_help()) ctx.exit() - while tasks: - task = tasks[0] - tasks_dict = dict([(t.name, t) for t in tasks]) + # Make sure that define_cache_entry is list + global_args.define_cache_entry = list(global_args.define_cache_entry) - name_with_aliases = task.name - if task.aliases: - name_with_aliases += " (aliases: %s)" % ", ".join(task.aliases) - - ready_to_run = True + # Go through the task and create depended but missing tasks + all_tasks = [t.name for t in tasks] + tasks, tasks_to_handle = [], tasks + while tasks_to_handle: + task = tasks_to_handle.pop() + tasks.append(task) for dep in task.dependencies: - if dep not in completed_tasks: + if dep not in all_tasks: print( 'Adding %s\'s dependency "%s" to list of actions' % (task.name, dep) @@ -858,8 +867,20 @@ def init_cli(): if option and (option.scope.is_global or option.scope.is_shared): dep_task.action_args.pop(key) - tasks.insert(0, dep_task) - ready_to_run = False + tasks_to_handle.append(dep_task) + all_tasks.append(dep_task.name) + + # very simple dependency management + completed_tasks = set() + while tasks: + task = tasks[0] + tasks_dict = dict([(t.name, t) for t in tasks]) + + name_with_aliases = task.name + if task.aliases: + name_with_aliases += " (aliases: %s)" % ", ".join(task.aliases) + + ready_to_run = True for dep in task.order_dependencies: if dep in tasks_dict.keys() and dep not in completed_tasks: @@ -1080,7 +1101,22 @@ def init_cli(): + "For example, \"idf.py -DNAME='VALUE' reconfigure\" " + 'can be used to set variable "NAME" in CMake cache to value "VALUE".', "options": global_options, - "order_dependencies": ["menuconfig"], + "order_dependencies": ["menuconfig", "set-target", "fullclean"], + }, + "set-target": { + "callback": set_target, + "short_help": "Set the chip target to build.", + "help": "Set the chip target to build. This will remove the " + + "existing sdkconfig file and corresponding CMakeCache and " + + "create new ones according to the new target.\nFor example, " + + "\"idf.py set-target esp32\" will select esp32 as the new chip " + + "target.", + "arguments": [{ + "names": ["idf-target"], + "nargs": 1, + "type": click.Choice(SUPPORTED_TARGETS), + }], + "dependencies": ["fullclean", "reconfigure"], }, "clean": { "callback": clean, From 74be271f62acfcad2cf645690678c898664da255 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 19 Aug 2019 11:42:39 +1000 Subject: [PATCH 019/146] kconfig: Add -MP option so .d files include empty targets Prevents make-level errors when switching IDF versions, as headers may be removed. Similar to some mentioned here: https://github.com/espressif/esp-idf/issues/712 --- tools/kconfig/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/kconfig/Makefile b/tools/kconfig/Makefile index b91cbc2828..9fc367b454 100644 --- a/tools/kconfig/Makefile +++ b/tools/kconfig/Makefile @@ -165,7 +165,7 @@ check-lxdialog := $(SRCDIR)/lxdialog/check-lxdialog.sh # Use recursively expanded variables so we do not call gcc unless # we really need to do so. (Do not call gcc as part of make mrproper) CFLAGS += $(shell $(CONFIG_SHELL) $(check-lxdialog) -ccflags) \ - -DLOCALE -MMD + -DLOCALE -MMD -MP CFLAGS += -I "." -I "${SRCDIR}" From d296aad2a99f16978031b1561a64927d771f76be Mon Sep 17 00:00:00 2001 From: Tomoyuki Sakurai Date: Sun, 18 Aug 2019 14:30:48 +0900 Subject: [PATCH 020/146] build systems: Changes to work on FreeBSD Merges https://github.com/espressif/esp-idf/pull/2029 --- tools/cmake/kconfig.cmake | 7 ++++++- tools/idf.py | 4 ++++ tools/kconfig/lxdialog/check-lxdialog.sh | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/cmake/kconfig.cmake b/tools/cmake/kconfig.cmake index d1d34234d4..66b78e2576 100644 --- a/tools/cmake/kconfig.cmake +++ b/tools/cmake/kconfig.cmake @@ -34,6 +34,11 @@ function(__kconfig_init) set(MCONF "\"${WINPTY}\" \"${MCONF}\"") endif() endif() + if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "FreeBSD") + set(MAKE_COMMMAND "gmake") + else() + set(MAKE_COMMMAND "make") + endif() if(NOT MCONF) # Use the existing Makefile to build mconf (out of tree) when needed @@ -49,7 +54,7 @@ function(__kconfig_init) CONFIGURE_COMMAND "" BINARY_DIR "kconfig_bin" BUILD_COMMAND rm -f ${src_path}/zconf.lex.c ${src_path}/zconf.hash.c - COMMAND make -f ${src_path}/Makefile mconf-idf + COMMAND ${MAKE_COMMMAND} -f ${src_path}/Makefile mconf-idf BUILD_BYPRODUCTS ${MCONF} INSTALL_COMMAND "" EXCLUDE_FROM_ALL 1 diff --git a/tools/idf.py b/tools/idf.py index 1083d1d026..8f1a71103c 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -36,6 +36,7 @@ import re import shutil import subprocess import sys +import platform class FatalError(RuntimeError): @@ -64,6 +65,9 @@ if "MSYSTEM" in os.environ: # MSYS elif os.name == "nt": # other Windows MAKE_CMD = "mingw32-make" MAKE_GENERATOR = "MinGW Makefiles" +elif platform.system() == "FreeBSD": + MAKE_CMD = "gmake" + MAKE_GENERATOR = "Unix Makefiles" else: MAKE_CMD = "make" MAKE_GENERATOR = "Unix Makefiles" diff --git a/tools/kconfig/lxdialog/check-lxdialog.sh b/tools/kconfig/lxdialog/check-lxdialog.sh index e9daa6270c..de48a1c260 100755 --- a/tools/kconfig/lxdialog/check-lxdialog.sh +++ b/tools/kconfig/lxdialog/check-lxdialog.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # Check ncurses compatibility # What library to link From 003a9872b7de69d799e9d37521cfbcaff9b37e85 Mon Sep 17 00:00:00 2001 From: liu zhifu Date: Fri, 5 Jul 2019 16:58:04 +0800 Subject: [PATCH 021/146] esp_wifi: wifi support new event mechanism 1. WiFi support new event mechanism 2. Update examples to use new event mechanism --- components/driver/test/test_adc2.c | 56 ++++++-- components/esp_event/event_send.c | 128 +++++++++++++++++- .../esp_event/include/esp_event_legacy.h | 38 +++++- components/esp_event/include/esp_event_loop.h | 4 +- components/esp_event/test/test_event.c | 2 +- .../include/esp_websocket_client.h | 1 - components/esp_wifi/include/esp_mesh.h | 17 +-- .../include/esp_private/esp_wifi_private.h | 2 +- .../esp_private/esp_wifi_types_private.h | 2 +- components/esp_wifi/include/esp_wifi.h | 2 +- components/esp_wifi/include/esp_wifi_types.h | 3 +- components/esp_wifi/lib_esp32 | 2 +- components/esp_wifi/test/test_wifi.c | 64 ++++++--- components/mdns/include/mdns.h | 2 +- components/mqtt/esp-mqtt | 2 +- components/tcpip_adapter/event_handlers.c | 26 ++-- components/tcpip_adapter/tcpip_adapter_lwip.c | 106 +++++++++------ .../include/wifi_provisioning/manager.h | 2 +- .../src/wifi_provisioning_priv.h | 2 - .../src/esp_supplicant/esp_wifi_driver.h | 1 - .../src/esp_supplicant/esp_wps.c | 36 ++--- .../main/ble_hidd_demo_main.c | 2 +- .../ble/blufi/main/blufi_example_main.c | 51 ++++--- .../bluedroid/ble/blufi/main/blufi_security.c | 2 +- .../components/iperf/cmd_wifi.c | 39 ++++-- .../eth2ap/main/ethernet_example_main.c | 1 - .../spi_slave/receiver/main/app_main.c | 1 - .../spi_slave/sender/main/app_main.c | 1 - .../http_server/advanced_tests/main/main.c | 2 +- .../http_server/file_serving/main/main.c | 2 +- .../persistent_sockets/main/main.c | 2 +- .../restful_server/main/esp_rest_main.c | 1 - examples/protocols/https_server/main/main.c | 2 +- .../websocket/main/websocket_example.c | 3 +- .../provisioning/ble_prov/main/app_prov.h | 2 - .../provisioning/console_prov/main/app_prov.h | 2 - .../custom_config/main/app_prov.h | 2 +- .../provisioning/softap_prov/main/app_prov.h | 2 +- .../default_event_loop/main/event_source.h | 3 +- .../user_event_loops/main/event_source.h | 3 +- .../system/network_tests/main/net_suite.c | 2 +- .../main/advanced_https_ota_example.c | 1 - .../main/native_ota_example.c | 1 - .../main/simple_ota_example.c | 1 - 44 files changed, 425 insertions(+), 201 deletions(-) diff --git a/components/driver/test/test_adc2.c b/components/driver/test/test_adc2.c index 40549428ef..1438ae4d5c 100644 --- a/components/driver/test/test_adc2.c +++ b/components/driver/test/test_adc2.c @@ -6,7 +6,7 @@ #include "driver/dac.h" #include "unity.h" #include "esp_system.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_wifi.h" #include "esp_log.h" #include "nvs_flash.h" @@ -17,27 +17,56 @@ static const char* TAG = "test_adc2"; #define DEFAULT_SSID "TEST_SSID" #define DEFAULT_PWD "TEST_PASS" -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { printf("ev_handle_called.\n"); - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); + switch(event_id) { + case WIFI_EVENT_STA_START: + ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); //do not actually connect in test case //; break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); + case WIFI_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); TEST_ESP_OK(esp_wifi_connect()); break; default: break; } + return ; +} + +static void ip_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ip_event_got_ip_t *event; + printf("ev_handle_called.\n"); + switch(event_id) { + case IP_EVENT_STA_GOT_IP: + event = (ip_event_got_ip_t*)event_data; + ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP"); + ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->ip_info.ip)); + break; + default: + break; + } + + return ; +} + +static int event_init(void) +{ + TEST_ESP_OK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL)); + return ESP_OK; +} + +static int event_deinit(void) +{ + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler)); + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler)); return ESP_OK; } @@ -66,7 +95,7 @@ TEST_CASE("adc2 work with wifi","[adc]") } TEST_ESP_OK( r); tcpip_adapter_init(); - TEST_ESP_OK(esp_event_loop_init(event_handler, NULL)); + event_init(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); TEST_ESP_OK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { @@ -100,6 +129,7 @@ TEST_CASE("adc2 work with wifi","[adc]") printf("wifi stop...\n"); TEST_ESP_OK( esp_wifi_stop() ); TEST_ESP_OK(esp_wifi_deinit()); + event_deinit(); nvs_flash_deinit(); //test read value diff --git a/components/esp_event/event_send.c b/components/esp_event/event_send.c index 2c31ee0f6c..80fe464a81 100644 --- a/components/esp_event/event_send.c +++ b/components/esp_event/event_send.c @@ -12,21 +12,119 @@ // See the License for the specific language governing permissions and // limitations under the License. - +#include +#include "esp_log.h" #include "esp_event.h" #include "esp_event_legacy.h" -esp_err_t esp_event_send_noop(system_event_t *event); +#define TAG "event_send" +esp_err_t esp_event_send_noop(system_event_t *event); extern esp_err_t esp_event_send_legacy(system_event_t *event) __attribute__((weak, alias("esp_event_send_noop"))); extern esp_err_t esp_event_send_to_default_loop(system_event_t *event) __attribute((weak, alias("esp_event_send_noop"))); - esp_err_t esp_event_send_noop(system_event_t *event) { return ESP_OK; } +static system_event_id_t esp_event_legacy_wifi_event_id(int32_t event_id) +{ + switch (event_id) { + case WIFI_EVENT_WIFI_READY: + return SYSTEM_EVENT_WIFI_READY; + + case WIFI_EVENT_SCAN_DONE: + return SYSTEM_EVENT_SCAN_DONE; + + case WIFI_EVENT_STA_START: + return SYSTEM_EVENT_STA_START; + + case WIFI_EVENT_STA_STOP: + return SYSTEM_EVENT_STA_STOP; + + case WIFI_EVENT_STA_CONNECTED: + return SYSTEM_EVENT_STA_CONNECTED; + + case WIFI_EVENT_STA_DISCONNECTED: + return SYSTEM_EVENT_STA_DISCONNECTED; + + case WIFI_EVENT_STA_AUTHMODE_CHANGE: + return SYSTEM_EVENT_STA_AUTHMODE_CHANGE; + + case WIFI_EVENT_STA_WPS_ER_SUCCESS: + return SYSTEM_EVENT_STA_WPS_ER_SUCCESS; + + case WIFI_EVENT_STA_WPS_ER_FAILED: + return SYSTEM_EVENT_STA_WPS_ER_FAILED; + + case WIFI_EVENT_STA_WPS_ER_TIMEOUT: + return SYSTEM_EVENT_STA_WPS_ER_TIMEOUT; + + case WIFI_EVENT_STA_WPS_ER_PIN: + return SYSTEM_EVENT_STA_WPS_ER_PIN; + + case WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP: + return SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP; + + case WIFI_EVENT_AP_START: + return SYSTEM_EVENT_AP_START; + + case WIFI_EVENT_AP_STOP: + return SYSTEM_EVENT_AP_STOP; + + case WIFI_EVENT_AP_STACONNECTED: + return SYSTEM_EVENT_AP_STACONNECTED; + + case WIFI_EVENT_AP_STADISCONNECTED: + return SYSTEM_EVENT_AP_STADISCONNECTED; + + case WIFI_EVENT_AP_PROBEREQRECVED: + return SYSTEM_EVENT_AP_PROBEREQRECVED; + + default: + ESP_LOGE(TAG, "invalid wifi event id %d", event_id); + return SYSTEM_EVENT_MAX; + } +} + +static system_event_id_t esp_event_legacy_ip_event_id(int32_t event_id) +{ + switch (event_id) { + case IP_EVENT_STA_GOT_IP: + return SYSTEM_EVENT_STA_GOT_IP; + + case IP_EVENT_STA_LOST_IP: + return SYSTEM_EVENT_STA_LOST_IP; + + case IP_EVENT_AP_STAIPASSIGNED: + return SYSTEM_EVENT_AP_STAIPASSIGNED; + + case IP_EVENT_GOT_IP6: + return SYSTEM_EVENT_GOT_IP6; + + case IP_EVENT_ETH_GOT_IP: + return SYSTEM_EVENT_ETH_GOT_IP; + + default: + ESP_LOGE(TAG, "invalid ip event id %d", event_id); + return SYSTEM_EVENT_MAX; + } +} + + +static system_event_id_t esp_event_legacy_event_id(esp_event_base_t event_base, int32_t event_id) +{ + if (event_base == WIFI_EVENT) { + return esp_event_legacy_wifi_event_id(event_id); + } else if (event_base == IP_EVENT) { + return esp_event_legacy_ip_event_id(event_id); + } else { + ESP_LOGE(TAG, "invalid event base %s", event_base); + return SYSTEM_EVENT_MAX; + } +} + esp_err_t esp_event_send(system_event_t *event) { // send the event to the new style event loop @@ -43,3 +141,27 @@ esp_err_t esp_event_send(system_event_t *event) return ESP_OK; } + +esp_err_t esp_event_send_internal(esp_event_base_t event_base, + int32_t event_id, + void* event_data, + size_t event_data_size, + TickType_t ticks_to_wait) +{ + system_event_t event; + + // send the event to the new style event loop + esp_err_t err = esp_event_post(event_base, event_id, event_data, event_data_size, ticks_to_wait); + if (err != ESP_OK) { + return err; + } + + event.event_id = esp_event_legacy_event_id(event_base, event_id); + + if (event_data) { + memcpy(&event.event_info, event_data, event_data_size); + } + + return esp_event_send_legacy(&event); +} + diff --git a/components/esp_event/include/esp_event_legacy.h b/components/esp_event/include/esp_event_legacy.h index 9509ce6a75..139c81763d 100644 --- a/components/esp_event/include/esp_event_legacy.h +++ b/components/esp_event/include/esp_event_legacy.h @@ -121,7 +121,11 @@ typedef struct { } system_event_t; /** Event handler function type */ -typedef esp_err_t (*system_event_handler_t)(system_event_t *event); +typedef esp_err_t (*system_event_handler_t)(esp_event_base_t event_base, + int32_t event_id, + void* event_data, + size_t event_data_size, + TickType_t ticks_to_wait); /** * @brief Send a event to event task @@ -135,7 +139,29 @@ typedef esp_err_t (*system_event_handler_t)(system_event_t *event); * @return ESP_OK : succeed * @return others : fail */ -esp_err_t esp_event_send(system_event_t *event); +esp_err_t esp_event_send(system_event_t *event) __attribute__ ((deprecated)); + +/** + * @brief Send a event to event task + * + * @note This API is used by WiFi Driver only. + * + * Other task/modules, such as the tcpip_adapter, can call this API to send an event to event task + * + * @param[in] event_base the event base that identifies the event + * @param[in] event_id the event id that identifies the event + * @param[in] event_data the data, specific to the event occurence, that gets passed to the handler + * @param[in] event_data_size the size of the event data + * @param[in] ticks_to_wait number of ticks to block on a full event queue + * + * @return ESP_OK : succeed + * @return others : fail + */ +esp_err_t esp_event_send_internal(esp_event_base_t event_base, + int32_t event_id, + void* event_data, + size_t event_data_size, + TickType_t ticks_to_wait); /** * @brief Default event handler for system events @@ -152,7 +178,7 @@ esp_err_t esp_event_send(system_event_t *event); * @param event pointer to event to be handled * @return ESP_OK if an event was handled successfully */ -esp_err_t esp_event_process_default(system_event_t *event); +esp_err_t esp_event_process_default(system_event_t *event) __attribute__ ((deprecated)); /** * @brief Install default event handlers for Ethernet interface @@ -167,7 +193,7 @@ void esp_event_set_default_eth_handlers(void); * * @note This API is part of the legacy event system. New code should use event library API in esp_event.h */ -void esp_event_set_default_wifi_handlers(void); +void esp_event_set_default_wifi_handlers(void) __attribute__ ((deprecated)); /** * @brief Application specified event callback function @@ -198,7 +224,7 @@ typedef esp_err_t (*system_event_cb_t)(void *ctx, system_event_t *event); * - ESP_OK: succeed * - others: fail */ -esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx); +esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx) __attribute__ ((deprecated)); /** * @brief Set application specified event callback function @@ -214,7 +240,7 @@ esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx); * * @return old callback */ -system_event_cb_t esp_event_loop_set_cb(system_event_cb_t cb, void *ctx); +system_event_cb_t esp_event_loop_set_cb(system_event_cb_t cb, void *ctx) __attribute__ ((deprecated)); #ifdef __cplusplus } diff --git a/components/esp_event/include/esp_event_loop.h b/components/esp_event/include/esp_event_loop.h index 6267ee37d9..14ab627e51 100644 --- a/components/esp_event/include/esp_event_loop.h +++ b/components/esp_event/include/esp_event_loop.h @@ -1 +1,3 @@ -#include "esp_event_legacy.h" +#pragma once +#warning "esp_event_loop.h is deprecated, please include esp_event.h instead" +#include "esp_event.h" diff --git a/components/esp_event/test/test_event.c b/components/esp_event/test/test_event.c index b375d3f90a..c016f48f3c 100644 --- a/components/esp_event/test/test_event.c +++ b/components/esp_event/test/test_event.c @@ -5,7 +5,7 @@ #include "sdkconfig.h" #include "freertos/FreeRTOS.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "freertos/task.h" #include "freertos/portmacro.h" #include "esp_log.h" diff --git a/components/esp_websocket_client/include/esp_websocket_client.h b/components/esp_websocket_client/include/esp_websocket_client.h index 898fab5ae0..a8bcc5e2f4 100644 --- a/components/esp_websocket_client/include/esp_websocket_client.h +++ b/components/esp_websocket_client/include/esp_websocket_client.h @@ -22,7 +22,6 @@ #include "freertos/FreeRTOS.h" #include "esp_err.h" #include "esp_event.h" -#include "esp_event_loop.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_wifi/include/esp_mesh.h b/components/esp_wifi/include/esp_mesh.h index 0666b182e0..4e52e649d8 100644 --- a/components/esp_wifi/include/esp_mesh.h +++ b/components/esp_wifi/include/esp_mesh.h @@ -281,7 +281,7 @@ typedef struct { * @brief Parent connected information */ typedef struct { - system_event_sta_connected_t connected; /**< parent information, same as Wi-Fi event SYSTEM_EVENT_STA_CONNECTED does */ + wifi_event_sta_connected_t connected; /**< parent information, same as Wi-Fi event SYSTEM_EVENT_STA_CONNECTED does */ uint8_t self_layer; /**< layer */ } mesh_event_connected_t; @@ -324,11 +324,6 @@ typedef struct { uint8_t router_bssid[6]; /**< router BSSID */ } mesh_event_find_network_t; -/** - * @brief IP settings from LwIP stack - */ -typedef system_event_sta_got_ip_t mesh_event_root_got_ip_t; - /** * @brief Root address */ @@ -337,17 +332,17 @@ typedef mesh_addr_t mesh_event_root_address_t; /** * @brief Parent disconnected information */ -typedef system_event_sta_disconnected_t mesh_event_disconnected_t; +typedef wifi_event_sta_disconnected_t mesh_event_disconnected_t; /** * @brief Child connected information */ -typedef system_event_ap_staconnected_t mesh_event_child_connected_t; +typedef wifi_event_ap_staconnected_t mesh_event_child_connected_t; /** * @brief Child disconnected information */ -typedef system_event_ap_stadisconnected_t mesh_event_child_disconnected_t; +typedef wifi_event_ap_stadisconnected_t mesh_event_child_disconnected_t; /** * @brief Root switch request information @@ -398,7 +393,7 @@ typedef struct { /** * @brief New router information */ -typedef system_event_sta_connected_t mesh_event_router_switch_t; +typedef wifi_event_sta_connected_t mesh_event_router_switch_t; /** * @brief Mesh event information @@ -417,7 +412,7 @@ typedef union { packets out. If not, devices had better to wait until this state changes to be MESH_TODS_REACHABLE. */ mesh_event_vote_started_t vote_started; /**< vote started */ - mesh_event_root_got_ip_t got_ip; /**< root obtains IP address */ + //mesh_event_root_got_ip_t got_ip; /**< root obtains IP address */ mesh_event_root_address_t root_addr; /**< root address */ mesh_event_root_switch_req_t switch_req; /**< root switch request */ mesh_event_root_conflict_t root_conflict; /**< other powerful root */ diff --git a/components/esp_wifi/include/esp_private/esp_wifi_private.h b/components/esp_wifi/include/esp_private/esp_wifi_private.h index 80828aa886..087a4a1664 100644 --- a/components/esp_wifi/include/esp_private/esp_wifi_private.h +++ b/components/esp_wifi/include/esp_private/esp_wifi_private.h @@ -1,4 +1,4 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/components/esp_wifi/include/esp_private/esp_wifi_types_private.h b/components/esp_wifi/include/esp_private/esp_wifi_types_private.h index 3a1eebac61..55c70c77ca 100644 --- a/components/esp_wifi/include/esp_private/esp_wifi_types_private.h +++ b/components/esp_wifi/include/esp_private/esp_wifi_types_private.h @@ -1,4 +1,4 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index 57014b8d9a..ad13bd6c7f 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -190,7 +190,7 @@ extern const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs; #endif #define WIFI_INIT_CONFIG_DEFAULT() { \ - .event_handler = &esp_event_send, \ + .event_handler = &esp_event_send_internal, \ .osi_funcs = &g_wifi_osi_funcs, \ .wpa_crypto_funcs = g_wifi_default_wpa_crypto_funcs, \ .static_rx_buf_num = CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM,\ diff --git a/components/esp_wifi/include/esp_wifi_types.h b/components/esp_wifi/include/esp_wifi_types.h index fea3f14676..4dae5dc6c8 100644 --- a/components/esp_wifi/include/esp_wifi_types.h +++ b/components/esp_wifi/include/esp_wifi_types.h @@ -509,8 +509,9 @@ typedef enum { WIFI_EVENT_AP_STOP, /**< ESP32 soft-AP stop */ WIFI_EVENT_AP_STACONNECTED, /**< a station connected to ESP32 soft-AP */ WIFI_EVENT_AP_STADISCONNECTED, /**< a station disconnected from ESP32 soft-AP */ - WIFI_EVENT_AP_PROBEREQRECVED, /**< Receive probe request packet in soft-AP interface */ + + WIFI_EVENT_MAX, /**< Invalid WiFi event ID */ } wifi_event_t; /** @cond **/ diff --git a/components/esp_wifi/lib_esp32 b/components/esp_wifi/lib_esp32 index 09ed80c2b0..00ed323d85 160000 --- a/components/esp_wifi/lib_esp32 +++ b/components/esp_wifi/lib_esp32 @@ -1 +1 @@ -Subproject commit 09ed80c2b047ae5bb41ddbfb9be44ec2bc71fedd +Subproject commit 00ed323d855e71eab0e7cbf114733de92bf0d6bb diff --git a/components/esp_wifi/test/test_wifi.c b/components/esp_wifi/test/test_wifi.c index 08996d9de3..b3d950e08e 100644 --- a/components/esp_wifi/test/test_wifi.c +++ b/components/esp_wifi/test/test_wifi.c @@ -5,7 +5,7 @@ #include "esp_system.h" #include "unity.h" #include "esp_system.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_wifi.h" #include "esp_wifi_types.h" #include "esp_log.h" @@ -28,25 +28,18 @@ static uint32_t wifi_event_handler_flag; static EventGroupHandle_t wifi_events; -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - printf("ev_handle_called.\n"); - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); + printf("wifi ev_handle_called.\n"); + switch(event_id) { + case WIFI_EVENT_STA_START: + ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); //do not actually connect in test case //; break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); - ESP_LOGI(TAG, "got ip:%s\n", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - if (wifi_events) { - xEventGroupSetBits(wifi_events, GOT_IP_EVENT); - } - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); + case WIFI_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); if (! (EVENT_HANDLER_FLAG_DO_NOT_AUTO_RECONNECT & wifi_event_handler_flag) ) { TEST_ESP_OK(esp_wifi_connect()); } @@ -57,6 +50,37 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) default: break; } + return; +} + + +static void ip_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ip_event_got_ip_t *event; + + printf("ip ev_handle_called.\n"); + switch(event_id) { + case IP_EVENT_STA_GOT_IP: + event = (ip_event_got_ip_t*)event_data; + ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP"); + ESP_LOGI(TAG, "got ip:%s\n", + ip4addr_ntoa(&event->ip_info.ip)); + if (wifi_events) { + xEventGroupSetBits(wifi_events, GOT_IP_EVENT); + } + break; + default: + break; + } + return; +} + +static esp_err_t event_init(void) +{ + ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL)); return ESP_OK; } @@ -124,8 +148,8 @@ TEST_CASE("wifi stop and deinit","[wifi]") ESP_LOGI(TAG, EMPH_STR("tcpip_adapter_init")); tcpip_adapter_init(); //init event loop - ESP_LOGI(TAG, EMPH_STR("esp_event_loop_init")); - TEST_ESP_OK(esp_event_loop_init(event_handler, NULL)); + ESP_LOGI(TAG, EMPH_STR("event_init")); + event_init(); ESP_LOGI(TAG, "test wifi init & deinit..."); test_wifi_init_deinit(&cfg, &wifi_config); @@ -158,7 +182,7 @@ static void start_wifi_as_softap(void) .ap.beacon_interval = 100, }; - TEST_ESP_OK(esp_event_loop_init(event_handler, NULL)); + event_init(); // can't deinit event loop, need to reset leak check unity_reset_leak_checks(); @@ -180,7 +204,7 @@ static void start_wifi_as_sta(void) // do not auto connect wifi_event_handler_flag |= EVENT_HANDLER_FLAG_DO_NOT_AUTO_RECONNECT; - TEST_ESP_OK(esp_event_loop_init(event_handler, NULL)); + event_init(); // can't deinit event loop, need to reset leak check unity_reset_leak_checks(); diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index a30534640d..1ffa312c05 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -354,7 +354,7 @@ esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, ip6_addr_t * * @param ctx The system event context * @param event The system event */ -esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event); +esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event) __attribute__((deprecated)); #ifdef __cplusplus } diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 117eef2dad..fb3d2107cd 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 117eef2dad54e0f9e25b3005fcfc18e7695ff29e +Subproject commit fb3d2107cdac440d84f2fab81ea9b5217aa4ba1f diff --git a/components/tcpip_adapter/event_handlers.c b/components/tcpip_adapter/event_handlers.c index 5e3717c568..6d2128e526 100644 --- a/components/tcpip_adapter/event_handlers.c +++ b/components/tcpip_adapter/event_handlers.c @@ -32,8 +32,6 @@ do{\ }\ } while(0) -typedef esp_err_t (*system_event_handler_t)(system_event_t *e); - static void handle_ap_start(void *arg, esp_event_base_t base, int32_t event_id, void *data); static void handle_ap_stop(void *arg, esp_event_base_t base, int32_t event_id, void *data); static void handle_sta_start(void *arg, esp_event_base_t base, int32_t event_id, void *data); @@ -79,13 +77,12 @@ static void handle_eth_connected(void *arg, esp_event_base_t base, int32_t event tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, ð_ip); if (!(ip4_addr_isany_val(eth_ip.ip) || ip4_addr_isany_val(eth_ip.netmask))) { - system_event_t evt; + ip_event_got_ip_t evt; //notify event - evt.event_id = SYSTEM_EVENT_ETH_GOT_IP; - memcpy(&evt.event_info.got_ip.ip_info, ð_ip, sizeof(tcpip_adapter_ip_info_t)); - - esp_event_send(&evt); + evt.if_index = TCPIP_ADAPTER_IF_ETH; + memcpy(&evt.ip_info, ð_ip, sizeof(tcpip_adapter_ip_info_t)); + API_CALL_CHECK("handle_eth_connected", esp_event_send_internal(IP_EVENT, IP_EVENT_ETH_GOT_IP, &evt, sizeof(evt), 0), ESP_OK); } else { ESP_LOGE(TAG, "invalid static ip"); } @@ -171,20 +168,21 @@ static void handle_sta_connected(void *arg, esp_event_base_t base, int32_t event tcpip_adapter_get_old_ip_info(TCPIP_ADAPTER_IF_STA, &sta_old_ip); if (!(ip4_addr_isany_val(sta_ip.ip) || ip4_addr_isany_val(sta_ip.netmask))) { - system_event_t evt; + + ip_event_got_ip_t evt; - evt.event_id = SYSTEM_EVENT_STA_GOT_IP; - evt.event_info.got_ip.ip_changed = false; + evt.if_index = TCPIP_ADAPTER_IF_STA; + evt.ip_changed = false; if (memcmp(&sta_ip, &sta_old_ip, sizeof(sta_ip))) { - evt.event_info.got_ip.ip_changed = true; + evt.ip_changed = true; } - memcpy(&evt.event_info.got_ip.ip_info, &sta_ip, sizeof(tcpip_adapter_ip_info_t)); + memcpy(&evt.ip_info, &sta_ip, sizeof(tcpip_adapter_ip_info_t)); tcpip_adapter_set_old_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); - esp_event_send(&evt); - ESP_LOGD(TAG, "static ip: ip changed=%d", evt.event_info.got_ip.ip_changed); + API_CALL_CHECK("handle_sta_connected", esp_event_send_internal(IP_EVENT, IP_EVENT_STA_GOT_IP, &evt, sizeof(evt), 0), ESP_OK); + ESP_LOGD(TAG, "static ip: ip changed=%d", evt.ip_changed); } else { ESP_LOGE(TAG, "invalid static ip"); } diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 237b082a34..307babe3a6 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -92,13 +92,18 @@ static void tcpip_adapter_api_cb(void *api_msg) static void tcpip_adapter_dhcps_cb(u8_t client_ip[4]) { + int ret; + ESP_LOGI(TAG, "softAP assign IP to station,IP is: %d.%d.%d.%d", client_ip[0], client_ip[1], client_ip[2], client_ip[3]); - system_event_t evt; - memset(&evt, 0, sizeof(system_event_t)); - evt.event_id = SYSTEM_EVENT_AP_STAIPASSIGNED; - memcpy((char *)&evt.event_info.ap_staipassigned.ip.addr, (char *)client_ip, sizeof(evt.event_info.ap_staipassigned.ip.addr)); - esp_event_send(&evt); + ip_event_ap_staipassigned_t evt; + + memset(&evt, 0, sizeof(ip_event_ap_staipassigned_t)); + memcpy((char *)&evt.ip.addr, (char *)client_ip, sizeof(evt.ip.addr)); + ret = esp_event_send_internal(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "dhcps cb: failed to post IP_EVENT_AP_STAIPASSIGNED (%x)", ret); + } } void tcpip_adapter_init(void) @@ -447,23 +452,31 @@ esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_ada netif_set_addr(p_netif, &ip_info->ip, &ip_info->netmask, &ip_info->gw); if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { if (!(ip4_addr_isany_val(ip_info->ip) || ip4_addr_isany_val(ip_info->netmask) || ip4_addr_isany_val(ip_info->gw))) { - system_event_t evt; - memset(&evt, 0, sizeof(system_event_t)); - if (tcpip_if == TCPIP_ADAPTER_IF_STA) { - evt.event_id = SYSTEM_EVENT_STA_GOT_IP; - } else if (tcpip_if == TCPIP_ADAPTER_IF_ETH) { - evt.event_id = SYSTEM_EVENT_ETH_GOT_IP; - } - evt.event_info.got_ip.ip_changed = false; + + ip_event_t evt_id = IP_EVENT_STA_GOT_IP; + ip_event_got_ip_t evt; + int ret; + + memset(&evt, 0, sizeof(ip_event_got_ip_t)); + evt.if_index = tcpip_if; + evt.ip_changed = false; if (memcmp(ip_info, &esp_ip_old[tcpip_if], sizeof(tcpip_adapter_ip_info_t))) { - evt.event_info.got_ip.ip_changed = true; + evt.ip_changed = true; } - memcpy(&evt.event_info.got_ip.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t)); + if (tcpip_if == TCPIP_ADAPTER_IF_ETH) { + evt_id = IP_EVENT_ETH_GOT_IP; + } + + memcpy(&evt.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t)); memcpy(&esp_ip_old[tcpip_if], ip_info, sizeof(tcpip_adapter_ip_info_t)); - esp_event_send(&evt); - ESP_LOGD(TAG, "if%d tcpip adapter set static ip: ip changed=%d", tcpip_if, evt.event_info.got_ip.ip_changed); + ret = esp_event_send_internal(IP_EVENT, evt_id, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "set ip info: failed to post got ip event (%x)", ret); + } + + ESP_LOGD(TAG, "if%d tcpip adapter set static ip: ip changed=%d", tcpip_if, evt.ip_changed); } } } @@ -479,13 +492,12 @@ static esp_err_t tcpip_adapter_set_ip_info_api(tcpip_adapter_api_msg_t *msg) static void tcpip_adapter_nd6_cb(struct netif *p_netif, uint8_t ip_idex) { tcpip_adapter_ip6_info_t *ip6_info; + int ret; - system_event_t evt; - memset(&evt, 0, sizeof(system_event_t)); + ip_event_got_ip6_t evt; + memset(&evt, 0, sizeof(ip_event_got_ip6_t)); //notify event - evt.event_id = SYSTEM_EVENT_GOT_IP6; - if (!p_netif) { ESP_LOGD(TAG, "null p_netif=%p", p_netif); return; @@ -493,21 +505,24 @@ static void tcpip_adapter_nd6_cb(struct netif *p_netif, uint8_t ip_idex) if (p_netif == esp_netif[TCPIP_ADAPTER_IF_STA]) { ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_STA]; - evt.event_info.got_ip6.if_index = TCPIP_ADAPTER_IF_STA; + evt.if_index = TCPIP_ADAPTER_IF_STA; } else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_AP]) { ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_AP]; - evt.event_info.got_ip6.if_index = TCPIP_ADAPTER_IF_AP; + evt.if_index = TCPIP_ADAPTER_IF_AP; } else if (p_netif == esp_netif[TCPIP_ADAPTER_IF_ETH]) { ip6_info = &esp_ip6[TCPIP_ADAPTER_IF_ETH]; - evt.event_info.got_ip6.if_index = TCPIP_ADAPTER_IF_ETH; + evt.if_index = TCPIP_ADAPTER_IF_ETH; } else { return; } ip6_addr_set(&ip6_info->ip, ip_2_ip6(&p_netif->ip6_addr[ip_idex])); - memcpy(&evt.event_info.got_ip6.ip6_info, ip6_info, sizeof(tcpip_adapter_ip6_info_t)); - esp_event_send(&evt); + memcpy(&evt.ip6_info, ip6_info, sizeof(tcpip_adapter_ip6_info_t)); + ret = esp_event_send_internal(IP_EVENT, IP_EVENT_GOT_IP6, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "nd6 cb: failed to post IP_EVENT_GOT_IP6 (%x)", ret); + } } esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if) @@ -921,30 +936,37 @@ static void tcpip_adapter_dhcpc_cb(struct netif *netif) if ( !ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), (&ip_info->ip)) || !ip4_addr_cmp(ip_2_ip4(&netif->netmask), (&ip_info->netmask)) || !ip4_addr_cmp(ip_2_ip4(&netif->gw), (&ip_info->gw)) ) { - system_event_t evt; - memset(&evt, 0, sizeof(system_event_t)); + ip_event_got_ip_t evt; + ip_event_t evt_id; + int ret; + + memset(&evt, 0, sizeof(ip_event_got_ip_t)); ip4_addr_set(&ip_info->ip, ip_2_ip4(&netif->ip_addr)); ip4_addr_set(&ip_info->netmask, ip_2_ip4(&netif->netmask)); ip4_addr_set(&ip_info->gw, ip_2_ip4(&netif->gw)); //notify event + evt.if_index = tcpip_if; if (tcpip_if == TCPIP_ADAPTER_IF_ETH) { - evt.event_id = SYSTEM_EVENT_ETH_GOT_IP; - evt.event_info.got_ip.ip_changed = true; + evt_id = IP_EVENT_ETH_GOT_IP; + evt.ip_changed = true; } else { - evt.event_id = SYSTEM_EVENT_STA_GOT_IP; - evt.event_info.got_ip.ip_changed = false; + evt_id = IP_EVENT_STA_GOT_IP; + evt.ip_changed = false; } if (memcmp(ip_info, ip_info_old, sizeof(tcpip_adapter_ip_info_t))) { - evt.event_info.got_ip.ip_changed = true; + evt.ip_changed = true; } - memcpy(&evt.event_info.got_ip.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t)); + memcpy(&evt.ip_info, ip_info, sizeof(tcpip_adapter_ip_info_t)); memcpy(ip_info_old, ip_info, sizeof(tcpip_adapter_ip_info_t)); - ESP_LOGD(TAG, "if%d ip changed=%d", tcpip_if, evt.event_info.got_ip.ip_changed); - esp_event_send(&evt); + ESP_LOGD(TAG, "if%d ip changed=%d", tcpip_if, evt.ip_changed); + ret = esp_event_send_internal(IP_EVENT, evt_id, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "dhcpc cb: failed to post got ip event (%x)", ret); + } } else { ESP_LOGD(TAG, "if%d ip unchanged", tcpip_if); } @@ -997,13 +1019,17 @@ static void tcpip_adapter_ip_lost_timer(void *arg) struct netif *netif = esp_netif[tcpip_if]; if ( (!netif) || (netif && ip4_addr_cmp(ip_2_ip4(&netif->ip_addr), IP4_ADDR_ANY4))) { - system_event_t evt; - memset(&evt, 0, sizeof(system_event_t)); + ip_event_got_ip_t evt; + int ret; + memset(&evt, 0, sizeof(ip_event_got_ip_t)); ESP_LOGD(TAG, "if%d ip lost tmr: raise ip lost event", tcpip_if); + evt.if_index = tcpip_if; memset(&esp_ip_old[tcpip_if], 0, sizeof(tcpip_adapter_ip_info_t)); - evt.event_id = SYSTEM_EVENT_STA_LOST_IP; - esp_event_send(&evt); + ret = esp_event_send_internal(IP_EVENT, IP_EVENT_STA_LOST_IP, &evt, sizeof(evt), 0); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "ip lost timer: failed to post lost ip event (%x)", ret); + } } else { ESP_LOGD(TAG, "if%d ip lost tmr: no need raise ip lost event", tcpip_if); } diff --git a/components/wifi_provisioning/include/wifi_provisioning/manager.h b/components/wifi_provisioning/include/wifi_provisioning/manager.h index 8f31aedba5..2edd43fee9 100644 --- a/components/wifi_provisioning/include/wifi_provisioning/manager.h +++ b/components/wifi_provisioning/include/wifi_provisioning/manager.h @@ -14,9 +14,9 @@ #pragma once -#include #include +#include "esp_event.h" #include "wifi_provisioning/wifi_config.h" #ifdef __cplusplus diff --git a/components/wifi_provisioning/src/wifi_provisioning_priv.h b/components/wifi_provisioning/src/wifi_provisioning_priv.h index 756e82f452..a607b49dcb 100644 --- a/components/wifi_provisioning/src/wifi_provisioning_priv.h +++ b/components/wifi_provisioning/src/wifi_provisioning_priv.h @@ -14,8 +14,6 @@ #pragma once -#include - #include #include diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h b/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h index 62d2d6246e..820b713924 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wifi_driver.h @@ -213,7 +213,6 @@ bool esp_wifi_get_sniffer_internal(void); int esp_wifi_set_wps_cb_internal(struct wps_funcs *wps_cb); bool esp_wifi_enable_sta_privacy_internal(void); uint8_t esp_wifi_get_user_init_flag_internal(void); -esp_err_t esp_wifi_send_event_internal(system_event_t *evt); esp_err_t esp_wifi_internal_supplicant_header_md5_check(const char *md5); int esp_wifi_sta_update_ap_info_internal(void); uint8_t *esp_wifi_sta_get_ap_info_prof_pmk_internal(void); diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wps.c b/components/wpa_supplicant/src/esp_supplicant/esp_wps.c index cbcd351973..16f6f9965b 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wps.c +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wps.c @@ -413,10 +413,9 @@ struct wps_data *wps_init(void) os_bzero(tmpp, 9); memcpy(tmpp, data->dev_password, 8); wpa_printf(MSG_DEBUG, "WPS PIN [%s]", tmpp); - system_event_t evt; - evt.event_id = SYSTEM_EVENT_STA_WPS_ER_PIN; - memcpy(evt.event_info.sta_er_pin.pin_code, data->dev_password, 8); - esp_wifi_send_event_internal(&evt); + wifi_event_sta_wps_er_pin_t evt; + memcpy(evt.pin_code, data->dev_password, 8); + esp_event_send_internal(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_PIN, &evt, sizeof(evt), portMAX_DELAY); } while (0); } else if (wps_get_type() == WPS_TYPE_PBC) { data->pbc = 1; @@ -931,7 +930,7 @@ int wps_start_pending(void) return wps_tx_start(); } -int wps_stop_process(system_event_sta_wps_fail_reason_t reason_code) +int wps_stop_process(wifi_event_sta_wps_fail_reason_t reason_code) { struct wps_sm *sm = gWpsSm; @@ -955,10 +954,8 @@ int wps_stop_process(system_event_sta_wps_fail_reason_t reason_code) esp_wifi_disconnect(); wpa_printf(MSG_DEBUG, "Write wps_fail_information"); - system_event_t evt; - evt.event_id = SYSTEM_EVENT_STA_WPS_ER_FAILED; - evt.event_info.sta_er_fail_reason = reason_code; - esp_wifi_send_event_internal(&evt); + + esp_event_send_internal(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, &reason_code, sizeof(reason_code), portMAX_DELAY); return ESP_OK; } @@ -976,9 +973,7 @@ int wps_finish(void) wifi_config_t *config = (wifi_config_t *)os_zalloc(sizeof(wifi_config_t)); if (config == NULL) { - system_event_t evt; - evt.event_id = SYSTEM_EVENT_STA_WPS_ER_FAILED; - esp_wifi_send_event_internal(&evt); + esp_event_send_internal(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, 0, 0, portMAX_DELAY); return ESP_FAIL; } @@ -1251,9 +1246,7 @@ out: esp_wifi_disarm_sta_connection_timer_internal(); ets_timer_disarm(&sm->wps_timeout_timer); - system_event_t evt; - evt.event_id = SYSTEM_EVENT_STA_WPS_ER_FAILED; - esp_wifi_send_event_internal(&evt); + esp_event_send_internal(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, 0, 0, portMAX_DELAY); return ret; } @@ -1456,9 +1449,7 @@ wifi_station_wps_timeout_internal(void) wps_set_status(WPS_STATUS_DISABLE); - system_event_t evt; - evt.event_id = SYSTEM_EVENT_STA_WPS_ER_TIMEOUT; - esp_wifi_send_event_internal(&evt); + esp_event_send_internal(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_TIMEOUT, 0, 0, portMAX_DELAY); } void wifi_station_wps_timeout(void) @@ -1503,9 +1494,7 @@ void wifi_station_wps_msg_timeout(void) void wifi_station_wps_success_internal(void) { - system_event_t evt; - evt.event_id = SYSTEM_EVENT_STA_WPS_ER_SUCCESS; - esp_wifi_send_event_internal(&evt); + esp_event_send_internal(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_SUCCESS, 0, 0, portMAX_DELAY); } void wifi_station_wps_success(void) @@ -1780,10 +1769,7 @@ wifi_wps_scan_done(void *arg, STATUS status) } else { wpa_printf(MSG_INFO, "PBC session overlap!"); wps_set_status(WPS_STATUS_DISABLE); - - system_event_t evt; - evt.event_id = SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP; - esp_wifi_send_event_internal(&evt); + esp_event_send_internal(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP, 0, 0, portMAX_DELAY); } wpa_printf(MSG_DEBUG, "wps scan_done discover_ssid_cnt = %d", sm->discover_ssid_cnt); diff --git a/examples/bluetooth/bluedroid/ble/ble_hid_device_demo/main/ble_hidd_demo_main.c b/examples/bluetooth/bluedroid/ble/ble_hid_device_demo/main/ble_hidd_demo_main.c index b7584735a9..f7900d59ba 100644 --- a/examples/bluetooth/bluedroid/ble/ble_hid_device_demo/main/ble_hidd_demo_main.c +++ b/examples/bluetooth/bluedroid/ble/ble_hid_device_demo/main/ble_hidd_demo_main.c @@ -12,7 +12,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "esp_bt.h" diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c index bd3b86e219..cf3e153303 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_example_main.c @@ -22,7 +22,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "esp_bt.h" @@ -93,15 +93,14 @@ static int gl_sta_ssid_len; /* connect infor*/ static uint8_t server_if; static uint16_t conn_id; -static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) + +static void ip_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { wifi_mode_t mode; - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: { + switch (event_id) { + case IP_EVENT_STA_GOT_IP: { esp_blufi_extra_info_t info; xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); @@ -115,13 +114,30 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); break; } - case SYSTEM_EVENT_STA_CONNECTED: + default: + break; + } + return; +} + +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + wifi_event_sta_connected_t *event; + wifi_mode_t mode; + + switch (event_id) { + case WIFI_EVENT_STA_START: + esp_wifi_connect(); + break; + case WIFI_EVENT_STA_CONNECTED: gl_sta_connected = true; - memcpy(gl_sta_bssid, event->event_info.connected.bssid, 6); - memcpy(gl_sta_ssid, event->event_info.connected.ssid, event->event_info.connected.ssid_len); - gl_sta_ssid_len = event->event_info.connected.ssid_len; + event = (wifi_event_sta_connected_t*) event_data; + memcpy(gl_sta_bssid, event->bssid, 6); + memcpy(gl_sta_ssid, event->ssid, event->ssid_len); + gl_sta_ssid_len = event->ssid_len; break; - case SYSTEM_EVENT_STA_DISCONNECTED: + case WIFI_EVENT_STA_DISCONNECTED: /* This is a workaround as ESP32 WiFi libs don't currently auto-reassociate. */ gl_sta_connected = false; @@ -131,7 +147,7 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) esp_wifi_connect(); xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); break; - case SYSTEM_EVENT_AP_START: + case WIFI_EVENT_AP_START: esp_wifi_get_mode(&mode); /* TODO: get config or information of softap, then set to report extra_info */ @@ -141,7 +157,7 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); } break; - case SYSTEM_EVENT_SCAN_DONE: { + case WIFI_EVENT_SCAN_DONE: { uint16_t apCount = 0; esp_wifi_scan_get_ap_num(&apCount); if (apCount == 0) { @@ -176,14 +192,17 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) default: break; } - return ESP_OK; + return; } static void initialise_wifi(void) { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(example_net_event_handler, NULL) ); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); diff --git a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_security.c b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_security.c index f790e58e5b..635d5f00a2 100644 --- a/examples/bluetooth/bluedroid/ble/blufi/main/blufi_security.c +++ b/examples/bluetooth/bluedroid/ble/blufi/main/blufi_security.c @@ -14,7 +14,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "esp_bt.h" diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c index 15ebcb21cc..8fd95ce474 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/components/iperf/cmd_wifi.c @@ -17,7 +17,6 @@ #include "freertos/event_groups.h" #include "esp_wifi.h" #include "tcpip_adapter.h" -#include "esp_event_loop.h" #include "iperf.h" typedef struct { @@ -74,22 +73,18 @@ static void scan_done_handler(void) free(ap_list_buffer); } -static esp_err_t event_handler(void *ctx, system_event_t *event) +static void wifi_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch (event->event_id) { - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT); - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - ESP_LOGI(TAG, "got ip"); - break; - case SYSTEM_EVENT_SCAN_DONE: + switch (event_id) { + case WIFI_EVENT_SCAN_DONE: scan_done_handler(); ESP_LOGI(TAG, "sta scan done"); break; - case SYSTEM_EVENT_STA_CONNECTED: + case WIFI_EVENT_STA_CONNECTED: ESP_LOGI(TAG, "L2 connected"); break; - case SYSTEM_EVENT_STA_DISCONNECTED: + case WIFI_EVENT_STA_DISCONNECTED: if (reconnect) { ESP_LOGI(TAG, "sta disconnect, reconnect..."); esp_wifi_connect(); @@ -102,7 +97,22 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) default: break; } - return ESP_OK; + return; +} + +static void ip_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + switch (event_id) { + case IP_EVENT_STA_GOT_IP: + xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT); + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + ESP_LOGI(TAG, "got ip"); + break; + default: + break; + } + return; } void initialise_wifi(void) @@ -116,7 +126,10 @@ void initialise_wifi(void) tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_ps(WIFI_PS_MIN_MODEM) ); //must call this diff --git a/examples/ethernet/eth2ap/main/ethernet_example_main.c b/examples/ethernet/eth2ap/main/ethernet_example_main.c index 3b540fad05..b9b2193a2a 100644 --- a/examples/ethernet/eth2ap/main/ethernet_example_main.c +++ b/examples/ethernet/eth2ap/main/ethernet_example_main.c @@ -11,7 +11,6 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" -#include "esp_event_loop.h" #include "esp_event.h" #include "esp_log.h" #include "esp_eth.h" diff --git a/examples/peripherals/spi_slave/receiver/main/app_main.c b/examples/peripherals/spi_slave/receiver/main/app_main.c index 0defcb4ec5..cd01465d86 100644 --- a/examples/peripherals/spi_slave/receiver/main/app_main.c +++ b/examples/peripherals/spi_slave/receiver/main/app_main.c @@ -24,7 +24,6 @@ #include "esp_wifi.h" #include "esp_system.h" #include "esp_event.h" -#include "esp_event_loop.h" #include "nvs_flash.h" #include "soc/rtc_periph.h" #include "driver/spi_slave.h" diff --git a/examples/peripherals/spi_slave/sender/main/app_main.c b/examples/peripherals/spi_slave/sender/main/app_main.c index a8bcc6e0b6..c3a8007c7d 100644 --- a/examples/peripherals/spi_slave/sender/main/app_main.c +++ b/examples/peripherals/spi_slave/sender/main/app_main.c @@ -24,7 +24,6 @@ #include "esp_wifi.h" #include "esp_system.h" #include "esp_event.h" -#include "esp_event_loop.h" #include "nvs_flash.h" #include "soc/rtc_periph.h" #include "driver/spi_master.h" diff --git a/examples/protocols/http_server/advanced_tests/main/main.c b/examples/protocols/http_server/advanced_tests/main/main.c index af9ae1a87d..118a380a8f 100644 --- a/examples/protocols/http_server/advanced_tests/main/main.c +++ b/examples/protocols/http_server/advanced_tests/main/main.c @@ -8,7 +8,7 @@ */ #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" diff --git a/examples/protocols/http_server/file_serving/main/main.c b/examples/protocols/http_server/file_serving/main/main.c index 421355da33..4a6faf5a2d 100644 --- a/examples/protocols/http_server/file_serving/main/main.c +++ b/examples/protocols/http_server/file_serving/main/main.c @@ -10,7 +10,7 @@ #include #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "esp_spiffs.h" diff --git a/examples/protocols/http_server/persistent_sockets/main/main.c b/examples/protocols/http_server/persistent_sockets/main/main.c index 725de62e10..8e6132b1e6 100644 --- a/examples/protocols/http_server/persistent_sockets/main/main.c +++ b/examples/protocols/http_server/persistent_sockets/main/main.c @@ -8,7 +8,7 @@ */ #include -#include +#include #include #include #include diff --git a/examples/protocols/http_server/restful_server/main/esp_rest_main.c b/examples/protocols/http_server/restful_server/main/esp_rest_main.c index ee13bfa354..c82711ca4b 100644 --- a/examples/protocols/http_server/restful_server/main/esp_rest_main.c +++ b/examples/protocols/http_server/restful_server/main/esp_rest_main.c @@ -6,7 +6,6 @@ software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include "esp_event_loop.h" #include "driver/sdmmc_host.h" #include "driver/gpio.h" #include "esp_vfs_semihost.h" diff --git a/examples/protocols/https_server/main/main.c b/examples/protocols/https_server/main/main.c index a71f44e598..6d6a36931a 100644 --- a/examples/protocols/https_server/main/main.c +++ b/examples/protocols/https_server/main/main.c @@ -8,7 +8,7 @@ */ #include -#include +#include #include #include #include diff --git a/examples/protocols/websocket/main/websocket_example.c b/examples/protocols/websocket/main/websocket_example.c index 6b64ef25d0..901d2ad92c 100644 --- a/examples/protocols/websocket/main/websocket_example.c +++ b/examples/protocols/websocket/main/websocket_example.c @@ -12,7 +12,7 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -23,7 +23,6 @@ #include "esp_log.h" #include "esp_websocket_client.h" #include "esp_event.h" -#include "esp_event_loop.h" static const char *TAG = "WEBSOCKET"; static const char *WEBSOCKET_ECHO_ENDPOINT = CONFIG_WEBSOCKET_URI; diff --git a/examples/provisioning/ble_prov/main/app_prov.h b/examples/provisioning/ble_prov/main/app_prov.h index 97d5abaf9c..64e4dbe3ad 100644 --- a/examples/provisioning/ble_prov/main/app_prov.h +++ b/examples/provisioning/ble_prov/main/app_prov.h @@ -9,8 +9,6 @@ #pragma once -#include - #include #include diff --git a/examples/provisioning/console_prov/main/app_prov.h b/examples/provisioning/console_prov/main/app_prov.h index c6e026bc0a..5e6d884741 100644 --- a/examples/provisioning/console_prov/main/app_prov.h +++ b/examples/provisioning/console_prov/main/app_prov.h @@ -9,8 +9,6 @@ #pragma once -#include - #include #include diff --git a/examples/provisioning/custom_config/main/app_prov.h b/examples/provisioning/custom_config/main/app_prov.h index b575100bf0..be46b29451 100644 --- a/examples/provisioning/custom_config/main/app_prov.h +++ b/examples/provisioning/custom_config/main/app_prov.h @@ -9,7 +9,7 @@ #pragma once -#include +#include #include #include diff --git a/examples/provisioning/softap_prov/main/app_prov.h b/examples/provisioning/softap_prov/main/app_prov.h index 312d00e409..c87ff4f0b9 100644 --- a/examples/provisioning/softap_prov/main/app_prov.h +++ b/examples/provisioning/softap_prov/main/app_prov.h @@ -9,7 +9,7 @@ #pragma once -#include +#include #include #include diff --git a/examples/system/esp_event/default_event_loop/main/event_source.h b/examples/system/esp_event/default_event_loop/main/event_source.h index 91ca4f10c5..e9733a3df4 100644 --- a/examples/system/esp_event/default_event_loop/main/event_source.h +++ b/examples/system/esp_event/default_event_loop/main/event_source.h @@ -11,7 +11,6 @@ #define EVENT_SOURCE_H_ #include "esp_event.h" -#include "esp_event_loop.h" #include "esp_timer.h" #ifdef __cplusplus @@ -50,4 +49,4 @@ enum { } #endif -#endif // #ifndef EVENT_SOURCE_H_ \ No newline at end of file +#endif // #ifndef EVENT_SOURCE_H_ diff --git a/examples/system/esp_event/user_event_loops/main/event_source.h b/examples/system/esp_event/user_event_loops/main/event_source.h index 73a3b69c12..2e1787d8e2 100644 --- a/examples/system/esp_event/user_event_loops/main/event_source.h +++ b/examples/system/esp_event/user_event_loops/main/event_source.h @@ -11,7 +11,6 @@ #define EVENT_SOURCE_H_ #include "esp_event.h" -#include "esp_event_loop.h" #include "esp_timer.h" #ifdef __cplusplus @@ -32,4 +31,4 @@ enum { } #endif -#endif // #ifndef EVENT_SOURCE_H_ \ No newline at end of file +#endif // #ifndef EVENT_SOURCE_H_ diff --git a/examples/system/network_tests/main/net_suite.c b/examples/system/network_tests/main/net_suite.c index 9b9dfe2d02..dc88173573 100644 --- a/examples/system/network_tests/main/net_suite.c +++ b/examples/system/network_tests/main/net_suite.c @@ -12,7 +12,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "driver/uart.h" diff --git a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c index aafa488883..a207a91304 100644 --- a/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c +++ b/examples/system/ota/advanced_https_ota/main/advanced_https_ota_example.c @@ -12,7 +12,6 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_event.h" -#include "esp_event_loop.h" #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_http_client.h" diff --git a/examples/system/ota/native_ota_example/main/native_ota_example.c b/examples/system/ota/native_ota_example/main/native_ota_example.c index 9d84cc07c6..1c8ea033f7 100644 --- a/examples/system/ota/native_ota_example/main/native_ota_example.c +++ b/examples/system/ota/native_ota_example/main/native_ota_example.c @@ -11,7 +11,6 @@ #include "freertos/task.h" #include "esp_system.h" #include "esp_event.h" -#include "esp_event_loop.h" #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_http_client.h" diff --git a/examples/system/ota/simple_ota_example/main/simple_ota_example.c b/examples/system/ota/simple_ota_example/main/simple_ota_example.c index 9d0187f3bc..f85186e2b6 100644 --- a/examples/system/ota/simple_ota_example/main/simple_ota_example.c +++ b/examples/system/ota/simple_ota_example/main/simple_ota_example.c @@ -10,7 +10,6 @@ #include "freertos/task.h" #include "esp_system.h" #include "esp_event.h" -#include "esp_event_loop.h" #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_http_client.h" From 19a1af88a54ea9265e2c03305259bb4aff5e8b26 Mon Sep 17 00:00:00 2001 From: Nachiket Kukade Date: Fri, 9 Aug 2019 17:19:37 +0530 Subject: [PATCH 022/146] wps: Relax the check on older config methods in case of WPS2.0 Some APs incorrectly advertize newer WPS2.0 config method bits without setting bits for the corresponding older methods. This results in failures during 8-way handshake. Add a workaround to relax this check so that WPS handshake can proceed. --- components/wpa_supplicant/src/wps/wps_validate.c | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/components/wpa_supplicant/src/wps/wps_validate.c b/components/wpa_supplicant/src/wps/wps_validate.c index 34f0865b11..de0a46e7dd 100644 --- a/components/wpa_supplicant/src/wps/wps_validate.c +++ b/components/wpa_supplicant/src/wps/wps_validate.c @@ -96,23 +96,11 @@ static int wps_validate_response_type(const u8 *response_type, int mandatory) static int valid_config_methods(u16 val, int wps2) { if (wps2) { - if ((val & 0x6000) && !(val & WPS_CONFIG_DISPLAY)) { - wpa_printf(MSG_INFO, "WPS-STRICT: Physical/Virtual " - "Display flag without old Display flag " - "set"); - return 0; - } if (!(val & 0x6000) && (val & WPS_CONFIG_DISPLAY)) { wpa_printf(MSG_INFO, "WPS-STRICT: Display flag " "without Physical/Virtual Display flag"); return 0; } - if ((val & 0x0600) && !(val & WPS_CONFIG_PUSHBUTTON)) { - wpa_printf(MSG_INFO, "WPS-STRICT: Physical/Virtual " - "PushButton flag without old PushButton " - "flag set"); - return 0; - } if (!(val & 0x0600) && (val & WPS_CONFIG_PUSHBUTTON)) { wpa_printf(MSG_INFO, "WPS-STRICT: PushButton flag " "without Physical/Virtual PushButton flag"); From 209fdc1e059fde88e23c40892e3062464bc09e8c Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 15 Aug 2019 18:37:52 +0200 Subject: [PATCH 023/146] docs: jtag-debugging: update for IDF Tools installation method - rely on OPENOCD_SCRIPTS variable in all cases, remove -s flags - replace installation section with a reference to the Getting Started guides - add Windows-specific commands in a few cases --- .../jtag-debugging/configure-wrover.rst | 10 +-- docs/en/api-guides/jtag-debugging/index.rst | 83 +++++++++---------- .../jtag-debugging/setup-openocd-linux.rst | 34 -------- .../jtag-debugging/setup-openocd-macos.rst | 39 --------- .../jtag-debugging/setup-openocd-windows.rst | 40 --------- .../jtag-debugging/tips-and-quirks.rst | 10 +-- .../jtag-debugging/windows-openocd-note.rst | 3 - .../jtag-debugging/configure-wrover.rst | 10 +-- .../jtag-debugging/setup-openocd-linux.rst | 34 -------- .../jtag-debugging/setup-openocd-macos.rst | 39 --------- .../jtag-debugging/setup-openocd-windows.rst | 39 --------- .../jtag-debugging/tips-and-quirks.rst | 10 +-- .../jtag-debugging/windows-openocd-note.rst | 3 - 13 files changed, 47 insertions(+), 307 deletions(-) delete mode 100644 docs/en/api-guides/jtag-debugging/setup-openocd-linux.rst delete mode 100644 docs/en/api-guides/jtag-debugging/setup-openocd-macos.rst delete mode 100644 docs/en/api-guides/jtag-debugging/setup-openocd-windows.rst delete mode 100644 docs/en/api-guides/jtag-debugging/windows-openocd-note.rst delete mode 100644 docs/zh_CN/api-guides/jtag-debugging/setup-openocd-linux.rst delete mode 100644 docs/zh_CN/api-guides/jtag-debugging/setup-openocd-macos.rst delete mode 100644 docs/zh_CN/api-guides/jtag-debugging/setup-openocd-windows.rst delete mode 100644 docs/zh_CN/api-guides/jtag-debugging/windows-openocd-note.rst diff --git a/docs/en/api-guides/jtag-debugging/configure-wrover.rst b/docs/en/api-guides/jtag-debugging/configure-wrover.rst index a851b9efad..3f3d5015c5 100644 --- a/docs/en/api-guides/jtag-debugging/configure-wrover.rst +++ b/docs/en/api-guides/jtag-debugging/configure-wrover.rst @@ -121,20 +121,14 @@ Manually unloading the driver sudo kextunload -b com.apple.driver.AppleUSBFTDI -4. Run OpenOCD (paths are given for downloadable OpenOCD archive):: +4. Run OpenOCD:: - bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg - - Or, if OpenOCD was built from source:: - - src/openocd -s tcl -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + bin/openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg 5. In another terminal window, load FTDI serial port driver again:: sudo kextload -b com.FTDI.driver.FTDIUSBSerialDriver -.. include:: ./windows-openocd-note.rst - .. note:: If you need to restart OpenOCD, there is no need to unload FTDI driver again — just stop OpenOCD and start it again. The driver only needs to be unloaded if WROVER KIT was reconnected or power was toggled. diff --git a/docs/en/api-guides/jtag-debugging/index.rst b/docs/en/api-guides/jtag-debugging/index.rst index c7be7711f9..c27d890152 100644 --- a/docs/en/api-guides/jtag-debugging/index.rst +++ b/docs/en/api-guides/jtag-debugging/index.rst @@ -12,7 +12,7 @@ GDB. The document is structured as follows: :ref:`jtag-debugging-selecting-jtag-adapter` What are the criteria and options to select JTAG adapter hardware. :ref:`jtag-debugging-setup-openocd` - Procedure to install OpenOCD using prebuilt software packages for :doc:`Windows `, :doc:`Linux ` and :doc:`MacOS ` operating systems. + Procedure to install OpenOCD and verify that it is installed. :ref:`jtag-debugging-configuring-esp32-target` Configuration of OpenOCD software and set up JTAG adapter hardware that will make together a debugging target. :ref:`jtag-debugging-launching-debugger` @@ -84,45 +84,28 @@ The minimal signalling to get a working JTAG connection are TDI, TDO, TCK, TMS a Setup of OpenOCD ---------------- -This step covers installation of OpenOCD binaries. If you like to build OpenOCS from sources then refer to section :ref:`jtag-debugging-building-openocd`. All OpenOCD files will be placed in ``~/esp/openocd-esp32`` directory. You may choose any other directory, but need to adjust respective paths used in examples. +.. highlight:: bash -.. toctree:: - :hidden: +If you have already set up ESP-IDF with CMake build system according to the :doc:`Getting Started Guide <../../get-started/index>`, then OpenOCD is already installed. After :ref:`setting up the environment ` in your terminal, you should be able to run OpenOCD. Check this by executing the following command:: - Windows - Linux - MacOS + openocd --version -Pick up your OS below and follow provided instructions to setup OpenOCD. +.. highlight:: none -+-------------------+-------------------+-------------------+ -| |windows-logo| | |linux-logo| | |macos-logo| | -+-------------------+-------------------+-------------------+ -| `Windows`_ | `Linux`_ | `Mac OS`_ | -+-------------------+-------------------+-------------------+ +The output should be as follows (although the version may be more recent than listed here):: -.. |windows-logo| image:: ../../../_static/windows-logo.png - :target: ../jtag-debugging/setup-openocd-windows.html + Open On-Chip Debugger v0.10.0-esp32-20190708 (2019-07-08-11:04) + Licensed under GNU GPL v2 + For bug reports, read + http://openocd.org/doc/doxygen/bugs.html -.. |linux-logo| image:: ../../../_static/linux-logo.png - :target: ../jtag-debugging/setup-openocd-linux.html +You may also verify that OpenOCD knows where its configuration scripts are located by printing the value of ``OPENOCD_SCRIPTS`` environment variable, by typing ``echo $OPENOCD_SCRIPTS`` (for Linux and macOS) or ``echo %OPENOCD_SCRIPTS%`` (for Windows). If a valid path is printed, then OpenOCD is set up correctly. -.. |macos-logo| image:: ../../../_static/macos-logo.png - :target: ../jtag-debugging/setup-openocd-macos.html - -.. _Windows: ../jtag-debugging/setup-openocd-windows.html -.. _Linux: ../jtag-debugging/setup-openocd-linux.html -.. _Mac OS: ../jtag-debugging/setup-openocd-macos.html - -After installation is complete, get familiar with two key directories inside ``openocd-esp32`` installation folder: - -* ``bin`` containing OpenOCD executable -* ``share\openocd\scripts`` containing configuration files invoked together with OpenOCD as command line parameters +If any of these steps do not work, please go back to the :ref:`setting up the tools ` section of the Getting Started Guide. .. note:: - Directory names and structure above are specific to binary distribution of OpenOCD. They are used in examples of invoking OpenOCD throughout this guide. Directories for OpenOCD build from sources are different, so the way to invoke OpenOCD. For details see :ref:`jtag-debugging-building-openocd`. - + It is also possible to build OpenOCD from source. Please refer to :ref:`jtag-debugging-building-openocd` section for details. .. _jtag-debugging-configuring-esp32-target: @@ -157,23 +140,20 @@ Once target is configured and connected to computer, you are ready to launch Ope .. highlight:: bash -Open terminal, go to directory where OpenOCD is installed and start it up:: +Open a terminal and set it up for using the ESP-IDF as described in the :ref:`setting up the environment ` section of the Getting Started Guide. Then run OpenOCD (this command works on Windows, Linux, and macOS):: - cd ~/esp/openocd-esp32 - bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg .. note:: - The files provided after ``-f`` above, are specific for ESP-WROVER-KIT with :ref:`esp-modules-and-boards-esp32-wroom-32` module. You may need to provide different files depending on used hardware, For guidance see :ref:`jtag-debugging-tip-openocd-configure-target`. - -.. include:: ./windows-openocd-note.rst + The files provided after ``-f`` above are specific for ESP-WROVER-KIT with :ref:`esp-modules-and-boards-esp32-wroom-32` module. You may need to provide different files depending on used hardware. For guidance see :ref:`jtag-debugging-tip-openocd-configure-target`. .. highlight:: none You should now see similar output (this log is for ESP-WROVER-KIT):: - user-name@computer-name:~/esp/openocd-esp32$ bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg - Open On-Chip Debugger 0.10.0-dev-ged7b1a9 (2017-07-10-07:16) + user-name@computer-name:~/esp/esp-idf$ openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + Open On-Chip Debugger v0.10.0-esp32-20190708 (2019-07-08-11:04) Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html @@ -201,10 +181,7 @@ Build and upload your application to ESP32 as usual, see :ref:`get-started-build Another option is to write application image to flash using OpenOCD via JTAG with commands like this:: - cd ~/esp/openocd-esp32 - bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "program_esp32 filename.bin 0x10000 verify exit" - -.. include:: ./windows-openocd-note.rst + openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "program_esp32 filename.bin 0x10000 verify exit" OpenOCD flashing command ``program_esp32`` has the following format: @@ -268,13 +245,27 @@ Please refer to separate documents listed below, that describe build process. Linux MacOS -.. note:: +The examples of invoking OpenOCD in this document assume using pre-built binary distribution described in section :ref:`jtag-debugging-setup-openocd`. - Examples of invoking OpenOCD in this document assume using pre-built binary distribution described in section :ref:`jtag-debugging-setup-openocd`. To use binaries build locally from sources, change the path to OpenOCD executable to ``src/openocd`` and the path to configuration files to ``-s tcl``. +.. highlight:: bash - Example of invoking OpenOCD build locally from sources:: +To use binaries build locally from sources, change the path to OpenOCD executable to ``src/openocd`` and set the ``OPENOCD_SCRIPTS`` environment variable so that OpenOCD can find the configuration files. For Linux and macOS:: - src/openocd -s tcl -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + cd ~/esp/openocd-esp32 + export OPENOCD_SCRIPTS=$PWD/tcl + +For Windows:: + + cd %USERPROFILE%\esp\openocd-esp32 + set "OPENOCD_SCRIPTS=%CD%\tcl" + +Example of invoking OpenOCD build locally from sources, for Linux and macOS:: + + src/openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + +and Windows:: + + src\openocd -f interface\ftdi\esp32_devkitj_v1.cfg -f board\esp-wroom-32.cfg .. _jtag-debugging-tips-and-quirks: diff --git a/docs/en/api-guides/jtag-debugging/setup-openocd-linux.rst b/docs/en/api-guides/jtag-debugging/setup-openocd-linux.rst deleted file mode 100644 index 1e265ccdb7..0000000000 --- a/docs/en/api-guides/jtag-debugging/setup-openocd-linux.rst +++ /dev/null @@ -1,34 +0,0 @@ -************************ -Set up OpenOCD for Linux -************************ -:link_to_translation:`zh_CN:[中文]` - -Set up OpenOCD -============== - -OpenOCD for 64-bit Linux is available for download from Github: - -https://github.com/espressif/openocd-esp32/releases - -Download latest release archive with `linux64` in its name, for example `openocd-esp32-linux64-0.10.0-esp32-20180418.tar.gz`. - -Extract the downloaded file in ``~/esp/`` directory:: - - cd ~/esp - tar -xzf ~/Downloads/openocd-esp32-linux64-.tar.gz - - -Next Steps -========== - -To carry on with debugging environment setup, proceed to section :ref:`jtag-debugging-configuring-esp32-target`. - - -Related Documents -================= - -.. toctree:: - :maxdepth: 1 - - building-openocd-linux - diff --git a/docs/en/api-guides/jtag-debugging/setup-openocd-macos.rst b/docs/en/api-guides/jtag-debugging/setup-openocd-macos.rst deleted file mode 100644 index aa4090c2dd..0000000000 --- a/docs/en/api-guides/jtag-debugging/setup-openocd-macos.rst +++ /dev/null @@ -1,39 +0,0 @@ -************************ -Set up OpenOCD for MacOS -************************ -:link_to_translation:`zh_CN:[中文]` - -Install libusb -============== - -Use `Homebrew `_ or `Macports `_ to install `libusb` package. - -Set up OpenOCD -============== - -OpenOCD for MacOS is available for download from Github: - -https://github.com/espressif/openocd-esp32/releases - -Download latest release archive with `macos` in its name, for example `openocd-esp32-macos-0.10.0-esp32-20180418.tar.gz`. - -Extract the downloaded file in ``~/esp/`` directory:: - - cd ~/esp - tar -xzf ~/Downloads/openocd-esp32-macos-.tar.gz - - -Next Steps -========== - -To carry on with debugging environment setup, proceed to section :ref:`jtag-debugging-configuring-esp32-target`. - - -Related Documents -================= - -.. toctree:: - :maxdepth: 1 - - building-openocd-macos - diff --git a/docs/en/api-guides/jtag-debugging/setup-openocd-windows.rst b/docs/en/api-guides/jtag-debugging/setup-openocd-windows.rst deleted file mode 100644 index 67eca902a8..0000000000 --- a/docs/en/api-guides/jtag-debugging/setup-openocd-windows.rst +++ /dev/null @@ -1,40 +0,0 @@ -************************** -Set up OpenOCD for Windows -************************** -:link_to_translation:`zh_CN:[中文]` - -IDF Tools Installer -=================== - -If you are using CMake build system and followed the :doc:`/get-started/windows-setup` with the ``ESP-IDF Tools Installer`` V1.2 or newer, then by default you will already have ``openocd`` installed. - -``ESP-IDF Tools Installer`` adds ``openocd`` to the ``PATH`` so that it can be run from any directory. - -Set up OpenOCD -============== - - -OpenOCD for Windows is available for download from Github: - -https://github.com/espressif/openocd-esp32/releases - -Download latest release archive with `win32` in its name, for example `openocd-esp32-macos-0.10.0-win32-20180418.zip`. - -Extract the downloaded file in ``~/esp/`` directory. - - cd ~/esp - unzip /c/Users//Downloads/openocd-esp32-win32-.zip - -Next Steps -========== - -To carry on with debugging environment setup, proceed to section :ref:`jtag-debugging-configuring-esp32-target`. - - -Related Documents -================= - -.. toctree:: - :maxdepth: 1 - - building-openocd-windows diff --git a/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst b/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst index d3a76940cc..7fb45e5dae 100644 --- a/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst +++ b/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst @@ -36,7 +36,7 @@ Offset should be in hex format. To reset to the default behaviour you can specif Since GDB requests memory map from OpenOCD only once when connecting to it, this command should be specified in one of the TCL configuration files, or passed to OpenOCD via its command line. In the latter case command line should look like below: - ``bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "init; halt; esp32 appimage_offset 0x210000"`` + ``openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "init; halt; esp32 appimage_offset 0x210000"`` Another option is to execute that command via OpenOCD telnet session and then connect GDB, but it seems to be less handy. @@ -259,17 +259,13 @@ In case you encounter a problem with OpenOCD or GDB programs itself and do not f :: - bin/openocd -l openocd_log.txt -d 3 -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + openocd -l openocd_log.txt -d 3 -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg Logging to a file this way will prevent information displayed on the terminal. This may be a good thing taken amount of information provided, when increased debug level ``-d 3`` is set. If you still like to see the log on the screen, then use another command instead: :: - bin/openocd -d 3 -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg 2>&1 | tee openocd.log - - .. note:: - - See :ref:`jtag-debugging-building-openocd` for slightly different command format, when running OpenOCD built from sources. + openocd -d 3 -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg 2>&1 | tee openocd.log Debugger: diff --git a/docs/en/api-guides/jtag-debugging/windows-openocd-note.rst b/docs/en/api-guides/jtag-debugging/windows-openocd-note.rst deleted file mode 100644 index 4451ae722b..0000000000 --- a/docs/en/api-guides/jtag-debugging/windows-openocd-note.rst +++ /dev/null @@ -1,3 +0,0 @@ -.. note:: - - If you installed openocd on Windows using the ESP-IDF Tools Installer, can run ``openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg`` without needing to change directory first, and without the ``-s share/openocd/scripts`` argument. diff --git a/docs/zh_CN/api-guides/jtag-debugging/configure-wrover.rst b/docs/zh_CN/api-guides/jtag-debugging/configure-wrover.rst index f8d2f0843a..0d7ce00ff3 100644 --- a/docs/zh_CN/api-guides/jtag-debugging/configure-wrover.rst +++ b/docs/zh_CN/api-guides/jtag-debugging/configure-wrover.rst @@ -121,20 +121,14 @@ MacOS sudo kextunload -b com.apple.driver.AppleUSBFTDI -4. 运行 OpenOCD(以下路径为 Github 上可供下载的预编译后的 OpenOCD):: +4. 运行 OpenOCD:: - bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg - - 如果 OpenOCD 是从源码编译得到的,那么路径需要做相应修改:: - - src/openocd -s tcl -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg 5. 在另一个终端窗口,再一次加载 FTDI 串口驱动:: sudo kextload -b com.FTDI.driver.FTDIUSBSerialDriver -.. include:: ./windows-openocd-note.rst - .. note:: 如果你需要重启 OpenOCD,则无需再次卸载 FTDI 驱动程序,只需停止 OpenOCD 并再次启动它。只有在重新连接 ESP-WROVER-KIT 或者切换了电源的情况下才需要再次卸载驱动。 diff --git a/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-linux.rst b/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-linux.rst deleted file mode 100644 index f61d6eece2..0000000000 --- a/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-linux.rst +++ /dev/null @@ -1,34 +0,0 @@ -*************************** -在 Linux 环境下安装 OpenOCD -*************************** -:link_to_translation:`en:[English]` - -安装 OpenOCD -============ - -64 位 Linux 系统版本的 OpenOCD 可以直接从以下 Github 链接中下载: - -https://github.com/espressif/openocd-esp32/releases - -下载文件名称包含 `linux64` 字样的最新发布的归档文件,例如 `openocd-esp32-linux64-0.10.0-esp32-20180418.tar.gz`。 - -将该文件解压缩到 ``~/esp/`` 目录下:: - - cd ~/esp - tar -xzf ~/Downloads/openocd-esp32-linux64-.tar.gz - - -下一步 -====== - -进一下配置调试环境,请前往 :ref:`jtag-debugging-configuring-esp32-target` 章节。 - - -相关文档 -======== - -.. toctree:: - :maxdepth: 1 - - building-openocd-linux - diff --git a/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-macos.rst b/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-macos.rst deleted file mode 100644 index 5f46715cc0..0000000000 --- a/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-macos.rst +++ /dev/null @@ -1,39 +0,0 @@ -*************************** -在 MacOS 环境下安装 OpenOCD -*************************** -:link_to_translation:`en:[English]` - -安装 libusb -=========== - -使用 `Homebrew `_ 或者 `Macports `_ 来安装 `libusb` 软件包。 - -安装 OpenOCD -============ - -MacOS 系统版本的 OpenOCD 可以直接从以下 Github 链接中下载: - -https://github.com/espressif/openocd-esp32/releases - -下载文件名包含 `macos` 字样的最新发布的归档文件,例如 `openocd-esp32-macos-0.10.0-esp32-20180418.tar.gz`。 - -将该文件解压缩到 ``~/esp/`` 目录下:: - - cd ~/esp - tar -xzf ~/Downloads/openocd-esp32-macos-.tar.gz - - -下一步 -====== - -进一下配置调试环境,请前往 :ref:`jtag-debugging-configuring-esp32-target` 章节。 - - -相关文档 -======== - -.. toctree:: - :maxdepth: 1 - - building-openocd-macos - diff --git a/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-windows.rst b/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-windows.rst deleted file mode 100644 index 6cd2e508c2..0000000000 --- a/docs/zh_CN/api-guides/jtag-debugging/setup-openocd-windows.rst +++ /dev/null @@ -1,39 +0,0 @@ -***************************** -在 Windows 环境下安装 OpenOCD -***************************** -:link_to_translation:`en:[English]` - -IDF 工具安装程序 -================ - -如果您正在使用 CMake 构建系统,并遵循 :doc:`/get-started/windows-setup` 章节的指导使用了 ``ESP-IDF Tools Installer`` 的 V1.2 及其以上版本,那么默认情况下您已经安装好了 ``OpenOCD`` 软件。 - -``ESP-IDF Tools Installer`` 会将 ``OpenOCD`` 添加到环境变量 ``PATH`` 中,这样你就可以在任何目录运行它。 - -安装 OpenOCD -============ - -Windows 系统版本的 OpenOCD 可以直接从以下 Github 链接中下载: - -https://github.com/espressif/openocd-esp32/releases - -下载文件名包含 `win32` 字样的最新发布的归档文件,例如 `openocd-esp32-macos-0.10.0-win32-20180418.zip`。 - -将该文件解压缩到 ``~/esp/`` 目录下:: - - cd ~/esp - unzip /c/Users//Downloads/openocd-esp32-win32-.zip - -下一步 -====== - -进一下配置调试环境,请前往 :ref:`jtag-debugging-configuring-esp32-target` 章节。 - - -相关文档 -======== - -.. toctree:: - :maxdepth: 1 - - building-openocd-windows diff --git a/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst b/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst index 2588c8aa16..f3594ac246 100644 --- a/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst +++ b/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst @@ -35,7 +35,7 @@ ESP32 调试器支持 2 个硬件断点和 64 个软件断点。硬件断点是 由于 GDB 在连接 OpenOCD 时仅仅请求一次内存映射,所以可以在 TCL 配置文件中指定该命令,或者通过命令行传递给 OpenOCD。对于后者,命令行示例如下: - ``bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "init; halt; esp32 appimage_offset 0x210000"`` + ``openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "init; halt; esp32 appimage_offset 0x210000"`` 另外还可以通过 OpenOCD 的 telnet 会话执行该命令,然后再连接 GDB, 不过这种方式似乎没有那么便捷。 @@ -258,17 +258,13 @@ ESP32 的目标配置文件 :: - bin/openocd -l openocd_log.txt -d 3 -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + openocd -l openocd_log.txt -d 3 -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg 这种方式会将日志输出到文件,但是它会阻止调试信息打印在终端上。当有大量信息需要输出的时候(比如调试等级提高到 ``-d 3``)这是个不错的选择。如果你仍然希望在屏幕上看到调试日志,请改用以下命令: :: - bin/openocd -d 3 -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg 2>&1 | tee openocd.log - - .. note:: - - 如果运行的 OpenOCD 是从源码自行编译的,命令的格式会有些许不同,具体请参阅: :ref:`jtag-debugging-building-openocd`。 + openocd -d 3 -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg 2>&1 | tee openocd.log Debugger 端: diff --git a/docs/zh_CN/api-guides/jtag-debugging/windows-openocd-note.rst b/docs/zh_CN/api-guides/jtag-debugging/windows-openocd-note.rst deleted file mode 100644 index 1fe3676cc4..0000000000 --- a/docs/zh_CN/api-guides/jtag-debugging/windows-openocd-note.rst +++ /dev/null @@ -1,3 +0,0 @@ -.. note:: - - 如果您在 Windows 上使用 ``ESP-IDF Tools Installer`` 安装的 OpenOCD,则无需切换目录即可运行 ``openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg`` ,也无需使用 ``-s share/openocd/scripts`` 参数指定脚本文件的搜索路径。 From 7237e6fab72ff37b755476de5c03b5fc82323099 Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Fri, 16 Aug 2019 15:34:49 +0800 Subject: [PATCH 024/146] docs: jtag-debugging: update translation --- .../jtag-debugging/building-openocd-linux.rst | 2 +- .../jtag-debugging/building-openocd-macos.rst | 2 +- .../building-openocd-windows.rst | 2 +- .../jtag-debugging/building-openocd-linux.rst | 2 +- .../jtag-debugging/building-openocd-macos.rst | 2 +- .../building-openocd-windows.rst | 2 +- .../zh_CN/api-guides/jtag-debugging/index.rst | 85 +++++++++---------- docs/zh_CN/get-started/index.rst | 2 + 8 files changed, 47 insertions(+), 52 deletions(-) diff --git a/docs/en/api-guides/jtag-debugging/building-openocd-linux.rst b/docs/en/api-guides/jtag-debugging/building-openocd-linux.rst index 754c1c5cf7..846161881c 100644 --- a/docs/en/api-guides/jtag-debugging/building-openocd-linux.rst +++ b/docs/en/api-guides/jtag-debugging/building-openocd-linux.rst @@ -3,7 +3,7 @@ Building OpenOCD from Sources for Linux *************************************** :link_to_translation:`zh_CN:[中文]` -The following instructions are alternative to downloading binary OpenOCD from `Espressif GitHub `_. To quickly setup the binary OpenOCD, instead of compiling it yourself, backup and proceed to section :doc:`setup-openocd-linux`. +The following instructions are alternative to downloading binary OpenOCD from `Espressif GitHub `_. To quickly setup the binary OpenOCD, instead of compiling it yourself, backup and proceed to section :ref:`jtag-debugging-setup-openocd`. .. highlight:: bash diff --git a/docs/en/api-guides/jtag-debugging/building-openocd-macos.rst b/docs/en/api-guides/jtag-debugging/building-openocd-macos.rst index ab5656661f..0edb05149d 100644 --- a/docs/en/api-guides/jtag-debugging/building-openocd-macos.rst +++ b/docs/en/api-guides/jtag-debugging/building-openocd-macos.rst @@ -3,7 +3,7 @@ Building OpenOCD from Sources for MacOS *************************************** :link_to_translation:`zh_CN:[中文]` -The following instructions are alternative to downloading binary OpenOCD from `Espressif GitHub `_. To quickly setup the binary OpenOCD, instead of compiling it yourself, backup and proceed to section :doc:`setup-openocd-macos`. +The following instructions are alternative to downloading binary OpenOCD from `Espressif GitHub `_. To quickly setup the binary OpenOCD, instead of compiling it yourself, backup and proceed to section :ref:`jtag-debugging-setup-openocd`. .. highlight:: bash diff --git a/docs/en/api-guides/jtag-debugging/building-openocd-windows.rst b/docs/en/api-guides/jtag-debugging/building-openocd-windows.rst index 2452235176..3163c640f8 100644 --- a/docs/en/api-guides/jtag-debugging/building-openocd-windows.rst +++ b/docs/en/api-guides/jtag-debugging/building-openocd-windows.rst @@ -3,7 +3,7 @@ Building OpenOCD from Sources for Windows ***************************************** :link_to_translation:`zh_CN:[中文]` -The following instructions are alternative to downloading binary OpenOCD from `Espressif GitHub `_. To quickly setup the binary OpenOCD, instead of compiling it yourself, backup and proceed to section :doc:`setup-openocd-windows`. +The following instructions are alternative to downloading binary OpenOCD from `Espressif GitHub `_. To quickly setup the binary OpenOCD, instead of compiling it yourself, backup and proceed to section :ref:`jtag-debugging-setup-openocd`. .. highlight:: bash diff --git a/docs/zh_CN/api-guides/jtag-debugging/building-openocd-linux.rst b/docs/zh_CN/api-guides/jtag-debugging/building-openocd-linux.rst index f237599b69..9041dd1195 100644 --- a/docs/zh_CN/api-guides/jtag-debugging/building-openocd-linux.rst +++ b/docs/zh_CN/api-guides/jtag-debugging/building-openocd-linux.rst @@ -3,7 +3,7 @@ Linux 环境下从源码编译 OpenOCD ****************************** :link_to_translation:`en:[English]` -除了从 `Espressif 官方 `_ 直接下载 OpenOCD 可执行文件,你还可以选择从源码编译得到 OpenOCD。如果想要快速设置 OpenOCD 而不是自行编译,请备份好当前文件,前往 :doc:`setup-openocd-linux` 章节查阅。 +除了从 `Espressif 官方 `_ 直接下载 OpenOCD 可执行文件,你还可以选择从源码编译得到 OpenOCD。如果想要快速设置 OpenOCD 而不是自行编译,请备份好当前文件,前往 :ref:`jtag-debugging-setup-openocd` 章节查阅。 .. highlight:: bash diff --git a/docs/zh_CN/api-guides/jtag-debugging/building-openocd-macos.rst b/docs/zh_CN/api-guides/jtag-debugging/building-openocd-macos.rst index 95c42b7e36..376aba6ba7 100644 --- a/docs/zh_CN/api-guides/jtag-debugging/building-openocd-macos.rst +++ b/docs/zh_CN/api-guides/jtag-debugging/building-openocd-macos.rst @@ -3,7 +3,7 @@ MacOS 环境下从源码编译 OpenOCD ****************************** :link_to_translation:`en:[English]` -除了从 `Espressif 官方 `_ 直接下载 OpenOCD 可执行文件,你还可以选择从源码编译得到 OpenOCD。如果想要快速设置 OpenOCD 而不是自行编译,请备份好当前文件,前往 :doc:`setup-openocd-macos` 章节查阅。 +除了从 `Espressif 官方 `_ 直接下载 OpenOCD 可执行文件,你还可以选择从源码编译得到 OpenOCD。如果想要快速设置 OpenOCD 而不是自行编译,请备份好当前文件,前往 :ref:`jtag-debugging-setup-openocd` 章节查阅。 .. highlight:: bash diff --git a/docs/zh_CN/api-guides/jtag-debugging/building-openocd-windows.rst b/docs/zh_CN/api-guides/jtag-debugging/building-openocd-windows.rst index a9a44c842e..1a554b7515 100644 --- a/docs/zh_CN/api-guides/jtag-debugging/building-openocd-windows.rst +++ b/docs/zh_CN/api-guides/jtag-debugging/building-openocd-windows.rst @@ -3,7 +3,7 @@ Windows 环境下从源码编译 OpenOCD ******************************** :link_to_translation:`en:[English]` -除了从 `Espressif 官方 `_ 直接下载 OpenOCD 可执行文件,你还可以选择从源码编译得到 OpenOCD。如果想要快速设置 OpenOCD 而不是自行编译,请备份好当前文件,前往 :doc:`setup-openocd-windows` 章节查阅。 +除了从 `Espressif 官方 `_ 直接下载 OpenOCD 可执行文件,你还可以选择从源码编译得到 OpenOCD。如果想要快速设置 OpenOCD 而不是自行编译,请备份好当前文件,前往 :ref:`jtag-debugging-setup-openocd` 章节查阅。 .. highlight:: bash diff --git a/docs/zh_CN/api-guides/jtag-debugging/index.rst b/docs/zh_CN/api-guides/jtag-debugging/index.rst index 8d525a77ca..ba73268537 100644 --- a/docs/zh_CN/api-guides/jtag-debugging/index.rst +++ b/docs/zh_CN/api-guides/jtag-debugging/index.rst @@ -11,7 +11,7 @@ JTAG 调试 :ref:`jtag-debugging-selecting-jtag-adapter` 介绍有关 JTAG 硬件适配器的选择及参照标准。 :ref:`jtag-debugging-setup-openocd` - 介绍如何在 :doc:`Windows `,:doc:`Linux ` 和 :doc:`MacOS ` 操作系统上安装预编译好的 OpenOCD 软件包。 + 介绍如何安装官方预编译好的 OpenOCD 软件包并验证是否安装成功。 :ref:`jtag-debugging-configuring-esp32-target` 介绍如何设置 OpenOCD 软件并安装 JTAG 硬件适配器,这两者共同组成最终的调试目标。 :ref:`jtag-debugging-launching-debugger` @@ -81,44 +81,28 @@ JTAG 正常工作至少需要连接的信号线有:TDI,TDO,TCK,TMS 和 G 安装 OpenOCD ------------ -本节会介绍 OpenOCD 软件包的安装,如果你想从源码构建 OpenOCD,请参阅 :ref:`jtag-debugging-building-openocd`。默认所有 OpenOCD 相关的文件都会被存放到 ``~/esp/openocd-esp32`` 目录下,你也可以选择任何其它的目录,但相应地,你也需要调整本文档示例中使用的相对路径。 +.. highlight:: bash -.. toctree:: - :hidden: +如果你已经按照 :doc:`快速入门 <../../get-started/index>` 一文中的介绍安装好了 ESP-IDF 及其 CMake 构建系统,那么 OpenOCD 已经被默认安装到了你的开发系统中。在 :ref:`设置开发环境 ` 结束后,你应该能够在终端中运行如下 OpenOCD 命令:: - Windows - Linux - MacOS + openocd --version -从下面选择你使用的操作系统,并按照提示进一步设置 OpenOCD。 +.. highlight:: none -+-------------------+-------------------+-------------------+ -| |windows-logo| | |linux-logo| | |macos-logo| | -+-------------------+-------------------+-------------------+ -| `Windows`_ | `Linux`_ | `Mac OS`_ | -+-------------------+-------------------+-------------------+ +终端会输出以下信息(实际版本号可能会比这里列出的更新):: -.. |windows-logo| image:: ../../../_static/windows-logo.png - :target: ../jtag-debugging/setup-openocd-windows.html + Open On-Chip Debugger v0.10.0-esp32-20190708 (2019-07-08-11:04) + Licensed under GNU GPL v2 + For bug reports, read + http://openocd.org/doc/doxygen/bugs.html -.. |linux-logo| image:: ../../../_static/linux-logo.png - :target: ../jtag-debugging/setup-openocd-linux.html +你还可以检查 ``OPENOCD_SCRIPTS`` 环境变量的值来确认 OpenOCD 配置文件的路径,Linux 和 macOS 用户可以在终端输入 ``echo $OPENOCD_SCRIPTS``,Windows 用户需要输入 ``echo %OPENOCD_SCRIPTS%``。如果终端打印了有效的路径,则表明 OpenOCD 已经被正确安装。 -.. |macos-logo| image:: ../../../_static/macos-logo.png - :target: ../jtag-debugging/setup-openocd-macos.html - -.. _Windows: setup-openocd-windows.html -.. _Linux: setup-openocd-linux.html -.. _Mac OS: setup-openocd-macos.html - -安装完成后,请熟悉一下 ``openocd-esp32`` 安装路径下的两个关键目录: - -- ``bin`` 目录下包含了 OpenOCD 的可执行文件 -- ``share\openocd\scripts`` 目录下包含了一些配置文件,它们会作为命令行参数与 OpenOCD 一同被调用 +如果上述步骤没有成功执行,请返回快速入门手册,参考其中 :ref:`设置安装工具 ` 章节的说明。 .. note:: - 上面的目录名称和结构特定于 OpenOCD 的二进制发行版,它们会被用在本指南中的 OpenOCD 示例中。从源码构建得到的 OpenOCD 存放的目录可能会不一样,所以调用 OpenOCD 的方式也会略有不同。更多详细信息请参阅 :ref:`jtag-debugging-building-openocd`。 + 另外,我们还可以从源代码编译 OpenOCD 工具,相关详细信息请参阅 :ref:`jtag-debugging-building-openocd` 章节。 .. _jtag-debugging-configuring-esp32-target: @@ -154,23 +138,20 @@ JTAG 正常工作至少需要连接的信号线有:TDI,TDO,TCK,TMS 和 G .. highlight:: bash -打开终端,进入安装目录并启动 OpenOCD:: +打开终端,按照快速入门中的指南 :ref:`设置好开发环境 ` ,然后运行如下命令,启动 OpenOCD(该命令在 Windows,Linux,和 macOS 中通用):: - cd ~/esp/openocd-esp32 - bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg .. note:: - 如上所示,``-f`` 后面的文件是特定于板载 :ref:`ESP-WROOM-32 ` 模组的 ESP-WROVER-KIT 开发板的。您可能需要根据具体使用的硬件而提供不同的配置文件,相关指导请参阅 :ref:`jtag-debugging-tip-openocd-configure-target`。 - -.. include:: ./windows-openocd-note.rst + 上述命令中 ``-f`` 选项后跟的配置文件专用于板载 :ref:`ESP-WROOM-32 ` 模组的 ESP-WROVER-KIT 开发板。您可能需要根据具体使用的硬件而选择或修改不同的配置文件,相关指导请参阅 :ref:`jtag-debugging-tip-openocd-configure-target`。 .. highlight:: none -现在应该可以看到类似下面的输出(此日志来自 ESP-WROVER-KIT):: +现在应该可以看到如下输入(此日志来自 ESP-WROVER-KIT):: - user-name@computer-name:~/esp/openocd-esp32$ bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg - Open On-Chip Debugger 0.10.0-dev-ged7b1a9 (2017-07-10-07:16) + user-name@computer-name:~/esp/esp-idf$ openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + Open On-Chip Debugger v0.10.0-esp32-20190708 (2019-07-08-11:04) Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html @@ -198,10 +179,7 @@ JTAG 正常工作至少需要连接的信号线有:TDI,TDO,TCK,TMS 和 G 除此以外,还支持使用 OpenOCD 通过 JTAG 接口将应用程序镜像烧写到闪存中,命令如下:: - cd ~/esp/openocd-esp32 - bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "program_esp32 filename.bin 0x10000 verify exit" - -.. include:: ./windows-openocd-note.rst + openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg -c "program_esp32 filename.bin 0x10000 verify exit" 其中 OpenOCD 的烧写命令 ``program_esp32`` 具有以下格式: @@ -265,13 +243,28 @@ ESP32 的工具链中带有 GNU 调试器(简称 GDB) ``xtensa-esp32-elf-gdb Linux MacOS -.. note:: - 本文档演示所使用的 OpenOCD 是 :ref:`jtag-debugging-setup-openocd` 章节中介绍的预编译好的二进制发行版,如果要使用本地从源代码构建得到的 OpenOCD 程序,需要将相应可执行文件的路径修改为 ``src/openocd``,并将配置文件的路径修改为 ``-s tcl``。 +本文档演示所使用的 OpenOCD 是 :ref:`jtag-debugging-setup-openocd` 章节中介绍的预编译好的二进制发行版。 - 具体使用示例如下:: +.. highlight:: bash - src/openocd -s tcl -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg +如果要使用本地从源代码编译的 OpenOCD 程序,需要将相应可执行文件的路径修改为 ``src/openocd``,并设置 ``OPENOCD_SCRIPTS`` 环境变量,这样 OpenOCD 才能找到配置文件。Linux 和 macOS 用户可以执行:: + + cd ~/esp/openocd-esp32 + export OPENOCD_SCRIPTS=$PWD/tcl + +Windows 用户可以执行:: + + cd %USERPROFILE%\esp\openocd-esp32 + set "OPENOCD_SCRIPTS=%CD%\tcl" + +运行本地编译的 OpenOCD 的示例如下(Linux 和 macOS 用户):: + + src/openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg + +Windows 用户:: + + src\openocd -f interface\ftdi\esp32_devkitj_v1.cfg -f board\esp-wroom-32.cfg .. _jtag-debugging-tips-and-quirks: diff --git a/docs/zh_CN/get-started/index.rst b/docs/zh_CN/get-started/index.rst index 9f0a9ee900..06428eb994 100644 --- a/docs/zh_CN/get-started/index.rst +++ b/docs/zh_CN/get-started/index.rst @@ -133,6 +133,7 @@ ESP32 采用 40 nm 工艺制成,具有最佳的功耗性能、射频性能、 .. _get-started-get-esp-idf: +.. _get-started-set-up-tools: 第二步:获取 ESP-IDF =========================== @@ -181,6 +182,7 @@ ESP-IDF 将下载至 ``%userprofile%\esp\esp-idf``。 git submodule update --init .. _get-started-setup-path: +.. _get-started-set-up-env: 第三步:设置环境变量 =========================== From 58418c790a4320ee2d39cc160aaa8b6a129c21d9 Mon Sep 17 00:00:00 2001 From: Sergei Silnov Date: Tue, 2 Jul 2019 20:33:32 +0200 Subject: [PATCH 025/146] idf.py: Add support for deprecation of command/options --- tools/ci/test_build_system_cmake.sh | 12 +++++ tools/idf.py | 84 ++++++++++++++++++++++++++++- tools/test_idf_py/idf_ext.py | 70 ++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 tools/test_idf_py/idf_ext.py diff --git a/tools/ci/test_build_system_cmake.sh b/tools/ci/test_build_system_cmake.sh index 47e1ae23ee..0872d3320e 100755 --- a/tools/ci/test_build_system_cmake.sh +++ b/tools/ci/test_build_system_cmake.sh @@ -527,6 +527,18 @@ endmenu\n" >> ${IDF_PATH}/Kconfig; rm -rf CMakeLists.txt mv CMakeLists.txt.bak CMakeLists.txt rm -rf CMakeLists.txt.bak + + print_status "Print all required argument deprecation warnings" + idf.py -C${IDF_PATH}/tools/test_idf_py --test-0=a --test-1=b --test-2=c --test-3=d test-0 --test-sub-0=sa --test-sub-1=sb ta test-1 > out.txt + ! grep -e '"test-0" is deprecated' -e '"test_0" is deprecated' out.txt || failure "Deprecation warnings are displayed for non-deprecated option/command" + grep -e 'Warning: Option "test_sub_1" is deprecated and will be removed in future versions.' \ + -e 'Warning: Command "test-1" is deprecated and will be removed in future versions. Please use alternative command.' \ + -e 'Warning: Option "test_1" is deprecated and will be removed in future versions.' \ + -e 'Warning: Option "test_2" is deprecated and will be removed in future versions. Please update your parameters.' \ + -e 'Warning: Option "test_3" is deprecated and will be removed in future versions.' \ + out.txt \ + || failure "Deprecation warnings are not displayed" + rm out.txt print_status "All tests completed" if [ -n "${FAILURES}" ]; then diff --git a/tools/idf.py b/tools/idf.py index 6d4e5fe43a..e33152e631 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -545,6 +545,46 @@ def init_cli(): # Click is imported here to run it after check_environment() import click + class DeprecationMessage(object): + """Construct deprecation notice for help messages""" + + def __init__(self, deprecated=False): + self.deprecated = deprecated + self.since = None + self.removed = None + self.custom_message = "" + + if isinstance(deprecated, dict): + self.custom_message = deprecated.get("message", "") + self.since = deprecated.get("since", None) + self.removed = deprecated.get("removed", None) + elif isinstance(deprecated, str): + self.custom_message = deprecated + + def full_message(self, type="Option"): + return "%s is deprecated %sand will be removed in%s.%s" % ( + type, + "since %s " % self.since if self.since else "", + " %s" % self.removed if self.removed else " future versions", + " %s" % self.custom_message if self.custom_message else "" + ) + + def help(self, text, type="Option", separator=" "): + text = text or "" + return self.full_message(type) + separator + text if self.deprecated else text + + def short_help(self, text): + text = text or "" + return ("Deprecated! " + text) if self.deprecated else text + + def print_deprecation_warning(ctx): + """Prints deprectation warnings for arguments in given context""" + for option in ctx.command.params: + default = () if option.multiple else option.default + if isinstance(option, Option) and option.deprecated and ctx.params[option.name] != default: + print("Warning: %s" % DeprecationMessage(option.deprecated). + full_message('Option "%s"' % option.name)) + class Task(object): def __init__( self, callback, name, aliases, dependencies, order_dependencies, action_args @@ -567,6 +607,7 @@ def init_cli(): self, name=None, aliases=None, + deprecated=False, dependencies=None, order_dependencies=None, **kwargs @@ -574,6 +615,7 @@ def init_cli(): super(Action, self).__init__(name, **kwargs) self.name = self.name or self.callback.__name__ + self.deprecated = deprecated if aliases is None: aliases = [] @@ -592,6 +634,11 @@ def init_cli(): # Show first line of help if short help is missing self.short_help = self.short_help or self.help.split("\n")[0] + if deprecated: + deprecation = DeprecationMessage(deprecated) + self.short_help = deprecation.short_help(self.short_help) + self.help = deprecation.help(self.help, type="Command", separator="\n") + # Add aliases to help string if aliases: aliases_help = "Aliases: %s." % ", ".join(aliases) @@ -614,8 +661,21 @@ def init_cli(): self.callback = wrapped_callback + def invoke(self, ctx): + if self.deprecated: + print("Warning: %s" % DeprecationMessage(self.deprecated).full_message('Command "%s"' % self.name)) + self.deprecated = False # disable Click's built-in deprecation handling + + # Print warnings for options + print_deprecation_warning(ctx) + return super(Action, self).invoke(ctx) + class Argument(click.Argument): - """Positional argument""" + """ + Positional argument + + names - alias of 'param_decls' + """ def __init__(self, **kwargs): names = kwargs.pop("names") @@ -656,12 +716,28 @@ def init_cli(): class Option(click.Option): """Option that knows whether it should be global""" - def __init__(self, scope=None, **kwargs): + def __init__(self, scope=None, deprecated=False, **kwargs): + """ + Keyword arguments additional to Click's Option class: + + names - alias of 'param_decls' + deprecated - marks option as deprecated. May be boolean, string (with custom deprecation message) + or dict with optional keys: + since: version of deprecation + removed: version when option will be removed + custom_message: Additional text to deprecation warning + """ + kwargs["param_decls"] = kwargs.pop("names") super(Option, self).__init__(**kwargs) + self.deprecated = deprecated self.scope = Scope(scope) + if deprecated: + deprecation = DeprecationMessage(deprecated) + self.help = deprecation.help(self.help) + if self.scope.is_global: self.help += " This option can be used at most once either globally, or for one subcommand." @@ -823,6 +899,7 @@ def init_cli(): for task in tasks: for key in list(task.action_args): option = next((o for o in ctx.command.params if o.name == key), None) + if option and (option.scope.is_global or option.scope.is_shared): local_value = task.action_args.pop(key) global_value = global_args[key] @@ -836,6 +913,9 @@ def init_cli(): if local_value != default: global_args[key] = local_value + # Show warnings about global arguments + print_deprecation_warning(ctx) + # Validate global arguments for action_callback in ctx.command.global_action_callbacks: action_callback(ctx, global_args, tasks) diff --git a/tools/test_idf_py/idf_ext.py b/tools/test_idf_py/idf_ext.py new file mode 100644 index 0000000000..b8fc8c3f21 --- /dev/null +++ b/tools/test_idf_py/idf_ext.py @@ -0,0 +1,70 @@ +def action_extensions(base_actions, project_path=None): + def echo(name, *args, **kwargs): + print(name, args, kwargs) + + # Add global options + extensions = { + "global_options": [ + { + "names": ["--test-0"], + "help": "Non-deprecated option.", + "deprecated": False + }, + { + "names": ["--test-1"], + "help": "Deprecated option 1.", + "deprecated": True + }, + { + "names": ["--test-2"], + "help": "Deprecated option 2.", + "deprecated": "Please update your parameters." + }, + { + "names": ["--test-3"], + "help": "Deprecated option 3.", + "deprecated": { + "custom_message": "Please update your parameters." + } + }, + { + "names": ["--test-4"], + "help": "Deprecated option 3.", + "deprecated": { + "since": "v4.0", + "removed": "v5.0" + } + }, + ], + "actions": { + "test-0": { + "callback": + echo, + "help": + "Non-deprecated command 0", + "options": [ + { + "names": ["--test-sub-0"], + "help": "Non-deprecated subcommand option 0", + "default": None, + }, + { + "names": ["--test-sub-1"], + "help": "Deprecated subcommand option 1", + "default": None, + "deprecated": True + }, + ], + "arguments": [{ + "names": ["test-arg-0"], + }], + }, + "test-1": { + "callback": echo, + "help": "Deprecated command 1", + "deprecated": "Please use alternative command." + }, + }, + } + + return extensions From 36920b4062535bd7abed1ceee1c73922d98b8878 Mon Sep 17 00:00:00 2001 From: xueyunfei Date: Thu, 15 Aug 2019 11:17:15 +0800 Subject: [PATCH 026/146] fix bug for nonblocking udp is zero --- components/lwip/linker.lf | 3 +++ components/lwip/lwip | 2 +- components/lwip/port/esp32/include/lwipopts.h | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/components/lwip/linker.lf b/components/lwip/linker.lf index 157e4b88bc..ec48930ea7 100644 --- a/components/lwip/linker.lf +++ b/components/lwip/linker.lf @@ -7,6 +7,7 @@ entries: sockets:tryget_socket_unconn (noflash_text) sockets:sock_inc_used (noflash_text) sockets:tryget_socket_unconn_nouse (noflash_text) + sockets:tryget_socket_unconn_locked (noflash_text) sockets:done_socket (noflash_text) sockets:lwip_recvfrom (noflash_text) sockets:lwip_recv_tcp (noflash_text) @@ -15,6 +16,8 @@ entries: sockets:lwip_send (noflash_text) sockets:lwip_sendto (noflash_text) sockets:event_callback (noflash_text) + sockets:lwip_select (noflash_text) + sockets:select_check_waiters (noflash_text) api_lib:netconn_apimsg (noflash_text) api_lib:netconn_recv_data (noflash_text) api_lib:netconn_tcp_recvd_msg (noflash_text) diff --git a/components/lwip/lwip b/components/lwip/lwip index 663b2fdb41..8c801667e8 160000 --- a/components/lwip/lwip +++ b/components/lwip/lwip @@ -1 +1 @@ -Subproject commit 663b2fdb41177c82f2aa5939e41aef54427d15cd +Subproject commit 8c801667e8586ae6dedf80d13aaaec71b7b9a33d diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index e37d272ccc..5b85fc6ccf 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -778,6 +778,7 @@ #define ESP_PPP 1 #define ESP_IPV6 1 #define ESP_SOCKET 1 +#define ESP_LWIP_SELECT 1 #ifdef ESP_IRAM_ATTR #undef ESP_IRAM_ATTR From 3eecd43b31a765aae0231ff9a227786bac31b9a2 Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Wed, 21 Aug 2019 12:00:55 +0800 Subject: [PATCH 027/146] cmake: fix issue with checking build process args --- tools/cmake/build.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cmake/build.cmake b/tools/cmake/build.cmake index 5383581f12..11810d9af3 100644 --- a/tools/cmake/build.cmake +++ b/tools/cmake/build.cmake @@ -295,7 +295,7 @@ endmacro() # macro(__build_set_default var default) set(_var __${var}) - if(${_var}) + if(NOT "${_var}" STREQUAL "") idf_build_set_property(${var} "${${_var}}") else() idf_build_set_property(${var} "${default}") From 4690152eca28cffdeee66bb0552b536d4ebd5380 Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Wed, 21 Aug 2019 12:43:19 +0800 Subject: [PATCH 028/146] cmake: make default version 1 --- docs/en/api-guides/build-system.rst | 2 +- tools/cmake/build.cmake | 4 ++-- tools/cmake/project.cmake | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/en/api-guides/build-system.rst b/docs/en/api-guides/build-system.rst index 2eed1fa5ab..30d59bf8fa 100644 --- a/docs/en/api-guides/build-system.rst +++ b/docs/en/api-guides/build-system.rst @@ -949,7 +949,7 @@ The call requires the target chip to be specified with *target* argument. Option - PROJECT_DIR - directory of the project; defaults to CMAKE_SOURCE_DIR - PROJECT_NAME - name of the project; defaults to CMAKE_PROJECT_NAME -- PROJECT_VER - version/revision of the project; defaults to "0.0.0" +- PROJECT_VER - version/revision of the project; defaults to "1" - SDKCONFIG - output path of generated sdkconfig file; defaults to PROJECT_DIR/sdkconfig or CMAKE_SOURCE_DIR/sdkconfig depending if PROJECT_DIR is set - SDKCONFIG_DEFAULTS - defaults file to use for the build; defaults to empty - BUILD_DIR - directory to place ESP-IDF build-related artifacts, such as generated binaries, text files, components; defaults to CMAKE_BINARY_DIR diff --git a/tools/cmake/build.cmake b/tools/cmake/build.cmake index 11810d9af3..9285a53e1c 100644 --- a/tools/cmake/build.cmake +++ b/tools/cmake/build.cmake @@ -330,7 +330,7 @@ endfunction() # @param[in, optional] PROJECT_DIR (single value) directory of the main project the buildsystem # is processed for; defaults to CMAKE_SOURCE_DIR # @param[in, optional] PROJECT_VER (single value) version string of the main project; defaults -# to 0.0.0 +# to 1 # @param[in, optional] PROJECT_NAME (single value) main project name, defaults to CMAKE_PROJECT_NAME # @param[in, optional] SDKCONFIG (single value) sdkconfig output path, defaults to PROJECT_DIR/sdkconfig # if PROJECT_DIR is set and CMAKE_SOURCE_DIR/sdkconfig if not @@ -366,7 +366,7 @@ macro(idf_build_process target) __build_set_default(PROJECT_DIR ${CMAKE_SOURCE_DIR}) __build_set_default(PROJECT_NAME ${CMAKE_PROJECT_NAME}) - __build_set_default(PROJECT_VER "0.0.0") + __build_set_default(PROJECT_VER 1) __build_set_default(BUILD_DIR ${CMAKE_BINARY_DIR}) idf_build_get_property(project_dir PROJECT_DIR) diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index ff9b75ec6f..a8e28a12d8 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -40,7 +40,6 @@ function(__project_get_revision var) else() message(STATUS "Project is not inside a git repository, \ will not use 'git describe' to determine PROJECT_VER.") - set(PROJECT_VER "1") endif() endif() endif() From b7e73025fe086f62fcef49a3f8444e5287b409e1 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 19 Aug 2019 19:46:47 +0200 Subject: [PATCH 029/146] Fixed syntax error in esp_http_server.rst Changed the "const char[] name" into "const char name[]", so now the code doesn't produce the "expected identifier or '(' before '[' token" error. Closes https://github.com/espressif/esp-idf/pull/3940 --- docs/en/api-reference/protocols/esp_http_server.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/api-reference/protocols/esp_http_server.rst b/docs/en/api-reference/protocols/esp_http_server.rst index e37982bf66..a660173a6b 100644 --- a/docs/en/api-reference/protocols/esp_http_server.rst +++ b/docs/en/api-reference/protocols/esp_http_server.rst @@ -22,7 +22,7 @@ Application Example esp_err_t get_handler(httpd_req_t *req) { /* Send a simple response */ - const char[] resp = "URI GET Response"; + const char resp[] = "URI GET Response"; httpd_resp_send(req, resp, strlen(resp)); return ESP_OK; } @@ -55,7 +55,7 @@ Application Example } /* Send a simple response */ - const char[] resp = "URI POST Response"; + const char resp[] = "URI POST Response"; httpd_resp_send(req, resp, strlen(resp)); return ESP_OK; } From 393eb1700d3ddb9fb9a90024fc12a1d15019f578 Mon Sep 17 00:00:00 2001 From: Prasad Alatkar Date: Wed, 21 Aug 2019 19:28:47 +0800 Subject: [PATCH 030/146] NimBLE: Add misc menuconfig options - Add option to configure stack size of NimBLE host task, update NimBLE submodule for the same. - Change max concurrent connections supported to 9 inline with controller. --- components/bt/host/nimble/Kconfig.in | 11 +++++++++-- components/bt/host/nimble/nimble | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index 2fcbf91c78..acf34a73f2 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -1,7 +1,7 @@ config BT_NIMBLE_MAX_CONNECTIONS int "Maximum number of concurrent connections" - range 1 10 + range 1 9 default 1 depends on BT_NIMBLE_ENABLED help @@ -23,7 +23,7 @@ config BT_NIMBLE_MAX_CCCDS config BT_NIMBLE_L2CAP_COC_MAX_NUM int "Maximum number of connection oriented channels" - range 0 10 + range 0 9 depends on BT_NIMBLE_ENABLED default 0 help @@ -50,6 +50,13 @@ config BT_NIMBLE_PINNED_TO_CORE default 1 if BT_NIMBLE_PINNED_TO_CORE_1 default 0 +config BT_NIMBLE_TASK_STACK_SIZE + int "NimBLE Host task stack size" + depends on BT_NIMBLE_ENABLED + default 4096 + help + This configures stack size of NimBLE host task + config BT_NIMBLE_ROLE_CENTRAL bool "Enable BLE Central role" depends on BT_NIMBLE_ENABLED diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index 4839d84f61..adcd940869 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit 4839d84f61296b7d7479350ebb92908b0fdb1329 +Subproject commit adcd9408695cb4f873f117eb8c92007455b2c066 From 1e32fa2cf7cf328d965a8f5015cbb28d787beb58 Mon Sep 17 00:00:00 2001 From: Tim Nordell Date: Fri, 12 Apr 2019 15:35:34 -0500 Subject: [PATCH 031/146] bootloader: Remove extraneous newlines from some debug statements ESP_LOGD(...) provides its own new lines so remove these from these debug strings. Signed-off-by: Tim Nordell --- components/bootloader_support/src/esp_image_format.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/bootloader_support/src/esp_image_format.c b/components/bootloader_support/src/esp_image_format.c index 620d884a2e..95959325d0 100644 --- a/components/bootloader_support/src/esp_image_format.c +++ b/components/bootloader_support/src/esp_image_format.c @@ -494,15 +494,15 @@ static bool should_load(uint32_t load_addr) if (!load_rtc_memory) { if (load_addr >= SOC_RTC_IRAM_LOW && load_addr < SOC_RTC_IRAM_HIGH) { - ESP_LOGD(TAG, "Skipping RTC fast memory segment at 0x%08x\n", load_addr); + ESP_LOGD(TAG, "Skipping RTC fast memory segment at 0x%08x", load_addr); return false; } if (load_addr >= SOC_RTC_DRAM_LOW && load_addr < SOC_RTC_DRAM_HIGH) { - ESP_LOGD(TAG, "Skipping RTC fast memory segment at 0x%08x\n", load_addr); + ESP_LOGD(TAG, "Skipping RTC fast memory segment at 0x%08x", load_addr); return false; } if (load_addr >= SOC_RTC_DATA_LOW && load_addr < SOC_RTC_DATA_HIGH) { - ESP_LOGD(TAG, "Skipping RTC slow memory segment at 0x%08x\n", load_addr); + ESP_LOGD(TAG, "Skipping RTC slow memory segment at 0x%08x", load_addr); return false; } } From 82984f05394bc04e5abda365bc734d168c2d69b7 Mon Sep 17 00:00:00 2001 From: Tim Nordell Date: Fri, 12 Apr 2019 15:34:46 -0500 Subject: [PATCH 032/146] bootloader: Do not obfuscate RAM if we are not doing image validation No need to take this step if we are not doing image validation. The obfuscation only buys us a tiny bit of "security" anyways since the main parts of flash are memory mapped, too. This saves a little bit of wake-up time when waking up from deep sleep when the BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP option is set. Signed-off-by: Tim Nordell --- components/bootloader_support/src/esp_image_format.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/bootloader_support/src/esp_image_format.c b/components/bootloader_support/src/esp_image_format.c index 95959325d0..4bda87af19 100644 --- a/components/bootloader_support/src/esp_image_format.c +++ b/components/bootloader_support/src/esp_image_format.c @@ -225,7 +225,7 @@ static esp_err_t image_load(esp_image_load_mode_t mode, const esp_partition_pos_ } #ifdef BOOTLOADER_BUILD - if (do_load) { // Need to deobfuscate RAM + if (do_load && ram_obfs_value[0] != 0 && ram_obfs_value[1] != 0) { // Need to deobfuscate RAM for (int i = 0; i < data->image.segment_count; i++) { uint32_t load_addr = data->segments[i].load_addr; if (should_load(load_addr)) { @@ -403,6 +403,12 @@ static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, ui return ESP_FAIL; } + if (checksum == NULL && sha_handle == NULL) { + memcpy((void *)load_addr, data, data_len); + bootloader_munmap(data); + return ESP_OK; + } + #ifdef BOOTLOADER_BUILD // Set up the obfuscation value to use for loading while (ram_obfs_value[0] == 0 || ram_obfs_value[1] == 0) { From 43393cf4d17dadb478b2a9e0fda473b121af7f48 Mon Sep 17 00:00:00 2001 From: Tim Nordell Date: Fri, 12 Apr 2019 15:32:47 -0500 Subject: [PATCH 033/146] bootloader: Support for skipping validation upon wake from deep sleep This saves time when waking up from deep sleep, but potentially decreases the security of the system. If the application able to modify itself (especially areas that are loaded into RAM) in flash while running without crashing or is modifies the cached bits of information about what was last booted from the bootloader, this could cause security issues if the user does a "deep sleep reset" since the full validation is skipped. Signed-off-by: Tim Nordell --- components/bootloader/Kconfig.projbuild | 25 +++ .../include/esp_image_format.h | 26 ++- .../src/bootloader_utility.c | 24 +++ .../bootloader_support/src/esp_image_format.c | 166 ++++++++++-------- components/esp32/ld/esp32.ld | 8 +- 5 files changed, 175 insertions(+), 74 deletions(-) diff --git a/components/bootloader/Kconfig.projbuild b/components/bootloader/Kconfig.projbuild index a2c2df6444..ba08b72385 100644 --- a/components/bootloader/Kconfig.projbuild +++ b/components/bootloader/Kconfig.projbuild @@ -219,6 +219,31 @@ menu "Bootloader config" It allow to test anti-rollback implemention without permanent write eFuse bits. In partition table should be exist this partition `emul_efuse, data, 5, , 0x2000`. + config BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP + bool "Skip image validation when exiting deep sleep" + depends on (SECURE_BOOT_ENABLED && SECURE_BOOT_INSECURE) || !SECURE_BOOT_ENABLED + default n + help + This option disables the normal validation of an image coming out of + deep sleep (checksums, SHA256, and signature). This is a trade-off + between wakeup performance from deep sleep, and image integrity checks. + + Only enable this if you know what you are doing. It should not be used + in conjunction with using deep_sleep() entry and changing the active OTA + partition as this would skip the validation upon first load of the new + OTA partition. + + config BOOTLOADER_RESERVE_RTC_SIZE + hex + default 0x10 if BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP + default 0 + help + Reserve RTC FAST memory for Skip image validation. This option in bytes. + This option reserves an area in the RTC FAST memory (access only PRO_CPU). + Used to save the addresses of the selected application. + When a wakeup occurs (from Deep sleep), the bootloader retrieves it and + loads the application without validation. + endmenu # Bootloader diff --git a/components/bootloader_support/include/esp_image_format.h b/components/bootloader_support/include/esp_image_format.h index d0a9469d66..09d4bda3da 100644 --- a/components/bootloader_support/include/esp_image_format.h +++ b/components/bootloader_support/include/esp_image_format.h @@ -42,12 +42,12 @@ typedef struct { uint8_t image_digest[32]; /* appended SHA-256 digest */ } esp_image_metadata_t; -/* Mode selection for esp_image_load() */ typedef enum { - ESP_IMAGE_VERIFY, /* Verify image contents, load metadata. Print errors. */ - ESP_IMAGE_VERIFY_SILENT, /* Verify image contents, load metadata. Don't print errors. */ + ESP_IMAGE_VERIFY, /* Verify image contents, not load to memory, load metadata. Print errors. */ + ESP_IMAGE_VERIFY_SILENT, /* Verify image contents, not load to memory, load metadata. Don't print errors. */ #ifdef BOOTLOADER_BUILD - ESP_IMAGE_LOAD, /* Verify image contents, load to memory. Print errors. */ + ESP_IMAGE_LOAD, /* Verify image contents, load to memory, load metadata. Print errors. */ + ESP_IMAGE_LOAD_NO_VALIDATE, /* Not verify image contents, load to memory, load metadata. Print errors. */ #endif } esp_image_load_mode_t; @@ -134,6 +134,24 @@ esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t */ esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data); +/** + * @brief Load an app image without verification (available only in space of bootloader). + * + * If encryption is enabled, data will be transparently decrypted. + * + * @param part Partition to load the app from. + * @param[inout] data Pointer to the image metadata structure which is be filled in by this function. + * 'start_addr' member should be set (to the start address of the image.) + * Other fields will all be initialised by this function. + * + * @return + * - ESP_OK if verify or load was successful + * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs + * - ESP_ERR_IMAGE_INVALID if the image appears invalid. + * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid. + */ +esp_err_t bootloader_load_image_no_verify(const esp_partition_pos_t *part, esp_image_metadata_t *data); + /** * @brief Verify the bootloader image. * diff --git a/components/bootloader_support/src/bootloader_utility.c b/components/bootloader_support/src/bootloader_utility.c index 3f9d9fe8a7..7d5f67ac7e 100644 --- a/components/bootloader_support/src/bootloader_utility.c +++ b/components/bootloader_support/src/bootloader_utility.c @@ -28,6 +28,7 @@ #include "esp32/rom/uart.h" #include "esp32/rom/gpio.h" #include "esp32/rom/secure_boot.h" +#include "esp32/rom/rtc.h" #include "soc/soc.h" #include "soc/cpu.h" @@ -416,8 +417,31 @@ static void set_actual_ota_seq(const bootloader_state_t *bs, int index) update_anti_rollback(&bs->ota[index]); #endif } +#if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) + esp_partition_pos_t partition = index_to_partition(bs, index); + bootloader_common_update_rtc_retain_mem(&partition, true); +#endif } +#ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP +void bootloader_utility_load_boot_image_from_deep_sleep(void) +{ + if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) { + esp_partition_pos_t* partition = bootloader_common_get_rtc_retain_mem_partition(); + if (partition != NULL) { + esp_image_metadata_t image_data; + if (bootloader_load_image_no_verify(partition, &image_data) == ESP_OK) { + ESP_LOGI(TAG, "Fast booting app from partition at offset 0x%x", partition->offset); + bootloader_common_update_rtc_retain_mem(NULL, true); + load_image(&image_data); + } + } + ESP_LOGE(TAG, "Fast booting is not successful"); + ESP_LOGI(TAG, "Try to load an app as usual with all validations"); + } +} +#endif + #define TRY_LOG_FORMAT "Trying partition index %d offs 0x%x size 0x%x" void bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_index) diff --git a/components/bootloader_support/src/esp_image_format.c b/components/bootloader_support/src/esp_image_format.c index 4bda87af19..b0db9b3984 100644 --- a/components/bootloader_support/src/esp_image_format.c +++ b/components/bootloader_support/src/esp_image_format.c @@ -97,14 +97,17 @@ static esp_err_t __attribute__((unused)) verify_simple_hash(bootloader_sha256_ha static esp_err_t image_load(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data) { #ifdef BOOTLOADER_BUILD - bool do_load = (mode == ESP_IMAGE_LOAD); + bool do_load = (mode == ESP_IMAGE_LOAD) || (mode == ESP_IMAGE_LOAD_NO_VALIDATE); + bool do_verify = (mode == ESP_IMAGE_LOAD) || (mode == ESP_IMAGE_VERIFY) || (mode == ESP_IMAGE_VERIFY_SILENT); #else - bool do_load = false; // Can't load the image in app mode + bool do_load = false; // Can't load the image in app mode + bool do_verify = true; // In app mode is avalible only verify mode #endif - bool silent = (mode == ESP_IMAGE_VERIFY_SILENT); + bool silent = (mode == ESP_IMAGE_VERIFY_SILENT); esp_err_t err = ESP_OK; // checksum the image a word at a time. This shaves 30-40ms per MB of image size uint32_t checksum_word = ESP_ROM_CHECKSUM_INITIAL; + uint32_t *checksum = NULL; bootloader_sha256_handle_t sha_handle = NULL; if (data == NULL || part == NULL) { @@ -125,41 +128,45 @@ static esp_err_t image_load(esp_image_load_mode_t mode, const esp_partition_pos_ goto err; } - // Calculate SHA-256 of image if secure boot is on, or if image has a hash appended + if (do_verify) { + checksum = &checksum_word; + + // Calculate SHA-256 of image if secure boot is on, or if image has a hash appended #ifdef SECURE_BOOT_CHECK_SIGNATURE - if (1) { + if (1) { #else - if (data->image.hash_appended) { + if (data->image.hash_appended) { #endif - sha_handle = bootloader_sha256_start(); - if (sha_handle == NULL) { - return ESP_ERR_NO_MEM; + sha_handle = bootloader_sha256_start(); + if (sha_handle == NULL) { + return ESP_ERR_NO_MEM; + } + bootloader_sha256_data(sha_handle, &data->image, sizeof(esp_image_header_t)); } - bootloader_sha256_data(sha_handle, &data->image, sizeof(esp_image_header_t)); - } - ESP_LOGD(TAG, "image header: 0x%02x 0x%02x 0x%02x 0x%02x %08x", - data->image.magic, - data->image.segment_count, - data->image.spi_mode, - data->image.spi_size, - data->image.entry_addr); + ESP_LOGD(TAG, "image header: 0x%02x 0x%02x 0x%02x 0x%02x %08x", + data->image.magic, + data->image.segment_count, + data->image.spi_mode, + data->image.spi_size, + data->image.entry_addr); - err = verify_image_header(data->start_addr, &data->image, silent); - if (err != ESP_OK) { - goto err; - } + err = verify_image_header(data->start_addr, &data->image, silent); + if (err != ESP_OK) { + goto err; + } - if (data->image.segment_count > ESP_IMAGE_MAX_SEGMENTS) { - FAIL_LOAD("image at 0x%x segment count %d exceeds max %d", - data->start_addr, data->image.segment_count, ESP_IMAGE_MAX_SEGMENTS); - } + if (data->image.segment_count > ESP_IMAGE_MAX_SEGMENTS) { + FAIL_LOAD("image at 0x%x segment count %d exceeds max %d", + data->start_addr, data->image.segment_count, ESP_IMAGE_MAX_SEGMENTS); + } + } // if (do_verify) uint32_t next_addr = data->start_addr + sizeof(esp_image_header_t); for(int i = 0; i < data->image.segment_count; i++) { esp_image_segment_header_t *header = &data->segments[i]; ESP_LOGV(TAG, "loading segment header %d at offset 0x%x", i, next_addr); - err = process_segment(i, next_addr, header, silent, do_load, sha_handle, &checksum_word); + err = process_segment(i, next_addr, header, silent, do_load, sha_handle, checksum); if (err != ESP_OK) { goto err; } @@ -168,58 +175,61 @@ static esp_err_t image_load(esp_image_load_mode_t mode, const esp_partition_pos_ next_addr += header->data_len; } - // Segments all loaded, verify length - uint32_t end_addr = next_addr; - if (end_addr < data->start_addr) { - FAIL_LOAD("image offset has wrapped"); - } - - data->image_len = end_addr - data->start_addr; - ESP_LOGV(TAG, "image start 0x%08x end of last section 0x%08x", data->start_addr, end_addr); - if (!esp_cpu_in_ocd_debug_mode()) { - err = verify_checksum(sha_handle, checksum_word, data); - if (err != ESP_OK) { - goto err; + if (do_verify) { + // Segments all loaded, verify length + uint32_t end_addr = next_addr; + if (end_addr < data->start_addr) { + FAIL_LOAD("image offset has wrapped"); } - } - if (data->image_len > part->size) { - FAIL_LOAD("Image length %d doesn't fit in partition length %d", data->image_len, part->size); - } - bool is_bootloader = (data->start_addr == ESP_BOOTLOADER_OFFSET); - /* For secure boot, we don't verify signature on bootloaders. + data->image_len = end_addr - data->start_addr; + ESP_LOGV(TAG, "image start 0x%08x end of last section 0x%08x", data->start_addr, end_addr); + if (NULL != checksum && !esp_cpu_in_ocd_debug_mode()) { + err = verify_checksum(sha_handle, checksum_word, data); + if (err != ESP_OK) { + goto err; + } + } - For non-secure boot, we don't verify any SHA-256 hash appended to the bootloader because esptool.py may have - rewritten the header - rely on esptool.py having verified the bootloader at flashing time, instead. - */ - if (!is_bootloader) { + if (data->image_len > part->size) { + FAIL_LOAD("Image length %d doesn't fit in partition length %d", data->image_len, part->size); + } + + bool is_bootloader = (data->start_addr == ESP_BOOTLOADER_OFFSET); + /* For secure boot, we don't verify signature on bootloaders. + + For non-secure boot, we don't verify any SHA-256 hash appended to the bootloader because esptool.py may have + rewritten the header - rely on esptool.py having verified the bootloader at flashing time, instead. + */ + if (!is_bootloader) { #ifdef SECURE_BOOT_CHECK_SIGNATURE - // secure boot images have a signature appended - err = verify_secure_boot_signature(sha_handle, data); + // secure boot images have a signature appended + err = verify_secure_boot_signature(sha_handle, data); #else - // No secure boot, but SHA-256 can be appended for basic corruption detection - if (sha_handle != NULL && !esp_cpu_in_ocd_debug_mode()) { - err = verify_simple_hash(sha_handle, data); - } + // No secure boot, but SHA-256 can be appended for basic corruption detection + if (sha_handle != NULL && !esp_cpu_in_ocd_debug_mode()) { + err = verify_simple_hash(sha_handle, data); + } #endif // SECURE_BOOT_CHECK_SIGNATURE - } else { // is_bootloader - // bootloader may still have a sha256 digest handle open - if (sha_handle != NULL) { - bootloader_sha256_finish(sha_handle, NULL); + } else { // is_bootloader + // bootloader may still have a sha256 digest handle open + if (sha_handle != NULL) { + bootloader_sha256_finish(sha_handle, NULL); + } } - } - if (data->image.hash_appended) { - const void *hash = bootloader_mmap(data->start_addr + data->image_len - HASH_LEN, HASH_LEN); - if (hash == NULL) { - err = ESP_FAIL; - goto err; + if (data->image.hash_appended) { + const void *hash = bootloader_mmap(data->start_addr + data->image_len - HASH_LEN, HASH_LEN); + if (hash == NULL) { + err = ESP_FAIL; + goto err; + } + memcpy(data->image_digest, hash, HASH_LEN); + bootloader_munmap(hash); } - memcpy(data->image_digest, hash, HASH_LEN); - bootloader_munmap(hash); - } + sha_handle = NULL; + } // if (do_verify) - sha_handle = NULL; if (err != ESP_OK) { goto err; } @@ -263,6 +273,15 @@ esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metad #endif } +esp_err_t bootloader_load_image_no_verify(const esp_partition_pos_t *part, esp_image_metadata_t *data) +{ +#ifdef BOOTLOADER_BUILD + return image_load(ESP_IMAGE_LOAD_NO_VALIDATE, part, data); +#else + return ESP_FAIL; +#endif +} + esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data) { return image_load(mode, part, data); @@ -396,6 +415,13 @@ err: static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, uint32_t data_len, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum) { + // If we are not loading, and the checksum is empty, skip processing this + // segment for data + if(!do_load && checksum == NULL) { + ESP_LOGD(TAG, "skipping checksum for segment"); + return ESP_OK; + } + const uint32_t *data = (const uint32_t *)bootloader_mmap(data_addr, data_len); if(!data) { ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", @@ -422,7 +448,9 @@ static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, ui for (int i = 0; i < data_len; i += 4) { int w_i = i/4; // Word index uint32_t w = src[w_i]; - *checksum ^= w; + if (checksum != NULL) { + *checksum ^= w; + } #ifdef BOOTLOADER_BUILD if (do_load) { dest[w_i] = w ^ ((w_i & 1) ? ram_obfs_value[0] : ram_obfs_value[1]); diff --git a/components/esp32/ld/esp32.ld b/components/esp32/ld/esp32.ld index d88cdc0342..514b9ebd82 100644 --- a/components/esp32/ld/esp32.ld +++ b/components/esp32/ld/esp32.ld @@ -21,6 +21,12 @@ #define CONFIG_BT_RESERVE_DRAM 0 #endif +#if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) +#define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE) +#else +#define ESP_BOOTLOADER_RESERVE_RTC 0 +#endif + #if defined(CONFIG_ESP32_USE_FIXED_STATIC_RAM_SIZE) ASSERT((CONFIG_ESP32_FIXED_STATIC_RAM_SIZE <= 0x2c200), @@ -74,7 +80,7 @@ MEMORY rtc_iram_seg(RWX) : org = 0x400C0000, len = 0x2000 /* RTC fast memory (same block as above), viewed from data bus */ - rtc_data_seg(RW) : org = 0x3ff80000, len = 0x2000 + rtc_data_seg(RW) : org = 0x3ff80000, len = 0x2000 - ESP_BOOTLOADER_RESERVE_RTC /* RTC slow memory (data accessible). Persists over deep sleep. From 83db8d402b4fc08aff5ba3d4d103ae3253f90a5b Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Fri, 5 Jul 2019 18:05:35 +0800 Subject: [PATCH 034/146] examples: Add the fast wakeup option for deep sleep examples The CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP option is enabled by default for the following examples: - system/deep_sleep, - system/ulp, - system/ulp_adc. --- examples/system/deep_sleep/README.md | 1 + examples/system/deep_sleep/sdkconfig.defaults | 1 + examples/system/ulp/README.md | 2 ++ examples/system/ulp/sdkconfig.defaults | 1 + examples/system/ulp_adc/README.md | 2 ++ examples/system/ulp_adc/sdkconfig.defaults | 1 + 6 files changed, 8 insertions(+) diff --git a/examples/system/deep_sleep/README.md b/examples/system/deep_sleep/README.md index 3d4438de7a..c25c316fa7 100644 --- a/examples/system/deep_sleep/README.md +++ b/examples/system/deep_sleep/README.md @@ -9,3 +9,4 @@ The following wake up sources are configured: - Touch: wake up the chip if any of the touch pads are pressed (GPIO32, GPIO33) - ULP: wake up when the chip temperature changes by more than ~5 degrees Celsius (this value hasn't been characterized exactly yet). +In this example, the `CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` Kconfig option is used, which allows you to reduce the boot time of the bootloader during waking up from deep sleep. The bootloader stores in rtc memory the address of a running partition and uses it when it wakes up. This example allows you to skip all image checks and speed up the boot. \ No newline at end of file diff --git a/examples/system/deep_sleep/sdkconfig.defaults b/examples/system/deep_sleep/sdkconfig.defaults index c80761b50f..8a0c139042 100644 --- a/examples/system/deep_sleep/sdkconfig.defaults +++ b/examples/system/deep_sleep/sdkconfig.defaults @@ -4,3 +4,4 @@ CONFIG_ESP32_ULP_COPROC_ENABLED=y CONFIG_ESP32_ULP_COPROC_RESERVE_MEM=512 CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y CONFIG_ESP32_RTC_CLK_SRC_INT_RC=y +CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP=y \ No newline at end of file diff --git a/examples/system/ulp/README.md b/examples/system/ulp/README.md index 4b2c5341a1..3c854043ed 100644 --- a/examples/system/ulp/README.md +++ b/examples/system/ulp/README.md @@ -12,6 +12,8 @@ Upon wakeup, the main program saves total edge count into NVS and returns to dee In this example the input signal is connected to GPIO0. Note that this pin was chosen because most development boards have a button connected to it, so the pulses to be counted can be generated by pressing the button. For real world applications this is not a good choice of a pin, because GPIO0 also acts as a bootstrapping pin. To change the pin number, check the ESP32 Chip Pin List document and adjust `gpio_num` and `ulp_io_number` variables in main.c. +In this example, the `CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` Kconfig option is used, which allows you to reduce the boot time of the bootloader during waking up from deep sleep. The bootloader stores in rtc memory the address of a running partition and uses it when it wakes up. This example allows you to skip all image checks and speed up the boot. + ## Example output ``` diff --git a/examples/system/ulp/sdkconfig.defaults b/examples/system/ulp/sdkconfig.defaults index a5e2b8ccd7..7214df17c1 100644 --- a/examples/system/ulp/sdkconfig.defaults +++ b/examples/system/ulp/sdkconfig.defaults @@ -6,3 +6,4 @@ CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y CONFIG_BOOTLOADER_LOG_LEVEL=2 CONFIG_LOG_DEFAULT_LEVEL_WARN=y CONFIG_LOG_DEFAULT_LEVEL=2 +CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP=y diff --git a/examples/system/ulp_adc/README.md b/examples/system/ulp_adc/README.md index f07cf0fc77..7948dd4821 100644 --- a/examples/system/ulp_adc/README.md +++ b/examples/system/ulp_adc/README.md @@ -16,6 +16,8 @@ Measurement frequency, Hz | Average current, uA 50 | 20 100 | 37 +In this example, the `CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` Kconfig option is used, which allows you to reduce the boot time of the bootloader during waking up from deep sleep. The bootloader stores in rtc memory the address of a running partition and uses it when it wakes up. This example allows you to skip all image checks and speed up the boot. + ## Example output Below is the output from this example. diff --git a/examples/system/ulp_adc/sdkconfig.defaults b/examples/system/ulp_adc/sdkconfig.defaults index a5e2b8ccd7..7214df17c1 100644 --- a/examples/system/ulp_adc/sdkconfig.defaults +++ b/examples/system/ulp_adc/sdkconfig.defaults @@ -6,3 +6,4 @@ CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y CONFIG_BOOTLOADER_LOG_LEVEL=2 CONFIG_LOG_DEFAULT_LEVEL_WARN=y CONFIG_LOG_DEFAULT_LEVEL=2 +CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP=y From abffc3b11d7d0bd936cb20a35634d93d8b253e5d Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Fri, 5 Jul 2019 18:11:33 +0800 Subject: [PATCH 035/146] docs: Add a description of the fast wakeup --- docs/en/api-guides/bootloader.rst | 4 ++++ docs/en/api-guides/deep-sleep-stub.rst | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/en/api-guides/bootloader.rst b/docs/en/api-guides/bootloader.rst index c699ac2fd1..a849eb6953 100644 --- a/docs/en/api-guides/bootloader.rst +++ b/docs/en/api-guides/bootloader.rst @@ -55,6 +55,10 @@ After the GPIO input is deactivated and the device reboots, the normally configu :ref:`CONFIG_BOOTLOADER_HOLD_TIME_GPIO` - this is hold time of GPIO for reset/test mode (by default 5 seconds). The GPIO must be held low continuously for this period of time after reset before a factory reset or test partition boot (as applicable) is performed. +Fast boot from Deep Sleep +------------------------- +The bootloader has the :ref:`CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` option which allows to reduce the wake-up time (useful to reduce consumption). This option is available when the :ref:`CONFIG_SECURE_BOOT_ENABLED` option is disabled. Reduction of time is achieved due to the lack of image verification. During the first boot, the bootloader stores the address of the application being launched in the RTC FAST memory. And during the awakening, this address is used for booting without any checks, thus fast loading is achieved. + Customer bootloader --------------------- The current bootloader implementation allows the customer to override it. To do this, you must copy the folder `/esp-idf/components/bootloader` and then edit `/your_project/components/bootloader/subproject/main/bootloader_main.c`. diff --git a/docs/en/api-guides/deep-sleep-stub.rst b/docs/en/api-guides/deep-sleep-stub.rst index 6fd547271a..5cd98144eb 100644 --- a/docs/en/api-guides/deep-sleep-stub.rst +++ b/docs/en/api-guides/deep-sleep-stub.rst @@ -88,4 +88,4 @@ For example, the equivalent example in ``rtc_wake_stub_counter.c``:: The second way is a better option if you need to use strings, or write other more complex code. - +To reduce wake-up time use the `CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` Kconfig option, see more information in :doc:`Fast boot from Deep Sleep `. From c543aac91ed54dc93c4cd5a46756648c3ca1169b Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Fri, 5 Jul 2019 18:18:58 +0800 Subject: [PATCH 036/146] bootloader: API for the fast wakeup and custom using RTC mem Added "Reserve RTC FAST memory for custom purposes" option. Added a boot counter. --- components/bootloader/Kconfig.projbuild | 24 ++++++- .../subproject/main/bootloader_start.c | 8 +++ .../include/bootloader_common.h | 62 ++++++++++++++++++ .../include/esp_image_format.h | 28 +++++++++ .../include_bootloader/bootloader_utility.h | 10 +++ .../src/bootloader_common.c | 63 +++++++++++++++++++ .../src/bootloader_utility.c | 2 +- components/esp32/ld/esp32.ld | 4 +- 8 files changed, 198 insertions(+), 3 deletions(-) diff --git a/components/bootloader/Kconfig.projbuild b/components/bootloader/Kconfig.projbuild index ba08b72385..2d4a050919 100644 --- a/components/bootloader/Kconfig.projbuild +++ b/components/bootloader/Kconfig.projbuild @@ -235,7 +235,7 @@ menu "Bootloader config" config BOOTLOADER_RESERVE_RTC_SIZE hex - default 0x10 if BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP + default 0x10 if BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP || BOOTLOADER_CUSTOM_RESERVE_RTC default 0 help Reserve RTC FAST memory for Skip image validation. This option in bytes. @@ -244,6 +244,28 @@ menu "Bootloader config" When a wakeup occurs (from Deep sleep), the bootloader retrieves it and loads the application without validation. + config BOOTLOADER_CUSTOM_RESERVE_RTC + bool "Reserve RTC FAST memory for custom purposes" + default n + help + This option allows the customer to place data in the RTC FAST memory, + this area remains valid when rebooted, except for power loss. + This memory is located at a fixed address and is available + for both the bootloader and the application. + (The application and bootoloader must be compiled with the same option). + The RTC FAST memory has access only through PRO_CPU. + + config BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE + hex "Size in bytes for custom purposes" + range 0 0x10 + default 0 + depends on BOOTLOADER_CUSTOM_RESERVE_RTC + help + This option reserves in RTC FAST memory the area for custom purposes. + If you want to create your own bootloader and save more information + in this area of memory, you can increase it. It must be a multiple of 4 bytes. + This area (rtc_retain_mem_t) is reserved and has access from the bootloader and an application. + endmenu # Bootloader diff --git a/components/bootloader/subproject/main/bootloader_start.c b/components/bootloader/subproject/main/bootloader_start.c index e9bd32d881..f93fa5694b 100644 --- a/components/bootloader/subproject/main/bootloader_start.c +++ b/components/bootloader/subproject/main/bootloader_start.c @@ -41,6 +41,14 @@ void __attribute__((noreturn)) call_start_cpu0(void) bootloader_reset(); } +#ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP + // If this boot is a wake up from the deep sleep then go to the short way, + // try to load the application which worked before deep sleep. + // It skips a lot of checks due to it was done before (while first boot). + bootloader_utility_load_boot_image_from_deep_sleep(); + // If it is not successful try to load an application as usual. +#endif + // 2. Select the number of boot partition bootloader_state_t bs = { 0 }; int boot_index = select_partition_number(&bs); diff --git a/components/bootloader_support/include/bootloader_common.h b/components/bootloader_support/include/bootloader_common.h index 4b887f55f9..1058b20cc5 100644 --- a/components/bootloader_support/include/bootloader_common.h +++ b/components/bootloader_support/include/bootloader_common.h @@ -146,6 +146,68 @@ esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t */ void bootloader_common_vddsdio_configure(void); +#if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC ) +/** + * @brief Returns partition from rtc_retain_mem + * + * Uses to get the partition of application which was worked before to go to the deep sleep. + * This partition was stored in rtc_retain_mem. + * Note: This function operates the RTC FAST memory which available only for PRO_CPU. + * Make sure that this function is used only PRO_CPU. + * + * @return partition: If rtc_retain_mem is valid. + * - NULL: If it is not valid. + */ +esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void); + +/** + * @brief Update the partition and reboot_counter in rtc_retain_mem. + * + * This function saves the partition of application for fast booting from the deep sleep. + * An algorithm uses this partition to avoid reading the otadata and does not validate an image. + * Note: This function operates the RTC FAST memory which available only for PRO_CPU. + * Make sure that this function is used only PRO_CPU. + * + * @param[in] partition App partition description. Can be NULL, in this case rtc_retain_mem.partition is not updated. + * @param[in] reboot_counter If true then update reboot_counter. + * + */ +void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter); + +/** + * @brief Reset entire rtc_retain_mem. + * + * Note: This function operates the RTC FAST memory which available only for PRO_CPU. + * Make sure that this function is used only PRO_CPU. + */ +void bootloader_common_reset_rtc_retain_mem(void); + +/** + * @brief Returns reboot_counter from rtc_retain_mem + * + * The reboot_counter counts the number of reboots. Reset only when power is off. + * The very first launch of the application will be from 1. + * Overflow is not possible, it will stop at the value UINT16_MAX. + * Note: This function operates the RTC FAST memory which available only for PRO_CPU. + * Make sure that this function is used only PRO_CPU. + * + * @return reboot_counter: 1..65535 + * - 0: If rtc_retain_mem is not valid. + */ +uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void); + +/** + * @brief Returns rtc_retain_mem + * + * Note: This function operates the RTC FAST memory which available only for PRO_CPU. + * Make sure that this function is used only PRO_CPU. + * + * @return rtc_retain_mem + */ +rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void); + +#endif + #ifdef __cplusplus } #endif diff --git a/components/bootloader_support/include/esp_image_format.h b/components/bootloader_support/include/esp_image_format.h index 09d4bda3da..4e656a17b6 100644 --- a/components/bootloader_support/include/esp_image_format.h +++ b/components/bootloader_support/include/esp_image_format.h @@ -51,6 +51,34 @@ typedef enum { #endif } esp_image_load_mode_t; +typedef struct { + esp_partition_pos_t partition; /*!< Partition of application which worked before goes to the deep sleep. */ + uint16_t reboot_counter; /*!< Reboot counter. Reset only when power is off. */ + uint16_t reserve; /*!< Reserve */ +#ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC + uint8_t custom[CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE]; /*!< Reserve for custom propose */ +#endif + uint32_t crc; /*!< Check sum crc32 */ +} rtc_retain_mem_t; + +#ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC +_Static_assert(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE must be a multiple of 4 bytes"); +#endif + +#if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC) +_Static_assert(CONFIG_BOOTLOADER_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_RESERVE_RTC_SIZE must be a multiple of 4 bytes"); +#endif + +#ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC +#define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE) +#elif defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) +#define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE) +#endif + +#if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC) +_Static_assert(sizeof(rtc_retain_mem_t) <= ESP_BOOTLOADER_RESERVE_RTC, "Reserved RTC area must exceed size of rtc_retain_mem_t"); +#endif + /** * @brief Verify and (optionally, in bootloader mode) load an app image. * diff --git a/components/bootloader_support/include_bootloader/bootloader_utility.h b/components/bootloader_support/include_bootloader/bootloader_utility.h index 84d0cb48f3..4f31f5acbc 100644 --- a/components/bootloader_support/include_bootloader/bootloader_utility.h +++ b/components/bootloader_support/include_bootloader/bootloader_utility.h @@ -54,6 +54,16 @@ int bootloader_utility_get_selected_boot_partition(const bootloader_state_t *bs) */ __attribute__((noreturn)) void bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_index); +#ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP +/** + * @brief Load that application which was worked before we go to the deep sleep. + * + * Checks the reboot reason if it is the deep sleep and has a valid partition in the RTC memory + * then try to load the application which was worked before we go to the deep sleep. + * + */ +void bootloader_utility_load_boot_image_from_deep_sleep(void); +#endif /** * @brief Software reset the ESP32 diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index a10fb2f2ac..acc7644ea3 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -270,3 +270,66 @@ void bootloader_common_vddsdio_configure(void) } #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST } + + +#if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC ) + +rtc_retain_mem_t *const rtc_retain_mem = (rtc_retain_mem_t *)(SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t)); + +static bool check_rtc_retain_mem(void) +{ + return crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, sizeof(rtc_retain_mem_t) - sizeof(rtc_retain_mem->crc)) == rtc_retain_mem->crc && rtc_retain_mem->crc != UINT32_MAX; +} + +static void update_rtc_retain_mem_crc(void) +{ + rtc_retain_mem->crc = crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, sizeof(rtc_retain_mem_t) - sizeof(rtc_retain_mem->crc)); +} + +void bootloader_common_reset_rtc_retain_mem(void) +{ + memset(rtc_retain_mem, 0, sizeof(rtc_retain_mem_t)); +} + +uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void) +{ + if (check_rtc_retain_mem()) { + return rtc_retain_mem->reboot_counter; + } + return 0; +} + +esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void) +{ + if (check_rtc_retain_mem()) { + return &rtc_retain_mem->partition; + } + return NULL; +} + +void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter) +{ + if (reboot_counter) { + if (!check_rtc_retain_mem()) { + bootloader_common_reset_rtc_retain_mem(); + } + if (++rtc_retain_mem->reboot_counter == 0) { + // do not allow to overflow. Stop it. + --rtc_retain_mem->reboot_counter; + } + + } + + if (partition != NULL) { + rtc_retain_mem->partition.offset = partition->offset; + rtc_retain_mem->partition.size = partition->size; + } + + update_rtc_retain_mem_crc(); +} + +rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void) +{ + return rtc_retain_mem; +} +#endif \ No newline at end of file diff --git a/components/bootloader_support/src/bootloader_utility.c b/components/bootloader_support/src/bootloader_utility.c index 7d5f67ac7e..b9d76ed9bc 100644 --- a/components/bootloader_support/src/bootloader_utility.c +++ b/components/bootloader_support/src/bootloader_utility.c @@ -417,7 +417,7 @@ static void set_actual_ota_seq(const bootloader_state_t *bs, int index) update_anti_rollback(&bs->ota[index]); #endif } -#if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) +#if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC ) esp_partition_pos_t partition = index_to_partition(bs, index); bootloader_common_update_rtc_retain_mem(&partition, true); #endif diff --git a/components/esp32/ld/esp32.ld b/components/esp32/ld/esp32.ld index 514b9ebd82..a52b56f90d 100644 --- a/components/esp32/ld/esp32.ld +++ b/components/esp32/ld/esp32.ld @@ -21,7 +21,9 @@ #define CONFIG_BT_RESERVE_DRAM 0 #endif -#if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) +#ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC +#define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE) +#elif defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE) #else #define ESP_BOOTLOADER_RESERVE_RTC 0 From 5ba9be9c2e06989028162294b9d75be645da8a7d Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Aug 2019 14:45:33 +0200 Subject: [PATCH 037/146] docs: ulp: update for IDF Tools installation method binutils-esp32ulp is installed automatically by install.sh/install.bat or the IDF Tools Installer for Windows. --- docs/en/api-guides/ulp.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/en/api-guides/ulp.rst b/docs/en/api-guides/ulp.rst index df3b813c75..505af97b79 100644 --- a/docs/en/api-guides/ulp.rst +++ b/docs/en/api-guides/ulp.rst @@ -17,10 +17,9 @@ Installing the toolchain ULP coprocessor code is written in assembly and compiled using the `binutils-esp32ulp toolchain`_. -1. Download pre-built binaries of the latest toolchain release from: -https://github.com/espressif/binutils-esp32ulp/releases. +If you have already set up ESP-IDF with CMake build system according to the :doc:`Getting Started Guide <../../get-started/index>`, then the ULP toolchain is already installed. -2. Extract the toolchain into a directory, and add the path to the ``bin/`` directory of the toolchain to the ``PATH`` environment variable. +If you are using ESP-IDF with the legacy GNU Make based build system, refer to the instructions on this page: :doc:`ulp-legacy`. Compiling ULP code ------------------ From 43b29e4f4de1662458144e9b54e6d6f907fd804f Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Wed, 21 Aug 2019 21:52:48 +0800 Subject: [PATCH 038/146] doc: update translation of ulp install instuctions --- docs/zh_CN/api-guides/ulp.rst | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/zh_CN/api-guides/ulp.rst b/docs/zh_CN/api-guides/ulp.rst index a6b8f9246d..13ed09045c 100644 --- a/docs/zh_CN/api-guides/ulp.rst +++ b/docs/zh_CN/api-guides/ulp.rst @@ -1,5 +1,5 @@ ULP 协处理器编程 -=================================== +================ :link_to_translation:`en:[English]` @@ -13,16 +13,17 @@ ULP 协处理器编程 ULP(Ultra Low Power 超低功耗)协处理器是一种简单的有限状态机 (FSM),可以在主处理器处于深度睡眠模式时,使用 ADC、温度传感器和外部 I2C 传感器执行测量操作。ULP 协处理器可以访问 RTC_SLOW_MEM 内存区域及 RTC_CNTL、RTC_IO、SARADC 等外设寄存器。ULP 协处理器使用 32 位固定宽度的指令,32 位内存寻址,配备 4 个 16 位通用寄存器。 安装工具链 ------------------------- +---------- ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp 工具链`_ 进行编译。 -1. 从提供的网址中下载最新工具链的预编译二进制文件:https://github.com/espressif/binutils-esp32ulp/releases. +如果你已经按照 :doc:`快速入门指南 <../../get-started/index>` 中的介绍安装好了 ESP-IDF 及其 CMake 构建系统,那么 ULP 工具链已经被默认安装到了你的开发环境中。 + +如果你的 ESP-IDF 仍在使用旧版本的基于 GNU Make 的构建系统,请参考 :doc:`ulp-legacy` 一文中的说明,完成工具链的安装。 -2. 将工具链解压缩到一个目录中,并将工具链的 ``bin/`` 目录路径添加到 ``PATH`` 环境变量中。 编译 ULP 代码 ------------------- +------------- 若需要将 ULP 代码编译为某组件的一部分,则必须执行以下步骤: @@ -73,7 +74,7 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp 8. **将生成的二进制文件添加到要嵌入应用程序的二进制文件列表中。** 访问 ULP 程序变量 -------------------------------- +----------------- 在 ULP 程序中定义的全局符号也可以在主程序中使用。 @@ -114,7 +115,7 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp printf("Last measurement value: %d\n", ulp_last_measurement & UINT16_MAX); 启动 ULP 程序 ------------------------- +------------- 要运行 ULP 程序,主应用程序需要调用 ``ulp_load_binary`` 函数将 ULP 程序加载到 RTC 内存中,然后调用 ``ulp_run`` 函数,启动 ULP 程序。 @@ -149,7 +150,7 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp ULP 程序流 ----------------- +---------- ULP 协处理器由定时器启动,而调用 ``ulp_run`` 则可启动此定时器。定时器为 RTC_SLOW_CLK 的 Tick 事件计数(默认情况下,Tick 由内部 150 KHz 晶振器生成)。使用 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器 (x = 0..4) 设置 Tick 数值。第一次启动 ULP 时,使用 ``SENS_ULP_CP_SLEEP_CYC0_REG`` 设置定时器 Tick 数值,之后,ULP 程序可以使用 ``sleep`` 指令来另外选择 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器。 From 72ddc940e01a290c32c7618fdab9d4fff667431e Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Thu, 22 Aug 2019 09:38:33 +0800 Subject: [PATCH 039/146] cmake: hide internal targets Use imported library, which does not create additional rules, but still allows attaching arbitraty properties instead of custom targets. This allows the targets to not appear in the target list of IDEs such as CLion. --- tools/cmake/build.cmake | 2 +- tools/cmake/component.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/cmake/build.cmake b/tools/cmake/build.cmake index 5383581f12..f8dc4a21a3 100644 --- a/tools/cmake/build.cmake +++ b/tools/cmake/build.cmake @@ -129,7 +129,7 @@ endfunction() # function(__build_init idf_path) # Create the build target, to which the ESP-IDF build properties, dependencies are attached to - add_custom_target(__idf_build_target) + add_library(__idf_build_target STATIC IMPORTED) set_default(python "python") diff --git a/tools/cmake/component.cmake b/tools/cmake/component.cmake index 1ec7efcfca..d807e38587 100644 --- a/tools/cmake/component.cmake +++ b/tools/cmake/component.cmake @@ -173,7 +173,7 @@ function(__component_add component_dir prefix) # 'override' components added earlier. if(NOT component_target IN_LIST component_targets) if(NOT TARGET ${component_target}) - add_custom_target(${component_target} EXCLUDE_FROM_ALL) + add_library(${component_target} STATIC IMPORTED) endif() idf_build_set_property(__COMPONENT_TARGETS ${component_target} APPEND) endif() From 1248f17749bee6bc37bc636c607e945cca12ee45 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 22 Aug 2019 13:17:10 +0530 Subject: [PATCH 040/146] cmake: fix issue with handling of EXCLUDE_SRCS --- tools/cmake/component.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/cmake/component.cmake b/tools/cmake/component.cmake index 1ec7efcfca..a7f099e4bf 100644 --- a/tools/cmake/component.cmake +++ b/tools/cmake/component.cmake @@ -271,7 +271,7 @@ macro(__component_add_sources sources) if(__EXCLUDE_SRCS) foreach(src ${__EXCLUDE_SRCS}) get_filename_component(src "${src}" ABSOLUTE) - list(REMOVE_ITEM source "${src}") + list(REMOVE_ITEM sources "${src}") endforeach() endif() endif() @@ -551,4 +551,4 @@ endfunction() # Wrapper around target_compile_definitions that passes the component name function(component_compile_definitions) target_compile_definitions(${COMPONENT_LIB} PRIVATE ${ARGV}) -endfunction() \ No newline at end of file +endfunction() From 5a09de8245ba3c41cdeb993437de5a1b5cfad1fd Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Mon, 12 Aug 2019 22:07:47 +0800 Subject: [PATCH 041/146] cbor: add tinycbor library and example --- .gitmodules | 4 + components/cbor/CMakeLists.txt | 21 ++ components/cbor/component.mk | 8 + components/cbor/port/include/cbor.h | 2 + components/cbor/tinycbor | 1 + examples/protocols/cbor/CMakeLists.txt | 6 + examples/protocols/cbor/Makefile | 9 + examples/protocols/cbor/README.md | 56 +++++ examples/protocols/cbor/main/CMakeLists.txt | 2 + .../protocols/cbor/main/cbor_example_main.c | 226 ++++++++++++++++++ examples/protocols/cbor/main/component.mk | 5 + tools/ci/build_examples.sh | 5 +- tools/ci/build_examples_cmake.sh | 3 +- 13 files changed, 345 insertions(+), 3 deletions(-) create mode 100644 components/cbor/CMakeLists.txt create mode 100644 components/cbor/component.mk create mode 100644 components/cbor/port/include/cbor.h create mode 160000 components/cbor/tinycbor create mode 100644 examples/protocols/cbor/CMakeLists.txt create mode 100644 examples/protocols/cbor/Makefile create mode 100644 examples/protocols/cbor/README.md create mode 100644 examples/protocols/cbor/main/CMakeLists.txt create mode 100644 examples/protocols/cbor/main/cbor_example_main.c create mode 100644 examples/protocols/cbor/main/component.mk diff --git a/.gitmodules b/.gitmodules index 70bd0c478e..8366568747 100644 --- a/.gitmodules +++ b/.gitmodules @@ -74,3 +74,7 @@ [submodule "components/bt/host/nimble/nimble"] path = components/bt/host/nimble/nimble url = ../../espressif/esp-nimble.git + +[submodule "components/cbor/tinycbor"] + path = components/cbor/tinycbor + url = ../../intel/tinycbor.git diff --git a/components/cbor/CMakeLists.txt b/components/cbor/CMakeLists.txt new file mode 100644 index 0000000000..5aaaaf80fb --- /dev/null +++ b/components/cbor/CMakeLists.txt @@ -0,0 +1,21 @@ +idf_component_register(SRCS "tinycbor/src/cborencoder_close_container_checked.c" + "tinycbor/src/cborencoder.c" + "tinycbor/src/cborerrorstrings.c" + "tinycbor/src/cborparser_dup_string.c" + "tinycbor/src/cborparser.c" + "tinycbor/src/cborpretty_stdio.c" + "tinycbor/src/cborpretty.c" + "tinycbor/src/cbortojson.c" + "tinycbor/src/cborvalidation.c" + "tinycbor/src/open_memstream.c" + INCLUDE_DIRS "port/include" + PRIV_INCLUDE_DIRS "tinycbor/src") + +# for open_memstream.c +target_compile_definitions(${COMPONENT_LIB} PRIVATE "__GLIBC__") + +# cbortojson.c:378:17: assignment discards 'const' qualifier from pointer target type +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-discarded-qualifiers") + +# cborvalidation.c:429:22: 'valf' may be used uninitialized +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-maybe-uninitialized") diff --git a/components/cbor/component.mk b/components/cbor/component.mk new file mode 100644 index 0000000000..9f9d37c361 --- /dev/null +++ b/components/cbor/component.mk @@ -0,0 +1,8 @@ +COMPONENT_SUBMODULES += tinycbor +COMPONENT_ADD_INCLUDEDIRS := port/include +COMPONENT_SRCDIRS := tinycbor/src +COMPONENT_PRIV_INCLUDEDIRS := tinycbor/src + +tinycbor/src/open_memstream.o: CFLAGS += -D__GLIBC__ +tinycbor/src/cbortojson.o: CFLAGS += -Wno-discarded-qualifiers +tinycbor/src/cborvalidation.o: CFLAGS += -Wno-maybe-uninitialized diff --git a/components/cbor/port/include/cbor.h b/components/cbor/port/include/cbor.h new file mode 100644 index 0000000000..c8e6ccf483 --- /dev/null +++ b/components/cbor/port/include/cbor.h @@ -0,0 +1,2 @@ +#include "../../tinycbor/src/cbor.h" +#include "../../tinycbor/src/cborjson.h" diff --git a/components/cbor/tinycbor b/components/cbor/tinycbor new file mode 160000 index 0000000000..d2dd95cb88 --- /dev/null +++ b/components/cbor/tinycbor @@ -0,0 +1 @@ +Subproject commit d2dd95cb8841d88d5a801e3ef9c328fd6200e7bd diff --git a/examples/protocols/cbor/CMakeLists.txt b/examples/protocols/cbor/CMakeLists.txt new file mode 100644 index 0000000000..8b524ec386 --- /dev/null +++ b/examples/protocols/cbor/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(cbor) diff --git a/examples/protocols/cbor/Makefile b/examples/protocols/cbor/Makefile new file mode 100644 index 0000000000..7b394132d2 --- /dev/null +++ b/examples/protocols/cbor/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := cbor + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/protocols/cbor/README.md b/examples/protocols/cbor/README.md new file mode 100644 index 0000000000..3b6f81a1a2 --- /dev/null +++ b/examples/protocols/cbor/README.md @@ -0,0 +1,56 @@ +# CBOR Example +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +## Overview + +The [CBOR](https://en.wikipedia.org/wiki/CBOR)(Concise Binary Object Representation) is a binary data serialization format which is similar to JSON but with smaller footprint. This example will illustrate how to encode and decode CBOR data using the APIs provided by [tinycbor](https://github.com/intel/tinycbor). + +For detailed information about how CBOR encoding and decoding works, please refer to [REF7049](https://tools.ietf.org/html/rfc7049) or [cbor.io](http://cbor.io/); + +## How to use example + +### Hardware Required + +This example should be able to run on any commonly available ESP32 development board. + +### Build and Flash + +Run `idf.py -p PORT flash monitor` to build and flash the project. + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +```bash +I (320) example: encoded buffer size 67 +I (320) example: convert CBOR to JSON +[{"chip":"esp32","unicore":false,"ip":[192,168,1,100]},3.1400001049041748,"simple(99)","2019-07-10 09:00:00+0000","undefined"] +I (340) example: decode CBOR manually +Array[ + Map{ + chip + esp32 + unicore + false + ip + Array[ + 192 + 168 + 1 + 100 + ] + } + 3.14 + simple(99) + 2019-07-10 09:00:00+0000 + undefined +] +``` + +## Troubleshooting + +For more API usage, please refer to [tinycbor API](https://intel.github.io/tinycbor/current/). + +(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.) diff --git a/examples/protocols/cbor/main/CMakeLists.txt b/examples/protocols/cbor/main/CMakeLists.txt new file mode 100644 index 0000000000..31dfa610cb --- /dev/null +++ b/examples/protocols/cbor/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "cbor_example_main.c" + INCLUDE_DIRS "") diff --git a/examples/protocols/cbor/main/cbor_example_main.c b/examples/protocols/cbor/main/cbor_example_main.c new file mode 100644 index 0000000000..3807ce0e3d --- /dev/null +++ b/examples/protocols/cbor/main/cbor_example_main.c @@ -0,0 +1,226 @@ +/* CBOR Example + + 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 +#include "esp_log.h" +#include "cbor.h" + +static const char *TAG = "example"; + +#define CBOR_CHECK(a, str, goto_tag, ret_value, ...) \ + do \ + { \ + if ((a) != CborNoError) \ + { \ + ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ + ret = ret_value; \ + goto goto_tag; \ + } \ + } while (0) + +static void indent(int nestingLevel) +{ + while (nestingLevel--) { + printf(" "); + } +} + +static void dumpbytes(const uint8_t *buf, size_t len) +{ + while (len--) { + printf("%02X ", *buf++); + } +} + +/** + * Decode CBOR data manuallly + */ +static CborError example_dump_cbor_buffer(CborValue *it, int nestingLevel) +{ + CborError ret = CborNoError; + while (!cbor_value_at_end(it)) { + CborType type = cbor_value_get_type(it); + + indent(nestingLevel); + switch (type) { + case CborArrayType: { + CborValue recursed; + assert(cbor_value_is_container(it)); + puts("Array["); + ret = cbor_value_enter_container(it, &recursed); + CBOR_CHECK(ret, "enter container failed", err, ret); + ret = example_dump_cbor_buffer(&recursed, nestingLevel + 1); + CBOR_CHECK(ret, "recursive dump failed", err, ret); + ret = cbor_value_leave_container(it, &recursed); + CBOR_CHECK(ret, "leave container failed", err, ret); + indent(nestingLevel); + puts("]"); + continue; + } + case CborMapType: { + CborValue recursed; + assert(cbor_value_is_container(it)); + puts("Map{"); + ret = cbor_value_enter_container(it, &recursed); + CBOR_CHECK(ret, "enter container failed", err, ret); + ret = example_dump_cbor_buffer(&recursed, nestingLevel + 1); + CBOR_CHECK(ret, "recursive dump failed", err, ret); + ret = cbor_value_leave_container(it, &recursed); + CBOR_CHECK(ret, "leave container failed", err, ret); + indent(nestingLevel); + puts("}"); + continue; + } + case CborIntegerType: { + int64_t val; + ret = cbor_value_get_int64(it, &val); + CBOR_CHECK(ret, "parse int64 failed", err, ret); + printf("%lld\n", (long long)val); + break; + } + case CborByteStringType: { + uint8_t *buf; + size_t n; + ret = cbor_value_dup_byte_string(it, &buf, &n, it); + CBOR_CHECK(ret, "parse byte string failed", err, ret); + dumpbytes(buf, n); + puts(""); + free(buf); + continue; + } + case CborTextStringType: { + char *buf; + size_t n; + ret = cbor_value_dup_text_string(it, &buf, &n, it); + CBOR_CHECK(ret, "parse text string failed", err, ret); + puts(buf); + free(buf); + continue; + } + case CborTagType: { + CborTag tag; + ret = cbor_value_get_tag(it, &tag); + CBOR_CHECK(ret, "parse tag failed", err, ret); + printf("Tag(%lld)\n", (long long)tag); + break; + } + case CborSimpleType: { + uint8_t type; + ret = cbor_value_get_simple_type(it, &type); + CBOR_CHECK(ret, "parse simple type failed", err, ret); + printf("simple(%u)\n", type); + break; + } + case CborNullType: + puts("null"); + break; + case CborUndefinedType: + puts("undefined"); + break; + case CborBooleanType: { + bool val; + ret = cbor_value_get_boolean(it, &val); + CBOR_CHECK(ret, "parse boolean type failed", err, ret); + puts(val ? "true" : "false"); + break; + } + case CborHalfFloatType: { + uint16_t val; + ret = cbor_value_get_half_float(it, &val); + CBOR_CHECK(ret, "parse half float type failed", err, ret); + printf("__f16(%04x)\n", val); + break; + } + case CborFloatType: { + float val; + ret = cbor_value_get_float(it, &val); + CBOR_CHECK(ret, "parse float type failed", err, ret); + printf("%g\n", val); + break; + } + case CborDoubleType: { + double val; + ret = cbor_value_get_double(it, &val); + CBOR_CHECK(ret, "parse double float type failed", err, ret); + printf("%g\n", val); + break; + } + case CborInvalidType: { + ret = CborErrorUnknownType; + CBOR_CHECK(ret, "unknown cbor type", err, ret); + break; + } + } + + ret = cbor_value_advance_fixed(it); + CBOR_CHECK(ret, "fix value failed", err, ret); + } + return CborNoError; +err: + return ret; +} + + +void app_main(void) +{ + CborEncoder root_encoder; + CborParser root_parser; + CborValue it; + uint8_t buf[100]; + + // Initialize the outermost cbor encoder + cbor_encoder_init(&root_encoder, buf, sizeof(buf), 0); + + // Create an array containing several items + CborEncoder array_encoder; + CborEncoder map_encoder; + cbor_encoder_create_array(&root_encoder, &array_encoder, 5); // [ + // 1. Create a map containing several pairs + cbor_encoder_create_map(&array_encoder, &map_encoder, 3); // { + // chip:esp32 + cbor_encode_text_stringz(&map_encoder, "chip"); + cbor_encode_text_stringz(&map_encoder, "esp32"); + // unicore:false + cbor_encode_text_stringz(&map_encoder, "unicore"); + cbor_encode_boolean(&map_encoder, false); + // ip:[192,168,1,100] + cbor_encode_text_stringz(&map_encoder, "ip"); + CborEncoder array2; + cbor_encoder_create_array(&map_encoder, &array2, 4); // [ + // Encode several numbers + cbor_encode_uint(&array2, 192); + cbor_encode_uint(&array2, 168); + cbor_encode_uint(&array2, 1); + cbor_encode_uint(&array2, 100); + cbor_encoder_close_container(&map_encoder, &array2); // ] + cbor_encoder_close_container(&array_encoder, &map_encoder); // } + // 2. Encode float number + cbor_encode_float(&array_encoder, 3.14); + // 3. Encode simple value + cbor_encode_simple_value(&array_encoder, 99); + // 4. Encode a string + cbor_encode_text_stringz(&array_encoder, "2019-07-10 09:00:00+0000"); + // 5. Encode a undefined value + cbor_encode_undefined(&array_encoder); + cbor_encoder_close_container(&root_encoder, &array_encoder); // ] + + // If error happend when encoding, then this value should be meaningless + ESP_LOGI(TAG, "encoded buffer size %d", cbor_encoder_get_buffer_size(&root_encoder, buf)); + + // Initialize the cbor parser and the value iterator + cbor_parser_init(buf, sizeof(buf), 0, &root_parser, &it); + + ESP_LOGI(TAG, "convert CBOR to JSON"); + // Dump the values in JSON format + cbor_value_to_json(stdout, &it, 0); + puts(""); + + ESP_LOGI(TAG, "decode CBOR manually"); + // Decode CBOR data manully + example_dump_cbor_buffer(&it, 0); +} diff --git a/examples/protocols/cbor/main/component.mk b/examples/protocols/cbor/main/component.mk new file mode 100644 index 0000000000..0b9d7585e7 --- /dev/null +++ b/examples/protocols/cbor/main/component.mk @@ -0,0 +1,5 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) + diff --git a/tools/ci/build_examples.sh b/tools/ci/build_examples.sh index 9be1525b3f..9dc831d43c 100755 --- a/tools/ci/build_examples.sh +++ b/tools/ci/build_examples.sh @@ -99,7 +99,7 @@ build_example () { local EXAMPLE_DIR=$(dirname "${MAKE_FILE}") local EXAMPLE_NAME=$(basename "${EXAMPLE_DIR}") - + # Check if the example needs a different base directory. # Path of the Makefile relative to $IDF_PATH local MAKE_FILE_REL=${MAKE_FILE#"${IDF_PATH}/"} @@ -182,7 +182,8 @@ echo -e "\nFound issues:" IGNORE_WARNS="\ library/error\.o\ \|\ -Werror\ -\|error\.d\ +\|.*error.*\.o\ +\|.*error.*\.d\ \|reassigning to symbol\ \|changes choice state\ \|Compiler version is not supported\ diff --git a/tools/ci/build_examples_cmake.sh b/tools/ci/build_examples_cmake.sh index cc5f3adc00..9a8751ae26 100755 --- a/tools/ci/build_examples_cmake.sh +++ b/tools/ci/build_examples_cmake.sh @@ -145,7 +145,7 @@ build_example () { cat ${BUILDLOG} popd - grep -i "error\|warning" "${BUILDLOG}" 2>&1 | grep -v "error.c.obj" >> "${LOG_SUSPECTED}" || : + grep -i "error\|warning" "${BUILDLOG}" 2>&1 >> "${LOG_SUSPECTED}" || : } EXAMPLE_NUM=0 @@ -175,6 +175,7 @@ echo -e "\nFound issues:" # 'Compiler and toochain versions is not supported' from crosstool_version_check.cmake IGNORE_WARNS="\ library/error\.o\ +\|.*error.*\.c\.obj\ \|\ -Werror\ \|error\.d\ \|reassigning to symbol\ From 8b8b2f12e479d07ae95c86b55fb37839cfd56bc3 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Wed, 21 Aug 2019 16:45:01 +0200 Subject: [PATCH 042/146] Fix sdkconfig.rename paths for confgen.py in MSYS Closes https://github.com/espressif/esp-idf/issues/3950 --- make/project_config.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/make/project_config.mk b/make/project_config.mk index 7eede5ebbc..1a4e741266 100644 --- a/make/project_config.mk +++ b/make/project_config.mk @@ -9,6 +9,7 @@ ifeq ($(OS),Windows_NT) # kconfiglib requires Windows-style paths for kconfig files COMPONENT_KCONFIGS := $(shell cygpath -w $(COMPONENT_KCONFIGS)) COMPONENT_KCONFIGS_PROJBUILD := $(shell cygpath -w $(COMPONENT_KCONFIGS_PROJBUILD)) +COMPONENT_SDKCONFIG_RENAMES := $(shell cygpath -w $(COMPONENT_SDKCONFIG_RENAMES)) endif #For doing make menuconfig etc From 8bf1ef2bfe06d68f28749adb11c4e36d1a143486 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Thu, 22 Aug 2019 16:10:17 +0200 Subject: [PATCH 043/146] Tools: Add OpenOCD for the ARM architecture --- tools/tools.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/tools.json b/tools/tools.json index d3a61b30ed..8c2fdfa94b 100644 --- a/tools/tools.json +++ b/tools/tools.json @@ -195,7 +195,6 @@ { "install": "on_request", "platforms": [ - "linux-armel", "linux-i686" ] } @@ -212,6 +211,11 @@ "size": 1675213, "url": "https://github.com/espressif/openocd-esp32/releases/download/v0.10.0-esp32-20190708/openocd-esp32-linux64-0.10.0-esp32-20190708.tar.gz" }, + "linux-armel": { + "sha256": "f3f01d2b0ec440127b8951b82ee79dd04b9d2774bab4de741067c5f65cffaa7e", + "size": 1729130, + "url": "https://github.com/espressif/openocd-esp32/releases/download/v0.10.0-esp32-20190708/openocd-esp32-armel-0.10.0-esp32-20190708.tar.gz" + }, "macos": { "sha256": "840c94ce6208c5b21d0ba6c3113a06405daf6b27085a287cfbb6c9d21e80bd70", "size": 1760060, From e28b6e59b6d97fcf703337820b6c06c807fdc81a Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 23 Aug 2019 09:20:08 +1000 Subject: [PATCH 044/146] docs: Add TinyCBOR copyright line --- docs/en/COPYRIGHT.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/en/COPYRIGHT.rst b/docs/en/COPYRIGHT.rst index 2891e13f58..3ff5e1c7d6 100644 --- a/docs/en/COPYRIGHT.rst +++ b/docs/en/COPYRIGHT.rst @@ -51,6 +51,8 @@ These third party libraries can be included into the application (firmware) prod * `SPIFFS`_ library, Copyright (c) 2013-2017 Peter Andersson, is licensed under MIT license. +* `TinyCBOR`_ library, Copyright (c) 2017 Intel Corporation, is licensed under MIT License. + * :component_file:`SD/MMC driver ` is derived from `OpenBSD SD/MMC driver`_, Copyright (c) 2006 Uwe Stuehler, and is licensed under BSD license. * :component:`Asio `, Copyright (c) 2003-2018 Christopher M. Kohlhoff is licensed under the Boost Software License. @@ -164,3 +166,4 @@ Copyright (C) 2011, ChaN, all right reserved. .. _mqtt: https://github.com/espressif/esp-mqtt .. _zephyr: https://github.com/zephyrproject-rtos/zephyr .. _mynewt-nimble: https://github.com/apache/mynewt-nimble +.. _TinyCBOR: https://github.com/intel/tinycbor From 76b6902e96154485cd80e03277722f128675964f Mon Sep 17 00:00:00 2001 From: wangmengyang Date: Fri, 23 Aug 2019 08:26:15 +0800 Subject: [PATCH 045/146] components/bt: set the minimum encryption key size to be 7 octects for BR/EDR link for preventing KNOB attack This patch is to address the CVE-2019-9506 vulnerability. --- components/bt/controller/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib b/components/bt/controller/lib index 717f0c6ec7..9ee3b17ba9 160000 --- a/components/bt/controller/lib +++ b/components/bt/controller/lib @@ -1 +1 @@ -Subproject commit 717f0c6ec71a016a0b292acffeacf239d007b8ff +Subproject commit 9ee3b17ba9879ecee09070c5a543a57d8aff2036 From 12c9d9a5643ea3abdb55a01652f6efcb4b4ffbfd Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 23 Aug 2019 12:37:55 +0800 Subject: [PATCH 046/146] spi_flash: remove duplicate definition of spi_flash_unlock The other (static) definition is in flash_ops.c, all references are also in flash_ops.c. --- components/bootloader/Kconfig.projbuild | 14 +++ .../src/esp32/flash_encrypt.c | 8 +- components/esp32/cpu_start.c | 11 +- components/spi_flash/esp_flash_api.c | 6 - components/spi_flash/flash_ops.c | 4 +- .../spi_flash/test/test_flash_encryption.c | 9 +- components/spi_flash/test/test_mmap.c | 51 +++++--- examples/security/flash_encryption/README.md | 119 +++++++----------- .../security/flash_encryption/example_test.py | 41 ++++++ .../main/flash_encrypt_main.c | 96 +++++++++----- .../flash_encryption/partitions_example.csv | 5 + .../security/flash_encryption/sdkconfig.ci | 12 ++ .../flash_encryption/sdkconfig.defaults | 4 + tools/ci/config/target-test.yml | 12 ++ tools/tiny-test-fw/IDF/IDFApp.py | 5 + tools/tiny-test-fw/IDF/IDFDUT.py | 2 +- tools/unit-test-app/configs/flash_encryption | 10 ++ .../unit-test-app/tools/ModuleDefinition.yml | 5 + 18 files changed, 274 insertions(+), 140 deletions(-) create mode 100644 examples/security/flash_encryption/example_test.py create mode 100644 examples/security/flash_encryption/partitions_example.csv create mode 100644 examples/security/flash_encryption/sdkconfig.ci create mode 100644 examples/security/flash_encryption/sdkconfig.defaults create mode 100644 tools/unit-test-app/configs/flash_encryption diff --git a/components/bootloader/Kconfig.projbuild b/components/bootloader/Kconfig.projbuild index a2c2df6444..df25d75bcb 100644 --- a/components/bootloader/Kconfig.projbuild +++ b/components/bootloader/Kconfig.projbuild @@ -515,6 +515,20 @@ menu "Security features" Only set this option in testing environments. + config SECURE_FLASH_REQUIRE_ALREADY_ENABLED + bool "Require flash encryption to be already enabled" + depends on SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT + default N + help + If not set (default), and flash encryption is not yet enabled in eFuses, the 2nd stage bootloader + will enable flash encryption: generate the flash encryption key and program eFuses. + If this option is set, and flash encryption is not yet enabled, the bootloader will error out and + reboot. + If flash encryption is enabled in eFuses, this option does not change the bootloader behavior. + + Only use this option in testing environments, to avoid accidentally enabling flash encryption on + the wrong device. The device needs to have flash encryption already enabled using espefuse.py. + endmenu # Potentially Insecure endmenu # Security features diff --git a/components/bootloader_support/src/esp32/flash_encrypt.c b/components/bootloader_support/src/esp32/flash_encrypt.c index 41925de272..0e81ccff0b 100644 --- a/components/bootloader_support/src/esp32/flash_encrypt.c +++ b/components/bootloader_support/src/esp32/flash_encrypt.c @@ -37,7 +37,7 @@ static const char *TAG = "flash_encrypt"; /* Static functions for stages of flash encryption */ static esp_err_t initialise_flash_encryption(void); -static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_crypt_wr_dis); +static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_crypt_wr_dis) __attribute__((unused)); static esp_err_t encrypt_bootloader(void); static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions); static esp_err_t encrypt_partition(int index, const esp_partition_info_t *partition); @@ -60,8 +60,14 @@ esp_err_t esp_flash_encrypt_check_and_update(void) return ESP_OK; } else { +#ifndef CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED /* Flash is not encrypted, so encrypt it! */ return encrypt_flash_contents(flash_crypt_cnt, flash_crypt_wr_dis); +#else + ESP_LOGE(TAG, "flash encryption is not enabled, and SECURE_FLASH_REQUIRE_ALREADY_ENABLED " + "is set, refusing to boot."); + return ESP_ERR_INVALID_STATE; +#endif // CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED } } diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 7f06aff568..5526a1cb43 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -444,10 +444,13 @@ void start_cpu0_default(void) #endif bootloader_flash_update_id(); -#if !CONFIG_SPIRAM_BOOT_INIT // If psram is uninitialized, we need to improve some flash configuration. - esp_image_header_t fhdr; - const esp_partition_t *partition = esp_ota_get_running_partition(); - spi_flash_read(partition->address, &fhdr, sizeof(esp_image_header_t)); +#if !CONFIG_SPIRAM_BOOT_INIT + // Read the application binary image header. This will also decrypt the header if the image is encrypted. + esp_image_header_t fhdr = {0}; + // This assumes that DROM is the first segment in the application binary, i.e. that we can read + // the binary header through cache by accessing SOC_DROM_LOW address. + memcpy(&fhdr, (void*) SOC_DROM_LOW, sizeof(fhdr)); + // If psram is uninitialized, we need to improve some flash configuration. bootloader_flash_clock_config(&fhdr); bootloader_flash_gpio_config(&fhdr); bootloader_flash_dummy_config(&fhdr); diff --git a/components/spi_flash/esp_flash_api.c b/components/spi_flash/esp_flash_api.c index 0e16fc80d2..146ebd12f8 100644 --- a/components/spi_flash/esp_flash_api.c +++ b/components/spi_flash/esp_flash_api.c @@ -646,10 +646,4 @@ esp_err_t spi_flash_read(size_t src, void *dstv, size_t size) return spi_flash_translate_rc(err); } -esp_err_t spi_flash_unlock(void) -{ - esp_err_t err = esp_flash_set_chip_write_protect(NULL, false); - return spi_flash_translate_rc(err); -} - #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index 41405c8d08..1c5406a1c3 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -198,7 +198,7 @@ static esp_rom_spiflash_result_t IRAM_ATTR spi_flash_unlock(void) } return ESP_ROM_SPIFLASH_RESULT_OK; } -#endif +#endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL esp_err_t IRAM_ATTR spi_flash_erase_sector(size_t sec) { @@ -419,7 +419,7 @@ out: return spi_flash_translate_rc(rc); } -#endif +#endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size) { diff --git a/components/spi_flash/test/test_flash_encryption.c b/components/spi_flash/test/test_flash_encryption.c index ea647f3360..e16ca93719 100644 --- a/components/spi_flash/test/test_flash_encryption.c +++ b/components/spi_flash/test/test_flash_encryption.c @@ -9,6 +9,8 @@ #include #include +#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED + static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length); static void verify_erased_flash(size_t offset, size_t length); @@ -23,14 +25,10 @@ static void setup_tests(void) } } -TEST_CASE("test 16 byte encrypted writes", "[spi_flash]") +TEST_CASE("test 16 byte encrypted writes", "[flash_encryption][test_env=UT_T1_FlashEncryption]") { setup_tests(); - if (!esp_flash_encryption_enabled()) { - TEST_IGNORE_MESSAGE("flash encryption disabled, skipping spi_flash_write_encrypted() tests"); - } - TEST_ASSERT_EQUAL_HEX(ESP_OK, spi_flash_erase_sector(start / SPI_FLASH_SEC_SIZE)); @@ -101,3 +99,4 @@ static void verify_erased_flash(size_t offset, size_t length) } } +#endif // CONFIG_SECURE_FLASH_ENC_ENABLED diff --git a/components/spi_flash/test/test_mmap.c b/components/spi_flash/test/test_mmap.c index 52a5be1f3a..80458e77e1 100644 --- a/components/spi_flash/test/test_mmap.c +++ b/components/spi_flash/test/test_mmap.c @@ -21,6 +21,24 @@ static uint32_t end; static spi_flash_mmap_handle_t handle1, handle2, handle3; +static esp_err_t spi_flash_read_maybe_encrypted(size_t src_addr, void *des_addr, size_t size) +{ + if (!esp_flash_encryption_enabled()) { + return spi_flash_read(src_addr, des_addr, size); + } else { + return spi_flash_read_encrypted(src_addr, des_addr, size); + } +} + +static esp_err_t spi_flash_write_maybe_encrypted(size_t des_addr, const void *src_addr, size_t size) +{ + if (!esp_flash_encryption_enabled()) { + return spi_flash_write(des_addr, src_addr, size); + } else { + return spi_flash_write_encrypted(des_addr, src_addr, size); + } +} + static void setup_mmap_tests(void) { if (start == 0) { @@ -54,7 +72,7 @@ static void setup_mmap_tests(void) uint32_t sector_offs = abs_sector * SPI_FLASH_SEC_SIZE; bool sector_needs_write = false; - ESP_ERROR_CHECK( spi_flash_read(sector_offs, buffer, sizeof(buffer)) ); + ESP_ERROR_CHECK( spi_flash_read_maybe_encrypted(sector_offs, buffer, sizeof(buffer)) ); for (uint32_t word = 0; word < 1024; ++word) { uint32_t val = rand(); @@ -69,13 +87,13 @@ static void setup_mmap_tests(void) /* Only rewrite the sector if it has changed */ if (sector_needs_write) { ESP_ERROR_CHECK( spi_flash_erase_sector((uint16_t) abs_sector) ); - ESP_ERROR_CHECK( spi_flash_write(sector_offs, (const uint8_t *) buffer, sizeof(buffer)) ); + ESP_ERROR_CHECK( spi_flash_write_maybe_encrypted(sector_offs, (const uint8_t *) buffer, sizeof(buffer)) ); } } } } -TEST_CASE("Can mmap into data address space", "[spi_flash]") +TEST_CASE("Can mmap into data address space", "[spi_flash][mmap]") { setup_mmap_tests(); @@ -135,7 +153,7 @@ TEST_CASE("Can mmap into data address space", "[spi_flash]") TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(start, SPI_FLASH_MMAP_DATA)); } -TEST_CASE("Can mmap into instruction address space", "[mmap]") +TEST_CASE("Can mmap into instruction address space", "[spi_flash][mmap]") { setup_mmap_tests(); @@ -183,7 +201,7 @@ TEST_CASE("Can mmap into instruction address space", "[mmap]") } -TEST_CASE("Can mmap unordered pages into contiguous memory", "[spi_flash]") +TEST_CASE("Can mmap unordered pages into contiguous memory", "[spi_flash][mmap]") { int nopages; int *pages; @@ -226,7 +244,7 @@ TEST_CASE("Can mmap unordered pages into contiguous memory", "[spi_flash]") } -TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash]") +TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash][mmap]") { const void *ptr1; @@ -275,7 +293,7 @@ TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash]") handle1 = 0; } -TEST_CASE("flash_mmap can mmap after get enough free MMU pages", "[spi_flash]") +TEST_CASE("flash_mmap can mmap after get enough free MMU pages", "[spi_flash][mmap]") { //this test case should make flash size >= 4MB, because max size of Dcache can mapped is 4MB setup_mmap_tests(); @@ -324,7 +342,7 @@ TEST_CASE("flash_mmap can mmap after get enough free MMU pages", "[spi_flash]") TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(start, SPI_FLASH_MMAP_DATA)); } -TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash]") +TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash][mmap]") { uint8_t buf[64]; @@ -337,7 +355,7 @@ TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash]") TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(phys, SPI_FLASH_MMAP_DATA)); /* Read the flash @ 'phys' and compare it to the data we get via regular cache access */ - spi_flash_read(phys, buf, sizeof(buf)); + spi_flash_read_maybe_encrypted(phys, buf, sizeof(buf)); TEST_ASSERT_EQUAL_HEX32_ARRAY((void *)esp_partition_find, buf, sizeof(buf)/sizeof(uint32_t)); /* spi_flash_mmap is in IRAM */ @@ -353,11 +371,11 @@ TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash]") TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(phys, SPI_FLASH_MMAP_INST)); /* Read the flash @ 'phys' and compare it to the data we get via normal cache access */ - spi_flash_read(phys, buf, sizeof(constant_data)); + spi_flash_read_maybe_encrypted(phys, buf, sizeof(constant_data)); TEST_ASSERT_EQUAL_HEX8_ARRAY(constant_data, buf, sizeof(constant_data)); } -TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash]") +TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash][mmap]") { const void *ptr = NULL; const size_t test_size = 2 * SPI_FLASH_MMU_PAGE_SIZE; @@ -382,7 +400,7 @@ TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash]") TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr)); } -TEST_CASE("munmap followed by mmap flushes cache", "[spi_flash]") +TEST_CASE("munmap followed by mmap flushes cache", "[spi_flash][mmap]") { setup_mmap_tests(); @@ -401,9 +419,10 @@ TEST_CASE("munmap followed by mmap flushes cache", "[spi_flash]") TEST_ASSERT_NOT_EQUAL(0, memcmp(buf, data, sizeof(buf))); } -TEST_CASE("no stale data read post mmap and write partition", "[spi_flash]") +TEST_CASE("no stale data read post mmap and write partition", "[spi_flash][mmap]") { - const char buf[] = "Test buffer data for partition"; + /* Buffer size is set to 32 to allow encrypted flash writes */ + const char buf[32] = "Test buffer data for partition"; char read_data[sizeof(buf)]; setup_mmap_tests(); @@ -415,7 +434,9 @@ TEST_CASE("no stale data read post mmap and write partition", "[spi_flash]") SPI_FLASH_MMAP_DATA, (const void **) &data, &handle) ); memcpy(read_data, data, sizeof(read_data)); TEST_ESP_OK(esp_partition_erase_range(p, 0, SPI_FLASH_MMU_PAGE_SIZE)); - TEST_ESP_OK(esp_partition_write(p, 0, buf, sizeof(buf))); + /* not using esp_partition_write here, since the partition in not marked as "encrypted" + in the partition table */ + TEST_ESP_OK(spi_flash_write_maybe_encrypted(p->address + 0, buf, sizeof(buf))); /* This should retrigger actual flash content read */ memcpy(read_data, data, sizeof(read_data)); diff --git a/examples/security/flash_encryption/README.md b/examples/security/flash_encryption/README.md index 750ee2a7b8..3df704a656 100644 --- a/examples/security/flash_encryption/README.md +++ b/examples/security/flash_encryption/README.md @@ -1,6 +1,9 @@ # Flash Encryption -The example checks if the flash encryption feature is enabled/disabled and if enabled prints the flash encryption mode (DEVELOPMENT / RELEASE) and FLASH_CRYPT_CNT eFuse value +The example checks if the flash encryption feature is enabled/disabled and if enabled prints the flash encryption mode (DEVELOPMENT / RELEASE) and FLASH_CRYPT_CNT eFuse value. + +The example also demonstrates writing and reading encrypted partitions in flash. + ## How to use example ### Hardware Required @@ -21,7 +24,7 @@ idf.py menuconfig ### Build and Flash -When building the project and flashing it to the board FOR THE FIRST TIME after enabling flash encryption feature in menuconfig, run following command to program ESP32 and monitor the output +When building the project and flashing it to the board FOR THE FIRST TIME after enabling flash encryption feature in menuconfig, run following command to program ESP32 and monitor the output: ``` idf.py -p PORT flash monitor @@ -31,21 +34,40 @@ idf.py -p PORT flash monitor See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. -When reprogramming the device subsequently use following command for encrypted write of new plaintext application +When reprogramming the device subsequently use following command for encrypted write of new plaintext application: ``` -idf.py encrypted-app-flash monitor +idf.py -p PORT encrypted-app-flash monitor ``` -Please note above command programs only the app partition. In order to reprogram all partitions (bootloader, partition table and application) in encrypted form use +Please note above command programs only the app partition. In order to reprogram all partitions (bootloader, partition table and application) in encrypted form use: ``` -idf.py encrypted-flash monitor +idf.py -p PORT encrypted-flash monitor ``` ## Example Output -When the device boots for the first time after enabling flash encryption in Development mode the output would contain following +When running the example without enabling flash encryption, the output would be as follows: + +``` +Example to check Flash Encryption status +This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 0, 2MB external flash +FLASH_CRYPT_CNT eFuse value is 0 +Flash encryption feature is disabled +Erasing partition "storage" (0x1000 bytes) +Writing data with esp_partition_write: +I (378) example: 0x3ffb4dc0 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................| +I (378) example: 0x3ffb4dd0 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................| +Reading with esp_partition_read: +I (388) example: 0x3ffb4da0 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................| +I (398) example: 0x3ffb4db0 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................| +Reading with spi_flash_read: +I (408) example: 0x3ffb4da0 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................| +I (418) example: 0x3ffb4db0 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................| +``` + +After enabling flash encryption in Development mode, the output shows the process of enabling the flash encryption: ``` I (168) boot: Checking flash encryption... @@ -64,74 +86,23 @@ I (13229) flash_encrypt: Flash encryption completed I (13229) boot: Resetting with flash encryption enabled... ``` -Once the flash encryption is enabled the device will reset itself. At this stage the flash contents are in encrypted form. The output would be similar to +Once the flash encryption is enabled the device will reset itself. At this stage the flash contents are in encrypted form. The output would be similar to: ``` -rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) -configsip: 0, SPIWP:0xee -clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 -mode:DIO, clock div:2 -load:0x3fff0018,len:4 -load:0x3fff001c,len:8452 -load:0x40078000,len:13652 -ho 0 tail 12 room 4 -load:0x40080400,len:6664 -entry 0x40080764 -I (30) boot: ESP-IDF v4.0-dev-850-gc4447462d-dirty 2nd stage bootloader -I (30) boot: compile time 16:32:53 -I (31) boot: Enabling RNG early entropy source... -I (37) boot: SPI Speed : 40MHz -I (41) boot: SPI Mode : DIO -I (45) boot: SPI Flash Size : 4MB -I (49) boot: Partition Table: -I (52) boot: ## Label Usage Type ST Offset Length -I (60) boot: 0 nvs WiFi data 01 02 0000a000 00006000 -I (67) boot: 1 phy_init RF data 01 01 00010000 00001000 -I (75) boot: 2 factory factory app 00 00 00020000 00100000 -I (82) boot: End of partition table -I (86) esp_image: segment 0: paddr=0x00020020 vaddr=0x3f400020 size=0x0808c ( 32908) map -I (107) esp_image: segment 1: paddr=0x000280b4 vaddr=0x3ffb0000 size=0x01ea4 ( 7844) load -I (111) esp_image: segment 2: paddr=0x00029f60 vaddr=0x40080000 size=0x00400 ( 1024) load -0x40080000: _WindowOverflow4 at esp-idf/esp-idf/components/freertos/xtensa_vectors.S:1778 - -I (116) esp_image: segment 3: paddr=0x0002a368 vaddr=0x40080400 size=0x05ca8 ( 23720) load -I (134) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x126a8 ( 75432) map -0x400d0018: _flash_cache_start at ??:? - -I (162) esp_image: segment 5: paddr=0x000426c8 vaddr=0x400860a8 size=0x01f4c ( 8012) load -0x400860a8: prvAddNewTaskToReadyList at esp-idf/esp-idf/components/freertos/tasks.c:4561 - -I (171) boot: Loaded app from partition at offset 0x20000 -I (171) boot: Checking flash encryption... -I (171) flash_encrypt: flash encryption is enabled (3 plaintext flashes left) -I (178) boot: Disabling RNG early entropy source... -I (184) cpu_start: Pro cpu up. -I (188) cpu_start: Application information: -I (193) cpu_start: Project name: flash-encryption -I (198) cpu_start: App version: v4.0-dev-850-gc4447462d-dirty -I (205) cpu_start: Compile time: Jun 17 2019 16:32:52 -I (211) cpu_start: ELF file SHA256: 8770c886bdf561a7... -I (217) cpu_start: ESP-IDF: v4.0-dev-850-gc4447462d-dirty -I (224) cpu_start: Starting app cpu, entry point is 0x40080e4c -0x40080e4c: call_start_cpu1 at esp-idf/esp-idf/components/esp32/cpu_start.c:265 - -I (0) cpu_start: App cpu up. -I (235) heap_init: Initializing. RAM available for dynamic allocation: -I (241) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM -I (247) heap_init: At 3FFB2EC8 len 0002D138 (180 KiB): DRAM -I (254) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM -I (260) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM -I (266) heap_init: At 40087FF4 len 0001800C (96 KiB): IRAM -I (273) cpu_start: Pro cpu start user code -I (291) cpu_start: Starting scheduler on PRO CPU. -I (0) cpu_start: Starting scheduler on APP CPU. - - Sample program to check Flash Encryption - This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash - Flash encryption feature is enabled - Flash encryption mode is DEVELOPMENT - Flash in encrypted mode with flash_crypt_cnt = 1 - Halting... +Example to check Flash Encryption status +This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 0, 4MB external flash +FLASH_CRYPT_CNT eFuse value is 1 +Flash encryption feature is enabled in DEVELOPMENT mode +Erasing partition "storage" (0x1000 bytes) +Writing data with esp_partition_write: +I (451) example: 0x3ffb4dc0 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................| +I (451) example: 0x3ffb4dd0 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................| +Reading with esp_partition_read: +I (461) example: 0x3ffb4da0 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................| +I (471) example: 0x3ffb4db0 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................| +Reading with spi_flash_read: +I (491) example: 0x3ffb4da0 29 68 2e 13 88 a0 5b 7f cc 6b 39 f9 d7 7b 32 2f |)h....[..k9..{2/| +I (491) example: 0x3ffb4db0 9f e6 55 37 4b 91 b0 83 cd a6 e9 4e cd fa b4 c7 |..U7K......N....| ``` ## Troubleshooting @@ -139,7 +110,7 @@ I (0) cpu_start: Starting scheduler on APP CPU. It is also possible to use esptool.py utility to read the eFuse values and check if flash encryption is enabled or not ``` -python $IDF_PATH/components/esptool_py/esptool/espefuse.py --port /dev/cu.SLAB_USBtoUART summary +python $IDF_PATH/components/esptool_py/esptool/espefuse.py --port PORT summary ``` If FLASH_CRYPT_CNT eFuse value is non-zero flash encryption is enabled diff --git a/examples/security/flash_encryption/example_test.py b/examples/security/flash_encryption/example_test.py new file mode 100644 index 0000000000..401083b0cd --- /dev/null +++ b/examples/security/flash_encryption/example_test.py @@ -0,0 +1,41 @@ +from __future__ import print_function +import os +import sys + +try: + import IDF +except ImportError: + test_fw_path = os.getenv('TEST_FW_PATH') + if test_fw_path and test_fw_path not in sys.path: + sys.path.insert(0, test_fw_path) + import IDF + + +# To prepare a test runner for this example: +# 1. Generate zero flash encryption key: +# dd if=/dev/zero of=key.bin bs=1 count=32 +# 2.Burn Efuses: +# espefuse.py --do-not-confirm -p $ESPPORT burn_efuse FLASH_CRYPT_CONFIG 0xf +# espefuse.py --do-not-confirm -p $ESPPORT burn_efuse FLASH_CRYPT_CNT 0x1 +# espefuse.py --do-not-confirm -p $ESPPORT burn_key flash_encryption key.bin +@IDF.idf_example_test(env_tag='Example_Flash_Encryption') +def test_examples_security_flash_encryption(env, extra_data): + dut = env.get_dut('flash_encryption', 'examples/security/flash_encryption') + # start test + dut.start_app() + lines = [ + 'FLASH_CRYPT_CNT eFuse value is 1', + 'Flash encryption feature is enabled in DEVELOPMENT mode', + 'with esp_partition_write', + '00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f', + 'with esp_partition_read', + '00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f', + 'with spi_flash_read', + '29 68 2e 13 88 a0 5b 7f cc 6b 39 f9 d7 7b 32 2f' + ] + for line in lines: + dut.expect(line, timeout=2) + + +if __name__ == '__main__': + test_examples_security_flash_encryption() diff --git a/examples/security/flash_encryption/main/flash_encrypt_main.c b/examples/security/flash_encryption/main/flash_encrypt_main.c index 73ab39afe5..ae367e94c5 100644 --- a/examples/security/flash_encryption/main/flash_encrypt_main.c +++ b/examples/security/flash_encryption/main/flash_encrypt_main.c @@ -13,16 +13,29 @@ #include "esp_efuse.h" #include "esp_system.h" #include "esp_spi_flash.h" +#include "esp_partition.h" #include "esp_flash_encrypt.h" #include "esp_efuse_table.h" +static void example_print_chip_info(void); +static void example_print_flash_encryption_status(void); +static void example_read_write_flash(void); + +static const char* TAG = "example"; + + void app_main(void) { - uint32_t flash_crypt_cnt = 0; - esp_flash_enc_mode_t mode; + printf("\nExample to check Flash Encryption status\n"); - printf("\nSample program to check Flash Encryption\n"); + example_print_chip_info(); + example_print_flash_encryption_status(); + example_read_write_flash(); +} + +static void example_print_chip_info(void) +{ /* Print chip information */ esp_chip_info_t chip_info; esp_chip_info(&chip_info); @@ -35,33 +48,52 @@ void app_main(void) printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024), (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); - - esp_efuse_read_field_blob(ESP_EFUSE_FLASH_CRYPT_CNT, &flash_crypt_cnt, 7); - - if (esp_flash_encryption_enabled()) { - printf("Flash encryption feature is enabled\n"); - mode = esp_get_flash_encryption_mode(); - - if (mode == ESP_FLASH_ENC_MODE_DEVELOPMENT) { - printf("Flash encryption mode is DEVELOPMENT\n"); - /* If odd bits set flash encryption is enabled else disabled */ - if (__builtin_parity(flash_crypt_cnt) == 1) { - printf("Flash in encrypted mode with flash_crypt_cnt = %d\n", flash_crypt_cnt); - } - else { - printf("Flash in plaintext mode with flash_crypt_cnt = %d\n", flash_crypt_cnt); - } - } else { - printf("Flash encryption mode is RELEASE\n"); - } - } - else { - printf("FLASH_CRYPT_CNT eFuse value is %d\n", flash_crypt_cnt); - printf("Flash encryption feature is disabled\n\n"); - } - - printf("Halting...\n"); - while(1) { - vTaskDelay(1000 / portTICK_PERIOD_MS); - } +} + + +static void example_print_flash_encryption_status(void) +{ + uint32_t flash_crypt_cnt = 0; + esp_efuse_read_field_blob(ESP_EFUSE_FLASH_CRYPT_CNT, &flash_crypt_cnt, 7); + printf("FLASH_CRYPT_CNT eFuse value is %d\n", flash_crypt_cnt); + + esp_flash_enc_mode_t mode = esp_get_flash_encryption_mode(); + if (mode == ESP_FLASH_ENC_MODE_DISABLED) { + printf("Flash encryption feature is disabled\n"); + } else { + printf("Flash encryption feature is enabled in %s mode\n", + mode == ESP_FLASH_ENC_MODE_DEVELOPMENT ? "DEVELOPMENT" : "RELEASE"); + } +} + + +static void example_read_write_flash(void) +{ + const esp_partition_t* partition = esp_partition_find_first( + ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage"); + assert(partition); + + printf("Erasing partition \"%s\" (0x%x bytes)\n", partition->label, partition->size); + + ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size)); + + /* Generate the data which will be written */ + const size_t data_size = 32; + uint8_t plaintext_data[data_size]; + for (uint8_t i = 0; i < data_size; ++i) { + plaintext_data[i] = i; + } + + printf("Writing data with esp_partition_write:\n"); + ESP_LOG_BUFFER_HEXDUMP(TAG, plaintext_data, data_size, ESP_LOG_INFO); + ESP_ERROR_CHECK(esp_partition_write(partition, 0, plaintext_data, data_size)); + + uint8_t read_data[data_size]; + printf("Reading with esp_partition_read:\n"); + ESP_ERROR_CHECK(esp_partition_read(partition, 0, read_data, data_size)); + ESP_LOG_BUFFER_HEXDUMP(TAG, read_data, data_size, ESP_LOG_INFO); + + printf("Reading with spi_flash_read:\n"); + ESP_ERROR_CHECK(spi_flash_read(partition->address, read_data, data_size)); + ESP_LOG_BUFFER_HEXDUMP(TAG, read_data, data_size, ESP_LOG_INFO); } diff --git a/examples/security/flash_encryption/partitions_example.csv b/examples/security/flash_encryption/partitions_example.csv new file mode 100644 index 0000000000..a7df975994 --- /dev/null +++ b/examples/security/flash_encryption/partitions_example.csv @@ -0,0 +1,5 @@ +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x6000, +# Extra partition to demonstrate reading/writing of encrypted flash +storage, data, 0xff, 0xf000, 0x1000, encrypted +factory, app, factory, 0x10000, 1M, diff --git a/examples/security/flash_encryption/sdkconfig.ci b/examples/security/flash_encryption/sdkconfig.ci new file mode 100644 index 0000000000..b7f834c1bf --- /dev/null +++ b/examples/security/flash_encryption/sdkconfig.ci @@ -0,0 +1,12 @@ +# Default settings for testing this example in CI. +# This configuration is not secure, don't use it in production! +# See Flash Encryption API Guide for more details. + +CONFIG_SECURE_FLASH_ENC_ENABLED=y +CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=y +CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC=y +CONFIG_SECURE_BOOT_ALLOW_JTAG=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=y +CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED=y diff --git a/examples/security/flash_encryption/sdkconfig.defaults b/examples/security/flash_encryption/sdkconfig.defaults new file mode 100644 index 0000000000..6a8feba65b --- /dev/null +++ b/examples/security/flash_encryption/sdkconfig.defaults @@ -0,0 +1,4 @@ +# This example uses an extra partition to demonstrate encrypted/non-encrypted reads/writes. +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_example.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions_example.csv" diff --git a/tools/ci/config/target-test.yml b/tools/ci/config/target-test.yml index 6aa25316ea..4609f10be1 100644 --- a/tools/ci/config/target-test.yml +++ b/tools/ci/config/target-test.yml @@ -196,6 +196,12 @@ example_test_007: - ESP32 - Example_I2C_CCS811_SENSOR +example_test_008: + extends: .example_test_template + tags: + - ESP32 + - Example_Flash_Encryption + UT_001: extends: .unit_test_template parallel: 50 @@ -412,6 +418,12 @@ UT_030: - ESP32_IDF - UT_T1_1 +UT_031: + extends: .unit_test_template + tags: + - ESP32_IDF + - UT_T1_FlashEncryption + nvs_compatible_test: extends: .test_template artifacts: diff --git a/tools/tiny-test-fw/IDF/IDFApp.py b/tools/tiny-test-fw/IDF/IDFApp.py index 7033301ff5..82f42b0c35 100644 --- a/tools/tiny-test-fw/IDF/IDFApp.py +++ b/tools/tiny-test-fw/IDF/IDFApp.py @@ -135,6 +135,11 @@ class IDFApp(App.BaseApp): # offs, filename flash_files.append((args[idx], args[idx + 1])) + # The build metadata file does not currently have details, which files should be encrypted and which not. + # Assume that all files should be encrypted if flash encryption is enabled in development mode. + sdkconfig_dict = self.get_sdkconfig() + flash_settings["encrypt"] = "CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT" in sdkconfig_dict + # make file offsets into integers, make paths absolute flash_files = [(int(offs, 0), os.path.join(self.binary_path, path.strip())) for (offs, path) in flash_files] diff --git a/tools/tiny-test-fw/IDF/IDFDUT.py b/tools/tiny-test-fw/IDF/IDFDUT.py index c8ee24f236..bb217ca2c8 100644 --- a/tools/tiny-test-fw/IDF/IDFDUT.py +++ b/tools/tiny-test-fw/IDF/IDFDUT.py @@ -217,7 +217,7 @@ class IDFDUT(DUT.SerialDUT): 'no_stub': False, 'compress': True, 'verify': False, - 'encrypt': False, + 'encrypt': self.app.flash_settings.get("encrypt", False), 'erase_all': False, }) diff --git a/tools/unit-test-app/configs/flash_encryption b/tools/unit-test-app/configs/flash_encryption new file mode 100644 index 0000000000..7334d67370 --- /dev/null +++ b/tools/unit-test-app/configs/flash_encryption @@ -0,0 +1,10 @@ +TEST_COMPONENTS=spi_flash +TEST_GROUPS=flash_encryption +CONFIG_SECURE_FLASH_ENC_ENABLED=y +CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=y +CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC=y +CONFIG_SECURE_BOOT_ALLOW_JTAG=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=y +CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=y +CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED=y diff --git a/tools/unit-test-app/tools/ModuleDefinition.yml b/tools/unit-test-app/tools/ModuleDefinition.yml index 0f9a31f901..a7b54404e3 100644 --- a/tools/unit-test-app/tools/ModuleDefinition.yml +++ b/tools/unit-test-app/tools/ModuleDefinition.yml @@ -78,6 +78,11 @@ spi_flash_write: module abbr: DRV sub module: SPI sub module abbr: SPI +flash_encryption: + module: Driver + module abbr: DRV + sub module: SPI + sub module abbr: SPI esp32: module: System module abbr: SYS From ff96965a91290f4c4abcf06405d1075bdc014cce Mon Sep 17 00:00:00 2001 From: zhangyanjiao Date: Fri, 23 Aug 2019 14:37:08 +0800 Subject: [PATCH 047/146] wifi: fix the bug for softAP set authmode --- components/esp_wifi/lib_esp32 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/lib_esp32 b/components/esp_wifi/lib_esp32 index 00ed323d85..8b2b62efb3 160000 --- a/components/esp_wifi/lib_esp32 +++ b/components/esp_wifi/lib_esp32 @@ -1 +1 @@ -Subproject commit 00ed323d855e71eab0e7cbf114733de92bf0d6bb +Subproject commit 8b2b62efb3ce88b053e41c21a1cdb3d1d409e32e From 69f45c367443f18f52a21203f64f1eacc696d3ed Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Wed, 31 Jul 2019 22:31:58 +0800 Subject: [PATCH 048/146] bootloader: Factory reset not for deep sleep Closes: https://github.com/espressif/esp-idf/issues/3800 Closes: IDFGH-1536 --- components/bootloader/subproject/main/bootloader_start.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/bootloader/subproject/main/bootloader_start.c b/components/bootloader/subproject/main/bootloader_start.c index e9bd32d881..0800ef26de 100644 --- a/components/bootloader/subproject/main/bootloader_start.c +++ b/components/bootloader/subproject/main/bootloader_start.c @@ -24,6 +24,7 @@ #include "bootloader_common.h" #include "sdkconfig.h" #include "esp_image_format.h" +#include "esp32/rom/rtc.h" static const char* TAG = "boot"; @@ -74,7 +75,8 @@ static int selected_boot_partition(const bootloader_state_t *bs) int boot_index = bootloader_utility_get_selected_boot_partition(bs); if (boot_index == INVALID_INDEX) { return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents) - } else { + } + if (rtc_get_reset_reason(0) != DEEPSLEEP_RESET) { // Factory firmware. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) { From 30953ba9c4b4af4c2b22f7850671b8dd0059dc33 Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Wed, 31 Jul 2019 22:35:23 +0800 Subject: [PATCH 049/146] app_update: Fix UTs for FACTORY_RESET and APP_TEST --- components/app_update/test/test_switch_ota.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/app_update/test/test_switch_ota.c b/components/app_update/test/test_switch_ota.c index f0d056d9bd..4b0907b41a 100644 --- a/components/app_update/test/test_switch_ota.c +++ b/components/app_update/test/test_switch_ota.c @@ -444,7 +444,7 @@ static void test_flow4(void) // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//-- // 3 Stage: run OTA0 -> check it -> set_pin_factory_reset -> reboot --//-- // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, sets pin_factory_reset, factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow4, test_flow4, test_flow4); +TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, sets pin_factory_reset, factory", "[app_update][timeout=90][ignore][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow4, test_flow4, test_flow4); #endif #ifdef CONFIG_BOOTLOADER_APP_TEST @@ -487,7 +487,7 @@ static void test_flow5(void) // 2 Stage: run factory -> check it -> copy factory to Test and set pin_test_app -> reboot --//-- // 3 Stage: run test -> check it -> reset pin_test_app -> reboot --//-- // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Switching between factory, test, factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow5, test_flow5, test_flow5); +TEST_CASE_MULTIPLE_STAGES("Switching between factory, test, factory", "[app_update][timeout=90][ignore][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow5, test_flow5, test_flow5); #endif static const esp_partition_t* app_update(void) From 954a23be37f58b7ff8a8dc7405503c2c67f686e7 Mon Sep 17 00:00:00 2001 From: He Yin Ling Date: Sun, 25 Aug 2019 14:09:17 +0800 Subject: [PATCH 050/146] test: fix nvs compatible case error: nvs compatible test case uses new test env and app. update test config file. --- .../integration_test/CIConfigs/nvs_compatible_test_.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/idf_test/integration_test/CIConfigs/nvs_compatible_test_.yml b/components/idf_test/integration_test/CIConfigs/nvs_compatible_test_.yml index 6318d4e425..917efd7481 100644 --- a/components/idf_test/integration_test/CIConfigs/nvs_compatible_test_.yml +++ b/components/idf_test/integration_test/CIConfigs/nvs_compatible_test_.yml @@ -1,11 +1,11 @@ BinPath: - path: SSC/ssc_bin/ESP32_IDF/SSC_BLE - test app: SSC_BLE + path: SSC/ssc_bin/ESP32_IDF/SSC_BLE_WIFI + test app: SSC_BLE_WIFI DUT: [SSC1] Filter: - Add: SDK: ESP32_IDF - Test App: SSC_BLE + Test App: SSC_BLE_WIFI summary: 'use old NVS data WIFI function test' From f002d1c6aab7aa0eaadbfb6ae0638b02263ef7cf Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Mon, 26 Aug 2019 11:19:23 +0800 Subject: [PATCH 051/146] Component/bt: fix dome BLE bugs - fix data loss and send same pkt twice sometimes - fix adv data error in air sometimes - fix adv start/stop crash sometimes --- components/bt/controller/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib b/components/bt/controller/lib index 9ee3b17ba9..a45ff23f0f 160000 --- a/components/bt/controller/lib +++ b/components/bt/controller/lib @@ -1 +1 @@ -Subproject commit 9ee3b17ba9879ecee09070c5a543a57d8aff2036 +Subproject commit a45ff23f0f6682caeb40dbb673f9dc54f0f8e1cd From f2a4131c241fa1959283e903bc884bc63b948af5 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Mon, 26 Aug 2019 13:32:23 +0200 Subject: [PATCH 052/146] Tools: Fix shebang for MacOs Revert a change introduced in d296aad2a99f16978031b1561a64927d771f76be for intended compatibility with BSD which breaks the build on MacOs: "clang: error: unknown argument: '-n'" for "echo". BSD users will have to use some workaround since MacOs is a supported OS of IDF and BSD is not. --- tools/kconfig/lxdialog/check-lxdialog.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/kconfig/lxdialog/check-lxdialog.sh b/tools/kconfig/lxdialog/check-lxdialog.sh index de48a1c260..e9daa6270c 100755 --- a/tools/kconfig/lxdialog/check-lxdialog.sh +++ b/tools/kconfig/lxdialog/check-lxdialog.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Check ncurses compatibility # What library to link From 79dd142ab8536643815582585c2aa6c7ceda57b5 Mon Sep 17 00:00:00 2001 From: Andrei Gramakov Date: Tue, 27 Aug 2019 13:45:50 +0800 Subject: [PATCH 053/146] tools: add install.ps1, export.ps1 Closes IDF-855 --- docs/en/get-started/index.rst | 23 ++++++-- docs/en/get-started/windows-setup-update.rst | 17 ++++-- export.ps1 | 62 ++++++++++++++++++++ install.ps1 | 22 +++++++ 4 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 export.ps1 create mode 100644 install.ps1 diff --git a/docs/en/get-started/index.rst b/docs/en/get-started/index.rst index 713362bb72..10b10c74bb 100644 --- a/docs/en/get-started/index.rst +++ b/docs/en/get-started/index.rst @@ -1,4 +1,4 @@ -*********** +*********** Get Started *********** @@ -62,7 +62,7 @@ If you have one of ESP32 development boards listed below, you can click on the l ESP-WROVER-KIT <../hw-reference/get-started-wrover-kit> ESP32-PICO-KIT <../hw-reference/get-started-pico-kit> ESP32-Ethernet-Kit <../hw-reference/get-started-ethernet-kit> - + .. _get-started-step-by-step: @@ -175,6 +175,13 @@ If you want to install the tools without the help of ESP-IDF Tools Installer, op cd %userprofile%\esp\esp-idf install.bat +or with Windows PowerShell + +.. code-block:: powershell + + cd ~/esp/esp-idf + ./install.ps1 + Linux and macOS ~~~~~~~~~~~~~~~ @@ -188,7 +195,7 @@ Customizing the tools installation path The scripts introduced in this step install compilation tools required by ESP-IDF inside the user home directory: ``$HOME/.espressif`` on Linux and macOS, ``%USERPROFILE%\.espressif`` on Windows. If you wish to install the tools into a different directory, set the environment variable ``IDF_TOOLS_PATH`` before running the installation scripts. Make sure that your user has sufficient permissions to read and write this path. -If changing the ``IDF_TOOLS_PATH``, make sure it is set to the same value every time the ``install.bat``/``install.sh`` and ``export.bat``/``export.sh`` scripts are executed. +If changing the ``IDF_TOOLS_PATH``, make sure it is set to the same value every time the Install script (``install.bat``, ``install.ps1`` or ``install.sh``) and an Export script (``export.bat``, ``export.ps1`` or ``export.sh``) are executed. .. _get-started-set-up-env: @@ -208,6 +215,12 @@ Alternatively, if you want to use ESP-IDF in an existing Command Prompt window, %userprofile%\esp\esp-idf\export.bat +or with Windows PowerShell + +.. code-block:: powershell + + .$HOME/esp/esp-idf/export.ps1 + Linux and macOS ~~~~~~~~~~~~~~~ @@ -481,9 +494,9 @@ You should update ESP-IDF from time to time, as newer versions fix bugs and prov Another solution is to update only what has changed. :ref:`The update procedure depends on the version of ESP-IDF you are using `. -After updating ESP-IDF, execute ``install.sh`` (``install.bat`` on Windows) again, in case the new ESP-IDF version requires different versions of tools. See instructions at :ref:`get-started-set-up-tools`. +After updating ESP-IDF, execute the Install script again, in case the new ESP-IDF version requires different versions of tools. See instructions at :ref:`get-started-set-up-tools`. -Once the new tools are installed, update the environment using ``export.sh`` (``export.bat`` on Windows). See instructions at :ref:`get-started-set-up-env`. +Once the new tools are installed, update the environment using the Export script. See instructions at :ref:`get-started-set-up-env`. Related Documents ================= diff --git a/docs/en/get-started/windows-setup-update.rst b/docs/en/get-started/windows-setup-update.rst index a1206354d4..531573e7dc 100644 --- a/docs/en/get-started/windows-setup-update.rst +++ b/docs/en/get-started/windows-setup-update.rst @@ -4,20 +4,24 @@ Updating ESP-IDF tools on Windows .. _get-started-install_bat-windows: -Install ESP-IDF tools using ``install.bat`` -=========================================== +Install ESP-IDF tools using a script +==================================== From the Windows Command Prompt, change to the directory where ESP-IDF is installed. Then run:: install.bat +For Powershell, change to the directory where ESP-IDF is installed. Then run:: + + install.ps1 + This will download and install the tools necessary to use ESP-IDF. If the specific version of the tool is already installed, no action will be taken. The tools are downloaded and installed into a directory specified during ESP-IDF Tools Installer process. By default, this is ``C:\Users\username\.espressif``. .. _get-started-export_bat-windows: -Add ESP-IDF tools to PATH using ``export.bat`` -============================================== +Add ESP-IDF tools to PATH using an export script +================================================ ESP-IDF tools installer creates a Start menu shortcut for "ESP-IDF Command Prompt". This shortcut opens a Command Prompt window where all the tools are already available. @@ -28,4 +32,9 @@ In the command prompt where you need to use ESP-IDF, change to the directory whe cd %userprofile%\esp\esp-idf export.bat +Alternatively in the Powershell where you need to use ESP-IDF, change to the directory where ESP-IDF is installed, then execute ``export.ps1``:: + + cd ~/esp/esp-idf + export.ps1 + When this is done, the tools will be available in this command prompt. diff --git a/export.ps1 b/export.ps1 new file mode 100644 index 0000000000..a2cafd55ae --- /dev/null +++ b/export.ps1 @@ -0,0 +1,62 @@ +if ($env:MSYSTEM -ne $null) { + Write-Output "This .ps1 file is for Windows Powershell only. When using MSYS, run:`n. ./export.sh." + exit 1 +} + + +$IDF_PATH = $PSScriptRoot + +Write-Output "Setting IDF_PATH: $IDF_PATH" +$env:IDF_PATH=$IDF_PATH + +Write-Output "Adding ESP-IDF tools to PATH..." +$OLD_PATH=$env:Path.split(";") | Select-Object -Unique # array without duplicates +# using idf_tools.py to get $envars_array to set +$envars_raw = python.exe $IDF_PATH\tools\idf_tools.py export --format key-value +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error + +$envars_array # will be filled like: +# [ +# [vname1, vval1], [vname2, vval2], ... +# ] +foreach ($line in $envars_raw) +{ + $pair = $line.split("=") # split in name, val + $var_name = $pair[0].Trim() # trim spaces on the ends of the name + $var_val = $pair[1].Trim() # trim spaces on the ends of the val + $var_val = $var_val -replace "%(.+)%", "`$env:`$1" # convert var syntax to PS using RegEx + $var_val = $ExecutionContext.InvokeCommand.ExpandString($var_val) # expand variables to values + $envars_array+=(,($var_name, $var_val)) +} + +foreach ($pair in $envars_array) # setting the values +{ + $var_name = $pair[0].Trim() # trim spaces on the ends of the name + $var_val = $pair[1].Trim() # trim spaces on the ends of the val + Set-Item -Path "Env:$var_name" -Value "$var_val" +} + +#Compare Path's OLD vs. NEW +$NEW_PATH = $env:Path.split(";") | Select-Object -Unique # array without duplicates +$dif_Path = Compare-Object -ReferenceObject $OLD_PATH -DifferenceObject $NEW_PATH -PassThru +if ($dif_Path -ne $null) +{ + Write-Output $dif_Path +} +else { + Write-Output "No directories added to PATH:" + Write-Output $OLD_PATH +} + + +Write-Output "Checking if Python packages are up to date..." + +Start-Process -Wait -NoNewWindow -FilePath "python" -Args "$IDF_PATH/tools/check_python_dependencies.py" +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error + +Write-Output " +Done! You can now compile ESP-IDF projects. +Go to the project directory and run: + idf.py build + +" diff --git a/install.ps1 b/install.ps1 new file mode 100644 index 0000000000..8067df81ee --- /dev/null +++ b/install.ps1 @@ -0,0 +1,22 @@ +if ($env:MSYSTEM -ne $null) { + Write-Output "This .ps1 file is for Windows Powershell only. When using MSYS, run:`n. ./export.sh." + exit 1 +} + + +$IDF_PATH = $PSScriptRoot + + +Write-Output "Installing ESP-IDF tools" +Start-Process -Wait -NoNewWindow -FilePath "python.exe" -Args "$IDF_PATH/tools/idf_tools.py install" +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error + +Write-Output "Setting up Python environment" +Start-Process -Wait -NoNewWindow -FilePath "python.exe" -Args "$IDF_PATH/tools/idf_tools.py install-python-env" +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE} # if error + + +Write-Output " +All done! You can now run: + export.ps1 +" From c50a67567ae497f7d080c63db3630a3aa7c780bb Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Tue, 27 Aug 2019 10:26:34 +0800 Subject: [PATCH 054/146] ldgen: fix build issue on make when build dir is a symlink Fixes an issue where build fails if the build directory is a symlink. The issue is caused by the rule target and the final executable dependency not matching. Closes https://github.com/espressif/esp-idf/issues/3626 --- components/esp32/Makefile.projbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp32/Makefile.projbuild b/components/esp32/Makefile.projbuild index cf8746f268..d13dbe2d0b 100644 --- a/components/esp32/Makefile.projbuild +++ b/components/esp32/Makefile.projbuild @@ -10,7 +10,7 @@ ifneq ("$(filter esp32,$(TEST_COMPONENTS_LIST))","") CPPFLAGS += -DESP_TIMER_DYNAMIC_OVERFLOW_VAL endif ESP32_LINKER_SCRIPT_TEMPLATE := $(COMPONENT_PATH)/ld/esp32.project.ld.in -ESP32_LINKER_SCRIPT_OUTPUT_DIR := $(abspath $(BUILD_DIR_BASE)/esp32) +ESP32_LINKER_SCRIPT_OUTPUT_DIR := $(realpath $(BUILD_DIR_BASE)/esp32) # Target to generate linker script generator from fragments presented by each of # the components From 474f6d9d9839cf0de0f632e2f590643f4131d0dd Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Tue, 27 Aug 2019 11:58:45 +0200 Subject: [PATCH 055/146] Fix path in the Efuse table test --- tools/ci/config/host-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ci/config/host-test.yml b/tools/ci/config/host-test.yml index 9ac5c0b4a9..89223be320 100644 --- a/tools/ci/config/host-test.yml +++ b/tools/ci/config/host-test.yml @@ -221,7 +221,7 @@ test_esp_efuse_table_on_host: - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh -p 2.7.15 ./efuse_table_gen.py ${IDF_PATH}/components/efuse/esp32/esp_efuse_table.csv - git diff --exit-code -- esp32/esp_efuse_table.c || { echo 'Differences found. Please run make efuse_common_table or idf.py efuse_common_table and commit the changes.'; exit 1; } - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh -p 3.4.8 ./efuse_table_gen.py ${IDF_PATH}/components/efuse/esp32/esp_efuse_table.csv - - git diff --exit-code -- ../components/esp32/esp_efuse_table.c || { echo 'Differences found between running under Python 2 and 3.'; exit 1; } + - git diff --exit-code -- esp32/esp_efuse_table.c || { echo 'Differences found between running under Python 2 and 3.'; exit 1; } - cd ${IDF_PATH}/components/efuse/test_efuse_host - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh ./efuse_tests.py From fb72a6f629a62c0655e8eff1d2d3f67d7aa9b62c Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 27 Aug 2019 00:22:10 +0200 Subject: [PATCH 056/146] bootloader: add definition of esp_clk_apb_freq Commit 8cd04c80 has added a dependency of efuse component on esp_clk_apb_freq, however there was no definition of this function in the bootloader context. Reported at https://esp32.com/viewtopic.php?f=13&t=12035 --- components/bootloader_support/src/bootloader_clock.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/components/bootloader_support/src/bootloader_clock.c b/components/bootloader_support/src/bootloader_clock.c index 3befda16dd..a40cb0688f 100644 --- a/components/bootloader_support/src/bootloader_clock.c +++ b/components/bootloader_support/src/bootloader_clock.c @@ -58,3 +58,12 @@ void bootloader_clock_configure(void) } #endif } + +#ifdef BOOTLOADER_BUILD + +int esp_clk_apb_freq(void) +{ + return rtc_clk_apb_freq_get(); +} + +#endif // BOOTLOADER_BUILD From 31cdd3874450f28358a8d96feee383b6b7977383 Mon Sep 17 00:00:00 2001 From: Per Malmberg Date: Tue, 20 Aug 2019 20:56:24 +0200 Subject: [PATCH 057/146] Make sure ESP_PLATFORM is defined when processing CMake files. --- tools/cmake/component.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/cmake/component.cmake b/tools/cmake/component.cmake index a7f099e4bf..8358bf2dfe 100644 --- a/tools/cmake/component.cmake +++ b/tools/cmake/component.cmake @@ -213,6 +213,7 @@ function(__component_get_requirements) __component_write_properties(${component_properties_file}) execute_process(COMMAND "${CMAKE_COMMAND}" + -D "ESP_PLATFORM=1" -D "BUILD_PROPERTIES_FILE=${build_properties_file}" -D "COMPONENT_PROPERTIES_FILE=${component_properties_file}" -D "COMPONENT_REQUIRES_FILE=${component_requires_file}" From ba2ec73d59754d97a7d8fada7d68f65db6b99484 Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Tue, 27 Aug 2019 09:24:17 +0800 Subject: [PATCH 058/146] cmake: fix build failure when git repo is initialized but no first commit Closes https://github.com/espressif/esp-idf/issues/3920 --- tools/cmake/project.cmake | 5 +++-- .../third_party/GetGitRevisionDescription.cmake.in | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index a8e28a12d8..3deb014889 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -38,8 +38,9 @@ function(__project_get_revision var) if(PROJECT_VER_GIT) set(PROJECT_VER ${PROJECT_VER_GIT}) else() - message(STATUS "Project is not inside a git repository, \ - will not use 'git describe' to determine PROJECT_VER.") + message(STATUS "Project is not inside a git repository, or git repository has no commits;" + " will not use 'git describe' to determine PROJECT_VER.") + set(PROJECT_VER 1) endif() endif() endif() diff --git a/tools/cmake/third_party/GetGitRevisionDescription.cmake.in b/tools/cmake/third_party/GetGitRevisionDescription.cmake.in index 6d8b708efe..0cf025b147 100644 --- a/tools/cmake/third_party/GetGitRevisionDescription.cmake.in +++ b/tools/cmake/third_party/GetGitRevisionDescription.cmake.in @@ -24,10 +24,12 @@ if(HEAD_CONTENTS MATCHES "ref") if(EXISTS "@GIT_DIR@/${HEAD_REF}") configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY) else() - configure_file("@GIT_DIR@/packed-refs" "@GIT_DATA@/packed-refs" COPYONLY) - file(READ "@GIT_DATA@/packed-refs" PACKED_REFS) - if(${PACKED_REFS} MATCHES "([0-9a-z]*) ${HEAD_REF}") - set(HEAD_HASH "${CMAKE_MATCH_1}") + if(EXISTS "@GIT_DIR@/packed-refs") + configure_file("@GIT_DIR@/packed-refs" "@GIT_DATA@/packed-refs" COPYONLY) + file(READ "@GIT_DATA@/packed-refs" PACKED_REFS) + if(${PACKED_REFS} MATCHES "([0-9a-z]*) ${HEAD_REF}") + set(HEAD_HASH "${CMAKE_MATCH_1}") + endif() endif() endif() else() From 5582878f8e35111bd147bcc38eeaafe64ed37a2b Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Tue, 30 Jul 2019 16:48:03 +0530 Subject: [PATCH 059/146] mbedtls: keep asymmetric tls buffer length option enabled by default --- components/mbedtls/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mbedtls/Kconfig b/components/mbedtls/Kconfig index 91b5c506ea..e597af2824 100644 --- a/components/mbedtls/Kconfig +++ b/components/mbedtls/Kconfig @@ -57,7 +57,7 @@ menu "mbedTLS" config MBEDTLS_ASYMMETRIC_CONTENT_LEN bool "Asymmetric in/out fragment length" - default n + default y help If enabled, this option allows customizing TLS in/out fragment length in asymmetric way. Please note that enabling this with default values From 30bc5953f2c47e4f112bfed8382c1d9e4cd918f2 Mon Sep 17 00:00:00 2001 From: wangmengyang Date: Wed, 28 Aug 2019 15:16:10 +0800 Subject: [PATCH 060/146] bugfix that call of esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_SUSPEND) will not get an ACK from callback on A2DP sink # Conflicts: # components/bootloader/subproject/components/micro-ecc/micro-ecc --- .../host/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c b/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c index aca3f5d860..d5e1b1ca80 100644 --- a/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c +++ b/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c @@ -117,7 +117,7 @@ void btc_a2dp_control_media_ctrl(esp_a2d_media_ctrl_t ctrl) APPL_TRACE_DEBUG("BTC MEDIA (A2DP-DATA) EVENT %u", ctrl); if (btc_aa_ctrl_cb.a2dp_cmd_pending != ESP_A2D_MEDIA_CTRL_NONE) { - APPL_TRACE_DEBUG("un-acked a2dp cmd: %u", btc_aa_ctrl_cb.a2dp_cmd_pending); + APPL_TRACE_WARNING("un-acked a2dp cmd: %u", btc_aa_ctrl_cb.a2dp_cmd_pending); a2dp_cmd_acknowledge(ctrl, ESP_A2D_MEDIA_CTRL_ACK_BUSY); return; } @@ -179,6 +179,11 @@ void btc_a2dp_control_media_ctrl(esp_a2d_media_ctrl_t ctrl) /* local suspend */ if (btc_av_stream_started_ready()) { btc_dispatch_sm_event(BTC_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0); +#if (BTC_AV_SINK_INCLUDED == TRUE) + if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) { + btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_SUCCESS); + } +#endif } else { /* we are not in started state; just ack back ok. This can happen if we are remotely suspended; clear REMOTE SUSPEND Flag */ From ebe53416ad09650da2bd1ea1467fa5cb7a4c3638 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 15 Aug 2019 12:35:59 +0530 Subject: [PATCH 061/146] ipc: prevent code getting pulled in for unicore configuration --- components/bt/controller/bt.c | 10 +++++++--- components/driver/gpio.c | 10 +++++++++- components/esp32/cpu_start.c | 1 - components/esp32/intr_alloc.c | 10 +++++++++- components/esp32/test/test_ipc.c | 7 +++---- components/esp_common/CMakeLists.txt | 22 ++++++++++++++-------- components/esp_common/component.mk | 5 +++++ 7 files changed, 47 insertions(+), 18 deletions(-) diff --git a/components/bt/controller/bt.c b/components/bt/controller/bt.c index 0453a102e4..0b29e506e4 100644 --- a/components/bt/controller/bt.c +++ b/components/bt/controller/bt.c @@ -37,13 +37,14 @@ #include "esp_err.h" #include "esp_log.h" #include "esp_pm.h" -#include "esp_ipc.h" #include "driver/periph_ctrl.h" #include "soc/rtc.h" #include "soc/soc_memory_layout.h" #include "esp32/clk.h" #include "esp_coexist_internal.h" - +#if !CONFIG_FREERTOS_UNICORE +#include "esp_ipc.h" +#endif #if CONFIG_BT_ENABLED @@ -755,12 +756,15 @@ static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no) { esp_err_t err = ESP_OK; +#if CONFIG_FREERTOS_UNICORE + cause_sw_intr((void *)intr_no); +#else /* CONFIG_FREERTOS_UNICORE */ if (xPortGetCoreID() == core_id) { cause_sw_intr((void *)intr_no); } else { err = esp_ipc_call(core_id, cause_sw_intr, (void *)intr_no); } - +#endif /* !CONFIG_FREERTOS_UNICORE */ return err; } diff --git a/components/driver/gpio.c b/components/driver/gpio.c index 96fc7a6658..f27a181f6c 100644 --- a/components/driver/gpio.c +++ b/components/driver/gpio.c @@ -20,7 +20,9 @@ #include "soc/soc.h" #include "soc/gpio_periph.h" #include "esp_log.h" +#if !CONFIG_FREERTOS_UNICORE #include "esp_ipc.h" +#endif #define GPIO_CHECK(a, str, ret_val) \ if (!(a)) { \ @@ -454,7 +456,13 @@ esp_err_t gpio_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, isr_core_id = xPortGetCoreID(); } portEXIT_CRITICAL(&gpio_spinlock); - esp_err_t ret = esp_ipc_call_blocking(isr_core_id, gpio_isr_register_on_core_static, (void *)&p); + esp_err_t ret; +#if CONFIG_FREERTOS_UNICORE + gpio_isr_register_on_core_static(&p); + ret = ESP_OK; +#else /* CONFIG_FREERTOS_UNICORE */ + ret = esp_ipc_call_blocking(isr_core_id, gpio_isr_register_on_core_static, (void *)&p); +#endif /* !CONFIG_FREERTOS_UNICORE */ if(ret != ESP_OK || p.ret != ESP_OK) { return ESP_ERR_NOT_FOUND; } diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 5526a1cb43..9ce5df4aef 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -47,7 +47,6 @@ #include "nvs_flash.h" #include "esp_event.h" #include "esp_spi_flash.h" -#include "esp_ipc.h" #include "esp_private/crosscore_int.h" #include "esp_log.h" #include "esp_vfs_dev.h" diff --git a/components/esp32/intr_alloc.c b/components/esp32/intr_alloc.c index 6256f6a8be..11466a5997 100644 --- a/components/esp32/intr_alloc.c +++ b/components/esp32/intr_alloc.c @@ -28,9 +28,11 @@ #include "esp_log.h" #include "esp_intr_alloc.h" #include "esp_attr.h" -#include "esp_ipc.h" #include #include +#if !CONFIG_FREERTOS_UNICORE +#include "esp_ipc.h" +#endif static const char* TAG = "intr_alloc"; @@ -705,20 +707,26 @@ esp_err_t IRAM_ATTR esp_intr_set_in_iram(intr_handle_t handle, bool is_in_iram) return ESP_OK; } +#if !CONFIG_FREERTOS_UNICORE static void esp_intr_free_cb(void *arg) { (void)esp_intr_free((intr_handle_t)arg); } +#endif /* !CONFIG_FREERTOS_UNICORE */ esp_err_t esp_intr_free(intr_handle_t handle) { bool free_shared_vector=false; if (!handle) return ESP_ERR_INVALID_ARG; + +#if !CONFIG_FREERTOS_UNICORE //Assign this routine to the core where this interrupt is allocated on. if (handle->vector_desc->cpu!=xPortGetCoreID()) { esp_err_t ret = esp_ipc_call_blocking(handle->vector_desc->cpu, &esp_intr_free_cb, (void *)handle); return ret == ESP_OK ? ESP_OK : ESP_FAIL; } +#endif /* !CONFIG_FREERTOS_UNICORE */ + portENTER_CRITICAL(&spinlock); esp_intr_disable(handle); if (handle->vector_desc->flags&VECDESC_FL_SHARED) { diff --git a/components/esp32/test/test_ipc.c b/components/esp32/test/test_ipc.c index c58e8f79a4..f6dd53103c 100644 --- a/components/esp32/test/test_ipc.c +++ b/components/esp32/test/test_ipc.c @@ -3,7 +3,9 @@ #include "freertos/task.h" #include "unity.h" #include "esp_ipc.h" +#include "sdkconfig.h" +#if !CONFIG_FREERTOS_UNICORE static void test_func_ipc_cb(void *arg) { vTaskDelay(50); @@ -14,10 +16,7 @@ static void test_func_ipc_cb(void *arg) TEST_CASE("Test blocking IPC function call", "[ipc]") { int val = 0x5a5a; -#ifdef CONFIG_FREERTOS_UNICORE - esp_ipc_call_blocking(xPortGetCoreID(), test_func_ipc_cb, &val); -#else esp_ipc_call_blocking(!xPortGetCoreID(), test_func_ipc_cb, &val); -#endif TEST_ASSERT_EQUAL_HEX(val, 0xa5a5); } +#endif /* !CONFIG_FREERTOS_UNICORE */ diff --git a/components/esp_common/CMakeLists.txt b/components/esp_common/CMakeLists.txt index bb9bd8a7a4..1c3274dd09 100644 --- a/components/esp_common/CMakeLists.txt +++ b/components/esp_common/CMakeLists.txt @@ -4,14 +4,20 @@ if(BOOTLOADER_BUILD) set_property(TARGET ${COMPONENT_LIB} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "-Wl,--gc-sections") else() # Regular app build - idf_component_register(SRCS "src/dbg_stubs.c" - "src/esp_err_to_name.c" - "src/esp_timer.c" - "src/ets_timer_legacy.c" - "src/freertos_hooks.c" - "src/ipc.c" - "src/pm_locks.c" - "src/stack_check.c" + set(srcs "src/dbg_stubs.c" + "src/esp_err_to_name.c" + "src/esp_timer.c" + "src/ets_timer_legacy.c" + "src/freertos_hooks.c" + "src/pm_locks.c" + "src/stack_check.c") + + # IPC framework is not applicable if freertos unicore config is selected + if(NOT CONFIG_FREERTOS_UNICORE) + list(APPEND srcs "src/ipc.c") + endif() + + idf_component_register(SRCS "${srcs}" INCLUDE_DIRS include PRIV_REQUIRES soc) diff --git a/components/esp_common/component.mk b/components/esp_common/component.mk index 64c67abcc5..4d80bc4bf6 100644 --- a/components/esp_common/component.mk +++ b/components/esp_common/component.mk @@ -5,5 +5,10 @@ COMPONENT_ADD_INCLUDEDIRS := include COMPONENT_SRCDIRS := src +# IPC framework is not applicable if freertos unicore config is selected +ifdef CONFIG_FREERTOS_UNICORE +COMPONENT_OBJEXCLUDE := src/ipc.o +endif + # disable stack protection in files which are involved in initialization of that feature src/stack_check.o: CFLAGS := $(filter-out -fstack-protector%, $(CFLAGS)) From de682a13b152b505b4cc280fccad171fd8ed7fca Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Wed, 26 Jun 2019 02:41:19 +0800 Subject: [PATCH 062/146] examples: Update system examples README The following commit updates the first half of the the system example README files. Some other changes were also made: * Updated base_mac_address example * Moved contents in GCOV README to GCOV docs * Some *main.c file names updated * Updated example README template --- docs/TEMPLATE_EXAMPLE_README.md | 4 + docs/en/api-guides/app_trace.rst | 167 ++++++++++ examples/system/app_trace_to_host/README.md | 296 +++++++++--------- examples/system/base_mac_address/README.md | 93 +++++- .../base_mac_address/main/Kconfig.projbuild | 38 ++- .../main/base_mac_address_example_main.c | 100 ++++-- examples/system/console/README.md | 91 ++++-- examples/system/cpp_exceptions/README.md | 16 +- examples/system/cpp_pthread/README.md | 49 ++- examples/system/deep_sleep/README.md | 91 +++++- .../esp_event/default_event_loop/README.md | 121 ++++--- .../esp_event/user_event_loops/README.md | 70 +++-- examples/system/esp_timer/README.md | 211 +++++++++---- .../system/freertos/real_time_stats/README.md | 27 +- .../real_time_stats/main/CMakeLists.txt | 2 +- ...{main.c => real_time_stats_example_main.c} | 0 examples/system/gcov/README.md | 224 ++++++------- examples/system/gcov/main/CMakeLists.txt | 5 +- .../{gcov_example.c => gcov_example_main.c} | 0 examples/system/himem/README.md | 31 +- examples/system/himem/main/CMakeLists.txt | 2 +- ...himem_test_main.c => himem_example_main.c} | 0 examples/system/light_sleep/README.md | 10 +- 23 files changed, 1111 insertions(+), 537 deletions(-) rename examples/system/freertos/real_time_stats/main/{main.c => real_time_stats_example_main.c} (100%) rename examples/system/gcov/main/{gcov_example.c => gcov_example_main.c} (100%) rename examples/system/himem/main/{himem_test_main.c => himem_example_main.c} (100%) diff --git a/docs/TEMPLATE_EXAMPLE_README.md b/docs/TEMPLATE_EXAMPLE_README.md index c641f8d64d..06b558028e 100644 --- a/docs/TEMPLATE_EXAMPLE_README.md +++ b/docs/TEMPLATE_EXAMPLE_README.md @@ -57,3 +57,7 @@ _For examples where ESP32 is connected with some other hardware, include a tabl ## Troubleshooting _If there are any likely problems or errors which many users might encounter, mention them here. Remove this section for very simple examples where nothing is likely to go wrong._ + +## Example Breakdown + +_If the example source code is lengthy, complex, or cannot be easily understood, use this section to break down and explain the source code. This can be done by breaking down the execution path step by step, or explaining what each major function/task/source file does. Add sub titles if necessary. Remove this section for very simple examples where the source code is self explanatory._ \ No newline at end of file diff --git a/docs/en/api-guides/app_trace.rst b/docs/en/api-guides/app_trace.rst index ccc9379de5..be431a0428 100644 --- a/docs/en/api-guides/app_trace.rst +++ b/docs/en/api-guides/app_trace.rst @@ -12,6 +12,7 @@ Developers can use this library to send application specific state of execution 1. Collecting application specific data, see :ref:`app_trace-application-specific-tracing` 2. Lightweight logging to the host, see :ref:`app_trace-logging-to-host` 3. System behavior analysis, see :ref:`app_trace-system-behaviour-analysis-with-segger-systemview` +4. Source code coverage, see :ref:`app_trace-gcov-source-code-coverage` Tracing components when working over JTAG interface are shown in the figure below. @@ -434,3 +435,169 @@ After installing Impulse and ensuring that it can successfully load trace files .. note:: If you have problems with visualization (no data are shown or strange behavior of zoom action is observed) you can try to delete current signal hierarchy and double click on the necessary file or port. Eclipse will ask you to create new signal hierarchy. + + +.. _app_trace-gcov-source-code-coverage: + +Gcov (Source Code Coverage) +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Basics of Gcov and Lcov +""""""""""""""""""""""" + +Source code coverage is data indicating the count and frequency of every program execution path that has been taken within a program's runtime. `Gcov `_ is a GCC tool that, when used in concert with the compiler, can generate log files indicating the execution count of each line of a source file. The Lcov tool is similar to Gcov but is a graphical front end for Gcov, and generates code coverage reports in HTML format. + +Generally, using Gcov to compile and run programs on the Host will undergo these steps: + +1. Compile the source code using GCC with the ``--coverage`` option enabled. This will cause the compiler to generate a ``.gcno`` notes files during compilation. The notes files contain information to reconstruct execution path block graphs and map each block to source code line numbers. Each source file compiled with the ``--coverage`` option should have their own ``.gcno`` file of the same name (e.g., a ``main.c`` will generate a ``main.gcno`` when compiled). +2. Execute the program. During execution, the program should generate ``.gcda`` data files. These data files contain the counts of the number of times an execution path was taken. The program will generate a ``.gcda`` file for each source file compiled with the ``--coverage`` option (e.g., ``main.c`` will generate a ``main.gcda``. +3. Gcov or Lcov can be used generate a code coverage based on the ``.gcno``, ``.gcda``, and source files. Gcov will generate a text based coverage report for each source file in the form of a ``.gcov`` file, whilst Lcov will generate a coverage report in HTML format. + +Gcov and Lcov in ESP-IDF +"""""""""""""""""""""""" + +Using Gcov in ESP-IDF is complicated by the fact that the program is running remotely from the Host (i.e., on the target). The code coverage data (i.e., the ``.gcda`` files) is initially stored on the target itself. OpenOCD is then used to dump the code coverage data from the target to the host via JTAG during runtime. Using Gcov in ESP-IDF can be split into the following steps. + +1. :ref:`app_trace-gcov-setup-project` +2. :ref:`app_trace-gcov-dumping-data` +3. :ref:`app_trace-gcov-generate-report` + +.. _app_trace-gcov-setup-project: + +Setting Up a Project for Gcov +""""""""""""""""""""""""""""" + +Compiler Option +~~~~~~~~~~~~~~~ + +In order to obtain code coverage data in a project, one or more source files within the project must be compiled with the ``--coverage`` option. In ESP-IDF, this can be achieved at the component level or the individual source file level: + +To cause all source files in a component to be compiled with the ``--coverage`` option. + - Add ``target_compile_options(${COMPONENT_LIB} PRIVATE --coverage)`` to the ``CMakeLists.txt`` file of the component if using CMake. + - Add ``CFLAGS += --coverage`` to the ``component.mk`` file of the component if using Make. + +To cause a select number of source files (e.g. ``sourec1.c`` and ``source2.c``) in the same component to be compiled with the ``--coverage`` option. + - Add ``set_source_files_properties(source1.c source2.c PROPERTIES COMPILE_FLAGS --coverage)`` to the ``CMakeLists.txt`` file of the component if using CMake. + - Add ``source1.o: CFLAGS += --coverage`` and ``source2.o: CFLAGS += --coverage`` to the ``component.mk`` file of the component if using Make. + +When a source file is compiled with the ``--coverage`` option (e.g. ``gcov_example.c``), the compiler will generate the ``gcov_example.gcno`` file in the project's build directory. + +Project Configuration +~~~~~~~~~~~~~~~~~~~~~ + +Before building a project with source code coverage, ensure that the following project configuration options are enabled by running ``idf.py menuconfig`` (or ``make menuconfig`` if using the legacy Make build system). + +- Enable the application tracing module by choosing *Trace Memory* for the :ref:`CONFIG_ESP32_APPTRACE_DESTINATION` option. +- Enable Gcov to host via the :ref:`CONFIG_ESP32_GCOV_ENABLE` + +.. _app_trace-gcov-dumping-data: + +Dumping Code Coverage Data +"""""""""""""""""""""""""" + +Once a project has been complied with the ``--coverage`` option and flashed onto the target, code coverage data will be stored internally on the target (i.e., in trace memory) whilst the application runs. The process of transferring code coverage data from the target to the Host is know as dumping. + +The dumping of coverage data is done via OpenOCD (see :doc:`JTAG Debugging <../api-guides/jtag-debugging/index>` on how to setup and run OpenOCD). A dump is triggered by issuing commands to OpenOCD, therefore a telnet session to OpenOCD must be opened to issue such commands (run ``telnet localhost 4444``). Note that GDB could be used instead of telnet to issue commands to OpenOCD, however all commands issued from GDB will need to be prefixed as ``mon ``. + +When the target dumps code coverage data, the ``.gcda`` files are stored in the project's build directory. For example, if ``gcov_example_main.c`` of the ``main`` component was compiled with the ``--coverage`` option, then dumping the code coverage data would generate a ``gcov_example_main.gcda`` in ``build/esp-idf/main/CMakeFiles/__idf_main.dir/gcov_example_main.c.gcda`` (or ``build/main/gcov_example_main.gcda`` if using the legacy Make build system). Note that the ``.gcno`` files produced during compilation are also placed in the same directory. + +The dumping of code coverage data can be done multiple times throughout an application's life time. Each dump will simply update the ``.gcda`` file with the newest code coverage information. Code coverage data is accumulative, thus the newest data will contain the total execution count of each code path over the application's entire lifetime. + +ESP-IDF supports two methods of dumping code coverage data form the target to the host: + +* Instant Run-Time Dump +* Hard-coded Dump + +Instant Run-Time Dump +~~~~~~~~~~~~~~~~~~~~~ + +An Instant Run-Time Dump is triggered by calling the ``esp32 gcov`` OpenOCD command (via a telnet session). Once called, OpenOCD will immediately preempt the ESP32's current state and execute a builtin IDF Gcov debug stub function. The debug stub function will handle the dumping of data to the Host. Upon completion, the ESP32 will resume it's current state. + +.. note:: + Due to the use of the debug stub function, the OpenOCD Debug Stub option must be enabled in project configuration. The option can be found under ``Component config -> ESP32-specific -> OpenOCD debug stubs``. + +Hard-coded Dump +~~~~~~~~~~~~~~~ + +A Hard-coded Dump is triggered by the application itself by calling :cpp:func:`esp_gcov_dump` from somewhere within the application. When called, the application will halt and wait for OpenOCD to connect and retrieve the code coverage data. Once :cpp:func:`esp_gcov_dump` is called, the Host must execute the ``esp32 gcov dump`` OpenOCD command (via a telnet session). The ``esp32 gcov dump`` command will cause OpenOCD to connect to the ESP32, retrieve the code coverage data, then disconnect from the ESP32 thus allowing the application to resume. Hard-coded Dumps can also be triggered multiple times throughout an application's lifetime. + +Hard-coded dumps are useful if code coverage data is required at certain points of an application's lifetime by placing :cpp:func:`esp_gcov_dump` where necessary (e.g., after application initialization, during each iteration of an application's main loop). + +GDB can be used to set a breakpoint on :cpp:func:`esp_gcov_dump`, then call ``mon esp32 gcov dump`` automatically via the use a ``gdbinit`` script (see Using GDB from :ref:`jtag-debugging-using-debugger-command-line`). + +The following GDB script is will add a breakpoint at :cpp:func:`esp_gcov_dump`, then call the ``mon esp32 gcov dump`` OpenOCD command. + +.. code-block:: none + + b esp_gcov_dump + commands + mon esp32 gcov dump + end + + +.. note:: + Note that all OpenOCD commands should be invoked in GDB as: ``mon ``. + +.. _app_trace-gcov-generate-report: + +Generating Coverage Report +"""""""""""""""""""""""""" + +Once the code coverage data has been dumped, the ``.gcno``, ``.gcda`` and the source files can be used to generate a code coverage report. A code coverage report is simply a report indicating the number of times each line in a source file has been executed. + +Both Gcov and Lcov (along with genhtml) can be used to generate code coverage reports. Gcov is provided along with the Xtensa toolchian, whilst Lcov may need to be installed separately. For details on how to use Gcov or Lcov, refer to `Gcov documentation `_ and `Lcov documentation `_. + +.. note:: + There is currently no support for Lcov for the CMake build system on Windows. + +Adding Lcov Build Target to Project +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To make report generation more convenient, users can define additional build targets in their projects such report generation can be done with a single build command. + +CMake Build System +****************** + +For the CMake build systems, add the following lines to the ``CMakeLists.txt`` file of your project. + +.. code-block:: none + + include($ENV{IDF_PATH}/tools/cmake/gcov.cmake) + idf_create_coverage_report(${CMAKE_CURRENT_BINARY_DIR}/coverage_report) + idf_clean_coverage_report(${CMAKE_CURRENT_BINARY_DIR}/coverage_report) + +The following commands can now be used: + + * ``cmake --build build/ --target lcov-report`` will generate an HTML coverga report in ``$(BUILD_DIR_BASE)/coverage_report/html`` directory. + * ``cmake --build build/ --target cov-data-clean`` will remove all coverage data files and report. + + + +Make Build System +***************** + +For the Make build systems, add the following lines to the ``Makefile`` of your project. + +.. code-block:: none + + GCOV := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))gcov + REPORT_DIR := $(BUILD_DIR_BASE)/coverage_report + + lcov-report: + echo "Generating coverage report in: $(REPORT_DIR)" + echo "Using gcov: $(GCOV)" + mkdir -p $(REPORT_DIR)/html + lcov --gcov-tool $(GCOV) -c -d $(BUILD_DIR_BASE) -o $(REPORT_DIR)/$(PROJECT_NAME).info + genhtml -o $(REPORT_DIR)/html $(REPORT_DIR)/$(PROJECT_NAME).info + + cov-data-clean: + echo "Remove coverage data files..." + find $(BUILD_DIR_BASE) -name "*.gcda" -exec rm {} + + rm -rf $(REPORT_DIR) + + .PHONY: lcov-report cov-data-clean + +The following commands can now be used: + + * ``make lcov-report`` will generate an HTML coverga report in ``$(BUILD_DIR_BASE)/coverage_report/html`` directory. + * ``make cov-data-clean`` will remove all coverage data files and report. diff --git a/examples/system/app_trace_to_host/README.md b/examples/system/app_trace_to_host/README.md index aa5786242d..6a361bd25f 100644 --- a/examples/system/app_trace_to_host/README.md +++ b/examples/system/app_trace_to_host/README.md @@ -1,97 +1,45 @@ -# Example: Application Level Tracing - Logging to Host (app_trace_to_host) +# Application Level Tracing Example (Logging to Host) (See the README.md file in the upper level 'examples' directory for more information about examples.) -## Overview +This example demonstrates how to use the [Application Level Tracing Library](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html#) (henceforth referred to as **App Trace**) to log messages to a host via JTAG instead of the normal method of logging via UART. -This example will show how to perform high speed logging by redirecting the log messages to a host instead of UART. +UART logs are time consuming and can significantly slow down the function that calls it. Therefore, it is generally a bad idea to use UART logs in time-critical functions. Logging to host via JTAG is significantly faster and can be used in time-critical functions. For more details regarding logging to host via JTAG, refer to the [Logging to Host Documentation](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html#app-trace-logging-to-host). -For more description of [logging to host](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html#logging-to-host) please refer to **ESP-IDF Programming Guide**, section **Application Level Tracing library**. The following example provides practical implementation of this functionality. +This example demonstrates JTAG logging to host in the context of polling for a [zero crossing](https://en.wikipedia.org/wiki/Zero_crossing). The ESP32 will continuously sample a 50 to 60 Hz sinusoidal signal (using the ADC) and log the sampled values (via JTAG). Due to the higher speed of JTAG logging, the polling rate of the ADC should be high enough to detect a zero crossing. - -### Use Case - -Debugging of time critical functions may not work as desired if log messages are sent through the UART port. Printing out the logs may considerably slow down tested function to the point where it will not operate as expected. - -Let's consider a case we are testing implementation of [zero level crossing](https://en.wikipedia.org/wiki/Zero_crossing) detection for a 50 Hz signal with ESP32's ADC. - -We will start by checking if we can read ADC, what is the signal level and how many samples can be collected over 20 ms period by using a code snippet below: - -```c -int sampling_period = 20; -int i = 0; -uint32_t sampling_start = esp_log_timestamp(); //this clock counts miliseconds -do { - ESP_LOGI(TAG, "Sample:%d, Value:%d", ++i, adc1_get_raw(ADC1_TEST_CHANNEL)); -} while (esp_log_timestamp() - sampling_start < sampling_period); -``` - -Above code is sampling ADC in a loop continuously for 20 ms of time to collect one full period of 50 Hz signal (use 17 ms for 60 Hz signal), and printing out results. If you run this code, result will be likely as below: - -```bash -I (4309) example: Sample:1, Value:2224 -I (4309) example: Sample:2, Value:840 -I (4309) example: Sample:3, Value:3503 -I (4319) example: Sample:4, Value:27 -I (4319) example: Sample:5, Value:4095 -``` - -As you see we were able to collect only five samples. This seems rather not adequate for zero crossing detection. - -We can remove `ESP_LOGI()` line and sample much faster, but then will not be able to see the values. To see the values we would need to save them in the memory and print out later. - -Instead of saving samples to memory, a simple and compelling solution to this issue is sending logs to a host over a JTAG interface. This works much faster than UART. To do so, we need to redirect log messages to JTAG by calling the following function before `ESP_LOGx` line: - -```c -esp_log_set_vprintf(esp_apptrace_vprintf); -``` - -Once time critical messages are sent out, we can redirect `ESP_LOGx` back back to the UART by adding extra two lines of code. - -```c -esp_log_set_vprintf(vprintf); -esp_apptrace_flush(ESP_APPTRACE_DEST_TRAX, 100000); -``` - -Note command `esp_apptrace_flush()` used to empty buffer with pending messages to the host. - -If checking the log received by the host, we will see over 400 samples collected! - -```bash -... -I (59379) example: Sample:424, Value:298 -I (59379) example: Sample:425, Value:345 -I (59379) example: Sample:426, Value:386 -I (59379) example: Sample:427, Value:432 -I (61409) example: Sample:1, Value:2653 -``` +This example utilizes the following ESP-IDF features: +* [DAC driver](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/dac.html) to generate the 50 Hz sinusoidal signal. +* [ADC driver](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/adc.html) to sample the sinusoidal signal. +* [Application Level Tracing Library](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html#) to log ADC samples to host. +* [OpenOCD](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html#setup-of-openocd) to interface with the ESP32 and receive the log output over JTAG. ## How to use example ### Hardware Required -To run this example, you need an ESP32 dev board (e.g. [ESP-WROVER-KIT](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html#esp-wrover-kit-v4-1)) which integrates an on-board JTAG adapter. ESP32 core board (e.g. ESP32-DevKitC) can also work as long as you connect it to an external JTAG adapter (e.g. FT2232H, J-LINK). For convenience, here we will take the [ESP-WROVER-KIT](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html#esp-wrover-kit-v4-1) for example. +To run this example, you need an ESP32 dev board connected to a JTAG adapter, which can come in the following forms: + +* [ESP-WROVER-KIT](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html#esp-wrover-kit-v4-1) which integrates an on-board JTAG adapter. Ensure that the [required jumpers to enable JTAG are connected](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/get-started-wrover-kit.html#setup-options) on the WROVER-KIT. +* ESP32 core board (e.g. ESP32-DevKitC) can also work as long as you connect it to an external JTAG adapter (e.g. FT2232H, J-LINK). + +This example will assume that that an ESP-WROVER-KIT is used. #### Pin Assignment: -**Note:** Connect 50 / 60 Hz sinusoidal signal to ADC1_CHANNEL_6 / GPIO34. The signal range should be from 0V to max 3.1V. Optionally you can bridge GPIO34 with DAC_CHANNEL_1 / GPIO25. +The sinusoidal signal of 50 to 60 Hz ranging from 0 V ~ 3.1 V should be input into `GPIO34` (`ADC1_CHANNEL_6`). Users may provide this signal themselves, our use the DAC generated signal by bridging GPIO34 with `GPIO25` (`DAC_CHANNEL_1`). -| DAC Output | ADC Input | -| ----------------- | ----------------- | -| Channel1 (GPIO25) | Channel6 (GPIO34) | +| DAC Output | ADC Input | +| ------------------ | ------------------ | +| Channel 1 (GPIO25) | Channel 6 (GPIO34) | #### Extra Connections: -1. Connect JTAG interface to ESP32 board, power up both JTAG and ESP32. For details about how to set up JTAG interface, please see [JTAG Debugging](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html). +1. Connect the JTAG interface to ESP32 board, and power up both the JTAG and ESP32. For details about how to set up JTAG interface, please see [JTAG Debugging](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html). -2. After connecting JTAG interface, you need to [Run OpenOCD](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html#run-openocd). If you are using the [Binary Distribution of OpenOCD](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html#jtag-debugging-setup-openocd) and one of versions of [ESP-WROVER-KIT](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html#esp-wrover-kit-v4-1), respective command line will look as follows: +2. After connecting JTAG interface, you need to [Run OpenOCD](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html#run-openocd). - ```bash - cd ~/esp/openocd-esp32 - bin/openocd -s share/openocd/scripts -f interface/ftdi/esp32_devkitj_v1.cfg -f board/esp-wroom-32.cfg - ``` - -3. Open a separate terminal window and run telnet by entering: +3. Open a separate terminal window and run telnet by entering the command below. The telnet terminal window is used to feed commands to OpenOCD: ```bash telnet localhost 4444 @@ -99,84 +47,39 @@ To run this example, you need an ESP32 dev board (e.g. [ESP-WROVER-KIT](https:// ### Configure the project -Open the project configuration menu (`idf.py menuconfig`). Then go into `Example Configuration` menu. +``` +idf.py menuconfig +``` -- By default, the DAC will generate 130 Hz signal within 0V..3.1V. To get 50Hz, you need to set non-standard driver of RTC 8MHz clock to lower minimum CW (Cosine Waveform) generator's frequency in `Set custom RTC 8 MHz clock divider to lower CW frequency`. +* By default, the DAC will generate 130 Hz signal ranging from 0 V ~ 3.1 V. To generate a 50 Hz signal, the RTC 8 MHz clock will need to use a non-standard divider. This is achieved by enabling the `Example Configuration > Set custom RTC 8 MHz clock divider to lower CW frequency` configuration option. -**Note** To enable application tracing, go to `Component config > Application Level Tracing` and select `(X) Trace memory`. (By default, this option has been switched on in the sdkconfig.defaults). +* To enable application tracing, select the `(X) Trace memory` option under `Component config > Application Level Tracing`. This option should have been selected by default. -### Build, Flash and Run +### Build, Flash, and Run -1. Run `idf.py -p PORT flash monitor` to build and flash the project.. (To exit the serial monitor, type ``Ctrl-]``.) -See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects. +Build the project and flash it to the board, then run monitor tool to view serial output: -2. In the telnet session window, run the following command. This command will collect 9000 bytes of log data and save them to `adc.log` file in `~/esp/openocd-esp32` folder. +``` +idf.py -p PORT flash monitor +``` - ```bash - esp32 apptrace start file://adc.log 0 9000 5 0 0 - ``` +(Replace PORT with the name of the serial port to use.) -3. Decode and print out retrieved log file by executing: +**Start App Trace:** In the telnet session window, trigger OpenOCD to start App Trace on the ESP32 by entering the command below. This command will collect 9000 bytes of JTAG log data and save them to `adc.log` file in `~/esp/openocd-esp32` folder. - ```bash - $IDF_PATH/tools/esp_app_trace/logtrace_proc.py ~/esp/openocd-esp32/adc.log ~/esp/app_trace_to_host/build/app_trace_to_host_test.elf - ``` +```bash +esp32 apptrace start file://adc.log 0 9000 5 0 0 +``` - This should provide a similar output: +**Note:** For more details on OpenOCD commands regarding App Trace, refer to the [OpenOCD Application Level Tracing Commands](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html#openocd-application-level-tracing-commands) - ``` - Parse trace file '/user-home/esp/openocd-esp32/adc.log'... - Unprocessed 7 bytes of log record args! - Parsing completed. - ==================================================================== - I (59369) example: Sample:1, Value:3717 - I (59369) example: Sample:2, Value:3647 - I (59369) example: Sample:3, Value:3575 - I (59369) example: Sample:4, Value:3491 - ... +(To exit the serial monitor, type ``Ctrl-]``.) - I (59379) example: Sample:398, Value:78 - I (59379) example: Sample:399, Value:58 - I (59379) example: Sample:400, Value:22 - I (59379) example: Sample:401, Value:14 - I (59379) example: Sample:402, Value:0 - I (59379) example: Sample:403, Value:0 - I (59379) example: Sample:404, Value:0 - I (59379) example: Sample:405, Value:0 - I (59379) example: Sample:406, Value:0 - I (59379) example: Sample:407, Value:0 - I (59379) example: Sample:408, Value:0 - I (59379) example: Sample:409, Value:0 - I (59379) example: Sample:410, Value:0 - I (59379) example: Sample:411, Value:0 - I (59379) example: Sample:412, Value:0 - I (59379) example: Sample:413, Value:0 - I (59379) example: Sample:414, Value:16 - I (59379) example: Sample:415, Value:32 - I (59379) example: Sample:416, Value:40 - I (59379) example: Sample:417, Value:74 - I (59379) example: Sample:418, Value:89 - I (59379) example: Sample:419, Value:113 - I (59379) example: Sample:420, Value:160 - I (59379) example: Sample:421, Value:192 - I (59379) example: Sample:422, Value:221 - I (59379) example: Sample:423, Value:256 - I (59379) example: Sample:424, Value:298 - I (59379) example: Sample:425, Value:345 - I (59379) example: Sample:426, Value:386 - I (59379) example: Sample:427, Value:432 - I (61409) example: Sample:1, Value:2653 - - ==================================================================== - - Log records count: 428 - ``` - -This is the log we have been looking for, complete with timestamps as if printed to UART, but almost two orders of magnitude faster. +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. ## Example Output -Check the full example code [app_trace_to_host](main/app_trace_to_host_test.c) that combines both tests above and runs them in a loop showing instantly the number of samples collected: +The example will continuously sample the ADC for 20ms per iteration, and will alternate between JTAG and UART logging per iteration. However, the JTAG logs should be captured by OpenOCD, thus will not appear in the monitor's output. Therefore, the monitor should only display the iterations where UART logging was used (i.e. every alternate iteration) such as the following: ``` I (4289) example: Sampling ADC and sending data to the host... @@ -191,15 +94,120 @@ I (4319) example: Sample:5, Value:4095 I (4329) example: Collected 5 samples in 20 ms. ``` -**note** Above result is generated under CPU running at 240 MHz. +**Note:** The UART log above was produced with the CPU running at 240 MHz. -## Conclusion +To access the JTAG logs, the `adc.log` file should be decoded. This can be done by using the `logtrace_proc.py` script as such: -With this example code we have demonstrated powerful functionality of logging to host with JTAG interface. With standard UART communication baud rate setting of 115200, printing out a single line of a log message takes about 4 ms. This is also the maximum period we can sequentially execute other tasks in between. By providing the same logging over JTAG, we will be able to improve performance of this process over 80 times. +```bash +$IDF_PATH/tools/esp_app_trace/logtrace_proc.py ~/esp/openocd-esp32/adc.log ~/esp/app_trace_to_host/build/app_trace_to_host_test.elf +``` + +The `logtrace_proc.py` script should produce the following output when decoding: + +``` +Parse trace file '/user-home/esp/openocd-esp32/adc.log'... +Unprocessed 7 bytes of log record args! +Parsing completed. +==================================================================== +I (59369) example: Sample:1, Value:3717 +I (59369) example: Sample:2, Value:3647 +I (59369) example: Sample:3, Value:3575 +I (59369) example: Sample:4, Value:3491 +... + +I (59379) example: Sample:398, Value:78 +I (59379) example: Sample:399, Value:58 +I (59379) example: Sample:400, Value:22 +I (59379) example: Sample:401, Value:14 +I (59379) example: Sample:402, Value:0 +I (59379) example: Sample:403, Value:0 +I (59379) example: Sample:404, Value:0 +I (59379) example: Sample:405, Value:0 +I (59379) example: Sample:406, Value:0 +I (59379) example: Sample:407, Value:0 +I (59379) example: Sample:408, Value:0 +I (59379) example: Sample:409, Value:0 +I (59379) example: Sample:410, Value:0 +I (59379) example: Sample:411, Value:0 +I (59379) example: Sample:412, Value:0 +I (59379) example: Sample:413, Value:0 +I (59379) example: Sample:414, Value:16 +I (59379) example: Sample:415, Value:32 +I (59379) example: Sample:416, Value:40 +I (59379) example: Sample:417, Value:74 +I (59379) example: Sample:418, Value:89 +I (59379) example: Sample:419, Value:113 +I (59379) example: Sample:420, Value:160 +I (59379) example: Sample:421, Value:192 +I (59379) example: Sample:422, Value:221 +I (59379) example: Sample:423, Value:256 +I (59379) example: Sample:424, Value:298 +I (59379) example: Sample:425, Value:345 +I (59379) example: Sample:426, Value:386 +I (59379) example: Sample:427, Value:432 +I (61409) example: Sample:1, Value:2653 + +==================================================================== + +Log records count: 428 +``` ## Troubleshooting -1. I can not flash new firmware when OpenOCD is connected to ESP32. - * One likely cause would be that you set wrong SPI flash voltage when you start OpenOCD. Suppose you're working with an ESP32 board / module which has a 3.3V powered SPI flash, but you select -`board/esp32-wrover.cfg` configuration file when start OpenOCD. In this situation, you might not be able to flash ESP32 when OpenOCD is connected. So make sure what the working voltage of the SPI flash is. Currently, for 1.8V flash, we'd like to suggest using `board/esp32-wrover.cfg` and for 3.3V flash, using `board/esp-wroom-32.cfg`. For more information about it, please refer to [ESP32 Modules and Boards](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html) and [Set SPI Flash Voltage](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/tips-and-quirks.html#why-to-set-spi-flash-voltage-in-openocd-configuration). + +### Unable to flash when OpenOCD is connected to ESP32 + +One likely cause would be an incorrect SPI flash voltage when starting OpenOCD. Suppose an ESP32 board/module with a 3.3 V powered SPI flash is being used, but the `board/esp32-wrover.cfg` configuration file is selected when starting OpenOCD which can set the SPI flash voltage to 1.8 V. In this situation, the SPI flash will not work after OpenOCD connects to the ESP32 as OpenOCD has changed the SPI flash voltage. Therefore, you might not be able to flash ESP32 when OpenOCD is connected. + +To work around this issue, users are suggested to use `board/esp32-wrover.cfg` for ESP32 boards/modules operating with an SPI flash voltage of 1.8 V, and `board/esp-wroom-32.cfg` for 3.3 V. Refer to [ESP32 Modules and Boards](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html) and [Set SPI Flash Voltage](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/tips-and-quirks.html#why-to-set-spi-flash-voltage-in-openocd-configuration) for more details. (For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.) + +The log should be identical to those printed via UART (complete with timestamps), but almost two orders of magnitude faster. + +## Example Breakdown + +The following code snippet demonstrates a loop of the sampling and logging the ADC over a 20 ms period in order to capture one full period of a 50 Hz signal. + +```c +int sampling_period = 20; +int i = 0; +uint32_t sampling_start = esp_log_timestamp(); //this clock counts milliseconds +do { + ESP_LOGI(TAG, "Sample:%d, Value:%d", ++i, adc1_get_raw(ADC1_TEST_CHANNEL)); +} while (esp_log_timestamp() - sampling_start < sampling_period); +``` + +If `ESP_LOGI()` is routed via UART (occurs by default), the log output produced will likely resemble the output shown below. Notice that due to UART logging is time consuming, thus the ADC is only sampled five times, which is too infrequent to consistently detect a zero crossing (where the zero crossing is `4096/2 = 2048` i.e., the mid point of the 12-bit ADC). + +```bash +I (4309) example: Sample:1, Value:2224 +I (4309) example: Sample:2, Value:840 +I (4309) example: Sample:3, Value:3503 +I (4319) example: Sample:4, Value:27 +I (4319) example: Sample:5, Value:4095 +I (4329) example: Collected 5 samples in 20 ms. +``` + +However, by logging via JTAG, the logging is much quicker hence allows a much higher sampling frequency (over 400 times) as shown the the log output below thus would be able to detect a zero crossing more consistently. + +```c +esp_log_set_vprintf(esp_apptrace_vprintf); +``` + +```bash +... + +I (59379) example: Sample:423, Value:256 +I (59379) example: Sample:424, Value:298 +I (59379) example: Sample:425, Value:345 +I (59379) example: Sample:426, Value:386 +I (59379) example: Sample:427, Value:432 +I (61409) example: Sample:1, Value:2653 + +==================================================================== + +Log records count: 428 + +``` + +This example has demonstrated powerful functionality of logging to host via JTAG interface. With standard UART communication at a baud rate of 115200, printing out a single line log message takes approximately 4 ms. This also means that logged tasks cannot run more frequently than every 4 ms. By providing the same logging over JTAG, logging performance is improved 80 fold. \ No newline at end of file diff --git a/examples/system/base_mac_address/README.md b/examples/system/base_mac_address/README.md index faf1bfaf48..aa4368b155 100644 --- a/examples/system/base_mac_address/README.md +++ b/examples/system/base_mac_address/README.md @@ -1,4 +1,93 @@ -# Example: base mac address +# Base MAC Address -This example illustrates how to get and set base MAC address. +(See the README.md file in the upper level 'examples' directory for more information about examples.) +Each network interface on the ESP32 (e.g., WiFi Station/AP, BT, Ethernet) must be assigned a unique Medium Access Control (MAC) address. Each interface's MAC address is [derived from a base MAC address](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/system.html#mac-address) that must be set before any network interface's initialization is called. + +This example demonstrates the following: + +1. How to retrieve a base MAC address from non-volatile memory +2. How to set the base MAC address +3. How to obtain the derived MAC addresses of each network interface + +This example utilizes the [MAC Address API](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/system.html#mac-address). + +## How to use example + +### Hardware Required + +This example should be able to run on any commonly available ESP32 development board. However, if an external source of non-volatile memory is used to store the base MAC address (e.g. an I2C Serial EEPROM), the user should wire the connections to their chosen external storage as needed. + +### Configure the project + +``` +idf.py menuconfig +``` + +* To select the storage source of the base MAC address, go to `Example Configuration > Storage location of the base MAC address` where the following options are available: + * `eFuse BLK0` will cause this example to read the base MAC address from eFuse Block 0 and is selected by default. The `eFuse BLK0` MAC address is factory programmed into each ESP32. + * `eFuse BLK3` will cause this example to read the base MAC address from words 1 & 2 of eFuse Block 3. `eFuse BLK3` allows users to use a custom eFuse, but must be burned into the ESP32 using the [espefuse tool](https://github.com/espressif/esptool/wiki/espefuse). Attempting to read `eFuse BLK3` without burning the eFuse will result in an error. + * `Other external storage` will call a `external_storage_mac_get()` which will merely retrieve an arbitrary base MAC address that is set in software. Users should re-implement `external_storage_mac_get()` to access their chosen external storage (e.g. an I2C Serial EEPROM) + +* If `eFuse BLK3` is chosen, the `Behavior when retrieving BLK3 eFuse fails` option may also be selected. When retrieving the base MAC address from `eFuse BLK3` without burning eFuse Block 3, the example can either abort or default to retrieving from eFuse Block 0. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +Depending on the `Storage of the base MAC address` configuration option, the console should output one of the following logs to indicate the source of the base MAC address. + +``` +I (288) BASE_MAC: Base MAC Address read from EFUSE BLK0 +... +I (288) BASE_MAC: Base MAC Address read from EFUSE BLK3 +... +I (288) BASE_MAC: Base MAC Address read from external storage +``` + +The console will also output the retrieved 6 byte MAC address when it sets it as the system wide base MAC address. +``` +I (298) BASE_MAC: Using "0xd8, 0xa0, 0x1d, 0x0, 0xb, 0x88" as base MAC address +``` + +The console will then output the derived MAC address of each network interface as such: +``` +I (297) WIFI_STA MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x55 +I (307) SoftAP MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x56 +I (317) BT MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x57 +I (317) Ethernet MAC: 0x0, 0x11, 0x22, 0x33, 0x44, 0x58 +``` + +## Troubleshooting + +When attempting to retrieve the base MAC address from `eFuse BLK3` without first burning eFuse Block 3 will result in one of the following situations. + +If the example has been configured to abort when retrieving from `eFuse BLK3` fails. + +``` +E (288) system_api: Base MAC address from BLK3 of EFUSE version error, version = 0 +E (298) BASE_MAC: Failed to get base MAC address from EFUSE BLK3. (ESP_ERR_INVALID_VERSION) +E (308) BASE_MAC: Aborting +abort() was called at PC 0x400d237b on core 0 +``` + +If not configured to abort, the example will default to `eFuse BLK0` and output the following instead. + +``` +E (288) system_api: Base MAC address from BLK3 of EFUSE version error, version = 0 +E (298) BASE_MAC: Failed to get base MAC address from EFUSE BLK3. (ESP_ERR_INVALID_VERSION) +I (308) BASE_MAC: Defaulting to base MAC address in BLK0 of EFUSE +I (308) BASE_MAC: Base MAC Address read from EFUSE BLK0 +``` \ No newline at end of file diff --git a/examples/system/base_mac_address/main/Kconfig.projbuild b/examples/system/base_mac_address/main/Kconfig.projbuild index f64f01bf25..ce787cdb98 100644 --- a/examples/system/base_mac_address/main/Kconfig.projbuild +++ b/examples/system/base_mac_address/main/Kconfig.projbuild @@ -1,19 +1,22 @@ menu "Example Configuration" choice BASE_MAC_ADDRESS_STORAGE - prompt "Storage of the base MAC address" + prompt "Storage location of the base MAC address" default BASE_MAC_STORED_EFUSE_BLK0 help - Select storage of the base MAC address which is used to generate MAC addresses of all network interfaces - when networking is initialized. - If "Default (Espressif factory) eFuse BLK0" is selected, esp32 will use default base MAC address which is - written into eFuse block 0 words 1, 2 when the chip is manufactured. - If "Custom eFuse BLK3" is selected, ESP32 will use customer-defined base MAC address which is written into - eFuse Block 3 words 0, 1. Users must call esp_efuse_mac_get_custom to get base MAC address and - esp_base_mac_addr_set to set the base MAC address before network interfaces are initialised. - If "Other external storage" is selected, esp32 will use customer-defined base MAC address from external - storage(flash, EEPROM, etc). Users must get the base MAC address first and call esp_base_mac_addr_set to - set the base MAC address before network interfaces are initialised. + Select the storage location of the base MAC addresses. + 1. eFuse BLK0: The "Default (Espressif factory)" selection. The + default base MAC address is written to words 1 and 2 of eFuse block + 0 when the chip was manufactured. Call esp_efuse_mac_get_default() + read retrieve the "eFuse BLK0" MAC address. + 2. eFuse BLK3: A custom base MAC address is burned by the user into + eFuse word 0 of block 3. Call esp_efuse_mac_get_custom() to read + the "eFuse BLK3" MAC address. + 3. Other External Storage: Selecting this option will cause the + example to call external_storage_mac_get() which is defined in this + example to simply return a MAC address preset in software. Users + should modify this function to access their desired storage mediums + (e.g. flash, EEPROM etc). config BASE_MAC_STORED_EFUSE_BLK0 bool "Default (Espressif factory) eFuse BLK0" @@ -24,19 +27,20 @@ menu "Example Configuration" endchoice choice BASE_MAC_STORED_EFUSE_BLK3_ERROR_BEHAV - prompt "Read base MAC address from BLK3 of eFuse error behavior" + prompt "Behavior when retrieving eFuse BLK3 fails" depends on BASE_MAC_STORED_EFUSE_BLK3 default BASE_MAC_STORED_EFUSE_BLK3_ERROR_DEFAULT help - Select behavior when reading base MAC address from BLK3 of eFuse error. - If "Abort" is selected, esp32 will abort. - If "Use base MAC address from BLK3 of eFuse" is selected, esp32 will use the base MAC address which is - written into eFuse block 0 words 1, 2 when the chip is manufactured. + Select the behavior when reading base MAC address "eFuse BLK3" fails + (i.e. the retrieved result is all 0). + - If "Abort" is selected, the ESP32 will abort. + - If "Use the default base MAC address from BLK0 of eFuse" is + selected, the default "eFuse BLK0" will be used instead. config BASE_MAC_STORED_EFUSE_BLK3_ERROR_ABORT bool "Abort" config BASE_MAC_STORED_EFUSE_BLK3_ERROR_DEFAULT - bool "Use base MAC address from BLK3 of eFuse" + bool "Use the default base MAC address eFuse BLK0" endchoice endmenu diff --git a/examples/system/base_mac_address/main/base_mac_address_example_main.c b/examples/system/base_mac_address/main/base_mac_address_example_main.c index 7353ad76bf..60cdd71aa5 100644 --- a/examples/system/base_mac_address/main/base_mac_address_example_main.c +++ b/examples/system/base_mac_address/main/base_mac_address_example_main.c @@ -15,11 +15,13 @@ #define TAG "BASE_MAC" #ifdef CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE -/* This is an example to show getting base MAC address from other external storage (flash, EEPROM, etc). */ + static esp_err_t external_storage_mac_get(uint8_t *mac) { - uint8_t external_storage_mac_addr[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; - + /* This function simulates getting a base MAC address from external storage + * by simply setting the base MAC to an arbitrary address. Users should + * re-implement this function to access external storage (e.g. flash, EEPROM) */ + uint8_t external_storage_mac_addr[6] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; if (mac == NULL) { ESP_LOGE(TAG, "The mac parameter is NULL"); abort(); @@ -32,45 +34,75 @@ static esp_err_t external_storage_mac_get(uint8_t *mac) void app_main(void) { -#if defined(CONFIG_BASE_MAC_STORED_EFUSE_BLK3) || defined(CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE) - uint8_t mac_addr[8] = {0}; + //Get the base MAC address from different sources + uint8_t base_mac_addr[6] = {0}; esp_err_t ret = ESP_OK; - #ifdef CONFIG_BASE_MAC_STORED_EFUSE_BLK3 - /* Get base MAC address from BLK3 of EFUSE */ - ret = esp_efuse_mac_get_custom(mac_addr); + //Get base MAC address from EFUSE BLK3 + ret = esp_efuse_mac_get_custom(base_mac_addr); if (ret != ESP_OK) { - ESP_LOGE(TAG, "Get base MAC address from BLK3 of EFUSE error (%s)", esp_err_to_name(ret)); - /* If get custom base MAC address error, the application developer can decide what to do: - * abort or use the default base MAC address which is stored in BLK0 of EFUSE by doing - * nothing. - */ + ESP_LOGE(TAG, "Failed to get base MAC address from EFUSE BLK3. (%s)", esp_err_to_name(ret)); #ifdef CONFIG_BASE_MAC_STORED_EFUSE_BLK3_ERROR_ABORT + ESP_LOGE(TAG, "Aborting"); abort(); #else - ESP_LOGI(TAG, "Use base MAC address which is stored in BLK0 of EFUSE"); + ESP_LOGI(TAG, "Defaulting to base MAC address in BLK0 of EFUSE"); + esp_efuse_mac_get_default(base_mac_addr); + ESP_LOGI(TAG, "Base MAC Address read from EFUSE BLK0"); #endif//CONFIG_BASE_MAC_STORED_EFUSE_BLK3_ERROR_ABORT + } else { + ESP_LOGI(TAG, "Base MAC Address read from EFUSE BLK3"); } - else { - esp_base_mac_addr_set(mac_addr); - ESP_LOGI(TAG, "Use base MAC address which is stored in BLK3 of EFUSE"); +#elif defined(CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE) + //Get base MAC address from other external storage, or set by software + ret = external_storage_mac_get(base_mac_addr); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to get base MAC address from external storage. (%s)", esp_err_to_name(ret)); + ESP_LOGE(TAG, "Aborting"); + abort(); + } else { + ESP_LOGI(TAG, "Base MAC Address read from external storage"); } -#endif//CONFIG_BASE_MAC_STORED_EFUSE_BLK3 - -#ifdef CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE - /* the application developer can get base MAC address which is stored in other external - * storage (flash, EEPROM, etc) by calling some functions here. - */ - ret = external_storage_mac_get(mac_addr); - if (ret == ESP_OK) { - esp_base_mac_addr_set(mac_addr); - ESP_LOGI(TAG, "Use base MAC address which is stored in other external storage(flash, EEPROM, etc)"); - } - else { - ESP_LOGI(TAG, "Use base MAC address which is stored in BLK0 of EFUSE (%s)", esp_err_to_name(ret)); - } -#endif//CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE #else - ESP_LOGI(TAG, "Use base MAC address which is stored in BLK0 of EFUSE"); -#endif//CONFIG_BASE_MAC_STORED_EFUSE_BLK3 || CONFIG_BASE_MAC_STORED_OTHER_EXTERNAL_STORAGE + //Get base MAC address from EFUSE BLK0(default option) + ret = esp_efuse_mac_get_default(base_mac_addr); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to get base MAC address from EFUSE BLK0. (%s)", esp_err_to_name(ret)); + ESP_LOGE(TAG, "Aborting"); + abort(); + } else { + ESP_LOGI(TAG, "Base MAC Address read from EFUSE BLK0"); + } +#endif + + //Set the base MAC address using the retrieved MAC address + ESP_LOGI(TAG, "Using \"0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\" as base MAC address", + base_mac_addr[0], base_mac_addr[1], base_mac_addr[2], base_mac_addr[3], base_mac_addr[4], base_mac_addr[5]); + esp_base_mac_addr_set(base_mac_addr); + + //Get the derived MAC address for each network interface + uint8_t derived_mac_addr[6] = {0}; + //Get MAC address for WiFi Station interface + ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_WIFI_STA)); + ESP_LOGI("WIFI_STA MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x", + derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2], + derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]); + + //Get MAC address for SoftAp interface + ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_WIFI_SOFTAP)); + ESP_LOGI("SoftAP MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x", + derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2], + derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]); + + //Get MAC address for Bluetooth + ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_BT)); + ESP_LOGI("BT MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x", + derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2], + derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]); + + //Get MAC address for Ethernet + ESP_ERROR_CHECK(esp_read_mac(derived_mac_addr, ESP_MAC_ETH)); + ESP_LOGI("Ethernet MAC", "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x", + derived_mac_addr[0], derived_mac_addr[1], derived_mac_addr[2], + derived_mac_addr[3], derived_mac_addr[4], derived_mac_addr[5]); } diff --git a/examples/system/console/README.md b/examples/system/console/README.md index 9341a5ca37..695d319ef9 100644 --- a/examples/system/console/README.md +++ b/examples/system/console/README.md @@ -1,42 +1,42 @@ -# Console example +# Console Example -This example illustrates usage of `console` component to create an interactive shell. +(See the README.md file in the upper level 'examples' directory for more information about examples.) -## Configuring UART and VFS +This example illustrates the usage of the [Console Component](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/console.html#console) to create an interactive shell on the ESP32. The interactive shell running on the ESP32 can then be controlled/interacted with over a serial port (UART). -``initialize_console`` function configures some aspects of UART and VFS relevant to the operation of console: +The interactive shell implemented in this example contains a wide variety of commands, and can act as a basis for applications that require a command-line interface (CLI). -- By default `stdin` and `stdout` are buffered streams. This means that the text written to `stdout` will not be immediately sent to UART. This is not the desirable behavior for the console, so buffering for `stdin` and `stdout` is disabled using `setvbuf` function. +## How to use example -- Line endings are configured to match those expected/generated by common serial monitor programs, such as `screen`, `minicom`, and the `idf_monitor.py` included in the SDK. The default behavior for these commands is: - - When 'enter' key is pressed on the keyboard, `CR` (0x13) code is sent to the serial device. - - To move the cursor to the beginning of the next line, serial device needs to send `CR LF` (0x13 0x10) sequence. +### Hardware Required -- UART driver is initialized, and VFS is configured to use UART driver's interrupt-driver read and write functions. +This example should be able to run on any commonly available ESP32 development board. -## Line editing +### Configure the project -The main source file of the example illustrates how to use `linenoise` library, including line completion, hints, and history. +``` +idf.py menuconfig +``` -## Commands +* Enable/disable `Example Configuration > Store command history in flash` as necessary -Several commands are registered using `esp_console_cmd_register` function. See `register_wifi` and `register_system` functions in `cmd_wifi.c` and `cmd_system.c` files. +### Build and Flash -## Command handling +Build the project and flash it to the board, then run monitor tool to view serial output: -Main loop inside `app_main` function illustrates how to use `linenoise` and `esp_console_run` to implement read/eval loop. +``` +idf.py -p PORT flash monitor +``` -## Argument parsing +(Replace PORT with the name of the serial port to use.) -Several commands implemented in `cmd_wifi.c` and `cmd_system.c` use Argtable3 library to parse and check the arguments. +(To exit the serial monitor, type ``Ctrl-]``.) -## Command history +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. -Each time a new command line is obtained from `linenoise`, it is written into history and the history is saved into a file in flash memory. On reset, history is initialized from that file. +## Example Output -# Example output - -Here is an sample session with the console example. GPIO15 is connected to GND to remove boot log output. +Enter the `help` command get a full list of all available commands. The following is a sample session of the Console Example where a variety of commands provided by the Console Example are used. Note that GPIO15 is connected to GND to remove the boot log output. ``` This is an example of ESP-IDF console component. @@ -103,6 +103,49 @@ Press TAB when typing command name to auto-complete. ``` ---- +## Troubleshooting -See the README.md file in the upper level 'examples' directory for more information about examples. +### Line Endings + +The line endings in the Console Example are configured to match particular serial monitors. Therefore, if the following log output appears, consider using a different serial monitor (e.g. Putty for Windows) or modify the example's [UART configuration](#Configuring-UART-and-VFS). + +``` +This is an example of ESP-IDF console component. +Type 'help' to get the list of commands. +Use UP/DOWN arrows to navigate through command history. +Press TAB when typing command name to auto-complete. +Your terminal application does not support escape sequences. +Line editing and history features are disabled. +On Windows, try using Putty instead. +esp32> +``` + +## Example Breakdown + +### Configuring UART + +The ``initialize_console()`` function in the example configures some aspects of UART relevant to the operation of the console. + +- **Line Endings**: The default line endings are configured to match those expected/generated by common serial monitor programs, such as `screen`, `minicom`, and the `idf_monitor.py` included in the SDK. The default behavior for these commands are: + - When 'enter' key is pressed on the keyboard, `CR` (0x13) code is sent to the serial device. + - To move the cursor to the beginning of the next line, serial device needs to send `CR LF` (0x13 0x10) sequence. + +### Line editing + +The main source file of the example illustrates how to use `linenoise` library, including line completion, hints, and history. + +### Commands + +Several commands are registered using `esp_console_cmd_register()` function. See the `register_wifi()` and `register_system()` functions in `cmd_wifi.c` and `cmd_system.c` files. + +### Command handling + +Main loop inside `app_main()` function illustrates how to use `linenoise` and `esp_console_run()` to implement read/eval loop. + +### Argument parsing + +Several commands implemented in `cmd_wifi.c` and `cmd_system.c` use the Argtable3 library to parse and check the arguments. + +### Command history + +Each time a new command line is obtained from `linenoise`, it is written into history and the history is saved into a file in flash memory. On reset, history is initialized from that file. \ No newline at end of file diff --git a/examples/system/cpp_exceptions/README.md b/examples/system/cpp_exceptions/README.md index 0589d885df..7b49194131 100644 --- a/examples/system/cpp_exceptions/README.md +++ b/examples/system/cpp_exceptions/README.md @@ -6,12 +6,24 @@ This example demonstrates usage of C++ exceptions in ESP-IDF. By default, C++ exceptions support is disabled in ESP-IDF. It can be enabled using `CONFIG_COMPILER_CXX_EXCEPTIONS` configuration option. -In this example, `sdkconfig.defaults` file sets `CONFIG_COMPILER_CXX_EXCEPTIONS` option. This enables both compile time support (`-fexceptions` compiler flag) and run-time support for C++ exception handling. +In this example, the `sdkconfig.defaults` file sets the `CONFIG_COMPILER_CXX_EXCEPTIONS` option. This enables both compile time support (`-fexceptions` compiler flag) and run-time support for C++ exception handling. -Example source code declares a class which can throw exception from the constructor, depending on the argument. It illustrates that exceptions can be thrown and caught using standard C++ facilities. +The example source code declares a class which can throw exception from the constructor if the argument provided is equal to `0`. This is used to demonstrate that exceptions can be thrown and caught using standard C++ facilities. + +**Note: Due to the use of the C++ exceptions, this example is written in C++ instead of C.** ## How to use example +### Hardware Required + +This example should be able to run on any commonly available ESP32 development board. + +### Configure the project + +``` +idf.py menuconfig +``` + ### Build and Flash ``` diff --git a/examples/system/cpp_pthread/README.md b/examples/system/cpp_pthread/README.md index 4f925b7dbb..244036adb2 100644 --- a/examples/system/cpp_pthread/README.md +++ b/examples/system/cpp_pthread/README.md @@ -1,5 +1,48 @@ -# pthread examples +# C++ pthread Example -This example shows how to use the pthread API to create std::threads with different stack sizes, names, priorities and pinned to certain cores. +(See the README.md file in the upper level 'examples' directory for more information about examples.) -This example is in C++, contrary to the the normal standard of pure C. +Support for the [C++ threads](http://www.cplusplus.com/reference/thread/thread/) in ESP-IDF is implemented on top of the [ESP-pthread](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/esp_pthread.html#overview) component. Thus, C++ threads created using the standard thread class constructor will automatically inherit the current ESP-pthread configuration. This example demonstrates how to leverage the thread configuration functions provided by ESP-pthread (e.g., `esp_pthread_get_default_config()` and `esp_pthread_set_cfg()`) to modify the stack sizes, priorities, names, and core affinities of the C++ threads. + +**Note: Due to the use of the C++ threads, this example is written in C++ instead of C.** + +## How to use example + +### Hardware Required + +This example should be able to run on any commonly available ESP32 development board. + +### Configure the project + +``` +idf.py menuconfig +``` + +* The default ESP-pthread configuration may also be modified under `Component config > PThreads` + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +The following log output should appear when the example runs (note that the bootloader log has been omitted). + +``` +... +I (380) Thread 1: Core id: 0, prio: 5, minimum free stack: 2068 bytes. +I (0) pthread: This thread (with the default name) may run on any core.Core id: 1, prio: 5, minimum free stack: 2056 bytes. +I (390) Thread 1: This is the INHERITING thread with the same parameters as our parent, including name. Core id: 0, prio: 5, minimum free stack: 2092 bytes. +I (410) Thread 2: Core id: 1, prio: 5, minimum free stack: 2088 bytes. +I (410) main: core id: 0, prio: 1, minimum free stack: 2928 bytes. +``` diff --git a/examples/system/deep_sleep/README.md b/examples/system/deep_sleep/README.md index c25c316fa7..0f2e08e2d4 100644 --- a/examples/system/deep_sleep/README.md +++ b/examples/system/deep_sleep/README.md @@ -1,12 +1,87 @@ -# Example: deep sleep +# Deep Sleep Example -This example illustrates usage of deep sleep mode and various wakeup sources. +(See the README.md file in the upper level 'examples' directory for more information about examples.) -The following wake up sources are configured: +The [deep sleep mode](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#sleep-modes) of the ESP32 is a power saving mode that causes the CPU, majority of RAM, and digital peripherals that are clocked from APB_CLK to be powered off. Deep sleep mode can be exited using one of multiple [wake up sources](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#wakeup-sources). This example demonstrates how to use the [`esp_sleep.h`](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#api-reference) API to enter deep sleep mode, then wake up form different sources. -- Timer: wake up the chip in 20 seconds -- EXT1: wake up the chip if any of the two buttons are pressed (GPIO25, GPIO26) -- Touch: wake up the chip if any of the touch pads are pressed (GPIO32, GPIO33) -- ULP: wake up when the chip temperature changes by more than ~5 degrees Celsius (this value hasn't been characterized exactly yet). +The following wake up sources are demonstrated in this example (refer to the [Wakeup Sources documentation](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#wakeup-sources) for more details regarding wake up sources): -In this example, the `CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` Kconfig option is used, which allows you to reduce the boot time of the bootloader during waking up from deep sleep. The bootloader stores in rtc memory the address of a running partition and uses it when it wakes up. This example allows you to skip all image checks and speed up the boot. \ No newline at end of file +1. **Timer:** An RTC timer that can be programmed to trigger a wake up after a preset time. This example will trigger a wake up every 20 seconds. +2. **EXT1:** External wake up 1 which is tied to multiple RTC GPIOs. This example use GPIO25 and GPIO26 to trigger a wake up with any one of the two pins are HIGH. +3. **Touch:** Touch pad sensor interrupt. This example uses touch pads connected to GPIO32 and GPIO33 to trigger a wake up when any of the pads are pressed. +4. **ULP:** Ultra Low Power Coprocessor which can continue to run during deep sleep. This example utilizes the ULP and constantly sample the chip's temperature and trigger a wake up if the chips temperature exceeds ~5 degrees Celsius. + +Note: Some wake up sources can be disabled via configuration (see section on [project configuration](#Configure-the-project)) + +In this example, the `CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` Kconfig option is used, which allows you to reduce the boot time of the bootloader during waking up from deep sleep. The bootloader stores in rtc memory the address of a running partition and uses it when it wakes up. This example allows you to skip all image checks and speed up the boot. + +## How to use example + +### Hardware Required + +This example should be able to run on any commonly available ESP32 development board without any extra hardware if only **Timer** and **ULP** wake up sources are used. However, the following extra connections will be required for the remaining wake up sources. + +- **EXT1:** GPIO25 and GPIO26 should be connected to LOW to avoid floating pins. When triggering a wake up, connect one or both of then pins to HIGH. Note that floating pins may trigger an wake up. + +- **Touch:** GPIO32 and GPIO33 should be connected to touch sensors (see [Touch Sensor Application Note](https://github.com/espressif/esp-iot-solution/blob/master/documents/touch_pad_solution/touch_sensor_design_en.md)). + +### Configure the project + +``` +idf.py menuconfig +``` + +* **Touch wake up** can be enabled/disabled via `Example configuration > Enable touch wake up` +* **ULT wake up** can be enabled/disabled via `Example configuration > Enable temperature monitoring by ULP` + +Wake up sources that are unused or unconnected should be disabled in configuration to prevent inadvertent triggering of wake up as a result of floating pins. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +On initial startup, this example will detect that this is the first boot and output the following low: + +``` +... +I (304) cpu_start: Starting scheduler on PRO CPU. +I (0) cpu_start: Starting scheduler on APP CPU. +Not a deep sleep reset +Enabling timer wakeup, 20s +Enabling EXT1 wakeup on pins GPIO25, GPIO26 +Touch pad #8 average: 2148, wakeup threshold set to 2048. +Touch pad #9 average: 2148, wakeup threshold set to 2048. +Enabling touch pad wakeup +Enabling ULP wakeup +Entering deep sleep +``` + +The ESP32 will then enter deep sleep. When a wake up occurs, the ESP32 must undergo the entire boot process again. However the example will detect that this boot is due to a wake up and indicate the wake up source in the output log such as the following: + +``` +... +I (304) cpu_start: Starting scheduler on PRO CPU. +I (0) cpu_start: Starting scheduler on APP CPU. +Wake up from timer. Time spent in deep sleep: 20313ms +ULP did 110 temperature measurements in 20313 ms +Initial T=87, latest T=87 +Enabling timer wakeup, 20s +Enabling EXT1 wakeup on pins GPIO25, GPIO26 +Touch pad #8 average: 2149, wakeup threshold set to 2049. +Touch pad #9 average: 2146, wakeup threshold set to 2046. +Enabling touch pad wakeup +Enabling ULP wakeup +Entering deep sleep +``` diff --git a/examples/system/esp_event/default_event_loop/README.md b/examples/system/esp_event/default_event_loop/README.md index f9e10c6f21..ad3042e371 100644 --- a/examples/system/esp_event/default_event_loop/README.md +++ b/examples/system/esp_event/default_event_loop/README.md @@ -1,36 +1,58 @@ -# Event Loop Library ('esp_event') Default Event Loop Example +# Default Event Loop Example -This example illustrates the basics of the event loop library. To keep the example simple, demonstration is limited to the use of the default event loop. -The default event loop is the event loop the system uses for its events. For some use cases, this could be sufficient. -However, should the user need to create their own event loops, the example `user_event_loops` should be able to provide guidance. +(See the README.md file in the upper level 'examples' directory for more information about examples.) -Here are the things this example illustrates: +Note: Should users need to create their own event loops, refer to the **user event loops** [documentation](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/esp_event.html#using-esp-event-apis) and [example](https://github.com/espressif/esp-idf/tree/master/examples/system/esp_event/user_event_loops). + +The [**default event loop**](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/esp_event.html#default-event-loop) is an [event loop](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/esp_event.html#) the system uses to post and handle events (e.g. Wi-Fi events). This example demonstrates the usage of the following default event loop features: ### Declaring and Defining Events -This example shows the typical setup of having the event base and event IDs declared in a header -file and then having the definitions in a source file. - -Declaration of the event base makes use of the macro `ESP_EVENT_DECLARE_BASE`, while the event IDs are declared as an `enum`; see -``event_source.h``. The source file ``main.c`` holds the definition of the event base using the macro `ESP_EVENT_DEFINE_BASE`. +This example shows the typical setup of having the event base and event IDs declared in a header file, and having the definitions in a source file. Declaration of the event base makes use of the macro `ESP_EVENT_DECLARE_BASE()`, whilst the event IDs are declared as an `enum` (see `event_source.h`). The source file `main.c` holds the definition of the event base using the `ESP_EVENT_DEFINE_BASE()` macro. ### Creating the Default Event Loop -This example illustrates the creation of the default event loop using the API function `esp_event_loop_create_default`. +This example illustrates the creation of the default event loop using the API function `esp_event_loop_create_default()`. ### Posting Events to the Default Event Loop -Simply put, posting an event to a loop is the act of queueing its handlers for execution. For the default loop, this is done using the API `esp_event_post`. The ability to pass event-specific data to the handler is also illustrated. +Simply put, posting an event to a loop is the act of queueing its handlers for execution. For the default loop, this is done using the API `esp_event_post()`. The ability to pass event-specific data to the handler is also demonstrated. ### Handler Registration/Unregistration -This example illustrates handler registration to the default event loop using `esp_event_handler_register` for (1) specific events, (2) *any* event under a certain base, and (3) *any* event. This also shows the possbility of registering multiple handlers to the same event. +This example demonstrates handler registration to the default event loop using `esp_event_handler_register` for (1) specific events, (2) **any** event under a certain base, and (3) **any** event. This also shows the possbility of registering multiple handlers to the same event. -Unregistering a handler is done using `esp_event_handler_register`. Unregistering a handler means that it no longer executes even when the event it was previously registered to gets posted to the loop. +Unregistering a handler is done using `esp_event_handler_unregister()`. Unregistering a handler means that it no longer executes even when the event it was previously registered to gets posted to the loop. -## Example Flow Explained +## How to use example -The example flow is best explained using a sample log output. +### Hardware Required + +This example should be able to run on any commonly available ESP32 development board. + +### Configure the project + +``` +idf.py menuconfig +``` + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +The example should have the following log output: ``` I (276) default_event_loop: setting up @@ -71,80 +93,83 @@ I (3326) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STOPPED: deleted timer eve I (3326) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STOPPED: timer_any_handler I (3336) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STOPPED: all_event_handler I (3346) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: deleting task event source - ``` -### Setting +## Example Breakdown -This example uses two event sources: a periodic timer and a task with a loop inside. Events are raised for the periodic timer when (1) the timer is started (2) the timer period expires and (3) the timer is stopped. Events are raised for the when (1) the loop iterates. +### Setting of Event Sources -All of the events mentioned above has their own specific handler. There are additional handlers, however. One handler executes when *any* event under the periodic timer event is posted; while the other executes if *any* event is posted. +This example uses two event sources: -The number of periodic timer expiries and loop iterations are limited. When the limit for the timer expiry is reached, -the timer is stopped. When the limit for the iterarations is reached, the task is deleted. In the case of the loop iteration, there is another limit: the number of iterations for when its handler will be unregistered. +- A periodic timer. An event is raised when (1) the timer is started (2) the timer period expires and (3) the timer is stopped +- A task with a loop inside. An event is raised for the when (1) the loop iterates. + +All of the events mentioned above have their own specific handler, however there are the following additional handlers. + +- One handler executes when **any** event under the periodic timer event is posted +- The other handler executes if **any** event is posted. + +The number of periodic timer expiries and loop iterations are limited. When the limit for the number of timer expiries is reached, the timer is stopped. When the limit for the number of iterations is reached, the task is deleted. In the case of the loop iteration, there is another limit: the number of iterations for when its handler will be unregistered. ### Step-by-Step Explanation -The following text explains the important points of the sample log output. +The following text explains the important points of the [sample log output](#Example-Output). -a. +#### 1. Setting up of default event loop and event handlers ``` I (297) default_event_loop: setting up ``` -At this stage the default event loop is created, as well as the handlers for the different events registered. +At this stage the default event loop is created, and the handlers for the different events are registered. + + +#### 2. Posting to the event loop -b. ``` I (276) default_event_loop: starting event sources I (276) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STARTED: posting to default loop I (276) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: posting to default loop, 1 out of 5 ``` - The two event sources are started. The respective events are posted to the default event loop. -c. +#### 3. Execution of handlers + ``` I (296) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STARTED: timer_started_handler I (306) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STARTED: timer_any_handler I (316) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STARTED: all_event_handler -``` -``` +... I (326) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: task_iteration_handler, executed 1 times I (336) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler ``` -The handlers are executed for the events posted in **(b)**. In addition to event-specific handlers `timer_started_handler` -and `task_iteration_handler`, the handlers `timer_any_handler` and `all_event_handler` also executed. +The handlers are executed for the events that were posted in **(2)**. In addition to the event-specific handlers `timer_started_handler()` and `task_iteration_handler()`, the `timer_any_handler()` and `all_event_handler()` are also executed. -The handler `timer_any_handler` executes for *any* timer event. It can be seen executing for the timer expiry and timer stopped events in the upcoming parts of the log. +The `timer_any_handler()` executes for **any** timer event. It can be seen executing for the timer expiry and timer stopped events in the subsequent parts of the log. -On the other hand, `all_event_handler` executes for *any* event. This is the reason why it executes for both ``TIMER_EVENTS:TIMER_EVENT_STARTED`` and ``TASK_EVENTS:TASK_ITERATION_EVENT``. +On the other hand, `all_event_handler()` executes for **any** event, hence why it executes for both ``TIMER_EVENTS:TIMER_EVENT_STARTED`` and ``TASK_EVENTS:TASK_ITERATION_EVENT``. -For both the timer and task events, notice that the handlers are executed in the same order they are registered in the code. This is -a guarantee that the `esp_event` library provides. +For both the timer and task events, notice that the handlers are executed in the same order they are registered in the code. This is a guarantee that the `esp_event` library provides. -The proceeding lines of the log follows the same pattern: the event is posted to the loop and the handlers are executed. +The subsequent lines of the log follows the same pattern: the event is posted to the loop and the handlers are executed. -d. +#### 4. Unregistering the `task_iteration_handler()` ``` ... I (1316) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: unregistering task_iteration_handler ``` -At this point in the execution the handler `task_iteration_handler` is unregistered, therefore it no longer executes -when the event ``TASK_EVENTS:TASK_ITERATION_EVENT`` is posted. +At this point in the execution the handler `task_iteration_handler()` is unregistered, therefore it no longer executes when the event ``TASK_EVENTS:TASK_ITERATION_EVENT`` is posted. ``` I (1867) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: posting to default loop, 4 out of 5 I (1867) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handler - ... - I (1846) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: posting to default loop, 4 out of 5 I (1846) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: all_event_handle ``` -The iteration event continues to get posted, but only `all_event_handler` gets executed. +The iteration event continues to get posted, but only the `all_event_handler()` gets executed. + +#### 5. Iteration Limit -e. ``` ... I (3276) default_event_loop: TIMER_EVENTS:TIMER_EVENT_EXPIRY: posting to default loop @@ -155,15 +180,11 @@ I (3306) default_event_loop: TIMER_EVENTS:TIMER_EVENT_EXPIRY: all_event_handler I (3316) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STOPPED: timer_stopped_handler I (3326) default_event_loop: TIMER_EVENTS:TIMER_EVENT_STOPPED: deleted timer event source ``` -When the periodic timer expiry limit is reached, the event ``TIMER_EVENTS:TIMER_EVENT_STOPPED`` is posted to the loop. The periodic timer is consequently deleted in the handler `timer_stopped_handler`. +When the periodic timer expiry limit is reached, the event ``TIMER_EVENTS:TIMER_EVENT_STOPPED`` is posted to the loop. The periodic timer is consequently deleted in the handler `timer_stopped_handler()`. ``` ... I (3346) default_event_loop: TASK_EVENTS:TASK_ITERATION_EVENT: deleting task event source ... ``` -The task containing the loop that posts iteration events also gets deleted. The example ends at this point. - ---- - -See the README.md file in the upper level 'examples' directory for more information about examples. +The task containing the loop that posts iteration events also gets deleted. The example ends at this point. \ No newline at end of file diff --git a/examples/system/esp_event/user_event_loops/README.md b/examples/system/esp_event/user_event_loops/README.md index 0baf163a03..752630cade 100644 --- a/examples/system/esp_event/user_event_loops/README.md +++ b/examples/system/esp_event/user_event_loops/README.md @@ -1,29 +1,59 @@ -# Event Loop Library ('esp_event') User Event Loops Example +# User Event Loops Example -This example demonstrates the creation and use of user event loops. This example is a supplement -to the `default_event_loop` example, if the default event loop is not sufficient for the user's use case. +(See the README.md file in the upper level 'examples' directory for more information about examples.) -Here are the things this example illustrates: +This example demonstrates the creation and use of [**user event loops**](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/esp_event.html#). This example is a supplement to the [**default event loop** example](https://github.com/espressif/esp-idf/tree/master/examples/system/esp_event/default_event_loop), if the default event loop is not sufficient for the user's use case. + +This example demonstrates the following things regarding user event loops: ### Creating Event Loops -Creating a loop entails populating the structure `esp_event_loop_args_t` with the desired parameters and calling `esp_event_loop_create`. The call to `esp_event_loop_create` produces a handle to the loop, which is used to perform actions on that loop such as handler registration/unregistration and posting events. +Creating a loop entails populating the structure `esp_event_loop_args_t` with the desired parameters and calling `esp_event_loop_create()`. The call to `esp_event_loop_create()` produces a handle to the loop, which is used to perform actions on that loop such as handler registration/unregistration and posting events. ### Running Event Loops -Depending on the parameters, the user can create either a loop with a dedicated task or one without. The purpose of the dedicated task is to unqueue events from the loop and execute its handlers. For loops without the dedicated task, the user should make a call to `esp_event_loop_run` in an application task. +Depending on the parameters, the user can create either a loop with a dedicated task or one without. The purpose of the dedicated task is to unqueue events from the loop and execute its handlers. For loops without the dedicated task, the user should make a call to `esp_event_loop_run()` in an application task. ### Handler Registration/Unregistration, -Handler registration and unregistration works the same way as the default event loop, just with a different API, `esp_event_handler_register_with` and `esp_event_handler_register_with` respectively. There are two things this example highlights: (1) the possibility of registering the same handler for different loops and (2) the ability to pass static data to handlers. +Handler registration and unregistration works the same way as the default event loop, just with a different API, `esp_event_handler_register_with()` and `esp_event_handler_register_with()` respectively. There are two things this example highlights: + +1. The possibility of registering the same handler for different loops +2. The ability to pass static data to handlers. ### Posting Events to the Default Event Loop -Posting events also works the same way as the default event loop, except with a different API, `esp_event_post_to`. +Posting events also works the same way as the default event loop, except with a different API, `esp_event_post_to()`. -## Example Flow Explained +## How to use example -The example flow is best explained using the sample log output. +### Hardware Required + +This example should be able to run on any commonly available ESP32 development board. + +### Configure the project + +``` +idf.py menuconfig +``` + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +The example should have the following log output: ``` I (296) user_event_loops: setting up @@ -62,17 +92,17 @@ I (9126) user_event_loops: application_task: running application task ... ``` -### Setting +## Example Breakdown + +### Setting of Event Sources This example has a single event source: a task with a loop inside. Events are raised for the task event source when the loop iterates. -Two loops are created, one with a dedicated task and one without. Events are posted to either loops,depending on whether the iteration is odd or even. For the loop with a dedicated task, event handlers are automatically executed. However, for the loop without the dedicated task, a call to run the loop is made in one of the application tasks. As a result, the execution of the event handlers for this loop is interspersed with the execution of application task code. +Two user event loops are created, one with a dedicated task and one without. Events are posted to either loops, depending on whether the iteration is odd or even. For the loop with a dedicated task, event handlers are automatically executed. However, for the loop without the dedicated task, a call to run the loop is made in one of the application tasks. As a result, the execution of the event handlers for this loop is interspersed with the execution of application task code. ### Step-by-Step Explanation -The following text explains the important points of this example's sample log output. - -a. +#### 1.Setting up user event loop and event handlers ``` I (296) user_event_loops: setting up @@ -81,7 +111,7 @@ I (296) user_event_loops: starting application task ``` At this stage the two event loops are created, as well as the handlers for the iteration event registered. The event source is started, which will post the event to the appropriate loop. The application task which makes the call to run the loop without dedicated task, is also created and started. -b. +#### 2. Posting to the event loop ``` I (296) user_event_loops: posting TASK_EVENTS:TASK_ITERATION_EVENT to loop_without_task, iteration 1 out of 10 I (316) user_event_loops: application_task: running application task @@ -96,7 +126,7 @@ I (1826) user_event_loops: posting TASK_EVENTS:TASK_ITERATION_EVENT to loop_with ``` In this section of the log we see the odd iterations posted to the loop without dedicated task, and the even iterations to the loop with a dedicated task. For the event with dedicated task, event handlers are executed automatically. The loop without a dedicated task, on the other hand, runs in the context of the application task. -c. +#### 3. Iteration Limit ``` ... @@ -110,8 +140,4 @@ I (9126) user_event_loops: application_task: running application task ... ``` -The last of the iteration event is posted, and the event source is deleted. Because the loop without the task no longer receive events to execute, only the application task code executes. - ---- - -See the README.md file in the upper level 'examples' directory for more information about examples. +The last of the iteration event is posted, and the event source is deleted. Because the loop without the task no longer receive events to execute, only the application task code executes. \ No newline at end of file diff --git a/examples/system/esp_timer/README.md b/examples/system/esp_timer/README.md index ce7742e463..9490175849 100644 --- a/examples/system/esp_timer/README.md +++ b/examples/system/esp_timer/README.md @@ -1,79 +1,156 @@ -# High resolution timer (`esp_timer`) example +# High Resolution Timer Example (`esp_timer`) -This example illustrates usage of `esp_timer` APIs to create one-shot and periodic software timers. +(See the README.md file in the upper level 'examples' directory for more information about examples.) -`esp_timer` APIs allow application to create multiple timers using single hardware timer, and hides complexity associated with managing multiple timers, invoking callbacks, accounting for APB frequency changes (if dynamic frequency scaling is enabled), and maintaining correct time after light sleep. +The [High Resolution Timer (`esp_timer`)](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/esp_timer.html) APIs allow an application to create multiple timers using a single hardware timer, and hides complexity associated with managing multiple timers, invoking callbacks, accounting for APB frequency changes (if dynamic frequency scaling is enabled), and maintaining correct time after light sleep. -`esp_timer` API also provides `esp_timer_get_time` function which returns time since boot, in microseconds. This can be useful for fine-grained timing in tasks and ISRs. +This example illustrates the usage of the [`esp_timer` API](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/esp_timer.html#api-reference) to create one-shot and periodic software timers. +The `esp_timer` API also provides the `esp_timer_get_time()` function which returns the time since boot in microseconds. This can be useful for fine-grained timing in tasks and ISRs thus is also demonstrated in this example. -## Example flow explained +## How to use example -1. Example starts and timers are created. +### Hardware Required - ``` - I (265) example: Starting timers, time since boot: 2479 us - ``` +This example should be able to run on any commonly available ESP32 development board. -2. These two repeating lines are the output of `esp_timer_dump` function. There is one line for each of the timers created. This function can be useful for debugging purposes. Note that such debugging information is available because the example sets `CONFIG_ESP_TIMER_PROFILING` option in sdkconfig. Without this option, less information will be available. See documentation of `esp_timer_dump` in ESP-IDF programming guide for more details. +### Configure the project - ``` - timer period next time times times callback - name to fire started fired run time (us) - ------------------------------------------------------------------------------------ +``` +idf.py menuconfig +``` + +Under `Component config > Common ESP-related` are the following `esp_timer` related configurations + +* `High-resolution timer task stack size` can be increased if timer callbacks require a larger stack +* `Enable esp_timer profiling features` will cause `esp_timer_dump()` to include more information. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +The example should have the following log output: + +``` +... +I (294) example: Started timers, time since boot: 9662 us +periodic 500000 509644 1 0 0 +one-shot 0 5009654 1 0 0 +I (794) example: Periodic timer called, time since boot: 509694 us +I (1294) example: Periodic timer called, time since boot: 1009671 us +I (1794) example: Periodic timer called, time since boot: 1509671 us +I (2294) example: Periodic timer called, time since boot: 2009671 us +periodic 500000 2509644 1 4 542 +one-shot 0 5009654 1 0 0 +I (2794) example: Periodic timer called, time since boot: 2509671 us +I (3294) example: Periodic timer called, time since boot: 3009671 us +I (3794) example: Periodic timer called, time since boot: 3509671 us +I (4294) example: Periodic timer called, time since boot: 4009671 us +periodic 500000 4509644 1 8 1026 +one-shot 0 5009654 1 0 0 +I (4794) example: Periodic timer called, time since boot: 4509671 us +I (5294) example: Periodic timer called, time since boot: 5009669 us +I (5294) example: One-shot timer called, time since boot: 5009788 us +I (5294) example: Restarted periodic timer with 1s period, time since boot: 5012675 us +I (6294) example: Periodic timer called, time since boot: 6012692 us +periodic 1000000 7012666 2 11 1391 +one-shot 0 0 1 1 11472 +I (7294) example: Periodic timer called, time since boot: 7012692 us +I (8294) example: Periodic timer called, time since boot: 8012692 us +periodic 1000000 9012666 2 13 1639 +one-shot 0 0 1 1 11472 +I (9294) example: Periodic timer called, time since boot: 9012692 us +I (10294) example: Periodic timer called, time since boot: 10012692 us +I (10314) example: Entering light sleep for 0.5s, time since boot: 10024351 us +I (10314) example: Woke up from light sleep, time since boot: 10525143 us +... +``` + +## Example Breakdown + +### 1. Creating and starting timers + +The example starts by creating a periodic and a one shot timer using the `esp_timer_create()` function. Once created, the two timers are started using the `esp_timer_start_periodic()` and `esp_timer_start_once()` functions. + +``` +I (265) example: Starting timers, time since boot: 2479 us +``` + +### 2. Getting initial timer dump + +These two repeating lines are the output of `esp_timer_dump()` function. There is one line for each of the timers created. This function can be useful for debugging purposes. Note that such debugging information is available because the example sets `CONFIG_ESP_TIMER_PROFILING` option in sdkconfig. Without this option, less information will be available. See documentation of `esp_timer_dump()` in ESP-IDF programming guide for more details. + +``` +timer period next time times times callback +name to fire started fired run time (us) +------------------------------------------------------------------------------------ + +periodic 500000 502455 1 0 0 +one-shot 0 5002469 1 0 0 +``` + +### 3. Periodic timer keeps running at 500ms period: + +``` +I (765) example: Periodic timer called, time since boot: 502506 us +I (1265) example: Periodic timer called, time since boot: 1002478 us +I (1765) example: Periodic timer called, time since boot: 1502478 us +I (2265) example: Periodic timer called, time since boot: 2002478 us +periodic 500000 2502455 1 4 511 +one-shot 0 5002469 1 0 0 +I (2765) example: Periodic timer called, time since boot: 2502478 us +I (3265) example: Periodic timer called, time since boot: 3002478 us +I (3765) example: Periodic timer called, time since boot: 3502478 us +I (4265) example: Periodic timer called, time since boot: 4002478 us +periodic 500000 4502455 1 8 971 +one-shot 0 5002469 1 0 0 +I (4765) example: Periodic timer called, time since boot: 4502478 us +I (5265) example: Periodic timer called, time since boot: 5002476 us +``` + +### 4. One-shot timer runs + +The one-shot timer runs and changes the period of the periodic timer. Now the periodic timer runs with a period of 1 second: + +``` +I (5265) example: One-shot timer called, time since boot: 5002586 us +I (5265) example: Restarted periodic timer with 1s period, time since boot: 5005475 us +I (6265) example: Periodic timer called, time since boot: 6005492 us +periodic 1000000 7005469 2 11 1316 +one-shot 0 0 1 1 11474 +I (7265) example: Periodic timer called, time since boot: 7005492 us +I (8265) example: Periodic timer called, time since boot: 8005492 us +periodic 1000000 9005469 2 13 1550 +one-shot 0 0 1 1 11474 +I (9265) example: Periodic timer called, time since boot: 9005492 us +I (10265) example: Periodic timer called, time since boot: 10005492 us +``` + +### 5. Continuation through light sleep + +To illustrate that timekeeping continues correctly after light sleep, the example enters light sleep for 0.5 seconds. This sleep does not impact timer period, and the timer is executed 1 second after the previous iteration. Note that the timers can not execute during light sleep, since the CPU is not running at that time. Such timers would execute immediately after light sleep, and then continue running with their normal period. - periodic 500000 502455 1 0 0 - one-shot 0 5002469 1 0 0 - ``` +``` +I (10275) example: Entering light sleep for 0.5s, time since boot: 10011559 us +I (10275) example: Woke up from light sleep, time since boot: 10512007 us +I (10765) example: Periodic timer called, time since boot: 11005492 us +I (11765) example: Periodic timer called, time since boot: 12005492 us +``` -3. Periodic timer keeps running at 500ms period: +### 6. Finally, timers are deleted. - ``` - I (765) example: Periodic timer called, time since boot: 502506 us - I (1265) example: Periodic timer called, time since boot: 1002478 us - I (1765) example: Periodic timer called, time since boot: 1502478 us - I (2265) example: Periodic timer called, time since boot: 2002478 us - periodic 500000 2502455 1 4 511 - one-shot 0 5002469 1 0 0 - I (2765) example: Periodic timer called, time since boot: 2502478 us - I (3265) example: Periodic timer called, time since boot: 3002478 us - I (3765) example: Periodic timer called, time since boot: 3502478 us - I (4265) example: Periodic timer called, time since boot: 4002478 us - periodic 500000 4502455 1 8 971 - one-shot 0 5002469 1 0 0 - I (4765) example: Periodic timer called, time since boot: 4502478 us - I (5265) example: Periodic timer called, time since boot: 5002476 us - ``` -4. One-shot timer runs, and changes the period of the periodic timer. Now the periodic timer runs with 1 second period: - - ``` - I (5265) example: One-shot timer called, time since boot: 5002586 us - I (5265) example: Restarted periodic timer with 1s period, time since boot: 5005475 us - I (6265) example: Periodic timer called, time since boot: 6005492 us - periodic 1000000 7005469 2 11 1316 - one-shot 0 0 1 1 11474 - I (7265) example: Periodic timer called, time since boot: 7005492 us - I (8265) example: Periodic timer called, time since boot: 8005492 us - periodic 1000000 9005469 2 13 1550 - one-shot 0 0 1 1 11474 - I (9265) example: Periodic timer called, time since boot: 9005492 us - I (10265) example: Periodic timer called, time since boot: 10005492 us - ``` -5. To illustrate that timekeeping continues correctly after light sleep, the example enters light sleep for 0.5s. This sleep does not impact timer period, and the timer is executed 1 second after the previous iteration. Note that the timers can not execute during light sleep, since the CPU is not running at that time. Such timers would execute immediately after light sleep, and then continue running with their normal period. - - ``` - I (10275) example: Entering light sleep for 0.5s, time since boot: 10011559 us - I (10275) example: Woke up from light sleep, time since boot: 10512007 us - I (10765) example: Periodic timer called, time since boot: 11005492 us - I (11765) example: Periodic timer called, time since boot: 12005492 us - ``` - -6. Finally, timers are deleted. - - ``` - I (12275) example: Stopped and deleted timers - ``` - ---- - -See the README.md file in the upper level 'examples' directory for more information about examples. +``` +I (12275) example: Stopped and deleted timers +``` \ No newline at end of file diff --git a/examples/system/freertos/real_time_stats/README.md b/examples/system/freertos/real_time_stats/README.md index 51e49832e1..bf52abcef2 100644 --- a/examples/system/freertos/real_time_stats/README.md +++ b/examples/system/freertos/real_time_stats/README.md @@ -18,11 +18,9 @@ This example should be able to run on any commonly available ESP32 development b idf.py menuconfig ``` -* Set serial port under Serial Flasher Options. +* Select `Enable FreeRTOS to collect run time stats` under `Component Config > FreeRTOS` (this should be enabled in the example by default) -* Enable FreeRTOS to collect run time stats under `Component Config->FreeRTOS` - -* The clock source of reference timer used for FreeRTOS statistics can be configured under `Component Config->FreeRTOS` +* `Choose the clock source for run time stats` configured under `Component Config > FreeRTOS`. The `esp_timer` should be selected be default. This option will affect the time unit resolution in which the statistics are measured with respect to. ### Build and Flash @@ -32,13 +30,18 @@ Build the project and flash it to the board, then run monitor tool to view seria idf.py -p PORT flash monitor ``` +(Replace PORT with the name of the serial port to use.) + (To exit the serial monitor, type ``Ctrl-]``.) See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. ## Example Output +The example should have the following log output: + ``` +... Getting real time stats over 100 ticks | Task | Run Time | Percentage | stats | 1304 | 0% @@ -55,17 +58,15 @@ Getting real time stats over 100 ticks | ipc1 | 0 | 0% | ipc0 | 0 | 0% Real time stats obtained +... ``` -- When compiled in dual core mode, the percentage is with respect to the combined run time of both CPUs. Thus, `50%` would indicate full utilization of a single CPU. -- In single core mode, the percentage is with respect to a single CPU. Thus, `100%` would indicate full utilization of the CPU. +## Example Breakdown -The unit of `Run Time` is the period of the timer clock source used for FreeRTOS statistics. +### Spin tasks -## Troubleshooting +During the examples initialization process, multiple `spin` tasks are created. These tasks will simply spin a certain number of CPU cycles to consume CPU time, then block for a predetermined period. -``` -Getting real time stats over 100 ticks -Error getting real time stats -``` -If the above is output when running the example, users should check the return value of `print_real_time_stats()` to determine the reason for failure. +### Understanding the stats + +From the log output, it can be seen that the spin tasks consume nearly an equal amount of time over the specified stats collection period of `print_real_time_stats()`. The real time stats also display the CPU time consumption of other tasks created by default in ESP-IDF (e.g. `IDLE` and `ipc` tasks). \ No newline at end of file diff --git a/examples/system/freertos/real_time_stats/main/CMakeLists.txt b/examples/system/freertos/real_time_stats/main/CMakeLists.txt index e0287b7b9e..61db59218c 100644 --- a/examples/system/freertos/real_time_stats/main/CMakeLists.txt +++ b/examples/system/freertos/real_time_stats/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "main.c" +idf_component_register(SRCS "real_time_stats_example_main.c" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/system/freertos/real_time_stats/main/main.c b/examples/system/freertos/real_time_stats/main/real_time_stats_example_main.c similarity index 100% rename from examples/system/freertos/real_time_stats/main/main.c rename to examples/system/freertos/real_time_stats/main/real_time_stats_example_main.c diff --git a/examples/system/gcov/README.md b/examples/system/gcov/README.md index ffb5fef3bd..c753d4f22c 100644 --- a/examples/system/gcov/README.md +++ b/examples/system/gcov/README.md @@ -1,150 +1,102 @@ -# Blink Example with Coverage Info +# Blink Example With Coverage Info (Gcov) -See the README.md file in the upper level 'examples' directory for more information about examples. +(See the README.md file in the upper level 'examples' directory for more information about examples.) -GCC has useful feature which allows to generate code coverage information. Generated data show how many times every program execution paths has been taken. -Basing on coverage data developers can detect untested pieces of code and also it gives valuable information about critical (frequently used) execution paths. -In general case when coverage option is enabled GCC generates additional code to accumulate necessary data and save them into files. File system is not always available in ESP32 based projects or size of the file storage can be very limited to keep all the coverage data. To overcome those limitations IDF provides functionality to transfer the data to host and save them on its file system. Data transfer is done via JTAG. -This example shows how to generate coverage information for the program. +The following example demonstrates how to compile an ESP-IDF project to generate code coverage data, and how generate a code coverage report using Gcov or Lcov. Refer to the [Gcov Guide](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html#gcov-source-code-coverage) for more details on the code coverage features supported in ESP-IDF. -## How To Gather Coverage Info +This example implements a simple blink application but with code coverage enabled. The example will demonstrate the following features: +* How to compile a project with the `--coverage` flag +* Various methods of dumping code coverage data (e.g. Instant Run-Time Dump and Hard-coded Dump). +* How to generate a code coverage report. -There are two ways to collect gcov data: -* Hard-coded call to `esp_gcov_dump`. -* Instant run-time dumping w/o changes in your code via IDF's gcov debug stub. +## How to use example -### Generic Steps +### Hardware Required -Below are generic steps which should be performed to obtain coverage info. The steps are already done for this example project. +To run this example, you need an ESP32 dev board connected to a JTAG adapter, which can come in the following forms: -1. Enable application tracing module in menuconfig. Choose `Trace memory` in `Component config -> Application Level Tracing -> Data Destination`. -2. Enable GCOV to host interface in menuconfig `Component config -> Application Level Tracing -> GCOV to Host Enable`. -3. Enable coverage info generation for necessary source files. To do this add the following line to the 'component.mk' files of your project: -`CFLAGS += --coverage` -It will enable coverage info for all source files of your component. If you need to enable the option only for certain files the following line should be added for every file of interest: -`gcov_example.o: CFLAGS += --coverage` -Replace `gcov_example.o` with path to your file. +* [ESP-WROVER-KIT](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html#esp-wrover-kit-v4-1) which integrates an on-board JTAG adapter. Ensure that the [required jumpers to enable JTAG are connected](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/get-started-wrover-kit.html#setup-options) on the WROVER-KIT. +* ESP32 core board (e.g. ESP32-DevKitC) can also work as long as you connect it to an external JTAG adapter (e.g. FT2232H, J-LINK). - For CMake-based build system, use `target_compile_options(${COMPONENT_LIB} PRIVATE --coverage)` or: ` set_source_files_properties(gcov_example.c PROPERTIES COMPILE_FLAGS --coverage` +This example will assume that that an ESP-WROVER-KIT is used. +1. Connect the JTAG interface to ESP32 board, and power up both the JTAG and ESP32. For details about how to set up JTAG interface, please see [JTAG Debugging](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html). -### Hard-coded Dump Call +2. After connecting JTAG interface, you need to [Run OpenOCD](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html#run-openocd). -This method requires `esp_gcov_dump` to be called from your application's code. Below are additional steps which should be performed after the generic ones to obtain coverage info via hard-coded call. Step 1 is already done for this example project. +3. Open a separate terminal window and run telnet by entering the command below. The telnet terminal window is used to feed commands to OpenOCD: -1. Add call to `esp_gcov_dump` function in your program. This function will wait for command from host and dump coverage data. The exact place where to put the call depends on the program. -Usually it should be placed at the end of the program execution (at exit). But if you need to generate GCOV data incrementally `esp_gcov_dump` can be called multiple times. See `gcov_example.c` for example. -2. Build, flash and run program. -3. Wait until `esp_gcov_dump` is called. To detect this a call to `printf` can be used (see `gcov_example.c`) or, for example, you can use a LED to indicate the readiness to dump data. -Another way to detect call to `esp_gcov_dump` is to set breakpoint on that function, start target execution and wait for the target to be stopped. See the next section for respective GDB example. -4. Connect OpenOCD to the target and start telnet session with it. -5. Run the following OpenOCD command: `esp32 gcov dump` - -Example of the command output: -``` -> esp32 gcov dump -Total trace memory: 16384 bytes -Connect targets... -Target halted. PRO_CPU: PC=0x40088BC3 (active) APP_CPU: PC=0x400D14E6 -Targets connected. -Open file 0x1 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example.gcda' -Open file 0x1 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example.gcda' -Open file 0x2 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example_func.gcda' -Open file 0x2 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example_func.gcda' -Disconnect targets... -Target halted. PRO_CPU: PC=0x400D14E6 (active) APP_CPU: PC=0x400D14E6 -Targets disconnected. +```bash +telnet localhost 4444 ``` -#### Dump Using GDB - -As it was said above breakpoint can be used to detect when `esp_gcov_dump` is called. -The following GDB commands can be used to dump data upon call to `esp_gcov_dump` automatically (you can put them into `gdbinit` file): -``` -b esp_gcov_dump -commands -mon esp32 gcov dump -end -``` -Note that all OpenOCD commands should be invoked in gdb as: `mon `. - -### Instant Run-Time Dump - -Instant dump does not require to call `esp_gcov_dump`, so your application's code does not need to be modified. This method stops target at its current state and executes builtin IDF gcov debug stub function. -Having data dumped target resumes its execution. Below are the steps which should be performed to do instant dump. Step 1 is already done for this example project. - -1. Enable OpenOCD debug stubs in menuconfig `Component config -> ESP32-specific -> OpenOCD debug stubs`. -2. Build, flash and run program. -3. Connect OpenOCD to the target and start telnet session with it. -4. Run the following OpenOCD command: `esp32 gcov` - -Example of the command output: -``` -> esp32 gcov -Total trace memory: 16384 bytes -Connect targets... -Target halted. PRO_CPU: PC=0x400D14DA (active) APP_CPU: PC=0x400D14DA -Targets connected. -Open file 0x1 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example.gcda' -Open file 0x1 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example.gcda' -Open file 0x2 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example_func.gcda' -Open file 0x2 '/home/alexey/projects/esp/esp-idf/examples/system/gcov/build/main/gcov_example_func.gcda' -Target halted. PRO_CPU: PC=0x400844CE (active) APP_CPU: PC=0x400855E3 -Disconnect targets... -Targets disconnected. -> -``` - -### Coverage Data Accumulation - -Coverage data from several dumps are automatically accumulated. So the resulting gcov data files contain statistics since the board reset. Every data dump updates files accordingly. -New data collection is started if target has been reset. - -## How To Process Coverage Info - -There are several ways to process collected data. Two of the most common are: - -1. Using `gcov` tool supplied along with xtensa toolchain. See [GCOV documentation](https://gcc.gnu.org/onlinedocs/gcc/Gcov.html) for details. -2. Using `lcov` and `genhtml` tools. This way allows to generate pretty looking coverage reports in html. This example shows how to add build target to generate such kind of reports. -Add the following lines to you project's `Makefile` after the line including `project.mk`: +### Configure the project ``` -GCOV := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))gcov -REPORT_DIR := $(BUILD_DIR_BASE)/coverage_report - -lcov-report: - echo "Generating coverage report in: $(REPORT_DIR)" - echo "Using gcov: $(GCOV)" - mkdir -p $(REPORT_DIR)/html - lcov --gcov-tool $(GCOV) -c -d $(BUILD_DIR_BASE) -o $(REPORT_DIR)/$(PROJECT_NAME).info - genhtml -o $(REPORT_DIR)/html $(REPORT_DIR)/$(PROJECT_NAME).info - -cov-data-clean: - echo "Remove coverage data files..." - find $(BUILD_DIR_BASE) -name "*.gcda" -exec rm {} + - rm -rf $(REPORT_DIR) - -.PHONY: lcov-report cov-data-clean +idf.py menuconfig ``` -For CMake-based build system, add the following lines to you project's `CMakeLists.txt` after the line including `project.cmake`: +The example will enable the following options by default: + +* Enable the Application Tracing Module under `Component config -> Application Level Tracing -> Data Destination` by choosing `Trace memory`. +* Enable GCOV to host interface under `Component config -> Application Level Tracing -> GCOV to Host Enable`. +* Enable OpenOCD Debug Stubs under `Component config -> ESP32-specific -> OpenOCD debug stubs` + +### Build, Flash, and Run + +Build the project and flash it to the board, then run monitor tool to view serial output: ``` -include($ENV{IDF_PATH}/tools/cmake/gcov.cmake) -idf_create_coverage_report(${CMAKE_CURRENT_BINARY_DIR}/coverage_report) -idf_clean_coverage_report(${CMAKE_CURRENT_BINARY_DIR}/coverage_report) +idf.py -p PORT flash monitor ``` -Those lines add two build targets: -* `lcov-report` - generates html coverage report in `$(BUILD_DIR_BASE)/coverage_report/html` directory. -* `cov-data-clean` - removes all coverage data files and reports. +(Replace PORT with the name of the serial port to use.) -To generate report type the following command: -`make lcov-report` +(To exit the serial monitor, type ``Ctrl-]``.) - For CMake-based build system, type `cmake --build build/ --target lcov-report` +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. -The sample output of the command is below: +## Example Output + +### 1. Hard-coded Dump + +The example will initially execute two hard-coded dumps. Therefore, when the application outputs `Ready to dump GCOV data...`, users should execute the `esp32 gcov dump` OpenOCD command. The example should output the following: + +``` +Counter = 0 +Ready to dump GCOV data... +GCOV data have been dumped. +Counter = 1 +Ready to dump GCOV data... +GCOV data have been dumped. +``` + +### 2. Instant Run-Time Dump + +After the two hard-coded dumps, the example will continue looping through it's main blink function. Users can call `esp32 gcov` OpenOCD command to trigger an instant run-time dump. The output should resemble the following: + +``` +Counter = 2 +Counter = 3 +Counter = 4 +Counter = 5 +Counter = 6 +Counter = 7 +Counter = 8 +Counter = 9 +Counter = 10 +... +``` + +### Generating Lcov Report + +After dumping one or more times, a coverage report can be generated by calling `cmake --build build/ --target lcov-report`. This should result in an HTML code coverage report being generated in the build directory. + +To clean Gcov and report related data from the build directory, call `cmake --build build/ --target cov-data-clean` + +**Note:** Currently, the CMake build system on Windows does not support Lcov. However, Lcov is available in MINGW (installed through package manager). Therefore, if the project is built using the legacy GNU Make build system, call `make lcov-report` or `make cov-data-clean` instead. + +The following log should be output when generating the coverage report: ``` Generating coverage report in: /home/alexey/projects/esp/esp-idf/examples/system/gcov/build/coverage_report @@ -169,5 +121,29 @@ Overall coverage rate: functions..: 100.0% (3 of 3 functions) ``` -NOTE: Since `lcov` tool is not part of GCC bundle it can happen that format of GCOV binary data has been changed and your local version of `lcov` fails to understand it. -So it always better to use the latest `lcov` version from [LCOV repo](https://github.com/linux-test-project/lcov). +## Troubleshooting + +### OpenOCD Out of Sync + +If the following log is output when issuing an OpenOCD command via telnet, it could indicate that OpenOCD and the ESP32 are out of sync. This occurs when the ESP32 is externally reset whilst connected to OpenOCD (e.g., by pressing the EN button). + +``` +Open On-Chip Debugger +> esp32 gcov dump +Target halted. PRO_CPU: PC=0x4008AFF4 (active) APP_CPU: PC=0x400E396E +Total trace memory: 16384 bytes +Connect targets... +Target halted. PRO_CPU: PC=0x400D5D74 (active) APP_CPU: PC=0x400E396E +timed out while waiting for target halted / 1 - 2 +Failed to wait halt on bp target (-4)! +Failed to halt targets (-4)! +Failed to connect to targets (-4)! +``` + +This issue can be resolved in the following ways: +* Reset the board by issuing the `reset` command via telnet +* Restart OpenOCD + +### Outdated Lcov + +Due to `lcov` not being part of the GCC bundle, it is possible that the format of the GCOV binary data can change resulting in `lcov` failing to understand it. Therefore, it is always better to use the latest `lcov` version from the [Lcov repo](https://github.com/linux-test-project/lcov). diff --git a/examples/system/gcov/main/CMakeLists.txt b/examples/system/gcov/main/CMakeLists.txt index 0da58c5e1e..e2548349ac 100644 --- a/examples/system/gcov/main/CMakeLists.txt +++ b/examples/system/gcov/main/CMakeLists.txt @@ -1,7 +1,8 @@ -idf_component_register(SRCS "gcov_example.c" +idf_component_register(SRCS "gcov_example_main.c" "gcov_example_func.c" INCLUDE_DIRS ".") -set_source_files_properties(gcov_example.c +set_source_files_properties(gcov_example_main.c + gcov_example_func.c PROPERTIES COMPILE_FLAGS --coverage) diff --git a/examples/system/gcov/main/gcov_example.c b/examples/system/gcov/main/gcov_example_main.c similarity index 100% rename from examples/system/gcov/main/gcov_example.c rename to examples/system/gcov/main/gcov_example_main.c diff --git a/examples/system/himem/README.md b/examples/system/himem/README.md index 7eba56c40c..d8401c0433 100644 --- a/examples/system/himem/README.md +++ b/examples/system/himem/README.md @@ -1,30 +1,22 @@ -# _Himem API example_ +# Himem API Example (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example uses the Himem API to run a memory test of the upper 4MiB of an 8MiB PSRAM chip. +This example uses the [Himem API](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/himem.html) to run a memory test of the upper 4 MiB of an 8 MiB PSRAM chip. -The ESP32 has the ability to access external SPI RAM in the same way as internal memory can be accessed, that is, if -enabled in menuconfig, you can allocate memory in external RAM using standard C allocation APIs like `malloc()`, `calloc()`, etc. +The ESP32 has the ability to access external SPI RAM in the same way internal memory can be accessed. In other words, if enabled in project configuration, you can allocate memory in external RAM using standard C allocation APIs like `malloc()`, `calloc()`, etc. -However, because of hardware limitations, this only works for up to 4MiB of external memory. If you have, for instance, -an ESP32-WROVER module with 8MiB of PSRAM, you cannot use the upper 4MiB of PSRAM this way. However, this memory is not wasted, -using the Himem API (which essentially is a bank switching scheme for the upper memory regions), it is still usable. +However, because of hardware limitations, this only works for up to 4 MiB of external memory. If you have, for instance, an ESP32-WROVER module with 8 MiB of PSRAM, you cannot use the upper 4 MiB of PSRAM this way. However, this memory is not wasted, using the Himem API (which essentially is a bank switching scheme for the upper memory regions), the upper 4 MiB is still usable. -The Himem subsystem does this by reserving some amount of address space, then allowing applications to swap in and out normally -unreachable ranges of physical SPI RAM. While this does not allow transparent access in the way memory allocated with `malloc()` does, -it does provide an usable way to store data for e.g. large audio or video buffers in the upper 4MiB. +The Himem subsystem does this by reserving some amount of address space, then allowing applications to swap in and out normally unreachable ranges of physical SPI RAM. While this does not allow transparent access in the way memory allocated with `malloc()` does, it does provide an usable way to store data for e.g., large audio or video buffers in the upper 4 MiB. -This example uses the Himem API to run a simple memory test of the entire range of upper memory. It illustrates how to allocate -address space to bankswitch the physical memory in, allocate the physical memory, and switch it in or out of the allocated address space. +This example uses the Himem API to run a simple memory test of the entire range of upper memory. It illustrates how to allocate address space to bankswitch the physical memory in, allocate the physical memory, and switch it in or out of the allocated address space. ## How to use example ### Hardware Required -This example requires an ESP32 with external SPI RAM connected, for instance an ESP32-WROVER module. The example is intended to run on -an ESP32 with 8MiB external RAM connected. It will still run on an ESP32 with 4MiB external RAM, but in practice using Himem with such -a setup does not make much sense. +This example requires an ESP32 with external SPI RAM connected, for instance an ESP32-WROVER module. The example is intended to run on an ESP32 with 8 MiB external RAM connected. It will still run on an ESP32 with 4 MiB external RAM, but in practice using Himem with such a setup does not make much sense. ### Configure the project @@ -34,9 +26,7 @@ idf.py menuconfig * Set serial port under Serial Flasher Options. -* Make sure that SPI RAM bank switching is enabled. (Compiling the example with default values will automatically enable this.) It can be found under - Component config -> ESP32-specific -> Support for external, SPI-connected RAM -> SPI RAM config . - +* Make sure that SPI RAM bank switching is enabled. (Compiling the example with default values will automatically enable this.) It can be found under `Component config > ESP32-specific > Support for external, SPI-connected RAM > SPI RAM config`. ### Build and Flash @@ -107,7 +97,6 @@ I (121) esp_himem: Initialized. Using last 62 32KB address blocks for bank switc I (122) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. I (132) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations -Himem has 6080KiB of memory, 6080KiB of which is free. Testing the free memory... +Himem has 6080 KiB of memory, 6080 KiB of which is free. Testing the free memory... Done! -``` - +``` \ No newline at end of file diff --git a/examples/system/himem/main/CMakeLists.txt b/examples/system/himem/main/CMakeLists.txt index 780e49a35c..63ad17a959 100644 --- a/examples/system/himem/main/CMakeLists.txt +++ b/examples/system/himem/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "himem_test_main.c" +idf_component_register(SRCS "himem_example_main.c" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/system/himem/main/himem_test_main.c b/examples/system/himem/main/himem_example_main.c similarity index 100% rename from examples/system/himem/main/himem_test_main.c rename to examples/system/himem/main/himem_example_main.c diff --git a/examples/system/light_sleep/README.md b/examples/system/light_sleep/README.md index bdc107cf43..c9212067c6 100644 --- a/examples/system/light_sleep/README.md +++ b/examples/system/light_sleep/README.md @@ -2,7 +2,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example illustrates usage of light sleep mode. Unlike deep sleep mode, light sleep preserves the state of the memory, CPU, and peripherals. Execution of code on both CPUs is stopped when `esp_light_sleep_start` function is called. When the chip exits light sleep mode, execution continues at the point where it was stopped, and `esp_light_sleep_start` function returns. +This example illustrates usage of light sleep mode. Unlike deep sleep mode, light sleep preserves the state of the memory, CPU, and peripherals. Execution of code on both CPUs is stopped when `esp_light_sleep_start()` function is called. When the chip exits light sleep mode, execution continues at the point where it was stopped, and `esp_light_sleep_start()` function returns. The example enables the following wakeup sources: @@ -15,7 +15,13 @@ The example also prints time spent in light sleep mode to illustrate that timeke ### Hardware Required -This example can be used with any ESP32 development board. Most boards have a button attached to GPIO0, often labelled "BOOT". If the board does not have such button, an external button can be connected, along with a 10k pull-up resistor, and a 100nF capacitor to ground for debouncing. +This example can be used with any ESP32 development board. Most boards have a button attached to GPIO0, often labelled `BOOT`. If the board does not have such button, an external button can be connected, along with a 10k pull-up resistor, and a 100nF capacitor to ground for debouncing. + +### Configure the project + +``` +idf.py menuconfig +``` ### Build and Flash From a36cc962a3201f29b998d6d779192de6134582de Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Fri, 23 Aug 2019 12:51:05 +0800 Subject: [PATCH 063/146] smartconfig: move smartconfig_ack to esp_wifi conponent --- components/esp_wifi/CMakeLists.txt | 3 ++- .../{smartconfig_ack => esp_wifi}/include/smartconfig_ack.h | 0 components/{smartconfig_ack => esp_wifi/src}/smartconfig_ack.c | 0 components/smartconfig_ack/CMakeLists.txt | 3 --- components/smartconfig_ack/component.mk | 2 -- 5 files changed, 2 insertions(+), 6 deletions(-) rename components/{smartconfig_ack => esp_wifi}/include/smartconfig_ack.h (100%) rename components/{smartconfig_ack => esp_wifi/src}/smartconfig_ack.c (100%) delete mode 100644 components/smartconfig_ack/CMakeLists.txt delete mode 100644 components/smartconfig_ack/component.mk diff --git a/components/esp_wifi/CMakeLists.txt b/components/esp_wifi/CMakeLists.txt index 925704f933..3b0e3d530e 100644 --- a/components/esp_wifi/CMakeLists.txt +++ b/components/esp_wifi/CMakeLists.txt @@ -10,9 +10,10 @@ idf_component_register(SRCS "src/coexist.c" "src/phy_init.c" "src/restore.c" "src/smartconfig.c" + "src/smartconfig_ack.c" "src/wifi_init.c" INCLUDE_DIRS "include" "${idf_target}/include" - PRIV_REQUIRES wpa_supplicant nvs_flash smartconfig_ack + PRIV_REQUIRES wpa_supplicant nvs_flash LDFRAGMENTS "${ldfragments}") idf_build_get_property(build_dir BUILD_DIR) diff --git a/components/smartconfig_ack/include/smartconfig_ack.h b/components/esp_wifi/include/smartconfig_ack.h similarity index 100% rename from components/smartconfig_ack/include/smartconfig_ack.h rename to components/esp_wifi/include/smartconfig_ack.h diff --git a/components/smartconfig_ack/smartconfig_ack.c b/components/esp_wifi/src/smartconfig_ack.c similarity index 100% rename from components/smartconfig_ack/smartconfig_ack.c rename to components/esp_wifi/src/smartconfig_ack.c diff --git a/components/smartconfig_ack/CMakeLists.txt b/components/smartconfig_ack/CMakeLists.txt deleted file mode 100644 index eb34b0713c..0000000000 --- a/components/smartconfig_ack/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRCS "smartconfig_ack.c" - INCLUDE_DIRS include - PRIV_REQUIRES lwip tcpip_adapter) \ No newline at end of file diff --git a/components/smartconfig_ack/component.mk b/components/smartconfig_ack/component.mk deleted file mode 100644 index 58eac9a550..0000000000 --- a/components/smartconfig_ack/component.mk +++ /dev/null @@ -1,2 +0,0 @@ -# -# Component Makefile From e7033716dbd00be114a8dab5822c8676b142d6e4 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 28 Aug 2019 22:08:58 +0100 Subject: [PATCH 064/146] LwIP: Make IP_PKTINFO support configurable LwIP has support for IP_PKTINFO, but it cannot be activated as it is not configurable. This fix adds in the ability to configure it. Merges https://github.com/espressif/esp-idf/pull/3983 --- components/lwip/Kconfig | 7 +++++++ components/lwip/port/esp32/include/lwipopts.h | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 4b7d237b54..ec6787b80f 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -90,6 +90,13 @@ menu "LWIP" help Enabling this option allows checking for available data on a netconn. + config LWIP_NETBUF_RECVINFO + bool "Enable IP_PKTINFO option" + default n + help + Enabling this option allows checking for the destination address + of a received IPv4 Packet. + config LWIP_IP_FRAG bool "Enable fragment outgoing IP packets" default n diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 5b85fc6ccf..884ffb880a 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -556,6 +556,12 @@ */ #define SO_REUSE_RXTOALL CONFIG_LWIP_SO_REUSE_RXTOALL +/** + * LWIP_NETBUF_RECVINFO==1: Enable IP_PKTINFO option. + * This option is set via menuconfig. + */ +#define LWIP_NETBUF_RECVINFO CONFIG_LWIP_NETBUF_RECVINFO + /* ---------------------------------------- ---------- Statistics options ---------- From b6fe44b5204c7dd6a693af94ff242afd3bd9cbf1 Mon Sep 17 00:00:00 2001 From: baohongde Date: Thu, 29 Aug 2019 11:27:57 +0800 Subject: [PATCH 065/146] Fix 2 bugs in r_ld_fm_sket_isr 1. When remove two elt in the 1st isr, then assert in then 2nd one. 2. When timestamp of first elt is equal to sket_clkn, then crash. --- components/bt/controller/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib b/components/bt/controller/lib index a45ff23f0f..d122b08024 160000 --- a/components/bt/controller/lib +++ b/components/bt/controller/lib @@ -1 +1 @@ -Subproject commit a45ff23f0f6682caeb40dbb673f9dc54f0f8e1cd +Subproject commit d122b080242fdf045bd5a8ba8b5879f2f9c7885e From 612db28b6fe45ca95868cd566503f12c11ae41cd Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Tue, 27 Aug 2019 11:12:49 +0200 Subject: [PATCH 066/146] Fix error code collision and CI check --- components/esp_common/src/esp_err_to_name.c | 484 +++++++++++------- .../esp_http_server/include/esp_http_server.h | 2 +- components/lwip/include/apps/esp_ping.h | 2 +- components/soc/include/hal/esp_flash_err.h | 21 +- tools/ci/config/host-test.yml | 4 +- 5 files changed, 309 insertions(+), 204 deletions(-) diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index d0b17543ce..12b5e21cb9 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -17,6 +17,9 @@ #if __has_include("esp_http_server.h") #include "esp_http_server.h" #endif +#if __has_include("esp_https_ota.h") +#include "esp_https_ota.h" +#endif #if __has_include("esp_image_format.h") #include "esp_image_format.h" #endif @@ -35,11 +38,17 @@ #if __has_include("esp_spi_flash.h") #include "esp_spi_flash.h" #endif +#if __has_include("esp_supplicant/esp_wps.h") +#include "esp_supplicant/esp_wps.h" +#endif +#if __has_include("esp_tls.h") +#include "esp_tls.h" +#endif #if __has_include("esp_wifi.h") #include "esp_wifi.h" #endif -#if __has_include("esp_wps.h") -#include "esp_wps.h" +#if __has_include("hal/esp_flash_err.h") +#include "hal/esp_flash_err.h" #endif #if __has_include("nvs.h") #include "nvs.h" @@ -59,477 +68,570 @@ typedef struct { static const esp_err_msg_t esp_err_msg_table[] = { // components/esp_common/include/esp_err.h # ifdef ESP_FAIL - ERR_TBL_IT(ESP_FAIL), /* -1 Generic esp_err_t code indicating failure */ + ERR_TBL_IT(ESP_FAIL), /* -1 Generic esp_err_t code indicating failure */ # endif # ifdef ESP_OK - ERR_TBL_IT(ESP_OK), /* 0 esp_err_t value indicating success (no error) */ + ERR_TBL_IT(ESP_OK), /* 0 esp_err_t value indicating success (no error) */ # endif # ifdef ESP_ERR_NO_MEM - ERR_TBL_IT(ESP_ERR_NO_MEM), /* 257 0x101 Out of memory */ + ERR_TBL_IT(ESP_ERR_NO_MEM), /* 257 0x101 Out of memory */ # endif # ifdef ESP_ERR_INVALID_ARG - ERR_TBL_IT(ESP_ERR_INVALID_ARG), /* 258 0x102 Invalid argument */ + ERR_TBL_IT(ESP_ERR_INVALID_ARG), /* 258 0x102 Invalid argument */ # endif # ifdef ESP_ERR_INVALID_STATE - ERR_TBL_IT(ESP_ERR_INVALID_STATE), /* 259 0x103 Invalid state */ + ERR_TBL_IT(ESP_ERR_INVALID_STATE), /* 259 0x103 Invalid state */ # endif # ifdef ESP_ERR_INVALID_SIZE - ERR_TBL_IT(ESP_ERR_INVALID_SIZE), /* 260 0x104 Invalid size */ + ERR_TBL_IT(ESP_ERR_INVALID_SIZE), /* 260 0x104 Invalid size */ # endif # ifdef ESP_ERR_NOT_FOUND - ERR_TBL_IT(ESP_ERR_NOT_FOUND), /* 261 0x105 Requested resource not found */ + ERR_TBL_IT(ESP_ERR_NOT_FOUND), /* 261 0x105 Requested resource not found */ # endif # ifdef ESP_ERR_NOT_SUPPORTED - ERR_TBL_IT(ESP_ERR_NOT_SUPPORTED), /* 262 0x106 Operation or feature not supported */ + ERR_TBL_IT(ESP_ERR_NOT_SUPPORTED), /* 262 0x106 Operation or feature not supported */ # endif # ifdef ESP_ERR_TIMEOUT - ERR_TBL_IT(ESP_ERR_TIMEOUT), /* 263 0x107 Operation timed out */ + ERR_TBL_IT(ESP_ERR_TIMEOUT), /* 263 0x107 Operation timed out */ # endif # ifdef ESP_ERR_INVALID_RESPONSE - ERR_TBL_IT(ESP_ERR_INVALID_RESPONSE), /* 264 0x108 Received response was invalid */ + ERR_TBL_IT(ESP_ERR_INVALID_RESPONSE), /* 264 0x108 Received response was invalid */ # endif # ifdef ESP_ERR_INVALID_CRC - ERR_TBL_IT(ESP_ERR_INVALID_CRC), /* 265 0x109 CRC or checksum was invalid */ + ERR_TBL_IT(ESP_ERR_INVALID_CRC), /* 265 0x109 CRC or checksum was invalid */ # endif # ifdef ESP_ERR_INVALID_VERSION - ERR_TBL_IT(ESP_ERR_INVALID_VERSION), /* 266 0x10a Version was invalid */ + ERR_TBL_IT(ESP_ERR_INVALID_VERSION), /* 266 0x10a Version was invalid */ # endif # ifdef ESP_ERR_INVALID_MAC - ERR_TBL_IT(ESP_ERR_INVALID_MAC), /* 267 0x10b MAC address was invalid */ + ERR_TBL_IT(ESP_ERR_INVALID_MAC), /* 267 0x10b MAC address was invalid */ # endif // components/nvs_flash/include/nvs.h # ifdef ESP_ERR_NVS_BASE - ERR_TBL_IT(ESP_ERR_NVS_BASE), /* 4352 0x1100 Starting number of error codes */ + ERR_TBL_IT(ESP_ERR_NVS_BASE), /* 4352 0x1100 Starting number of error codes */ # endif # ifdef ESP_ERR_NVS_NOT_INITIALIZED - ERR_TBL_IT(ESP_ERR_NVS_NOT_INITIALIZED), /* 4353 0x1101 The storage driver is not initialized */ + ERR_TBL_IT(ESP_ERR_NVS_NOT_INITIALIZED), /* 4353 0x1101 The storage driver is not initialized */ # endif # ifdef ESP_ERR_NVS_NOT_FOUND - ERR_TBL_IT(ESP_ERR_NVS_NOT_FOUND), /* 4354 0x1102 Id namespace doesn’t exist yet and mode is - NVS_READONLY */ + ERR_TBL_IT(ESP_ERR_NVS_NOT_FOUND), /* 4354 0x1102 Id namespace doesn’t exist yet and mode + is NVS_READONLY */ # endif # ifdef ESP_ERR_NVS_TYPE_MISMATCH - ERR_TBL_IT(ESP_ERR_NVS_TYPE_MISMATCH), /* 4355 0x1103 The type of set or get operation doesn't - match the type of value stored in NVS */ + ERR_TBL_IT(ESP_ERR_NVS_TYPE_MISMATCH), /* 4355 0x1103 The type of set or get operation doesn't + match the type of value stored in NVS */ # endif # ifdef ESP_ERR_NVS_READ_ONLY - ERR_TBL_IT(ESP_ERR_NVS_READ_ONLY), /* 4356 0x1104 Storage handle was opened as read only */ + ERR_TBL_IT(ESP_ERR_NVS_READ_ONLY), /* 4356 0x1104 Storage handle was opened as read only */ # endif # ifdef ESP_ERR_NVS_NOT_ENOUGH_SPACE - ERR_TBL_IT(ESP_ERR_NVS_NOT_ENOUGH_SPACE), /* 4357 0x1105 There is not enough space in the underlying - storage to save the value */ + ERR_TBL_IT(ESP_ERR_NVS_NOT_ENOUGH_SPACE), /* 4357 0x1105 There is not enough space in the + underlying storage to save the value */ # endif # ifdef ESP_ERR_NVS_INVALID_NAME - ERR_TBL_IT(ESP_ERR_NVS_INVALID_NAME), /* 4358 0x1106 Namespace name doesn’t satisfy constraints */ + ERR_TBL_IT(ESP_ERR_NVS_INVALID_NAME), /* 4358 0x1106 Namespace name doesn’t satisfy constraints */ # endif # ifdef ESP_ERR_NVS_INVALID_HANDLE - ERR_TBL_IT(ESP_ERR_NVS_INVALID_HANDLE), /* 4359 0x1107 Handle has been closed or is NULL */ + ERR_TBL_IT(ESP_ERR_NVS_INVALID_HANDLE), /* 4359 0x1107 Handle has been closed or is NULL */ # endif # ifdef ESP_ERR_NVS_REMOVE_FAILED - ERR_TBL_IT(ESP_ERR_NVS_REMOVE_FAILED), /* 4360 0x1108 The value wasn’t updated because flash write - operation has failed. The value was written - however, and update will be finished after - re-initialization of nvs, provided that - flash operation doesn’t fail again. */ + ERR_TBL_IT(ESP_ERR_NVS_REMOVE_FAILED), /* 4360 0x1108 The value wasn’t updated because flash + write operation has failed. The value + was written however, and update will be + finished after re-initialization of nvs, + provided that flash operation doesn’t + fail again. */ # endif # ifdef ESP_ERR_NVS_KEY_TOO_LONG - ERR_TBL_IT(ESP_ERR_NVS_KEY_TOO_LONG), /* 4361 0x1109 Key name is too long */ + ERR_TBL_IT(ESP_ERR_NVS_KEY_TOO_LONG), /* 4361 0x1109 Key name is too long */ # endif # ifdef ESP_ERR_NVS_PAGE_FULL - ERR_TBL_IT(ESP_ERR_NVS_PAGE_FULL), /* 4362 0x110a Internal error; never returned by nvs API - functions */ + ERR_TBL_IT(ESP_ERR_NVS_PAGE_FULL), /* 4362 0x110a Internal error; never returned by nvs + API functions */ # endif # ifdef ESP_ERR_NVS_INVALID_STATE - ERR_TBL_IT(ESP_ERR_NVS_INVALID_STATE), /* 4363 0x110b NVS is in an inconsistent state due to a - previous error. Call nvs_flash_init and - nvs_open again, then retry. */ + ERR_TBL_IT(ESP_ERR_NVS_INVALID_STATE), /* 4363 0x110b NVS is in an inconsistent state due to a + previous error. Call nvs_flash_init and + nvs_open again, then retry. */ # endif # ifdef ESP_ERR_NVS_INVALID_LENGTH - ERR_TBL_IT(ESP_ERR_NVS_INVALID_LENGTH), /* 4364 0x110c String or blob length is not sufficient to - store data */ + ERR_TBL_IT(ESP_ERR_NVS_INVALID_LENGTH), /* 4364 0x110c String or blob length is not sufficient + to store data */ # endif # ifdef ESP_ERR_NVS_NO_FREE_PAGES - ERR_TBL_IT(ESP_ERR_NVS_NO_FREE_PAGES), /* 4365 0x110d NVS partition doesn't contain any empty - pages. This may happen if NVS partition was - truncated. Erase the whole partition and - call nvs_flash_init again. */ + ERR_TBL_IT(ESP_ERR_NVS_NO_FREE_PAGES), /* 4365 0x110d NVS partition doesn't contain any empty + pages. This may happen if NVS partition + was truncated. Erase the whole partition + and call nvs_flash_init again. */ # endif # ifdef ESP_ERR_NVS_VALUE_TOO_LONG - ERR_TBL_IT(ESP_ERR_NVS_VALUE_TOO_LONG), /* 4366 0x110e String or blob length is longer than - supported by the implementation */ + ERR_TBL_IT(ESP_ERR_NVS_VALUE_TOO_LONG), /* 4366 0x110e String or blob length is longer than + supported by the implementation */ # endif # ifdef ESP_ERR_NVS_PART_NOT_FOUND - ERR_TBL_IT(ESP_ERR_NVS_PART_NOT_FOUND), /* 4367 0x110f Partition with specified name is not found - in the partition table */ + ERR_TBL_IT(ESP_ERR_NVS_PART_NOT_FOUND), /* 4367 0x110f Partition with specified name is not + found in the partition table */ # endif # ifdef ESP_ERR_NVS_NEW_VERSION_FOUND - ERR_TBL_IT(ESP_ERR_NVS_NEW_VERSION_FOUND), /* 4368 0x1110 NVS partition contains data in new format - and cannot be recognized by this version of - code */ + ERR_TBL_IT(ESP_ERR_NVS_NEW_VERSION_FOUND), /* 4368 0x1110 NVS partition contains data in new + format and cannot be recognized by this + version of code */ # endif # ifdef ESP_ERR_NVS_XTS_ENCR_FAILED - ERR_TBL_IT(ESP_ERR_NVS_XTS_ENCR_FAILED), /* 4369 0x1111 XTS encryption failed while writing NVS entry */ + ERR_TBL_IT(ESP_ERR_NVS_XTS_ENCR_FAILED), /* 4369 0x1111 XTS encryption failed while writing NVS entry */ # endif # ifdef ESP_ERR_NVS_XTS_DECR_FAILED - ERR_TBL_IT(ESP_ERR_NVS_XTS_DECR_FAILED), /* 4370 0x1112 XTS decryption failed while reading NVS entry */ + ERR_TBL_IT(ESP_ERR_NVS_XTS_DECR_FAILED), /* 4370 0x1112 XTS decryption failed while reading NVS entry */ # endif # ifdef ESP_ERR_NVS_XTS_CFG_FAILED - ERR_TBL_IT(ESP_ERR_NVS_XTS_CFG_FAILED), /* 4371 0x1113 XTS configuration setting failed */ + ERR_TBL_IT(ESP_ERR_NVS_XTS_CFG_FAILED), /* 4371 0x1113 XTS configuration setting failed */ # endif # ifdef ESP_ERR_NVS_XTS_CFG_NOT_FOUND - ERR_TBL_IT(ESP_ERR_NVS_XTS_CFG_NOT_FOUND), /* 4372 0x1114 XTS configuration not found */ + ERR_TBL_IT(ESP_ERR_NVS_XTS_CFG_NOT_FOUND), /* 4372 0x1114 XTS configuration not found */ # endif # ifdef ESP_ERR_NVS_ENCR_NOT_SUPPORTED - ERR_TBL_IT(ESP_ERR_NVS_ENCR_NOT_SUPPORTED), /* 4373 0x1115 NVS encryption is not supported in this version */ + ERR_TBL_IT(ESP_ERR_NVS_ENCR_NOT_SUPPORTED), /* 4373 0x1115 NVS encryption is not supported in this version */ # endif # ifdef ESP_ERR_NVS_KEYS_NOT_INITIALIZED - ERR_TBL_IT(ESP_ERR_NVS_KEYS_NOT_INITIALIZED), /* 4374 0x1116 NVS key partition is uninitialized */ + ERR_TBL_IT(ESP_ERR_NVS_KEYS_NOT_INITIALIZED), /* 4374 0x1116 NVS key partition is uninitialized */ # endif # ifdef ESP_ERR_NVS_CORRUPT_KEY_PART - ERR_TBL_IT(ESP_ERR_NVS_CORRUPT_KEY_PART), /* 4375 0x1117 NVS key partition is corrupt */ + ERR_TBL_IT(ESP_ERR_NVS_CORRUPT_KEY_PART), /* 4375 0x1117 NVS key partition is corrupt */ +# endif +# ifdef ESP_ERR_NVS_CONTENT_DIFFERS + ERR_TBL_IT(ESP_ERR_NVS_CONTENT_DIFFERS), /* 4376 0x1118 Internal error; never returned by nvs + API functions. NVS key is different in + comparison */ # endif // components/ulp/include/esp32/ulp.h # ifdef ESP_ERR_ULP_BASE - ERR_TBL_IT(ESP_ERR_ULP_BASE), /* 4608 0x1200 Offset for ULP-related error codes */ + ERR_TBL_IT(ESP_ERR_ULP_BASE), /* 4608 0x1200 Offset for ULP-related error codes */ # endif # ifdef ESP_ERR_ULP_SIZE_TOO_BIG - ERR_TBL_IT(ESP_ERR_ULP_SIZE_TOO_BIG), /* 4609 0x1201 Program doesn't fit into RTC memory reserved - for the ULP */ + ERR_TBL_IT(ESP_ERR_ULP_SIZE_TOO_BIG), /* 4609 0x1201 Program doesn't fit into RTC memory + reserved for the ULP */ # endif # ifdef ESP_ERR_ULP_INVALID_LOAD_ADDR - ERR_TBL_IT(ESP_ERR_ULP_INVALID_LOAD_ADDR), /* 4610 0x1202 Load address is outside of RTC memory - reserved for the ULP */ + ERR_TBL_IT(ESP_ERR_ULP_INVALID_LOAD_ADDR), /* 4610 0x1202 Load address is outside of RTC memory + reserved for the ULP */ # endif # ifdef ESP_ERR_ULP_DUPLICATE_LABEL - ERR_TBL_IT(ESP_ERR_ULP_DUPLICATE_LABEL), /* 4611 0x1203 More than one label with the same number was - defined */ + ERR_TBL_IT(ESP_ERR_ULP_DUPLICATE_LABEL), /* 4611 0x1203 More than one label with the same number + was defined */ # endif # ifdef ESP_ERR_ULP_UNDEFINED_LABEL - ERR_TBL_IT(ESP_ERR_ULP_UNDEFINED_LABEL), /* 4612 0x1204 Branch instructions references an undefined label */ + ERR_TBL_IT(ESP_ERR_ULP_UNDEFINED_LABEL), /* 4612 0x1204 Branch instructions references an undefined label */ # endif # ifdef ESP_ERR_ULP_BRANCH_OUT_OF_RANGE - ERR_TBL_IT(ESP_ERR_ULP_BRANCH_OUT_OF_RANGE), /* 4613 0x1205 Branch target is out of range of B - instruction (try replacing with BX) */ + ERR_TBL_IT(ESP_ERR_ULP_BRANCH_OUT_OF_RANGE), /* 4613 0x1205 Branch target is out of range of B + instruction (try replacing with BX) */ # endif // components/app_update/include/esp_ota_ops.h # ifdef ESP_ERR_OTA_BASE - ERR_TBL_IT(ESP_ERR_OTA_BASE), /* 5376 0x1500 Base error code for ota_ops api */ + ERR_TBL_IT(ESP_ERR_OTA_BASE), /* 5376 0x1500 Base error code for ota_ops api */ # endif # ifdef ESP_ERR_OTA_PARTITION_CONFLICT - ERR_TBL_IT(ESP_ERR_OTA_PARTITION_CONFLICT), /* 5377 0x1501 Error if request was to write or erase the - current running partition */ + ERR_TBL_IT(ESP_ERR_OTA_PARTITION_CONFLICT), /* 5377 0x1501 Error if request was to write or erase + the current running partition */ # endif # ifdef ESP_ERR_OTA_SELECT_INFO_INVALID - ERR_TBL_IT(ESP_ERR_OTA_SELECT_INFO_INVALID), /* 5378 0x1502 Error if OTA data partition contains invalid - content */ + ERR_TBL_IT(ESP_ERR_OTA_SELECT_INFO_INVALID), /* 5378 0x1502 Error if OTA data partition contains + invalid content */ # endif # ifdef ESP_ERR_OTA_VALIDATE_FAILED - ERR_TBL_IT(ESP_ERR_OTA_VALIDATE_FAILED), /* 5379 0x1503 Error if OTA app image is invalid */ + ERR_TBL_IT(ESP_ERR_OTA_VALIDATE_FAILED), /* 5379 0x1503 Error if OTA app image is invalid */ # endif # ifdef ESP_ERR_OTA_SMALL_SEC_VER - ERR_TBL_IT(ESP_ERR_OTA_SMALL_SEC_VER), /* 5380 0x1504 Error if the firmware has a secure version - less than the running firmware. */ + ERR_TBL_IT(ESP_ERR_OTA_SMALL_SEC_VER), /* 5380 0x1504 Error if the firmware has a secure + version less than the running firmware. */ # endif # ifdef ESP_ERR_OTA_ROLLBACK_FAILED - ERR_TBL_IT(ESP_ERR_OTA_ROLLBACK_FAILED), /* 5381 0x1505 Error if flash does not have valid firmware - in passive partition and hence rollback is - not possible */ + ERR_TBL_IT(ESP_ERR_OTA_ROLLBACK_FAILED), /* 5381 0x1505 Error if flash does not have valid + firmware in passive partition and hence + rollback is not possible */ # endif # ifdef ESP_ERR_OTA_ROLLBACK_INVALID_STATE - ERR_TBL_IT(ESP_ERR_OTA_ROLLBACK_INVALID_STATE), /* 5382 0x1506 Error if current active firmware is still - marked in pending validation state - (ESP_OTA_IMG_PENDING_VERIFY), essentially - first boot of firmware image post upgrade - and hence firmware upgrade is not possible */ + ERR_TBL_IT(ESP_ERR_OTA_ROLLBACK_INVALID_STATE), /* 5382 0x1506 Error if current active firmware is + still marked in pending validation state + (ESP_OTA_IMG_PENDING_VERIFY), + essentially first boot of firmware image + post upgrade and hence firmware upgrade + is not possible */ # endif // components/efuse/include/esp_efuse.h # ifdef ESP_ERR_EFUSE - ERR_TBL_IT(ESP_ERR_EFUSE), /* 5632 0x1600 Base error code for efuse api. */ + ERR_TBL_IT(ESP_ERR_EFUSE), /* 5632 0x1600 Base error code for efuse api. */ # endif # ifdef ESP_OK_EFUSE_CNT - ERR_TBL_IT(ESP_OK_EFUSE_CNT), /* 5633 0x1601 OK the required number of bits is set. */ + ERR_TBL_IT(ESP_OK_EFUSE_CNT), /* 5633 0x1601 OK the required number of bits is set. */ # endif # ifdef ESP_ERR_EFUSE_CNT_IS_FULL - ERR_TBL_IT(ESP_ERR_EFUSE_CNT_IS_FULL), /* 5634 0x1602 Error field is full. */ + ERR_TBL_IT(ESP_ERR_EFUSE_CNT_IS_FULL), /* 5634 0x1602 Error field is full. */ # endif # ifdef ESP_ERR_EFUSE_REPEATED_PROG - ERR_TBL_IT(ESP_ERR_EFUSE_REPEATED_PROG), /* 5635 0x1603 Error repeated programming of programmed - bits is strictly forbidden. */ + ERR_TBL_IT(ESP_ERR_EFUSE_REPEATED_PROG), /* 5635 0x1603 Error repeated programming of programmed + bits is strictly forbidden. */ # endif # ifdef ESP_ERR_CODING - ERR_TBL_IT(ESP_ERR_CODING), /* 5636 0x1604 Error while a encoding operation. */ + ERR_TBL_IT(ESP_ERR_CODING), /* 5636 0x1604 Error while a encoding operation. */ # endif // components/bootloader_support/include/esp_image_format.h # ifdef ESP_ERR_IMAGE_BASE - ERR_TBL_IT(ESP_ERR_IMAGE_BASE), /* 8192 0x2000 */ + ERR_TBL_IT(ESP_ERR_IMAGE_BASE), /* 8192 0x2000 */ # endif # ifdef ESP_ERR_IMAGE_FLASH_FAIL - ERR_TBL_IT(ESP_ERR_IMAGE_FLASH_FAIL), /* 8193 0x2001 */ + ERR_TBL_IT(ESP_ERR_IMAGE_FLASH_FAIL), /* 8193 0x2001 */ # endif # ifdef ESP_ERR_IMAGE_INVALID - ERR_TBL_IT(ESP_ERR_IMAGE_INVALID), /* 8194 0x2002 */ + ERR_TBL_IT(ESP_ERR_IMAGE_INVALID), /* 8194 0x2002 */ # endif // components/esp_common/include/esp_err.h # ifdef ESP_ERR_WIFI_BASE - ERR_TBL_IT(ESP_ERR_WIFI_BASE), /* 12288 0x3000 Starting number of WiFi error codes */ + ERR_TBL_IT(ESP_ERR_WIFI_BASE), /* 12288 0x3000 Starting number of WiFi error codes */ # endif // components/esp_wifi/include/esp_wifi.h # ifdef ESP_ERR_WIFI_NOT_INIT - ERR_TBL_IT(ESP_ERR_WIFI_NOT_INIT), /* 12289 0x3001 WiFi driver was not installed by esp_wifi_init */ + ERR_TBL_IT(ESP_ERR_WIFI_NOT_INIT), /* 12289 0x3001 WiFi driver was not installed by esp_wifi_init */ # endif # ifdef ESP_ERR_WIFI_NOT_STARTED - ERR_TBL_IT(ESP_ERR_WIFI_NOT_STARTED), /* 12290 0x3002 WiFi driver was not started by esp_wifi_start */ + ERR_TBL_IT(ESP_ERR_WIFI_NOT_STARTED), /* 12290 0x3002 WiFi driver was not started by esp_wifi_start */ # endif # ifdef ESP_ERR_WIFI_NOT_STOPPED - ERR_TBL_IT(ESP_ERR_WIFI_NOT_STOPPED), /* 12291 0x3003 WiFi driver was not stopped by esp_wifi_stop */ + ERR_TBL_IT(ESP_ERR_WIFI_NOT_STOPPED), /* 12291 0x3003 WiFi driver was not stopped by esp_wifi_stop */ # endif # ifdef ESP_ERR_WIFI_IF - ERR_TBL_IT(ESP_ERR_WIFI_IF), /* 12292 0x3004 WiFi interface error */ + ERR_TBL_IT(ESP_ERR_WIFI_IF), /* 12292 0x3004 WiFi interface error */ # endif # ifdef ESP_ERR_WIFI_MODE - ERR_TBL_IT(ESP_ERR_WIFI_MODE), /* 12293 0x3005 WiFi mode error */ + ERR_TBL_IT(ESP_ERR_WIFI_MODE), /* 12293 0x3005 WiFi mode error */ # endif # ifdef ESP_ERR_WIFI_STATE - ERR_TBL_IT(ESP_ERR_WIFI_STATE), /* 12294 0x3006 WiFi internal state error */ + ERR_TBL_IT(ESP_ERR_WIFI_STATE), /* 12294 0x3006 WiFi internal state error */ # endif # ifdef ESP_ERR_WIFI_CONN - ERR_TBL_IT(ESP_ERR_WIFI_CONN), /* 12295 0x3007 WiFi internal control block of station or - soft-AP error */ + ERR_TBL_IT(ESP_ERR_WIFI_CONN), /* 12295 0x3007 WiFi internal control block of station + or soft-AP error */ # endif # ifdef ESP_ERR_WIFI_NVS - ERR_TBL_IT(ESP_ERR_WIFI_NVS), /* 12296 0x3008 WiFi internal NVS module error */ + ERR_TBL_IT(ESP_ERR_WIFI_NVS), /* 12296 0x3008 WiFi internal NVS module error */ # endif # ifdef ESP_ERR_WIFI_MAC - ERR_TBL_IT(ESP_ERR_WIFI_MAC), /* 12297 0x3009 MAC address is invalid */ + ERR_TBL_IT(ESP_ERR_WIFI_MAC), /* 12297 0x3009 MAC address is invalid */ # endif # ifdef ESP_ERR_WIFI_SSID - ERR_TBL_IT(ESP_ERR_WIFI_SSID), /* 12298 0x300a SSID is invalid */ + ERR_TBL_IT(ESP_ERR_WIFI_SSID), /* 12298 0x300a SSID is invalid */ # endif # ifdef ESP_ERR_WIFI_PASSWORD - ERR_TBL_IT(ESP_ERR_WIFI_PASSWORD), /* 12299 0x300b Password is invalid */ + ERR_TBL_IT(ESP_ERR_WIFI_PASSWORD), /* 12299 0x300b Password is invalid */ # endif # ifdef ESP_ERR_WIFI_TIMEOUT - ERR_TBL_IT(ESP_ERR_WIFI_TIMEOUT), /* 12300 0x300c Timeout error */ + ERR_TBL_IT(ESP_ERR_WIFI_TIMEOUT), /* 12300 0x300c Timeout error */ # endif # ifdef ESP_ERR_WIFI_WAKE_FAIL - ERR_TBL_IT(ESP_ERR_WIFI_WAKE_FAIL), /* 12301 0x300d WiFi is in sleep state(RF closed) and wakeup fail */ + ERR_TBL_IT(ESP_ERR_WIFI_WAKE_FAIL), /* 12301 0x300d WiFi is in sleep state(RF closed) and wakeup fail */ # endif # ifdef ESP_ERR_WIFI_WOULD_BLOCK - ERR_TBL_IT(ESP_ERR_WIFI_WOULD_BLOCK), /* 12302 0x300e The caller would block */ + ERR_TBL_IT(ESP_ERR_WIFI_WOULD_BLOCK), /* 12302 0x300e The caller would block */ # endif # ifdef ESP_ERR_WIFI_NOT_CONNECT - ERR_TBL_IT(ESP_ERR_WIFI_NOT_CONNECT), /* 12303 0x300f Station still in disconnect status */ + ERR_TBL_IT(ESP_ERR_WIFI_NOT_CONNECT), /* 12303 0x300f Station still in disconnect status */ # endif - // components/esp_wifi/include/esp_wps.h +# ifdef ESP_ERR_WIFI_POST + ERR_TBL_IT(ESP_ERR_WIFI_POST), /* 12306 0x3012 Failed to post the event to WiFi task */ +# endif +# ifdef ESP_ERR_WIFI_INIT_STATE + ERR_TBL_IT(ESP_ERR_WIFI_INIT_STATE), /* 12307 0x3013 Invalod WiFi state when init/deinit is called */ +# endif +# ifdef ESP_ERR_WIFI_STOP_STATE + ERR_TBL_IT(ESP_ERR_WIFI_STOP_STATE), /* 12308 0x3014 Returned when WiFi is stopping */ +# endif + // components/wpa_supplicant/include/esp_supplicant/esp_wps.h # ifdef ESP_ERR_WIFI_REGISTRAR - ERR_TBL_IT(ESP_ERR_WIFI_REGISTRAR), /* 12339 0x3033 WPS registrar is not supported */ + ERR_TBL_IT(ESP_ERR_WIFI_REGISTRAR), /* 12339 0x3033 WPS registrar is not supported */ # endif # ifdef ESP_ERR_WIFI_WPS_TYPE - ERR_TBL_IT(ESP_ERR_WIFI_WPS_TYPE), /* 12340 0x3034 WPS type error */ + ERR_TBL_IT(ESP_ERR_WIFI_WPS_TYPE), /* 12340 0x3034 WPS type error */ # endif # ifdef ESP_ERR_WIFI_WPS_SM - ERR_TBL_IT(ESP_ERR_WIFI_WPS_SM), /* 12341 0x3035 WPS state machine is not initialized */ + ERR_TBL_IT(ESP_ERR_WIFI_WPS_SM), /* 12341 0x3035 WPS state machine is not initialized */ # endif // components/esp_wifi/include/esp_now.h # ifdef ESP_ERR_ESPNOW_BASE - ERR_TBL_IT(ESP_ERR_ESPNOW_BASE), /* 12388 0x3064 ESPNOW error number base. */ + ERR_TBL_IT(ESP_ERR_ESPNOW_BASE), /* 12388 0x3064 ESPNOW error number base. */ # endif # ifdef ESP_ERR_ESPNOW_NOT_INIT - ERR_TBL_IT(ESP_ERR_ESPNOW_NOT_INIT), /* 12389 0x3065 ESPNOW is not initialized. */ + ERR_TBL_IT(ESP_ERR_ESPNOW_NOT_INIT), /* 12389 0x3065 ESPNOW is not initialized. */ # endif # ifdef ESP_ERR_ESPNOW_ARG - ERR_TBL_IT(ESP_ERR_ESPNOW_ARG), /* 12390 0x3066 Invalid argument */ + ERR_TBL_IT(ESP_ERR_ESPNOW_ARG), /* 12390 0x3066 Invalid argument */ # endif # ifdef ESP_ERR_ESPNOW_NO_MEM - ERR_TBL_IT(ESP_ERR_ESPNOW_NO_MEM), /* 12391 0x3067 Out of memory */ + ERR_TBL_IT(ESP_ERR_ESPNOW_NO_MEM), /* 12391 0x3067 Out of memory */ # endif # ifdef ESP_ERR_ESPNOW_FULL - ERR_TBL_IT(ESP_ERR_ESPNOW_FULL), /* 12392 0x3068 ESPNOW peer list is full */ + ERR_TBL_IT(ESP_ERR_ESPNOW_FULL), /* 12392 0x3068 ESPNOW peer list is full */ # endif # ifdef ESP_ERR_ESPNOW_NOT_FOUND - ERR_TBL_IT(ESP_ERR_ESPNOW_NOT_FOUND), /* 12393 0x3069 ESPNOW peer is not found */ + ERR_TBL_IT(ESP_ERR_ESPNOW_NOT_FOUND), /* 12393 0x3069 ESPNOW peer is not found */ # endif # ifdef ESP_ERR_ESPNOW_INTERNAL - ERR_TBL_IT(ESP_ERR_ESPNOW_INTERNAL), /* 12394 0x306a Internal error */ + ERR_TBL_IT(ESP_ERR_ESPNOW_INTERNAL), /* 12394 0x306a Internal error */ # endif # ifdef ESP_ERR_ESPNOW_EXIST - ERR_TBL_IT(ESP_ERR_ESPNOW_EXIST), /* 12395 0x306b ESPNOW peer has existed */ + ERR_TBL_IT(ESP_ERR_ESPNOW_EXIST), /* 12395 0x306b ESPNOW peer has existed */ # endif # ifdef ESP_ERR_ESPNOW_IF - ERR_TBL_IT(ESP_ERR_ESPNOW_IF), /* 12396 0x306c Interface error */ + ERR_TBL_IT(ESP_ERR_ESPNOW_IF), /* 12396 0x306c Interface error */ # endif // components/esp_common/include/esp_err.h # ifdef ESP_ERR_MESH_BASE - ERR_TBL_IT(ESP_ERR_MESH_BASE), /* 16384 0x4000 Starting number of MESH error codes */ + ERR_TBL_IT(ESP_ERR_MESH_BASE), /* 16384 0x4000 Starting number of MESH error codes */ # endif // components/esp_wifi/include/esp_mesh.h # ifdef ESP_ERR_MESH_WIFI_NOT_START - ERR_TBL_IT(ESP_ERR_MESH_WIFI_NOT_START), /* 16385 0x4001 */ + ERR_TBL_IT(ESP_ERR_MESH_WIFI_NOT_START), /* 16385 0x4001 */ # endif # ifdef ESP_ERR_MESH_NOT_INIT - ERR_TBL_IT(ESP_ERR_MESH_NOT_INIT), /* 16386 0x4002 */ + ERR_TBL_IT(ESP_ERR_MESH_NOT_INIT), /* 16386 0x4002 */ # endif # ifdef ESP_ERR_MESH_NOT_CONFIG - ERR_TBL_IT(ESP_ERR_MESH_NOT_CONFIG), /* 16387 0x4003 */ + ERR_TBL_IT(ESP_ERR_MESH_NOT_CONFIG), /* 16387 0x4003 */ # endif # ifdef ESP_ERR_MESH_NOT_START - ERR_TBL_IT(ESP_ERR_MESH_NOT_START), /* 16388 0x4004 */ + ERR_TBL_IT(ESP_ERR_MESH_NOT_START), /* 16388 0x4004 */ # endif # ifdef ESP_ERR_MESH_NOT_SUPPORT - ERR_TBL_IT(ESP_ERR_MESH_NOT_SUPPORT), /* 16389 0x4005 */ + ERR_TBL_IT(ESP_ERR_MESH_NOT_SUPPORT), /* 16389 0x4005 */ # endif # ifdef ESP_ERR_MESH_NOT_ALLOWED - ERR_TBL_IT(ESP_ERR_MESH_NOT_ALLOWED), /* 16390 0x4006 */ + ERR_TBL_IT(ESP_ERR_MESH_NOT_ALLOWED), /* 16390 0x4006 */ # endif # ifdef ESP_ERR_MESH_NO_MEMORY - ERR_TBL_IT(ESP_ERR_MESH_NO_MEMORY), /* 16391 0x4007 */ + ERR_TBL_IT(ESP_ERR_MESH_NO_MEMORY), /* 16391 0x4007 */ # endif # ifdef ESP_ERR_MESH_ARGUMENT - ERR_TBL_IT(ESP_ERR_MESH_ARGUMENT), /* 16392 0x4008 */ + ERR_TBL_IT(ESP_ERR_MESH_ARGUMENT), /* 16392 0x4008 */ # endif # ifdef ESP_ERR_MESH_EXCEED_MTU - ERR_TBL_IT(ESP_ERR_MESH_EXCEED_MTU), /* 16393 0x4009 */ + ERR_TBL_IT(ESP_ERR_MESH_EXCEED_MTU), /* 16393 0x4009 */ # endif # ifdef ESP_ERR_MESH_TIMEOUT - ERR_TBL_IT(ESP_ERR_MESH_TIMEOUT), /* 16394 0x400a */ + ERR_TBL_IT(ESP_ERR_MESH_TIMEOUT), /* 16394 0x400a */ # endif # ifdef ESP_ERR_MESH_DISCONNECTED - ERR_TBL_IT(ESP_ERR_MESH_DISCONNECTED), /* 16395 0x400b */ + ERR_TBL_IT(ESP_ERR_MESH_DISCONNECTED), /* 16395 0x400b */ # endif # ifdef ESP_ERR_MESH_QUEUE_FAIL - ERR_TBL_IT(ESP_ERR_MESH_QUEUE_FAIL), /* 16396 0x400c */ + ERR_TBL_IT(ESP_ERR_MESH_QUEUE_FAIL), /* 16396 0x400c */ # endif # ifdef ESP_ERR_MESH_QUEUE_FULL - ERR_TBL_IT(ESP_ERR_MESH_QUEUE_FULL), /* 16397 0x400d */ + ERR_TBL_IT(ESP_ERR_MESH_QUEUE_FULL), /* 16397 0x400d */ # endif # ifdef ESP_ERR_MESH_NO_PARENT_FOUND - ERR_TBL_IT(ESP_ERR_MESH_NO_PARENT_FOUND), /* 16398 0x400e */ + ERR_TBL_IT(ESP_ERR_MESH_NO_PARENT_FOUND), /* 16398 0x400e */ # endif # ifdef ESP_ERR_MESH_NO_ROUTE_FOUND - ERR_TBL_IT(ESP_ERR_MESH_NO_ROUTE_FOUND), /* 16399 0x400f */ + ERR_TBL_IT(ESP_ERR_MESH_NO_ROUTE_FOUND), /* 16399 0x400f */ # endif # ifdef ESP_ERR_MESH_OPTION_NULL - ERR_TBL_IT(ESP_ERR_MESH_OPTION_NULL), /* 16400 0x4010 */ + ERR_TBL_IT(ESP_ERR_MESH_OPTION_NULL), /* 16400 0x4010 */ # endif # ifdef ESP_ERR_MESH_OPTION_UNKNOWN - ERR_TBL_IT(ESP_ERR_MESH_OPTION_UNKNOWN), /* 16401 0x4011 */ + ERR_TBL_IT(ESP_ERR_MESH_OPTION_UNKNOWN), /* 16401 0x4011 */ # endif # ifdef ESP_ERR_MESH_XON_NO_WINDOW - ERR_TBL_IT(ESP_ERR_MESH_XON_NO_WINDOW), /* 16402 0x4012 */ + ERR_TBL_IT(ESP_ERR_MESH_XON_NO_WINDOW), /* 16402 0x4012 */ # endif # ifdef ESP_ERR_MESH_INTERFACE - ERR_TBL_IT(ESP_ERR_MESH_INTERFACE), /* 16403 0x4013 */ + ERR_TBL_IT(ESP_ERR_MESH_INTERFACE), /* 16403 0x4013 */ # endif # ifdef ESP_ERR_MESH_DISCARD_DUPLICATE - ERR_TBL_IT(ESP_ERR_MESH_DISCARD_DUPLICATE), /* 16404 0x4014 */ + ERR_TBL_IT(ESP_ERR_MESH_DISCARD_DUPLICATE), /* 16404 0x4014 */ # endif # ifdef ESP_ERR_MESH_DISCARD - ERR_TBL_IT(ESP_ERR_MESH_DISCARD), /* 16405 0x4015 */ + ERR_TBL_IT(ESP_ERR_MESH_DISCARD), /* 16405 0x4015 */ # endif # ifdef ESP_ERR_MESH_VOTING - ERR_TBL_IT(ESP_ERR_MESH_VOTING), /* 16406 0x4016 */ + ERR_TBL_IT(ESP_ERR_MESH_VOTING), /* 16406 0x4016 */ # endif // components/tcpip_adapter/include/tcpip_adapter.h # ifdef ESP_ERR_TCPIP_ADAPTER_BASE - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_BASE), /* 20480 0x5000 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_BASE), /* 20480 0x5000 */ # endif # ifdef ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS), /* 20481 0x5001 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS), /* 20481 0x5001 */ # endif # ifdef ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY), /* 20482 0x5002 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY), /* 20482 0x5002 */ # endif # ifdef ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED), /* 20483 0x5003 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED), /* 20483 0x5003 */ # endif # ifdef ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED), /* 20484 0x5004 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED), /* 20484 0x5004 */ # endif # ifdef ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED), /* 20485 0x5005 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED), /* 20485 0x5005 */ # endif # ifdef ESP_ERR_TCPIP_ADAPTER_NO_MEM - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_NO_MEM), /* 20486 0x5006 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_NO_MEM), /* 20486 0x5006 */ # endif # ifdef ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED - ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED), /* 20487 0x5007 */ + ERR_TBL_IT(ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED), /* 20487 0x5007 */ # endif - // components/lwip/include/apps/esp_ping.h -# ifdef ESP_ERR_PING_BASE - ERR_TBL_IT(ESP_ERR_PING_BASE), /* 24576 0x6000 */ + // components/esp_common/include/esp_err.h +# ifdef ESP_ERR_FLASH_BASE + ERR_TBL_IT(ESP_ERR_FLASH_BASE), /* 24576 0x6000 Starting number of flash error codes */ # endif -# ifdef ESP_ERR_PING_INVALID_PARAMS - ERR_TBL_IT(ESP_ERR_PING_INVALID_PARAMS), /* 24577 0x6001 */ + // components/spi_flash/include/esp_spi_flash.h +# ifdef ESP_ERR_FLASH_OP_FAIL + ERR_TBL_IT(ESP_ERR_FLASH_OP_FAIL), /* 24577 0x6001 */ # endif -# ifdef ESP_ERR_PING_NO_MEM - ERR_TBL_IT(ESP_ERR_PING_NO_MEM), /* 24578 0x6002 */ +# ifdef ESP_ERR_FLASH_OP_TIMEOUT + ERR_TBL_IT(ESP_ERR_FLASH_OP_TIMEOUT), /* 24578 0x6002 */ +# endif + // components/soc/include/hal/esp_flash_err.h +# ifdef ESP_ERR_FLASH_NOT_INITIALISED + ERR_TBL_IT(ESP_ERR_FLASH_NOT_INITIALISED), /* 24579 0x6003 */ +# endif +# ifdef ESP_ERR_FLASH_UNSUPPORTED_HOST + ERR_TBL_IT(ESP_ERR_FLASH_UNSUPPORTED_HOST), /* 24580 0x6004 */ +# endif +# ifdef ESP_ERR_FLASH_UNSUPPORTED_CHIP + ERR_TBL_IT(ESP_ERR_FLASH_UNSUPPORTED_CHIP), /* 24581 0x6005 */ +# endif +# ifdef ESP_ERR_FLASH_PROTECTED + ERR_TBL_IT(ESP_ERR_FLASH_PROTECTED), /* 24582 0x6006 */ # endif // components/esp_http_client/include/esp_http_client.h # ifdef ESP_ERR_HTTP_BASE - ERR_TBL_IT(ESP_ERR_HTTP_BASE), /* 28672 0x7000 Starting number of HTTP error codes */ + ERR_TBL_IT(ESP_ERR_HTTP_BASE), /* 28672 0x7000 Starting number of HTTP error codes */ # endif # ifdef ESP_ERR_HTTP_MAX_REDIRECT - ERR_TBL_IT(ESP_ERR_HTTP_MAX_REDIRECT), /* 28673 0x7001 The error exceeds the number of HTTP redirects */ + ERR_TBL_IT(ESP_ERR_HTTP_MAX_REDIRECT), /* 28673 0x7001 The error exceeds the number of HTTP redirects */ # endif # ifdef ESP_ERR_HTTP_CONNECT - ERR_TBL_IT(ESP_ERR_HTTP_CONNECT), /* 28674 0x7002 Error open the HTTP connection */ + ERR_TBL_IT(ESP_ERR_HTTP_CONNECT), /* 28674 0x7002 Error open the HTTP connection */ # endif # ifdef ESP_ERR_HTTP_WRITE_DATA - ERR_TBL_IT(ESP_ERR_HTTP_WRITE_DATA), /* 28675 0x7003 Error write HTTP data */ + ERR_TBL_IT(ESP_ERR_HTTP_WRITE_DATA), /* 28675 0x7003 Error write HTTP data */ # endif # ifdef ESP_ERR_HTTP_FETCH_HEADER - ERR_TBL_IT(ESP_ERR_HTTP_FETCH_HEADER), /* 28676 0x7004 Error read HTTP header from server */ + ERR_TBL_IT(ESP_ERR_HTTP_FETCH_HEADER), /* 28676 0x7004 Error read HTTP header from server */ # endif # ifdef ESP_ERR_HTTP_INVALID_TRANSPORT - ERR_TBL_IT(ESP_ERR_HTTP_INVALID_TRANSPORT), /* 28677 0x7005 There are no transport support for the input - scheme */ + ERR_TBL_IT(ESP_ERR_HTTP_INVALID_TRANSPORT), /* 28677 0x7005 There are no transport support for the + input scheme */ # endif # ifdef ESP_ERR_HTTP_CONNECTING - ERR_TBL_IT(ESP_ERR_HTTP_CONNECTING), /* 28678 0x7006 HTTP connection hasn't been established yet */ + ERR_TBL_IT(ESP_ERR_HTTP_CONNECTING), /* 28678 0x7006 HTTP connection hasn't been established yet */ # endif # ifdef ESP_ERR_HTTP_EAGAIN - ERR_TBL_IT(ESP_ERR_HTTP_EAGAIN), /* 28679 0x7007 Mapping of errno EAGAIN to esp_err_t */ + ERR_TBL_IT(ESP_ERR_HTTP_EAGAIN), /* 28679 0x7007 Mapping of errno EAGAIN to esp_err_t */ +# endif + // components/esp-tls/esp_tls.h +# ifdef ESP_ERR_ESP_TLS_BASE + ERR_TBL_IT(ESP_ERR_ESP_TLS_BASE), /* 32768 0x8000 Starting number of ESP-TLS error codes */ +# endif +# ifdef ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME + ERR_TBL_IT(ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME), /* 32769 0x8001 Error if hostname couldn't be resolved + upon tls connection */ +# endif +# ifdef ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET + ERR_TBL_IT(ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET), /* 32770 0x8002 Failed to create socket */ +# endif +# ifdef ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY + ERR_TBL_IT(ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY), /* 32771 0x8003 Unsupported protocol family */ +# endif +# ifdef ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST + ERR_TBL_IT(ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST), /* 32772 0x8004 Failed to connect to host */ +# endif +# ifdef ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED + ERR_TBL_IT(ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED), /* 32773 0x8005 failed to set socket option */ +# endif +# ifdef ESP_ERR_MBEDTLS_CERT_PARTLY_OK + ERR_TBL_IT(ESP_ERR_MBEDTLS_CERT_PARTLY_OK), /* 32774 0x8006 mbedtls parse certificates was partly successful */ +# endif +# ifdef ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED), /* 32775 0x8007 mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED), /* 32776 0x8008 mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED), /* 32777 0x8009 mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED), /* 32778 0x800a mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED), /* 32779 0x800b mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED), /* 32780 0x800c mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_SETUP_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_SETUP_FAILED), /* 32781 0x800d mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_WRITE_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_WRITE_FAILED), /* 32782 0x800e mbedtls api returned error */ +# endif +# ifdef ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED), /* 32783 0x800f mbedtls api returned failed */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED), /* 32784 0x8010 mbedtls api returned failed */ +# endif +# ifdef ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED + ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED), /* 32785 0x8011 mbedtls api returned failed */ +# endif + // components/esp_https_ota/include/esp_https_ota.h +# ifdef ESP_ERR_HTTPS_OTA_BASE + ERR_TBL_IT(ESP_ERR_HTTPS_OTA_BASE), /* 36864 0x9000 */ +# endif +# ifdef ESP_ERR_HTTPS_OTA_IN_PROGRESS + ERR_TBL_IT(ESP_ERR_HTTPS_OTA_IN_PROGRESS), /* 36865 0x9001 */ +# endif + // components/lwip/include/apps/esp_ping.h +# ifdef ESP_ERR_PING_BASE + ERR_TBL_IT(ESP_ERR_PING_BASE), /* 40960 0xa000 */ +# endif +# ifdef ESP_ERR_PING_INVALID_PARAMS + ERR_TBL_IT(ESP_ERR_PING_INVALID_PARAMS), /* 40961 0xa001 */ +# endif +# ifdef ESP_ERR_PING_NO_MEM + ERR_TBL_IT(ESP_ERR_PING_NO_MEM), /* 40962 0xa002 */ # endif // components/esp_http_server/include/esp_http_server.h # ifdef ESP_ERR_HTTPD_BASE - ERR_TBL_IT(ESP_ERR_HTTPD_BASE), /* 32768 0x8000 Starting number of HTTPD error codes */ + ERR_TBL_IT(ESP_ERR_HTTPD_BASE), /* 45056 0xb000 Starting number of HTTPD error codes */ # endif # ifdef ESP_ERR_HTTPD_HANDLERS_FULL - ERR_TBL_IT(ESP_ERR_HTTPD_HANDLERS_FULL), /* 32769 0x8001 All slots for registering URI handlers have - been consumed */ + ERR_TBL_IT(ESP_ERR_HTTPD_HANDLERS_FULL), /* 45057 0xb001 All slots for registering URI handlers + have been consumed */ # endif # ifdef ESP_ERR_HTTPD_HANDLER_EXISTS - ERR_TBL_IT(ESP_ERR_HTTPD_HANDLER_EXISTS), /* 32770 0x8002 URI handler with same method and target URI - already registered */ + ERR_TBL_IT(ESP_ERR_HTTPD_HANDLER_EXISTS), /* 45058 0xb002 URI handler with same method and target + URI already registered */ # endif # ifdef ESP_ERR_HTTPD_INVALID_REQ - ERR_TBL_IT(ESP_ERR_HTTPD_INVALID_REQ), /* 32771 0x8003 Invalid request pointer */ + ERR_TBL_IT(ESP_ERR_HTTPD_INVALID_REQ), /* 45059 0xb003 Invalid request pointer */ # endif # ifdef ESP_ERR_HTTPD_RESULT_TRUNC - ERR_TBL_IT(ESP_ERR_HTTPD_RESULT_TRUNC), /* 32772 0x8004 Result string truncated */ + ERR_TBL_IT(ESP_ERR_HTTPD_RESULT_TRUNC), /* 45060 0xb004 Result string truncated */ # endif # ifdef ESP_ERR_HTTPD_RESP_HDR - ERR_TBL_IT(ESP_ERR_HTTPD_RESP_HDR), /* 32773 0x8005 Response header field larger than supported */ + ERR_TBL_IT(ESP_ERR_HTTPD_RESP_HDR), /* 45061 0xb005 Response header field larger than supported */ # endif # ifdef ESP_ERR_HTTPD_RESP_SEND - ERR_TBL_IT(ESP_ERR_HTTPD_RESP_SEND), /* 32774 0x8006 Error occured while sending response packet */ + ERR_TBL_IT(ESP_ERR_HTTPD_RESP_SEND), /* 45062 0xb006 Error occured while sending response packet */ # endif # ifdef ESP_ERR_HTTPD_ALLOC_MEM - ERR_TBL_IT(ESP_ERR_HTTPD_ALLOC_MEM), /* 32775 0x8007 Failed to dynamically allocate memory for - resource */ + ERR_TBL_IT(ESP_ERR_HTTPD_ALLOC_MEM), /* 45063 0xb007 Failed to dynamically allocate memory + for resource */ # endif # ifdef ESP_ERR_HTTPD_TASK - ERR_TBL_IT(ESP_ERR_HTTPD_TASK), /* 32776 0x8008 Failed to launch server task/thread */ -# endif - // components/spi_flash/include/esp_spi_flash.h -# ifdef ESP_ERR_FLASH_BASE - ERR_TBL_IT(ESP_ERR_FLASH_BASE), /* 65552 0x10010 */ -# endif -# ifdef ESP_ERR_FLASH_OP_FAIL - ERR_TBL_IT(ESP_ERR_FLASH_OP_FAIL), /* 65553 0x10011 */ -# endif -# ifdef ESP_ERR_FLASH_OP_TIMEOUT - ERR_TBL_IT(ESP_ERR_FLASH_OP_TIMEOUT), /* 65554 0x10012 */ + ERR_TBL_IT(ESP_ERR_HTTPD_TASK), /* 45064 0xb008 Failed to launch server task/thread */ # endif }; #endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP diff --git a/components/esp_http_server/include/esp_http_server.h b/components/esp_http_server/include/esp_http_server.h index f5a524dbab..8f6ef5f9e6 100644 --- a/components/esp_http_server/include/esp_http_server.h +++ b/components/esp_http_server/include/esp_http_server.h @@ -53,7 +53,7 @@ initializer that should be kept in sync .uri_match_fn = NULL \ } -#define ESP_ERR_HTTPD_BASE (0x8000) /*!< Starting number of HTTPD error codes */ +#define ESP_ERR_HTTPD_BASE (0xb000) /*!< Starting number of HTTPD error codes */ #define ESP_ERR_HTTPD_HANDLERS_FULL (ESP_ERR_HTTPD_BASE + 1) /*!< All slots for registering URI handlers have been consumed */ #define ESP_ERR_HTTPD_HANDLER_EXISTS (ESP_ERR_HTTPD_BASE + 2) /*!< URI handler with same method and target URI already registered */ #define ESP_ERR_HTTPD_INVALID_REQ (ESP_ERR_HTTPD_BASE + 3) /*!< Invalid request pointer */ diff --git a/components/lwip/include/apps/esp_ping.h b/components/lwip/include/apps/esp_ping.h index 9abb883c23..2386795283 100644 --- a/components/lwip/include/apps/esp_ping.h +++ b/components/lwip/include/apps/esp_ping.h @@ -24,7 +24,7 @@ extern "C" { // gen_esp_err_to_name.py: include this as "esp_ping.h" because "components/lwip/include/apps/" is in the compiler path // and not "components/lwip/include" -#define ESP_ERR_PING_BASE 0x6000 +#define ESP_ERR_PING_BASE 0xa000 #define ESP_ERR_PING_INVALID_PARAMS ESP_ERR_PING_BASE + 0x01 #define ESP_ERR_PING_NO_MEM ESP_ERR_PING_BASE + 0x02 diff --git a/components/soc/include/hal/esp_flash_err.h b/components/soc/include/hal/esp_flash_err.h index cde56aa507..3da8b56de9 100644 --- a/components/soc/include/hal/esp_flash_err.h +++ b/components/soc/include/hal/esp_flash_err.h @@ -28,17 +28,20 @@ extern "C" { * replacable header. This header should ensure the consistency to esp_err_t. */ -/* These should be consistent with esp_err_t errors */ -#define ESP_ERR_FLASH_SIZE_NOT_MATCH ESP_ERR_INVALID_SIZE ///< The chip doesn't have enough space for the current partition table -#define ESP_ERR_FLASH_NO_RESPONSE ESP_ERR_INVALID_RESPONSE ///< Chip did not respond to the command, or timed out. +enum { + /* These codes should be consistent with esp_err_t errors. However, error codes with the same values are not + * allowed in ESP-IDF. This is a workaround in order to not introduce a dependency between the "soc" and + * "esp_common" components. The disadvantage is that the output of esp_err_to_name(ESP_ERR_FLASH_SIZE_NOT_MATCH) + * will be ESP_ERR_INVALID_SIZE. */ + ESP_ERR_FLASH_SIZE_NOT_MATCH = ESP_ERR_INVALID_SIZE, ///< The chip doesn't have enough space for the current partition table + ESP_ERR_FLASH_NO_RESPONSE = ESP_ERR_INVALID_RESPONSE, ///< Chip did not respond to the command, or timed out. +}; - -#define ESP_ERR_FLASH_ERR_BASE 0x6000 ///< Starting number of Flash error codes */ //The ROM code has already taken 1 and 2, to avoid possible conflicts, start from 3. -#define ESP_ERR_FLASH_NOT_INITIALISED (ESP_ERR_FLASH_ERR_BASE+3) ///< esp_flash_chip_t structure not correctly initialised by esp_flash_init(). -#define ESP_ERR_FLASH_UNSUPPORTED_HOST (ESP_ERR_FLASH_ERR_BASE+4) ///< Requested operation isn't supported via this host SPI bus (chip->spi field). -#define ESP_ERR_FLASH_UNSUPPORTED_CHIP (ESP_ERR_FLASH_ERR_BASE+5) ///< Requested operation isn't supported by this model of SPI flash chip. -#define ESP_ERR_FLASH_PROTECTED (ESP_ERR_FLASH_ERR_BASE+6) ///< Write operation failed due to chip's write protection being enabled. +#define ESP_ERR_FLASH_NOT_INITIALISED (ESP_ERR_FLASH_BASE+3) ///< esp_flash_chip_t structure not correctly initialised by esp_flash_init(). +#define ESP_ERR_FLASH_UNSUPPORTED_HOST (ESP_ERR_FLASH_BASE+4) ///< Requested operation isn't supported via this host SPI bus (chip->spi field). +#define ESP_ERR_FLASH_UNSUPPORTED_CHIP (ESP_ERR_FLASH_BASE+5) ///< Requested operation isn't supported by this model of SPI flash chip. +#define ESP_ERR_FLASH_PROTECTED (ESP_ERR_FLASH_BASE+6) ///< Write operation failed due to chip's write protection being enabled. #ifdef __cplusplus } diff --git a/tools/ci/config/host-test.yml b/tools/ci/config/host-test.yml index 89223be320..4fcab23b8b 100644 --- a/tools/ci/config/host-test.yml +++ b/tools/ci/config/host-test.yml @@ -205,9 +205,9 @@ test_esp_err_to_name_on_host: script: - cd ${IDF_PATH}/tools/ - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh -p 2.7.15 ./gen_esp_err_to_name.py - - git diff --exit-code -- ../components/esp32/esp_err_to_name.c || { echo 'Differences found. Please run gen_esp_err_to_name.py and commit the changes.'; exit 1; } + - git diff --exit-code -- ../components/esp_common/src/esp_err_to_name.c || { echo 'Differences found. Please run gen_esp_err_to_name.py and commit the changes.'; exit 1; } - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh -p 3.4.8 ./gen_esp_err_to_name.py - - git diff --exit-code -- ../components/esp32/esp_err_to_name.c || { echo 'Differences found between running under Python 2 and 3.'; exit 1; } + - git diff --exit-code -- ../components/esp_common/src/esp_err_to_name.c || { echo 'Differences found between running under Python 2 and 3.'; exit 1; } test_esp_efuse_table_on_host: extends: .host_test_template From e3e21b7954580431142ccd8fd527d7113aa00241 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 29 Aug 2019 18:53:16 +1000 Subject: [PATCH 067/146] build system: Pass Python executable through to bootloader build Fixes failures if the PYTHON cache setting doesn't match the default of "python" on the PATH. --- components/bootloader/project_include.cmake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/bootloader/project_include.cmake b/components/bootloader/project_include.cmake index 019d1df71e..fa26fd097f 100644 --- a/components/bootloader/project_include.cmake +++ b/components/bootloader/project_include.cmake @@ -96,12 +96,13 @@ endif() idf_build_get_property(idf_path IDF_PATH) idf_build_get_property(idf_target IDF_TARGET) idf_build_get_property(sdkconfig SDKCONFIG) +idf_build_get_property(python PYTHON) externalproject_add(bootloader SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/subproject" BINARY_DIR "${BOOTLOADER_BUILD_DIR}" CMAKE_ARGS -DSDKCONFIG=${sdkconfig} -DIDF_PATH=${idf_path} -DIDF_TARGET=${idf_target} - -DPYTHON_DEPS_CHECKED=1 + -DPYTHON_DEPS_CHECKED=1 -DPYTHON=${python} -DEXTRA_COMPONENT_DIRS=${CMAKE_CURRENT_LIST_DIR} ${sign_key_arg} ${ver_key_arg} # LEGACY_INCLUDE_COMMON_HEADERS has to be passed in via cache variable since From d12503eb55765e0aae2dad2a28ce3dcca45c5a9f Mon Sep 17 00:00:00 2001 From: baohongde Date: Thu, 29 Aug 2019 19:58:05 +0800 Subject: [PATCH 068/146] components/bt: Remove unused macro in menuconfig --- components/bt/host/bluedroid/Kconfig.in | 10 ---------- .../common/include/common/bluedroid_user_config.h | 15 --------------- .../bluedroid/common/include/common/bt_target.h | 5 ----- components/bt/sdkconfig.rename | 2 -- .../coex/a2dp_gatts_coex/sdkconfig.defaults | 1 - 5 files changed, 33 deletions(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index 21b3fc985a..8d6d80768b 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -54,16 +54,6 @@ config BT_A2DP_ENABLE help Advanced Audio Distrubution Profile -config BT_A2DP_SINK_TASK_STACK_SIZE - int "A2DP sink (audio stream decoding) task stack size" - depends on BT_A2DP_ENABLE - default 2048 - -config BT_A2DP_SOURCE_TASK_STACK_SIZE - int "A2DP source (audio stream encoding) task stack size" - depends on BT_A2DP_ENABLE - default 2048 - config BT_SPP_ENABLED bool "SPP" depends on BT_CLASSIC_ENABLED diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index b107a01e15..e14773310a 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -19,21 +19,6 @@ #include "bt_common.h" #include "bt_user_config.h" -/********************************************************** - * Thread/Task reference - **********************************************************/ - -#ifdef CONFIG_A2DP_SINK_TASK_STACK_SIZE -#define UC_A2DP_SINK_TASK_STACK_SIZE CONFIG_A2DP_SINK_TASK_STACK_SIZE -#else -#define UC_A2DP_SINK_TASK_STACK_SIZE 2048 -#endif -#ifdef CONFIG_A2DP_SOURCE_TASK_STACK_SIZE -#define UC_A2DP_SOURCE_TASK_STACK_SIZE CONFIG_A2DP_SOURCE_TASK_STACK_SIZE -#else -#define UC_A2DP_SOURCE_TASK_STACK_SIZE 2048 -#endif - /********************************************************** * Profile reference **********************************************************/ diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index 146a1ad110..a59d3793e0 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -41,11 +41,6 @@ #include "stack/dyn_mem.h" /* defines static and/or dynamic memory for components */ - -/* OS Configuration from User config (eg: sdkconfig) */ -#define A2DP_SINK_TASK_STACK_SIZE UC_A2DP_SINK_TASK_STACK_SIZE -#define A2DP_SOURCE_TASK_STACK_SIZE UC_A2DP_SOURCE_TASK_STACK_SIZE - /****************************************************************************** ** ** Classic BT features diff --git a/components/bt/sdkconfig.rename b/components/bt/sdkconfig.rename index 73c222685d..2affb7cb77 100644 --- a/components/bt/sdkconfig.rename +++ b/components/bt/sdkconfig.rename @@ -42,8 +42,6 @@ CONFIG_BTU_TASK_STACK_SIZE CONFIG_BT_BTU_TASK_S CONFIG_BLUEDROID_MEM_DEBUG CONFIG_BT_BLUEDROID_MEM_DEBUG CONFIG_CLASSIC_BT_ENABLED CONFIG_BT_CLASSIC_ENABLED CONFIG_A2DP_ENABLE CONFIG_BT_A2DP_ENABLE -CONFIG_A2DP_SINK_TASK_STACK_SIZE CONFIG_BT_A2DP_SINK_TASK_STACK_SIZE -CONFIG_A2DP_SOURCE_TASK_STACK_SIZE CONFIG_BT_A2DP_SOURCE_TASK_STACK_SIZE CONFIG_HFP_ENABLE CONFIG_BT_HFP_ENABLE CONFIG_HFP_ROLE CONFIG_BT_HFP_ROLE CONFIG_HFP_CLIENT_ENABLE CONFIG_BT_HFP_CLIENT_ENABLE diff --git a/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/sdkconfig.defaults b/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/sdkconfig.defaults index 9c42997e13..ca97b31243 100644 --- a/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/sdkconfig.defaults +++ b/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/sdkconfig.defaults @@ -19,7 +19,6 @@ CONFIG_BT_CLASSIC_ENABLED=y CONFIG_BT_A2DP_ENABLE=y CONFIG_A2DP_SINK_ENABLE=y CONFIG_A2DP_SRC_ENABLE=n -CONFIG_BT_A2DP_SINK_TASK_STACK_SIZE=2048 CONFIG_BT_SPP_ENABLED=n CONFIG_BT_GATTS_ENABLE=y CONFIG_BT_GATTC_ENABLE=n From abd6d40796ea409765f972ad4fed4bc82db64d9a Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 30 Aug 2019 09:35:47 +1000 Subject: [PATCH 069/146] secure boot: Ensure mbedTLS enables ECDSA if signatures are checked in app and all ECDSA to be disabled if secure boot is not enabled Previously if ECDSA disabled in config then secure_boot_signatures.c would fail to build (whether or not secure boot was enabled). To avoid breaking apps that might be using the signature scheme with custom OTA without enabling secure boot signatures in config, this change just disables this functionality if unavailable in mbedTLS config. Possible fix for root cause of https://github.com/espressif/esp-idf/pull/3703 --- components/bootloader/Kconfig.projbuild | 5 ++++- .../bootloader_support/src/idf/secure_boot_signatures.c | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/components/bootloader/Kconfig.projbuild b/components/bootloader/Kconfig.projbuild index 02ef42f5a5..3aaa3eaf8c 100644 --- a/components/bootloader/Kconfig.projbuild +++ b/components/bootloader/Kconfig.projbuild @@ -281,12 +281,15 @@ menu "Security features" config SECURE_SIGNED_ON_UPDATE bool default y - select MBEDTLS_ECP_DP_SECP256R1_ENABLED depends on SECURE_BOOT_ENABLED || SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT config SECURE_SIGNED_APPS bool default y + select MBEDTLS_ECP_DP_SECP256R1_ENABLED + select MBEDTLS_ECP_C + select MBEDTLS_ECDH_C + select MBEDTLS_ECDSA_C depends on SECURE_SIGNED_ON_BOOT || SECURE_SIGNED_ON_UPDATE diff --git a/components/bootloader_support/src/idf/secure_boot_signatures.c b/components/bootloader_support/src/idf/secure_boot_signatures.c index 14e8faea53..df1424124b 100644 --- a/components/bootloader_support/src/idf/secure_boot_signatures.c +++ b/components/bootloader_support/src/idf/secure_boot_signatures.c @@ -56,6 +56,10 @@ esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length) esp_err_t esp_secure_boot_verify_signature_block(const esp_secure_boot_sig_block_t *sig_block, const uint8_t *image_digest) { +#if !(defined(CONFIG_MBEDTLS_ECDSA_C) && defined(CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED)) + ESP_LOGE(TAG, "Signature verification requires ECDSA & SECP256R1 curve enabled"); + return ESP_ERR_NOT_SUPPORTED; +#else ptrdiff_t keylen; keylen = signature_verification_key_end - signature_verification_key_start; @@ -117,4 +121,5 @@ cleanup: mbedtls_mpi_free(&s); mbedtls_ecdsa_free(&ecdsa_context); return ret == 0 ? ESP_OK : ESP_ERR_IMAGE_INVALID; +#endif // CONFIG_MBEDTLS_ECDSA_C && CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED } From 6123f78897ad8e656f34226b29a768e89a5fd53f Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 30 Aug 2019 15:01:43 +1000 Subject: [PATCH 070/146] mbedtls: Make ECDHE-PSK config item depend on ECDHE --- components/mbedtls/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mbedtls/Kconfig b/components/mbedtls/Kconfig index e597af2824..a9e7983bd2 100644 --- a/components/mbedtls/Kconfig +++ b/components/mbedtls/Kconfig @@ -241,7 +241,7 @@ menu "mbedTLS" config MBEDTLS_KEY_EXCHANGE_ECDHE_PSK bool "Enable ECDHE-PSK based ciphersuite modes" - depends on MBEDTLS_PSK_MODES + depends on MBEDTLS_PSK_MODES && MBEDTLS_ECDH_C default y help Enable to support Elliptic-Curve-Diffie-Hellman PSK (pre-shared-key) TLS authentication modes. From 70b1ff7bc6bdeb1d2ee27dd23aa53bacfca92b85 Mon Sep 17 00:00:00 2001 From: joedeveloper Date: Sun, 31 Mar 2019 05:45:20 +0700 Subject: [PATCH 071/146] docs: Explain ncurses5 for other distributions as well Ran into ncurses5 dependency issue on popOS stub section for covering libncurses 5 workarounds on distributions that need it, when only Arch was mentioned it was too easy to skip over the section Merges https://github.com/espressif/esp-idf/pull/3236 --- docs/en/get-started/linux-setup.rst | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/docs/en/get-started/linux-setup.rst b/docs/en/get-started/linux-setup.rst index a9afdfc207..c27f91f748 100644 --- a/docs/en/get-started/linux-setup.rst +++ b/docs/en/get-started/linux-setup.rst @@ -32,22 +32,20 @@ Permission issues /dev/ttyUSB0 With some Linux distributions you may get the ``Failed to open port /dev/ttyUSB0`` error message when flashing the ESP32. :ref:`This can be solved by adding the current user to the dialout group`. +ncurses 5 dependency +-------------------- -Arch Linux Users ----------------- +To run the precompiled gdb (xtensa-esp32-elf-gdb) in Linux requires ncurses 5, but some newer distributions only provide ncurses 6 by default. -To run the precompiled gdb (xtensa-esp32-elf-gdb) in Arch Linux requires ncurses 5, but Arch uses ncurses 6. +Consult your distribution's documentation to see if ncurses 5 libraries are available. Alternatively, use crosstool-NG to compile a gdb that links against ncurses 6. -Backwards compatibility libraries are available in AUR_ for native and lib32 configurations: +For Arch Linux users, ncurses 5 libraries are available in AUR_ for native and lib32 configurations: - https://aur.archlinux.org/packages/ncurses5-compat-libs/ - https://aur.archlinux.org/packages/lib32-ncurses5-compat-libs/ Before installing these packages you might need to add the author's public key to your keyring as described in the "Comments" section at the links above. -Alternatively, use crosstool-NG to compile a gdb that links against ncurses 6. - - Next Steps ========== From 3af18c0422f9ee249d37d1ea80b25f2c911e5d17 Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Fri, 30 Aug 2019 21:18:34 +0800 Subject: [PATCH 072/146] fix some coex bugs 1. fix the bug that bb reset lock unhandled may cause assert in vPortCPUReleaseMutexIntsDisabledInternal 2. fix wifi mac reset may blocking then cause wdt timeout 3. fix bug wifi mac reset before coex init --- components/esp_wifi/lib_esp32 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/lib_esp32 b/components/esp_wifi/lib_esp32 index 8b2b62efb3..63590d0f5e 160000 --- a/components/esp_wifi/lib_esp32 +++ b/components/esp_wifi/lib_esp32 @@ -1 +1 @@ -Subproject commit 8b2b62efb3ce88b053e41c21a1cdb3d1d409e32e +Subproject commit 63590d0f5e024ad8baf4191ca6195bb7cf514381 From 91346438077f92b462ef662140ce16b2fd536592 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 2 Sep 2019 10:56:43 +1000 Subject: [PATCH 073/146] idf.py: Remove dead code around ccache base dir --- tools/idf.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tools/idf.py b/tools/idf.py index 4c35b5ca9f..bce234e9ca 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -310,15 +310,6 @@ def build_target(target_name, ctx, args): _ensure_build_directory(args) generator_cmd = GENERATOR_CMDS[args.generator] - if args.ccache: - # Setting CCACHE_BASEDIR & CCACHE_NO_HASHDIR ensures that project paths aren't stored in the ccache entries - # (this means ccache hits can be shared between different projects. It may mean that some debug information - # will point to files in another project, if these files are perfect duplicates of each other.) - # - # It would be nicer to set these from cmake, but there's no cross-platform way to set build-time environment - # os.environ["CCACHE_BASEDIR"] = args.build_dir - # os.environ["CCACHE_NO_HASHDIR"] = "1" - pass if args.verbose: generator_cmd += [GENERATOR_VERBOSE[args.generator]] From c310d40299b58e0fd147c4ee28579be275f20fa7 Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 7 Aug 2019 16:33:42 +0800 Subject: [PATCH 074/146] ble_mesh: add ble mesh example description --- examples/bluetooth/esp_ble_mesh/README.md | 74 +++++++++++++++++++ .../ble_mesh_fast_prov_client/README.md | 8 +- .../ble_mesh_fast_prov_server/README.md | 8 +- 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 examples/bluetooth/esp_ble_mesh/README.md diff --git a/examples/bluetooth/esp_ble_mesh/README.md b/examples/bluetooth/esp_ble_mesh/README.md new file mode 100644 index 0000000000..43ed0cabb6 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/README.md @@ -0,0 +1,74 @@ +# ESP BLE Mesh Examples + +[ESP BLE Mesh]($IDF_PATH/components/bt/esp_ble_mesh/) is the official BLE Mesh stack of Espressif Systems. We will provide long-term support for new features, performance optimization, etc. + +Please help note that breaking changes may be introduced into ESP BLE Mesh on [minor IDF versions](https://docs.espressif.com/projects/esp-idf/en/latest/versions.html). + +Note: To use examples in this directory, you need to have Bluetooth enabled in configuration and Bluedroid selected as the host stack. ESP BLE Mesh will support NimBLE host soon, and currently the integration of NimBLE host with ESP BLE Mesh stack is under development. + +# Example Layout + +This directory includes examples to demonstrate BLE Mesh functionality based on Zephyr (https://github.com/zephyrproject-rtos/zephyr) Mesh stack. + +## ble_mesh_client_model + +This example shows how ESP32 acts as a BLE Mesh Node with Generic OnOff Client & Server Models in the Primary Element. + +See the [README.md](./ble_mesh_client_model/README.md) file in the example [ble_mesh_client_model](./ble_mesh_client_model/). + +## ble_mesh_console + +This example demonstrates how ESP BLE Mesh uses Console for message transmitting/receiving tests. + +### ble_mesh_node + +This example shows how ESP32 acts as a BLE Mesh Node and sends vendor messages for testing. + +See the [README.md](./ble_mesh_console/ble_mesh_node/README.md) file in the example [ble_mesh_node](./ble_mesh_console/ble_mesh_node/). + +### ble_mesh_provisioner + +This example shows how ESP32 acts as a BLE Mesh Provisioner and sends vendor messages for testing. + +See the [README.md](./ble_mesh_console/ble_mesh_provisioner/README.md) file in the example [ble_mesh_provisioner](./ble_mesh_console/ble_mesh_provisioner/). + +## ble_mesh_fast_provision + +This example illustrates the solution of ESP BLE Mesh Fast Provisioning. + +### ble_mesh_fast_prov_client + +This example shows how ESP32, acting as a BLE Mesh Fast Provisioning Client, provisions other unprovisioned devices and then control the nodes. + +See the [README.md](./ble_mesh_fast_provision/ble_mesh_fast_prov_client/README.md) file in the example [ble_mesh_fast_prov_client](./ble_mesh_fast_provision/ble_mesh_fast_prov_client/). + +### ble_mesh_fast_prov_server + +This example illustrates the process that: +1. Firstly as a BLE Mesh Fast Provisioning Server, ESP32 is provisioned into a Node; +2. and then provisions other unprovisioned devices as a Temporary Provisioner. + +See the [README.md](./ble_mesh_fast_provision/ble_mesh_fast_prov_server/README.md) file in the example [ble_mesh_fast_prov_server](./ble_mesh_fast_provision/ble_mesh_fast_prov_server/). + +## ble_mesh_node + +This example shows how ESP32 acts as a BLE Mesh Node with only Generic OnOff Server Model in the Primary Element. + +See the [README.md](./ble_mesh_node/README.md) file in the example [ble_mesh_node](./ble_mesh_node/). + +## ble_mesh_provisioner + +This example shows how ESP32 acts as a BLE Mesh Provisioner and provisions other unprovisioned devices. + +See the [README.md](./ble_mesh_provisioner/README.md) file in the example [ble_mesh_provisioner](./ble_mesh_provisioner/). + +## ble_mesh_wifi_coexist + +This example shows how ESP32 acts as a BLE Mesh Fast Provisioning Server and coexists with Wi-Fi iperf functionality. + +See the [README.md](./ble_mesh_wifi_coexist/README.md) file in the example [ble_mesh_wifi_coexist](./ble_mesh_wifi_coexist/). + +# More + +See the [README.md](../../README.md) file in the upper level [examples](../../) directory for more information about examples. + diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/README.md b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/README.md index 9e45f4f31b..d49238e974 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/README.md +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/README.md @@ -1,2 +1,6 @@ -ESP BLE Mesh Fast Provisioning Client Demo -======================== \ No newline at end of file +ESP BLE Mesh Fast Provisioning Client example +======================== + +This example shows how a BLE Mesh device functions as a Fast Provisioning Client. + +Please check the [tutorial](tutorial/ble_mesh_fast_provision_client.md) for more information about this example. \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/README.md b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/README.md index 2746ed629a..e12c5d3fb4 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/README.md +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/README.md @@ -1,2 +1,6 @@ -ESP BLE Mesh Fast Provisioning Server Demo -======================== \ No newline at end of file +ESP BLE Mesh Fast Provisioning Server example +======================== + +This example shows how a BLE Mesh device functions as a Fast Provisioning Server. + +Please check the [tutorial](tutorial/ble_mesh_fast_provision_server.md) for more information about this example. \ No newline at end of file From 75578300f1e11d79d496434f7c394a76b20bb22b Mon Sep 17 00:00:00 2001 From: nif Date: Thu, 20 Jun 2019 12:01:02 +0200 Subject: [PATCH 075/146] BT HFP: Add AT+NREC=0 command for disabling AG echo cancellation. --- .../bt/host/bluedroid/api/esp_hf_client_api.c | 16 +++++++++++++ .../api/include/api/esp_hf_client_api.h | 14 +++++++++++ .../btc/profile/std/hf_client/btc_hf_client.c | 24 +++++++++++++++++++ .../btc/profile/std/include/btc_hf_client.h | 1 + 4 files changed, 55 insertions(+) diff --git a/components/bt/host/bluedroid/api/esp_hf_client_api.c b/components/bt/host/bluedroid/api/esp_hf_client_api.c index 7db990206a..c0a6cbe800 100644 --- a/components/bt/host/bluedroid/api/esp_hf_client_api.c +++ b/components/bt/host/bluedroid/api/esp_hf_client_api.c @@ -432,6 +432,22 @@ esp_err_t esp_hf_client_request_last_voice_tag_number(void) return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; } +esp_err_t esp_hf_client_send_nrec(void) +{ + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + btc_msg_t msg; + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_HF_CLIENT; + msg.act = BTC_HF_CLIENT_SEND_NREC_EVT; + + /* Switch to BTC context */ + bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL); + return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL; +} + esp_err_t esp_hf_client_register_data_callback(esp_hf_client_incoming_data_cb_t recv, esp_hf_client_outgoing_data_cb_t send) { diff --git a/components/bt/host/bluedroid/api/include/api/esp_hf_client_api.h b/components/bt/host/bluedroid/api/include/api/esp_hf_client_api.h index 8e3dc956b7..d82308b2cf 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_hf_client_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_hf_client_api.h @@ -578,6 +578,20 @@ esp_err_t esp_hf_client_send_dtmf(char code); */ esp_err_t esp_hf_client_request_last_voice_tag_number(void); +/** + * + * @brief Disable echo cancellation and noise reduction in the AG (use AT+NREC=0 command) + * As a precondition to use this API, Service Level Connection shall exist with AG + * + * @return + * - ESP_OK: NREC=0 request is sent to lower layer + * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled + * - ESP_FAIL: others + * + */ +esp_err_t esp_hf_client_send_nrec(void); + + /** * @brief Register HFP client data output function; the callback is only used in * the case that Voice Over HCI is enabled. diff --git a/components/bt/host/bluedroid/btc/profile/std/hf_client/btc_hf_client.c b/components/bt/host/bluedroid/btc/profile/std/hf_client/btc_hf_client.c index d3ccd66997..64fb8e2bac 100644 --- a/components/bt/host/bluedroid/btc/profile/std/hf_client/btc_hf_client.c +++ b/components/bt/host/bluedroid/btc/profile/std/hf_client/btc_hf_client.c @@ -630,6 +630,27 @@ static bt_status_t btc_hf_client_request_last_voice_tag_number(void) return BT_STATUS_UNSUPPORTED; } +/******************************************************************************* +** +** Function btc_hf_client_send_nrec +** +** Description Request AG to disable echo cancellation & noise reduction +** +** Returns bt_status_t +** +*******************************************************************************/ +static bt_status_t btc_hf_client_send_nrec(void) +{ + CHECK_HF_CLIENT_SLC_CONNECTED(); + + if (hf_client_local_param.btc_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_ECNR) + { + BTA_HfClientSendAT(hf_client_local_param.btc_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_NREC, 0, 0, NULL); + return BT_STATUS_SUCCESS; + } + return BT_STATUS_UNSUPPORTED; +} + /******************************************************************************* ** ** Function bte_hf_client_evt @@ -1073,6 +1094,9 @@ void btc_hf_client_call_handler(btc_msg_t *msg) case BTC_HF_CLIENT_REGISTER_DATA_CALLBACK_EVT: btc_hf_client_reg_data_cb(arg->reg_data_cb.recv, arg->reg_data_cb.send); break; + case BTC_HF_CLIENT_SEND_NREC_EVT: + btc_hf_client_send_nrec(); + break; default: BTC_TRACE_WARNING("%s : unhandled event: %d\n", __FUNCTION__, msg->act); } diff --git a/components/bt/host/bluedroid/btc/profile/std/include/btc_hf_client.h b/components/bt/host/bluedroid/btc/profile/std/include/btc_hf_client.h index 04226e72a2..e3befc9f4a 100644 --- a/components/bt/host/bluedroid/btc/profile/std/include/btc_hf_client.h +++ b/components/bt/host/bluedroid/btc/profile/std/include/btc_hf_client.h @@ -57,6 +57,7 @@ typedef enum { BTC_HF_CLIENT_SEND_DTMF_EVT, BTC_HF_CLIENT_REQUEST_LAST_VOICE_TAG_NUMBER_EVT, BTC_HF_CLIENT_REGISTER_DATA_CALLBACK_EVT, + BTC_HF_CLIENT_SEND_NREC_EVT, } btc_hf_client_act_t; /* btc_hf_client_args_t */ From 185f47ab7e77843f1ab20b30e3f1f650530b9933 Mon Sep 17 00:00:00 2001 From: baohongde Date: Mon, 2 Sep 2019 20:50:09 +0800 Subject: [PATCH 076/146] components/bt: Disable Wide Bond Speech when SCO data path is PCM --- components/bt/host/bluedroid/Kconfig.in | 8 ++++++++ .../bt/host/bluedroid/bta/hf_client/bta_hf_client_main.c | 4 ++++ .../common/include/common/bluedroid_user_config.h | 6 ++++++ .../bt/host/bluedroid/common/include/common/bt_target.h | 2 +- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index 21b3fc985a..6e225b9ec2 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -98,6 +98,14 @@ choice BT_HFP_AUDIO_DATA_PATH bool "HCI" endchoice +config BT_HFP_WBS_ENABLE + bool "Wide Band Speech" + depends on BT_HFP_AUDIO_DATA_PATH_HCI + default y + help + This enables Wide Band Speech. Should disable it when SCO data path is PCM. + Otherwise there will be no data transmited via GPIOs. + config BT_SSP_ENABLED bool "Secure Simple Pairing" depends on BT_CLASSIC_ENABLED diff --git a/components/bt/host/bluedroid/bta/hf_client/bta_hf_client_main.c b/components/bt/host/bluedroid/bta/hf_client/bta_hf_client_main.c index dcaf665dbc..8eb44a9d65 100644 --- a/components/bt/host/bluedroid/bta/hf_client/bta_hf_client_main.c +++ b/components/bt/host/bluedroid/bta/hf_client/bta_hf_client_main.c @@ -232,7 +232,11 @@ const tBTA_HF_CLIENT_ST_TBL bta_hf_client_st_tbl[] = { bta_hf_client_st_closing }; +#if BTM_WBS_INCLUDED const char *bta_hf_client_version = "1.6"; +#else +const char *bta_hf_client_version = "1.5"; +#endif /* HF Client control block */ #if BTA_DYNAMIC_MEMORY == FALSE diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index b107a01e15..0fcb7d9ff8 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -176,6 +176,12 @@ #define UC_BT_HFP_AUDIO_DATA_PATH_HCI FALSE #endif +//Wide Band Speech +#ifdef CONFIG_BT_HFP_WBS_ENABLE +#define UC_BT_HFP_WBS_ENABLE CONFIG_BT_HFP_WBS_ENABLE +#else +#define UC_BT_HFP_WBS_ENABLE FALSE +#endif /********************************************************** * Memory reference diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index 146a1ad110..6801d6ebe0 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -592,7 +592,7 @@ /* Includes WBS if TRUE */ #ifndef BTM_WBS_INCLUDED -#define BTM_WBS_INCLUDED FALSE /* TRUE includes WBS code */ +#define BTM_WBS_INCLUDED UC_BT_HFP_WBS_ENABLE /* TRUE includes WBS code */ #endif /* This is used to work around a controller bug that doesn't like Disconnect From 9a7ab28cc28c6ac570f0bc7adfc51b943f74d324 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 2 Sep 2019 10:56:56 +1000 Subject: [PATCH 077/146] idf.py: Add environment variable to enable ccache by default --- docs/en/api-guides/build-system.rst | 13 +++++++++++++ tools/ci/test_build_system_cmake.sh | 6 +++--- tools/cmake/project.cmake | 2 +- tools/idf.py | 4 ++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/en/api-guides/build-system.rst b/docs/en/api-guides/build-system.rst index 30d59bf8fa..408464ff45 100644 --- a/docs/en/api-guides/build-system.rst +++ b/docs/en/api-guides/build-system.rst @@ -99,6 +99,18 @@ Advanced Commands The order of multiple ``idf.py`` commands on the same invocation is not important, they will automatically be executed in the correct order for everything to take effect (ie building before flashing, erasing before flashing, etc.). +idf.py options +^^^^^^^^^^^^^^ + +To list all available options, run ``idf.py --help``. + +- ``-C `` allows overriding the project directory from the default current working directory. +- ``-B `` allows overriding the build directory from the default ``build`` subdirectory of the project directory. +- ``--ccache`` flag can be used to enable CCache_ when compiling source files, if the CCache_ tool is installed. This can dramatically reduce some build times. + +Note that some older versions of CCache may exhibit bugs on some platforms, so if files are not rebuilt as expected then try disabling ccache and build again. CCache can be enabled by default by setting the ``IDF_ENABLE_CCACHE`` environment variable to a non-zero value. +- ``-v`` flag causes both ``idf.py`` and the build system to produce verbose build output. This can be useful for debugging build problems. + Using CMake Directly -------------------- @@ -1365,3 +1377,4 @@ Flashing from make .. _quirc: https://github.com/dlbeer/quirc .. _pyenv: https://github.com/pyenv/pyenv#README .. _virtualenv: https://virtualenv.pypa.io/en/stable/ +.. _CCache: https://ccache.dev/ diff --git a/tools/ci/test_build_system_cmake.sh b/tools/ci/test_build_system_cmake.sh index 47e1ae23ee..2aceedced9 100755 --- a/tools/ci/test_build_system_cmake.sh +++ b/tools/ci/test_build_system_cmake.sh @@ -439,11 +439,11 @@ endmenu\n" >> ${IDF_PATH}/Kconfig; print_status "Check ccache is used to build" touch ccache && chmod +x ccache # make sure that ccache is present for this test - (export PATH=$PWD:$PATH && idf.py --ccache reconfigure | grep "ccache will be used for faster builds") || failure "ccache should be used when --cache is specified" + (export PATH=$PWD:$PATH && idf.py --ccache reconfigure | grep "ccache will be used") || failure "ccache should be used when --cache is specified" idf.py fullclean - (export PATH=$PWD:$PATH && idf.py reconfigure| grep -c "ccache will be used for faster builds" | grep -wq 0) \ + (export PATH=$PWD:$PATH && idf.py reconfigure| grep -c "ccache will be used" | grep -wq 0) \ || failure "ccache should not be used even when present if --ccache is not specified" - (export PATH=$PWD:$PATH && idf.py --no-ccache reconfigure| grep -c "ccache will be used for faster builds" | grep -wq 0) \ + (export PATH=$PWD:$PATH && idf.py --no-ccache reconfigure| grep -c "ccache will be used" | grep -wq 0) \ || failure "--no-ccache causes no issue for backward compatibility" rm -f ccache diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index 3deb014889..0208f3e9e6 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -247,7 +247,7 @@ macro(project project_name) if(CCACHE_ENABLE) find_program(CCACHE_FOUND ccache) if(CCACHE_FOUND) - message(STATUS "ccache will be used for faster builds") + message(STATUS "ccache will be used for faster recompilation") set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) else() message(WARNING "enabled ccache in build but ccache program not found") diff --git a/tools/idf.py b/tools/idf.py index bce234e9ca..894543469c 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -988,9 +988,9 @@ def init_cli(): }, { "names": ["--ccache/--no-ccache"], - "help": "Use ccache in build. Disabled by default.", + "help": "Use ccache in build. Disabled by default, unless IDF_CCACHE_ENABLE environment variable is set to a non-zero value.", "is_flag": True, - "default": False, + "default": os.getenv("IDF_CCACHE_ENABLE") not in [None, "", "0"], }, { "names": ["-G", "--generator"], From f8212988c4c9c52849c96dd60e80f4376cfaafc0 Mon Sep 17 00:00:00 2001 From: Zac Bond Date: Wed, 28 Aug 2019 17:30:07 -0400 Subject: [PATCH 078/146] Use CONFIG value instead of hard-coded value. Add intermediate defines. Use CONFIG_BTU_TASK_STACK_SIZE instead of hard-coded value in Bluedroid component --- .../common/include/common/bluedroid_user_config.h | 10 ++++++++++ .../host/bluedroid/common/include/common/bt_target.h | 4 ++++ components/bt/host/bluedroid/stack/btu/btu_init.c | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index e14773310a..82082e1ea1 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -19,6 +19,16 @@ #include "bt_common.h" #include "bt_user_config.h" +/********************************************************** + * Thread/Task reference + **********************************************************/ +#ifdef CONFIG_BT_BTU_TASK_STACK_SIZE +#define UC_BT_BTU_TASK_STACK_SIZE CONFIG_BT_BTU_TASK_STACK_SIZE +#else +#define UC_BT_BTU_TASK_STACK_SIZE 4096 +#endif + + /********************************************************** * Profile reference **********************************************************/ diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index a59d3793e0..bf5600580a 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -41,6 +41,10 @@ #include "stack/dyn_mem.h" /* defines static and/or dynamic memory for components */ + +/* OS Configuration from User config (eg: sdkconfig) */ +#define BT_BTU_TASK_STACK_SIZE UC_BT_BTU_TASK_STACK_SIZE + /****************************************************************************** ** ** Classic BT features diff --git a/components/bt/host/bluedroid/stack/btu/btu_init.c b/components/bt/host/bluedroid/stack/btu/btu_init.c index 80678bef82..430cd65af6 100644 --- a/components/bt/host/bluedroid/stack/btu/btu_init.c +++ b/components/bt/host/bluedroid/stack/btu/btu_init.c @@ -45,7 +45,7 @@ #endif #define BTU_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) -#define BTU_TASK_STACK_SIZE (4096 + BT_TASK_EXTRA_STACK_SIZE) +#define BTU_TASK_STACK_SIZE (BT_BTU_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) #define BTU_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 5) #define BTU_TASK_NAME "btuT" From 019067b6ac3dddb60458154b4743e177652ae93d Mon Sep 17 00:00:00 2001 From: baohongde Date: Fri, 30 Aug 2019 10:58:28 +0800 Subject: [PATCH 079/146] components/bt: Rename some macros --- .../bluedroid/common/include/common/bluedroid_user_config.h | 6 +++--- .../bt/host/bluedroid/common/include/common/bt_target.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index 82082e1ea1..a71cf85c07 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -22,10 +22,10 @@ /********************************************************** * Thread/Task reference **********************************************************/ -#ifdef CONFIG_BT_BTU_TASK_STACK_SIZE -#define UC_BT_BTU_TASK_STACK_SIZE CONFIG_BT_BTU_TASK_STACK_SIZE +#ifdef CONFIG_BTU_TASK_STACK_SIZE +#define UC_BTU_TASK_STACK_SIZE CONFIG_BTU_TASK_STACK_SIZE #else -#define UC_BT_BTU_TASK_STACK_SIZE 4096 +#define UC_BTU_TASK_STACK_SIZE 4096 #endif diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index bf5600580a..c958204836 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -43,7 +43,7 @@ /* OS Configuration from User config (eg: sdkconfig) */ -#define BT_BTU_TASK_STACK_SIZE UC_BT_BTU_TASK_STACK_SIZE +#define BT_BTU_TASK_STACK_SIZE UC_BTU_TASK_STACK_SIZE /****************************************************************************** ** From 14ffd355e6a24574d1ed2898794955a30c1aba22 Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Wed, 28 Aug 2019 16:04:50 +0800 Subject: [PATCH 080/146] ethernet: bugfix and optimize 1. check frame length before pass to stack 2. replace semaphore with task notify --- components/esp_eth/Kconfig | 4 +- components/esp_eth/src/esp_eth_mac_dm9051.c | 6 ++- components/esp_eth/src/esp_eth_mac_esp32.c | 41 +++++++++------------ 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/components/esp_eth/Kconfig b/components/esp_eth/Kconfig index 40e7ee36dc..8e894d397d 100644 --- a/components/esp_eth/Kconfig +++ b/components/esp_eth/Kconfig @@ -114,7 +114,7 @@ menu "Ethernet" config ETH_DMA_RX_BUFFER_NUM int "Amount of Ethernet DMA Rx buffers" - range 3 20 + range 3 30 default 10 help Number of DMA receive buffers. Each buffer's size is ETH_DMA_BUFFER_SIZE. @@ -122,7 +122,7 @@ menu "Ethernet" config ETH_DMA_TX_BUFFER_NUM int "Amount of Ethernet DMA Tx buffers" - range 3 20 + range 3 30 default 10 help Number of DMA transmit buffers. Each buffer's size is ETH_DMA_BUFFER_SIZE. diff --git a/components/esp_eth/src/esp_eth_mac_dm9051.c b/components/esp_eth/src/esp_eth_mac_dm9051.c index b76c5a4fd5..b2878cb134 100644 --- a/components/esp_eth/src/esp_eth_mac_dm9051.c +++ b/components/esp_eth/src/esp_eth_mac_dm9051.c @@ -511,7 +511,11 @@ static void emac_dm9051_task(void *arg) buffer = (uint8_t *)heap_caps_malloc(ETH_MAX_PACKET_SIZE, MALLOC_CAP_DMA); if (emac->parent.receive(&emac->parent, buffer, &length) == ESP_OK) { /* pass the buffer to stack (e.g. TCP/IP layer) */ - emac->eth->stack_input(emac->eth, buffer, length); + if (length) { + emac->eth->stack_input(emac->eth, buffer, length); + } else { + free(buffer); + } } else { free(buffer); } diff --git a/components/esp_eth/src/esp_eth_mac_esp32.c b/components/esp_eth/src/esp_eth_mac_esp32.c index cea8868a3b..bbc3df22da 100644 --- a/components/esp_eth/src/esp_eth_mac_esp32.c +++ b/components/esp_eth/src/esp_eth_mac_esp32.c @@ -48,7 +48,6 @@ typedef struct { esp_eth_mediator_t *eth; emac_hal_context_t hal; intr_handle_t intr_hdl; - SemaphoreHandle_t rx_counting_sem; TaskHandle_t rx_task_hdl; uint32_t sw_reset_timeout_ms; uint32_t frames_remain; @@ -237,18 +236,20 @@ static void emac_esp32_rx_task(void *arg) uint8_t *buffer = NULL; uint32_t length = 0; while (1) { - if (xSemaphoreTake(emac->rx_counting_sem, pdMS_TO_TICKS(RX_QUEUE_WAIT_MS)) == pdTRUE) { - buffer = (uint8_t *)malloc(ETH_MAX_PACKET_SIZE); - if (emac_esp32_receive(&emac->parent, buffer, &length) == ESP_OK) { - /* pass the buffer to stack (e.g. TCP/IP layer) */ - emac->eth->stack_input(emac->eth, buffer, length); - } else { - free(buffer); - } - } - /* there might be some frames left in DMA buffer */ - else if (emac->frames_remain) { - xSemaphoreGive(emac->rx_counting_sem); + if (ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(RX_QUEUE_WAIT_MS))) { + do { + buffer = (uint8_t *)malloc(ETH_MAX_PACKET_SIZE); + if (emac_esp32_receive(&emac->parent, buffer, &length) == ESP_OK) { + /* pass the buffer to stack (e.g. TCP/IP layer) */ + if (length) { + emac->eth->stack_input(emac->eth, buffer, length); + } else { + free(buffer); + } + } else { + free(buffer); + } + } while (emac->frames_remain); } } vTaskDelete(NULL); @@ -331,7 +332,6 @@ static esp_err_t emac_esp32_del(esp_eth_mac_t *mac) emac_esp32_t *emac = __containerof(mac, emac_esp32_t, parent); esp_intr_free(emac->intr_hdl); vTaskDelete(emac->rx_task_hdl); - vSemaphoreDelete(emac->rx_counting_sem); int i = 0; for (i = 0; i < CONFIG_ETH_DMA_RX_BUFFER_NUM; i++) { free(emac->hal.rx_buf[i]); @@ -416,17 +416,12 @@ esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_mac_config_t *config) MAC_CHECK(esp_intr_alloc(ETS_ETH_MAC_INTR_SOURCE, ESP_INTR_FLAG_IRAM, emac_esp32_isr_handler, &emac->hal, &(emac->intr_hdl)) == ESP_OK, "alloc emac interrupt failed", err_intr, NULL); - /* create counting semaphore */ - emac->rx_counting_sem = xSemaphoreCreateCounting(config->queue_len, 0); - MAC_CHECK(emac->rx_counting_sem, "create semaphore failed", err_sem, NULL); /* create rx task */ BaseType_t xReturned = xTaskCreate(emac_esp32_rx_task, "emac_rx", config->rx_task_stack_size, emac, config->rx_task_prio, &emac->rx_task_hdl); MAC_CHECK(xReturned == pdPASS, "create emac_rx task failed", err_task, NULL); return &(emac->parent); err_task: - vSemaphoreDelete(emac->rx_counting_sem); -err_sem: esp_intr_free(emac->intr_hdl); err_intr: for (int i = 0; i < CONFIG_ETH_DMA_TX_BUFFER_NUM; i++) { @@ -448,8 +443,8 @@ void emac_hal_rx_complete_cb(void *arg) emac_hal_context_t *hal = (emac_hal_context_t *)arg; emac_esp32_t *emac = __containerof(hal, emac_esp32_t, hal); BaseType_t high_task_wakeup; - /* send message to rx thread */ - xSemaphoreGiveFromISR(emac->rx_counting_sem, &high_task_wakeup); + /* notify receive task */ + vTaskNotifyGiveFromISR(emac->rx_task_hdl, &high_task_wakeup); if (high_task_wakeup == pdTRUE) { emac->isr_need_yield = true; } @@ -460,8 +455,8 @@ void emac_hal_rx_unavail_cb(void *arg) emac_hal_context_t *hal = (emac_hal_context_t *)arg; emac_esp32_t *emac = __containerof(hal, emac_esp32_t, hal); BaseType_t high_task_wakeup; - /* send message to rx thread */ - xSemaphoreGiveFromISR(emac->rx_counting_sem, &high_task_wakeup); + /* notify receive task */ + vTaskNotifyGiveFromISR(emac->rx_task_hdl, &high_task_wakeup); if (high_task_wakeup == pdTRUE) { emac->isr_need_yield = true; } From 3bb42d073659c0be422fc8a163536ff27be1a640 Mon Sep 17 00:00:00 2001 From: Martin Thierer Date: Sun, 1 Sep 2019 21:41:34 +0200 Subject: [PATCH 081/146] spi: Put argument of macro SPI_SWAP_DATA_TX/RX in parentheses Close https://github.com/espressif/esp-idf/pull/3996 --- components/driver/include/driver/spi_common.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/driver/include/driver/spi_common.h b/components/driver/include/driver/spi_common.h index 9c8bf75460..50bc0fbcea 100644 --- a/components/driver/include/driver/spi_common.h +++ b/components/driver/include/driver/spi_common.h @@ -41,11 +41,11 @@ extern "C" * * Then points tx_buffer to ``&data``. * - * @param data Data to be sent, can be uint8_t, uint16_t or uint32_t. @param - * len Length of data to be sent, since the SPI peripheral sends from the MSB, - * this helps to shift the data to the MSB. + * @param DATA Data to be sent, can be uint8_t, uint16_t or uint32_t. + * @param LEN Length of data to be sent, since the SPI peripheral sends from + * the MSB, this helps to shift the data to the MSB. */ -#define SPI_SWAP_DATA_TX(data, len) __builtin_bswap32((uint32_t)data<<(32-len)) +#define SPI_SWAP_DATA_TX(DATA, LEN) __builtin_bswap32((uint32_t)(DATA)<<(32-(LEN))) /** * Transform received data of length <= 32 bits to the format of an unsigned integer. @@ -54,11 +54,11 @@ extern "C" * * uint16_t data = SPI_SWAP_DATA_RX(*(uint32_t*)t->rx_data, 15); * - * @param data Data to be rearranged, can be uint8_t, uint16_t or uint32_t. - * @param len Length of data received, since the SPI peripheral writes from + * @param DATA Data to be rearranged, can be uint8_t, uint16_t or uint32_t. + * @param LEN Length of data received, since the SPI peripheral writes from * the MSB, this helps to shift the data to the LSB. */ -#define SPI_SWAP_DATA_RX(data, len) (__builtin_bswap32(data)>>(32-len)) +#define SPI_SWAP_DATA_RX(DATA, LEN) (__builtin_bswap32(DATA)>>(32-(LEN))) /** * @brief This is a configuration structure for a SPI bus. From a4ee680bbf1e49692bac7027eba69be61b70242b Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Tue, 3 Sep 2019 19:41:50 +0800 Subject: [PATCH 082/146] app_update: use build system api to get partition_table dir --- components/app_update/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/app_update/CMakeLists.txt b/components/app_update/CMakeLists.txt index 8d0e45dbb0..9fa866e594 100644 --- a/components/app_update/CMakeLists.txt +++ b/components/app_update/CMakeLists.txt @@ -29,8 +29,11 @@ if(NOT BOOTLOADER_BUILD) idf_build_get_property(idf_path IDF_PATH) idf_build_get_property(python PYTHON) + + idf_component_get_property(partition_table_dir partition_table COMPONENT_DIR) + add_custom_command(OUTPUT ${blank_otadata_file} - COMMAND ${python} ${idf_path}/components/partition_table/gen_empty_partition.py + COMMAND ${python} ${partition_table_dir}/gen_empty_partition.py ${otadata_size} ${blank_otadata_file}) add_custom_target(blank_ota_data ALL DEPENDS ${blank_otadata_file}) From eb2ddbfe48051f7fe9da0d554f305ced6bc15155 Mon Sep 17 00:00:00 2001 From: Hao Ning Date: Wed, 4 Sep 2019 17:09:13 +0800 Subject: [PATCH 083/146] add chinese translation for timer.rst in api-reference folder --- docs/en/api-reference/peripherals/timer.rst | 2 + .../zh_CN/api-reference/peripherals/timer.rst | 108 +++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/docs/en/api-reference/peripherals/timer.rst b/docs/en/api-reference/peripherals/timer.rst index 77eb64028d..385c5699c6 100644 --- a/docs/en/api-reference/peripherals/timer.rst +++ b/docs/en/api-reference/peripherals/timer.rst @@ -1,6 +1,8 @@ Timer ===== +:link_to_translation:`zh_CN:[中文]` + Introduction ------------ diff --git a/docs/zh_CN/api-reference/peripherals/timer.rst b/docs/zh_CN/api-reference/peripherals/timer.rst index 777ec06c22..2d01e6a4dd 100644 --- a/docs/zh_CN/api-reference/peripherals/timer.rst +++ b/docs/zh_CN/api-reference/peripherals/timer.rst @@ -1 +1,107 @@ -.. include:: ../../../en/api-reference/peripherals/timer.rst \ No newline at end of file +定时器 +============ + +:link_to_translation:`en:[English]` + +简介 +------------ + +ESP32 芯片提供两组硬件定时器,每组包含两个通用硬件定时器。所有定时器均为 64 位通用定时器,包括 16 位预分频器和 64 位自动重载向上/向下计数器。 + + +功能概述 +------------------- + +下文介绍了配置和操作定时器的常规步骤: + +* :ref:`timer-api-timer-initialization` - 启动定时器前应设置的参数,以及每个设置提供的具体功能。 +* :ref:`timer-api-timer-control` - 如何读取定时器的值,如何暂停/启动定时器以及如何改变定时器的操作方式。 +* :ref:`timer-api-alarms` - 如何设置和使用警报。 +* :ref:`timer-api-interrupts`- 如何使能和使用中断。 + + +.. _timer-api-timer-initialization: + +定时器初始化 +^^^^^^^^^^^^^^^^^^^^ + +两个 ESP32 定时器组中,每组都有两个定时器,两组共有四个定时器供使用。ESP32 定时器组应使用 :cpp:type:`timer_group_t` 识别,而每组中的个体定时器则应使用 :cpp:type:`timer_idx_t` 识别。 + +首先调用 :cpp:func:`timer_init` 函数,并将 :cpp:type:`timer_config_t` 结构体传递给此函数,用于定义定时器的工作方式,实现定时器初始化。特别注意以下定时器参数可设置为: + + * **分频器**: 设置定时器中计数器计数的速度,:cpp:member:`divider` 的设置将用作输入的 80 MHz APB_CLK 时钟的除数。 + * **模式**: 设置计数器是递增还是递减。可通过从 :cpp:type:`timer_count_dir_t` 中选取一个值,后使用 :cpp:member:`counter_dir` 来选择模式。 + * **计数器使能**: 如果计数器已使能,则在调用 :cpp:func:`timer_init` 后计数器将立即开始递增/递减。您可通过从 :cpp:type:`timer_start_t` 中选取一个值,后使用 :cpp:member:`counter_en` 改变此行为。 + * **报警使能**: 可使用 :cpp:member:`alarm_en` 设置。 + * **自动重载**: 设置计数器是否应该在定时器警报上使用 :cpp:member:`auto_reload` 自动重载首个计数值,还是继续递增或递减。 + * **中断类型**: 选择定时器警报上应触发的中断类型,请设置 :cpp:type:`timer_intr_mode_t` 中定义的值。 + +要获取定时器设置的当前值,请使用函数 :cpp:func:`timer_get_config`。 + + +.. _timer-api-timer-control: + +定时器控制 +^^^^^^^^^^^^^ + +定时器使能后便开始计数。要使能定时器,可首先设置 :cpp:member:`counter_en` 为 ``true``,然后调用函数 :cpp:func:`timer_init`,或者直接调用函数 :cpp:func:`timer_start`。您可通过调用函数 :cpp:func:`timer_set_counter_value` 来指定定时器的首个计数值。要检查定时器的当前值,调用函数 :cpp:func:`timer_get_counter_value` 或 :cpp:func:`timer_get_counter_time_sec`。 + +可通过调用函数 :cpp:func:`timer_pause` 随时暂停定时器。要再次启动它,调用函数 :cpp:func:`timer_start`。 + +要重新配置定时器,可调用函数 :cpp:func:`timer_init`,该函数详细介绍见 :ref:`timer-api-timer-initialization`。 + +除此之外,还可通过使用专有函数更改个别设置来重新配置定时器: + +============= =================================== ========================================================================== +设置 专有函数 描述 +============= =================================== ========================================================================== + 分频器 :cpp:func:`timer_set_divider` 更改计数频率。为避免发生不可预测情况,更改分频器时应暂停定时器。如果定时器正在运行,则使用 :cpp:func:`timer_set_divider` 将其暂停并更改设置,然后重启定时器。 +模式 :cpp:func:`timer_set_counter_mode` 设置计数器应递增还是递减 +自动重载 :cpp:func:`timer_set_auto_reload` 设置是否应在定时器警报上重载首个计数值 +============= =================================== ========================================================================== + +.. _timer-api-alarms: + +警报 +^^^^^^ + +要设置警报,先调用函数 :cpp:func:`timer_set_alarm_value`,然后使用 :cpp:func:`timer_set_alarm` 使能警报。当调用函数 :cpp:func:`timer_init` 时,也可以在定时器初始化阶段使能警报。 + +警报已使能且定时器达到警报值后,根据配置,可能会出现以下两种行为: + + * 如果先前已配置,此时将触发中断。有关如何配置中断,请参见 :ref:`timer-api-interrupts`。 + * 如 :cpp:member:`auto_reload` 已使能,定时器的计数器将重新加载,从先前配置好的值开始再次计数。应使用函数 :cpp:func:`timer_set_counter_value` 预先设置该值。 + +.. note:: + + * 如果已设置警报值且定时器已超过该值,则将立即触发警报。 + * 一旦触发后,警报将自动关闭,需要重新使能以再次触发。 + +要检查某特定的警报值,调用函数 :cpp:func:`timer_get_alarm_value`。 + + +.. _timer-api-interrupts: + +中断 +^^^^^^^^^^ + +可通过调用函数 :cpp:func:`timer_isr_register` 为特定定时器组和定时器注册中断处理程序。 + +调用 :cpp:func:`timer_group_intr_enable` 使能定时器组的中断程序,调用 :cpp:func:`timer_enable_intr` 使能某定时器的中断程序。调用 :cpp:func:`timer_group_intr_disable` 关闭定时器组的中断程序,调用 :cpp:func:`timer_disable_intr` 关闭某定时器的中断程序。 + +在中断服务程序(ISR)中处理中断时,需要明确地清除中断状态位。为此,请设置定义在 :component_file:`soc/esp32/include/soc/timer_group_struct.h` 中的 ``TIMERGN.int_clr_timers.tM`` 结构。该结构中 ``N`` 是定时器组别编号 [0, 1],``M`` 是定时器编号 [0, 1]。例如,要清除定时器组别 0 中定时器 1 的中断状态位,请调用以下命令:: + + TIMERG0.int_clr_timers.t1 = 1 + +有关如何使用中断,请参阅应用示例。 + + +应用示例 +------------------- + +64 位硬件定时器示例::example:`peripherals/timer_group`。 + +API 参考 +------------- + +.. include:: /_build/inc/timer.inc From 45c72dd3728126a2599441de7a5f0eafca53bca5 Mon Sep 17 00:00:00 2001 From: zhangyanjiao Date: Thu, 5 Sep 2019 20:20:42 +0800 Subject: [PATCH 084/146] fix the bug that STA fails to connect WPA_WPA2_PSK Apple AP --- components/esp_wifi/lib_esp32 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/lib_esp32 b/components/esp_wifi/lib_esp32 index 63590d0f5e..3ea3a8f30c 160000 --- a/components/esp_wifi/lib_esp32 +++ b/components/esp_wifi/lib_esp32 @@ -1 +1 @@ -Subproject commit 63590d0f5e024ad8baf4191ca6195bb7cf514381 +Subproject commit 3ea3a8f30c6cd5b29ccfb8b9b74d3e6741bdc1c2 From f23b3fdbe4d40be570563e2cbf2d2495e552eac2 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 6 Sep 2019 11:01:34 +1000 Subject: [PATCH 085/146] rom: Add warnings for miniz functions that won't work due to missing malloc Closes https://github.com/espressif/esp-idf/issues/4024 --- components/esp_rom/include/esp32/rom/miniz.h | 27 +++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/components/esp_rom/include/esp32/rom/miniz.h b/components/esp_rom/include/esp32/rom/miniz.h index ed79beb2cb..773a0d0570 100644 --- a/components/esp_rom/include/esp32/rom/miniz.h +++ b/components/esp_rom/include/esp32/rom/miniz.h @@ -522,7 +522,10 @@ mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const // Reads a single file from an archive into a heap block. // Returns NULL on failure. -void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags); +// +// Note: Due to MINIZ_NO_MALLOC, this function will not work. +// +void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags) __attribute__((warning("miniz is compiled without malloc so this function does not work"))); #endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS @@ -551,7 +554,10 @@ enum // Function returns a pointer to the decompressed data, or NULL on failure. // *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. // The caller must call mz_free() on the returned block when it's no longer needed. -void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); +// +// Note: Due to MINIZ_NO_MALLOC, this function will not work. +// +void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags) __attribute__((warning("miniz is compiled without malloc so this function does not work"))); // tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. // Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. @@ -559,9 +565,12 @@ void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, siz size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); // tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer. -// Returns 1 on success or 0 on failure. +// +// Note: Due to MINIZ_NO_MALLOC, this function will not work. +// +// Returns 1 on success or 0 or -1 on failure. typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser); -int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); +int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) __attribute__((warning("miniz is compiled without malloc so this function does not work")));; struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor; @@ -663,7 +672,10 @@ enum // Function returns a pointer to the compressed data, or NULL on failure. // *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. // The caller must free() the returned block when it's no longer needed. -void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); +// +// Note: Due to MINIZ_NO_MALLOC, this function will not work. +// +void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags) __attribute__((warning("miniz is compiled without malloc so this function does not work")));; // tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. // Returns 0 on failure. @@ -686,7 +698,10 @@ void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser); // tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally. -mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); +// +// Note: Due to MINIZ_NO_MALLOC, this function will not work. +// +mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) __attribute__((warning("miniz is compiled without malloc so this function does not work")));; enum { TDEFL_MAX_HUFF_TABLES = 3, TDEFL_MAX_HUFF_SYMBOLS_0 = 288, TDEFL_MAX_HUFF_SYMBOLS_1 = 32, TDEFL_MAX_HUFF_SYMBOLS_2 = 19, TDEFL_LZ_DICT_SIZE = 32768, TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, TDEFL_MIN_MATCH_LEN = 3, TDEFL_MAX_MATCH_LEN = 258 }; From a6cc964a2646f8c2289f54fa614b0d908cfa6deb Mon Sep 17 00:00:00 2001 From: Chinmay Chhajed Date: Fri, 6 Sep 2019 11:02:36 +0800 Subject: [PATCH 086/146] esp_http_server : Bugfix in parsing of empty header values This MR is intended to fix incorrect parsing of HTTP requests when empty header values are present. The issue is was due to asymmetric behavior of `http_parser` library, which in case of: non-empty header values : invokes callbacks with the pointer to the start of a value empty header values : invokes callbacks with pointer to the start of next header or section Since HTTP server relies on this pointer (along with length of the value) to locate the end of a value, and replace the line terminators (CRLFs) with null characters, the second case needed to be handled correctly. Closes IDFGH-1539 Closes https://github.com/espressif/esp-idf/issues/3803 --- components/esp_http_server/src/httpd_parse.c | 17 ++++ .../http_server_advanced_test.py | 2 + .../http_server/advanced_tests/main/tests.c | 97 +++++++++++++++++++ .../advanced_tests/scripts/test.py | 44 ++++++++- 4 files changed, 159 insertions(+), 1 deletion(-) diff --git a/components/esp_http_server/src/httpd_parse.c b/components/esp_http_server/src/httpd_parse.c index 765510f0c2..ec73bbb5f5 100644 --- a/components/esp_http_server/src/httpd_parse.c +++ b/components/esp_http_server/src/httpd_parse.c @@ -267,6 +267,23 @@ static esp_err_t cb_header_value(http_parser *parser, const char *at, size_t len parser_data->last.at = at; parser_data->last.length = 0; parser_data->status = PARSING_HDR_VALUE; + + if (length == 0) { + /* As per behavior of http_parser, when length > 0, + * `at` points to the start of CRLF. But, in the + * case when header value is empty (zero length), + * then `at` points to the position right after + * the CRLF. Since for our purpose we need `last.at` + * to point to exactly where the CRLF starts, it + * needs to be adjusted by the right offset */ + char *at_adj = (char *)parser_data->last.at; + /* Find the end of header field string */ + while (*(--at_adj) != ':'); + /* Now skip leading spaces' */ + while (*(++at_adj) == ' '); + /* Now we are at the right position */ + parser_data->last.at = at_adj; + } } else if (parser_data->status != PARSING_HDR_VALUE) { ESP_LOGE(TAG, LOG_FMT("unexpected state transition")); parser_data->error = HTTPD_500_INTERNAL_SERVER_ERROR; diff --git a/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py b/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py index a27ad5ee02..f3e7033d77 100644 --- a/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py +++ b/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py @@ -133,6 +133,8 @@ def test_examples_protocol_http_server_advanced(env, extra_data): failed = True if not client.get_false_uri(got_ip, got_port): failed = True + if not client.get_test_headers(got_ip, got_port): + failed = True Utility.console_log("Error code tests...") if not client.code_500_server_error_test(got_ip, got_port): diff --git a/examples/protocols/http_server/advanced_tests/main/tests.c b/examples/protocols/http_server/advanced_tests/main/tests.c index dd4a973155..f78e5e62e3 100644 --- a/examples/protocols/http_server/advanced_tests/main/tests.c +++ b/examples/protocols/http_server/advanced_tests/main/tests.c @@ -27,6 +27,96 @@ static esp_err_t hello_get_handler(httpd_req_t *req) #undef STR } +/* This handler is intended to check what happens in case of empty values of headers. + * Here `Header2` is an empty header and `Header1` and `Header3` will have `Value1` + * and `Value3` in them. */ +static esp_err_t test_header_get_handler(httpd_req_t *req) +{ + httpd_resp_set_type(req, HTTPD_TYPE_TEXT); + int buf_len; + char *buf; + + buf_len = httpd_req_get_hdr_value_len(req, "Header1"); + if (buf_len > 0) { + buf = malloc(++buf_len); + if (!buf) { + ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len); + httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed"); + return ESP_ERR_NO_MEM; + } + /* Copy null terminated value string into buffer */ + if (httpd_req_get_hdr_value_str(req, "Header1", buf, buf_len) == ESP_OK) { + ESP_LOGI(TAG, "Header1 content: %s", buf); + if (strcmp("Value1", buf) != 0) { + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Wrong value of Header1 received"); + free(buf); + return ESP_ERR_INVALID_ARG; + } else { + ESP_LOGI(TAG, "Expected value and received value matched for Header1"); + } + } else { + ESP_LOGE(TAG, "Error in getting value of Header1"); + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Error in getting value of Header1"); + free(buf); + return ESP_FAIL; + } + free(buf); + } else { + ESP_LOGE(TAG, "Header1 not found"); + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header1 not found"); + return ESP_ERR_NOT_FOUND; + } + + buf_len = httpd_req_get_hdr_value_len(req, "Header3"); + if (buf_len > 0) { + buf = malloc(++buf_len); + if (!buf) { + ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len); + httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed"); + return ESP_ERR_NO_MEM; + } + /* Copy null terminated value string into buffer */ + if (httpd_req_get_hdr_value_str(req, "Header3", buf, buf_len) == ESP_OK) { + ESP_LOGI(TAG, "Header3 content: %s", buf); + if (strcmp("Value3", buf) != 0) { + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Wrong value of Header3 received"); + free(buf); + return ESP_ERR_INVALID_ARG; + } else { + ESP_LOGI(TAG, "Expected value and received value matched for Header3"); + } + } else { + ESP_LOGE(TAG, "Error in getting value of Header3"); + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Error in getting value of Header3"); + free(buf); + return ESP_FAIL; + } + free(buf); + } else { + ESP_LOGE(TAG, "Header3 not found"); + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header3 not found"); + return ESP_ERR_NOT_FOUND; + } + + buf_len = httpd_req_get_hdr_value_len(req, "Header2"); + buf = malloc(++buf_len); + if (!buf) { + ESP_LOGE(TAG, "Failed to allocate memory of %d bytes!", buf_len); + httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Memory allocation failed"); + return ESP_ERR_NO_MEM; + } + if (httpd_req_get_hdr_value_str(req, "Header2", buf, buf_len) == ESP_OK) { + ESP_LOGI(TAG, "Header2 content: %s", buf); + httpd_resp_send(req, buf, strlen(buf)); + } else { + ESP_LOGE(TAG, "Header2 not found"); + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Header2 not found"); + return ESP_FAIL; + } + + return ESP_OK; +} + static esp_err_t hello_type_get_handler(httpd_req_t *req) { #define STR "Hello World!" @@ -217,6 +307,11 @@ static const httpd_uri_t basic_handlers[] = { .handler = hello_type_get_handler, .user_ctx = NULL, }, + { .uri = "/test_header", + .method = HTTP_GET, + .handler = test_header_get_handler, + .user_ctx = NULL, + }, { .uri = "/hello", .method = HTTP_GET, .handler = hello_get_handler, @@ -275,6 +370,8 @@ static httpd_handle_t test_httpd_start(void) pre_start_mem = esp_get_free_heap_size(); httpd_handle_t hd; httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + /* Modify this setting to match the number of test URI handlers */ + config.max_uri_handlers = 9; config.server_port = 1234; /* This check should be a part of http_server */ diff --git a/examples/protocols/http_server/advanced_tests/scripts/test.py b/examples/protocols/http_server/advanced_tests/scripts/test.py index d1a923aeda..4196ff532a 100644 --- a/examples/protocols/http_server/advanced_tests/scripts/test.py +++ b/examples/protocols/http_server/advanced_tests/scripts/test.py @@ -142,7 +142,20 @@ import http.client import sys import string import random -import Utility + + +try: + import Utility +except ImportError: + import os + + # This environment variable is expected on the host machine + # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw + test_fw_path = os.getenv("TEST_FW_PATH") + if test_fw_path and test_fw_path not in sys.path: + sys.path.insert(0, test_fw_path) + + import Utility _verbose_ = False @@ -427,6 +440,34 @@ def get_echo(dut, port): return True +def get_test_headers(dut, port): + # GET /test_header returns data of Header2' + Utility.console_log("[test] GET /test_header =>", end=' ') + conn = http.client.HTTPConnection(dut, int(port), timeout=15) + custom_header = {"Header1": "Value1", "Header3": "Value3"} + header2_values = ["", " ", "Value2", " Value2", "Value2 ", " Value2 "] + for val in header2_values: + custom_header["Header2"] = val + conn.request("GET", "/test_header", headers=custom_header) + resp = conn.getresponse() + if not test_val("status_code", 200, resp.status): + conn.close() + return False + hdr_val_start_idx = val.find("Value2") + if hdr_val_start_idx == -1: + if not test_val("header: Header2", "", resp.read().decode()): + conn.close() + return False + else: + if not test_val("header: Header2", val[hdr_val_start_idx:], resp.read().decode()): + conn.close() + return False + resp.read() + Utility.console_log("Success") + conn.close() + return True + + def get_hello_type(dut, port): # GET /hello/type_html returns text/html as Content-Type' Utility.console_log("[test] GET /hello/type_html has Content-Type of text/html =>", end=' ') @@ -966,6 +1007,7 @@ if __name__ == '__main__': get_hello_type(dut, port) get_hello_status(dut, port) get_false_uri(dut, port) + get_test_headers(dut, port) Utility.console_log("### Error code tests") code_500_server_error_test(dut, port) From 7ff9538c4803f84c876033da9fc60e8a920788a7 Mon Sep 17 00:00:00 2001 From: Alex Lisitsyn Date: Fri, 6 Sep 2019 15:37:55 +0800 Subject: [PATCH 087/146] espcoredump: fix issue with spi_flash access spi_flash has been updated and its functions work from flash by default instead of IRAM that cause issue add Kconfig value into espcoredump to enable spi_flash legacy mode (CONFIG_SPI_FLASH_USE_LEGACY_IMPL) when core dump is selected fix spi_flash issues to work correctly with legacy mode when CONFIG_SPI_FLASH_USE_LEGACY_IMPL is used --- components/esp32/Kconfig | 14 ++++++++++ components/esp32/cpu_start.c | 2 ++ components/esp32/panic.c | 8 ++++++ components/esp_gdbstub/linker.lf | 7 +++-- components/espcoredump/Kconfig | 1 + components/espcoredump/linker.lf | 11 +++++--- components/espcoredump/src/core_dump_flash.c | 8 +++--- components/espcoredump/src/core_dump_port.c | 4 ++- components/spi_flash/cache_utils.c | 28 ++++++++++++++++---- components/spi_flash/include/esp_spi_flash.h | 7 +++++ components/spi_flash/partition.c | 9 ++++++- docs/en/api-guides/fatal-errors.rst | 4 +++ 12 files changed, 87 insertions(+), 16 deletions(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index d0e9b5da0f..63afe63a82 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -776,6 +776,20 @@ menu "ESP32-specific" To prevent interrupting DPORT workarounds, need to disable interrupt with a maximum used level in the system. + config ESP32_PANIC_HANDLER_IRAM + bool "Place panic handler code in IRAM" + default n + help + If this option is disabled (default), the panic handler code is placed in flash not IRAM. + This means that if ESP-IDF crashes while flash cache is disabled, the panic handler will + automatically re-enable flash cache before running GDB Stub or Core Dump. This adds some minor + risk, if the flash cache status is also corrupted during the crash. + + If this option is enabled, the panic handler code is placed in IRAM. This allows the panic + handler to run without needing to re-enable cache first. This may be necessary to debug some + complex issues with crashes while flash cache is disabled (for example, when writing to + SPI flash.) + endmenu # ESP32-Specific menu "Power Management" diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 9ce5df4aef..975f09b309 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -404,9 +404,11 @@ void start_cpu0_default(void) /* init default OS-aware flash access critical section */ spi_flash_guard_set(&g_flash_guard_default_ops); +#ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL esp_flash_app_init(); esp_err_t flash_ret = esp_flash_init_default_chip(); assert(flash_ret == ESP_OK); +#endif uint8_t revision = esp_efuse_get_chip_ver(); ESP_LOGI(TAG, "Chip Revision: %d", revision); diff --git a/components/esp32/panic.c b/components/esp32/panic.c index 294c3635d2..d84ec1ecb4 100644 --- a/components/esp32/panic.c +++ b/components/esp32/panic.c @@ -608,6 +608,14 @@ static __attribute__((noreturn)) void commonErrorHandler(XtExcFrame *frame) reconfigureAllWdts(); #endif +#if !CONFIG_ESP32_PANIC_HANDLER_IRAM + // Re-enable CPU cache for current CPU if it was disabled + if (!spi_flash_cache_enabled()) { + spi_flash_enable_cache(core_id); + panicPutStr("Re-enable cpu cache.\r\n"); + } +#endif + #if CONFIG_ESP32_PANIC_GDBSTUB disableAllWdts(); rtc_wdt_disable(); diff --git a/components/esp_gdbstub/linker.lf b/components/esp_gdbstub/linker.lf index b5d88a2675..7093ea8b2a 100644 --- a/components/esp_gdbstub/linker.lf +++ b/components/esp_gdbstub/linker.lf @@ -1,4 +1,7 @@ [mapping:esp_gdbstub] archive: libesp_gdbstub.a -entries: - * (noflash) +entries: + if ESP32_PANIC_HANDLER_IRAM = y: + * (noflash_text) + else: + * (default) \ No newline at end of file diff --git a/components/espcoredump/Kconfig b/components/espcoredump/Kconfig index 991bf18f7b..fe8b6e4553 100644 --- a/components/espcoredump/Kconfig +++ b/components/espcoredump/Kconfig @@ -13,6 +13,7 @@ menu "Core dump" config ESP32_ENABLE_COREDUMP_TO_FLASH bool "Flash" select ESP32_ENABLE_COREDUMP + select SPI_FLASH_USE_LEGACY_IMPL config ESP32_ENABLE_COREDUMP_TO_UART bool "UART" select ESP32_ENABLE_COREDUMP diff --git a/components/espcoredump/linker.lf b/components/espcoredump/linker.lf index 02dd9cfe78..131e10c30b 100644 --- a/components/espcoredump/linker.lf +++ b/components/espcoredump/linker.lf @@ -1,7 +1,10 @@ [mapping:espcoredump] archive: libespcoredump.a entries: - core_dump_uart (noflash_text) - core_dump_flash (noflash_text) - core_dump_common (noflash_text) - core_dump_port (noflash_text) \ No newline at end of file + if ESP32_PANIC_HANDLER_IRAM = y: + core_dump_uart (noflash_text) + core_dump_flash (noflash_text) + core_dump_common (noflash_text) + core_dump_port (noflash_text) + else: + * (default) diff --git a/components/espcoredump/src/core_dump_flash.c b/components/espcoredump/src/core_dump_flash.c index 9c2ac78766..21e85731e0 100644 --- a/components/espcoredump/src/core_dump_flash.c +++ b/components/espcoredump/src/core_dump_flash.c @@ -200,8 +200,8 @@ static esp_err_t esp_core_dump_flash_write_data(void *priv, void * data, uint32_ void esp_core_dump_to_flash(XtExcFrame *frame) { - core_dump_write_config_t wr_cfg; - core_dump_write_flash_data_t wr_data; + static core_dump_write_config_t wr_cfg; + static core_dump_write_flash_data_t wr_data; core_dump_crc_t crc = esp_core_dump_calc_flash_config_crc(); if (s_core_flash_config.partition_config_crc != crc) { @@ -214,8 +214,10 @@ void esp_core_dump_to_flash(XtExcFrame *frame) return; } - /* init non-OS flash access critical section */ +#if CONFIG_SPI_FLASH_USE_LEGACY_IMPL + // init non-OS flash access critical section spi_flash_guard_set(&g_flash_guard_no_os_ops); +#endif memset(&wr_cfg, 0, sizeof(wr_cfg)); wr_cfg.prepare = esp_core_dump_flash_write_prepare; diff --git a/components/espcoredump/src/core_dump_port.c b/components/espcoredump/src/core_dump_port.c index 2ac1b6482b..1e6ab6a906 100644 --- a/components/espcoredump/src/core_dump_port.c +++ b/components/espcoredump/src/core_dump_port.c @@ -94,7 +94,9 @@ bool esp_core_dump_process_stack(core_dump_task_header_t* task_snaphort, uint32_ ESP_COREDUMP_LOG_PROCESS("Stack len = %lu (%x %x)", len, task_snaphort->stack_start, task_snaphort->stack_end); // Take stack padding into account - *length = (len + sizeof(uint32_t) - 1) & ~(sizeof(uint32_t) - 1); + if (length) { + *length = (len + sizeof(uint32_t) - 1) & ~(sizeof(uint32_t) - 1); + } task_is_valid = true; } return task_is_valid; diff --git a/components/spi_flash/cache_utils.c b/components/spi_flash/cache_utils.c index 25f937d749..63603097ce 100644 --- a/components/spi_flash/cache_utils.c +++ b/components/spi_flash/cache_utils.c @@ -31,6 +31,18 @@ #include "esp_spi_flash.h" #include "esp_log.h" +#define DPORT_CACHE_BIT(cpuid, regid) DPORT_ ## cpuid ## regid + +#define DPORT_CACHE_MASK(cpuid) (DPORT_CACHE_BIT(cpuid, _CACHE_MASK_OPSDRAM) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \ + DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IROM0) | \ + DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0) ) + +#define DPORT_CACHE_VAL(cpuid) (~(DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \ + DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | \ + DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0))) + +#define DPORT_CACHE_GET_VAL(cpuid) (cpuid == 0) ? DPORT_CACHE_VAL(PRO) : DPORT_CACHE_VAL(APP) +#define DPORT_CACHE_GET_MASK(cpuid) (cpuid == 0) ? DPORT_CACHE_MASK(PRO) : DPORT_CACHE_MASK(APP) static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state); static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state); @@ -256,13 +268,10 @@ void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void) * Cache_Flush before Cache_Read_Enable, even if cached data was not modified. */ -static const uint32_t cache_mask = DPORT_APP_CACHE_MASK_OPSDRAM | DPORT_APP_CACHE_MASK_DROM0 | - DPORT_APP_CACHE_MASK_DRAM1 | DPORT_APP_CACHE_MASK_IROM0 | - DPORT_APP_CACHE_MASK_IRAM1 | DPORT_APP_CACHE_MASK_IRAM0; - static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state) { uint32_t ret = 0; + const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid); if (cpuid == 0) { ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0); while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) { @@ -281,6 +290,7 @@ static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_st static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state) { + const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid); if (cpuid == 0) { DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S); DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0); @@ -290,7 +300,6 @@ static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_sta } } - IRAM_ATTR bool spi_flash_cache_enabled(void) { bool result = (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE) != 0); @@ -299,3 +308,12 @@ IRAM_ATTR bool spi_flash_cache_enabled(void) #endif return result; } + +void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid) +{ + uint32_t cache_value = DPORT_CACHE_GET_VAL(cpuid); + cache_value &= DPORT_CACHE_GET_MASK(cpuid); + + // Re-enable cache on this CPU + spi_flash_restore_cache(cpuid, cache_value); +} diff --git a/components/spi_flash/include/esp_spi_flash.h b/components/spi_flash/include/esp_spi_flash.h index 33bdc4d83c..5966f0ecc1 100644 --- a/components/spi_flash/include/esp_spi_flash.h +++ b/components/spi_flash/include/esp_spi_flash.h @@ -295,6 +295,13 @@ const void *spi_flash_phys2cache(size_t phys_offs, spi_flash_mmap_memory_t memor */ bool spi_flash_cache_enabled(void); +/** + * @brief Re-enable cache for the core defined as cpuid parameter. + * + * @param cpuid the core number to enable instruction cache for + */ +void spi_flash_enable_cache(uint32_t cpuid); + /** * @brief SPI flash critical section enter function. * diff --git a/components/spi_flash/partition.c b/components/spi_flash/partition.c index 14de32c9f5..a6b33a1382 100644 --- a/components/spi_flash/partition.c +++ b/components/spi_flash/partition.c @@ -172,7 +172,9 @@ static esp_err_t load_partitions(void) err = ESP_ERR_NO_MEM; break; } +#ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL item->info.flash_chip = esp_flash_default_chip; +#endif item->info.address = it->pos.offset; item->info.size = it->pos.size; item->info.type = it->type; @@ -334,10 +336,11 @@ esp_err_t esp_partition_read(const esp_partition_t* partition, #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL } else { #if CONFIG_SECURE_FLASH_ENC_ENABLED +#ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL if (partition->flash_chip != esp_flash_default_chip) { return ESP_ERR_NOT_SUPPORTED; } - +#endif /* Encrypted partitions need to be read via a cache mapping */ const void *buf; spi_flash_mmap_handle_t handle; @@ -376,9 +379,11 @@ esp_err_t esp_partition_write(const esp_partition_t* partition, #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL } else { #if CONFIG_SECURE_FLASH_ENC_ENABLED +#ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL if (partition->flash_chip != esp_flash_default_chip) { return ESP_ERR_NOT_SUPPORTED; } +#endif return spi_flash_write_encrypted(dst_offset, src, size); #else return ESP_ERR_NOT_SUPPORTED; @@ -428,9 +433,11 @@ esp_err_t esp_partition_mmap(const esp_partition_t* partition, size_t offset, si if (offset + size > partition->size) { return ESP_ERR_INVALID_SIZE; } +#ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL if (partition->flash_chip != esp_flash_default_chip) { return ESP_ERR_NOT_SUPPORTED; } +#endif size_t phys_addr = partition->address + offset; // offset within 64kB block size_t region_offset = phys_addr & 0xffff; diff --git a/docs/en/api-guides/fatal-errors.rst b/docs/en/api-guides/fatal-errors.rst index c64268fb76..f1ca3f1842 100644 --- a/docs/en/api-guides/fatal-errors.rst +++ b/docs/en/api-guides/fatal-errors.rst @@ -62,6 +62,10 @@ Behavior of panic handler is affected by two other configuration options. - If :doc:`Core Dump ` feature is enabled (``CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH`` or ``CONFIG_ESP32_ENABLE_COREDUMP_TO_UART`` options), then system state (task stacks and registers) will be dumped either to Flash or UART, for later analysis. +- If :ref:`CONFIG_ESP32_PANIC_HANDLER_IRAM` is disabled (disabled by default), the panic handler code is placed in flash memory not IRAM. This means that if ESP-IDF crashes while flash cache is disabled, the panic handler will automatically re-enable flash cache before running GDB Stub or Core Dump. This adds some minor risk, if the flash cache status is also corrupted during the crash. + + If this option is enabled, the panic handler code is placed in IRAM. This allows the panic handler to run without needing to re-enable cache first. This may be necessary to debug some complex issues with crashes while flash cache is disabled (for example, when writing to SPI flash). + The following diagram illustrates panic handler behavior: .. blockdiag:: From 5df2e6b19ca4ff4acd1789805f37ffecae090f1f Mon Sep 17 00:00:00 2001 From: Nachiket Kukade Date: Fri, 23 Aug 2019 16:07:20 +0530 Subject: [PATCH 088/146] wpa_supplicant: Fix EAP Re-authentication issue EAP reauth frames are dropped at various stages due to current implementation of WPA2 ENT states and EAP SM init/deinit logic. Route EAPOL frames based on EAP pkt type and maintain EAP SM to facilitate EAP re-authentication process. The full fix for the change includes a fix from wifi library (commit - 36f99df849214fbf9b0d15e58554632a568e05aa). --- .../src/esp_supplicant/esp_wpa2.c | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c b/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c index 584b2acc1b..089f3e87bb 100644 --- a/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c +++ b/components/wpa_supplicant/src/esp_supplicant/esp_wpa2.c @@ -58,7 +58,7 @@ static struct eap_sm *gEapSm = NULL; static int eap_peer_sm_init(void); static void eap_peer_sm_deinit(void); -static int wpa2_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid); +static int eap_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid); static int wpa2_start_eapol_internal(void); int wpa2_post(uint32_t sig, uint32_t par); @@ -216,7 +216,7 @@ void wpa2_task(void *pvParameters ) struct wpa2_rx_param *param = NULL; while ((param = wpa2_rxq_dequeue()) != NULL){ - wpa2_sm_rx_eapol_internal(param->sa, param->buf, param->len, param->bssid); + eap_sm_rx_eapol_internal(param->sa, param->buf, param->len, param->bssid); os_free(param->buf); os_free(param); } @@ -511,7 +511,7 @@ out: return ret; } -static int wpa2_sm_rx_eapol(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) +static int eap_sm_rx_eapol(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) { struct eap_sm *sm = gEapSm; @@ -541,12 +541,35 @@ static int wpa2_sm_rx_eapol(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) } #else - return wpa2_sm_rx_eapol_internal(src_addr, buf, len, bssid); + return eap_sm_rx_eapol_internal(src_addr, buf, len, bssid); #endif } +static int wpa2_ent_rx_eapol(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) +{ + struct ieee802_1x_hdr *hdr; + int ret = ESP_OK; -static int wpa2_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) + hdr = (struct ieee802_1x_hdr *) buf; + + switch (hdr->type) { + case IEEE802_1X_TYPE_EAPOL_START: + case IEEE802_1X_TYPE_EAP_PACKET: + case IEEE802_1X_TYPE_EAPOL_LOGOFF: + ret = eap_sm_rx_eapol(src_addr, buf, len, bssid); + break; + case IEEE802_1X_TYPE_EAPOL_KEY: + ret = wpa_sm_rx_eapol(src_addr, buf, len); + break; + default: + wpa_printf(MSG_ERROR, "Unknown EAPOL packet type - %d\n", hdr->type); + break; + } + + return ret; +} + +static int eap_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid) { struct eap_sm *sm = gEapSm; u32 plen, data_len; @@ -614,6 +637,12 @@ static int wpa2_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bs #ifdef EAP_PEER_METHOD switch (ehdr->code) { case EAP_CODE_REQUEST: + /* Handle EAP-reauthentication case */ + if (sm->finish_state == WPA2_ENT_EAP_STATE_SUCCESS) { + wpa_printf(MSG_INFO, ">>>>>wpa2 EAP Re-authentication in progress\n"); + wpa2_set_eap_state(WPA2_ENT_EAP_STATE_IN_PROGRESS); + } + req = wpabuf_alloc_copy((u8 *)ehdr, len - sizeof(*hdr)); ret = eap_sm_process_request(sm, req); break; @@ -628,6 +657,7 @@ static int wpa2_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bs wpa_printf(MSG_INFO, ">>>>>wpa2 FINISH\n"); ret = WPA2_ENT_EAP_STATE_SUCCESS; wpa2_set_eap_state(WPA2_ENT_EAP_STATE_SUCCESS); + eap_deinit_prev_method(sm, "EAP Success"); } else { wpa_printf(MSG_INFO, ">>>>>wpa2 FAILED, receive EAP_SUCCESS but pmk is empty, potential attack!\n"); ret = WPA2_ENT_EAP_STATE_FAIL; @@ -821,7 +851,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_enable_fn(void *arg) return ESP_ERR_NO_MEM; } - wpa2_cb->wpa2_sm_rx_eapol = wpa2_sm_rx_eapol; + wpa2_cb->wpa2_sm_rx_eapol = wpa2_ent_rx_eapol; wpa2_cb->wpa2_start = wpa2_start_eapol; wpa2_cb->wpa2_init = eap_peer_sm_init; wpa2_cb->wpa2_deinit = eap_peer_sm_deinit; From 430b73776034e62dcdd3541bec6ee0dd47e67d22 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 28 Aug 2019 10:03:17 +0100 Subject: [PATCH 089/146] Fix libcoap building with various MbedTLS compile time options components/coap/port/coap_mbedtls.c: Add in additional compile time check wrappers for different MbedTLS configurations. components/coap/CMakeLists.txt: components/coap/component.mk: components/coap/port/coap_notls.c: components/coap/port/include/coap_config_posix.h: Add in the ability to compile and run if MbedTLS does not have any TLS mode enabled. examples/protocols/coap_client/main/coap_client_example_main.c: Inform user that MbedTLS Client Mode is required for DTLS if not enabled, and coaps:// has been requested. [Lower libcoap library will still error and report this condition] examples/protocols/coap_server/main/coap_server_example_main.c: Inform user that MbedTLS Server Mode is required for DTLS if not enabled. [Lower libcoap library will still error and report this condition] Closes https://github.com/espressif/esp-idf/issues/3961 Closes https://github.com/espressif/esp-idf/issues/3971 Closes https://github.com/espressif/esp-idf/pull/3977 --- components/coap/CMakeLists.txt | 1 + components/coap/component.mk | 2 +- components/coap/port/coap_mbedtls.c | 49 ++++- components/coap/port/coap_notls.c | 190 ++++++++++++++++++ .../coap/port/include/coap_config_posix.h | 16 +- .../main/coap_client_example_main.c | 13 +- .../main/coap_server_example_main.c | 8 + 7 files changed, 266 insertions(+), 13 deletions(-) create mode 100644 components/coap/port/coap_notls.c diff --git a/components/coap/CMakeLists.txt b/components/coap/CMakeLists.txt index 3b2da20ce8..50c55e7723 100644 --- a/components/coap/CMakeLists.txt +++ b/components/coap/CMakeLists.txt @@ -19,6 +19,7 @@ set(srcs "libcoap/src/subscribe.c" "libcoap/src/uri.c" "libcoap/src/coap_io.c" + "port/coap_notls.c" "port/coap_mbedtls.c") idf_component_register(SRCS "${srcs}" diff --git a/components/coap/component.mk b/components/coap/component.mk index 46f7039870..448a94f6cb 100644 --- a/components/coap/component.mk +++ b/components/coap/component.mk @@ -4,7 +4,7 @@ COMPONENT_ADD_INCLUDEDIRS := port/include port/include/coap libcoap/include libcoap/include/coap2 -COMPONENT_OBJS = libcoap/src/address.o libcoap/src/async.o libcoap/src/block.o libcoap/src/coap_event.o libcoap/src/coap_hashkey.o libcoap/src/coap_session.o libcoap/src/coap_time.o port/coap_debug.o libcoap/src/encode.o libcoap/src/mem.o libcoap/src/net.o libcoap/src/option.o libcoap/src/pdu.o libcoap/src/resource.o libcoap/src/str.o libcoap/src/subscribe.o libcoap/src/uri.o port/coap_mbedtls.o libcoap/src/coap_io.o +COMPONENT_OBJS = libcoap/src/address.o libcoap/src/async.o libcoap/src/block.o libcoap/src/coap_event.o libcoap/src/coap_hashkey.o libcoap/src/coap_session.o libcoap/src/coap_time.o port/coap_debug.o libcoap/src/encode.o libcoap/src/mem.o libcoap/src/net.o libcoap/src/option.o libcoap/src/pdu.o libcoap/src/resource.o libcoap/src/str.o libcoap/src/subscribe.o libcoap/src/uri.o port/coap_mbedtls.o libcoap/src/coap_io.o port/coap_notls.o COMPONENT_SRCDIRS := libcoap/src libcoap port diff --git a/components/coap/port/coap_mbedtls.c b/components/coap/port/coap_mbedtls.c index 9f8c1fab98..4d616e9098 100644 --- a/components/coap/port/coap_mbedtls.c +++ b/components/coap/port/coap_mbedtls.c @@ -188,7 +188,7 @@ coap_dgram_write(void *ctx, const unsigned char *send_buffer, return result; } -#if !defined(ESPIDF_VERSION) || defined(CONFIG_MBEDTLS_SSL_PROTO_DTLS) +#if !defined(ESPIDF_VERSION) || (defined(CONFIG_MBEDTLS_SSL_PROTO_DTLS) && defined(CONFIG_MBEDTLS_TLS_SERVER)) static char* get_ip_addr(const struct coap_address_t *addr) { @@ -222,9 +222,9 @@ get_ip_addr(const struct coap_address_t *addr) } return str; } -#endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_SSL_PROTO_DTLS */ +#endif /* !ESPIDF_VERSION || (CONFIG_MBEDTLS_SSL_PROTO_DTLS && CONFIG_MBEDTLS_TLS_SERVER) */ -#if !defined(ESPIDF_VERSION) || (defined(CONFIG_MBEDTLS_SSL_PROTO_DTLS) && defined(CONFIG_MBEDTLS_PSK_MODES)) +#if !defined(ESPIDF_VERSION) || (defined(CONFIG_MBEDTLS_SSL_PROTO_DTLS) && defined(CONFIG_MBEDTLS_PSK_MODES) && defined(CONFIG_MBEDTLS_TLS_SERVER)) /* * Server side PSK callback */ @@ -277,7 +277,7 @@ static int psk_server_callback(void *p_info, mbedtls_ssl_context *ssl, mbedtls_ssl_set_hs_psk(ssl, buf, psk_len); return 0; } -#endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_SSL_PROTO_DTLS */ +#endif /* !ESPIDF_VERSION || (CONFIG_MBEDTLS_SSL_PROTO_DTLS && CONFIG_MBEDTLS_PSK_MODES && CONFIG_MBEDTLS_TLS_SERVER) */ static char* get_san_or_cn_from_cert(mbedtls_x509_crt *crt) @@ -691,6 +691,7 @@ setup_pki_credentials(mbedtls_x509_crt *cacert, return 0; } +#if !defined(ESPIDF_VERSION) || defined(CONFIG_MBEDTLS_TLS_SERVER) /* * PKI SNI callback. */ @@ -763,6 +764,7 @@ end: } return ret; } +#endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_TLS_SERVER */ #ifdef PSK2_PR /* @@ -836,6 +838,8 @@ end: } #endif /* PSK2_PR */ +#if !defined(ESPIDF_VERSION) || defined(CONFIG_MBEDTLS_TLS_SERVER) + static int setup_server_ssl_session(coap_session_t *c_session, coap_mbedtls_env_t *m_env) { @@ -897,11 +901,12 @@ static int setup_server_ssl_session(coap_session_t *c_session, mbedtls_ssl_conf_dtls_cookies(&m_env->conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &m_env->cookie_ctx ); - mbedtls_ssl_set_mtu(&m_env->ssl, c_session->mtu); + mbedtls_ssl_set_mtu(&m_env->ssl, c_session->mtu); #endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_SSL_PROTO_DTLS */ fail: return ret; } +#endif /* !defined(ESPIDF_VERSION) || CONFIG_MBEDTLS_TLS_SERVER) */ #define MAX_CIPHERS 100 static int psk_ciphers[MAX_CIPHERS]; @@ -1043,6 +1048,7 @@ static int setup_client_ssl_session(coap_session_t *c_session, coap_log(LOG_ERR, "PKI setup failed\n"); return ret; } +#if !defined(ESPIDF_VERSION) || defined(CONFIG_MBEDTLS_TLS_SERVER) if (c_session->proto == COAP_PROTO_TLS) { const char *alpn_list[2]; @@ -1053,6 +1059,7 @@ static int setup_client_ssl_session(coap_session_t *c_session, coap_log(LOG_ERR, "ALPN setup failed %d)\n", ret); } } +#endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_TLS_SERVER */ if (m_context->setup_data.client_sni) { mbedtls_ssl_set_hostname(&m_env->ssl, m_context->setup_data.client_sni); } @@ -1189,10 +1196,12 @@ static coap_mbedtls_env_t *coap_dtls_new_mbedtls_env(coap_session_t *c_session, if (setup_client_ssl_session(c_session, m_env) != 0) { goto fail; } +#if !defined(ESPIDF_VERSION) || defined(CONFIG_MBEDTLS_TLS_SERVER) } else if (role == COAP_DTLS_ROLE_SERVER) { if (setup_server_ssl_session(c_session, m_env) != 0) { goto fail; } +#endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_TLS_SERVER */ } else { goto fail; } @@ -1251,6 +1260,12 @@ int coap_dtls_context_set_psk(struct coap_context_t *c_context, { coap_mbedtls_context_t *m_context = ((coap_mbedtls_context_t *)c_context->dtls_context); +#if defined(ESPIDF_VERSION) && !defined(CONFIG_MBEDTLS_TLS_SERVER) + coap_log(LOG_EMERG, "coap_dtls_context_set_psk:" + " libcoap not compiled for Server Mode for MbedTLS" + " - update MbedTLS to include Server Mode\n"); + return 0; +#endif /* ESPIDF_VERSION && !CONFIG_MBEDTLS_TLS_SERVER */ m_context->psk_pki_enabled |= IS_PSK; return 1; } @@ -1266,6 +1281,12 @@ coap_dtls_context_set_spsk(coap_context_t *c_context, coap_mbedtls_context_t *m_context = ((coap_mbedtls_context_t *)c_context->dtls_context); +#if defined(ESPIDF_VERSION) && !defined(CONFIG_MBEDTLS_TLS_SERVER) + coap_log(LOG_EMERG, "coap_dtls_context_set_spsk:" + " libcoap not compiled for Server Mode for MbedTLS" + " - update MbedTLS to include Server Mode\n"); + return 0; +#endif /* ESPIDF_VERSION && !CONFIG_MBEDTLS_TLS_SERVER */ if (!m_context || !setup_data) return 0; @@ -1315,6 +1336,7 @@ int coap_dtls_context_set_pki_root_cas(struct coap_context_t *c_context, { coap_mbedtls_context_t *m_context = ((coap_mbedtls_context_t *)c_context->dtls_context); + if (!m_context) { coap_log(LOG_WARNING, "coap_context_set_pki_root_cas: (D)TLS environment " @@ -1383,6 +1405,13 @@ void coap_dtls_free_context(void *dtls_context) void *coap_dtls_new_client_session(coap_session_t *c_session) { +#if defined(ESPIDF_VERSION) && !defined(CONFIG_MBEDTLS_TLS_CLIENT) + (void)c_session; + coap_log(LOG_EMERG, "coap_dtls_new_client_session:" + " libcoap not compiled for Client Mode for MbedTLS" + " - update MbedTLS to include Client Mode\n"); + return NULL; +#else /* !ESPIDF_VERSION || CONFIG_MBEDTLS_TLS_CLIENT */ coap_mbedtls_env_t *m_env = coap_dtls_new_mbedtls_env(c_session, COAP_DTLS_ROLE_CLIENT); int ret; @@ -1395,6 +1424,7 @@ void *coap_dtls_new_client_session(coap_session_t *c_session) } } return m_env; +#endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_TLS_CLIENT */ } void *coap_dtls_new_server_session(coap_session_t *c_session) @@ -1628,12 +1658,15 @@ int coap_dtls_hello(coap_session_t *c_session, const uint8_t *data, size_t data_len) { -#if defined(ESPIDF_VERSION) && !defined(CONFIG_MBEDTLS_SSL_PROTO_DTLS) +#if defined(ESPIDF_VERSION) && (!defined(CONFIG_MBEDTLS_SSL_PROTO_DTLS) || !defined(CONFIG_MBEDTLS_TLS_SERVER)) (void)c_session; (void)data; (void)data_len; + coap_log(LOG_EMERG, "coap_dtls_hello:" + " libcoap not compiled for DTLS or Server Mode for MbedTLS" + " - update MbedTLS to include DTLS and Server Mode\n"); return -1; -#else /* !ESPIDF_VERSION) || CONFIG_MBEDTLS_SSL_PROTO_DTLS */ +#else /* !ESPIDF_VERSION) || (CONFIG_MBEDTLS_SSL_PROTO_DTLS && CONFIG_MBEDTLS_TLS_SERVER) */ coap_mbedtls_env_t *m_env = (coap_mbedtls_env_t *)c_session->tls; coap_ssl_t *ssl_data = m_env ? &m_env->coap_ssl_data : NULL; int ret; @@ -1697,7 +1730,7 @@ int coap_dtls_hello(coap_session_t *c_session, return 1; } return 0; -#endif /* !ESPIDF_VERSION || CONFIG_MBEDTLS_SSL_PROTO_DTLS */ +#endif /* !ESPIDF_VERSION) || (CONFIG_MBEDTLS_SSL_PROTO_DTLS && CONFIG_MBEDTLS_TLS_SERVER) */ } unsigned int coap_dtls_get_overhead(coap_session_t *c_session UNUSED) diff --git a/components/coap/port/coap_notls.c b/components/coap/port/coap_notls.c new file mode 100644 index 0000000000..aa2c27c688 --- /dev/null +++ b/components/coap/port/coap_notls.c @@ -0,0 +1,190 @@ +/* +* coap_notls.c -- Stub Datagram Transport Layer Support for libcoap +* +* Copyright (C) 2016 Olaf Bergmann +* +* This file is part of the CoAP library libcoap. Please see README for terms +* of use. +*/ + +#include "coap_config.h" + +#if !defined(HAVE_LIBTINYDTLS) && !defined(HAVE_OPENSSL) && !defined(HAVE_LIBGNUTLS) && !defined(HAVE_MBEDTLS) + +#include "net.h" + +#ifdef __GNUC__ +#define UNUSED __attribute__((unused)) +#else /* __GNUC__ */ +#define UNUSED +#endif /* __GNUC__ */ + +int +coap_dtls_is_supported(void) { + return 0; +} + +int +coap_tls_is_supported(void) { + return 0; +} + +coap_tls_version_t * +coap_get_tls_library_version(void) { + static coap_tls_version_t version; + version.version = 0; + version.type = COAP_TLS_LIBRARY_NOTLS; + return &version; +} + +int +coap_dtls_context_set_pki(coap_context_t *ctx UNUSED, + coap_dtls_pki_t* setup_data UNUSED, + coap_dtls_role_t role UNUSED +) { + return 0; +} + +int +coap_dtls_context_set_pki_root_cas(struct coap_context_t *ctx UNUSED, + const char *ca_file UNUSED, + const char *ca_path UNUSED +) { + return 0; +} + +int +coap_dtls_context_set_psk(coap_context_t *ctx UNUSED, + const char *hint UNUSED, + coap_dtls_role_t role UNUSED +) { + return 0; +} + +int +coap_dtls_context_check_keys_enabled(coap_context_t *ctx UNUSED) +{ + return 0; +} + +static int dtls_log_level = 0; + +void coap_dtls_startup(void) { +} + +void +coap_dtls_set_log_level(int level) { + dtls_log_level = level; +} + +int +coap_dtls_get_log_level(void) { + return dtls_log_level; +} + +void * +coap_dtls_new_context(struct coap_context_t *coap_context UNUSED) { + return NULL; +} + +void +coap_dtls_free_context(void *handle UNUSED) { +} + +void *coap_dtls_new_server_session(coap_session_t *session UNUSED) { + return NULL; +} + +void *coap_dtls_new_client_session(coap_session_t *session UNUSED) { + return NULL; +} + +void coap_dtls_free_session(coap_session_t *coap_session UNUSED) { +} + +void coap_dtls_session_update_mtu(coap_session_t *session UNUSED) { +} + +int +coap_dtls_send(coap_session_t *session UNUSED, + const uint8_t *data UNUSED, + size_t data_len UNUSED +) { + return -1; +} + +int coap_dtls_is_context_timeout(void) { + return 1; +} + +coap_tick_t coap_dtls_get_context_timeout(void *dtls_context UNUSED) { + return 0; +} + +coap_tick_t +coap_dtls_get_timeout(coap_session_t *session UNUSED, coap_tick_t now UNUSED) { + return 0; +} + +void coap_dtls_handle_timeout(coap_session_t *session UNUSED) { +} + +int +coap_dtls_receive(coap_session_t *session UNUSED, + const uint8_t *data UNUSED, + size_t data_len UNUSED +) { + return -1; +} + +int +coap_dtls_hello(coap_session_t *session UNUSED, + const uint8_t *data UNUSED, + size_t data_len UNUSED +) { + return 0; +} + +unsigned int coap_dtls_get_overhead(coap_session_t *session UNUSED) { + return 0; +} + +void *coap_tls_new_client_session(coap_session_t *session UNUSED, int *connected UNUSED) { + return NULL; +} + +void *coap_tls_new_server_session(coap_session_t *session UNUSED, int *connected UNUSED) { + return NULL; +} + +void coap_tls_free_session(coap_session_t *coap_session UNUSED) { +} + +ssize_t coap_tls_write(coap_session_t *session UNUSED, + const uint8_t *data UNUSED, + size_t data_len UNUSED +) { + return -1; +} + +ssize_t coap_tls_read(coap_session_t *session UNUSED, + uint8_t *data UNUSED, + size_t data_len UNUSED +) { + return -1; +} + +#undef UNUSED + +#else /* !HAVE_LIBTINYDTLS && !HAVE_OPENSSL && !HAVE_LIBGNUTLS */ + +#ifdef __clang__ +/* Make compilers happy that do not like empty modules. As this function is + * never used, we ignore -Wunused-function at the end of compiling this file + */ +#pragma GCC diagnostic ignored "-Wunused-function" +#endif +static inline void dummy(void) { +} + +#endif /* !HAVE_LIBTINYDTLS && !HAVE_OPENSSL && !HAVE_LIBGNUTLS */ diff --git a/components/coap/port/include/coap_config_posix.h b/components/coap/port/include/coap_config_posix.h index dc1166090d..3f6b7c605f 100644 --- a/components/coap/port/include/coap_config_posix.h +++ b/components/coap/port/include/coap_config_posix.h @@ -29,13 +29,27 @@ #define HAVE_TIME_H #define HAVE_NETDB_H #define HAVE_NETINET_IN_H +#define HAVE_STRUCT_CMSGHDR -#define IPV6_PKTINFO IPV6_V6ONLY +#define ipi_spec_dst ipi_addr +struct in6_pktinfo { + struct in6_addr ipi6_addr; /* src/dst IPv6 address */ + unsigned int ipi6_ifindex; /* send/recv interface index */ +}; +#define IN6_IS_ADDR_V4MAPPED(a) \ + ((((__const uint32_t *) (a))[0] == 0) \ + && (((__const uint32_t *) (a))[1] == 0) \ + && (((__const uint32_t *) (a))[2] == htonl (0xffff))) + +/* As not defined, just need to define is as something innocuous */ +#define IPV6_PKTINFO IPV6_CHECKSUM #define PACKAGE_NAME "libcoap-posix" #define PACKAGE_VERSION "?" +#ifdef CONFIG_MBEDTLS_TLS_ENABLED #define HAVE_MBEDTLS +#endif /* CONFIG_MBEDTLS_TLS_ENABLED */ #define COAP_CONSTRAINED_STACK 1 #define ESPIDF_VERSION diff --git a/examples/protocols/coap_client/main/coap_client_example_main.c b/examples/protocols/coap_client/main/coap_client_example_main.c index c7223bc0e6..de7ec60be9 100644 --- a/examples/protocols/coap_client/main/coap_client_example_main.c +++ b/examples/protocols/coap_client/main/coap_client_example_main.c @@ -219,9 +219,12 @@ static void coap_example_client(void *p) break; } - if ((uri.scheme == COAP_URI_SCHEME_COAPS && !coap_dtls_is_supported()) || - (uri.scheme == COAP_URI_SCHEME_COAPS_TCP && !coap_tls_is_supported())) { - ESP_LOGE(TAG, "CoAP server uri scheme is not supported"); + if (uri.scheme == COAP_URI_SCHEME_COAPS && !coap_dtls_is_supported()) { + ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); + break; + } + if (uri.scheme == COAP_URI_SCHEME_COAPS_TCP && !coap_tls_is_supported()) { + ESP_LOGE(TAG, "CoAP server uri coaps+tcp:// scheme is not supported"); break; } @@ -308,6 +311,10 @@ static void coap_example_client(void *p) * but the code is left in for completeness. */ if (uri.scheme == COAP_URI_SCHEME_COAPS || uri.scheme == COAP_URI_SCHEME_COAPS_TCP) { +#ifndef CONFIG_MBEDTLS_TLS_CLIENT + ESP_LOGE(TAG, "MbedTLS (D)TLS Client Mode not configured"); + goto clean_up; +#endif /* CONFIG_MBEDTLS_TLS_CLIENT */ #ifdef CONFIG_COAP_MBEDTLS_PSK session = coap_new_client_session_psk(ctx, NULL, &dst_addr, uri.scheme == COAP_URI_SCHEME_COAPS ? COAP_PROTO_DTLS : COAP_PROTO_TLS, diff --git a/examples/protocols/coap_server/main/coap_server_example_main.c b/examples/protocols/coap_server/main/coap_server_example_main.c index 29bf691223..562c85a833 100644 --- a/examples/protocols/coap_server/main/coap_server_example_main.c +++ b/examples/protocols/coap_server/main/coap_server_example_main.c @@ -252,12 +252,20 @@ static void coap_example_server(void *p) } #if defined(CONFIG_COAP_MBEDTLS_PSK) || defined(CONFIG_COAP_MBEDTLS_PKI) if (coap_dtls_is_supported()) { +#ifndef CONFIG_MBEDTLS_TLS_SERVER + /* This is not critical as unencrypted support is still available */ + ESP_LOGI(TAG, "MbedTLS (D)TLS Server Mode not configured"); +#else /* CONFIG_MBEDTLS_TLS_SERVER */ serv_addr.addr.sin.sin_port = htons(COAPS_DEFAULT_PORT); ep = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_DTLS); if (!ep) { ESP_LOGE(TAG, "dtls: coap_new_endpoint() failed"); goto clean_up; } +#endif /* CONFIG_MBEDTLS_TLS_SERVER */ + } else { + /* This is not critical as unencrypted support is still available */ + ESP_LOGI(TAG, "MbedTLS (D)TLS Server Mode not configured"); } #endif /* CONFIG_COAP_MBEDTLS_PSK CONFIG_COAP_MBEDTLS_PKI */ resource = coap_resource_init(coap_make_str_const("Espressif"), 0); From 758abe6e0104312efbe8b708945231a2c1dbefba Mon Sep 17 00:00:00 2001 From: Wang Fang Date: Fri, 6 Sep 2019 19:12:36 +0800 Subject: [PATCH 090/146] Add Chinese translation for the rst documents in storage folder, including: 1. docs/zh_CN/api-reference/storage/fatfs.rst 2. docs/zh_CN/api-reference/storage/index.rst 3. docs/zh_CN/api-reference/storage/sdmmc.rst 4. docs/zh_CN/api-reference/storage/spiffs.rst 5. docs/zh_CN/api-reference/storage/wear-levelling.rst --- docs/en/api-reference/storage/fatfs.rst | 2 + docs/en/api-reference/storage/index.rst | 2 + docs/en/api-reference/storage/spiffs.rst | 2 + docs/zh_CN/api-reference/storage/fatfs.rst | 84 +++++++++- docs/zh_CN/api-reference/storage/index.rst | 21 ++- docs/zh_CN/api-reference/storage/spiffs.rst | 143 +++++++++++++++++- .../api-reference/storage/wear-levelling.rst | 34 ++++- 7 files changed, 284 insertions(+), 4 deletions(-) diff --git a/docs/en/api-reference/storage/fatfs.rst b/docs/en/api-reference/storage/fatfs.rst index 6e91897a5f..3997d7ce8f 100644 --- a/docs/en/api-reference/storage/fatfs.rst +++ b/docs/en/api-reference/storage/fatfs.rst @@ -1,6 +1,8 @@ FAT Filesystem Support ====================== +:link_to_translation:`zh_CN:[中文]` + ESP-IDF uses the `FatFs `_ library to work with FAT filesystems. FatFs resides in the ``fatfs`` component. Although the library can be used directly, many of its features can be accessed via VFS, using the C standard library and POSIX API functions. Additionally, FatFs has been modified to support the runtime pluggable disk I/O layer. This allows mapping of FatFs drives to physical disks at runtime. diff --git a/docs/en/api-reference/storage/index.rst b/docs/en/api-reference/storage/index.rst index 23cb38b647..60d762f2f7 100644 --- a/docs/en/api-reference/storage/index.rst +++ b/docs/en/api-reference/storage/index.rst @@ -1,6 +1,8 @@ Storage API *********** +:link_to_translation:`zh_CN:[中文]` + .. toctree:: :maxdepth: 1 diff --git a/docs/en/api-reference/storage/spiffs.rst b/docs/en/api-reference/storage/spiffs.rst index 5620e83d77..4182c34f21 100644 --- a/docs/en/api-reference/storage/spiffs.rst +++ b/docs/en/api-reference/storage/spiffs.rst @@ -1,6 +1,8 @@ SPIFFS Filesystem ================= +:link_to_translation:`zh_CN:[中文]` + Overview -------- diff --git a/docs/zh_CN/api-reference/storage/fatfs.rst b/docs/zh_CN/api-reference/storage/fatfs.rst index a867563eba..08b1a4c0b0 100644 --- a/docs/zh_CN/api-reference/storage/fatfs.rst +++ b/docs/zh_CN/api-reference/storage/fatfs.rst @@ -1 +1,83 @@ -.. include:: ../../../en/api-reference/storage/fatfs.rst \ No newline at end of file +FAT 文件系统 +====================== + +:link_to_translation:`en:[English]` + +ESP-IDF 使用 `FatFs `_ 库来实现 FAT 文件系统。FatFs 库位于 ``fatfs`` 组件中,您可以直接使用,也可以借助 C 标准库和 POSIX API 通过 VFS(虚拟文件系统)使用 FatFs 库的大多数功能。 + +此外,我们对 FatFs 库进行了扩展,新增了支持可插拔磁盘 I/O 调度层,从而允许在运行时将 FatFs 驱动映射到物理磁盘。 + +FatFs 与 VFS 配合使用 +---------------------------- + +:component_file:`fatfs/vfs/esp_vfs_fat.h` 头文件定义了连接 FatFs 和 VFS 的函数。 + +函数 :cpp:func:`esp_vfs_fat_register` 分配一个 ``FATFS`` 结构,并在 VFS 中注册特定路径前缀。如果文件路径以此前缀开头,则对此文件的后续操作将转至 FatFs API。函数 :cpp:func:`esp_vfs_fat_unregister_path` 删除在 VFS 中的注册,并释放 ``FATFS`` 结构。 + +多数应用程序在使用 ``esp_vfs_fat_`` 函数时,采用如下步骤: + +1. 调用 :cpp:func:`esp_vfs_fat_register`,指定: + + - 挂载文件系统的路径前缀(例如,``"/sdcard"`` 或 ``"/spiflash"``) + - FatFs 驱动编号 + - 一个用于接收指向 ``FATFS`` 结构指针的变量 + +2. 调用 :cpp:func:`ff_diskio_register` 为上述步骤中的驱动编号注册磁盘 I/O 驱动; + +3. 调用 FatFs 函数 ``f_mount``,或 ``f_fdisk``, ``f_mkfs``,并使用与传递到 :cpp:func:`esp_vfs_fat_register` 相同的驱动编号挂载文件系统。请参考 `FatFs 文档 `_,查看更多信息; + +4. 调用 C 标准库和 POSIX API 对路径中带有步骤 1 中所述前缀的文件(例如,``"/sdcard/hello.txt"``)执行打开、读取、写入、擦除、复制等操作。 + +5. 您可以选择直接调用 FatFs 库函数,但需要使用没有 VFS 前缀的路径(例如,``"/hello.txt"``); + +6. 关闭所有打开的文件; + +7. 调用 ``f_mount`` 并使用 NULL ``FATFS*`` 参数为与上述编号相同的驱动卸载文件系统; + +8. 调用 FatFs 函数 :cpp:func:`ff_diskio_register` 并使用 NULL ``ff_diskio_impl_t*`` 参数和相同的驱动编号来释放注册的磁盘 I/O 驱动。 + +9. 调用 :cpp:func:`esp_vfs_fat_unregister_path` 并使用文件系统挂载的路径将 FatFs 从 NVS 中移除,并释放步骤 1 中分配的 FatFs 结构。 + +``esp_vfs_fat_sdmmc_mount`` 和 ``esp_vfs_fat_sdmmc_unmount`` 这两个便捷函数对上述步骤进行了封装,并加入对 SD 卡初始化的处理,非常便捷。我们将在下一章节详细介绍这两个函数。 + +.. doxygenfunction:: esp_vfs_fat_register +.. doxygenfunction:: esp_vfs_fat_unregister_path + + +FatFs 与 VFS 和 SD 卡配合使用 +--------------------------------- + +:component_file:`fatfs/vfs/esp_vfs_fat.h` 头文件定义了两个便捷函数 :cpp:func:`esp_vfs_fat_sdmmc_mount` 和 :cpp:func:`esp_vfs_fat_sdmmc_unmount`。这两个函数分别执行上一章节的步骤 1-3 和步骤 7-9,并初始化 SD 卡,但仅提供有限的错误处理功能。我们鼓励开发人员查看源代码并将更多高级功能集成到产品应用中。 + +:cpp:func:`esp_vfs_fat_sdmmc_unmount` 函数用于卸载文件系统并释放从 :cpp:func:`esp_vfs_fat_sdmmc_mount` 函数获取的资源。 + +.. doxygenfunction:: esp_vfs_fat_sdmmc_mount +.. doxygenstruct:: esp_vfs_fat_mount_config_t + :members: +.. doxygenfunction:: esp_vfs_fat_sdmmc_unmount + + +FatFs 与 VFS 配合使用(只读模式下) +-------------------------------------- + +:component_file:`fatfs/vfs/esp_vfs_fat.h` 头文件也定义了两个便捷函数 :cpp:func:`esp_vfs_fat_rawflash_mount` 和 :cpp:func:`esp_vfs_fat_rawflash_unmount`。上述两个函数分别对 FAT 只读分区执行步骤 1-3 和步骤 7-9。有些数据分区仅在工厂时写入一次,之后在整个硬件生命周期内都不会再有任何改动。利用上述两个函数处理这种数据分区非常方便。 + +.. doxygenfunction:: esp_vfs_fat_rawflash_mount +.. doxygenfunction:: esp_vfs_fat_rawflash_unmount + + +FatFs 磁盘 I/O 层 +------------------- + +我们对 FatFs API 函数进行了扩展,实现了运行期间注册磁盘 I/O 驱动。 + +上述 API 为 SD/MMC 卡提供了磁盘 I/O 函数实现方式,可使用 :cpp:func:`ff_diskio_register_sdmmc` 注册指定的 FatFs 驱动编号。 + +.. doxygenfunction:: ff_diskio_register +.. doxygenstruct:: ff_diskio_impl_t + :members: +.. doxygenfunction:: ff_diskio_register_sdmmc +.. doxygenfunction:: ff_diskio_register_wl_partition +.. doxygenfunction:: ff_diskio_register_raw_partition + + diff --git a/docs/zh_CN/api-reference/storage/index.rst b/docs/zh_CN/api-reference/storage/index.rst index f30a807b73..902f803c7a 100644 --- a/docs/zh_CN/api-reference/storage/index.rst +++ b/docs/zh_CN/api-reference/storage/index.rst @@ -1 +1,20 @@ -.. include:: ../../../en/api-reference/storage/index.rst \ No newline at end of file +存储 API +*********** + +:link_to_translation:`en:[English]` + +.. toctree:: + :maxdepth: 1 + + SPI Flash 和分区 API + SD/SDIO/MMC 驱动程序 + 非易失性存储 + NVS 分区生成程序 + 虚拟文件系统 + FAT 文件系统 + 磨损均衡 + SPIFFS 文件系统 + 量产程序 + + +此部分 API 代码示例详见 ESP-IDF 项下 :example:`storage` 目录。 diff --git a/docs/zh_CN/api-reference/storage/spiffs.rst b/docs/zh_CN/api-reference/storage/spiffs.rst index 4179e3c661..941cd423ba 100644 --- a/docs/zh_CN/api-reference/storage/spiffs.rst +++ b/docs/zh_CN/api-reference/storage/spiffs.rst @@ -1 +1,142 @@ -.. include:: ../../../en/api-reference/storage/spiffs.rst \ No newline at end of file +SPIFFS 文件系统 +================= + +:link_to_translation:`en:[English]` + +概述 +-------- + +SPIFFS 是一个用于 SPI NOR flash 设备的嵌入式文件系统,支持磨损均衡、文件系统一致性检查等功能。 + +说明 +----- + + - 目前,SPIFFS 尚不支持目录,但可以生成扁平结构。如果 SPIFFS 挂载在 ``/spiffs`` 下,在 ``/spiffs/tmp/myfile.txt`` 路径下创建一个文件则会在 SPIFFS 中生成一个名为 ``/tmp/myfile.txt`` 的文件,而不是在 ``/spiffs/tmp`` 下生成名为 ``myfile.txt`` 的文件; + - SPIFFS 并非实时栈,每次写操作耗时不等; + - 目前,SPIFFS 尚不支持检测或处理已损坏的块。 + +工具 +----- + +spiffsgen.py +^^^^^^^^^^^^^^^^ + +:component_file:`spiffsgen.py`(只写)是 SPIFFS 的一种 Python 实现,可用于从主机文件夹内容生成文件系统映像。打开终端并运行以下命令即可使用 ``spiffsgen.py``:: + + python spiffsgen.py + +参数(必选)说明如下: + +- **image_size**:分区大小,用于烧录生成的 SPIFFS 映像; +- **base_dir**:创建 SPIFFS 映像的目录; +- **output_file**:SPIFFS 映像输出文件。 + +其他参数(可选)也参与控制映像的生成,您可以运行以下帮助命令,查看这些参数的具体信息:: + + python spiffsgen.py --help + +上述可选参数对应 SPIFFS 构建配置选项。若想顺利生成可用的映像,请确保使用的参数或配置与构建 SPIFFS 时所用的参数或配置相同。运行帮助命令将显示参数所对应的 SPIFFS 构建配置。如未指定参数,将使用帮助信息中的默认值。 + +映像生成后,您可以使用 ``esptool.py`` 或 ``parttool.py`` 烧录映像。 + +您可以在命令行或脚本中手动单独调用 ``spiffsgen.py``,也可以直接从构建系统调用 ``spiffs_create_partition_image`` 来使用 ``spiffsgen.py``。 + +在 Make 构建系统中运行:: + + SPIFFS_IMAGE_FLASH_IN_PROJECT := ... + SPIFFS_IMAGE_DEPENDS := ... + $(eval $(call spiffs_create_partition_image,,)) + +在 CMake 构建系统中运行:: + + spiffs_create_partition_image( [FLASH_IN_PROJECT] [DEPENDS dep dep dep...]) + +在构建系统中使用 ``spiffsgen.py`` 更为方便,构建配置自动传递给 ``spiffsgen.py`` 工具,确保生成的映像可用于构建。比如,单独调用 ``spiffsgen.py`` 时需要用到 *image_size* 参数,但在构建系统中调用 ``spiffs_create_partition_image`` 时,仅需要 *partition* 参数,映像大小将直接从工程分区表中获取。 + +Make 构建系统和 CMake 构建系统结构有所不同,请注意以下几点: + +- 在 Make 构建系统中使用 ``spiffs_create_partition_image``,需从工程 Makefile 中调用; +- 在 CMake 构建系统中使用 ``spiffs_create_partition_image``,需从组件 CMakeLists.txt 文件调用。 + +您也可以指定 ``FLASH_IN_PROJECT``,然后使用 ``idf.py flash`` 或 ``make flash`` 将映像与应用程序二进制文件、分区表等一起自动烧录至设备,例如: + +在 Make 构建系统中运行:: + + SPIFFS_IMAGE_FLASH_IN_PROJECT := 1 + $(eval $(call spiffs_create_partition_image,,)) + +在 CMake 构建系统中运行:: + + spiffs_create_partition_image(my_spiffs_partition my_folder FLASH_IN_PROJECT) + +不指定 FLASH_IN_PROJECT/SPIFFS_IMAGE_FLASH_IN_PROJECT 也可以生成映像,但须使用 ``esptool.py``、``parttool.py`` 或自定义构建系统目标手动烧录。 + +有时基本目录中的内容是在构建时生成的,您可以使用 DEPENDS/SPIFFS_IMAGE_DEPENDS 指定目标,因此可以在生成映像之前执行此目标。 + +在 Make 构建系统中运行:: + + dep: + ... + + SPIFFS_IMAGE_DEPENDS := dep + $(eval $(call spiffs_create_partition_image,,)) + +在 CMake 构建系统中运行:: + + add_custom_target(dep COMMAND ...) + + spiffs_create_partition_image(my_spiffs_partition my_folder DEPENDS dep) + +请参考 :example:`examples/storage/spiffsgen>`,查看示例。 + +mkspiffs +^^^^^^^^^^^ + +您也可以使用 `mkspiffs `_ 工具创建 SPIFFS 分区映像。与 ``spiffsgen.py`` 相似,`mkspiffs `_ 也可以用于从指定文件夹中生成映像,然后使用 ``esptool.py`` 烧录映像。 + +该工具需要获取以下参数: + +- **Block Size**:4096(SPI flash 标准) +- **Page Size**:256(SPI flash 标准) +- **Image Size**:分区大小(以字节为单位,可从分区表中获取) +- **Partition Offset**:分区起始地址(可从分区表内获取) + +运行以下命令,将文件夹打包成 1 MB 大小的映像:: + + mkspiffs -c [src_folder] -b 4096 -p 256 -s 0x100000 spiffs.bin + +运行以下命令,将映像烧录到 ESP32(偏移量:0x110000):: + + python esptool.py --chip esp32 --port [port] --baud [baud] write_flash -z 0x110000 spiffs.bin + + +选择合适的 SPIFFS 工具 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +上面介绍的两款 SPIFFS 工具功能相似,需根据实际情况,选择合适的一款。 + +以下情况优先选用 ``spiffsgen.py`` 工具: + +1. 仅需在构建时简单生成 SPIFFS 映像,请选择使用 ``spiffsgen.py``,``spiffsgen.py`` 可以直接在构建系统中使用函数或命令生成 SPIFFS 映像。 +2. 主机没有可用的 C/C++ 编译器时,可以选择使用 ``spiffsgen.py`` 工具,``spiffsgen.py`` 不需要编译。 + +以下情况优先选用 ``mkspiffs`` 工具: + +1. 如果您除了需要生成映像外,还需要拆包 SPIFFS 映像,请选择使用 ``mkspiffs`` 工具。``spiffsgen.py`` 目前尚不支持此功能。 +2. 如果您当前环境中 Python 解释器不可用,但主机编译器可用,或者有预编译的 ``mkspiffs`` 二进制文件,此时请选择使用 ``mkspiffs`` 工具。但是,``mkspiffs`` 没有集成到构建系统,用户必须自己完成以下工作:在构建期间编译 ``mkspiffs`` (如果未使用预编译的二进制文件),为输出文件创建构建规则或目标,将适当的参数传递给工具等。 + +另请参阅 +-------- + +- :doc:`分区表 <../../api-guides/partition-tables>` + +应用示例 +------------------- + +:example:`storage/spiffs` 目录下提供了 SPIFFS 应用示例。该示例初始化并挂载了一个 SPIFFS 分区,然后使用 POSIX 和 C 库 API 写入和读取数据。请参考 ``example`` 目录下的 README.md 文件,查看详细信息。 + + +高级 API 参考 +------------------------ + +.. include:: /_build/inc/esp_spiffs.inc diff --git a/docs/zh_CN/api-reference/storage/wear-levelling.rst b/docs/zh_CN/api-reference/storage/wear-levelling.rst index 065b714541..4c50431d80 100644 --- a/docs/zh_CN/api-reference/storage/wear-levelling.rst +++ b/docs/zh_CN/api-reference/storage/wear-levelling.rst @@ -1 +1,33 @@ -.. include:: ../../../en/api-reference/storage/wear-levelling.rst \ No newline at end of file +.. include:: ../../../../components/wear_levelling/README.rst + +另请参阅 +----------- + +- :doc:`FAT 文件系统 <./fatfs>` +- :doc:`分区表 <../../api-guides/partition-tables>` + +应用示例 +------------------- + +:example:`storage/wear_levelling` 中提供了一款磨损均衡驱动与 FatFs 库结合使用的示例。该示例初始化磨损均衡驱动,挂载 FAT 文件系统分区,并使用 POSIX(可移植操作系统接口)和 C 库 API 从中写入和读取数据。如需了解更多信息,请参考 :example:`storage/wear_levelling/README.md`。 + +高级 API 参考 +------------------------ + +头文件 +^^^^^^^^^^^^ + +* :component_file:`fatfs/src/esp_vfs_fat.h` + +函数 +^^^^^^^^^ + +.. doxygenfunction:: esp_vfs_fat_spiflash_mount +.. doxygenstruct:: esp_vfs_fat_mount_config_t + :members: +.. doxygenfunction:: esp_vfs_fat_spiflash_unmount + +中层 API 参考 +----------------------- + +.. include:: /_build/inc/wear_levelling.inc From bea507378e2339a59b3ab1d0ccdcbd6f591999e3 Mon Sep 17 00:00:00 2001 From: Wang Fang Date: Fri, 6 Sep 2019 20:18:19 +0800 Subject: [PATCH 091/146] Add Chinese translation for README files in components and tools folders, including: 1. components/nvs_flash/nvs_partition_generator/README_CN.rst 2. components/wear_levelling/README_CN.rst 3. tools/mass_mfg/docs/README_CN.rst --- .../nvs_partition_generator/README.rst | 2 + .../nvs_partition_generator/README_CN.rst | 304 ++++++++++++++++++ components/wear_levelling/README.rst | 2 + components/wear_levelling/README_CN.rst | 47 +++ docs/zh_CN/api-reference/storage/mass_mfg.rst | 2 +- .../storage/nvs_partition_gen.rst | 2 +- .../api-reference/storage/wear-levelling.rst | 3 +- tools/mass_mfg/docs/README.rst | 2 + tools/mass_mfg/docs/README_CN.rst | 256 +++++++++++++++ 9 files changed, 617 insertions(+), 3 deletions(-) create mode 100644 components/nvs_flash/nvs_partition_generator/README_CN.rst create mode 100644 components/wear_levelling/README_CN.rst create mode 100644 tools/mass_mfg/docs/README_CN.rst diff --git a/components/nvs_flash/nvs_partition_generator/README.rst b/components/nvs_flash/nvs_partition_generator/README.rst index 800344223e..ae6212f511 100644 --- a/components/nvs_flash/nvs_partition_generator/README.rst +++ b/components/nvs_flash/nvs_partition_generator/README.rst @@ -1,6 +1,8 @@ NVS Partition Generator Utility =============================== +:link_to_translation:`zh_CN:[中文]` + Introduction ------------ diff --git a/components/nvs_flash/nvs_partition_generator/README_CN.rst b/components/nvs_flash/nvs_partition_generator/README_CN.rst new file mode 100644 index 0000000000..24094e9939 --- /dev/null +++ b/components/nvs_flash/nvs_partition_generator/README_CN.rst @@ -0,0 +1,304 @@ +NVS 分区生成程序 +=============================== + +:link_to_translation:`en:[English]` + +介绍 +------------ + +NVS 分区生成程序 (:component_file:`nvs_flash/nvs_partition_generator/nvs_partition_gen.py`) 根据 CSV 文件中的键值对生成二进制文件。该二进制文件与 :doc:`非易失性存储器 (NVS) ` 中定义的 NVS 结构兼容。NVS 分区生成程序适合用于生成二进制数据(Blob),其中包括设备生产时可从外部烧录的 ODM/OEM 数据。这也使得生产制造商在使用同一个固件的基础上,通过自定义参数,如序列号等,为每个设备生成不同配置。 + +准备工作 +------------- + +在加密模式下使用该程序,需安装下列软件包: + - cryptography package + +根目录下的 `requirements.txt` 包含必需 python 包,请预先安装。 + + +CSV 文件格式 +--------------- + +.csv 文件每行需包含四个参数,以逗号隔开。具体参数描述见下表: + ++------+----------+--------------------------------------------------------------+-----------------------------------------------------------------+ +| 序号 | 参数 | 描述 | 说明 | ++======+==========+==============================================================+=================================================================+ +| 1 | Key | 主键,应用程序可通过查询此键来获取数据。 | | ++------+----------+--------------------------------------------------------------+-----------------------------------------------------------------+ +| 2 | Type | 支持 ``file``、``data`` 和 ``namespace``。 | | ++------+----------+--------------------------------------------------------------+-----------------------------------------------------------------+ +| 3 | Encoding | 支持 ``u8``、``i8``、``u16``、``u32``、 | ``file`` | +| | | ``i32``、``string``、``hex2bin``、``base64`` 和 ``binary``。 | 类型当前仅支持 | +| | | 决定二进制 ``bin`` 文件中 value 被编码成的类型。 | ``hex2bin``、``base64``、 | +| | | ``string`` 和 ``binary`` 编码的区别在于, | ``string`` 和 ``binary`` 编码。 | +| | | ``string`` 数据以 NULL 字符结尾,``binary`` 数据则不是。 | | ++------+----------+--------------------------------------------------------------+-----------------------------------------------------------------+ +| 4 | Value | Data value | ``namespace`` 字段的 ``encoding`` 和 ``value`` 应为空。 | +| | | | ``namespace`` 的 ``encoding`` 和 ``value`` 为固定值,不可设置。 | +| | | | 这些单元格中的所有值都会被忽视。 | ++------+----------+--------------------------------------------------------------+-----------------------------------------------------------------+ + +.. note:: CSV 文件的第一行应为列标题,不可设置。 + +此类 CSV 文件的 Dump 示例如下:: + + key,type,encoding,value <-- 列标题 + namespace_name,namespace,, <-- 第一个条目为 "namespace" + key1,data,u8,1 + key2,file,string,/path/to/file + + +.. note:: + + 请确保: + - 逗号 ',' 前后无空格; + - CSV 文件每行末尾无空格。 + +NVS 条目和命名空间 (namespace) +----------------------------------- + +如 CSV 文件中出现命名空间条目,后续条目均会被视为该命名空间的一部分,直至找到下一个命名空间条目。找到新命名空间条目后,后续所有条目都会被视为新命名空间的一部分。 + +.. note:: CSV 文件中第一个条目应始终为 ``namespace``。 + + +支持多页 Blob +---------------------- + +默认情况下,二进制 Blob 可跨多页,格式参考 :ref:`structure_of_entry` 章节。如需使用旧版格式,可在程序中禁用该功能。 + + +支持加密 +------------------- + +NVS 分区生成程序还可使用 AES-XTS 加密生成二进制加密文件。更多信息详见 :ref:`nvs_encryption`。 + +支持解密 +------------------- +如果 NVS 二进制文件采用了 AES-XTS 加密,该程序还可对此类文件进行解密,更多信息详见 :ref:`nvs_encryption`。 + +运行程序 +------------------- + +**使用方法**:: + + python nvs_partition_gen.py [-h] {generate,generate-key,encrypt,decrypt} ... + +**可选参数**: + ++------+------------+----------------------+ +| 序号 | 参数 | 描述 | ++------+------------+----------------------+ +| 1 | -h, --help | 显示帮助信息并退出 | ++------+------------+----------------------+ + +**命令**:: + + 运行 nvs_partition_gen.py {command} -h 查看更多帮助信息 + ++------+--------------+---------------+ +| 序号 | 参数 | 描述 | ++------+--------------+---------------+ +| 1 | generate | 生成 NVS 分区 | ++------+--------------+---------------+ +| 2 | generate-key | 生成加密密钥 | ++------+--------------+---------------+ +| 3 | encrypt | 加密 NVS 分区 | ++------+--------------+---------------+ +| 4 | decrypt | 解密 NVS 分区 | ++------+--------------+---------------+ + + +生成 NVS 分区(默认模式) +---------------------------------- + +**使用方法**:: + + python nvs_partition_gen.py generate [-h] [--version {1,2}] [--outdir OUTDIR] + input output size + +**位置参数**: + ++--------+--------------------------------------------------+ +| 参数 | 描述 | ++--------+--------------------------------------------------+ +| input | 待解析的 CSV 文件路径 | ++--------+--------------------------------------------------+ +| output | NVS 二进制文件的输出路径 | ++--------+--------------------------------------------------+ +| size | NVS 分区大小(以字节为单位,且为 4096 的整数倍) | ++--------+--------------------------------------------------+ + +**可选参数**: + ++-----------------+------------------------------------------------+ +| 参数 | 描述 | ++-----------------+------------------------------------------------+ +| -h, --help | 显示帮助信息并退出 | ++-----------------+------------------------------------------------+ +| --version {1,2} | - 设置多页 Blob 版本。 | +| | - 版本 1:禁用多页 Blob; | +| | - 版本 2:启用多页 Blob; | +| | - 默认版本:版本 2。 | ++-----------------+------------------------------------------------+ +| --outdir OUTDIR | 输出目录,用于存储创建的文件。(默认当前目录) | ++-----------------+------------------------------------------------+ + +运行如下命令创建 NVS 分区,该程序同时会提供 CSV 示例文件:: + + python nvs_partition_gen.py generate sample_singlepage_blob.csv sample.bin 0x3000 + +仅生成加密密钥 +----------------------- + +**使用方法**:: + + python nvs_partition_gen.py generate-key [-h] [--keyfile KEYFILE] + [--outdir OUTDIR] + +**可选参数**: + ++-------------------+------------------------------------------------+ +| 参数 | 描述 | ++-------------------+------------------------------------------------+ +| -h, --help | 显示帮助信息并退出 | ++-------------------+------------------------------------------------+ +| --keyfile KEYFILE | 加密密钥文件的输出路径 | ++-------------------+------------------------------------------------+ +| --outdir OUTDIR | 输出目录,用于存储创建的文件。(默认当前目录) | ++-------------------+------------------------------------------------+ + +运行以下命令仅生成加密密钥:: + + python nvs_partition_gen.py generate-key + +生成 NVS 加密分区 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**使用方法**:: + + python nvs_partition_gen.py encrypt [-h] [--version {1,2}] [--keygen] + [--keyfile KEYFILE] [--inputkey INPUTKEY] + [--outdir OUTDIR] + input output size + +**位置参数**: + ++--------+--------------------------------------+ +| 参数 | 描述 | ++--------+--------------------------------------+ +| input | 待解析 CSV 文件的路径 | ++--------+--------------------------------------+ +| output | NVS 二进制文件的输出路径 | ++--------+--------------------------------------+ +| size | NVS 分区大小 | +| | (以字节为单位,且为 4096 的整数倍) | ++--------+--------------------------------------+ + +**可选参数**: + ++---------------------+------------------------------+ +| 参数 | 描述 | ++---------------------+------------------------------+ +| -h, --help | 显示帮助信息并退出 | ++---------------------+------------------------------+ +| --version {1,2} | - 设置多页 Blob 版本。 | +| | - 版本 1:禁用多页 Blob; | +| | - 版本 2:启用多页 Blob; | +| | - 默认版本:版本 2。 | ++---------------------+------------------------------+ +| --keygen | 生成 NVS 分区加密密钥 | ++---------------------+------------------------------+ +| --keyfile KEYFILE | 密钥文件的输出路径 | ++---------------------+------------------------------+ +| --inputkey INPUTKEY | 内含 NVS 分区加密密钥的文件 | ++---------------------+------------------------------+ +| --outdir OUTDIR | 输出目录,用于存储创建的文件 | +| | (默认当前目录) | ++---------------------+------------------------------+ + +运行以下命令加密 NVS 分区,该程序同时会提供一个 CSV 示例文件。 + +- 通过 NVS 分区生成程序生成加密密钥来加密:: + + python nvs_partition_gen.py encrypt sample_singlepage_blob.csv sample_encr.bin 0x3000 --keygen + +.. note:: 创建的加密密钥格式为 ``/keys/keys-.bin``。 + +- 通过 NVS 分区生成程序生成加密密钥,并将密钥存储于自定义的文件中:: + + python nvs_partition_gen.py encrypt sample_singlepage_blob.csv sample_encr.bin 0x3000 --keygen --keyfile sample_keys.bin + +.. note:: 创建的加密密钥格式为 ``/keys/keys-.bin``。 +.. note:: 加密密钥存储于新建文件的 ``keys/`` 目录下,与 NVS 密钥分区结构兼容。更多信息请参考 :ref:`nvs_key_partition`。 + +- 将加密密钥用作二进制输入文件来进行加密:: + + python nvs_partition_gen.py encrypt sample_singlepage_blob.csv sample_encr.bin 0x3000 --inputkey sample_keys.bin + +解密 NVS 分区 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**使用方法**:: + + python nvs_partition_gen.py decrypt [-h] [--outdir OUTDIR] input key output + +**位置参数**: + ++--------+-------------------------------+ +| 参数 | 描述 | ++--------+-------------------------------+ +| input | 待解析的 NVS 加密分区文件路径 | ++--------+-------------------------------+ +| key | 含有解密密钥的文件路径 | ++--------+-------------------------------+ +| output | 已解密的二进制文件输出路径 | ++--------+-------------------------------+ + +**可选参数**: + ++-----------------+------------------------------+ +| 参数 | 描述 | ++-----------------+------------------------------+ +| -h, --help | 显示帮助信息并退出 | ++-----------------+------------------------------+ +| --outdir OUTDIR | 输出目录,用于存储创建的文件 | +| | (默认当前目录) | ++-----------------+------------------------------+ + +运行以下命令解密已加密的 NVS 分区:: + + python nvs_partition_gen.py decrypt sample_encr.bin sample_keys.bin sample_decr.bin + +您可以自定义格式版本号: + +- 版本 1:禁用多页 Blob +- 版本 2:启用多页 Blob + +版本 1:禁用多页 Blob +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +如需禁用多页 Blob,请按照如下命令将版本参数设置为 1,以此格式运行分区生成程序。该程序同时会提供一个 CSV 示例文件:: + + python nvs_partition_gen.py generate sample_singlepage_blob.csv sample.bin 0x3000 --version 1 + +版本 2:启用多页 Blob +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +如需启用多页 Blob,请按照如下命令将版本参数设置为 2,以此格式运行分区生成程序。该程序同时会提供一个 CSV 示例文件:: + + python nvs_partition_gen.py generate sample_multipage_blob.csv sample.bin 0x4000 --version 2 + +.. note:: NVS 分区最小为 0x3000 字节。 + +.. note:: 将二进制文件烧录至设备时,请确保与应用的 sdkconfig 设置一致。 + + +说明 +------- + +- 分区生成程序不会对重复键进行检查,而将数据同时写入这两个重复键中。请注意不要使用同名的键; +- 新页面创建后,前一页的空白处不会再写入数据。CSV 文件中的字段须按次序排列以优化内存; +- 暂不支持 64 位数据类型。 diff --git a/components/wear_levelling/README.rst b/components/wear_levelling/README.rst index 44def742e5..4623bf12b4 100644 --- a/components/wear_levelling/README.rst +++ b/components/wear_levelling/README.rst @@ -1,6 +1,8 @@ Wear Levelling API ================== +:link_to_translation:`zh_CN:[中文]` + Overview -------- Most of flash memory and especially SPI flash that is used in ESP32 has a sector-based organization and also has a limited number of erase/modification cycles per memory sector. The wear levelling component helps to distribute wear and tear among sectors more evenly without requiring any attention from the user. diff --git a/components/wear_levelling/README_CN.rst b/components/wear_levelling/README_CN.rst new file mode 100644 index 0000000000..13d5abdd0c --- /dev/null +++ b/components/wear_levelling/README_CN.rst @@ -0,0 +1,47 @@ +磨损均衡 API +================== + +:link_to_translation:`en:[English]` + +概述 +-------- + +ESP32 所使用的 flash,特别是 SPI flash 多数具备扇区结构,且每个扇区仅允许有限次数的擦除/修改操作。为了避免过度使用某一扇区,乐鑫提供了磨损均衡组件,无需用户介入即可帮助用户均衡各个扇区之间的磨损。 + +磨损均衡组件包含了通过分区组件对外部 SPI flash 进行数据读取、写入、擦除和存储器映射相关的 API 函数。磨损均衡组件还具有软件上更高级别的 API 函数,与 :doc:`FAT 文件系统 ` 协同工作。 + +磨损均衡组件与 FAT 文件系统组件共用 FAT 文件系统的扇区,扇区大小为 4096 字节,是标准 flash 扇区的大小。在这种模式下,磨损均衡组件性能达到最佳,但需要在 RAM 中占用更多内存。 + +为了节省内存,磨损均衡组件还提供了另外两种模式,均使用 512 字节大小的扇区: + +- **性能模式**:先将数据保存在 RAM 中,擦除扇区,然后将数据存储回 flash。如果设备在扇区擦写过程中突然断电,则整个扇区(4096 字节)数据将全部丢失。 +- **安全模式**:数据先保存在 flash 中空余扇区,擦除扇区后,数据即存储回去。如果设备断电,上电后可立即恢复数据。 + +设备默认设置如下: + +- 定义扇区大小为 512 字节 +- 默认使用性能模式 + +您可以使用配置菜单更改设置。 + +磨损均衡组件不会将数据缓存在 RAM 中。写入和擦除函数直接修改 flash,函数返回后,flash 即完成修改。 + +磨损均衡访问 API +----------------------------------- + +处理 flash 数据常用的 API 如下所示: + +- ``wl_mount`` - 为指定分区挂载并初始化磨损均衡模块 +- ``wl_unmount`` - 卸载分区并释放磨损均衡模块 +- ``wl_erase_range`` - 擦除 flash 中指定的地址范围 +- ``wl_write`` - 将数据写入分区 +- ``wl_read`` - 从分区读取数据 +- ``wl_size`` - 返回可用内存的大小(以字节为单位) +- ``wl_sector_size`` - 返回一个扇区的大小 + +请尽量避免直接使用原始磨损均衡函数,建议您使用文件系统特定的函数。 + +内存大小 +----------- + +内存大小是根据分区参数在磨损均衡模块中计算所得,由于模块使用 flash 部分扇区存储内部数据,因此计算所得内存大小有少许偏差。 diff --git a/docs/zh_CN/api-reference/storage/mass_mfg.rst b/docs/zh_CN/api-reference/storage/mass_mfg.rst index d640f48f48..26e61c402b 100644 --- a/docs/zh_CN/api-reference/storage/mass_mfg.rst +++ b/docs/zh_CN/api-reference/storage/mass_mfg.rst @@ -1 +1 @@ -.. include:: /../../tools/mass_mfg/docs/README.rst +.. include:: /../../tools/mass_mfg/docs/README_CN.rst diff --git a/docs/zh_CN/api-reference/storage/nvs_partition_gen.rst b/docs/zh_CN/api-reference/storage/nvs_partition_gen.rst index c94a708c86..ff70e2bec2 100644 --- a/docs/zh_CN/api-reference/storage/nvs_partition_gen.rst +++ b/docs/zh_CN/api-reference/storage/nvs_partition_gen.rst @@ -1 +1 @@ -.. include:: /../../components/nvs_flash/nvs_partition_generator/README.rst +.. include:: /../../components/nvs_flash/nvs_partition_generator/README_CN.rst diff --git a/docs/zh_CN/api-reference/storage/wear-levelling.rst b/docs/zh_CN/api-reference/storage/wear-levelling.rst index 4c50431d80..45b1e3b16c 100644 --- a/docs/zh_CN/api-reference/storage/wear-levelling.rst +++ b/docs/zh_CN/api-reference/storage/wear-levelling.rst @@ -1,4 +1,4 @@ -.. include:: ../../../../components/wear_levelling/README.rst +.. include:: ../../../../components/wear_levelling/README_CN.rst 另请参阅 ----------- @@ -31,3 +31,4 @@ ----------------------- .. include:: /_build/inc/wear_levelling.inc + diff --git a/tools/mass_mfg/docs/README.rst b/tools/mass_mfg/docs/README.rst index e38b1f1ece..ae1cc649e3 100644 --- a/tools/mass_mfg/docs/README.rst +++ b/tools/mass_mfg/docs/README.rst @@ -1,6 +1,8 @@ Manufacturing Utility ===================== +:link_to_translation:`zh_CN:[中文]` + Introduction ------------ diff --git a/tools/mass_mfg/docs/README_CN.rst b/tools/mass_mfg/docs/README_CN.rst new file mode 100644 index 0000000000..929978e82b --- /dev/null +++ b/tools/mass_mfg/docs/README_CN.rst @@ -0,0 +1,256 @@ +量产程序 +===================== + +:link_to_translation:`en:[English]` + +介绍 +------------ + +这一程序主要用于量产时为每一设备创建工厂 NVS(非易失性存储器)分区映像。NVS 分区映像由 CSV(逗号分隔值)文件生成,文件中包含了用户提供的配置项及配置值。 + +注意,该程序仅创建用于量产的二进制映像,您需要使用以下工具将映像烧录到设备上: + +- esptool.py +- Flash 下载工具(仅适用于 Windows) +- 直接烧录程序 + + +准备工作 +------------- + +**该程序需要用到分区公用程序。** + +* 操作系统要求: + - Linux、MacOS 或 Windows(标准版) + +* 安装依赖包: + - Python:最低版本要求为 2.7。下载地址:https://www.python.org/downloads/。 + +.. note:: + + 使用该程序之前,请确保: + - Python 路径已添加到 PATH 环境变量中; + - 已经安装 `requirement.txt` 中的软件包,`requirement.txt` 在 esp-idf 根目录下。 + + +具体流程 +----------- + +.. blockdiag:: + + blockdiag { + A [label = "CSV 配置文件"]; + B [label = "主 CSV 文件"]; + C [label = "二进制 bin 文件", stacked]; + + A -- B -> C + } + + +CSV 配置文件 +---------------------- + +CSV 配置文件中包含设备待烧录的配置信息,定义了待烧录的配置项。例如定义 ``firmware_key`` (``key``) 的 ``type`` 为 ``data``,``encoding`` 为 ``hex2bin``。 + +配置文件中数据格式如下(`REPEAT` 标签可选):: + + name1,namespace, <-- 第一行为 "namespace" 条目 + key1,type1,encoding1 + key2,type2,encoding2,REPEAT + name2,namespace, + key3,type3,encoding3 + key4,type4,encoding4 + +.. note:: 文件第一行应始终为 ``namespace`` 条目。 + +每行应包含三个参数:``key``、``type`` 和 ``encoding``,并以逗号分隔。如果有 ``REPEAT`` 标签,则主 CSV 文件中所有设备此键值均相同。 + +*有关各个参数的详细说明,请参阅 NVS 分区生成程序的 README 文件。* + +CSV 配置文件示例如下:: + + app,namespace, + firmware_key,data,hex2bin + serial_no,data,string,REPEAT <-- "serial_no" 被标记为 "REPEAT" + device_no,data,i32 + +.. note:: + + 请确保: + - 逗号 ',' 前后无空格; + - CSV 文件每行末尾无空格。 + + +主 CSV 文件 +--------------------- + +主 CSV 文件中包含设备待烧录的详细信息,文件中每行均对应一个设备实体。主 CSV 文件中的 ``key`` 应首先在 CSV 配置文件中定义。 + +主 CSV 文件的数据格式如下:: + + key1,key2,key3,..... + value1,value2,value3,.... <-- 对应一个设备实体 + value4,value5,value6,.... <-- 对应一个设备实体 + value7,value8,value9,.... <-- 对应一个设备实体 + +.. note:: 文件中键 (``key``) 名应始终置于文件首行。从配置文件中获取的键,在此文件中的排列顺序应与其在配置文件中的排列顺序相同。主 CSV 文件同时可以包含其它列(键),这些列将被视为元数据,而不会编译进最终二进制文件。 + +每行应包含相应键的键值 (``value``) ,并用逗号隔开。如果某键带有 ``REPEAT`` 标签,则仅需在第二行(即第一个条目)输入对应的值,后面其他行为空。 + +参数描述如下: + +``value`` + Data value + +``value`` 是与键对应的键值。 + +主 CSV 文件示例如下:: + + id,firmware_key,serial_no,device_no + 1,1a2b3c4d5e6faabb,A1,101 <-- 对应一个设备实体(在 CSV 配置文件中标记为 `REPEAT` 的键,除第一个条目外,其他均为空) + 2,1a2b3c4d5e6fccdd,,102 <-- 对应一个设备实体 + 3,1a2b3c4d5e6feeff,,103 <-- 对应一个设备实体 + +.. note:: 如果出现 `REPEAT` 标签,则会在相同目录下生成一个新的主 CSV 文件用作主输入文件,并在每行为带有 `REPEAT` 标签的键插入键值。 + +量产程序还会创建中间 CSV 文件,NVS 分区程序将使用此 CSV 文件作为输入,然后生成二进制文件。 + +中间 CSV 文件的格式如下:: + + key,type,encoding,value + key,namespace, , + key1,type1,encoding1,value1 + key2,type2,encoding2,value2 + +此步骤将为每一设备生成一个中间 CSV 文件。 + +运行量产程序 +------------------- + +**使用方法**:: + + python mfg_gen.py [-h] {generate,generate-key} ... + +**可选参数**: + ++------+------------+----------------------+ +| 序号 | 参数 | 描述 | ++------+------------+----------------------+ +| 1 | -h, --help | 显示帮助信息并退出 | ++------+------------+----------------------+ + + +**命令**: + +运行 mfg_gen.py {command} -h 查看更多帮助信息 + ++------+--------------+---------------+ +| 序号 | 参数 | 描述 | ++------+--------------+---------------+ +| 1 | generate | 生成 NVS 分区 | ++------+--------------+---------------+ +| 2 | generate-key | 生成加密密钥 | ++------+--------------+---------------+ + +**为每个设备生成工厂映像(默认)** + +**使用方法**:: + + python mfg_gen.py generate [-h] [--fileid FILEID] [--version {1,2}] [--keygen] + [--keyfile KEYFILE] [--inputkey INPUTKEY] + [--outdir OUTDIR] + conf values prefix size + + +**位置参数**: + ++--------+--------------------------------------------------+ +| 参数 | 描述 | ++--------+--------------------------------------------------+ +| conf | 待解析的 CSV 配置文件路径 | ++--------+--------------------------------------------------+ +| values | 待解析的主 CSV 文件路径 | ++--------+--------------------------------------------------+ +| prefix | 每个输出文件名前缀的唯一名称 | ++--------+--------------------------------------------------+ +| size | NVS 分区大小(以字节为单位,且为 4096 的整数倍) | ++--------+--------------------------------------------------+ + + +**可选参数**: + ++---------------------+--------------------------------------------------------------------------------+ +| 参数 | 描述 | ++---------------------+--------------------------------------------------------------------------------+ +| -h, --help | 显示帮助信息并退出 | ++---------------------+--------------------------------------------------------------------------------+ +| --fileid FILEID | 每个文件名后缀的唯一文件标识符(主 CSV 文件中的任意键),默认为数值 1、2、3... | ++---------------------+--------------------------------------------------------------------------------+ +| --version {1,2} | - 设置多页 Blob 版本。 | +| | - 版本 1 - 禁用多页 Blob; | +| | - 版本 2 - 启用多页 Blob; | +| | - 默认版本:版本 2 | ++---------------------+--------------------------------------------------------------------------------+ +| --keygen | 生成 NVS 分区加密密钥 | ++---------------------+--------------------------------------------------------------------------------+ +| --inputkey INPUTKEY | 内含 NVS 分区加密密钥的文件 | ++---------------------+--------------------------------------------------------------------------------+ +| --outdir OUTDIR | 输出目录,用于存储创建的文件(默认当前目录) | ++---------------------+--------------------------------------------------------------------------------+ + +请运行以下命令为每个设备生成工厂映像,量产程序同时提供了一个 CSV 示例文件:: + + python mfg_gen.py generate samples/sample_config.csmples/sample_values_singlepage_blob.csv Sample 0x3000 + +主 CSV 文件应在 ``file`` 类型下设置一个相对路径,指向运行该程序的当前目录。 + +**为每个设备生成工厂加密映像** + +运行以下命令为每一设备生成工厂加密映像,量产程序同时提供了一个 CSV 示例文件。 + +- 通过量产程序生成加密密钥来进行加密:: + + python mfg_gen.py generate samples/sample_config.csv samples/sample_values_singlepage_blob.csv Sample 0x3000 --keygen + +.. note:: 创建的加密密钥格式为 ``/keys/keys--.bin``。 +.. note:: 加密密钥存储于新建文件的 ``keys/`` 目录下,与 NVS 密钥分区结构兼容。更多信息请参考 :ref:`nvs_key_partition`。 + +- 提供加密密钥用作二进制输入文件来进行加密:: + + python mfg_gen.py generate samples/sample_config.csv samples/sample_values_singlepage_blob.csv Sample 0x3000 --inputkey keys/sample_keys.bin + +**仅生成加密密钥** + +**使用方法**:: + + python mfg_gen.py generate-key [-h] [--keyfile KEYFILE] [--outdir OUTDIR] + +**可选参数:** + ++-------------------+----------------------------------------------+ +| 参数 | 描述 | ++-------------------+----------------------------------------------+ +| -h, --help | 显示帮助信息并退出 | ++-------------------+----------------------------------------------+ +| --keyfile KEYFILE | 加密密钥文件的输出路径 | ++-------------------+----------------------------------------------+ +| --outdir OUTDIR | 输出目录,用于存储创建的文件(默认当前目录) | ++-------------------+----------------------------------------------+ + +运行以下命令仅生成加密密钥:: + + python mfg_gen.py generate-key + +.. note:: 创建的加密密钥格式为 ``/keys/keys-.bin``。时间戳格式为:``%m-%d_%H-%M``。 +.. note:: 如需自定义目标文件名,请使用 --keyfile 参数。 + +生成的加密密钥二进制文件还可以用于为每个设备的工厂映像加密。 + +``fileid`` 参数的默认值为 1、2、3...,与主 CSV 文件中的行一一对应,内含设备配置值。 + +运行量产程序时,将在指定的 ``outdir`` 目录下创建以下文件夹: + +- ``bin/`` 存储生成的二进制文件 +- ``csv/`` 存储生成的中间 CSV 文件 +- ``keys/`` 存储加密密钥(创建工厂加密映像时会用到) + From c648054e088b00eaff85341d415de1e4c4cd5ab2 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 11:17:25 +0800 Subject: [PATCH 092/146] ble_mesh: fix provisioner prov auth bug --- .../esp_ble_mesh/mesh_core/provisioner_prov.c | 18 +++--------------- .../esp_ble_mesh/mesh_core/provisioner_prov.h | 4 +--- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c index dbaae8ebb6..e07be193ad 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -575,16 +575,6 @@ static int provisioner_check_unprov_dev_info(const u8_t uuid[16]) return -EALREADY; } - /* Check if this device is currently being provisioned. - * According to Zephyr's device code, if we connect with - * one device and start to provision it, we may still can - * receive the connectable prov adv pkt from this device. - * Here we check both PB-GATT and PB-ADV link status. - */ - if (is_unprov_dev_being_provision(uuid)) { - return -EALREADY; - } - /* Check if the device has already been provisioned */ for (i = 0U; i < ARRAY_SIZE(prov_nodes); i++) { if (prov_nodes[i].provisioned) { @@ -1757,7 +1747,7 @@ static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) bt_mesh_rand(str, size); /* Normalize to '0' .. '9' & 'A' .. 'Z' */ - for (j = 0; j < size; j++) { + for (j = 0U; j < size; j++) { str[j] %= 36; if (str[j] < 10) { str[j] += '0'; @@ -1768,7 +1758,7 @@ static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) str[size] = '\0'; memcpy(link[idx].auth, str, size); - memset(link[idx].auth + size, 0, sizeof(link[idx].auth) - size); + memset(link[idx].auth + size, 0, PROV_AUTH_VAL_SIZE - size); return prov->prov_output_num(AUTH_METHOD_INPUT, input, str, size, idx); } else { @@ -1892,8 +1882,7 @@ int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_fl return 0; } -#if 0 -int bt_mesh_prov_set_oob_output_data(const u8_t idx, u8_t *num, u8_t size, bool num_flag) +int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag) { /** This function should be called in the prov_output_num * callback, after the data has been output by provisioner. @@ -1923,7 +1912,6 @@ int bt_mesh_prov_set_oob_output_data(const u8_t idx, u8_t *num, u8_t size, bool return 0; } -#endif int bt_mesh_prov_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]) { diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h index 103a223f21..13360598ef 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h @@ -313,9 +313,7 @@ int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_fl * * @return Zero - success, otherwise - fail */ -#if 0 -int bt_mesh_prov_set_oob_output_data(const u8_t idx, u8_t *num, u8_t size, bool num_flag); -#endif +int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag); /** * @brief This function is called to read unprovisioned device's oob public key. From a788e7cd3db4172fce495d2e418adea79f797213 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 11:20:15 +0800 Subject: [PATCH 093/146] ble_mesh: sync zephyr v1.14.0 bt_hex() --- components/bt/esp_ble_mesh/mesh_core/mesh_util.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/mesh_util.c b/components/bt/esp_ble_mesh/mesh_core/mesh_util.c index 4650bb2603..232662baaf 100644 --- a/components/bt/esp_ble_mesh/mesh_core/mesh_util.c +++ b/components/bt/esp_ble_mesh/mesh_core/mesh_util.c @@ -17,19 +17,11 @@ const char *bt_hex(const void *buf, size_t len) { static const char hex[] = "0123456789abcdef"; - static char hexbufs[4][129]; - static u8_t curbuf; + static char str[129]; const u8_t *b = buf; - unsigned int mask; - char *str; int i; - mask = bt_mesh_irq_lock(); - str = hexbufs[curbuf++]; - curbuf %= ARRAY_SIZE(hexbufs); - bt_mesh_irq_unlock(mask); - - len = MIN(len, (sizeof(hexbufs[0]) - 1) / 2); + len = MIN(len, (sizeof(str) - 1) / 2); for (i = 0; i < len; i++) { str[i * 2] = hex[b[i] >> 4]; From 5f6259a625df43837b54c0ac0355c4265d4a8351 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 11:41:39 +0800 Subject: [PATCH 094/146] ble_mesh: reorganize ble mesh model files --- components/bt/CMakeLists.txt | 15 +++++++------- components/bt/component.mk | 18 +++++++++-------- .../bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 2 +- .../esp_ble_mesh/mesh_core/include/cfg_cli.h | 2 +- .../mesh_core/include/health_cli.h | 2 +- .../bt/esp_ble_mesh/mesh_core/transport.c | 2 +- .../client_common.c} | 20 +------------------ .../mesh_models/{ => client}/generic_client.c | 0 .../include/client_common.h} | 10 ---------- .../{ => client}/include/generic_client.h | 2 +- .../{ => client}/include/lighting_client.h | 2 +- .../{ => client}/include/sensor_client.h | 2 +- .../{ => client}/include/time_scene_client.h | 2 +- .../{ => client}/lighting_client.c | 0 .../mesh_models/{ => client}/sensor_client.c | 0 .../{ => client}/time_scene_client.c | 0 .../{ => common}/include/mesh_common.h | 15 ++++++++++++++ .../{ => common}/include/model_opcode.h | 0 .../mesh_models/{ => common}/mesh_common.c | 20 +++++++++++++++++++ 19 files changed, 62 insertions(+), 52 deletions(-) rename components/bt/esp_ble_mesh/mesh_models/{model_common.c => client/client_common.c} (95%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/generic_client.c (100%) rename components/bt/esp_ble_mesh/mesh_models/{include/model_common.h => client/include/client_common.h} (93%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/include/generic_client.h (99%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/include/lighting_client.h (99%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/include/sensor_client.h (99%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/include/time_scene_client.h (99%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/lighting_client.c (100%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/sensor_client.c (100%) rename components/bt/esp_ble_mesh/mesh_models/{ => client}/time_scene_client.c (100%) rename components/bt/esp_ble_mesh/mesh_models/{ => common}/include/mesh_common.h (71%) rename components/bt/esp_ble_mesh/mesh_models/{ => common}/include/model_opcode.h (100%) rename components/bt/esp_ble_mesh/mesh_models/{ => common}/mesh_common.c (72%) diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 095493c1dd..2b8a96363d 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -297,7 +297,8 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_core/include" "esp_ble_mesh/mesh_core/settings" "esp_ble_mesh/btc/include" - "esp_ble_mesh/mesh_models/include" + "esp_ble_mesh/mesh_models/common/include" + "esp_ble_mesh/mesh_models/client/include" "esp_ble_mesh/api/core/include" "esp_ble_mesh/api/models/include" "esp_ble_mesh/api") @@ -350,12 +351,12 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_core/settings.c" "esp_ble_mesh/mesh_core/test.c" "esp_ble_mesh/mesh_core/transport.c" - "esp_ble_mesh/mesh_models/generic_client.c" - "esp_ble_mesh/mesh_models/lighting_client.c" - "esp_ble_mesh/mesh_models/mesh_common.c" - "esp_ble_mesh/mesh_models/model_common.c" - "esp_ble_mesh/mesh_models/sensor_client.c" - "esp_ble_mesh/mesh_models/time_scene_client.c") + "esp_ble_mesh/mesh_models/common/mesh_common.c" + "esp_ble_mesh/mesh_models/client/client_common.c" + "esp_ble_mesh/mesh_models/client/generic_client.c" + "esp_ble_mesh/mesh_models/client/lighting_client.c" + "esp_ble_mesh/mesh_models/client/sensor_client.c" + "esp_ble_mesh/mesh_models/client/time_scene_client.c") endif() if(CONFIG_BT_NIMBLE_ENABLED) diff --git a/components/bt/component.mk b/components/bt/component.mk index 2e7148d22e..7fa74dcf03 100644 --- a/components/bt/component.mk +++ b/components/bt/component.mk @@ -132,19 +132,21 @@ COMPONENT_SRCDIRS += common/osi \ endif ifdef CONFIG_BLE_MESH - COMPONENT_ADD_INCLUDEDIRS += esp_ble_mesh/mesh_core \ - esp_ble_mesh/mesh_core/include \ - esp_ble_mesh/mesh_core/settings \ - esp_ble_mesh/btc/include \ - esp_ble_mesh/mesh_models/include \ - esp_ble_mesh/api/core/include \ - esp_ble_mesh/api/models/include \ + COMPONENT_ADD_INCLUDEDIRS += esp_ble_mesh/mesh_core \ + esp_ble_mesh/mesh_core/include \ + esp_ble_mesh/mesh_core/settings \ + esp_ble_mesh/btc/include \ + esp_ble_mesh/mesh_models/common/include \ + esp_ble_mesh/mesh_models/client/include \ + esp_ble_mesh/api/core/include \ + esp_ble_mesh/api/models/include \ esp_ble_mesh/api COMPONENT_SRCDIRS += esp_ble_mesh/mesh_core \ esp_ble_mesh/mesh_core/settings \ esp_ble_mesh/btc \ - esp_ble_mesh/mesh_models \ + esp_ble_mesh/mesh_models/common \ + esp_ble_mesh/mesh_models/client \ esp_ble_mesh/api/core \ esp_ble_mesh/api/models endif diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index c825e93907..e1517d6bb3 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -44,7 +44,7 @@ #include "lighting_client.h" #include "sensor_client.h" #include "time_scene_client.h" -#include "model_common.h" +#include "client_common.h" #include "btc_ble_mesh_prov.h" #include "btc_ble_mesh_config_model.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h index b001d84b8a..8f46a045ad 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h @@ -13,7 +13,7 @@ #include "mesh_access.h" #include "mesh_kernel.h" -#include "model_common.h" +#include "client_common.h" /** * @brief Bluetooth Mesh diff --git a/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h index 9d7230ebac..b1e1b693d0 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h @@ -13,7 +13,7 @@ #include "mesh_access.h" #include "mesh_kernel.h" -#include "model_common.h" +#include "client_common.h" /** * @brief Bluetooth Mesh diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index a456ebbfc8..0902783515 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -31,7 +31,7 @@ #include "settings.h" #include "transport.h" #include "mesh_common.h" -#include "model_common.h" +#include "client_common.h" #include "provisioner_main.h" /* The transport layer needs at least three buffers for itself to avoid diff --git a/components/bt/esp_ble_mesh/mesh_models/model_common.c b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c similarity index 95% rename from components/bt/esp_ble_mesh/mesh_models/model_common.c rename to components/bt/esp_ble_mesh/mesh_models/client/client_common.c index cc694082cd..8802e424cc 100644 --- a/components/bt/esp_ble_mesh/mesh_models/model_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c @@ -23,7 +23,7 @@ #include "mesh_main.h" #include "mesh.h" -#include "model_common.h" +#include "client_common.h" bt_mesh_client_node_t *bt_mesh_is_model_message_publish(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, @@ -316,21 +316,3 @@ int bt_mesh_set_model_role(bt_mesh_role_param_t *common) return 0; } -u8_t bt_mesh_get_model_role(struct bt_mesh_model *model, bool srv_send) -{ - bt_mesh_client_common_t *client = NULL; - - if (srv_send) { - BT_DBG("%s, Message is sent by a server model", __func__); - return NODE; - } - - if (!model || !model->user_data) { - BT_ERR("%s, Invalid parameter", __func__); - return ROLE_NVAL; - } - - client = (bt_mesh_client_common_t *)model->user_data; - - return client->msg_role; -} diff --git a/components/bt/esp_ble_mesh/mesh_models/generic_client.c b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c similarity index 100% rename from components/bt/esp_ble_mesh/mesh_models/generic_client.c rename to components/bt/esp_ble_mesh/mesh_models/client/generic_client.c diff --git a/components/bt/esp_ble_mesh/mesh_models/include/model_common.h b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h similarity index 93% rename from components/bt/esp_ble_mesh/mesh_models/include/model_common.h rename to components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h index 486cdb769d..55d78831d3 100644 --- a/components/bt/esp_ble_mesh/mesh_models/include/model_common.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h @@ -119,15 +119,5 @@ typedef struct bt_mesh_role_param { */ int bt_mesh_set_model_role(bt_mesh_role_param_t *common); -/** - * @brief This function gets msg role for stack internal use. - * - * @param[in] model: Pointer to the model structure - * @param[in] srv_send: Indicate if the message is sent by a server model - * - * @return 0 - Node, 1 - Provisioner - */ -u8_t bt_mesh_get_model_role(struct bt_mesh_model *model, bool srv_send); - #endif /* _MODEL_COMMON_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/include/generic_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h similarity index 99% rename from components/bt/esp_ble_mesh/mesh_models/include/generic_client.h rename to components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h index e82587d766..b3cee9c756 100644 --- a/components/bt/esp_ble_mesh/mesh_models/include/generic_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h @@ -22,7 +22,7 @@ #include "mesh_access.h" #include "mesh_kernel.h" -#include "model_common.h" +#include "client_common.h" /* Generic client model common structure */ typedef bt_mesh_client_common_t bt_mesh_generic_client_t; diff --git a/components/bt/esp_ble_mesh/mesh_models/include/lighting_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h similarity index 99% rename from components/bt/esp_ble_mesh/mesh_models/include/lighting_client.h rename to components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h index 9e9789a821..bd2b735a6e 100644 --- a/components/bt/esp_ble_mesh/mesh_models/include/lighting_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h @@ -22,7 +22,7 @@ #include "mesh_access.h" #include "mesh_kernel.h" -#include "model_common.h" +#include "client_common.h" /* Light client model common structure */ typedef bt_mesh_client_common_t bt_mesh_light_client_t; diff --git a/components/bt/esp_ble_mesh/mesh_models/include/sensor_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h similarity index 99% rename from components/bt/esp_ble_mesh/mesh_models/include/sensor_client.h rename to components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h index 095499ef5d..14b450513e 100644 --- a/components/bt/esp_ble_mesh/mesh_models/include/sensor_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h @@ -22,7 +22,7 @@ #include "mesh_access.h" #include "mesh_kernel.h" -#include "model_common.h" +#include "client_common.h" /* Sensor Client Model Context */ extern const struct bt_mesh_model_op sensor_cli_op[]; diff --git a/components/bt/esp_ble_mesh/mesh_models/include/time_scene_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h similarity index 99% rename from components/bt/esp_ble_mesh/mesh_models/include/time_scene_client.h rename to components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h index a37cf878f4..b6c8d18bf0 100644 --- a/components/bt/esp_ble_mesh/mesh_models/include/time_scene_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h @@ -22,7 +22,7 @@ #include "mesh_access.h" #include "mesh_kernel.h" -#include "model_common.h" +#include "client_common.h" /* Time scene client model common structure */ typedef bt_mesh_client_common_t bt_mesh_time_scene_client_t; diff --git a/components/bt/esp_ble_mesh/mesh_models/lighting_client.c b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c similarity index 100% rename from components/bt/esp_ble_mesh/mesh_models/lighting_client.c rename to components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c diff --git a/components/bt/esp_ble_mesh/mesh_models/sensor_client.c b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c similarity index 100% rename from components/bt/esp_ble_mesh/mesh_models/sensor_client.c rename to components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c diff --git a/components/bt/esp_ble_mesh/mesh_models/time_scene_client.c b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c similarity index 100% rename from components/bt/esp_ble_mesh/mesh_models/time_scene_client.c rename to components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c diff --git a/components/bt/esp_ble_mesh/mesh_models/include/mesh_common.h b/components/bt/esp_ble_mesh/mesh_models/common/include/mesh_common.h similarity index 71% rename from components/bt/esp_ble_mesh/mesh_models/include/mesh_common.h rename to components/bt/esp_ble_mesh/mesh_models/common/include/mesh_common.h index 468cf65afc..501450c272 100644 --- a/components/bt/esp_ble_mesh/mesh_models/include/mesh_common.h +++ b/components/bt/esp_ble_mesh/mesh_models/common/include/mesh_common.h @@ -24,6 +24,7 @@ #include "mesh_types.h" #include "mesh_buf.h" #include "mesh_trace.h" +#include "mesh_access.h" /** * @brief This function allocates memory to store outgoing message. @@ -43,4 +44,18 @@ struct net_buf_simple *bt_mesh_alloc_buf(u16_t size); */ void bt_mesh_free_buf(struct net_buf_simple *buf); +/** + * @brief This function gets device role for stack internal use. + * + * @Note Currently Provisioner only support client models, Node supports + * client models and server models. Hence if srv_send is set to be + * TRUE, then role NODE will be returned. + * + * @param[in] model: Pointer to the model structure + * @param[in] srv_send: Indicate if the message is sent by a server model + * + * @return 0 - Node, 1 - Provisioner + */ +u8_t bt_mesh_get_model_role(struct bt_mesh_model *model, bool srv_send); + #endif /* _MESH_COMMON_H_ */ \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/include/model_opcode.h b/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h similarity index 100% rename from components/bt/esp_ble_mesh/mesh_models/include/model_opcode.h rename to components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h diff --git a/components/bt/esp_ble_mesh/mesh_models/mesh_common.c b/components/bt/esp_ble_mesh/mesh_models/common/mesh_common.c similarity index 72% rename from components/bt/esp_ble_mesh/mesh_models/mesh_common.c rename to components/bt/esp_ble_mesh/mesh_models/common/mesh_common.c index bb4aa6f934..9e7185d451 100644 --- a/components/bt/esp_ble_mesh/mesh_models/mesh_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/common/mesh_common.c @@ -15,6 +15,7 @@ #include #include +#include "client_common.h" #include "mesh_common.h" struct net_buf_simple *bt_mesh_alloc_buf(u16_t size) @@ -44,3 +45,22 @@ void bt_mesh_free_buf(struct net_buf_simple *buf) osi_free(buf); } } + +u8_t bt_mesh_get_model_role(struct bt_mesh_model *model, bool srv_send) +{ + bt_mesh_client_common_t *client = NULL; + + if (srv_send) { + BT_DBG("%s, Message is sent by a server model", __func__); + return NODE; + } + + if (!model || !model->user_data) { + BT_ERR("%s, Invalid parameter", __func__); + return ROLE_NVAL; + } + + client = (bt_mesh_client_common_t *)model->user_data; + + return client->msg_role; +} From b3bc60183da7d219ea383981ba8acc67d86b299f Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 12:06:20 +0800 Subject: [PATCH 095/146] ble_mesh: rename ble mesh client model variables and functions --- .../btc/btc_ble_mesh_config_model.c | 4 +- .../btc/btc_ble_mesh_generic_model.c | 6 +- .../btc/btc_ble_mesh_health_model.c | 4 +- .../btc/btc_ble_mesh_lighting_model.c | 6 +- .../bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 54 +++++++------- .../btc/btc_ble_mesh_sensor_model.c | 6 +- .../btc/btc_ble_mesh_time_scene_model.c | 6 +- components/bt/esp_ble_mesh/mesh_core/access.c | 4 +- .../bt/esp_ble_mesh/mesh_core/cfg_cli.c | 2 +- .../bt/esp_ble_mesh/mesh_core/health_cli.c | 4 +- .../esp_ble_mesh/mesh_core/include/cfg_cli.h | 4 +- .../mesh_core/include/health_cli.h | 6 +- .../bt/esp_ble_mesh/mesh_core/transport.c | 2 +- .../mesh_models/client/client_common.c | 35 +++++---- .../mesh_models/client/generic_client.c | 10 +-- .../client/include/client_common.h | 73 +++++++++++-------- .../client/include/generic_client.h | 24 +++--- .../client/include/lighting_client.h | 18 ++--- .../client/include/sensor_client.h | 8 +- .../client/include/time_scene_client.h | 14 ++-- .../mesh_models/client/lighting_client.c | 10 +-- .../mesh_models/client/sensor_client.c | 8 +- .../mesh_models/client/time_scene_client.c | 10 +-- .../mesh_models/common/include/mesh_common.h | 2 +- .../mesh_models/common/mesh_common.c | 6 +- 25 files changed, 168 insertions(+), 158 deletions(-) diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c index 0937721bb2..30257a5c6a 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c @@ -389,7 +389,7 @@ void btc_mesh_cfg_client_call_handler(btc_msg_t *msg) cfg_client_cb.params = arg->cfg_client_get_state.params; role_param.model = (struct bt_mesh_model *)cfg_client_cb.params->model; role_param.role = cfg_client_cb.params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } @@ -405,7 +405,7 @@ void btc_mesh_cfg_client_call_handler(btc_msg_t *msg) cfg_client_cb.params = arg->cfg_client_set_state.params; role_param.model = (struct bt_mesh_model *)cfg_client_cb.params->model; role_param.role = cfg_client_cb.params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c index 761fbeebe5..fc0854a42a 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c @@ -443,7 +443,7 @@ void btc_mesh_generic_client_call_handler(btc_msg_t *msg) esp_ble_mesh_generic_client_cb_param_t generic_client_cb = {0}; esp_ble_mesh_client_common_param_t *params = NULL; btc_ble_mesh_generic_client_args_t *arg = NULL; - struct bt_mesh_common_param common = {0}; + bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { @@ -458,7 +458,7 @@ void btc_mesh_generic_client_call_handler(btc_msg_t *msg) params = arg->generic_client_get_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } @@ -487,7 +487,7 @@ void btc_mesh_generic_client_call_handler(btc_msg_t *msg) params = arg->generic_client_set_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c index 1f6d88340b..f5a176af88 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c @@ -489,7 +489,7 @@ void btc_mesh_health_client_call_handler(btc_msg_t *msg) health_client_cb.params = arg->health_client_get_state.params; role_param.model = (struct bt_mesh_model *)health_client_cb.params->model; role_param.role = health_client_cb.params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } @@ -506,7 +506,7 @@ void btc_mesh_health_client_call_handler(btc_msg_t *msg) health_client_cb.params = arg->health_client_set_state.params; role_param.model = (struct bt_mesh_model *)health_client_cb.params->model; role_param.role = health_client_cb.params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c index d07dba47a0..9a1b57edf3 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c @@ -283,7 +283,7 @@ void btc_mesh_light_client_call_handler(btc_msg_t *msg) esp_ble_mesh_light_client_cb_param_t light_client_cb = {0}; esp_ble_mesh_client_common_param_t *params = NULL; btc_ble_mesh_light_client_args_t *arg = NULL; - struct bt_mesh_common_param common = {0}; + bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { @@ -298,7 +298,7 @@ void btc_mesh_light_client_call_handler(btc_msg_t *msg) params = arg->light_client_get_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } @@ -327,7 +327,7 @@ void btc_mesh_light_client_call_handler(btc_msg_t *msg) params = arg->light_client_set_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index e1517d6bb3..4d11458dc3 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -299,8 +299,8 @@ static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, struct net_buf_simple *buf) { esp_ble_mesh_model_cb_param_t mesh_param = {0}; - bt_mesh_client_common_t *client_param = NULL; - bt_mesh_internal_data_t *data = NULL; + bt_mesh_client_user_data_t *client_param = NULL; + bt_mesh_client_internal_data_t *data = NULL; bt_mesh_client_node_t *node = NULL; btc_msg_t msg = {0}; bt_status_t ret; @@ -310,14 +310,14 @@ static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, return; } - client_param = (bt_mesh_client_common_t *)model->user_data; - data = (bt_mesh_internal_data_t *)client_param->internal_data; + client_param = (bt_mesh_client_user_data_t *)model->user_data; + data = (bt_mesh_client_internal_data_t *)client_param->internal_data; if (!data) { LOG_ERROR("%s, Client internal_data is NULL", __func__); return; } - node = bt_mesh_is_model_message_publish(model, ctx, buf, false); + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, false); if (node == NULL) { msg.act = ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT; mesh_param.client_recv_publish_msg.opcode = mesh_opcode; @@ -594,8 +594,8 @@ static void btc_prov_register_complete_cb(int err_code) static void btc_client_model_timeout_cb(struct k_work *work) { esp_ble_mesh_model_cb_param_t mesh_param = {0}; - bt_mesh_client_common_t *client_param = NULL; - bt_mesh_internal_data_t *data = NULL; + bt_mesh_client_user_data_t *client_param = NULL; + bt_mesh_client_internal_data_t *data = NULL; bt_mesh_client_node_t *node = NULL; btc_msg_t msg = {0}; bt_status_t ret; @@ -606,8 +606,8 @@ static void btc_client_model_timeout_cb(struct k_work *work) return; } - client_param = (bt_mesh_client_common_t *)node->ctx.model->user_data; - data = (bt_mesh_internal_data_t *)client_param->internal_data; + client_param = (bt_mesh_client_user_data_t *)node->ctx.model->user_data; + data = (bt_mesh_client_internal_data_t *)client_param->internal_data; if (!data) { LOG_ERROR("%s, Client internal_data is NULL", __func__); return; @@ -986,7 +986,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_onoff_cli_op); - bt_mesh_gen_onoff_cli_t *cli = (bt_mesh_gen_onoff_cli_t *)model->user_data; + bt_mesh_gen_onoff_client_t *cli = (bt_mesh_gen_onoff_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -994,7 +994,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_LEVEL_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_level_cli_op); - bt_mesh_gen_level_cli_t *cli = (bt_mesh_gen_level_cli_t *)model->user_data; + bt_mesh_gen_level_client_t *cli = (bt_mesh_gen_level_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -1002,7 +1002,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_def_trans_time_cli_op); - bt_mesh_gen_def_trans_time_cli_t *cli = (bt_mesh_gen_def_trans_time_cli_t *)model->user_data; + bt_mesh_gen_def_trans_time_client_t *cli = (bt_mesh_gen_def_trans_time_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -1010,7 +1010,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_power_onoff_cli_op); - bt_mesh_gen_power_onoff_cli_t *cli = (bt_mesh_gen_power_onoff_cli_t *)model->user_data; + bt_mesh_gen_power_onoff_client_t *cli = (bt_mesh_gen_power_onoff_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -1018,7 +1018,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_power_level_cli_op); - bt_mesh_gen_power_level_cli_t *cli = (bt_mesh_gen_power_level_cli_t *)model->user_data; + bt_mesh_gen_power_level_client_t *cli = (bt_mesh_gen_power_level_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -1026,7 +1026,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_BATTERY_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_battery_cli_op); - bt_mesh_gen_battery_cli_t *cli = (bt_mesh_gen_battery_cli_t *)model->user_data; + bt_mesh_gen_battery_client_t *cli = (bt_mesh_gen_battery_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -1034,7 +1034,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_LOCATION_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_location_cli_op); - bt_mesh_gen_location_cli_t *cli = (bt_mesh_gen_location_cli_t *)model->user_data; + bt_mesh_gen_location_client_t *cli = (bt_mesh_gen_location_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -1042,7 +1042,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_GEN_PROP_CLI: { model->op = ((esp_ble_mesh_model_op_t *)gen_property_cli_op); - bt_mesh_gen_property_cli_t *cli = (bt_mesh_gen_property_cli_t *)model->user_data; + bt_mesh_gen_property_client_t *cli = (bt_mesh_gen_property_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_generic_client_publish_callback; } @@ -1050,7 +1050,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI: { model->op = ((esp_ble_mesh_model_op_t *)light_lightness_cli_op); - bt_mesh_light_lightness_cli_t *cli = (bt_mesh_light_lightness_cli_t *)model->user_data; + bt_mesh_light_lightness_client_t *cli = (bt_mesh_light_lightness_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_light_client_publish_callback; } @@ -1058,7 +1058,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_LIGHT_CTL_CLI: { model->op = ((esp_ble_mesh_model_op_t *)light_ctl_cli_op); - bt_mesh_light_ctl_cli_t *cli = (bt_mesh_light_ctl_cli_t *)model->user_data; + bt_mesh_light_ctl_client_t *cli = (bt_mesh_light_ctl_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_light_client_publish_callback; } @@ -1066,7 +1066,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_LIGHT_HSL_CLI: { model->op = ((esp_ble_mesh_model_op_t *)light_hsl_cli_op); - bt_mesh_light_hsl_cli_t *cli = (bt_mesh_light_hsl_cli_t *)model->user_data; + bt_mesh_light_hsl_client_t *cli = (bt_mesh_light_hsl_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_light_client_publish_callback; } @@ -1074,7 +1074,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_LIGHT_XYL_CLI: { model->op = ((esp_ble_mesh_model_op_t *)light_xyl_cli_op); - bt_mesh_light_xyl_cli_t *cli = (bt_mesh_light_xyl_cli_t *)model->user_data; + bt_mesh_light_xyl_client_t *cli = (bt_mesh_light_xyl_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_light_client_publish_callback; } @@ -1082,7 +1082,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_LIGHT_LC_CLI: { model->op = ((esp_ble_mesh_model_op_t *)light_lc_cli_op); - bt_mesh_light_lc_cli_t *cli = (bt_mesh_light_lc_cli_t *)model->user_data; + bt_mesh_light_lc_client_t *cli = (bt_mesh_light_lc_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_light_client_publish_callback; } @@ -1098,7 +1098,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_TIME_CLI: { model->op = ((esp_ble_mesh_model_op_t *)time_cli_op); - bt_mesh_time_scene_client_t *cli = (bt_mesh_time_scene_client_t *)model->user_data; + bt_mesh_time_client_t *cli = (bt_mesh_time_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_time_scene_client_publish_callback; } @@ -1106,7 +1106,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_SCENE_CLI: { model->op = ((esp_ble_mesh_model_op_t *)scene_cli_op); - bt_mesh_time_scene_client_t *cli = (bt_mesh_time_scene_client_t *)model->user_data; + bt_mesh_scene_client_t *cli = (bt_mesh_scene_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_time_scene_client_publish_callback; } @@ -1114,7 +1114,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) } case BLE_MESH_MODEL_ID_SCHEDULER_CLI: { model->op = ((esp_ble_mesh_model_op_t *)scheduler_cli_op); - bt_mesh_time_scene_client_t *cli = (bt_mesh_time_scene_client_t *)model->user_data; + bt_mesh_scheduler_client_t *cli = (bt_mesh_scheduler_client_t *)model->user_data; if (cli != NULL) { cli->publish_status = btc_mesh_time_scene_client_publish_callback; } @@ -1429,7 +1429,7 @@ void btc_mesh_model_call_handler(btc_msg_t *msg) bt_mesh_role_param_t common = {0}; common.model = (struct bt_mesh_model *)(arg->model_publish.model); common.role = arg->model_publish.device_role; - if (bt_mesh_set_model_role(&common)) { + if (bt_mesh_set_client_model_role(&common)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } @@ -1467,7 +1467,7 @@ void btc_mesh_model_call_handler(btc_msg_t *msg) arg->model_send.ctx->srv_send = false; common.model = (struct bt_mesh_model *)(arg->model_send.model); common.role = arg->model_send.device_role; - if (bt_mesh_set_model_role(&common)) { + if (bt_mesh_set_client_model_role(&common)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c index b9f3de6bde..df267023a7 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c @@ -534,7 +534,7 @@ void btc_mesh_sensor_client_call_handler(btc_msg_t *msg) esp_ble_mesh_sensor_client_cb_param_t sensor_client_cb = {0}; esp_ble_mesh_client_common_param_t *params = NULL; btc_ble_mesh_sensor_client_args_t *arg = NULL; - struct bt_mesh_common_param common = {0}; + bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { @@ -549,7 +549,7 @@ void btc_mesh_sensor_client_call_handler(btc_msg_t *msg) params = arg->sensor_client_get_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } @@ -578,7 +578,7 @@ void btc_mesh_sensor_client_call_handler(btc_msg_t *msg) params = arg->sensor_client_set_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c index 4f03567dc9..907c1430ff 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c @@ -285,7 +285,7 @@ void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg) esp_ble_mesh_time_scene_client_cb_param_t time_scene_client_cb = {0}; btc_ble_mesh_time_scene_client_args_t *arg = NULL; esp_ble_mesh_client_common_param_t *params = NULL; - struct bt_mesh_common_param common = {0}; + bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { @@ -300,7 +300,7 @@ void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg) params = arg->time_scene_client_get_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } @@ -329,7 +329,7 @@ void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg) params = arg->time_scene_client_set_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; - if (bt_mesh_set_model_role(&role_param)) { + if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); return; } diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index 6184d30091..d8365ecdd5 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -740,7 +740,7 @@ static int model_send(struct bt_mesh_model *model, tx->ctx->app_idx, tx->ctx->addr); BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - role = bt_mesh_get_model_role(model, tx->ctx->srv_send); + role = bt_mesh_get_device_role(model, tx->ctx->srv_send); if (role == ROLE_NVAL) { BT_ERR("%s, Failed to get model role", __func__); return -EINVAL; @@ -819,7 +819,7 @@ int bt_mesh_model_send(struct bt_mesh_model *model, struct bt_mesh_subnet *sub = NULL; u8_t role; - role = bt_mesh_get_model_role(model, ctx->srv_send); + role = bt_mesh_get_device_role(model, ctx->srv_send); if (role == ROLE_NVAL) { BT_ERR("%s, Failed to get model role", __func__); return -EINVAL; diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c index aa2daf0da8..7e2f717b43 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c @@ -139,7 +139,7 @@ static void cfg_client_cancel(struct bt_mesh_model *model, /* If it is a publish message, sent to the user directly. */ buf.data = (u8_t *)status; buf.len = (u16_t)len; - node = bt_mesh_is_model_message_publish(model, ctx, &buf, true); + node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true); if (!node) { BT_DBG("Unexpected config status message 0x%x", ctx->recv_op); } else { diff --git a/components/bt/esp_ble_mesh/mesh_core/health_cli.c b/components/bt/esp_ble_mesh/mesh_core/health_cli.c index c66e42afcd..05e47cd295 100644 --- a/components/bt/esp_ble_mesh/mesh_core/health_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/health_cli.c @@ -95,7 +95,7 @@ static void health_client_cancel(struct bt_mesh_model *model, /* If it is a publish message, sent to the user directly. */ buf.data = (u8_t *)status; buf.len = (u16_t)len; - node = bt_mesh_is_model_message_publish(model, ctx, &buf, true); + node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true); if (!node) { BT_DBG("Unexpected health status message 0x%x", ctx->recv_op); } else { @@ -169,7 +169,7 @@ static void health_current_status(struct bt_mesh_model *model, bt_hex(buf->data, buf->len)); /* Health current status is a publish message, sent to the user directly. */ - if (!(node = bt_mesh_is_model_message_publish(model, ctx, buf, true))) { + if (!(node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true))) { return; } diff --git a/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h index 8f46a045ad..5bf8b2032f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h @@ -23,8 +23,8 @@ */ /* Config client model common structure */ -typedef bt_mesh_client_common_t bt_mesh_config_client_t; -typedef bt_mesh_internal_data_t config_internal_data_t; +typedef bt_mesh_client_user_data_t bt_mesh_config_client_t; +typedef bt_mesh_client_internal_data_t config_internal_data_t; extern const struct bt_mesh_model_op bt_mesh_cfg_cli_op[]; diff --git a/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h index b1e1b693d0..4f080766cd 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h @@ -23,10 +23,8 @@ */ /* Health client model common structure */ -typedef bt_mesh_client_common_t bt_mesh_health_client_t; -typedef bt_mesh_internal_data_t health_internal_data_t; - -typedef bt_mesh_internal_data_t health_client_internal_data_t; +typedef bt_mesh_client_user_data_t bt_mesh_health_client_t; +typedef bt_mesh_client_internal_data_t health_internal_data_t; extern const struct bt_mesh_model_op bt_mesh_health_cli_op[]; diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 0902783515..98f93cb04e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -474,7 +474,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, tx->ctx->app_idx, tx->ctx->addr); BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - role = bt_mesh_get_model_role(tx->ctx->model, tx->ctx->srv_send); + role = bt_mesh_get_device_role(tx->ctx->model, tx->ctx->srv_send); if (role == ROLE_NVAL) { BT_ERR("%s, Failed to get model role", __func__); return -EINVAL; diff --git a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c index 8802e424cc..c2590935bc 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c @@ -25,13 +25,13 @@ #include "mesh.h" #include "client_common.h" -bt_mesh_client_node_t *bt_mesh_is_model_message_publish(struct bt_mesh_model *model, +bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg( + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf, - bool need_pub) + struct net_buf_simple *buf, bool need_pub) { - bt_mesh_internal_data_t *data = NULL; - bt_mesh_client_common_t *cli = NULL; + bt_mesh_client_internal_data_t *data = NULL; + bt_mesh_client_user_data_t *cli = NULL; bt_mesh_client_node_t *node = NULL; u32_t rsp; @@ -40,7 +40,7 @@ bt_mesh_client_node_t *bt_mesh_is_model_message_publish(struct bt_mesh_model *mo return NULL; } - cli = (bt_mesh_client_common_t *)model->user_data; + cli = (bt_mesh_client_user_data_t *)model->user_data; if (!cli) { BT_ERR("%s, Clinet user_data is NULL", __func__); return NULL; @@ -65,7 +65,7 @@ bt_mesh_client_node_t *bt_mesh_is_model_message_publish(struct bt_mesh_model *mo * message, then the message is from another element and * push it to application layer. */ - data = (bt_mesh_internal_data_t *)cli->internal_data; + data = (bt_mesh_client_internal_data_t *)cli->internal_data; if (!data) { BT_ERR("%s, Client internal_data is NULL", __func__); return NULL; @@ -172,8 +172,8 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, const struct bt_mesh_send_cb *cb, void *cb_data) { - bt_mesh_internal_data_t *internal = NULL; - bt_mesh_client_common_t *cli = NULL; + bt_mesh_client_internal_data_t *internal = NULL; + bt_mesh_client_user_data_t *cli = NULL; bt_mesh_client_node_t *node = NULL; int err; @@ -182,9 +182,9 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, return -EINVAL; } - cli = (bt_mesh_client_common_t *)model->user_data; + cli = (bt_mesh_client_user_data_t *)model->user_data; __ASSERT(cli, "Invalid client value when sent client msg."); - internal = (bt_mesh_internal_data_t *)cli->internal_data; + internal = (bt_mesh_client_internal_data_t *)cli->internal_data; __ASSERT(internal, "Invalid internal value when sent client msg."); if (!need_ack) { @@ -224,8 +224,8 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, int bt_mesh_client_init(struct bt_mesh_model *model) { - bt_mesh_internal_data_t *data = NULL; - bt_mesh_client_common_t *cli = NULL; + bt_mesh_client_internal_data_t *data = NULL; + bt_mesh_client_user_data_t *cli = NULL; if (!model) { BT_ERR("%s, Invalid parameter", __func__); @@ -244,7 +244,7 @@ int bt_mesh_client_init(struct bt_mesh_model *model) } /* TODO: call osi_free() when deinit function is invoked */ - data = osi_calloc(sizeof(bt_mesh_internal_data_t)); + data = osi_calloc(sizeof(bt_mesh_client_internal_data_t)); if (!data) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -275,16 +275,16 @@ int bt_mesh_client_free_node(sys_slist_t *queue, bt_mesh_client_node_t *node) return 0; } -int bt_mesh_set_model_role(bt_mesh_role_param_t *common) +int bt_mesh_set_client_model_role(bt_mesh_role_param_t *common) { - bt_mesh_client_common_t *client = NULL; + bt_mesh_client_user_data_t *client = NULL; if (!common || !common->model || !common->model->user_data) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } - client = (bt_mesh_client_common_t *)common->model->user_data; + client = (bt_mesh_client_user_data_t *)common->model->user_data; switch (common->role) { #if CONFIG_BLE_MESH_NODE @@ -315,4 +315,3 @@ int bt_mesh_set_model_role(bt_mesh_role_param_t *common) return 0; } - diff --git a/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c index 9e963095dd..32860a603d 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c @@ -535,7 +535,7 @@ static void generic_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - node = bt_mesh_is_model_message_publish(model, ctx, buf, true); + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { BT_DBG("Unexpected generic status message 0x%x", rsp); } else { @@ -687,7 +687,7 @@ const struct bt_mesh_model_op gen_property_cli_op[] = { BLE_MESH_MODEL_OP_END, }; -static int gen_get_state(struct bt_mesh_common_param *common, void *value) +static int gen_get_state(bt_mesh_client_common_param_t *common, void *value) { NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_GEN_GET_STATE_MSG_LEN); int err; @@ -736,7 +736,7 @@ static int gen_get_state(struct bt_mesh_common_param *common, void *value) return err; } -static int gen_set_state(struct bt_mesh_common_param *common, +static int gen_set_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; @@ -918,7 +918,7 @@ end: return err; } -int bt_mesh_generic_client_get_state(struct bt_mesh_common_param *common, void *get, void *status) +int bt_mesh_generic_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) { bt_mesh_generic_client_t *client = NULL; @@ -981,7 +981,7 @@ int bt_mesh_generic_client_get_state(struct bt_mesh_common_param *common, void * return gen_get_state(common, get); } -int bt_mesh_generic_client_set_state(struct bt_mesh_common_param *common, void *set, void *status) +int bt_mesh_generic_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_generic_client_t *client = NULL; u16_t length = 0; diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h index 55d78831d3..384d08a05d 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h @@ -17,17 +17,23 @@ #include "mesh_access.h" -/** Mesh Client Model Context */ +/** Client model opcode pair table */ typedef struct { - u32_t cli_op; /* The client opcode */ - u32_t status_op; /* The server status opcode corresponding to the client opcode */ + u32_t cli_op; /* Client message opcode */ + u32_t status_op; /* Corresponding status message opcode */ } bt_mesh_client_op_pair_t; -/** Mesh Client Model Context */ +/** Client model user data context */ typedef struct { + /** Pointer to the client model */ struct bt_mesh_model *model; - int op_pair_size; /* the size of op_pair */ + + /** Size of the opcode pair table */ + int op_pair_size; + + /** Pointer to the opcode pair table */ const bt_mesh_client_op_pair_t *op_pair; + /** * @brief This function is a callback function used to push the received unsolicited * messages to the application layer. @@ -40,26 +46,42 @@ typedef struct { * @return None */ void (*publish_status)(u32_t opcode, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); - void *internal_data; /* Pointer of the structure of internal data */ - u8_t msg_role; /* device role of the tx message */ -} bt_mesh_client_common_t; + /** Pointer to the internal data of client model */ + void *internal_data; + + /** Role of the device to which the client model belongs */ + u8_t msg_role; +} bt_mesh_client_user_data_t; + +/** Client model internal data context */ typedef struct { sys_slist_t queue; -} bt_mesh_internal_data_t; +} bt_mesh_client_internal_data_t; +/** Client model sending message related context */ typedef struct { sys_snode_t client_node; - struct bt_mesh_msg_ctx ctx; - u32_t opcode; /* Indicate the opcode of the message sending */ - u32_t op_pending; /* Indicate the status message waiting for */ - struct k_delayed_work timer; /* Message send Timer. Only for stack-internal use. */ + struct bt_mesh_msg_ctx ctx; /* Message context */ + u32_t opcode; /* Message opcode */ + u32_t op_pending; /* Expected status message opcode */ + struct k_delayed_work timer; /* Time used to get response. Only for internal use. */ } bt_mesh_client_node_t; +/** Client model sending message parameters */ +typedef struct { + u32_t opcode; /* Message opcode */ + struct bt_mesh_model *model; /* Pointer to the client model */ + struct bt_mesh_msg_ctx ctx; /* Message context */ + s32_t msg_timeout; /* Time to get corresponding response */ + const struct bt_mesh_send_cb *cb; /* User defined callback function */ + void *cb_data; /* User defined callback value */ +} bt_mesh_client_common_param_t; + int bt_mesh_client_init(struct bt_mesh_model *model); /** - * @brief Check the msg is a publish msg or not + * @brief Check if the msg received by client model is a publish msg or not * * @param model Mesh (client) Model that the message belongs to. * @param ctx Message context, includes keys, TTL, etc. @@ -67,10 +89,10 @@ int bt_mesh_client_init(struct bt_mesh_model *model); * @param need_pub Indicate if the msg sent to app layer as a publish msg * @return 0 on success, or (negative) error code on failure. */ -bt_mesh_client_node_t *bt_mesh_is_model_message_publish(struct bt_mesh_model *model, +bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg( + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf, - bool need_pub); + struct net_buf_simple *buf, bool need_pub); bool bt_mesh_client_find_opcode_in_list(sys_slist_t *list, u32_t opcode); @@ -94,18 +116,9 @@ enum { FAST_PROV, }; -#define ROLE_NVAL 0xFF +#define ROLE_NVAL 0xFF -struct bt_mesh_common_param { - u32_t opcode; /* Message opcode */ - struct bt_mesh_model *model; /* Pointer to cli structure */ - struct bt_mesh_msg_ctx ctx; /* Message context */ - s32_t msg_timeout; /* Time to get response messages */ - const struct bt_mesh_send_cb *cb; /* User defined callback function */ - void *cb_data; /* Data as parameter of the cb function */ -}; - -typedef struct bt_mesh_role_param { +typedef struct { struct bt_mesh_model *model; /* The client model structure */ u8_t role; /* Role of the device - Node/Provisioner */ } bt_mesh_role_param_t; @@ -113,11 +126,11 @@ typedef struct bt_mesh_role_param { /** * @brief This function copies node_index for stack internal use. * - * @param[in] common: Pointer to the struct bt_mesh_role_param structure + * @param[in] common: Pointer to the bt_mesh_role_param_t structure * * @return Zero - success, otherwise - fail */ -int bt_mesh_set_model_role(bt_mesh_role_param_t *common); +int bt_mesh_set_client_model_role(bt_mesh_role_param_t *common); #endif /* _MODEL_COMMON_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h index b3cee9c756..e697e25716 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h @@ -25,8 +25,8 @@ #include "client_common.h" /* Generic client model common structure */ -typedef bt_mesh_client_common_t bt_mesh_generic_client_t; -typedef bt_mesh_internal_data_t generic_internal_data_t; +typedef bt_mesh_client_user_data_t bt_mesh_generic_client_t; +typedef bt_mesh_client_internal_data_t generic_internal_data_t; /* Generic OnOff Client Model Context */ extern const struct bt_mesh_model_op gen_onoff_cli_op[]; @@ -45,7 +45,7 @@ extern const struct bt_mesh_model_op gen_onoff_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, \ gen_onoff_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_onoff_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_onoff_client_t; struct bt_mesh_gen_onoff_status { bool op_en; /* Indicate whether optional parameters included */ @@ -79,7 +79,7 @@ extern const struct bt_mesh_model_op gen_level_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, \ gen_level_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_level_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_level_client_t; struct bt_mesh_gen_level_status { bool op_en; /* Indicate whether optional parameters included */ @@ -130,7 +130,7 @@ extern const struct bt_mesh_model_op gen_def_trans_time_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, \ gen_def_trans_time_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_def_trans_time_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_def_trans_time_client_t; struct bt_mesh_gen_def_trans_time_set { u8_t trans_time; /* The value of the Generic Default Transition Time state */ @@ -157,7 +157,7 @@ extern const struct bt_mesh_model_op gen_power_onoff_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, \ gen_power_onoff_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_power_onoff_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_power_onoff_client_t; struct bt_mesh_gen_onpowerup_set { u8_t onpowerup; /* The value of the Generic OnPowerUp state */ @@ -184,7 +184,7 @@ extern const struct bt_mesh_model_op gen_power_level_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, \ gen_power_level_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_power_level_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_power_level_client_t; struct bt_mesh_gen_power_level_status { bool op_en; /* Indicate whether optional parameters included */ @@ -241,7 +241,7 @@ extern const struct bt_mesh_model_op gen_battery_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, \ gen_battery_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_battery_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_battery_client_t; struct bt_mesh_gen_battery_status { u32_t battery_level : 8; /* Value of Generic Battery Level state */ @@ -267,7 +267,7 @@ extern const struct bt_mesh_model_op gen_location_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, \ gen_location_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_location_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_location_client_t; struct bt_mesh_gen_loc_global_status { s32_t global_latitude; /* Global Coordinates (Latitude) */ @@ -314,7 +314,7 @@ extern const struct bt_mesh_model_op gen_property_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_PROP_CLI, \ gen_property_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_gen_property_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_gen_property_client_t; struct bt_mesh_gen_user_properties_status { struct net_buf_simple *user_property_ids; /* Buffer contains a sequence of N User Property IDs */ @@ -475,7 +475,7 @@ int bt_mesh_gen_property_cli_init(struct bt_mesh_model *model, bool primary); * * @return Zero-success, other-fail */ -int bt_mesh_generic_client_get_state(struct bt_mesh_common_param *common, void *get, void *status); +int bt_mesh_generic_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); /** * @brief This function is called to set generic states. @@ -486,6 +486,6 @@ int bt_mesh_generic_client_get_state(struct bt_mesh_common_param *common, void * * * @return Zero-success, other-fail */ -int bt_mesh_generic_client_set_state(struct bt_mesh_common_param *common, void *set, void *status); +int bt_mesh_generic_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); #endif /* _GENERIC_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h index bd2b735a6e..8502052155 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h @@ -25,8 +25,8 @@ #include "client_common.h" /* Light client model common structure */ -typedef bt_mesh_client_common_t bt_mesh_light_client_t; -typedef bt_mesh_internal_data_t light_internal_data_t; +typedef bt_mesh_client_user_data_t bt_mesh_light_client_t; +typedef bt_mesh_client_internal_data_t light_internal_data_t; /* Light Lightness Client Model Context */ extern const struct bt_mesh_model_op light_lightness_cli_op[]; @@ -45,7 +45,7 @@ extern const struct bt_mesh_model_op light_lightness_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, \ light_lightness_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_light_lightness_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_light_lightness_client_t; struct bt_mesh_light_lightness_status { bool op_en; /* Indicate whether optional parameters included */ @@ -117,7 +117,7 @@ extern const struct bt_mesh_model_op light_ctl_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, \ light_ctl_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_light_ctl_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_light_ctl_client_t; struct bt_mesh_light_ctl_status { bool op_en; /* Indicate whether optional parameters included */ @@ -196,7 +196,7 @@ extern const struct bt_mesh_model_op light_hsl_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, \ light_hsl_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_light_hsl_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_light_hsl_client_t; struct bt_mesh_light_hsl_status { bool op_en; /* Indicate whether optional parameters included */ @@ -298,7 +298,7 @@ extern const struct bt_mesh_model_op light_xyl_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, \ light_xyl_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_light_xyl_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_light_xyl_client_t; struct bt_mesh_light_xyl_status { bool op_en; /* Indicate whether optional parameters included */ @@ -370,7 +370,7 @@ extern const struct bt_mesh_model_op light_lc_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_LC_CLI, \ light_lc_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_light_lc_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_light_lc_client_t; struct bt_mesh_light_lc_mode_status { u8_t mode; /* The present value of the Light LC Mode state */ @@ -476,7 +476,7 @@ int bt_mesh_light_lc_cli_init(struct bt_mesh_model *model, bool primary); * * @return Zero-success, other-fail */ -int bt_mesh_light_client_get_state(struct bt_mesh_common_param *common, void *get, void *status); +int bt_mesh_light_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); /** * @brief This function is called to set light states. @@ -487,6 +487,6 @@ int bt_mesh_light_client_get_state(struct bt_mesh_common_param *common, void *ge * * @return Zero-success, other-fail */ -int bt_mesh_light_client_set_state(struct bt_mesh_common_param *common, void *set, void *status); +int bt_mesh_light_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); #endif /* _LIGHTING_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h index 14b450513e..39855bd9a7 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h @@ -41,8 +41,8 @@ extern const struct bt_mesh_model_op sensor_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_SENSOR_CLI, \ sensor_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_sensor_client_t; -typedef bt_mesh_internal_data_t sensor_internal_data_t; +typedef bt_mesh_client_user_data_t bt_mesh_sensor_client_t; +typedef bt_mesh_client_internal_data_t sensor_internal_data_t; struct bt_mesh_sensor_descriptor_status { struct net_buf_simple *descriptor; /* Sequence of 8-octet sensor descriptors (optional) */ @@ -151,7 +151,7 @@ int bt_mesh_sensor_cli_init(struct bt_mesh_model *model, bool primary); * * @return Zero-success, other-fail */ -int bt_mesh_sensor_client_get_state(struct bt_mesh_common_param *common, void *get, void *status); +int bt_mesh_sensor_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); /** * @brief This function is called to set sensor states. @@ -162,6 +162,6 @@ int bt_mesh_sensor_client_get_state(struct bt_mesh_common_param *common, void *g * * @return Zero-success, other-fail */ -int bt_mesh_sensor_client_set_state(struct bt_mesh_common_param *common, void *set, void *status); +int bt_mesh_sensor_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); #endif /* _SENSOR_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h index b6c8d18bf0..182316e05d 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h @@ -25,8 +25,8 @@ #include "client_common.h" /* Time scene client model common structure */ -typedef bt_mesh_client_common_t bt_mesh_time_scene_client_t; -typedef bt_mesh_internal_data_t time_scene_internal_data_t; +typedef bt_mesh_client_user_data_t bt_mesh_time_scene_client_t; +typedef bt_mesh_client_internal_data_t time_scene_internal_data_t; /* Time Client Model Context */ extern const struct bt_mesh_model_op time_cli_op[]; @@ -45,7 +45,7 @@ extern const struct bt_mesh_model_op time_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_TIME_CLI, \ time_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_time_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_time_client_t; struct bt_mesh_time_status { u8_t tai_seconds[5]; /* The current TAI time in seconds */ @@ -115,7 +115,7 @@ extern const struct bt_mesh_model_op scene_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_SCENE_CLI, \ scene_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_scene_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_scene_client_t; struct bt_mesh_scene_status { bool op_en; /* Indicate whether optional parameters included */ @@ -164,7 +164,7 @@ extern const struct bt_mesh_model_op scheduler_cli_op[]; BLE_MESH_MODEL(BLE_MESH_MODEL_ID_SCHEDULER_CLI, \ scheduler_cli_op, cli_pub, cli_data) -typedef bt_mesh_client_common_t bt_mesh_scheduler_cli_t; +typedef bt_mesh_client_user_data_t bt_mesh_scheduler_client_t; struct bt_mesh_scheduler_status { u16_t schedules; /* Bit field indicating defined Actions in the Schedule Register */ @@ -241,7 +241,7 @@ int bt_mesh_scheduler_cli_init(struct bt_mesh_model *model, bool primary); * * @return Zero-success, other-fail */ -int bt_mesh_time_scene_client_get_state(struct bt_mesh_common_param *common, void *get, void *status); +int bt_mesh_time_scene_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); /** * @brief This function is called to set scene states. @@ -252,6 +252,6 @@ int bt_mesh_time_scene_client_get_state(struct bt_mesh_common_param *common, voi * * @return Zero-success, other-fail */ -int bt_mesh_time_scene_client_set_state(struct bt_mesh_common_param *common, void *set, void *status); +int bt_mesh_time_scene_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); #endif /* _TIME_SCENE_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c index 8f0481b86d..d71f3ccce5 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c @@ -650,7 +650,7 @@ static void light_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - node = bt_mesh_is_model_message_publish(model, ctx, buf, true); + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { BT_DBG("Unexpected light status message 0x%x", rsp); } else { @@ -770,7 +770,7 @@ const struct bt_mesh_model_op light_lc_cli_op[] = { BLE_MESH_MODEL_OP_END, }; -static int light_get_state(struct bt_mesh_common_param *common, void *value) +static int light_get_state(bt_mesh_client_common_param_t *common, void *value) { NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_LIGHT_GET_STATE_MSG_LEN); int err; @@ -801,7 +801,7 @@ static int light_get_state(struct bt_mesh_common_param *common, void *value) return err; } -static int light_set_state(struct bt_mesh_common_param *common, +static int light_set_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; @@ -1042,7 +1042,7 @@ end: return err; } -int bt_mesh_light_client_get_state(struct bt_mesh_common_param *common, void *get, void *status) +int bt_mesh_light_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) { bt_mesh_light_client_t *client = NULL; @@ -1095,7 +1095,7 @@ int bt_mesh_light_client_get_state(struct bt_mesh_common_param *common, void *ge return light_get_state(common, get); } -int bt_mesh_light_client_set_state(struct bt_mesh_common_param *common, void *set, void *status) +int bt_mesh_light_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_light_client_t *client = NULL; u16_t length = 0; diff --git a/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c index 26ea1f8d7e..2187f18551 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c @@ -262,7 +262,7 @@ static void sensor_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - node = bt_mesh_is_model_message_publish(model, ctx, buf, true); + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { BT_DBG("Unexpected sensor status message 0x%x", rsp); } else { @@ -352,7 +352,7 @@ const struct bt_mesh_model_op sensor_cli_op[] = { BLE_MESH_MODEL_OP_END, }; -static int sensor_act_state(struct bt_mesh_common_param *common, +static int sensor_act_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; @@ -460,7 +460,7 @@ end: return err; } -int bt_mesh_sensor_client_get_state(struct bt_mesh_common_param *common, void *get, void *status) +int bt_mesh_sensor_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) { bt_mesh_sensor_client_t *client = NULL; u16_t length = 0; @@ -525,7 +525,7 @@ int bt_mesh_sensor_client_get_state(struct bt_mesh_common_param *common, void *g return sensor_act_state(common, get, length, true); } -int bt_mesh_sensor_client_set_state(struct bt_mesh_common_param *common, void *set, void *status) +int bt_mesh_sensor_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_sensor_client_t *client = NULL; u16_t length = 0; diff --git a/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c index 921c504ae3..3632c653b0 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c @@ -299,7 +299,7 @@ static void time_scene_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - node = bt_mesh_is_model_message_publish(model, ctx, buf, true); + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { BT_DBG("Unexpected time scene status message 0x%x", rsp); } else { @@ -369,7 +369,7 @@ const struct bt_mesh_model_op scheduler_cli_op[] = { BLE_MESH_MODEL_OP_END, }; -static int time_scene_get_state(struct bt_mesh_common_param *common, void *value) +static int time_scene_get_state(bt_mesh_client_common_param_t *common, void *value) { NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_SCENE_GET_STATE_MSG_LEN); int err; @@ -400,7 +400,7 @@ static int time_scene_get_state(struct bt_mesh_common_param *common, void *value return err; } -static int time_scene_set_state(struct bt_mesh_common_param *common, +static int time_scene_set_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; @@ -497,7 +497,7 @@ end: return err; } -int bt_mesh_time_scene_client_get_state(struct bt_mesh_common_param *common, void *get, void *status) +int bt_mesh_time_scene_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) { bt_mesh_time_scene_client_t *client = NULL; @@ -535,7 +535,7 @@ int bt_mesh_time_scene_client_get_state(struct bt_mesh_common_param *common, voi return time_scene_get_state(common, get); } -int bt_mesh_time_scene_client_set_state(struct bt_mesh_common_param *common, void *set, void *status) +int bt_mesh_time_scene_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_time_scene_client_t *client = NULL; u16_t length = 0; diff --git a/components/bt/esp_ble_mesh/mesh_models/common/include/mesh_common.h b/components/bt/esp_ble_mesh/mesh_models/common/include/mesh_common.h index 501450c272..2355bf0f3d 100644 --- a/components/bt/esp_ble_mesh/mesh_models/common/include/mesh_common.h +++ b/components/bt/esp_ble_mesh/mesh_models/common/include/mesh_common.h @@ -56,6 +56,6 @@ void bt_mesh_free_buf(struct net_buf_simple *buf); * * @return 0 - Node, 1 - Provisioner */ -u8_t bt_mesh_get_model_role(struct bt_mesh_model *model, bool srv_send); +u8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send); #endif /* _MESH_COMMON_H_ */ \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/common/mesh_common.c b/components/bt/esp_ble_mesh/mesh_models/common/mesh_common.c index 9e7185d451..704e540a20 100644 --- a/components/bt/esp_ble_mesh/mesh_models/common/mesh_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/common/mesh_common.c @@ -46,9 +46,9 @@ void bt_mesh_free_buf(struct net_buf_simple *buf) } } -u8_t bt_mesh_get_model_role(struct bt_mesh_model *model, bool srv_send) +u8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send) { - bt_mesh_client_common_t *client = NULL; + bt_mesh_client_user_data_t *client = NULL; if (srv_send) { BT_DBG("%s, Message is sent by a server model", __func__); @@ -60,7 +60,7 @@ u8_t bt_mesh_get_model_role(struct bt_mesh_model *model, bool srv_send) return ROLE_NVAL; } - client = (bt_mesh_client_common_t *)model->user_data; + client = (bt_mesh_client_user_data_t *)model->user_data; return client->msg_role; } From 014a35c1df238250c5918cf6a37647d5f39abbc4 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 12:36:37 +0800 Subject: [PATCH 096/146] ble_mesh: fix MESH/NODE/CFG/HBS/BV-01-C related bug --- .../bt/esp_ble_mesh/mesh_core/cfg_srv.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c index 4e330e61a0..6154ee36c7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -3121,15 +3121,10 @@ static void hb_sub_send_status(struct bt_mesh_model *model, net_buf_simple_add_u8(&msg, status); net_buf_simple_add_le16(&msg, cfg->hb_sub.src); net_buf_simple_add_le16(&msg, cfg->hb_sub.dst); - if (cfg->hb_sub.src == BLE_MESH_ADDR_UNASSIGNED || - cfg->hb_sub.dst == BLE_MESH_ADDR_UNASSIGNED) { - memset(net_buf_simple_add(&msg, 4), 0, 4); - } else { - net_buf_simple_add_u8(&msg, hb_log(period)); - net_buf_simple_add_u8(&msg, hb_log(cfg->hb_sub.count)); - net_buf_simple_add_u8(&msg, cfg->hb_sub.min_hops); - net_buf_simple_add_u8(&msg, cfg->hb_sub.max_hops); - } + net_buf_simple_add_u8(&msg, hb_log(period)); + net_buf_simple_add_u8(&msg, hb_log(cfg->hb_sub.count)); + net_buf_simple_add_u8(&msg, cfg->hb_sub.min_hops); + net_buf_simple_add_u8(&msg, cfg->hb_sub.max_hops); if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { BT_ERR("%s, Unable to send Config Heartbeat Subscription Status", __func__); @@ -3188,9 +3183,13 @@ static void heartbeat_sub_set(struct bt_mesh_model *model, * trigger clearing of the values according to * MESH/NODE/CFG/HBS/BV-02-C. */ - if (cfg->hb_sub.src != sub_src || cfg->hb_sub.dst != sub_dst) { + if (sub_src == BLE_MESH_ADDR_UNASSIGNED || + sub_dst == BLE_MESH_ADDR_UNASSIGNED) { cfg->hb_sub.src = BLE_MESH_ADDR_UNASSIGNED; cfg->hb_sub.dst = BLE_MESH_ADDR_UNASSIGNED; + cfg->hb_sub.min_hops = BLE_MESH_TTL_MAX; + cfg->hb_sub.max_hops = 0U; + cfg->hb_sub.count = 0U; } period_ms = 0; From f25640e9e5e5f49e48dba069dc989a8b92509a8c Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 12:38:49 +0800 Subject: [PATCH 097/146] ble_mesh: fix MESH/NODE/CFG/GPXY/BV-02-C & MESH/NODE/CFG/NID/BV-02-C related bug --- components/bt/esp_ble_mesh/mesh_core/proxy.c | 21 +++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy.c b/components/bt/esp_ble_mesh/mesh_core/proxy.c index fd9807f54b..4945906fd5 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy.c +++ b/components/bt/esp_ble_mesh/mesh_core/proxy.c @@ -1144,14 +1144,25 @@ static bool advertise_subnet(struct bt_mesh_subnet *sub) static struct bt_mesh_subnet *next_sub(void) { + struct bt_mesh_subnet *sub = NULL; int i; - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub; - - sub = &bt_mesh.sub[(i + next_idx) % ARRAY_SIZE(bt_mesh.sub)]; + for (i = next_idx; i < ARRAY_SIZE(bt_mesh.sub); i++) { + sub = &bt_mesh.sub[i]; if (advertise_subnet(sub)) { - next_idx = (next_idx + 1) % ARRAY_SIZE(bt_mesh.sub); + next_idx = (i + 1) % ARRAY_SIZE(bt_mesh.sub); + return sub; + } + } + + /** + * If among [next_idx, ARRAY_SIZE(bt_mesh.sub)], there is no subnet + * to advertise, then try to start advertising from Primary subnet. + */ + for (i = 0; i < next_idx; i++) { + sub = &bt_mesh.sub[i]; + if (advertise_subnet(sub)) { + next_idx = (i + 1) % ARRAY_SIZE(bt_mesh.sub); return sub; } } From 1f96bf5906e1bcd761828ed9eadd7de06ca65d1c Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 12:40:31 +0800 Subject: [PATCH 098/146] ble_mesh: fix MESH/NODE/FRND/FN/BV-11-C related bug --- components/bt/esp_ble_mesh/mesh_core/friend.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.c b/components/bt/esp_ble_mesh/mesh_core/friend.c index b47e86fe81..de8ca4cc2c 100644 --- a/components/bt/esp_ble_mesh/mesh_core/friend.c +++ b/components/bt/esp_ble_mesh/mesh_core/friend.c @@ -688,7 +688,7 @@ static void clear_procedure_start(struct bt_mesh_friend *frnd) { BT_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd); - frnd->clear.start = k_uptime_get_32() + (2 * frnd->poll_to); + frnd->clear.start = k_uptime_get_32(); frnd->clear.repeat_sec = 1U; send_friend_clear(frnd); From 624bf7e3a0ca9567d47304811266d5736ef5eb19 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 13:01:06 +0800 Subject: [PATCH 099/146] ble_mesh: ble mesh btc miscellaneous modifications --- components/bt/common/btc/core/btc_task.c | 20 +- .../bt/common/btc/include/btc/btc_task.h | 6 +- .../api/core/esp_ble_mesh_networking_api.c | 2 +- .../bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 4 +- .../models/esp_ble_mesh_config_model_api.c | 16 +- .../models/esp_ble_mesh_lighting_model_api.c | 18 +- .../include/esp_ble_mesh_generic_model_api.h | 1 - .../include/esp_ble_mesh_lighting_model_api.h | 321 +++++----- .../include/esp_ble_mesh_sensor_model_api.h | 49 +- .../esp_ble_mesh_time_scene_model_api.h | 131 ++-- .../btc/btc_ble_mesh_config_model.c | 562 ++++++++-------- .../btc/btc_ble_mesh_generic_model.c | 188 +++--- .../btc/btc_ble_mesh_health_model.c | 604 +++++++++--------- .../btc/btc_ble_mesh_lighting_model.c | 178 +++--- .../bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 259 ++++---- .../btc/btc_ble_mesh_sensor_model.c | 222 ++++--- .../btc/btc_ble_mesh_time_scene_model.c | 156 +++-- .../btc/include/btc_ble_mesh_config_model.h | 45 +- .../btc/include/btc_ble_mesh_generic_model.h | 21 +- .../btc/include/btc_ble_mesh_health_model.h | 60 +- .../btc/include/btc_ble_mesh_lighting_model.h | 31 +- .../btc/include/btc_ble_mesh_prov.h | 15 +- .../btc/include/btc_ble_mesh_sensor_model.h | 21 +- .../include/btc_ble_mesh_time_scene_model.h | 21 +- .../bt/esp_ble_mesh/mesh_core/cfg_cli.c | 12 +- .../bt/esp_ble_mesh/mesh_core/cfg_srv.c | 4 +- .../bt/esp_ble_mesh/mesh_core/health_cli.c | 12 +- .../mesh_core/include/mesh_access.h | 4 +- .../bt/esp_ble_mesh/mesh_core/settings.c | 14 +- .../mesh_core/settings/settings_nvs.h | 6 +- .../mesh_models/client/generic_client.c | 14 +- .../client/include/generic_client.h | 10 +- .../mesh_models/client/lighting_client.c | 10 +- .../mesh_models/client/sensor_client.c | 10 +- .../mesh_models/client/time_scene_client.c | 10 +- 35 files changed, 1565 insertions(+), 1492 deletions(-) diff --git a/components/bt/common/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c index 4ac156555b..cdcfe74700 100644 --- a/components/bt/common/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -114,16 +114,16 @@ static const btc_func_t profile_tab[BTC_PID_NUM] = { #endif /* #if CLASSIC_BT_INCLUDED */ #endif #if CONFIG_BLE_MESH - [BTC_PID_PROV] = {btc_mesh_prov_call_handler, btc_mesh_prov_cb_handler}, - [BTC_PID_MODEL] = {btc_mesh_model_call_handler, btc_mesh_model_cb_handler}, - [BTC_PID_HEALTH_CLIENT] = {btc_mesh_health_client_call_handler, btc_mesh_health_client_cb_handler}, - [BTC_PID_HEALTH_SERVER] = {btc_mesh_health_server_call_handler, btc_mesh_health_server_cb_handler}, - [BTC_PID_CFG_CLIENT] = {btc_mesh_cfg_client_call_handler, btc_mesh_cfg_client_cb_handler}, - [BTC_PID_CFG_SERVER] = {NULL , btc_mesh_cfg_server_cb_handler}, - [BTC_PID_GENERIC_CLIENT] = {btc_mesh_generic_client_call_handler, btc_mesh_generic_client_cb_handler}, - [BTC_PID_LIGHT_CLIENT] = {btc_mesh_light_client_call_handler, btc_mesh_light_client_cb_handler}, - [BTC_PID_SENSOR_CLIENT] = {btc_mesh_sensor_client_call_handler, btc_mesh_sensor_client_cb_handler}, - [BTC_PID_TIME_SCENE_CLIENT] = {btc_mesh_time_scene_client_call_handler, btc_mesh_time_scene_client_cb_handler}, + [BTC_PID_PROV] = {btc_ble_mesh_prov_call_handler, btc_ble_mesh_prov_cb_handler }, + [BTC_PID_MODEL] = {btc_ble_mesh_model_call_handler, btc_ble_mesh_model_cb_handler }, + [BTC_PID_HEALTH_CLIENT] = {btc_ble_mesh_health_client_call_handler, btc_ble_mesh_health_client_cb_handler }, + [BTC_PID_HEALTH_SERVER] = {btc_ble_mesh_health_server_call_handler, btc_ble_mesh_health_server_cb_handler }, + [BTC_PID_CONFIG_CLIENT] = {btc_ble_mesh_config_client_call_handler, btc_ble_mesh_config_client_cb_handler }, + [BTC_PID_CONFIG_SERVER] = {NULL, btc_ble_mesh_config_server_cb_handler }, + [BTC_PID_GENERIC_CLIENT] = {btc_ble_mesh_generic_client_call_handler, btc_ble_mesh_generic_client_cb_handler }, + [BTC_PID_LIGHTING_CLIENT] = {btc_ble_mesh_lighting_client_call_handler, btc_ble_mesh_lighting_client_cb_handler }, + [BTC_PID_SENSOR_CLIENT] = {btc_ble_mesh_sensor_client_call_handler, btc_ble_mesh_sensor_client_cb_handler }, + [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler}, #endif /* #if CONFIG_BLE_MESH */ }; diff --git a/components/bt/common/btc/include/btc/btc_task.h b/components/bt/common/btc/include/btc/btc_task.h index c94de7126a..45512d7293 100644 --- a/components/bt/common/btc/include/btc/btc_task.h +++ b/components/bt/common/btc/include/btc/btc_task.h @@ -72,10 +72,10 @@ typedef enum { BTC_PID_MODEL, BTC_PID_HEALTH_CLIENT, BTC_PID_HEALTH_SERVER, - BTC_PID_CFG_CLIENT, - BTC_PID_CFG_SERVER, + BTC_PID_CONFIG_CLIENT, + BTC_PID_CONFIG_SERVER, BTC_PID_GENERIC_CLIENT, - BTC_PID_LIGHT_CLIENT, + BTC_PID_LIGHTING_CLIENT, BTC_PID_SENSOR_CLIENT, BTC_PID_TIME_SCENE_CLIENT, #endif /* CONFIG_BLE_MESH */ diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c index 721999bb0e..7ebe7916ea 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c @@ -157,7 +157,7 @@ esp_err_t esp_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) if (model == NULL) { return ESP_ERR_INVALID_ARG; } - return btc_ble_mesh_client_init(model); + return btc_ble_mesh_client_model_init(model); } esp_err_t esp_ble_mesh_server_model_send_msg(esp_ble_mesh_model_t *model, diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h index e72faf95e7..9166af62cc 100644 --- a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -123,7 +123,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; #define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_SRV BLE_MESH_MODEL_ID_GEN_BATTERY_SRV #define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_CLI BLE_MESH_MODEL_ID_GEN_BATTERY_CLI #define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SRV BLE_MESH_MODEL_ID_GEN_LOCATION_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV BLE_MESH_MODEL_ID_GEN_LOCATION_SETUPSRV +#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV #define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_CLI BLE_MESH_MODEL_ID_GEN_LOCATION_CLI #define ESP_BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV #define ESP_BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV @@ -158,7 +158,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; #define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV #define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_CLI BLE_MESH_MODEL_ID_LIGHT_XYL_CLI #define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SRV BLE_MESH_MODEL_ID_LIGHT_LC_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_LC_SETUPSRV +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV #define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_CLI BLE_MESH_MODEL_ID_LIGHT_LC_CLI /*!< The following opcodes will only be used in the esp_ble_mesh_config_client_get_state function. */ diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c index 593927ff2f..08cfcc04fe 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c @@ -27,20 +27,20 @@ esp_err_t esp_ble_mesh_register_config_client_callback(esp_ble_mesh_cfg_client_c { ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); - return (btc_profile_cb_set(BTC_PID_CFG_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); + return (btc_profile_cb_set(BTC_PID_CONFIG_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_mesh_register_config_server_callback(esp_ble_mesh_cfg_server_cb_t callback) { ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); - return (btc_profile_cb_set(BTC_PID_CFG_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); + return (btc_profile_cb_set(BTC_PID_CONFIG_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_cfg_client_get_state_t *get_state) { - btc_ble_mesh_cfg_client_args_t arg = {0}; + btc_ble_mesh_config_client_args_t arg = {0}; btc_msg_t msg = {0}; if (!params || !params->model || !params->ctx.addr || !get_state) { @@ -50,19 +50,19 @@ esp_err_t esp_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_ ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_CFG_CLIENT; + msg.pid = BTC_PID_CONFIG_CLIENT; msg.act = BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE; arg.cfg_client_get_state.params = params; arg.cfg_client_get_state.get_state = get_state; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_cfg_client_args_t), btc_ble_mesh_cfg_client_arg_deep_copy) + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_config_client_args_t), btc_ble_mesh_config_client_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_cfg_client_set_state_t *set_state) { - btc_ble_mesh_cfg_client_args_t arg = {0}; + btc_ble_mesh_config_client_args_t arg = {0}; btc_msg_t msg = {0}; if (!params || !params->model || !params->ctx.addr || !set_state) { @@ -72,11 +72,11 @@ esp_err_t esp_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_ ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_CFG_CLIENT; + msg.pid = BTC_PID_CONFIG_CLIENT; msg.act = BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE; arg.cfg_client_set_state.params = params; arg.cfg_client_set_state.set_state = set_state; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_cfg_client_args_t), btc_ble_mesh_cfg_client_arg_deep_copy) + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_config_client_args_t), btc_ble_mesh_config_client_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c index 83f0c98b5b..df998b243f 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c @@ -27,13 +27,13 @@ esp_err_t esp_ble_mesh_register_light_client_callback(esp_ble_mesh_light_client_ { ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); - return (btc_profile_cb_set(BTC_PID_LIGHT_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); + return (btc_profile_cb_set(BTC_PID_LIGHTING_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_light_client_get_state_t *get_state) { - btc_ble_mesh_light_client_args_t arg = {0}; + btc_ble_mesh_lighting_client_args_t arg = {0}; btc_msg_t msg = {0}; if (!params || !params->model || !params->ctx.addr || !get_state) { @@ -43,19 +43,19 @@ esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_LIGHT_CLIENT; - msg.act = BTC_BLE_MESH_ACT_LIGHT_CLIENT_GET_STATE; + msg.pid = BTC_PID_LIGHTING_CLIENT; + msg.act = BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE; arg.light_client_get_state.params = params; arg.light_client_get_state.get_state = get_state; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_light_client_args_t), btc_ble_mesh_light_client_arg_deep_copy) + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_lighting_client_args_t), btc_ble_mesh_lighting_client_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_mesh_light_client_set_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_light_client_set_state_t *set_state) { - btc_ble_mesh_light_client_args_t arg = {0}; + btc_ble_mesh_lighting_client_args_t arg = {0}; btc_msg_t msg = {0}; if (!params || !params->model || !params->ctx.addr || !set_state) { @@ -65,12 +65,12 @@ esp_err_t esp_ble_mesh_light_client_set_state(esp_ble_mesh_client_common_param_t ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_LIGHT_CLIENT; - msg.act = BTC_BLE_MESH_ACT_LIGHT_CLIENT_SET_STATE; + msg.pid = BTC_PID_LIGHTING_CLIENT; + msg.act = BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE; arg.light_client_set_state.params = params; arg.light_client_set_state.set_state = set_state; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_light_client_args_t), btc_ble_mesh_light_client_arg_deep_copy) + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_lighting_client_args_t), btc_ble_mesh_lighting_client_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h index e34c706302..df9fb3c2da 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h @@ -19,7 +19,6 @@ #ifndef _ESP_BLE_MESH_GENERIC_MODEL_API_H_ #define _ESP_BLE_MESH_GENERIC_MODEL_API_H_ -#include "generic_client.h" #include "esp_ble_mesh_defs.h" /** @def ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h index d40ead593b..f0291a3508 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h @@ -19,7 +19,6 @@ #ifndef _ESP_BLE_MESH_LIGHTING_MODEL_API_H_ #define _ESP_BLE_MESH_LIGHTING_MODEL_API_H_ -#include "lighting_client.h" #include "esp_ble_mesh_defs.h" /** @def ESP_BLE_MESH_MODEL_LIGHT_LIGHTNESS_CLI @@ -108,164 +107,164 @@ /** Parameters of Light Lightness Set */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t lightness; /*!< Target value of light lightness actual state */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light lightness actual state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_lightness_set_t; /** Parameters of Light Lightness Linear Set */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t lightness; /*!< Target value of light lightness linear state */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light lightness linear state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_lightness_linear_set_t; /** Parameter of Light Lightness Default Set */ typedef struct { - u16_t lightness; /*!< The value of the Light Lightness Default state */ + uint16_t lightness; /*!< The value of the Light Lightness Default state */ } esp_ble_mesh_light_lightness_default_set_t; /** Parameters of Light Lightness Range Set */ typedef struct { - u16_t range_min; /*!< Value of range min field of light lightness range state */ - u16_t range_max; /*!< Value of range max field of light lightness range state */ + uint16_t range_min; /*!< Value of range min field of light lightness range state */ + uint16_t range_max; /*!< Value of range max field of light lightness range state */ } esp_ble_mesh_light_lightness_range_set_t; /** Parameters of Light CTL Set */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t ctl_lightness; /*!< Target value of light ctl lightness state */ - u16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ - s16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t ctl_lightness; /*!< Target value of light ctl lightness state */ + uint16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ + int16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_ctl_set_t; /** Parameters of Light CTL Temperature Set */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ - s16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ + int16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_ctl_temperature_set_t; /** Parameters of Light CTL Temperature Range Set */ typedef struct { - u16_t range_min; /*!< Value of temperature range min field of light ctl temperature range state */ - u16_t range_max; /*!< Value of temperature range max field of light ctl temperature range state */ + uint16_t range_min; /*!< Value of temperature range min field of light ctl temperature range state */ + uint16_t range_max; /*!< Value of temperature range max field of light ctl temperature range state */ } esp_ble_mesh_light_ctl_temperature_range_set_t; /** Parameters of Light CTL Default Set */ typedef struct { - u16_t lightness; /*!< Value of light lightness default state */ - u16_t temperature; /*!< Value of light temperature default state */ - s16_t delta_uv; /*!< Value of light delta UV default state */ + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t temperature; /*!< Value of light temperature default state */ + int16_t delta_uv; /*!< Value of light delta UV default state */ } esp_ble_mesh_light_ctl_default_set_t; /** Parameters of Light HSL Set */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t hsl_lightness; /*!< Target value of light hsl lightness state */ - u16_t hsl_hue; /*!< Target value of light hsl hue state */ - u16_t hsl_saturation; /*!< Target value of light hsl saturation state */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hsl_lightness; /*!< Target value of light hsl lightness state */ + uint16_t hsl_hue; /*!< Target value of light hsl hue state */ + uint16_t hsl_saturation; /*!< Target value of light hsl saturation state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_hsl_set_t; /** Parameters of Light HSL Hue Set */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t hue; /*!< Target value of light hsl hue state */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hue; /*!< Target value of light hsl hue state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_hsl_hue_set_t; /** Parameters of Light HSL Saturation Set */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t saturation; /*!< Target value of light hsl hue state */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t saturation; /*!< Target value of light hsl hue state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_hsl_saturation_set_t; /** Parameters of Light HSL Default Set */ typedef struct { - u16_t lightness; /*!< Value of light lightness default state */ - u16_t hue; /*!< Value of light hue default state */ - u16_t saturation; /*!< Value of light saturation default state */ + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t hue; /*!< Value of light hue default state */ + uint16_t saturation; /*!< Value of light saturation default state */ } esp_ble_mesh_light_hsl_default_set_t; /** Parameters of Light HSL Range Set */ typedef struct { - u16_t hue_range_min; /*!< Value of hue range min field of light hsl hue range state */ - u16_t hue_range_max; /*!< Value of hue range max field of light hsl hue range state */ - u16_t saturation_range_min; /*!< Value of saturation range min field of light hsl saturation range state */ - u16_t saturation_range_max; /*!< Value of saturation range max field of light hsl saturation range state */ + uint16_t hue_range_min; /*!< Value of hue range min field of light hsl hue range state */ + uint16_t hue_range_max; /*!< Value of hue range max field of light hsl hue range state */ + uint16_t saturation_range_min; /*!< Value of saturation range min field of light hsl saturation range state */ + uint16_t saturation_range_max; /*!< Value of saturation range max field of light hsl saturation range state */ } esp_ble_mesh_light_hsl_range_set_t; /** Parameters of Light xyL Set */ typedef struct { - bool op_en; /*!< Indicate whether optional parameters included */ - u16_t xyl_lightness; /*!< The target value of the Light xyL Lightness state */ - u16_t xyl_x; /*!< The target value of the Light xyL x state */ - u16_t xyl_y; /*!< The target value of the Light xyL y state */ - u8_t tid; /*!< Transaction Identifier */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate whether optional parameters included */ + uint16_t xyl_lightness; /*!< The target value of the Light xyL Lightness state */ + uint16_t xyl_x; /*!< The target value of the Light xyL x state */ + uint16_t xyl_y; /*!< The target value of the Light xyL y state */ + uint8_t tid; /*!< Transaction Identifier */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_xyl_set_t; /** Parameters of Light xyL Default Set */ typedef struct { - u16_t lightness; /*!< The value of the Light Lightness Default state */ - u16_t xyl_x; /*!< The value of the Light xyL x Default state */ - u16_t xyl_y; /*!< The value of the Light xyL y Default state */ + uint16_t lightness; /*!< The value of the Light Lightness Default state */ + uint16_t xyl_x; /*!< The value of the Light xyL x Default state */ + uint16_t xyl_y; /*!< The value of the Light xyL y Default state */ } esp_ble_mesh_light_xyl_default_set_t; /** Parameters of Light xyL Range Set */ typedef struct { - u16_t xyl_x_range_min; /*!< The value of the xyL x Range Min field of the Light xyL x Range state */ - u16_t xyl_x_range_max; /*!< The value of the xyL x Range Max field of the Light xyL x Range state */ - u16_t xyl_y_range_min; /*!< The value of the xyL y Range Min field of the Light xyL y Range state */ - u16_t xyl_y_range_max; /*!< The value of the xyL y Range Max field of the Light xyL y Range state */ + uint16_t xyl_x_range_min; /*!< The value of the xyL x Range Min field of the Light xyL x Range state */ + uint16_t xyl_x_range_max; /*!< The value of the xyL x Range Max field of the Light xyL x Range state */ + uint16_t xyl_y_range_min; /*!< The value of the xyL y Range Min field of the Light xyL y Range state */ + uint16_t xyl_y_range_max; /*!< The value of the xyL y Range Max field of the Light xyL y Range state */ } esp_ble_mesh_light_xyl_range_set_t; /** Parameter of Light LC Mode Set */ typedef struct { - u8_t mode; /*!< The target value of the Light LC Mode state */ + uint8_t mode; /*!< The target value of the Light LC Mode state */ } esp_ble_mesh_light_lc_mode_set_t; /** Parameter of Light LC OM Set */ typedef struct { - u8_t mode; /*!< The target value of the Light LC Occupancy Mode state */ + uint8_t mode; /*!< The target value of the Light LC Occupancy Mode state */ } esp_ble_mesh_light_lc_om_set_t; /** Parameters of Light LC Light OnOff Set */ typedef struct { - bool op_en; /*!< Indicate whether optional parameters included */ - u8_t light_onoff; /*!< The target value of the Light LC Light OnOff state */ - u8_t tid; /*!< Transaction Identifier */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate whether optional parameters included */ + uint8_t light_onoff; /*!< The target value of the Light LC Light OnOff state */ + uint8_t tid; /*!< Transaction Identifier */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_light_lc_light_onoff_set_t; /** Parameter of Light LC Property Get */ typedef struct { - u16_t property_id; /*!< Property ID identifying a Light LC Property */ + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ } esp_ble_mesh_light_lc_property_get_t; /** Parameters of Light LC Property Set */ typedef struct { - u16_t property_id; /*!< Property ID identifying a Light LC Property */ + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ struct net_buf_simple *property_value; /*!< Raw value for the Light LC Property */ } esp_ble_mesh_light_lc_property_set_t; @@ -308,176 +307,176 @@ typedef union { /** Parameters of Light Lightness Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t present_lightness; /*!< Current value of light lightness actual state */ - u16_t target_lightness; /*!< Target value of light lightness actual state (optional) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_lightness; /*!< Current value of light lightness actual state */ + uint16_t target_lightness; /*!< Target value of light lightness actual state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_light_lightness_status_cb_t; /** Parameters of Light Lightness Linear Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t present_lightness; /*!< Current value of light lightness linear state */ - u16_t target_lightness; /*!< Target value of light lightness linear state (optional) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_lightness; /*!< Current value of light lightness linear state */ + uint16_t target_lightness; /*!< Target value of light lightness linear state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_light_lightness_linear_status_cb_t; /** Parameter of Light Lightness Last Status */ typedef struct { - u16_t lightness; /*!< The value of the Light Lightness Last state */ + uint16_t lightness; /*!< The value of the Light Lightness Last state */ } esp_ble_mesh_light_lightness_last_status_cb_t; /** Parameter of Light Lightness Default Status */ typedef struct { - u16_t lightness; /*!< The value of the Light Lightness default State */ + uint16_t lightness; /*!< The value of the Light Lightness default State */ } esp_ble_mesh_light_lightness_default_status_cb_t; /** Parameters of Light Lightness Range Status */ typedef struct { - u8_t status_code; /*!< Status Code for the request message */ - u16_t range_min; /*!< Value of range min field of light lightness range state */ - u16_t range_max; /*!< Value of range max field of light lightness range state */ + uint8_t status_code; /*!< Status Code for the request message */ + uint16_t range_min; /*!< Value of range min field of light lightness range state */ + uint16_t range_max; /*!< Value of range max field of light lightness range state */ } esp_ble_mesh_light_lightness_range_status_cb_t; /** Parameters of Light CTL Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t present_ctl_lightness; /*!< Current value of light ctl lightness state */ - u16_t present_ctl_temperature; /*!< Current value of light ctl temperature state */ - u16_t target_ctl_lightness; /*!< Target value of light ctl lightness state (optional) */ - u16_t target_ctl_temperature; /*!< Target value of light ctl temperature state (C.1) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_ctl_lightness; /*!< Current value of light ctl lightness state */ + uint16_t present_ctl_temperature; /*!< Current value of light ctl temperature state */ + uint16_t target_ctl_lightness; /*!< Target value of light ctl lightness state (optional) */ + uint16_t target_ctl_temperature; /*!< Target value of light ctl temperature state (C.1) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_light_ctl_status_cb_t; /** Parameters of Light CTL Temperature Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t present_ctl_temperature; /*!< Current value of light ctl temperature state */ - u16_t present_ctl_delta_uv; /*!< Current value of light ctl delta UV state */ - u16_t target_ctl_temperature; /*!< Target value of light ctl temperature state (optional) */ - u16_t target_ctl_delta_uv; /*!< Target value of light ctl delta UV state (C.1) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_ctl_temperature; /*!< Current value of light ctl temperature state */ + uint16_t present_ctl_delta_uv; /*!< Current value of light ctl delta UV state */ + uint16_t target_ctl_temperature; /*!< Target value of light ctl temperature state (optional) */ + uint16_t target_ctl_delta_uv; /*!< Target value of light ctl delta UV state (C.1) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_light_ctl_temperature_status_cb_t; /** Parameters of Light CTL Temperature Range Status */ typedef struct { - u8_t status_code; /*!< Status code for the request message */ - u16_t range_min; /*!< Value of temperature range min field of light ctl temperature range state */ - u16_t range_max; /*!< Value of temperature range max field of light ctl temperature range state */ + uint8_t status_code; /*!< Status code for the request message */ + uint16_t range_min; /*!< Value of temperature range min field of light ctl temperature range state */ + uint16_t range_max; /*!< Value of temperature range max field of light ctl temperature range state */ } esp_ble_mesh_light_ctl_temperature_range_status_cb_t; /** Parameters of Light CTL Default Status */ typedef struct { - u16_t lightness; /*!< Value of light lightness default state */ - u16_t temperature; /*!< Value of light temperature default state */ - s16_t delta_uv; /*!< Value of light delta UV default state */ + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t temperature; /*!< Value of light temperature default state */ + int16_t delta_uv; /*!< Value of light delta UV default state */ } esp_ble_mesh_light_ctl_default_status_cb_t; /** Parameters of Light HSL Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t hsl_lightness; /*!< Current value of light hsl lightness state */ - u16_t hsl_hue; /*!< Current value of light hsl hue state */ - u16_t hsl_saturation; /*!< Current value of light hsl saturation state */ - u8_t remain_time; /*!< Time to complete state transition (optional) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hsl_lightness; /*!< Current value of light hsl lightness state */ + uint16_t hsl_hue; /*!< Current value of light hsl hue state */ + uint16_t hsl_saturation; /*!< Current value of light hsl saturation state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ } esp_ble_mesh_light_hsl_status_cb_t; /** Parameters of Light HSL Target Status */ typedef struct { bool op_en; /*!< Indicate if optional parameters are included */ - u16_t hsl_lightness_target; /*!< Target value of light hsl lightness state */ - u16_t hsl_hue_target; /*!< Target value of light hsl hue state */ - u16_t hsl_saturation_target; /*!< Target value of light hsl saturation state */ - u8_t remain_time; /*!< Time to complete state transition (optional) */ + uint16_t hsl_lightness_target; /*!< Target value of light hsl lightness state */ + uint16_t hsl_hue_target; /*!< Target value of light hsl hue state */ + uint16_t hsl_saturation_target; /*!< Target value of light hsl saturation state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ } esp_ble_mesh_light_hsl_target_status_cb_t; /** Parameters of Light HSL Hue Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t present_hue; /*!< Current value of light hsl hue state */ - u16_t target_hue; /*!< Target value of light hsl hue state (optional) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_hue; /*!< Current value of light hsl hue state */ + uint16_t target_hue; /*!< Target value of light hsl hue state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_light_hsl_hue_status_cb_t; /** Parameters of Light HSL Saturation Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t present_saturation; /*!< Current value of light hsl saturation state */ - u16_t target_saturation; /*!< Target value of light hsl saturation state (optional) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_saturation; /*!< Current value of light hsl saturation state */ + uint16_t target_saturation; /*!< Target value of light hsl saturation state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_light_hsl_saturation_status_cb_t; /** Parameters of Light HSL Default Status */ typedef struct { - u16_t lightness; /*!< Value of light lightness default state */ - u16_t hue; /*!< Value of light hue default state */ - u16_t saturation; /*!< Value of light saturation default state */ + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t hue; /*!< Value of light hue default state */ + uint16_t saturation; /*!< Value of light saturation default state */ } esp_ble_mesh_light_hsl_default_status_cb_t; /** Parameters of Light HSL Range Status */ typedef struct { - u8_t status_code; /*!< Status code for the request message */ - u16_t hue_range_min; /*!< Value of hue range min field of light hsl hue range state */ - u16_t hue_range_max; /*!< Value of hue range max field of light hsl hue range state */ - u16_t saturation_range_min; /*!< Value of saturation range min field of light hsl saturation range state */ - u16_t saturation_range_max; /*!< Value of saturation range max field of light hsl saturation range state */ + uint8_t status_code; /*!< Status code for the request message */ + uint16_t hue_range_min; /*!< Value of hue range min field of light hsl hue range state */ + uint16_t hue_range_max; /*!< Value of hue range max field of light hsl hue range state */ + uint16_t saturation_range_min; /*!< Value of saturation range min field of light hsl saturation range state */ + uint16_t saturation_range_max; /*!< Value of saturation range max field of light hsl saturation range state */ } esp_ble_mesh_light_hsl_range_status_cb_t; /** Parameters of Light xyL Status */ typedef struct { - bool op_en; /*!< Indicate whether optional parameters included */ - u16_t xyl_lightness; /*!< The present value of the Light xyL Lightness state */ - u16_t xyl_x; /*!< The present value of the Light xyL x state */ - u16_t xyl_y; /*!< The present value of the Light xyL y state */ - u8_t remain_time; /*!< Time to complete state transition (optional) */ + bool op_en; /*!< Indicate whether optional parameters included */ + uint16_t xyl_lightness; /*!< The present value of the Light xyL Lightness state */ + uint16_t xyl_x; /*!< The present value of the Light xyL x state */ + uint16_t xyl_y; /*!< The present value of the Light xyL y state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ } esp_ble_mesh_light_xyl_status_cb_t; /** Parameters of Light xyL Target Status */ typedef struct { - bool op_en; /*!< Indicate whether optional parameters included */ - u16_t target_xyl_lightness; /*!< The target value of the Light xyL Lightness state */ - u16_t target_xyl_x; /*!< The target value of the Light xyL x state */ - u16_t target_xyl_y; /*!< The target value of the Light xyL y state */ - u8_t remain_time; /*!< Time to complete state transition (optional) */ + bool op_en; /*!< Indicate whether optional parameters included */ + uint16_t target_xyl_lightness; /*!< The target value of the Light xyL Lightness state */ + uint16_t target_xyl_x; /*!< The target value of the Light xyL x state */ + uint16_t target_xyl_y; /*!< The target value of the Light xyL y state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ } esp_ble_mesh_light_xyl_target_status_cb_t; /** Parameters of Light xyL Default Status */ typedef struct { - u16_t lightness; /*!< The value of the Light Lightness Default state */ - u16_t xyl_x; /*!< The value of the Light xyL x Default state */ - u16_t xyl_y; /*!< The value of the Light xyL y Default state */ + uint16_t lightness; /*!< The value of the Light Lightness Default state */ + uint16_t xyl_x; /*!< The value of the Light xyL x Default state */ + uint16_t xyl_y; /*!< The value of the Light xyL y Default state */ } esp_ble_mesh_light_xyl_default_status_cb_t; /** Parameters of Light xyL Range Status */ typedef struct { - u8_t status_code; /*!< Status Code for the requesting message */ - u16_t xyl_x_range_min; /*!< The value of the xyL x Range Min field of the Light xyL x Range state */ - u16_t xyl_x_range_max; /*!< The value of the xyL x Range Max field of the Light xyL x Range state */ - u16_t xyl_y_range_min; /*!< The value of the xyL y Range Min field of the Light xyL y Range state */ - u16_t xyl_y_range_max; /*!< The value of the xyL y Range Max field of the Light xyL y Range state */ + uint8_t status_code; /*!< Status Code for the requesting message */ + uint16_t xyl_x_range_min; /*!< The value of the xyL x Range Min field of the Light xyL x Range state */ + uint16_t xyl_x_range_max; /*!< The value of the xyL x Range Max field of the Light xyL x Range state */ + uint16_t xyl_y_range_min; /*!< The value of the xyL y Range Min field of the Light xyL y Range state */ + uint16_t xyl_y_range_max; /*!< The value of the xyL y Range Max field of the Light xyL y Range state */ } esp_ble_mesh_light_xyl_range_status_cb_t; /** Parameter of Light LC Mode Status */ typedef struct { - u8_t mode; /*!< The present value of the Light LC Mode state */ + uint8_t mode; /*!< The present value of the Light LC Mode state */ } esp_ble_mesh_light_lc_mode_status_cb_t; /** Parameter of Light LC OM Status */ typedef struct { - u8_t mode; /*!< The present value of the Light LC Occupancy Mode state */ + uint8_t mode; /*!< The present value of the Light LC Occupancy Mode state */ } esp_ble_mesh_light_lc_om_status_cb_t; /** Parameters of Light LC Light OnOff Status */ typedef struct { - bool op_en; /*!< Indicate whether optional parameters included */ - u8_t present_light_onoff; /*!< The present value of the Light LC Light OnOff state */ - u8_t target_light_onoff; /*!< The target value of the Light LC Light OnOff state (Optional) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate whether optional parameters included */ + uint8_t present_light_onoff; /*!< The present value of the Light LC Light OnOff state */ + uint8_t target_light_onoff; /*!< The target value of the Light LC Light OnOff state (Optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_light_lc_light_onoff_status_cb_t; /** Parameters of Light LC Property Status */ typedef struct { - u16_t property_id; /*!< Property ID identifying a Light LC Property */ + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ struct net_buf_simple *property_value; /*!< Raw value for the Light LC Property */ } esp_ble_mesh_light_lc_property_status_cb_t; @@ -571,7 +570,7 @@ esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t * please refer to esp_ble_mesh_light_message_opcode_t in esp_ble_mesh_defs.h * * @param[in] params: Pointer to BLE Mesh common client parameters. - * @param[in] set_state: Pointer of generic set message value. + * @param[in] set_state: Pointer of light set message value. * Shall not be set to NULL. * * @return ESP_OK on success or error code otherwise. diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h index d303e63a14..1bc237a8ab 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h @@ -19,7 +19,6 @@ #ifndef _ESP_BLE_MESH_SENSOR_MODEL_API_H_ #define _ESP_BLE_MESH_SENSOR_MODEL_API_H_ -#include "sensor_client.h" #include "esp_ble_mesh_defs.h" /** @def ESP_BLE_MESH_MODEL_SENSOR_CLI @@ -45,60 +44,60 @@ /** Parameters of Sensor Descriptor Get */ typedef struct { bool op_en; /*!< Indicate if optional parameters are included */ - u16_t property_id; /*!< Property ID of a sensor (optional) */ + uint16_t property_id; /*!< Property ID of a sensor (optional) */ } esp_ble_mesh_sensor_descriptor_get_t; /** Parameter of Sensor Cadence Get */ typedef struct { - u16_t property_id; /*!< Property ID of a sensor */ + uint16_t property_id; /*!< Property ID of a sensor */ } esp_ble_mesh_sensor_cadence_get_t; /** Parameters of Sensor Cadence Set */ typedef struct { - u16_t property_id; /*!< Property ID for the sensor */ - u8_t fast_cadence_period_divisor : 7, /*!< Divisor for the publish period */ - status_trigger_type : 1; /*!< The unit and format of the Status Trigger Delta fields */ + uint16_t property_id; /*!< Property ID for the sensor */ + uint8_t fast_cadence_period_divisor : 7, /*!< Divisor for the publish period */ + status_trigger_type : 1; /*!< The unit and format of the Status Trigger Delta fields */ struct net_buf_simple *status_trigger_delta_down; /*!< Delta down value that triggers a status message */ struct net_buf_simple *status_trigger_delta_up; /*!< Delta up value that triggers a status message */ - u8_t status_min_interval; /*!< Minimum interval between two consecutive Status messages */ + uint8_t status_min_interval; /*!< Minimum interval between two consecutive Status messages */ struct net_buf_simple *fast_cadence_low; /*!< Low value for the fast cadence range */ struct net_buf_simple *fast_cadence_high; /*!< Fast value for the fast cadence range */ } esp_ble_mesh_sensor_cadence_set_t; /** Parameter of Sensor Settings Get */ typedef struct { - u16_t sensor_property_id; /*!< Property ID of a sensor */ + uint16_t sensor_property_id; /*!< Property ID of a sensor */ } esp_ble_mesh_sensor_settings_get_t; /** Parameters of Sensor Setting Get */ typedef struct { - u16_t sensor_property_id; /*!< Property ID of a sensor */ - u16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ + uint16_t sensor_property_id; /*!< Property ID of a sensor */ + uint16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ } esp_ble_mesh_sensor_setting_get_t; /** Parameters of Sensor Setting Set */ typedef struct { - u16_t sensor_property_id; /*!< Property ID identifying a sensor */ - u16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ + uint16_t sensor_property_id; /*!< Property ID identifying a sensor */ + uint16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ struct net_buf_simple *sensor_setting_raw; /*!< Raw value for the setting */ } esp_ble_mesh_sensor_setting_set_t; /** Parameters of Sensor Get */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t property_id; /*!< Property ID for the sensor (optional) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID for the sensor (optional) */ } esp_ble_mesh_sensor_get_t; /** Parameters of Sensor Column Get */ typedef struct { - u16_t property_id; /*!< Property identifying a sensor */ + uint16_t property_id; /*!< Property identifying a sensor */ struct net_buf_simple *raw_value_x; /*!< Raw value identifying a column */ } esp_ble_mesh_sensor_column_get_t; /** Parameters of Sensor Series Get */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t property_id; /*!< Property identifying a sensor */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property identifying a sensor */ struct net_buf_simple *raw_value_x1; /*!< Raw value identifying a starting column (optional) */ struct net_buf_simple *raw_value_x2; /*!< Raw value identifying an ending column (C.1) */ } esp_ble_mesh_sensor_series_get_t; @@ -135,22 +134,22 @@ typedef struct { /** Parameters of Sensor Cadence Status */ typedef struct { - u16_t property_id; /*!< Property for the sensor */ + uint16_t property_id; /*!< Property for the sensor */ struct net_buf_simple *sensor_cadence_value; /*!< Value of sensor cadence state */ } esp_ble_mesh_sensor_cadence_status_cb_t; /** Parameters of Sensor Settings Status */ typedef struct { - u16_t sensor_property_id; /*!< Property ID identifying a sensor */ + uint16_t sensor_property_id; /*!< Property ID identifying a sensor */ struct net_buf_simple *sensor_setting_property_ids; /*!< A sequence of N sensor setting property IDs (optional) */ } esp_ble_mesh_sensor_settings_status_cb_t; /** Parameters of Sensor Setting Status */ typedef struct { - bool op_en; /*!< Indicate id optional parameters are included */ - u16_t sensor_property_id; /*!< Property ID identifying a sensor */ - u16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ - u8_t sensor_setting_access; /*!< Read/Write access rights for the setting (optional) */ + bool op_en; /*!< Indicate id optional parameters are included */ + uint16_t sensor_property_id; /*!< Property ID identifying a sensor */ + uint16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ + uint8_t sensor_setting_access; /*!< Read/Write access rights for the setting (optional) */ struct net_buf_simple *sensor_setting_raw; /*!< Raw value for the setting */ } esp_ble_mesh_sensor_setting_status_cb_t; @@ -161,13 +160,13 @@ typedef struct { /** Parameters of Sensor Column Status */ typedef struct { - u16_t property_id; /*!< Property identifying a sensor and the Y axis */ + uint16_t property_id; /*!< Property identifying a sensor and the Y axis */ struct net_buf_simple *sensor_column_value; /*!< Left values of sensor column status */ } esp_ble_mesh_sensor_column_status_cb_t; /** Parameters of Sensor Series Status */ typedef struct { - u16_t property_id; /*!< Property identifying a sensor and the Y axis */ + uint16_t property_id; /*!< Property identifying a sensor and the Y axis */ struct net_buf_simple *sensor_series_value; /*!< Left values of sensor series status */ } esp_ble_mesh_sensor_series_status_cb_t; diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h index da45b40517..3d2aebd12c 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h @@ -19,7 +19,6 @@ #ifndef _ESP_BLE_MESH_TIME_SCENE_MODEL_API_H_ #define _ESP_BLE_MESH_TIME_SCENE_MODEL_API_H_ -#include "time_scene_client.h" #include "esp_ble_mesh_defs.h" /** @def ESP_BLE_MESH_MODEL_TIME_CLI @@ -76,69 +75,69 @@ /** Parameters of Time Set */ typedef struct { - u8_t tai_seconds[5]; /*!< The current TAI time in seconds */ - u8_t sub_second; /*!< The sub-second time in units of 1/256 second */ - u8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ - u16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ - u16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ - u8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t sub_second; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ } esp_ble_mesh_time_set_t; /** Parameters of Time Zone Set */ typedef struct { - u8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ - u8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ + uint8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ + uint8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ } esp_ble_mesh_time_zone_set_t; /** Parameters of TAI-UTC Delta Set */ typedef struct { - u16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ - u16_t padding : 1; /*!< Always 0b0. Other values are Prohibited. */ - u8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ + uint16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ + uint16_t padding : 1; /*!< Always 0b0. Other values are Prohibited. */ + uint8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ } esp_ble_mesh_tai_utc_delta_set_t; /** Parameter of Time Role Set */ typedef struct { - u8_t time_role; /*!< The Time Role for the element */ + uint8_t time_role; /*!< The Time Role for the element */ } esp_ble_mesh_time_role_set_t; /** Parameter of Scene Store */ typedef struct { - u16_t scene_number; /*!< The number of scenes to be stored */ + uint16_t scene_number; /*!< The number of scenes to be stored */ } esp_ble_mesh_scene_store_t; /** Parameters of Scene Recall */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u16_t scene_number; /*!< The number of scenes to be recalled */ - u8_t tid; /*!< Transaction ID */ - u8_t trans_time; /*!< Time to complete state transition (optional) */ - u8_t delay; /*!< Indicate message execution delay (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t scene_number; /*!< The number of scenes to be recalled */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ } esp_ble_mesh_scene_recall_t; /** Parameter of Scene Delete */ typedef struct { - u16_t scene_number; /*!< The number of scenes to be deleted */ + uint16_t scene_number; /*!< The number of scenes to be deleted */ } esp_ble_mesh_scene_delete_t; /** Parameter of Scheduler Action Get */ typedef struct { - u8_t index; /*!< Index of the Schedule Register entry to get */ + uint8_t index; /*!< Index of the Schedule Register entry to get */ } esp_ble_mesh_scheduler_act_get_t; /** Parameters of Scheduler Action Set */ typedef struct { - u64_t index : 4; /*!< Index of the Schedule Register entry to set */ - u64_t year : 7; /*!< Scheduled year for the action */ - u64_t month : 12; /*!< Scheduled month for the action */ - u64_t day : 5; /*!< Scheduled day of the month for the action */ - u64_t hour : 5; /*!< Scheduled hour for the action */ - u64_t minute : 6; /*!< Scheduled minute for the action */ - u64_t second : 6; /*!< Scheduled second for the action */ - u64_t day_of_week : 7; /*!< Schedule days of the week for the action */ - u64_t action : 4; /*!< Action to be performed at the scheduled time */ - u64_t trans_time : 8; /*!< Transition time for this action */ - u16_t scene_number; /*!< Transition time for this action */ + uint64_t index : 4; /*!< Index of the Schedule Register entry to set */ + uint64_t year : 7; /*!< Scheduled year for the action */ + uint64_t month : 12; /*!< Scheduled month for the action */ + uint64_t day : 5; /*!< Scheduled day of the month for the action */ + uint64_t hour : 5; /*!< Scheduled hour for the action */ + uint64_t minute : 6; /*!< Scheduled minute for the action */ + uint64_t second : 6; /*!< Scheduled second for the action */ + uint64_t day_of_week : 7; /*!< Schedule days of the week for the action */ + uint64_t action : 4; /*!< Action to be performed at the scheduled time */ + uint64_t trans_time : 8; /*!< Transition time for this action */ + uint16_t scene_number; /*!< Transition time for this action */ } esp_ble_mesh_scheduler_act_set_t; /** @@ -168,69 +167,69 @@ typedef union { /** Parameters of Time Status */ typedef struct { - u8_t tai_seconds[5]; /*!< The current TAI time in seconds */ - u8_t sub_second; /*!< The sub-second time in units of 1/256 second */ - u8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ - u16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ - u16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ - u8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t sub_second; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ } esp_ble_mesh_time_status_cb_t; /** Parameters of Time Zone Status */ typedef struct { - u8_t time_zone_offset_curr; /*!< Current local time zone offset */ - u8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ - u8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ + uint8_t time_zone_offset_curr; /*!< Current local time zone offset */ + uint8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ + uint8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ } esp_ble_mesh_time_zone_status_cb_t; /** Parameters of TAI-UTC Delta Status */ typedef struct { - u16_t tai_utc_delta_curr : 15; /*!< Current difference between TAI and UTC in seconds */ - u16_t padding_1 : 1; /*!< Always 0b0. Other values are Prohibited. */ - u16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ - u16_t padding_2 : 1; /*!< Always 0b0. Other values are Prohibited. */ - u8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ + uint16_t tai_utc_delta_curr : 15; /*!< Current difference between TAI and UTC in seconds */ + uint16_t padding_1 : 1; /*!< Always 0b0. Other values are Prohibited. */ + uint16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ + uint16_t padding_2 : 1; /*!< Always 0b0. Other values are Prohibited. */ + uint8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ } esp_ble_mesh_tai_utc_delta_status_cb_t; /** Parameter of Time Role Status */ typedef struct { - u8_t time_role; /*!< The Time Role for the element */ + uint8_t time_role; /*!< The Time Role for the element */ } esp_ble_mesh_time_role_status_cb_t; /** Parameters of Scene Status */ typedef struct { - bool op_en; /*!< Indicate if optional parameters are included */ - u8_t status_code; /*!< Status code of the last operation */ - u16_t current_scene; /*!< Scene Number of the current scene */ - u16_t target_scene; /*!< Scene Number of the target scene (optional) */ - u8_t remain_time; /*!< Time to complete state transition (C.1) */ + bool op_en; /*!< Indicate if optional parameters are included */ + uint8_t status_code; /*!< Status code of the last operation */ + uint16_t current_scene; /*!< Scene Number of the current scene */ + uint16_t target_scene; /*!< Scene Number of the target scene (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ } esp_ble_mesh_scene_status_cb_t; /** Parameters of Scene Register Status */ typedef struct { - u8_t status_code; /*!< Status code for the previous operation */ - u16_t current_scene; /*!< Scene Number of the current scene */ + uint8_t status_code; /*!< Status code for the previous operation */ + uint16_t current_scene; /*!< Scene Number of the current scene */ struct net_buf_simple *scenes; /*!< A list of scenes stored within an element */ } esp_ble_mesh_scene_register_status_cb_t; /** Parameter of Scheduler Status */ typedef struct { - u16_t schedules; /*!< Bit field indicating defined Actions in the Schedule Register */ + uint16_t schedules; /*!< Bit field indicating defined Actions in the Schedule Register */ } esp_ble_mesh_scheduler_status_cb_t; /** Parameters of Scheduler Action Status */ typedef struct { - u64_t index : 4; /*!< Enumerates (selects) a Schedule Register entry */ - u64_t year : 7; /*!< Scheduled year for the action */ - u64_t month : 12; /*!< Scheduled month for the action */ - u64_t day : 5; /*!< Scheduled day of the month for the action */ - u64_t hour : 5; /*!< Scheduled hour for the action */ - u64_t minute : 6; /*!< Scheduled minute for the action */ - u64_t second : 6; /*!< Scheduled second for the action */ - u64_t day_of_week : 7; /*!< Schedule days of the week for the action */ - u64_t action : 4; /*!< Action to be performed at the scheduled time */ - u64_t trans_time : 8; /*!< Transition time for this action */ - u16_t scene_number; /*!< Transition time for this action */ + uint64_t index : 4; /*!< Enumerates (selects) a Schedule Register entry */ + uint64_t year : 7; /*!< Scheduled year for the action */ + uint64_t month : 12; /*!< Scheduled month for the action */ + uint64_t day : 5; /*!< Scheduled day of the month for the action */ + uint64_t hour : 5; /*!< Scheduled hour for the action */ + uint64_t minute : 6; /*!< Scheduled minute for the action */ + uint64_t second : 6; /*!< Scheduled second for the action */ + uint64_t day_of_week : 7; /*!< Schedule days of the week for the action */ + uint64_t action : 4; /*!< Action to be performed at the scheduled time */ + uint64_t trans_time : 8; /*!< Transition time for this action */ + uint16_t scene_number; /*!< Transition time for this action */ } esp_ble_mesh_scheduler_act_status_cb_t; /** diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c index 30257a5c6a..bfab0a9e1c 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c @@ -27,28 +27,22 @@ extern s32_t config_msg_timeout; -static inline void btc_ble_mesh_cfg_client_cb_to_app(esp_ble_mesh_cfg_client_cb_event_t event, +/* Configuration Client Model related functions */ + +static inline void btc_ble_mesh_config_client_cb_to_app(esp_ble_mesh_cfg_client_cb_event_t event, esp_ble_mesh_cfg_client_cb_param_t *param) { - esp_ble_mesh_cfg_client_cb_t btc_mesh_cb = (esp_ble_mesh_cfg_client_cb_t)btc_profile_cb_get(BTC_PID_CFG_CLIENT); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); + esp_ble_mesh_cfg_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_cfg_client_cb_t)btc_profile_cb_get(BTC_PID_CONFIG_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); } } -static inline void btc_ble_mesh_cfg_server_cb_to_app(esp_ble_mesh_cfg_server_cb_event_t event, - esp_ble_mesh_cfg_server_cb_param_t *param) +void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) { - esp_ble_mesh_cfg_server_cb_t btc_mesh_cb = (esp_ble_mesh_cfg_server_cb_t)btc_profile_cb_get(BTC_PID_CFG_SERVER); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); - } -} - -void btc_ble_mesh_cfg_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) -{ - btc_ble_mesh_cfg_client_args_t *dst = (btc_ble_mesh_cfg_client_args_t *)p_dest; - btc_ble_mesh_cfg_client_args_t *src = (btc_ble_mesh_cfg_client_args_t *)p_src; + btc_ble_mesh_config_client_args_t *dst = (btc_ble_mesh_config_client_args_t *)p_dest; + btc_ble_mesh_config_client_args_t *src = (btc_ble_mesh_config_client_args_t *)p_src; if (!msg || !dst || !src) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -88,11 +82,43 @@ void btc_ble_mesh_cfg_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p } } -static void btc_ble_mesh_cfg_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +static void btc_ble_mesh_config_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_config_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_config_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: + if (arg->cfg_client_get_state.params) { + osi_free(arg->cfg_client_get_state.params); + } + if (arg->cfg_client_get_state.get_state) { + osi_free(arg->cfg_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: + if (arg->cfg_client_set_state.params) { + osi_free(arg->cfg_client_set_state.params); + } + if (arg->cfg_client_set_state.set_state) { + osi_free(arg->cfg_client_set_state.set_state); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { esp_ble_mesh_cfg_client_cb_param_t *p_dest_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_dest; esp_ble_mesh_cfg_client_cb_param_t *p_src_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !p_src_data || !p_dest_data) { @@ -105,8 +131,7 @@ static void btc_ble_mesh_cfg_client_copy_req_data(btc_msg_t *msg, void *p_dest, case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: if (p_src_data->params) { - opcode = p_src_data->params->opcode; - switch (opcode) { + switch (p_src_data->params->opcode) { case OP_DEV_COMP_DATA_GET: case OP_DEV_COMP_DATA_STATUS: if (p_src_data->status_cb.comp_data_status.composition_data) { @@ -200,10 +225,9 @@ static void btc_ble_mesh_cfg_client_copy_req_data(btc_msg_t *msg, void *p_dest, } } -static void btc_ble_mesh_cfg_client_free_req_data(btc_msg_t *msg) +static void btc_ble_mesh_config_client_free_req_data(btc_msg_t *msg) { esp_ble_mesh_cfg_client_cb_param_t *arg = NULL; - u32_t opcode; if (!msg || !msg->arg) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -217,8 +241,7 @@ static void btc_ble_mesh_cfg_client_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: if (arg->params) { - opcode = arg->params->opcode; - switch (opcode) { + switch (arg->params->opcode) { case OP_DEV_COMP_DATA_GET: case OP_DEV_COMP_DATA_STATUS: bt_mesh_free_buf(arg->status_cb.comp_data_status.composition_data); @@ -257,63 +280,28 @@ static void btc_ble_mesh_cfg_client_free_req_data(btc_msg_t *msg) } } -void btc_ble_mesh_cfg_client_arg_deep_free(btc_msg_t *msg) -{ - btc_ble_mesh_cfg_client_args_t *arg = NULL; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - arg = (btc_ble_mesh_cfg_client_args_t *)(msg->arg); - - switch (msg->act) { - case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: - if (arg->cfg_client_get_state.params) { - osi_free(arg->cfg_client_get_state.params); - } - if (arg->cfg_client_get_state.get_state) { - osi_free(arg->cfg_client_get_state.get_state); - } - break; - case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: - if (arg->cfg_client_set_state.params) { - osi_free(arg->cfg_client_set_state.params); - } - if (arg->cfg_client_set_state.set_state) { - osi_free(arg->cfg_client_set_state.set_state); - } - break; - default: - break; - } - - return; -} - -static void btc_mesh_cfg_client_callback(esp_ble_mesh_cfg_client_cb_param_t *cb_params, uint8_t act) +static void btc_ble_mesh_config_client_callback(esp_ble_mesh_cfg_client_cb_param_t *cb_params, uint8_t act) { btc_msg_t msg = {0}; LOG_DEBUG("%s", __func__); msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_CFG_CLIENT; + msg.pid = BTC_PID_CONFIG_CLIENT; msg.act = act; btc_transfer_context(&msg, cb_params, - sizeof(esp_ble_mesh_cfg_client_cb_param_t), btc_ble_mesh_cfg_client_copy_req_data); + sizeof(esp_ble_mesh_cfg_client_cb_param_t), btc_ble_mesh_config_client_copy_req_data); } -void bt_mesh_callback_config_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_config_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len) { esp_ble_mesh_cfg_client_cb_param_t cb_params = {0}; esp_ble_mesh_client_common_param_t params = {0}; - size_t length; + size_t length; uint8_t act; if (!model || !ctx) { @@ -322,16 +310,16 @@ void bt_mesh_callback_config_status_to_btc(u32_t opcode, u8_t evt_type, } switch (evt_type) { - case 0x00: + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE: act = ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT; break; - case 0x01: + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE: act = ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT; break; - case 0x02: + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH: act = ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT; break; - case 0x03: + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT: act = ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT; break; default: @@ -356,101 +344,32 @@ void bt_mesh_callback_config_status_to_btc(u32_t opcode, u8_t evt_type, memcpy(&cb_params.status_cb, val, length); } - btc_mesh_cfg_client_callback(&cb_params, act); + btc_ble_mesh_config_client_callback(&cb_params, act); + return; } - -void btc_mesh_cfg_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) +void btc_ble_mesh_config_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { if (!model || !ctx || !buf) { LOG_ERROR("%s, Invalid parameter", __func__); return; } - bt_mesh_callback_config_status_to_btc(opcode, 0x02, model, ctx, buf->data, buf->len); + bt_mesh_config_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; } -void btc_mesh_cfg_client_call_handler(btc_msg_t *msg) -{ - esp_ble_mesh_cfg_client_cb_param_t cfg_client_cb = {0}; - btc_ble_mesh_cfg_client_args_t *arg = NULL; - bt_mesh_role_param_t role_param = {0}; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - arg = (btc_ble_mesh_cfg_client_args_t *)(msg->arg); - - switch (msg->act) { - case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: { - cfg_client_cb.params = arg->cfg_client_get_state.params; - role_param.model = (struct bt_mesh_model *)cfg_client_cb.params->model; - role_param.role = cfg_client_cb.params->msg_role; - if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); - return; - } - btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params, - arg->cfg_client_get_state.get_state, - &cfg_client_cb); - if (cfg_client_cb.error_code) { - btc_mesh_cfg_client_callback(&cfg_client_cb, ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT); - } - break; - } - case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: { - cfg_client_cb.params = arg->cfg_client_set_state.params; - role_param.model = (struct bt_mesh_model *)cfg_client_cb.params->model; - role_param.role = cfg_client_cb.params->msg_role; - if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); - return; - } - btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params, - arg->cfg_client_set_state.set_state, - &cfg_client_cb); - if (cfg_client_cb.error_code) { - btc_mesh_cfg_client_callback(&cfg_client_cb, ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT); - } - break; - } - default: - break; - } - - btc_ble_mesh_cfg_client_arg_deep_free(msg); -} - -void btc_mesh_cfg_client_cb_handler(btc_msg_t *msg) -{ - esp_ble_mesh_cfg_client_cb_param_t *param = NULL; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - param = (esp_ble_mesh_cfg_client_cb_param_t *)(msg->arg); - - if (msg->act < ESP_BLE_MESH_CFG_CLIENT_EVT_MAX) { - btc_ble_mesh_cfg_client_cb_to_app(msg->act, param); - } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); - } - - btc_ble_mesh_cfg_client_free_req_data(msg); -} - -int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, - esp_ble_mesh_cfg_client_get_state_t *get_state, - esp_ble_mesh_cfg_client_cb_param_t *cfg_client_cb) +static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_get_state_t *get, + esp_ble_mesh_cfg_client_cb_param_t *cb) { struct bt_mesh_msg_ctx ctx = {0}; - if (!params || !cfg_client_cb) { + if (!params || !cb) { LOG_ERROR("%s, Invalid parameter", __func__); return -EINVAL; } @@ -465,66 +384,68 @@ int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *par switch (params->opcode) { case ESP_BLE_MESH_MODEL_OP_BEACON_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_beacon_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_beacon_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_ttl_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_ttl_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_FRIEND_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_friend_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_friend_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_gatt_proxy_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_gatt_proxy_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_RELAY_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_relay_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_relay_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_pub_get(&ctx, get_state->model_pub_get.element_addr, get_state->model_pub_get.model_id, - get_state->model_pub_get.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_pub_get(&ctx, get->model_pub_get.element_addr, + get->model_pub_get.model_id, get->model_pub_get.company_id)); case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_hb_pub_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_hb_pub_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_hb_sub_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_hb_sub_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_comp_data_get(&ctx, get_state->comp_data_get.page)); + return (cb->error_code = bt_mesh_cfg_comp_data_get(&ctx, get->comp_data_get.page)); case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_get(&ctx, get_state->sig_model_sub_get.element_addr, get_state->sig_model_sub_get.model_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_get(&ctx, get->sig_model_sub_get.element_addr, + get->sig_model_sub_get.model_id)); case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_get_vnd(&ctx, get_state->vnd_model_sub_get.element_addr, - get_state->vnd_model_sub_get.model_id, get_state->vnd_model_sub_get.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_get_vnd(&ctx, get->vnd_model_sub_get.element_addr, + get->vnd_model_sub_get.model_id, get->vnd_model_sub_get.company_id)); case ESP_BLE_MESH_MODEL_OP_NET_KEY_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_net_key_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_net_key_get(&ctx)); case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_app_key_get(&ctx, get_state->app_key_get.net_idx)); + return (cb->error_code = bt_mesh_cfg_app_key_get(&ctx, get->app_key_get.net_idx)); case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_node_identity_get(&ctx, get_state->node_identity_get.net_idx)); + return (cb->error_code = bt_mesh_cfg_node_identity_get(&ctx, get->node_identity_get.net_idx)); case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_app_get(&ctx, get_state->sig_model_app_get.element_addr, get_state->sig_model_app_get.model_id)); + return (cb->error_code = + bt_mesh_cfg_mod_app_get(&ctx, get->sig_model_app_get.element_addr, + get->sig_model_app_get.model_id)); case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_app_get_vnd(&ctx, get_state->vnd_model_app_get.element_addr, - get_state->vnd_model_app_get.model_id, get_state->vnd_model_app_get.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_app_get_vnd(&ctx, get->vnd_model_app_get.element_addr, + get->vnd_model_app_get.model_id, get->vnd_model_app_get.company_id)); case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_kr_phase_get(&ctx, get_state->kr_phase_get.net_idx)); + return (cb->error_code = bt_mesh_cfg_kr_phase_get(&ctx, get->kr_phase_get.net_idx)); case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_lpn_timeout_get(&ctx, get_state->lpn_pollto_get.lpn_addr)); + return (cb->error_code = bt_mesh_cfg_lpn_timeout_get(&ctx, get->lpn_pollto_get.lpn_addr)); case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET: - return (cfg_client_cb->error_code = bt_mesh_cfg_net_transmit_get(&ctx)); + return (cb->error_code = bt_mesh_cfg_net_transmit_get(&ctx)); default: - BT_WARN("%s, Invalid opcode 0x%x", __func__, params->opcode); - return (cfg_client_cb->error_code = -EINVAL); + LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return (cb->error_code = -EINVAL); } return 0; } -int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, - esp_ble_mesh_cfg_client_set_state_t *set_state, - esp_ble_mesh_cfg_client_cb_param_t *cfg_client_cb) +static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_set_state_t *set, + esp_ble_mesh_cfg_client_cb_param_t *cb) { struct bt_mesh_msg_ctx ctx = {0}; - if (!params || !set_state || !cfg_client_cb) { + if (!params || !set || !cb) { LOG_ERROR("%s, Invalid parameter", __func__); return -EINVAL; } @@ -539,141 +460,245 @@ int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *par switch (params->opcode) { case ESP_BLE_MESH_MODEL_OP_BEACON_SET: - return (cfg_client_cb->error_code = bt_mesh_cfg_beacon_set(&ctx, set_state->beacon_set.beacon)); + return (cb->error_code = bt_mesh_cfg_beacon_set(&ctx, set->beacon_set.beacon)); case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET: - return (cfg_client_cb->error_code = bt_mesh_cfg_ttl_set(&ctx, set_state->default_ttl_set.ttl)); + return (cb->error_code = bt_mesh_cfg_ttl_set(&ctx, set->default_ttl_set.ttl)); case ESP_BLE_MESH_MODEL_OP_FRIEND_SET: - return (cfg_client_cb->error_code = bt_mesh_cfg_friend_set(&ctx, set_state->friend_set.friend_state)); + return (cb->error_code = bt_mesh_cfg_friend_set(&ctx, set->friend_set.friend_state)); case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET: - return (cfg_client_cb->error_code = bt_mesh_cfg_gatt_proxy_set(&ctx, set_state->gatt_proxy_set.gatt_proxy)); + return (cb->error_code = bt_mesh_cfg_gatt_proxy_set(&ctx, set->gatt_proxy_set.gatt_proxy)); case ESP_BLE_MESH_MODEL_OP_RELAY_SET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_relay_set(&ctx, set_state->relay_set.relay, set_state->relay_set.relay_retransmit)); + return (cb->error_code = + bt_mesh_cfg_relay_set(&ctx, set->relay_set.relay, set->relay_set.relay_retransmit)); case ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD: - return (cfg_client_cb->error_code = - bt_mesh_cfg_net_key_add(&ctx, set_state->net_key_add.net_idx, &set_state->net_key_add.net_key[0])); + return (cb->error_code = + bt_mesh_cfg_net_key_add(&ctx, set->net_key_add.net_idx, + &set->net_key_add.net_key[0])); case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: - return (cfg_client_cb->error_code = - bt_mesh_cfg_app_key_add(&ctx, set_state->app_key_add.net_idx, - set_state->app_key_add.app_idx, &set_state->app_key_add.app_key[0])); + return (cb->error_code = + bt_mesh_cfg_app_key_add(&ctx, set->app_key_add.net_idx, + set->app_key_add.app_idx, &set->app_key_add.app_key[0])); case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_app_bind(&ctx, set_state->model_app_bind.element_addr, set_state->model_app_bind.model_app_idx, - set_state->model_app_bind.model_id, set_state->model_app_bind.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_app_bind(&ctx, set->model_app_bind.element_addr, + set->model_app_bind.model_app_idx, set->model_app_bind.model_id, + set->model_app_bind.company_id)); case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET: { struct bt_mesh_cfg_mod_pub model_pub = { - .addr = set_state->model_pub_set.publish_addr, - .app_idx = set_state->model_pub_set.publish_app_idx, - .cred_flag = set_state->model_pub_set.cred_flag, - .ttl = set_state->model_pub_set.publish_ttl, - .period = set_state->model_pub_set.publish_period, - .transmit = set_state->model_pub_set.publish_retransmit, + .addr = set->model_pub_set.publish_addr, + .app_idx = set->model_pub_set.publish_app_idx, + .cred_flag = set->model_pub_set.cred_flag, + .ttl = set->model_pub_set.publish_ttl, + .period = set->model_pub_set.publish_period, + .transmit = set->model_pub_set.publish_retransmit, }; - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_pub_set(&ctx, set_state->model_pub_set.element_addr, set_state->model_pub_set.model_id, - set_state->model_pub_set.company_id, &model_pub)); + return (cb->error_code = + bt_mesh_cfg_mod_pub_set(&ctx, set->model_pub_set.element_addr, + set->model_pub_set.model_id, set->model_pub_set.company_id, &model_pub)); } case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_add(&ctx, set_state->model_sub_add.element_addr, set_state->model_sub_add.sub_addr, - set_state->model_sub_add.model_id, set_state->model_sub_add.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_add(&ctx, set->model_sub_add.element_addr, + set->model_sub_add.sub_addr, set->model_sub_add.model_id, + set->model_sub_add.company_id)); case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_del(&ctx, set_state->model_sub_delete.element_addr, set_state->model_sub_delete.sub_addr, - set_state->model_sub_delete.model_id, set_state->model_sub_delete.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_del(&ctx, set->model_sub_delete.element_addr, + set->model_sub_delete.sub_addr, set->model_sub_delete.model_id, + set->model_sub_delete.company_id)); case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_overwrite(&ctx, set_state->model_sub_overwrite.element_addr, set_state->model_sub_overwrite.sub_addr, - set_state->model_sub_overwrite.model_id, set_state->model_sub_overwrite.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_overwrite(&ctx, set->model_sub_overwrite.element_addr, + set->model_sub_overwrite.sub_addr, set->model_sub_overwrite.model_id, + set->model_sub_overwrite.company_id)); case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_va_add(&ctx, set_state->model_sub_va_add.element_addr, &set_state->model_sub_va_add.label_uuid[0], - set_state->model_sub_va_add.model_id, set_state->model_sub_va_add.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_va_add(&ctx, set->model_sub_va_add.element_addr, + &set->model_sub_va_add.label_uuid[0], set->model_sub_va_add.model_id, + set->model_sub_va_add.company_id)); case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_va_overwrite(&ctx, set_state->model_sub_va_overwrite.element_addr, &set_state->model_sub_va_overwrite.label_uuid[0], - set_state->model_sub_va_overwrite.model_id, set_state->model_sub_va_overwrite.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_va_overwrite(&ctx, set->model_sub_va_overwrite.element_addr, + &set->model_sub_va_overwrite.label_uuid[0], set->model_sub_va_overwrite.model_id, + set->model_sub_va_overwrite.company_id)); case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_va_del(&ctx, set_state->model_sub_va_delete.element_addr, &set_state->model_sub_va_delete.label_uuid[0], - set_state->model_sub_va_delete.model_id, set_state->model_sub_va_delete.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_va_del(&ctx, set->model_sub_va_delete.element_addr, + &set->model_sub_va_delete.label_uuid[0], set->model_sub_va_delete.model_id, + set->model_sub_va_delete.company_id)); case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_hb_sub_set(&ctx, (struct bt_mesh_cfg_hb_sub *)&set_state->heartbeat_sub_set)); + return (cb->error_code = + bt_mesh_cfg_hb_sub_set(&ctx, + (struct bt_mesh_cfg_hb_sub *)&set->heartbeat_sub_set)); case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_hb_pub_set(&ctx, (const struct bt_mesh_cfg_hb_pub *)&set_state->heartbeat_pub_set)); + return (cb->error_code = + bt_mesh_cfg_hb_pub_set(&ctx, + (const struct bt_mesh_cfg_hb_pub *)&set->heartbeat_pub_set)); case ESP_BLE_MESH_MODEL_OP_NODE_RESET: - return (cfg_client_cb->error_code = bt_mesh_cfg_node_reset(&ctx)); + return (cb->error_code = bt_mesh_cfg_node_reset(&ctx)); case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_VIRTUAL_ADDR_SET: { struct bt_mesh_cfg_mod_pub model_pub = { - .app_idx = set_state->model_pub_va_set.publish_app_idx, - .cred_flag = set_state->model_pub_va_set.cred_flag, - .ttl = set_state->model_pub_va_set.publish_ttl, - .period = set_state->model_pub_va_set.publish_period, - .transmit = set_state->model_pub_va_set.publish_retransmit, + .app_idx = set->model_pub_va_set.publish_app_idx, + .cred_flag = set->model_pub_va_set.cred_flag, + .ttl = set->model_pub_va_set.publish_ttl, + .period = set->model_pub_va_set.publish_period, + .transmit = set->model_pub_va_set.publish_retransmit, }; - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_pub_va_set(&ctx, set_state->model_pub_va_set.element_addr, set_state->model_pub_va_set.model_id, - set_state->model_pub_va_set.company_id, set_state->model_pub_va_set.label_uuid, &model_pub)); + return (cb->error_code = + bt_mesh_cfg_mod_pub_va_set(&ctx, set->model_pub_va_set.element_addr, + set->model_pub_va_set.model_id, set->model_pub_va_set.company_id, + set->model_pub_va_set.label_uuid, &model_pub)); } case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE_ALL: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_sub_del_all(&ctx, set_state->model_sub_delete_all.element_addr, - set_state->model_sub_delete_all.model_id, set_state->model_sub_delete_all.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_sub_del_all(&ctx, set->model_sub_delete_all.element_addr, + set->model_sub_delete_all.model_id, set->model_sub_delete_all.company_id)); case ESP_BLE_MESH_MODEL_OP_NET_KEY_UPDATE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_net_key_update(&ctx, set_state->net_key_update.net_idx, set_state->net_key_update.net_key)); + return (cb->error_code = + bt_mesh_cfg_net_key_update(&ctx, set->net_key_update.net_idx, + set->net_key_update.net_key)); case ESP_BLE_MESH_MODEL_OP_NET_KEY_DELETE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_net_key_delete(&ctx, set_state->net_key_delete.net_idx)); + return (cb->error_code = + bt_mesh_cfg_net_key_delete(&ctx, set->net_key_delete.net_idx)); case ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_app_key_update(&ctx, set_state->app_key_update.net_idx, set_state->app_key_update.app_idx, - set_state->app_key_update.app_key)); + return (cb->error_code = + bt_mesh_cfg_app_key_update(&ctx, set->app_key_update.net_idx, + set->app_key_update.app_idx, set->app_key_update.app_key)); case ESP_BLE_MESH_MODEL_OP_APP_KEY_DELETE: - return (cfg_client_cb->error_code = - bt_mesh_cfg_app_key_delete(&ctx, set_state->app_key_delete.net_idx, set_state->app_key_delete.app_idx)); + return (cb->error_code = + bt_mesh_cfg_app_key_delete(&ctx, set->app_key_delete.net_idx, + set->app_key_delete.app_idx)); case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_SET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_node_identity_set(&ctx, set_state->node_identity_set.net_idx, set_state->node_identity_set.identity)); + return (cb->error_code = + bt_mesh_cfg_node_identity_set(&ctx, set->node_identity_set.net_idx, + set->node_identity_set.identity)); case ESP_BLE_MESH_MODEL_OP_MODEL_APP_UNBIND: - return (cfg_client_cb->error_code = - bt_mesh_cfg_mod_app_unbind(&ctx, set_state->model_app_unbind.element_addr, set_state->model_app_unbind.model_app_idx, - set_state->model_app_unbind.model_id, set_state->model_app_unbind.company_id)); + return (cb->error_code = + bt_mesh_cfg_mod_app_unbind(&ctx, set->model_app_unbind.element_addr, + set->model_app_unbind.model_app_idx, set->model_app_unbind.model_id, + set->model_app_unbind.company_id)); case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_SET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_kr_phase_set(&ctx, set_state->kr_phase_set.net_idx, set_state->kr_phase_set.transition)); + return (cb->error_code = + bt_mesh_cfg_kr_phase_set(&ctx, set->kr_phase_set.net_idx, + set->kr_phase_set.transition)); case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_SET: - return (cfg_client_cb->error_code = - bt_mesh_cfg_net_transmit_set(&ctx, set_state->net_transmit_set.net_transmit)); + return (cb->error_code = + bt_mesh_cfg_net_transmit_set(&ctx, set->net_transmit_set.net_transmit)); default: - BT_WARN("%s, Invalid opcode 0x%x", __func__, params->opcode); - return (cfg_client_cb->error_code = -EINVAL); + LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return (cb->error_code = -EINVAL); } return 0; } -static void btc_mesh_cfg_server_callback(esp_ble_mesh_cfg_server_cb_param_t *cb_params, uint8_t act) +void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) +{ + btc_ble_mesh_config_client_args_t *arg = NULL; + esp_ble_mesh_cfg_client_cb_param_t cb = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_config_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: { + cb.params = arg->cfg_client_get_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + LOG_ERROR("%s, Failed to set model role", __func__); + return; + } + btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params, + arg->cfg_client_get_state.get_state, + &cb); + if (cb.error_code) { + btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: { + cb.params = arg->cfg_client_set_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + LOG_ERROR("%s, Failed to set model role", __func__); + return; + } + btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params, + arg->cfg_client_set_state.set_state, + &cb); + if (cb.error_code) { + btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_config_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_cfg_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_cfg_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_CFG_CLIENT_EVT_MAX) { + btc_ble_mesh_config_client_cb_to_app(msg->act, param); + } else { + LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_config_client_free_req_data(msg); + return; +} + +/* Configuration Server Model related functions */ + +static inline void btc_ble_mesh_config_server_cb_to_app(esp_ble_mesh_cfg_server_cb_event_t event, + esp_ble_mesh_cfg_server_cb_param_t *param) +{ + esp_ble_mesh_cfg_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_cfg_server_cb_t)btc_profile_cb_get(BTC_PID_CONFIG_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_config_server_callback(esp_ble_mesh_cfg_server_cb_param_t *cb_params, uint8_t act) { btc_msg_t msg = {0}; LOG_DEBUG("%s", __func__); msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_CFG_SERVER; + msg.pid = BTC_PID_CONFIG_SERVER; msg.act = act; btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_cfg_server_cb_param_t), NULL); } -void bt_mesh_callback_cfg_server_event_to_btc(u8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_config_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len) { esp_ble_mesh_cfg_server_cb_param_t cb_params = {0}; - size_t length; + size_t length; uint8_t act; if (!model || !ctx) { @@ -682,7 +707,7 @@ void bt_mesh_callback_cfg_server_event_to_btc(u8_t evt_type, struct bt_mesh_mode } switch (evt_type) { - case 0x00: + case BTC_BLE_MESH_EVT_CONFIG_SERVER_RECV_MSG: act = ESP_BLE_MESH_CFG_SERVER_RECV_MSG_EVT; break; default: @@ -703,10 +728,11 @@ void bt_mesh_callback_cfg_server_event_to_btc(u8_t evt_type, struct bt_mesh_mode memcpy(&cb_params.status_cb, val, length); } - btc_mesh_cfg_server_callback(&cb_params, act); + btc_ble_mesh_config_server_callback(&cb_params, act); + return; } -void btc_mesh_cfg_server_cb_handler(btc_msg_t *msg) +void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg) { esp_ble_mesh_cfg_server_cb_param_t *param = NULL; @@ -718,7 +744,7 @@ void btc_mesh_cfg_server_cb_handler(btc_msg_t *msg) param = (esp_ble_mesh_cfg_server_cb_param_t *)(msg->arg); if (msg->act < ESP_BLE_MESH_CFG_SERVER_EVT_MAX) { - btc_ble_mesh_cfg_server_cb_to_app(msg->act, param); + btc_ble_mesh_config_server_cb_to_app(msg->act, param); } else { LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c index fc0854a42a..e155a49fad 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c @@ -18,16 +18,19 @@ #include "btc/btc_manage.h" #include "osi/allocator.h" -#include "cfg_cli.h" +#include "generic_client.h" #include "btc_ble_mesh_generic_model.h" #include "esp_ble_mesh_generic_model_api.h" -static inline void btc_ble_mesh_cb_to_app(esp_ble_mesh_generic_client_cb_event_t event, +/* Generic Client Models related functions */ + +static inline void btc_ble_mesh_generic_client_cb_to_app(esp_ble_mesh_generic_client_cb_event_t event, esp_ble_mesh_generic_client_cb_param_t *param) { - esp_ble_mesh_generic_client_cb_t btc_mesh_cb = (esp_ble_mesh_generic_client_cb_t)btc_profile_cb_get(BTC_PID_GENERIC_CLIENT); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); + esp_ble_mesh_generic_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_generic_client_cb_t)btc_profile_cb_get(BTC_PID_GENERIC_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); } } @@ -35,7 +38,6 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi { btc_ble_mesh_generic_client_args_t *dst = (btc_ble_mesh_generic_client_args_t *)p_dest; btc_ble_mesh_generic_client_args_t *src = (btc_ble_mesh_generic_client_args_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !dst || !src) { @@ -66,8 +68,7 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi memcpy(dst->generic_client_set_state.set_state, src->generic_client_set_state.set_state, sizeof(esp_ble_mesh_generic_client_set_state_t)); - opcode = src->generic_client_set_state.params->opcode; - switch (opcode) { + switch (src->generic_client_set_state.params->opcode) { case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: if (src->generic_client_set_state.set_state->user_property_set.property_value) { length = src->generic_client_set_state.set_state->user_property_set.property_value->len; @@ -108,11 +109,55 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi } } -static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +static void btc_ble_mesh_generic_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_generic_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_generic_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: + if (arg->generic_client_get_state.params) { + osi_free(arg->generic_client_get_state.params); + } + if (arg->generic_client_get_state.get_state) { + osi_free(arg->generic_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: + if (arg->generic_client_set_state.set_state) { + if (arg->generic_client_set_state.params) { + switch (arg->generic_client_set_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + bt_mesh_free_buf(arg->generic_client_set_state.set_state->user_property_set.property_value); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + bt_mesh_free_buf(arg->generic_client_set_state.set_state->admin_property_set.property_value); + break; + default: + break; + } + } + osi_free(arg->generic_client_set_state.set_state); + } + if (arg->generic_client_set_state.params) { + osi_free(arg->generic_client_set_state.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { esp_ble_mesh_generic_client_cb_param_t *p_dest_data = (esp_ble_mesh_generic_client_cb_param_t *)p_dest; esp_ble_mesh_generic_client_cb_param_t *p_src_data = (esp_ble_mesh_generic_client_cb_param_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !p_src_data || !p_dest_data) { @@ -125,8 +170,7 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: if (p_src_data->params) { - opcode = p_src_data->params->opcode; - switch (opcode) { + switch (p_src_data->params->opcode) { case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: if (p_src_data->status_cb.user_properties_status.property_ids) { @@ -247,10 +291,9 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src } } -static void btc_ble_mesh_free_req_data(btc_msg_t *msg) +static void btc_ble_mesh_generic_client_free_req_data(btc_msg_t *msg) { esp_ble_mesh_generic_client_cb_param_t *arg = NULL; - u32_t opcode; if (!msg || !msg->arg) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -264,8 +307,7 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: if (arg->params) { - opcode = arg->params->opcode; - switch (opcode) { + switch (arg->params->opcode) { case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: bt_mesh_free_buf(arg->status_cb.user_properties_status.property_ids); @@ -311,56 +353,7 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) } } -void btc_ble_mesh_generic_client_arg_deep_free(btc_msg_t *msg) -{ - btc_ble_mesh_generic_client_args_t *arg = NULL; - u32_t opcode = 0; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - arg = (btc_ble_mesh_generic_client_args_t *)(msg->arg); - - switch (msg->act) { - case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: - if (arg->generic_client_get_state.params) { - osi_free(arg->generic_client_get_state.params); - } - if (arg->generic_client_get_state.get_state) { - osi_free(arg->generic_client_get_state.get_state); - } - break; - case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: - if (arg->generic_client_set_state.params) { - opcode = arg->generic_client_set_state.params->opcode; - osi_free(arg->generic_client_set_state.params); - } - if (arg->generic_client_set_state.set_state) { - if (opcode) { - switch (opcode) { - case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: - bt_mesh_free_buf(arg->generic_client_set_state.set_state->user_property_set.property_value); - break; - case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: - bt_mesh_free_buf(arg->generic_client_set_state.set_state->admin_property_set.property_value); - break; - default: - break; - } - } - osi_free(arg->generic_client_set_state.set_state); - } - break; - default: - break; - } - - return; -} - -static void btc_mesh_generic_client_callback(esp_ble_mesh_generic_client_cb_param_t *cb_params, uint8_t act) +static void btc_ble_mesh_generic_client_callback(esp_ble_mesh_generic_client_cb_param_t *cb_params, uint8_t act) { btc_msg_t msg = {0}; @@ -371,10 +364,10 @@ static void btc_mesh_generic_client_callback(esp_ble_mesh_generic_client_cb_para msg.act = act; btc_transfer_context(&msg, cb_params, - sizeof(esp_ble_mesh_generic_client_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_generic_client_cb_param_t), btc_ble_mesh_generic_client_copy_req_data); } -void bt_mesh_callback_generic_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_generic_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len) @@ -390,16 +383,16 @@ void bt_mesh_callback_generic_status_to_btc(u32_t opcode, u8_t evt_type, } switch (evt_type) { - case 0x00: + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_GET_STATE: act = ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT; break; - case 0x01: + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_SET_STATE: act = ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT; break; - case 0x02: + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_PUBLISH: act = ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT; break; - case 0x03: + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT: act = ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT; break; default: @@ -424,25 +417,30 @@ void bt_mesh_callback_generic_status_to_btc(u32_t opcode, u8_t evt_type, memcpy(&cb_params.status_cb, val, length); } - btc_mesh_generic_client_callback(&cb_params, act); + btc_ble_mesh_generic_client_callback(&cb_params, act); + return; } -void btc_mesh_generic_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) +void btc_ble_mesh_generic_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { if (!model || !ctx || !buf) { LOG_ERROR("%s, Invalid parameter", __func__); return; } - bt_mesh_callback_generic_status_to_btc(opcode, 0x02, model, ctx, buf->data, buf->len); + bt_mesh_generic_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; } -void btc_mesh_generic_client_call_handler(btc_msg_t *msg) +void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) { - esp_ble_mesh_generic_client_cb_param_t generic_client_cb = {0}; esp_ble_mesh_client_common_param_t *params = NULL; btc_ble_mesh_generic_client_args_t *arg = NULL; + esp_ble_mesh_generic_client_cb_param_t cb = {0}; bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; @@ -471,15 +469,12 @@ void btc_mesh_generic_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - generic_client_cb.params = arg->generic_client_get_state.params; - generic_client_cb.error_code = - bt_mesh_generic_client_get_state(&common, - (void *)arg->generic_client_get_state.get_state, - (void *)&generic_client_cb.status_cb); - if (generic_client_cb.error_code) { + cb.params = arg->generic_client_get_state.params; + cb.error_code = bt_mesh_generic_client_get_state(&common, + (void *)arg->generic_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_generic_client_callback(&generic_client_cb, - ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT); + btc_ble_mesh_generic_client_callback(&cb, ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT); } break; } @@ -500,15 +495,12 @@ void btc_mesh_generic_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - generic_client_cb.params = arg->generic_client_set_state.params; - generic_client_cb.error_code = - bt_mesh_generic_client_set_state(&common, - (void *)arg->generic_client_set_state.set_state, - (void *)&generic_client_cb.status_cb); - if (generic_client_cb.error_code) { + cb.params = arg->generic_client_set_state.params; + cb.error_code = bt_mesh_generic_client_set_state(&common, + (void *)arg->generic_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_generic_client_callback(&generic_client_cb, - ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT); + btc_ble_mesh_generic_client_callback(&cb, ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT); } break; } @@ -517,9 +509,10 @@ void btc_mesh_generic_client_call_handler(btc_msg_t *msg) } btc_ble_mesh_generic_client_arg_deep_free(msg); + return; } -void btc_mesh_generic_client_cb_handler(btc_msg_t *msg) +void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg) { esp_ble_mesh_generic_client_cb_param_t *param = NULL; @@ -531,10 +524,11 @@ void btc_mesh_generic_client_cb_handler(btc_msg_t *msg) param = (esp_ble_mesh_generic_client_cb_param_t *)(msg->arg); if (msg->act < ESP_BLE_MESH_GENERIC_CLIENT_EVT_MAX) { - btc_ble_mesh_cb_to_app(msg->act, param); + btc_ble_mesh_generic_client_cb_to_app(msg->act, param); } else { LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); } - btc_ble_mesh_free_req_data(msg); + btc_ble_mesh_generic_client_free_req_data(msg); + return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c index f5a176af88..a38b52dab2 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c @@ -28,21 +28,15 @@ extern s32_t health_msg_timeout; +/* Health Client Model related functions */ + static inline void btc_ble_mesh_health_client_cb_to_app(esp_ble_mesh_health_client_cb_event_t event, esp_ble_mesh_health_client_cb_param_t *param) { - esp_ble_mesh_health_client_cb_t btc_mesh_cb = (esp_ble_mesh_health_client_cb_t)btc_profile_cb_get(BTC_PID_HEALTH_CLIENT); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); - } -} - -static inline void btc_ble_mesh_health_server_cb_to_app(esp_ble_mesh_health_server_cb_event_t event, - esp_ble_mesh_health_server_cb_param_t *param) -{ - esp_ble_mesh_health_server_cb_t btc_mesh_cb = (esp_ble_mesh_health_server_cb_t)btc_profile_cb_get(BTC_PID_HEALTH_SERVER); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); + esp_ble_mesh_health_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_health_client_cb_t)btc_profile_cb_get(BTC_PID_HEALTH_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); } } @@ -120,45 +114,12 @@ static void btc_ble_mesh_health_client_arg_deep_free(btc_msg_t *msg) default: break; } - - return; -} - -void btc_ble_mesh_health_server_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) -{ - if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - switch (msg->act) { - case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: - break; - default: - break; - } -} - -static void btc_ble_mesh_health_server_arg_deep_free(btc_msg_t *msg) -{ - if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - switch (msg->act) { - case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: - break; - default: - break; - } } static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { esp_ble_mesh_health_client_cb_param_t *p_dest_data = (esp_ble_mesh_health_client_cb_param_t *)p_dest; esp_ble_mesh_health_client_cb_param_t *p_src_data = (esp_ble_mesh_health_client_cb_param_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !p_src_data || !p_dest_data) { @@ -171,8 +132,7 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des case ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT: if (p_src_data->params) { - opcode = p_src_data->params->opcode; - switch (opcode) { + switch (p_src_data->params->opcode) { case OP_HEALTH_CURRENT_STATUS: if (p_src_data->status_cb.current_status.fault_array) { length = p_src_data->status_cb.current_status.fault_array->len; @@ -224,7 +184,6 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des static void btc_ble_mesh_health_client_free_req_data(btc_msg_t *msg) { esp_ble_mesh_health_client_cb_param_t *arg = NULL; - u32_t opcode; if (!msg || !msg->arg) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -238,8 +197,7 @@ static void btc_ble_mesh_health_client_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT: if (arg->params) { - opcode = arg->params->opcode; - switch (opcode) { + switch (arg->params->opcode) { case OP_HEALTH_CURRENT_STATUS: bt_mesh_free_buf(arg->status_cb.current_status.fault_array); break; @@ -263,6 +221,292 @@ static void btc_ble_mesh_health_client_free_req_data(btc_msg_t *msg) } } +static void btc_ble_mesh_health_client_callback(esp_ble_mesh_health_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + LOG_DEBUG("%s", __func__); + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_HEALTH_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_health_client_cb_param_t), btc_ble_mesh_health_client_copy_req_data); +} + +void bt_mesh_health_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, u16_t len) +{ + esp_ble_mesh_health_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + size_t length; + uint8_t act; + + if (!model || !ctx) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_GET_STATE: + act = ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_SET_STATE: + act = ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_PUBLISH: + act = ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT; + break; + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT: + act = ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT; + break; + default: + LOG_ERROR("%s, Unknown health client event type %d", __func__, evt_type); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + + cb_params.error_code = 0; + cb_params.params = ¶ms; + + if (val && len) { + length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); + memcpy(&cb_params.status_cb, val, length); + } + + btc_ble_mesh_health_client_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_health_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_health_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; +} + +static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_get_state_t *get, + esp_ble_mesh_health_client_cb_param_t *cb) +{ + struct bt_mesh_msg_ctx ctx = {0}; + + if (!params || !cb) { + LOG_ERROR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + ctx.net_idx = params->ctx.net_idx; + ctx.app_idx = params->ctx.app_idx; + ctx.addr = params->ctx.addr; + ctx.send_rel = params->ctx.send_rel; + ctx.send_ttl = params->ctx.send_ttl; + + health_msg_timeout = params->msg_timeout; + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_ATTENTION_GET: + return (cb->error_code = bt_mesh_health_attention_get(&ctx)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET: + return (cb->error_code = bt_mesh_health_period_get(&ctx)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET: + return (cb->error_code = bt_mesh_health_fault_get(&ctx, get->fault_get.company_id)); + default: + LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return (cb->error_code = -EINVAL); + } + + return 0; +} + +static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_set_state_t *set, + esp_ble_mesh_health_client_cb_param_t *cb) +{ + struct bt_mesh_msg_ctx ctx = {0}; + + if (!params || !set || !cb) { + LOG_ERROR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + ctx.net_idx = params->ctx.net_idx; + ctx.app_idx = params->ctx.app_idx; + ctx.addr = params->ctx.addr; + ctx.send_rel = params->ctx.send_rel; + ctx.send_ttl = params->ctx.send_ttl; + + health_msg_timeout = params->msg_timeout; + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_ATTENTION_SET: + return (cb->error_code = + bt_mesh_health_attention_set(&ctx, set->attention_set.attention, true)); + case ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK: + return (cb->error_code = + bt_mesh_health_attention_set(&ctx, set->attention_set.attention, false)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET: + return (cb->error_code = + bt_mesh_health_period_set(&ctx, set->period_set.fast_period_divisor, true)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK: + return (cb->error_code = + bt_mesh_health_period_set(&ctx, set->period_set.fast_period_divisor, false)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST: + return (cb->error_code = + bt_mesh_health_fault_test(&ctx, set->fault_test.company_id, set->fault_test.test_id, true)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK: + return (cb->error_code = + bt_mesh_health_fault_test(&ctx, set->fault_test.company_id, set->fault_test.test_id, false)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR: + return (cb->error_code = + bt_mesh_health_fault_clear(&ctx, set->fault_clear.company_id, true)); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK: + return (cb->error_code = + bt_mesh_health_fault_clear(&ctx, set->fault_clear.company_id, false)); + default: + LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return (cb->error_code = -EINVAL); + } + + return 0; +} + +void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) +{ + btc_ble_mesh_health_client_args_t *arg = NULL; + esp_ble_mesh_health_client_cb_param_t cb = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_health_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE: { + cb.params = arg->health_client_get_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + LOG_ERROR("%s, Failed to set model role", __func__); + return; + } + btc_ble_mesh_health_client_get_state(arg->health_client_get_state.params, + arg->health_client_get_state.get_state, &cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_health_client_callback(&cb, ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE: { + cb.params = arg->health_client_set_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + LOG_ERROR("%s, Failed to set model role", __func__); + return; + } + btc_ble_mesh_health_client_set_state(arg->health_client_set_state.params, + arg->health_client_set_state.set_state, &cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_health_client_callback(&cb, ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_health_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_health_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_health_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_HEALTH_CLIENT_EVT_MAX) { + btc_ble_mesh_health_client_cb_to_app(msg->act, param); + } else { + LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_health_client_free_req_data(msg); + return; +} + +/* Health Server Model related functions */ + +static inline void btc_ble_mesh_health_server_cb_to_app(esp_ble_mesh_health_server_cb_event_t event, + esp_ble_mesh_health_server_cb_param_t *param) +{ + esp_ble_mesh_health_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_health_server_cb_t)btc_profile_cb_get(BTC_PID_HEALTH_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_health_server_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + if (!msg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: + break; + default: + break; + } +} + +static void btc_ble_mesh_health_server_arg_deep_free(btc_msg_t *msg) +{ + if (!msg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: + break; + default: + break; + } +} + static void btc_ble_mesh_health_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { if (!msg) { @@ -293,21 +537,7 @@ static void btc_ble_mesh_health_server_free_req_data(btc_msg_t *msg) } } -static void btc_mesh_health_client_callback(esp_ble_mesh_health_client_cb_param_t *cb_params, uint8_t act) -{ - btc_msg_t msg = {0}; - - LOG_DEBUG("%s", __func__); - - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_HEALTH_CLIENT; - msg.act = act; - - btc_transfer_context(&msg, cb_params, - sizeof(esp_ble_mesh_health_client_cb_param_t), btc_ble_mesh_health_client_copy_req_data); -} - -static void btc_mesh_health_server_callback(esp_ble_mesh_health_server_cb_param_t *cb_params, uint8_t act) +static void btc_ble_mesh_health_server_callback(esp_ble_mesh_health_server_cb_param_t *cb_params, uint8_t act) { btc_msg_t msg = {0}; @@ -318,236 +548,10 @@ static void btc_mesh_health_server_callback(esp_ble_mesh_health_server_cb_param_ msg.act = act; btc_transfer_context(&msg, cb_params, - sizeof(esp_ble_mesh_health_server_cb_param_t), btc_ble_mesh_health_server_copy_req_data); + sizeof(esp_ble_mesh_health_server_cb_param_t), btc_ble_mesh_health_server_copy_req_data); } -int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params, - esp_ble_mesh_health_client_get_state_t *get_state, - esp_ble_mesh_health_client_cb_param_t *client_cb) -{ - struct bt_mesh_msg_ctx ctx = {0}; - - if (!params || !client_cb) { - LOG_ERROR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - ctx.net_idx = params->ctx.net_idx; - ctx.app_idx = params->ctx.app_idx; - ctx.addr = params->ctx.addr; - ctx.send_rel = params->ctx.send_rel; - ctx.send_ttl = params->ctx.send_ttl; - - health_msg_timeout = params->msg_timeout; - - switch (params->opcode) { - case ESP_BLE_MESH_MODEL_OP_ATTENTION_GET: - return (client_cb->error_code = bt_mesh_health_attention_get(&ctx)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET: - return (client_cb->error_code = bt_mesh_health_period_get(&ctx)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET: - return (client_cb->error_code = bt_mesh_health_fault_get(&ctx, get_state->fault_get.company_id)); - default: - BT_WARN("%s, invalid opcode 0x%x", __func__, params->opcode); - return (client_cb->error_code = -EINVAL); - } - - return 0; -} - -int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params, - esp_ble_mesh_health_client_set_state_t *set_state, - esp_ble_mesh_health_client_cb_param_t *client_cb) -{ - struct bt_mesh_msg_ctx ctx = {0}; - - if (!params || !set_state || !client_cb) { - LOG_ERROR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - ctx.net_idx = params->ctx.net_idx; - ctx.app_idx = params->ctx.app_idx; - ctx.addr = params->ctx.addr; - ctx.send_rel = params->ctx.send_rel; - ctx.send_ttl = params->ctx.send_ttl; - - health_msg_timeout = params->msg_timeout; - - switch (params->opcode) { - case ESP_BLE_MESH_MODEL_OP_ATTENTION_SET: - return (client_cb->error_code = - bt_mesh_health_attention_set(&ctx, set_state->attention_set.attention, true)); - case ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK: - return (client_cb->error_code = - bt_mesh_health_attention_set(&ctx, set_state->attention_set.attention, false)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET: - return (client_cb->error_code = - bt_mesh_health_period_set(&ctx, set_state->period_set.fast_period_divisor, true)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK: - return (client_cb->error_code = - bt_mesh_health_period_set(&ctx, set_state->period_set.fast_period_divisor, false)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST: - return (client_cb->error_code = - bt_mesh_health_fault_test(&ctx, set_state->fault_test.company_id, set_state->fault_test.test_id, true)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK: - return (client_cb->error_code = - bt_mesh_health_fault_test(&ctx, set_state->fault_test.company_id, set_state->fault_test.test_id, false)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR: - return (client_cb->error_code = - bt_mesh_health_fault_clear(&ctx, set_state->fault_clear.company_id, true)); - case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK: - return (client_cb->error_code = - bt_mesh_health_fault_clear(&ctx, set_state->fault_clear.company_id, false)); - default: - BT_WARN("%s, Invalid opcode 0x%x", __func__, params->opcode); - return (client_cb->error_code = -EINVAL); - } - - return 0; -} - -void bt_mesh_callback_health_status_to_btc(u32_t opcode, u8_t evt_type, - struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - const u8_t *val, u16_t len) -{ - esp_ble_mesh_health_client_cb_param_t cb_params = {0}; - esp_ble_mesh_client_common_param_t params = {0}; - size_t length; - uint8_t act; - - if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - switch (evt_type) { - case 0x00: - act = ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT; - break; - case 0x01: - act = ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT; - break; - case 0x02: - act = ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT; - break; - case 0x03: - act = ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT; - break; - default: - LOG_ERROR("%s, Unknown health client event type %d", __func__, evt_type); - return; - } - - params.opcode = opcode; - params.model = (esp_ble_mesh_model_t *)model; - params.ctx.net_idx = ctx->net_idx; - params.ctx.app_idx = ctx->app_idx; - params.ctx.addr = ctx->addr; - params.ctx.recv_ttl = ctx->recv_ttl; - params.ctx.recv_op = ctx->recv_op; - params.ctx.recv_dst = ctx->recv_dst; - - cb_params.error_code = 0; - cb_params.params = ¶ms; - - if (val && len) { - length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); - memcpy(&cb_params.status_cb, val, length); - } - - btc_mesh_health_client_callback(&cb_params, act); -} - -void btc_mesh_health_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) -{ - if (!model || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - bt_mesh_callback_health_status_to_btc(opcode, 0x02, model, ctx, buf->data, buf->len); -} - -void btc_mesh_health_client_call_handler(btc_msg_t *msg) -{ - btc_ble_mesh_health_client_args_t *arg = NULL; - esp_ble_mesh_health_client_cb_param_t health_client_cb = {0}; - bt_mesh_role_param_t role_param = {0}; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - arg = (btc_ble_mesh_health_client_args_t *)(msg->arg); - - switch (msg->act) { - case BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE: { - health_client_cb.params = arg->health_client_get_state.params; - role_param.model = (struct bt_mesh_model *)health_client_cb.params->model; - role_param.role = health_client_cb.params->msg_role; - if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); - return; - } - btc_ble_mesh_health_client_get_state(arg->health_client_get_state.params, - arg->health_client_get_state.get_state, - &health_client_cb); - if (health_client_cb.error_code) { - /* If send failed, callback error_code to app layer immediately */ - btc_mesh_health_client_callback(&health_client_cb, ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT); - } - break; - } - case BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE: { - health_client_cb.params = arg->health_client_set_state.params; - role_param.model = (struct bt_mesh_model *)health_client_cb.params->model; - role_param.role = health_client_cb.params->msg_role; - if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); - return; - } - btc_ble_mesh_health_client_set_state(arg->health_client_set_state.params, - arg->health_client_set_state.set_state, - &health_client_cb); - if (health_client_cb.error_code) { - /* If send failed, callback error_code to app layer immediately */ - btc_mesh_health_client_callback(&health_client_cb, ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT); - } - break; - } - default: - break; - } - - btc_ble_mesh_health_client_arg_deep_free(msg); - return; -} - -void btc_mesh_health_client_cb_handler(btc_msg_t *msg) -{ - esp_ble_mesh_health_client_cb_param_t *param = NULL; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - param = (esp_ble_mesh_health_client_cb_param_t *)(msg->arg); - - if (msg->act < ESP_BLE_MESH_HEALTH_CLIENT_EVT_MAX) { - btc_ble_mesh_health_client_cb_to_app(msg->act, param); - } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); - } - - btc_ble_mesh_health_client_free_req_data(msg); -} - -void btc_mesh_health_server_call_handler(btc_msg_t *msg) +void btc_ble_mesh_health_server_call_handler(btc_msg_t *msg) { esp_ble_mesh_health_server_cb_param_t health_server_cb = {0}; btc_ble_mesh_health_server_args_t *arg = NULL; @@ -560,18 +564,21 @@ void btc_mesh_health_server_call_handler(btc_msg_t *msg) arg = (btc_ble_mesh_health_server_args_t *)(msg->arg); switch (msg->act) { - case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: { - health_server_cb.error_code = bt_mesh_fault_update((struct bt_mesh_elem *)arg->fault_update.element); - btc_mesh_health_server_callback(&health_server_cb, ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMPLETE_EVT); - } + case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: + health_server_cb.error_code = + bt_mesh_fault_update((struct bt_mesh_elem *)arg->fault_update.element); + btc_ble_mesh_health_server_callback( + &health_server_cb, ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMPLETE_EVT); + break; default: break; } btc_ble_mesh_health_server_arg_deep_free(msg); + return; } -void btc_mesh_health_server_cb_handler(btc_msg_t *msg) +void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg) { esp_ble_mesh_health_server_cb_param_t *param = NULL; @@ -589,4 +596,5 @@ void btc_mesh_health_server_cb_handler(btc_msg_t *msg) } btc_ble_mesh_health_server_free_req_data(msg); + return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c index 9a1b57edf3..fdac1b6fe7 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c @@ -22,19 +22,22 @@ #include "btc_ble_mesh_lighting_model.h" #include "esp_ble_mesh_lighting_model_api.h" -static inline void btc_ble_mesh_cb_to_app(esp_ble_mesh_light_client_cb_event_t event, +/* Lighting Client Models related functions */ + +static inline void btc_ble_mesh_lighting_client_cb_to_app(esp_ble_mesh_light_client_cb_event_t event, esp_ble_mesh_light_client_cb_param_t *param) { - esp_ble_mesh_light_client_cb_t btc_mesh_cb = (esp_ble_mesh_light_client_cb_t)btc_profile_cb_get(BTC_PID_LIGHT_CLIENT); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); + esp_ble_mesh_light_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_light_client_cb_t)btc_profile_cb_get(BTC_PID_LIGHTING_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); } } -void btc_ble_mesh_light_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +void btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) { - btc_ble_mesh_light_client_args_t *dst = (btc_ble_mesh_light_client_args_t *)p_dest; - btc_ble_mesh_light_client_args_t *src = (btc_ble_mesh_light_client_args_t *)p_src; + btc_ble_mesh_lighting_client_args_t *dst = (btc_ble_mesh_lighting_client_args_t *)p_dest; + btc_ble_mesh_lighting_client_args_t *src = (btc_ble_mesh_lighting_client_args_t *)p_src; if (!msg || !dst || !src) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -42,7 +45,7 @@ void btc_ble_mesh_light_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void } switch (msg->act) { - case BTC_BLE_MESH_ACT_LIGHT_CLIENT_GET_STATE: { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: { dst->light_client_get_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); dst->light_client_get_state.get_state = (esp_ble_mesh_light_client_get_state_t *)osi_malloc(sizeof(esp_ble_mesh_light_client_get_state_t)); if (dst->light_client_get_state.params && dst->light_client_get_state.get_state) { @@ -55,7 +58,7 @@ void btc_ble_mesh_light_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void } break; } - case BTC_BLE_MESH_ACT_LIGHT_CLIENT_SET_STATE: { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: { dst->light_client_set_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); dst->light_client_set_state.set_state = (esp_ble_mesh_light_client_set_state_t *)osi_malloc(sizeof(esp_ble_mesh_light_client_set_state_t)); if (dst->light_client_set_state.params && dst->light_client_set_state.set_state) { @@ -74,11 +77,43 @@ void btc_ble_mesh_light_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void } } -static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +static void btc_ble_mesh_lighting_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_lighting_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_lighting_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: + if (arg->light_client_get_state.params) { + osi_free(arg->light_client_get_state.params); + } + if (arg->light_client_get_state.get_state) { + osi_free(arg->light_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: + if (arg->light_client_set_state.params) { + osi_free(arg->light_client_set_state.params); + } + if (arg->light_client_set_state.set_state) { + osi_free(arg->light_client_set_state.set_state); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { esp_ble_mesh_light_client_cb_param_t *p_dest_data = (esp_ble_mesh_light_client_cb_param_t *)p_dest; esp_ble_mesh_light_client_cb_param_t *p_src_data = (esp_ble_mesh_light_client_cb_param_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !p_src_data || !p_dest_data) { @@ -91,8 +126,7 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src case ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT: if (p_src_data->params) { - opcode = p_src_data->params->opcode; - switch (opcode) { + switch (p_src_data->params->opcode) { case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: @@ -127,10 +161,9 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src } } -static void btc_ble_mesh_free_req_data(btc_msg_t *msg) +static void btc_ble_mesh_lighting_client_free_req_data(btc_msg_t *msg) { esp_ble_mesh_light_client_cb_param_t *arg = NULL; - u32_t opcode; if (!msg || !msg->arg) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -144,8 +177,7 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT: if (arg->params) { - opcode = arg->params->opcode; - switch (opcode) { + switch (arg->params->opcode) { case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: @@ -165,56 +197,21 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) } } -void btc_ble_mesh_light_client_arg_deep_free(btc_msg_t *msg) -{ - btc_ble_mesh_light_client_args_t *arg = NULL; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - arg = (btc_ble_mesh_light_client_args_t *)(msg->arg); - - switch (msg->act) { - case BTC_BLE_MESH_ACT_LIGHT_CLIENT_GET_STATE: - if (arg->light_client_get_state.params) { - osi_free(arg->light_client_get_state.params); - } - if (arg->light_client_get_state.get_state) { - osi_free(arg->light_client_get_state.get_state); - } - break; - case BTC_BLE_MESH_ACT_LIGHT_CLIENT_SET_STATE: - if (arg->light_client_set_state.params) { - osi_free(arg->light_client_set_state.params); - } - if (arg->light_client_set_state.set_state) { - osi_free(arg->light_client_set_state.set_state); - } - break; - default: - break; - } - - return; -} - -static void btc_mesh_light_client_callback(esp_ble_mesh_light_client_cb_param_t *cb_params, uint8_t act) +static void btc_ble_mesh_lighting_client_callback(esp_ble_mesh_light_client_cb_param_t *cb_params, uint8_t act) { btc_msg_t msg = {0}; LOG_DEBUG("%s", __func__); msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_LIGHT_CLIENT; + msg.pid = BTC_PID_LIGHTING_CLIENT; msg.act = act; btc_transfer_context(&msg, cb_params, - sizeof(esp_ble_mesh_light_client_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_light_client_cb_param_t), btc_ble_mesh_lighting_client_copy_req_data); } -void bt_mesh_callback_light_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_lighting_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len) @@ -230,16 +227,16 @@ void bt_mesh_callback_light_status_to_btc(u32_t opcode, u8_t evt_type, } switch (evt_type) { - case 0x00: + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_GET_STATE: act = ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT; break; - case 0x01: + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_SET_STATE: act = ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT; break; - case 0x02: + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH: act = ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT; break; - case 0x03: + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT: act = ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT; break; default: @@ -264,25 +261,30 @@ void bt_mesh_callback_light_status_to_btc(u32_t opcode, u8_t evt_type, memcpy(&cb_params.status_cb, val, length); } - btc_mesh_light_client_callback(&cb_params, act); + btc_ble_mesh_lighting_client_callback(&cb_params, act); + return; } -void btc_mesh_light_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) +void btc_ble_mesh_lighting_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { if (!model || !ctx || !buf) { LOG_ERROR("%s, Invalid parameter", __func__); return; } - bt_mesh_callback_light_status_to_btc(opcode, 0x02, model, ctx, buf->data, buf->len); + bt_mesh_lighting_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; } -void btc_mesh_light_client_call_handler(btc_msg_t *msg) +void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) { - esp_ble_mesh_light_client_cb_param_t light_client_cb = {0}; esp_ble_mesh_client_common_param_t *params = NULL; - btc_ble_mesh_light_client_args_t *arg = NULL; + btc_ble_mesh_lighting_client_args_t *arg = NULL; + esp_ble_mesh_light_client_cb_param_t cb = {0}; bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; @@ -291,10 +293,10 @@ void btc_mesh_light_client_call_handler(btc_msg_t *msg) return; } - arg = (btc_ble_mesh_light_client_args_t *)(msg->arg); + arg = (btc_ble_mesh_lighting_client_args_t *)(msg->arg); switch (msg->act) { - case BTC_BLE_MESH_ACT_LIGHT_CLIENT_GET_STATE: { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: { params = arg->light_client_get_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; @@ -311,19 +313,16 @@ void btc_mesh_light_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - light_client_cb.params = arg->light_client_get_state.params; - light_client_cb.error_code = - bt_mesh_light_client_get_state(&common, - (void *)arg->light_client_get_state.get_state, - (void *)&light_client_cb.status_cb); - if (light_client_cb.error_code) { + cb.params = arg->light_client_get_state.params; + cb.error_code = bt_mesh_light_client_get_state(&common, + (void *)arg->light_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_light_client_callback(&light_client_cb, - ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT); + btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT); } break; } - case BTC_BLE_MESH_ACT_LIGHT_CLIENT_SET_STATE: { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: { params = arg->light_client_set_state.params; role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; @@ -340,15 +339,12 @@ void btc_mesh_light_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - light_client_cb.params = arg->light_client_set_state.params; - light_client_cb.error_code = - bt_mesh_light_client_set_state(&common, - (void *)arg->light_client_set_state.set_state, - (void *)&light_client_cb.status_cb); - if (light_client_cb.error_code) { + cb.params = arg->light_client_set_state.params; + cb.error_code = bt_mesh_light_client_set_state(&common, + (void *)arg->light_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_light_client_callback(&light_client_cb, - ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT); + btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT); } break; } @@ -356,10 +352,11 @@ void btc_mesh_light_client_call_handler(btc_msg_t *msg) break; } - btc_ble_mesh_light_client_arg_deep_free(msg); + btc_ble_mesh_lighting_client_arg_deep_free(msg); + return; } -void btc_mesh_light_client_cb_handler(btc_msg_t *msg) +void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg) { esp_ble_mesh_light_client_cb_param_t *param = NULL; @@ -371,11 +368,12 @@ void btc_mesh_light_client_cb_handler(btc_msg_t *msg) param = (esp_ble_mesh_light_client_cb_param_t *)(msg->arg); if (msg->act < ESP_BLE_MESH_LIGHT_CLIENT_EVT_MAX) { - btc_ble_mesh_cb_to_app(msg->act, param); + btc_ble_mesh_lighting_client_cb_to_app(msg->act, param); } else { LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); } - btc_ble_mesh_free_req_data(msg); + btc_ble_mesh_lighting_client_free_req_data(msg); + return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index 4d11458dc3..31639aff43 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -59,7 +59,25 @@ #include "esp_ble_mesh_provisioning_api.h" #include "esp_ble_mesh_networking_api.h" -#define BLE_MESH_MAX_DATA_SIZE 379 +static inline void btc_ble_mesh_prov_cb_to_app(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param) +{ + esp_ble_mesh_prov_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_prov_cb_t)btc_profile_cb_get(BTC_PID_PROV); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static inline void btc_ble_mesh_model_cb_to_app(esp_ble_mesh_model_cb_event_t event, + esp_ble_mesh_model_cb_param_t *param) +{ + esp_ble_mesh_model_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_model_cb_t)btc_profile_cb_get(BTC_PID_MODEL); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) { @@ -97,7 +115,7 @@ void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) } } -void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg) +static void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg) { btc_ble_mesh_model_args_t *arg = NULL; @@ -125,7 +143,7 @@ void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg) return; } -static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { esp_ble_mesh_model_cb_param_t *p_dest_data = (esp_ble_mesh_model_cb_param_t *)p_dest; esp_ble_mesh_model_cb_param_t *p_src_data = (esp_ble_mesh_model_cb_param_t *)p_src; @@ -201,7 +219,7 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src } } -static void btc_ble_mesh_free_req_data(btc_msg_t *msg) +static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg) { esp_ble_mesh_model_cb_param_t *arg = NULL; @@ -248,24 +266,6 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) } } -static inline void btc_ble_mesh_cb_to_app(esp_ble_mesh_prov_cb_event_t event, - esp_ble_mesh_prov_cb_param_t *param) -{ - esp_ble_mesh_prov_cb_t btc_mesh_cb = (esp_ble_mesh_prov_cb_t)btc_profile_cb_get(BTC_PID_PROV); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); - } -} - -static inline void btc_ble_mesh_model_cb_to_app(esp_ble_mesh_model_cb_event_t event, - esp_ble_mesh_model_cb_param_t *param) -{ - esp_ble_mesh_model_cb_t btc_mesh_cb = (esp_ble_mesh_model_cb_t)btc_profile_cb_get(BTC_PID_MODEL); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); - } -} - extern u32_t mesh_opcode; static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model, @@ -286,7 +286,7 @@ static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model, msg.pid = BTC_PID_MODEL; msg.act = ESP_BLE_MESH_MODEL_OPERATION_EVT; ret = btc_transfer_context(&msg, &mesh_param, - sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_model_copy_req_data); if (ret != BT_STATUS_SUCCESS) { LOG_ERROR("%s btc_transfer_context failed", __func__); @@ -341,7 +341,7 @@ static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, bt_mesh_client_free_node(&data->queue, node); } ret = btc_transfer_context(&msg, &mesh_param, - sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_model_copy_req_data); if (ret != BT_STATUS_SUCCESS) { LOG_ERROR("%s, btc_transfer_context failed", __func__); @@ -364,7 +364,7 @@ static void btc_ble_mesh_model_send_comp_cb(esp_ble_mesh_model_t *model, esp_ble msg.pid = BTC_PID_MODEL; msg.act = ESP_BLE_MESH_MODEL_SEND_COMP_EVT; ret = btc_transfer_context(&msg, &mesh_param, - sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_model_copy_req_data); if (ret != BT_STATUS_SUCCESS) { LOG_ERROR("%s btc_transfer_context failed", __func__); @@ -393,8 +393,8 @@ static void btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t *model, int return; } -#if defined(CONFIG_BLE_MESH_NODE) -static void btc_oob_pub_key_cb(void) +#if CONFIG_BLE_MESH_NODE +static void btc_ble_mesh_oob_pub_key_cb(void) { btc_msg_t msg = {0}; @@ -410,7 +410,7 @@ static void btc_oob_pub_key_cb(void) return; } -static int btc_output_number_cb(bt_mesh_output_action_t act, u32_t num) +static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, u32_t num) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -434,7 +434,7 @@ static int btc_output_number_cb(bt_mesh_output_action_t act, u32_t num) return 0; } -static int btc_output_string_cb(const char *str) +static int btc_ble_mesh_output_string_cb(const char *str) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -457,7 +457,7 @@ static int btc_output_string_cb(const char *str) return 0; } -static int btc_input_cb(bt_mesh_input_action_t act, u8_t size) +static int btc_ble_mesh_input_cb(bt_mesh_input_action_t act, u8_t size) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -481,7 +481,7 @@ static int btc_input_cb(bt_mesh_input_action_t act, u8_t size) return 0; } -static void btc_link_open_cb(bt_mesh_prov_bearer_t bearer) +static void btc_ble_mesh_link_open_cb(bt_mesh_prov_bearer_t bearer) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -503,7 +503,7 @@ static void btc_link_open_cb(bt_mesh_prov_bearer_t bearer) return; } -static void btc_link_close_cb(bt_mesh_prov_bearer_t bearer) +static void btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -525,7 +525,7 @@ static void btc_link_close_cb(bt_mesh_prov_bearer_t bearer) return; } -static void btc_complete_cb(u16_t net_idx, u16_t addr, u8_t flags, u32_t iv_index) +static void btc_ble_mesh_complete_cb(u16_t net_idx, u16_t addr, u8_t flags, u32_t iv_index) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -550,7 +550,7 @@ static void btc_complete_cb(u16_t net_idx, u16_t addr, u8_t flags, u32_t iv_inde return; } -static void btc_reset_cb(void) +static void btc_ble_mesh_reset_cb(void) { btc_msg_t msg = {0}; bt_status_t ret; @@ -567,9 +567,9 @@ static void btc_reset_cb(void) } return; } -#endif /* defined(CONFIG_BLE_MESH_NODE) */ +#endif /* CONFIG_BLE_MESH_NODE */ -static void btc_prov_register_complete_cb(int err_code) +static void btc_ble_mesh_prov_register_complete_cb(int err_code) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -591,7 +591,7 @@ static void btc_prov_register_complete_cb(int err_code) return; } -static void btc_client_model_timeout_cb(struct k_work *work) +static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work) { esp_ble_mesh_model_cb_param_t mesh_param = {0}; bt_mesh_client_user_data_t *client_param = NULL; @@ -621,7 +621,7 @@ static void btc_client_model_timeout_cb(struct k_work *work) msg.pid = BTC_PID_MODEL; msg.act = ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT; ret = btc_transfer_context(&msg, &mesh_param, - sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_model_copy_req_data); if (ret != BT_STATUS_SUCCESS) { LOG_ERROR("%s btc_transfer_context failed", __func__); @@ -631,7 +631,7 @@ static void btc_client_model_timeout_cb(struct k_work *work) return; } -static int btc_model_publish_update(struct bt_mesh_model *mod) +static int btc_ble_mesh_model_publish_update(struct bt_mesh_model *mod) { esp_ble_mesh_model_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -654,7 +654,7 @@ static int btc_model_publish_update(struct bt_mesh_model *mod) return 0; } -static void btc_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_t act) +static void btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_t act) { btc_msg_t msg = {0}; bt_status_t ret; @@ -673,8 +673,9 @@ static void btc_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_ return; } -#if (CONFIG_BLE_MESH_PROVISIONER) -static void btc_provisioner_recv_unprov_adv_pkt_cb(const u8_t addr[6], const u8_t addr_type, +#if CONFIG_BLE_MESH_PROVISIONER +static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb( + const u8_t addr[6], const u8_t addr_type, const u8_t adv_type, const u8_t dev_uuid[16], u16_t oob_info, bt_mesh_prov_bearer_t bearer) { @@ -709,7 +710,7 @@ static void btc_provisioner_recv_unprov_adv_pkt_cb(const u8_t addr[6], const u8_ return; } -static int btc_provisioner_prov_read_oob_pub_key_cb(u8_t link_idx) +static int btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(u8_t link_idx) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -732,7 +733,8 @@ static int btc_provisioner_prov_read_oob_pub_key_cb(u8_t link_idx) return 0; } -static int btc_provisioner_prov_input_cb(u8_t method, bt_mesh_output_action_t act, u8_t size, u8_t link_idx) +static int btc_ble_mesh_provisioner_prov_input_cb(u8_t method, + bt_mesh_output_action_t act, u8_t size, u8_t link_idx) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -758,7 +760,8 @@ static int btc_provisioner_prov_input_cb(u8_t method, bt_mesh_output_action_t ac return 0; } -static int btc_provisioner_prov_output_cb(u8_t method, bt_mesh_input_action_t act, void *data, u8_t size, u8_t link_idx) +static int btc_ble_mesh_provisioner_prov_output_cb(u8_t method, + bt_mesh_input_action_t act, void *data, u8_t size, u8_t link_idx) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -789,7 +792,7 @@ static int btc_provisioner_prov_output_cb(u8_t method, bt_mesh_input_action_t ac return 0; } -static void btc_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer) +static void btc_ble_mesh_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -811,7 +814,7 @@ static void btc_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer) return; } -static void btc_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, u8_t reason) +static void btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, u8_t reason) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; btc_msg_t msg = {0}; @@ -834,7 +837,8 @@ static void btc_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, u8_t rea return; } -static void btc_provisioner_prov_complete_cb(int node_idx, const u8_t device_uuid[16], +static void btc_ble_mesh_provisioner_prov_complete_cb( + int node_idx, const u8_t device_uuid[16], u16_t unicast_addr, u8_t element_num, u16_t netkey_idx) { @@ -863,7 +867,7 @@ static void btc_provisioner_prov_complete_cb(int node_idx, const u8_t device_uui } #endif /* CONFIG_BLE_MESH_PROVISIONER */ -int btc_ble_mesh_client_init(esp_ble_mesh_model_t *model) +int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) { __ASSERT(model && model->op, "%s, Invalid parameter", __func__); esp_ble_mesh_model_op_t *op = model->op; @@ -943,7 +947,7 @@ extern const struct bt_mesh_model_op time_cli_op[]; extern const struct bt_mesh_model_op scene_cli_op[]; extern const struct bt_mesh_model_op scheduler_cli_op[]; -static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) +static void btc_ble_mesh_model_op_add(esp_ble_mesh_model_t *model) { esp_ble_mesh_model_op_t *op = NULL; @@ -968,7 +972,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_cfg_cli_op; bt_mesh_config_client_t *cli = (bt_mesh_config_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_cfg_client_publish_callback; + cli->publish_status = btc_ble_mesh_config_client_publish_callback; } break; } @@ -980,7 +984,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_health_cli_op; bt_mesh_health_client_t *cli = (bt_mesh_health_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_health_publish_callback; + cli->publish_status = btc_ble_mesh_health_publish_callback; } break; } @@ -988,7 +992,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_onoff_cli_op); bt_mesh_gen_onoff_client_t *cli = (bt_mesh_gen_onoff_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -996,7 +1000,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_level_cli_op); bt_mesh_gen_level_client_t *cli = (bt_mesh_gen_level_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -1004,7 +1008,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_def_trans_time_cli_op); bt_mesh_gen_def_trans_time_client_t *cli = (bt_mesh_gen_def_trans_time_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -1012,7 +1016,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_power_onoff_cli_op); bt_mesh_gen_power_onoff_client_t *cli = (bt_mesh_gen_power_onoff_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -1020,7 +1024,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_power_level_cli_op); bt_mesh_gen_power_level_client_t *cli = (bt_mesh_gen_power_level_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -1028,7 +1032,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_battery_cli_op); bt_mesh_gen_battery_client_t *cli = (bt_mesh_gen_battery_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -1036,7 +1040,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_location_cli_op); bt_mesh_gen_location_client_t *cli = (bt_mesh_gen_location_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -1044,7 +1048,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)gen_property_cli_op); bt_mesh_gen_property_client_t *cli = (bt_mesh_gen_property_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_generic_client_publish_callback; + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } @@ -1052,7 +1056,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)light_lightness_cli_op); bt_mesh_light_lightness_client_t *cli = (bt_mesh_light_lightness_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_light_client_publish_callback; + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; } @@ -1060,7 +1064,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)light_ctl_cli_op); bt_mesh_light_ctl_client_t *cli = (bt_mesh_light_ctl_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_light_client_publish_callback; + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; } @@ -1068,7 +1072,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)light_hsl_cli_op); bt_mesh_light_hsl_client_t *cli = (bt_mesh_light_hsl_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_light_client_publish_callback; + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; } @@ -1076,7 +1080,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)light_xyl_cli_op); bt_mesh_light_xyl_client_t *cli = (bt_mesh_light_xyl_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_light_client_publish_callback; + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; } @@ -1084,7 +1088,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)light_lc_cli_op); bt_mesh_light_lc_client_t *cli = (bt_mesh_light_lc_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_light_client_publish_callback; + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; } @@ -1092,7 +1096,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)sensor_cli_op); bt_mesh_sensor_client_t *cli = (bt_mesh_sensor_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_sensor_client_publish_callback; + cli->publish_status = btc_ble_mesh_sensor_client_publish_callback; } break; } @@ -1100,7 +1104,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)time_cli_op); bt_mesh_time_client_t *cli = (bt_mesh_time_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_time_scene_client_publish_callback; + cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; } break; } @@ -1108,7 +1112,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)scene_cli_op); bt_mesh_scene_client_t *cli = (bt_mesh_scene_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_time_scene_client_publish_callback; + cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; } break; } @@ -1116,7 +1120,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = ((esp_ble_mesh_model_op_t *)scheduler_cli_op); bt_mesh_scheduler_client_t *cli = (bt_mesh_scheduler_client_t *)model->user_data; if (cli != NULL) { - cli->publish_status = btc_mesh_time_scene_client_publish_callback; + cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; } break; } @@ -1128,7 +1132,7 @@ static void btc_mesh_model_op_add(esp_ble_mesh_model_t *model) add_model_op: if (model->pub) { - model->pub->update = (esp_ble_mesh_cb_t)btc_model_publish_update; + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; } op = model->op; while (op != NULL && op->opcode != 0) { @@ -1138,7 +1142,7 @@ add_model_op: return; } -void btc_mesh_prov_call_handler(btc_msg_t *msg) +void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) { esp_ble_mesh_prov_cb_param_t param = {0}; btc_ble_mesh_prov_args_t *arg = NULL; @@ -1161,10 +1165,10 @@ void btc_mesh_prov_call_handler(btc_msg_t *msg) /* The opcode of sig model should be 1 or 2 bytes. */ if (sig_model && sig_model->op && (sig_model->op->opcode >= 0x10000)) { err_code = -EINVAL; - btc_prov_register_complete_cb(err_code); + btc_ble_mesh_prov_register_complete_cb(err_code); return; } - btc_mesh_model_op_add(sig_model); + btc_ble_mesh_model_op_add(sig_model); } /* For vendor models */ for (int k = 0; k < elem->vnd_model_count; k++) { @@ -1172,36 +1176,36 @@ void btc_mesh_prov_call_handler(btc_msg_t *msg) /* The opcode of vendor model should be 3 bytes. */ if (vnd_model && vnd_model->op && vnd_model->op->opcode < 0x10000) { err_code = -EINVAL; - btc_prov_register_complete_cb(err_code); + btc_ble_mesh_prov_register_complete_cb(err_code); return; } - btc_mesh_model_op_add(vnd_model); + btc_ble_mesh_model_op_add(vnd_model); } } #if CONFIG_BLE_MESH_NODE - arg->mesh_init.prov->oob_pub_key_cb = (esp_ble_mesh_cb_t)btc_oob_pub_key_cb; - arg->mesh_init.prov->output_num_cb = (esp_ble_mesh_cb_t)btc_output_number_cb; - arg->mesh_init.prov->output_str_cb = (esp_ble_mesh_cb_t)btc_output_string_cb; - arg->mesh_init.prov->input_cb = (esp_ble_mesh_cb_t)btc_input_cb; - arg->mesh_init.prov->link_open_cb = (esp_ble_mesh_cb_t)btc_link_open_cb; - arg->mesh_init.prov->link_close_cb = (esp_ble_mesh_cb_t)btc_link_close_cb; - arg->mesh_init.prov->complete_cb = (esp_ble_mesh_cb_t)btc_complete_cb; - arg->mesh_init.prov->reset_cb = (esp_ble_mesh_cb_t)btc_reset_cb; + arg->mesh_init.prov->oob_pub_key_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_oob_pub_key_cb; + arg->mesh_init.prov->output_num_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_output_number_cb; + arg->mesh_init.prov->output_str_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_output_string_cb; + arg->mesh_init.prov->input_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_input_cb; + arg->mesh_init.prov->link_open_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_link_open_cb; + arg->mesh_init.prov->link_close_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_link_close_cb; + arg->mesh_init.prov->complete_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_complete_cb; + arg->mesh_init.prov->reset_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_reset_cb; #endif /* CONFIG_BLE_MESH_NODE */ -#if (CONFIG_BLE_MESH_PROVISIONER) - arg->mesh_init.prov->provisioner_prov_read_oob_pub_key = (esp_ble_mesh_cb_t)btc_provisioner_prov_read_oob_pub_key_cb; - arg->mesh_init.prov->provisioner_prov_input = (esp_ble_mesh_cb_t)btc_provisioner_prov_input_cb; - arg->mesh_init.prov->provisioner_prov_output = (esp_ble_mesh_cb_t)btc_provisioner_prov_output_cb; - arg->mesh_init.prov->provisioner_link_open = (esp_ble_mesh_cb_t)btc_provisioner_link_open_cb; - arg->mesh_init.prov->provisioner_link_close = (esp_ble_mesh_cb_t)btc_provisioner_link_close_cb; - arg->mesh_init.prov->provisioner_prov_comp = (esp_ble_mesh_cb_t)btc_provisioner_prov_complete_cb; - bt_mesh_prov_adv_pkt_cb_register(btc_provisioner_recv_unprov_adv_pkt_cb); +#if CONFIG_BLE_MESH_PROVISIONER + arg->mesh_init.prov->provisioner_prov_read_oob_pub_key = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb; + arg->mesh_init.prov->provisioner_prov_input = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_input_cb; + arg->mesh_init.prov->provisioner_prov_output = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_output_cb; + arg->mesh_init.prov->provisioner_link_open = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_open_cb; + arg->mesh_init.prov->provisioner_link_close = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_close_cb; + arg->mesh_init.prov->provisioner_prov_comp = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_complete_cb; + bt_mesh_prov_adv_pkt_cb_register(btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb); #endif /* CONFIG_BLE_MESH_PROVISIONER */ err_code = bt_mesh_init((struct bt_mesh_prov *)arg->mesh_init.prov, (struct bt_mesh_comp *)arg->mesh_init.comp); /* Give the semaphore when BLE Mesh initialization is finished. */ xSemaphoreGive(arg->mesh_init.semaphore); - btc_prov_register_complete_cb(err_code); + btc_ble_mesh_prov_register_complete_cb(err_code); return; } #if CONFIG_BLE_MESH_NODE @@ -1253,7 +1257,7 @@ void btc_mesh_prov_call_handler(btc_msg_t *msg) break; #endif /* CONFIG_BLE_MESH_GATT_PROXY */ #endif /* CONFIG_BLE_MESH_NODE */ -#if (CONFIG_BLE_MESH_PROVISIONER) +#if CONFIG_BLE_MESH_PROVISIONER case BTC_BLE_MESH_ACT_PROVISIONER_READ_OOB_PUB_KEY: act = ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT; param.provisioner_prov_read_oob_pub_key_comp.err_code = @@ -1382,7 +1386,7 @@ void btc_mesh_prov_call_handler(btc_msg_t *msg) break; } #endif /* CONFIG_BLE_MESH_PROVISIONER */ -#if (CONFIG_BLE_MESH_FAST_PROV) +#if CONFIG_BLE_MESH_FAST_PROV case BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO: act = ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT; param.set_fast_prov_info_comp.status_unicast = @@ -1402,19 +1406,38 @@ void btc_mesh_prov_call_handler(btc_msg_t *msg) param.set_fast_prov_action_comp.status_action = bt_mesh_set_fast_prov_action(arg->set_fast_prov_action.action); break; -#endif /* (CONFIG_BLE_MESH_FAST_PROV) */ +#endif /* CONFIG_BLE_MESH_FAST_PROV */ default: LOG_WARN("%s, Invalid msg->act %d", __func__, msg->act); return; } - btc_prov_set_complete_cb(¶m, act); + btc_ble_mesh_prov_set_complete_cb(¶m, act); return; } -void btc_mesh_model_call_handler(btc_msg_t *msg) +void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_prov_cb_param_t *param = NULL; + + if (!msg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_prov_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_PROV_EVT_MAX) { + btc_ble_mesh_prov_cb_to_app(msg->act, param); + } else { + LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + } +} + +void btc_ble_mesh_model_call_handler(btc_msg_t *msg) { btc_ble_mesh_model_args_t *arg = NULL; + int err; if (!msg || !msg->arg) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -1434,7 +1457,7 @@ void btc_mesh_model_call_handler(btc_msg_t *msg) return; } } - int err = bt_mesh_model_publish((struct bt_mesh_model *)arg->model_publish.model); + err = bt_mesh_model_publish((struct bt_mesh_model *)arg->model_publish.model); btc_ble_mesh_model_publish_comp_cb(arg->model_publish.model, err); break; } @@ -1447,9 +1470,9 @@ void btc_mesh_model_call_handler(btc_msg_t *msg) } net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); arg->model_send.ctx->srv_send = true; - int err = bt_mesh_model_send((struct bt_mesh_model *)arg->model_send.model, - (struct bt_mesh_msg_ctx *)arg->model_send.ctx, - buf, NULL, NULL); + err = bt_mesh_model_send((struct bt_mesh_model *)arg->model_send.model, + (struct bt_mesh_msg_ctx *)arg->model_send.ctx, + buf, NULL, NULL); bt_mesh_free_buf(buf); btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx, arg->model_send.opcode, err); @@ -1471,11 +1494,11 @@ void btc_mesh_model_call_handler(btc_msg_t *msg) LOG_ERROR("%s, Failed to set model role", __func__); return; } - int err = bt_mesh_client_send_msg((struct bt_mesh_model *)arg->model_send.model, - arg->model_send.opcode, - (struct bt_mesh_msg_ctx *)arg->model_send.ctx, buf, - btc_client_model_timeout_cb, arg->model_send.msg_timeout, - arg->model_send.need_rsp, NULL, NULL); + err = bt_mesh_client_send_msg((struct bt_mesh_model *)arg->model_send.model, + arg->model_send.opcode, + (struct bt_mesh_msg_ctx *)arg->model_send.ctx, buf, + btc_ble_mesh_client_model_timeout_cb, arg->model_send.msg_timeout, + arg->model_send.need_rsp, NULL, NULL); bt_mesh_free_buf(buf); btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx, arg->model_send.opcode, err); @@ -1487,9 +1510,10 @@ void btc_mesh_model_call_handler(btc_msg_t *msg) } btc_ble_mesh_prov_arg_deep_free(msg); + return; } -void btc_mesh_model_cb_handler(btc_msg_t *msg) +void btc_ble_mesh_model_cb_handler(btc_msg_t *msg) { esp_ble_mesh_model_cb_param_t *param = NULL; @@ -1506,23 +1530,6 @@ void btc_mesh_model_cb_handler(btc_msg_t *msg) LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); } - btc_ble_mesh_free_req_data(msg); -} - -void btc_mesh_prov_cb_handler(btc_msg_t *msg) -{ - esp_ble_mesh_prov_cb_param_t *param = NULL; - - if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - param = (esp_ble_mesh_prov_cb_param_t *)(msg->arg); - - if (msg->act < ESP_BLE_MESH_PROV_EVT_MAX) { - btc_ble_mesh_cb_to_app(msg->act, param); - } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); - } + btc_ble_mesh_model_free_req_data(msg); + return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c index df267023a7..0eff3e9f4a 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c @@ -22,12 +22,15 @@ #include "btc_ble_mesh_sensor_model.h" #include "esp_ble_mesh_sensor_model_api.h" -static inline void btc_ble_mesh_cb_to_app(esp_ble_mesh_sensor_client_cb_event_t event, +/* Sensor Client Models related functions */ + +static inline void btc_ble_mesh_sensor_client_cb_to_app(esp_ble_mesh_sensor_client_cb_event_t event, esp_ble_mesh_sensor_client_cb_param_t *param) { - esp_ble_mesh_sensor_client_cb_t btc_mesh_cb = (esp_ble_mesh_sensor_client_cb_t)btc_profile_cb_get(BTC_PID_SENSOR_CLIENT); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); + esp_ble_mesh_sensor_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_sensor_client_cb_t)btc_profile_cb_get(BTC_PID_SENSOR_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); } } @@ -35,7 +38,6 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void { btc_ble_mesh_sensor_client_args_t *dst = (btc_ble_mesh_sensor_client_args_t *)p_dest; btc_ble_mesh_sensor_client_args_t *src = (btc_ble_mesh_sensor_client_args_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !dst || !src) { @@ -53,8 +55,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void memcpy(dst->sensor_client_get_state.get_state, src->sensor_client_get_state.get_state, sizeof(esp_ble_mesh_sensor_client_get_state_t)); - opcode = src->sensor_client_get_state.params->opcode; - switch (opcode) { + switch (src->sensor_client_get_state.params->opcode) { case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: if (src->sensor_client_get_state.get_state->column_get.raw_value_x) { length = src->sensor_client_get_state.get_state->column_get.raw_value_x->len; @@ -109,8 +110,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void memcpy(dst->sensor_client_set_state.set_state, src->sensor_client_set_state.set_state, sizeof(esp_ble_mesh_sensor_client_set_state_t)); - opcode = src->sensor_client_set_state.params->opcode; - switch (opcode) { + switch (src->sensor_client_set_state.params->opcode) { case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: if (src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down) { length = src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down->len; @@ -184,11 +184,71 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void } } -static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +void btc_ble_mesh_sensor_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_sensor_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_sensor_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: + if (arg->sensor_client_get_state.get_state) { + if (arg->sensor_client_get_state.params) { + switch (arg->sensor_client_get_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: + bt_mesh_free_buf(arg->sensor_client_get_state.get_state->column_get.raw_value_x); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: + bt_mesh_free_buf(arg->sensor_client_get_state.get_state->series_get.raw_value_x1); + bt_mesh_free_buf(arg->sensor_client_get_state.get_state->series_get.raw_value_x2); + break; + default: + break; + } + } + osi_free(arg->sensor_client_get_state.get_state); + } + if (arg->sensor_client_get_state.params) { + osi_free(arg->sensor_client_get_state.params); + } + break; + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: + if (arg->sensor_client_set_state.set_state) { + if (arg->sensor_client_set_state.params) { + switch (arg->sensor_client_set_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down); + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up); + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.fast_cadence_low); + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.fast_cadence_high); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->setting_set.sensor_setting_raw); + break; + default: + break; + } + } + osi_free(arg->sensor_client_set_state.set_state); + } + if (arg->sensor_client_set_state.params) { + osi_free(arg->sensor_client_set_state.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { esp_ble_mesh_sensor_client_cb_param_t *p_dest_data = (esp_ble_mesh_sensor_client_cb_param_t *)p_dest; esp_ble_mesh_sensor_client_cb_param_t *p_src_data = (esp_ble_mesh_sensor_client_cb_param_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !p_src_data || !p_dest_data) { @@ -201,8 +261,7 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src case ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT: if (p_src_data->params) { - opcode = p_src_data->params->opcode; - switch (opcode) { + switch (p_src_data->params->opcode) { case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: if (p_src_data->status_cb.descriptor_status.descriptor) { @@ -322,10 +381,9 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src } } -static void btc_ble_mesh_free_req_data(btc_msg_t *msg) +static void btc_ble_mesh_sensor_client_free_req_data(btc_msg_t *msg) { esp_ble_mesh_sensor_client_cb_param_t *arg = NULL; - u32_t opcode; if (!msg || !msg->arg) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -339,8 +397,7 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT: if (arg->params) { - opcode = arg->params->opcode; - switch (opcode) { + switch (arg->params->opcode) { case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: bt_mesh_free_buf(arg->status_cb.descriptor_status.descriptor); @@ -385,73 +442,7 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) } } -void btc_ble_mesh_sensor_client_arg_deep_free(btc_msg_t *msg) -{ - btc_ble_mesh_sensor_client_args_t *arg = NULL; - u32_t opcode = 0; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - arg = (btc_ble_mesh_sensor_client_args_t *)(msg->arg); - - switch (msg->act) { - case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: - if (arg->sensor_client_get_state.params) { - opcode = arg->sensor_client_get_state.params->opcode; - osi_free(arg->sensor_client_get_state.params); - } - if (arg->sensor_client_get_state.get_state) { - if (opcode) { - switch (opcode) { - case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: - bt_mesh_free_buf(arg->sensor_client_get_state.get_state->column_get.raw_value_x); - break; - case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: - bt_mesh_free_buf(arg->sensor_client_get_state.get_state->series_get.raw_value_x1); - bt_mesh_free_buf(arg->sensor_client_get_state.get_state->series_get.raw_value_x2); - break; - default: - break; - } - } - osi_free(arg->sensor_client_get_state.get_state); - } - break; - case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: - if (arg->sensor_client_set_state.params) { - opcode = arg->sensor_client_set_state.params->opcode; - osi_free(arg->sensor_client_set_state.params); - } - if (arg->sensor_client_set_state.set_state) { - if (opcode) { - switch (opcode) { - case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: - bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down); - bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up); - bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.fast_cadence_low); - bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.fast_cadence_high); - break; - case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: - bt_mesh_free_buf(arg->sensor_client_set_state.set_state->setting_set.sensor_setting_raw); - break; - default: - break; - } - } - osi_free(arg->sensor_client_set_state.set_state); - } - break; - default: - break; - } - - return; -} - -static void btc_mesh_sensor_client_callback(esp_ble_mesh_sensor_client_cb_param_t *cb_params, uint8_t act) +static void btc_ble_mesh_sensor_client_callback(esp_ble_mesh_sensor_client_cb_param_t *cb_params, uint8_t act) { btc_msg_t msg = {0}; @@ -462,10 +453,10 @@ static void btc_mesh_sensor_client_callback(esp_ble_mesh_sensor_client_cb_param_ msg.act = act; btc_transfer_context(&msg, cb_params, - sizeof(esp_ble_mesh_sensor_client_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_sensor_client_cb_param_t), btc_ble_mesh_sensor_client_copy_req_data); } -void bt_mesh_callback_sensor_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_sensor_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len) @@ -481,16 +472,16 @@ void bt_mesh_callback_sensor_status_to_btc(u32_t opcode, u8_t evt_type, } switch (evt_type) { - case 0x00: + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_GET_STATE: act = ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT; break; - case 0x01: + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_SET_STATE: act = ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT; break; - case 0x02: + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_PUBLISH: act = ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT; break; - case 0x03: + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT: act = ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT; break; default: @@ -515,25 +506,30 @@ void bt_mesh_callback_sensor_status_to_btc(u32_t opcode, u8_t evt_type, memcpy(&cb_params.status_cb, val, length); } - btc_mesh_sensor_client_callback(&cb_params, act); + btc_ble_mesh_sensor_client_callback(&cb_params, act); + return; } -void btc_mesh_sensor_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) +void btc_ble_mesh_sensor_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { if (!model || !ctx || !buf) { LOG_ERROR("%s, Invalid parameter", __func__); return; } - bt_mesh_callback_sensor_status_to_btc(opcode, 0x02, model, ctx, buf->data, buf->len); + bt_mesh_sensor_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; } -void btc_mesh_sensor_client_call_handler(btc_msg_t *msg) +void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) { - esp_ble_mesh_sensor_client_cb_param_t sensor_client_cb = {0}; esp_ble_mesh_client_common_param_t *params = NULL; btc_ble_mesh_sensor_client_args_t *arg = NULL; + esp_ble_mesh_sensor_client_cb_param_t cb = {0}; bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; @@ -562,15 +558,12 @@ void btc_mesh_sensor_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - sensor_client_cb.params = arg->sensor_client_get_state.params; - sensor_client_cb.error_code = - bt_mesh_sensor_client_get_state(&common, - (void *)arg->sensor_client_get_state.get_state, - (void *)&sensor_client_cb.status_cb); - if (sensor_client_cb.error_code) { + cb.params = arg->sensor_client_get_state.params; + cb.error_code = bt_mesh_sensor_client_get_state(&common, + (void *)arg->sensor_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_sensor_client_callback(&sensor_client_cb, - ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT); + btc_ble_mesh_sensor_client_callback(&cb, ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT); } break; } @@ -591,15 +584,12 @@ void btc_mesh_sensor_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - sensor_client_cb.params = arg->sensor_client_set_state.params; - sensor_client_cb.error_code = - bt_mesh_sensor_client_set_state(&common, - (void *)arg->sensor_client_set_state.set_state, - (void *)&sensor_client_cb.status_cb); - if (sensor_client_cb.error_code) { + cb.params = arg->sensor_client_set_state.params; + cb.error_code = bt_mesh_sensor_client_set_state(&common, + (void *)arg->sensor_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_sensor_client_callback(&sensor_client_cb, - ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT); + btc_ble_mesh_sensor_client_callback(&cb, ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT); } break; } @@ -608,9 +598,10 @@ void btc_mesh_sensor_client_call_handler(btc_msg_t *msg) } btc_ble_mesh_sensor_client_arg_deep_free(msg); + return; } -void btc_mesh_sensor_client_cb_handler(btc_msg_t *msg) +void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg) { esp_ble_mesh_sensor_client_cb_param_t *param = NULL; @@ -622,11 +613,12 @@ void btc_mesh_sensor_client_cb_handler(btc_msg_t *msg) param = (esp_ble_mesh_sensor_client_cb_param_t *)(msg->arg); if (msg->act < ESP_BLE_MESH_SENSOR_CLIENT_EVT_MAX) { - btc_ble_mesh_cb_to_app(msg->act, param); + btc_ble_mesh_sensor_client_cb_to_app(msg->act, param); } else { LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); } - btc_ble_mesh_free_req_data(msg); + btc_ble_mesh_sensor_client_free_req_data(msg); + return; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c index 907c1430ff..4691d3f423 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c @@ -22,12 +22,15 @@ #include "btc_ble_mesh_time_scene_model.h" #include "esp_ble_mesh_time_scene_model_api.h" -static inline void btc_ble_mesh_cb_to_app(esp_ble_mesh_time_scene_client_cb_event_t event, +/* Time and Scenes Client Models related functions */ + +static inline void btc_ble_mesh_time_scene_client_cb_to_app(esp_ble_mesh_time_scene_client_cb_event_t event, esp_ble_mesh_time_scene_client_cb_param_t *param) { - esp_ble_mesh_time_scene_client_cb_t btc_mesh_cb = (esp_ble_mesh_time_scene_client_cb_t)btc_profile_cb_get(BTC_PID_TIME_SCENE_CLIENT); - if (btc_mesh_cb) { - btc_mesh_cb(event, param); + esp_ble_mesh_time_scene_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_time_scene_client_cb_t)btc_profile_cb_get(BTC_PID_TIME_SCENE_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); } } @@ -74,11 +77,43 @@ void btc_ble_mesh_time_scene_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, } } -static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +void btc_ble_mesh_time_scene_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_time_scene_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + LOG_ERROR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_time_scene_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: + if (arg->time_scene_client_get_state.params) { + osi_free(arg->time_scene_client_get_state.params); + } + if (arg->time_scene_client_get_state.get_state) { + osi_free(arg->time_scene_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: + if (arg->time_scene_client_set_state.params) { + osi_free(arg->time_scene_client_set_state.params); + } + if (arg->time_scene_client_set_state.set_state) { + osi_free(arg->time_scene_client_set_state.set_state); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_time_scene_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { esp_ble_mesh_time_scene_client_cb_param_t *p_dest_data = (esp_ble_mesh_time_scene_client_cb_param_t *)p_dest; esp_ble_mesh_time_scene_client_cb_param_t *p_src_data = (esp_ble_mesh_time_scene_client_cb_param_t *)p_src; - u32_t opcode; u16_t length; if (!msg || !p_src_data || !p_dest_data) { @@ -91,8 +126,7 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src case ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_TIME_SCENE_CLIENT_PUBLISH_EVT: if (p_src_data->params) { - opcode = p_src_data->params->opcode; - switch (opcode) { + switch (p_src_data->params->opcode) { case ESP_BLE_MESH_MODEL_OP_SCENE_STORE: case ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: case ESP_BLE_MESH_MODEL_OP_SCENE_DELETE: @@ -128,10 +162,9 @@ static void btc_ble_mesh_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src } } -static void btc_ble_mesh_free_req_data(btc_msg_t *msg) +static void btc_ble_mesh_time_scene_client_free_req_data(btc_msg_t *msg) { esp_ble_mesh_time_scene_client_cb_param_t *arg = NULL; - u32_t opcode; if (!msg || !msg->arg) { LOG_ERROR("%s, Invalid parameter", __func__); @@ -145,8 +178,7 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT: case ESP_BLE_MESH_TIME_SCENE_CLIENT_PUBLISH_EVT: if (arg->params) { - opcode = arg->params->opcode; - switch (opcode) { + switch (arg->params->opcode) { case ESP_BLE_MESH_MODEL_OP_SCENE_STORE: case ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: case ESP_BLE_MESH_MODEL_OP_SCENE_DELETE: @@ -167,42 +199,7 @@ static void btc_ble_mesh_free_req_data(btc_msg_t *msg) } } -void btc_ble_mesh_time_scene_client_arg_deep_free(btc_msg_t *msg) -{ - btc_ble_mesh_time_scene_client_args_t *arg = NULL; - - if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); - return; - } - - arg = (btc_ble_mesh_time_scene_client_args_t *)(msg->arg); - - switch (msg->act) { - case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: - if (arg->time_scene_client_get_state.params) { - osi_free(arg->time_scene_client_get_state.params); - } - if (arg->time_scene_client_get_state.get_state) { - osi_free(arg->time_scene_client_get_state.get_state); - } - break; - case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: - if (arg->time_scene_client_set_state.params) { - osi_free(arg->time_scene_client_set_state.params); - } - if (arg->time_scene_client_set_state.set_state) { - osi_free(arg->time_scene_client_set_state.set_state); - } - break; - default: - break; - } - - return; -} - -static void btc_mesh_time_scene_client_callback(esp_ble_mesh_time_scene_client_cb_param_t *cb_params, uint8_t act) +static void btc_ble_mesh_time_scene_client_callback(esp_ble_mesh_time_scene_client_cb_param_t *cb_params, uint8_t act) { btc_msg_t msg = {0}; @@ -213,10 +210,10 @@ static void btc_mesh_time_scene_client_callback(esp_ble_mesh_time_scene_client_c msg.act = act; btc_transfer_context(&msg, cb_params, - sizeof(esp_ble_mesh_time_scene_client_cb_param_t), btc_ble_mesh_copy_req_data); + sizeof(esp_ble_mesh_time_scene_client_cb_param_t), btc_ble_mesh_time_scene_client_copy_req_data); } -void bt_mesh_callback_time_scene_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_time_scene_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len) @@ -232,16 +229,16 @@ void bt_mesh_callback_time_scene_status_to_btc(u32_t opcode, u8_t evt_type, } switch (evt_type) { - case 0x00: + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_GET_STATE: act = ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT; break; - case 0x01: + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_SET_STATE: act = ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT; break; - case 0x02: + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_PUBLISH: act = ESP_BLE_MESH_TIME_SCENE_CLIENT_PUBLISH_EVT; break; - case 0x03: + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT: act = ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT; break; default: @@ -266,25 +263,30 @@ void bt_mesh_callback_time_scene_status_to_btc(u32_t opcode, u8_t evt_type, memcpy(&cb_params.status_cb, val, length); } - btc_mesh_time_scene_client_callback(&cb_params, act); + btc_ble_mesh_time_scene_client_callback(&cb_params, act); + return; } -void btc_mesh_time_scene_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) +void btc_ble_mesh_time_scene_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { if (!model || !ctx || !buf) { LOG_ERROR("%s, Invalid parameter", __func__); return; } - bt_mesh_callback_time_scene_status_to_btc(opcode, 0x02, model, ctx, buf->data, buf->len); + bt_mesh_time_scene_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; } -void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg) +void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) { - esp_ble_mesh_time_scene_client_cb_param_t time_scene_client_cb = {0}; btc_ble_mesh_time_scene_client_args_t *arg = NULL; esp_ble_mesh_client_common_param_t *params = NULL; + esp_ble_mesh_time_scene_client_cb_param_t cb = {0}; bt_mesh_client_common_param_t common = {0}; bt_mesh_role_param_t role_param = {0}; @@ -313,15 +315,12 @@ void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - time_scene_client_cb.params = arg->time_scene_client_get_state.params; - time_scene_client_cb.error_code = - bt_mesh_time_scene_client_get_state(&common, - (void *)arg->time_scene_client_get_state.get_state, - (void *)&time_scene_client_cb.status_cb); - if (time_scene_client_cb.error_code) { + cb.params = arg->time_scene_client_get_state.params; + cb.error_code = bt_mesh_time_scene_client_get_state(&common, + (void *)arg->time_scene_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_time_scene_client_callback(&time_scene_client_cb, - ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT); + btc_ble_mesh_time_scene_client_callback(&cb, ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT); } break; } @@ -342,15 +341,12 @@ void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg) common.ctx.send_ttl = params->ctx.send_ttl; common.msg_timeout = params->msg_timeout; - time_scene_client_cb.params = arg->time_scene_client_set_state.params; - time_scene_client_cb.error_code = - bt_mesh_time_scene_client_set_state(&common, - (void *)arg->time_scene_client_set_state.set_state, - (void *)&time_scene_client_cb.status_cb); - if (time_scene_client_cb.error_code) { + cb.params = arg->time_scene_client_set_state.params; + cb.error_code = bt_mesh_time_scene_client_set_state(&common, + (void *)arg->time_scene_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ - btc_mesh_time_scene_client_callback(&time_scene_client_cb, - ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT); + btc_ble_mesh_time_scene_client_callback(&cb, ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT); } break; } @@ -359,9 +355,10 @@ void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg) } btc_ble_mesh_time_scene_client_arg_deep_free(msg); + return; } -void btc_mesh_time_scene_client_cb_handler(btc_msg_t *msg) +void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg) { esp_ble_mesh_time_scene_client_cb_param_t *param = NULL; @@ -373,11 +370,12 @@ void btc_mesh_time_scene_client_cb_handler(btc_msg_t *msg) param = (esp_ble_mesh_time_scene_client_cb_param_t *)(msg->arg); if (msg->act < ESP_BLE_MESH_TIME_SCENE_CLIENT_EVT_MAX) { - btc_ble_mesh_cb_to_app(msg->act, param); + btc_ble_mesh_time_scene_client_cb_to_app(msg->act, param); } else { LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); } - btc_ble_mesh_free_req_data(msg); + btc_ble_mesh_time_scene_client_free_req_data(msg); + return; } diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h index 87889923cc..dc81116efe 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h @@ -22,42 +22,53 @@ typedef enum { BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE, BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE, -} btc_ble_mesh_cfg_client_act_t; + BTC_BLE_MESH_ACT_CONFIG_CLIENT_MAX, +} btc_ble_mesh_config_client_act_t; typedef union { - struct ble_mesh_clg_client_get_state_reg_args { + struct ble_mesh_cfg_client_get_state_reg_args { esp_ble_mesh_client_common_param_t *params; esp_ble_mesh_cfg_client_get_state_t *get_state; } cfg_client_get_state; - struct ble_mesh_clg_client_set_state_reg_args { + struct ble_mesh_cfg_client_set_state_reg_args { esp_ble_mesh_client_common_param_t *params; esp_ble_mesh_cfg_client_set_state_t *set_state; } cfg_client_set_state; -} btc_ble_mesh_cfg_client_args_t; +} btc_ble_mesh_config_client_args_t; -void btc_mesh_cfg_client_call_handler(btc_msg_t *msg); +typedef enum { + BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_MAX, +} btc_ble_mesh_config_client_evt_t; -void btc_mesh_cfg_client_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg); -void btc_ble_mesh_cfg_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); +void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg); -int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_cfg_client_get_state_t *get_state, - esp_ble_mesh_cfg_client_cb_param_t *cfg_client_cb); +void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_cfg_client_set_state_t *set_state, - esp_ble_mesh_cfg_client_cb_param_t *cfg_client_cb); +void btc_ble_mesh_config_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); -void btc_mesh_cfg_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); - -void bt_mesh_callback_config_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_config_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len); -void btc_mesh_cfg_server_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg); -void bt_mesh_callback_cfg_server_event_to_btc(u8_t evt_type, struct bt_mesh_model *model, +typedef enum { + BTC_BLE_MESH_EVT_CONFIG_SERVER_RECV_MSG, + BTC_BLE_MESH_EVT_CONFIG_SERVER_MAX, +} btc_ble_mesh_config_server_evt_t; + +void bt_mesh_config_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h index 21a159f20a..a60f4a2b05 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h @@ -22,6 +22,7 @@ typedef enum { BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE, BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_GENERIC_CLIENT_MAX, } btc_ble_mesh_generic_client_act_t; typedef union { @@ -35,16 +36,26 @@ typedef union { } generic_client_set_state; } btc_ble_mesh_generic_client_args_t; -void btc_mesh_generic_client_call_handler(btc_msg_t *msg); +typedef enum { + BTC_BLE_MESH_EVT_GENERIC_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_MAX, +} btc_ble_mesh_generic_client_evt_t; -void btc_mesh_generic_client_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg); void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -void btc_mesh_generic_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); +void btc_ble_mesh_generic_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); -void bt_mesh_callback_generic_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_generic_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h index a00efb0c1e..d124c7d8eb 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h @@ -25,11 +25,6 @@ typedef enum { BTC_BLE_MESH_ACT_HEALTH_CLIENT_MAX, } btc_ble_mesh_health_client_act_t; -typedef enum { - BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE, - BTC_BLE_MESH_ACT_HEALTH_SERVER_MAX, -} btc_ble_mesh_health_server_act_t; - typedef union { struct ble_mesh_health_client_get_state_reg_args { esp_ble_mesh_client_common_param_t *params; @@ -41,38 +36,45 @@ typedef union { } health_client_set_state; } btc_ble_mesh_health_client_args_t; +typedef enum { + BTC_BLE_MESH_EVT_HEALTH_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_MAX, +} btc_ble_mesh_health_client_evt_t; + +void btc_ble_mesh_health_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_health_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_health_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, u16_t len); + +typedef enum { + BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE, + BTC_BLE_MESH_ACT_HEALTH_SERVER_MAX, +} btc_ble_mesh_health_server_act_t; + typedef union { struct ble_mesh_health_server_fault_update_args { esp_ble_mesh_elem_t *element; } fault_update; } btc_ble_mesh_health_server_args_t; -void btc_mesh_health_client_call_handler(btc_msg_t *msg); +void btc_ble_mesh_health_server_call_handler(btc_msg_t *msg); -void btc_mesh_health_client_cb_handler(btc_msg_t *msg); - -void btc_mesh_health_server_call_handler(btc_msg_t *msg); - -void btc_mesh_health_server_cb_handler(btc_msg_t *msg); - -void btc_ble_mesh_health_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); +void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg); void btc_ble_mesh_health_server_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params, - esp_ble_mesh_health_client_get_state_t *get_state, - esp_ble_mesh_health_client_cb_param_t *client_cb); - -int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params, - esp_ble_mesh_health_client_set_state_t *set_state, - esp_ble_mesh_health_client_cb_param_t *client_cb); - -void btc_mesh_health_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); - -void bt_mesh_callback_health_status_to_btc(u32_t opcode, u8_t evt_type, - struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - const u8_t *val, u16_t len); - #endif /* _BTC_BLE_MESH_HEALTH_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h index 98353c6acd..2bb33e1b39 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h @@ -20,9 +20,10 @@ #include "esp_ble_mesh_lighting_model_api.h" typedef enum { - BTC_BLE_MESH_ACT_LIGHT_CLIENT_GET_STATE, - BTC_BLE_MESH_ACT_LIGHT_CLIENT_SET_STATE, -} btc_ble_mesh_light_client_act_t; + BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_LIGHTING_CLIENT_MAX, +} btc_ble_mesh_lighting_client_act_t; typedef union { struct ble_mesh_light_client_get_state_reg_args { @@ -33,18 +34,28 @@ typedef union { esp_ble_mesh_client_common_param_t *params; esp_ble_mesh_light_client_set_state_t *set_state; } light_client_set_state; -} btc_ble_mesh_light_client_args_t; +} btc_ble_mesh_lighting_client_args_t; -void btc_mesh_light_client_call_handler(btc_msg_t *msg); +typedef enum { + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_MAX, +} btc_ble_mesh_lighting_client_evt_t; -void btc_mesh_light_client_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg); -void btc_ble_mesh_light_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); +void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg); -void btc_mesh_light_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); +void btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -void bt_mesh_callback_light_status_to_btc(u32_t opcode, u8_t evt_type, +void btc_ble_mesh_lighting_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_lighting_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h index ff0f4154fd..f51e71428f 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h @@ -61,7 +61,7 @@ typedef enum { typedef enum { BTC_BLE_MESH_ACT_MODEL_PUBLISH, BTC_BLE_MESH_ACT_SERVER_MODEL_SEND, - BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND + BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND, } btc_ble_mesh_model_act_t; typedef union { @@ -176,11 +176,9 @@ typedef union { } model_send; } btc_ble_mesh_model_args_t; -void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg); - void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -int btc_ble_mesh_client_init(esp_ble_mesh_model_t *model); +int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model); int32_t btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t *mod); @@ -200,11 +198,10 @@ esp_ble_mesh_model_t *btc_ble_mesh_model_find(const esp_ble_mesh_elem_t *elem, const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void); -void btc_mesh_model_call_handler(btc_msg_t *msg); -void btc_mesh_model_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_model_call_handler(btc_msg_t *msg); +void btc_ble_mesh_model_cb_handler(btc_msg_t *msg); -void btc_mesh_prov_call_handler(btc_msg_t *msg); - -void btc_mesh_prov_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_prov_call_handler(btc_msg_t *msg); +void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg); #endif /* _BTC_BLE_MESH_PROV_H_ */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h index e94ccdc4b0..ea8cbbe220 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h @@ -22,6 +22,7 @@ typedef enum { BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE, BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_SENSOR_CLIENT_MAX, } btc_ble_mesh_sensor_client_act_t; typedef union { @@ -35,16 +36,26 @@ typedef union { } sensor_client_set_state; } btc_ble_mesh_sensor_client_args_t; -void btc_mesh_sensor_client_call_handler(btc_msg_t *msg); +typedef enum { + BTC_BLE_MESH_EVT_SENSOR_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_MAX, +} btc_ble_mesh_sensor_client_evt_t; -void btc_mesh_sensor_client_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg); void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -void btc_mesh_sensor_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); +void btc_ble_mesh_sensor_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); -void bt_mesh_callback_sensor_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_sensor_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h index 0ee1d244c8..74fe902daa 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h @@ -22,6 +22,7 @@ typedef enum { BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE, BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_MAX, } btc_ble_mesh_time_scene_client_act_t; typedef union { @@ -35,16 +36,26 @@ typedef union { } time_scene_client_set_state; } btc_ble_mesh_time_scene_client_args_t; -void btc_mesh_time_scene_client_call_handler(btc_msg_t *msg); +typedef enum { + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_MAX, +} btc_ble_mesh_time_scene_client_evt_t; -void btc_mesh_time_scene_client_cb_handler(btc_msg_t *msg); +void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg); void btc_ble_mesh_time_scene_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -void btc_mesh_time_scene_client_publish_callback(u32_t opcode, struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); +void btc_ble_mesh_time_scene_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); -void bt_mesh_callback_time_scene_status_to_btc(u32_t opcode, u8_t evt_type, +void bt_mesh_time_scene_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const u8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c index 7e2f717b43..3165ccb3f0 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c @@ -108,8 +108,8 @@ static void timeout_handler(struct k_work *work) return; } - bt_mesh_callback_config_status_to_btc(node->opcode, 0x03, node->ctx.model, - &node->ctx, NULL, 0); + bt_mesh_config_client_cb_evt_to_btc(node->opcode, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); bt_mesh_client_free_node(&internal->queue, node); @@ -163,7 +163,7 @@ static void cfg_client_cancel(struct bt_mesh_model *model, case OP_HEARTBEAT_SUB_GET: case OP_LPN_TIMEOUT_GET: case OP_NET_TRANSMIT_GET: - evt_type = 0x00; + evt_type = BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE; break; case OP_BEACON_SET: case OP_DEFAULT_TTL_SET: @@ -193,14 +193,14 @@ static void cfg_client_cancel(struct bt_mesh_model *model, case OP_HEARTBEAT_PUB_SET: case OP_HEARTBEAT_SUB_SET: case OP_NET_TRANSMIT_SET: - evt_type = 0x01; + evt_type = BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE; break; default: break; } - bt_mesh_callback_config_status_to_btc(node->opcode, evt_type, model, - ctx, (const u8_t *)status, len); + bt_mesh_config_client_cb_evt_to_btc( + node->opcode, evt_type, model, ctx, (const u8_t *)status, len); // Don't forget to release the node at the end. bt_mesh_client_free_node(&data->queue, node); } diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c index 6154ee36c7..c906e7c01a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -522,8 +522,8 @@ static void app_key_add(struct bt_mesh_model *model, } #if defined(CONFIG_BLE_MESH_FAST_PROV) - bt_mesh_callback_cfg_server_event_to_btc(0x0, model, ctx, - (u8_t *)&key_app_idx, sizeof(u16_t)); + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_RECV_MSG, + model, ctx, (u8_t *)&key_app_idx, sizeof(u16_t)); #endif } diff --git a/components/bt/esp_ble_mesh/mesh_core/health_cli.c b/components/bt/esp_ble_mesh/mesh_core/health_cli.c index 05e47cd295..1e13befc64 100644 --- a/components/bt/esp_ble_mesh/mesh_core/health_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/health_cli.c @@ -64,8 +64,8 @@ static void timeout_handler(struct k_work *work) return; } - bt_mesh_callback_health_status_to_btc(node->opcode, 0x03, node->ctx.model, - &node->ctx, NULL, 0); + bt_mesh_health_client_cb_evt_to_btc(node->opcode, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); bt_mesh_client_free_node(&internal->queue, node); @@ -103,20 +103,20 @@ static void health_client_cancel(struct bt_mesh_model *model, case OP_HEALTH_FAULT_GET: case OP_HEALTH_PERIOD_GET: case OP_ATTENTION_GET: - evt_type = 0x00; + evt_type = BTC_BLE_MESH_EVT_HEALTH_CLIENT_GET_STATE; break; case OP_HEALTH_FAULT_CLEAR: case OP_HEALTH_FAULT_TEST: case OP_HEALTH_PERIOD_SET: case OP_ATTENTION_SET: - evt_type = 0x01; + evt_type = BTC_BLE_MESH_EVT_HEALTH_CLIENT_SET_STATE; break; default: break; } - bt_mesh_callback_health_status_to_btc(node->opcode, evt_type, model, - ctx, (const u8_t *)status, len); + bt_mesh_health_client_cb_evt_to_btc( + node->opcode, evt_type, model, ctx, (const u8_t *)status, len); // Don't forget to release the node at the end. bt_mesh_client_free_node(&data->queue, node); } diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h index bdabab15ca..48b9a188b1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h @@ -88,7 +88,7 @@ struct bt_mesh_elem { #define BLE_MESH_MODEL_ID_GEN_BATTERY_SRV 0x100c #define BLE_MESH_MODEL_ID_GEN_BATTERY_CLI 0x100d #define BLE_MESH_MODEL_ID_GEN_LOCATION_SRV 0x100e -#define BLE_MESH_MODEL_ID_GEN_LOCATION_SETUPSRV 0x100f +#define BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV 0x100f #define BLE_MESH_MODEL_ID_GEN_LOCATION_CLI 0x1010 #define BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV 0x1011 #define BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012 @@ -123,7 +123,7 @@ struct bt_mesh_elem { #define BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV 0x130d #define BLE_MESH_MODEL_ID_LIGHT_XYL_CLI 0x130e #define BLE_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f -#define BLE_MESH_MODEL_ID_LIGHT_LC_SETUPSRV 0x1310 +#define BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV 0x1310 #define BLE_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311 /** Message sending context. */ diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 6606142c2b..adf63e5073 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -59,10 +59,6 @@ #if CONFIG_BLE_MESH_SETTINGS -#define GET_ELEMENT_IDX(x) ((u8_t)((x) >> 8)) -#define GET_MODEL_IDX(x) ((u8_t)(x)) -#define GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | b)) - /* Tracking of what storage changes are pending for App and Net Keys. We * track this in a separate array here instead of within the respective * bt_mesh_app_key and bt_mesh_subnet structs themselves, since once a key @@ -653,8 +649,8 @@ static int model_set(bool vnd, const char *name) for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { u16_t model_key = net_buf_simple_pull_le16(buf); - elem_idx = GET_ELEMENT_IDX(model_key); - model_idx = GET_MODEL_IDX(model_key); + elem_idx = BLE_MESH_GET_ELEM_IDX(model_key); + model_idx = BLE_MESH_GET_MODEL_IDX(model_key); model = bt_mesh_model_get(vnd, elem_idx, model_idx); if (!model) { @@ -1196,7 +1192,7 @@ static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd) u16_t model_key; int err; - model_key = GET_MODEL_KEY(model->elem_idx, model->model_idx); + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key); @@ -1226,7 +1222,7 @@ static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd) u16_t model_key; int err; - model_key = GET_MODEL_KEY(model->elem_idx, model->model_idx); + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key); @@ -1270,7 +1266,7 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) pub.period_div = model->pub->period_div; pub.cred = model->pub->cred; - model_key = GET_MODEL_KEY(model->elem_idx, model->model_idx); + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); diff --git a/components/bt/esp_ble_mesh/mesh_core/settings/settings_nvs.h b/components/bt/esp_ble_mesh/mesh_core/settings/settings_nvs.h index a4216fadca..c4d5ad064f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings/settings_nvs.h +++ b/components/bt/esp_ble_mesh/mesh_core/settings/settings_nvs.h @@ -23,7 +23,11 @@ extern "C" { #endif -#define SETTINGS_ITEM_SIZE sizeof(u16_t) +#define SETTINGS_ITEM_SIZE sizeof(u16_t) + +#define BLE_MESH_GET_ELEM_IDX(x) ((u8_t)((x) >> 8)) +#define BLE_MESH_GET_MODEL_IDX(x) ((u8_t)(x)) +#define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | b)) void bt_mesh_settings_foreach(void); diff --git a/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c index 32860a603d..f821fb3d93 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c @@ -145,8 +145,8 @@ static void timeout_handler(struct k_work *work) return; } - bt_mesh_callback_generic_status_to_btc(node->opcode, 0x03, node->ctx.model, - &node->ctx, NULL, 0); + bt_mesh_generic_client_cb_evt_to_btc(node->opcode, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); bt_mesh_client_free_node(&internal->queue, node); @@ -558,7 +558,7 @@ static void generic_status(struct bt_mesh_model *model, case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET: case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: - evt = 0x00; + evt = BTC_BLE_MESH_EVT_GENERIC_CLIENT_GET_STATE; break; case BLE_MESH_MODEL_OP_GEN_ONOFF_SET: case BLE_MESH_MODEL_OP_GEN_LEVEL_SET: @@ -574,13 +574,13 @@ static void generic_status(struct bt_mesh_model *model, case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET: - evt = 0x01; + evt = BTC_BLE_MESH_EVT_GENERIC_CLIENT_SET_STATE; break; default: break; } - bt_mesh_callback_generic_status_to_btc(node->opcode, evt, model, ctx, val, len); + bt_mesh_generic_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); // Don't forget to release the node at the end. bt_mesh_client_free_node(&internal->queue, node); } @@ -781,8 +781,8 @@ static int gen_set_state(bt_mesh_client_common_param_t *common, case BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK: { struct bt_mesh_gen_delta_set *set; set = (struct bt_mesh_gen_delta_set *)value; - net_buf_simple_add_le32(msg, set->level); - net_buf_simple_add_u8(msg, set->tid); + net_buf_simple_add_le32(msg, set->delta_level); + net_buf_simple_add_u8(msg, set->tid); if (set->op_en) { net_buf_simple_add_u8(msg, set->trans_time); net_buf_simple_add_u8(msg, set->delay); diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h index e697e25716..a5c72b638d 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h @@ -97,11 +97,11 @@ struct bt_mesh_gen_level_set { }; struct bt_mesh_gen_delta_set { - bool op_en; /* Indicate whether optional parameters included */ - s32_t level; /* Delta change of Generic Level state */ - u8_t tid; /* Transaction Identifier */ - u8_t trans_time; /* Time to complete state transition (optional) */ - u8_t delay; /* Indicate message execution delay (C.1) */ + bool op_en; /* Indicate whether optional parameters included */ + s32_t delta_level; /* Delta change of Generic Level state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ }; struct bt_mesh_gen_move_set { diff --git a/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c index d71f3ccce5..b7b9a0413b 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c @@ -154,8 +154,8 @@ static void timeout_handler(struct k_work *work) return; } - bt_mesh_callback_light_status_to_btc(node->opcode, 0x03, node->ctx.model, - &node->ctx, NULL, 0); + bt_mesh_lighting_client_cb_evt_to_btc(node->opcode, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); bt_mesh_client_free_node(&internal->queue, node); @@ -678,7 +678,7 @@ static void light_status(struct bt_mesh_model *model, case BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET: case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET: case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: - evt = 0x00; + evt = BTC_BLE_MESH_EVT_LIGHTING_CLIENT_GET_STATE; break; case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET: case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET: @@ -700,13 +700,13 @@ static void light_status(struct bt_mesh_model *model, case BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET: case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET: case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: - evt = 0x01; + evt = BTC_BLE_MESH_EVT_LIGHTING_CLIENT_SET_STATE; break; default: break; } - bt_mesh_callback_light_status_to_btc(node->opcode, evt, model, ctx, val, len); + bt_mesh_lighting_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); // Don't forget to release the node at the end. bt_mesh_client_free_node(&internal->queue, node); } diff --git a/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c index 2187f18551..3f95168c3a 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c @@ -83,8 +83,8 @@ static void timeout_handler(struct k_work *work) return; } - bt_mesh_callback_sensor_status_to_btc(node->opcode, 0x03, node->ctx.model, - &node->ctx, NULL, 0); + bt_mesh_sensor_client_cb_evt_to_btc(node->opcode, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); bt_mesh_client_free_node(&internal->queue, node); @@ -274,17 +274,17 @@ static void sensor_status(struct bt_mesh_model *model, case BLE_MESH_MODEL_OP_SENSOR_GET: case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: - evt = 0x00; + evt = BTC_BLE_MESH_EVT_SENSOR_CLIENT_GET_STATE; break; case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: - evt = 0x01; + evt = BTC_BLE_MESH_EVT_SENSOR_CLIENT_SET_STATE; break; default: break; } - bt_mesh_callback_sensor_status_to_btc(node->opcode, evt, model, ctx, val, len); + bt_mesh_sensor_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); // Don't forget to release the node at the end. bt_mesh_client_free_node(&internal->queue, node); } diff --git a/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c index 3632c653b0..275b29cc6b 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c @@ -99,8 +99,8 @@ static void timeout_handler(struct k_work *work) return; } - bt_mesh_callback_time_scene_status_to_btc(node->opcode, 0x03, node->ctx.model, - &node->ctx, NULL, 0); + bt_mesh_time_scene_client_cb_evt_to_btc(node->opcode, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); bt_mesh_client_free_node(&internal->queue, node); @@ -312,7 +312,7 @@ static void time_scene_status(struct bt_mesh_model *model, case BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: case BLE_MESH_MODEL_OP_SCHEDULER_GET: case BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET: - evt = 0x00; + evt = BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_GET_STATE; break; case BLE_MESH_MODEL_OP_TIME_SET: case BLE_MESH_MODEL_OP_TIME_ZONE_SET: @@ -322,13 +322,13 @@ static void time_scene_status(struct bt_mesh_model *model, case BLE_MESH_MODEL_OP_SCENE_RECALL: case BLE_MESH_MODEL_OP_SCENE_DELETE: case BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET: - evt = 0x01; + evt = BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_SET_STATE; break; default: break; } - bt_mesh_callback_time_scene_status_to_btc(node->opcode, evt, model, ctx, val, len); + bt_mesh_time_scene_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); // Don't forget to release the node at the end. bt_mesh_client_free_node(&internal->queue, node); } From b8cb769ce352e7135cb1be51315eac49d1b92786 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 13:08:05 +0800 Subject: [PATCH 100/146] ble_mesh: fix failed to set device role caused mem leak --- .../bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c | 4 ++-- .../bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c | 4 ++-- .../bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c | 4 ++-- .../bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c | 4 ++-- components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 8 ++++---- .../bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c | 4 ++-- .../bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c | 4 ++-- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c index bfab0a9e1c..7d4d865b3f 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c @@ -612,7 +612,7 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params, arg->cfg_client_get_state.get_state, @@ -628,7 +628,7 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params, arg->cfg_client_set_state.set_state, diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c index e155a49fad..f13b86bb6f 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c @@ -458,7 +458,7 @@ void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; @@ -484,7 +484,7 @@ void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c index a38b52dab2..5e1e01e958 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c @@ -410,7 +410,7 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } btc_ble_mesh_health_client_get_state(arg->health_client_get_state.params, arg->health_client_get_state.get_state, &cb); @@ -426,7 +426,7 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } btc_ble_mesh_health_client_set_state(arg->health_client_set_state.params, arg->health_client_set_state.set_state, &cb); diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c index fdac1b6fe7..fdb5d2e49a 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c @@ -302,7 +302,7 @@ void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; @@ -328,7 +328,7 @@ void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index 31639aff43..b0619d7090 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -1454,7 +1454,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) common.role = arg->model_publish.device_role; if (bt_mesh_set_client_model_role(&common)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } } err = bt_mesh_model_publish((struct bt_mesh_model *)arg->model_publish.model); @@ -1466,7 +1466,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + 8); if (!buf) { LOG_ERROR("%s, Failed to allocate memory", __func__); - return; + break; } net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); arg->model_send.ctx->srv_send = true; @@ -1484,7 +1484,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + 8); if (!buf) { LOG_ERROR("%s, Failed to allocate memory", __func__); - return; + break; } net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); arg->model_send.ctx->srv_send = false; @@ -1492,7 +1492,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) common.role = arg->model_send.device_role; if (bt_mesh_set_client_model_role(&common)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } err = bt_mesh_client_send_msg((struct bt_mesh_model *)arg->model_send.model, arg->model_send.opcode, diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c index 0eff3e9f4a..f3766f1d46 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c @@ -547,7 +547,7 @@ void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; @@ -573,7 +573,7 @@ void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c index 4691d3f423..c87b67237a 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c @@ -304,7 +304,7 @@ void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; @@ -330,7 +330,7 @@ void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { LOG_ERROR("%s, Failed to set model role", __func__); - return; + break; } common.opcode = params->opcode; common.model = (struct bt_mesh_model *)params->model; From 0c82a32524ab9f7ab6dede2783491f15bbe056c2 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:00:24 +0800 Subject: [PATCH 101/146] ble_mesh: fix finding netkey/appkey/devkey for tx/rx msg --- components/bt/esp_ble_mesh/mesh_core/access.c | 466 +++++++++++++----- components/bt/esp_ble_mesh/mesh_core/access.h | 18 + components/bt/esp_ble_mesh/mesh_core/net.c | 86 +--- .../esp_ble_mesh/mesh_core/provisioner_main.c | 14 +- .../esp_ble_mesh/mesh_core/provisioner_main.h | 8 +- .../bt/esp_ble_mesh/mesh_core/transport.c | 146 +----- 6 files changed, 397 insertions(+), 341 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index d8365ecdd5..529d8ebab1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -728,67 +728,72 @@ void bt_mesh_model_msg_init(struct net_buf_simple *msg, u32_t opcode) net_buf_simple_add_le16(msg, opcode & 0xffff); } +static bool ready_to_send(u8_t role, u16_t dst) +{ +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (!bt_mesh_is_provisioned()) { + BT_ERR("%s, Local node is not yet provisioned", __func__); + return false; + } + if (!bt_mesh_is_provisioner_en()) { + return true; + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (!provisioner_check_msg_dst_addr(dst)) { + BT_ERR("%s, Failed to find DST 0x%04x", __func__, dst); + return false; + } + if (bt_mesh_is_provisioner_en()) { + return true; + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (!provisioner_check_msg_dst_addr(dst)) { + BT_ERR("%s, Failed to check DST", __func__); + return false; + } + if (bt_mesh_is_provisioner_en()) { + return true; + } + } else { + if (!bt_mesh_is_provisioned()) { + BT_ERR("%s, Local node is not yet provisioned", __func__); + return false; + } + + return true; + } +#endif + + return false; +} + static int model_send(struct bt_mesh_model *model, struct bt_mesh_net_tx *tx, bool implicit_bind, struct net_buf_simple *msg, const struct bt_mesh_send_cb *cb, void *cb_data) { - bool check = false; u8_t role; - BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->ctx->net_idx, - tx->ctx->app_idx, tx->ctx->addr); - BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - role = bt_mesh_get_device_role(model, tx->ctx->srv_send); if (role == ROLE_NVAL) { BT_ERR("%s, Failed to get model role", __func__); return -EINVAL; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioned()) { - BT_ERR("%s, Local node is not yet provisioned", __func__); - return -EAGAIN; - } - if (!bt_mesh_is_provisioner_en()) { - check = true; - } - } -#endif + BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->ctx->net_idx, + tx->ctx->app_idx, tx->ctx->addr); + BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (!provisioner_check_msg_dst_addr(tx->ctx->addr)) { - BT_ERR("%s, Failed to check DST", __func__); - return -EINVAL; - } - if (bt_mesh_is_provisioner_en()) { - check = true; - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (!provisioner_check_msg_dst_addr(tx->ctx->addr)) { - BT_ERR("%s, Failed to check DST", __func__); - return -EINVAL; - } - if (bt_mesh_is_provisioner_en()) { - check = true; - } - } else { - if (!bt_mesh_is_provisioned()) { - BT_ERR("%s, Local node is not yet provisioned", __func__); - return -EAGAIN; - } - check = true; - } -#endif - - if (!check) { + if (!ready_to_send(role, tx->ctx->addr)) { BT_ERR("%s, fail", __func__); return -EINVAL; } @@ -825,36 +830,7 @@ int bt_mesh_model_send(struct bt_mesh_model *model, return -EINVAL; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioner_en()) { - sub = bt_mesh_subnet_get(ctx->net_idx); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - sub = provisioner_subnet_get(ctx->net_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - sub = bt_mesh_subnet_get(ctx->net_idx); - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - sub = provisioner_subnet_get(ctx->net_idx); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - sub = get_fast_prov_subnet(ctx->net_idx); -#endif - } -#endif - + sub = bt_mesh_tx_netkey_get(role, ctx->net_idx); if (!sub) { BT_ERR("%s, Failed to get subnet", __func__); return -EINVAL; @@ -899,34 +875,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) return -EADDRNOTAVAIL; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh_app_key_find(pub->key); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_app_key_find(pub->key); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh_app_key_find(pub->key); - } - } else if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_app_key_find(pub->key); - } - } -#endif - + key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key, BLE_MESH_KEY_ANY); if (!key) { BT_ERR("%s, Failed to get AppKey", __func__); return -EADDRNOTAVAIL; @@ -950,34 +899,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) tx.friend_cred = pub->cred; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - tx.sub = bt_mesh_subnet_get(ctx.net_idx); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - tx.sub = provisioner_subnet_get(ctx.net_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (pub->dev_role == NODE) { - if (bt_mesh_is_provisioned()) { - tx.sub = bt_mesh_subnet_get(ctx.net_idx); - } - } else if (pub->dev_role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - tx.sub = provisioner_subnet_get(ctx.net_idx); - } - } -#endif - + tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, ctx.net_idx); if (!tx.sub) { BT_ERR("%s, Failed to get subnet", __func__); return -EADDRNOTAVAIL; @@ -1041,3 +963,281 @@ const struct bt_mesh_comp *bt_mesh_comp_get(void) { return dev_comp; } + +/* APIs used by messages encryption in upper transport layer & network layer */ +struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + sub = bt_mesh_subnet_get(net_idx); + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + sub = provisioner_subnet_get(net_idx); + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + sub = bt_mesh_subnet_get(net_idx); + } + } else if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + sub = provisioner_subnet_get(net_idx); + } + } else if (role == FAST_PROV) { +#if CONFIG_BLE_MESH_FAST_PROV + sub = fast_prov_subnet_get(net_idx); +#endif + } +#endif + + return sub; +} + +const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst) +{ + const u8_t *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh.dev_key; + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_dev_key_get(dst); + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh.dev_key; + } + } else if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_dev_key_get(dst); + } + } else if (role == FAST_PROV) { +#if CONFIG_BLE_MESH_FAST_PROV + key = fast_prov_dev_key_get(dst); +#endif + } +#endif + + return key; +} + +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx, u16_t net_idx) +{ + struct bt_mesh_app_key *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh_app_key_find(app_idx); + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_app_key_find(app_idx); + } + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (role == NODE) { + if (bt_mesh_is_provisioned()) { + key = bt_mesh_app_key_find(app_idx); + } + } else if (role == PROVISIONER) { + if (bt_mesh_is_provisioner_en()) { + key = provisioner_app_key_find(app_idx); + } + } else if (role == FAST_PROV) { +#if CONFIG_BLE_MESH_FAST_PROV + key = fast_prov_app_key_find(net_idx, app_idx); +#endif + } +#endif + + return key; +} + +/* APIs used by messages decryption in network layer & upper transport layer */ +size_t bt_mesh_rx_netkey_size(void) +{ + size_t size = 0; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + size = ARRAY_SIZE(bt_mesh.sub); + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = ARRAY_SIZE(bt_mesh.p_sub); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = ARRAY_SIZE(bt_mesh.sub); + if (bt_mesh_is_provisioner_en()) { + size += ARRAY_SIZE(bt_mesh.p_sub); + } +#endif + + return size; +} + +struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index) +{ + struct bt_mesh_subnet *sub = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + sub = &bt_mesh.sub[index]; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + sub = bt_mesh.p_sub[index]; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < ARRAY_SIZE(bt_mesh.sub)) { + sub = &bt_mesh.sub[index]; + } else { + sub = bt_mesh.p_sub[index - ARRAY_SIZE(bt_mesh.sub)]; + } +#endif + + return sub; +} + +size_t bt_mesh_rx_devkey_size(void) +{ + size_t size = 0; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (!bt_mesh_is_provisioner_en()) { + size = 1; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = 1; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = 1; + if (bt_mesh_is_provisioner_en()) { + size += 1; + } +#endif + + return size; +} + +const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src) +{ + const u8_t *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + key = bt_mesh.dev_key; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + key = provisioner_dev_key_get(src); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < 1) { + key = bt_mesh.dev_key; + } else { + key = provisioner_dev_key_get(src); + } +#endif + + return key; +} + +size_t bt_mesh_rx_appkey_size(void) +{ + size_t size = 0; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + size = ARRAY_SIZE(bt_mesh.app_keys); + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = ARRAY_SIZE(bt_mesh.p_app_keys); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = ARRAY_SIZE(bt_mesh.app_keys); + if (bt_mesh_is_provisioner_en()) { + size += ARRAY_SIZE(bt_mesh.p_app_keys); + } +#endif + + return size; +} + +struct bt_mesh_app_key *bt_mesh_rx_appkey_get(size_t index) +{ + struct bt_mesh_app_key *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + key = &bt_mesh.app_keys[index]; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + key = bt_mesh.p_app_keys[index]; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < ARRAY_SIZE(bt_mesh.app_keys)) { + key = &bt_mesh.app_keys[index]; + } else { + key = bt_mesh.p_app_keys[index - ARRAY_SIZE(bt_mesh.app_keys)]; + } +#endif + + return key; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/access.h b/components/bt/esp_ble_mesh/mesh_core/access.h index 114c1662be..119ad23627 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.h +++ b/components/bt/esp_ble_mesh/mesh_core/access.h @@ -57,4 +57,22 @@ void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); int bt_mesh_comp_register(const struct bt_mesh_comp *comp); +struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx); + +const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst); + +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx, u16_t net_idx); + +size_t bt_mesh_rx_netkey_size(void); + +struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index); + +size_t bt_mesh_rx_devkey_size(void); + +const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src); + +size_t bt_mesh_rx_appkey_size(void); + +struct bt_mesh_app_key *bt_mesh_rx_appkey_get(size_t index); + #endif /* _ACCESS_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 020ea02707..a944f83092 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -1078,56 +1078,15 @@ static bool net_find_and_decrypt(const u8_t *data, size_t data_len, struct net_buf_simple *buf) { struct bt_mesh_subnet *sub = NULL; - u32_t array_size = 0; + size_t array_size = 0; int i; BT_DBG("%s", __func__); -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.sub); - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.p_sub); - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - array_size = ARRAY_SIZE(bt_mesh.sub); - if (bt_mesh_is_provisioner_en()) { - array_size += ARRAY_SIZE(bt_mesh.p_sub); - } -#endif - - if (!array_size) { - BT_ERR("%s, Unable to get subnet size", __func__); - return false; - } + array_size = bt_mesh_rx_netkey_size(); for (i = 0; i < array_size; i++) { -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - sub = &bt_mesh.sub[i]; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - sub = bt_mesh.p_sub[i]; - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (i < ARRAY_SIZE(bt_mesh.sub)) { - sub = &bt_mesh.sub[i]; - } else { - sub = bt_mesh.p_sub[i - ARRAY_SIZE(bt_mesh.sub)]; - } -#endif - + sub = bt_mesh_rx_netkey_get(i); if (!sub) { BT_DBG("%s, NULL subnet", __func__); continue; @@ -1373,6 +1332,29 @@ int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, return 0; } +static bool ready_to_recv(void) +{ +#if CONFIG_BLE_MESH_NODE + if (!bt_mesh_is_provisioner_en()) { + if (!bt_mesh_is_provisioned()) { + return false; + } + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (!bt_mesh_is_provisioner_en()) { + BT_WARN("%s, Provisioner is disabled", __func__); + return false; + } + if (!provisioner_get_prov_node_count()) { + return false; + } +#endif + + return true; +} + void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, enum bt_mesh_net_if net_if) { @@ -1382,23 +1364,9 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, BT_DBG("rssi %d net_if %u", rssi, net_if); -#if CONFIG_BLE_MESH_NODE - if (!bt_mesh_is_provisioner_en()) { - if (!bt_mesh_is_provisioned()) { - return; - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - BT_WARN("%s, Provisioner is disabled", __func__); + if (!ready_to_recv()) { return; } - if (!provisioner_get_prov_node_count()) { - return; - } -#endif if (bt_mesh_net_decode(data, net_if, &rx, &buf)) { return; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index 586fd33842..44049fd0c8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -330,7 +330,7 @@ bool provisioner_check_msg_dst_addr(u16_t dst_addr) return false; } -const u8_t *provisioner_get_device_key(u16_t dst_addr) +const u8_t *provisioner_dev_key_get(u16_t dst_addr) { /* Device key is only used to encrypt configuration messages. * Configuration model shall only be supported by the primary @@ -1140,7 +1140,7 @@ int bt_mesh_provisioner_print_local_element_info(void) #if CONFIG_BLE_MESH_FAST_PROV -const u8_t *get_fast_prov_device_key(u16_t addr) +const u8_t *fast_prov_dev_key_get(u16_t addr) { struct bt_mesh_node_t *node = NULL; @@ -1165,7 +1165,7 @@ const u8_t *get_fast_prov_device_key(u16_t addr) return NULL; } -struct bt_mesh_subnet *get_fast_prov_subnet(u16_t net_idx) +struct bt_mesh_subnet *fast_prov_subnet_get(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; @@ -1188,7 +1188,7 @@ struct bt_mesh_subnet *get_fast_prov_subnet(u16_t net_idx) return NULL; } -struct bt_mesh_app_key *get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) +struct bt_mesh_app_key *fast_prov_app_key_find(u16_t net_idx, u16_t app_idx) { struct bt_mesh_app_key *key = NULL; @@ -1216,7 +1216,7 @@ u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) struct bt_mesh_subnet *sub = NULL; struct bt_mesh_subnet_keys *key = NULL; - sub = get_fast_prov_subnet(net_idx); + sub = fast_prov_subnet_get(net_idx); if (sub) { key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0]; return provisioner_set_fast_prov_net_idx(key->net, net_idx); @@ -1253,7 +1253,7 @@ const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; - sub = get_fast_prov_subnet(net_idx); + sub = fast_prov_subnet_get(net_idx); if (!sub) { BT_ERR("%s, Failed to get subnet", __func__); return NULL; @@ -1266,7 +1266,7 @@ const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) { struct bt_mesh_app_key *key = NULL; - key = get_fast_prov_app_key(net_idx, app_idx); + key = fast_prov_app_key_find(net_idx, app_idx); if (!key) { BT_ERR("%s, Failed to get AppKey", __func__); return NULL; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h index 50bb656b2c..6fe4150a88 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -55,7 +55,7 @@ struct bt_mesh_subnet *provisioner_subnet_get(u16_t net_idx); bool provisioner_check_msg_dst_addr(u16_t dst_addr); -const u8_t *provisioner_get_device_key(u16_t dst_addr); +const u8_t *provisioner_dev_key_get(u16_t dst_addr); struct bt_mesh_app_key *provisioner_app_key_find(u16_t app_idx); @@ -105,11 +105,11 @@ int bt_mesh_provisioner_print_local_element_info(void); /* The following APIs are for fast provisioning */ -const u8_t *get_fast_prov_device_key(u16_t dst_addr); +const u8_t *fast_prov_dev_key_get(u16_t dst_addr); -struct bt_mesh_subnet *get_fast_prov_subnet(u16_t net_idx); +struct bt_mesh_subnet *fast_prov_subnet_get(u16_t net_idx); -struct bt_mesh_app_key *get_fast_prov_app_key(u16_t net_idx, u16_t app_idx); +struct bt_mesh_app_key *fast_prov_app_key_find(u16_t net_idx, u16_t app_idx); u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx); diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 98f93cb04e..82a0ddfae9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -481,36 +481,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, } if (tx->ctx->app_idx == BLE_MESH_KEY_DEV) { -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioner_en()) { - key = bt_mesh.dev_key; - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_get_device_key(tx->ctx->addr); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - key = bt_mesh.dev_key; - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_get_device_key(tx->ctx->addr); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - key = get_fast_prov_device_key(tx->ctx->addr); -#endif - } -#endif - + key = bt_mesh_tx_devkey_get(role, tx->ctx->addr); if (!key) { BT_ERR("%s, Failed to get Device Key", __func__); return -EINVAL; @@ -520,36 +491,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, } else { struct bt_mesh_app_key *app_key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioner_en()) { - app_key = bt_mesh_app_key_find(tx->ctx->app_idx); - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - app_key = provisioner_app_key_find(tx->ctx->app_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - app_key = bt_mesh_app_key_find(tx->ctx->app_idx); - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - app_key = provisioner_app_key_find(tx->ctx->app_idx); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - app_key = get_fast_prov_app_key(tx->ctx->net_idx, tx->ctx->app_idx); -#endif - } -#endif - + app_key = bt_mesh_tx_appkey_get(role, tx->ctx->app_idx, tx->ctx->net_idx); if (!app_key) { BT_ERR("%s, Failed to get AppKey", __func__); return -EINVAL; @@ -677,7 +619,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, u8_t aszmic, struct net_buf_simple *buf) { struct net_buf_simple *sdu = NULL; - u32_t array_size = 0; + size_t array_size = 0; u8_t *ad; u16_t i; int err; @@ -715,48 +657,12 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, } if (!AKF(&hdr)) { -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - array_size = 1; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - array_size = 1; - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - array_size = 1; - if (bt_mesh_is_provisioner_en()) { - array_size += 1; - } -#endif + array_size = bt_mesh_rx_devkey_size(); for (i = 0; i < array_size; i++) { const u8_t *dev_key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - dev_key = bt_mesh.dev_key; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - dev_key = provisioner_get_device_key(rx->ctx.addr); - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (i < 1) { - dev_key = bt_mesh.dev_key; - } else { - dev_key = provisioner_get_device_key(rx->ctx.addr); - } -#endif - + dev_key = bt_mesh_rx_devkey_get(i, rx->ctx.addr); if (!dev_key) { BT_DBG("%s, NULL Device Key", __func__); continue; @@ -783,49 +689,13 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, return -ENODEV; } -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.app_keys); - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - array_size = ARRAY_SIZE(bt_mesh.p_app_keys); - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - array_size = ARRAY_SIZE(bt_mesh.app_keys); - if (bt_mesh_is_provisioner_en()) { - array_size += ARRAY_SIZE(bt_mesh.p_app_keys); - } -#endif + array_size = bt_mesh_rx_appkey_size(); for (i = 0; i < array_size; i++) { - struct bt_mesh_app_key *key = NULL; struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - key = &bt_mesh.app_keys[i]; - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - key = bt_mesh.p_app_keys[i]; - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (i < ARRAY_SIZE(bt_mesh.app_keys)) { - key = &bt_mesh.app_keys[i]; - } else { - key = bt_mesh.p_app_keys[i - ARRAY_SIZE(bt_mesh.app_keys)]; - } -#endif - + key = bt_mesh_rx_appkey_get(i); if (!key) { BT_DBG("%s, NULL AppKey", __func__); continue; From f3e7656c0616e4094159c39b15d0e956949aa9e6 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:01:54 +0800 Subject: [PATCH 102/146] ble_mesh: remove non-standard relay opearation --- components/bt/esp_ble_mesh/mesh_core/net.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index a944f83092..79d27c70d6 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -1195,15 +1195,6 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, transmit = bt_mesh_relay_retransmit_get(); } else { transmit = bt_mesh_net_transmit_get(); - if (rx->net_if == BLE_MESH_NET_IF_PROXY && - transmit < BLE_MESH_TRANSMIT(5, 20)) { - /** - * Add this in case EspBleMesh APP just send a message once, and - * the Proxy Node will send this message using advertising bearer - * with duration not less than 180ms. - */ - transmit = BLE_MESH_TRANSMIT(5, 20); - } } buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, transmit, K_NO_WAIT); From 9a7efd30ef8e5e47104ad5462e8c0d0589eebcaf Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:06:15 +0800 Subject: [PATCH 103/146] ble_mesh: fix output MIC with additional data --- components/bt/esp_ble_mesh/mesh_core/crypto.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/crypto.c b/components/bt/esp_ble_mesh/mesh_core/crypto.c index d2e6507882..7573e164fd 100644 --- a/components/bt/esp_ble_mesh/mesh_core/crypto.c +++ b/components/bt/esp_ble_mesh/mesh_core/crypto.c @@ -261,7 +261,7 @@ static int bt_mesh_ccm_decrypt(const u8_t key[16], u8_t nonce[13], } } - for (i = 0; i < aad_len; i++, j++) { + for (; i < aad_len; i++, j++) { pmsg[i] = Xn[i] ^ aad[j]; } @@ -425,7 +425,7 @@ static int bt_mesh_ccm_encrypt(const u8_t key[16], u8_t nonce[13], } } - for (i = 0; i < aad_len; i++, j++) { + for (; i < aad_len; i++, j++) { pmsg[i] = Xn[i] ^ aad[j]; } From c44a3f12094126bdd77418064f24c9e8781f9646 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:16:33 +0800 Subject: [PATCH 104/146] ble_mesh: fix RPL storage timeout handling --- components/bt/esp_ble_mesh/mesh_core/net.h | 2 +- .../bt/esp_ble_mesh/mesh_core/settings.c | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/net.h b/components/bt/esp_ble_mesh/mesh_core/net.h index 90515fd2ca..28cd91ce87 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.h +++ b/components/bt/esp_ble_mesh/mesh_core/net.h @@ -209,7 +209,7 @@ enum { BLE_MESH_IVU_TEST, /* IV Update test mode */ BLE_MESH_IVU_PENDING, /* Update blocked by SDU in progress */ - /* pending storage actions */ + /* pending storage actions, must reside within first 32 flags */ BLE_MESH_RPL_PENDING, BLE_MESH_KEYS_PENDING, BLE_MESH_NET_PENDING, diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index adf63e5073..0a6a8b9c25 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -833,19 +833,29 @@ int settings_core_commit(void) return 0; } +/* Pending flags that use K_NO_WAIT as the storage timeout */ +#define NO_WAIT_PENDING_BITS (BIT(BLE_MESH_NET_PENDING) | \ + BIT(BLE_MESH_IV_PENDING) | \ + BIT(BLE_MESH_SEQ_PENDING)) + +/* Pending flags that use CONFIG_BLE_MESH_STORE_TIMEOUT */ +#define GENERIC_PENDING_BITS (BIT(BLE_MESH_KEYS_PENDING) | \ + BIT(BLE_MESH_HB_PUB_PENDING) | \ + BIT(BLE_MESH_CFG_PENDING) | \ + BIT(BLE_MESH_MOD_PENDING)) + static void schedule_store(int flag) { s32_t timeout; bt_mesh_atomic_set_bit(bt_mesh.flags, flag); - if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_NET_PENDING) || - bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IV_PENDING) || - bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_SEQ_PENDING)) { + if (bt_mesh_atomic_get(bt_mesh.flags) & NO_WAIT_PENDING_BITS) { timeout = K_NO_WAIT; } else if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING) && - (CONFIG_BLE_MESH_RPL_STORE_TIMEOUT < - CONFIG_BLE_MESH_STORE_TIMEOUT)) { + (!(bt_mesh_atomic_get(bt_mesh.flags) & GENERIC_PENDING_BITS) || + (CONFIG_BLE_MESH_RPL_STORE_TIMEOUT < + CONFIG_BLE_MESH_STORE_TIMEOUT))) { timeout = K_SECONDS(CONFIG_BLE_MESH_RPL_STORE_TIMEOUT); } else { timeout = K_SECONDS(CONFIG_BLE_MESH_STORE_TIMEOUT); From 9c98a8d645bb10201b9ccc902ab2e62e14d0da5c Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:17:47 +0800 Subject: [PATCH 105/146] ble_mesh: fix postponing storage deadline indefinitely --- components/bt/esp_ble_mesh/mesh_core/settings.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 0a6a8b9c25..491782f400 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -846,7 +846,7 @@ int settings_core_commit(void) static void schedule_store(int flag) { - s32_t timeout; + s32_t timeout, remaining; bt_mesh_atomic_set_bit(bt_mesh.flags, flag); @@ -861,6 +861,12 @@ static void schedule_store(int flag) timeout = K_SECONDS(CONFIG_BLE_MESH_STORE_TIMEOUT); } + remaining = k_delayed_work_remaining_get(&pending_store); + if (remaining && remaining < timeout) { + BT_DBG("Not rescheduling due to existing earlier deadline"); + return; + } + BT_DBG("Waiting %d seconds", timeout / MSEC_PER_SEC); if (timeout) { From 4bf4094e409762909c7b0ddbffb2c5c58fe7cb75 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:21:03 +0800 Subject: [PATCH 106/146] ble_mesh: update proxy sar operation from zephyr --- components/bt/esp_ble_mesh/mesh_core/proxy.c | 69 ++++++++------------ 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy.c b/components/bt/esp_ble_mesh/mesh_core/proxy.c index 4945906fd5..bf5a227b3d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy.c +++ b/components/bt/esp_ble_mesh/mesh_core/proxy.c @@ -31,6 +31,12 @@ #define PDU_TYPE(data) (data[0] & BIT_MASK(6)) #define PDU_SAR(data) (data[0] >> 6) +/* Mesh Profile 1.0 Section 6.6: + * "The timeout for the SAR transfer is 20 seconds. When the timeout + * expires, the Proxy Server shall disconnect." + */ +#define PROXY_SAR_TIMEOUT K_SECONDS(20) + #define SAR_COMPLETE 0x00 #define SAR_FIRST 0x01 #define SAR_CONT 0x02 @@ -71,13 +77,6 @@ static u16_t prov_ccc_val; static bool prov_fast_adv; #endif -enum { - SAR_TIMER_START, /* Timer for SAR transfer has been started */ - NUM_FLAGS, -}; - -#define PROXY_SAR_TRANS_TIMEOUT K_SECONDS(20) - static struct bt_mesh_proxy_client { struct bt_mesh_conn *conn; u16_t filter[CONFIG_BLE_MESH_PROXY_FILTER_SIZE]; @@ -91,10 +90,8 @@ static struct bt_mesh_proxy_client { #if defined(CONFIG_BLE_MESH_GATT_PROXY) struct k_work send_beacons; #endif + struct k_delayed_work sar_timer; struct net_buf_simple buf; - /* Proxy Server: 20s timeout for each segmented proxy pdu */ - BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS); - struct k_delayed_work sar_timer; } clients[BLE_MESH_MAX_CONN] = { [0 ... (BLE_MESH_MAX_CONN - 1)] = { #if defined(CONFIG_BLE_MESH_GATT_PROXY) @@ -145,6 +142,22 @@ static struct bt_mesh_proxy_client *find_client(struct bt_mesh_conn *conn) return NULL; } +static void proxy_sar_timeout(struct k_work *work) +{ + struct bt_mesh_proxy_client *client = NULL; + + BT_WARN("Proxy SAR timeout"); + + client = CONTAINER_OF(work, struct bt_mesh_proxy_client, sar_timer.work); + if (!client || !client->conn) { + BT_ERR("%s, Invalid proxy client parameter", __func__); + return; + } + + net_buf_simple_reset(&client->buf); + bt_mesh_gatts_disconnect(client->conn, 0x13); +} + #if defined(CONFIG_BLE_MESH_GATT_PROXY) /* Next subnet in queue to be advertised */ static int next_idx; @@ -500,10 +513,7 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return -EINVAL; } - if (!bt_mesh_atomic_test_and_set_bit(client->flags, SAR_TIMER_START)) { - k_delayed_work_submit(&client->sar_timer, PROXY_SAR_TRANS_TIMEOUT); - } - + k_delayed_work_submit(&client->sar_timer, PROXY_SAR_TIMEOUT); client->msg_type = PDU_TYPE(data); net_buf_simple_add_mem(&client->buf, data + 1, len - 1); break; @@ -519,6 +529,7 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return -EINVAL; } + k_delayed_work_submit(&client->sar_timer, PROXY_SAR_TIMEOUT); net_buf_simple_add_mem(&client->buf, data + 1, len - 1); break; @@ -533,10 +544,7 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return -EINVAL; } - if (bt_mesh_atomic_test_and_clear_bit(client->flags, SAR_TIMER_START)) { - k_delayed_work_cancel(&client->sar_timer); - } - + k_delayed_work_cancel(&client->sar_timer); net_buf_simple_add_mem(&client->buf, data + 1, len - 1); proxy_complete_pdu(client); break; @@ -599,10 +607,7 @@ static void proxy_disconnected(struct bt_mesh_conn *conn, u8_t reason) bt_mesh_pb_gatt_close(conn); } - if (bt_mesh_atomic_test_and_clear_bit(client->flags, SAR_TIMER_START)) { - k_delayed_work_cancel(&client->sar_timer); - } - + k_delayed_work_cancel(&client->sar_timer); bt_mesh_conn_unref(client->conn); client->conn = NULL; break; @@ -1361,23 +1366,6 @@ static struct bt_mesh_conn_cb conn_callbacks = { .disconnected = proxy_disconnected, }; -static void proxy_recv_timeout(struct k_work *work) -{ - struct bt_mesh_proxy_client *client = NULL; - - BT_DBG("%s", __func__); - - client = CONTAINER_OF(work, struct bt_mesh_proxy_client, sar_timer.work); - if (!client || !client->conn) { - BT_ERR("%s, Invalid proxy client parameter", __func__); - return; - } - - bt_mesh_atomic_clear_bit(client->flags, SAR_TIMER_START); - net_buf_simple_reset(&client->buf); - bt_mesh_gatts_disconnect(client->conn, 0x13); -} - int bt_mesh_proxy_init(void) { int i; @@ -1388,7 +1376,8 @@ int bt_mesh_proxy_init(void) client->buf.size = CLIENT_BUF_SIZE; client->buf.__buf = client_buf_data + (i * CLIENT_BUF_SIZE); - k_delayed_work_init(&client->sar_timer, proxy_recv_timeout); + + k_delayed_work_init(&client->sar_timer, proxy_sar_timeout); } bt_mesh_gatts_conn_cb_register(&conn_callbacks); From 5f5f5cf305e9d815b79ef093e44316ead37562cd Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:22:28 +0800 Subject: [PATCH 107/146] ble_mesh: clear LPN sent_req on failure --- components/bt/esp_ble_mesh/mesh_core/lpn.c | 1 + 1 file changed, 1 insertion(+) diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.c b/components/bt/esp_ble_mesh/mesh_core/lpn.c index 8adf2a24bb..7788ee7503 100644 --- a/components/bt/esp_ble_mesh/mesh_core/lpn.c +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.c @@ -764,6 +764,7 @@ static void lpn_timeout(struct k_work *work) } lpn->counter++; lpn_set_state(BLE_MESH_LPN_ENABLED); + lpn->sent_req = 0U; k_delayed_work_submit(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT); break; case BLE_MESH_LPN_OFFER_RECV: From 716db01a7f9054404f35f0cc8cae21a6bc1bae5c Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:26:32 +0800 Subject: [PATCH 108/146] ble_mesh: fix segmented message RPL behavior --- .../bt/esp_ble_mesh/mesh_core/transport.c | 64 +++++++++++++------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 82a0ddfae9..79f59b0ceb 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -555,7 +555,23 @@ int bt_mesh_trans_resend(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, return err; } -static bool is_replay(struct bt_mesh_net_rx *rx) +static void update_rpl(struct bt_mesh_rpl *rpl, struct bt_mesh_net_rx *rx) +{ + rpl->src = rx->ctx.addr; + rpl->seq = rx->seq; + rpl->old_iv = rx->old_iv; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_rpl(rpl); + } +} + +/* Check the Replay Protection List for a replay attempt. If non-NULL match + * parameter is given the RPL slot is returned but it is not immediately + * updated (needed for segmented messages), whereas if a NULL match is given + * the RPL is immediately updated (used for unsegmented messages). + */ +static bool is_replay(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match) { int i; @@ -564,17 +580,20 @@ static bool is_replay(struct bt_mesh_net_rx *rx) return false; } + /* The RPL is used only for the local node */ + if (!rx->local_match) { + return false; + } + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; /* Empty slot */ if (!rpl->src) { - rpl->src = rx->ctx.addr; - rpl->seq = rx->seq; - rpl->old_iv = rx->old_iv; - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_rpl(rpl); + if (match) { + *match = rpl; + } else { + update_rpl(rpl, rx); } return false; @@ -597,11 +616,10 @@ static bool is_replay(struct bt_mesh_net_rx *rx) if ((!rx->old_iv && rpl->old_iv) || (rpl->seq < rx->seq) || (rpl->seq > rx->seq + 10)) { #endif /* #if !CONFIG_BLE_MESH_PATCH_FOR_SLAB_APP_1_1_0 */ - rpl->seq = rx->seq; - rpl->old_iv = rx->old_iv; - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_rpl(rpl); + if (match) { + *match = rpl; + } else { + update_rpl(rpl, rx); } return false; @@ -950,7 +968,7 @@ static int trans_unseg(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx, return -EINVAL; } - if (rx->local_match && is_replay(rx)) { + if (is_replay(rx, NULL)) { BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x", rx->ctx.addr, rx->ctx.recv_dst, rx->seq); return -EINVAL; @@ -1229,6 +1247,7 @@ static struct seg_rx *seg_rx_alloc(struct bt_mesh_net_rx *net_rx, static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, enum bt_mesh_friend_pdu_type *pdu_type, u64_t *seq_auth) { + struct bt_mesh_rpl *rpl = NULL; struct seg_rx *rx; u8_t *hdr = buf->data; u16_t seq_zero; @@ -1241,6 +1260,12 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, return -EINVAL; } + if (is_replay(net_rx, &rpl)) { + BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x", + net_rx->ctx.addr, net_rx->ctx.recv_dst, net_rx->seq); + return -EINVAL; + } + BT_DBG("ASZMIC %u AKF %u AID 0x%02x", ASZMIC(hdr), AKF(hdr), AID(hdr)); net_buf_simple_pull(buf, 1); @@ -1300,6 +1325,11 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr, net_rx->ctx.send_ttl, seq_auth, rx->block, rx->obo); + + if (rpl) { + update_rpl(rpl, net_rx); + } + return -EALREADY; } @@ -1387,12 +1417,8 @@ found_rx: BT_DBG("Complete SDU"); - if (net_rx->local_match && is_replay(net_rx)) { - BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x", - net_rx->ctx.addr, net_rx->ctx.recv_dst, net_rx->seq); - /* Clear the segment's bit */ - rx->block &= ~BIT(seg_o); - return -EINVAL; + if (rpl) { + update_rpl(rpl, net_rx); } *pdu_type = BLE_MESH_FRIEND_PDU_COMPLETE; From 630e91195813492bec86746aecb07caab5475cca Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:32:54 +0800 Subject: [PATCH 109/146] ble_mesh: update protocol error timeout from zephyr --- components/bt/esp_ble_mesh/mesh_core/prov.c | 171 +++++++++----------- 1 file changed, 73 insertions(+), 98 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index 0a3329ff65..4a9572328a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -105,7 +105,7 @@ enum { SEND_CONFIRM, /* Waiting to send Confirm value */ WAIT_NUMBER, /* Waiting for number input from user */ WAIT_STRING, /* Waiting for string input from user */ - TIMEOUT_START, /* Provision timeout timer has started */ + LINK_INVALID, /* Error occurred during provisioning */ NUM_FLAGS, }; @@ -161,8 +161,7 @@ struct prov_link { } tx; #endif - /* Provision timeout timer */ - struct k_delayed_work timeout; + struct k_delayed_work prot_timer; }; struct prov_rx { @@ -176,11 +175,11 @@ struct prov_rx { #if defined(CONFIG_BLE_MESH_FAST_PROV) #define RETRANSMIT_TIMEOUT K_MSEC(360) #define TRANSACTION_TIMEOUT K_SECONDS(3) -#define PROVISION_TIMEOUT K_SECONDS(6) +#define PROTOCOL_TIMEOUT K_SECONDS(6) #else #define RETRANSMIT_TIMEOUT K_MSEC(500) #define TRANSACTION_TIMEOUT K_SECONDS(30) -#define PROVISION_TIMEOUT K_SECONDS(60) +#define PROTOCOL_TIMEOUT K_SECONDS(60) #endif /* CONFIG_BLE_MESH_FAST_PROV */ #if defined(CONFIG_BLE_MESH_PB_GATT) @@ -197,10 +196,10 @@ static struct prov_link link; static const struct bt_mesh_prov *prov; -static void close_link(u8_t err, u8_t reason); - static void reset_state(void) { + k_delayed_work_cancel(&link.prot_timer); + /* Disable Attention Timer if it was set */ if (link.conf_inputs[0]) { bt_mesh_attention(NULL, 0); @@ -213,7 +212,9 @@ static void reset_state(void) #endif #if defined(CONFIG_BLE_MESH_PB_ADV) - /* Clear everything except the retransmit delayed work config */ + /* Clear everything except the retransmit and protocol timer + * delayed work objects. + */ (void)memset(&link, 0, offsetof(struct prov_link, tx.retransmit)); link.rx.prev_id = XACT_NVAL; @@ -224,8 +225,9 @@ static void reset_state(void) link.rx.buf = &rx_buf; #endif /* PB_GATT */ -#else - (void)memset(&link, 0, offsetof(struct prov_link, timeout)); +#else /* !PB_ADV */ + /* Clear everything except the protocol timer (k_delayed_work) */ + (void)memset(&link, 0, offsetof(struct prov_link, prot_timer)); #endif /* PB_ADV */ } @@ -280,10 +282,6 @@ static void reset_link(void) { prov_clear_tx(); - if (bt_mesh_atomic_test_and_clear_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link.timeout); - } - if (prov->link_close) { prov->link_close(BLE_MESH_PROV_ADV); } @@ -425,7 +423,7 @@ static int prov_send_adv(struct net_buf_simple *msg) struct net_buf *start, *buf; u8_t seg_len, seg_id; u8_t xact_id; - s32_t timeout = PROVISION_TIMEOUT; + s32_t timeout = PROTOCOL_TIMEOUT; BT_DBG("%s, len %u: %s", __func__, msg->len, bt_hex(msg->data, msg->len)); @@ -486,17 +484,12 @@ static int prov_send_adv(struct net_buf_simple *msg) /* Changed by Espressif, add provisioning timeout timer operations. * When sending a provisioning PDU successfully, restart the 60s timer. */ - if (bt_mesh_atomic_test_and_clear_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link.timeout); - } #if defined(CONFIG_BLE_MESH_FAST_PROV) if (link.tx_pdu_type >= PROV_COMPLETE) { timeout = K_SECONDS(60); } #endif - if (!bt_mesh_atomic_test_and_set_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_submit(&link.timeout, timeout); - } + k_delayed_work_submit(&link.prot_timer, timeout); return 0; } @@ -521,14 +514,7 @@ static int prov_send_gatt(struct net_buf_simple *msg) return err; } - if (bt_mesh_atomic_test_and_clear_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link.timeout); - } - if (msg->data[1] != PROV_COMPLETE && msg->data[1] != PROV_FAILED) { - if (!bt_mesh_atomic_test_and_set_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_submit(&link.timeout, PROVISION_TIMEOUT); - } - } + k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); return 0; } @@ -561,6 +547,8 @@ static void prov_send_fail_msg(u8_t err) prov_buf_init(&buf, PROV_FAILED); net_buf_simple_add_u8(&buf, err); prov_send(&buf); + + bt_mesh_atomic_set_bit(link.flags, LINK_INVALID); } static void prov_invite(const u8_t *data) @@ -605,7 +593,7 @@ static void prov_invite(const u8_t *data) if (prov_send(&buf)) { BT_ERR("%s, Failed to send capabilities", __func__); - close_link(PROV_ERR_RESOURCES, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_RESOURCES); return; } @@ -821,7 +809,7 @@ static void send_confirm(void) if (bt_mesh_prov_conf_salt(link.conf_inputs, link.conf_salt)) { BT_ERR("%s, Unable to generate confirmation salt", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -829,7 +817,7 @@ static void send_confirm(void) if (bt_mesh_prov_conf_key(link.dhkey, link.conf_salt, link.conf_key)) { BT_ERR("%s, Unable to generate confirmation key", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -837,7 +825,7 @@ static void send_confirm(void) if (bt_mesh_rand(link.rand, 16)) { BT_ERR("%s, Unable to generate random number", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -848,13 +836,13 @@ static void send_confirm(void) if (bt_mesh_prov_conf(link.conf_key, link.rand, link.auth, net_buf_simple_add(&cfm, 16))) { BT_ERR("%s, Unable to generate confirmation value", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } if (prov_send(&cfm)) { BT_ERR("%s, Unable to send Provisioning Confirm", __func__); - close_link(PROV_ERR_RESOURCES, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_RESOURCES); return; } @@ -921,7 +909,7 @@ static void prov_dh_key_cb(const u8_t key[32], const u8_t idx) if (!key) { BT_ERR("%s, DHKey generation failed", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -949,7 +937,7 @@ static void send_pub_key(void) key = bt_mesh_pub_key_get(); if (!key) { BT_ERR("%s, No public key available", __func__); - close_link(PROV_ERR_RESOURCES, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -974,7 +962,7 @@ static void send_pub_key(void) if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { BT_ERR("%s, Unable to generate DHKey", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -994,7 +982,7 @@ static int bt_mesh_calc_dh_key(void) if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { BT_ERR("%s, Unable to generate DHKey", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return -EIO; } @@ -1096,7 +1084,7 @@ static void prov_random(const u8_t *data) if (bt_mesh_prov_conf(link.conf_key, data, link.auth, conf_verify)) { BT_ERR("%s, Unable to calculate confirmation verification", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -1104,7 +1092,7 @@ static void prov_random(const u8_t *data) BT_ERR("%s, Invalid confirmation value", __func__); BT_DBG("Received: %s", bt_hex(link.conf, 16)); BT_DBG("Calculated: %s", bt_hex(conf_verify, 16)); - close_link(PROV_ERR_CFM_FAILED, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_CFM_FAILED); return; } @@ -1113,14 +1101,14 @@ static void prov_random(const u8_t *data) if (prov_send(&rnd)) { BT_ERR("%s, Failed to send Provisioning Random", __func__); - close_link(PROV_ERR_RESOURCES, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_RESOURCES); return; } if (bt_mesh_prov_salt(link.conf_salt, data, link.rand, link.prov_salt)) { BT_ERR("%s, Failed to generate provisioning salt", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -1157,7 +1145,7 @@ static void prov_data(const u8_t *data) err = bt_mesh_session_key(link.dhkey, link.prov_salt, session_key); if (err) { BT_ERR("%s, Unable to generate session key", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -1166,7 +1154,7 @@ static void prov_data(const u8_t *data) err = bt_mesh_prov_nonce(link.dhkey, link.prov_salt, nonce); if (err) { BT_ERR("%s, Unable to generate session nonce", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -1175,14 +1163,14 @@ static void prov_data(const u8_t *data) err = bt_mesh_prov_decrypt(session_key, nonce, data, pdu); if (err) { BT_ERR("%s, Unable to decrypt provisioning data", __func__); - close_link(PROV_ERR_DECRYPT, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_DECRYPT); return; } err = bt_mesh_dev_key(link.dhkey, link.prov_salt, dev_key); if (err) { BT_ERR("%s, Unable to generate device key", __func__); - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); return; } @@ -1249,35 +1237,6 @@ static const struct { { prov_failed, 1 }, }; -static void close_link(u8_t err, u8_t reason) -{ -#if defined(CONFIG_BLE_MESH_PB_GATT) - if (link.conn) { - bt_mesh_pb_gatt_close(link.conn); - return; - } -#endif - -#if defined(CONFIG_BLE_MESH_PB_ADV) - if (err) { - prov_send_fail_msg(err); - } - - link.rx.seg = 0U; - bearer_ctl_send(LINK_CLOSE, &reason, sizeof(reason)); -#endif - - reset_state(); -} - -/* Changed by Espressif, add provisioning timeout timer callback */ -static void prov_timeout(struct k_work *work) -{ - BT_DBG("%s", __func__); - - close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_TIMEOUT); -} - #if defined(CONFIG_BLE_MESH_PB_ADV) static void prov_retransmit(struct k_work *work) { @@ -1425,6 +1384,12 @@ static void prov_msg_recv(void) link.rx.prev_id = link.rx.id; link.rx.id = 0U; + if (bt_mesh_atomic_test_bit(link.flags, LINK_INVALID)) { + BT_WARN("Unexpected msg 0x%02x on invalidated link", type); + prov_send_fail_msg(PROV_ERR_UNEXP_PDU); + return; + } + if (type != PROV_FAILED && type != link.expect) { BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, link.expect); prov_send_fail_msg(PROV_ERR_UNEXP_PDU); @@ -1433,26 +1398,21 @@ static void prov_msg_recv(void) if (type >= ARRAY_SIZE(prov_handlers)) { BT_ERR("%s, Unknown provisioning PDU type 0x%02x", __func__, type); - close_link(PROV_ERR_NVAL_PDU, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_NVAL_PDU); return; } if (1 + prov_handlers[type].len != link.rx.buf->len) { BT_ERR("%s, Invalid length %u for type 0x%02x", __func__, link.rx.buf->len, type); - close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } /* Changed by Espressif, add provisioning timeout timer operations. * When received a provisioning PDU, restart the 60s timer. */ - if (bt_mesh_atomic_test_and_clear_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link.timeout); - } - if (!bt_mesh_atomic_test_and_set_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_submit(&link.timeout, PROVISION_TIMEOUT); - } + k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); prov_handlers[type].func(&link.rx.buf->data[1]); } @@ -1485,7 +1445,7 @@ static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) if (seg > link.rx.last_seg) { BT_ERR("%s, Invalid segment index %u", __func__, seg); - close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } else if (seg == link.rx.last_seg) { u8_t expect_len; @@ -1495,7 +1455,7 @@ static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) if (expect_len != buf->len) { BT_ERR("%s, Incorrect last seg len: %u != %u", __func__, expect_len, buf->len); - close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } } @@ -1548,20 +1508,20 @@ static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf) if (link.rx.buf->len < 1) { BT_ERR("%s, Ignoring zero-length provisioning PDU", __func__); - close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } if (link.rx.buf->len > link.rx.buf->size) { BT_ERR("%s, Too large provisioning PDU (%u bytes)", __func__, link.rx.buf->len); - // close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); + // prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } if (START_LAST_SEG(rx->gpc) > 0 && link.rx.buf->len <= 20U) { BT_ERR("%s, Too small total length for multi-segment PDU", __func__); - close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } @@ -1668,12 +1628,7 @@ int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) /* Changed by Espressif, add provisioning timeout timer operations. * When received a provisioning PDU, restart the 60s timer. */ - if (bt_mesh_atomic_test_and_clear_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link.timeout); - } - if (!bt_mesh_atomic_test_and_set_bit(link.flags, TIMEOUT_START)) { - k_delayed_work_submit(&link.timeout, PROVISION_TIMEOUT); - } + k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); prov_handlers[type].func(buf->data); @@ -1727,6 +1682,27 @@ bool bt_prov_active(void) return bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE); } +static void protocol_timeout(struct k_work *work) +{ + BT_DBG("Protocol timeout"); + +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (link.conn) { + bt_mesh_pb_gatt_close(link.conn); + return; + } +#endif + +#if defined(CONFIG_BLE_MESH_PB_ADV) + u8_t reason = CLOSE_REASON_TIMEOUT; + + link.rx.seg = 0U; + bearer_ctl_send(LINK_CLOSE, &reason, sizeof(reason)); + + reset_state(); +#endif +} + int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info) { const u8_t *key = NULL; @@ -1743,15 +1719,14 @@ int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info) return -EIO; } + k_delayed_work_init(&link.prot_timer, protocol_timeout); + prov = prov_info; #if defined(CONFIG_BLE_MESH_PB_ADV) k_delayed_work_init(&link.tx.retransmit, prov_retransmit); #endif - /* Changed by Espressif, add provisioning timeout timer init */ - k_delayed_work_init(&link.timeout, prov_timeout); - reset_state(); return 0; From f2b0b00020af530e8bcf06464ac7bf12ce82436c Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:34:58 +0800 Subject: [PATCH 110/146] ble_mesh: fix public key mismatch error handling --- components/bt/esp_ble_mesh/mesh_core/prov.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index 4a9572328a..c10aaba8ea 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -48,6 +48,9 @@ #define INPUT_OOB_NUMBER 0x02 #define INPUT_OOB_STRING 0x03 +#define PUB_KEY_NO_OOB 0x00 +#define PUB_KEY_OOB 0x01 + #define PROV_ERR_NONE 0x00 #define PROV_ERR_NVAL_PDU 0x01 #define PROV_ERR_NVAL_FMT 0x02 @@ -772,8 +775,8 @@ static void prov_start(const u8_t *data) return; } - if (data[1] > 0x01) { - BT_ERR("%s, Invalid public key value: 0x%02x", __func__, data[1]); + if (data[1] != prov->oob_pub_key) { + BT_ERR("%s, Invalid public key type: 0x%02x", __func__, data[1]); prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } From 3e489d40ff7688a190658f7ecd603249541fabb4 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:36:24 +0800 Subject: [PATCH 111/146] ble_mesh: rename reset_link() to reset_adv_link() --- components/bt/esp_ble_mesh/mesh_core/prov.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index c10aaba8ea..51555678a0 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -281,7 +281,7 @@ static void prov_clear_tx(void) free_segments(); } -static void reset_link(void) +static void reset_adv_link(void) { prov_clear_tx(); @@ -1261,7 +1261,7 @@ static void prov_retransmit(struct k_work *work) #endif if (k_uptime_get() - link.tx.start > timeout) { BT_WARN("Node timeout, giving up transaction"); - reset_link(); + reset_adv_link(); return; } @@ -1341,7 +1341,7 @@ static void link_close(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len); - reset_link(); + reset_adv_link(); } static void gen_prov_ctl(struct prov_rx *rx, struct net_buf_simple *buf) From 3e47f3ec87a4f297a8e4b5fda189d4d4a3c23b70 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:38:12 +0800 Subject: [PATCH 112/146] ble_mesh: add error checks for scan start/stop --- components/bt/esp_ble_mesh/mesh_core/adv.c | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index 87ef363061..a1c4cf9825 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -7,6 +7,9 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include + #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/task.h" @@ -387,6 +390,8 @@ void bt_mesh_adv_init(void) int bt_mesh_scan_enable(void) { + int err; + struct bt_mesh_scan_param scan_param = { .type = BLE_MESH_SCAN_PASSIVE, #if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) @@ -400,12 +405,26 @@ int bt_mesh_scan_enable(void) BT_DBG("%s", __func__); - return bt_le_scan_start(&scan_param, bt_mesh_scan_cb); + err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); + if (err && err != -EALREADY) { + BT_ERR("starting scan failed (err %d)", err); + return err; + } + + return 0; } int bt_mesh_scan_disable(void) { + int err; + BT_DBG("%s", __func__); - return bt_le_scan_stop(); + err = bt_le_scan_stop(); + if (err && err != -EALREADY) { + BT_ERR("stopping scan failed (err %d)", err); + return err; + } + + return 0; } From 1c06494293a9ec34b0205c6e352588407c1bee1a Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:40:28 +0800 Subject: [PATCH 113/146] ble_mesh: fix rejecting invalid remote public key --- components/bt/esp_ble_mesh/mesh_core/prov.c | 28 ++++++++++++--------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index 51555678a0..c7c1dae4dc 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -937,6 +937,20 @@ static void send_pub_key(void) PROV_BUF(buf, 65); const u8_t *key; + /* Copy remote key in little-endian for bt_mesh_dh_key_gen(). + * X and Y halves are swapped independently. Use response + * buffer as a temporary storage location. The validating of + * the remote public key is finished when it is received. + */ + sys_memcpy_swap(buf.data, &link.conf_inputs[17], 32); + sys_memcpy_swap(&buf.data[32], &link.conf_inputs[49], 32); + + if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { + BT_ERR("%s, Unable to generate DHKey", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + key = bt_mesh_pub_key_get(); if (!key) { BT_ERR("%s, No public key available", __func__); @@ -954,18 +968,8 @@ static void send_pub_key(void) memcpy(&link.conf_inputs[81], &buf.data[1], 64); - prov_send(&buf); - - /* Copy remote key in little-endian for bt_mesh_dh_key_gen(). - * X and Y halves are swapped independently. - */ - net_buf_simple_reset(&buf); - sys_memcpy_swap(buf.data, &link.conf_inputs[17], 32); - sys_memcpy_swap(&buf.data[32], &link.conf_inputs[49], 32); - - if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { - BT_ERR("%s, Unable to generate DHKey", __func__); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + if (prov_send(&buf)) { + BT_ERR("Failed to send Public Key"); return; } From edf0b9ee02c31c0aebaec784d6dce4e846db883f Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:42:35 +0800 Subject: [PATCH 114/146] ble_mesh: fix provisioning send error handling --- components/bt/esp_ble_mesh/mesh_core/prov.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index c7c1dae4dc..b270519365 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -549,7 +549,10 @@ static void prov_send_fail_msg(u8_t err) prov_buf_init(&buf, PROV_FAILED); net_buf_simple_add_u8(&buf, err); - prov_send(&buf); + + if (prov_send(&buf)) { + BT_ERR("Failed to send Provisioning Failed message"); + } bt_mesh_atomic_set_bit(link.flags, LINK_INVALID); } @@ -596,7 +599,6 @@ static void prov_invite(const u8_t *data) if (prov_send(&buf)) { BT_ERR("%s, Failed to send capabilities", __func__); - prov_send_fail_msg(PROV_ERR_RESOURCES); return; } @@ -845,7 +847,6 @@ static void send_confirm(void) if (prov_send(&cfm)) { BT_ERR("%s, Unable to send Provisioning Confirm", __func__); - prov_send_fail_msg(PROV_ERR_RESOURCES); return; } @@ -857,7 +858,9 @@ static void send_input_complete(void) PROV_BUF(buf, 1); prov_buf_init(&buf, PROV_INPUT_COMPLETE); - prov_send(&buf); + if (prov_send(&buf)) { + BT_ERR("Failed to send Provisioning Input Complete"); + } } int bt_mesh_input_number(u32_t num) @@ -1108,7 +1111,6 @@ static void prov_random(const u8_t *data) if (prov_send(&rnd)) { BT_ERR("%s, Failed to send Provisioning Random", __func__); - prov_send_fail_msg(PROV_ERR_RESOURCES); return; } @@ -1192,7 +1194,10 @@ static void prov_data(const u8_t *data) net_idx, iv_index, addr); prov_buf_init(&msg, PROV_COMPLETE); - prov_send(&msg); + if (prov_send(&msg)) { + BT_ERR("Failed to send Provisioning Complete"); + return; + } /* Ignore any further PDUs on this link */ link.expect = 0U; From 75b0f50aa0beda913849ed102956722b204876e9 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:48:43 +0800 Subject: [PATCH 115/146] ble_mesh: move heartbeat sending to transport layer --- .../bt/esp_ble_mesh/mesh_core/cfg_srv.c | 58 ++----------------- .../bt/esp_ble_mesh/mesh_core/transport.c | 51 +++++++++++++++- .../bt/esp_ble_mesh/mesh_core/transport.h | 2 + 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c index c906e7c01a..9e1db8517e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -53,55 +53,6 @@ static struct label { u8_t uuid[16]; } labels[CONFIG_BLE_MESH_LABEL_COUNT]; -static void hb_send(struct bt_mesh_model *model) -{ - struct bt_mesh_cfg_srv *cfg = model->user_data; - u16_t feat = 0U; - struct __packed { - u8_t init_ttl; - u16_t feat; - } hb; - struct bt_mesh_msg_ctx ctx = { - .net_idx = cfg->hb_pub.net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = cfg->hb_pub.dst, - .send_ttl = cfg->hb_pub.ttl, - }; - struct bt_mesh_net_tx tx = { - .sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx), - .ctx = &ctx, - .src = bt_mesh_model_elem(model)->addr, - .xmit = bt_mesh_net_transmit_get(), - }; - - hb.init_ttl = cfg->hb_pub.ttl; - - if (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED) { - feat |= BLE_MESH_FEAT_RELAY; - } - - if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) { - feat |= BLE_MESH_FEAT_PROXY; - } - - if (bt_mesh_friend_get() == BLE_MESH_FRIEND_ENABLED) { - feat |= BLE_MESH_FEAT_FRIEND; - } - -#if defined(CONFIG_BLE_MESH_LOW_POWER) - if (bt_mesh.lpn.state != BLE_MESH_LPN_DISABLED) { - feat |= BLE_MESH_FEAT_LOW_POWER; - } -#endif - - hb.feat = sys_cpu_to_be16(feat); - - BT_DBG("InitTTL %u feat 0x%04x", cfg->hb_pub.ttl, feat); - - bt_mesh_ctl_send(&tx, TRANS_CTL_OP_HEARTBEAT, &hb, sizeof(hb), - NULL, NULL, NULL); -} - static int comp_add_elem(struct net_buf_simple *buf, struct bt_mesh_elem *elem, bool primary) { @@ -897,7 +848,7 @@ static void gatt_proxy_set(struct bt_mesh_model *model, sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx); if ((cfg->hb_pub.feat & BLE_MESH_FEAT_PROXY) && sub) { - hb_send(model); + bt_mesh_heartbeat_send(); } send_status: @@ -1015,7 +966,7 @@ static void relay_set(struct bt_mesh_model *model, sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx); if ((cfg->hb_pub.feat & BLE_MESH_FEAT_RELAY) && sub && change) { - hb_send(model); + bt_mesh_heartbeat_send(); } } else { BT_WARN("Invalid Relay value 0x%02x", buf->data[0]); @@ -2767,7 +2718,7 @@ static void friend_set(struct bt_mesh_model *model, sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx); if ((cfg->hb_pub.feat & BLE_MESH_FEAT_FRIEND) && sub) { - hb_send(model); + bt_mesh_heartbeat_send(); } send_status: @@ -3279,7 +3230,6 @@ static void hb_publish(struct k_work *work) struct bt_mesh_cfg_srv *cfg = CONTAINER_OF(work, struct bt_mesh_cfg_srv, hb_pub.timer.work); - struct bt_mesh_model *model = cfg->model; struct bt_mesh_subnet *sub; u16_t period_ms; @@ -3302,7 +3252,7 @@ static void hb_publish(struct k_work *work) k_delayed_work_submit(&cfg->hb_pub.timer, period_ms); } - hb_send(model); + bt_mesh_heartbeat_send(); if (cfg->hb_pub.count != 0xffff) { cfg->hb_pub.count--; diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 79f59b0ceb..cfbfd888cb 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -32,7 +32,7 @@ #include "transport.h" #include "mesh_common.h" #include "client_common.h" -#include "provisioner_main.h" +#include "cfg_srv.h" /* The transport layer needs at least three buffers for itself to avoid * deadlocks. Ensure that there are a sufficient number of advertising @@ -1575,3 +1575,52 @@ void bt_mesh_rpl_clear(void) BT_DBG("%s", __func__); (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); } + +void bt_mesh_heartbeat_send(void) +{ + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); + u16_t feat = 0U; + struct __packed { + u8_t init_ttl; + u16_t feat; + } hb; + struct bt_mesh_msg_ctx ctx = { + .net_idx = cfg->hb_pub.net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = cfg->hb_pub.dst, + .send_ttl = cfg->hb_pub.ttl, + }; + struct bt_mesh_net_tx tx = { + .sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx), + .ctx = &ctx, + .src = bt_mesh_model_elem(cfg->model)->addr, + .xmit = bt_mesh_net_transmit_get(), + }; + + hb.init_ttl = cfg->hb_pub.ttl; + + if (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED) { + feat |= BLE_MESH_FEAT_RELAY; + } + + if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) { + feat |= BLE_MESH_FEAT_PROXY; + } + + if (bt_mesh_friend_get() == BLE_MESH_FRIEND_ENABLED) { + feat |= BLE_MESH_FEAT_FRIEND; + } + +#if defined(CONFIG_BLE_MESH_LOW_POWER) + if (bt_mesh.lpn.state != BLE_MESH_LPN_DISABLED) { + feat |= BLE_MESH_FEAT_LOW_POWER; + } +#endif + + hb.feat = sys_cpu_to_be16(feat); + + BT_DBG("InitTTL %u feat 0x%04x", cfg->hb_pub.ttl, feat); + + bt_mesh_ctl_send(&tx, TRANS_CTL_OP_HEARTBEAT, &hb, sizeof(hb), + NULL, NULL, NULL); +} diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.h b/components/bt/esp_ble_mesh/mesh_core/transport.h index 13845f345c..50a7aae1e8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.h +++ b/components/bt/esp_ble_mesh/mesh_core/transport.h @@ -99,4 +99,6 @@ void bt_mesh_trans_init(void); void bt_mesh_rpl_clear(void); +void bt_mesh_heartbeat_send(void); + #endif /* _TRANSPORT_H_ */ From 2afc9b820d755990a97d59e4ce898218ddcdb1b1 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:51:35 +0800 Subject: [PATCH 116/146] ble_mesh: remove some useless copy during ecdh calculation --- .../mesh_core/mesh_bearer_adapt.c | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c index 67bea67e03..8724bf8573 100644 --- a/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c @@ -1653,8 +1653,9 @@ void bt_mesh_set_private_key(const u8_t pri_key[32]) const u8_t *bt_mesh_pub_key_get(void) { - Point public_key; - BT_OCTET32 pri_key; + BT_OCTET32 private_key = {0}; + Point public_key = {0}; + #if 1 if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY)) { return bt_mesh_public_key; @@ -1665,13 +1666,14 @@ const u8_t *bt_mesh_pub_key_get(void) * Note: if enabled, when Provisioner provision multiple devices * at the same time, this may cause invalid confirmation value. */ - if (bt_mesh_rand(bt_mesh_private_key, 32)) { + if (bt_mesh_rand(bt_mesh_private_key, BT_OCTET32_LEN)) { BT_ERR("%s, Unable to generate bt_mesh_private_key", __func__); return NULL; } #endif - mem_rcopy(pri_key, bt_mesh_private_key, 32); - ECC_PointMult(&public_key, &(curve_p256.G), (DWORD *)pri_key, KEY_LENGTH_DWORDS_P256); + + memcpy(private_key, bt_mesh_private_key, BT_OCTET32_LEN); + ECC_PointMult(&public_key, &(curve_p256.G), (DWORD *)private_key, KEY_LENGTH_DWORDS_P256); memcpy(bt_mesh_public_key, public_key.x, BT_OCTET32_LEN); memcpy(bt_mesh_public_key + BT_OCTET32_LEN, public_key.y, BT_OCTET32_LEN); @@ -1697,29 +1699,26 @@ bool bt_mesh_check_public_key(const u8_t key[64]) int bt_mesh_dh_key_gen(const u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u8_t idx) { - BT_OCTET32 private_key; - Point peer_publ_key; - Point new_publ_key; - BT_OCTET32 dhkey; + BT_OCTET32 private_key = {0}; + Point peer_pub_key = {0}; + Point new_pub_key = {0}; BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, BT_OCTET32_LEN)); - mem_rcopy(private_key, bt_mesh_private_key, BT_OCTET32_LEN); - memcpy(peer_publ_key.x, remote_pk, BT_OCTET32_LEN); - memcpy(peer_publ_key.y, &remote_pk[BT_OCTET32_LEN], BT_OCTET32_LEN); + memcpy(private_key, bt_mesh_private_key, BT_OCTET32_LEN); + memcpy(peer_pub_key.x, remote_pk, BT_OCTET32_LEN); + memcpy(peer_pub_key.y, &remote_pk[BT_OCTET32_LEN], BT_OCTET32_LEN); - BT_DBG("remote public key x = %s", bt_hex(peer_publ_key.x, BT_OCTET32_LEN)); - BT_DBG("remote public key y = %s", bt_hex(peer_publ_key.y, BT_OCTET32_LEN)); + BT_DBG("remote public key x = %s", bt_hex(peer_pub_key.x, BT_OCTET32_LEN)); + BT_DBG("remote public key y = %s", bt_hex(peer_pub_key.y, BT_OCTET32_LEN)); - ECC_PointMult(&new_publ_key, &peer_publ_key, (DWORD *) private_key, KEY_LENGTH_DWORDS_P256); + ECC_PointMult(&new_pub_key, &peer_pub_key, (DWORD *)private_key, KEY_LENGTH_DWORDS_P256); - memcpy(dhkey, new_publ_key.x, BT_OCTET32_LEN); - - BT_DBG("new public key x = %s", bt_hex(new_publ_key.x, 32)); - BT_DBG("new public key y = %s", bt_hex(new_publ_key.y, 32)); + BT_DBG("new public key x = %s", bt_hex(new_pub_key.x, 32)); + BT_DBG("new public key y = %s", bt_hex(new_pub_key.y, 32)); if (cb != NULL) { - cb((const u8_t *)dhkey, idx); + cb((const u8_t *)new_pub_key.x, idx); } return 0; From f449cb7843e48cb7f3827ce98c5d39a1edfa8871 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:55:09 +0800 Subject: [PATCH 117/146] ble_mesh: fix canceled buffer memory leak --- components/bt/esp_ble_mesh/mesh_core/adv.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index a1c4cf9825..21fd0580c9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -189,6 +189,8 @@ static void adv_thread(void *p) BT_ERR("%s, xQueueSendToFront failed", __func__); } } + } else { + net_buf_unref(*buf); } /* Give other threads a chance to run */ From e152df43f14cf46099e15b4ebf35db31ae8873e1 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 14:59:41 +0800 Subject: [PATCH 118/146] ble_mesh: fix not sending all segments through the Friend Queue --- .../bt/esp_ble_mesh/mesh_core/transport.c | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index cfbfd888cb..552bc653fd 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -391,8 +391,6 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, net_buf_add_mem(seg, sdu->data, len); net_buf_simple_pull(sdu, len); - tx->seg[seg_o] = net_buf_ref(seg); - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { enum bt_mesh_friend_pdu_type type; @@ -411,11 +409,13 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, * out through the Friend Queue. */ net_buf_unref(seg); - return 0; + continue; } } } + tx->seg[seg_o] = net_buf_ref(seg); + BT_DBG("Sending %u/%u", seg_o, tx->seg_n); err = bt_mesh_net_send(net_tx, seg, @@ -428,6 +428,23 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, } } + /* This can happen if segments only went into the Friend Queue */ + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !tx->seg[0]) { + seg_tx_reset(tx); + /* If there was a callback notify sending immediately since + * there's no other way to track this (at least currently) + * with the Friend Queue. + */ + if (cb) { + if (cb->start) { + cb->start(0, 0, cb_data); + } + if (cb->end) { + cb->end(0, cb_data); + } + } + } + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && bt_mesh_lpn_established()) { From 10bffaebf1fa232761ad593edf500bb696a81686 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:03:42 +0800 Subject: [PATCH 119/146] ble_mesh: fix resending segments on correct bearer --- components/bt/esp_ble_mesh/mesh_core/net.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 79d27c70d6..4e3cac55c7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -731,6 +731,7 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, { const u8_t *enc, *priv; u32_t seq; + u16_t dst; int err; BT_DBG("net_idx 0x%04x new_key %u len %u", sub->net_idx, new_key, @@ -757,6 +758,9 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, buf->data[3] = seq >> 8; buf->data[4] = seq; + /* Get destination, in case it's a proxy client */ + dst = DST(buf->data); + err = bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false); if (err) { BT_ERR("%s, Encrypt failed (err %d)", __func__, err); @@ -769,7 +773,12 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, return err; } - bt_mesh_adv_send(buf, cb, cb_data); + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY) && + bt_mesh_proxy_relay(&buf->b, dst)) { + net_buf_unref(buf); + } else { + bt_mesh_adv_send(buf, cb, cb_data); + } if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) && bt_mesh.seq > IV_UPDATE_SEQ_LIMIT) { From 3f6968e31b99ae2121100c98b4d3e4bb3a8a6382 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:04:52 +0800 Subject: [PATCH 120/146] ble_mesh: fix canceling publication retransmit timer --- components/bt/esp_ble_mesh/mesh_core/access.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index 529d8ebab1..8b04e926cf 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -326,11 +326,6 @@ static void mod_publish(struct k_work *work) if (err) { BT_ERR("%s, Publishing failed (err %d)", __func__, err); } - - if (pub->count) { - /* Retransmissions also control the timer */ - k_delayed_work_cancel(&pub->timer); - } } struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod) From b9c43cec19809b2fc5b580dfbfbd398b043e7dd3 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:07:20 +0800 Subject: [PATCH 121/146] ble_mesh: fix starting iv update when not on primary subnet --- components/bt/esp_ble_mesh/mesh_core/net.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 4e3cac55c7..667be152ff 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -722,6 +722,16 @@ u32_t bt_mesh_next_seq(void) bt_mesh_store_seq(); } + if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) && + bt_mesh.seq > IV_UPDATE_SEQ_LIMIT && + bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY)) { +#if CONFIG_BLE_MESH_NODE + bt_mesh_beacon_ivu_initiator(true); +#endif + bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); + bt_mesh_net_sec_update(NULL); + } + return seq; } @@ -780,15 +790,6 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, bt_mesh_adv_send(buf, cb, cb_data); } - if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) && - bt_mesh.seq > IV_UPDATE_SEQ_LIMIT) { -#if CONFIG_BLE_MESH_NODE - bt_mesh_beacon_ivu_initiator(true); -#endif - bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); - bt_mesh_net_sec_update(NULL); - } - return 0; } From 9f80c24b74641ec640411e7dfe8e0ce462a6a050 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:09:48 +0800 Subject: [PATCH 122/146] ble_mesh: fix checking for active heartbeat publication --- components/bt/esp_ble_mesh/mesh_core/cfg_srv.c | 12 +++--------- components/bt/esp_ble_mesh/mesh_core/transport.c | 5 +++++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c index 9e1db8517e..82e0989006 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -787,7 +787,6 @@ static void gatt_proxy_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_cfg_srv *cfg = model->user_data; - struct bt_mesh_subnet *sub; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -846,8 +845,7 @@ static void gatt_proxy_set(struct bt_mesh_model *model, bt_mesh_adv_update(); #endif - sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx); - if ((cfg->hb_pub.feat & BLE_MESH_FEAT_PROXY) && sub) { + if (cfg->hb_pub.feat & BLE_MESH_FEAT_PROXY) { bt_mesh_heartbeat_send(); } @@ -943,7 +941,6 @@ static void relay_set(struct bt_mesh_model *model, if (!cfg) { BT_WARN("No Configuration Server context available"); } else if (buf->data[0] == 0x00 || buf->data[0] == 0x01) { - struct bt_mesh_subnet *sub; bool change; if (cfg->relay == BLE_MESH_RELAY_NOT_SUPPORTED) { @@ -964,8 +961,7 @@ static void relay_set(struct bt_mesh_model *model, BLE_MESH_TRANSMIT_COUNT(cfg->relay_retransmit), BLE_MESH_TRANSMIT_INT(cfg->relay_retransmit)); - sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx); - if ((cfg->hb_pub.feat & BLE_MESH_FEAT_RELAY) && sub && change) { + if ((cfg->hb_pub.feat & BLE_MESH_FEAT_RELAY) && change) { bt_mesh_heartbeat_send(); } } else { @@ -2682,7 +2678,6 @@ static void friend_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_cfg_srv *cfg = model->user_data; - struct bt_mesh_subnet *sub; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -2716,8 +2711,7 @@ static void friend_set(struct bt_mesh_model *model, } } - sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx); - if ((cfg->hb_pub.feat & BLE_MESH_FEAT_FRIEND) && sub) { + if (cfg->hb_pub.feat & BLE_MESH_FEAT_FRIEND) { bt_mesh_heartbeat_send(); } diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 552bc653fd..ed244ccb8f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -1614,6 +1614,11 @@ void bt_mesh_heartbeat_send(void) .xmit = bt_mesh_net_transmit_get(), }; + /* Do nothing if heartbeat publication is not enabled */ + if (cfg->hb_pub.dst == BLE_MESH_ADDR_UNASSIGNED) { + return; + } + hb.init_ttl = cfg->hb_pub.ttl; if (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED) { From 05128c98ec424c37150817a965a7b51b6de140c6 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:12:12 +0800 Subject: [PATCH 123/146] ble_mesh: fix heartbeat sending on friendship established/lost --- components/bt/esp_ble_mesh/mesh_core/lpn.c | 12 ++++++++++++ components/bt/esp_ble_mesh/mesh_core/transport.c | 4 +--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.c b/components/bt/esp_ble_mesh/mesh_core/lpn.c index 7788ee7503..ad301db06e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/lpn.c +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.c @@ -26,6 +26,7 @@ #include "beacon.h" #include "foundation.h" #include "lpn.h" +#include "cfg_srv.h" #ifdef CONFIG_BLE_MESH_LOW_POWER @@ -201,6 +202,7 @@ static int send_friend_clear(void) static void clear_friendship(bool force, bool disable) { + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); struct bt_mesh_lpn *lpn = &bt_mesh.lpn; BT_DBG("force %u disable %u", force, disable); @@ -248,6 +250,10 @@ static void clear_friendship(bool force, bool disable) */ lpn->groups_changed = 1U; + if (cfg->hb_pub.feat & BLE_MESH_FEAT_LOW_POWER) { + bt_mesh_heartbeat_send(); + } + if (disable) { lpn_set_state(BLE_MESH_LPN_DISABLED); return; @@ -952,6 +958,8 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, } if (!lpn->established) { + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); + /* This is normally checked on the transport layer, however * in this state we're also still accepting master * credentials so we need to ensure the right ones (Friend @@ -966,6 +974,10 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, BT_INFO("Friendship established with 0x%04x", lpn->frnd); + if (cfg->hb_pub.feat & BLE_MESH_FEAT_LOW_POWER) { + bt_mesh_heartbeat_send(); + } + if (lpn_cb) { lpn_cb(lpn->frnd, true); } diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index ed244ccb8f..d358d9d446 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -1633,11 +1633,9 @@ void bt_mesh_heartbeat_send(void) feat |= BLE_MESH_FEAT_FRIEND; } -#if defined(CONFIG_BLE_MESH_LOW_POWER) - if (bt_mesh.lpn.state != BLE_MESH_LPN_DISABLED) { + if (bt_mesh_lpn_established()) { feat |= BLE_MESH_FEAT_LOW_POWER; } -#endif hb.feat = sys_cpu_to_be16(feat); From 9a672caa7ee5524012a42031bdc93055e864ecdd Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:15:28 +0800 Subject: [PATCH 124/146] ble_mesh: introduce a helper for send callback function --- components/bt/esp_ble_mesh/mesh_core/net.c | 12 ++---------- components/bt/esp_ble_mesh/mesh_core/net.h | 16 ++++++++++++++++ components/bt/esp_ble_mesh/mesh_core/transport.c | 10 ++-------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 667be152ff..59576349d5 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -785,7 +785,7 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY) && bt_mesh_proxy_relay(&buf->b, dst)) { - net_buf_unref(buf); + send_cb_finalize(cb, cb_data); } else { bt_mesh_adv_send(buf, cb, cb_data); } @@ -901,15 +901,7 @@ int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, /* Notify completion if this only went * through the Mesh Proxy. */ - if (cb) { - if (cb->start) { - cb->start(0, 0, cb_data); - } - - if (cb->end) { - cb->end(0, cb_data); - } - } + send_cb_finalize(cb, cb_data); err = 0; goto done; diff --git a/components/bt/esp_ble_mesh/mesh_core/net.h b/components/bt/esp_ble_mesh/mesh_core/net.h index 28cd91ce87..49907cef0b 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.h +++ b/components/bt/esp_ble_mesh/mesh_core/net.h @@ -385,4 +385,20 @@ struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, u16_t addr, void friend_cred_clear(struct friend_cred *cred); int friend_cred_del(u16_t net_idx, u16_t addr); +static inline void send_cb_finalize(const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + if (!cb) { + return; + } + + if (cb->start) { + cb->start(0, 0, cb_data); + } + + if (cb->end) { + cb->end(0, cb_data); + } +} + #endif /* _NET_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index d358d9d446..359dffac4b 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -148,6 +148,7 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, * out through the Friend Queue. */ net_buf_unref(buf); + send_cb_finalize(cb, cb_data); return 0; } } @@ -435,14 +436,7 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, * there's no other way to track this (at least currently) * with the Friend Queue. */ - if (cb) { - if (cb->start) { - cb->start(0, 0, cb_data); - } - if (cb->end) { - cb->end(0, cb_data); - } - } + send_cb_finalize(cb, cb_data); } if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { From 386a5d649cb948ed3195b8d94aa5ef888f2ac1d2 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:17:20 +0800 Subject: [PATCH 125/146] ble_mesh: lpn remove msg from cache on rejection --- components/bt/esp_ble_mesh/mesh_core/net.c | 17 +++++++++++++++-- components/bt/esp_ble_mesh/mesh_core/net.h | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 59576349d5..ec13cbf359 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -135,7 +135,8 @@ static bool msg_cache_match(struct bt_mesh_net_rx *rx, } /* Add to the cache */ - msg_cache[msg_cache_next++] = hash; + rx->msg_cache_idx = msg_cache_next++; + msg_cache[rx->msg_cache_idx] = hash; msg_cache_next %= ARRAY_SIZE(msg_cache); return false; @@ -1380,7 +1381,19 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) || bt_mesh_elem_find(rx.ctx.recv_dst)); - bt_mesh_trans_recv(&buf, &rx); + /* The transport layer has indicated that it has rejected the message, + * but would like to see it again if it is received in the future. + * This can happen if a message is received when the device is in + * Low Power mode, but the message was not encrypted with the friend + * credentials. Remove it from the message cache so that we accept + * it again in the future. + */ + if (bt_mesh_trans_recv(&buf, &rx) == -EAGAIN) { + BT_WARN("Removing rejected message from Network Message Cache"); + msg_cache[rx.msg_cache_idx] = 0ULL; + /* Rewind the next index now that we're not using this entry */ + msg_cache_next = rx.msg_cache_idx; + } /* Relay if this was a group/virtual address, or if the destination * was neither a local element nor an LPN we're Friends for. diff --git a/components/bt/esp_ble_mesh/mesh_core/net.h b/components/bt/esp_ble_mesh/mesh_core/net.h index 49907cef0b..75e60c8486 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.h +++ b/components/bt/esp_ble_mesh/mesh_core/net.h @@ -289,6 +289,7 @@ struct bt_mesh_net_rx { net_if: 2, /* Network interface */ local_match: 1, /* Matched a local element */ friend_match: 1; /* Matched an LPN we're friends for */ + u16_t msg_cache_idx; /* Index of entry in message cache */ s8_t rssi; }; From 29de1a9acfc99387c435fe79fdc250b19731b40e Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:18:33 +0800 Subject: [PATCH 126/146] ble_mesh: remove useless code bt_mesh_trans_resend() --- .../bt/esp_ble_mesh/mesh_core/transport.c | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 359dffac4b..8246d3316a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -547,25 +547,6 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, return err; } -int bt_mesh_trans_resend(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, - const struct bt_mesh_send_cb *cb, void *cb_data) -{ - struct net_buf_simple_state state; - int err; - - net_buf_simple_save(msg, &state); - - if (tx->ctx->send_rel || msg->len > 15) { - err = send_seg(tx, msg, cb, cb_data); - } else { - err = send_unseg(tx, msg, cb, cb_data); - } - - net_buf_simple_restore(msg, &state); - - return err; -} - static void update_rpl(struct bt_mesh_rpl *rpl, struct bt_mesh_net_rx *rx) { rpl->src = rx->ctx.addr; From 8a7ab6cb3d5f4d5c2ff022a1f1e464e370198747 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:30:40 +0800 Subject: [PATCH 127/146] ble_mesh: fix adhering to the configured Friend Queue size --- components/bt/esp_ble_mesh/mesh_core/friend.c | 254 +++++++++++++----- components/bt/esp_ble_mesh/mesh_core/friend.h | 9 +- components/bt/esp_ble_mesh/mesh_core/net.h | 6 + .../bt/esp_ble_mesh/mesh_core/transport.c | 65 ++++- 4 files changed, 256 insertions(+), 78 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.c b/components/bt/esp_ble_mesh/mesh_core/friend.c index de8ca4cc2c..3d637d9bd2 100644 --- a/components/bt/esp_ble_mesh/mesh_core/friend.c +++ b/components/bt/esp_ble_mesh/mesh_core/friend.c @@ -70,48 +70,6 @@ static struct bt_mesh_adv *adv_alloc(int id) return &adv_pool[id].adv; } -static void discard_buffer(void) -{ - struct bt_mesh_friend *frnd = &bt_mesh.frnd[0]; - struct net_buf *buf; - int i; - - /* Find the Friend context with the most queued buffers */ - for (i = 1; i < ARRAY_SIZE(bt_mesh.frnd); i++) { - if (bt_mesh.frnd[i].queue_size > frnd->queue_size) { - frnd = &bt_mesh.frnd[i]; - } - } - - buf = net_buf_slist_get(&frnd->queue); - __ASSERT_NO_MSG(buf != NULL); - BT_WARN("Discarding buffer %p for LPN 0x%04x", buf, frnd->lpn); - net_buf_unref(buf); -} - -static struct net_buf *friend_buf_alloc(u16_t src) -{ - struct net_buf *buf; - - BT_DBG("src 0x%04x", src); - - do { - buf = bt_mesh_adv_create_from_pool(&friend_buf_pool, adv_alloc, - BLE_MESH_ADV_DATA, - FRIEND_XMIT, K_NO_WAIT); - if (!buf) { - discard_buffer(); - } - } while (!buf); - - BLE_MESH_ADV(buf)->addr = src; - FRIEND_ADV(buf)->seq_auth = TRANS_SEQ_AUTH_NVAL; - - BT_DBG("allocated buf %p", buf); - - return buf; -} - static bool is_lpn_unicast(struct bt_mesh_friend *frnd, u16_t addr) { if (frnd->lpn == BLE_MESH_ADDR_UNASSIGNED) { @@ -151,6 +109,20 @@ struct bt_mesh_friend *bt_mesh_friend_find(u16_t net_idx, u16_t lpn_addr, return NULL; } +static void purge_buffers(sys_slist_t *list) +{ + while (!sys_slist_is_empty(list)) { + struct net_buf *buf; + + buf = (void *)sys_slist_get_not_empty(list); + + buf->frags = NULL; + buf->flags &= ~NET_BUF_FRAGS; + + net_buf_unref(buf); + } +} + /* Intentionally start a little bit late into the ReceiveWindow when * it's large enough. This may improve reliability with some platforms, * like the PTS, where the receiver might not have sufficiently compensated @@ -185,16 +157,13 @@ static void friend_clear(struct bt_mesh_friend *frnd) frnd->last = NULL; } - while (!sys_slist_is_empty(&frnd->queue)) { - net_buf_unref(net_buf_slist_get(&frnd->queue)); - } + purge_buffers(&frnd->queue); for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { struct bt_mesh_friend_seg *seg = &frnd->seg[i]; - while (!sys_slist_is_empty(&seg->queue)) { - net_buf_unref(net_buf_slist_get(&seg->queue)); - } + purge_buffers(&seg->queue); + seg->seg_count = 0U; } frnd->valid = 0U; @@ -336,7 +305,15 @@ static struct net_buf *create_friend_pdu(struct bt_mesh_friend *frnd, sub = bt_mesh_subnet_get(frnd->net_idx); __ASSERT_NO_MSG(sub != NULL); - buf = friend_buf_alloc(info->src); + buf = bt_mesh_adv_create_from_pool(&friend_buf_pool, adv_alloc, + BLE_MESH_ADV_DATA, + FRIEND_XMIT, K_NO_WAIT); + if (!buf) { + return NULL; + } + + BLE_MESH_ADV(buf)->addr = info->src; + FRIEND_ADV(buf)->seq_auth = TRANS_SEQ_AUTH_NVAL; /* Friend Offer needs master security credentials */ if (info->ctl && TRANS_CTL_OP(sdu->data) == TRANS_CTL_OP_FRIEND_OFFER) { @@ -897,7 +874,8 @@ init_friend: } static struct bt_mesh_friend_seg *get_seg(struct bt_mesh_friend *frnd, - u16_t src, u64_t *seq_auth) + u16_t src, u64_t *seq_auth, + u8_t seg_count) { struct bt_mesh_friend_seg *unassigned = NULL; int i; @@ -916,12 +894,16 @@ static struct bt_mesh_friend_seg *get_seg(struct bt_mesh_friend *frnd, } } + if (unassigned) { + unassigned->seg_count = seg_count; + } + return unassigned; } static void enqueue_friend_pdu(struct bt_mesh_friend *frnd, enum bt_mesh_friend_pdu_type type, - struct net_buf *buf) + u8_t seg_count, struct net_buf *buf) { struct bt_mesh_friend_seg *seg; struct friend_adv *adv; @@ -938,7 +920,7 @@ static void enqueue_friend_pdu(struct bt_mesh_friend *frnd, } adv = FRIEND_ADV(buf); - seg = get_seg(frnd, BLE_MESH_ADV(buf)->addr, &adv->seq_auth); + seg = get_seg(frnd, BLE_MESH_ADV(buf)->addr, &adv->seq_auth, seg_count); if (!seg) { BT_ERR("%s, No free friend segment RX contexts for 0x%04x", __func__, BLE_MESH_ADV(buf)->addr); @@ -963,6 +945,10 @@ static void enqueue_friend_pdu(struct bt_mesh_friend *frnd, } sys_slist_merge_slist(&frnd->queue, &seg->queue); + seg->seg_count = 0U; + } else { + /* Mark the buffer as having more to come after it */ + buf->flags |= NET_BUF_FRAGS; } } @@ -1028,13 +1014,17 @@ static void friend_timeout(struct k_work *work) return; } - frnd->last = net_buf_slist_get(&frnd->queue); + frnd->last = (void *)sys_slist_get(&frnd->queue); if (!frnd->last) { BT_WARN("%s, Friendship not established with 0x%04x", __func__, frnd->lpn); friend_clear(frnd); return; } + /* Clear the flag we use for segment tracking */ + frnd->last->flags &= ~NET_BUF_FRAGS; + frnd->last->frags = NULL; + BT_DBG("Sending buf %p from Friend Queue of LPN 0x%04x", frnd->last, frnd->lpn); frnd->queue_size--; @@ -1097,7 +1087,8 @@ static void friend_purge_old_ack(struct bt_mesh_friend *frnd, u64_t *seq_auth, static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd, struct bt_mesh_net_rx *rx, enum bt_mesh_friend_pdu_type type, - u64_t *seq_auth, struct net_buf_simple *sbuf) + u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) { struct friend_pdu_info info; struct net_buf *buf; @@ -1135,7 +1126,7 @@ static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd, FRIEND_ADV(buf)->seq_auth = *seq_auth; } - enqueue_friend_pdu(frnd, type, buf); + enqueue_friend_pdu(frnd, type, seg_count, buf); BT_DBG("Queued message for LPN 0x%04x, queue_size %u", frnd->lpn, frnd->queue_size); @@ -1144,7 +1135,8 @@ static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd, static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd, struct bt_mesh_net_tx *tx, enum bt_mesh_friend_pdu_type type, - u64_t *seq_auth, struct net_buf_simple *sbuf) + u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) { struct friend_pdu_info info; struct net_buf *buf; @@ -1179,7 +1171,7 @@ static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd, FRIEND_ADV(buf)->seq_auth = *seq_auth; } - enqueue_friend_pdu(frnd, type, buf); + enqueue_friend_pdu(frnd, type, seg_count, buf); BT_DBG("Queued message for LPN 0x%04x", frnd->lpn); } @@ -1229,9 +1221,118 @@ bool bt_mesh_friend_match(u16_t net_idx, u16_t addr) return false; } +static bool friend_queue_has_space(struct bt_mesh_friend *frnd, u16_t addr, + u64_t *seq_auth, u8_t seg_count) +{ + u32_t total = 0U; + int i; + + if (seg_count > CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE) { + return false; + } + + for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { + struct bt_mesh_friend_seg *seg = &frnd->seg[i]; + + if (seq_auth) { + struct net_buf *buf; + + /* If there's a segment queue for this message then the + * space verification has already happened. + */ + buf = (void *)sys_slist_peek_head(&seg->queue); + if (buf && BLE_MESH_ADV(buf)->addr == addr && + FRIEND_ADV(buf)->seq_auth == *seq_auth) { + return true; + } + } + + total += seg->seg_count; + } + + /* If currently pending segments combined with this segmented message + * are more than the Friend Queue Size, then there's no space. This + * is because we don't have a mechanism of aborting already pending + * segmented messages to free up buffers. + */ + return (CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE - total) > seg_count; +} + +bool bt_mesh_friend_queue_has_space(u16_t net_idx, u16_t src, u16_t dst, + u64_t *seq_auth, u8_t seg_count) +{ + bool someone_has_space = false, friend_match = false; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (!friend_lpn_matches(frnd, net_idx, dst)) { + continue; + } + + friend_match = true; + + if (friend_queue_has_space(frnd, src, seq_auth, seg_count)) { + someone_has_space = true; + } + } + + /* If there were no matched LPNs treat this as success, so the + * transport layer can continue its work. + */ + if (!friend_match) { + return true; + } + + /* From the transport layers perspective it's good enough that at + * least one Friend Queue has space. If there were multiple Friend + * matches then the destination must be a group address, in which + * case e.g. segment acks are not sent. + */ + return someone_has_space; +} + +static bool friend_queue_prepare_space(struct bt_mesh_friend *frnd, u16_t addr, + u64_t *seq_auth, u8_t seg_count) +{ + bool pending_segments; + u8_t avail_space; + + if (!friend_queue_has_space(frnd, addr, seq_auth, seg_count)) { + return false; + } + + avail_space = CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE - frnd->queue_size; + pending_segments = false; + + while (pending_segments || avail_space < seg_count) { + struct net_buf *buf = (void *)sys_slist_get(&frnd->queue); + + if (!buf) { + BT_ERR("Unable to free up enough buffers"); + return false; + } + + frnd->queue_size--; + avail_space++; + + pending_segments = (buf->flags & NET_BUF_FRAGS); + + /* Make sure old slist entry state doesn't remain */ + buf->frags = NULL; + buf->flags &= ~NET_BUF_FRAGS; + + net_buf_unref(buf); + } + + return true; +} + void bt_mesh_friend_enqueue_rx(struct bt_mesh_net_rx *rx, enum bt_mesh_friend_pdu_type type, - u64_t *seq_auth, struct net_buf_simple *sbuf) + u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) { int i; @@ -1248,16 +1349,25 @@ void bt_mesh_friend_enqueue_rx(struct bt_mesh_net_rx *rx, for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; - if (friend_lpn_matches(frnd, rx->sub->net_idx, - rx->ctx.recv_dst)) { - friend_lpn_enqueue_rx(frnd, rx, type, seq_auth, sbuf); + if (!friend_lpn_matches(frnd, rx->sub->net_idx, + rx->ctx.recv_dst)) { + continue; } + + if (!friend_queue_prepare_space(frnd, rx->ctx.addr, seq_auth, + seg_count)) { + continue; + } + + friend_lpn_enqueue_rx(frnd, rx, type, seq_auth, seg_count, + sbuf); } } bool bt_mesh_friend_enqueue_tx(struct bt_mesh_net_tx *tx, enum bt_mesh_friend_pdu_type type, - u64_t *seq_auth, struct net_buf_simple *sbuf) + u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) { bool matched = false; int i; @@ -1273,10 +1383,19 @@ bool bt_mesh_friend_enqueue_tx(struct bt_mesh_net_tx *tx, for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; - if (friend_lpn_matches(frnd, tx->sub->net_idx, tx->ctx->addr)) { - friend_lpn_enqueue_tx(frnd, tx, type, seq_auth, sbuf); - matched = true; + if (!friend_lpn_matches(frnd, tx->sub->net_idx, + tx->ctx->addr)) { + continue; } + + if (!friend_queue_prepare_space(frnd, tx->src, seq_auth, + seg_count)) { + continue; + } + + friend_lpn_enqueue_tx(frnd, tx, type, seq_auth, seg_count, + sbuf); + matched = true; } return matched; @@ -1316,9 +1435,8 @@ void bt_mesh_friend_clear_incomplete(struct bt_mesh_subnet *sub, u16_t src, BT_WARN("%s, Clearing incomplete segments for 0x%04x", __func__, src); - while (!sys_slist_is_empty(&seg->queue)) { - net_buf_unref(net_buf_slist_get(&seg->queue)); - } + purge_buffers(&seg->queue); + seg->seg_count = 0U; } } } diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.h b/components/bt/esp_ble_mesh/mesh_core/friend.h index 008a342c9b..c6ecf7e6c3 100644 --- a/components/bt/esp_ble_mesh/mesh_core/friend.h +++ b/components/bt/esp_ble_mesh/mesh_core/friend.h @@ -20,12 +20,17 @@ bool bt_mesh_friend_match(u16_t net_idx, u16_t addr); struct bt_mesh_friend *bt_mesh_friend_find(u16_t net_idx, u16_t lpn_addr, bool valid, bool established); +bool bt_mesh_friend_queue_has_space(u16_t net_idx, u16_t src, u16_t dst, + u64_t *seq_auth, u8_t seg_count); + void bt_mesh_friend_enqueue_rx(struct bt_mesh_net_rx *rx, enum bt_mesh_friend_pdu_type type, - u64_t *seq_auth, struct net_buf_simple *sbuf); + u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf); bool bt_mesh_friend_enqueue_tx(struct bt_mesh_net_tx *tx, enum bt_mesh_friend_pdu_type type, - u64_t *seq_auth, struct net_buf_simple *sbuf); + u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf); void bt_mesh_friend_clear_incomplete(struct bt_mesh_subnet *sub, u16_t src, u16_t dst, u64_t *seq_auth); diff --git a/components/bt/esp_ble_mesh/mesh_core/net.h b/components/bt/esp_ble_mesh/mesh_core/net.h index 75e60c8486..150e2ff361 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.h +++ b/components/bt/esp_ble_mesh/mesh_core/net.h @@ -115,6 +115,12 @@ struct bt_mesh_friend { struct bt_mesh_friend_seg { sys_slist_t queue; + + /* The target number of segments, i.e. not necessarily + * the current number of segments, in the queue. This is + * used for Friend Queue free space calculations. + */ + u8_t seg_count; } seg[FRIEND_SEG_RX]; struct net_buf *last; diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 8246d3316a..19a3b0c668 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -141,8 +141,21 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + if (!bt_mesh_friend_queue_has_space(tx->sub->net_idx, + tx->src, tx->ctx->addr, + NULL, 1)) { + if (BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + BT_ERR("Not enough space in Friend Queue"); + net_buf_unref(buf); + return -ENOBUFS; + } else { + BT_WARN("No space in Friend Queue"); + goto send; + } + } + if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE, - NULL, &buf->b) && + NULL, 1, &buf->b) && BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { /* PDUs for a specific Friend should only go * out through the Friend Queue. @@ -154,6 +167,7 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, } } +send: return bt_mesh_net_send(tx, buf, cb, cb_data); } @@ -365,6 +379,17 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, BT_DBG("SeqZero 0x%04x", seq_zero); + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && + !bt_mesh_friend_queue_has_space(tx->sub->net_idx, net_tx->src, + tx->dst, &tx->seq_auth, + tx->seg_n + 1) && + BLE_MESH_ADDR_IS_UNICAST(tx->dst)) { + BT_ERR("Not enough space in Friend Queue for %u segments", + tx->seg_n + 1); + seg_tx_reset(tx); + return -ENOBUFS; + } + for (seg_o = 0U; sdu->len; seg_o++) { struct net_buf *seg; u16_t len; @@ -404,8 +429,9 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, if (bt_mesh_friend_enqueue_tx(net_tx, type, &tx->seq_auth, + tx->seg_n + 1, &seg->b) && - BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) { + BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) { /* PDUs for a specific Friend should only go * out through the Friend Queue. */ @@ -1029,8 +1055,8 @@ int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE, - seq_auth, &buf->b) && - BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + seq_auth, 1, &buf->b) && + BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { /* PDUs for a specific Friend should only go * out through the Friend Queue. */ @@ -1237,7 +1263,8 @@ static struct seg_rx *seg_rx_alloc(struct bt_mesh_net_rx *net_rx, } static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, - enum bt_mesh_friend_pdu_type *pdu_type, u64_t *seq_auth) + enum bt_mesh_friend_pdu_type *pdu_type, u64_t *seq_auth, + u8_t *seg_count) { struct bt_mesh_rpl *rpl = NULL; struct seg_rx *rx; @@ -1294,6 +1321,8 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, ((((net_rx->seq & BIT_MASK(14)) - seq_zero)) & BIT_MASK(13)))); + *seg_count = seg_n + 1; + /* Look for old RX sessions */ rx = seg_rx_find(net_rx, seq_auth); if (rx) { @@ -1342,6 +1371,22 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, return -EMSGSIZE; } + /* Verify early that there will be space in the Friend Queue(s) in + * case this message is destined to an LPN of ours. + */ + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && + net_rx->friend_match && !net_rx->local_match && + !bt_mesh_friend_queue_has_space(net_rx->sub->net_idx, + net_rx->ctx.addr, + net_rx->ctx.recv_dst, seq_auth, + *seg_count)) { + BT_ERR("No space in Friend Queue for %u segments", *seg_count); + send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr, + net_rx->ctx.send_ttl, seq_auth, 0, + net_rx->friend_match); + return -ENOBUFS; + } + /* Look for free slot for a new RX session */ rx = seg_rx_alloc(net_rx, hdr, seq_auth, seg_n); if (!rx) { @@ -1436,6 +1481,7 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) u64_t seq_auth = TRANS_SEQ_AUTH_NVAL; enum bt_mesh_friend_pdu_type pdu_type = BLE_MESH_FRIEND_PDU_SINGLE; struct net_buf_simple_state state; + u8_t seg_count = 0U; int err; if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { @@ -1479,8 +1525,9 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) return 0; } - err = trans_seg(buf, rx, &pdu_type, &seq_auth); + err = trans_seg(buf, rx, &pdu_type, &seq_auth, &seg_count); } else { + seg_count = 1U; err = trans_unseg(buf, rx, &seq_auth); } @@ -1508,9 +1555,11 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match && !err) { if (seq_auth == TRANS_SEQ_AUTH_NVAL) { - bt_mesh_friend_enqueue_rx(rx, pdu_type, NULL, buf); + bt_mesh_friend_enqueue_rx(rx, pdu_type, NULL, + seg_count, buf); } else { - bt_mesh_friend_enqueue_rx(rx, pdu_type, &seq_auth, buf); + bt_mesh_friend_enqueue_rx(rx, pdu_type, &seq_auth, + seg_count, buf); } } } From 8580a3211b1daf496fef47c948ac0c72c6695d9a Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:32:20 +0800 Subject: [PATCH 128/146] ble_mesh: fix bt_mesh_net_resend() caused compiling error --- components/bt/esp_ble_mesh/mesh_core/net.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index ec13cbf359..a9bbb2e6b8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -784,13 +784,15 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, return err; } + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY) && bt_mesh_proxy_relay(&buf->b, dst)) { send_cb_finalize(cb, cb_data); - } else { - bt_mesh_adv_send(buf, cb, cb_data); + return 0; + } } + bt_mesh_adv_send(buf, cb, cb_data); return 0; } From 715cf7d5782c49bc5e1ab746c51150521fca2cd0 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:35:03 +0800 Subject: [PATCH 129/146] ble_mesh: remove useless mesh_opcode --- components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 8 +++----- components/bt/esp_ble_mesh/mesh_core/access.c | 4 ---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index b0619d7090..a60fa6c0f7 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -266,8 +266,6 @@ static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg) } } -extern u32_t mesh_opcode; - static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) @@ -276,7 +274,7 @@ static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model, btc_msg_t msg = {0}; bt_status_t ret; - mesh_param.model_operation.opcode = mesh_opcode; + mesh_param.model_operation.opcode = ctx->recv_op; mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model; mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx; mesh_param.model_operation.length = buf->len; @@ -320,14 +318,14 @@ static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, false); if (node == NULL) { msg.act = ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT; - mesh_param.client_recv_publish_msg.opcode = mesh_opcode; + mesh_param.client_recv_publish_msg.opcode = ctx->recv_op; mesh_param.client_recv_publish_msg.model = (esp_ble_mesh_model_t *)model; mesh_param.client_recv_publish_msg.ctx = (esp_ble_mesh_msg_ctx_t *)ctx; mesh_param.client_recv_publish_msg.length = buf->len; mesh_param.client_recv_publish_msg.msg = buf->data; } else { msg.act = ESP_BLE_MESH_MODEL_OPERATION_EVT; - mesh_param.model_operation.opcode = mesh_opcode; + mesh_param.model_operation.opcode = ctx->recv_op; mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model; mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx; mesh_param.model_operation.length = buf->len; diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index 8b04e926cf..17f4b88e5e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -611,8 +611,6 @@ bool bt_mesh_fixed_group_match(u16_t addr) } } -u32_t mesh_opcode; - void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { struct bt_mesh_model *models, *model; @@ -632,8 +630,6 @@ void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) BT_DBG("OpCode 0x%08x", opcode); - mesh_opcode = opcode; - for (i = 0; i < dev_comp->elem_count; i++) { struct bt_mesh_elem *elem = &dev_comp->elem[i]; From 8a41132507516dd931853ec0c9682c18ebd531b8 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:37:10 +0800 Subject: [PATCH 130/146] ble_mesh: fix publication period timestamp initialization --- components/bt/esp_ble_mesh/mesh_core/access.c | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index 17f4b88e5e..94374d98e6 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -220,7 +220,24 @@ static void publish_sent(int err, void *user_data) } } +static void publish_start(u16_t duration, int err, void *user_data) +{ + struct bt_mesh_model *mod = user_data; + struct bt_mesh_model_pub *pub = mod->pub; + + if (err) { + BT_ERR("Failed to publish: err %d", err); + return; + } + + /* Initialize the timestamp for the beginning of a new period */ + if (pub->count == BLE_MESH_PUB_TRANSMIT_COUNT(pub->retransmit)) { + pub->period_start = k_uptime_get_32(); + } +} + static const struct bt_mesh_send_cb pub_sent_cb = { + .start = publish_start, .end = publish_sent, }; @@ -310,8 +327,6 @@ static void mod_publish(struct k_work *work) __ASSERT_NO_MSG(pub->update != NULL); - pub->period_start = k_uptime_get_32(); - /* Callback the model publish update event to the application layer. * In the event, users can update the context of the publish message * which will be published in the next period. @@ -856,7 +871,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) BT_DBG("%s", __func__); - if (!pub) { + if (!pub || !pub->msg) { BT_ERR("%s, Model has no publication support", __func__); return -ENOTSUP; } From d48c89b56ae2cc4115435acf20f61c580ce6b8cc Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 2 Sep 2019 15:39:59 +0800 Subject: [PATCH 131/146] ble_mesh: fix ble mesh btc may caused memory leak --- .../btc/btc_ble_mesh_config_model.c | 18 ++++++++++-------- .../btc/btc_ble_mesh_generic_model.c | 18 ++++++++++-------- .../btc/btc_ble_mesh_health_model.c | 18 ++++++++++-------- .../btc/btc_ble_mesh_lighting_model.c | 18 ++++++++++-------- .../btc/btc_ble_mesh_sensor_model.c | 18 ++++++++++-------- .../btc/btc_ble_mesh_time_scene_model.c | 18 ++++++++++-------- 6 files changed, 60 insertions(+), 48 deletions(-) diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c index 7d4d865b3f..fe2700e20a 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c @@ -126,6 +126,16 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des return; } + if (p_src_data->params) { + p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + switch (msg->act) { case ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT: case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: @@ -211,14 +221,6 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des } } case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT: - if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - if (p_dest_data->params) { - memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); - } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); - } - } break; default: break; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c index f13b86bb6f..a33ad6fca1 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c @@ -165,6 +165,16 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de return; } + if (p_src_data->params) { + p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + switch (msg->act) { case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: @@ -277,14 +287,6 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de } } case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: - if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - if (p_dest_data->params) { - memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); - } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); - } - } break; default: break; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c index 5e1e01e958..c728f135ee 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c @@ -127,6 +127,16 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des return; } + if (p_src_data->params) { + p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + switch (msg->act) { case ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT: case ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT: @@ -167,14 +177,6 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des } } case ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT: - if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - if (p_dest_data->params) { - memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); - } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); - } - } break; default: break; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c index fdb5d2e49a..9d75fbd976 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c @@ -121,6 +121,16 @@ static void btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t *msg, void *p_d return; } + if (p_src_data->params) { + p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + switch (msg->act) { case ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT: case ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT: @@ -147,14 +157,6 @@ static void btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t *msg, void *p_d } } case ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT: - if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - if (p_dest_data->params) { - memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); - } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); - } - } break; default: break; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c index f3766f1d46..da03d540fe 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c @@ -256,6 +256,16 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des return; } + if (p_src_data->params) { + p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + switch (msg->act) { case ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT: case ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT: @@ -367,14 +377,6 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des } } case ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT: - if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - if (p_dest_data->params) { - memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); - } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); - } - } break; default: break; diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c index c87b67237a..7e29d2ea14 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c @@ -121,6 +121,16 @@ static void btc_ble_mesh_time_scene_client_copy_req_data(btc_msg_t *msg, void *p return; } + if (p_src_data->params) { + p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + switch (msg->act) { case ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT: case ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT: @@ -148,14 +158,6 @@ static void btc_ble_mesh_time_scene_client_copy_req_data(btc_msg_t *msg, void *p } } case ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT: - if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - if (p_dest_data->params) { - memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); - } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); - } - } break; default: break; From 1a8f37b383de736f34bec19565d2151591584990 Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 3 Sep 2019 19:32:05 +0800 Subject: [PATCH 132/146] ble_mesh: update ble mesh examples sdkconfig.defaults --- .../mesh_core/mesh_bearer_adapt.c | 16 ++--- components/bt/esp_ble_mesh/mesh_core/net.c | 10 +-- components/bt/esp_ble_mesh/mesh_core/prov.c | 2 +- .../bt/esp_ble_mesh/mesh_core/transport.c | 62 +++++++++---------- .../ble_mesh_client_model/sdkconfig.defaults | 2 + .../ble_mesh_node/sdkconfig.defaults | 2 + .../ble_mesh_provisioner/sdkconfig.defaults | 2 + .../sdkconfig.defaults | 2 + .../sdkconfig.defaults | 2 + .../ble_mesh_node/sdkconfig.defaults | 2 + .../ble_mesh_provisioner/sdkconfig.defaults | 2 + .../ble_mesh_wifi_coexist/sdkconfig.defaults | 2 + 12 files changed, 62 insertions(+), 44 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c index 8724bf8573..d6eed43b8e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c @@ -1656,21 +1656,23 @@ const u8_t *bt_mesh_pub_key_get(void) BT_OCTET32 private_key = {0}; Point public_key = {0}; -#if 1 if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY)) { return bt_mesh_public_key; } -#else + /* BLE Mesh BQB test case MESH/NODE/PROV/UPD/BV-12-C requires * different public key for each provisioning procedure. * Note: if enabled, when Provisioner provision multiple devices * at the same time, this may cause invalid confirmation value. + * + * Use the following code for generating different private key + * for each provisioning procedure. + * + * if (bt_mesh_rand(bt_mesh_private_key, BT_OCTET32_LEN)) { + * BT_ERR("%s, Unable to generate bt_mesh_private_key", __func__); + * return NULL; + * } */ - if (bt_mesh_rand(bt_mesh_private_key, BT_OCTET32_LEN)) { - BT_ERR("%s, Unable to generate bt_mesh_private_key", __func__); - return NULL; - } -#endif memcpy(private_key, bt_mesh_private_key, BT_OCTET32_LEN); ECC_PointMult(&public_key, &(curve_p256.G), (DWORD *)private_key, KEY_LENGTH_DWORDS_P256); diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index a9bbb2e6b8..ad778269a9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -785,11 +785,11 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, } if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY) && - bt_mesh_proxy_relay(&buf->b, dst)) { - send_cb_finalize(cb, cb_data); - return 0; - } + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY) && + bt_mesh_proxy_relay(&buf->b, dst)) { + send_cb_finalize(cb, cb_data); + return 0; + } } bt_mesh_adv_send(buf, cb, cb_data); diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index b270519365..63848eaa51 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -1527,7 +1527,7 @@ static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf) if (link.rx.buf->len > link.rx.buf->size) { BT_ERR("%s, Too large provisioning PDU (%u bytes)", __func__, link.rx.buf->len); - // prov_send_fail_msg(PROV_ERR_NVAL_FMT); + /* Zephyr uses prov_send_fail_msg() here */ return; } diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 19a3b0c668..9b3606a981 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -418,28 +418,28 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, net_buf_simple_pull(sdu, len); if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - enum bt_mesh_friend_pdu_type type; + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + enum bt_mesh_friend_pdu_type type; - if (seg_o == tx->seg_n) { - type = BLE_MESH_FRIEND_PDU_COMPLETE; - } else { - type = BLE_MESH_FRIEND_PDU_PARTIAL; - } + if (seg_o == tx->seg_n) { + type = BLE_MESH_FRIEND_PDU_COMPLETE; + } else { + type = BLE_MESH_FRIEND_PDU_PARTIAL; + } - if (bt_mesh_friend_enqueue_tx(net_tx, type, - &tx->seq_auth, - tx->seg_n + 1, - &seg->b) && - BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) { - /* PDUs for a specific Friend should only go - * out through the Friend Queue. - */ - net_buf_unref(seg); - continue; + if (bt_mesh_friend_enqueue_tx(net_tx, type, + &tx->seq_auth, + tx->seg_n + 1, + &seg->b) && + BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) { + /* PDUs for a specific Friend should only go + * out through the Friend Queue. + */ + net_buf_unref(seg); + continue; + } } } - } tx->seg[seg_o] = net_buf_ref(seg); @@ -1543,26 +1543,26 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) * */ if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && - (bt_mesh_lpn_timer() || - (bt_mesh_lpn_established() && bt_mesh_lpn_waiting_update()))) { - bt_mesh_lpn_msg_received(rx); - } + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && + (bt_mesh_lpn_timer() || + (bt_mesh_lpn_established() && bt_mesh_lpn_waiting_update()))) { + bt_mesh_lpn_msg_received(rx); + } } net_buf_simple_restore(buf, &state); if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match && !err) { - if (seq_auth == TRANS_SEQ_AUTH_NVAL) { - bt_mesh_friend_enqueue_rx(rx, pdu_type, NULL, - seg_count, buf); - } else { - bt_mesh_friend_enqueue_rx(rx, pdu_type, &seq_auth, - seg_count, buf); + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match && !err) { + if (seq_auth == TRANS_SEQ_AUTH_NVAL) { + bt_mesh_friend_enqueue_rx(rx, pdu_type, NULL, + seg_count, buf); + } else { + bt_mesh_friend_enqueue_rx(rx, pdu_type, &seq_auth, + seg_count, buf); + } } } - } return err; } diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/sdkconfig.defaults index e1aee38f72..0be67101dd 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/sdkconfig.defaults @@ -7,10 +7,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_GATTS_ENABLE=y CONFIG_GATTS_SEND_SERVICE_CHANGE_MANUAL=y CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults index 732947bb16..1a9c02e1bf 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults @@ -10,10 +10,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_GATTS_ENABLE=y CONFIG_GATTS_SEND_SERVICE_CHANGE_MANUAL=y CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults index 9c0ad1b87c..cf11fdcc8c 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults @@ -7,10 +7,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_BLE_MESH=y CONFIG_BLE_MESH_HCI_5_0=y CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults index a5c4750931..22854582c3 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults @@ -7,10 +7,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_BLE_MESH=y CONFIG_BLE_MESH_HCI_5_0=y CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults index 4f6ca5787a..5118ab757f 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults @@ -7,10 +7,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_GATTS_ENABLE=y CONFIG_GATTS_SEND_SERVICE_CHANGE_MANUAL=y CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/sdkconfig.defaults index 565e251ebd..292c41a327 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/sdkconfig.defaults @@ -7,10 +7,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_GATTS_ENABLE=y CONFIG_GATTS_SEND_SERVICE_CHANGE_MANUAL=y CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults index 12c5be5d12..ae512edd8c 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults @@ -7,10 +7,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_BLE_MESH=y CONFIG_BLE_MESH_HCI_5_0=y CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults index 5ef063bfee..214f15eb6c 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults @@ -11,10 +11,12 @@ CONFIG_BTDM_CONTROLLER_MODE_BTDM= CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=n CONFIG_BLE_SCAN_DUPLICATE=y CONFIG_SCAN_DUPLICATE_TYPE=2 +CONFIG_SCAN_DUPLICATE_BY_ADV_DATA_AND_DEVICE_ADDR=y CONFIG_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BLE_MESH_SCAN_DUPLICATE_EN=y CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE=200 CONFIG_BTDM_CONTROLLER_FULL_SCAN_SUPPORTED=y +CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_SUPPORTED=n CONFIG_GATTS_ENABLE=y CONFIG_GATTS_SEND_SERVICE_CHANGE_MANUAL=y CONFIG_BLE_MESH=y From 95552d4d136c2d031052b31b656732d3369a546e Mon Sep 17 00:00:00 2001 From: Kirill Chalov Date: Thu, 8 Aug 2019 20:15:48 +0800 Subject: [PATCH 133/146] Review the file api-reference/peripherals/sdspi_host.rst --- .../api-reference/peripherals/sdspi_host.rst | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/docs/en/api-reference/peripherals/sdspi_host.rst b/docs/en/api-reference/peripherals/sdspi_host.rst index aad91c1d7f..e1545e422e 100644 --- a/docs/en/api-reference/peripherals/sdspi_host.rst +++ b/docs/en/api-reference/peripherals/sdspi_host.rst @@ -4,19 +4,44 @@ SD SPI Host Driver Overview -------- -SPI controllers accessible via spi_master driver (HSPI, VSPI) can be used to work with SD cards. The driver which provides this capability is called "SD SPI Host", due to its similarity with the :doc:`SDMMC Host ` driver. +The SD SPI host driver allows using the SPI2 (HSPI) or SPI3 (VSPI) controller for communication with SD cards. This driver's naming pattern was adopted from the :doc:`SDMMC Host ` driver due to their similarity. Likewise, the APIs of both drivers are also very similar. -In SPI mode, SD driver has lower throughput than in 1-line SD mode. However SPI mode makes pin selection more flexible, as SPI peripheral can be connected to any ESP32 pins using GPIO Matrix. SD SPI driver uses software controlled CS signal. Currently SD SPI driver assumes that it can use the SPI controller exclusively, so applications which need to share SPI bus between SD cards and other peripherals need to make sure that SD card and other devices are not used at the same time from different tasks. +The SD SPI host driver has the following modes: -SD SPI driver is represented using an :cpp:class:`sdmmc_host_t` structure initialized using :c:macro:`SDSPI_HOST_DEFAULT` macro. For slot initialization, :c:macro:`SDSPI_SLOT_CONFIG_DEFAULT` can be used to fill in default pin mapping, which is the same as the pin mapping in SD mode. +- **SPI mode**: offers lower throughput but makes pin selection more flexible. With the help of the GPIO matrix, an SPI peripheral's signals can be routed to any ESP32 pin. +- **1-bit SD mode**: offers higher throughput but requires routing the signals through their dedicated IO_MUX pins only. -SD SPI driver APIs are very similar to :doc:`SDMMC host APIs `. As with the SDMMC host driver, only :cpp:func:`sdspi_host_init`, :cpp:func:`sdspi_host_init_slot`, and :cpp:func:`sdspi_host_deinit` functions are normally used by the applications. Other functions are called by the protocol level driver via function pointers in :cpp:class:`sdmmc_host_t` structure. +The SD SPI driver uses software-controlled CS signal. -.. note: - - SD over SPI does not support speeds above SDMMC_FREQ_DEFAULT due to a limitation of SPI driver. +Currently, the SD SPI driver cannot handle multi-threaded environments as does not support time-division multiplexing on the same SPI bus. It means that if your application needs to communicate with an SD card and other devices on the same SPI bus, the application itself must ensure that its different tasks do not try to access the SPI slaves at the same time. -See :doc:`SD/SDIO/MMC Driver <../storage/sdmmc>` for the higher level driver which implements the protocol layer. + +How to Use +---------- + +The state and configurations of the SD SPI host driver are stored in a :cpp:type:`sdmmc_host_t` structure. This structure can be initialized using the :c:macro:`SDSPI_HOST_DEFAULT` macro. + +The state and configurations of the SD slot are stored in a :cpp:type:`sdmmc_slot_config_t` structure. Use the macro :c:macro:`SDSPI_SLOT_CONFIG_DEFAULT` to initialize the structure and to fill in the default pin mappings (SD mode pin mappings). + +Only the following driver's API functions are normally used by most applications: + +- :cpp:func:`sdspi_host_init` +- :cpp:func:`sdspi_host_init_slot` +- :cpp:func:`sdspi_host_deinit` + +Other functions are mostly used by the protocol level SD/SDIO/MMC driver via function pointers in the :cpp:type:`sdmmc_host_t` structure. For more details, see :doc:`the SD/SDIO/MMC Driver <../storage/sdmmc>`. + +.. note:: + + SD over SPI does not support speeds above :c:macro:`SDMMC_FREQ_DEFAULT` due to the limitations of the SPI driver. + + +.. todo + +.. The SD SPI API reference could use more detail such as: +.. - Configuration. What are some key points of concern regarding slot configuration. +.. - Which function/how is a transaction done? +.. - Are there code snippets or corresponding application examples? API Reference From e52c0d0da2ce613efe10d8f4b7147d0cc051a332 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Mon, 9 Sep 2019 21:56:39 +0800 Subject: [PATCH 134/146] heap: Fix printf usage in heap poisoning This commit fixes the bug where printf() is used in verify_allocated_region() when ets_printf() should be used. --- components/heap/multi_heap_poisoning.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/heap/multi_heap_poisoning.c b/components/heap/multi_heap_poisoning.c index dabf6cc241..a896043abb 100644 --- a/components/heap/multi_heap_poisoning.c +++ b/components/heap/multi_heap_poisoning.c @@ -110,7 +110,7 @@ static poison_head_t *verify_allocated_region(void *data, bool print_errors) } if (canary != TAIL_CANARY_PATTERN) { if (print_errors) { - printf("CORRUPT HEAP: Bad tail at %p. Expected 0x%08x got 0x%08x\n", &tail->tail_canary, + MULTI_HEAP_STDERR_PRINTF("CORRUPT HEAP: Bad tail at %p. Expected 0x%08x got 0x%08x\n", &tail->tail_canary, TAIL_CANARY_PATTERN, canary); } return NULL; From b0710f9d0b4264f058f5bb9e97fd1337acc581be Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 30 Aug 2019 18:08:19 +1000 Subject: [PATCH 135/146] docs: Explain build system REQUIRES & PRIV_REQUIRES in more detail Including an example. --- docs/en/api-guides/build-system.rst | 177 ++++++++++++++++++++++++---- 1 file changed, 154 insertions(+), 23 deletions(-) diff --git a/docs/en/api-guides/build-system.rst b/docs/en/api-guides/build-system.rst index 30d59bf8fa..512842f85c 100644 --- a/docs/en/api-guides/build-system.rst +++ b/docs/en/api-guides/build-system.rst @@ -193,7 +193,8 @@ An example project directory tree might look like this:: - Kconfig - src1.c - include/ - component2.h - - main/ - src1.c + - main/ - CMakeLists.txt + - src1.c - src2.c - build/ @@ -205,9 +206,9 @@ This example "myProject" contains the following elements: - "sdkconfig" project configuration file. This file is created/updated when ``idf.py menuconfig`` runs, and holds configuration for all of the components in the project (including ESP-IDF itself). The "sdkconfig" file may or may not be added to the source control system of the project. -- Optional "components" directory contains components that are part of the project. A project does not have to contain custom components of this kind, but it can be useful for structuring reusable code or including third party components that aren't part of ESP-IDF. +- Optional "components" directory contains components that are part of the project. A project does not have to contain custom components of this kind, but it can be useful for structuring reusable code or including third party components that aren't part of ESP-IDF. Alternatively, ``EXTRA_COMPONENT_DIRS`` can be set in the top-level CMakeLists.txt to look for components in other places. See the :ref:`renaming main ` section for more info. If you have a lot of source files in your project, we recommend grouping most into components instead of putting them all in "main". -- "main" directory is a special "pseudo-component" that contains source code for the project itself. "main" is a default name, the CMake variable ``COMPONENT_DIRS`` includes this component but you can modify this variable. Alternatively, ``EXTRA_COMPONENT_DIRS`` can be set in the top-level CMakeLists.txt to look for components in other places. See the :ref:`renaming main ` section for more info. If you have a lot of source files in your project, we recommend grouping most into components instead of putting them all in "main". +- "main" directory is a special component that contains source code for the project itself. "main" is a default name, the CMake variable ``COMPONENT_DIRS`` includes this component but you can modify this variable. - "build" directory is where build output is created. This directory is created by ``idf.py`` if it doesn't already exist. CMake configures the project and generates interim build files in this directory. Then, after the main build process is run, this directory will also contain interim object files and libraries as well as final binary output files. This directory is usually not added to source control or distributed with the project source code. @@ -250,7 +251,7 @@ Optional Project Variables These variables all have default values that can be overridden for custom behaviour. Look in :idf_file:`/tools/cmake/project.cmake` for all of the implementation details. -- ``COMPONENT_DIRS``,``COMPONENTS_DIRS``: Directories to search for components. Defaults to `IDF_PATH/components`, `PROJECT_DIR/components`, and ``EXTRA_COMPONENT_DIRS``. Override this variable if you don't want to search for components in these places. +- ``COMPONENT_DIRS``, ``COMPONENTS_DIRS``: Directories to search for components. Defaults to ``IDF_PATH/components``, ``PROJECT_DIR/components``, and ``EXTRA_COMPONENT_DIRS``. Override this variable if you don't want to search for components in these places. - ``EXTRA_COMPONENT_DIRS``, ``EXTRA_COMPONENTS_DIRS``: Optional list of additional directories to search for components. Paths can be relative to the project directory, or absolute. - ``COMPONENTS``: A list of component names to build into the project. Defaults to all components found in the ``COMPONENT_DIRS`` directories. Use this variable to "trim down" the project for faster build times. Note that any component which "requires" another component via the REQUIRES or PRIV_REQUIRES arguments on component registration will automatically have it added to this list, so the ``COMPONENTS`` list can be very short. @@ -307,18 +308,20 @@ Minimal Component CMakeLists The minimal component ``CMakeLists.txt`` file simply registers the component to the build system using ``idf_component_register``:: idf_component_register(SRCS "foo.c" "bar.c" - INCLUDE_DIRS "include") + INCLUDE_DIRS "include" + REQUIRES mbedtls) - ``SRCS`` is a list of source files (``*.c``, ``*.cpp``, ``*.cc``, ``*.S``). These source files will be compiled into the component library. - ``INCLUDE_DIRS`` is a list of directories to add to the global include search path for any component which requires this component, and also the main source files. +- ``REQUIRES`` is not actually required, but it is very often required to declare what other components this component will use. See :ref:`component requirements`. -A library with the name of the component will be built and linked into the final app. +A library with the name of the component will be built and linked into the final app. Directories are usually specified relative to the ``CMakeLists.txt`` file itself, although they can be absolute. -There are other arguments that can be passed to ``idf_component_register``. These arguments +There are other arguments that can be passed to ``idf_component_register``. These arguments are discussed :ref:`here`. -See `example component CMakeLists`_ for more complete component ``CMakeLists.txt`` examples. +See `example component requirements`_ and `example component CMakeLists`_ for more complete component ``CMakeLists.txt`` examples. .. _component variables: @@ -400,34 +403,162 @@ The ESP-IDF build system adds the following C preprocessor definitions on the co - ``ESP_PLATFORM`` : Can be used to detect that build happens within ESP-IDF. - ``IDF_VER`` : Defined to a git version string. E.g. ``v2.0`` for a tagged release or ``v1.0-275-g0efaa4f`` for an arbitrary commit. +.. _component requirements: + Component Requirements ====================== -When compiling each component, the ESP-IDF build system recursively evaluates its components. - -Each component's source file is compiled with these include path directories, as specified in the passed arguments -to ``idf_component_register``: - -- The current component's ``INCLUDE_DIRS`` and ``PRIV_INCLUDE_DIRS``. -- The ``INCLUDE_DIRS`` set by all components in the current component's ``REQUIRES`` and ``PRIV_REQUIRES`` variables (ie all the current component's public and private dependencies). -- All of the ``REQUIRES`` of those components, evaluated recursively (ie all public dependencies of this component's dependencies, recursively expanded). +When compiling each component, the ESP-IDF build system recursively evaluates its dependencies. This means each component needs to declare the components that it depends on ("requires"). When writing a component ------------------------ +.. code-block:: cmake + + idf_component_register(... + REQUIRES mbedtls + PRIV_REQUIRES console spiffs) + - ``REQUIRES`` should be set to all components whose header files are #included from the *public* header files of this component. -- ``PRIV_REQUIRES`` should be set to all components whose header files are #included from *any source files* of this component, unless already listed in ``COMPONENT_REQUIRES``. Or any component which is required to be linked in order for this component to function correctly. -- ``REQUIRES`` and/or ``PRIV_REQUIRES`` should be set before calling ``idf_component_register()``. +- ``PRIV_REQUIRES`` should be set to all components whose header files are #included from *any source files* in this component, unless already listed in ``REQUIRES``. Also any component which is required to be linked in order for this component to function correctly. - The values of ``REQUIRES`` and ``PRIV_REQUIRES`` should not depend on any configuration choices (``CONFIG_xxx`` macros). This is because requirements are expanded before configuration is loaded. Other component variables (like include paths or source files) can depend on configuration choices. -- Not setting either or both ``REQUIRES`` variables is fine. If the component has no requirements except for the "common" components needed for RTOS, libc, etc (``COMPONENT_REQUIRES_COMMON``) then both variables can be empty or unset. +- Not setting either or both ``REQUIRES`` variables is fine. If the component has no requirements except for the `Common component requirements`_ needed for RTOS, libc, etc. -Components which support only some targets (values of ``IDF_TARGET``) may specify ``REQUIRED_IDF_TARGETS`` in the ``idf_component_register`` call to express these requirements. In this case the build system will generate an error if the component is included into the build, but does not support selected target. +If a components only supports some target chips (values of ``IDF_TARGET``) then it can specify ``REQUIRED_IDF_TARGETS`` in the ``idf_component_register`` call to express these requirements. In this case the build system will generate an error if the component is included into the build, but does not support the selected target. -When creating a project ------------------------ +.. note:: In CMake terms, ``REQUIRES`` & ``PRIV_REQUIRES`` are approximate wrappers around the CMake functions ``target_link_libraries(... PUBLIC ...)`` and ``target_link_libraries(... PRIVATE ...)``. + +.. _example component requirements: + +Example of component requirements +--------------------------------- + +Imagine there is a ``car`` component, which uses the ``engine`` component, which uses the ``spark_plug`` component: + +.. code-block:: none + + - autoProject/ + - CMakeLists.txt + - components/ - car/ - CMakeLists.txt + - car.c + - car.h + - engine/ - CMakeLists.txt + - engine.c + - include/ - engine.h + - spark_plug/ - CMakeLists.txt + - plug.c + - plug.h + +Car component +^^^^^^^^^^^^^ + +.. highlight:: c + +The ``car.h`` header file is the public interface for the ``car`` component. This header includes ``engine.h`` directly because it uses some declarations from this header:: + + /* car.h */ + #include "engine.h" + + #ifdef ENGINE_IS_HYBRID + #define CAR_MODEL "Hybrid" + #endif + +And car.c includes ``car.h`` as well:: + + /* car.c */ + #include "car.h" + +This means the ``car/CMakeLists.txt`` file needs to declare that ``car`` requires ``engine``: + +.. code-block:: cmake + + idf_component_register(SRCS "car.c" + INCLUDE_DIRS "." + REQUIRES engine) + +- ``SRCS`` gives the list of source files in the ``car`` component. +- ``INCLUDE_DIRS`` gives the list of public include directories for this component. Because the public interface is ``car.h``, the directory containing ``car.h`` is listed here. +- ``REQUIRES`` gives the list of components required by the public interface of this component. Because ``car.h`` is a public header and includes a header from ``engine``, we include ``engine`` here. This makes sure that any other component which includes ``car.h`` will be able to recursively include the required ``engine.h`` also. + +Engine component +^^^^^^^^^^^^^^^^ + +.. highlight:: c + +The ``engine`` component also has a public header file ``include/engine.h``, but this header is simpler:: + + /* engine.h */ + #define ENGINE_IS_HYBRID + + void engine_start(void); + +The implementation is in ``engine.c``:: + + /* engine.c */ + #include "engine.h" + #include "spark_plug.h" + + ... + +In this component, ``engine`` depends on ``spark_plug`` but this is a private dependency. ``spark_plug.h`` is needed to compile ``engine.c``, but not needed to include ``engine.h``. + +This means that the ``engine/CMakeLists.txt`` file can use ``PRIV_REQUIRES``: + +.. code-block:: cmake + + idf_component_register(SRCS "engine.c" + INCLUDE_DIRS "include" + PRIV_REQUIRES spark_plug) + +As a result, source files in the ``car`` component don't need the ``spark_plug`` include directories added to their compiler search path. This can speed up compilation, and stops compiler command lines from becoming longer than necessary. + +Spark Plug Component +^^^^^^^^^^^^^^^^^^^^ + +The ``spark_plug`` component doesn't depend on anything else. It has a public header file ``spark_plug.h``, but this doesn't include headers from any other components. + +This means that the ``spark_plug/CMakeLists.txt`` file doesn't need any ``REQUIRES`` or ``PRIV_REQUIRES`` clauses: + +.. code-block:: cmake + + idf_component_register(SRCS "spark_plug.c" + INCLUDE_DIRS ".") + + + +Source File Include Directories +------------------------------- + +Each component's source file is compiled with these include path directories, as specified in the passed arguments to ``idf_component_register``: + +.. code-block:: cmake + + idf_component_register(.. + INCLUDE_DIRS "include" + PRIV_INCLUDE_DIRS "other") + + +- The current component's ``INCLUDE_DIRS`` and ``PRIV_INCLUDE_DIRS``. +- The ``INCLUDE_DIRS`` belonging to all other components listed in the ``REQUIRES`` and ``PRIV_REQUIRES`` parameters (ie all the current component's public and private dependencies). +- Recursively, all of the ``INCLUDE_DIRS`` of those components ``REQUIRES`` lists (ie all public dependencies of this component's dependencies, recursively expanded). + +Main component requirements +--------------------------- + +The component named ``main`` is special because it automatically requires all other components in the build. So it's not necessary to pass ``REQUIRES`` or ``PRIV_REQUIRES`` to this component. See :ref:`renaming main ` for a description of what needs to be changed if no longer using the ``main`` component. + +Common component requirements +----------------------------- + +To avoid duplication, every component automatically requires some "common" IDF components even if they are not mentioned explicitly. Headers from these components can always be included. + +The list of common components is: freertos, newlib, heap, log, soc, esp_rom, esp_common, xtensa, cxx. + +Including components in the build +---------------------------------- - By default, every component is included in the build. -- If you set the ``COMPONENTS`` variable to a minimal list of components used directly by your project, then the build will include: +- If you set the ``COMPONENTS`` variable to a minimal list of components used directly by your project, then the build will expand to also include required components. The full list of components will be: - Components mentioned explicitly in ``COMPONENTS``. - Those components' requirements (evaluated recursively). From b547aef2a03d05105df1a027363424a33f2642c5 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 12 Jul 2019 16:29:40 +1000 Subject: [PATCH 136/146] flash encryption: Ensure flash encryption can't be disabled if Secure Boot is on --- .../include/esp_flash_encrypt.h | 15 +++++++ .../bootloader_support/src/flash_encrypt.c | 42 +++++++++++++++++++ components/esp32/cpu_start.c | 13 +----- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/components/bootloader_support/include/esp_flash_encrypt.h b/components/bootloader_support/include/esp_flash_encrypt.h index 028842d97e..acfbf5d1cf 100644 --- a/components/bootloader_support/include/esp_flash_encrypt.h +++ b/components/bootloader_support/include/esp_flash_encrypt.h @@ -133,6 +133,21 @@ void esp_flash_write_protect_crypt_cnt(void); */ esp_flash_enc_mode_t esp_get_flash_encryption_mode(void); + +/** @brief Check the flash encryption mode during startup + * + * @note This function is called automatically during app startup, + * it doesn't need to be called from the app. + * + * Verifies the flash encryption config during startup: + * + * - Correct any insecure flash encryption settings if hardware + * Secure Boot is enabled. + * - Log warnings if the efuse config doesn't match the project + * config in any way + */ +void esp_flash_encryption_init_checks(void); + #ifdef __cplusplus } #endif diff --git a/components/bootloader_support/src/flash_encrypt.c b/components/bootloader_support/src/flash_encrypt.c index d64d2fd9d7..5a76fc0fda 100644 --- a/components/bootloader_support/src/flash_encrypt.c +++ b/components/bootloader_support/src/flash_encrypt.c @@ -13,9 +13,51 @@ // limitations under the License. #include +#include "sdkconfig.h" +#include "esp_log.h" #include "esp_efuse.h" #include "esp_efuse_table.h" #include "esp_flash_encrypt.h" +#include "esp_secure_boot.h" + +#ifndef BOOTLOADER_BUILD +static const char *TAG = "flash_encrypt"; + +void esp_flash_encryption_init_checks() +{ + esp_flash_enc_mode_t mode; + + // First check is: if Release mode flash encryption & secure boot are enabled then + // FLASH_CRYPT_CNT *must* be write protected. This will have happened automatically + // if bootloader is IDF V4.0 or newer but may not have happened for previous ESP-IDF bootloaders. +#ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE +#ifdef CONFIG_SECURE_BOOT_ENABLED + if (esp_secure_boot_enabled() && esp_flash_encryption_enabled()) { + uint8_t flash_crypt_cnt_wr_dis = 0; + esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1); + if (!flash_crypt_cnt_wr_dis) { + ESP_EARLY_LOGE(TAG, "Flash encryption & Secure Boot together requires FLASH_CRYPT_CNT efuse to be write protected. Fixing now..."); + esp_flash_write_protect_crypt_cnt(); + } + } +#endif // CONFIG_SECURE_BOOT_ENABLED +#endif // CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE + + // Second check is to print a warning or error if the current running flash encryption mode + // doesn't match the expectation from project config (due to mismatched bootloader and app, probably) + mode = esp_get_flash_encryption_mode(); + if (mode == ESP_FLASH_ENC_MODE_DEVELOPMENT) { +#ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE + ESP_EARLY_LOGE(TAG, "Flash encryption settings error: app is configured for RELEASE but efuses are set for DEVELOPMENT"); + ESP_EARLY_LOGE(TAG, "Mismatch found in security options in bootloader menuconfig and efuse settings. Device is not secure."); +#else + ESP_EARLY_LOGW(TAG, "Flash encryption mode is DEVELOPMENT (not secure)"); +#endif + } else if (mode == ESP_FLASH_ENC_MODE_RELEASE) { + ESP_EARLY_LOGI(TAG, "Flash encryption mode is RELEASE"); + } +} +#endif void esp_flash_write_protect_crypt_cnt(void) { diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 975f09b309..c5f2528818 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -204,18 +204,9 @@ void IRAM_ATTR call_start_cpu0(void) } ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1); - esp_flash_enc_mode_t mode; - mode = esp_get_flash_encryption_mode(); - if (mode == ESP_FLASH_ENC_MODE_DEVELOPMENT) { -#ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE - ESP_EARLY_LOGE(TAG, "Flash encryption settings error: mode should be RELEASE but is actually DEVELOPMENT"); - ESP_EARLY_LOGE(TAG, "Mismatch found in security options in menuconfig and efuse settings"); -#else - ESP_EARLY_LOGW(TAG, "Flash encryption mode is DEVELOPMENT"); +#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED + esp_flash_encryption_init_checks(); #endif - } else if (mode == ESP_FLASH_ENC_MODE_RELEASE) { - ESP_EARLY_LOGI(TAG, "Flash encryption mode is RELEASE"); - } //Flush and enable icache for APP CPU Cache_Flush(1); From 5c5770dddb597a3333d7bb87c8633d91f8f791ca Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 10 Sep 2019 10:52:13 +1000 Subject: [PATCH 137/146] docs: Small cleanup of flash encryption docs --- docs/en/security/flash-encryption.rst | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/en/security/flash-encryption.rst b/docs/en/security/flash-encryption.rst index f13a27aa10..a17cf596de 100644 --- a/docs/en/security/flash-encryption.rst +++ b/docs/en/security/flash-encryption.rst @@ -22,7 +22,10 @@ With flash encryption enabled, following kinds of flash data are encrypted by de - Secure boot bootloader digest (if secure boot is enabled) - Any partition marked with the "encrypted" flag in the partition table -Flash encryption is separate from the :doc:`Secure Boot ` feature, and you can use flash encryption without enabling secure boot. However, for a secure environment both should be used simultaneously. In absence of secure boot, additional configuration needs to be performed to ensure effectiveness of flash encryption. See :ref:`flash-encryption-without-secure-boot` for more details. +Flash encryption is separate from the :doc:`Secure Boot ` feature, and you can use flash encryption without enabling secure boot. However, for a secure environment both should be used simultaneously. + +.. important:: + For production use, flash encryption should be enabled in the "Release" mode only. .. important:: Enabling flash encryption limits the options for further updates of the ESP32. Make sure to read this document (including :ref:`flash-encryption-limitations`) and understand the implications of enabling flash encryption. @@ -31,7 +34,7 @@ Flash encryption is separate from the :doc:`Secure Boot ` feature, eFuse Used During Flash Encryption Process ------------------------------------------- -The flash encryption operation is controlled by various eFuses available on ESP32. Below is the list of eFuse and their description: +The flash encryption operation is controlled by various eFuses available on ESP32. Below is the list of eFuse and their description: :: @@ -300,6 +303,8 @@ Using Host Generated Flash Encryption Key ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ It is possible to pregenerate the flash encryption key on the host computer and burn it into the ESP32's eFuse key block. This allows data to be pre-encrypted on the host and flashed to the ESP32 without needing a plaintext flash update. This feature allows encrypted flashing in both :ref:`flash_enc_development_mode` and :ref:`flash_enc_release_mode` modes. +.. note:: This option is not recommended for production unless a separate key is generated for each individual device. + - Ensure you have a ESP32 device with default flash encryption eFuse settings as shown in :ref:`flash-encryption-efuse`. - Generate a random key with espsecure.py:: @@ -321,7 +326,6 @@ It is possible to pregenerate the flash encryption key on the host computer and - Update to the partition table offset may be required since after enabling flash encryption the size of bootloader is increased. See :ref:`secure-boot-bootloader-size` - Save the configuration and exit. - Build and flash the complete image including: bootloader, partition table and app. These partitions are initially written to the flash unencrypted @@ -375,7 +379,7 @@ For subsequent plaintext update in field OTA scheme should be used. Please refer Possible Failures ^^^^^^^^^^^^^^^^^ -Once the flash encryption is enabled and if the ``FLASH_CRYPT_CNT`` eFuse value has odd number of bits set then all the partitions (which are marked with encryption flag) are expected to contain encrypted ciphertext. Below are three typical failure cases if the ESP32 is loaded with plaintext data: +Once flash encryption is enabled and if the ``FLASH_CRYPT_CNT`` eFuse value has an odd number of bits set then all the partitions (which are marked with encryption flag) are expected to contain encrypted ciphertext. Below are three typical failure cases if the ESP32 is loaded with plaintext data: 1. In case the bootloader partition is re-updated with plaintext bootloader image the ROM loader will fail to load the bootloader and following type of failure will be displayed: @@ -406,6 +410,8 @@ Once the flash encryption is enabled and if the ``FLASH_CRYPT_CNT`` eFuse value ets_main.c 371 ets Jun 8 2016 00:22:57 +.. note:: This error also appears in the flash contents is erased or corrupted. + 2. In case the bootloader is encrypted but partition table is re-updated with plaintext partition table image the bootloader will fail to read the partition table and following type of failure will be displayed: :: @@ -547,7 +553,7 @@ Disabling Flash Encryption If you've accidentally enabled flash encryption for some reason, the next flash of plaintext data will soft-brick the ESP32 (the device will reboot continuously, printing the error ``flash read err, 1000``). -You can disable flash encryption again by writing ``FLASH_CRYPT_CNT`` eFuse (only in Development mode): +If flash encryption is enabled in Development mode, you can disable flash encryption again by writing ``FLASH_CRYPT_CNT`` eFuse. This can only be done three times per chip. - First, open :ref:`project-configuration-menu` and disable :ref:`Enable flash encryption boot ` under "Security Features". - Exit menuconfig and save the new configuration. @@ -565,13 +571,13 @@ Limitations of Flash Encryption Flash encryption prevents plaintext readout of the encrypted flash, to protect firmware against unauthorised readout and modification. It is important to understand the limitations of the flash encryption system: -- Flash encryption is only as strong as the key. For this reason, we recommend keys are generated on the device during first boot (default behaviour). If generating keys off-device, ensure proper procedure is followed. +- Flash encryption is only as strong as the key. For this reason, we recommend keys are generated on the device during first boot (default behaviour). If generating keys off-device, ensure proper procedure is followed and don't share the same key between all production devices. - Not all data is stored encrypted. If storing data on flash, check if the method you are using (library, API, etc.) supports flash encryption. - Flash encryption does not prevent an attacker from understanding the high-level layout of the flash. This is because the same AES key is used for every pair of adjacent 16 byte AES blocks. When these adjacent 16 byte blocks contain identical content (such as empty or padding areas), these blocks will encrypt to produce matching pairs of encrypted blocks. This may allow an attacker to make high-level comparisons between encrypted devices (i.e. to tell if two devices are probably running the same firmware version). -- For the same reason, an attacker can always tell when a pair of adjacent 16 byte blocks (32 byte aligned) contain identical content. Keep this in mind if storing sensitive data on the flash, design your flash storage so this doesn't happen (using a counter byte or some other non-identical value every 16 bytes is sufficient). +- For the same reason, an attacker can always tell when a pair of adjacent 16 byte blocks (32 byte aligned) contain two identical 16 byte sequences. Keep this in mind if storing sensitive data on the flash, design your flash storage so this doesn't happen (using a counter byte or some other non-identical value every 16 bytes is sufficient). :ref:`NVS Encryption ` deals with this and is suitable for many uses. - Flash encryption alone may not prevent an attacker from modifying the firmware of the device. To prevent unauthorised firmware from running on the device, use flash encryption in combination with :doc:`Secure Boot `. @@ -586,13 +592,6 @@ It is recommended to use flash encryption and secure boot together. However, if - :ref:`Plaintext serial flash updates ` are only possible if the :ref:`Reflashable ` Secure Boot mode is selected and a Secure Boot key was pre-generated and burned to the ESP32 (refer to :ref:`Secure Boot ` docs.). In this configuration, ``idf.py bootloader`` will produce a pre-digested bootloader and secure boot digest file for flashing at offset 0x0. When following the plaintext serial reflashing steps it is necessary to re-flash this file before flashing other plaintext data. - :ref:`Reflashing via Pregenerated Flash Encryption Key ` is still possible, provided the bootloader is not reflashed. Reflashing the bootloader requires the same :ref:`Reflashable ` option to be enabled in the Secure Boot config. -.. _flash-encryption-without-secure-boot: - -Using Flash Encryption Without Secure Boot ------------------------------------------- - -Even though flash encryption and secure boot can be used independently it is strongly recommended to use flash encryption ALONG with secure boot to achieve strong security. - .. _flash-encryption-advanced-features: Flash Encryption Advanced Features From c052a38e2a0947eb9c4cb7a38c9a8e71770ec6a2 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 10 Sep 2019 11:28:17 +1000 Subject: [PATCH 138/146] bootloader: Link RTC clock functions to the iram_loader section As flash encryption & secure boot needs these functions after the app is loaded. Fixes regression introduced in fb72a6f629a62c0655e8eff1d2d3f67d7aa9b62c --- components/bootloader/subproject/main/esp32.bootloader.ld | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/bootloader/subproject/main/esp32.bootloader.ld b/components/bootloader/subproject/main/esp32.bootloader.ld index 847049096a..3dca756097 100644 --- a/components/bootloader/subproject/main/esp32.bootloader.ld +++ b/components/bootloader/subproject/main/esp32.bootloader.ld @@ -40,6 +40,7 @@ SECTIONS *(.iram1 .iram1.*) /* catch stray IRAM_ATTR */ *liblog.a:(.literal .text .literal.* .text.*) *libgcc.a:(.literal .text .literal.* .text.*) + *libbootloader_support.a:bootloader_clock.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_common.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_random.*(.literal .text .literal.* .text.*) @@ -53,6 +54,7 @@ SECTIONS *libmicro-ecc.a:*.*(.literal .text .literal.* .text.*) *libspi_flash.a:*.*(.literal .text .literal.* .text.*) *libsoc.a:rtc_wdt.*(.literal .text .literal.* .text.*) + *libsoc.a:rtc_clk.*(.literal .text .literal.* .text.*) *libefuse.a:*.*(.literal .text .literal.* .text.*) *(.fini.literal) *(.fini) From 7cc225c85be8443d287c6a7a1fc61bc984899257 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Mon, 9 Sep 2019 15:02:38 +0200 Subject: [PATCH 139/146] VFS: Fix memory access after free() in UART select() Closes https://github.com/espressif/esp-idf/issues/4030 --- components/vfs/vfs_uart.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/vfs/vfs_uart.c b/components/vfs/vfs_uart.c index f1513ae1dc..7e02ef963d 100644 --- a/components/vfs/vfs_uart.c +++ b/components/vfs/vfs_uart.c @@ -474,10 +474,6 @@ static esp_err_t uart_end_select(void *end_select_args) { uart_select_args_t *args = end_select_args; - if (args) { - free(args); - } - portENTER_CRITICAL(uart_get_selectlock()); esp_err_t ret = unregister_select(args); for (int i = 0; i < UART_NUM; ++i) { @@ -485,6 +481,10 @@ static esp_err_t uart_end_select(void *end_select_args) } portEXIT_CRITICAL(uart_get_selectlock()); + if (args) { + free(args); + } + return ret; } From 1b311917534862ec9f20fa310468cecf52d84c62 Mon Sep 17 00:00:00 2001 From: jiangguangming Date: Mon, 9 Sep 2019 15:46:47 +0800 Subject: [PATCH 140/146] fix bug for cmake build system The path of ${SDKCONFIG_H} does not exist, should be replaced by ${sdkconfig_header}. --- components/esp32/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/esp32/CMakeLists.txt b/components/esp32/CMakeLists.txt index c8a8166eaa..7d9ad3872d 100644 --- a/components/esp32/CMakeLists.txt +++ b/components/esp32/CMakeLists.txt @@ -1,3 +1,4 @@ +idf_build_get_property(sdkconfig_header SDKCONFIG_HEADER) if(BOOTLOADER_BUILD) # For bootloader, all we need from esp32 is headers idf_component_register(INCLUDE_DIRS include) @@ -75,7 +76,7 @@ else() add_custom_command( OUTPUT esp32_out.ld COMMAND "${CMAKE_C_COMPILER}" -C -P -x c -E -o esp32_out.ld -I ${config_dir} ${LD_DIR}/esp32.ld - MAIN_DEPENDENCY ${LD_DIR}/esp32.ld ${SDKCONFIG_H} + MAIN_DEPENDENCY ${LD_DIR}/esp32.ld ${sdkconfig_header} COMMENT "Generating linker script..." VERBATIM) From 7c491d23355d79f8fe3dedb1e3d420ffb7c3307e Mon Sep 17 00:00:00 2001 From: Kirill Chalov Date: Wed, 11 Sep 2019 17:15:00 +0800 Subject: [PATCH 141/146] Allow the extension for adding todo notes to rst files. Add information on how to use this extension to the contribution section. --- docs/conf_common.py | 6 +++++ docs/en/contribute/documenting-code.rst | 29 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/docs/conf_common.py b/docs/conf_common.py index 505dab8a0c..12616080c3 100644 --- a/docs/conf_common.py +++ b/docs/conf_common.py @@ -154,8 +154,14 @@ extensions = ['breathe', 'sphinxcontrib.rackdiag', 'sphinxcontrib.packetdiag', 'html_redirects', + 'sphinx.ext.todo', ] +# sphinx.ext.todo extension parameters +# If the below parameter is True, the extension +# produces output, else it produces nothing. +todo_include_todos = False + # Enabling this fixes cropping of blockdiag edge labels seqdiag_antialias = True diff --git a/docs/en/contribute/documenting-code.rst b/docs/en/contribute/documenting-code.rst index fb83f73e18..a75601e26d 100644 --- a/docs/en/contribute/documenting-code.rst +++ b/docs/en/contribute/documenting-code.rst @@ -215,6 +215,35 @@ Try them out by modifying the source code and see the diagram instantly renderin There may be slight differences in rendering of font used by the `interactive shell`_ compared to the font used in the esp-idf documentation. +Add Notes +--------- + +Working on a document, you might need to: + +- Place some suggestions on what should be added or modified in future. +- Leave a reminder for yourself or somebody else to follow up. + +In this case, add a todo note to your reST file using the directive ``.. todo::``. For example: + +.. code-block:: none + + .. todo:: + + Add a package diagram. + +If you add ``.. todolist::`` to a reST file, the directive will be replaced by a list of all todo notes from the whole documentation. + +By default, the directives ``.. todo::`` and ``.. todolist::`` are ignored by documentation builders. If you want the notes and the list of notes to be visible in your locally built documentation, do the following: + +1. Open your local ``conf_common.py`` file. +2. Find the parameter ``todo_include_todos``. +3. Change its value from ``False`` to ``True``. + +Before pushing your changes to origin, please set the value of ``todo_include_todos`` back to ``False``. + +For more details about the extension, see `its website `_. + + Put it all together ------------------- From 1dcdc56a7fec9cf200cf7fd4c2644aaf9cc32433 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Wed, 11 Sep 2019 11:18:18 +0200 Subject: [PATCH 142/146] Use kconfiglib from $IDF_PATH/tools/kconfig_new --- tools/kconfig_new/confgen.py | 7 ++++++- tools/kconfig_new/confserver.py | 7 ++++++- tools/kconfig_new/gen_kconfig_doc.py | 9 ++++++++- tools/ldgen/sdkconfig.py | 6 +++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/tools/kconfig_new/confgen.py b/tools/kconfig_new/confgen.py index 0af2be1bcc..11b3427e77 100755 --- a/tools/kconfig_new/confgen.py +++ b/tools/kconfig_new/confgen.py @@ -30,7 +30,12 @@ import sys import tempfile import gen_kconfig_doc -import kconfiglib + +try: + from . import kconfiglib +except Exception: + sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) + import kconfiglib __version__ = "0.1" diff --git a/tools/kconfig_new/confserver.py b/tools/kconfig_new/confserver.py index cddb572c92..b02a2f1ceb 100755 --- a/tools/kconfig_new/confserver.py +++ b/tools/kconfig_new/confserver.py @@ -7,12 +7,17 @@ from __future__ import print_function import argparse import confgen import json -import kconfiglib import os import sys import tempfile from confgen import FatalError, __version__ +try: + from . import kconfiglib +except Exception: + sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) + import kconfiglib + # Min/Max supported protocol versions MIN_PROTOCOL_VERSION = 1 MAX_PROTOCOL_VERSION = 2 diff --git a/tools/kconfig_new/gen_kconfig_doc.py b/tools/kconfig_new/gen_kconfig_doc.py index 6d6d7b1faf..5d9f41c0c1 100644 --- a/tools/kconfig_new/gen_kconfig_doc.py +++ b/tools/kconfig_new/gen_kconfig_doc.py @@ -21,8 +21,15 @@ # See the License for the specific language governing permissions and # limitations under the License. from __future__ import print_function +import os import re -import kconfiglib +import sys + +try: + from . import kconfiglib +except Exception: + sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) + import kconfiglib # Indentation to be used in the generated file INDENT = ' ' diff --git a/tools/ldgen/sdkconfig.py b/tools/ldgen/sdkconfig.py index 943013dfcf..28ea6b0f45 100644 --- a/tools/ldgen/sdkconfig.py +++ b/tools/ldgen/sdkconfig.py @@ -19,11 +19,11 @@ from pyparsing import Word, alphanums, printables, Combine, Literal, hexnums, qu import sys try: - import kconfiglib -except ImportError: + from . import kconfiglib +except Exception: parent_dir_name = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) kconfig_new_dir = os.path.abspath(parent_dir_name + "/kconfig_new") - sys.path.append(kconfig_new_dir) + sys.path.insert(0, kconfig_new_dir) import kconfiglib From f0dc5f8dc2cc663add1e671f9041cbc08477f522 Mon Sep 17 00:00:00 2001 From: Kirill Chalov Date: Thu, 12 Sep 2019 12:36:13 +0800 Subject: [PATCH 143/146] Apply suggestion to docs/en/contribute/documenting-code.rst --- docs/en/contribute/documenting-code.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/contribute/documenting-code.rst b/docs/en/contribute/documenting-code.rst index a75601e26d..2a92d88098 100644 --- a/docs/en/contribute/documenting-code.rst +++ b/docs/en/contribute/documenting-code.rst @@ -241,7 +241,7 @@ By default, the directives ``.. todo::`` and ``.. todolist::`` are ignored by do Before pushing your changes to origin, please set the value of ``todo_include_todos`` back to ``False``. -For more details about the extension, see `its website `_. +For more details about the extension, see `sphinx.ext.todo `_ documenation. Put it all together From d008c47dac652c0cc7593b04c9b219c9ffd6c30e Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Thu, 5 Sep 2019 18:45:45 +0800 Subject: [PATCH 144/146] esp_flash: add support for encrypted read and write Using legacy implementation. --- components/spi_flash/esp_flash_api.c | 44 ++++++++----- components/spi_flash/include/esp_flash.h | 21 ++++++- .../spi_flash/test/test_flash_encryption.c | 62 +++++++++++++++++++ 3 files changed, 111 insertions(+), 16 deletions(-) diff --git a/components/spi_flash/esp_flash_api.c b/components/spi_flash/esp_flash_api.c index 146ebd12f8..e45f906abc 100644 --- a/components/spi_flash/esp_flash_api.c +++ b/components/spi_flash/esp_flash_api.c @@ -569,28 +569,27 @@ esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint3 return err; } +//currently the legacy implementation is used, from flash_ops.c +esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size); + esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length) { - VERIFY_OP(write_encrypted); - if (((memspi_host_data_t*)chip->host->driver_data)->spi != 0) { - // Encrypted operations have to use SPI0 - return ESP_ERR_FLASH_UNSUPPORTED_HOST; + /* + * Since currently this feature is supported only by the hardware, there + * is no way to support non-standard chips. We use the legacy + * implementation and skip the chip and driver layers. + */ + if (chip == NULL) { + chip = esp_flash_default_chip; + } else if (chip != esp_flash_default_chip) { + return ESP_ERR_NOT_SUPPORTED; } if (buffer == NULL || address > chip->size || address+length > chip->size) { return ESP_ERR_INVALID_ARG; } - - esp_err_t err = spiflash_start(chip); - if (err != ESP_OK) { - return err; - } - - err = chip->chip_drv->write_encrypted(chip, buffer, address, length); - - return spiflash_end(chip, err); + return spi_flash_write_encrypted(address, buffer, length); } - inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len) { uint32_t a_end = a_start + a_len; @@ -598,6 +597,23 @@ inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,ui return (a_end > b_start && b_end > a_start); } +//currently the legacy implementation is used, from flash_ops.c +esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size); + +esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length) +{ + /* + * Since currently this feature is supported only by the hardware, there + * is no way to support non-standard chips. We use the legacy + * implementation and skip the chip and driver layers. + */ + if (chip == NULL) { + chip = esp_flash_default_chip; + } else if (chip != esp_flash_default_chip) { + return ESP_ERR_NOT_SUPPORTED; + } + return spi_flash_read_encrypted(address, out_buffer, length); +} /*------------------------------------------------------------------------------ Adapter layer to original api before IDF v4.0 diff --git a/components/spi_flash/include/esp_flash.h b/components/spi_flash/include/esp_flash.h index 4a7c184de4..0de03e1218 100644 --- a/components/spi_flash/include/esp_flash.h +++ b/components/spi_flash/include/esp_flash.h @@ -247,17 +247,34 @@ esp_err_t esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t addres /** @brief Encrypted and write data to the SPI flash chip using on-chip hardware flash encryption * - * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init() + * @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted write is not supported. * @param address Address on flash to write to. 16 byte aligned. Must be previously erased (SPI NOR flash can only write bits 1->0). * @param buffer Pointer to a buffer with the data to write. * @param length Length (in bytes) of data to write. 16 byte aligned. * * @note Both address & length must be 16 byte aligned, as this is the encryption block size * - * @return ESP_OK on success, or a flash error code if operation failed. + * @return + * - ESP_OK: on success + * - ESP_ERR_NOT_SUPPORTED: encrypted write not supported for this chip. + * - ESP_ERR_INVALID_ARG: Either the address, buffer or length is invalid. + * - or other flash error code from spi_flash_write_encrypted(). */ esp_err_t esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length); +/** @brief Read and decrypt data from the SPI flash chip using on-chip hardware flash encryption + * + * @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted read is not supported. + * @param address Address on flash to read from. + * @param out_buffer Pointer to a buffer for the data to read to. + * @param length Length (in bytes) of data to read. + * + * @return + * - ESP_OK: on success + * - ESP_ERR_NOT_SUPPORTED: encrypted read not supported for this chip. + * - or other flash error code from spi_flash_read_encrypted(). + */ +esp_err_t esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length); /** @brief Pointer to the "default" SPI flash chip, ie the main chip attached to the MCU. diff --git a/components/spi_flash/test/test_flash_encryption.c b/components/spi_flash/test/test_flash_encryption.c index e16ca93719..a5cc5201ed 100644 --- a/components/spi_flash/test/test_flash_encryption.c +++ b/components/spi_flash/test/test_flash_encryption.c @@ -12,6 +12,7 @@ #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length); +static void test_encrypted_write_new_impl(size_t offset, const uint8_t *data, size_t length); static void verify_erased_flash(size_t offset, size_t length); static size_t start; @@ -86,6 +87,67 @@ static void test_encrypted_write(size_t offset, const uint8_t *data, size_t leng TEST_ASSERT_EQUAL_HEX8_ARRAY(data, readback, length); } +TEST_CASE("test 16 byte encrypted writes (esp_flash)", "[flash_encryption][esp_flash_enc][test_env=UT_T1_FlashEncryption]") +{ + setup_tests(); + + TEST_ASSERT_EQUAL_HEX(ESP_OK, + spi_flash_erase_sector(start / SPI_FLASH_SEC_SIZE)); + + uint8_t fortyeight_bytes[0x30]; // 0, 1, 2, 3, 4... 47 + for(int i = 0; i < sizeof(fortyeight_bytes); i++) { + fortyeight_bytes[i] = i; + } + + /* Verify unaligned start or length fails */ + TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_ARG, + esp_flash_write_encrypted(NULL, start+1, fortyeight_bytes, 32)); + + TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_SIZE, + esp_flash_write_encrypted(NULL, start, fortyeight_bytes, 15)); + + /* ensure nothing happened to the flash yet */ + verify_erased_flash(start, 0x20); + + /* Write 32 byte block, this is the "normal" encrypted write */ + test_encrypted_write_new_impl(start, fortyeight_bytes, 0x20); + verify_erased_flash(start + 0x20, 0x20); + + /* Slip in an unaligned esp_flash_read_encrypted() test */ + uint8_t buf[0x10]; + esp_flash_read_encrypted(NULL, start+0x10, buf, 0x10); + TEST_ASSERT_EQUAL_HEX8_ARRAY(fortyeight_bytes+0x10, buf, 16); + + /* Write 16 bytes unaligned */ + test_encrypted_write_new_impl(start + 0x30, fortyeight_bytes, 0x10); + /* the 16 byte regions before and after the 16 bytes we just wrote should still be 0xFF */ + verify_erased_flash(start + 0x20, 0x10); + verify_erased_flash(start + 0x40, 0x10); + + /* Write 48 bytes starting at a 32-byte aligned offset */ + test_encrypted_write_new_impl(start + 0x40, fortyeight_bytes, 0x30); + /* 16 bytes after this write should still be 0xFF -unencrypted- */ + verify_erased_flash(start + 0x70, 0x10); + + /* Write 48 bytes starting at a 16-byte aligned offset */ + test_encrypted_write_new_impl(start + 0x90, fortyeight_bytes, 0x30); + /* 16 bytes after this write should still be 0xFF -unencrypted- */ + verify_erased_flash(start + 0x120, 0x10); +} + +static void test_encrypted_write_new_impl(size_t offset, const uint8_t *data, size_t length) +{ + uint8_t readback[length]; + printf("encrypt %d bytes at 0x%x\n", length, offset); + TEST_ASSERT_EQUAL_HEX(ESP_OK, + esp_flash_write_encrypted(NULL, offset, data, length)); + + TEST_ASSERT_EQUAL_HEX(ESP_OK, + esp_flash_read_encrypted(NULL, offset, readback, length)); + + TEST_ASSERT_EQUAL_HEX8_ARRAY(data, readback, length); +} + static void verify_erased_flash(size_t offset, size_t length) { uint8_t readback[length]; From d7407699166cf04a881aec6ae4d9adf4f554613e Mon Sep 17 00:00:00 2001 From: Hrudaynath Dhabe Date: Thu, 12 Sep 2019 13:45:44 +0800 Subject: [PATCH 145/146] docs: wifi: wifi_guide update for disconnection warning in case of invalid initiation in `WIFI_MODE_APSTA`. Added warning to expect momentary disconnection of exSTA if channel of exAP is different than that of softAP and a prescribed solution. --- docs/en/api-guides/wifi.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-guides/wifi.rst b/docs/en/api-guides/wifi.rst index b6afd63779..66c8c683be 100644 --- a/docs/en/api-guides/wifi.rst +++ b/docs/en/api-guides/wifi.rst @@ -1385,7 +1385,7 @@ Following table depicts which country info is used in different WiFi Mode and di Home Channel ************************* -In AP mode, the home channel is defined as that of the AP channel. In Station mode, the home channel is defined as the channel of the AP to which the station is connected. In Station+AP mode, the home channel of AP and station must be the same. If the home channels of Station and AP are different, the station's home channel is always in priority. Take the following as an example: at the beginning, the AP is on channel 6, then the station connects to an AP whose channel is 9. Since the station's home channel has a higher priority, the AP needs to switch its channel from 6 to 9 to make sure that both station and AP have the same home channel. +In AP mode, the home channel is defined as that of the AP channel. In Station mode, the home channel is defined as the channel of the AP to which the station is connected. In Station+AP mode, the home channel of AP and station must be the same. If the home channels of Station and AP are different, the station's home channel is always in priority. Take the following as an example: at the beginning, the AP is on channel 6, then the station connects to an AP whose channel is 9. Since the station's home channel has a higher priority, the AP needs to switch its channel from 6 to 9 to make sure that both station and AP have the same home channel. While switching channel, the ESP32 in SoftAP mode will notify the connected stations about the channel migration using a Channel Switch Announcement (CSA). Stations that support channel switching will transition smoothly whereas stations who do not will disconnect and reconnect to the SoftAP. Wi-Fi Vendor IE Configuration From 3ca07b3e70f7c2716be87cea6535d27dfaaf46a4 Mon Sep 17 00:00:00 2001 From: Liu Han Date: Thu, 12 Sep 2019 13:55:47 +0800 Subject: [PATCH 146/146] fix(transport): Fix a bug of the connection whether be active or not by timeout option when the select function return a correct value. --- components/esp-tls/esp_tls.c | 1 + components/tcp_transport/transport_ssl.c | 29 ++++++++++++++++++++++-- components/tcp_transport/transport_tcp.c | 29 ++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index fc5e76754b..a0c916a660 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -131,6 +131,7 @@ static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *s struct timeval tv; ms_to_timeval(cfg->timeout_ms, &tv); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); } if (cfg->non_block) { int flags = fcntl(fd, F_GETFL, 0); diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index d5b13490ac..b92c21157c 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -78,30 +78,55 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int ssl->tls = NULL; return -1; } + return 0; } static int ssl_poll_read(esp_transport_handle_t t, int timeout_ms) { transport_ssl_t *ssl = esp_transport_get_context_data(t); + int ret = -1; fd_set readset; + fd_set errset; FD_ZERO(&readset); + FD_ZERO(&errset); FD_SET(ssl->tls->sockfd, &readset); + FD_SET(ssl->tls->sockfd, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(ssl->tls->sockfd + 1, &readset, NULL, NULL, &timeout); + ret = select(ssl->tls->sockfd + 1, &readset, NULL, &errset, &timeout); + if (ret > 0 && FD_ISSET(ssl->tls->sockfd, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "ssl_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd); + ret = -1; + } + return ret; } static int ssl_poll_write(esp_transport_handle_t t, int timeout_ms) { transport_ssl_t *ssl = esp_transport_get_context_data(t); + int ret = -1; fd_set writeset; + fd_set errset; FD_ZERO(&writeset); + FD_ZERO(&errset); FD_SET(ssl->tls->sockfd, &writeset); + FD_SET(ssl->tls->sockfd, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(ssl->tls->sockfd + 1, NULL, &writeset, NULL, &timeout); + ret = select(ssl->tls->sockfd + 1, NULL, &writeset, &errset, &timeout); + if (ret > 0 && FD_ISSET(ssl->tls->sockfd, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "ssl_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd); + ret = -1; + } + return ret; } static int ssl_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms) diff --git a/components/tcp_transport/transport_tcp.c b/components/tcp_transport/transport_tcp.c index 6de1d22379..3fba399a05 100644 --- a/components/tcp_transport/transport_tcp.c +++ b/components/tcp_transport/transport_tcp.c @@ -77,6 +77,7 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int esp_transport_utils_ms_to_timeval(timeout_ms, &tv); setsockopt(tcp->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(tcp->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); ESP_LOGD(TAG, "[sock=%d],connecting to server IP:%s,Port:%d...", tcp->sock, ipaddr_ntoa((const ip_addr_t*)&remote_ip.sin_addr.s_addr), port); @@ -115,23 +116,47 @@ static int tcp_read(esp_transport_handle_t t, char *buffer, int len, int timeout static int tcp_poll_read(esp_transport_handle_t t, int timeout_ms) { transport_tcp_t *tcp = esp_transport_get_context_data(t); + int ret = -1; fd_set readset; + fd_set errset; FD_ZERO(&readset); + FD_ZERO(&errset); FD_SET(tcp->sock, &readset); + FD_SET(tcp->sock, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(tcp->sock + 1, &readset, NULL, NULL, &timeout); + ret = select(tcp->sock + 1, &readset, NULL, &errset, &timeout); + if (ret > 0 && FD_ISSET(tcp->sock, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "tcp_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock); + ret = -1; + } + return ret; } static int tcp_poll_write(esp_transport_handle_t t, int timeout_ms) { transport_tcp_t *tcp = esp_transport_get_context_data(t); + int ret = -1; fd_set writeset; + fd_set errset; FD_ZERO(&writeset); + FD_ZERO(&errset); FD_SET(tcp->sock, &writeset); + FD_SET(tcp->sock, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(tcp->sock + 1, NULL, &writeset, NULL, &timeout); + ret = select(tcp->sock + 1, NULL, &writeset, &errset, &timeout); + if (ret > 0 && FD_ISSET(tcp->sock, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "tcp_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock); + ret = -1; + } + return ret; } static int tcp_close(esp_transport_handle_t t)