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 // ***************************************************************************** 39 // 40 // minimal setup for SDP client over USB or UART 41 // 42 // ***************************************************************************** 43 44 #include "btstack_config.h" 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 52 #include "hci_cmd.h" 53 #include "btstack_run_loop.h" 54 55 #include "hci.h" 56 #include "btstack_memory.h" 57 #include "hci_dump.h" 58 #include "l2cap.h" 59 #include "classic/sdp_query_rfcomm.h" 60 #include "btstack_event.h" 61 62 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 63 64 // static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; 65 static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15}; 66 67 static uint8_t service_index = 0; 68 static uint8_t channel_nr[10]; 69 static char* service_name[10]; 70 static btstack_packet_callback_registration_t hci_event_callback_registration; 71 72 73 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 74 // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); 75 76 if (packet_type != HCI_EVENT_PACKET) return; 77 uint8_t event = packet[0]; 78 79 switch (event) { 80 case BTSTACK_EVENT_STATE: 81 // bt stack activated, get started 82 if (packet[2] == HCI_STATE_WORKING){ 83 sdp_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_PublicBrowseGroup); 84 } 85 break; 86 default: 87 break; 88 } 89 } 90 91 static void store_found_service(const char * name, uint8_t port){ 92 printf("APP: Service name: '%s', RFCOMM port %u\n", name, port); 93 channel_nr[service_index] = port; 94 service_name[service_index] = (char*) malloc(SDP_SERVICE_NAME_LEN+1); 95 strncpy(service_name[service_index], (char*) name, SDP_SERVICE_NAME_LEN); 96 service_name[service_index][SDP_SERVICE_NAME_LEN] = 0; 97 service_index++; 98 } 99 100 static void report_found_services(void){ 101 printf("\n *** Client query response done. "); 102 if (service_index == 0){ 103 printf("No service found.\n\n"); 104 } else { 105 printf("Found following %d services:\n", service_index); 106 } 107 int i; 108 for (i=0; i<service_index; i++){ 109 printf(" Service name %s, RFCOMM port %u\n", service_name[i], channel_nr[i]); 110 } 111 printf(" ***\n\n"); 112 } 113 114 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 115 switch (packet[0]){ 116 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 117 store_found_service(sdp_event_query_rfcomm_service_get_name(packet), 118 sdp_event_query_rfcomm_service_get_rfcomm_channel(packet)); 119 break; 120 case SDP_EVENT_QUERY_COMPLETE: 121 report_found_services(); 122 break; 123 } 124 } 125 126 int btstack_main(int argc, const char * argv[]); 127 int btstack_main(int argc, const char * argv[]){ 128 129 printf("Client HCI init done\r\n"); 130 131 // register for HCI events 132 hci_event_callback_registration.callback = &packet_handler; 133 hci_add_event_handler(&hci_event_callback_registration); 134 135 // init L2CAP 136 l2cap_init(); 137 138 // turn on! 139 hci_power_control(HCI_POWER_ON); 140 141 return 0; 142 } 143