2015-02-06 11:25:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 BlueKitchen GmbH
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
|
|
* personal benefit and not for any commercial purpose or for
|
|
|
|
* monetary gain.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
2021-11-02 09:31:00 +01:00
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
|
|
|
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
2015-02-06 11:25:18 +00:00
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* Please inquire about commercial licensing options at
|
|
|
|
* contact@bluekitchen-gmbh.com
|
|
|
|
*
|
|
|
|
*/
|
2017-03-24 23:39:20 +01:00
|
|
|
|
2019-05-11 19:31:55 +02:00
|
|
|
#define BTSTACK_FILE__ "sdp_rfcomm_query.c"
|
2015-02-06 11:25:18 +00:00
|
|
|
|
2014-10-16 09:15:23 +00:00
|
|
|
// *****************************************************************************
|
2020-10-23 09:54:54 +02:00
|
|
|
/* EXAMPLE_START(sdp_rfcomm_query): SDP Client - Query RFCOMM SDP record
|
|
|
|
*
|
|
|
|
* @text The example shows how the SDP Client is used to get all RFCOMM service
|
|
|
|
* records from a remote device. It extracts the remote RFCOMM Server Channel,
|
|
|
|
* which are needed to connect to a remote RFCOMM service.
|
|
|
|
*/
|
2014-10-16 09:15:23 +00:00
|
|
|
// *****************************************************************************
|
2013-06-10 10:43:53 +00:00
|
|
|
|
2016-01-21 15:41:16 +01:00
|
|
|
#include "btstack_config.h"
|
2013-06-10 10:43:53 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
2016-01-21 11:33:17 +01:00
|
|
|
#include "hci_cmd.h"
|
2016-01-20 15:08:39 +01:00
|
|
|
#include "btstack_run_loop.h"
|
2013-06-10 10:43:53 +00:00
|
|
|
|
2017-03-26 22:19:18 +02:00
|
|
|
#include "btstack.h"
|
2013-06-10 10:43:53 +00:00
|
|
|
|
2018-11-28 10:54:20 +01:00
|
|
|
#define NUM_SERVICES 10
|
|
|
|
|
2016-02-17 20:21:21 +01:00
|
|
|
static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
|
|
|
|
2018-10-03 17:19:22 +02:00
|
|
|
static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};
|
2013-06-10 10:43:53 +00:00
|
|
|
|
2018-11-28 10:54:20 +01:00
|
|
|
static struct {
|
|
|
|
uint8_t channel_nr;
|
|
|
|
char service_name[SDP_SERVICE_NAME_LEN+1];
|
|
|
|
} services[NUM_SERVICES];
|
|
|
|
|
2013-06-10 10:43:53 +00:00
|
|
|
static uint8_t service_index = 0;
|
2018-11-28 10:54:20 +01:00
|
|
|
|
2016-02-03 21:55:36 +01:00
|
|
|
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
2020-09-07 11:36:34 +02:00
|
|
|
static btstack_context_callback_registration_t handle_sdp_client_query_request;
|
2013-06-10 10:43:53 +00:00
|
|
|
|
2020-09-07 11:36:34 +02:00
|
|
|
static void handle_start_sdp_client_query(void * context){
|
|
|
|
UNUSED(context);
|
|
|
|
sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
|
|
|
|
}
|
2013-06-10 10:43:53 +00:00
|
|
|
|
2016-02-04 17:45:02 +01:00
|
|
|
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
2016-12-22 22:16:16 +01:00
|
|
|
UNUSED(channel);
|
|
|
|
UNUSED(size);
|
|
|
|
|
2013-06-10 10:43:53 +00:00
|
|
|
if (packet_type != HCI_EVENT_PACKET) return;
|
2016-02-19 14:52:36 +01:00
|
|
|
uint8_t event = hci_event_packet_get_type(packet);
|
2013-06-10 10:43:53 +00:00
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case BTSTACK_EVENT_STATE:
|
2016-04-08 23:23:09 +02:00
|
|
|
// BTstack activated, get started
|
2016-04-01 12:07:08 +02:00
|
|
|
if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
|
2020-09-07 11:36:34 +02:00
|
|
|
handle_sdp_client_query_request.callback = &handle_start_sdp_client_query;
|
2020-09-08 10:50:12 +02:00
|
|
|
(void) sdp_client_register_query_callback(&handle_sdp_client_query_request);
|
2013-06-10 10:43:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 18:06:32 +01:00
|
|
|
static void store_found_service(const char * name, uint8_t port){
|
2013-06-10 10:43:53 +00:00
|
|
|
printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
|
2018-11-28 10:54:20 +01:00
|
|
|
if (service_index < NUM_SERVICES){
|
|
|
|
services[service_index].channel_nr = port;
|
|
|
|
strncpy(services[service_index].service_name, (char*) name, SDP_SERVICE_NAME_LEN);
|
|
|
|
services[service_index].service_name[SDP_SERVICE_NAME_LEN] = 0;
|
|
|
|
service_index++;
|
|
|
|
} else {
|
|
|
|
printf("APP: list full - ignore\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-06-10 10:43:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 19:43:35 +01:00
|
|
|
static void report_found_services(void){
|
2013-06-10 10:43:53 +00:00
|
|
|
printf("\n *** Client query response done. ");
|
|
|
|
if (service_index == 0){
|
|
|
|
printf("No service found.\n\n");
|
|
|
|
} else {
|
|
|
|
printf("Found following %d services:\n", service_index);
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for (i=0; i<service_index; i++){
|
2018-11-28 10:54:20 +01:00
|
|
|
printf(" Service name %s, RFCOMM port %u\n", services[i].service_name, services[i].channel_nr);
|
2013-06-10 10:43:53 +00:00
|
|
|
}
|
|
|
|
printf(" ***\n\n");
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:21:21 +01:00
|
|
|
static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
2016-12-22 22:16:16 +01:00
|
|
|
UNUSED(packet_type);
|
|
|
|
UNUSED(channel);
|
|
|
|
UNUSED(size);
|
|
|
|
|
2020-10-27 13:44:18 +01:00
|
|
|
switch (hci_event_packet_get_type(packet)){
|
2016-01-30 23:58:36 +01:00
|
|
|
case SDP_EVENT_QUERY_RFCOMM_SERVICE:
|
2016-02-03 16:10:30 +01:00
|
|
|
store_found_service(sdp_event_query_rfcomm_service_get_name(packet),
|
|
|
|
sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
|
2013-06-10 10:43:53 +00:00
|
|
|
break;
|
2016-01-30 23:58:36 +01:00
|
|
|
case SDP_EVENT_QUERY_COMPLETE:
|
2017-08-22 11:11:10 +02:00
|
|
|
if (sdp_event_query_complete_get_status(packet)){
|
|
|
|
printf("SDP query failed 0x%02x\n", sdp_event_query_complete_get_status(packet));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("SDP query done.\n");
|
2013-06-10 10:43:53 +00:00
|
|
|
report_found_services();
|
|
|
|
break;
|
2020-10-27 13:44:18 +01:00
|
|
|
default:
|
|
|
|
break;
|
2013-06-10 10:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:42:29 +00:00
|
|
|
int btstack_main(int argc, const char * argv[]);
|
|
|
|
int btstack_main(int argc, const char * argv[]){
|
2016-12-22 22:16:16 +01:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
2018-08-24 10:03:14 +02:00
|
|
|
// init L2CAP
|
|
|
|
l2cap_init();
|
|
|
|
|
2016-02-03 21:55:36 +01:00
|
|
|
// register for HCI events
|
|
|
|
hci_event_callback_registration.callback = &packet_handler;
|
|
|
|
hci_add_event_handler(&hci_event_callback_registration);
|
|
|
|
|
2014-11-14 20:42:29 +00:00
|
|
|
// turn on!
|
|
|
|
hci_power_control(HCI_POWER_ON);
|
|
|
|
|
2013-06-10 10:43:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-10-23 09:54:54 +02:00
|
|
|
|
|
|
|
/* EXAMPLE_END */
|