Merge branch 'develop' of https://github.com/bluekitchen/btstack into develop

This commit is contained in:
Milanka Ringwald 2017-06-07 15:09:37 +02:00
commit 25107fb724
6 changed files with 133 additions and 28 deletions

View File

@ -32,6 +32,7 @@
#include "btstack_tlv_flash_sector.h"
#include "btstack_debug.h"
#include "btstack_util.h"
#include "btstack_debug.h"
#include <string.h>
@ -96,7 +97,7 @@ static int btstack_tlv_flash_sector_get_latest_bank(btstack_tlv_flash_sector_t *
uint8_t header1[BTSTACK_TLV_HEADER_LEN];
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
int valid0 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
if (!valid0 && !valid1) return -1;
if ( valid0 && !valid1) return 0;

View File

@ -3,7 +3,11 @@
#include "btstack_debug.h"
#include "btstack_chipset_cc256x.h"
#include "btstack_run_loop_embedded.h"
#include "btstack_link_key_db_fixed.h"
#include "classic/btstack_link_key_db_fixed.h"
#include "classic/btstack_link_key_db_tlv.h"
#include "hal_flash_sector.h"
#include "btstack_tlv.h"
#include "btstack_tlv_flash_sector.h"
#include "stm32f4xx_hal.h"
#define __BTSTACK_FILE__ "port.c"
@ -244,6 +248,7 @@ int _fstat(int file){
return -1;
}
// main.c
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(size);
@ -277,6 +282,85 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
}
}
// hal_flash_sector.h
typedef struct {
uint32_t sector_size;
uint32_t sectors[2];
uintptr_t banks[2];
} hal_flash_sector_stm32_t;
static uint32_t hal_flash_sector_stm32_get_size(void * context){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
return self->sector_size;
}
static void hal_flash_sector_stm32_erase(void * context, int bank){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
if (bank > 1) return;
FLASH_EraseInitTypeDef eraseInit;
eraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
eraseInit.Sector = self->sectors[bank];
eraseInit.NbSectors = 1;
eraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_1; // safe value
uint32_t sectorError;
HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&eraseInit, &sectorError);
HAL_FLASH_Lock();
}
static void hal_flash_sector_stm32_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
if (bank > 1) return;
if (offset > self->sector_size) return;
if ((offset + size) > self->sector_size) return;
memcpy(buffer, ((uint8_t *) self->banks[bank]) + offset, size);
}
static void hal_flash_sector_stm32_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
if (bank > 1) return;
if (offset > self->sector_size) return;
if ((offset + size) > self->sector_size) return;
unsigned int i;
HAL_FLASH_Unlock();
for (i=0;i<size;i++){
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, self->banks[bank] + offset +i, data[i]);
}
HAL_FLASH_Lock();
}
static const hal_flash_sector_t hal_flash_sector_stm32_impl = {
/* uint32_t (*get_size)() */ &hal_flash_sector_stm32_get_size,
/* void (*erase)(int); */ &hal_flash_sector_stm32_erase,
/* void (*read)(..); */ &hal_flash_sector_stm32_read,
/* void (*write)(..); */ &hal_flash_sector_stm32_write,
};
static const hal_flash_sector_t * hal_flash_sector_stm32_init_instance(hal_flash_sector_stm32_t * context, uint32_t sector_size,
uint32_t bank_0_sector, uint32_t bank_1_sector, uintptr_t bank_0_addr, uintptr_t bank_1_addr){
context->sector_size = sector_size;
context->sectors[0] = bank_0_sector;
context->sectors[1] = bank_1_sector;
context->banks[0] = bank_0_addr;
context->banks[1] = bank_1_addr;
return &hal_flash_sector_stm32_impl;
}
static btstack_tlv_flash_sector_t btstack_tlv_flash_sector_context;
static hal_flash_sector_stm32_t hal_flash_sector_context;
#define HAL_FLASH_SECTOR_SIZE (128 * 1024)
#define HAL_FLASH_SECTOR_BANK_0_ADDR 0x080C0000
#define HAL_FLASH_SECTOR_BANK_1_ADDR 0x080E0000
#define HAL_FLASH_SECTOR_BANK_0_SECTOR FLASH_SECTOR_10
#define HAL_FLASH_SECTOR_BANK_1_SECTOR FLASH_SECTOR_11
//
int btstack_main(int argc, char ** argv);
void port_main(void){
@ -284,13 +368,27 @@ void port_main(void){
btstack_memory_init();
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
// hci_dump_open( NULL, HCI_DUMP_STDOUT );
hci_dump_open( NULL, HCI_DUMP_STDOUT );
// init HCI
hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config);
hci_set_link_key_db(btstack_link_key_db_fixed_instance());
hci_set_chipset(btstack_chipset_cc256x_instance());
// setup Link Key DB
const hal_flash_sector_t * hal_flash_sector_impl = hal_flash_sector_stm32_init_instance(
&hal_flash_sector_context,
HAL_FLASH_SECTOR_SIZE,
HAL_FLASH_SECTOR_BANK_0_SECTOR,
HAL_FLASH_SECTOR_BANK_1_SECTOR,
HAL_FLASH_SECTOR_BANK_0_ADDR,
HAL_FLASH_SECTOR_BANK_1_ADDR);
const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(
&btstack_tlv_flash_sector_context,
hal_flash_sector_impl,
&hal_flash_sector_context);
const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_sector_context);
hci_set_link_key_db(btstack_link_key_db);
// inform about BTstack state
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);

View File

@ -131,10 +131,10 @@ typedef enum {
*/
//
// Error Codes
// Error Codes rfom Bluetooth Core Specification
//
// from Bluetooth Core Specification
/* ENUM_START: BLUETOOTH_ERROR_CODE */
#define ERROR_CODE_SUCCESS 0x00
#define ERROR_CODE_UNKNOWN_HCI_COMMAND 0x01
#define ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER 0x02
@ -200,6 +200,7 @@ typedef enum {
#define ERROR_CODE_CONNECTION_FAILED_TO_BE_ESTABLISHED 0x3E
#define ERROR_CODE_MAC_CONNECTION_FAILED 0x3F
#define ERROR_CODE_COARSE_CLOCK_ADJUSTMENT_REJECTED_BUT_WILL_TRY_TO_ADJUST_USING_CLOCK_DRAGGING 0x40
/* ENUM_END */
// HCI roles
#define HCI_ROLE_MASTER 0

View File

@ -116,8 +116,9 @@ typedef uint8_t sm_key_t[16];
// ERRORS
// last error code in 2.1 is 0x38 - we start with 0x50 for BTstack errors
/* ENUM_START: BTSTACK_ERROR_CODE */
#define BTSTACK_CONNECTION_TO_BTDAEMON_FAILED 0x50
#define BTSTACK_ACTIVATION_FAILED_SYSTEM_BLUETOOTH 0x51
#define BTSTACK_ACTIVATION_POWERON_FAILED 0x52
@ -156,7 +157,7 @@ typedef uint8_t sm_key_t[16];
#define SDP_HANDLE_INVALID 0x83
#define SDP_QUERY_BUSY 0x84
#define ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS 0x90
#define ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS 0x90
#define ATT_HANDLE_VALUE_INDICATION_TIMEOUT 0x91
#define GATT_CLIENT_NOT_CONNECTED 0x93
@ -171,6 +172,13 @@ typedef uint8_t sm_key_t[16];
#define BNEP_CHANNEL_NOT_CONNECTED 0xA1
#define BNEP_DATA_LEN_EXCEEDS_MTU 0xA2
// OBEX ERRORS
#define OBEX_UNKNOWN_ERROR 0xB0
#define OBEX_CONNECT_FAILED 0xB1
#define OBEX_DISCONNECTED 0xB2
#define OBEX_NOT_FOUND 0xB3
/* ENUM_END */
// DAEMON COMMANDS
#define OGF_BTSTACK 0x3d
@ -276,11 +284,6 @@ typedef uint8_t sm_key_t[16];
#define GATT_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION 0X81
#define GATT_GET_MTU 0x82
// OBEX ERRORS
#define OBEX_UNKNOWN_ERROR 0x90
#define OBEX_CONNECT_FAILED 0x91
#define OBEX_DISCONNECTED 0x92
#define OBEX_NOT_FOUND 0x93
// EVENTS
@ -938,7 +941,7 @@ typedef uint8_t sm_key_t[16];
* @param page_scan_repetition_mode
* @param class_of_device
* @param clock_offset
* @param rssi_availabe
* @param rssi_available
* @param rssi
* @param name_available
* @param name_len

View File

@ -350,27 +350,27 @@ static inline uint16_t hci_event_authentication_complete_event_get_connection_ha
* @return status
* @note: btstack_type 1
*/
// static inline uint8_t hci_event_remote_name_request_complete_get_status(const uint8_t * event){
// not implemented yet
// }
static inline uint8_t hci_event_remote_name_request_complete_get_status(const uint8_t * event){
return event[2];
}
/**
* @brief Get field bd_addr from event HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
* @param event packet
* @return bd_addr
* @param Pointer to storage for bd_addr
* @note: btstack_type B
*/
// static inline bd_addr_t hci_event_remote_name_request_complete_get_bd_addr(const uint8_t * event){
// not implemented yet
// }
static inline void hci_event_remote_name_request_complete_get_bd_addr(const uint8_t * event, bd_addr_t bd_addr){
reverse_bd_addr(&event[3], bd_addr);
}
/**
* @brief Get field remote_name from event HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
* @param event packet
* @return remote_name
* @note: btstack_type N
*/
// static inline String hci_event_remote_name_request_complete_get_remote_name(const uint8_t * event){
// not implemented yet
// }
static inline const char * hci_event_remote_name_request_complete_get_remote_name(const uint8_t * event){
return (const char *) &event[9];
}
/**
* @brief Get field status from event HCI_EVENT_ENCRYPTION_CHANGE
@ -2981,12 +2981,12 @@ static inline uint16_t gap_event_inquiry_result_get_clock_offset(const uint8_t *
return little_endian_read_16(event, 12);
}
/**
* @brief Get field rssi_availabe from event GAP_EVENT_INQUIRY_RESULT
* @brief Get field rssi_available from event GAP_EVENT_INQUIRY_RESULT
* @param event packet
* @return rssi_availabe
* @return rssi_available
* @note: btstack_type 1
*/
static inline uint8_t gap_event_inquiry_result_get_rssi_availabe(const uint8_t * event){
static inline uint8_t gap_event_inquiry_result_get_rssi_available(const uint8_t * event){
return event[14];
}
/**

View File

@ -185,17 +185,19 @@ param_read = {
'H' : 'return little_endian_read_16(event, {offset});',
'B' : 'reverse_bd_addr(&event[{offset}], {result_name});',
'R' : 'return &event[{offset}];',
'N' : 'return (const char *) &event[{offset}];',
'T' : 'return (const char *) &event[{offset}];',
'Q' : 'reverse_bytes(&event[{offset}], {result_name}, 32);',
'V' : 'return &event[{offset}];',
'X' : 'gatt_client_deserialize_service(event, {offset}, {result_name});',
'Y' : 'gatt_client_deserialize_characteristic(event, {offset}, {result_name});',
'Z' : 'gatt_client_deserialize_characteristic_descriptor(event, {offset}, {result_name});',
'V' : 'return &event[{offset}];',
}
def c_type_for_btstack_type(type):
param_types = { '1' : 'uint8_t', '2' : 'uint16_t', '3' : 'uint32_t', '4' : 'uint32_t', 'H' : 'hci_con_handle_t', 'B' : 'bd_addr_t',
'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'String' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *',
'D' : 'const uint8_t *', 'E' : 'const uint8_t * ', 'N' : 'const char *' , 'P' : 'const uint8_t *', 'A' : 'const uint8_t *',
'R' : 'const uint8_t *', 'S' : 'const uint8_t *',
'J' : 'int', 'L' : 'int', 'V' : 'const uint8_t *', 'U' : 'BT_UUID',
'Q' : 'uint8_t *',