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