gap_link_keys.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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__ "gap_link_keys.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(gap_link_keys): GAP Link Key Management Example
  40. * @text Shows how to iterate over the Classic Link Keys stored in NVS
  41. * Link Keys are per device-device bonding. If the Bluetooth Controller can be swapped,
  42. * e.g. on desktop systems, a Link Key DB for each Controller is needed.
  43. * We need to wait until the Bluetooth Stack has started up and selected
  44. * the correct Link Key DB based on the Controller's BD_ADDR.
  45. */
  46. // *****************************************************************************
  47. #include <stdint.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include "btstack.h"
  52. static btstack_packet_callback_registration_t hci_event_callback_registration;
  53. /* @section GAP Link Key Logic
  54. *
  55. * @text List stored link keys
  56. */
  57. static void list_link_keys(void){
  58. bd_addr_t addr;
  59. link_key_t link_key;
  60. link_key_type_t type;
  61. btstack_link_key_iterator_t it;
  62. int ok = gap_link_key_iterator_init(&it);
  63. if (!ok) {
  64. printf("Link key iterator not implemented\n");
  65. return;
  66. }
  67. printf("Stored link keys: \n");
  68. while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){
  69. printf("%s - type %u, key: ", bd_addr_to_str(addr), (int) type);
  70. printf_hexdump(link_key, 16);
  71. }
  72. printf(".\n");
  73. gap_link_key_iterator_done(&it);
  74. }
  75. /* @section Bluetooth Logic
  76. *
  77. * @text Wait for Bluetooth startup before listing the stored link keys
  78. */
  79. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  80. UNUSED(channel);
  81. UNUSED(size);
  82. if (packet_type != HCI_EVENT_PACKET) return;
  83. switch(hci_event_packet_get_type(packet)){
  84. case BTSTACK_EVENT_STATE:
  85. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
  86. list_link_keys();
  87. break;
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. }
  94. /* @section Main Application Setup
  95. *
  96. * @text Listing MainConfiguration shows main application code.
  97. * It registers the HCI packet handler and starts the Bluetooth stack.
  98. */
  99. /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */
  100. int btstack_main(int argc, const char * argv[]);
  101. int btstack_main(int argc, const char * argv[]) {
  102. (void)argc;
  103. (void)argv;
  104. hci_event_callback_registration.callback = &packet_handler;
  105. hci_add_event_handler(&hci_event_callback_registration);
  106. // turn on!
  107. hci_power_control(HCI_POWER_ON);
  108. return 0;
  109. }
  110. /* LISTING_END */
  111. /* EXAMPLE_END */