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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH 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
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "spp_streamer_client.c"
3915de8206SMilanka Ringwald
4015de8206SMilanka Ringwald /*
4115de8206SMilanka Ringwald * spp_streamer_client.c
4215de8206SMilanka Ringwald */
4315de8206SMilanka Ringwald
44cee62800SMatthias Ringwald // *****************************************************************************
45ec8ae085SMilanka Ringwald /* EXAMPLE_START(spp_streamer_client): Performance - Stream Data over SPP (Client)
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
6071a95af9SMatthias Ringwald // prototypes
6171a95af9SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
6271a95af9SMatthias Ringwald
6371a95af9SMatthias Ringwald static uint8_t rfcomm_server_channel;
64cee62800SMatthias Ringwald
6583c12399SMatthias Ringwald #define NUM_ROWS 25
6683c12399SMatthias Ringwald #define NUM_COLS 40
6783c12399SMatthias Ringwald
68cee62800SMatthias Ringwald #define TEST_COD 0x1234
69cee62800SMatthias Ringwald
7083c12399SMatthias Ringwald #define TEST_MODE_SEND 1
7183c12399SMatthias Ringwald #define TEST_MODE_RECEIVE 2
7283c12399SMatthias Ringwald #define TEST_MODE_DUPLEX 3
7383c12399SMatthias Ringwald
7483c12399SMatthias Ringwald // configure test mode: send only, receive only, full duplex
7583c12399SMatthias Ringwald #define TEST_MODE TEST_MODE_SEND
7683c12399SMatthias Ringwald
77cee62800SMatthias Ringwald typedef enum {
78cee62800SMatthias Ringwald // SPP
79cee62800SMatthias Ringwald W4_PEER_COD,
80cee62800SMatthias Ringwald W4_SCAN_COMPLETE,
81cee62800SMatthias Ringwald W4_SDP_RESULT,
82db010f46SMilanka Ringwald W2_SEND_SDP_QUERY,
83cee62800SMatthias Ringwald W4_RFCOMM_CHANNEL,
84cee62800SMatthias Ringwald SENDING,
85cee62800SMatthias Ringwald DONE
86cee62800SMatthias Ringwald } state_t;
87cee62800SMatthias Ringwald
8883c12399SMatthias Ringwald static uint8_t test_data[NUM_ROWS * NUM_COLS];
8983c12399SMatthias Ringwald static uint16_t spp_test_data_len;
9083c12399SMatthias Ringwald
91cee62800SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
92db010f46SMilanka Ringwald static btstack_context_callback_registration_t handle_sdp_client_query_request;
93cee62800SMatthias Ringwald
94cee62800SMatthias Ringwald static bd_addr_t peer_addr;
95080ca4eeSMatthias Ringwald static state_t state;
96cee62800SMatthias Ringwald
97cee62800SMatthias Ringwald // SPP
98cee62800SMatthias Ringwald static uint16_t rfcomm_mtu;
99cee62800SMatthias Ringwald static uint16_t rfcomm_cid = 0;
100cee62800SMatthias Ringwald // static uint32_t data_to_send = DATA_VOLUME;
101cee62800SMatthias Ringwald
1028cef84f3SMatthias Ringwald /**
1038cef84f3SMatthias Ringwald * RFCOMM can make use for ERTM. Due to the need to re-transmit packets,
1048cef84f3SMatthias Ringwald * a large buffer is needed to still get high throughput
1058cef84f3SMatthias Ringwald */
106278494d6SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM
1078cef84f3SMatthias Ringwald static uint8_t ertm_buffer[20000];
1088cef84f3SMatthias Ringwald static l2cap_ertm_config_t ertm_config = {
1098cef84f3SMatthias Ringwald 0, // ertm mandatory
1108cef84f3SMatthias Ringwald 8, // max transmit
1118cef84f3SMatthias Ringwald 2000,
1128cef84f3SMatthias Ringwald 12000,
1138cef84f3SMatthias Ringwald 1000, // l2cap ertm mtu
1148cef84f3SMatthias Ringwald 8,
1158cef84f3SMatthias Ringwald 8,
1168cef84f3SMatthias Ringwald 0, // No FCS
1178cef84f3SMatthias Ringwald };
1188cef84f3SMatthias Ringwald static int ertm_buffer_in_use;
rfcomm_ertm_request_handler(rfcomm_ertm_request_t * ertm_request)1198cef84f3SMatthias Ringwald static void rfcomm_ertm_request_handler(rfcomm_ertm_request_t * ertm_request){
1208cef84f3SMatthias Ringwald printf("ERTM Buffer requested, buffer in use %u\n", ertm_buffer_in_use);
1218cef84f3SMatthias Ringwald if (ertm_buffer_in_use) return;
1228cef84f3SMatthias Ringwald ertm_buffer_in_use = 1;
1238cef84f3SMatthias Ringwald ertm_request->ertm_config = &ertm_config;
1248cef84f3SMatthias Ringwald ertm_request->ertm_buffer = ertm_buffer;
1258cef84f3SMatthias Ringwald ertm_request->ertm_buffer_size = sizeof(ertm_buffer);
1268cef84f3SMatthias Ringwald }
rfcomm_ertm_released_handler(uint16_t ertm_id)1278cef84f3SMatthias Ringwald static void rfcomm_ertm_released_handler(uint16_t ertm_id){
1288cef84f3SMatthias Ringwald printf("ERTM Buffer released, buffer in use %u, ertm_id %x\n", ertm_buffer_in_use, ertm_id);
1298cef84f3SMatthias Ringwald ertm_buffer_in_use = 0;
1308cef84f3SMatthias Ringwald }
1318cef84f3SMatthias Ringwald #endif
132cee62800SMatthias Ringwald
133cee62800SMatthias Ringwald /**
134cee62800SMatthias Ringwald * Find remote peer by COD
135cee62800SMatthias Ringwald */
136cee62800SMatthias Ringwald #define INQUIRY_INTERVAL 5
start_scan(void)137cee62800SMatthias Ringwald static void start_scan(void){
138cee62800SMatthias Ringwald printf("Starting inquiry scan..\n");
139cee62800SMatthias Ringwald state = W4_PEER_COD;
140287c758dSMatthias Ringwald gap_inquiry_start(INQUIRY_INTERVAL);
141cee62800SMatthias Ringwald }
stop_scan(void)142cee62800SMatthias Ringwald static void stop_scan(void){
143cee62800SMatthias Ringwald printf("Stopping inquiry scan..\n");
144cee62800SMatthias Ringwald state = W4_SCAN_COMPLETE;
145287c758dSMatthias Ringwald gap_inquiry_stop();
146cee62800SMatthias Ringwald }
147cee62800SMatthias Ringwald /*
148cee62800SMatthias Ringwald * @section Track throughput
149cee62800SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of
150cee62800SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
151cee62800SMatthias Ringwald * and reset the counter and start time.
152cee62800SMatthias Ringwald */
153cee62800SMatthias Ringwald
154cee62800SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
155cee62800SMatthias Ringwald #define REPORT_INTERVAL_MS 3000
156cee62800SMatthias Ringwald static uint32_t test_data_transferred;
157cee62800SMatthias Ringwald static uint32_t test_data_start;
158cee62800SMatthias Ringwald
test_reset(void)159cee62800SMatthias Ringwald static void test_reset(void){
160cee62800SMatthias Ringwald test_data_start = btstack_run_loop_get_time_ms();
161cee62800SMatthias Ringwald test_data_transferred = 0;
162cee62800SMatthias Ringwald }
163cee62800SMatthias Ringwald
test_track_transferred(int bytes_sent)164cee62800SMatthias Ringwald static void test_track_transferred(int bytes_sent){
165cee62800SMatthias Ringwald test_data_transferred += bytes_sent;
166cee62800SMatthias Ringwald // evaluate
167cee62800SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms();
168cee62800SMatthias Ringwald uint32_t time_passed = now - test_data_start;
169cee62800SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return;
170cee62800SMatthias Ringwald // print speed
171cee62800SMatthias Ringwald int bytes_per_second = test_data_transferred * 1000 / time_passed;
172cee62800SMatthias Ringwald printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000);
173cee62800SMatthias Ringwald
174cee62800SMatthias Ringwald // restart
175cee62800SMatthias Ringwald test_data_start = now;
176cee62800SMatthias Ringwald test_data_transferred = 0;
177cee62800SMatthias Ringwald }
178cee62800SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
179cee62800SMatthias Ringwald
18083c12399SMatthias Ringwald #if (TEST_MODE & TEST_MODE_SEND)
spp_create_test_data(void)18183c12399SMatthias Ringwald static void spp_create_test_data(void){
18283c12399SMatthias Ringwald int x,y;
18383c12399SMatthias Ringwald for (y=0;y<NUM_ROWS;y++){
18483c12399SMatthias Ringwald for (x=0;x<NUM_COLS-2;x++){
18583c12399SMatthias Ringwald test_data[y*NUM_COLS+x] = '0' + (x % 10);
18683c12399SMatthias Ringwald }
18783c12399SMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
18883c12399SMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
18983c12399SMatthias Ringwald }
19083c12399SMatthias Ringwald }
spp_send_packet(void)19183c12399SMatthias Ringwald static void spp_send_packet(void){
19283c12399SMatthias Ringwald rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len);
19383c12399SMatthias Ringwald test_track_transferred(spp_test_data_len);
19483c12399SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid);
19583c12399SMatthias Ringwald }
19683c12399SMatthias Ringwald #endif
19783c12399SMatthias Ringwald
198cee62800SMatthias Ringwald /*
19971a95af9SMatthias Ringwald * @section SDP Query Packet Handler
200cee62800SMatthias Ringwald *
20171a95af9SMatthias Ringwald * @text Store RFCOMM Channel for SPP service and initiates RFCOMM connection
20271a95af9SMatthias Ringwald */
handle_query_rfcomm_event(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)20371a95af9SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20471a95af9SMatthias Ringwald UNUSED(packet_type);
20571a95af9SMatthias Ringwald UNUSED(channel);
20671a95af9SMatthias Ringwald UNUSED(size);
20771a95af9SMatthias Ringwald
2087bbeb3adSMilanka Ringwald switch (hci_event_packet_get_type(packet)){
20971a95af9SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE:
21071a95af9SMatthias Ringwald rfcomm_server_channel = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
21171a95af9SMatthias Ringwald break;
21271a95af9SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE:
21371a95af9SMatthias Ringwald if (sdp_event_query_complete_get_status(packet)){
214fcd55a0bSMilanka Ringwald printf("SDP query failed, status 0x%02x\n", sdp_event_query_complete_get_status(packet));
21571a95af9SMatthias Ringwald break;
21671a95af9SMatthias Ringwald }
21771a95af9SMatthias Ringwald if (rfcomm_server_channel == 0){
21871a95af9SMatthias Ringwald printf("No SPP service found\n");
21971a95af9SMatthias Ringwald break;
22071a95af9SMatthias Ringwald }
221fcd55a0bSMilanka Ringwald printf("SDP query done, channel 0x%02x.\n", rfcomm_server_channel);
22271a95af9SMatthias Ringwald rfcomm_create_channel(packet_handler, peer_addr, rfcomm_server_channel, NULL);
22371a95af9SMatthias Ringwald break;
2247bbeb3adSMilanka Ringwald default:
2257bbeb3adSMilanka Ringwald break;
22671a95af9SMatthias Ringwald }
22771a95af9SMatthias Ringwald }
22871a95af9SMatthias Ringwald
handle_start_sdp_client_query(void * context)229db010f46SMilanka Ringwald static void handle_start_sdp_client_query(void * context){
230db010f46SMilanka Ringwald UNUSED(context);
231db010f46SMilanka Ringwald if (state != W2_SEND_SDP_QUERY) return;
232db010f46SMilanka Ringwald state = W4_RFCOMM_CHANNEL;
233*6bac88f4SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, peer_addr, BLUETOOTH_SERVICE_CLASS_SERIAL_PORT);
234db010f46SMilanka Ringwald }
235db010f46SMilanka Ringwald
236db010f46SMilanka Ringwald
23771a95af9SMatthias Ringwald /*
23871a95af9SMatthias Ringwald * @section Gerenal Packet Handler
23971a95af9SMatthias Ringwald *
24071a95af9SMatthias Ringwald * @text Handles startup (BTSTACK_EVENT_STATE), inquiry, pairing, starts SDP query for SPP service, and RFCOMM connection
241cee62800SMatthias Ringwald */
242cee62800SMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)243cee62800SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
244cee62800SMatthias Ringwald UNUSED(channel);
245cee62800SMatthias Ringwald
246cee62800SMatthias Ringwald bd_addr_t event_addr;
247cee62800SMatthias Ringwald uint8_t rfcomm_channel_nr;
248cee62800SMatthias Ringwald uint32_t class_of_device;
249cee62800SMatthias Ringwald
250cee62800SMatthias Ringwald switch (packet_type) {
251cee62800SMatthias Ringwald case HCI_EVENT_PACKET:
252cee62800SMatthias Ringwald switch (hci_event_packet_get_type(packet)) {
253cee62800SMatthias Ringwald
254cee62800SMatthias Ringwald case BTSTACK_EVENT_STATE:
255cee62800SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
256cee62800SMatthias Ringwald start_scan();
257cee62800SMatthias Ringwald break;
258cee62800SMatthias Ringwald
259cee62800SMatthias Ringwald case GAP_EVENT_INQUIRY_RESULT:
2607c03fe59SMatthias Ringwald if (state != W4_PEER_COD) break;
261cee62800SMatthias Ringwald class_of_device = gap_event_inquiry_result_get_class_of_device(packet);
262cee62800SMatthias Ringwald gap_event_inquiry_result_get_bd_addr(packet, event_addr);
263cee62800SMatthias Ringwald if (class_of_device == TEST_COD){
264cee62800SMatthias Ringwald memcpy(peer_addr, event_addr, 6);
265cee62800SMatthias Ringwald printf("Peer found: %s\n", bd_addr_to_str(peer_addr));
266cee62800SMatthias Ringwald stop_scan();
267cee62800SMatthias Ringwald } else {
268cee62800SMatthias Ringwald printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device);
269cee62800SMatthias Ringwald }
270cee62800SMatthias Ringwald break;
271cee62800SMatthias Ringwald
272287c758dSMatthias Ringwald case GAP_EVENT_INQUIRY_COMPLETE:
2737c03fe59SMatthias Ringwald switch (state){
2747c03fe59SMatthias Ringwald case W4_PEER_COD:
275cee62800SMatthias Ringwald printf("Inquiry complete\n");
276cee62800SMatthias Ringwald printf("Peer not found, starting scan again\n");
277cee62800SMatthias Ringwald start_scan();
278cee62800SMatthias Ringwald break;
2797c03fe59SMatthias Ringwald case W4_SCAN_COMPLETE:
28071a95af9SMatthias Ringwald printf("Start to connect and query for SPP service\n");
281db010f46SMilanka Ringwald state = W2_SEND_SDP_QUERY;
282db010f46SMilanka Ringwald handle_sdp_client_query_request.callback = &handle_start_sdp_client_query;
283231147c5SMilanka Ringwald (void) sdp_client_register_query_callback(&handle_sdp_client_query_request);
2847c03fe59SMatthias Ringwald break;
2857c03fe59SMatthias Ringwald default:
2867c03fe59SMatthias Ringwald break;
2877c03fe59SMatthias Ringwald }
2887c03fe59SMatthias Ringwald if (state == W4_PEER_COD){
2897c03fe59SMatthias Ringwald }
2907c03fe59SMatthias Ringwald break;
291cee62800SMatthias Ringwald
29283c12399SMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST:
29383c12399SMatthias Ringwald // inform about pin code request
29483c12399SMatthias Ringwald printf("Pin code request - using '0000'\n");
29583c12399SMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr);
29683c12399SMatthias Ringwald gap_pin_code_response(event_addr, "0000");
29783c12399SMatthias Ringwald break;
29883c12399SMatthias Ringwald
29983c12399SMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST:
30083c12399SMatthias Ringwald // inform about user confirmation request
30183c12399SMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
30283c12399SMatthias Ringwald printf("SSP User Confirmation Auto accept\n");
30383c12399SMatthias Ringwald break;
30483c12399SMatthias Ringwald
30583c12399SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION:
30683c12399SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
30783c12399SMatthias Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
30883c12399SMatthias Ringwald rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
309fcd55a0bSMilanka Ringwald printf("RFCOMM channel 0x%02x requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
31083c12399SMatthias Ringwald rfcomm_accept_connection(rfcomm_cid);
31183c12399SMatthias Ringwald break;
31283c12399SMatthias Ringwald
31383c12399SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED:
31483c12399SMatthias Ringwald if (rfcomm_event_channel_opened_get_status(packet)) {
315fcd55a0bSMilanka Ringwald printf("RFCOMM channel open failed, status 0x%02x\n", rfcomm_event_channel_opened_get_status(packet));
31683c12399SMatthias Ringwald } else {
31783c12399SMatthias Ringwald rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
31883c12399SMatthias Ringwald rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
319fcd55a0bSMilanka Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID 0x%02x, max frame size %u\n", rfcomm_cid, rfcomm_mtu);
32083c12399SMatthias Ringwald test_reset();
32183c12399SMatthias Ringwald
32283c12399SMatthias Ringwald // disable page/inquiry scan to get max performance
32383c12399SMatthias Ringwald gap_discoverable_control(0);
32483c12399SMatthias Ringwald gap_connectable_control(0);
32583c12399SMatthias Ringwald
32683c12399SMatthias Ringwald #if (TEST_MODE & TEST_MODE_SEND)
32783c12399SMatthias Ringwald // configure test data
32883c12399SMatthias Ringwald spp_test_data_len = rfcomm_mtu;
32983c12399SMatthias Ringwald if (spp_test_data_len > sizeof(test_data)){
33083c12399SMatthias Ringwald spp_test_data_len = sizeof(test_data);
33183c12399SMatthias Ringwald }
33283c12399SMatthias Ringwald spp_create_test_data();
333db010f46SMilanka Ringwald state = SENDING;
33483c12399SMatthias Ringwald // start sending
33583c12399SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid);
33683c12399SMatthias Ringwald #endif
33783c12399SMatthias Ringwald }
33883c12399SMatthias Ringwald break;
33983c12399SMatthias Ringwald
34083c12399SMatthias Ringwald #if (TEST_MODE & TEST_MODE_SEND)
34183c12399SMatthias Ringwald case RFCOMM_EVENT_CAN_SEND_NOW:
34204d1de7fSMatthias Ringwald spp_send_packet();
34383c12399SMatthias Ringwald break;
34483c12399SMatthias Ringwald #endif
34583c12399SMatthias Ringwald
34683c12399SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED:
34783c12399SMatthias Ringwald printf("RFCOMM channel closed\n");
34883c12399SMatthias Ringwald rfcomm_cid = 0;
34983c12399SMatthias Ringwald
35083c12399SMatthias Ringwald // re-enable page/inquiry scan again
35183c12399SMatthias Ringwald gap_discoverable_control(1);
35283c12399SMatthias Ringwald gap_connectable_control(1);
35383c12399SMatthias Ringwald break;
35483c12399SMatthias Ringwald
35583c12399SMatthias Ringwald
35683c12399SMatthias Ringwald
357cee62800SMatthias Ringwald default:
358cee62800SMatthias Ringwald break;
359cee62800SMatthias Ringwald }
360cee62800SMatthias Ringwald break;
361cee62800SMatthias Ringwald
362cee62800SMatthias Ringwald case RFCOMM_DATA_PACKET:
363cee62800SMatthias Ringwald test_track_transferred(size);
3647c03fe59SMatthias Ringwald
365cee62800SMatthias Ringwald #if 0
3666058cb0dSMatthias Ringwald // optional: print received data as ASCII text
367cee62800SMatthias Ringwald printf("RCV: '");
368cee62800SMatthias Ringwald for (i=0;i<size;i++){
369cee62800SMatthias Ringwald putchar(packet[i]);
370cee62800SMatthias Ringwald }
371cee62800SMatthias Ringwald printf("'\n");
372cee62800SMatthias Ringwald #endif
373cee62800SMatthias Ringwald break;
374cee62800SMatthias Ringwald
375cee62800SMatthias Ringwald default:
376cee62800SMatthias Ringwald break;
377cee62800SMatthias Ringwald }
378cee62800SMatthias Ringwald }
379cee62800SMatthias Ringwald
380cee62800SMatthias Ringwald /*
381cee62800SMatthias Ringwald * @section Main Application Setup
382cee62800SMatthias Ringwald *
383cee62800SMatthias Ringwald * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
384cee62800SMatthias Ringwald */
385cee62800SMatthias Ringwald
386cee62800SMatthias Ringwald
387cee62800SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */
388cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])389cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
390cee62800SMatthias Ringwald UNUSED(argc);
391cee62800SMatthias Ringwald (void)argv;
392cee62800SMatthias Ringwald
393cee62800SMatthias Ringwald l2cap_init();
394cee62800SMatthias Ringwald
3958c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE
3968c9bb29eSMatthias Ringwald // Initialize LE Security Manager. Needed for cross-transport key derivation
3978c9bb29eSMatthias Ringwald sm_init();
3988c9bb29eSMatthias Ringwald #endif
3998c9bb29eSMatthias Ringwald
400cee62800SMatthias Ringwald rfcomm_init();
401cee62800SMatthias Ringwald
402278494d6SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM
4038cef84f3SMatthias Ringwald // setup ERTM management
4048cef84f3SMatthias Ringwald rfcomm_enable_l2cap_ertm(&rfcomm_ertm_request_handler, &rfcomm_ertm_released_handler);
4058cef84f3SMatthias Ringwald #endif
4068cef84f3SMatthias Ringwald
407a4fe6467SMatthias Ringwald // register for HCI events
408a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
409a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
410a4fe6467SMatthias Ringwald
411cee62800SMatthias Ringwald // init SDP
412cee62800SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
413cee62800SMatthias Ringwald
414cee62800SMatthias Ringwald // turn on!
415cee62800SMatthias Ringwald hci_power_control(HCI_POWER_ON);
416cee62800SMatthias Ringwald
417cee62800SMatthias Ringwald return 0;
418cee62800SMatthias Ringwald }
419cee62800SMatthias Ringwald /* LISTING_END */
420cee62800SMatthias Ringwald /* EXAMPLE_END */
421