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 BLUEKITCHEN 24 * GMBH 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__ "nordic_spp_le_counter.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(nordic_le_counter): LE Nordic SPP-like Heartbeat Server 42 * 43 */ 44 // ***************************************************************************** 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "btstack.h" 52 #include "ble/gatt-service/nordic_spp_service_server.h" 53 54 // nordic_spp_le_counter.gatt contains the declaration of the provided GATT Services + Characteristics 55 // nordic_spp_le_counter.h contains the binary representation of nordic_spp_le_counter.gatt 56 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py nordic_spp_le_counter.gatt nordic_spp_le_counter.h 57 // it needs to be regenerated when the GATT Database declared in nordic_spp_le_counter.gatt file is modified 58 #include "nordic_spp_le_counter.h" 59 60 61 #define HEARTBEAT_PERIOD_MS 1000 62 63 /* @section Main Application Setup 64 * 65 * @text Listing MainConfiguration shows main application code. 66 * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled 67 * ATT Database generated from $nordic_le_counter.gatt$. 68 * Additionally, it enables the Battery Service Server with the current battery level. 69 * Finally, it configures the advertisements 70 * and the heartbeat handler and boots the Bluetooth stack. 71 * In this example, the Advertisement contains the Flags attribute and the device name. 72 * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported. 73 */ 74 75 /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */ 76 static btstack_timer_source_t heartbeat; 77 static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID; 78 static btstack_context_callback_registration_t send_request; 79 static btstack_packet_callback_registration_t hci_event_callback_registration; 80 81 const uint8_t adv_data[] = { 82 // Flags general discoverable, BR/EDR not supported 83 2, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 84 // Name 85 8, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'n', 'R', 'F',' ', 'S', 'P', 'P', 86 // UUID ... 87 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, 88 }; 89 const uint8_t adv_data_len = sizeof(adv_data); 90 91 /* LISTING_END */ 92 93 /* 94 * @section Heartbeat Handler 95 * 96 * @text The heartbeat handler updates the value of the single Characteristic provided in this example, 97 * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat. 98 */ 99 100 /* LISTING_START(heartbeat): Hearbeat Handler */ 101 static int counter = 0; 102 static char counter_string[30]; 103 static int counter_string_len; 104 105 static void beat(void){ 106 counter++; 107 counter_string_len = snprintf(counter_string, sizeof(counter_string), "BTstack counter %03u", counter); 108 } 109 110 static void nordic_can_send(void * context){ 111 UNUSED(context); 112 printf("SEND: %s\n", counter_string); 113 nordic_spp_service_server_send(con_handle, (uint8_t*) counter_string, counter_string_len); 114 } 115 116 static void heartbeat_handler(struct btstack_timer_source *ts){ 117 if (con_handle != HCI_CON_HANDLE_INVALID) { 118 beat(); 119 send_request.callback = &nordic_can_send; 120 nordic_spp_service_server_request_can_send_now(&send_request, con_handle); 121 } 122 btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 123 btstack_run_loop_add_timer(ts); 124 } 125 /* LISTING_END */ 126 127 static void nordic_spp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 128 UNUSED(channel); 129 switch (packet_type){ 130 case HCI_EVENT_PACKET: 131 if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) break; 132 switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 133 case GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED: 134 con_handle = gattservice_subevent_spp_service_connected_get_con_handle(packet); 135 printf("Connected with handle 0x%04x\n", con_handle); 136 break; 137 case GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED: 138 con_handle = HCI_CON_HANDLE_INVALID; 139 break; 140 default: 141 break; 142 } 143 break; 144 case RFCOMM_DATA_PACKET: 145 printf("RECV: "); 146 printf_hexdump(packet, size); 147 break; 148 default: 149 break; 150 } 151 } 152 153 /* 154 * @section Packet Handler 155 * 156 * @text The packet handler is used to: 157 * - stop the counter after a disconnect 158 */ 159 160 /* LISTING_START(packetHandler): Packet Handler */ 161 static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 162 UNUSED(channel); 163 UNUSED(size); 164 165 if (packet_type != HCI_EVENT_PACKET) return; 166 167 switch (hci_event_packet_get_type(packet)) { 168 case HCI_EVENT_DISCONNECTION_COMPLETE: 169 con_handle = HCI_CON_HANDLE_INVALID; 170 break; 171 default: 172 break; 173 } 174 } 175 /* LISTING_END */ 176 177 178 int btstack_main(void); 179 int btstack_main(void) 180 { 181 // register for HCI events 182 hci_event_callback_registration.callback = &hci_packet_handler; 183 hci_add_event_handler(&hci_event_callback_registration); 184 185 l2cap_init(); 186 187 // setup SM: Display only 188 sm_init(); 189 190 // setup ATT server 191 att_server_init(profile_data, NULL, NULL); 192 193 // setup Nordic SPP service 194 nordic_spp_service_server_init(&nordic_spp_packet_handler); 195 196 // setup advertisements 197 uint16_t adv_int_min = 0x0030; 198 uint16_t adv_int_max = 0x0030; 199 uint8_t adv_type = 0; 200 bd_addr_t null_addr; 201 memset(null_addr, 0, 6); 202 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 203 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 204 gap_advertisements_enable(1); 205 206 // set one-shot timer 207 heartbeat.process = &heartbeat_handler; 208 btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 209 btstack_run_loop_add_timer(&heartbeat); 210 211 // beat once 212 beat(); 213 214 // turn on! 215 hci_power_control(HCI_POWER_ON); 216 217 return 0; 218 } 219 /* EXAMPLE_END */ 220