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