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