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