diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c index 80f96ed05a..e121250684 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c @@ -362,6 +362,27 @@ esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_inf == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t length) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (value == NULL || length == 0 || length > 16) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL; + + arg.set_static_oob_val.length = length; + memcpy(arg.set_static_oob_val.value, value, length); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + #endif /* CONFIG_BLE_MESH_PROVISIONER */ /* The following APIs are for fast provisioning */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h index 34dc38ce7a..9145cc91cc 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h @@ -290,6 +290,17 @@ esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val, */ esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_info_t *prov_data_info); +/** + * @brief This function is called by Provisioner to set static oob value used for provisioning. + * + * @param[in] value: Pointer to the static oob value. + * @param[in] length: Length of the static oob value. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t length); + /** * @brief This function is called to set provisioning data information before starting * fast provisioning. 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 224f9a37f8..f421706958 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 @@ -737,6 +737,7 @@ typedef enum { ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT, /*!< Provisioner delete a device from the list, close provisioning link with the device if it exists and remove the device from network completion event */ ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT, /*!< Provisioner set the value to be compared with part of the unprovisioned device UUID completion event */ ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT, /*!< Provisioner set net_idx/flags/iv_index used for provisioning completion event */ + ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT, /*!< Provisioner set static oob value used for provisioning completion event */ ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT, /*!< Provisioner read unprovisioned device OOB public key completion event */ ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT, /*!< Provisioner input number completion event */ ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT, /*!< Provisioner input string completion event */ @@ -976,6 +977,12 @@ typedef union { struct ble_mesh_provisioner_set_prov_data_info_comp_param { int err_code; /*!< Indicate the result of setting provisioning info by the Provisioner */ } provisioner_set_prov_data_info_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT + */ + struct ble_mesh_provisioner_set_static_oob_val_comp_param { + int err_code; /*!< Indicate the result of setting static oob value by the Provisioner */ + } provisioner_set_static_oob_val_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT */ 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 63a9bf4310..08660588f2 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 @@ -1513,7 +1513,7 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) 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); + bt_mesh_provisioner_adv_pkt_cb_register(btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb); #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if CONFIG_BLE_MESH_LOW_POWER bt_mesh_lpn_set_cb(btc_ble_mesh_lpn_cb); @@ -1587,20 +1587,20 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) 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 = - bt_mesh_prov_read_oob_pub_key(arg->provisioner_read_oob_pub_key.link_idx, - arg->provisioner_read_oob_pub_key.pub_key_x, - arg->provisioner_read_oob_pub_key.pub_key_y); + bt_mesh_provisioner_read_oob_pub_key(arg->provisioner_read_oob_pub_key.link_idx, + arg->provisioner_read_oob_pub_key.pub_key_x, + arg->provisioner_read_oob_pub_key.pub_key_y); break; case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR: act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT; param.provisioner_prov_input_str_comp.err_code = - bt_mesh_prov_set_oob_input_data(arg->provisioner_input_str.link_idx, - (const u8_t *)&arg->provisioner_input_str.string, false); + bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_str.link_idx, + (const u8_t *)&arg->provisioner_input_str.string, false); break; case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_NUM: act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT; param.provisioner_prov_input_num_comp.err_code = - bt_mesh_prov_set_oob_input_data(arg->provisioner_input_num.link_idx, + bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_num.link_idx, (const u8_t *)&arg->provisioner_input_num.number, true); break; case BTC_BLE_MESH_ACT_PROVISIONER_ENABLE: @@ -1660,6 +1660,12 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) bt_mesh_provisioner_set_prov_data_info(&info); break; } + case BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL: + act = ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT; + param.provisioner_set_static_oob_val_comp.err_code = + bt_mesh_provisioner_set_static_oob_value( + arg->set_static_oob_val.value, arg->set_static_oob_val.length); + break; case BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME: act = ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT; param.provisioner_set_node_name_comp.err_code = 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 577854aee5..c871534c02 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 @@ -49,6 +49,7 @@ typedef enum { BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL, BTC_BLE_MESH_ACT_PROVISIONER_SET_DEV_UUID_MATCH, BTC_BLE_MESH_ACT_PROVISIONER_SET_PROV_DATA_INFO, + BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL, BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME, BTC_BLE_MESH_ACT_PROVISIONER_SET_LOCAL_APP_KEY, BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP, @@ -133,6 +134,10 @@ typedef union { struct ble_mesh_provisioner_set_prov_net_idx_args { esp_ble_mesh_prov_data_info_t prov_data; } set_prov_data_info; + struct ble_mesh_provisioner_set_static_oob_val_args { + uint8_t value[16]; + uint8_t length; + } set_static_oob_val; struct ble_mesh_provisioner_set_node_name_args { uint16_t index; char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN]; diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h index 6d1aeb85d1..5e95e3b1a0 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h @@ -268,8 +268,14 @@ s64_t k_uptime_get(void); */ void k_sleep(s32_t duration); -void bt_mesh_irq_lock(void); -void bt_mesh_irq_unlock(void); +void bt_mesh_list_lock(void); +void bt_mesh_list_unlock(void); + +void bt_mesh_buf_lock(void); +void bt_mesh_buf_unlock(void); + +void bt_mesh_atomic_lock(void); +void bt_mesh_atomic_unlock(void); void bt_mesh_k_init(void); diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c b/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c index 026f103ff5..6c2e940a19 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c @@ -57,12 +57,12 @@ bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic { bt_mesh_atomic_val_t ret; - bt_mesh_irq_lock(); + bt_mesh_atomic_lock(); ret = *target; *target = value; - bt_mesh_irq_unlock(); + bt_mesh_atomic_unlock(); return ret; } @@ -84,12 +84,12 @@ bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_ { bt_mesh_atomic_val_t ret; - bt_mesh_irq_lock(); + bt_mesh_atomic_lock(); ret = *target; *target |= value; - bt_mesh_irq_unlock(); + bt_mesh_atomic_unlock(); return ret; } @@ -111,12 +111,12 @@ bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic { bt_mesh_atomic_val_t ret; - bt_mesh_irq_lock(); + bt_mesh_atomic_lock(); ret = *target; *target &= value; - bt_mesh_irq_unlock(); + bt_mesh_atomic_unlock(); return ret; } @@ -136,12 +136,12 @@ bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target) { bt_mesh_atomic_val_t ret; - bt_mesh_irq_lock(); + bt_mesh_atomic_lock(); ret = *target; (*target)--; - bt_mesh_irq_unlock(); + bt_mesh_atomic_unlock(); return ret; } @@ -161,12 +161,12 @@ bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target) { bt_mesh_atomic_val_t ret; - bt_mesh_irq_lock(); + bt_mesh_atomic_lock(); ret = *target; (*target)++; - bt_mesh_irq_unlock(); + bt_mesh_atomic_unlock(); return ret; } diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c index af9eda6edb..2cb31d6d56 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c @@ -247,9 +247,9 @@ void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf) tail->flags |= NET_BUF_FRAGS; } - bt_mesh_irq_lock(); + bt_mesh_list_lock(); sys_slist_append_list(list, &buf->node, &tail->node); - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); } struct net_buf *net_buf_slist_get(sys_slist_t *list) @@ -258,9 +258,9 @@ struct net_buf *net_buf_slist_get(sys_slist_t *list) NET_BUF_ASSERT(list); - bt_mesh_irq_lock(); + bt_mesh_list_lock(); buf = (void *)sys_slist_get(list); - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); if (!buf) { return NULL; @@ -268,9 +268,9 @@ struct net_buf *net_buf_slist_get(sys_slist_t *list) /* Get any fragments belonging to this buffer */ for (frag = buf; (frag->flags & NET_BUF_FRAGS); frag = frag->frags) { - bt_mesh_irq_lock(); + bt_mesh_list_lock(); frag->frags = (void *)sys_slist_get(list); - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); NET_BUF_ASSERT(frag->frags); @@ -387,7 +387,7 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size, /* We need to lock interrupts temporarily to prevent race conditions * when accessing pool->uninit_count. */ - bt_mesh_irq_lock(); + bt_mesh_buf_lock(); /* If there are uninitialized buffers we're guaranteed to succeed * with the allocation one way or another. @@ -397,13 +397,13 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size, for (i = pool->buf_count; i > 0; i--) { buf = pool_get_uninit(pool, i); if (!buf->ref) { - bt_mesh_irq_unlock(); + bt_mesh_buf_unlock(); goto success; } } } - bt_mesh_irq_unlock(); + bt_mesh_buf_unlock(); NET_BUF_ERR("%s, Failed to get free buffer", __func__); return NULL; diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c index b14be67f1b..3d75efe15f 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c @@ -22,7 +22,9 @@ #include "provisioner_prov.h" static osi_mutex_t bm_alarm_lock; -static osi_mutex_t bm_irq_lock; +static osi_mutex_t bm_list_lock; +static osi_mutex_t bm_buf_lock; +static osi_mutex_t bm_atomic_lock; static hash_map_t *bm_alarm_hash_map; static const size_t BLE_MESH_GENERAL_ALARM_HASH_MAP_SIZE = 20 + CONFIG_BLE_MESH_PBA_SAME_TIME + \ CONFIG_BLE_MESH_PBG_SAME_TIME; @@ -35,14 +37,92 @@ typedef struct alarm_t { int64_t deadline_us; } osi_alarm_t; -void bt_mesh_irq_lock(void) +static void bt_mesh_alarm_mutex_new(void) { - osi_mutex_lock(&bm_irq_lock, OSI_MUTEX_MAX_TIMEOUT); + if (!bm_alarm_lock) { + osi_mutex_new(&bm_alarm_lock); + __ASSERT(bm_alarm_lock, "%s, fail", __func__); + } } -void bt_mesh_irq_unlock(void) +static void bt_mesh_alarm_lock(void) { - osi_mutex_unlock(&bm_irq_lock); + if (bm_alarm_lock) { + osi_mutex_lock(&bm_alarm_lock, OSI_MUTEX_MAX_TIMEOUT); + } +} + +static void bt_mesh_alarm_unlock(void) +{ + if (bm_alarm_lock) { + osi_mutex_unlock(&bm_alarm_lock); + } +} + +static void bt_mesh_list_mutex_new(void) +{ + if (!bm_list_lock) { + osi_mutex_new(&bm_list_lock); + __ASSERT(bm_list_lock, "%s, fail", __func__); + } +} + +void bt_mesh_list_lock(void) +{ + if (bm_list_lock) { + osi_mutex_lock(&bm_list_lock, OSI_MUTEX_MAX_TIMEOUT); + } +} + +void bt_mesh_list_unlock(void) +{ + if (bm_list_lock) { + osi_mutex_unlock(&bm_list_lock); + } +} + +static void bt_mesh_buf_mutex_new(void) +{ + if (!bm_buf_lock) { + osi_mutex_new(&bm_buf_lock); + __ASSERT(bm_buf_lock, "%s, fail", __func__); + } +} + +void bt_mesh_buf_lock(void) +{ + if (bm_buf_lock) { + osi_mutex_lock(&bm_buf_lock, OSI_MUTEX_MAX_TIMEOUT); + } +} + +void bt_mesh_buf_unlock(void) +{ + if (bm_buf_lock) { + osi_mutex_unlock(&bm_buf_lock); + } +} + +static void bt_mesh_atomic_mutex_new(void) +{ + if (!bm_atomic_lock) { + osi_mutex_new(&bm_atomic_lock); + __ASSERT(bm_atomic_lock, "%s, fail", __func__); + } +} + +void bt_mesh_atomic_lock(void) +{ + if (bm_atomic_lock) { + osi_mutex_lock(&bm_atomic_lock, OSI_MUTEX_MAX_TIMEOUT); + } +} + +void bt_mesh_atomic_unlock(void) +{ + if (bm_atomic_lock) { + osi_mutex_unlock(&bm_atomic_lock); + } } s64_t k_uptime_get(void) @@ -69,8 +149,10 @@ void k_sleep(s32_t duration) void bt_mesh_k_init(void) { - osi_mutex_new(&bm_alarm_lock); - osi_mutex_new(&bm_irq_lock); + bt_mesh_alarm_mutex_new(); + bt_mesh_list_mutex_new(); + bt_mesh_buf_mutex_new(); + bt_mesh_atomic_mutex_new(); bm_alarm_hash_map = hash_map_new(BLE_MESH_GENERAL_ALARM_HASH_MAP_SIZE, hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL); @@ -85,18 +167,19 @@ void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) k_work_init(&work->work, handler); - osi_mutex_lock(&bm_alarm_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_alarm_lock(); if (!hash_map_has_key(bm_alarm_hash_map, (void *)work)) { alarm = osi_alarm_new("bt_mesh", (osi_alarm_callback_t)handler, (void *)&work->work, 0); if (alarm == NULL) { BT_ERR("%s, Unable to create alarm", __func__); + bt_mesh_alarm_unlock(); return; } if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { BT_ERR("%s Unable to add the timer to hash map.", __func__); } } - osi_mutex_unlock(&bm_alarm_lock); + bt_mesh_alarm_unlock(); alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index 1a0c611293..b82422a9e9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -118,8 +118,10 @@ static inline void adv_send_end(int err, const struct bt_mesh_send_cb *cb, static inline int adv_send(struct net_buf *buf) { +#if 0 const s32_t adv_int_min = ((bt_mesh_dev.hci_version >= BLE_MESH_HCI_VERSION_5_0) ? ADV_INT_FAST_MS : ADV_INT_DEFAULT_MS); +#endif const struct bt_mesh_send_cb *cb = BLE_MESH_ADV(buf)->cb; void *cb_data = BLE_MESH_ADV(buf)->cb_data; struct bt_mesh_adv_param param = {0}; @@ -150,6 +152,8 @@ static inline int adv_send(struct net_buf *buf) param.interval_min = ADV_SCAN_UNIT(ADV_INT_FAST_MS); param.interval_max = param.interval_min; + bt_mesh_adv_buf_ref_debug(__func__, buf, 4U, BLE_MESH_BUF_REF_SMALL); + err = bt_le_adv_start(¶m, &ad, 1, NULL, 0); net_buf_unref(buf); adv_send_start(duration, err, cb, cb_data); @@ -585,7 +589,7 @@ static void bt_mesh_adv_srv_data_recv(struct net_buf_simple *buf, const bt_mesh_ } BT_DBG("Start to handle Mesh Prov Service Data"); - provisioner_prov_adv_ind_recv(buf, addr); + bt_mesh_provisioner_prov_adv_ind_recv(buf, addr); } break; #endif @@ -598,7 +602,7 @@ static void bt_mesh_adv_srv_data_recv(struct net_buf_simple *buf, const bt_mesh_ } BT_DBG("Start to handle Mesh Proxy Service Data"); - proxy_client_adv_ind_recv(buf, addr); + bt_mesh_proxy_client_adv_ind_recv(buf, addr); break; #endif default: @@ -667,7 +671,7 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, #endif #if CONFIG_BLE_MESH_PROVISIONER if (bt_mesh_is_provisioner_en()) { - provisioner_pb_adv_recv(buf); + bt_mesh_provisioner_pb_adv_recv(buf); } #endif break; @@ -680,7 +684,7 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, #endif #if CONFIG_BLE_MESH_PROVISIONER if (bt_mesh_is_provisioner_en()) { - provisioner_beacon_recv(buf); + bt_mesh_provisioner_beacon_recv(buf); } #endif break; diff --git a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c index fe470c8799..e9196257e7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c @@ -1586,7 +1586,7 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } @@ -1597,14 +1597,14 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { /* Decrease provisioner pbg_count */ - provisioner_pbg_count_dec(); + bt_mesh_provisioner_pbg_count_dec(); } #endif /* Reset corresponding gattc info */ 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 3cb08f0765..343eff9e86 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c @@ -86,26 +86,28 @@ static const bt_mesh_client_op_pair_t cfg_op_pair[] = { { OP_NET_TRANSMIT_SET, OP_NET_TRANSMIT_STATUS }, }; -static osi_mutex_t cfg_client_mutex; +static osi_mutex_t cfg_client_lock; static void bt_mesh_cfg_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&cfg_client_mutex); - init = true; + if (!cfg_client_lock) { + osi_mutex_new(&cfg_client_lock); + __ASSERT(cfg_client_lock, "%s, fail", __func__); } } static void bt_mesh_cfg_client_lock(void) { - osi_mutex_lock(&cfg_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (cfg_client_lock) { + osi_mutex_lock(&cfg_client_lock, OSI_MUTEX_MAX_TIMEOUT); + } } static void bt_mesh_cfg_client_unlock(void) { - osi_mutex_unlock(&cfg_client_mutex); + if (cfg_client_lock) { + osi_mutex_unlock(&cfg_client_lock); + } } static void timeout_handler(struct k_work *work) @@ -1652,20 +1654,23 @@ int bt_mesh_cfg_cli_init(struct bt_mesh_model *model, bool primary) return -EINVAL; } - /* TODO: call osi_free() when deinit function is invoked*/ - internal = osi_calloc(sizeof(config_internal_data_t)); - if (!internal) { - BT_ERR("Allocate memory for Configuration Client internal data fail"); - return -ENOMEM; + if (!client->internal_data) { + internal = osi_calloc(sizeof(config_internal_data_t)); + if (!internal) { + BT_ERR("Allocate memory for Configuration Client internal data fail"); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(cfg_op_pair); + client->op_pair = cfg_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); } - sys_slist_init(&internal->queue); - - client->model = model; - client->op_pair_size = ARRAY_SIZE(cfg_op_pair); - client->op_pair = cfg_op_pair; - client->internal_data = internal; - cli = client; /* Configuration Model security is device-key based */ 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 b3c2b26096..1897422958 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -831,7 +831,6 @@ static void gatt_proxy_set(struct bt_mesh_model *model, bt_mesh_store_cfg(); } -#if CONFIG_BLE_MESH_NODE if (cfg->gatt_proxy == BLE_MESH_GATT_PROXY_DISABLED) { int i; @@ -855,7 +854,6 @@ static void gatt_proxy_set(struct bt_mesh_model *model, } bt_mesh_adv_update(); -#endif if (cfg->hb_pub.feat & BLE_MESH_FEAT_PROXY) { bt_mesh_heartbeat_send(); @@ -2246,10 +2244,8 @@ static void net_key_add(struct bt_mesh_model *model, if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; -#if CONFIG_BLE_MESH_NODE bt_mesh_proxy_beacon_send(sub); bt_mesh_adv_update(); -#endif } else { sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; } @@ -2506,7 +2502,6 @@ static void node_identity_set(struct bt_mesh_model *model, } else { net_buf_simple_add_u8(&msg, STATUS_SUCCESS); net_buf_simple_add_le16(&msg, idx); -#if CONFIG_BLE_MESH_NODE /* Section 4.2.11.1: "When the GATT Proxy state is set to * 0x00, the Node Identity state for all subnets shall be set * to 0x00 and shall not be changed." @@ -2520,7 +2515,6 @@ static void node_identity_set(struct bt_mesh_model *model, } bt_mesh_adv_update(); } -#endif net_buf_simple_add_u8(&msg, sub->node_id); } 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 66c15d44ff..b34a0c47f3 100644 --- a/components/bt/esp_ble_mesh/mesh_core/health_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/health_cli.c @@ -39,26 +39,28 @@ static const bt_mesh_client_op_pair_t health_op_pair[] = { { OP_ATTENTION_SET, OP_ATTENTION_STATUS }, }; -static osi_mutex_t health_client_mutex; +static osi_mutex_t health_client_lock; static void bt_mesh_health_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&health_client_mutex); - init = true; + if (!health_client_lock) { + osi_mutex_new(&health_client_lock); + __ASSERT(health_client_lock, "%s, fail", __func__); } } static void bt_mesh_health_client_lock(void) { - osi_mutex_lock(&health_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (health_client_lock) { + osi_mutex_lock(&health_client_lock, OSI_MUTEX_MAX_TIMEOUT); + } } static void bt_mesh_health_client_unlock(void) { - osi_mutex_unlock(&health_client_mutex); + if (health_client_lock) { + osi_mutex_unlock(&health_client_lock); + } } static void timeout_handler(struct k_work *work) @@ -454,20 +456,23 @@ int bt_mesh_health_cli_init(struct bt_mesh_model *model, bool primary) return -EINVAL; } - /* TODO: call osi_free() when deinit function is invoked*/ - internal = osi_calloc(sizeof(health_internal_data_t)); - if (!internal) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; + if (!client->internal_data) { + internal = osi_calloc(sizeof(health_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(health_op_pair); + client->op_pair = health_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); } - sys_slist_init(&internal->queue); - - client->model = model; - client->op_pair_size = ARRAY_SIZE(health_op_pair); - client->op_pair = health_op_pair; - client->internal_data = internal; - bt_mesh_health_client_mutex_new(); /* Set the default health client pointer */ diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index 400d3f4aaf..b523007911 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -41,8 +41,6 @@ static volatile bool provisioner_en = false; #define ACTION_SUSPEND 0x02 #define ACTION_EXIT 0x03 -#if CONFIG_BLE_MESH_NODE - int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, u8_t flags, u32_t iv_index, u16_t addr, const u8_t dev_key[16]) @@ -207,8 +205,6 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers) return 0; } -#endif /* CONFIG_BLE_MESH_NODE */ - static void model_suspend(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, void *user_data) { @@ -372,13 +368,19 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) int err; if (bt_mesh_is_provisioner_en()) { - BT_WARN("%s, Provisioner is already enabled", __func__); + BT_WARN("%s, Already", __func__); return -EALREADY; } + err = bt_mesh_provisioner_set_prov_info(); + if (err) { + BT_ERR("%s, Failed to set provisioning info", __func__); + return err; + } + err = bt_mesh_provisioner_net_create(); if (err) { - BT_ERR("%s, Provisioner failed to create network", __func__); + BT_ERR("%s, Failed to create network", __func__); return err; } @@ -405,7 +407,7 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && (bearers & BLE_MESH_PROV_GATT)) { - provisioner_pb_gatt_enable(); + bt_mesh_provisioner_pb_gatt_enable(); } if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { @@ -430,7 +432,7 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && (bearers & BLE_MESH_PROV_GATT)) { - provisioner_pb_gatt_disable(); + bt_mesh_provisioner_pb_gatt_disable(); } if ((IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && @@ -484,13 +486,13 @@ u8_t bt_mesh_set_fast_prov_action(u8_t action) bt_mesh_beacon_disable(); } if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { - provisioner_pb_gatt_enable(); + bt_mesh_provisioner_pb_gatt_enable(); } - provisioner_set_fast_prov_flag(true); + bt_mesh_provisioner_set_fast_prov_flag(true); provisioner_en = true; } else { if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { - provisioner_pb_gatt_disable(); + bt_mesh_provisioner_pb_gatt_disable(); } if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { bt_mesh_beacon_enable(); @@ -503,7 +505,7 @@ u8_t bt_mesh_set_fast_prov_action(u8_t action) bt_mesh_adv_update(); } #endif - provisioner_set_fast_prov_flag(false); + bt_mesh_provisioner_set_fast_prov_flag(false); provisioner_en = false; if (action == ACTION_EXIT) { bt_mesh_provisioner_remove_node(NULL); diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 0e60e95aa4..2e55f306a8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -630,9 +630,7 @@ void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub) if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) { -#if CONFIG_BLE_MESH_NODE bt_mesh_proxy_beacon_send(sub); -#endif } } @@ -912,23 +910,19 @@ int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, * "The output filter of the interface connected to advertising or * GATT bearers shall drop all messages with TTL value set to 1." */ -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - tx->ctx->send_ttl != 1U) { - if (bt_mesh_proxy_relay(&buf->b, tx->ctx->addr) && - BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { - /* Notify completion if this only went - * through the Mesh Proxy. - */ - send_cb_finalize(cb, cb_data); + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + tx->ctx->send_ttl != 1U) { + if (bt_mesh_proxy_relay(&buf->b, tx->ctx->addr) && + BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + /* Notify completion if this only went + * through the Mesh Proxy. + */ + send_cb_finalize(cb, cb_data); - err = 0; - goto done; - } + err = 0; + goto done; } } -#endif #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT if (tx->ctx->send_ttl != 1U) { @@ -1055,15 +1049,11 @@ static int net_decrypt(struct bt_mesh_subnet *sub, const u8_t *enc, BT_DBG("src 0x%04x", rx->ctx.addr); -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) && - rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) { - return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), - true); - } + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) && + rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) { + return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), + true); } -#endif return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), false); } @@ -1169,8 +1159,6 @@ static bool net_find_and_decrypt(const u8_t *data, size_t data_len, * get sent to the advertising bearer. If the packet came in through GATT, * then we should only relay it if the GATT Proxy state is enabled. */ -#if CONFIG_BLE_MESH_NODE - static bool relay_to_adv(enum bt_mesh_net_if net_if) { switch (net_if) { @@ -1310,8 +1298,6 @@ done: net_buf_unref(buf); } -#endif /* CONFIG_BLE_MESH_NODE */ - void bt_mesh_net_header_parse(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) { @@ -1429,14 +1415,10 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, /* Save the state so the buffer can later be relayed */ net_buf_simple_save(&buf, &state); -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - net_if == BLE_MESH_NET_IF_PROXY) { - bt_mesh_proxy_addr_add(data, rx.ctx.addr); - } + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + net_if == BLE_MESH_NET_IF_PROXY) { + bt_mesh_proxy_addr_add(data, rx.ctx.addr); } -#endif rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) || bt_mesh_elem_find(rx.ctx.recv_dst)); @@ -1458,15 +1440,12 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, /* Relay if this was a group/virtual address, or if the destination * was neither a local element nor an LPN we're Friends for. */ -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) || - (!rx.local_match && !rx.friend_match)) { - net_buf_simple_restore(&buf, &state); - bt_mesh_net_relay(&buf, &rx); - } + if (IS_ENABLED(CONFIG_BLE_MESH_RELAY) && + (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) || + (!rx.local_match && !rx.friend_match))) { + net_buf_simple_restore(&buf, &state); + bt_mesh_net_relay(&buf, &rx); } -#endif } static void ivu_refresh(struct k_work *work) @@ -1497,7 +1476,6 @@ static void ivu_refresh(struct k_work *work) } } -#if defined(CONFIG_BLE_MESH_NODE) void bt_mesh_net_start(void) { if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { @@ -1542,7 +1520,6 @@ void bt_mesh_net_start(void) bt_mesh_prov_complete(net_idx, net_key, addr, flags, iv_index); } } -#endif void bt_mesh_net_init(void) { diff --git a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c index 767c2bacea..3ad22920a6 100644 --- a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c @@ -453,7 +453,7 @@ static int disc_cb(struct ble_gap_event *event, void *arg) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } @@ -464,14 +464,14 @@ static int disc_cb(struct ble_gap_event *event, void *arg) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { /* Decrease prov pbg_count */ - provisioner_pbg_count_dec(); + bt_mesh_provisioner_pbg_count_dec(); } #endif /* Reset corresponding gattc info */ diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index 47828943c4..d64359aeaf 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -210,25 +210,31 @@ static struct prov_link link; static const struct bt_mesh_prov *prov; -static osi_mutex_t prov_buf_mutex; +#if defined(CONFIG_BLE_MESH_PB_ADV) +static osi_mutex_t pb_buf_lock; -static void bt_mesh_prov_buf_mutex_new(void) +static void bt_mesh_pb_buf_mutex_new(void) { - if (!prov_buf_mutex) { - osi_mutex_new(&prov_buf_mutex); - __ASSERT(prov_buf_mutex, "%s, fail", __func__); + if (!pb_buf_lock) { + osi_mutex_new(&pb_buf_lock); + __ASSERT(pb_buf_lock, "%s, fail", __func__); } } -static void bt_mesh_prov_buf_lock(void) +static void bt_mesh_pb_buf_lock(void) { - osi_mutex_lock(&prov_buf_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (pb_buf_lock) { + osi_mutex_lock(&pb_buf_lock, OSI_MUTEX_MAX_TIMEOUT); + } } -static void bt_mesh_prov_buf_unlock(void) +static void bt_mesh_pb_buf_unlock(void) { - osi_mutex_unlock(&prov_buf_mutex); + if (pb_buf_lock) { + osi_mutex_unlock(&pb_buf_lock); + } } +#endif /* CONFIG_BLE_MESH_PB_ADV */ static void reset_state(void) { @@ -283,7 +289,7 @@ static void free_segments(void) { int i; - bt_mesh_prov_buf_lock(); + bt_mesh_pb_buf_lock(); for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { struct net_buf *buf = link.tx.buf[i]; @@ -299,7 +305,7 @@ static void free_segments(void) net_buf_unref(buf); } - bt_mesh_prov_buf_unlock(); + bt_mesh_pb_buf_unlock(); } static void prov_clear_tx(void) @@ -1304,7 +1310,7 @@ static void prov_retransmit(struct k_work *work) return; } - bt_mesh_prov_buf_lock(); + bt_mesh_pb_buf_lock(); for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { struct net_buf *buf = link.tx.buf[i]; @@ -1327,7 +1333,7 @@ static void prov_retransmit(struct k_work *work) } - bt_mesh_prov_buf_unlock(); + bt_mesh_pb_buf_unlock(); } static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) @@ -1767,7 +1773,9 @@ int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info) reset_state(); - bt_mesh_prov_buf_mutex_new(); +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_buf_mutex_new(); +#endif return 0; } diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c index 0790b538de..df6ac3c740 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c @@ -42,7 +42,7 @@ static void provisioner_secure_beacon_recv(struct net_buf_simple *buf) // TODO: Provisioner receive and handle Secure Network Beacon } -void provisioner_beacon_recv(struct net_buf_simple *buf) +void bt_mesh_provisioner_beacon_recv(struct net_buf_simple *buf) { u8_t type; @@ -57,7 +57,7 @@ void provisioner_beacon_recv(struct net_buf_simple *buf) switch (type) { case BEACON_TYPE_UNPROVISIONED: BT_DBG("Unprovisioned device beacon received"); - provisioner_unprov_beacon_recv(buf); + bt_mesh_provisioner_unprov_beacon_recv(buf); break; case BEACON_TYPE_SECURE: provisioner_secure_beacon_recv(buf); diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h index a58de225fe..8daa49f898 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h @@ -15,6 +15,6 @@ #ifndef _PROVISIONER_BEACON_H_ #define _PROVISIONER_BEACON_H_ -void provisioner_beacon_recv(struct net_buf_simple *buf); +void bt_mesh_provisioner_beacon_recv(struct net_buf_simple *buf); #endif /* _PROVISIONER_BEACON_H_ */ \ No newline at end of file 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 a5c0286d7f..a23f9045d9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -39,26 +39,30 @@ #if CONFIG_BLE_MESH_PROVISIONER static struct bt_mesh_node *mesh_nodes[CONFIG_BLE_MESH_MAX_STORED_NODES]; -static osi_mutex_t provisioner_mutex; +static osi_mutex_t provisioner_lock; static u16_t all_node_count; static u16_t prov_node_count; -void bt_mesh_provisioner_mutex_new(void) +static void bt_mesh_provisioner_mutex_new(void) { - if (!provisioner_mutex) { - osi_mutex_new(&provisioner_mutex); - __ASSERT(provisioner_mutex, "%s, fail", __func__); + if (!provisioner_lock) { + osi_mutex_new(&provisioner_lock); + __ASSERT(provisioner_lock, "%s, fail", __func__); } } static void bt_mesh_provisioner_lock(void) { - osi_mutex_lock(&provisioner_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (provisioner_lock) { + osi_mutex_lock(&provisioner_lock, OSI_MUTEX_MAX_TIMEOUT); + } } static void bt_mesh_provisioner_unlock(void) { - osi_mutex_unlock(&provisioner_mutex); + if (provisioner_lock) { + osi_mutex_unlock(&provisioner_lock); + } } static int provisioner_check_node_index(u16_t index) @@ -108,10 +112,9 @@ u16_t bt_mesh_provisioner_get_all_node_count(void) return all_node_count; } -static int provisioner_store_node(struct bt_mesh_node *node, bool prov, u16_t *index) +static int provisioner_store_node(struct bt_mesh_node *node, bool prov, bool store, u16_t *index) { u16_t min, max; - int err = 0; size_t i; bt_mesh_provisioner_lock(); @@ -120,6 +123,7 @@ static int provisioner_store_node(struct bt_mesh_node *node, bool prov, u16_t *i for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, node->dev_uuid, 16)) { BT_WARN("Node already exists, uuid %s", bt_hex(node->dev_uuid, 16)); + bt_mesh_provisioner_unlock(); return -EEXIST; } } @@ -141,8 +145,8 @@ static int provisioner_store_node(struct bt_mesh_node *node, bool prov, u16_t *i mesh_nodes[i] = osi_calloc(sizeof(struct bt_mesh_node)); if (!mesh_nodes[i]) { BT_ERR("%s, Failed to allocate memory", __func__); - err = -ENOMEM; - goto unlock; + bt_mesh_provisioner_unlock(); + return -ENOMEM; } memcpy(mesh_nodes[i], node, sizeof(struct bt_mesh_node)); @@ -151,19 +155,18 @@ static int provisioner_store_node(struct bt_mesh_node *node, bool prov, u16_t *i *index = i; } - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { bt_mesh_store_node_info(mesh_nodes[i], prov); } - goto unlock; + + bt_mesh_provisioner_unlock(); + return 0; } } BT_ERR("%s, Node queue is full", __func__); - err = -ENOMEM; - -unlock: bt_mesh_provisioner_unlock(); - return err; + return -ENOMEM; } int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, @@ -194,7 +197,7 @@ int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16 node.iv_index = iv_index; memcpy(node.dev_key, dev_key, 16); - return provisioner_store_node(&node, true, index); + return provisioner_store_node(&node, true, true, index); } static int provisioner_remove_node(u16_t index) @@ -349,16 +352,10 @@ int bt_mesh_provisioner_net_create(void) { const struct bt_mesh_prov *prov = NULL; struct bt_mesh_subnet *sub = NULL; - static bool prov_net_create; u8_t p_key[16] = {0}; - size_t i; BT_DBG("%s", __func__); - if (prov_net_create) { - return 0; - } - prov = bt_mesh_provisioner_get_prov_info(); if (!prov) { BT_ERR("%s, NULL provisioning context", __func__); @@ -370,11 +367,10 @@ int bt_mesh_provisioner_net_create(void) */ bt_mesh_comp_provision(prov->prov_unicast_addr); - for (i = 0U; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { - if (bt_mesh.p_sub[i]) { - BT_DBG("Keys of Provisioner restored from flash"); - goto end; - } + if (bt_mesh.p_sub[0]) { + BT_DBG("Keys of Provisioner restored from flash"); + sub = bt_mesh.p_sub[0]; + goto done; } /* Generate the primary netkey */ @@ -436,9 +432,7 @@ int bt_mesh_provisioner_net_create(void) bt_mesh_store_iv(true); } -end: - prov_net_create = true; - +done: BT_DBG("net_idx 0x%03x, netkey %s, nid 0x%02x", sub->net_idx, bt_hex(sub->keys[0].net, 16), sub->keys[0].nid); @@ -559,14 +553,14 @@ struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx) return NULL; } -int bt_mesh_provisioner_load_node_info(struct bt_mesh_node *node, bool prov) +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov) { if (!node) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } - return provisioner_store_node(node, prov, NULL); + return provisioner_store_node(node, prov, false, NULL); } int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) @@ -576,7 +570,7 @@ int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) return -EINVAL; } - return provisioner_store_node(node, false, NULL); + return provisioner_store_node(node, false, true, NULL); } struct bt_mesh_node *bt_mesh_provisioner_get_node_info(u16_t unicast_addr) @@ -1351,12 +1345,12 @@ u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) sub = bt_mesh_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); + return bt_mesh_provisioner_set_fast_prov_net_idx(key->net, net_idx); } /* If net_idx is not found, set net_idx to fast_prov first, * and wait for primary provisioner to add net_key */ - return provisioner_set_fast_prov_net_idx(NULL, net_idx); + return bt_mesh_provisioner_set_fast_prov_net_idx(NULL, net_idx); } u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) @@ -1365,7 +1359,7 @@ u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) u16_t net_idx; int err; - net_idx = provisioner_get_fast_prov_net_idx(); + net_idx = bt_mesh_provisioner_get_fast_prov_net_idx(); bt_mesh.p_net_idx_next = net_idx; err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx); @@ -1378,7 +1372,7 @@ u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) return 0x01; /* status: add net_key fail */ } - return provisioner_set_fast_prov_net_idx(keys, net_idx); + return bt_mesh_provisioner_set_fast_prov_net_idx(keys, net_idx); } const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) 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 e3e0e827d5..90306785cf 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -38,8 +38,6 @@ struct bt_mesh_node { /* The following APIs are for key init, node provision & node reset. */ -void bt_mesh_provisioner_mutex_new(void); - u16_t bt_mesh_provisioner_get_prov_node_count(void); u16_t bt_mesh_provisioner_get_all_node_count(void); @@ -72,7 +70,7 @@ const u8_t *bt_mesh_provisioner_dev_key_get(u16_t dst); struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx); -int bt_mesh_provisioner_load_node_info(struct bt_mesh_node *node, bool prov); +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov); /* The following APIs are for provisioner application use. */ 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 06a5ab7ffb..fb9bbca826 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -245,6 +245,12 @@ struct prov_ctx_t { /* Current iv_index going to be used in provisioning data */ u16_t curr_iv_index; + /* Length of Static OOB value */ + u8_t static_oob_len; + + /* Static OOB value */ + u8_t *static_oob_val; + /* Offset of the device uuid to be matched, based on zero */ u8_t match_offset; @@ -257,11 +263,18 @@ struct prov_ctx_t { /* Indicate when received uuid_match adv_pkts, can provision it at once */ bool prov_after_match; +#if defined(CONFIG_BLE_MESH_PB_ADV) /* Mutex used to protect the PB-ADV procedure */ osi_mutex_t pb_adv_lock; + /* Mutex used to protect the adv buf during PB-ADV procedure */ + osi_mutex_t pb_buf_lock; +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) /* Mutex used to protect the PB-GATT procedure */ osi_mutex_t pb_gatt_lock; +#endif }; static struct prov_ctx_t prov_ctx; @@ -333,6 +346,7 @@ static u8_t adv_buf_data[ADV_BUF_SIZE * CONFIG_BLE_MESH_PBA_SAME_TIME]; { \ if (link[_idx].member) { \ osi_free(link[_idx].member); \ + link[_idx].member = NULL; \ } \ } @@ -350,27 +364,77 @@ static bool fast_prov_flag; #define FAST_PROV_FLAG_GET() fast_prov_flag -static osi_mutex_t prov_buf_mutex; - -static void bt_mesh_prov_buf_mutex_new(void) +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void bt_mesh_pb_adv_mutex_new(void) { - if (!prov_buf_mutex) { - osi_mutex_new(&prov_buf_mutex); - __ASSERT(prov_buf_mutex, "%s, fail", __func__); + if (!prov_ctx.pb_adv_lock) { + osi_mutex_new(&prov_ctx.pb_adv_lock); + __ASSERT(prov_ctx.pb_adv_lock, "%s, fail", __func__); } } -static void bt_mesh_prov_buf_lock(void) +static void bt_mesh_pb_adv_lock(void) { - osi_mutex_lock(&prov_buf_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (prov_ctx.pb_adv_lock) { + osi_mutex_lock(&prov_ctx.pb_adv_lock, OSI_MUTEX_MAX_TIMEOUT); + } } -static void bt_mesh_prov_buf_unlock(void) +static void bt_mesh_pb_adv_unlock(void) { - osi_mutex_unlock(&prov_buf_mutex); + if (prov_ctx.pb_adv_lock) { + osi_mutex_unlock(&prov_ctx.pb_adv_lock); + } } -void provisioner_pbg_count_dec(void) +static void bt_mesh_pb_buf_mutex_new(void) +{ + if (!prov_ctx.pb_buf_lock) { + osi_mutex_new(&prov_ctx.pb_buf_lock); + __ASSERT(prov_ctx.pb_buf_lock, "%s, fail", __func__); + } +} + +static void bt_mesh_pb_buf_lock(void) +{ + if (prov_ctx.pb_buf_lock) { + osi_mutex_lock(&prov_ctx.pb_buf_lock, OSI_MUTEX_MAX_TIMEOUT); + } +} + +static void bt_mesh_pb_buf_unlock(void) +{ + if (prov_ctx.pb_buf_lock) { + osi_mutex_unlock(&prov_ctx.pb_buf_lock); + } +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static void bt_mesh_pb_gatt_mutex_new(void) +{ + if (!prov_ctx.pb_gatt_lock) { + osi_mutex_new(&prov_ctx.pb_gatt_lock); + __ASSERT(prov_ctx.pb_gatt_lock, "%s, fail", __func__); + } +} + +static void bt_mesh_pb_gatt_lock(void) +{ + if (prov_ctx.pb_gatt_lock) { + osi_mutex_lock(&prov_ctx.pb_gatt_lock, OSI_MUTEX_MAX_TIMEOUT); + } +} + +static void bt_mesh_pb_gatt_unlock(void) +{ + if (prov_ctx.pb_gatt_lock) { + osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + } +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +void bt_mesh_provisioner_pbg_count_dec(void) { if (prov_ctx.pbg_count) { prov_ctx.pbg_count--; @@ -383,7 +447,7 @@ static inline void provisioner_pbg_count_inc(void) } #if defined(CONFIG_BLE_MESH_PB_GATT) -void provisioner_clear_link_conn_info(const u8_t addr[6]) +void bt_mesh_provisioner_clear_link_info(const u8_t addr[6]) { u8_t i; @@ -589,10 +653,10 @@ static int provisioner_start_prov_pb_adv(const u8_t uuid[16], return -EINVAL; } - osi_mutex_lock(&prov_ctx.pb_adv_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_pb_adv_lock(); if (is_unprov_dev_being_provision(uuid)) { - osi_mutex_unlock(&prov_ctx.pb_adv_lock); + bt_mesh_pb_adv_unlock(); return -EALREADY; } @@ -607,13 +671,14 @@ static int provisioner_start_prov_pb_adv(const u8_t uuid[16], memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); } send_link_open(i); - osi_mutex_unlock(&prov_ctx.pb_adv_lock); + bt_mesh_pb_adv_unlock(); return 0; } } BT_ERR("%s, No PB-ADV link is available", __func__); - osi_mutex_unlock(&prov_ctx.pb_adv_lock); + bt_mesh_pb_adv_unlock(); + return -ENOMEM; } #endif /* CONFIG_BLE_MESH_PB_ADV */ @@ -631,10 +696,10 @@ static int provisioner_start_prov_pb_gatt(const u8_t uuid[16], return -EINVAL; } - osi_mutex_lock(&prov_ctx.pb_gatt_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_pb_gatt_lock(); if (is_unprov_dev_being_provision(uuid)) { - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); return -EALREADY; } @@ -652,19 +717,20 @@ static int provisioner_start_prov_pb_gatt(const u8_t uuid[16], memset(link[i].uuid, 0, 16); link[i].oob_info = 0x0; memset(&link[i].addr, 0, sizeof(bt_mesh_addr_t)); - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); return -EIO; } /* If creating connection successfully, set connecting flag to 1 */ link[i].connecting = true; provisioner_pbg_count_inc(); - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); return 0; } } BT_ERR("%s, No PB-GATT link is available", __func__); - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); + return -ENOMEM; } #endif /* CONFIG_BLE_MESH_PB_GATT */ @@ -923,7 +989,7 @@ int bt_mesh_provisioner_set_dev_uuid_match(u8_t offset, u8_t length, return 0; } -int bt_mesh_prov_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb) +int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb) { if (!cb) { BT_ERR("%s, Invalid parameter", __func__); @@ -958,14 +1024,76 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info) return 0; } +int bt_mesh_provisioner_set_prov_info(void) +{ + const struct bt_mesh_comp *comp = NULL; + + if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) || + !BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) { + BT_ERR("%s, Invalid address, own 0x%04x, start 0x%04x", + __func__, prov->prov_unicast_addr, prov->prov_start_address); + return -EINVAL; + } + + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("%s, NULL composition data", __func__); + return -EINVAL; + } + + if (prov->prov_unicast_addr + comp->elem_count > prov->prov_start_address) { + BT_WARN("Too small start address 0x%04x, update to 0x%04x", + prov->prov_start_address, prov->prov_unicast_addr + comp->elem_count); + prov_ctx.current_addr = prov->prov_unicast_addr + comp->elem_count; + } else { + prov_ctx.current_addr = prov->prov_start_address; + } + prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY; + prov_ctx.curr_flags = prov->flags; + prov_ctx.curr_iv_index = prov->iv_index; + + return 0; +} + +int bt_mesh_provisioner_set_static_oob_value(const u8_t *value, u8_t length) +{ + size_t i; + + if (value == NULL || length == 0U || length > 16U) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + /* Make sure Static OOB is not being used. */ + for (i = 0U; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (link[i].auth_method == AUTH_METHOD_STATIC) { + BT_ERR("%s, Static OOB is being used", __func__); + return -EINVAL; + } + } + + if (prov_ctx.static_oob_val == NULL) { + prov_ctx.static_oob_val = osi_calloc(16); + if (!prov_ctx.static_oob_val) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + } + + prov_ctx.static_oob_len = MIN(16, length); + memcpy(prov_ctx.static_oob_val, value, prov_ctx.static_oob_len); + + return 0; +} + /* The following APIs are for fast provisioning */ -void provisioner_set_fast_prov_flag(bool flag) +void bt_mesh_provisioner_set_fast_prov_flag(bool flag) { fast_prov_flag = flag; } -u8_t provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx) +u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx) { fast_prov_info.net_idx = net_idx; fast_prov_info.net_key = net_key; @@ -978,7 +1106,7 @@ u8_t provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx) return 0x0; /* status: success */ } -u16_t provisioner_get_fast_prov_net_idx(void) +u16_t bt_mesh_provisioner_get_fast_prov_net_idx(void) { return fast_prov_info.net_idx; } @@ -1066,7 +1194,7 @@ static void free_segments(const u8_t idx) { u8_t i; - bt_mesh_prov_buf_lock(); + bt_mesh_pb_buf_lock(); for (i = 0U; i < ARRAY_SIZE(link[idx].tx.buf); i++) { struct net_buf *buf = link[idx].tx.buf[i]; @@ -1082,7 +1210,7 @@ static void free_segments(const u8_t idx) net_buf_unref(buf); } - bt_mesh_prov_buf_unlock(); + bt_mesh_pb_buf_unlock(); } static void prov_clear_tx(const u8_t idx) @@ -1520,7 +1648,7 @@ static void prov_capabilities(const u8_t idx, const u8_t *data) BT_ERR("%s, Invalid Static OOB type", __func__); goto fail; } - static_oob = (prov->prov_static_oob_val ? static_oob : 0x00); + static_oob = (prov_ctx.static_oob_val ? static_oob : 0x00); output_size = data[5]; BT_DBG("Output OOB Size: %u", output_size); @@ -1700,8 +1828,8 @@ static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) if (action || size) { return -EINVAL; } - memcpy(link[idx].auth + 16 - prov->prov_static_oob_len, - prov->prov_static_oob_val, prov->prov_static_oob_len); + memcpy(link[idx].auth + 16 - prov_ctx.static_oob_len, + prov_ctx.static_oob_val, prov_ctx.static_oob_len); memset(link[idx].auth, 0, 16 - prov->prov_static_oob_len); return 0; @@ -1834,7 +1962,7 @@ fail: return; } -int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag) +int bt_mesh_provisioner_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag) { /** This function should be called in the prov_input_num * callback, after the data output by device has been @@ -1864,7 +1992,7 @@ int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_fl return 0; } -int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag) +int bt_mesh_provisioner_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. @@ -1895,7 +2023,7 @@ int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, return 0; } -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]) +int bt_mesh_provisioner_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]) { if (!link[idx].conf_inputs) { BT_ERR("%s, Link conf_inputs is NULL", __func__); @@ -2456,7 +2584,7 @@ static void prov_retransmit(struct k_work *work) link[idx].send_link_close += BIT(3); } - bt_mesh_prov_buf_lock(); + bt_mesh_pb_buf_lock(); for (i = 0U; i < ARRAY_SIZE(link[idx].tx.buf); i++) { struct net_buf *buf = link[idx].tx.buf[i]; @@ -2478,7 +2606,7 @@ static void prov_retransmit(struct k_work *work) } } - bt_mesh_prov_buf_unlock(); + bt_mesh_pb_buf_unlock(); } static void link_ack(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) @@ -2773,7 +2901,7 @@ static int find_link(u32_t link_id, u8_t *idx) return -1; } -void provisioner_pb_adv_recv(struct net_buf_simple *buf) +void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf) { struct prov_rx rx = {0}; u8_t idx; @@ -2819,7 +2947,7 @@ static struct bt_mesh_conn *find_conn(struct bt_mesh_conn *conn, u8_t *idx) return NULL; } -int provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) +int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) { u8_t type; u8_t idx; @@ -2862,7 +2990,7 @@ fail: return -EINVAL; } -int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn) +int bt_mesh_provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn) { u8_t i; @@ -2882,7 +3010,7 @@ int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn) return -ENOMEM; } -int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) { u8_t idx = 0, i; @@ -2938,7 +3066,7 @@ int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) return 0; } -int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason) +int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason) { u8_t idx; @@ -2971,9 +3099,8 @@ int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason) int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info) { - const struct bt_mesh_comp *comp = NULL; const u8_t *key = NULL; - u8_t i; + size_t i; if (!prov_info) { BT_ERR("%s, No provisioning context provided", __func__); @@ -2988,6 +3115,16 @@ int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info) prov = prov_info; + if (prov->prov_static_oob_val && prov->prov_static_oob_len) { + prov_ctx.static_oob_val = osi_calloc(16); + if (!prov_ctx.static_oob_val) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + prov_ctx.static_oob_len = MIN(16, prov->prov_static_oob_len); + memcpy(prov_ctx.static_oob_val, prov->prov_static_oob_val, prov_ctx.static_oob_len); + } + #if defined(CONFIG_BLE_MESH_PB_ADV) for (i = 0U; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { struct prov_adv_buf *adv = &adv_buf[i]; @@ -3007,34 +3144,13 @@ int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info) link[i].timeout.work.index = (int)i; } - if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) || - !BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) { - BT_ERR("%s, Invalid address, own 0x%04x, start 0x%04x", - __func__, prov->prov_unicast_addr, prov->prov_start_address); - return -EINVAL; - } - - comp = bt_mesh_comp_get(); - if (!comp) { - BT_ERR("%s, NULL composition data", __func__); - return -EINVAL; - } - - if (prov->prov_unicast_addr + comp->elem_count > prov->prov_start_address) { - BT_WARN("Too small start address 0x%04x, update to 0x%04x", - prov->prov_start_address, prov->prov_unicast_addr + comp->elem_count); - prov_ctx.current_addr = prov->prov_unicast_addr + comp->elem_count; - } else { - prov_ctx.current_addr = prov->prov_start_address; - } - prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY; - prov_ctx.curr_flags = prov->flags; - prov_ctx.curr_iv_index = prov->iv_index; - - osi_mutex_new(&prov_ctx.pb_adv_lock); - osi_mutex_new(&prov_ctx.pb_gatt_lock); - - bt_mesh_prov_buf_mutex_new(); +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_adv_mutex_new(); + bt_mesh_pb_buf_mutex_new(); +#endif +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_pb_gatt_mutex_new(); +#endif return 0; } @@ -3069,7 +3185,7 @@ static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, return false; } -void provisioner_unprov_beacon_recv(struct net_buf_simple *buf) +void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf) { #if defined(CONFIG_BLE_MESH_PB_ADV) const bt_mesh_addr_t *addr = NULL; @@ -3105,7 +3221,7 @@ void provisioner_unprov_beacon_recv(struct net_buf_simple *buf) #endif /* CONFIG_BLE_MESH_PB_ADV */ } -void provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr) +void bt_mesh_provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr) { #if defined(CONFIG_BLE_MESH_PB_GATT) const u8_t *uuid = NULL; 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 76fdb82edf..724bb38094 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h @@ -73,7 +73,7 @@ struct bt_mesh_prov_data_info { * * @return None */ -void provisioner_pbg_count_dec(void); +void bt_mesh_provisioner_pbg_count_dec(void); /** * @brief This function clears the part of the link info of the proper device. @@ -82,7 +82,7 @@ void provisioner_pbg_count_dec(void); * * @return None */ -void provisioner_clear_link_conn_info(const u8_t addr[6]); +void bt_mesh_provisioner_clear_link_info(const u8_t addr[6]); /** * @brief This function handles the received PB-ADV PDUs. @@ -91,7 +91,7 @@ void provisioner_clear_link_conn_info(const u8_t addr[6]); * * @return Zero - success, otherwise - fail */ -void provisioner_pb_adv_recv(struct net_buf_simple *buf); +void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf); /** * @brief This function sends provisioning invite to start @@ -102,7 +102,7 @@ void provisioner_pb_adv_recv(struct net_buf_simple *buf); * * @return Zero - success, otherwise - fail */ -int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn); +int bt_mesh_provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn); /** * @brief This function sends provisioning invite to start @@ -113,7 +113,7 @@ int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn); * * @return Zero - success, otherwise - fail */ -int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr); +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr); /** * @brief This function resets the used information when @@ -124,7 +124,7 @@ int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr); * * @return Zero - success, otherwise - fail */ -int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason); +int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason); /** * @brief This function handles the received PB-GATT provision @@ -135,7 +135,7 @@ int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason); * * @return Zero - success, otherwise - fail */ -int provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf); +int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf); /** * @brief This function initializes provisioner's PB-GATT and PB-ADV @@ -156,9 +156,9 @@ int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info); * * @return None */ -void provisioner_unprov_beacon_recv(struct net_buf_simple *buf); +void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf); -void provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr); +void bt_mesh_provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr); /** * @brief This function gets the bt_mesh_prov pointer. @@ -239,7 +239,7 @@ typedef void (*unprov_adv_pkt_cb_t)(const u8_t addr[6], const u8_t addr_type, * * @return Zero - success, otherwise - fail */ -int bt_mesh_prov_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); +int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); /** * @brief This function changes net_idx or flags or iv_index used in provisioning data. @@ -250,6 +250,24 @@ int bt_mesh_prov_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); */ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info); +/** + * @brief This function sets the provisioning information needed by Provisioner, + * including unicast address, IV Index, etc. + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_prov_info(void); + +/** + * @brief This function sets the Static OOB value used by Provisioner. + * + * @param[in] value: Static OOB value + * @param[in] length: Static OOB value length + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_static_oob_value(const u8_t *value, u8_t length); + /** * @brief This function is called to input number/string out-put by unprovisioned device. * @@ -259,7 +277,7 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info); * * @return Zero - success, otherwise - fail */ -int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag); +int bt_mesh_provisioner_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag); /** * @brief This function is called to output number/string which will be input by unprovisioned device. @@ -271,7 +289,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 */ -int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag); +int bt_mesh_provisioner_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. @@ -282,7 +300,7 @@ int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, * * @return Zero - success, otherwise - fail */ -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]); +int bt_mesh_provisioner_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]); /* The following APIs are for fast provisioning */ @@ -293,7 +311,7 @@ int bt_mesh_prov_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], cons * * @return None */ -void provisioner_set_fast_prov_flag(bool flag); +void bt_mesh_provisioner_set_fast_prov_flag(bool flag); /** * @brief This function is called to set netkey index used for fast provisioning. @@ -303,14 +321,14 @@ void provisioner_set_fast_prov_flag(bool flag); * * @return status for set netkey index msg */ -u8_t provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx); +u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx); /** * @brief This function is called to get netkey index used for fast provisioning. * * @return net_idx of fast provisioning */ -u16_t provisioner_get_fast_prov_net_idx(void); +u16_t bt_mesh_provisioner_get_fast_prov_net_idx(void); /** * @brief This function is called to set unicast address range used for fast provisioning. diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_client.c b/components/bt/esp_ble_mesh/mesh_core/proxy_client.c index 6d3e5eff95..76c55e2edb 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy_client.c +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_client.c @@ -207,7 +207,7 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_server *server) BT_DBG("Mesh Beacon PDU"); if (bt_mesh_is_provisioner_en()) { #if CONFIG_BLE_MESH_PROVISIONER - provisioner_beacon_recv(&server->buf); + bt_mesh_provisioner_beacon_recv(&server->buf); #endif } else { #if CONFIG_BLE_MESH_NODE @@ -223,7 +223,7 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_server *server) #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT case BLE_MESH_PROXY_PROV: BT_DBG("Mesh Provisioning PDU"); - provisioner_pb_gatt_recv(server->conn, &server->buf); + bt_mesh_provisioner_pb_gatt_recv(server->conn, &server->buf); break; #endif default: @@ -438,7 +438,7 @@ static void proxy_disconnected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (server->conn_type == PROV) { - provisioner_pb_gatt_close(conn, reason); + bt_mesh_provisioner_pb_gatt_close(conn, reason); } #endif @@ -473,12 +473,12 @@ static ssize_t prov_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) if (server->conn_type == NONE) { server->conn_type = PROV; - if (provisioner_set_prov_conn(addr->val, server->conn)) { - BT_ERR("%s, provisioner_set_prov_conn failed", __func__); + if (bt_mesh_provisioner_set_prov_conn(addr->val, server->conn)) { + BT_ERR("%s, bt_mesh_provisioner_set_prov_conn failed", __func__); bt_mesh_gattc_disconnect(server->conn); return -EIO; } - return provisioner_pb_gatt_open(conn, addr->val); + return bt_mesh_provisioner_pb_gatt_open(conn, addr->val); } return -ENOMEM; @@ -500,7 +500,7 @@ static ssize_t prov_recv_ntf(struct bt_mesh_conn *conn, u8_t *data, u16_t len) return -EINVAL; } -int provisioner_pb_gatt_enable(void) +int bt_mesh_provisioner_pb_gatt_enable(void) { u8_t i; @@ -515,7 +515,7 @@ int provisioner_pb_gatt_enable(void) return 0; } -int provisioner_pb_gatt_disable(void) +int bt_mesh_provisioner_pb_gatt_disable(void) { u8_t i; @@ -659,7 +659,7 @@ static struct bt_mesh_subnet *bt_mesh_is_net_id_exist(const u8_t net_id[8]) return NULL; } -void proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr) +void bt_mesh_proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr) { bt_mesh_proxy_adv_ctx_t ctx = {0}; u8_t type; diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_client.h b/components/bt/esp_ble_mesh/mesh_core/proxy_client.h index 59f974ef20..b8c9b4676d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy_client.h +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_client.h @@ -72,8 +72,8 @@ typedef struct { int bt_mesh_proxy_prov_client_send(struct bt_mesh_conn *conn, u8_t type, struct net_buf_simple *msg); -int provisioner_pb_gatt_enable(void); -int provisioner_pb_gatt_disable(void); +int bt_mesh_provisioner_pb_gatt_enable(void); +int bt_mesh_provisioner_pb_gatt_disable(void); int bt_mesh_proxy_client_enable(void); int bt_mesh_proxy_client_disable(void); @@ -88,7 +88,7 @@ void bt_mesh_proxy_client_set_conn_cb(proxy_client_connect_cb_t cb); void bt_mesh_proxy_client_set_disconn_cb(proxy_client_disconnect_cb_t cb); void bt_mesh_proxy_client_set_filter_status_cb(proxy_client_recv_filter_status_cb_t cb); -void proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr); +void bt_mesh_proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr); int bt_mesh_proxy_client_connect(const u8_t addr[6], u8_t addr_type, u16_t net_idx); int bt_mesh_proxy_client_disconnect(u8_t conn_handle); diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index df1b480059..df1d7bea83 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -1121,7 +1121,7 @@ static int p_node_set(const char *name) node.element_num = val.element_num; memcpy(node.dev_key, val.dev_key, 16); - err = bt_mesh_provisioner_load_node_info(&node, prov); + err = bt_mesh_provisioner_restore_node_info(&node, prov); if (err) { BT_ERR("%s, Failed to store node 0x%04x", __func__, node.unicast_addr); goto free; diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 5b0a9a968b..bda7533a96 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -110,6 +110,30 @@ static u8_t seg_rx_buf_data[(CONFIG_BLE_MESH_RX_SEG_MSG_COUNT * static u16_t hb_sub_dst = BLE_MESH_ADDR_UNASSIGNED; +static osi_mutex_t tx_seg_lock; + +static void bt_mesh_tx_seg_mutex_new(void) +{ + if (!tx_seg_lock) { + osi_mutex_new(&tx_seg_lock); + __ASSERT(tx_seg_lock, "%s, fail", __func__); + } +} + +static void bt_mesh_tx_seg_lock(void) +{ + if (tx_seg_lock) { + osi_mutex_lock(&tx_seg_lock, OSI_MUTEX_MAX_TIMEOUT); + } +} + +static void bt_mesh_tx_seg_unlock(void) +{ + if (tx_seg_lock) { + osi_mutex_unlock(&tx_seg_lock); + } +} + void bt_mesh_set_hb_sub_dst(u16_t addr) { hb_sub_dst = addr; @@ -198,6 +222,8 @@ static void seg_tx_reset(struct seg_tx *tx) return; } + bt_mesh_tx_seg_lock(); + for (i = 0; i <= tx->seg_n; i++) { if (!tx->seg[i]) { continue; @@ -209,6 +235,8 @@ static void seg_tx_reset(struct seg_tx *tx) tx->seg[i] = NULL; } + bt_mesh_tx_seg_unlock(); + tx->nack_count = 0U; if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) { @@ -276,6 +304,8 @@ static void seg_tx_send_unacked(struct seg_tx *tx) { int i, err; + bt_mesh_tx_seg_lock(); + for (i = 0; i <= tx->seg_n; i++) { struct net_buf *seg = tx->seg[i]; @@ -291,6 +321,7 @@ static void seg_tx_send_unacked(struct seg_tx *tx) if (!(BLE_MESH_ADV(seg)->seg.attempts--)) { BT_WARN("Ran out of retransmit attempts"); seg_tx_complete(tx, -ETIMEDOUT); + bt_mesh_tx_seg_unlock(); return; } @@ -301,9 +332,12 @@ static void seg_tx_send_unacked(struct seg_tx *tx) if (err) { BT_ERR("%s, Sending segment failed", __func__); seg_tx_complete(tx, -EIO); + bt_mesh_tx_seg_unlock(); return; } } + + bt_mesh_tx_seg_unlock(); } static void seg_retransmit(struct k_work *work) @@ -1573,6 +1607,8 @@ void bt_mesh_trans_init(void) (i * CONFIG_BLE_MESH_RX_SDU_MAX)); seg_rx[i].buf.data = seg_rx[i].buf.__buf; } + + bt_mesh_tx_seg_mutex_new(); } void bt_mesh_rpl_clear(void) 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 35cfeafa75..649c58c760 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 @@ -31,9 +31,9 @@ static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, u16_t bt_mesh_client_node_t *node = NULL; sys_snode_t *cur = NULL; - bt_mesh_irq_lock(); + bt_mesh_list_lock(); if (sys_slist_is_empty(list)) { - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); return NULL; } @@ -41,12 +41,12 @@ static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, u16_t cur != NULL; cur = sys_slist_peek_next(cur)) { node = (bt_mesh_client_node_t *)cur; if (node->ctx.addr == tx_dst) { - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); return node; } } - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); return NULL; } @@ -125,9 +125,9 @@ static bool bt_mesh_client_check_node_in_list(sys_slist_t *list, u16_t tx_dst) bt_mesh_client_node_t *node = NULL; sys_snode_t *cur = NULL; - bt_mesh_irq_lock(); + bt_mesh_list_lock(); if (sys_slist_is_empty(list)) { - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); return false; } @@ -135,12 +135,12 @@ static bool bt_mesh_client_check_node_in_list(sys_slist_t *list, u16_t tx_dst) cur != NULL; cur = sys_slist_peek_next(cur)) { node = (bt_mesh_client_node_t *)cur; if (node->ctx.addr == tx_dst) { - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); return true; } } - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); return false; } @@ -212,9 +212,9 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, if ((err = bt_mesh_model_send(model, ctx, msg, cb, cb_data)) != 0) { osi_free(node); } else { - bt_mesh_irq_lock(); + bt_mesh_list_lock(); sys_slist_append(&internal->queue, &node->client_node); - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); k_delayed_work_init(&node->timer, timer_handler); #if !CONFIG_BLE_MESH_PROV_TEST @@ -232,26 +232,28 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, return err; } -static osi_mutex_t client_model_mutex; +static osi_mutex_t client_model_lock; static void bt_mesh_client_model_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&client_model_mutex); - init = true; + if (!client_model_lock) { + osi_mutex_new(&client_model_lock); + __ASSERT(client_model_lock, "%s, fail", __func__); } } void bt_mesh_client_model_lock(void) { - osi_mutex_lock(&client_model_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (client_model_lock) { + osi_mutex_lock(&client_model_lock, OSI_MUTEX_MAX_TIMEOUT); + } } void bt_mesh_client_model_unlock(void) { - osi_mutex_unlock(&client_model_mutex); + if (client_model_lock) { + osi_mutex_unlock(&client_model_lock); + } } int bt_mesh_client_init(struct bt_mesh_model *model) @@ -275,19 +277,22 @@ int bt_mesh_client_init(struct bt_mesh_model *model) return -EINVAL; } - /* TODO: call osi_free() when deinit function is invoked */ - data = osi_calloc(sizeof(bt_mesh_client_internal_data_t)); - if (!data) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; + if (!cli->internal_data) { + data = osi_calloc(sizeof(bt_mesh_client_internal_data_t)); + if (!data) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + /* Init the client data queue */ + sys_slist_init(&data->queue); + + cli->model = model; + cli->internal_data = data; + } else { + bt_mesh_client_clear_list(cli->internal_data); } - /* Init the client data queue */ - sys_slist_init(&data->queue); - - cli->model = model; - cli->internal_data = data; - bt_mesh_client_model_mutex_new(); return 0; @@ -316,15 +321,37 @@ int bt_mesh_client_free_node(bt_mesh_client_node_t *node) } // Release the client node from the queue - bt_mesh_irq_lock(); + bt_mesh_list_lock(); sys_slist_find_and_remove(&internal->queue, &node->client_node); - bt_mesh_irq_unlock(); + bt_mesh_list_unlock(); // Free the node osi_free(node); return 0; } +int bt_mesh_client_clear_list(void *data) +{ + bt_mesh_client_internal_data_t *internal = NULL; + bt_mesh_client_node_t *node = NULL; + + if (!data) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + internal = (bt_mesh_client_internal_data_t *)data; + + bt_mesh_list_lock(); + while (!sys_slist_is_empty(&internal->queue)) { + node = (void *)sys_slist_get_not_empty(&internal->queue); + osi_free(node); + } + bt_mesh_list_unlock(); + + return 0; +} + int bt_mesh_set_client_model_role(bt_mesh_role_param_t *common) { bt_mesh_client_user_data_t *client = NULL; 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 48b540b415..6862dca739 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 @@ -120,26 +120,28 @@ static const bt_mesh_client_op_pair_t gen_op_pair[] = { { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS }, }; -static osi_mutex_t generic_client_mutex; +static osi_mutex_t generic_client_lock; static void bt_mesh_generic_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&generic_client_mutex); - init = true; + if (!generic_client_lock) { + osi_mutex_new(&generic_client_lock); + __ASSERT(generic_client_lock, "%s, fail", __func__); } } static void bt_mesh_generic_client_lock(void) { - osi_mutex_lock(&generic_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (generic_client_lock) { + osi_mutex_lock(&generic_client_lock, OSI_MUTEX_MAX_TIMEOUT); + } } static void bt_mesh_generic_client_unlock(void) { - osi_mutex_unlock(&generic_client_mutex); + if (generic_client_lock) { + osi_mutex_unlock(&generic_client_lock); + } } static void timeout_handler(struct k_work *work) @@ -1172,20 +1174,23 @@ static int generic_client_init(struct bt_mesh_model *model, bool primary) return -EINVAL; } - /* TODO: call osi_free() when deinit function is invoked*/ - internal = osi_calloc(sizeof(generic_internal_data_t)); - if (!internal) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; + if (!client->internal_data) { + internal = osi_calloc(sizeof(generic_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(gen_op_pair); + client->op_pair = gen_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); } - sys_slist_init(&internal->queue); - - client->model = model; - client->op_pair_size = ARRAY_SIZE(gen_op_pair); - client->op_pair = gen_op_pair; - client->internal_data = internal; - bt_mesh_generic_client_mutex_new(); return 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 f420167223..86fae9bad6 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 @@ -108,6 +108,8 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, int bt_mesh_client_free_node(bt_mesh_client_node_t *node); +int bt_mesh_client_clear_list(void *data); + typedef struct { struct bt_mesh_model *model; /* The client model structure */ u8_t role; /* Role of the device - Node/Provisioner */ 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 78b80d7b05..9816466fa0 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 @@ -129,26 +129,28 @@ static const bt_mesh_client_op_pair_t light_op_pair[] = { { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET, BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS }, }; -static osi_mutex_t light_client_mutex; +static osi_mutex_t light_client_lock; static void bt_mesh_light_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&light_client_mutex); - init = true; + if (!light_client_lock) { + osi_mutex_new(&light_client_lock); + __ASSERT(light_client_lock, "%s, fail", __func__); } } static void bt_mesh_light_client_lock(void) { - osi_mutex_lock(&light_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (light_client_lock) { + osi_mutex_lock(&light_client_lock, OSI_MUTEX_MAX_TIMEOUT); + } } static void bt_mesh_light_client_unlock(void) { - osi_mutex_unlock(&light_client_mutex); + if (light_client_lock) { + osi_mutex_unlock(&light_client_lock); + } } static void timeout_handler(struct k_work *work) @@ -1362,20 +1364,23 @@ static int light_client_init(struct bt_mesh_model *model, bool primary) return -EINVAL; } - /* TODO: call osi_free() when deinit function is invoked*/ - internal = osi_calloc(sizeof(light_internal_data_t)); - if (!internal) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; + if (!client->internal_data) { + internal = osi_calloc(sizeof(light_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(light_op_pair); + client->op_pair = light_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); } - sys_slist_init(&internal->queue); - - client->model = model; - client->op_pair_size = ARRAY_SIZE(light_op_pair); - client->op_pair = light_op_pair; - client->internal_data = internal; - bt_mesh_light_client_mutex_new(); return 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 7b93aecfb1..1017591e06 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 @@ -58,26 +58,28 @@ static const bt_mesh_client_op_pair_t sensor_op_pair[] = { { BLE_MESH_MODEL_OP_SENSOR_SERIES_GET, BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS }, }; -static osi_mutex_t sensor_client_mutex; +static osi_mutex_t sensor_client_lock; static void bt_mesh_sensor_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&sensor_client_mutex); - init = true; + if (!sensor_client_lock) { + osi_mutex_new(&sensor_client_lock); + __ASSERT(sensor_client_lock, "%s, fail", __func__); } } static void bt_mesh_sensor_client_lock(void) { - osi_mutex_lock(&sensor_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (sensor_client_lock) { + osi_mutex_lock(&sensor_client_lock, OSI_MUTEX_MAX_TIMEOUT); + } } static void bt_mesh_sensor_client_unlock(void) { - osi_mutex_unlock(&sensor_client_mutex); + if (sensor_client_lock) { + osi_mutex_unlock(&sensor_client_lock); + } } static void timeout_handler(struct k_work *work) @@ -604,20 +606,23 @@ int bt_mesh_sensor_cli_init(struct bt_mesh_model *model, bool primary) return -EINVAL; } - /* TODO: call osi_free() when deinit function is invoked*/ - internal = osi_calloc(sizeof(sensor_internal_data_t)); - if (!internal) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; + if (!client->internal_data) { + internal = osi_calloc(sizeof(sensor_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(sensor_op_pair); + client->op_pair = sensor_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); } - sys_slist_init(&internal->queue); - - client->model = model; - client->op_pair_size = ARRAY_SIZE(sensor_op_pair); - client->op_pair = sensor_op_pair; - client->internal_data = internal; - bt_mesh_sensor_client_mutex_new(); return 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 878ef3da3a..035f970bd1 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 @@ -74,26 +74,28 @@ static const bt_mesh_client_op_pair_t time_scene_op_pair[] = { { BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET, BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS }, }; -static osi_mutex_t time_scene_client_mutex; +static osi_mutex_t time_scene_client_lock; static void bt_mesh_time_scene_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&time_scene_client_mutex); - init = true; + if (!time_scene_client_lock) { + osi_mutex_new(&time_scene_client_lock); + __ASSERT(time_scene_client_lock, "%s, fail", __func__); } } static void bt_mesh_time_scene_client_lock(void) { - osi_mutex_lock(&time_scene_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (time_scene_client_lock) { + osi_mutex_lock(&time_scene_client_lock, OSI_MUTEX_MAX_TIMEOUT); + } } static void bt_mesh_time_scene_client_unlock(void) { - osi_mutex_unlock(&time_scene_client_mutex); + if (time_scene_client_lock) { + osi_mutex_unlock(&time_scene_client_lock); + } } static void timeout_handler(struct k_work *work) @@ -667,20 +669,23 @@ static int time_scene_client_init(struct bt_mesh_model *model, bool primary) return -EINVAL; } - /* TODO: call osi_free() when deinit function is invoked*/ - internal = osi_calloc(sizeof(time_scene_internal_data_t)); - if (!internal) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; + if (!client->internal_data) { + internal = osi_calloc(sizeof(time_scene_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(time_scene_op_pair); + client->op_pair = time_scene_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); } - sys_slist_init(&internal->queue); - - client->model = model; - client->op_pair_size = ARRAY_SIZE(time_scene_op_pair); - client->op_pair = time_scene_op_pair; - client->internal_data = internal; - bt_mesh_time_scene_client_mutex_new(); return 0; diff --git a/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c b/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c index 4bc4bbf656..54b3a417dd 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c @@ -29,24 +29,28 @@ #include "btc_ble_mesh_generic_model.h" -static osi_mutex_t generic_server_mutex; +static osi_mutex_t generic_server_lock; static void bt_mesh_generic_server_mutex_new(void) { - if (!generic_server_mutex) { - osi_mutex_new(&generic_server_mutex); - __ASSERT(generic_server_mutex, "%s, fail", __func__); + if (!generic_server_lock) { + osi_mutex_new(&generic_server_lock); + __ASSERT(generic_server_lock, "%s, fail", __func__); } } void bt_mesh_generic_server_lock(void) { - osi_mutex_lock(&generic_server_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (generic_server_lock) { + osi_mutex_lock(&generic_server_lock, OSI_MUTEX_MAX_TIMEOUT); + } } void bt_mesh_generic_server_unlock(void) { - osi_mutex_unlock(&generic_server_mutex); + if (generic_server_lock) { + osi_mutex_unlock(&generic_server_lock); + } } /* message handlers (Start) */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c b/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c index 9948d8d24f..20664056a1 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c @@ -26,24 +26,28 @@ #include "btc_ble_mesh_lighting_model.h" -static osi_mutex_t light_server_mutex; +static osi_mutex_t light_server_lock; static void bt_mesh_light_server_mutex_new(void) { - if (!light_server_mutex) { - osi_mutex_new(&light_server_mutex); - __ASSERT(light_server_mutex, "%s, fail", __func__); + if (!light_server_lock) { + osi_mutex_new(&light_server_lock); + __ASSERT(light_server_lock, "%s, fail", __func__); } } void bt_mesh_light_server_lock(void) { - osi_mutex_lock(&light_server_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (light_server_lock) { + osi_mutex_lock(&light_server_lock, OSI_MUTEX_MAX_TIMEOUT); + } } void bt_mesh_light_server_unlock(void) { - osi_mutex_unlock(&light_server_mutex); + if (light_server_lock) { + osi_mutex_unlock(&light_server_lock); + } } /* message handlers (Start) */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/server_common.c b/components/bt/esp_ble_mesh/mesh_models/server/server_common.c index 5723c640ec..041cc3b002 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/server_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/server_common.c @@ -193,8 +193,10 @@ void bt_mesh_server_alloc_ctx(struct k_work *work) * Here we use the allocated heap memory to store the "struct bt_mesh_msg_ctx". */ __ASSERT(work, "%s, Invalid parameter", __func__); - work->_reserved = osi_calloc(sizeof(struct bt_mesh_msg_ctx)); - __ASSERT(work->_reserved, "%s, Failed to allocate memory", __func__); + if (!work->_reserved) { + work->_reserved = osi_calloc(sizeof(struct bt_mesh_msg_ctx)); + __ASSERT(work->_reserved, "%s, Failed to allocate memory", __func__); + } } bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last, diff --git a/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c b/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c index fec69b1665..1b10e42344 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c @@ -33,24 +33,28 @@ #include "btc_ble_mesh_time_scene_model.h" -static osi_mutex_t time_scene_server_mutex; +static osi_mutex_t time_scene_server_lock; static void bt_mesh_time_scene_server_mutex_new(void) { - if (!time_scene_server_mutex) { - osi_mutex_new(&time_scene_server_mutex); - __ASSERT(time_scene_server_mutex, "%s, fail", __func__); + if (!time_scene_server_lock) { + osi_mutex_new(&time_scene_server_lock); + __ASSERT(time_scene_server_lock, "%s, fail", __func__); } } void bt_mesh_time_scene_server_lock(void) { - osi_mutex_lock(&time_scene_server_mutex, OSI_MUTEX_MAX_TIMEOUT); + if (time_scene_server_lock) { + osi_mutex_lock(&time_scene_server_lock, OSI_MUTEX_MAX_TIMEOUT); + } } void bt_mesh_time_scene_server_unlock(void) { - osi_mutex_unlock(&time_scene_server_mutex); + if (time_scene_server_lock) { + osi_mutex_unlock(&time_scene_server_lock); + } } /* message handlers (Start) */