前言
在一些场景中,系统需要指定某个设备或多台设备添加到网络中;或者某个节点移除或者需要节点需要添加到其他网络时,如何进行节点的移除,本章主要讲解下ble mesh网络中对节点的添加加与删除方法
删除节点
1. 节点自身清除网络信息
直接将节点恢复到未配网状态,以泰凌微8258 mesh工程为例,用户可以通过长按机械按键进行恢复操作,将设备恢复到未配网状态(当然原网络中依然会保存有节点信息)
- 通过调用factory_reset清除全部信息,或调用mesh_reset_network清除网络信息
if(btn_value == BTN_PRESS_LONG_CLICK)
{
LOG_I("reset system !");
#if CLEAR_NET
mesh_reset_network(1); //重新进入配网
#else
factory_reset(); //恢复出厂
start_reboot();
#endif
}
2. 网关发起删除
- 接口esp_ble_mesh_provisioner_delete_node_with_uuid:通过匹配节点设备uuid来进行删除
- 接口esp_ble_mesh_provisioner_delete_node_with_addr:通过匹配节点地址unicast地址进行删除
esp_ble_mesh_node_t *node_info;
//通过单播地址获取
node_info = esp_ble_mesh_provisioner_get_node_with_addr(unicast);
if (node_info == NULL)
{
ESP_LOGE(TAG, "can't get node info %d", unicast);
return;
}
if(flag & DEL_DEV_ADDR_FLAG)//匹配地址删除
err = esp_ble_mesh_provisioner_delete_node_with_addr(unicast);
else
err = esp_ble_mesh_provisioner_delete_node_with_uuid(node_info->dev_uuid);//匹配uuid
if (err != ESP_OK)
{
ESP_LOGI(TAG, "Provisioner:DelAddr,Fail,%d", err);
}
else
{
ESP_LOGI(TAG, "Provisioner:DelAddr,OK");
}
添加未配网设备
1. 自动发起配网(匹配device uuid)
通过匹配未配网设备uuid中某一段特征标识,系统可自动进行配网
match_val
:匹配缓存、match_len
:长度、offset
:偏移
esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val, uint8_t match_len,uint8_t offset, bool prov_after_match);
- device uuid中的前两个字节与match缓存进行匹配
uint8_t match[2] = {0xdd, 0xdd};
esp_err_t err = ESP_OK;
err = esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false);//匹配0xdd 0xdd
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set matching device uuid (err %d)", err);
return err;
}
2. 主动发起配网(手动添加待配网设备)
通过主动将设备添加到待配网队列中,一旦被扫描到,配网器将自动对其进行配网;需要禁用掉dev uuid匹配的接口
- 2.1 主动添加设备列表,可以将待配网设备统一手动添加到队列中,配网器将对设备进行配网与配置操作
void gw_add_unprov_dev_test(void) //主动添加
{
esp_ble_mesh_unprov_dev_add_t add_dev[CONFIG_BLE_MESH_MAX_PROV_NODES] = {
{
.uuid = {0x5b,0xe9,0x21,0xbe,0xd6,0xe2,0xd4,0x3d,0x9b,0xd0,0x29,0xc9,0x26,0x66,0x66,0x66},//uuid
.oob_info = 0,
.bearer = ESP_BLE_MESH_PROV_ADV|ESP_BLE_MESH_PROV_GATT,//支持bear
.addr = {0x29,0xc9,0x26,0x66,0x66,0x66},
.addr_type = ESP_BLE_MESH_ADDR_TYPE_PUBLIC,
},
{
.uuid = {0x5c,0xe9,0x21,0xbe,0xd6,0xe2,0xd4,0x3d,0x9b,0xd0,0x29,0xc9,0x26,0x66,0x66,0x66},
.oob_info = 0,
.bearer = ESP_BLE_MESH_PROV_ADV|ESP_BLE_MESH_PROV_GATT,//支持
.addr = {0x29,0xc9,0x26,0x66,0x66,0x66},
.addr_type = ESP_BLE_MESH_ADDR_TYPE_PUBLIC,
},
/*
****
*/
};
for(int i = 0; i < CONFIG_BLE_MESH_MAX_PROV_NODES; i++)
esp_ble_mesh_provisioner_add_unprov_dev(&add_dev[i], ADD_DEV_FLUSHABLE_DEV_FLAG);
}
- 2.2 扫描后进行添加,在esp_ble_mesh_register_prov_callback回调事件ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT中进行调用recv_unprov_adv_pkt进行配网(发现设备后进行主动发起配网)
static void recv_unprov_adv_pkt(uint8_t dev_uuid[ESP_BLE_MESH_OCTET16_LEN], uint8_t addr[BD_ADDR_LEN],
esp_ble_mesh_addr_type_t addr_type, uint16_t oob_info,
uint8_t adv_type, esp_ble_mesh_prov_bearer_t bearer)
{
esp_ble_mesh_unprov_dev_add_t add_dev = {0};
esp_err_t err;
/* Due to the API esp_ble_mesh_provisioner_set_dev_uuid_match, Provisioner will only
* use this callback to report the devices, whose device UUID starts with 0xdd & 0xdd,
* to the application layer.
*/
ESP_LOG_BUFFER_HEX("Device address", addr, BD_ADDR_LEN);
ESP_LOGI(TAG, "Address type 0x%02x, adv type 0x%02x", addr_type, adv_type);
ESP_LOG_BUFFER_HEX("Device UUID", dev_uuid, ESP_BLE_MESH_OCTET16_LEN);
ESP_LOGI(TAG, "oob info 0x%04x, bearer %s", oob_info, (bearer & ESP_BLE_MESH_PROV_ADV) ? "PB-ADV" : "PB-GATT");
memcpy(add_dev.addr, addr, BD_ADDR_LEN);
add_dev.addr_type = (uint8_t)addr_type;
memcpy(add_dev.uuid, dev_uuid, ESP_BLE_MESH_OCTET16_LEN);
add_dev.oob_info = oob_info;
add_dev.bearer = (uint8_t)bearer;
/* Note: If unprovisioned device adv packets have not been received, we should not add
device with ADD_DEV_START_PROV_NOW_FLAG set. */
err = esp_ble_mesh_provisioner_add_unprov_dev(&add_dev,
ADD_DEV_RM_AFTER_PROV_FLAG | ADD_DEV_START_PROV_NOW_FLAG | ADD_DEV_FLUSHABLE_DEV_FLAG);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to start provisioning device");
}
}
提高配网效率
走标准的配网流程时,为了提高多个设备的配网效率,可以通过配置界面修改支持同时配网的个数(需要评估ram资源),其中
- PB-ADV:最大支持5个设备
- PB-GATT:最大支持10个设备
所以,单设备数量上百个时,通过这种方式配网还是会比较耗时,所以快速配网Fast Provisioning应运而生