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