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__ "spp_counter.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(spp_counter): SPP Server - Heartbeat Counter over RFCOMM 42 * 43 * @text The Serial port profile (SPP) is widely used as it provides a serial 44 * port over Bluetooth. The SPP counter example demonstrates how to setup an SPP 45 * service, and provide a periodic timer over RFCOMM. 46 */ 47 // ***************************************************************************** 48 49 #include <inttypes.h> 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include "btstack.h" 56 57 #define RFCOMM_SERVER_CHANNEL 1 58 #define HEARTBEAT_PERIOD_MS 1000 59 60 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 61 62 static uint16_t rfcomm_channel_id; 63 static uint8_t spp_service_buffer[150]; 64 static btstack_packet_callback_registration_t hci_event_callback_registration; 65 66 67 /* @section SPP Service Setup 68 *s 69 * @text To provide an SPP service, the L2CAP, RFCOMM, and SDP protocol layers 70 * are required. After setting up an RFCOMM service with channel nubmer 71 * RFCOMM_SERVER_CHANNEL, an SDP record is created and registered with the SDP server. 72 * Example code for SPP service setup is 73 * provided in Listing SPPSetup. The SDP record created by function 74 * spp_create_sdp_record consists of a basic SPP definition that uses the provided 75 * RFCOMM channel ID and service name. For more details, please have a look at it 76 * in \path{src/sdp_util.c}. 77 * The SDP record is created on the fly in RAM and is deterministic. 78 * To preserve valuable RAM, the result could be stored as constant data inside the ROM. 79 */ 80 81 /* LISTING_START(SPPSetup): SPP service setup */ 82 static void spp_service_setup(void){ 83 84 // register for HCI events 85 hci_event_callback_registration.callback = &packet_handler; 86 hci_add_event_handler(&hci_event_callback_registration); 87 88 l2cap_init(); 89 90 rfcomm_init(); 91 rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); // reserved channel, mtu limited by l2cap 92 93 // init SDP, create record for SPP and register with SDP 94 sdp_init(); 95 memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 96 spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter"); 97 sdp_register_service(spp_service_buffer); 98 printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); 99 } 100 /* LISTING_END */ 101 102 /* @section Periodic Timer Setup 103 * 104 * @text The heartbeat handler increases the real counter every second, 105 * and sends a text string with the counter value, as shown in Listing PeriodicCounter. 106 */ 107 108 /* LISTING_START(PeriodicCounter): Periodic Counter */ 109 static btstack_timer_source_t heartbeat; 110 static char lineBuffer[30]; 111 static void heartbeat_handler(struct btstack_timer_source *ts){ 112 static int counter = 0; 113 114 if (rfcomm_channel_id){ 115 sprintf(lineBuffer, "BTstack counter %04u\n", ++counter); 116 printf("%s", lineBuffer); 117 118 rfcomm_request_can_send_now_event(rfcomm_channel_id); 119 } 120 121 btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 122 btstack_run_loop_add_timer(ts); 123 } 124 125 static void one_shot_timer_setup(void){ 126 // set one-shot timer 127 heartbeat.process = &heartbeat_handler; 128 btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 129 btstack_run_loop_add_timer(&heartbeat); 130 } 131 /* LISTING_END */ 132 133 134 /* @section Bluetooth Logic 135 * @text The Bluetooth logic is implemented within the 136 * packet handler, see Listing SppServerPacketHandler. In this example, 137 * the following events are passed sequentially: 138 * - BTSTACK_EVENT_STATE, 139 * - HCI_EVENT_PIN_CODE_REQUEST (Standard pairing) or 140 * - HCI_EVENT_USER_CONFIRMATION_REQUEST (Secure Simple Pairing), 141 * - RFCOMM_EVENT_INCOMING_CONNECTION, 142 * - RFCOMM_EVENT_CHANNEL_OPENED, 143 * - RFCOMM_EVETN_CAN_SEND_NOW, and 144 * - RFCOMM_EVENT_CHANNEL_CLOSED 145 */ 146 147 /* @text Upon receiving HCI_EVENT_PIN_CODE_REQUEST event, we need to handle 148 * authentication. Here, we use a fixed PIN code "0000". 149 * 150 * When HCI_EVENT_USER_CONFIRMATION_REQUEST is received, the user will be 151 * asked to accept the pairing request. If the IO capability is set to 152 * SSP_IO_CAPABILITY_DISPLAY_YES_NO, the request will be automatically accepted. 153 * 154 * The RFCOMM_EVENT_INCOMING_CONNECTION event indicates an incoming connection. 155 * Here, the connection is accepted. More logic is need, if you want to handle connections 156 * from multiple clients. The incoming RFCOMM connection event contains the RFCOMM 157 * channel number used during the SPP setup phase and the newly assigned RFCOMM 158 * channel ID that is used by all BTstack commands and events. 159 * 160 * If RFCOMM_EVENT_CHANNEL_OPENED event returns status greater then 0, 161 * then the channel establishment has failed (rare case, e.g., client crashes). 162 * On successful connection, the RFCOMM channel ID and MTU for this 163 * channel are made available to the heartbeat counter. After opening the RFCOMM channel, 164 * the communication between client and the application 165 * takes place. In this example, the timer handler increases the real counter every 166 * second. 167 * 168 * RFCOMM_EVENT_CAN_SEND_NOW indicates that it's possible to send an RFCOMM packet 169 * on the rfcomm_cid that is include 170 171 */ 172 173 /* LISTING_START(SppServerPacketHandler): SPP Server - Heartbeat Counter over RFCOMM */ 174 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 175 UNUSED(channel); 176 177 /* LISTING_PAUSE */ 178 bd_addr_t event_addr; 179 uint8_t rfcomm_channel_nr; 180 uint16_t mtu; 181 int i; 182 183 switch (packet_type) { 184 case HCI_EVENT_PACKET: 185 switch (hci_event_packet_get_type(packet)) { 186 /* LISTING_RESUME */ 187 case HCI_EVENT_PIN_CODE_REQUEST: 188 // inform about pin code request 189 printf("Pin code request - using '0000'\n"); 190 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 191 gap_pin_code_response(event_addr, "0000"); 192 break; 193 194 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 195 // ssp: inform about user confirmation request 196 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 197 printf("SSP User Confirmation Auto accept\n"); 198 break; 199 200 case RFCOMM_EVENT_INCOMING_CONNECTION: 201 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 202 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 203 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 204 rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 205 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 206 rfcomm_accept_connection(rfcomm_channel_id); 207 break; 208 209 case RFCOMM_EVENT_CHANNEL_OPENED: 210 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 211 if (rfcomm_event_channel_opened_get_status(packet)) { 212 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 213 } else { 214 rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 215 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 216 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu); 217 } 218 break; 219 case RFCOMM_EVENT_CAN_SEND_NOW: 220 rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer)); 221 break; 222 223 /* LISTING_PAUSE */ 224 case RFCOMM_EVENT_CHANNEL_CLOSED: 225 printf("RFCOMM channel closed\n"); 226 rfcomm_channel_id = 0; 227 break; 228 229 default: 230 break; 231 } 232 break; 233 234 case RFCOMM_DATA_PACKET: 235 printf("RCV: '"); 236 for (i=0;i<size;i++){ 237 putchar(packet[i]); 238 } 239 printf("'\n"); 240 break; 241 242 default: 243 break; 244 } 245 /* LISTING_RESUME */ 246 } 247 /* LISTING_END */ 248 249 int btstack_main(int argc, const char * argv[]); 250 int btstack_main(int argc, const char * argv[]){ 251 (void)argc; 252 (void)argv; 253 254 one_shot_timer_setup(); 255 spp_service_setup(); 256 257 gap_discoverable_control(1); 258 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 259 gap_set_local_name("SPP Counter 00:00:00:00:00:00"); 260 261 // turn on! 262 hci_power_control(HCI_POWER_ON); 263 264 return 0; 265 } 266 /* EXAMPLE_END */ 267 268