btstack_link_key_db_memory.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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__ "btstack_link_key_db_memory.c"
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include "classic/btstack_link_key_db_memory.h"
  41. #include "btstack_debug.h"
  42. #include "btstack_linked_list.h"
  43. #include "btstack_memory.h"
  44. #include "btstack_util.h"
  45. #include "classic/core.h"
  46. // This list should be directly accessed only by tests
  47. btstack_linked_list_t db_mem_link_keys = NULL;
  48. // Device info
  49. static void db_open(void){
  50. }
  51. static void db_set_local_bd_addr(bd_addr_t bd_addr){
  52. (void)bd_addr;
  53. }
  54. static void db_close(void){
  55. }
  56. static btstack_link_key_db_memory_entry_t * get_item(btstack_linked_list_t list, bd_addr_t bd_addr) {
  57. btstack_linked_item_t *it;
  58. for (it = (btstack_linked_item_t *) list; it ; it = it->next){
  59. btstack_link_key_db_memory_entry_t * item = (btstack_link_key_db_memory_entry_t *) it;
  60. if (bd_addr_cmp(item->bd_addr, bd_addr) == 0) {
  61. return item;
  62. }
  63. }
  64. return NULL;
  65. }
  66. static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) {
  67. btstack_link_key_db_memory_entry_t * item = get_item(db_mem_link_keys, bd_addr);
  68. if (!item) return 0;
  69. (void)memcpy(link_key, item->link_key, LINK_KEY_LEN);
  70. if (link_key_type) {
  71. *link_key_type = item->link_key_type;
  72. }
  73. btstack_linked_list_remove(&db_mem_link_keys, (btstack_linked_item_t *) item);
  74. btstack_linked_list_add(&db_mem_link_keys, (btstack_linked_item_t *) item);
  75. return 1;
  76. }
  77. static void delete_link_key(bd_addr_t bd_addr){
  78. btstack_link_key_db_memory_entry_t * item = get_item(db_mem_link_keys, bd_addr);
  79. if (!item) return;
  80. btstack_linked_list_remove(&db_mem_link_keys, (btstack_linked_item_t *) item);
  81. btstack_memory_btstack_link_key_db_memory_entry_free((btstack_link_key_db_memory_entry_t*)item);
  82. }
  83. static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){
  84. // check for existing record and remove if found
  85. btstack_link_key_db_memory_entry_t * record = get_item(db_mem_link_keys, bd_addr);
  86. if (record){
  87. btstack_linked_list_remove(&db_mem_link_keys, (btstack_linked_item_t*) record);
  88. }
  89. // record not found, get new one from memory pool
  90. if (!record) {
  91. record = btstack_memory_btstack_link_key_db_memory_entry_get();
  92. }
  93. // if none left, re-use last item and remove from list
  94. if (!record){
  95. record = (btstack_link_key_db_memory_entry_t*) btstack_linked_list_get_last_item(&db_mem_link_keys);
  96. if (record) {
  97. btstack_linked_list_remove(&db_mem_link_keys, (btstack_linked_item_t*) record);
  98. }
  99. }
  100. if (!record) return;
  101. (void)memcpy(record->bd_addr, bd_addr, sizeof(bd_addr_t));
  102. (void)memcpy(record->link_key, link_key, LINK_KEY_LEN);
  103. record->link_key_type = link_key_type;
  104. btstack_linked_list_add(&db_mem_link_keys, (btstack_linked_item_t *) record);
  105. }
  106. static int iterator_init(btstack_link_key_iterator_t * it){
  107. it->context = (void*) db_mem_link_keys;
  108. return 1;
  109. }
  110. static int iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type){
  111. btstack_link_key_db_memory_entry_t *item = (btstack_link_key_db_memory_entry_t *) it->context;
  112. if (item == NULL) return 0;
  113. // fetch values
  114. (void)memcpy(bd_addr, item->bd_addr, 6);
  115. (void)memcpy(link_key, item->link_key, 16);
  116. *link_key_type = item->link_key_type;
  117. // next
  118. it->context = (void *) item->item.next;
  119. return 1;
  120. }
  121. static void iterator_done(btstack_link_key_iterator_t * it){
  122. UNUSED(it);
  123. }
  124. const btstack_link_key_db_t btstack_link_key_db_memory = {
  125. db_open,
  126. db_set_local_bd_addr,
  127. db_close,
  128. get_link_key,
  129. put_link_key,
  130. delete_link_key,
  131. iterator_init,
  132. iterator_get_next,
  133. iterator_done,
  134. };
  135. const btstack_link_key_db_t * btstack_link_key_db_memory_instance(void){
  136. return &btstack_link_key_db_memory;
  137. }