sdp_rfcomm_query.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "sdp_rfcomm_query.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(sdp_rfcomm_query): SDP Client - Query RFCOMM SDP record
  40. *
  41. * @text The example shows how the SDP Client is used to get all RFCOMM service
  42. * records from a remote device. It extracts the remote RFCOMM Server Channel,
  43. * which are needed to connect to a remote RFCOMM service.
  44. */
  45. // *****************************************************************************
  46. #include "btstack_config.h"
  47. #include <stdint.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include "hci_cmd.h"
  52. #include "btstack_run_loop.h"
  53. #include "btstack.h"
  54. #define NUM_SERVICES 10
  55. static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  56. static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};
  57. static struct {
  58. uint8_t channel_nr;
  59. char service_name[SDP_SERVICE_NAME_LEN+1];
  60. } services[NUM_SERVICES];
  61. static uint8_t service_index = 0;
  62. static btstack_packet_callback_registration_t hci_event_callback_registration;
  63. static btstack_context_callback_registration_t handle_sdp_client_query_request;
  64. static void handle_start_sdp_client_query(void * context){
  65. UNUSED(context);
  66. sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
  67. }
  68. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  69. UNUSED(channel);
  70. UNUSED(size);
  71. if (packet_type != HCI_EVENT_PACKET) return;
  72. uint8_t event = hci_event_packet_get_type(packet);
  73. switch (event) {
  74. case BTSTACK_EVENT_STATE:
  75. // BTstack activated, get started
  76. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
  77. handle_sdp_client_query_request.callback = &handle_start_sdp_client_query;
  78. (void) sdp_client_register_query_callback(&handle_sdp_client_query_request);
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. static void store_found_service(const char * name, uint8_t port){
  86. printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
  87. if (service_index < NUM_SERVICES){
  88. services[service_index].channel_nr = port;
  89. strncpy(services[service_index].service_name, (char*) name, SDP_SERVICE_NAME_LEN);
  90. services[service_index].service_name[SDP_SERVICE_NAME_LEN] = 0;
  91. service_index++;
  92. } else {
  93. printf("APP: list full - ignore\n");
  94. return;
  95. }
  96. }
  97. static void report_found_services(void){
  98. printf("\n *** Client query response done. ");
  99. if (service_index == 0){
  100. printf("No service found.\n\n");
  101. } else {
  102. printf("Found following %d services:\n", service_index);
  103. }
  104. int i;
  105. for (i=0; i<service_index; i++){
  106. printf(" Service name %s, RFCOMM port %u\n", services[i].service_name, services[i].channel_nr);
  107. }
  108. printf(" ***\n\n");
  109. }
  110. static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  111. UNUSED(packet_type);
  112. UNUSED(channel);
  113. UNUSED(size);
  114. switch (hci_event_packet_get_type(packet)){
  115. case SDP_EVENT_QUERY_RFCOMM_SERVICE:
  116. store_found_service(sdp_event_query_rfcomm_service_get_name(packet),
  117. sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
  118. break;
  119. case SDP_EVENT_QUERY_COMPLETE:
  120. if (sdp_event_query_complete_get_status(packet)){
  121. printf("SDP query failed 0x%02x\n", sdp_event_query_complete_get_status(packet));
  122. break;
  123. }
  124. printf("SDP query done.\n");
  125. report_found_services();
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. int btstack_main(int argc, const char * argv[]);
  132. int btstack_main(int argc, const char * argv[]){
  133. (void)argc;
  134. (void)argv;
  135. // init L2CAP
  136. l2cap_init();
  137. // register for HCI events
  138. hci_event_callback_registration.callback = &packet_handler;
  139. hci_add_event_handler(&hci_event_callback_registration);
  140. // turn on!
  141. hci_power_control(HCI_POWER_ON);
  142. return 0;
  143. }
  144. /* EXAMPLE_END */