xref: /btstack/example/spp_streamer.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 //
40*bcf00d8fSMatthias Ringwald // minimal setup for SDP client over USB or UART
41*bcf00d8fSMatthias Ringwald //
42*bcf00d8fSMatthias Ringwald // *****************************************************************************
43*bcf00d8fSMatthias Ringwald 
44*bcf00d8fSMatthias Ringwald #include "btstack_config.h"
45*bcf00d8fSMatthias Ringwald 
46*bcf00d8fSMatthias Ringwald #include <stdint.h>
47*bcf00d8fSMatthias Ringwald #include <stdio.h>
48*bcf00d8fSMatthias Ringwald #include <stdlib.h>
49*bcf00d8fSMatthias Ringwald #include <string.h>
50*bcf00d8fSMatthias Ringwald 
51*bcf00d8fSMatthias Ringwald 
52*bcf00d8fSMatthias Ringwald #include "hci_cmd.h"
53*bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h"
54*bcf00d8fSMatthias Ringwald 
55*bcf00d8fSMatthias Ringwald #include "hci.h"
56*bcf00d8fSMatthias Ringwald #include "btstack_memory.h"
57*bcf00d8fSMatthias Ringwald #include "hci_dump.h"
58*bcf00d8fSMatthias Ringwald #include "l2cap.h"
59*bcf00d8fSMatthias Ringwald #include "classic/sdp_query_rfcomm.h"
60*bcf00d8fSMatthias Ringwald #include "classic/rfcomm.h"
61*bcf00d8fSMatthias Ringwald #include "btstack_event.h"
62*bcf00d8fSMatthias Ringwald 
63*bcf00d8fSMatthias Ringwald #define NUM_ROWS 25
64*bcf00d8fSMatthias Ringwald #define NUM_COLS 40
65*bcf00d8fSMatthias Ringwald 
66*bcf00d8fSMatthias Ringwald typedef enum {
67*bcf00d8fSMatthias Ringwald     W4_SDP_RESULT,
68*bcf00d8fSMatthias Ringwald     W4_SDP_COMPLETE,
69*bcf00d8fSMatthias Ringwald     W4_RFCOMM_CHANNEL,
70*bcf00d8fSMatthias Ringwald     DONE
71*bcf00d8fSMatthias Ringwald } state_t;
72*bcf00d8fSMatthias Ringwald 
73*bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
74*bcf00d8fSMatthias Ringwald 
75*bcf00d8fSMatthias Ringwald #define DATA_VOLUME (1000 * 1000)
76*bcf00d8fSMatthias Ringwald 
77*bcf00d8fSMatthias Ringwald // configuration area {
78*bcf00d8fSMatthias Ringwald static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};     // address of remote device
79*bcf00d8fSMatthias Ringwald static const char * spp_service_name_prefix = "Bluetooth-Incoming"; // default on OS X
80*bcf00d8fSMatthias Ringwald // configuration area }
81*bcf00d8fSMatthias Ringwald 
82*bcf00d8fSMatthias Ringwald static uint8_t  test_data[NUM_ROWS * NUM_COLS];
83*bcf00d8fSMatthias Ringwald static uint16_t test_data_len = sizeof(test_data);
84*bcf00d8fSMatthias Ringwald static uint8_t  channel_nr = 0;
85*bcf00d8fSMatthias Ringwald static uint16_t mtu;
86*bcf00d8fSMatthias Ringwald static uint16_t rfcomm_cid = 0;
87*bcf00d8fSMatthias Ringwald static uint32_t data_to_send =  DATA_VOLUME;
88*bcf00d8fSMatthias Ringwald static state_t state = W4_SDP_RESULT;
89*bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
90*bcf00d8fSMatthias Ringwald 
91*bcf00d8fSMatthias Ringwald static void create_test_data(void){
92*bcf00d8fSMatthias Ringwald     int x,y;
93*bcf00d8fSMatthias Ringwald     for (y=0;y<NUM_ROWS;y++){
94*bcf00d8fSMatthias Ringwald         for (x=0;x<NUM_COLS-2;x++){
95*bcf00d8fSMatthias Ringwald             test_data[y*NUM_COLS+x] = '0' + (x % 10);
96*bcf00d8fSMatthias Ringwald         }
97*bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
98*bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
99*bcf00d8fSMatthias Ringwald     }
100*bcf00d8fSMatthias Ringwald }
101*bcf00d8fSMatthias Ringwald 
102*bcf00d8fSMatthias Ringwald static void send_packet(void){
103*bcf00d8fSMatthias Ringwald     int err = rfcomm_send(rfcomm_cid, (uint8_t*) test_data, test_data_len);
104*bcf00d8fSMatthias Ringwald     if (err){
105*bcf00d8fSMatthias Ringwald         printf("rfcomm_send -> error 0X%02x", err);
106*bcf00d8fSMatthias Ringwald         return;
107*bcf00d8fSMatthias Ringwald     }
108*bcf00d8fSMatthias Ringwald 
109*bcf00d8fSMatthias Ringwald     if (data_to_send < test_data_len){
110*bcf00d8fSMatthias Ringwald         rfcomm_disconnect(rfcomm_cid);
111*bcf00d8fSMatthias Ringwald         rfcomm_cid = 0;
112*bcf00d8fSMatthias Ringwald         state = DONE;
113*bcf00d8fSMatthias Ringwald         printf("SPP Streamer: enough data send, closing DLC\n");
114*bcf00d8fSMatthias Ringwald         return;
115*bcf00d8fSMatthias Ringwald     }
116*bcf00d8fSMatthias Ringwald     data_to_send -= test_data_len;
117*bcf00d8fSMatthias Ringwald }
118*bcf00d8fSMatthias Ringwald 
119*bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
120*bcf00d8fSMatthias Ringwald     // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
121*bcf00d8fSMatthias Ringwald 
122*bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
123*bcf00d8fSMatthias Ringwald     uint8_t event = packet[0];
124*bcf00d8fSMatthias Ringwald 
125*bcf00d8fSMatthias Ringwald     switch (event) {
126*bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
127*bcf00d8fSMatthias Ringwald             // bt stack activated, get started
128*bcf00d8fSMatthias Ringwald             if (packet[2] == HCI_STATE_WORKING){
129*bcf00d8fSMatthias Ringwald                 sdp_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_PublicBrowseGroup);
130*bcf00d8fSMatthias Ringwald             }
131*bcf00d8fSMatthias Ringwald             break;
132*bcf00d8fSMatthias Ringwald         case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
133*bcf00d8fSMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
134*bcf00d8fSMatthias Ringwald             if (packet[2]) {
135*bcf00d8fSMatthias Ringwald                 printf("RFCOMM channel open failed, status %u\n", packet[2]);
136*bcf00d8fSMatthias Ringwald             } else {
137*bcf00d8fSMatthias Ringwald                 // data: event(8), len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
138*bcf00d8fSMatthias Ringwald                 rfcomm_cid = little_endian_read_16(packet, 12);
139*bcf00d8fSMatthias Ringwald                 mtu = little_endian_read_16(packet, 14);
140*bcf00d8fSMatthias Ringwald                 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, mtu);
141*bcf00d8fSMatthias Ringwald                 if ((test_data_len > mtu)) {
142*bcf00d8fSMatthias Ringwald                     test_data_len = mtu;
143*bcf00d8fSMatthias Ringwald                 }
144*bcf00d8fSMatthias Ringwald                 if (rfcomm_can_send_packet_now(rfcomm_cid)) send_packet();
145*bcf00d8fSMatthias Ringwald                 break;
146*bcf00d8fSMatthias Ringwald             }
147*bcf00d8fSMatthias Ringwald             break;
148*bcf00d8fSMatthias Ringwald         case RFCOMM_EVENT_CAN_SEND_NOW:
149*bcf00d8fSMatthias Ringwald             if (rfcomm_can_send_packet_now(rfcomm_cid)) send_packet();
150*bcf00d8fSMatthias Ringwald             break;
151*bcf00d8fSMatthias Ringwald         default:
152*bcf00d8fSMatthias Ringwald             break;
153*bcf00d8fSMatthias Ringwald     }
154*bcf00d8fSMatthias Ringwald }
155*bcf00d8fSMatthias Ringwald 
156*bcf00d8fSMatthias Ringwald static void handle_found_service(const char * name, uint8_t port){
157*bcf00d8fSMatthias Ringwald     printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
158*bcf00d8fSMatthias Ringwald 
159*bcf00d8fSMatthias Ringwald     if (strncmp(name, spp_service_name_prefix, strlen(spp_service_name_prefix)) != 0) return;
160*bcf00d8fSMatthias Ringwald 
161*bcf00d8fSMatthias Ringwald     printf("APP: matches requested SPP Service Name\n");
162*bcf00d8fSMatthias Ringwald     channel_nr = port;
163*bcf00d8fSMatthias Ringwald     state = W4_SDP_COMPLETE;
164*bcf00d8fSMatthias Ringwald }
165*bcf00d8fSMatthias Ringwald 
166*bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
167*bcf00d8fSMatthias Ringwald     switch (packet[0]){
168*bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
169*bcf00d8fSMatthias Ringwald             handle_found_service(sdp_event_query_rfcomm_service_get_name(packet),
170*bcf00d8fSMatthias Ringwald                                  sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
171*bcf00d8fSMatthias Ringwald             break;
172*bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
173*bcf00d8fSMatthias Ringwald             if (state != W4_SDP_COMPLETE){
174*bcf00d8fSMatthias Ringwald                 printf("Requested SPP Service %s not found \n", spp_service_name_prefix);
175*bcf00d8fSMatthias Ringwald                 break;
176*bcf00d8fSMatthias Ringwald             }
177*bcf00d8fSMatthias Ringwald             // connect
178*bcf00d8fSMatthias Ringwald             printf("Requested SPP Service found, creating RFCOMM channel\n");
179*bcf00d8fSMatthias Ringwald             state = W4_RFCOMM_CHANNEL;
180*bcf00d8fSMatthias Ringwald             rfcomm_create_channel(packet_handler, remote, channel_nr, NULL);
181*bcf00d8fSMatthias Ringwald             break;
182*bcf00d8fSMatthias Ringwald         default:
183*bcf00d8fSMatthias Ringwald             break;
184*bcf00d8fSMatthias Ringwald     }
185*bcf00d8fSMatthias Ringwald }
186*bcf00d8fSMatthias Ringwald 
187*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
188*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
189*bcf00d8fSMatthias Ringwald 
190*bcf00d8fSMatthias Ringwald     create_test_data();
191*bcf00d8fSMatthias Ringwald 
192*bcf00d8fSMatthias Ringwald     printf("Client HCI init done\r\n");
193*bcf00d8fSMatthias Ringwald 
194*bcf00d8fSMatthias Ringwald     // register for HCI events
195*bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
196*bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
197*bcf00d8fSMatthias Ringwald 
198*bcf00d8fSMatthias Ringwald     // init L2CAP
199*bcf00d8fSMatthias Ringwald     l2cap_init();
200*bcf00d8fSMatthias Ringwald 
201*bcf00d8fSMatthias Ringwald     // turn on!
202*bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
203*bcf00d8fSMatthias Ringwald 
204*bcf00d8fSMatthias Ringwald     return 0;
205*bcf00d8fSMatthias Ringwald }
206