xref: /btstack/example/sdp_rfcomm_query.c (revision a4fe6467953bdb173fdf96a604f6527ed88f81c3)
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 "btstack.h"
58 
59 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
60 
61 // static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3};
62 static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};
63 
64 static uint8_t  service_index = 0;
65 static uint8_t  channel_nr[10];
66 static char*    service_name[10];
67 static btstack_packet_callback_registration_t hci_event_callback_registration;
68 
69 
70 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
71     UNUSED(channel);
72     UNUSED(size);
73 
74     if (packet_type != HCI_EVENT_PACKET) return;
75     uint8_t event = hci_event_packet_get_type(packet);
76 
77     switch (event) {
78         case BTSTACK_EVENT_STATE:
79             // BTstack activated, get started
80             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
81                 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
82             }
83             break;
84         default:
85             break;
86     }
87 }
88 
89 static void store_found_service(const char * name, uint8_t port){
90     printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
91     channel_nr[service_index] = port;
92     service_name[service_index] = (char*) malloc(SDP_SERVICE_NAME_LEN+1);
93     strncpy(service_name[service_index], (char*) name, SDP_SERVICE_NAME_LEN);
94     service_name[service_index][SDP_SERVICE_NAME_LEN] = 0;
95     service_index++;
96 }
97 
98 static void report_found_services(void){
99     printf("\n *** Client query response done. ");
100     if (service_index == 0){
101         printf("No service found.\n\n");
102     } else {
103         printf("Found following %d services:\n", service_index);
104     }
105     int i;
106     for (i=0; i<service_index; i++){
107         printf("     Service name %s, RFCOMM port %u\n", service_name[i], channel_nr[i]);
108     }
109     printf(" ***\n\n");
110 }
111 
112 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
113     UNUSED(packet_type);
114     UNUSED(channel);
115     UNUSED(size);
116 
117     switch (packet[0]){
118         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
119             store_found_service(sdp_event_query_rfcomm_service_get_name(packet),
120                                 sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
121             break;
122         case SDP_EVENT_QUERY_COMPLETE:
123             if (sdp_event_query_complete_get_status(packet)){
124                 printf("SDP query failed 0x%02x\n", sdp_event_query_complete_get_status(packet));
125                 break;
126             }
127             printf("SDP query done.\n");
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     (void)argc;
136     (void)argv;
137 
138     // init L2CAP
139     l2cap_init();
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     // turn on!
146     hci_power_control(HCI_POWER_ON);
147 
148     return 0;
149 }
150