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