mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-04-17 02:42:33 +00:00
esp32: implement btstack_tlv.h interface on top of esp-idf nvs storage
This commit is contained in:
parent
01dc6e35bf
commit
6bbd991acf
160
port/esp32/template/components/btstack/btstack_tlv_esp32.c
Normal file
160
port/esp32/template/components/btstack/btstack_tlv_esp32.c
Normal file
@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (C) 2017 BlueKitchen GmbH
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#define __BTSTACK_FILE__ "btstack_tlv_esp32.c"
|
||||
|
||||
#include "btstack_tlv.h"
|
||||
#include "btstack_util.h"
|
||||
#include "btstack_debug.h"
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static nvs_handle the_nvs_handle;
|
||||
static int nvs_active;
|
||||
|
||||
// @param buffer char array of size 9
|
||||
static void key_for_tag(uint32_t tag, char * key_buffer){
|
||||
int i;
|
||||
for (i=0;i<8;i++){
|
||||
key_buffer[i] = char_for_nibble( (tag >> (4*(7-i))) & 0x0f);
|
||||
}
|
||||
key_buffer[i] = 0;
|
||||
log_info("tag %08x -> %s", tag, key_buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Value for Tag
|
||||
* @param tag
|
||||
* @param buffer
|
||||
* @param buffer_size
|
||||
* @returns size of value
|
||||
*/
|
||||
static int btstack_tlv_esp32_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
|
||||
if (!nvs_active) return 0;
|
||||
char key_buffer[9];
|
||||
key_for_tag(tag, key_buffer);
|
||||
log_info("read tag %s", key_buffer);
|
||||
size_t size = 0;
|
||||
esp_err_t err = nvs_get_blob(the_nvs_handle, key_buffer, NULL, &size);
|
||||
switch (err) {
|
||||
case ESP_OK:
|
||||
if (size > buffer_size){
|
||||
log_error("buffer_size %u < value size %u", buffer_size, size);
|
||||
return 0;
|
||||
}
|
||||
nvs_get_blob(the_nvs_handle, key_buffer, buffer, &size);
|
||||
return size;
|
||||
case ESP_ERR_NVS_NOT_FOUND:
|
||||
break;
|
||||
default :
|
||||
log_error("Error (0x%04x) reading %s!\n", err, key_buffer);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store Tag
|
||||
* @param tag
|
||||
* @param data
|
||||
* @param data_size
|
||||
*/
|
||||
static void btstack_tlv_esp32_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
|
||||
if (!nvs_active) return;
|
||||
char key_buffer[9];
|
||||
key_for_tag(tag, key_buffer);
|
||||
log_info("store tag %s", key_buffer);
|
||||
esp_err_t err = nvs_set_blob(the_nvs_handle, key_buffer, data, data_size);
|
||||
if (err != ESP_OK){
|
||||
printf("Error (0x%04x) nvs_set_blob %s!\n", err, key_buffer);
|
||||
return;
|
||||
}
|
||||
err = nvs_commit(the_nvs_handle);
|
||||
if (err != ESP_OK){
|
||||
printf("Error (0x%04x) nvs_commit %s!\n", err, key_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Tag
|
||||
* @param tag
|
||||
*/
|
||||
static void btstack_tlv_esp32_delete_tag(void * context, uint32_t tag){
|
||||
if (!nvs_active) return;
|
||||
char key_buffer[9];
|
||||
key_for_tag(tag, key_buffer);
|
||||
log_info("delete tag %s", key_buffer);
|
||||
esp_err_t err = nvs_erase_key(the_nvs_handle, key_buffer);
|
||||
switch (err) {
|
||||
case ESP_OK:
|
||||
case ESP_ERR_NVS_NOT_FOUND:
|
||||
break;
|
||||
break;
|
||||
default :
|
||||
log_error("Error (0x%04x) deleting reading %s!\n", err, key_buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static const btstack_tlv_t btstack_tlv_esp32 = {
|
||||
/* int (*get_tag)(..); */ &btstack_tlv_esp32_get_tag,
|
||||
/* void (*store_tag)(..); */ &btstack_tlv_esp32_store_tag,
|
||||
/* void (*delete_tag)(v..); */ &btstack_tlv_esp32_delete_tag,
|
||||
};
|
||||
|
||||
/**
|
||||
* Init Tag Length Value Store
|
||||
*/
|
||||
const btstack_tlv_t * btstack_tlv_esp32_get_instance(void){
|
||||
// Initialize NVS
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
|
||||
log_info("Error (0x%04x) init flash", err);
|
||||
// NVS partition was truncated and needs to be erased
|
||||
// Retry nvs_flash_init
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK( err );
|
||||
err = nvs_open("BTstack", NVS_READWRITE, &the_nvs_handle);
|
||||
if (err == ESP_OK) {
|
||||
nvs_active = 1;
|
||||
} else {
|
||||
log_info("Error (0x%04x) open flash 'BTstack' section", err);
|
||||
nvs_active = 0;
|
||||
}
|
||||
return &btstack_tlv_esp32;
|
||||
}
|
||||
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2017 BlueKitchen GmbH
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* btstack_tlv_esp32.h
|
||||
*
|
||||
* Implementation of BTstack's Tag Value Length Persistent Storage using similar esp-idf
|
||||
*/
|
||||
|
||||
#ifndef __BTSTACK_TLV_ESP32_H
|
||||
#define __BTSTACK_TLV_ESP32_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "btstack_tlv.h"
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Init Tag Length Value Store
|
||||
*/
|
||||
const btstack_tlv_t * btstack_tlv_esp32_get_instance(void);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // __BTSTACK_TLV_ESP32_H
|
@ -48,7 +48,10 @@
|
||||
#include "btstack_run_loop.h"
|
||||
#include "btstack_run_loop_freertos.h"
|
||||
#include "btstack_ring_buffer.h"
|
||||
//#include "classic/btstack_link_key_db.h"
|
||||
#include "btstack_tlv.h"
|
||||
#include "btstack_tlv_esp32.h"
|
||||
#include "classic/btstack_link_key_db.h"
|
||||
#include "classic/btstack_link_key_db_tlv.h"
|
||||
#include "hci.h"
|
||||
#include "hci_dump.h"
|
||||
#include "bt.h"
|
||||
@ -315,7 +318,11 @@ int app_main(void){
|
||||
|
||||
// init HCI
|
||||
hci_init(transport_get_instance(), NULL);
|
||||
hci_set_link_key_db(btstack_link_key_db_memory_instance()); // @TODO
|
||||
|
||||
// setup link key deb using esp32 btstack_tlv instance
|
||||
const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_esp32_get_instance();
|
||||
const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, NULL);
|
||||
hci_set_link_key_db(btstack_link_key_db);
|
||||
|
||||
// inform about BTstack state
|
||||
hci_event_callback_registration.callback = &packet_handler;
|
||||
|
Loading…
x
Reference in New Issue
Block a user