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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka 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 */
spp_service_setup(void)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
938c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE
948c9bb29eSMatthias Ringwald // Initialize LE Security Manager. Needed for cross-transport key derivation
958c9bb29eSMatthias Ringwald sm_init();
968c9bb29eSMatthias Ringwald #endif
978c9bb29eSMatthias Ringwald
98bcf00d8fSMatthias Ringwald rfcomm_init();
99bcf00d8fSMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); // reserved channel, mtu limited by l2cap
100bcf00d8fSMatthias Ringwald
101bcf00d8fSMatthias Ringwald // init SDP, create record for SPP and register with SDP
102bcf00d8fSMatthias Ringwald sdp_init();
103bcf00d8fSMatthias Ringwald memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
104*762141afSMatthias Ringwald spp_create_sdp_record(spp_service_buffer, sdp_create_service_record_handle(), RFCOMM_SERVER_CHANNEL, "SPP Counter");
105*762141afSMatthias Ringwald btstack_assert(de_get_len( spp_service_buffer) <= sizeof(spp_service_buffer));
106bcf00d8fSMatthias Ringwald sdp_register_service(spp_service_buffer);
107bcf00d8fSMatthias Ringwald }
108bcf00d8fSMatthias Ringwald /* LISTING_END */
109bcf00d8fSMatthias Ringwald
110bcf00d8fSMatthias Ringwald /* @section Periodic Timer Setup
111bcf00d8fSMatthias Ringwald *
112bcf00d8fSMatthias Ringwald * @text The heartbeat handler increases the real counter every second,
113bcf00d8fSMatthias Ringwald * and sends a text string with the counter value, as shown in Listing PeriodicCounter.
114bcf00d8fSMatthias Ringwald */
115bcf00d8fSMatthias Ringwald
116bcf00d8fSMatthias Ringwald /* LISTING_START(PeriodicCounter): Periodic Counter */
117bcf00d8fSMatthias Ringwald static btstack_timer_source_t heartbeat;
118a31ff992SMatthias Ringwald static char lineBuffer[30];
heartbeat_handler(struct btstack_timer_source * ts)119bcf00d8fSMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){
120bcf00d8fSMatthias Ringwald static int counter = 0;
121bcf00d8fSMatthias Ringwald
122bcf00d8fSMatthias Ringwald if (rfcomm_channel_id){
1235d6bd48fSMatthias Ringwald snprintf(lineBuffer, sizeof(lineBuffer), "BTstack counter %04u\n", ++counter);
124bcf00d8fSMatthias Ringwald printf("%s", lineBuffer);
125a31ff992SMatthias Ringwald
126a31ff992SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_channel_id);
127bcf00d8fSMatthias Ringwald }
128a31ff992SMatthias Ringwald
129bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
130bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(ts);
131bcf00d8fSMatthias Ringwald }
132bcf00d8fSMatthias Ringwald
one_shot_timer_setup(void)133bcf00d8fSMatthias Ringwald static void one_shot_timer_setup(void){
134bcf00d8fSMatthias Ringwald // set one-shot timer
135bcf00d8fSMatthias Ringwald heartbeat.process = &heartbeat_handler;
136bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
137bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(&heartbeat);
138bcf00d8fSMatthias Ringwald }
139bcf00d8fSMatthias Ringwald /* LISTING_END */
140bcf00d8fSMatthias Ringwald
141bcf00d8fSMatthias Ringwald
142bcf00d8fSMatthias Ringwald /* @section Bluetooth Logic
143bcf00d8fSMatthias Ringwald * @text The Bluetooth logic is implemented within the
144bcf00d8fSMatthias Ringwald * packet handler, see Listing SppServerPacketHandler. In this example,
145bcf00d8fSMatthias Ringwald * the following events are passed sequentially:
146bcf00d8fSMatthias Ringwald * - BTSTACK_EVENT_STATE,
147bcf00d8fSMatthias Ringwald * - HCI_EVENT_PIN_CODE_REQUEST (Standard pairing) or
148bcf00d8fSMatthias Ringwald * - HCI_EVENT_USER_CONFIRMATION_REQUEST (Secure Simple Pairing),
149bcf00d8fSMatthias Ringwald * - RFCOMM_EVENT_INCOMING_CONNECTION,
150a31ff992SMatthias Ringwald * - RFCOMM_EVENT_CHANNEL_OPENED,
151a31ff992SMatthias Ringwald * - RFCOMM_EVETN_CAN_SEND_NOW, and
152bcf00d8fSMatthias Ringwald * - RFCOMM_EVENT_CHANNEL_CLOSED
153bcf00d8fSMatthias Ringwald */
154bcf00d8fSMatthias Ringwald
155bcf00d8fSMatthias Ringwald /* @text Upon receiving HCI_EVENT_PIN_CODE_REQUEST event, we need to handle
156bcf00d8fSMatthias Ringwald * authentication. Here, we use a fixed PIN code "0000".
157bcf00d8fSMatthias Ringwald *
158bcf00d8fSMatthias Ringwald * When HCI_EVENT_USER_CONFIRMATION_REQUEST is received, the user will be
159bcf00d8fSMatthias Ringwald * asked to accept the pairing request. If the IO capability is set to
160bcf00d8fSMatthias Ringwald * SSP_IO_CAPABILITY_DISPLAY_YES_NO, the request will be automatically accepted.
161bcf00d8fSMatthias Ringwald *
162bcf00d8fSMatthias Ringwald * The RFCOMM_EVENT_INCOMING_CONNECTION event indicates an incoming connection.
163bcf00d8fSMatthias Ringwald * Here, the connection is accepted. More logic is need, if you want to handle connections
164bcf00d8fSMatthias Ringwald * from multiple clients. The incoming RFCOMM connection event contains the RFCOMM
165bcf00d8fSMatthias Ringwald * channel number used during the SPP setup phase and the newly assigned RFCOMM
166bcf00d8fSMatthias Ringwald * channel ID that is used by all BTstack commands and events.
167bcf00d8fSMatthias Ringwald *
168f8f6a918SMatthias Ringwald * If RFCOMM_EVENT_CHANNEL_OPENED event returns status greater then 0,
169bcf00d8fSMatthias Ringwald * then the channel establishment has failed (rare case, e.g., client crashes).
170bcf00d8fSMatthias Ringwald * On successful connection, the RFCOMM channel ID and MTU for this
171bcf00d8fSMatthias Ringwald * channel are made available to the heartbeat counter. After opening the RFCOMM channel,
172bcf00d8fSMatthias Ringwald * the communication between client and the application
173bcf00d8fSMatthias Ringwald * takes place. In this example, the timer handler increases the real counter every
174bcf00d8fSMatthias Ringwald * second.
175a31ff992SMatthias Ringwald *
176a31ff992SMatthias Ringwald * RFCOMM_EVENT_CAN_SEND_NOW indicates that it's possible to send an RFCOMM packet
177a31ff992SMatthias Ringwald * on the rfcomm_cid that is include
178a31ff992SMatthias Ringwald
179bcf00d8fSMatthias Ringwald */
180bcf00d8fSMatthias Ringwald
181bcf00d8fSMatthias 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)182bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1839ec2630cSMatthias Ringwald UNUSED(channel);
1849ec2630cSMatthias Ringwald
185bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */
186bcf00d8fSMatthias Ringwald bd_addr_t event_addr;
187bcf00d8fSMatthias Ringwald uint8_t rfcomm_channel_nr;
188bcf00d8fSMatthias Ringwald uint16_t mtu;
189bcf00d8fSMatthias Ringwald int i;
190bcf00d8fSMatthias Ringwald
191bcf00d8fSMatthias Ringwald switch (packet_type) {
192bcf00d8fSMatthias Ringwald case HCI_EVENT_PACKET:
1930e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) {
194bcf00d8fSMatthias Ringwald /* LISTING_RESUME */
195bcf00d8fSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST:
19616f28706SMatthias Ringwald // inform about pin code request
197bcf00d8fSMatthias Ringwald printf("Pin code request - using '0000'\n");
198a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr);
19916f28706SMatthias Ringwald gap_pin_code_response(event_addr, "0000");
200bcf00d8fSMatthias Ringwald break;
201bcf00d8fSMatthias Ringwald
202bcf00d8fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST:
203bcf00d8fSMatthias Ringwald // ssp: inform about user confirmation request
204bcf00d8fSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
205bcf00d8fSMatthias Ringwald printf("SSP User Confirmation Auto accept\n");
206bcf00d8fSMatthias Ringwald break;
207bcf00d8fSMatthias Ringwald
208bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION:
209caa82391SMilanka Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
210caa82391SMilanka Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
211caa82391SMilanka Ringwald rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
212bcf00d8fSMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
213bcf00d8fSMatthias Ringwald rfcomm_accept_connection(rfcomm_channel_id);
214bcf00d8fSMatthias Ringwald break;
215bcf00d8fSMatthias Ringwald
216f8f6a918SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED:
217caa82391SMilanka Ringwald if (rfcomm_event_channel_opened_get_status(packet)) {
218fcd55a0bSMilanka Ringwald printf("RFCOMM channel open failed, status 0x%02x\n", rfcomm_event_channel_opened_get_status(packet));
219bcf00d8fSMatthias Ringwald } else {
220caa82391SMilanka Ringwald rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
221caa82391SMilanka Ringwald mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
222bcf00d8fSMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
223bcf00d8fSMatthias Ringwald }
224bcf00d8fSMatthias Ringwald break;
225a31ff992SMatthias Ringwald case RFCOMM_EVENT_CAN_SEND_NOW:
2265d6bd48fSMatthias Ringwald rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, (uint16_t) strlen(lineBuffer));
227a31ff992SMatthias Ringwald break;
228a31ff992SMatthias Ringwald
229bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */
230bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED:
231bcf00d8fSMatthias Ringwald printf("RFCOMM channel closed\n");
232bcf00d8fSMatthias Ringwald rfcomm_channel_id = 0;
233bcf00d8fSMatthias Ringwald break;
234bcf00d8fSMatthias Ringwald
235bcf00d8fSMatthias Ringwald default:
236bcf00d8fSMatthias Ringwald break;
237bcf00d8fSMatthias Ringwald }
238bcf00d8fSMatthias Ringwald break;
239bcf00d8fSMatthias Ringwald
240bcf00d8fSMatthias Ringwald case RFCOMM_DATA_PACKET:
241bcf00d8fSMatthias Ringwald printf("RCV: '");
242bcf00d8fSMatthias Ringwald for (i=0;i<size;i++){
243bcf00d8fSMatthias Ringwald putchar(packet[i]);
244bcf00d8fSMatthias Ringwald }
245bcf00d8fSMatthias Ringwald printf("'\n");
246bcf00d8fSMatthias Ringwald break;
247bcf00d8fSMatthias Ringwald
248bcf00d8fSMatthias Ringwald default:
249bcf00d8fSMatthias Ringwald break;
250bcf00d8fSMatthias Ringwald }
251bcf00d8fSMatthias Ringwald /* LISTING_RESUME */
252bcf00d8fSMatthias Ringwald }
253bcf00d8fSMatthias Ringwald /* LISTING_END */
254bcf00d8fSMatthias Ringwald
255bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])256bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
2579ec2630cSMatthias Ringwald (void)argc;
2589ec2630cSMatthias Ringwald (void)argv;
2599ec2630cSMatthias Ringwald
260bcf00d8fSMatthias Ringwald one_shot_timer_setup();
261bcf00d8fSMatthias Ringwald spp_service_setup();
262bcf00d8fSMatthias Ringwald
263bcf00d8fSMatthias Ringwald gap_discoverable_control(1);
264bcf00d8fSMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
2650c2b8870SMatthias Ringwald gap_set_local_name("SPP Counter 00:00:00:00:00:00");
266bcf00d8fSMatthias Ringwald
267bcf00d8fSMatthias Ringwald // turn on!
268bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON);
269bcf00d8fSMatthias Ringwald
270bcf00d8fSMatthias Ringwald return 0;
271bcf00d8fSMatthias Ringwald }
272bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
273bcf00d8fSMatthias Ringwald
274