[337] | 1 | /*
|
---|
| 2 | * Copyright (C) 2009-2012 by Matthias Ringwald
|
---|
| 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 MATTHIAS RINGWALD 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 btstack@ringwald.ch
|
---|
| 34 | *
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <string.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 |
|
---|
| 40 | #include "remote_device_db.h"
|
---|
| 41 | #include "btstack_memory.h"
|
---|
| 42 | #include "debug.h"
|
---|
| 43 |
|
---|
| 44 | #include <btstack/utils.h>
|
---|
| 45 | #include <btstack/linked_list.h>
|
---|
| 46 |
|
---|
| 47 | // This lists should be only accessed by tests.
|
---|
| 48 | linked_list_t db_mem_link_keys = NULL;
|
---|
| 49 | linked_list_t db_mem_names = NULL;
|
---|
| 50 | static linked_list_t db_mem_services = NULL;
|
---|
| 51 |
|
---|
| 52 | // Device info
|
---|
| 53 | static void db_open(void){
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | static void db_close(void){
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static db_mem_device_t * get_item(linked_list_t list, bd_addr_t *bd_addr) {
|
---|
| 60 | linked_item_t *it;
|
---|
| 61 | for (it = (linked_item_t *) list; it ; it = it->next){
|
---|
| 62 | db_mem_device_t * item = (db_mem_device_t *) it;
|
---|
| 63 | if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) {
|
---|
| 64 | return item;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return NULL;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) {
|
---|
| 71 | db_mem_device_name_t * item = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
|
---|
| 72 |
|
---|
| 73 | if (!item) return 0;
|
---|
| 74 |
|
---|
| 75 | strlcpy((char*)device_name, item->device_name, MAX_NAME_LEN);
|
---|
| 76 |
|
---|
| 77 | linked_list_remove(&db_mem_names, (linked_item_t *) item);
|
---|
| 78 | linked_list_add(&db_mem_names, (linked_item_t *) item);
|
---|
| 79 |
|
---|
| 80 | return 1;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key, link_key_type_t * link_key_type) {
|
---|
| 84 | db_mem_device_link_key_t * item = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
|
---|
| 85 |
|
---|
| 86 | if (!item) return 0;
|
---|
| 87 |
|
---|
| 88 | memcpy(link_key, item->link_key, LINK_KEY_LEN);
|
---|
| 89 | if (link_key_type) {
|
---|
| 90 | *link_key_type = item->link_key_type;
|
---|
| 91 | }
|
---|
| 92 | linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
|
---|
| 93 | linked_list_add(&db_mem_link_keys, (linked_item_t *) item);
|
---|
| 94 |
|
---|
| 95 | return 1;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | static void delete_link_key(bd_addr_t *bd_addr){
|
---|
| 99 | db_mem_device_t * item = get_item(db_mem_link_keys, bd_addr);
|
---|
| 100 |
|
---|
| 101 | if (!item) return;
|
---|
| 102 |
|
---|
| 103 | linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
|
---|
| 104 | btstack_memory_db_mem_device_link_key_free((db_mem_device_link_key_t*)item);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | static void put_link_key(bd_addr_t *bd_addr, link_key_t *link_key, link_key_type_t link_key_type){
|
---|
| 109 | db_mem_device_link_key_t * existingRecord = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
|
---|
| 110 | db_mem_device_link_key_t * newItem;
|
---|
| 111 |
|
---|
| 112 | if (existingRecord){
|
---|
| 113 | memcpy(existingRecord->link_key, link_key, LINK_KEY_LEN);
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | // Record not found, create new one for this device
|
---|
| 118 | newItem = btstack_memory_db_mem_device_link_key_get();
|
---|
| 119 | if (!newItem){
|
---|
| 120 | newItem = (db_mem_device_link_key_t*)linked_list_get_last_item(&db_mem_link_keys);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | if (!newItem) return;
|
---|
| 124 |
|
---|
| 125 | memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
|
---|
| 126 | memcpy(newItem->link_key, link_key, LINK_KEY_LEN);
|
---|
| 127 | newItem->link_key_type = link_key_type;
|
---|
| 128 | linked_list_add(&db_mem_link_keys, (linked_item_t *) newItem);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static void delete_name(bd_addr_t *bd_addr){
|
---|
| 132 | db_mem_device_t * item = get_item(db_mem_names, bd_addr);
|
---|
| 133 |
|
---|
| 134 | if (!item) return;
|
---|
| 135 |
|
---|
| 136 | linked_list_remove(&db_mem_names, (linked_item_t *) item);
|
---|
| 137 | btstack_memory_db_mem_device_name_free((db_mem_device_name_t*)item);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | static void put_name(bd_addr_t *bd_addr, device_name_t *device_name){
|
---|
| 141 | db_mem_device_name_t * existingRecord = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
|
---|
| 142 | db_mem_device_name_t * newItem;
|
---|
| 143 |
|
---|
| 144 | if (existingRecord){
|
---|
| 145 | strlcpy(existingRecord->device_name, (const char*) device_name, MAX_NAME_LEN);
|
---|
| 146 | return;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | // Record not found, create a new one for this device
|
---|
| 150 | newItem = btstack_memory_db_mem_device_name_get();
|
---|
| 151 | if (!newItem) {
|
---|
| 152 | newItem = (db_mem_device_name_t*)linked_list_get_last_item(&db_mem_names);
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | if (!newItem) return;
|
---|
| 156 |
|
---|
| 157 | memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
|
---|
| 158 | strlcpy(newItem->device_name, (const char*) device_name, MAX_NAME_LEN);
|
---|
| 159 | linked_list_add(&db_mem_names, (linked_item_t *) newItem);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 | // MARK: PERSISTENT RFCOMM CHANNEL ALLOCATION
|
---|
| 164 |
|
---|
| 165 | static uint8_t persistent_rfcomm_channel(char *serviceName){
|
---|
| 166 | linked_item_t *it;
|
---|
| 167 | db_mem_service_t * item;
|
---|
| 168 | uint8_t max_channel = 1;
|
---|
| 169 | db_mem_service_t * newItem;
|
---|
| 170 |
|
---|
| 171 | for (it = (linked_item_t *) db_mem_services; it ; it = it->next){
|
---|
| 172 | item = (db_mem_service_t *) it;
|
---|
| 173 | if (strncmp(item->service_name, serviceName, MAX_NAME_LEN) == 0) {
|
---|
| 174 | // Match found
|
---|
| 175 | return item->channel;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | // TODO prevent overflow
|
---|
| 179 | if (item->channel >= max_channel) max_channel = item->channel + 1;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | // Allocate new persistant channel
|
---|
| 183 | newItem = btstack_memory_db_mem_service_get();
|
---|
| 184 |
|
---|
| 185 | if (!newItem) return 0;
|
---|
| 186 |
|
---|
| 187 | strlcpy(newItem->service_name, serviceName, MAX_NAME_LEN);
|
---|
| 188 | newItem->channel = max_channel;
|
---|
| 189 | linked_list_add(&db_mem_services, (linked_item_t *) newItem);
|
---|
| 190 | return max_channel;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | const remote_device_db_t remote_device_db_memory = {
|
---|
| 195 | db_open,
|
---|
| 196 | db_close,
|
---|
| 197 | get_link_key,
|
---|
| 198 | put_link_key,
|
---|
| 199 | delete_link_key,
|
---|
| 200 | get_name,
|
---|
| 201 | put_name,
|
---|
| 202 | delete_name,
|
---|
| 203 | persistent_rfcomm_channel
|
---|
| 204 | };
|
---|