-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_device_db_memory.c
More file actions
198 lines (154 loc) · 6.52 KB
/
Copy pathremote_device_db_memory.c
File metadata and controls
198 lines (154 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (C) 2009-2012 by Matthias Ringwald
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at btstack@ringwald.ch
*
*/
#include <string.h>
#include <stdlib.h>
#include "remote_device_db.h"
#include "btstack_memory.h"
#include "debug.h"
#include <btstack/utils.h>
#include <btstack/linked_list.h>
// This lists should be only accessed by tests.
linked_list_t db_mem_link_keys = NULL;
linked_list_t db_mem_names = NULL;
static linked_list_t db_mem_services = NULL;
// Device info
static void db_open(void){
}
static void db_close(void){
}
static db_mem_device_t * get_item(linked_list_t list, bd_addr_t *bd_addr) {
linked_item_t *it;
for (it = (linked_item_t *) list; it ; it = it->next){
db_mem_device_t * item = (db_mem_device_t *) it;
if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) {
return item;
}
}
return NULL;
}
static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) {
db_mem_device_name_t * item = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
if (!item) return 0;
strncpy((char*)device_name, item->device_name, MAX_NAME_LEN);
linked_list_remove(&db_mem_names, (linked_item_t *) item);
linked_list_add(&db_mem_names, (linked_item_t *) item);
return 1;
}
static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) {
db_mem_device_link_key_t * item = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
if (!item) return 0;
memcpy(link_key, item->link_key, LINK_KEY_LEN);
linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
linked_list_add(&db_mem_link_keys, (linked_item_t *) item);
return 1;
}
static void delete_link_key(bd_addr_t *bd_addr){
db_mem_device_t * item = get_item(db_mem_link_keys, bd_addr);
if (!item) return;
linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
btstack_memory_db_mem_device_link_key_free(item);
}
static void put_link_key(bd_addr_t *bd_addr, link_key_t *link_key){
db_mem_device_link_key_t * existingRecord = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
if (existingRecord){
memcpy(existingRecord->link_key, link_key, LINK_KEY_LEN);
return;
}
// Record not found, create new one for this device
db_mem_device_link_key_t * newItem = (db_mem_device_link_key_t*) btstack_memory_db_mem_device_link_key_get();
if (!newItem){
newItem = (db_mem_device_link_key_t*)linked_list_get_last_item(&db_mem_link_keys);
}
if (!newItem) return;
memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
memcpy(newItem->link_key, link_key, LINK_KEY_LEN);
linked_list_add(&db_mem_link_keys, (linked_item_t *) newItem);
}
static void delete_name(bd_addr_t *bd_addr){
db_mem_device_t * item = get_item(db_mem_names, bd_addr);
if (!item) return;
linked_list_remove(&db_mem_names, (linked_item_t *) item);
btstack_memory_db_mem_device_name_free(item);
}
static void put_name(bd_addr_t *bd_addr, device_name_t *device_name){
db_mem_device_name_t * existingRecord = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
if (existingRecord){
strncpy(existingRecord->device_name, (const char*) device_name, MAX_NAME_LEN);
return;
}
// Record not found, create a new one for this device
db_mem_device_name_t * newItem = (db_mem_device_name_t *) btstack_memory_db_mem_device_name_get();
if (!newItem) {
newItem = (db_mem_device_name_t*)linked_list_get_last_item(&db_mem_names);
};
if (!newItem) return;
memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
strncpy(newItem->device_name, (const char*) device_name, MAX_NAME_LEN);
linked_list_add(&db_mem_names, (linked_item_t *) newItem);
}
// MARK: PERSISTENT RFCOMM CHANNEL ALLOCATION
static uint8_t persistent_rfcomm_channel(char *serviceName){
linked_item_t *it;
db_mem_service_t * item;
uint8_t max_channel = 1;
for (it = (linked_item_t *) db_mem_services; it ; it = it->next){
item = (db_mem_service_t *) it;
if (strncmp(item->service_name, serviceName, MAX_NAME_LEN) == 0) {
// Match found
return item->channel;
}
// TODO prevent overflow
if (item->channel >= max_channel) max_channel = item->channel + 1;
}
// Allocate new persistant channel
db_mem_service_t * newItem = (db_mem_service_t *) btstack_memory_db_mem_service_get();
if (!newItem) return 0;
strncpy(newItem->service_name, serviceName, MAX_NAME_LEN);
newItem->channel = max_channel;
linked_list_add(&db_mem_services, (linked_item_t *) newItem);
return max_channel;
}
const remote_device_db_t remote_device_db_memory = {
db_open,
db_close,
get_link_key,
put_link_key,
delete_link_key,
get_name,
put_name,
delete_name,
persistent_rfcomm_channel
};