xref: /btstack/example/spp_counter.c (revision 9ec2630ce4bfd36adcb438d2e8ba6fc26890f857)
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 
530e2df43fSMatthias 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
72efda0b48SMatthias Ringwald  * spp_create_sdp_record 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));
94efda0b48SMatthias Ringwald     spp_create_sdp_record(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;
108a31ff992SMatthias Ringwald static char lineBuffer[30];
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         sprintf(lineBuffer, "BTstack counter %04u\n", ++counter);
114bcf00d8fSMatthias Ringwald         printf("%s", lineBuffer);
115a31ff992SMatthias Ringwald 
116a31ff992SMatthias Ringwald         rfcomm_request_can_send_now_event(rfcomm_channel_id);
117bcf00d8fSMatthias Ringwald     }
118a31ff992SMatthias Ringwald 
119bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
120bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(ts);
121bcf00d8fSMatthias Ringwald }
122bcf00d8fSMatthias Ringwald 
123bcf00d8fSMatthias Ringwald static void one_shot_timer_setup(void){
124bcf00d8fSMatthias Ringwald     // set one-shot timer
125bcf00d8fSMatthias Ringwald     heartbeat.process = &heartbeat_handler;
126bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
127bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
128bcf00d8fSMatthias Ringwald }
129bcf00d8fSMatthias Ringwald /* LISTING_END */
130bcf00d8fSMatthias Ringwald 
131bcf00d8fSMatthias Ringwald 
132bcf00d8fSMatthias Ringwald /* @section Bluetooth Logic
133bcf00d8fSMatthias Ringwald  * @text The Bluetooth logic is implemented within the
134bcf00d8fSMatthias Ringwald  * packet handler, see Listing SppServerPacketHandler. In this example,
135bcf00d8fSMatthias Ringwald  * the following events are passed sequentially:
136bcf00d8fSMatthias Ringwald  * - BTSTACK_EVENT_STATE,
137bcf00d8fSMatthias Ringwald  * - HCI_EVENT_PIN_CODE_REQUEST (Standard pairing) or
138bcf00d8fSMatthias Ringwald  * - HCI_EVENT_USER_CONFIRMATION_REQUEST (Secure Simple Pairing),
139bcf00d8fSMatthias Ringwald  * - RFCOMM_EVENT_INCOMING_CONNECTION,
140a31ff992SMatthias Ringwald  * - RFCOMM_EVENT_CHANNEL_OPENED,
141a31ff992SMatthias Ringwald * - RFCOMM_EVETN_CAN_SEND_NOW, and
142bcf00d8fSMatthias Ringwald  * - RFCOMM_EVENT_CHANNEL_CLOSED
143bcf00d8fSMatthias Ringwald  */
144bcf00d8fSMatthias Ringwald 
145bcf00d8fSMatthias Ringwald /* @text Upon receiving HCI_EVENT_PIN_CODE_REQUEST event, we need to handle
146bcf00d8fSMatthias Ringwald  * authentication. Here, we use a fixed PIN code "0000".
147bcf00d8fSMatthias Ringwald  *
148bcf00d8fSMatthias Ringwald  * When HCI_EVENT_USER_CONFIRMATION_REQUEST is received, the user will be
149bcf00d8fSMatthias Ringwald  * asked to accept the pairing request. If the IO capability is set to
150bcf00d8fSMatthias Ringwald  * SSP_IO_CAPABILITY_DISPLAY_YES_NO, the request will be automatically accepted.
151bcf00d8fSMatthias Ringwald  *
152bcf00d8fSMatthias Ringwald  * The RFCOMM_EVENT_INCOMING_CONNECTION event indicates an incoming connection.
153bcf00d8fSMatthias Ringwald  * Here, the connection is accepted. More logic is need, if you want to handle connections
154bcf00d8fSMatthias Ringwald  * from multiple clients. The incoming RFCOMM connection event contains the RFCOMM
155bcf00d8fSMatthias Ringwald  * channel number used during the SPP setup phase and the newly assigned RFCOMM
156bcf00d8fSMatthias Ringwald  * channel ID that is used by all BTstack commands and events.
157bcf00d8fSMatthias Ringwald  *
158f8f6a918SMatthias Ringwald  * If RFCOMM_EVENT_CHANNEL_OPENED event returns status greater then 0,
159bcf00d8fSMatthias Ringwald  * then the channel establishment has failed (rare case, e.g., client crashes).
160bcf00d8fSMatthias Ringwald  * On successful connection, the RFCOMM channel ID and MTU for this
161bcf00d8fSMatthias Ringwald  * channel are made available to the heartbeat counter. After opening the RFCOMM channel,
162bcf00d8fSMatthias Ringwald  * the communication between client and the application
163bcf00d8fSMatthias Ringwald  * takes place. In this example, the timer handler increases the real counter every
164bcf00d8fSMatthias Ringwald  * second.
165a31ff992SMatthias Ringwald  *
166a31ff992SMatthias Ringwald  * RFCOMM_EVENT_CAN_SEND_NOW indicates that it's possible to send an RFCOMM packet
167a31ff992SMatthias Ringwald  * on the rfcomm_cid that is include
168a31ff992SMatthias Ringwald 
169bcf00d8fSMatthias Ringwald  */
170bcf00d8fSMatthias Ringwald 
171bcf00d8fSMatthias Ringwald /* LISTING_START(SppServerPacketHandler): SPP Server - Heartbeat Counter over RFCOMM */
172bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
173*9ec2630cSMatthias Ringwald     UNUSED(channel);
174*9ec2630cSMatthias Ringwald 
175bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */
176bcf00d8fSMatthias Ringwald     bd_addr_t event_addr;
177bcf00d8fSMatthias Ringwald     uint8_t   rfcomm_channel_nr;
178bcf00d8fSMatthias Ringwald     uint16_t  mtu;
179bcf00d8fSMatthias Ringwald     int i;
180bcf00d8fSMatthias Ringwald 
181bcf00d8fSMatthias Ringwald     switch (packet_type) {
182bcf00d8fSMatthias Ringwald         case HCI_EVENT_PACKET:
1830e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
184bcf00d8fSMatthias Ringwald /* LISTING_RESUME */
185bcf00d8fSMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
186bcf00d8fSMatthias Ringwald                     // pre-ssp: inform about pin code request
187bcf00d8fSMatthias Ringwald                     printf("Pin code request - using '0000'\n");
188a6ef64baSMilanka Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
189bcf00d8fSMatthias Ringwald                     hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
190bcf00d8fSMatthias Ringwald                     break;
191bcf00d8fSMatthias Ringwald 
192bcf00d8fSMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
193bcf00d8fSMatthias Ringwald                     // ssp: inform about user confirmation request
194bcf00d8fSMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
195bcf00d8fSMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
196bcf00d8fSMatthias Ringwald                     break;
197bcf00d8fSMatthias Ringwald 
198bcf00d8fSMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
199bcf00d8fSMatthias Ringwald                     // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
200caa82391SMilanka Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
201caa82391SMilanka Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
202caa82391SMilanka Ringwald                     rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
203bcf00d8fSMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
204bcf00d8fSMatthias Ringwald                     rfcomm_accept_connection(rfcomm_channel_id);
205bcf00d8fSMatthias Ringwald                     break;
206bcf00d8fSMatthias Ringwald 
207f8f6a918SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_OPENED:
208bcf00d8fSMatthias Ringwald                     // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
209caa82391SMilanka Ringwald                     if (rfcomm_event_channel_opened_get_status(packet)) {
210caa82391SMilanka Ringwald                         printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
211bcf00d8fSMatthias Ringwald                     } else {
212caa82391SMilanka Ringwald                         rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
213caa82391SMilanka Ringwald                         mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
214bcf00d8fSMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
215bcf00d8fSMatthias Ringwald                     }
216bcf00d8fSMatthias Ringwald                     break;
217a31ff992SMatthias Ringwald                 case RFCOMM_EVENT_CAN_SEND_NOW:
218a31ff992SMatthias Ringwald                     rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer));
219a31ff992SMatthias Ringwald                     break;
220a31ff992SMatthias Ringwald 
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[]){
249*9ec2630cSMatthias Ringwald     (void)argc;
250*9ec2630cSMatthias Ringwald     (void)argv;
251*9ec2630cSMatthias Ringwald 
252bcf00d8fSMatthias Ringwald     one_shot_timer_setup();
253bcf00d8fSMatthias Ringwald     spp_service_setup();
254bcf00d8fSMatthias Ringwald 
255bcf00d8fSMatthias Ringwald     gap_discoverable_control(1);
256bcf00d8fSMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
257bcf00d8fSMatthias Ringwald     gap_set_local_name("BTstack SPP Counter");
258bcf00d8fSMatthias Ringwald 
259bcf00d8fSMatthias Ringwald     // turn on!
260bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
261bcf00d8fSMatthias Ringwald 
262bcf00d8fSMatthias Ringwald     return 0;
263bcf00d8fSMatthias Ringwald }
264bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
265bcf00d8fSMatthias Ringwald 
266