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