xref: /btstack/example/nordic_spp_le_counter.c (revision 4163d2eb57800152df2d8adc265fabbe20bbf6e9)
1ebdf3c68SMilanka Ringwald /*
2ebdf3c68SMilanka Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3ebdf3c68SMilanka Ringwald  *
4ebdf3c68SMilanka Ringwald  * Redistribution and use in source and binary forms, with or without
5ebdf3c68SMilanka Ringwald  * modification, are permitted provided that the following conditions
6ebdf3c68SMilanka Ringwald  * are met:
7ebdf3c68SMilanka Ringwald  *
8ebdf3c68SMilanka Ringwald  * 1. Redistributions of source code must retain the above copyright
9ebdf3c68SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer.
10ebdf3c68SMilanka Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11ebdf3c68SMilanka Ringwald  *    notice, this list of conditions and the following disclaimer in the
12ebdf3c68SMilanka Ringwald  *    documentation and/or other materials provided with the distribution.
13ebdf3c68SMilanka Ringwald  * 3. Neither the name of the copyright holders nor the names of
14ebdf3c68SMilanka Ringwald  *    contributors may be used to endorse or promote products derived
15ebdf3c68SMilanka Ringwald  *    from this software without specific prior written permission.
16ebdf3c68SMilanka Ringwald  * 4. Any redistribution, use, or modification is done solely for
17ebdf3c68SMilanka Ringwald  *    personal benefit and not for any commercial purpose or for
18ebdf3c68SMilanka Ringwald  *    monetary gain.
19ebdf3c68SMilanka Ringwald  *
20ebdf3c68SMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21ebdf3c68SMilanka Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ebdf3c68SMilanka Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23ebdf3c68SMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24ebdf3c68SMilanka Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25ebdf3c68SMilanka Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26ebdf3c68SMilanka Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27ebdf3c68SMilanka Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28ebdf3c68SMilanka Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29ebdf3c68SMilanka Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30ebdf3c68SMilanka Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ebdf3c68SMilanka Ringwald  * SUCH DAMAGE.
32ebdf3c68SMilanka Ringwald  *
33ebdf3c68SMilanka Ringwald  * Please inquire about commercial licensing options at
34ebdf3c68SMilanka Ringwald  * [email protected]
35ebdf3c68SMilanka Ringwald  *
36ebdf3c68SMilanka Ringwald  */
37ebdf3c68SMilanka Ringwald 
38ebdf3c68SMilanka Ringwald #define __BTSTACK_FILE__ "nordic_le_counter.c"
39ebdf3c68SMilanka Ringwald 
40ebdf3c68SMilanka Ringwald // *****************************************************************************
41ebdf3c68SMilanka Ringwald /* EXAMPLE_START(nordic_le_counter): LE Peripheral - Nordic SPP-like profile
42ebdf3c68SMilanka Ringwald  *
43ebdf3c68SMilanka Ringwald  */
44ebdf3c68SMilanka Ringwald  // *****************************************************************************
45ebdf3c68SMilanka Ringwald 
46ebdf3c68SMilanka Ringwald #include <stdint.h>
47ebdf3c68SMilanka Ringwald #include <stdio.h>
48ebdf3c68SMilanka Ringwald #include <stdlib.h>
49ebdf3c68SMilanka Ringwald #include <string.h>
50ebdf3c68SMilanka Ringwald 
51ba7944beSMilanka Ringwald #include "nordic_spp_le_counter.h"
52ebdf3c68SMilanka Ringwald #include "btstack.h"
53ebdf3c68SMilanka Ringwald #include "ble/gatt-service/nordic_spp_service_server.h"
54ebdf3c68SMilanka Ringwald 
55ebdf3c68SMilanka Ringwald #define HEARTBEAT_PERIOD_MS 1000
56ebdf3c68SMilanka Ringwald 
57ebdf3c68SMilanka Ringwald /* @section Main Application Setup
58ebdf3c68SMilanka Ringwald  *
59ebdf3c68SMilanka Ringwald  * @text Listing MainConfiguration shows main application code.
60ebdf3c68SMilanka Ringwald  * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
61ebdf3c68SMilanka Ringwald  * ATT Database generated from $nordic_le_counter.gatt$.
62ebdf3c68SMilanka Ringwald  * Additionally, it enables the Battery Service Server with the current battery level.
63ebdf3c68SMilanka Ringwald  * Finally, it configures the advertisements
64ebdf3c68SMilanka Ringwald  * and the heartbeat handler and boots the Bluetooth stack.
65ebdf3c68SMilanka Ringwald  * In this example, the Advertisement contains the Flags attribute and the device name.
66ebdf3c68SMilanka Ringwald  * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
67ebdf3c68SMilanka Ringwald  */
68ebdf3c68SMilanka Ringwald 
69ebdf3c68SMilanka Ringwald /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */
70ebdf3c68SMilanka Ringwald static btstack_timer_source_t heartbeat;
71ebdf3c68SMilanka Ringwald static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
72ebdf3c68SMilanka Ringwald static btstack_context_callback_registration_t send_request;
73ebdf3c68SMilanka Ringwald static btstack_packet_callback_registration_t  hci_event_callback_registration;
74ebdf3c68SMilanka Ringwald 
75ebdf3c68SMilanka Ringwald const uint8_t adv_data[] = {
76ebdf3c68SMilanka Ringwald     // Flags general discoverable, BR/EDR not supported
77ebdf3c68SMilanka Ringwald     2, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
78ebdf3c68SMilanka Ringwald     // Name
79ba7944beSMilanka Ringwald     8, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'n', 'R', 'F',' ', 'S', 'P', 'P',
80ebdf3c68SMilanka Ringwald     // UUID ...
81*4163d2ebSMilanka Ringwald     17, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS, 0x9e, 0xca, 0xdc, 0x24, 0xe, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x1, 0x0, 0x40, 0x6e,
82ebdf3c68SMilanka Ringwald };
83ebdf3c68SMilanka Ringwald const uint8_t adv_data_len = sizeof(adv_data);
84ebdf3c68SMilanka Ringwald 
85ebdf3c68SMilanka Ringwald /* LISTING_END */
86ebdf3c68SMilanka Ringwald 
87ebdf3c68SMilanka Ringwald /*
88ebdf3c68SMilanka Ringwald  * @section Heartbeat Handler
89ebdf3c68SMilanka Ringwald  *
90ebdf3c68SMilanka Ringwald  * @text The heartbeat handler updates the value of the single Characteristic provided in this example,
91ebdf3c68SMilanka Ringwald  * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat.
92ebdf3c68SMilanka Ringwald  */
93ebdf3c68SMilanka Ringwald 
94ebdf3c68SMilanka Ringwald  /* LISTING_START(heartbeat): Hearbeat Handler */
95ebdf3c68SMilanka Ringwald static int  counter = 0;
96ebdf3c68SMilanka Ringwald static char counter_string[30];
97ebdf3c68SMilanka Ringwald static int  counter_string_len;
98ebdf3c68SMilanka Ringwald 
99ebdf3c68SMilanka Ringwald static void beat(void){
100ebdf3c68SMilanka Ringwald     counter++;
101ebdf3c68SMilanka Ringwald     counter_string_len = sprintf(counter_string, "BTstack counter %03u", counter);
102ebdf3c68SMilanka Ringwald }
103ebdf3c68SMilanka Ringwald 
104ebdf3c68SMilanka Ringwald static void nordic_can_send(void * context){
105ebdf3c68SMilanka Ringwald     UNUSED(context);
106ebdf3c68SMilanka Ringwald     printf("SEND: %s\n", counter_string);
107ebdf3c68SMilanka Ringwald     nordic_spp_service_server_send(con_handle, (uint8_t*) counter_string, counter_string_len);
108ebdf3c68SMilanka Ringwald }
109ebdf3c68SMilanka Ringwald 
110ebdf3c68SMilanka Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){
111ebdf3c68SMilanka Ringwald     if (con_handle != HCI_CON_HANDLE_INVALID) {
112ebdf3c68SMilanka Ringwald         beat();
113ebdf3c68SMilanka Ringwald         send_request.callback = &nordic_can_send;
114ebdf3c68SMilanka Ringwald         nordic_spp_service_server_request_can_send_now(&send_request, con_handle);
115ebdf3c68SMilanka Ringwald     }
116ebdf3c68SMilanka Ringwald     btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
117ebdf3c68SMilanka Ringwald     btstack_run_loop_add_timer(ts);
118ebdf3c68SMilanka Ringwald }
119ebdf3c68SMilanka Ringwald /* LISTING_END */
120ebdf3c68SMilanka Ringwald 
121ebdf3c68SMilanka Ringwald static void nordic_data_received(hci_con_handle_t tx_con_handle, const uint8_t * data, uint16_t size){
122ebdf3c68SMilanka Ringwald     if (size == 0 && con_handle == HCI_CON_HANDLE_INVALID ){
123ebdf3c68SMilanka Ringwald         con_handle = tx_con_handle;
124ebdf3c68SMilanka Ringwald         printf("Connected with handle 0x%04x\n", con_handle);
125ebdf3c68SMilanka Ringwald     } else {
126ebdf3c68SMilanka Ringwald         printf("RECV: ");
127ebdf3c68SMilanka Ringwald         printf_hexdump(data, size);
128ebdf3c68SMilanka Ringwald     }
129ebdf3c68SMilanka Ringwald }
130ebdf3c68SMilanka Ringwald 
131ebdf3c68SMilanka Ringwald /*
132ebdf3c68SMilanka Ringwald  * @section Packet Handler
133ebdf3c68SMilanka Ringwald  *
134ebdf3c68SMilanka Ringwald  * @text The packet handler is used to:
135ebdf3c68SMilanka Ringwald  *        - stop the counter after a disconnect
136ebdf3c68SMilanka Ringwald  */
137ebdf3c68SMilanka Ringwald 
138ebdf3c68SMilanka Ringwald /* LISTING_START(packetHandler): Packet Handler */
139ebdf3c68SMilanka Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
140ebdf3c68SMilanka Ringwald     UNUSED(channel);
141ebdf3c68SMilanka Ringwald     UNUSED(size);
142ebdf3c68SMilanka Ringwald 
143ebdf3c68SMilanka Ringwald     switch (packet_type) {
144ebdf3c68SMilanka Ringwald         case HCI_EVENT_PACKET:
145ebdf3c68SMilanka Ringwald             switch (hci_event_packet_get_type(packet)) {
146ebdf3c68SMilanka Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
147ebdf3c68SMilanka Ringwald                     con_handle = HCI_CON_HANDLE_INVALID;
148ebdf3c68SMilanka Ringwald                     break;
149ebdf3c68SMilanka Ringwald                 default:
150ebdf3c68SMilanka Ringwald                     break;
151ebdf3c68SMilanka Ringwald             }
152ebdf3c68SMilanka Ringwald             break;
153ebdf3c68SMilanka Ringwald     }
154ebdf3c68SMilanka Ringwald }
155ebdf3c68SMilanka Ringwald /* LISTING_END */
156ebdf3c68SMilanka Ringwald 
157ebdf3c68SMilanka Ringwald 
158ebdf3c68SMilanka Ringwald int btstack_main(void);
159ebdf3c68SMilanka Ringwald int btstack_main(void)
160ebdf3c68SMilanka Ringwald {
161ebdf3c68SMilanka Ringwald     // register for HCI events
162ebdf3c68SMilanka Ringwald     hci_event_callback_registration.callback = &packet_handler;
163ebdf3c68SMilanka Ringwald     hci_add_event_handler(&hci_event_callback_registration);
164ebdf3c68SMilanka Ringwald 
165ebdf3c68SMilanka Ringwald     l2cap_init();
166ebdf3c68SMilanka Ringwald 
167ebdf3c68SMilanka Ringwald     // setup LE device DB
168ebdf3c68SMilanka Ringwald     le_device_db_init();
169ebdf3c68SMilanka Ringwald 
170ebdf3c68SMilanka Ringwald     // setup SM: Display only
171ebdf3c68SMilanka Ringwald     sm_init();
172ebdf3c68SMilanka Ringwald 
173ebdf3c68SMilanka Ringwald     // setup ATT server
174ebdf3c68SMilanka Ringwald     att_server_init(profile_data, NULL, NULL);
175ebdf3c68SMilanka Ringwald 
176ebdf3c68SMilanka Ringwald     // setup Nordic SPP service
177ebdf3c68SMilanka Ringwald     nordic_spp_service_server_init(&nordic_data_received);
178ebdf3c68SMilanka Ringwald 
179ebdf3c68SMilanka Ringwald     // setup advertisements
180ebdf3c68SMilanka Ringwald     uint16_t adv_int_min = 0x0030;
181ebdf3c68SMilanka Ringwald     uint16_t adv_int_max = 0x0030;
182ebdf3c68SMilanka Ringwald     uint8_t adv_type = 0;
183ebdf3c68SMilanka Ringwald     bd_addr_t null_addr;
184ebdf3c68SMilanka Ringwald     memset(null_addr, 0, 6);
185ebdf3c68SMilanka Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
186ebdf3c68SMilanka Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
187ebdf3c68SMilanka Ringwald     gap_advertisements_enable(1);
188ebdf3c68SMilanka Ringwald 
189ebdf3c68SMilanka Ringwald     // set one-shot timer
190ebdf3c68SMilanka Ringwald     heartbeat.process = &heartbeat_handler;
191ebdf3c68SMilanka Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
192ebdf3c68SMilanka Ringwald     btstack_run_loop_add_timer(&heartbeat);
193ebdf3c68SMilanka Ringwald 
194ebdf3c68SMilanka Ringwald     // beat once
195ebdf3c68SMilanka Ringwald     beat();
196ebdf3c68SMilanka Ringwald 
197ebdf3c68SMilanka Ringwald     // turn on!
198ebdf3c68SMilanka Ringwald 	hci_power_control(HCI_POWER_ON);
199ebdf3c68SMilanka Ringwald 
200ebdf3c68SMilanka Ringwald     return 0;
201ebdf3c68SMilanka Ringwald }
202ebdf3c68SMilanka Ringwald /* EXAMPLE_END */
203