Service discovery process analysis

http://blog.chinaunix.net/uid-28852942-id-5748596.html


This lecture mainly focuses on the process of how the host can find the service it needs on the slave after the master-slave connection. Need to watch with the master-slave communication process analysis tutorial.

 

The service search process is after the master-slave connection, the host needs to find all the services on the slave and the information such as the characteristic values ​​and descriptors under the service. However, the master-slave communication example in SDK is a simple master-slave lighting demo, so the service search process is not to search for all services on the slave machine, but to search for services on the slave machine according to the service to be searched registered during initialization. It has the following feature values ​​and descriptor information.

 

There are many details of the service discovery process, and all the details considered are not covered by the text description, so the main process of service discovery is described here.

 

In the master code, when connected to the slave, the protocol stack will pass the BLE_GAP_EVT_CONNECTED event to the upper dispatch function ble_evt_dispatch

This function will pass the event to all event handling functions. The event handling function dm_ble_evt_handler of the device management module handles connection events



When dm_ble_evt_handler processes the connection event, it generates an internal event DM_EVT_CONNECTION of the device management module, and then passes it to the callback function registered when the device management module is initialized. As shown below


So finally the device_manager_event_handler function handles the DM_EVT_CONNECTION event.

 

And this function will call the client_handling_create function when processing the DM_EVT_CONNECTION event, and the service discovery process will be started inside the function


In the ble_db_discovery_start function, the uuid of the desired service registered by calling client_handling_init in the main function will be extracted. To perform a service lookup

As shown below:


When the service search is complete, the protocol stack passes the BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP event to the dispatch function, and the dispatch function is then submitted to the event handler. At the end of the event handler client_handling_ble_evt_handler, the ble_db_discovery_on_ble_evt function is called, which handles related events in the service discovery process.


After receiving the service discovery completion event, proceed to the on_primary_srv_discovery_rsp function for processing

   

The internal processing of this function is to start the search of the features in the service. As shown below

    


After each feature search is completed, it will return the BLE_GATTC_EVT_CHAR_DISC_RSP event, so enter

on_characteristic_discovery_rsp function processing


The processing function is relatively long and inconvenient for the overall screenshot. The main function of some screenshots here is to update the value of the total number of features that have been found, and to determine whether there are still possible features that need to be discovered, if possible ( The handle of the feature value of the currently found feature is less than the end handle of the service), then continue to perform the feature discovery and discovery mentioned above, if it is not, start the descriptor discovery process.







In summary, when the feature discovery is complete, the descriptors_discover is called to execute the descriptor discovery process.

The main function of this function is to determine whether there is a descriptor to be discovered, and if so, set the handle range to be searched during the discovery process. Some screenshots are as follows


Then execute the discovery descriptor function according to whether you need to find the descriptor mark


After the descriptor is found, the event BLE_GATTC_EVT_DESC_DISC_RSP will be received and processed by the corresponding event handler


The processing function will first determine whether the found descriptor is a CCCD, and if so, record its handle. Then determine when the descriptor is found under which characteristic, if it has not reached the total number of characteristics found before, continue to perform the search descriptor to find the descriptor of the next characteristic



When all descriptors are found, the following two functions will be called


 The discovery_complete_evt_trigger function will generate a service discovery completion event to the callback functions that will be executed after the services registered in the client_handling_init function are initialized in the main before the search is found, and those callback functions are called when all the registered services are found.

The on_srv_disc_completion function is responsible for judging whether to find all the services that are registered in client_handling_init before calling, and if not, continue to find the next service that needs to be found.

PS: These two functions are not necessarily called after the descriptor is found, or they may be called directly after the feature is found. Because there may be no descriptors to find


Guess you like

Origin blog.csdn.net/lilifang_2011/article/details/72876811