1415a769fSMatthias Ringwald /*
2415a769fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
3415a769fSMatthias Ringwald *
4415a769fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
5415a769fSMatthias Ringwald * modification, are permitted provided that the following conditions
6415a769fSMatthias Ringwald * are met:
7415a769fSMatthias Ringwald *
8415a769fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
9415a769fSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
10415a769fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
11415a769fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
12415a769fSMatthias Ringwald * documentation and/or other materials provided with the distribution.
13415a769fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
14415a769fSMatthias Ringwald * contributors may be used to endorse or promote products derived
15415a769fSMatthias Ringwald * from this software without specific prior written permission.
16415a769fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
17415a769fSMatthias Ringwald * personal benefit and not for any commercial purpose or for
18415a769fSMatthias Ringwald * monetary gain.
19415a769fSMatthias Ringwald *
20415a769fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21415a769fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22415a769fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*7fbe4b77SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*7fbe4b77SMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25415a769fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26415a769fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27415a769fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28415a769fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29415a769fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30415a769fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31415a769fSMatthias Ringwald * SUCH DAMAGE.
32415a769fSMatthias Ringwald *
33415a769fSMatthias Ringwald * Please inquire about commercial licensing options at
34415a769fSMatthias Ringwald * [email protected]
35415a769fSMatthias Ringwald *
36415a769fSMatthias Ringwald */
37415a769fSMatthias Ringwald
38415a769fSMatthias Ringwald #define BTSTACK_FILE__ "spp_counter.c"
39415a769fSMatthias Ringwald
40415a769fSMatthias Ringwald // *****************************************************************************
41415a769fSMatthias Ringwald /* EXAMPLE_START(spp_counter): SPP Server - Heartbeat Counter over RFCOMM
42415a769fSMatthias Ringwald *
43415a769fSMatthias Ringwald * @text The Serial port profile (SPP) is widely used as it provides a serial
44415a769fSMatthias Ringwald * port over Bluetooth. The SPP counter example demonstrates how to setup an SPP
45415a769fSMatthias Ringwald * service, and provide a periodic timer over RFCOMM.
46415a769fSMatthias Ringwald *
47415a769fSMatthias Ringwald * @text Note: To test, please run the spp_counter example, and then pair from
48415a769fSMatthias Ringwald * a remote device, and open the Virtual Serial Port.
49415a769fSMatthias Ringwald */
50415a769fSMatthias Ringwald // *****************************************************************************
51415a769fSMatthias Ringwald
52415a769fSMatthias Ringwald #include <inttypes.h>
53415a769fSMatthias Ringwald #include <stdint.h>
54415a769fSMatthias Ringwald #include <stdio.h>
55415a769fSMatthias Ringwald #include <stdlib.h>
56415a769fSMatthias Ringwald #include <string.h>
57415a769fSMatthias Ringwald
58415a769fSMatthias Ringwald #include "btstack.h"
59415a769fSMatthias Ringwald
60415a769fSMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1
61415a769fSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 1000
62415a769fSMatthias Ringwald
63415a769fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
64415a769fSMatthias Ringwald
65415a769fSMatthias Ringwald static uint16_t rfcomm_channel_id;
66415a769fSMatthias Ringwald static uint8_t spp_service_buffer[150];
67415a769fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
68415a769fSMatthias Ringwald
69415a769fSMatthias Ringwald
70415a769fSMatthias Ringwald /* @section SPP Service Setup
71415a769fSMatthias Ringwald *s
72415a769fSMatthias Ringwald * @text To provide an SPP service, the L2CAP, RFCOMM, and SDP protocol layers
73415a769fSMatthias Ringwald * are required. After setting up an RFCOMM service with channel nubmer
74415a769fSMatthias Ringwald * RFCOMM_SERVER_CHANNEL, an SDP record is created and registered with the SDP server.
75415a769fSMatthias Ringwald * Example code for SPP service setup is
76415a769fSMatthias Ringwald * provided in Listing SPPSetup. The SDP record created by function
77415a769fSMatthias Ringwald * spp_create_sdp_record consists of a basic SPP definition that uses the provided
78415a769fSMatthias Ringwald * RFCOMM channel ID and service name. For more details, please have a look at it
79415a769fSMatthias Ringwald * in \path{src/sdp_util.c}.
80415a769fSMatthias Ringwald * The SDP record is created on the fly in RAM and is deterministic.
81415a769fSMatthias Ringwald * To preserve valuable RAM, the result could be stored as constant data inside the ROM.
82415a769fSMatthias Ringwald */
83415a769fSMatthias Ringwald
84415a769fSMatthias Ringwald /* LISTING_START(SPPSetup): SPP service setup */
spp_service_setup(void)85415a769fSMatthias Ringwald static void spp_service_setup(void){
86415a769fSMatthias Ringwald
87415a769fSMatthias Ringwald // register for HCI events
88415a769fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
89415a769fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
90415a769fSMatthias Ringwald
91415a769fSMatthias Ringwald l2cap_init();
92415a769fSMatthias Ringwald
93*7fbe4b77SMatthias Ringwald #ifdef ENABLE_BLE
94*7fbe4b77SMatthias Ringwald // Initialize LE Security Manager. Needed for cross-transport key derivation
95*7fbe4b77SMatthias Ringwald sm_init();
96*7fbe4b77SMatthias Ringwald #endif
97*7fbe4b77SMatthias Ringwald
98415a769fSMatthias Ringwald rfcomm_init();
99415a769fSMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); // reserved channel, mtu limited by l2cap
100415a769fSMatthias Ringwald
101415a769fSMatthias Ringwald // init SDP, create record for SPP and register with SDP
102415a769fSMatthias Ringwald sdp_init();
103415a769fSMatthias Ringwald memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
104415a769fSMatthias Ringwald spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
105415a769fSMatthias Ringwald sdp_register_service(spp_service_buffer);
106*7fbe4b77SMatthias Ringwald printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
107415a769fSMatthias Ringwald }
108415a769fSMatthias Ringwald /* LISTING_END */
109415a769fSMatthias Ringwald
110415a769fSMatthias Ringwald /* @section Periodic Timer Setup
111415a769fSMatthias Ringwald *
112415a769fSMatthias Ringwald * @text The heartbeat handler increases the real counter every second,
113415a769fSMatthias Ringwald * and sends a text string with the counter value, as shown in Listing PeriodicCounter.
114415a769fSMatthias Ringwald */
115415a769fSMatthias Ringwald
116415a769fSMatthias Ringwald /* LISTING_START(PeriodicCounter): Periodic Counter */
117415a769fSMatthias Ringwald static btstack_timer_source_t heartbeat;
118415a769fSMatthias Ringwald static char lineBuffer[30];
heartbeat_handler(struct btstack_timer_source * ts)119415a769fSMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){
120415a769fSMatthias Ringwald static int counter = 0;
121415a769fSMatthias Ringwald
122415a769fSMatthias Ringwald if (rfcomm_channel_id){
123*7fbe4b77SMatthias Ringwald snprintf(lineBuffer, sizeof(lineBuffer), "BTstack counter %04u\n", ++counter);
124*7fbe4b77SMatthias Ringwald printf("%s", lineBuffer);
125415a769fSMatthias Ringwald
126415a769fSMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_channel_id);
127415a769fSMatthias Ringwald }
128415a769fSMatthias Ringwald
129415a769fSMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
130415a769fSMatthias Ringwald btstack_run_loop_add_timer(ts);
131415a769fSMatthias Ringwald }
132415a769fSMatthias Ringwald
one_shot_timer_setup(void)133415a769fSMatthias Ringwald static void one_shot_timer_setup(void){
134415a769fSMatthias Ringwald // set one-shot timer
135415a769fSMatthias Ringwald heartbeat.process = &heartbeat_handler;
136415a769fSMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
137415a769fSMatthias Ringwald btstack_run_loop_add_timer(&heartbeat);
138415a769fSMatthias Ringwald }
139415a769fSMatthias Ringwald /* LISTING_END */
140415a769fSMatthias Ringwald
141415a769fSMatthias Ringwald
142415a769fSMatthias Ringwald /* @section Bluetooth Logic
143415a769fSMatthias Ringwald * @text The Bluetooth logic is implemented within the
144415a769fSMatthias Ringwald * packet handler, see Listing SppServerPacketHandler. In this example,
145415a769fSMatthias Ringwald * the following events are passed sequentially:
146415a769fSMatthias Ringwald * - BTSTACK_EVENT_STATE,
147415a769fSMatthias Ringwald * - HCI_EVENT_PIN_CODE_REQUEST (Standard pairing) or
148415a769fSMatthias Ringwald * - HCI_EVENT_USER_CONFIRMATION_REQUEST (Secure Simple Pairing),
149415a769fSMatthias Ringwald * - RFCOMM_EVENT_INCOMING_CONNECTION,
150415a769fSMatthias Ringwald * - RFCOMM_EVENT_CHANNEL_OPENED,
151415a769fSMatthias Ringwald * - RFCOMM_EVETN_CAN_SEND_NOW, and
152415a769fSMatthias Ringwald * - RFCOMM_EVENT_CHANNEL_CLOSED
153415a769fSMatthias Ringwald */
154415a769fSMatthias Ringwald
155415a769fSMatthias Ringwald /* @text Upon receiving HCI_EVENT_PIN_CODE_REQUEST event, we need to handle
156415a769fSMatthias Ringwald * authentication. Here, we use a fixed PIN code "0000".
157415a769fSMatthias Ringwald *
158415a769fSMatthias Ringwald * When HCI_EVENT_USER_CONFIRMATION_REQUEST is received, the user will be
159415a769fSMatthias Ringwald * asked to accept the pairing request. If the IO capability is set to
160415a769fSMatthias Ringwald * SSP_IO_CAPABILITY_DISPLAY_YES_NO, the request will be automatically accepted.
161415a769fSMatthias Ringwald *
162415a769fSMatthias Ringwald * The RFCOMM_EVENT_INCOMING_CONNECTION event indicates an incoming connection.
163415a769fSMatthias Ringwald * Here, the connection is accepted. More logic is need, if you want to handle connections
164415a769fSMatthias Ringwald * from multiple clients. The incoming RFCOMM connection event contains the RFCOMM
165415a769fSMatthias Ringwald * channel number used during the SPP setup phase and the newly assigned RFCOMM
166415a769fSMatthias Ringwald * channel ID that is used by all BTstack commands and events.
167415a769fSMatthias Ringwald *
168415a769fSMatthias Ringwald * If RFCOMM_EVENT_CHANNEL_OPENED event returns status greater then 0,
169415a769fSMatthias Ringwald * then the channel establishment has failed (rare case, e.g., client crashes).
170415a769fSMatthias Ringwald * On successful connection, the RFCOMM channel ID and MTU for this
171415a769fSMatthias Ringwald * channel are made available to the heartbeat counter. After opening the RFCOMM channel,
172415a769fSMatthias Ringwald * the communication between client and the application
173415a769fSMatthias Ringwald * takes place. In this example, the timer handler increases the real counter every
174415a769fSMatthias Ringwald * second.
175415a769fSMatthias Ringwald *
176415a769fSMatthias Ringwald * RFCOMM_EVENT_CAN_SEND_NOW indicates that it's possible to send an RFCOMM packet
177415a769fSMatthias Ringwald * on the rfcomm_cid that is include
178415a769fSMatthias Ringwald
179415a769fSMatthias Ringwald */
180415a769fSMatthias Ringwald
181415a769fSMatthias Ringwald /* LISTING_START(SppServerPacketHandler): SPP Server - Heartbeat Counter over RFCOMM */
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)182415a769fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
183415a769fSMatthias Ringwald UNUSED(channel);
184415a769fSMatthias Ringwald
185415a769fSMatthias Ringwald /* LISTING_PAUSE */
186415a769fSMatthias Ringwald bd_addr_t event_addr;
187*7fbe4b77SMatthias Ringwald uint8_t rfcomm_channel_nr;
188*7fbe4b77SMatthias Ringwald uint16_t mtu;
189*7fbe4b77SMatthias Ringwald int i;
190415a769fSMatthias Ringwald
191415a769fSMatthias Ringwald switch (packet_type) {
192415a769fSMatthias Ringwald case HCI_EVENT_PACKET:
193415a769fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) {
194415a769fSMatthias Ringwald /* LISTING_RESUME */
195415a769fSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST:
196415a769fSMatthias Ringwald // inform about pin code request
197*7fbe4b77SMatthias Ringwald printf("Pin code request - using '0000'\n");
198415a769fSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr);
199415a769fSMatthias Ringwald gap_pin_code_response(event_addr, "0000");
200415a769fSMatthias Ringwald break;
201415a769fSMatthias Ringwald
202415a769fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST:
203415a769fSMatthias Ringwald // ssp: inform about user confirmation request
204*7fbe4b77SMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
205*7fbe4b77SMatthias Ringwald printf("SSP User Confirmation Auto accept\n");
206415a769fSMatthias Ringwald break;
207415a769fSMatthias Ringwald
208415a769fSMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION:
209415a769fSMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
210*7fbe4b77SMatthias Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
211415a769fSMatthias Ringwald rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
212*7fbe4b77SMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
213415a769fSMatthias Ringwald rfcomm_accept_connection(rfcomm_channel_id);
214415a769fSMatthias Ringwald break;
215415a769fSMatthias Ringwald
216415a769fSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED:
217415a769fSMatthias Ringwald if (rfcomm_event_channel_opened_get_status(packet)) {
218*7fbe4b77SMatthias Ringwald printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
219415a769fSMatthias Ringwald } else {
220415a769fSMatthias Ringwald rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
221*7fbe4b77SMatthias Ringwald mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
222*7fbe4b77SMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
223415a769fSMatthias Ringwald }
224415a769fSMatthias Ringwald break;
225415a769fSMatthias Ringwald case RFCOMM_EVENT_CAN_SEND_NOW:
226*7fbe4b77SMatthias Ringwald rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, (uint16_t) strlen(lineBuffer));
227415a769fSMatthias Ringwald break;
228415a769fSMatthias Ringwald
229415a769fSMatthias Ringwald /* LISTING_PAUSE */
230415a769fSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED:
231*7fbe4b77SMatthias Ringwald printf("RFCOMM channel closed\n");
232415a769fSMatthias Ringwald rfcomm_channel_id = 0;
233415a769fSMatthias Ringwald break;
234415a769fSMatthias Ringwald
235415a769fSMatthias Ringwald default:
236415a769fSMatthias Ringwald break;
237415a769fSMatthias Ringwald }
238415a769fSMatthias Ringwald break;
239415a769fSMatthias Ringwald
240415a769fSMatthias Ringwald case RFCOMM_DATA_PACKET:
241*7fbe4b77SMatthias Ringwald printf("RCV: '");
242*7fbe4b77SMatthias Ringwald for (i=0;i<size;i++){
243*7fbe4b77SMatthias Ringwald putchar(packet[i]);
244*7fbe4b77SMatthias Ringwald }
245*7fbe4b77SMatthias Ringwald printf("'\n");
246415a769fSMatthias Ringwald break;
247415a769fSMatthias Ringwald
248415a769fSMatthias Ringwald default:
249415a769fSMatthias Ringwald break;
250415a769fSMatthias Ringwald }
251415a769fSMatthias Ringwald /* LISTING_RESUME */
252415a769fSMatthias Ringwald }
253415a769fSMatthias Ringwald /* LISTING_END */
254415a769fSMatthias Ringwald
255415a769fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])256415a769fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
257415a769fSMatthias Ringwald (void)argc;
258415a769fSMatthias Ringwald (void)argv;
259415a769fSMatthias Ringwald
260415a769fSMatthias Ringwald one_shot_timer_setup();
261415a769fSMatthias Ringwald spp_service_setup();
262415a769fSMatthias Ringwald
263415a769fSMatthias Ringwald gap_discoverable_control(1);
264415a769fSMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
265415a769fSMatthias Ringwald gap_set_local_name("SPP Counter 00:00:00:00:00:00");
266415a769fSMatthias Ringwald
267415a769fSMatthias Ringwald // turn on!
268415a769fSMatthias Ringwald hci_power_control(HCI_POWER_ON);
269415a769fSMatthias Ringwald
270415a769fSMatthias Ringwald return 0;
271415a769fSMatthias Ringwald }
272415a769fSMatthias Ringwald /* EXAMPLE_END */
273415a769fSMatthias Ringwald
274