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