xref: /btstack/example/spp_streamer_client.c (revision a4fe6467953bdb173fdf96a604f6527ed88f81c3)
1cee62800SMatthias Ringwald /*
2cee62800SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3cee62800SMatthias Ringwald  *
4cee62800SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5cee62800SMatthias Ringwald  * modification, are permitted provided that the following conditions
6cee62800SMatthias Ringwald  * are met:
7cee62800SMatthias Ringwald  *
8cee62800SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9cee62800SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10cee62800SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11cee62800SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12cee62800SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13cee62800SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14cee62800SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15cee62800SMatthias Ringwald  *    from this software without specific prior written permission.
16cee62800SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17cee62800SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18cee62800SMatthias Ringwald  *    monetary gain.
19cee62800SMatthias Ringwald  *
20cee62800SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21cee62800SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22cee62800SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23cee62800SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24cee62800SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25cee62800SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26cee62800SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27cee62800SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28cee62800SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29cee62800SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30cee62800SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31cee62800SMatthias Ringwald  * SUCH DAMAGE.
32cee62800SMatthias Ringwald  *
33cee62800SMatthias Ringwald  * Please inquire about commercial licensing options at
34cee62800SMatthias Ringwald  * [email protected]
35cee62800SMatthias Ringwald  *
36cee62800SMatthias Ringwald  */
37cee62800SMatthias Ringwald 
3815de8206SMilanka Ringwald #define __BTSTACK_FILE__ "spp_streamer_client.c"
3915de8206SMilanka Ringwald 
4015de8206SMilanka Ringwald /*
4115de8206SMilanka Ringwald  * spp_streamer_client.c
4215de8206SMilanka Ringwald  */
4315de8206SMilanka Ringwald 
44cee62800SMatthias Ringwald // *****************************************************************************
45cee62800SMatthias Ringwald /* EXAMPLE_START(spp_streamer_client): Client for SPP Streamer
46cee62800SMatthias Ringwald  *
4758d7a529SMilanka Ringwald  * @text Note: The SPP Streamer Client scans for and connects to SPP Streamer,
4858d7a529SMilanka Ringwald  * and measures the throughput.
49cee62800SMatthias Ringwald  */
50cee62800SMatthias Ringwald // *****************************************************************************
51cee62800SMatthias Ringwald 
52cee62800SMatthias Ringwald #include <stdint.h>
53cee62800SMatthias Ringwald #include <stdio.h>
54cee62800SMatthias Ringwald #include <stdlib.h>
55cee62800SMatthias Ringwald #include <string.h>
56cee62800SMatthias Ringwald #include <inttypes.h>
57cee62800SMatthias Ringwald 
58cee62800SMatthias Ringwald #include "btstack.h"
59cee62800SMatthias Ringwald 
60cee62800SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1
61cee62800SMatthias Ringwald 
62cee62800SMatthias Ringwald #define TEST_COD 0x1234
63cee62800SMatthias Ringwald 
64cee62800SMatthias Ringwald typedef enum {
65cee62800SMatthias Ringwald     // SPP
66cee62800SMatthias Ringwald     W4_PEER_COD,
67cee62800SMatthias Ringwald     W4_SCAN_COMPLETE,
68cee62800SMatthias Ringwald     W4_SDP_RESULT,
69cee62800SMatthias Ringwald     W4_SDP_COMPLETE,
70cee62800SMatthias Ringwald     W4_RFCOMM_CHANNEL,
71cee62800SMatthias Ringwald     SENDING,
72cee62800SMatthias Ringwald     DONE
73cee62800SMatthias Ringwald } state_t;
74cee62800SMatthias Ringwald 
75cee62800SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
76cee62800SMatthias Ringwald 
77cee62800SMatthias Ringwald static bd_addr_t peer_addr;
78080ca4eeSMatthias Ringwald static state_t state;
79cee62800SMatthias Ringwald 
80cee62800SMatthias Ringwald // SPP
81cee62800SMatthias Ringwald static uint16_t  rfcomm_mtu;
82cee62800SMatthias Ringwald static uint16_t  rfcomm_cid = 0;
83cee62800SMatthias Ringwald // static uint32_t  data_to_send =  DATA_VOLUME;
84cee62800SMatthias Ringwald 
85cee62800SMatthias Ringwald 
86cee62800SMatthias Ringwald /**
87cee62800SMatthias Ringwald  * Find remote peer by COD
88cee62800SMatthias Ringwald  */
89cee62800SMatthias Ringwald #define INQUIRY_INTERVAL 5
90cee62800SMatthias Ringwald static void start_scan(void){
91cee62800SMatthias Ringwald     printf("Starting inquiry scan..\n");
92cee62800SMatthias Ringwald     state = W4_PEER_COD;
93287c758dSMatthias Ringwald     gap_inquiry_start(INQUIRY_INTERVAL);
94cee62800SMatthias Ringwald }
95cee62800SMatthias Ringwald static void stop_scan(void){
96cee62800SMatthias Ringwald     printf("Stopping inquiry scan..\n");
97cee62800SMatthias Ringwald     state = W4_SCAN_COMPLETE;
98287c758dSMatthias Ringwald     gap_inquiry_stop();
99cee62800SMatthias Ringwald }
100cee62800SMatthias Ringwald /*
101cee62800SMatthias Ringwald  * @section Track throughput
102cee62800SMatthias Ringwald  * @text We calculate the throughput by setting a start time and measuring the amount of
103cee62800SMatthias Ringwald  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
104cee62800SMatthias Ringwald  * and reset the counter and start time.
105cee62800SMatthias Ringwald  */
106cee62800SMatthias Ringwald 
107cee62800SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
108cee62800SMatthias Ringwald #define REPORT_INTERVAL_MS 3000
109cee62800SMatthias Ringwald static uint32_t test_data_transferred;
110cee62800SMatthias Ringwald static uint32_t test_data_start;
111cee62800SMatthias Ringwald 
112cee62800SMatthias Ringwald static void test_reset(void){
113cee62800SMatthias Ringwald     test_data_start = btstack_run_loop_get_time_ms();
114cee62800SMatthias Ringwald     test_data_transferred = 0;
115cee62800SMatthias Ringwald }
116cee62800SMatthias Ringwald 
117cee62800SMatthias Ringwald static void test_track_transferred(int bytes_sent){
118cee62800SMatthias Ringwald     test_data_transferred += bytes_sent;
119cee62800SMatthias Ringwald     // evaluate
120cee62800SMatthias Ringwald     uint32_t now = btstack_run_loop_get_time_ms();
121cee62800SMatthias Ringwald     uint32_t time_passed = now - test_data_start;
122cee62800SMatthias Ringwald     if (time_passed < REPORT_INTERVAL_MS) return;
123cee62800SMatthias Ringwald     // print speed
124cee62800SMatthias Ringwald     int bytes_per_second = test_data_transferred * 1000 / time_passed;
125cee62800SMatthias Ringwald     printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000);
126cee62800SMatthias Ringwald 
127cee62800SMatthias Ringwald     // restart
128cee62800SMatthias Ringwald     test_data_start = now;
129cee62800SMatthias Ringwald     test_data_transferred  = 0;
130cee62800SMatthias Ringwald }
131cee62800SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
132cee62800SMatthias Ringwald 
133cee62800SMatthias Ringwald /*
134cee62800SMatthias Ringwald  * @section Packet Handler
135cee62800SMatthias Ringwald  *
136cee62800SMatthias Ringwald  * @text The packet handler of the combined example is just the combination of the individual packet handlers.
137cee62800SMatthias Ringwald  */
138cee62800SMatthias Ringwald 
139cee62800SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
140cee62800SMatthias Ringwald     UNUSED(channel);
141cee62800SMatthias Ringwald 
142cee62800SMatthias Ringwald     bd_addr_t event_addr;
143cee62800SMatthias Ringwald     uint8_t   rfcomm_channel_nr;
144cee62800SMatthias Ringwald     uint32_t class_of_device;
145cee62800SMatthias Ringwald 
146cee62800SMatthias Ringwald 	switch (packet_type) {
147cee62800SMatthias Ringwald 		case HCI_EVENT_PACKET:
148cee62800SMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
149cee62800SMatthias Ringwald 
150cee62800SMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
151cee62800SMatthias Ringwald                     // inform about pin code request
152cee62800SMatthias Ringwald                     printf("Pin code request - using '0000'\n");
153cee62800SMatthias Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
154cee62800SMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
155cee62800SMatthias Ringwald                     break;
156cee62800SMatthias Ringwald 
157cee62800SMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
158cee62800SMatthias Ringwald                     // inform about user confirmation request
159cee62800SMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
160cee62800SMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
161cee62800SMatthias Ringwald                     break;
162cee62800SMatthias Ringwald 
163cee62800SMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
164cee62800SMatthias Ringwald 					// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
165cee62800SMatthias Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
166cee62800SMatthias Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
167cee62800SMatthias Ringwald                     rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
168cee62800SMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
169cee62800SMatthias Ringwald                     rfcomm_accept_connection(rfcomm_cid);
170cee62800SMatthias Ringwald 					break;
171cee62800SMatthias Ringwald 
172cee62800SMatthias Ringwald 				case RFCOMM_EVENT_CHANNEL_OPENED:
173cee62800SMatthias Ringwald 					// data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
174cee62800SMatthias Ringwald 					if (rfcomm_event_channel_opened_get_status(packet)) {
175cee62800SMatthias Ringwald                         printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
176cee62800SMatthias Ringwald                     } else {
177cee62800SMatthias Ringwald                         rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
178cee62800SMatthias Ringwald                         rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
179cee62800SMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu);
180cee62800SMatthias Ringwald                         test_reset();
1815aed585bSMatthias Ringwald 
1825aed585bSMatthias Ringwald                         // disable page/inquiry scan to get max performance
1835aed585bSMatthias Ringwald                         gap_discoverable_control(0);
184cee62800SMatthias Ringwald                         gap_connectable_control(0);
185cee62800SMatthias Ringwald                     }
186cee62800SMatthias Ringwald 					break;
187cee62800SMatthias Ringwald 
188cee62800SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
189cee62800SMatthias Ringwald                     printf("RFCOMM channel closed\n");
1905aed585bSMatthias Ringwald                     rfcomm_cid = 0;
1915aed585bSMatthias Ringwald 
1925aed585bSMatthias Ringwald                     // re-enable page/inquiry scan again
1935aed585bSMatthias Ringwald                     gap_discoverable_control(1);
194cee62800SMatthias Ringwald                     gap_connectable_control(1);
195cee62800SMatthias Ringwald                     break;
196cee62800SMatthias Ringwald 
197cee62800SMatthias Ringwald                 case BTSTACK_EVENT_STATE:
198cee62800SMatthias Ringwald                     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
199cee62800SMatthias Ringwald                     start_scan();
200cee62800SMatthias Ringwald                     break;
201cee62800SMatthias Ringwald 
202cee62800SMatthias Ringwald                 case GAP_EVENT_INQUIRY_RESULT:
2037c03fe59SMatthias Ringwald                     if (state != W4_PEER_COD) break;
204cee62800SMatthias Ringwald                     class_of_device = gap_event_inquiry_result_get_class_of_device(packet);
205cee62800SMatthias Ringwald                     gap_event_inquiry_result_get_bd_addr(packet, event_addr);
206cee62800SMatthias Ringwald                     if (class_of_device == TEST_COD){
207cee62800SMatthias Ringwald                         memcpy(peer_addr, event_addr, 6);
208cee62800SMatthias Ringwald                         printf("Peer found: %s\n", bd_addr_to_str(peer_addr));
209cee62800SMatthias Ringwald                         stop_scan();
210cee62800SMatthias Ringwald                     } else {
211cee62800SMatthias Ringwald                         printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device);
212cee62800SMatthias Ringwald                     }
213cee62800SMatthias Ringwald                     break;
214cee62800SMatthias Ringwald 
215287c758dSMatthias Ringwald                 case GAP_EVENT_INQUIRY_COMPLETE:
2167c03fe59SMatthias Ringwald                     switch (state){
2177c03fe59SMatthias Ringwald                         case W4_PEER_COD:
218cee62800SMatthias Ringwald                             printf("Inquiry complete\n");
219cee62800SMatthias Ringwald                             printf("Peer not found, starting scan again\n");
220cee62800SMatthias Ringwald                             start_scan();
221cee62800SMatthias Ringwald                             break;
2227c03fe59SMatthias Ringwald                         case W4_SCAN_COMPLETE:
2237c03fe59SMatthias Ringwald                             printf("Start to connect\n");
2247c03fe59SMatthias Ringwald                             state = W4_RFCOMM_CHANNEL;
2257c03fe59SMatthias Ringwald                             rfcomm_create_channel(packet_handler, peer_addr, RFCOMM_SERVER_CHANNEL, NULL);
2267c03fe59SMatthias Ringwald                             break;
2277c03fe59SMatthias Ringwald                         default:
2287c03fe59SMatthias Ringwald                             break;
2297c03fe59SMatthias Ringwald                     }
2307c03fe59SMatthias Ringwald                     if (state == W4_PEER_COD){
2317c03fe59SMatthias Ringwald                     }
2327c03fe59SMatthias Ringwald                     break;
233cee62800SMatthias Ringwald 
234cee62800SMatthias Ringwald                 default:
235cee62800SMatthias Ringwald                     break;
236cee62800SMatthias Ringwald 			}
237cee62800SMatthias Ringwald             break;
238cee62800SMatthias Ringwald 
239cee62800SMatthias Ringwald         case RFCOMM_DATA_PACKET:
240cee62800SMatthias Ringwald             test_track_transferred(size);
2417c03fe59SMatthias Ringwald 
242cee62800SMatthias Ringwald #if 0
243cee62800SMatthias Ringwald             printf("RCV: '");
244cee62800SMatthias Ringwald             for (i=0;i<size;i++){
245cee62800SMatthias Ringwald                 putchar(packet[i]);
246cee62800SMatthias Ringwald             }
247cee62800SMatthias Ringwald             printf("'\n");
248cee62800SMatthias Ringwald #endif
249cee62800SMatthias Ringwald             break;
250cee62800SMatthias Ringwald 
251cee62800SMatthias Ringwald         default:
252cee62800SMatthias Ringwald             break;
253cee62800SMatthias Ringwald 	}
254cee62800SMatthias Ringwald }
255cee62800SMatthias Ringwald 
256cee62800SMatthias Ringwald /*
257cee62800SMatthias Ringwald  * @section Main Application Setup
258cee62800SMatthias Ringwald  *
259cee62800SMatthias Ringwald  * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
260cee62800SMatthias Ringwald  */
261cee62800SMatthias Ringwald 
262cee62800SMatthias Ringwald 
263cee62800SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */
264cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
265cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
266cee62800SMatthias Ringwald     UNUSED(argc);
267cee62800SMatthias Ringwald     (void)argv;
268cee62800SMatthias Ringwald 
269cee62800SMatthias Ringwald     l2cap_init();
270cee62800SMatthias Ringwald 
271cee62800SMatthias Ringwald     rfcomm_init();
272cee62800SMatthias Ringwald     rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
273cee62800SMatthias Ringwald 
274*a4fe6467SMatthias Ringwald     // register for HCI events
275*a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
276*a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
277*a4fe6467SMatthias Ringwald 
278cee62800SMatthias Ringwald     // init SDP
279cee62800SMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
280cee62800SMatthias Ringwald 
281cee62800SMatthias Ringwald     // turn on!
282cee62800SMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
283cee62800SMatthias Ringwald 
284cee62800SMatthias Ringwald     return 0;
285cee62800SMatthias Ringwald }
286cee62800SMatthias Ringwald /* LISTING_END */
287cee62800SMatthias Ringwald /* EXAMPLE_END */
288