xref: /btstack/src/classic/btstack_link_key_db_tlv.c (revision de804f9e40fa10b2ead91db28fa5571afa6ec08a)
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_tlv.h"
42 
43 #include "btstack_debug.h"
44 #include "btstack_util.h"
45 #include "classic/core.h"
46 
47 // NVM_NUM_LINK_KEYS defines number of stored link keys
48 #ifndef NVM_NUM_LINK_KEYS
49 #define NVM_NUM_LINK_KEYS 1
50 #endif
51 
52 typedef struct {
53     const btstack_tlv_t * btstack_tlv_impl;
54     void * btstack_tlv_context;
55 } btstack_link_key_db_tlv_h;
56 
57 typedef struct link_key_nvm {
58     uint32_t seq_nr;    // used for "least recently stored" eviction strategy
59     bd_addr_t bd_addr;
60     link_key_t link_key;
61     link_key_type_t link_key_type;
62 } link_key_nvm_t;   // sizeof(link_key_nvm_t) = 27 bytes
63 
64 static btstack_link_key_db_tlv_h singleton;
65 static btstack_link_key_db_tlv_h * self = &singleton;
66 
67 static const char tag_0 = 'B';
68 static const char tag_1 = 'T';
69 static const char tag_2 = 'L';
70 
71 static uint32_t btstack_link_key_db_tag_for_index(uint8_t index){
72     return (tag_0 << 24) | (tag_1 << 16) | (tag_2 << 8) | index;
73 }
74 
75 // Device info
76 static void btstack_link_key_db_tlv_open(void){
77 }
78 
79 static void btstack_link_key_db_tlv_set_bd_addr(bd_addr_t bd_addr){
80     (void)bd_addr;
81 }
82 
83 static void btstack_link_key_db_tlv_close(void){
84 }
85 
86 static int btstack_link_key_db_tlv_get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) {
87     int i;
88     for (i=0;i<NVM_NUM_LINK_KEYS;i++){
89         link_key_nvm_t entry;
90         uint32_t tag = btstack_link_key_db_tag_for_index(i);
91         int size = self->btstack_tlv_impl->get_tag(self->btstack_tlv_context, tag, (uint8_t*) &entry, sizeof(entry));
92         if (size == 0) continue;
93         log_info("tag %x, addr %s", tag, bd_addr_to_str(entry.bd_addr));
94         if (memcmp(bd_addr, entry.bd_addr, 6)) continue;
95         // found, pass back
96         memcpy(link_key, entry.link_key, 16);
97         *link_key_type = entry.link_key_type;
98         return 1;
99     }
100 	return 0;
101 }
102 
103 static void btstack_link_key_db_tlv_delete_link_key(bd_addr_t bd_addr){
104     int i;
105     for (i=0;i<NVM_NUM_LINK_KEYS;i++){
106         link_key_nvm_t entry;
107         uint32_t tag = btstack_link_key_db_tag_for_index(i);
108         int size = self->btstack_tlv_impl->get_tag(self->btstack_tlv_context, tag, (uint8_t*) &entry, sizeof(entry));
109         if (size == 0) continue;
110         if (memcmp(bd_addr, entry.bd_addr, 6)) continue;
111         // found, delete tag
112         self->btstack_tlv_impl->delete_tag(self->btstack_tlv_context, tag);
113         break;
114     }
115 }
116 
117 static void btstack_link_key_db_tlv_put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){
118     int i;
119     uint32_t highest_seq_nr = 0;
120     uint32_t lowest_seq_nr = 0;
121     uint32_t tag_for_lowest_seq_nr = 0;
122     uint32_t tag_for_addr = 0;
123     uint32_t tag_for_empty = 0;
124 
125     for (i=0;i<NVM_NUM_LINK_KEYS;i++){
126         link_key_nvm_t entry;
127         uint32_t tag = btstack_link_key_db_tag_for_index(i);
128         int size = self->btstack_tlv_impl->get_tag(self->btstack_tlv_context, tag, (uint8_t*) &entry, sizeof(entry));
129         // empty/deleted tag
130         if (size == 0) {
131             tag_for_empty = tag;
132             continue;
133         }
134         // found addr?
135         if (memcmp(bd_addr, entry.bd_addr, 6) == 0){
136             tag_for_addr = tag;
137         }
138         // update highest seq nr
139         if (entry.seq_nr > highest_seq_nr){
140             highest_seq_nr = entry.seq_nr;
141         }
142         // find entry with lowest seq nr
143         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
144             tag_for_lowest_seq_nr = tag;
145             lowest_seq_nr = entry.seq_nr;
146         }
147     }
148 
149     log_info("tag_for_addr %x, tag_for_empy %x, tag_for_lowest_seq_nr %x", tag_for_addr, tag_for_empty, tag_for_lowest_seq_nr);
150 
151     uint32_t tag_to_use = 0;
152     if (tag_for_addr){
153         tag_to_use = tag_for_addr;
154     } else if (tag_for_empty){
155         tag_to_use = tag_for_empty;
156     } else if (tag_for_lowest_seq_nr){
157         tag_to_use = tag_for_lowest_seq_nr;
158     } else {
159         // should not happen
160         return;
161     }
162 
163     log_info("store with tag %x", tag_to_use);
164 
165     link_key_nvm_t entry;
166 
167     memcpy(entry.bd_addr, bd_addr, 6);
168     memcpy(entry.link_key, link_key, 16);
169     entry.link_key_type = link_key_type;
170     entry.seq_nr = highest_seq_nr + 1;
171 
172     self->btstack_tlv_impl->store_tag(self->btstack_tlv_context, tag_to_use, (uint8_t*) &entry, sizeof(entry));
173 }
174 
175 const btstack_link_key_db_t btstack_link_key_db_tlv = {
176     btstack_link_key_db_tlv_open,
177     btstack_link_key_db_tlv_set_bd_addr,
178     btstack_link_key_db_tlv_close,
179     btstack_link_key_db_tlv_get_link_key,
180     btstack_link_key_db_tlv_put_link_key,
181     btstack_link_key_db_tlv_delete_link_key,
182 };
183 
184 const btstack_link_key_db_t * btstack_link_key_db_tlv_get_instance(const btstack_tlv_t * btstack_tlv_impl, void * btstack_tlv_context){
185     self->btstack_tlv_impl = btstack_tlv_impl;
186     self->btstack_tlv_context = btstack_tlv_context;
187     return &btstack_link_key_db_tlv;
188 }
189 
190 
191