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