1bcf00d8fSMatthias Ringwald /* 2bcf00d8fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3bcf00d8fSMatthias Ringwald * 4bcf00d8fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5bcf00d8fSMatthias Ringwald * modification, are permitted provided that the following conditions 6bcf00d8fSMatthias Ringwald * are met: 7bcf00d8fSMatthias Ringwald * 8bcf00d8fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10bcf00d8fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12bcf00d8fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13bcf00d8fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14bcf00d8fSMatthias Ringwald * contributors may be used to endorse or promote products derived 15bcf00d8fSMatthias Ringwald * from this software without specific prior written permission. 16bcf00d8fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17bcf00d8fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18bcf00d8fSMatthias Ringwald * monetary gain. 19bcf00d8fSMatthias Ringwald * 20bcf00d8fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21bcf00d8fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22bcf00d8fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23bcf00d8fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24bcf00d8fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25bcf00d8fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26bcf00d8fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27bcf00d8fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28bcf00d8fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29bcf00d8fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30bcf00d8fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31bcf00d8fSMatthias Ringwald * SUCH DAMAGE. 32bcf00d8fSMatthias Ringwald * 33bcf00d8fSMatthias Ringwald * Please inquire about commercial licensing options at 34bcf00d8fSMatthias Ringwald * [email protected] 35bcf00d8fSMatthias Ringwald * 36bcf00d8fSMatthias Ringwald */ 37bcf00d8fSMatthias Ringwald 38bcf00d8fSMatthias Ringwald // ***************************************************************************** 39bcf00d8fSMatthias Ringwald /* EXAMPLE_START(spp_counter): SPP Server - Heartbeat Counter over RFCOMM 40bcf00d8fSMatthias Ringwald * 41bcf00d8fSMatthias Ringwald * @text The Serial port profile (SPP) is widely used as it provides a serial 42bcf00d8fSMatthias Ringwald * port over Bluetooth. The SPP counter example demonstrates how to setup an SPP 43bcf00d8fSMatthias Ringwald * service, and provide a periodic timer over RFCOMM. 44bcf00d8fSMatthias Ringwald */ 45bcf00d8fSMatthias Ringwald // ***************************************************************************** 46bcf00d8fSMatthias Ringwald 47bcf00d8fSMatthias Ringwald #include <inttypes.h> 48bcf00d8fSMatthias Ringwald #include <stdint.h> 49bcf00d8fSMatthias Ringwald #include <stdio.h> 50bcf00d8fSMatthias Ringwald #include <stdlib.h> 51bcf00d8fSMatthias Ringwald #include <string.h> 52bcf00d8fSMatthias Ringwald 53*0e2df43fSMatthias Ringwald #include "btstack.h" 54bcf00d8fSMatthias Ringwald 55bcf00d8fSMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1 56bcf00d8fSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000 57bcf00d8fSMatthias Ringwald 58bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 59bcf00d8fSMatthias Ringwald 60bcf00d8fSMatthias Ringwald static uint16_t rfcomm_channel_id; 61bcf00d8fSMatthias Ringwald static uint8_t spp_service_buffer[150]; 62bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 63bcf00d8fSMatthias Ringwald 64bcf00d8fSMatthias Ringwald 65bcf00d8fSMatthias Ringwald /* @section SPP Service Setup 66bcf00d8fSMatthias Ringwald *s 67bcf00d8fSMatthias Ringwald * @text To provide an SPP service, the L2CAP, RFCOMM, and SDP protocol layers 68bcf00d8fSMatthias Ringwald * are required. After setting up an RFCOMM service with channel nubmer 69bcf00d8fSMatthias Ringwald * RFCOMM_SERVER_CHANNEL, an SDP record is created and registered with the SDP server. 70bcf00d8fSMatthias Ringwald * Example code for SPP service setup is 71bcf00d8fSMatthias Ringwald * provided in Listing SPPSetup. The SDP record created by function 72bcf00d8fSMatthias Ringwald * sdp_create_spp_service consists of a basic SPP definition that uses the provided 73bcf00d8fSMatthias Ringwald * RFCOMM channel ID and service name. For more details, please have a look at it 74bcf00d8fSMatthias Ringwald * in \path{src/sdp_util.c}. 75bcf00d8fSMatthias Ringwald * The SDP record is created on the fly in RAM and is deterministic. 76bcf00d8fSMatthias Ringwald * To preserve valuable RAM, the result could be stored as constant data inside the ROM. 77bcf00d8fSMatthias Ringwald */ 78bcf00d8fSMatthias Ringwald 79bcf00d8fSMatthias Ringwald /* LISTING_START(SPPSetup): SPP service setup */ 80bcf00d8fSMatthias Ringwald static void spp_service_setup(void){ 81bcf00d8fSMatthias Ringwald 82bcf00d8fSMatthias Ringwald // register for HCI events 83bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 84bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 85bcf00d8fSMatthias Ringwald 86bcf00d8fSMatthias Ringwald l2cap_init(); 87bcf00d8fSMatthias Ringwald 88bcf00d8fSMatthias Ringwald rfcomm_init(); 89bcf00d8fSMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); // reserved channel, mtu limited by l2cap 90bcf00d8fSMatthias Ringwald 91bcf00d8fSMatthias Ringwald // init SDP, create record for SPP and register with SDP 92bcf00d8fSMatthias Ringwald sdp_init(); 93bcf00d8fSMatthias Ringwald memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 94bcf00d8fSMatthias Ringwald sdp_create_spp_service(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter"); 95bcf00d8fSMatthias Ringwald sdp_register_service(spp_service_buffer); 96bcf00d8fSMatthias Ringwald printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); 97bcf00d8fSMatthias Ringwald } 98bcf00d8fSMatthias Ringwald /* LISTING_END */ 99bcf00d8fSMatthias Ringwald 100bcf00d8fSMatthias Ringwald /* @section Periodic Timer Setup 101bcf00d8fSMatthias Ringwald * 102bcf00d8fSMatthias Ringwald * @text The heartbeat handler increases the real counter every second, 103bcf00d8fSMatthias Ringwald * and sends a text string with the counter value, as shown in Listing PeriodicCounter. 104bcf00d8fSMatthias Ringwald */ 105bcf00d8fSMatthias Ringwald 106bcf00d8fSMatthias Ringwald /* LISTING_START(PeriodicCounter): Periodic Counter */ 107bcf00d8fSMatthias Ringwald static btstack_timer_source_t heartbeat; 108bcf00d8fSMatthias Ringwald 109bcf00d8fSMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){ 110bcf00d8fSMatthias Ringwald static int counter = 0; 111bcf00d8fSMatthias Ringwald 112bcf00d8fSMatthias Ringwald if (rfcomm_channel_id){ 113bcf00d8fSMatthias Ringwald char lineBuffer[30]; 114bcf00d8fSMatthias Ringwald sprintf(lineBuffer, "BTstack counter %04u\n", ++counter); 115bcf00d8fSMatthias Ringwald printf("%s", lineBuffer); 116bcf00d8fSMatthias Ringwald if (rfcomm_can_send_packet_now(rfcomm_channel_id)) { 117bcf00d8fSMatthias Ringwald int err = rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer)); 118bcf00d8fSMatthias Ringwald if (err) { 119bcf00d8fSMatthias Ringwald log_error("rfcomm_send -> error 0X%02x", err); 120bcf00d8fSMatthias Ringwald } 121bcf00d8fSMatthias Ringwald } 122bcf00d8fSMatthias Ringwald } 123bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 124bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(ts); 125bcf00d8fSMatthias Ringwald } 126bcf00d8fSMatthias Ringwald 127bcf00d8fSMatthias Ringwald static void one_shot_timer_setup(void){ 128bcf00d8fSMatthias Ringwald // set one-shot timer 129bcf00d8fSMatthias Ringwald heartbeat.process = &heartbeat_handler; 130bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 131bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(&heartbeat); 132bcf00d8fSMatthias Ringwald } 133bcf00d8fSMatthias Ringwald /* LISTING_END */ 134bcf00d8fSMatthias Ringwald 135bcf00d8fSMatthias Ringwald 136bcf00d8fSMatthias Ringwald /* @section Bluetooth Logic 137bcf00d8fSMatthias Ringwald * @text The Bluetooth logic is implemented within the 138bcf00d8fSMatthias Ringwald * packet handler, see Listing SppServerPacketHandler. In this example, 139bcf00d8fSMatthias Ringwald * the following events are passed sequentially: 140bcf00d8fSMatthias Ringwald * - BTSTACK_EVENT_STATE, 141bcf00d8fSMatthias Ringwald * - HCI_EVENT_PIN_CODE_REQUEST (Standard pairing) or 142bcf00d8fSMatthias Ringwald * - HCI_EVENT_USER_CONFIRMATION_REQUEST (Secure Simple Pairing), 143bcf00d8fSMatthias Ringwald * - RFCOMM_EVENT_INCOMING_CONNECTION, 144bcf00d8fSMatthias Ringwald * - RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE, and 145bcf00d8fSMatthias Ringwald * - RFCOMM_EVENT_CHANNEL_CLOSED 146bcf00d8fSMatthias Ringwald */ 147bcf00d8fSMatthias Ringwald 148bcf00d8fSMatthias Ringwald /* @text Upon receiving HCI_EVENT_PIN_CODE_REQUEST event, we need to handle 149bcf00d8fSMatthias Ringwald * authentication. Here, we use a fixed PIN code "0000". 150bcf00d8fSMatthias Ringwald * 151bcf00d8fSMatthias Ringwald * When HCI_EVENT_USER_CONFIRMATION_REQUEST is received, the user will be 152bcf00d8fSMatthias Ringwald * asked to accept the pairing request. If the IO capability is set to 153bcf00d8fSMatthias Ringwald * SSP_IO_CAPABILITY_DISPLAY_YES_NO, the request will be automatically accepted. 154bcf00d8fSMatthias Ringwald * 155bcf00d8fSMatthias Ringwald * The RFCOMM_EVENT_INCOMING_CONNECTION event indicates an incoming connection. 156bcf00d8fSMatthias Ringwald * Here, the connection is accepted. More logic is need, if you want to handle connections 157bcf00d8fSMatthias Ringwald * from multiple clients. The incoming RFCOMM connection event contains the RFCOMM 158bcf00d8fSMatthias Ringwald * channel number used during the SPP setup phase and the newly assigned RFCOMM 159bcf00d8fSMatthias Ringwald * channel ID that is used by all BTstack commands and events. 160bcf00d8fSMatthias Ringwald * 161bcf00d8fSMatthias Ringwald * If RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE event returns status greater then 0, 162bcf00d8fSMatthias Ringwald * then the channel establishment has failed (rare case, e.g., client crashes). 163bcf00d8fSMatthias Ringwald * On successful connection, the RFCOMM channel ID and MTU for this 164bcf00d8fSMatthias Ringwald * channel are made available to the heartbeat counter. After opening the RFCOMM channel, 165bcf00d8fSMatthias Ringwald * the communication between client and the application 166bcf00d8fSMatthias Ringwald * takes place. In this example, the timer handler increases the real counter every 167bcf00d8fSMatthias Ringwald * second. 168bcf00d8fSMatthias Ringwald */ 169bcf00d8fSMatthias Ringwald 170bcf00d8fSMatthias Ringwald /* LISTING_START(SppServerPacketHandler): SPP Server - Heartbeat Counter over RFCOMM */ 171bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 172bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 173bcf00d8fSMatthias Ringwald bd_addr_t event_addr; 174bcf00d8fSMatthias Ringwald uint8_t rfcomm_channel_nr; 175bcf00d8fSMatthias Ringwald uint16_t mtu; 176bcf00d8fSMatthias Ringwald int i; 177bcf00d8fSMatthias Ringwald 178bcf00d8fSMatthias Ringwald switch (packet_type) { 179bcf00d8fSMatthias Ringwald case HCI_EVENT_PACKET: 180*0e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 181bcf00d8fSMatthias Ringwald 182bcf00d8fSMatthias Ringwald case BTSTACK_EVENT_STATE: 183bcf00d8fSMatthias Ringwald // BTstack activated, get started 184bcf00d8fSMatthias Ringwald if (packet[2] == HCI_STATE_WORKING) { 185bcf00d8fSMatthias Ringwald printf("BTstack is up and running\n"); 186bcf00d8fSMatthias Ringwald } 187bcf00d8fSMatthias Ringwald break; 188bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 189bcf00d8fSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 190bcf00d8fSMatthias Ringwald // pre-ssp: inform about pin code request 191bcf00d8fSMatthias Ringwald printf("Pin code request - using '0000'\n"); 192bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[2], event_addr); 193bcf00d8fSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 194bcf00d8fSMatthias Ringwald break; 195bcf00d8fSMatthias Ringwald 196bcf00d8fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 197bcf00d8fSMatthias Ringwald // ssp: inform about user confirmation request 198bcf00d8fSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 199bcf00d8fSMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 200bcf00d8fSMatthias Ringwald break; 201bcf00d8fSMatthias Ringwald 202bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 203bcf00d8fSMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 204bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[2], event_addr); 205bcf00d8fSMatthias Ringwald rfcomm_channel_nr = packet[8]; 206bcf00d8fSMatthias Ringwald rfcomm_channel_id = little_endian_read_16(packet, 9); 207bcf00d8fSMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 208bcf00d8fSMatthias Ringwald rfcomm_accept_connection(rfcomm_channel_id); 209bcf00d8fSMatthias Ringwald break; 210bcf00d8fSMatthias Ringwald 211bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: 212bcf00d8fSMatthias Ringwald // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 213bcf00d8fSMatthias Ringwald if (packet[2]) { 214bcf00d8fSMatthias Ringwald printf("RFCOMM channel open failed, status %u\n", packet[2]); 215bcf00d8fSMatthias Ringwald } else { 216bcf00d8fSMatthias Ringwald rfcomm_channel_id = little_endian_read_16(packet, 12); 217bcf00d8fSMatthias Ringwald mtu = little_endian_read_16(packet, 14); 218bcf00d8fSMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu); 219bcf00d8fSMatthias Ringwald } 220bcf00d8fSMatthias Ringwald break; 221bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 222bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 223bcf00d8fSMatthias Ringwald printf("RFCOMM channel closed\n"); 224bcf00d8fSMatthias Ringwald rfcomm_channel_id = 0; 225bcf00d8fSMatthias Ringwald break; 226bcf00d8fSMatthias Ringwald 227bcf00d8fSMatthias Ringwald default: 228bcf00d8fSMatthias Ringwald break; 229bcf00d8fSMatthias Ringwald } 230bcf00d8fSMatthias Ringwald break; 231bcf00d8fSMatthias Ringwald 232bcf00d8fSMatthias Ringwald case RFCOMM_DATA_PACKET: 233bcf00d8fSMatthias Ringwald printf("RCV: '"); 234bcf00d8fSMatthias Ringwald for (i=0;i<size;i++){ 235bcf00d8fSMatthias Ringwald putchar(packet[i]); 236bcf00d8fSMatthias Ringwald } 237bcf00d8fSMatthias Ringwald printf("'\n"); 238bcf00d8fSMatthias Ringwald break; 239bcf00d8fSMatthias Ringwald 240bcf00d8fSMatthias Ringwald default: 241bcf00d8fSMatthias Ringwald break; 242bcf00d8fSMatthias Ringwald } 243bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 244bcf00d8fSMatthias Ringwald } 245bcf00d8fSMatthias Ringwald /* LISTING_END */ 246bcf00d8fSMatthias Ringwald 247bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 248bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 249bcf00d8fSMatthias Ringwald one_shot_timer_setup(); 250bcf00d8fSMatthias Ringwald spp_service_setup(); 251bcf00d8fSMatthias Ringwald 252bcf00d8fSMatthias Ringwald gap_discoverable_control(1); 253bcf00d8fSMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 254bcf00d8fSMatthias Ringwald gap_set_local_name("BTstack SPP Counter"); 255bcf00d8fSMatthias Ringwald 256bcf00d8fSMatthias Ringwald // turn on! 257bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 258bcf00d8fSMatthias Ringwald 259bcf00d8fSMatthias Ringwald return 0; 260bcf00d8fSMatthias Ringwald } 261bcf00d8fSMatthias Ringwald /* EXAMPLE_END */ 262bcf00d8fSMatthias Ringwald 263