xref: /btstack/example/spp_and_gatt_counter.c (revision bdc352b16da8ddb06e0abca313290fcc1e06a43b)
1*bdc352b1SMatthias Ringwald /*
2*bdc352b1SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*bdc352b1SMatthias Ringwald  *
4*bdc352b1SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*bdc352b1SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*bdc352b1SMatthias Ringwald  * are met:
7*bdc352b1SMatthias Ringwald  *
8*bdc352b1SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*bdc352b1SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*bdc352b1SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*bdc352b1SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*bdc352b1SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*bdc352b1SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*bdc352b1SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*bdc352b1SMatthias Ringwald  *    from this software without specific prior written permission.
16*bdc352b1SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*bdc352b1SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*bdc352b1SMatthias Ringwald  *    monetary gain.
19*bdc352b1SMatthias Ringwald  *
20*bdc352b1SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*bdc352b1SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*bdc352b1SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*bdc352b1SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*bdc352b1SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*bdc352b1SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*bdc352b1SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*bdc352b1SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*bdc352b1SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*bdc352b1SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*bdc352b1SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*bdc352b1SMatthias Ringwald  * SUCH DAMAGE.
32*bdc352b1SMatthias Ringwald  *
33*bdc352b1SMatthias Ringwald  * Please inquire about commercial licensing options at
34*bdc352b1SMatthias Ringwald  * [email protected]
35*bdc352b1SMatthias Ringwald  *
36*bdc352b1SMatthias Ringwald  */
37*bdc352b1SMatthias Ringwald 
38*bdc352b1SMatthias Ringwald #define BTSTACK_FILE__ "spp_and_gatt_counter.c"
39*bdc352b1SMatthias Ringwald 
40*bdc352b1SMatthias Ringwald // *****************************************************************************
41*bdc352b1SMatthias Ringwald /* EXAMPLE_START(spp_and_le_counter): Dual mode example
42*bdc352b1SMatthias Ringwald  *
43*bdc352b1SMatthias Ringwald  * @text The SPP and LE Counter example combines the Bluetooth Classic SPP Counter
44*bdc352b1SMatthias Ringwald  * and the Bluetooth LE Counter into a single application.
45*bdc352b1SMatthias Ringwald  *
46*bdc352b1SMatthias Ringwald  * @text In this Section, we only point out the differences to the individual examples
47*bdc352b1SMatthias Ringwald  * and how the stack is configured.
48*bdc352b1SMatthias Ringwald  *
49*bdc352b1SMatthias Ringwald  * @text Note: To test, please run the example, and then:
50*bdc352b1SMatthias Ringwald  *    - for SPP pair from a remote device, and open the Virtual Serial Port,
51*bdc352b1SMatthias Ringwald  *    - for LE use some GATT Explorer, e.g. LightBlue, BLExplr, to enable notifications.
52*bdc352b1SMatthias Ringwald  */
53*bdc352b1SMatthias Ringwald // *****************************************************************************
54*bdc352b1SMatthias Ringwald 
55*bdc352b1SMatthias Ringwald #include <stdint.h>
56*bdc352b1SMatthias Ringwald #include <stdio.h>
57*bdc352b1SMatthias Ringwald #include <stdlib.h>
58*bdc352b1SMatthias Ringwald #include <string.h>
59*bdc352b1SMatthias Ringwald #include <inttypes.h>
60*bdc352b1SMatthias Ringwald 
61*bdc352b1SMatthias Ringwald #include "btstack.h"
62*bdc352b1SMatthias Ringwald #include "spp_and_gatt_counter.h"
63*bdc352b1SMatthias Ringwald 
64*bdc352b1SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1
65*bdc352b1SMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000
66*bdc352b1SMatthias Ringwald 
67*bdc352b1SMatthias Ringwald static uint16_t  rfcomm_channel_id;
68*bdc352b1SMatthias Ringwald static uint8_t   spp_service_buffer[150];
69*bdc352b1SMatthias Ringwald static int       le_notification_enabled;
70*bdc352b1SMatthias Ringwald static hci_con_handle_t att_con_handle;
71*bdc352b1SMatthias Ringwald 
72*bdc352b1SMatthias Ringwald // THE Couner
73*bdc352b1SMatthias Ringwald static btstack_timer_source_t heartbeat;
74*bdc352b1SMatthias Ringwald static int  counter = 0;
75*bdc352b1SMatthias Ringwald static char counter_string[30];
76*bdc352b1SMatthias Ringwald static int  counter_string_len;
77*bdc352b1SMatthias Ringwald 
78*bdc352b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
79*bdc352b1SMatthias Ringwald 
80*bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
81*bdc352b1SMatthias Ringwald static uint8_t gatt_service_buffer[70];
82*bdc352b1SMatthias Ringwald #endif
83*bdc352b1SMatthias Ringwald 
84*bdc352b1SMatthias Ringwald /*
85*bdc352b1SMatthias Ringwald  * @section Advertisements
86*bdc352b1SMatthias Ringwald  *
87*bdc352b1SMatthias Ringwald  * @text The Flags attribute in the Advertisement Data indicates if a device is in dual-mode or not.
88*bdc352b1SMatthias Ringwald  * Flag 0x06 indicates LE General Discoverable, BR/EDR not supported although we're actually using BR/EDR.
89*bdc352b1SMatthias Ringwald  * In the past, there have been problems with Anrdoid devices when the flag was not set.
90*bdc352b1SMatthias Ringwald  * Setting it should prevent the remote implementation to try to use GATT over LE/EDR, which is not
91*bdc352b1SMatthias Ringwald  * implemented by BTstack. So, setting the flag seems like the safer choice (while it's technically incorrect).
92*bdc352b1SMatthias Ringwald  */
93*bdc352b1SMatthias Ringwald /* LISTING_START(advertisements): Advertisement data: Flag 0x06 indicates LE-only device */
94*bdc352b1SMatthias Ringwald const uint8_t adv_data[] = {
95*bdc352b1SMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
96*bdc352b1SMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
97*bdc352b1SMatthias Ringwald     // Name
98*bdc352b1SMatthias Ringwald     0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r',
99*bdc352b1SMatthias Ringwald     // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
100*bdc352b1SMatthias Ringwald     0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff,
101*bdc352b1SMatthias Ringwald };
102*bdc352b1SMatthias Ringwald /* LISTING_END */
103*bdc352b1SMatthias Ringwald uint8_t adv_data_len = sizeof(adv_data);
104*bdc352b1SMatthias Ringwald 
105*bdc352b1SMatthias Ringwald 
106*bdc352b1SMatthias Ringwald /*
107*bdc352b1SMatthias Ringwald  * @section Packet Handler
108*bdc352b1SMatthias Ringwald  *
109*bdc352b1SMatthias Ringwald  * @text The packet handler of the combined example is just the combination of the individual packet handlers.
110*bdc352b1SMatthias Ringwald  */
111*bdc352b1SMatthias Ringwald 
112*bdc352b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
113*bdc352b1SMatthias Ringwald     UNUSED(channel);
114*bdc352b1SMatthias Ringwald 
115*bdc352b1SMatthias Ringwald     bd_addr_t event_addr;
116*bdc352b1SMatthias Ringwald     uint8_t   rfcomm_channel_nr;
117*bdc352b1SMatthias Ringwald     uint16_t  mtu;
118*bdc352b1SMatthias Ringwald     int i;
119*bdc352b1SMatthias Ringwald 
120*bdc352b1SMatthias Ringwald 	switch (packet_type) {
121*bdc352b1SMatthias Ringwald 		case HCI_EVENT_PACKET:
122*bdc352b1SMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
123*bdc352b1SMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
124*bdc352b1SMatthias Ringwald                     // inform about pin code request
125*bdc352b1SMatthias Ringwald                     printf("Pin code request - using '0000'\n");
126*bdc352b1SMatthias Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
127*bdc352b1SMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
128*bdc352b1SMatthias Ringwald                     break;
129*bdc352b1SMatthias Ringwald 
130*bdc352b1SMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
131*bdc352b1SMatthias Ringwald                     // inform about user confirmation request
132*bdc352b1SMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
133*bdc352b1SMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
134*bdc352b1SMatthias Ringwald                     break;
135*bdc352b1SMatthias Ringwald 
136*bdc352b1SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
137*bdc352b1SMatthias Ringwald                     le_notification_enabled = 0;
138*bdc352b1SMatthias Ringwald                     break;
139*bdc352b1SMatthias Ringwald 
140*bdc352b1SMatthias Ringwald                 case ATT_EVENT_CAN_SEND_NOW:
141*bdc352b1SMatthias Ringwald                     att_server_notify(att_con_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len);
142*bdc352b1SMatthias Ringwald                     break;
143*bdc352b1SMatthias Ringwald 
144*bdc352b1SMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
145*bdc352b1SMatthias Ringwald 					// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
146*bdc352b1SMatthias Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
147*bdc352b1SMatthias Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
148*bdc352b1SMatthias Ringwald                     rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
149*bdc352b1SMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
150*bdc352b1SMatthias Ringwald                     rfcomm_accept_connection(rfcomm_channel_id);
151*bdc352b1SMatthias Ringwald 					break;
152*bdc352b1SMatthias Ringwald 
153*bdc352b1SMatthias Ringwald 				case RFCOMM_EVENT_CHANNEL_OPENED:
154*bdc352b1SMatthias Ringwald 					// data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
155*bdc352b1SMatthias Ringwald 					if (rfcomm_event_channel_opened_get_status(packet)) {
156*bdc352b1SMatthias Ringwald                         printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
157*bdc352b1SMatthias Ringwald                     } else {
158*bdc352b1SMatthias Ringwald                         rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
159*bdc352b1SMatthias Ringwald                         mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
160*bdc352b1SMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
161*bdc352b1SMatthias Ringwald                     }
162*bdc352b1SMatthias Ringwald 					break;
163*bdc352b1SMatthias Ringwald 
164*bdc352b1SMatthias Ringwald                 case RFCOMM_EVENT_CAN_SEND_NOW:
165*bdc352b1SMatthias Ringwald                     rfcomm_send(rfcomm_channel_id, (uint8_t*) counter_string, counter_string_len);
166*bdc352b1SMatthias Ringwald                     break;
167*bdc352b1SMatthias Ringwald 
168*bdc352b1SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
169*bdc352b1SMatthias Ringwald                     printf("RFCOMM channel closed\n");
170*bdc352b1SMatthias Ringwald                     rfcomm_channel_id = 0;
171*bdc352b1SMatthias Ringwald                     break;
172*bdc352b1SMatthias Ringwald 
173*bdc352b1SMatthias Ringwald                 default:
174*bdc352b1SMatthias Ringwald                     break;
175*bdc352b1SMatthias Ringwald 			}
176*bdc352b1SMatthias Ringwald             break;
177*bdc352b1SMatthias Ringwald 
178*bdc352b1SMatthias Ringwald         case RFCOMM_DATA_PACKET:
179*bdc352b1SMatthias Ringwald             printf("RCV: '");
180*bdc352b1SMatthias Ringwald             for (i=0;i<size;i++){
181*bdc352b1SMatthias Ringwald                 putchar(packet[i]);
182*bdc352b1SMatthias Ringwald             }
183*bdc352b1SMatthias Ringwald             printf("'\n");
184*bdc352b1SMatthias Ringwald             break;
185*bdc352b1SMatthias Ringwald 
186*bdc352b1SMatthias Ringwald         default:
187*bdc352b1SMatthias Ringwald             break;
188*bdc352b1SMatthias Ringwald 	}
189*bdc352b1SMatthias Ringwald }
190*bdc352b1SMatthias Ringwald 
191*bdc352b1SMatthias Ringwald // ATT Client Read Callback for Dynamic Data
192*bdc352b1SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
193*bdc352b1SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
194*bdc352b1SMatthias Ringwald // @param offset defines start of attribute value
195*bdc352b1SMatthias Ringwald static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
196*bdc352b1SMatthias Ringwald     UNUSED(con_handle);
197*bdc352b1SMatthias Ringwald 
198*bdc352b1SMatthias Ringwald     if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){
199*bdc352b1SMatthias Ringwald         return att_read_callback_handle_blob((const uint8_t *)counter_string, buffer_size, offset, buffer, buffer_size);
200*bdc352b1SMatthias Ringwald     }
201*bdc352b1SMatthias Ringwald     return 0;
202*bdc352b1SMatthias Ringwald }
203*bdc352b1SMatthias Ringwald 
204*bdc352b1SMatthias Ringwald // write requests
205*bdc352b1SMatthias Ringwald static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
206*bdc352b1SMatthias Ringwald     // ignore cancel sent for new connections
207*bdc352b1SMatthias Ringwald     if (transaction_mode == ATT_TRANSACTION_MODE_CANCEL) return 0;
208*bdc352b1SMatthias Ringwald     // find characteristic for handle
209*bdc352b1SMatthias Ringwald     switch (att_handle){
210*bdc352b1SMatthias Ringwald         case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
211*bdc352b1SMatthias Ringwald             le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
212*bdc352b1SMatthias Ringwald             att_con_handle = con_handle;
213*bdc352b1SMatthias Ringwald             return 0;
214*bdc352b1SMatthias Ringwald         case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
215*bdc352b1SMatthias Ringwald             printf("Write on test characteristic: ");
216*bdc352b1SMatthias Ringwald             printf_hexdump(buffer, buffer_size);
217*bdc352b1SMatthias Ringwald             return 0;
218*bdc352b1SMatthias Ringwald         default:
219*bdc352b1SMatthias Ringwald             printf("WRITE Callback, handle %04x, mode %u, offset %u, data: ", con_handle, transaction_mode, offset);
220*bdc352b1SMatthias Ringwald             printf_hexdump(buffer, buffer_size);
221*bdc352b1SMatthias Ringwald             return 0;
222*bdc352b1SMatthias Ringwald     }
223*bdc352b1SMatthias Ringwald }
224*bdc352b1SMatthias Ringwald 
225*bdc352b1SMatthias Ringwald static void beat(void){
226*bdc352b1SMatthias Ringwald     counter++;
227*bdc352b1SMatthias Ringwald     counter_string_len = sprintf(counter_string, "BTstack counter %04u", counter);
228*bdc352b1SMatthias Ringwald     puts(counter_string);
229*bdc352b1SMatthias Ringwald }
230*bdc352b1SMatthias Ringwald 
231*bdc352b1SMatthias Ringwald /*
232*bdc352b1SMatthias Ringwald  * @section Heartbeat Handler
233*bdc352b1SMatthias Ringwald  *
234*bdc352b1SMatthias Ringwald  * @text Similar to the packet handler, the heartbeat handler is the combination of the individual ones.
235*bdc352b1SMatthias Ringwald  * After updating the counter, it requests an ATT_EVENT_CAN_SEND_NOW and/or RFCOMM_EVENT_CAN_SEND_NOW
236*bdc352b1SMatthias Ringwald  */
237*bdc352b1SMatthias Ringwald 
238*bdc352b1SMatthias Ringwald  /* LISTING_START(heartbeat): Combined Heartbeat handler */
239*bdc352b1SMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){
240*bdc352b1SMatthias Ringwald 
241*bdc352b1SMatthias Ringwald     if (rfcomm_channel_id || le_notification_enabled) {
242*bdc352b1SMatthias Ringwald         beat();
243*bdc352b1SMatthias Ringwald     }
244*bdc352b1SMatthias Ringwald 
245*bdc352b1SMatthias Ringwald     if (rfcomm_channel_id){
246*bdc352b1SMatthias Ringwald         rfcomm_request_can_send_now_event(rfcomm_channel_id);
247*bdc352b1SMatthias Ringwald     }
248*bdc352b1SMatthias Ringwald 
249*bdc352b1SMatthias Ringwald     if (le_notification_enabled) {
250*bdc352b1SMatthias Ringwald         att_server_request_can_send_now_event(att_con_handle);
251*bdc352b1SMatthias Ringwald     }
252*bdc352b1SMatthias Ringwald 
253*bdc352b1SMatthias Ringwald     btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
254*bdc352b1SMatthias Ringwald     btstack_run_loop_add_timer(ts);
255*bdc352b1SMatthias Ringwald }
256*bdc352b1SMatthias Ringwald /* LISTING_END */
257*bdc352b1SMatthias Ringwald 
258*bdc352b1SMatthias Ringwald /*
259*bdc352b1SMatthias Ringwald  * @section Main Application Setup
260*bdc352b1SMatthias Ringwald  *
261*bdc352b1SMatthias Ringwald  * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
262*bdc352b1SMatthias Ringwald  */
263*bdc352b1SMatthias Ringwald 
264*bdc352b1SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */
265*bdc352b1SMatthias Ringwald int btstack_main(void);
266*bdc352b1SMatthias Ringwald int btstack_main(void)
267*bdc352b1SMatthias Ringwald {
268*bdc352b1SMatthias Ringwald     l2cap_init();
269*bdc352b1SMatthias Ringwald 
270*bdc352b1SMatthias Ringwald     rfcomm_init();
271*bdc352b1SMatthias Ringwald     rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
272*bdc352b1SMatthias Ringwald 
273*bdc352b1SMatthias Ringwald     // init SDP, create record for SPP and register with SDP
274*bdc352b1SMatthias Ringwald     sdp_init();
275*bdc352b1SMatthias Ringwald     memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
276*bdc352b1SMatthias Ringwald     spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
277*bdc352b1SMatthias Ringwald     sdp_register_service(spp_service_buffer);
278*bdc352b1SMatthias Ringwald     printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
279*bdc352b1SMatthias Ringwald 
280*bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
281*bdc352b1SMatthias Ringwald     // init SDP, create record for GATT and register with SDP
282*bdc352b1SMatthias Ringwald     memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
283*bdc352b1SMatthias Ringwald     gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
284*bdc352b1SMatthias Ringwald     sdp_register_service(gatt_service_buffer);
285*bdc352b1SMatthias Ringwald     printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer));
286*bdc352b1SMatthias Ringwald #endif
287*bdc352b1SMatthias Ringwald 
288*bdc352b1SMatthias Ringwald     gap_set_local_name("SPP and LE Counter 00:00:00:00:00:00");
289*bdc352b1SMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
290*bdc352b1SMatthias Ringwald     gap_discoverable_control(1);
291*bdc352b1SMatthias Ringwald 
292*bdc352b1SMatthias Ringwald     // setup le device db
293*bdc352b1SMatthias Ringwald     le_device_db_init();
294*bdc352b1SMatthias Ringwald 
295*bdc352b1SMatthias Ringwald     // setup SM: Display only
296*bdc352b1SMatthias Ringwald     sm_init();
297*bdc352b1SMatthias Ringwald 
298*bdc352b1SMatthias Ringwald     // setup ATT server
299*bdc352b1SMatthias Ringwald     att_server_init(profile_data, att_read_callback, att_write_callback);
300*bdc352b1SMatthias Ringwald 
301*bdc352b1SMatthias Ringwald     // register for HCI events
302*bdc352b1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
303*bdc352b1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
304*bdc352b1SMatthias Ringwald 
305*bdc352b1SMatthias Ringwald     // register for ATT events
306*bdc352b1SMatthias Ringwald     att_server_register_packet_handler(packet_handler);
307*bdc352b1SMatthias Ringwald 
308*bdc352b1SMatthias Ringwald     // setup advertisements
309*bdc352b1SMatthias Ringwald     uint16_t adv_int_min = 0x0030;
310*bdc352b1SMatthias Ringwald     uint16_t adv_int_max = 0x0030;
311*bdc352b1SMatthias Ringwald     uint8_t adv_type = 0;
312*bdc352b1SMatthias Ringwald     bd_addr_t null_addr;
313*bdc352b1SMatthias Ringwald     memset(null_addr, 0, 6);
314*bdc352b1SMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
315*bdc352b1SMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
316*bdc352b1SMatthias Ringwald     gap_advertisements_enable(1);
317*bdc352b1SMatthias Ringwald 
318*bdc352b1SMatthias Ringwald     // set one-shot timer
319*bdc352b1SMatthias Ringwald     heartbeat.process = &heartbeat_handler;
320*bdc352b1SMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
321*bdc352b1SMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
322*bdc352b1SMatthias Ringwald 
323*bdc352b1SMatthias Ringwald     // beat once
324*bdc352b1SMatthias Ringwald     beat();
325*bdc352b1SMatthias Ringwald 
326*bdc352b1SMatthias Ringwald     // turn on!
327*bdc352b1SMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
328*bdc352b1SMatthias Ringwald 
329*bdc352b1SMatthias Ringwald     return 0;
330*bdc352b1SMatthias Ringwald }
331*bdc352b1SMatthias Ringwald /* LISTING_END */
332*bdc352b1SMatthias Ringwald /* EXAMPLE_END */
333