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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH 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
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "sdp_rfcomm_query.c"
39bcf00d8fSMatthias Ringwald
40bcf00d8fSMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(sdp_rfcomm_query): SDP Client - Query RFCOMM SDP record
42ec8ae085SMilanka Ringwald *
43ec8ae085SMilanka Ringwald * @text The example shows how the SDP Client is used to get all RFCOMM service
44ec8ae085SMilanka Ringwald * records from a remote device. It extracts the remote RFCOMM Server Channel,
45ec8ae085SMilanka Ringwald * which are needed to connect to a remote RFCOMM service.
46ec8ae085SMilanka Ringwald */
47bcf00d8fSMatthias Ringwald // *****************************************************************************
48bcf00d8fSMatthias Ringwald
49bcf00d8fSMatthias Ringwald #include "btstack_config.h"
50bcf00d8fSMatthias Ringwald
51bcf00d8fSMatthias Ringwald #include <stdint.h>
52bcf00d8fSMatthias Ringwald #include <stdio.h>
53bcf00d8fSMatthias Ringwald #include <stdlib.h>
54bcf00d8fSMatthias Ringwald #include <string.h>
55bcf00d8fSMatthias Ringwald
56bcf00d8fSMatthias Ringwald
57bcf00d8fSMatthias Ringwald #include "hci_cmd.h"
58bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h"
59bcf00d8fSMatthias Ringwald
60235946f1SMatthias Ringwald #include "btstack.h"
61bcf00d8fSMatthias Ringwald
6276780d6cSMatthias Ringwald #define NUM_SERVICES 10
6376780d6cSMatthias Ringwald
64bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
65bcf00d8fSMatthias Ringwald
665dcb6ca7SMatthias Ringwald static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};
67bcf00d8fSMatthias Ringwald
6876780d6cSMatthias Ringwald static struct {
6976780d6cSMatthias Ringwald uint8_t channel_nr;
7076780d6cSMatthias Ringwald char service_name[SDP_SERVICE_NAME_LEN+1];
7176780d6cSMatthias Ringwald } services[NUM_SERVICES];
7276780d6cSMatthias Ringwald
73bcf00d8fSMatthias Ringwald static uint8_t service_index = 0;
7476780d6cSMatthias Ringwald
75bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
76ef24a2f7SMilanka Ringwald static btstack_context_callback_registration_t handle_sdp_client_query_request;
77bcf00d8fSMatthias Ringwald
handle_start_sdp_client_query(void * context)78ef24a2f7SMilanka Ringwald static void handle_start_sdp_client_query(void * context){
79ef24a2f7SMilanka Ringwald UNUSED(context);
80ef24a2f7SMilanka Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
81ef24a2f7SMilanka Ringwald }
82bcf00d8fSMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)83bcf00d8fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
849ec2630cSMatthias Ringwald UNUSED(channel);
859ec2630cSMatthias Ringwald UNUSED(size);
869ec2630cSMatthias Ringwald
87bcf00d8fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return;
880e2df43fSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet);
89bcf00d8fSMatthias Ringwald
90bcf00d8fSMatthias Ringwald switch (event) {
91bcf00d8fSMatthias Ringwald case BTSTACK_EVENT_STATE:
92d356a6daSMatthias Ringwald // BTstack activated, get started
93fb42b6e5SMilanka Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
94ef24a2f7SMilanka Ringwald handle_sdp_client_query_request.callback = &handle_start_sdp_client_query;
95231147c5SMilanka Ringwald (void) sdp_client_register_query_callback(&handle_sdp_client_query_request);
96bcf00d8fSMatthias Ringwald }
97bcf00d8fSMatthias Ringwald break;
98bcf00d8fSMatthias Ringwald default:
99bcf00d8fSMatthias Ringwald break;
100bcf00d8fSMatthias Ringwald }
101bcf00d8fSMatthias Ringwald }
102bcf00d8fSMatthias Ringwald
store_found_service(const char * name,uint8_t port)103bcf00d8fSMatthias Ringwald static void store_found_service(const char * name, uint8_t port){
104bcf00d8fSMatthias Ringwald printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
10576780d6cSMatthias Ringwald if (service_index < NUM_SERVICES){
10676780d6cSMatthias Ringwald services[service_index].channel_nr = port;
107182f71a5SMatthias Ringwald btstack_strcpy(services[service_index].service_name, SDP_SERVICE_NAME_LEN + 1, name);
108bcf00d8fSMatthias Ringwald service_index++;
10976780d6cSMatthias Ringwald } else {
11076780d6cSMatthias Ringwald printf("APP: list full - ignore\n");
11176780d6cSMatthias Ringwald return;
11276780d6cSMatthias Ringwald }
113bcf00d8fSMatthias Ringwald }
114bcf00d8fSMatthias Ringwald
report_found_services(void)115bcf00d8fSMatthias Ringwald static void report_found_services(void){
116bcf00d8fSMatthias Ringwald printf("\n *** Client query response done. ");
117bcf00d8fSMatthias Ringwald if (service_index == 0){
118bcf00d8fSMatthias Ringwald printf("No service found.\n\n");
119bcf00d8fSMatthias Ringwald } else {
120bcf00d8fSMatthias Ringwald printf("Found following %d services:\n", service_index);
121bcf00d8fSMatthias Ringwald }
122bcf00d8fSMatthias Ringwald int i;
123bcf00d8fSMatthias Ringwald for (i=0; i<service_index; i++){
12476780d6cSMatthias Ringwald printf(" Service name %s, RFCOMM port %u\n", services[i].service_name, services[i].channel_nr);
125bcf00d8fSMatthias Ringwald }
126bcf00d8fSMatthias Ringwald printf(" ***\n\n");
127bcf00d8fSMatthias Ringwald }
128bcf00d8fSMatthias Ringwald
handle_query_rfcomm_event(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)129bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1309ec2630cSMatthias Ringwald UNUSED(packet_type);
1319ec2630cSMatthias Ringwald UNUSED(channel);
1329ec2630cSMatthias Ringwald UNUSED(size);
1339ec2630cSMatthias Ringwald
1347bbeb3adSMilanka Ringwald switch (hci_event_packet_get_type(packet)){
135bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE:
136bcf00d8fSMatthias Ringwald store_found_service(sdp_event_query_rfcomm_service_get_name(packet),
137bcf00d8fSMatthias Ringwald sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
138bcf00d8fSMatthias Ringwald break;
139bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE:
1406dbf1f9aSMilanka Ringwald if (sdp_event_query_complete_get_status(packet)){
141*fcd55a0bSMilanka Ringwald printf("SDP query failed, status 0x%02x\n", sdp_event_query_complete_get_status(packet));
1426dbf1f9aSMilanka Ringwald break;
1436dbf1f9aSMilanka Ringwald }
1446dbf1f9aSMilanka Ringwald printf("SDP query done.\n");
145bcf00d8fSMatthias Ringwald report_found_services();
146bcf00d8fSMatthias Ringwald break;
1477bbeb3adSMilanka Ringwald default:
1487bbeb3adSMilanka Ringwald break;
149bcf00d8fSMatthias Ringwald }
150bcf00d8fSMatthias Ringwald }
151bcf00d8fSMatthias Ringwald
152bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])153bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
1549ec2630cSMatthias Ringwald (void)argc;
1559ec2630cSMatthias Ringwald (void)argv;
1569ec2630cSMatthias Ringwald
157a4fe6467SMatthias Ringwald // init L2CAP
158a4fe6467SMatthias Ringwald l2cap_init();
159bcf00d8fSMatthias Ringwald
160bcf00d8fSMatthias Ringwald // register for HCI events
161bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
162bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
163bcf00d8fSMatthias Ringwald
164bcf00d8fSMatthias Ringwald // turn on!
165bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON);
166bcf00d8fSMatthias Ringwald
167bcf00d8fSMatthias Ringwald return 0;
168bcf00d8fSMatthias Ringwald }
169ec8ae085SMilanka Ringwald
170ec8ae085SMilanka Ringwald /* EXAMPLE_END */