123953804SMatthias Ringwald /*
223953804SMatthias Ringwald * Copyright (C) 2017 BlueKitchen GmbH
323953804SMatthias Ringwald *
423953804SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
523953804SMatthias Ringwald * modification, are permitted provided that the following conditions
623953804SMatthias Ringwald * are met:
723953804SMatthias Ringwald *
823953804SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
923953804SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
1023953804SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
1123953804SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
1223953804SMatthias Ringwald * documentation and/or other materials provided with the distribution.
1323953804SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
1423953804SMatthias Ringwald * contributors may be used to endorse or promote products derived
1523953804SMatthias Ringwald * from this software without specific prior written permission.
1623953804SMatthias Ringwald *
1723953804SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
1823953804SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1923953804SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2023953804SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
2123953804SMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2223953804SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2323953804SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2423953804SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2523953804SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2623953804SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
2723953804SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2823953804SMatthias Ringwald * SUCH DAMAGE.
2923953804SMatthias Ringwald *
3023953804SMatthias Ringwald */
3123953804SMatthias Ringwald
3223953804SMatthias Ringwald #define BTSTACK_FILE__ "btstack_tlv_windows.c"
3323953804SMatthias Ringwald
3423953804SMatthias Ringwald #include "btstack_tlv.h"
3523953804SMatthias Ringwald #include "btstack_tlv_windows.h"
3623953804SMatthias Ringwald #include "btstack_debug.h"
3723953804SMatthias Ringwald #include "btstack_util.h"
3823953804SMatthias Ringwald #include "btstack_debug.h"
3923953804SMatthias Ringwald #include "string.h"
4023953804SMatthias Ringwald
4123953804SMatthias Ringwald #include <stdlib.h>
4223953804SMatthias Ringwald #include <string.h>
4323953804SMatthias Ringwald
4423953804SMatthias Ringwald
4523953804SMatthias Ringwald // Header:
4623953804SMatthias Ringwald // - Magic: 'BTstack'
4723953804SMatthias Ringwald // - Status:
4823953804SMatthias Ringwald // - bits 765432: reserved
4923953804SMatthias Ringwald // - bits 10: epoch
5023953804SMatthias Ringwald
5123953804SMatthias Ringwald // Entries
5223953804SMatthias Ringwald // - Tag: 32 bit
5323953804SMatthias Ringwald // - Len: 32 bit
5423953804SMatthias Ringwald // - Value: Len in bytes
5523953804SMatthias Ringwald
5623953804SMatthias Ringwald #define BTSTACK_TLV_HEADER_LEN 8
5723953804SMatthias Ringwald
5823953804SMatthias Ringwald #define MAX_TLV_VALUE_SIZE 2048
5923953804SMatthias Ringwald
6023953804SMatthias Ringwald static const char btstack_tlv_header_magic[] = "BTstack";
6123953804SMatthias Ringwald
6223953804SMatthias Ringwald #define DUMMY_SIZE 4
6323953804SMatthias Ringwald typedef struct tlv_entry {
6423953804SMatthias Ringwald void * next;
6523953804SMatthias Ringwald uint32_t tag;
6623953804SMatthias Ringwald uint32_t len;
6723953804SMatthias Ringwald uint8_t value[DUMMY_SIZE]; // dummy size
6823953804SMatthias Ringwald } tlv_entry_t;
6923953804SMatthias Ringwald
btstack_tlv_windows_append_tag(btstack_tlv_windows_t * self,uint32_t tag,const uint8_t * data,uint32_t data_size)7023953804SMatthias Ringwald static int btstack_tlv_windows_append_tag(btstack_tlv_windows_t * self, uint32_t tag, const uint8_t * data, uint32_t data_size){
7123953804SMatthias Ringwald
7223953804SMatthias Ringwald if (self->file == INVALID_HANDLE_VALUE) return 1;
7323953804SMatthias Ringwald
7423953804SMatthias Ringwald log_info("append tag %04x, len %u", tag, data_size);
7523953804SMatthias Ringwald
7623953804SMatthias Ringwald uint8_t header[8];
7723953804SMatthias Ringwald big_endian_store_32(header, 0, tag);
7823953804SMatthias Ringwald big_endian_store_32(header, 4, data_size);
7923953804SMatthias Ringwald DWORD written_header = 0;
8023953804SMatthias Ringwald (void)WriteFile(
8123953804SMatthias Ringwald self->file, // file handle
8223953804SMatthias Ringwald &header, // start of data to write
8323953804SMatthias Ringwald sizeof(header), // number of bytes to write
8423953804SMatthias Ringwald &written_header, // number of bytes that were written
8523953804SMatthias Ringwald NULL); // no overlapped structure
8623953804SMatthias Ringwald
8723953804SMatthias Ringwald if (written_header != sizeof(header)) return 1;
8823953804SMatthias Ringwald if (data_size > 0) {
8923953804SMatthias Ringwald DWORD written_value = 0;
9023953804SMatthias Ringwald (void)WriteFile(
9123953804SMatthias Ringwald self->file, // file handle
92*54374dbcSMatthias Ringwald data, // start of data to write
93*54374dbcSMatthias Ringwald data_size, // number of bytes to write
9423953804SMatthias Ringwald &written_value, // number of bytes that were written
9523953804SMatthias Ringwald NULL); // no overlapped structure
9623953804SMatthias Ringwald if (written_value != data_size) return 1;
9723953804SMatthias Ringwald }
9823953804SMatthias Ringwald return 1;
9923953804SMatthias Ringwald }
10023953804SMatthias Ringwald
btstack_tlv_windows_find_entry(btstack_tlv_windows_t * self,uint32_t tag)10123953804SMatthias Ringwald static tlv_entry_t * btstack_tlv_windows_find_entry(btstack_tlv_windows_t * self, uint32_t tag){
10223953804SMatthias Ringwald btstack_linked_list_iterator_t it;
10323953804SMatthias Ringwald btstack_linked_list_iterator_init(&it, &self->entry_list);
10423953804SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
10523953804SMatthias Ringwald tlv_entry_t * entry = (tlv_entry_t*) btstack_linked_list_iterator_next(&it);
10623953804SMatthias Ringwald if (entry->tag != tag) continue;
10723953804SMatthias Ringwald return entry;
10823953804SMatthias Ringwald }
10923953804SMatthias Ringwald return NULL;
11023953804SMatthias Ringwald }
11123953804SMatthias Ringwald
11223953804SMatthias Ringwald /**
11323953804SMatthias Ringwald * Delete Tag
11423953804SMatthias Ringwald * @param tag
11523953804SMatthias Ringwald */
btstack_tlv_windows_delete_tag(void * context,uint32_t tag)11623953804SMatthias Ringwald static void btstack_tlv_windows_delete_tag(void * context, uint32_t tag){
11723953804SMatthias Ringwald btstack_tlv_windows_t * self = (btstack_tlv_windows_t *) context;
11823953804SMatthias Ringwald btstack_linked_list_iterator_t it;
11923953804SMatthias Ringwald btstack_linked_list_iterator_init(&it, &self->entry_list);
12023953804SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
12123953804SMatthias Ringwald tlv_entry_t * entry = (tlv_entry_t*) btstack_linked_list_iterator_next(&it);
12223953804SMatthias Ringwald if (entry->tag != tag) continue;
12323953804SMatthias Ringwald btstack_linked_list_iterator_remove(&it);
12423953804SMatthias Ringwald free(entry);
12523953804SMatthias Ringwald btstack_tlv_windows_append_tag(self, tag, NULL, 0);
12623953804SMatthias Ringwald return;
12723953804SMatthias Ringwald }
12823953804SMatthias Ringwald }
12923953804SMatthias Ringwald
13023953804SMatthias Ringwald /**
13123953804SMatthias Ringwald * Get Value for Tag
13223953804SMatthias Ringwald * @param tag
13323953804SMatthias Ringwald * @param buffer
13423953804SMatthias Ringwald * @param buffer_size
13523953804SMatthias Ringwald * @returns size of value
13623953804SMatthias Ringwald */
btstack_tlv_windows_get_tag(void * context,uint32_t tag,uint8_t * buffer,uint32_t buffer_size)13723953804SMatthias Ringwald static int btstack_tlv_windows_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
13823953804SMatthias Ringwald btstack_tlv_windows_t * self = (btstack_tlv_windows_t *) context;
13923953804SMatthias Ringwald tlv_entry_t * entry = btstack_tlv_windows_find_entry(self, tag);
14023953804SMatthias Ringwald // not found
14123953804SMatthias Ringwald if (!entry) return 0;
14223953804SMatthias Ringwald // return len if buffer = NULL
14323953804SMatthias Ringwald if (!buffer) return entry->len;
14423953804SMatthias Ringwald // otherwise copy data into buffer
14523953804SMatthias Ringwald uint16_t bytes_to_copy = btstack_min(buffer_size, entry->len);
14623953804SMatthias Ringwald memcpy(buffer, &entry->value[0], bytes_to_copy);
14723953804SMatthias Ringwald return bytes_to_copy;
14823953804SMatthias Ringwald }
14923953804SMatthias Ringwald
15023953804SMatthias Ringwald /**
15123953804SMatthias Ringwald * Store Tag
15223953804SMatthias Ringwald * @param tag
15323953804SMatthias Ringwald * @param data
15423953804SMatthias Ringwald * @param data_size
15523953804SMatthias Ringwald */
btstack_tlv_windows_store_tag(void * context,uint32_t tag,const uint8_t * data,uint32_t data_size)15623953804SMatthias Ringwald static int btstack_tlv_windows_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
15723953804SMatthias Ringwald btstack_tlv_windows_t * self = (btstack_tlv_windows_t *) context;
15823953804SMatthias Ringwald
15923953804SMatthias Ringwald // enforce arbitrary max value size
16023953804SMatthias Ringwald btstack_assert(data_size <= MAX_TLV_VALUE_SIZE);
16123953804SMatthias Ringwald
16223953804SMatthias Ringwald // remove old entry
16323953804SMatthias Ringwald tlv_entry_t * old_entry = btstack_tlv_windows_find_entry(self, tag);
16423953804SMatthias Ringwald if (old_entry){
16523953804SMatthias Ringwald btstack_linked_list_remove(&self->entry_list, (btstack_linked_item_t *) old_entry);
16623953804SMatthias Ringwald free(old_entry);
16723953804SMatthias Ringwald }
16823953804SMatthias Ringwald
16923953804SMatthias Ringwald // create new entry
17023953804SMatthias Ringwald uint32_t entry_size = sizeof(tlv_entry_t) - DUMMY_SIZE + data_size;
17123953804SMatthias Ringwald tlv_entry_t * new_entry = (tlv_entry_t *) malloc(entry_size);
17223953804SMatthias Ringwald if (!new_entry) return 0;
17323953804SMatthias Ringwald memset(new_entry, 0, entry_size);
17423953804SMatthias Ringwald new_entry->tag = tag;
17523953804SMatthias Ringwald new_entry->len = data_size;
17623953804SMatthias Ringwald memcpy(&new_entry->value[0], data, data_size);
17723953804SMatthias Ringwald
17823953804SMatthias Ringwald // append new entry
17923953804SMatthias Ringwald btstack_linked_list_add(&self->entry_list, (btstack_linked_item_t *) new_entry);
18023953804SMatthias Ringwald
18123953804SMatthias Ringwald // write new tag
18223953804SMatthias Ringwald btstack_tlv_windows_append_tag(self, tag, data, data_size);
18323953804SMatthias Ringwald
18423953804SMatthias Ringwald return 0;
18523953804SMatthias Ringwald }
18623953804SMatthias Ringwald
18723953804SMatthias Ringwald // returns 0 on success
btstack_tlv_windows_read_db(btstack_tlv_windows_t * self)18823953804SMatthias Ringwald static int btstack_tlv_windows_read_db(btstack_tlv_windows_t * self){
18923953804SMatthias Ringwald // open file
19023953804SMatthias Ringwald log_info("open db %s", self->db_path);
19123953804SMatthias Ringwald self->file = CreateFile(
19223953804SMatthias Ringwald self->db_path,
19323953804SMatthias Ringwald GENERIC_READ | GENERIC_WRITE,
19423953804SMatthias Ringwald 0, // do not share
19523953804SMatthias Ringwald NULL, // default security
19623953804SMatthias Ringwald OPEN_EXISTING, // only open if it exists
19723953804SMatthias Ringwald FILE_ATTRIBUTE_NORMAL,
19823953804SMatthias Ringwald NULL);
19923953804SMatthias Ringwald
20023953804SMatthias Ringwald uint8_t header[BTSTACK_TLV_HEADER_LEN];
20123953804SMatthias Ringwald if (self->file != INVALID_HANDLE_VALUE){
20223953804SMatthias Ringwald // check header
20323953804SMatthias Ringwald bool ok = ReadFile(self->file, header, BTSTACK_TLV_HEADER_LEN, NULL, NULL);
20423953804SMatthias Ringwald int file_valid = 0;
20523953804SMatthias Ringwald if (ok){
20623953804SMatthias Ringwald if (memcmp(header, btstack_tlv_header_magic, strlen(btstack_tlv_header_magic)) == 0){
20723953804SMatthias Ringwald log_info("BTstack Magic Header found");
20823953804SMatthias Ringwald // read entries
20923953804SMatthias Ringwald while (true){
21023953804SMatthias Ringwald uint8_t entry[8];
21123953804SMatthias Ringwald DWORD bytes_read;
21223953804SMatthias Ringwald ok = ReadFile(self->file, entry, sizeof(entry), &bytes_read, NULL);
21323953804SMatthias Ringwald if ( ok && (bytes_read == 0)) {
21423953804SMatthias Ringwald // EOF, we're good
21523953804SMatthias Ringwald file_valid = 1;
21623953804SMatthias Ringwald break;
21723953804SMatthias Ringwald }
21823953804SMatthias Ringwald
21923953804SMatthias Ringwald if ( (ok == false) || (bytes_read != sizeof(entry))) {
22023953804SMatthias Ringwald break;
22123953804SMatthias Ringwald }
22223953804SMatthias Ringwald
22323953804SMatthias Ringwald uint32_t tag = big_endian_read_32(entry, 0);
22423953804SMatthias Ringwald uint32_t len = big_endian_read_32(entry, 4);
22523953804SMatthias Ringwald
22623953804SMatthias Ringwald // arbitrary safety check: values <= MAX_TLV_VALUE_SIZE
22723953804SMatthias Ringwald if (len > MAX_TLV_VALUE_SIZE) {
22823953804SMatthias Ringwald break;
22923953804SMatthias Ringwald }
23023953804SMatthias Ringwald
23123953804SMatthias Ringwald // create new entry for regular tag
23223953804SMatthias Ringwald tlv_entry_t * new_entry = NULL;
23323953804SMatthias Ringwald if (len > 0) {
23423953804SMatthias Ringwald new_entry = (tlv_entry_t *) malloc(sizeof(tlv_entry_t) - DUMMY_SIZE + len);
23523953804SMatthias Ringwald if (!new_entry) return 0;
23623953804SMatthias Ringwald new_entry->tag = tag;
23723953804SMatthias Ringwald new_entry->len = len;
23823953804SMatthias Ringwald
23923953804SMatthias Ringwald // read
24023953804SMatthias Ringwald DWORD value_read;
24123953804SMatthias Ringwald ok = ReadFile(self->file, &new_entry->value[0],len, &value_read, NULL);
24223953804SMatthias Ringwald if ((ok == false) || (value_read != len)) {
24323953804SMatthias Ringwald break;
24423953804SMatthias Ringwald }
24523953804SMatthias Ringwald }
24623953804SMatthias Ringwald
24723953804SMatthias Ringwald // remove old entry
24823953804SMatthias Ringwald tlv_entry_t * old_entry = btstack_tlv_windows_find_entry(self, tag);
24923953804SMatthias Ringwald if (old_entry){
25023953804SMatthias Ringwald btstack_linked_list_remove(&self->entry_list, (btstack_linked_item_t *) old_entry);
25123953804SMatthias Ringwald free(old_entry);
25223953804SMatthias Ringwald }
25323953804SMatthias Ringwald
25423953804SMatthias Ringwald // append new entry
25523953804SMatthias Ringwald if (new_entry){
25623953804SMatthias Ringwald btstack_linked_list_add(&self->entry_list, (btstack_linked_item_t *) new_entry);
25723953804SMatthias Ringwald }
25823953804SMatthias Ringwald }
25923953804SMatthias Ringwald }
26023953804SMatthias Ringwald }
26123953804SMatthias Ringwald if (!file_valid) {
26223953804SMatthias Ringwald log_info("file invalid, re-create");
26323953804SMatthias Ringwald CloseHandle(self->file);
26423953804SMatthias Ringwald self->file = INVALID_HANDLE_VALUE;
26523953804SMatthias Ringwald }
26623953804SMatthias Ringwald }
26723953804SMatthias Ringwald if (self->file == INVALID_HANDLE_VALUE){
26823953804SMatthias Ringwald // create new file
26923953804SMatthias Ringwald self->file = CreateFile(
27023953804SMatthias Ringwald self->db_path,
27123953804SMatthias Ringwald GENERIC_WRITE,
27223953804SMatthias Ringwald 0, // do not share
27323953804SMatthias Ringwald NULL, // default security
27423953804SMatthias Ringwald CREATE_ALWAYS, // create new file / replace old one
27523953804SMatthias Ringwald FILE_ATTRIBUTE_NORMAL,
27623953804SMatthias Ringwald NULL);
27723953804SMatthias Ringwald
27823953804SMatthias Ringwald if (self->file == INVALID_HANDLE_VALUE) {
27923953804SMatthias Ringwald log_error("failed to create file");
28023953804SMatthias Ringwald return -1;
28123953804SMatthias Ringwald }
28223953804SMatthias Ringwald memset(header, 0, sizeof(header));
28323953804SMatthias Ringwald memcpy(header, btstack_tlv_header_magic, sizeof(btstack_tlv_header_magic));
28423953804SMatthias Ringwald
28523953804SMatthias Ringwald DWORD dwBytesWritten = 0;
28623953804SMatthias Ringwald (void)WriteFile(
28723953804SMatthias Ringwald self->file, // file handle
28823953804SMatthias Ringwald &header, // start of data to write
28923953804SMatthias Ringwald sizeof(header), // number of bytes to write
29023953804SMatthias Ringwald &dwBytesWritten, // number of bytes that were written
29123953804SMatthias Ringwald NULL); // no overlapped structure
29223953804SMatthias Ringwald UNUSED(dwBytesWritten);
29323953804SMatthias Ringwald
29423953804SMatthias Ringwald // write out all valid entries (if any)
29523953804SMatthias Ringwald btstack_linked_list_iterator_t it;
29623953804SMatthias Ringwald btstack_linked_list_iterator_init(&it, &self->entry_list);
29723953804SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
29823953804SMatthias Ringwald tlv_entry_t * entry = (tlv_entry_t*) btstack_linked_list_iterator_next(&it);
29923953804SMatthias Ringwald btstack_tlv_windows_append_tag(self, entry->tag, &entry->value[0], entry->len);
30023953804SMatthias Ringwald }
30123953804SMatthias Ringwald }
30223953804SMatthias Ringwald return 0;
30323953804SMatthias Ringwald }
30423953804SMatthias Ringwald
30523953804SMatthias Ringwald static const btstack_tlv_t btstack_tlv_windows = {
30623953804SMatthias Ringwald /* int (*get_tag)(..); */ &btstack_tlv_windows_get_tag,
30723953804SMatthias Ringwald /* int (*store_tag)(..); */ &btstack_tlv_windows_store_tag,
30823953804SMatthias Ringwald /* void (*delete_tag)(v..); */ &btstack_tlv_windows_delete_tag,
30923953804SMatthias Ringwald };
31023953804SMatthias Ringwald
31123953804SMatthias Ringwald /**
31223953804SMatthias Ringwald * Init Tag Length Value Store
31323953804SMatthias Ringwald */
btstack_tlv_windows_init_instance(btstack_tlv_windows_t * self,const char * db_path)31423953804SMatthias Ringwald const btstack_tlv_t * btstack_tlv_windows_init_instance(btstack_tlv_windows_t * self, const char * db_path){
31523953804SMatthias Ringwald memset(self, 0, sizeof(btstack_tlv_windows_t));
31623953804SMatthias Ringwald self->db_path = db_path;
31723953804SMatthias Ringwald self->file = INVALID_HANDLE_VALUE;
31823953804SMatthias Ringwald
31923953804SMatthias Ringwald // read DB
32023953804SMatthias Ringwald btstack_tlv_windows_read_db(self);
32123953804SMatthias Ringwald return &btstack_tlv_windows;
32223953804SMatthias Ringwald }
32323953804SMatthias Ringwald
32423953804SMatthias Ringwald /**
32523953804SMatthias Ringwald * Free TLV entries
32623953804SMatthias Ringwald * @param self
32723953804SMatthias Ringwald */
btstack_tlv_windows_deinit(btstack_tlv_windows_t * self)32823953804SMatthias Ringwald void btstack_tlv_windows_deinit(btstack_tlv_windows_t * self){
32923953804SMatthias Ringwald // free all entries
33023953804SMatthias Ringwald btstack_linked_list_iterator_t it;
33123953804SMatthias Ringwald btstack_linked_list_iterator_init(&it, &self->entry_list);
33223953804SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
33323953804SMatthias Ringwald tlv_entry_t * entry = (tlv_entry_t*) btstack_linked_list_iterator_next(&it);
33423953804SMatthias Ringwald btstack_linked_list_iterator_remove(&it);
33523953804SMatthias Ringwald free(entry);
33623953804SMatthias Ringwald }
33723953804SMatthias Ringwald }
338