Merge branch 'bugfix/ble_mesh_example_nvs_store_v4.0' into 'release/v4.0'

Bugfix/ble mesh example nvs store (v4.0)

See merge request espressif/esp-idf!9017
This commit is contained in:
Island 2020-06-03 15:12:17 +08:00
commit 5a61c4c24c
21 changed files with 515 additions and 218 deletions

View File

@ -179,9 +179,12 @@ esp_err_t esp_ble_mesh_server_model_send_msg(esp_ble_mesh_model_t *model,
esp_ble_mesh_msg_ctx_t *ctx, uint32_t opcode,
uint16_t length, uint8_t *data)
{
if (!model || !ctx) {
if (model == NULL || ctx == NULL ||
ctx->net_idx == ESP_BLE_MESH_KEY_UNUSED ||
ctx->app_idx == ESP_BLE_MESH_KEY_UNUSED) {
return ESP_ERR_INVALID_ARG;
}
return ble_mesh_model_send_msg(model, ctx, opcode, BTC_BLE_MESH_ACT_SERVER_MODEL_SEND,
length, data, 0, false, ROLE_NODE);
}
@ -191,9 +194,12 @@ esp_err_t esp_ble_mesh_client_model_send_msg(esp_ble_mesh_model_t *model,
uint16_t length, uint8_t *data, int32_t msg_timeout,
bool need_rsp, esp_ble_mesh_dev_role_t device_role)
{
if (!model || !ctx) {
if (model == NULL || ctx == NULL ||
ctx->net_idx == ESP_BLE_MESH_KEY_UNUSED ||
ctx->app_idx == ESP_BLE_MESH_KEY_UNUSED) {
return ESP_ERR_INVALID_ARG;
}
return ble_mesh_model_send_msg(model, ctx, opcode, BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND,
length, data, msg_timeout, need_rsp, device_role);
}
@ -202,9 +208,11 @@ esp_err_t esp_ble_mesh_model_publish(esp_ble_mesh_model_t *model, uint32_t opcod
uint16_t length, uint8_t *data,
esp_ble_mesh_dev_role_t device_role)
{
if (!model || !model->pub || !model->pub->msg) {
if (model == NULL || model->pub == NULL || model->pub->msg == NULL ||
model->pub->publish_addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
return ESP_ERR_INVALID_ARG;
}
return ble_mesh_model_send_msg(model, NULL, opcode, BTC_BLE_MESH_ACT_MODEL_PUBLISH,
length, data, 0, false, device_role);
}

View File

@ -58,8 +58,10 @@ esp_err_t esp_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_
btc_ble_mesh_config_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) ||
(config_client_get_need_param(params->opcode) && !get_state)) {
if (params == NULL || params->model == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
!ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) ||
(config_client_get_need_param(params->opcode) && get_state == NULL)) {
return ESP_ERR_INVALID_ARG;
}
@ -81,8 +83,10 @@ esp_err_t esp_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_
btc_ble_mesh_config_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) ||
(params->opcode != ESP_BLE_MESH_MODEL_OP_NODE_RESET && !set_state)) {
if (params == NULL || params->model == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
!ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) ||
(params->opcode != ESP_BLE_MESH_MODEL_OP_NODE_RESET && set_state == NULL)) {
return ESP_ERR_INVALID_ARG;
}

View File

@ -45,8 +45,11 @@ esp_err_t esp_ble_mesh_generic_client_get_state(esp_ble_mesh_client_common_param
btc_ble_mesh_generic_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr ||
(generic_client_get_need_param(params->opcode) && !get_state)) {
if (params == NULL || params->model == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED ||
(generic_client_get_need_param(params->opcode) && get_state == NULL)) {
return ESP_ERR_INVALID_ARG;
}
@ -68,7 +71,10 @@ esp_err_t esp_ble_mesh_generic_client_set_state(esp_ble_mesh_client_common_param
btc_ble_mesh_generic_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || !set_state) {
if (params == NULL || params->model == NULL || set_state == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
return ESP_ERR_INVALID_ARG;
}

View File

@ -39,8 +39,11 @@ esp_err_t esp_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_
btc_ble_mesh_health_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || (!get_state &&
params->opcode == ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET)) {
if (params == NULL || params->model == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED ||
(params->opcode == ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET && get_state == NULL)) {
return ESP_ERR_INVALID_ARG;
}
@ -62,7 +65,10 @@ esp_err_t esp_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_
btc_ble_mesh_health_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || !set_state) {
if (params == NULL || params->model == NULL || set_state == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
return ESP_ERR_INVALID_ARG;
}

View File

@ -32,8 +32,11 @@ esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t
btc_ble_mesh_lighting_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || (!get_state &&
params->opcode == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET)) {
if (params == NULL || params->model == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED ||
(params->opcode == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET && get_state == NULL)) {
return ESP_ERR_INVALID_ARG;
}
@ -55,7 +58,10 @@ esp_err_t esp_ble_mesh_light_client_set_state(esp_ble_mesh_client_common_param_t
btc_ble_mesh_lighting_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || !set_state) {
if (params == NULL || params->model == NULL || set_state == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
return ESP_ERR_INVALID_ARG;
}

View File

@ -32,7 +32,10 @@ esp_err_t esp_ble_mesh_sensor_client_get_state(esp_ble_mesh_client_common_param_
btc_ble_mesh_sensor_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || !get_state) {
if (params == NULL || params->model == NULL || get_state == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
return ESP_ERR_INVALID_ARG;
}
@ -54,7 +57,10 @@ esp_err_t esp_ble_mesh_sensor_client_set_state(esp_ble_mesh_client_common_param_
btc_ble_mesh_sensor_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || !set_state) {
if (params == NULL || params->model == NULL || set_state == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
return ESP_ERR_INVALID_ARG;
}

View File

@ -32,8 +32,11 @@ esp_err_t esp_ble_mesh_time_scene_client_get_state(esp_ble_mesh_client_common_pa
btc_ble_mesh_time_scene_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || (!get_state &&
params->opcode == ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET)) {
if (params == NULL || params->model == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED ||
(params->opcode == ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET && get_state == NULL)) {
return ESP_ERR_INVALID_ARG;
}
@ -55,7 +58,10 @@ esp_err_t esp_ble_mesh_time_scene_client_set_state(esp_ble_mesh_client_common_pa
btc_ble_mesh_time_scene_client_args_t arg = {0};
btc_msg_t msg = {0};
if (!params || !params->model || !params->ctx.addr || !set_state) {
if (params == NULL || params->model == NULL || set_state == NULL ||
params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED ||
params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
return ESP_ERR_INVALID_ARG;
}

View File

@ -380,12 +380,11 @@ void btc_ble_mesh_config_client_publish_callback(u32_t opcode,
}
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)
esp_ble_mesh_cfg_client_get_state_t *get)
{
struct bt_mesh_msg_ctx ctx = {0};
if (!params || !cb) {
if (params == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
@ -420,68 +419,65 @@ static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param
switch (params->opcode) {
case ESP_BLE_MESH_MODEL_OP_BEACON_GET:
return (cb->error_code = bt_mesh_cfg_beacon_get(&ctx));
return bt_mesh_cfg_beacon_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET:
return (cb->error_code = bt_mesh_cfg_ttl_get(&ctx));
return bt_mesh_cfg_ttl_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_FRIEND_GET:
return (cb->error_code = bt_mesh_cfg_friend_get(&ctx));
return bt_mesh_cfg_friend_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET:
return (cb->error_code = bt_mesh_cfg_gatt_proxy_get(&ctx));
return bt_mesh_cfg_gatt_proxy_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_RELAY_GET:
return (cb->error_code = bt_mesh_cfg_relay_get(&ctx));
return bt_mesh_cfg_relay_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET:
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));
return 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 (cb->error_code = bt_mesh_cfg_hb_pub_get(&ctx));
return bt_mesh_cfg_hb_pub_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET:
return (cb->error_code = bt_mesh_cfg_hb_sub_get(&ctx));
return bt_mesh_cfg_hb_sub_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET:
return (cb->error_code = bt_mesh_cfg_comp_data_get(&ctx, get->comp_data_get.page));
return bt_mesh_cfg_comp_data_get(&ctx, get->comp_data_get.page);
case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET:
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));
return 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 (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));
return 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 (cb->error_code = bt_mesh_cfg_net_key_get(&ctx));
return bt_mesh_cfg_net_key_get(&ctx);
case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET:
return (cb->error_code = bt_mesh_cfg_app_key_get(&ctx, get->app_key_get.net_idx));
return bt_mesh_cfg_app_key_get(&ctx, get->app_key_get.net_idx);
case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET:
return (cb->error_code = bt_mesh_cfg_node_identity_get(&ctx, get->node_identity_get.net_idx));
return bt_mesh_cfg_node_identity_get(&ctx, get->node_identity_get.net_idx);
case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET:
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));
return 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 (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));
return 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 (cb->error_code = bt_mesh_cfg_kr_phase_get(&ctx, get->kr_phase_get.net_idx));
return bt_mesh_cfg_kr_phase_get(&ctx, get->kr_phase_get.net_idx);
case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET:
return (cb->error_code = bt_mesh_cfg_lpn_timeout_get(&ctx, get->lpn_pollto_get.lpn_addr));
return bt_mesh_cfg_lpn_timeout_get(&ctx, get->lpn_pollto_get.lpn_addr);
case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET:
return (cb->error_code = bt_mesh_cfg_net_transmit_get(&ctx));
return bt_mesh_cfg_net_transmit_get(&ctx);
default:
BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode);
return (cb->error_code = -EINVAL);
return -EINVAL;
}
return 0;
}
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)
esp_ble_mesh_cfg_client_set_state_t *set)
{
struct bt_mesh_msg_ctx ctx = {0};
if (!params || !cb) {
if (params == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
@ -501,29 +497,28 @@ static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param
switch (params->opcode) {
case ESP_BLE_MESH_MODEL_OP_BEACON_SET:
return (cb->error_code = bt_mesh_cfg_beacon_set(&ctx, set->beacon_set.beacon));
return bt_mesh_cfg_beacon_set(&ctx, set->beacon_set.beacon);
case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET:
return (cb->error_code = bt_mesh_cfg_ttl_set(&ctx, set->default_ttl_set.ttl));
return bt_mesh_cfg_ttl_set(&ctx, set->default_ttl_set.ttl);
case ESP_BLE_MESH_MODEL_OP_FRIEND_SET:
return (cb->error_code = bt_mesh_cfg_friend_set(&ctx, set->friend_set.friend_state));
return bt_mesh_cfg_friend_set(&ctx, set->friend_set.friend_state);
case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET:
return (cb->error_code = bt_mesh_cfg_gatt_proxy_set(&ctx, set->gatt_proxy_set.gatt_proxy));
return bt_mesh_cfg_gatt_proxy_set(&ctx, set->gatt_proxy_set.gatt_proxy);
case ESP_BLE_MESH_MODEL_OP_RELAY_SET:
return (cb->error_code =
bt_mesh_cfg_relay_set(&ctx, set->relay_set.relay, set->relay_set.relay_retransmit));
return 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 (cb->error_code =
bt_mesh_cfg_net_key_add(&ctx, set->net_key_add.net_idx,
&set->net_key_add.net_key[0]));
return 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 (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]));
return 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 (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));
return 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->model_pub_set.publish_addr,
@ -533,50 +528,46 @@ static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param
.period = set->model_pub_set.publish_period,
.transmit = set->model_pub_set.publish_retransmit,
};
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));
return 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 (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));
return 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 (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));
return 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 (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));
return 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 (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));
return 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 (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));
return 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 (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));
return 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 (cb->error_code =
bt_mesh_cfg_hb_sub_set(&ctx,
(struct bt_mesh_cfg_hb_sub *)&set->heartbeat_sub_set));
return 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 (cb->error_code =
bt_mesh_cfg_hb_pub_set(&ctx,
(const struct bt_mesh_cfg_hb_pub *)&set->heartbeat_pub_set));
return 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 (cb->error_code = bt_mesh_cfg_node_reset(&ctx));
return 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->model_pub_va_set.publish_app_idx,
@ -585,49 +576,43 @@ static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param
.period = set->model_pub_va_set.publish_period,
.transmit = set->model_pub_va_set.publish_retransmit,
};
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));
return 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 (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));
return 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 (cb->error_code =
bt_mesh_cfg_net_key_update(&ctx, set->net_key_update.net_idx,
set->net_key_update.net_key));
return 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 (cb->error_code =
bt_mesh_cfg_net_key_delete(&ctx, set->net_key_delete.net_idx));
return bt_mesh_cfg_net_key_delete(&ctx, set->net_key_delete.net_idx);
case ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE:
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));
return 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 (cb->error_code =
bt_mesh_cfg_app_key_delete(&ctx, set->app_key_delete.net_idx,
set->app_key_delete.app_idx));
return 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 (cb->error_code =
bt_mesh_cfg_node_identity_set(&ctx, set->node_identity_set.net_idx,
set->node_identity_set.identity));
return 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 (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));
return 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 (cb->error_code =
bt_mesh_cfg_kr_phase_set(&ctx, set->kr_phase_set.net_idx,
set->kr_phase_set.transition));
return 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 (cb->error_code =
bt_mesh_cfg_net_transmit_set(&ctx, set->net_transmit_set.net_transmit));
return bt_mesh_cfg_net_transmit_set(&ctx, set->net_transmit_set.net_transmit);
default:
BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode);
return (cb->error_code = -EINVAL);
return -EINVAL;
}
return 0;
@ -655,9 +640,8 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg)
BT_ERR("%s, Failed to set model role", __func__);
break;
}
btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params,
arg->cfg_client_get_state.get_state,
&cb);
cb.error_code = btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params,
arg->cfg_client_get_state.get_state);
if (cb.error_code) {
btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT);
}
@ -671,9 +655,8 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg)
BT_ERR("%s, Failed to set model role", __func__);
break;
}
btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params,
arg->cfg_client_set_state.set_state,
&cb);
cb.error_code = btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params,
arg->cfg_client_set_state.set_state);
if (cb.error_code) {
btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT);
}

View File

@ -314,12 +314,11 @@ void btc_ble_mesh_health_publish_callback(u32_t opcode,
}
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)
esp_ble_mesh_health_client_get_state_t *get)
{
struct bt_mesh_msg_ctx ctx = {0};
if (!params || !cb) {
if (params == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
@ -339,26 +338,25 @@ static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param
switch (params->opcode) {
case ESP_BLE_MESH_MODEL_OP_ATTENTION_GET:
return (cb->error_code = bt_mesh_health_attention_get(&ctx));
return 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));
return 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));
return bt_mesh_health_fault_get(&ctx, get->fault_get.company_id);
default:
BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode);
return (cb->error_code = -EINVAL);
return -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)
esp_ble_mesh_health_client_set_state_t *set)
{
struct bt_mesh_msg_ctx ctx = {0};
if (!params || !set || !cb) {
if (params == NULL || set == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
@ -373,32 +371,24 @@ static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param
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));
return 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));
return 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));
return 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));
return 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));
return 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));
return 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));
return 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));
return bt_mesh_health_fault_clear(&ctx, set->fault_clear.company_id, false);
default:
BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode);
return (cb->error_code = -EINVAL);
return -EINVAL;
}
return 0;
@ -426,8 +416,8 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg)
BT_ERR("%s, Failed to set model role", __func__);
break;
}
btc_ble_mesh_health_client_get_state(arg->health_client_get_state.params,
arg->health_client_get_state.get_state, &cb);
cb.error_code = btc_ble_mesh_health_client_get_state(arg->health_client_get_state.params,
arg->health_client_get_state.get_state);
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);
@ -442,8 +432,8 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg)
BT_ERR("%s, Failed to set model role", __func__);
break;
}
btc_ble_mesh_health_client_set_state(arg->health_client_set_state.params,
arg->health_client_set_state.set_state, &cb);
cb.error_code = btc_ble_mesh_health_client_set_state(arg->health_client_set_state.params,
arg->health_client_set_state.set_state);
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);

View File

@ -398,13 +398,13 @@ static int publish_retransmit(struct bt_mesh_model *mod)
key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key);
if (!key) {
BT_ERR("%s, Failed to find AppKey", __func__);
BT_ERR("%s, AppKey 0x%03x not exists", __func__, pub->key);
return -EADDRNOTAVAIL;
}
tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, key->net_idx);
if (!tx.sub) {
BT_ERR("%s, Failed to get subnet", __func__);
BT_ERR("%s, Subnet 0x%04x not exists", __func__, key->net_idx);
return -EADDRNOTAVAIL;
}
@ -832,9 +832,9 @@ void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
u8_t count = 0U;
int i;
BT_INFO("app_idx 0x%04x src 0x%04x dst 0x%04x", rx->ctx.app_idx,
BT_INFO("recv, app_idx 0x%04x src 0x%04x dst 0x%04x", rx->ctx.app_idx,
rx->ctx.addr, rx->ctx.recv_dst);
BT_INFO("len %u: %s", buf->len, bt_hex(buf->data, buf->len));
BT_INFO("recv, len %u: %s", buf->len, bt_hex(buf->data, buf->len));
if (get_opcode(buf, &opcode) < 0) {
BT_WARN("%s, Unable to decode OpCode", __func__);
@ -955,12 +955,12 @@ static int model_send(struct bt_mesh_model *model,
return -EINVAL;
}
BT_INFO("app_idx 0x%04x src 0x%04x dst 0x%04x",
BT_INFO("send, app_idx 0x%04x src 0x%04x dst 0x%04x",
tx->ctx->app_idx, tx->src, tx->ctx->addr);
BT_INFO("len %u: %s", msg->len, bt_hex(msg->data, msg->len));
BT_INFO("send, len %u: %s", msg->len, bt_hex(msg->data, msg->len));
if (!ready_to_send(role, tx->ctx->addr)) {
BT_ERR("%s, fail", __func__);
BT_ERR("%s, Not ready", __func__);
return -EINVAL;
}
@ -1045,7 +1045,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model)
key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key);
if (!key) {
BT_ERR("%s, Failed to get AppKey", __func__);
BT_ERR("%s, AppKey 0x%03x not exists", __func__, pub->key);
return -EADDRNOTAVAIL;
}
@ -1070,7 +1070,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model)
tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, ctx.net_idx);
if (!tx.sub) {
BT_ERR("%s, Failed to get subnet", __func__);
BT_ERR("%s, Subnet 0x%04x not exists", __func__, ctx.net_idx);
return -EADDRNOTAVAIL;
}

View File

@ -636,6 +636,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg,
tx->ctx->addr, bt_mesh.seq,
BLE_MESH_NET_IVI_TX);
if (err) {
BT_ERR("%s, Encrypt failed", __func__);
return err;
}
@ -1967,7 +1968,7 @@ int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx,
app_key = bt_mesh_tx_appkey_get(role, app_idx);
if (!app_key) {
BT_ERR("%s, Failed to get AppKey", __func__);
BT_ERR("%s, AppKey 0x%04x not exists", __func__, app_idx);
return -ENOENT;
}

View File

@ -3,7 +3,8 @@
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/button
$ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init)
$ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init
$ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_nvs)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(onoff_client)

View File

@ -6,6 +6,7 @@
PROJECT_NAME := onoff_client
EXTRA_COMPONENT_DIRS := $(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/button \
$(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/example_init
$(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/example_init \
$(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/example_nvs
include $(IDF_PATH)/make/project.mk

View File

@ -21,17 +21,26 @@
#include "board.h"
#include "ble_mesh_example_init.h"
#include "ble_mesh_example_nvs.h"
#define CID_ESP 0x02E5
static uint8_t dev_uuid[16] = { 0xdd, 0xdd };
static uint16_t node_net_idx = ESP_BLE_MESH_KEY_UNUSED;
static uint16_t node_app_idx = ESP_BLE_MESH_KEY_UNUSED;
static uint8_t remote_onoff = LED_OFF;
static uint8_t msg_tid = 0x0;
/* The remote node address shall be input through UART1, see board.c */
uint16_t remote_addr = ESP_BLE_MESH_ADDR_UNASSIGNED;
static struct example_info_store {
uint16_t net_idx; /* NetKey Index */
uint16_t app_idx; /* AppKey Index */
uint8_t onoff; /* Remote OnOff */
uint8_t tid; /* Message TID */
} __attribute__((packed)) store = {
.net_idx = ESP_BLE_MESH_KEY_UNUSED,
.app_idx = ESP_BLE_MESH_KEY_UNUSED,
.onoff = LED_OFF,
.tid = 0x0,
};
static nvs_handle_t NVS_HANDLE;
static const char * NVS_KEY = "onoff_client";
static esp_ble_mesh_client_t onoff_client;
@ -85,20 +94,51 @@ static esp_ble_mesh_prov_t provision = {
#endif
};
static void mesh_example_info_store(void)
{
ble_mesh_nvs_store(NVS_HANDLE, NVS_KEY, &store, sizeof(store));
}
static void mesh_example_info_restore(void)
{
esp_err_t err = ESP_OK;
bool exist = false;
err = ble_mesh_nvs_restore(NVS_HANDLE, NVS_KEY, &store, sizeof(store), &exist);
if (err != ESP_OK) {
return;
}
if (exist) {
ESP_LOGI(TAG, "Restore, net_idx 0x%04x, app_idx 0x%04x, onoff %u, tid 0x%02x",
store.net_idx, store.app_idx, store.onoff, store.tid);
}
}
static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32_t iv_index)
{
ESP_LOGI(TAG, "net_idx: 0x%04x, addr: 0x%04x", net_idx, addr);
ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08x", flags, iv_index);
board_led_operation(LED_G, LED_OFF);
node_net_idx = net_idx;
store.net_idx = net_idx;
/* mesh_example_info_store() shall not be invoked here, because if the device
* is restarted and goes into a provisioned state, then the following events
* will come:
* 1st: ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT
* 2nd: ESP_BLE_MESH_PROV_REGISTER_COMP_EVT
* So the store.net_idx will be updated here, and if we store the mesh example
* info here, the wrong app_idx (initialized with 0xFFFF) will be stored in nvs
* just before restoring it.
*/
}
static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event,
esp_ble_mesh_prov_cb_param_t *param)
esp_ble_mesh_prov_cb_param_t *param)
{
switch (event) {
case ESP_BLE_MESH_PROV_REGISTER_COMP_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, err_code %d", param->prov_register_comp.err_code);
mesh_example_info_restore(); /* Restore proper mesh example info */
break;
case ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT, err_code %d", param->node_prov_enable_comp.err_code);
@ -130,12 +170,12 @@ void example_ble_mesh_send_gen_onoff_set(void)
{
esp_ble_mesh_generic_client_set_state_t set = {0};
esp_ble_mesh_client_common_param_t common = {0};
esp_err_t err;
esp_err_t err = ESP_OK;
common.opcode = ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK;
common.model = onoff_client.model;
common.ctx.net_idx = node_net_idx;
common.ctx.app_idx = node_app_idx;
common.ctx.net_idx = store.net_idx;
common.ctx.app_idx = store.app_idx;
common.ctx.addr = 0xFFFF; /* to all nodes */
common.ctx.send_ttl = 3;
common.ctx.send_rel = false;
@ -143,22 +183,24 @@ void example_ble_mesh_send_gen_onoff_set(void)
common.msg_role = ROLE_NODE;
set.onoff_set.op_en = false;
set.onoff_set.onoff = remote_onoff;
set.onoff_set.tid = msg_tid++;
set.onoff_set.onoff = store.onoff;
set.onoff_set.tid = store.tid++;
err = esp_ble_mesh_generic_client_set_state(&common, &set);
if (err) {
ESP_LOGE(TAG, "%s: Generic OnOff Set failed", __func__);
} else {
remote_onoff = !remote_onoff;
ESP_LOGE(TAG, "Send Generic OnOff Set Unack failed");
return;
}
store.onoff = !store.onoff;
mesh_example_info_store(); /* Store proper mesh example info */
}
static void example_ble_mesh_generic_client_cb(esp_ble_mesh_generic_client_cb_event_t event,
esp_ble_mesh_generic_client_cb_param_t *param)
{
ESP_LOGI(TAG, "%s: event is %d, error code is %d, opcode is 0x%x",
__func__, event, param->error_code, param->params->opcode);
ESP_LOGI(TAG, "Generic client, event %u, error code %d, opcode is 0x%04x",
event, param->error_code, param->params->opcode);
switch (event) {
case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT:
@ -209,7 +251,8 @@ static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t
param->value.state_change.mod_app_bind.model_id);
if (param->value.state_change.mod_app_bind.company_id == 0xFFFF &&
param->value.state_change.mod_app_bind.model_id == ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI) {
node_app_idx = param->value.state_change.mod_app_bind.app_idx;
store.app_idx = param->value.state_change.mod_app_bind.app_idx;
mesh_example_info_store(); /* Store proper mesh example info */
}
break;
default:
@ -220,7 +263,7 @@ static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t
static esp_err_t ble_mesh_init(void)
{
esp_err_t err = 0;
esp_err_t err = ESP_OK;
esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb);
esp_ble_mesh_register_generic_client_callback(example_ble_mesh_generic_client_cb);
@ -262,6 +305,12 @@ void app_main(void)
return;
}
/* Open nvs namespace for storing/restoring mesh example info */
err = ble_mesh_nvs_open(&NVS_HANDLE);
if (err) {
return;
}
ble_mesh_get_dev_uuid(dev_uuid);
/* Initialize the Bluetooth Mesh Subsystem */

View File

@ -3,7 +3,8 @@
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/button
$ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init)
$ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init
$ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_nvs)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(vendor_client)

View File

@ -6,6 +6,7 @@
PROJECT_NAME := vendor_client
EXTRA_COMPONENT_DIRS := $(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/button \
$(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/example_init
$(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/example_init \
$(IDF_PATH)/examples/bluetooth/esp_ble_mesh/common_components/example_nvs
include $(IDF_PATH)/make/project.mk

View File

@ -20,6 +20,7 @@
#include "esp_ble_mesh_config_model_api.h"
#include "ble_mesh_example_init.h"
#include "ble_mesh_example_nvs.h"
#include "board.h"
#define CID_ESP 0x02E5
@ -45,10 +46,18 @@
#define ESP_BLE_MESH_VND_MODEL_OP_SEND ESP_BLE_MESH_MODEL_OP_3(0x00, CID_ESP)
#define ESP_BLE_MESH_VND_MODEL_OP_STATUS ESP_BLE_MESH_MODEL_OP_3(0x01, CID_ESP)
static uint8_t dev_uuid[ESP_BLE_MESH_OCTET16_LEN];
static uint16_t server_address = ESP_BLE_MESH_ADDR_UNASSIGNED;
static uint16_t vnd_tid;
static int64_t start_time;
static uint8_t dev_uuid[ESP_BLE_MESH_OCTET16_LEN];
static struct example_info_store {
uint16_t server_addr; /* Vendor server unicast address */
uint16_t vnd_tid; /* TID contained in the vendor message */
} store = {
.server_addr = ESP_BLE_MESH_ADDR_UNASSIGNED,
.vnd_tid = 0,
};
static nvs_handle_t NVS_HANDLE;
static const char * NVS_KEY = "vendor_client";
static struct esp_ble_mesh_key {
uint16_t net_idx;
@ -111,6 +120,26 @@ static esp_ble_mesh_prov_t provision = {
.prov_start_address = 0x0005,
};
static void mesh_example_info_store(void)
{
ble_mesh_nvs_store(NVS_HANDLE, NVS_KEY, &store, sizeof(store));
}
static void mesh_example_info_restore(void)
{
esp_err_t err = ESP_OK;
bool exist = false;
err = ble_mesh_nvs_restore(NVS_HANDLE, NVS_KEY, &store, sizeof(store), &exist);
if (err != ESP_OK) {
return;
}
if (exist) {
ESP_LOGI(TAG, "Restore, server_addr 0x%04x, vnd_tid 0x%04x", store.server_addr, store.vnd_tid);
}
}
static void example_ble_mesh_set_msg_common(esp_ble_mesh_client_common_param_t *common,
esp_ble_mesh_node_t *node,
esp_ble_mesh_model_t *model, uint32_t opcode)
@ -139,7 +168,8 @@ static esp_err_t prov_complete(uint16_t node_index, const esp_ble_mesh_octet16_t
node_index, primary_addr, element_num, net_idx);
ESP_LOG_BUFFER_HEX("uuid", uuid, ESP_BLE_MESH_OCTET16_LEN);
server_address = primary_addr;
store.server_addr = primary_addr;
mesh_example_info_store(); /* Store proper mesh example info */
sprintf(name, "%s%02x", "NODE-", node_index);
err = esp_ble_mesh_provisioner_set_node_name(node_index, name);
@ -202,6 +232,7 @@ static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event,
switch (event) {
case ESP_BLE_MESH_PROV_REGISTER_COMP_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, err_code %d", param->prov_register_comp.err_code);
mesh_example_info_restore(); /* Restore proper mesh example info */
break;
case ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT:
ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT, err_code %d", param->provisioner_prov_enable_comp.err_code);
@ -422,31 +453,36 @@ void example_ble_mesh_send_vendor_message(bool resend)
ctx.net_idx = prov_key.net_idx;
ctx.app_idx = prov_key.app_idx;
ctx.addr = server_address;
ctx.addr = store.server_addr;
ctx.send_ttl = MSG_SEND_TTL;
ctx.send_rel = MSG_SEND_REL;
opcode = ESP_BLE_MESH_VND_MODEL_OP_SEND;
if (resend == false) {
vnd_tid++;
store.vnd_tid++;
}
err = esp_ble_mesh_client_model_send_msg(vendor_client.model, &ctx, opcode,
sizeof(vnd_tid), (uint8_t *)&vnd_tid, MSG_TIMEOUT, true, MSG_ROLE);
sizeof(store.vnd_tid), (uint8_t *)&store.vnd_tid, MSG_TIMEOUT, true, MSG_ROLE);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send vendor message 0x%06x", opcode);
return;
}
mesh_example_info_store(); /* Store proper mesh example info */
}
static void example_ble_mesh_custom_model_cb(esp_ble_mesh_model_cb_event_t event,
esp_ble_mesh_model_cb_param_t *param)
{
static int64_t start_time;
switch (event) {
case ESP_BLE_MESH_MODEL_OPERATION_EVT:
if (param->model_operation.opcode == ESP_BLE_MESH_VND_MODEL_OP_STATUS) {
int64_t end_time = esp_timer_get_time();
ESP_LOGI(TAG, "Recv 0x%06x, tid 0x%04x, time %lldus",
param->model_operation.opcode, vnd_tid, end_time - start_time);
param->model_operation.opcode, store.vnd_tid, end_time - start_time);
}
break;
case ESP_BLE_MESH_MODEL_SEND_COMP_EVT:
@ -538,6 +574,12 @@ void app_main(void)
return;
}
/* Open nvs namespace for storing/restoring mesh example info */
err = ble_mesh_nvs_open(&NVS_HANDLE);
if (err) {
return;
}
ble_mesh_get_dev_uuid(dev_uuid);
/* Initialize the Bluetooth Mesh Subsystem */

View File

@ -0,0 +1,3 @@
idf_component_register(SRCS "ble_mesh_example_nvs.c"
INCLUDE_DIRS "."
REQUIRES nvs_flash)

View File

@ -0,0 +1,151 @@
/*
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 <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "nvs_flash.h"
#define TAG "EXAMPLE_NVS"
#define NVS_NAME "mesh_example"
esp_err_t ble_mesh_nvs_open(nvs_handle_t *handle)
{
esp_err_t err = ESP_OK;
if (handle == NULL) {
ESP_LOGE(TAG, "Open, invalid nvs handle");
return ESP_ERR_INVALID_ARG;
}
err = nvs_open(NVS_NAME, NVS_READWRITE, handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Open, nvs_open failed, err %d", err);
return err;
}
ESP_LOGI(TAG, "Open namespace done, name \"%s\"", NVS_NAME);
return err;
}
esp_err_t ble_mesh_nvs_store(nvs_handle_t handle, const char *key, const void *data, size_t length)
{
esp_err_t err = ESP_OK;
if (key == NULL || data == NULL || length == 0) {
ESP_LOGE(TAG, "Store, invalid parameter");
return ESP_ERR_INVALID_ARG;
}
err = nvs_set_blob(handle, key, data, length);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Store, nvs_set_blob failed, err %d", err);
return err;
}
err = nvs_commit(handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Store, nvs_commit failed, err %d", err);
return err;
}
ESP_LOGI(TAG, "Store, key \"%s\", length %u", key, length);
ESP_LOG_BUFFER_HEX("EXAMPLE_NVS: Store, data", data, length);
return err;
}
esp_err_t ble_mesh_nvs_get_length(nvs_handle_t handle, const char *key, size_t *length)
{
esp_err_t err = ESP_OK;
if (key == NULL || length == NULL) {
ESP_LOGE(TAG, "Get length, invalid parameter");
return ESP_ERR_INVALID_ARG;
}
err = nvs_get_blob(handle, key, NULL, length);
if (err == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGI(TAG, "Get length, key \"%s\" not exists", key);
*length = 0;
return ESP_OK;
}
if (err != ESP_OK) {
ESP_LOGE(TAG, "Get length, nvs_get_blob failed, err %d", err);
} else {
ESP_LOGI(TAG, "Get length, key \"%s\", length %u", key, *length);
}
return err;
}
esp_err_t ble_mesh_nvs_restore(nvs_handle_t handle, const char *key, void *data, size_t length, bool *exist)
{
esp_err_t err = ESP_OK;
if (key == NULL || data == NULL || length == 0) {
ESP_LOGE(TAG, "Restore, invalid parameter");
return ESP_ERR_INVALID_ARG;
}
err = nvs_get_blob(handle, key, data, &length);
if (err == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGI(TAG, "Restore, key \"%s\" not exists", key);
if (exist) {
*exist = false;
}
return ESP_OK;
}
if (exist) {
*exist = true;
}
if (err != ESP_OK) {
ESP_LOGE(TAG, "Restore, nvs_get_blob failed, err %d", err);
} else {
ESP_LOGI(TAG, "Restore, key \"%s\", length %u", key, length);
ESP_LOG_BUFFER_HEX("EXAMPLE_NVS: Restore, data", data, length);
}
return err;
}
esp_err_t ble_mesh_nvs_erase(nvs_handle_t handle, const char *key)
{
esp_err_t err = ESP_OK;
if (key) {
err = nvs_erase_key(handle, key);
if (err == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGI(TAG, "Erase, key \"%s\" not exists", key);
return ESP_OK;
}
} else {
err = nvs_erase_all(handle);
}
if (err != ESP_OK) {
ESP_LOGE(TAG, "Erase, nvs_erase_%s failed, err %d", key ? "key" : "all", err);
return err;
}
err = nvs_commit(handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Erase, nvs_commit failed, err %d", err);
return err;
}
if (key) {
ESP_LOGI(TAG, "Erase done, key \"%s\"", key);
} else {
ESP_LOGI(TAG, "Erase namespace done, name \"%s\"", NVS_NAME);
}
return err;
}

View File

@ -0,0 +1,26 @@
/*
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.
*/
#ifndef _BLE_MESH_EXAMPLE_NVS_H_
#define _BLE_MESH_EXAMPLE_NVS_H_
#include <stdint.h>
#include "esp_err.h"
#include "nvs_flash.h"
esp_err_t ble_mesh_nvs_open(nvs_handle_t *handle);
esp_err_t ble_mesh_nvs_store(nvs_handle_t handle, const char *key, const void *data, size_t length);
esp_err_t ble_mesh_nvs_get_length(nvs_handle_t handle, const char *key, size_t *length);
esp_err_t ble_mesh_nvs_restore(nvs_handle_t handle, const char *key, void *data, size_t length, bool *exist);
esp_err_t ble_mesh_nvs_erase(nvs_handle_t handle, const char *key);
#endif /* _BLE_MESH_EXAMPLE_NVS_H_ */

View File

@ -0,0 +1,6 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
#
COMPONENT_ADD_INCLUDEDIRS := .