From 5cbb72602868427e6ce9ad3f646a3681c947a819 Mon Sep 17 00:00:00 2001 From: Chen Sheng Date: Sun, 22 Mar 2020 18:35:12 +0800 Subject: [PATCH] ble mesh: update ble mesh console example(backport v4.0) --- .../ble_mesh_node/main/CMakeLists.txt | 1 + .../ble_mesh_node/main/ble_mesh_adapter.c | 3 +- .../main/ble_mesh_cfg_srv_model.c | 17 +- .../main/ble_mesh_console_decl.h | 5 + .../ble_mesh_node/main/ble_mesh_console_lib.c | 14 +- .../main/ble_mesh_console_main.c | 5 + .../main/ble_mesh_reg_gen_onoff_client_cmd.c | 180 ++++++++++++ .../main/ble_mesh_register_node_cmd.c | 104 +++++-- .../main/ble_mesh_register_server_cmd.c | 29 ++ .../ble_mesh_node/sdkconfig.defaults | 9 +- .../ble_mesh_provisioner/main/CMakeLists.txt | 1 + .../main/ble_mesh_adapter.c | 27 +- .../main/ble_mesh_adapter.h | 16 +- .../main/ble_mesh_cfg_srv_model.c | 14 +- .../main/ble_mesh_console_main.c | 3 + .../main/ble_mesh_reg_cfg_client_cmd.c | 2 + .../main/ble_mesh_reg_gen_onoff_client_cmd.c | 5 +- .../main/ble_mesh_reg_test_perf_client_cmd.c | 15 +- .../main/ble_mesh_register_node_cmd.c | 47 ++-- .../main/ble_mesh_register_provisioner_cmd.c | 61 +--- .../ble_mesh_provisioner/main/transaction.c | 266 ++++++++++++++++++ .../ble_mesh_provisioner/main/transaction.h | 88 ++++++ .../ble_mesh_provisioner/sdkconfig.defaults | 10 +- 23 files changed, 760 insertions(+), 162 deletions(-) create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_reg_gen_onoff_client_cmd.c create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/transaction.c create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/transaction.h diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/CMakeLists.txt index 2275f9a4e..40921b35e 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/CMakeLists.txt +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/CMakeLists.txt @@ -5,6 +5,7 @@ set(COMPONENT_SRCS "ble_mesh_adapter.c" "ble_mesh_console_system.c" "ble_mesh_register_node_cmd.c" "ble_mesh_register_server_cmd.c" + "ble_mesh_reg_gen_onoff_client_cmd.c" "register_bluetooth.c") set(COMPONENT_ADD_INCLUDEDIRS ".") diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.c index b8d1775b3..41e1fc611 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.c @@ -29,8 +29,9 @@ esp_ble_mesh_model_t *ble_mesh_get_model(uint16_t model_id) model = &gen_onoff_srv_models[1]; break; #if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + // as server case ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: - model = &gen_onoff_cli_models[1]; + model = &gen_onoff_cli_models[2]; break; #endif case ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI: diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.c index 91d8fe396..69f37a3bd 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.c @@ -13,7 +13,7 @@ // limitations under the License. #include "ble_mesh_cfg_srv_model.h" - +#include "esp_ble_mesh_generic_model_api.h" uint8_t dev_uuid[16] = {0xdd, 0xdd}; #if CONFIG_BLE_MESH_NODE @@ -40,9 +40,8 @@ esp_ble_mesh_prov_t prov = { }; #endif //CONFIG_BLE_MESH_PROVISIONER -ESP_BLE_MESH_MODEL_PUB_DEFINE(model_pub_config, 2 + 1, ROLE_NODE); - esp_ble_mesh_model_pub_t vendor_model_pub_config; +ESP_BLE_MESH_MODEL_PUB_DEFINE(model_pub_config, 2 + 1, ROLE_NODE); // configure server module esp_ble_mesh_cfg_srv_t cfg_srv = { @@ -96,16 +95,15 @@ esp_ble_mesh_comp_t config_client_comp = { }; // configure special module -esp_ble_mesh_model_op_t gen_onoff_srv_model_op_config[] = { - ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET, 0), - ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET, 2), - ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK, 2), - ESP_BLE_MESH_MODEL_OP_END, +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, }; esp_ble_mesh_model_t gen_onoff_srv_models[] = { ESP_BLE_MESH_MODEL_CFG_SRV(&cfg_srv), - ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, gen_onoff_srv_model_op_config, &model_pub_config, NULL), + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server), }; esp_ble_mesh_elem_t gen_onoff_srv_elements[] = { @@ -126,6 +124,7 @@ esp_ble_mesh_client_t gen_onoff_cli; esp_ble_mesh_model_t gen_onoff_cli_models[] = { ESP_BLE_MESH_MODEL_CFG_SRV(&cfg_srv), ESP_BLE_MESH_MODEL_CFG_CLI(&cfg_cli), + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server), ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(&model_pub_config, &gen_onoff_cli), }; diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_decl.h b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_decl.h index 2e983265d..a18a4e7e4 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_decl.h +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_decl.h @@ -22,6 +22,11 @@ void ble_mesh_register_mesh_node(void); // Register mesh config server and generic server operation cmd void ble_mesh_register_server(void); +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) +// Register mesh client operation cmd +void ble_mesh_register_gen_onoff_client(void); +#endif + #if (CONFIG_BLE_MESH_CFG_CLI) // Register mesh config client operation cmd void ble_mesh_register_configuration_client_model(void); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.c index efcb6766a..7efe28c41 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.c @@ -66,10 +66,16 @@ int hexstr_2_bin(const char *hex, uint8_t *buf, uint32_t len) int get_value_string(char *value_in, char *buf) { int result = -1; - + uint8_t loop = 0; uint16_t length = strlen(value_in); - for(int i = 0; i 2) { @@ -85,7 +91,6 @@ int get_value_string(char *value_in, char *buf) strcpy(buf, value_in); result = 0; } - return result; } @@ -104,7 +109,6 @@ bool str_2_mac(uint8_t *str, uint8_t *dest) if (src_p[loop] != ':') { return false; } - continue; } diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_main.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_main.c index 5dba3a050..e4015d71c 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_main.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_main.c @@ -126,6 +126,8 @@ esp_err_t bluetooth_init(void) return ret; } + esp_log_level_set("*", ESP_LOG_ERROR); + esp_log_level_set("ble_mesh_node_console", ESP_LOG_INFO); return ret; } @@ -153,6 +155,9 @@ void app_main(void) register_bluetooth(); ble_mesh_register_mesh_node(); ble_mesh_register_server(); +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + ble_mesh_register_gen_onoff_client(); +#endif /* Prompt to be printed before each line. * This can be customized, made dynamic, etc. diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_reg_gen_onoff_client_cmd.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_reg_gen_onoff_client_cmd.c new file mode 100644 index 000000000..f9caa8931 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_reg_gen_onoff_client_cmd.c @@ -0,0 +1,180 @@ +// Copyright 2017-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. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_timer.h" +#include "ble_mesh_adapter.h" + +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) +typedef struct { + struct arg_str *action_type; + struct arg_int *op_en; + struct arg_int *unicast_address; + struct arg_int *onoff_state; + struct arg_int *trans_id; + struct arg_int *trans_time; + struct arg_int *delay; + struct arg_int *opcode; + struct arg_int *appkey_idx; + struct arg_int *role; + struct arg_int *net_idx; + struct arg_end *end; +} ble_mesh_gen_onoff_state_t; +ble_mesh_gen_onoff_state_t gen_onoff_state; + +void ble_mesh_register_gen_onoff_client_command(void); +void ble_mesh_generic_onoff_client_model_cb(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param); + +void ble_mesh_register_gen_onoff_client(void) +{ + ble_mesh_register_gen_onoff_client_command(); +} + +void ble_mesh_generic_onoff_client_model_cb(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param) +{ + uint32_t opcode = param->params->opcode; + + ESP_LOGD(TAG, "enter %s: event is %d, error code is %d, opcode is 0x%x\n", + __func__, event, param->error_code, opcode); + + switch (event) { + case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: { + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:GetStatus,OK,%d", param->status_cb.onoff_status.present_onoff); + } else { + ESP_LOGE(TAG, "GenOnOffClient:GetStatus,Fail,%d", param->error_code); + } + break; + default: + break; + } + break; + } + case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: { + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:SetStatus,OK,%d", param->status_cb.onoff_status.present_onoff); + } else { + ESP_LOGE(TAG, "GenOnOffClient:SetStatus,Fail,%d", param->error_code); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:SetUNACK,OK"); + } else { + ESP_LOGE(TAG, "GenOnOffClient:SetUNACK,Fail,%d", param->error_code); + } + break; + default: + break; + } + break; + } + case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: { + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:Publish,OK"); + } else { + ESP_LOGE(TAG, "GenOnOffClient:Publish,Fail,%d", param->error_code); + } + break; + } + case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: + ESP_LOGE(TAG, "GenOnOffClient:TimeOut,%d", param->error_code); + break; + case ESP_BLE_MESH_GENERIC_CLIENT_EVT_MAX: + ESP_LOGE(TAG, "GenONOFFClient:InvalidEvt,%d", param->error_code); + break; + default: + break; + } + ESP_LOGD(TAG, "exit %s \n", __func__); +} + +int ble_mesh_generic_onoff_client_model(int argc, char **argv) +{ + int err = ESP_OK; + esp_ble_mesh_generic_client_set_state_t gen_client_set; + esp_ble_mesh_generic_client_get_state_t gen_client_get; + esp_ble_mesh_client_common_param_t onoff_common = { + .msg_timeout = 0, + .ctx.send_ttl = 7, + }; + + ESP_LOGD(TAG, "enter %s\n", __func__); + + int nerrors = arg_parse(argc, argv, (void **) &gen_onoff_state); + if (nerrors != 0) { + arg_print_errors(stderr, gen_onoff_state.end, argv[0]); + return 1; + } + + onoff_common.model = ble_mesh_get_model(ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI); + + arg_int_to_value(gen_onoff_state.appkey_idx, onoff_common.ctx.app_idx, "appkey_index"); + arg_int_to_value(gen_onoff_state.opcode, onoff_common.opcode, "opcode"); + arg_int_to_value(gen_onoff_state.role, onoff_common.msg_role, "role"); + arg_int_to_value(gen_onoff_state.unicast_address, onoff_common.ctx.addr, "address"); + arg_int_to_value(gen_onoff_state.net_idx, onoff_common.ctx.net_idx, "network key index"); + arg_int_to_value(gen_onoff_state.op_en, gen_client_set.onoff_set.op_en, "op_en"); + arg_int_to_value(gen_onoff_state.onoff_state, gen_client_set.onoff_set.onoff, "onoff"); + arg_int_to_value(gen_onoff_state.trans_id, gen_client_set.onoff_set.tid, "tid"); + arg_int_to_value(gen_onoff_state.trans_time, gen_client_set.onoff_set.trans_time, "trans_time"); + arg_int_to_value(gen_onoff_state.delay, gen_client_set.onoff_set.delay, "delay"); + + if (gen_onoff_state.action_type->count != 0) { + if (strcmp(gen_onoff_state.action_type->sval[0], "get") == 0) { + err = esp_ble_mesh_generic_client_get_state(&onoff_common, &gen_client_get); + } else if (strcmp(gen_onoff_state.action_type->sval[0], "set") == 0) { + err = esp_ble_mesh_generic_client_set_state(&onoff_common, &gen_client_set); + } else if (strcmp(gen_onoff_state.action_type->sval[0], "reg") == 0) { + err = esp_ble_mesh_register_generic_client_callback(ble_mesh_generic_onoff_client_model_cb); + if (err == ESP_OK) { + ESP_LOGI(TAG, "GenONOFFClient:Reg,OK"); + } + } + } + ESP_LOGD(TAG, "exit %s\n", __func__); + return err; +} + +void ble_mesh_register_gen_onoff_client_command(void) +{ + gen_onoff_state.action_type = arg_str1("z", NULL, "", "action type"); + gen_onoff_state.opcode = arg_int0("o", NULL, "", "message opcode"); + gen_onoff_state.appkey_idx = arg_int0("a", NULL, "", "appkey index"); + gen_onoff_state.role = arg_int0("r", NULL, "", "role"); + gen_onoff_state.unicast_address = arg_int0("u", NULL, "
", "unicast address"); + gen_onoff_state.net_idx = arg_int0("n", NULL, "", "network key index"); + gen_onoff_state.op_en = arg_int0("e", NULL, "", "whether optional parameters included"); + gen_onoff_state.onoff_state = arg_int0("s", NULL, "", "present onoff state"); + gen_onoff_state.trans_id = arg_int0("i", NULL, "", "transaction identifier"); + gen_onoff_state.trans_time = arg_int0("t", NULL, "