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 */ 37ab2c6ae4SMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "sdp_rfcomm_query.c" 39bcf00d8fSMatthias Ringwald 40bcf00d8fSMatthias Ringwald // ***************************************************************************** 41bcf00d8fSMatthias Ringwald // 42bcf00d8fSMatthias Ringwald // minimal setup for SDP client over USB or UART 43bcf00d8fSMatthias Ringwald // 44bcf00d8fSMatthias Ringwald // ***************************************************************************** 45bcf00d8fSMatthias Ringwald 46bcf00d8fSMatthias Ringwald #include "btstack_config.h" 47bcf00d8fSMatthias Ringwald 48bcf00d8fSMatthias Ringwald #include <stdint.h> 49bcf00d8fSMatthias Ringwald #include <stdio.h> 50bcf00d8fSMatthias Ringwald #include <stdlib.h> 51bcf00d8fSMatthias Ringwald #include <string.h> 52bcf00d8fSMatthias Ringwald 53bcf00d8fSMatthias Ringwald 54bcf00d8fSMatthias Ringwald #include "hci_cmd.h" 55bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h" 56bcf00d8fSMatthias Ringwald 57235946f1SMatthias Ringwald #include "btstack.h" 58bcf00d8fSMatthias Ringwald 59bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 60bcf00d8fSMatthias Ringwald 61bcf00d8fSMatthias Ringwald // static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; 62bcf00d8fSMatthias Ringwald static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15}; 63bcf00d8fSMatthias Ringwald 64bcf00d8fSMatthias Ringwald static uint8_t service_index = 0; 65bcf00d8fSMatthias Ringwald static uint8_t channel_nr[10]; 66bcf00d8fSMatthias Ringwald static char* service_name[10]; 67bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 68bcf00d8fSMatthias Ringwald 69bcf00d8fSMatthias Ringwald 70bcf00d8fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 719ec2630cSMatthias Ringwald UNUSED(channel); 729ec2630cSMatthias Ringwald UNUSED(size); 739ec2630cSMatthias Ringwald 74bcf00d8fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 750e2df43fSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 76bcf00d8fSMatthias Ringwald 77bcf00d8fSMatthias Ringwald switch (event) { 78bcf00d8fSMatthias Ringwald case BTSTACK_EVENT_STATE: 79d356a6daSMatthias Ringwald // BTstack activated, get started 80fb42b6e5SMilanka Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 81235946f1SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 82bcf00d8fSMatthias Ringwald } 83bcf00d8fSMatthias Ringwald break; 84bcf00d8fSMatthias Ringwald default: 85bcf00d8fSMatthias Ringwald break; 86bcf00d8fSMatthias Ringwald } 87bcf00d8fSMatthias Ringwald } 88bcf00d8fSMatthias Ringwald 89bcf00d8fSMatthias Ringwald static void store_found_service(const char * name, uint8_t port){ 90bcf00d8fSMatthias Ringwald printf("APP: Service name: '%s', RFCOMM port %u\n", name, port); 91bcf00d8fSMatthias Ringwald channel_nr[service_index] = port; 92bcf00d8fSMatthias Ringwald service_name[service_index] = (char*) malloc(SDP_SERVICE_NAME_LEN+1); 93bcf00d8fSMatthias Ringwald strncpy(service_name[service_index], (char*) name, SDP_SERVICE_NAME_LEN); 94bcf00d8fSMatthias Ringwald service_name[service_index][SDP_SERVICE_NAME_LEN] = 0; 95bcf00d8fSMatthias Ringwald service_index++; 96bcf00d8fSMatthias Ringwald } 97bcf00d8fSMatthias Ringwald 98bcf00d8fSMatthias Ringwald static void report_found_services(void){ 99bcf00d8fSMatthias Ringwald printf("\n *** Client query response done. "); 100bcf00d8fSMatthias Ringwald if (service_index == 0){ 101bcf00d8fSMatthias Ringwald printf("No service found.\n\n"); 102bcf00d8fSMatthias Ringwald } else { 103bcf00d8fSMatthias Ringwald printf("Found following %d services:\n", service_index); 104bcf00d8fSMatthias Ringwald } 105bcf00d8fSMatthias Ringwald int i; 106bcf00d8fSMatthias Ringwald for (i=0; i<service_index; i++){ 107bcf00d8fSMatthias Ringwald printf(" Service name %s, RFCOMM port %u\n", service_name[i], channel_nr[i]); 108bcf00d8fSMatthias Ringwald } 109bcf00d8fSMatthias Ringwald printf(" ***\n\n"); 110bcf00d8fSMatthias Ringwald } 111bcf00d8fSMatthias Ringwald 112bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1139ec2630cSMatthias Ringwald UNUSED(packet_type); 1149ec2630cSMatthias Ringwald UNUSED(channel); 1159ec2630cSMatthias Ringwald UNUSED(size); 1169ec2630cSMatthias Ringwald 117bcf00d8fSMatthias Ringwald switch (packet[0]){ 118bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 119bcf00d8fSMatthias Ringwald store_found_service(sdp_event_query_rfcomm_service_get_name(packet), 120bcf00d8fSMatthias Ringwald sdp_event_query_rfcomm_service_get_rfcomm_channel(packet)); 121bcf00d8fSMatthias Ringwald break; 122bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 1236dbf1f9aSMilanka Ringwald if (sdp_event_query_complete_get_status(packet)){ 1246dbf1f9aSMilanka Ringwald printf("SDP query failed 0x%02x\n", sdp_event_query_complete_get_status(packet)); 1256dbf1f9aSMilanka Ringwald break; 1266dbf1f9aSMilanka Ringwald } 1276dbf1f9aSMilanka Ringwald printf("SDP query done.\n"); 128bcf00d8fSMatthias Ringwald report_found_services(); 129bcf00d8fSMatthias Ringwald break; 130bcf00d8fSMatthias Ringwald } 131bcf00d8fSMatthias Ringwald } 132bcf00d8fSMatthias Ringwald 133bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 134bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 1359ec2630cSMatthias Ringwald (void)argc; 1369ec2630cSMatthias Ringwald (void)argv; 1379ec2630cSMatthias Ringwald 138*a4fe6467SMatthias Ringwald // init L2CAP 139*a4fe6467SMatthias Ringwald l2cap_init(); 140bcf00d8fSMatthias Ringwald 141bcf00d8fSMatthias Ringwald // register for HCI events 142bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 143bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 144bcf00d8fSMatthias Ringwald 145bcf00d8fSMatthias Ringwald // turn on! 146bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 147bcf00d8fSMatthias Ringwald 148bcf00d8fSMatthias Ringwald return 0; 149bcf00d8fSMatthias Ringwald } 150