1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "sdp_rfcomm_query.c" 39 40 // ***************************************************************************** 41 // 42 // minimal setup for SDP client over USB or UART 43 // 44 // ***************************************************************************** 45 46 #include "btstack_config.h" 47 48 #include <stdint.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 53 54 #include "hci_cmd.h" 55 #include "btstack_run_loop.h" 56 57 #include "hci.h" 58 #include "btstack_memory.h" 59 #include "hci_dump.h" 60 #include "l2cap.h" 61 #include "classic/sdp_client_rfcomm.h" 62 #include "btstack_event.h" 63 64 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 65 66 // static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; 67 static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15}; 68 69 static uint8_t service_index = 0; 70 static uint8_t channel_nr[10]; 71 static char* service_name[10]; 72 static btstack_packet_callback_registration_t hci_event_callback_registration; 73 74 75 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 76 UNUSED(channel); 77 UNUSED(size); 78 79 if (packet_type != HCI_EVENT_PACKET) return; 80 uint8_t event = hci_event_packet_get_type(packet); 81 82 switch (event) { 83 case BTSTACK_EVENT_STATE: 84 // BTstack activated, get started 85 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 86 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_PublicBrowseGroup); 87 } 88 break; 89 default: 90 break; 91 } 92 } 93 94 static void store_found_service(const char * name, uint8_t port){ 95 printf("APP: Service name: '%s', RFCOMM port %u\n", name, port); 96 channel_nr[service_index] = port; 97 service_name[service_index] = (char*) malloc(SDP_SERVICE_NAME_LEN+1); 98 strncpy(service_name[service_index], (char*) name, SDP_SERVICE_NAME_LEN); 99 service_name[service_index][SDP_SERVICE_NAME_LEN] = 0; 100 service_index++; 101 } 102 103 static void report_found_services(void){ 104 printf("\n *** Client query response done. "); 105 if (service_index == 0){ 106 printf("No service found.\n\n"); 107 } else { 108 printf("Found following %d services:\n", service_index); 109 } 110 int i; 111 for (i=0; i<service_index; i++){ 112 printf(" Service name %s, RFCOMM port %u\n", service_name[i], channel_nr[i]); 113 } 114 printf(" ***\n\n"); 115 } 116 117 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 118 UNUSED(packet_type); 119 UNUSED(channel); 120 UNUSED(size); 121 122 switch (packet[0]){ 123 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 124 store_found_service(sdp_event_query_rfcomm_service_get_name(packet), 125 sdp_event_query_rfcomm_service_get_rfcomm_channel(packet)); 126 break; 127 case SDP_EVENT_QUERY_COMPLETE: 128 report_found_services(); 129 break; 130 } 131 } 132 133 int btstack_main(int argc, const char * argv[]); 134 int btstack_main(int argc, const char * argv[]){ 135 136 (void)argc; 137 (void)argv; 138 139 printf("Client HCI init done\r\n"); 140 141 // register for HCI events 142 hci_event_callback_registration.callback = &packet_handler; 143 hci_add_event_handler(&hci_event_callback_registration); 144 145 // init L2CAP 146 l2cap_init(); 147 148 // turn on! 149 hci_power_control(HCI_POWER_ON); 150 151 return 0; 152 } 153