nRF51822SDK中db_discovery学习笔记

来源:互联网 发布:windows 编译so 编辑:程序博客网 时间:2024/06/05 06:33

ble_Central模式下ble_app_hrs_c例程中的db_discovery学习笔记

不管Notification还是Indication,master端连接完成salve端后首先必须对他们进行ENABLE。使能他们除了conn handle之外还需要cccd handle(Client Characteristic Configuration Descriptor客户端特性配置描述符)。使能完成后通过notification和indication传输数据需要conn handle和handle value。这些都必须通过db discovery进行获得。

1. db_discovery_init()—service characteristic descriptor扫描初始化

m_num_of_handlers_reg = 0;
m_initialized = true;
m_num_of_discoveries_made = 0;
m_pending_usr_evt_index = 0;

2. hrs_c_init()—订阅Heart rate service

2.1. 把hrs_c_evt_handler函数地址传送给m_ble_hrs_c结构体的evt_handler函数指针,当通过notification接收到心率数据时回调hrs_c_evt_handler函数

hrs_c_init_obj.evt_handler = hrs_c_evt_handler;
ble_hrs_c_init(&m_ble_hrs_c, &hrs_c_init_obj);

2.2. 把hrs_uuid和db_discover_evt_handler回调函数传送给m_registered_handlers待搜索到匹配的service uuid就回调db_discover_evt_handler,在该回调函数内部再次回调2.1节中的hrs_c_evt_handler,只不过evt_type由BLE_HRS_C_EVT_HRM_NOTIFICATION变为了BLE_HRS_C_EVT_DISCOVERY_COMPLETE

hrs_uuid.type = BLE_UUID_TYPE_BLE;
hrs_uuid.uuid = BLE_UUID_HEART_RATE_SERVICE;
ble_db_discovery_evt_register(&hrs_uuid,db_discover_evt_handler);

bas service订阅也是一样的逻辑

3. ble_db_discovery.c中相关程序笔记

3.1 .service characteristic descriptor扫描和回调接口

sd_ble_gattc_primary_services_discover—>on_primary_srv_discovery_rsp;
sd_ble_gattc_characteristics_discover—>on_characteristic_discovery_rsp;
sd_ble_gattc_descriptors_discover—>on_descriptor_discovery_rsp.
与client的连接建立完成后通过调用ble_db_discovery_start接口就可以自动扫描client端的service characteristic descriptor并通过注册的回调函数通知application层进行相应处理。

3.2 .service 扫描

调用ble_db_discovery_start后从注册表m_registered_handlers中的第一个service uuid开始扫描,扫描完成后回调on_primary_srv_discovery_rsp;
把扫描到的service uuid和handle range保存到m_ble_hrs_c结构体中的services[curr_srv_ind]中;
搜索handle range范围内的characteristics。

3.3 .characteristics扫描

把discovered characteristics保存到m_ble_hrs_c.services[m_ble_hrs_c.curr_srv_ind].charateristics中并更新char_count;
检查每一个service对应的characteristic是否有descriptor需要搜索,如果有开始搜索,如果没有就调用扫描结束处理函数discovery_complete_evt_trigger和on_srv_disc_completion。

3.4 .descriptor扫描

Loop through all the descriptors to find the CCCD(Client Characteristic Configuration Descriptor)
p_char_being_discovered->cccd_handle = p_desc_disc_rsp_evt->descs[i].handle;通过该handle可以操作使能notification等相关配置操作。

3.5 .discovery_complete_evt_trigger和on_srv_disc_completion

把搜索完成的service的相关结果保存到m_pending_user_evts中,待所有service的characteristic和descriptor全部搜索完成后逐一回调注册的回调函数;
on_srv_disc_completion:determine if there are more services to be discovered,and if so, initiate the discovery of the next service.

4. 疑问点

/*@brief Event structure for @ref BLE_GATTC_EVT_CHAR_DISC_RSP. /
typedef struct
{
uint16_t count; /*< Characteristic count. /
ble_gattc_char_t chars[1]; /*< Characteristic data, variable length. /
} ble_gattc_evt_char_disc_rsp_t;

/*@brief Event structure for @ref BLE_GATTC_EVT_DESC_DISC_RSP. /
typedef struct
{
uint16_t count; /*< Descriptor count. /
ble_gattc_desc_t descs[1]; /*< Descriptor data, variable length. /
} ble_gattc_evt_desc_disc_rsp_t;
扫描service对应的characteristic和descriptor时往往可以扫描到几个,但是ble_gattc_evt_char_disc_rsp_t和ble_gattc_evt_desc_disc_rsp_t结构体却在其中只定义了一个元素。当扫描到几个时,解析依然可以成功,但.h头文件加了写保护不可更改。

1 0
原创粉丝点击