xref: /btstack/platform/posix/btstack_tlv_posix.c (revision 1b464e99afd70ddaf6b75be1ba7cc563a5f5dfd8)
1 /*
2  * Copyright (C) 2017 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  *
17  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #define BTSTACK_FILE__ "btstack_tlv_posix.c"
33 
34 #include "btstack_tlv.h"
35 #include "btstack_tlv_posix.h"
36 #include "btstack_debug.h"
37 #include "btstack_util.h"
38 #include "btstack_debug.h"
39 #include "string.h"
40 
41 #include <stdlib.h>
42 #include <string.h>
43 
44 
45 // Header:
46 // - Magic: 'BTstack'
47 // - Status:
48 //   - bits 765432: reserved
49 //	 - bits 10:     epoch
50 
51 // Entries
52 // - Tag: 32 bit
53 // - Len: 32 bit
54 // - Value: Len in bytes
55 
56 #define BTSTACK_TLV_HEADER_LEN 8
57 static const char * btstack_tlv_header_magic = "BTstack";
58 
59 #define DUMMY_SIZE 4
60 typedef struct tlv_entry {
61 	void   * next;
62 	uint32_t tag;
63 	uint32_t len;
64 	uint8_t  value[DUMMY_SIZE];	// dummy size
65 } tlv_entry_t;
66 
67 static int btstack_tlv_posix_append_tag(btstack_tlv_posix_t * self, uint32_t tag, const uint8_t * data, uint32_t data_size){
68 
69 	if (!self->file) return 1;
70 
71 	log_info("append tag %04x, len %u", tag, data_size);
72 
73 	uint8_t header[8];
74 	big_endian_store_32(header, 0, tag);
75 	big_endian_store_32(header, 4, data_size);
76 	size_t written_header = fwrite(header, 1, sizeof(header), self->file);
77 	if (written_header != sizeof(header)) return 1;
78 	if (data_size > 0) {
79 		size_t written_value = fwrite(data, 1, data_size, self->file);
80 		if (written_value != data_size) return 1;
81 	}
82 	fflush(self->file);
83 	return 1;
84 }
85 
86 static tlv_entry_t * btstack_tlv_posix_find_entry(btstack_tlv_posix_t * self, uint32_t tag){
87 	btstack_linked_list_iterator_t it;
88 	btstack_linked_list_iterator_init(&it, &self->entry_list);
89 	while (btstack_linked_list_iterator_has_next(&it)){
90 		tlv_entry_t * entry = (tlv_entry_t*) btstack_linked_list_iterator_next(&it);
91 		if (entry->tag != tag) continue;
92 		return entry;
93 	}
94 	return NULL;
95 }
96 
97 /**
98  * Delete Tag
99  * @param tag
100  */
101 static void btstack_tlv_posix_delete_tag(void * context, uint32_t tag){
102 	btstack_tlv_posix_t * self = (btstack_tlv_posix_t *) context;
103 	btstack_linked_list_iterator_t it;
104 	btstack_linked_list_iterator_init(&it, &self->entry_list);
105 	while (btstack_linked_list_iterator_has_next(&it)){
106 		tlv_entry_t * entry = (tlv_entry_t*) btstack_linked_list_iterator_next(&it);
107 		if (entry->tag != tag) continue;
108 		btstack_linked_list_iterator_remove(&it);
109 		free(entry);
110 		btstack_tlv_posix_append_tag(self, tag, NULL, 0);
111 		return;
112 	}
113 }
114 
115 /**
116  * Get Value for Tag
117  * @param tag
118  * @param buffer
119  * @param buffer_size
120  * @returns size of value
121  */
122 static int btstack_tlv_posix_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
123 	btstack_tlv_posix_t * self = (btstack_tlv_posix_t *) context;
124 	tlv_entry_t * entry = btstack_tlv_posix_find_entry(self, tag);
125 	// not found
126 	if (!entry) return 0;
127 	// return len if buffer = NULL
128 	if (!buffer) return entry->len;
129 	// otherwise copy data into buffer
130 	uint16_t bytes_to_copy = btstack_min(buffer_size, entry->len);
131 	memcpy(buffer, &entry->value[0], bytes_to_copy);
132 	return bytes_to_copy;
133 }
134 
135 /**
136  * Store Tag
137  * @param tag
138  * @param data
139  * @param data_size
140  */
141 static int btstack_tlv_posix_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
142 	btstack_tlv_posix_t * self = (btstack_tlv_posix_t *) context;
143 
144 	// remove old entry
145 	tlv_entry_t * old_entry = btstack_tlv_posix_find_entry(self, tag);
146 	if (old_entry){
147 		btstack_linked_list_remove(&self->entry_list, (btstack_linked_item_t *) old_entry);
148 		free(old_entry);
149 	}
150 
151 	// create new entry
152 	uint32_t entry_size = sizeof(tlv_entry_t) - DUMMY_SIZE + data_size;
153 	tlv_entry_t * new_entry = (tlv_entry_t *) malloc(entry_size);
154 	if (!new_entry) return 0;
155 	memset(new_entry, 0, entry_size);
156 	new_entry->tag = tag;
157 	new_entry->len = data_size;
158 	memcpy(&new_entry->value[0], data, data_size);
159 
160 	// append new entry
161 	btstack_linked_list_add(&self->entry_list, (btstack_linked_item_t *) new_entry);
162 
163 	// write new tag
164 	btstack_tlv_posix_append_tag(self, tag, data, data_size);
165 
166 	return 0;
167 }
168 
169 // returns 0 on success
170 static int btstack_tlv_posix_read_db(btstack_tlv_posix_t * self){
171 	// open file
172 	log_info("open db %s", self->db_path);
173     self->file = fopen(self->db_path,"r+");
174     uint8_t header[BTSTACK_TLV_HEADER_LEN];
175     if (self->file){
176     	// checker header
177 	    size_t objects_read = fread(header, 1, BTSTACK_TLV_HEADER_LEN, self->file );
178 	    int file_valid = 0;
179 	    if (objects_read == BTSTACK_TLV_HEADER_LEN){
180 	    	if (memcmp(header, btstack_tlv_header_magic, strlen(btstack_tlv_header_magic)) == 0){
181 		    	log_info("BTstack Magic Header found");
182 		    	// read entries
183 		    	while (true){
184 					uint8_t entry[8];
185 					size_t 	entries_read = fread(entry, 1, sizeof(entry), self->file);
186 					if (entries_read == 0){
187 						// EOF, we're good
188 						file_valid = 1;
189 						break;
190 					}
191 					if (entries_read != sizeof(entry)) break;
192 
193                     uint32_t tag = big_endian_read_32(entry, 0);
194                     uint32_t len = big_endian_read_32(entry, 4);
195 
196                     // arbitrary safety check: values < 1000 bytes each
197                     if (len > 1000) break;
198 
199                     // create new entry for regular tag
200                     tlv_entry_t * new_entry = NULL;
201                     if (len > 0) {
202                         new_entry = (tlv_entry_t *) malloc(sizeof(tlv_entry_t) - DUMMY_SIZE + len);
203                         if (!new_entry) return 0;
204                         new_entry->tag = tag;
205                         new_entry->len = len;
206 
207                         // read
208                         size_t value_read = fread(&new_entry->value[0], 1, len, self->file);
209                         if (value_read != len) break;
210                     }
211 
212                     // remove old entry
213                     tlv_entry_t * old_entry = btstack_tlv_posix_find_entry(self, tag);
214                     if (old_entry){
215                         btstack_linked_list_remove(&self->entry_list, (btstack_linked_item_t *) old_entry);
216                         free(old_entry);
217                     }
218 
219                     // append new entry
220                     if (new_entry){
221 	                    btstack_linked_list_add(&self->entry_list, (btstack_linked_item_t *) new_entry);
222                     }
223 		    	}
224 	    	}
225 	    }
226 	    if (!file_valid) {
227 	    	log_info("file invalid, re-create");
228     		fclose(self->file);
229     		self->file = NULL;
230 	    }
231     }
232     if (!self->file){
233     	// create truncate file
234 	    self->file = fopen(self->db_path,"w+");
235 	    memset(header, 0, sizeof(header));
236 	    strcpy((char *)header, btstack_tlv_header_magic);
237 	    fwrite(header, 1, sizeof(header), self->file);
238 	    // write out all valid entries (if any)
239 		btstack_linked_list_iterator_t it;
240 		btstack_linked_list_iterator_init(&it, &self->entry_list);
241 		while (btstack_linked_list_iterator_has_next(&it)){
242 			tlv_entry_t * entry = (tlv_entry_t*) btstack_linked_list_iterator_next(&it);
243 			btstack_tlv_posix_append_tag(self, entry->tag, &entry->value[0], entry->len);
244 		}
245     }
246 	return 0;
247 }
248 
249 static const btstack_tlv_t btstack_tlv_posix = {
250 	/* int  (*get_tag)(..);     */ &btstack_tlv_posix_get_tag,
251 	/* int (*store_tag)(..);    */ &btstack_tlv_posix_store_tag,
252 	/* void (*delete_tag)(v..); */ &btstack_tlv_posix_delete_tag,
253 };
254 
255 /**
256  * Init Tag Length Value Store
257  */
258 const btstack_tlv_t * btstack_tlv_posix_init_instance(btstack_tlv_posix_t * self, const char * db_path){
259 	memset(self, 0, sizeof(btstack_tlv_posix_t));
260 	self->db_path = db_path;
261 
262 	// read DB
263 	btstack_tlv_posix_read_db(self);
264 	return &btstack_tlv_posix;
265 }
266 
267