1733ac1e8SMilanka Ringwald /* 2733ac1e8SMilanka Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3733ac1e8SMilanka Ringwald * 4733ac1e8SMilanka Ringwald * Redistribution and use in source and binary forms, with or without 5733ac1e8SMilanka Ringwald * modification, are permitted provided that the following conditions 6733ac1e8SMilanka Ringwald * are met: 7733ac1e8SMilanka Ringwald * 8733ac1e8SMilanka Ringwald * 1. Redistributions of source code must retain the above copyright 9733ac1e8SMilanka Ringwald * notice, this list of conditions and the following disclaimer. 10733ac1e8SMilanka Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11733ac1e8SMilanka Ringwald * notice, this list of conditions and the following disclaimer in the 12733ac1e8SMilanka Ringwald * documentation and/or other materials provided with the distribution. 13733ac1e8SMilanka Ringwald * 3. Neither the name of the copyright holders nor the names of 14733ac1e8SMilanka Ringwald * contributors may be used to endorse or promote products derived 15733ac1e8SMilanka Ringwald * from this software without specific prior written permission. 16733ac1e8SMilanka Ringwald * 4. Any redistribution, use, or modification is done solely for 17733ac1e8SMilanka Ringwald * personal benefit and not for any commercial purpose or for 18733ac1e8SMilanka Ringwald * monetary gain. 19733ac1e8SMilanka Ringwald * 20733ac1e8SMilanka Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21733ac1e8SMilanka Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22733ac1e8SMilanka Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23733ac1e8SMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24733ac1e8SMilanka Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25733ac1e8SMilanka Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26733ac1e8SMilanka Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27733ac1e8SMilanka Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28733ac1e8SMilanka Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29733ac1e8SMilanka Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30733ac1e8SMilanka Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31733ac1e8SMilanka Ringwald * SUCH DAMAGE. 32733ac1e8SMilanka Ringwald * 33733ac1e8SMilanka Ringwald * Please inquire about commercial licensing options at 34733ac1e8SMilanka Ringwald * [email protected] 35733ac1e8SMilanka Ringwald * 36733ac1e8SMilanka Ringwald */ 37733ac1e8SMilanka Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "ublox_spp_le_counter.c" 39733ac1e8SMilanka Ringwald 40733ac1e8SMilanka Ringwald // ***************************************************************************** 41ec8ae085SMilanka Ringwald /* EXAMPLE_START(ublox_spp_le_counter): LE u-blox SPP-like Heartbeat Server 42733ac1e8SMilanka Ringwald * 43733ac1e8SMilanka Ringwald */ 44733ac1e8SMilanka Ringwald // ***************************************************************************** 45733ac1e8SMilanka Ringwald 46733ac1e8SMilanka Ringwald #include <stdint.h> 47733ac1e8SMilanka Ringwald #include <stdio.h> 48733ac1e8SMilanka Ringwald #include <stdlib.h> 49733ac1e8SMilanka Ringwald #include <string.h> 50733ac1e8SMilanka Ringwald 51733ac1e8SMilanka Ringwald #include "btstack.h" 526c9a65edSMilanka Ringwald #include "ble/gatt-service/device_information_service_server.h" 53733ac1e8SMilanka Ringwald #include "ble/gatt-service/ublox_spp_service_server.h" 54733ac1e8SMilanka Ringwald 55a63a688aSMatthias Ringwald // ublox_spp_le_counter.gatt contains the declaration of the provided GATT Services + Characteristics 56a63a688aSMatthias Ringwald // ublox_spp_le_counter.h contains the binary representation of ublox_spp_le_counter.gatt 57a63a688aSMatthias Ringwald // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py ublox_spp_le_counter.gatt ublox_spp_le_counter.h 58a63a688aSMatthias Ringwald // it needs to be regenerated when the GATT Database declared in ublox_spp_le_counter.gatt file is modified 59a63a688aSMatthias Ringwald #include "ublox_spp_le_counter.h" 60a63a688aSMatthias Ringwald 61a63a688aSMatthias Ringwald 62733ac1e8SMilanka Ringwald #define HEARTBEAT_PERIOD_MS 1000 63733ac1e8SMilanka Ringwald 64733ac1e8SMilanka Ringwald /* @section Main Application Setup 65733ac1e8SMilanka Ringwald * 66733ac1e8SMilanka Ringwald * @text Listing MainConfiguration shows main application code. 67733ac1e8SMilanka Ringwald * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled 68733ac1e8SMilanka Ringwald * ATT Database generated from $ublox_le_counter.gatt$. 69733ac1e8SMilanka Ringwald * Additionally, it enables the Battery Service Server with the current battery level. 70733ac1e8SMilanka Ringwald * Finally, it configures the advertisements 71733ac1e8SMilanka Ringwald * and the heartbeat handler and boots the Bluetooth stack. 72733ac1e8SMilanka Ringwald * In this example, the Advertisement contains the Flags attribute and the device name. 73733ac1e8SMilanka Ringwald * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported. 74733ac1e8SMilanka Ringwald */ 75733ac1e8SMilanka Ringwald 76733ac1e8SMilanka Ringwald /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */ 77733ac1e8SMilanka Ringwald static btstack_timer_source_t heartbeat; 78733ac1e8SMilanka Ringwald static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID; 79733ac1e8SMilanka Ringwald static btstack_context_callback_registration_t send_request; 80733ac1e8SMilanka Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 81733ac1e8SMilanka Ringwald 82733ac1e8SMilanka Ringwald const uint8_t adv_data[] = { 83733ac1e8SMilanka Ringwald // Flags general discoverable, BR/EDR not supported 84733ac1e8SMilanka Ringwald 2, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 85733ac1e8SMilanka Ringwald // Name 8640a35d0aSMilanka Ringwald 5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, '6', '-','5', '6', 87733ac1e8SMilanka Ringwald // UUID ... 88733ac1e8SMilanka Ringwald 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, 89733ac1e8SMilanka Ringwald }; 90733ac1e8SMilanka Ringwald 91733ac1e8SMilanka Ringwald const uint8_t adv_data_len = sizeof(adv_data); 92733ac1e8SMilanka Ringwald 93733ac1e8SMilanka Ringwald /* LISTING_END */ 94733ac1e8SMilanka Ringwald 95733ac1e8SMilanka Ringwald /* 96733ac1e8SMilanka Ringwald * @section Heartbeat Handler 97733ac1e8SMilanka Ringwald * 98733ac1e8SMilanka Ringwald * @text The heartbeat handler updates the value of the single Characteristic provided in this example, 99733ac1e8SMilanka Ringwald * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat. 100733ac1e8SMilanka Ringwald */ 101733ac1e8SMilanka Ringwald 102733ac1e8SMilanka Ringwald /* LISTING_START(heartbeat): Hearbeat Handler */ 103733ac1e8SMilanka Ringwald static int counter = 0; 104733ac1e8SMilanka Ringwald static char counter_string[30]; 105733ac1e8SMilanka Ringwald static int counter_string_len; 106733ac1e8SMilanka Ringwald 107733ac1e8SMilanka Ringwald static void beat(void){ 108733ac1e8SMilanka Ringwald counter++; 109733ac1e8SMilanka Ringwald counter_string_len = sprintf(counter_string, "BTstack counter %03u", counter); 110733ac1e8SMilanka Ringwald } 111733ac1e8SMilanka Ringwald 112733ac1e8SMilanka Ringwald static void ublox_can_send(void * context){ 113733ac1e8SMilanka Ringwald UNUSED(context); 114733ac1e8SMilanka Ringwald beat(); 115733ac1e8SMilanka Ringwald printf("SEND: %s\n", counter_string); 116733ac1e8SMilanka Ringwald ublox_spp_service_server_send(con_handle, (uint8_t*) counter_string, counter_string_len); 117733ac1e8SMilanka Ringwald } 118733ac1e8SMilanka Ringwald 119733ac1e8SMilanka Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){ 120733ac1e8SMilanka Ringwald if (con_handle != HCI_CON_HANDLE_INVALID) { 121733ac1e8SMilanka Ringwald send_request.callback = &ublox_can_send; 122733ac1e8SMilanka Ringwald ublox_spp_service_server_request_can_send_now(&send_request, con_handle); 123733ac1e8SMilanka Ringwald } 124733ac1e8SMilanka Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 125733ac1e8SMilanka Ringwald btstack_run_loop_add_timer(ts); 126733ac1e8SMilanka Ringwald } 127733ac1e8SMilanka Ringwald /* LISTING_END */ 128733ac1e8SMilanka Ringwald 129694f87bfSMatthias Ringwald static void ublox_spp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 130*6058cb0dSMatthias Ringwald UNUSED(channel); 131694f87bfSMatthias Ringwald switch (packet_type){ 132694f87bfSMatthias Ringwald case HCI_EVENT_PACKET: 133694f87bfSMatthias Ringwald if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) break; 134694f87bfSMatthias Ringwald switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 135694f87bfSMatthias Ringwald case GATTSERVICE_SUBEVENT_SPP_SERVICE_CONNECTED: 136694f87bfSMatthias Ringwald con_handle = gattservice_subevent_spp_service_connected_get_con_handle(packet); 137733ac1e8SMilanka Ringwald printf("Connected with handle 0x%04x\n", con_handle); 138694f87bfSMatthias Ringwald break; 139694f87bfSMatthias Ringwald case GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED: 140694f87bfSMatthias Ringwald con_handle = HCI_CON_HANDLE_INVALID; 141694f87bfSMatthias Ringwald break; 142694f87bfSMatthias Ringwald default: 143694f87bfSMatthias Ringwald break; 144694f87bfSMatthias Ringwald } 145694f87bfSMatthias Ringwald break; 146694f87bfSMatthias Ringwald case RFCOMM_DATA_PACKET: 147733ac1e8SMilanka Ringwald printf("RECV: "); 148694f87bfSMatthias Ringwald printf_hexdump(packet, size); 149694f87bfSMatthias Ringwald break; 150694f87bfSMatthias Ringwald default: 151694f87bfSMatthias Ringwald break; 152733ac1e8SMilanka Ringwald } 153733ac1e8SMilanka Ringwald } 154733ac1e8SMilanka Ringwald 155733ac1e8SMilanka Ringwald /* 156733ac1e8SMilanka Ringwald * @section Packet Handler 157733ac1e8SMilanka Ringwald * 158733ac1e8SMilanka Ringwald * @text The packet handler is used to: 159733ac1e8SMilanka Ringwald * - stop the counter after a disconnect 160733ac1e8SMilanka Ringwald */ 161733ac1e8SMilanka Ringwald 162733ac1e8SMilanka Ringwald /* LISTING_START(packetHandler): Packet Handler */ 163733ac1e8SMilanka Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 164733ac1e8SMilanka Ringwald UNUSED(channel); 165733ac1e8SMilanka Ringwald UNUSED(size); 166733ac1e8SMilanka Ringwald 167733ac1e8SMilanka Ringwald switch (packet_type) { 168733ac1e8SMilanka Ringwald case HCI_EVENT_PACKET: 169733ac1e8SMilanka Ringwald switch (hci_event_packet_get_type(packet)) { 170733ac1e8SMilanka Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 171733ac1e8SMilanka Ringwald con_handle = HCI_CON_HANDLE_INVALID; 172733ac1e8SMilanka Ringwald break; 173733ac1e8SMilanka Ringwald default: 174733ac1e8SMilanka Ringwald break; 175733ac1e8SMilanka Ringwald } 176733ac1e8SMilanka Ringwald break; 1777bbeb3adSMilanka Ringwald default: 1787bbeb3adSMilanka Ringwald break; 179733ac1e8SMilanka Ringwald } 180733ac1e8SMilanka Ringwald } 181733ac1e8SMilanka Ringwald /* LISTING_END */ 182733ac1e8SMilanka Ringwald 183733ac1e8SMilanka Ringwald 184733ac1e8SMilanka Ringwald int btstack_main(void); 185733ac1e8SMilanka Ringwald int btstack_main(void) 186733ac1e8SMilanka Ringwald { 187733ac1e8SMilanka Ringwald // register for HCI events 188733ac1e8SMilanka Ringwald hci_event_callback_registration.callback = &packet_handler; 189733ac1e8SMilanka Ringwald hci_add_event_handler(&hci_event_callback_registration); 190733ac1e8SMilanka Ringwald 191733ac1e8SMilanka Ringwald l2cap_init(); 192733ac1e8SMilanka Ringwald 193733ac1e8SMilanka Ringwald // setup LE device DB 194733ac1e8SMilanka Ringwald le_device_db_init(); 195733ac1e8SMilanka Ringwald 196733ac1e8SMilanka Ringwald // setup SM: Display only 197733ac1e8SMilanka Ringwald sm_init(); 198733ac1e8SMilanka Ringwald 199733ac1e8SMilanka Ringwald // setup ATT server 200733ac1e8SMilanka Ringwald att_server_init(profile_data, NULL, NULL); 20116e3e4afSMilanka Ringwald // setup device information service 20216e3e4afSMilanka Ringwald device_information_service_server_init(); 203733ac1e8SMilanka Ringwald // setup Nordic SPP service 204694f87bfSMatthias Ringwald ublox_spp_service_server_init(&ublox_spp_packet_handler); 205733ac1e8SMilanka Ringwald 206733ac1e8SMilanka Ringwald // setup advertisements 207733ac1e8SMilanka Ringwald uint16_t adv_int_min = 0x0030; 208733ac1e8SMilanka Ringwald uint16_t adv_int_max = 0x0030; 209733ac1e8SMilanka Ringwald uint8_t adv_type = 0; 210733ac1e8SMilanka Ringwald bd_addr_t null_addr; 211733ac1e8SMilanka Ringwald memset(null_addr, 0, 6); 212733ac1e8SMilanka Ringwald gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 213733ac1e8SMilanka Ringwald gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 214733ac1e8SMilanka Ringwald gap_advertisements_enable(1); 215733ac1e8SMilanka Ringwald 216733ac1e8SMilanka Ringwald // set one-shot timer 217733ac1e8SMilanka Ringwald heartbeat.process = &heartbeat_handler; 218733ac1e8SMilanka Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 219733ac1e8SMilanka Ringwald btstack_run_loop_add_timer(&heartbeat); 220733ac1e8SMilanka Ringwald 221733ac1e8SMilanka Ringwald // beat once 222733ac1e8SMilanka Ringwald beat(); 223733ac1e8SMilanka Ringwald 224733ac1e8SMilanka Ringwald // turn on! 225733ac1e8SMilanka Ringwald hci_power_control(HCI_POWER_ON); 226733ac1e8SMilanka Ringwald 227733ac1e8SMilanka Ringwald return 0; 228733ac1e8SMilanka Ringwald } 229733ac1e8SMilanka Ringwald /* EXAMPLE_END */ 230