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