1*503a627eSMilanka Ringwald# 2*503a627eSMilanka Ringwald 3*503a627eSMilanka RingwaldIn the following, we explain how the various Bluetooth profiles are used 4*503a627eSMilanka Ringwaldin BTstack. 5*503a627eSMilanka Ringwald 6*503a627eSMilanka Ringwald## GAP - Generic Access Profile: Classic 7*503a627eSMilanka Ringwald 8*503a627eSMilanka Ringwald 9*503a627eSMilanka RingwaldThe GAP profile defines how devices find each other and establish a 10*503a627eSMilanka Ringwaldsecure connection for other profiles. As mentioned before, the GAP 11*503a627eSMilanka Ringwaldfunctionality is split between and . Please check both. 12*503a627eSMilanka Ringwald 13*503a627eSMilanka Ringwald### Become discoverable 14*503a627eSMilanka Ringwald 15*503a627eSMilanka RingwaldA remote unconnected Bluetooth device must be set as “discoverable” in 16*503a627eSMilanka Ringwaldorder to be seen by a device performing the inquiry scan. To become 17*503a627eSMilanka Ringwalddiscoverable, an application can call *gap_discoverable_control* with 18*503a627eSMilanka Ringwaldinput parameter 1. If you want to provide a helpful name for your 19*503a627eSMilanka Ringwalddevice, the application can set its local name by calling 20*503a627eSMilanka Ringwald*gap_set_local_name*. To save energy, you may set the device as 21*503a627eSMilanka Ringwaldundiscoverable again, once a connection is established. See Listing 22*503a627eSMilanka Ringwald[below](#lst:Discoverable) for an example. 23*503a627eSMilanka Ringwald 24*503a627eSMilanka Ringwald~~~~ {#lst:Discoverable .c caption="{Setting discoverable mode.}"} 25*503a627eSMilanka Ringwald int main(void){ 26*503a627eSMilanka Ringwald ... 27*503a627eSMilanka Ringwald // make discoverable 28*503a627eSMilanka Ringwald gap_discoverable_control(1); 29*503a627eSMilanka Ringwald btstack_run_loop_execute(); 30*503a627eSMilanka Ringwald return 0; 31*503a627eSMilanka Ringwald } 32*503a627eSMilanka Ringwald void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){ 33*503a627eSMilanka Ringwald ... 34*503a627eSMilanka Ringwald switch(state){ 35*503a627eSMilanka Ringwald case W4_CHANNEL_COMPLETE: 36*503a627eSMilanka Ringwald // if connection is successful, make device undiscoverable 37*503a627eSMilanka Ringwald gap_discoverable_control(0); 38*503a627eSMilanka Ringwald ... 39*503a627eSMilanka Ringwald } 40*503a627eSMilanka Ringwald } 41*503a627eSMilanka Ringwald~~~~ 42*503a627eSMilanka Ringwald 43*503a627eSMilanka Ringwald### Discover remote devices {#sec:GAPdiscoverRemoteDevices} 44*503a627eSMilanka Ringwald 45*503a627eSMilanka RingwaldTo scan for remote devices, the *hci_inquiry* command is used. Found 46*503a627eSMilanka Ringwaldremote devices are reported as a part of: 47*503a627eSMilanka Ringwald 48*503a627eSMilanka Ringwald- HCI_EVENT_INQUIRY_RESULT, 49*503a627eSMilanka Ringwald 50*503a627eSMilanka Ringwald- HCI_EVENT-_INQUIRY_RESULT_WITH_RSSI, or 51*503a627eSMilanka Ringwald 52*503a627eSMilanka Ringwald- HCI_EVENT_EXTENDED_INQUIRY_RESPONSE events. 53*503a627eSMilanka Ringwald 54*503a627eSMilanka RingwaldEach response contains at least the Bluetooth address, the class of device, the page scan 55*503a627eSMilanka Ringwaldrepetition mode, and the clock offset of found device. The latter events 56*503a627eSMilanka Ringwaldadd information about the received signal strength or provide the 57*503a627eSMilanka RingwaldExtended Inquiry Result (EIR). A code snippet is shown in Listing 58*503a627eSMilanka Ringwald[below](#lst:DiscoverDevices). 59*503a627eSMilanka Ringwald 60*503a627eSMilanka Ringwald~~~~ {#lst:DiscoverDevices .c caption="{Discover remote devices.}"} 61*503a627eSMilanka Ringwald void print_inquiry_results(uint8_t *packet){ 62*503a627eSMilanka Ringwald int event = packet[0]; 63*503a627eSMilanka Ringwald int numResponses = hci_event_inquiry_result_get_num_responses(packet); 64*503a627eSMilanka Ringwald uint16_t classOfDevice, clockOffset; 65*503a627eSMilanka Ringwald uint8_t rssi, pageScanRepetitionMode; 66*503a627eSMilanka Ringwald for (i=0; i<numResponses; i++){ 67*503a627eSMilanka Ringwald bt_flip_addr(addr, &packet[3+i*6]); 68*503a627eSMilanka Ringwald pageScanRepetitionMode = packet [3 + numResponses*6 + i]; 69*503a627eSMilanka Ringwald if (event == HCI_EVENT_INQUIRY_RESULT){ 70*503a627eSMilanka Ringwald classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1+1) + i*3); 71*503a627eSMilanka Ringwald clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+1+3) + i*2) & 0x7fff; 72*503a627eSMilanka Ringwald rssi = 0; 73*503a627eSMilanka Ringwald } else { 74*503a627eSMilanka Ringwald classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1) + i*3); 75*503a627eSMilanka Ringwald clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+3) + i*2) & 0x7fff; 76*503a627eSMilanka Ringwald rssi = packet [3 + numResponses*(6+1+1+3+2) + i*1]; 77*503a627eSMilanka Ringwald } 78*503a627eSMilanka Ringwald printf("Device found: %s with COD: 0x%06x, pageScan %u, clock offset 0x%04x, rssi 0x%02x\n", bd_addr_to_str(addr), classOfDevice, pageScanRepetitionMode, clockOffset, rssi); 79*503a627eSMilanka Ringwald } 80*503a627eSMilanka Ringwald } 81*503a627eSMilanka Ringwald 82*503a627eSMilanka Ringwald void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){ 83*503a627eSMilanka Ringwald ... 84*503a627eSMilanka Ringwald switch (event) { 85*503a627eSMilanka Ringwald case HCI_STATE_WORKING: 86*503a627eSMilanka Ringwald hci_send_cmd(&hci_write_inquiry_mode, 0x01); // with RSSI 87*503a627eSMilanka Ringwald break; 88*503a627eSMilanka Ringwald case HCI_EVENT_COMMAND_COMPLETE: 89*503a627eSMilanka Ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_inquiry_mode) ) { 90*503a627eSMilanka Ringwald start_scan(); 91*503a627eSMilanka Ringwald } 92*503a627eSMilanka Ringwald case HCI_EVENT_COMMAND_STATUS: 93*503a627eSMilanka Ringwald if (COMMAND_STATUS_EVENT(packet, hci_write_inquiry_mode) ) { 94*503a627eSMilanka Ringwald printf("Ignoring error (0x%x) from hci_write_inquiry_mode.\n", packet[2]); 95*503a627eSMilanka Ringwald hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0); 96*503a627eSMilanka Ringwald } 97*503a627eSMilanka Ringwald break; 98*503a627eSMilanka Ringwald case HCI_EVENT_INQUIRY_RESULT: 99*503a627eSMilanka Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 100*503a627eSMilanka Ringwald print_inquiry_results(packet); 101*503a627eSMilanka Ringwald break; 102*503a627eSMilanka Ringwald ... 103*503a627eSMilanka Ringwald } 104*503a627eSMilanka Ringwald } 105*503a627eSMilanka Ringwald~~~~ 106*503a627eSMilanka Ringwald 107*503a627eSMilanka RingwaldBy default, neither RSSI values nor EIR are reported. If the Bluetooth 108*503a627eSMilanka Ringwalddevice implements Bluetooth Specification 2.1 or higher, the 109*503a627eSMilanka Ringwald*hci_write_inquiry_mode* command enables reporting of this advanced 110*503a627eSMilanka Ringwaldfeatures (0 for standard results, 1 for RSSI, 2 for RSSI and EIR). 111*503a627eSMilanka Ringwald 112*503a627eSMilanka RingwaldA complete GAP inquiry example is provided [here](examples/examples/#sec:gapinquiryExample). 113*503a627eSMilanka Ringwald 114*503a627eSMilanka Ringwald### Pairing of Devices 115*503a627eSMilanka Ringwald 116*503a627eSMilanka RingwaldBy default, Bluetooth communication is not authenticated, and any device 117*503a627eSMilanka Ringwaldcan talk to any other device. A Bluetooth device (for example, cellular 118*503a627eSMilanka Ringwaldphone) may choose to require authentication to provide a particular 119*503a627eSMilanka Ringwaldservice (for example, a Dial-Up service). The process of establishing 120*503a627eSMilanka Ringwaldauthentication is called pairing. Bluetooth provides two mechanism for 121*503a627eSMilanka Ringwaldthis. 122*503a627eSMilanka Ringwald 123*503a627eSMilanka RingwaldOn Bluetooth devices that conform to the Bluetooth v2.0 or older 124*503a627eSMilanka Ringwaldspecification, a PIN code (up to 16 bytes ASCII) has to be entered on 125*503a627eSMilanka Ringwaldboth sides. This isn’t optimal for embedded systems that do not have 126*503a627eSMilanka Ringwaldfull I/O capabilities. To support pairing with older devices using a 127*503a627eSMilanka RingwaldPIN, see Listing [below](#lst:PinCodeRequest). 128*503a627eSMilanka Ringwald 129*503a627eSMilanka Ringwald~~~~ {#lst:PinCodeRequest .c caption="{PIN code request.}"} 130*503a627eSMilanka Ringwald void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){ 131*503a627eSMilanka Ringwald ... 132*503a627eSMilanka Ringwald switch (event) { 133*503a627eSMilanka Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 134*503a627eSMilanka Ringwald // inform about pin code request 135*503a627eSMilanka Ringwald printf("Pin code request - using '0000'\n\r"); 136*503a627eSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, bd_addr); 137*503a627eSMilanka Ringwald 138*503a627eSMilanka Ringwald // baseband address, pin length, PIN: c-string 139*503a627eSMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, &bd_addr, 4, "0000"); 140*503a627eSMilanka Ringwald break; 141*503a627eSMilanka Ringwald ... 142*503a627eSMilanka Ringwald } 143*503a627eSMilanka Ringwald } 144*503a627eSMilanka Ringwald~~~~ 145*503a627eSMilanka Ringwald 146*503a627eSMilanka RingwaldThe Bluetooth v2.1 specification introduces Secure Simple Pairing (SSP), 147*503a627eSMilanka Ringwaldwhich is a better approach as it both improves security and is better 148*503a627eSMilanka Ringwaldadapted to embedded systems. With SSP, the devices first exchange their 149*503a627eSMilanka RingwaldIO Capabilities and then settle on one of several ways to verify that 150*503a627eSMilanka Ringwaldthe pairing is legitimate. If the Bluetooth device supports SSP, BTstack 151*503a627eSMilanka Ringwaldenables it by default and even automatically accepts SSP pairing 152*503a627eSMilanka Ringwaldrequests. Depending on the product in which BTstack is used, this may 153*503a627eSMilanka Ringwaldnot be desired and should be replaced with code to interact with the 154*503a627eSMilanka Ringwalduser. 155*503a627eSMilanka Ringwald 156*503a627eSMilanka RingwaldRegardless of the authentication mechanism (PIN/SSP), on success, both 157*503a627eSMilanka Ringwalddevices will generate a link key. The link key can be stored either in 158*503a627eSMilanka Ringwaldthe Bluetooth module itself or in a persistent storage, see 159*503a627eSMilanka Ringwald[here](porting/#sec:persistentStoragePorting). The next time the device connects and 160*503a627eSMilanka Ringwaldrequests an authenticated connection, both devices can use the 161*503a627eSMilanka Ringwaldpreviously generated link key. Please note that the pairing must be 162*503a627eSMilanka Ringwaldrepeated if the link key is lost by one device. 163*503a627eSMilanka Ringwald 164*503a627eSMilanka Ringwald### Dedicated Bonding 165*503a627eSMilanka Ringwald 166*503a627eSMilanka RingwaldAside from the regular bonding, Bluetooth also provides the concept of 167*503a627eSMilanka Ringwald“dedicated bonding”, where a connection is established for the sole 168*503a627eSMilanka Ringwaldpurpose of bonding the device. After the bonding process is over, the 169*503a627eSMilanka Ringwaldconnection will be automatically terminated. BTstack supports dedicated 170*503a627eSMilanka Ringwaldbonding via the *gap_dedicated_bonding* function. 171*503a627eSMilanka Ringwald 172*503a627eSMilanka Ringwald## SPP - Serial Port Profile 173*503a627eSMilanka Ringwald 174*503a627eSMilanka RingwaldThe SPP profile defines how to set up virtual serial ports and connect 175*503a627eSMilanka Ringwaldtwo Bluetooth enabled devices. Please keep in mind that a serial port does not 176*503a627eSMilanka Ringwaldpreserve packet boundaries if you try to send data as packets and read about 177*503a627eSMilanka Ringwald[RFCOMM packet boundaries]({protocols/#sec:noRfcommPacketBoundaries}). 178*503a627eSMilanka Ringwald 179*503a627eSMilanka Ringwald### Accessing an SPP Server on a remote device 180*503a627eSMilanka Ringwald 181*503a627eSMilanka RingwaldTo access a remote SPP server, you first need to query the remote device 182*503a627eSMilanka Ringwaldfor its SPP services. Section [on querying remote SDP service](#sec:querySDPProtocols) 183*503a627eSMilanka Ringwaldshows how to query for all RFCOMM channels. For SPP, you can do the same 184*503a627eSMilanka Ringwaldbut use the SPP UUID 0x1101 for the query. After you have identified the 185*503a627eSMilanka Ringwaldcorrect RFCOMM channel, you can create an RFCOMM connection as shown 186*503a627eSMilanka Ringwald[here](protocols/#sec:rfcommClientProtocols). 187*503a627eSMilanka Ringwald 188*503a627eSMilanka Ringwald### Providing an SPP Server 189*503a627eSMilanka Ringwald 190*503a627eSMilanka RingwaldTo provide an SPP Server, you need to provide an RFCOMM service with a 191*503a627eSMilanka Ringwaldspecific RFCOMM channel number as explained in section on 192*503a627eSMilanka Ringwald[RFCOMM service](protocols/#sec:rfcommServiceProtocols). Then, you need to create 193*503a627eSMilanka Ringwaldan SDP record for it and publish it with the SDP server by calling 194*503a627eSMilanka Ringwald*sdp_register_service*. BTstack provides the 195*503a627eSMilanka Ringwald*spp_create_sdp_record* function in that requires an empty buffer of 196*503a627eSMilanka Ringwaldapproximately 200 bytes, the service channel number, and a service name. 197*503a627eSMilanka RingwaldHave a look at the [SPP Counter example](examples/examples/#sec:sppcounterExample). 198*503a627eSMilanka Ringwald 199*503a627eSMilanka Ringwald 200*503a627eSMilanka Ringwald## PAN - Personal Area Networking Profile {#sec:panProfiles} 201*503a627eSMilanka Ringwald 202*503a627eSMilanka Ringwald 203*503a627eSMilanka RingwaldThe PAN profile uses BNEP to provide on-demand networking capabilities 204*503a627eSMilanka Ringwaldbetween Bluetooth devices. The PAN profile defines the following roles: 205*503a627eSMilanka Ringwald 206*503a627eSMilanka Ringwald- PAN User (PANU) 207*503a627eSMilanka Ringwald 208*503a627eSMilanka Ringwald- Network Access Point (NAP) 209*503a627eSMilanka Ringwald 210*503a627eSMilanka Ringwald- Group Ad-hoc Network (GN) 211*503a627eSMilanka Ringwald 212*503a627eSMilanka RingwaldPANU is a Bluetooth device that communicates as a client with GN, or 213*503a627eSMilanka RingwaldNAP, or with another PANU Bluetooth device, through a point-to-point 214*503a627eSMilanka Ringwaldconnection. Either the PANU or the other Bluetooth device may terminate 215*503a627eSMilanka Ringwaldthe connection at anytime. 216*503a627eSMilanka Ringwald 217*503a627eSMilanka RingwaldNAP is a Bluetooth device that provides the service of routing network 218*503a627eSMilanka Ringwaldpackets between PANU by using BNEP and the IP routing mechanism. A NAP 219*503a627eSMilanka Ringwaldcan also act as a bridge between Bluetooth networks and other network 220*503a627eSMilanka Ringwaldtechnologies by using the Ethernet packets. 221*503a627eSMilanka Ringwald 222*503a627eSMilanka RingwaldThe GN role enables two or more PANUs to interact with each other 223*503a627eSMilanka Ringwaldthrough a wireless network without using additional networking hardware. 224*503a627eSMilanka RingwaldThe devices are connected in a piconet where the GN acts as a master and 225*503a627eSMilanka Ringwaldcommunicates either point-to-point or a point-to-multipoint with a 226*503a627eSMilanka Ringwaldmaximum of seven PANU slaves by using BNEP. 227*503a627eSMilanka Ringwald 228*503a627eSMilanka RingwaldCurrently, BTstack supports only PANU. 229*503a627eSMilanka Ringwald 230*503a627eSMilanka Ringwald### Accessing a remote PANU service 231*503a627eSMilanka Ringwald 232*503a627eSMilanka RingwaldTo access a remote PANU service, you first need perform an SDP query to 233*503a627eSMilanka Ringwaldget the L2CAP PSM for the requested PANU UUID. With these two pieces of 234*503a627eSMilanka Ringwaldinformation, you can connect BNEP to the remote PANU service with the 235*503a627eSMilanka Ringwald*bnep_connect* function. The Section on [PANU Demo example](examples/examples/#sec:panudemoExample) 236*503a627eSMilanka Ringwaldshows how this is accomplished. 237*503a627eSMilanka Ringwald 238*503a627eSMilanka Ringwald### Providing a PANU service 239*503a627eSMilanka Ringwald 240*503a627eSMilanka RingwaldTo provide a PANU service, you need to provide a BNEP service with the 241*503a627eSMilanka Ringwaldservice UUID, e.g. the PANU UUID, and a maximal ethernet frame size, 242*503a627eSMilanka Ringwaldas explained in Section [on BNEP service](protocols/#sec:bnepServiceProtocols). Then, you need to 243*503a627eSMilanka Ringwaldcreate an SDP record for it and publish it with the SDP server by 244*503a627eSMilanka Ringwaldcalling *sdp_register_service*. BTstack provides the 245*503a627eSMilanka Ringwald*pan_create_panu_sdp_record* function in *src/pan.c* that requires an 246*503a627eSMilanka Ringwaldempty buffer of approximately 200 bytes, a description, and a security 247*503a627eSMilanka Ringwalddescription. 248*503a627eSMilanka Ringwald 249*503a627eSMilanka Ringwald## HSP - Headset Profile 250*503a627eSMilanka Ringwald 251*503a627eSMilanka RingwaldThe HSP profile defines how a Bluetooth-enabled headset should communicate 252*503a627eSMilanka Ringwaldwith another Bluetooth enabled device. It relies on SCO for audio encoded 253*503a627eSMilanka Ringwaldin 64 kbit/s CVSD and a subset of AT commands from GSM 07.07 for 254*503a627eSMilanka Ringwaldminimal controls including the ability to ring, answer a call, hang up and adjust the volume. 255*503a627eSMilanka Ringwald 256*503a627eSMilanka RingwaldThe HSP defines two roles: 257*503a627eSMilanka Ringwald 258*503a627eSMilanka Ringwald - Audio Gateway (AG) - a device that acts as the gateway of the audio, typically a mobile phone or PC. 259*503a627eSMilanka Ringwald 260*503a627eSMilanka Ringwald - Headset (HS) - a device that acts as the AG's remote audio input and output control. 261*503a627eSMilanka Ringwald 262*503a627eSMilanka RingwaldThere are following restrictions: 263*503a627eSMilanka Ringwald- The CVSD is used for audio transmission. 264*503a627eSMilanka Ringwald 265*503a627eSMilanka Ringwald- Between headset and audio gateway, only one audio connection at a time is supported. 266*503a627eSMilanka Ringwald 267*503a627eSMilanka Ringwald- The profile offers only basic interoperability – for example, handling of multiple calls at the audio gateway is not included. 268*503a627eSMilanka Ringwald 269*503a627eSMilanka Ringwald- The only assumption on the headset’s user interface is the possibility to detect a user initiated action (e.g. pressing a button). 270*503a627eSMilanka Ringwald 271*503a627eSMilanka Ringwald%TODO: audio paths 272*503a627eSMilanka Ringwald 273*503a627eSMilanka Ringwald 274*503a627eSMilanka Ringwald## HFP - Hands-Free Profile 275*503a627eSMilanka Ringwald 276*503a627eSMilanka RingwaldThe HFP profile defines how a Bluetooth-enabled device, e.g. a car kit or a headset, can be used to place and receive calls via a audio gateway device, typically a mobile phone. 277*503a627eSMilanka RingwaldIt relies on SCO for audio encoded in 64 kbit/s CVSD and a bigger subset of AT commands from GSM 07.07 then HSP for 278*503a627eSMilanka Ringwaldcontrols including the ability to ring, to place and receive calls, join a conference call, to answer, hold or reject a call, and adjust the volume. 279*503a627eSMilanka Ringwald 280*503a627eSMilanka RingwaldThe HFP defines two roles: 281*503a627eSMilanka Ringwald 282*503a627eSMilanka Ringwald- Audio Gateway (AG) – a device that acts as the gateway of the audio,, typically a mobile phone. 283*503a627eSMilanka Ringwald 284*503a627eSMilanka Ringwald- Hands-Free Unit (HF) – a device that acts as the AG's remote audio input and output control. 285*503a627eSMilanka Ringwald 286*503a627eSMilanka Ringwald%TODO: audio paths 287*503a627eSMilanka Ringwald 288*503a627eSMilanka Ringwald## HID - Human-Interface Device Profile 289*503a627eSMilanka Ringwald 290*503a627eSMilanka RingwaldThe HID profile allows an HID Host to connect to one or more HID Devices and communicate with them. 291*503a627eSMilanka RingwaldExamples of Bluetooth HID devices are keyboards, mice, joysticks, gamepads, remote controls, and also voltmeters and temperature sensors. 292*503a627eSMilanka RingwaldTypical HID hosts would be a personal computer, tablets, gaming console, industrial machine, or data-recording device. 293*503a627eSMilanka Ringwald 294*503a627eSMilanka RingwaldPlease refer to: 295*503a627eSMilanka Ringwald 296*503a627eSMilanka Ringwald- [HID Host API](appendix/apis/#sec:hidHostAPIAppendix) and [hid_host_demo](examples/examples/#sec:hidhostdemoExample) for the HID Host role 297*503a627eSMilanka Ringwald 298*503a627eSMilanka Ringwald- [HID Device API](appendix/apis/#sec:hidDeviceAPIAppendix), [hid_keyboard_demo](examples/examples/#sec:hidkeyboarddemoExample) and [hid_mouse_demo](examples/examples/#sec:hidmousedemoExample) for the HID Device role. 299*503a627eSMilanka Ringwald 300*503a627eSMilanka Ringwald 301*503a627eSMilanka Ringwald## GAP LE - Generic Access Profile for Low Energy 302*503a627eSMilanka Ringwald 303*503a627eSMilanka Ringwald 304*503a627eSMilanka RingwaldAs with GAP for Classic, the GAP LE profile defines how to discover and 305*503a627eSMilanka Ringwaldhow to connect to a Bluetooth Low Energy device. There are several GAP 306*503a627eSMilanka Ringwaldroles that a Bluetooth device can take, but the most important ones are 307*503a627eSMilanka Ringwaldthe Central and the Peripheral role. Peripheral devices are those that 308*503a627eSMilanka Ringwaldprovide information or can be controlled. Central devices are those that 309*503a627eSMilanka Ringwaldconsume information or control the peripherals. Before the connection 310*503a627eSMilanka Ringwaldcan be established, devices are first going through an advertising 311*503a627eSMilanka Ringwaldprocess. 312*503a627eSMilanka Ringwald 313*503a627eSMilanka Ringwald### Private addresses. 314*503a627eSMilanka Ringwald 315*503a627eSMilanka RingwaldTo better protect privacy, an LE device can choose to use a private i.e. 316*503a627eSMilanka Ringwaldrandom Bluetooth address. This address changes at a user-specified rate. 317*503a627eSMilanka RingwaldTo allow for later reconnection, the central and peripheral devices will 318*503a627eSMilanka Ringwaldexchange their Identity Resolving Keys (IRKs) during bonding. The IRK is 319*503a627eSMilanka Ringwaldused to verify if a new address belongs to a previously bonded device. 320*503a627eSMilanka Ringwald 321*503a627eSMilanka RingwaldTo toggle privacy mode using private addresses, call the 322*503a627eSMilanka Ringwald*gap_random_address_set_mode* function. The update period can be set 323*503a627eSMilanka Ringwaldwith *gap_random_address_set_update_period*. 324*503a627eSMilanka Ringwald 325*503a627eSMilanka RingwaldAfter a connection is established, the Security Manager will try to 326*503a627eSMilanka Ringwaldresolve the peer Bluetooth address as explained in Section on 327*503a627eSMilanka Ringwald[SMP](protocols/#sec:smpProtocols). 328*503a627eSMilanka Ringwald 329*503a627eSMilanka Ringwald### Advertising and Discovery 330*503a627eSMilanka Ringwald 331*503a627eSMilanka RingwaldAn LE device is discoverable and connectable, only if it periodically 332*503a627eSMilanka Ringwaldsends out Advertisements. An advertisement contains up to 31 bytes of 333*503a627eSMilanka Ringwalddata. To configure and enable advertisement broadcast, the following GAP 334*503a627eSMilanka Ringwaldfunctions can be used: 335*503a627eSMilanka Ringwald 336*503a627eSMilanka Ringwald- *gap_advertisements_set_data* 337*503a627eSMilanka Ringwald 338*503a627eSMilanka Ringwald- *gap_advertisements_set_params* 339*503a627eSMilanka Ringwald 340*503a627eSMilanka Ringwald- *gap_advertisements_enable* 341*503a627eSMilanka Ringwald 342*503a627eSMilanka RingwaldIn addition to the Advertisement data, a device in the peripheral role 343*503a627eSMilanka Ringwaldcan also provide Scan Response data, which has to be explicitly queried 344*503a627eSMilanka Ringwaldby the central device. It can be set with *gap_scan_response_set_data*. 345*503a627eSMilanka Ringwald 346*503a627eSMilanka RingwaldPlease have a look at the [SPP and LE 347*503a627eSMilanka RingwaldCounter example](examples/examples/#sec:sppandlecounterExample). 348*503a627eSMilanka Ringwald 349*503a627eSMilanka RingwaldThe scan parameters can be set with 350*503a627eSMilanka Ringwald*gap_set_scan_parameters*. The scan can be started/stopped 351*503a627eSMilanka Ringwaldwith *gap_start_scan*/*gap_stop_scan*. 352*503a627eSMilanka Ringwald 353*503a627eSMilanka RingwaldFinally, if a suitable device is found, a connection can be initiated by 354*503a627eSMilanka Ringwaldcalling *gap_connect*. In contrast to Bluetooth classic, there 355*503a627eSMilanka Ringwaldis no timeout for an LE connection establishment. To cancel such an 356*503a627eSMilanka Ringwaldattempt, *gap_connect_cancel* has be be called. 357*503a627eSMilanka Ringwald 358*503a627eSMilanka RingwaldBy default, a Bluetooth device stops sending Advertisements when it gets 359*503a627eSMilanka Ringwaldinto the Connected state. However, it does not start broadcasting 360*503a627eSMilanka Ringwaldadvertisements on disconnect again. To re-enable it, please send the 361*503a627eSMilanka Ringwald*hci_le_set_advertise_enable* again . 362*503a627eSMilanka Ringwald 363*503a627eSMilanka Ringwald## GATT Client {#sec:GATTClientProfiles} 364*503a627eSMilanka Ringwald 365*503a627eSMilanka RingwaldThe GATT profile uses ATT Attributes to represent a hierarchical 366*503a627eSMilanka Ringwaldstructure of GATT Services and GATT Characteristics. Each Service has 367*503a627eSMilanka Ringwaldone or more Characteristics. Each Characteristic has meta data attached 368*503a627eSMilanka Ringwaldlike its type or its properties. This hierarchy of Characteristics and 369*503a627eSMilanka RingwaldServices are queried and modified via ATT operations. 370*503a627eSMilanka Ringwald 371*503a627eSMilanka RingwaldGATT defines both a server and a client role. A device can implement one 372*503a627eSMilanka Ringwaldor both GATT roles. 373*503a627eSMilanka Ringwald 374*503a627eSMilanka RingwaldThe GATT Client is used to discover services, characteristics 375*503a627eSMilanka Ringwaldand their descriptors on a peer device. It allows to subscribe for 376*503a627eSMilanka Ringwaldnotifications or indications that the characteristic on the GATT server 377*503a627eSMilanka Ringwaldhas changed its value. 378*503a627eSMilanka Ringwald 379*503a627eSMilanka RingwaldTo perform GATT queries, it provides a rich interface. Before calling 380*503a627eSMilanka Ringwaldqueries, the GATT client must be initialized with *gatt_client_init* 381*503a627eSMilanka Ringwaldonce. 382*503a627eSMilanka Ringwald 383*503a627eSMilanka RingwaldTo allow for modular profile implementations, GATT client can be used 384*503a627eSMilanka Ringwaldindependently by multiple entities. 385*503a627eSMilanka Ringwald 386*503a627eSMilanka RingwaldAfter an LE connection was created using the GAP LE API, you can query 387*503a627eSMilanka Ringwaldfor the connection MTU with *gatt_client_get_mtu*. 388*503a627eSMilanka Ringwald 389*503a627eSMilanka RingwaldMultiple GATT queries to the same GATT Server cannot be interleaved. 390*503a627eSMilanka RingwaldTherefore, you can either use a state machine or similar to perform the 391*503a627eSMilanka Ringwaldqueries in sequence, or you can check if you can perform a GATT query 392*503a627eSMilanka Ringwaldon a particular connection right now using 393*503a627eSMilanka Ringwald*gatt_client_is_ready*, and retry later if it is not ready. 394*503a627eSMilanka RingwaldAs a result to a GATT query, zero to many 395*503a627eSMilanka Ringwald*GATT_EVENT_X*s are returned before a *GATT_EVENT_QUERY_COMPLETE* event 396*503a627eSMilanka Ringwaldcompletes the query. 397*503a627eSMilanka Ringwald 398*503a627eSMilanka RingwaldFor more details on the available GATT queries, please consult 399*503a627eSMilanka Ringwald[GATT Client API](#sec:gattClientAPIAppendix). 400*503a627eSMilanka Ringwald 401*503a627eSMilanka Ringwald### Authentication 402*503a627eSMilanka Ringwald 403*503a627eSMilanka RingwaldBy default, the GATT Server is responsible for security and the GATT Client does not enforce any kind of authentication. 404*503a627eSMilanka RingwaldIf the GATT Client accesses Characteristic that require encrytion or authentication, the remote GATT Server will return an error, 405*503a627eSMilanka Ringwaldwhich is returned in the *att status* of the *GATT_EVENT_QUERY_COMPLETE*. 406*503a627eSMilanka Ringwald 407*503a627eSMilanka RingwaldYou can define *ENABLE_GATT_CLIENT_PAIRING* to instruct the GATT Client to trigger pairing in this case and to repeat the request. 408*503a627eSMilanka Ringwald 409*503a627eSMilanka RingwaldThis model allows for an attacker to spoof another device, but don't require authentication for the Characteristics. 410*503a627eSMilanka RingwaldAs a first improvement, you can define *ENABLE_LE_PROACTIVE_AUTHENTICATION* in *btstack_config.h*. When defined, the GATT Client will 411*503a627eSMilanka Ringwaldrequest the Security Manager to re-encrypt the connection if there is stored bonding information available. 412*503a627eSMilanka RingwaldIf this fails, the *GATT_EVENT_QUERY_COMPLETE* will return with the att status *ATT_ERROR_BONDING_INFORMATION_MISSING*. 413*503a627eSMilanka Ringwald 414*503a627eSMilanka RingwaldWith *ENABLE_LE_PROACTIVE_AUTHENTICATION* defined and in Central role, you need to delete the local bonding information if the remote 415*503a627eSMilanka Ringwaldlost its bonding information, e.g. because of a device reset. See *example/sm_pairing_central.c*. 416*503a627eSMilanka Ringwald 417*503a627eSMilanka RingwaldEven with the Proactive Authentication, your device may still connect to an attacker that provides the same advertising data as 418*503a627eSMilanka Ringwaldyour actual device. If the device that you want to connect requires pairing, you can instruct the GATT Client to automatically 419*503a627eSMilanka Ringwaldrequest an encrypted connection before sending any GATT Client request by calling *gatt_client_set_required_security_level()*. 420*503a627eSMilanka RingwaldIf the device provides sufficient IO capabilities, a MITM attack can then be prevented. We call this 'Mandatory Authentication'. 421*503a627eSMilanka Ringwald 422*503a627eSMilanka RingwaldThe following diagrams provide a detailed overview about the GATT Client security mechanisms in different configurations: 423*503a627eSMilanka Ringwald 424*503a627eSMilanka Ringwald- [Reactive Authentication as Central](picts/gatt_client_security_reactive_authentication_central.svg) 425*503a627eSMilanka Ringwald- [Reactive Authentication as Peripheral](picts/gatt_client_security_reactive_authentication_peripheral.svg) 426*503a627eSMilanka Ringwald- [Proactive Authentication as Central](picts/gatt_client_security_proactive_authentication_central.svg) 427*503a627eSMilanka Ringwald- [Proactive Authentication as Peripheral](picts/gatt_client_security_proactive_authentication_peripheral.svg) 428*503a627eSMilanka Ringwald- [Mandatory Authentication as Central](picts/gatt_client_security_mandatory_authentication_central.svg) 429*503a627eSMilanka Ringwald- [Mandatory Authentication as Peripheral](picts/gatt_client_security_mandatory_authentication_peripheral.svg) 430*503a627eSMilanka Ringwald 431*503a627eSMilanka Ringwald## GATT Server {#sec:GATTServerProfiles} 432*503a627eSMilanka Ringwald 433*503a627eSMilanka RingwaldThe GATT server stores data and accepts GATT client requests, commands 434*503a627eSMilanka Ringwaldand confirmations. The GATT server sends responses to requests and when 435*503a627eSMilanka Ringwaldconfigured, sends indication and notifications asynchronously to the 436*503a627eSMilanka RingwaldGATT client. 437*503a627eSMilanka Ringwald 438*503a627eSMilanka RingwaldTo save on both code space and memory, BTstack does not provide a GATT 439*503a627eSMilanka RingwaldServer implementation. Instead, a textual description of the GATT 440*503a627eSMilanka Ringwaldprofile is directly converted into a compact internal ATT Attribute 441*503a627eSMilanka Ringwalddatabase by a GATT profile compiler. The ATT protocol server - 442*503a627eSMilanka Ringwaldimplemented by and - answers incoming ATT requests based on information 443*503a627eSMilanka Ringwaldprovided in the compiled database and provides read- and write-callbacks 444*503a627eSMilanka Ringwaldfor dynamic attributes. 445*503a627eSMilanka Ringwald 446*503a627eSMilanka RingwaldGATT profiles are defined by a simple textual comma separated value 447*503a627eSMilanka Ringwald(.csv) representation. While the description is easy to read and edit, 448*503a627eSMilanka Ringwaldit is compact and can be placed in ROM. 449*503a627eSMilanka Ringwald 450*503a627eSMilanka RingwaldThe current format is shown in Listing [below](#lst:GATTServerProfile). 451*503a627eSMilanka Ringwald 452*503a627eSMilanka Ringwald~~~~ {#lst:GATTServerProfile .c caption="{GATT profile.}"} 453*503a627eSMilanka Ringwald // import service_name 454*503a627eSMilanka Ringwald #import <service_name.gatt> 455*503a627eSMilanka Ringwald 456*503a627eSMilanka Ringwald PRIMARY_SERVICE, {SERVICE_UUID} 457*503a627eSMilanka Ringwald CHARACTERISTIC, {ATTRIBUTE_TYPE_UUID}, {PROPERTIES}, {VALUE} 458*503a627eSMilanka Ringwald CHARACTERISTIC, {ATTRIBUTE_TYPE_UUID}, {PROPERTIES}, {VALUE} 459*503a627eSMilanka Ringwald ... 460*503a627eSMilanka Ringwald PRIMARY_SERVICE, {SERVICE_UUID} 461*503a627eSMilanka Ringwald CHARACTERISTIC, {ATTRIBUTE_TYPE_UUID}, {PROPERTIES}, {VALUE} 462*503a627eSMilanka Ringwald ... 463*503a627eSMilanka Ringwald~~~~ 464*503a627eSMilanka Ringwald 465*503a627eSMilanka RingwaldUUIDs are either 16 bit (1800) or 128 bit 466*503a627eSMilanka Ringwald(00001234-0000-1000-8000-00805F9B34FB). 467*503a627eSMilanka Ringwald 468*503a627eSMilanka RingwaldValue can either be a string (“this is a string”), or, a sequence of hex 469*503a627eSMilanka Ringwaldbytes (e.g. 01 02 03). 470*503a627eSMilanka Ringwald 471*503a627eSMilanka RingwaldProperties can be a list of properties combined using '|' 472*503a627eSMilanka Ringwald 473*503a627eSMilanka RingwaldReads/writes to a Characteristic that is defined with the DYNAMIC flag, 474*503a627eSMilanka Ringwaldare forwarded to the application via callback. Otherwise, the 475*503a627eSMilanka RingwaldCharacteristics cannot be written and it will return the specified 476*503a627eSMilanka Ringwaldconstant value. 477*503a627eSMilanka Ringwald 478*503a627eSMilanka RingwaldAdding NOTIFY and/or INDICATE automatically creates an addition Client 479*503a627eSMilanka RingwaldConfiguration Characteristic. 480*503a627eSMilanka Ringwald 481*503a627eSMilanka RingwaldProperty | Meaning 482*503a627eSMilanka Ringwald------------------------|----------------------------------------------- 483*503a627eSMilanka RingwaldREAD | Characteristic can be read 484*503a627eSMilanka RingwaldWRITE | Characteristic can be written using Write Request 485*503a627eSMilanka RingwaldWRITE_WITHOUT_RESPONSE | Characteristic can be written using Write Command 486*503a627eSMilanka RingwaldNOTIFY | Characteristic allows notifications by server 487*503a627eSMilanka RingwaldINDICATE | Characteristic allows indication by server 488*503a627eSMilanka RingwaldDYNAMIC | Read or writes to Characteristic are handled by application 489*503a627eSMilanka Ringwald 490*503a627eSMilanka RingwaldTo require encryption or authentication before a Characteristic can be 491*503a627eSMilanka Ringwaldaccessed, you can add one or more of the following properties: 492*503a627eSMilanka Ringwald 493*503a627eSMilanka RingwaldProperty | Meaning 494*503a627eSMilanka Ringwald------------------------|----------------------------------------------- 495*503a627eSMilanka RingwaldAUTHENTICATION_REQUIRED | Read and Write operations require Authentication 496*503a627eSMilanka RingwaldREAD_ENCRYPTED | Read operations require Encryption 497*503a627eSMilanka RingwaldREAD_AUTHENTICATED | Read operations require Authentication 498*503a627eSMilanka RingwaldWRITE_ENCRYPTED | Write operations require Encryption 499*503a627eSMilanka RingwaldWRITE_AUTHENTICATED | Write operations require Authentication 500*503a627eSMilanka RingwaldENCRYPTION_KEY_SIZE_X | Require encryption size >= X, with W in [7..16] 501*503a627eSMilanka Ringwald 502*503a627eSMilanka RingwaldTo use already implemented GATT Services, you can import it 503*503a627eSMilanka Ringwaldusing the *#import <service_name.gatt>* command. See [list of provided services](gatt_services.md). 504*503a627eSMilanka Ringwald 505*503a627eSMilanka RingwaldBTstack only provides an ATT Server, while the GATT Server logic is 506*503a627eSMilanka Ringwaldmainly provided by the GATT compiler. While GATT identifies 507*503a627eSMilanka RingwaldCharacteristics by UUIDs, ATT uses Handles (16 bit values). To allow to 508*503a627eSMilanka Ringwaldidentify a Characteristic without hard-coding the attribute ID, the GATT 509*503a627eSMilanka Ringwaldcompiler creates a list of defines in the generated \*.h file. 510*503a627eSMilanka Ringwald 511*503a627eSMilanka RingwaldSimilar to other protocols, it might be not possible to send any time. 512*503a627eSMilanka RingwaldTo send a Notification, you can call *att_server_request_can_send_now* 513*503a627eSMilanka Ringwaldto receive a ATT_EVENT_CAN_SEND_NOW event. 514*503a627eSMilanka Ringwald 515*503a627eSMilanka RingwaldIf your application cannot handle an ATT Read Request in the *att_read_callback* 516*503a627eSMilanka Ringwaldin some situations, you can enable support for this by adding ENABLE_ATT_DELAYED_RESPONSE 517*503a627eSMilanka Ringwaldto *btstack_config.h*. Now, you can store the requested attribute handle and return 518*503a627eSMilanka Ringwald*ATT_READ_RESPONSE_PENDING* instead of the length of the provided data when you don't have the data ready. 519*503a627eSMilanka RingwaldFor ATT operations that read more than one attribute, your *att_read_callback* 520*503a627eSMilanka Ringwaldmight get called multiple times as well. To let you know that all necessary 521*503a627eSMilanka Ringwaldattribute handles have been 'requested' by the *att_server*, you'll get a final 522*503a627eSMilanka Ringwald*att_read_callback* with the attribute handle of *ATT_READ_RESPONSE_PENDING*. 523*503a627eSMilanka RingwaldWhen you've got the data for all requested attributes ready, you can call 524*503a627eSMilanka Ringwald*att_server_response_ready*, which will trigger processing of the current request. 525*503a627eSMilanka RingwaldPlease keep in mind that there is only one active ATT operation and that it has a 30 second 526*503a627eSMilanka Ringwaldtimeout after which the ATT server is considered defunct by the GATT Client. 527*503a627eSMilanka Ringwald 528*503a627eSMilanka Ringwald### Implementing Standard GATT Services {#sec:GATTStandardServices} 529*503a627eSMilanka Ringwald 530*503a627eSMilanka RingwaldImplementation of a standard GATT Service consists of the following 4 steps: 531*503a627eSMilanka Ringwald 532*503a627eSMilanka Ringwald 1. Identify full Service Name 533*503a627eSMilanka Ringwald 2. Use Service Name to fetch XML definition at Bluetooth SIG site and convert into generic .gatt file 534*503a627eSMilanka Ringwald 3. Edit .gatt file to set constant values and exclude unwanted Characteristics 535*503a627eSMilanka Ringwald 4. Implement Service server, e.g., battery_service_server.c 536*503a627eSMilanka Ringwald 537*503a627eSMilanka RingwaldStep 1: 538*503a627eSMilanka Ringwald 539*503a627eSMilanka RingwaldTo facilitate the creation of .gatt files for standard profiles defined by the Bluetooth SIG, 540*503a627eSMilanka Ringwaldthe *tool/convert_gatt_service.py* script can be used. When run without a parameter, it queries the 541*503a627eSMilanka RingwaldBluetooth SIG website and lists the available Services by their Specification Name, e.g., 542*503a627eSMilanka Ringwald*org.bluetooth.service.battery_service*. 543*503a627eSMilanka Ringwald 544*503a627eSMilanka Ringwald $ tool/convert_gatt_service.py 545*503a627eSMilanka Ringwald Fetching list of services from https://www.bluetooth.com/specifications/gatt/services 546*503a627eSMilanka Ringwald 547*503a627eSMilanka Ringwald Specification Type | Specification Name | UUID 548*503a627eSMilanka Ringwald -------------------------------------------------------+-------------------------------+----------- 549*503a627eSMilanka Ringwald org.bluetooth.service.alert_notification | Alert Notification Service | 0x1811 550*503a627eSMilanka Ringwald org.bluetooth.service.automation_io | Automation IO | 0x1815 551*503a627eSMilanka Ringwald org.bluetooth.service.battery_service | Battery Service | 0x180F 552*503a627eSMilanka Ringwald ... 553*503a627eSMilanka Ringwald org.bluetooth.service.weight_scale | Weight Scale | 0x181D 554*503a627eSMilanka Ringwald 555*503a627eSMilanka Ringwald To convert a service into a .gatt file template, please call the script again with the requested Specification Type and the output file name 556*503a627eSMilanka Ringwald Usage: tool/convert_gatt_service.py SPECIFICATION_TYPE [service_name.gatt] 557*503a627eSMilanka Ringwald 558*503a627eSMilanka RingwaldStep 2: 559*503a627eSMilanka Ringwald 560*503a627eSMilanka RingwaldTo convert service into .gatt file, call *tool/convert_gatt_service.py with the requested Specification Type and the output file name. 561*503a627eSMilanka Ringwald 562*503a627eSMilanka Ringwald $ tool/convert_gatt_service.py org.bluetooth.service.battery_service battery_service.gatt 563*503a627eSMilanka Ringwald Fetching org.bluetooth.service.battery_service from 564*503a627eSMilanka Ringwald https://www.bluetooth.com/api/gatt/xmlfile?xmlFileName=org.bluetooth.service.battery_service.xml 565*503a627eSMilanka Ringwald 566*503a627eSMilanka Ringwald Service Battery Service 567*503a627eSMilanka Ringwald - Characteristic Battery Level - properties ['Read', 'Notify'] 568*503a627eSMilanka Ringwald -- Descriptor Characteristic Presentation Format - TODO: Please set values 569*503a627eSMilanka Ringwald -- Descriptor Client Characteristic Configuration 570*503a627eSMilanka Ringwald 571*503a627eSMilanka Ringwald Service successfully converted into battery_service.gatt 572*503a627eSMilanka Ringwald Please check for TODOs in the .gatt file 573*503a627eSMilanka Ringwald 574*503a627eSMilanka Ringwald 575*503a627eSMilanka RingwaldStep 3: 576*503a627eSMilanka Ringwald 577*503a627eSMilanka RingwaldIn most cases, you will need to customize the .gatt file. Please pay attention to the tool output and have a look 578*503a627eSMilanka Ringwaldat the generated .gatt file. 579*503a627eSMilanka Ringwald 580*503a627eSMilanka RingwaldE.g. in the generated .gatt file for the Battery Service 581*503a627eSMilanka Ringwald 582*503a627eSMilanka Ringwald // Specification Type org.bluetooth.service.battery_service 583*503a627eSMilanka Ringwald // https://www.bluetooth.com/api/gatt/xmlfile?xmlFileName=org.bluetooth.service.battery_service.xml 584*503a627eSMilanka Ringwald 585*503a627eSMilanka Ringwald // Battery Service 180F 586*503a627eSMilanka Ringwald PRIMARY_SERVICE, ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE 587*503a627eSMilanka Ringwald CHARACTERISTIC, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL, DYNAMIC | READ | NOTIFY, 588*503a627eSMilanka Ringwald // TODO: Characteristic Presentation Format: please set values 589*503a627eSMilanka Ringwald #TODO CHARACTERISTIC_FORMAT, READ, _format_, _exponent_, _unit_, _name_space_, _description_ 590*503a627eSMilanka Ringwald CLIENT_CHARACTERISTIC_CONFIGURATION, READ | WRITE, 591*503a627eSMilanka Ringwald 592*503a627eSMilanka Ringwaldyou could delete the line regarding the CHARACTERISTIC_FORMAT, since it's not required if there is a single instance of the service. 593*503a627eSMilanka RingwaldPlease compare the .gatt file against the [Adopted Specifications](https://www.bluetooth.com/specifications/adopted-specifications). 594*503a627eSMilanka Ringwald 595*503a627eSMilanka RingwaldStep 4: 596*503a627eSMilanka Ringwald 597*503a627eSMilanka RingwaldAs described [above](#sec:GATTServerProfiles) all read/write requests are handled by the application. 598*503a627eSMilanka RingwaldTo implement the new services as a reusable module, it's necessary to get access to all read/write requests related to this service. 599*503a627eSMilanka Ringwald 600*503a627eSMilanka RingwaldFor this, the ATT DB allows to register read/write callbacks for a specific handle range with *att_server_register_can_send_now_callback()*. 601*503a627eSMilanka Ringwald 602*503a627eSMilanka RingwaldSince the handle range depends on the application's .gatt file, the handle range for Primary and Secondary Services can be queried with *gatt_server_get_get_handle_range_for_service_with_uuid16*. 603*503a627eSMilanka Ringwald 604*503a627eSMilanka RingwaldSimilarly, you will need to know the attribute handle for particular Characteristics to handle Characteristic read/writes requests. You can get the attribute value handle for a Characteristics *gatt_server_get_value_handle_for_characteristic_with_uuid16()*. 605*503a627eSMilanka Ringwald 606*503a627eSMilanka RingwaldIn addition to the attribute value handle, the handle for the Client Characteristic Configuration is needed to support Indications/Notifications. You can get this attribute handle with *gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16()* 607*503a627eSMilanka Ringwald 608*503a627eSMilanka RingwaldFinally, in order to send Notifications and Indications independently from the main application, *att_server_register_can_send_now_callback* can be used to request a callback when it's possible to send a Notification or Indication. 609*503a627eSMilanka Ringwald 610*503a627eSMilanka RingwaldTo see how this works together, please check out the Battery Service Server in *src/ble/battery_service_server.c*. 611*503a627eSMilanka Ringwald 612*503a627eSMilanka Ringwald### GATT Database Hash 613*503a627eSMilanka Ringwald 614*503a627eSMilanka RingwaldWhen a GATT Client connects to a GATT Server, it cannot know if the GATT Database has changed 615*503a627eSMilanka Ringwaldand has to discover the provided GATT Services and Characteristics after each connect. 616*503a627eSMilanka Ringwald 617*503a627eSMilanka RingwaldTo speed this up, the Bluetooth 618*503a627eSMilanka Ringwaldspecification defines a GATT Service Changed Characteristic, with the idea that a GATT Server would notify 619*503a627eSMilanka Ringwalda bonded GATT Client if its database changed. However, this is quite fragile and it is not clear how it can be implemented 620*503a627eSMilanka Ringwaldin a robust way. 621*503a627eSMilanka Ringwald 622*503a627eSMilanka RingwaldThe Bluetooth Core Spec 5.1 introduced the GATT Database Hash Characteristic, which allows for a simple 623*503a627eSMilanka Ringwaldrobust mechanism to cache a remote GATT Database. The GATT Database Hash is a 16-byte value that is calculated 624*503a627eSMilanka Ringwaldover the list of Services and Characteristics. If there is any change to the database, the hash will change as well. 625*503a627eSMilanka Ringwald 626*503a627eSMilanka RingwaldTo support this on the GATT Server, you only need to add a GATT Service with the GATT Database Characteristic to your .gatt file. 627*503a627eSMilanka RingwaldThe hash value is then calculated by the GATT compiler. 628*503a627eSMilanka Ringwald 629*503a627eSMilanka Ringwald 630*503a627eSMilanka Ringwald PRIMARY_SERVICE, GATT_SERVICE 631*503a627eSMilanka Ringwald CHARACTERISTIC, GATT_DATABASE_HASH, READ, 632*503a627eSMilanka Ringwald 633*503a627eSMilanka RingwaldNote: make sure to install the PyCryptodome python package as the hash is calculated using AES-CMAC, 634*503a627eSMilanka Ringwalde.g. with: 635*503a627eSMilanka Ringwald 636*503a627eSMilanka Ringwald pip install pycryptodomex 637