forking from 53:1e5c300cec7f

This commit is contained in:
Rohit Grover 2014-07-30 11:36:32 +01:00
commit d1a8667c1d
116 changed files with 22277 additions and 0 deletions

230
btle/btle.cpp Executable file
View file

@ -0,0 +1,230 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/common.h"
#include "nordic_common.h"
#include "app_timer.h"
#include "btle.h"
#include "ble_stack_handler_types.h"
#include "ble_radio_notification.h"
#include "ble_flash.h"
#if NEED_BOND_MANAGER
#include "ble_bondmngr.h"
#endif
#include "ble_conn_params.h"
#include "btle_gap.h"
#include "btle_advertising.h"
#include "custom/custom_helper.h"
#include "softdevice_handler.h"
#include "pstorage.h"
#include "GapEvents.h"
#include "nRF51Gap.h"
#include "nRF51GattServer.h"
#if NEED_BOND_MANAGER /* disabled by default */
static void service_error_callback(uint32_t nrf_error);
#endif
extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
void app_error_handler(uint32_t error_code,
uint32_t line_num,
const uint8_t *p_file_name);
#if NEED_BOND_MANAGER /* disabled by default */
static error_t bond_manager_init(void);
#endif
static void btle_handler(ble_evt_t *p_ble_evt);
static void sys_evt_dispatch(uint32_t sys_evt)
{
#if NEED_PSTORAGE /* disabled by default */
pstorage_sys_event_handler(sys_evt);
#endif
}
error_t btle_init(void)
{
APP_TIMER_INIT(0, 8, 5, false);
SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
#if NEED_BOND_MANAGER /* disabled by default */
bond_manager_init();
#endif
btle_gap_init();
return ERROR_NONE;
}
static void btle_handler(ble_evt_t *p_ble_evt)
{
/* Library service handlers */
#if NEED_BOND_MANAGER /* disabled by default */
ble_bondmngr_on_ble_evt(p_ble_evt);
#endif
ble_conn_params_on_ble_evt(p_ble_evt);
/* Custom event handler */
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED: {
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
nRF51Gap::getInstance().setConnectionHandle(handle);
nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_CONNECTED, handle);
break;
}
case BLE_GAP_EVT_DISCONNECTED: {
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
// Since we are not in a connection and have not started advertising,
// store bonds
nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
#if NEED_BOND_MANAGER /* disabled by default */
ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
#endif
nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_DISCONNECTED, handle);
break;
}
case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
ble_gap_sec_params_t sec_params = {0};
sec_params.timeout = 30; /*< Timeout for Pairing Request or
* Security Request (in seconds). */
sec_params.bond = 1; /**< Perform bonding. */
sec_params.mitm = CFG_BLE_SEC_PARAM_MITM;
sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
sec_params.oob = CFG_BLE_SEC_PARAM_OOB;
sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
ASSERT_STATUS_RET_VOID(
sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().
getConnectionHandle(),
BLE_GAP_SEC_STATUS_SUCCESS,
&sec_params));
}
break;
case BLE_GAP_EVT_TIMEOUT:
if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
}
break;
case BLE_GATTC_EVT_TIMEOUT:
case BLE_GATTS_EVT_TIMEOUT:
// Disconnect on GATT Server and Client timeout events.
// ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
// BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
break;
default:
break;
}
nRF51GattServer::getInstance().hwCallback(p_ble_evt);
}
#if NEED_BOND_MANAGER /* disabled by default */
/**************************************************************************/
/*!
@brief Initialises the bond manager
@note Bond data will be cleared on reset if the bond delete
button is pressed during initialisation (the button is
defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
@returns
*/
/**************************************************************************/
static error_t bond_manager_init(void)
{
ble_bondmngr_init_t bond_para = {0};
ASSERT_STATUS ( pstorage_init());
bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND;
bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR;
//bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
bond_para.evt_handler = NULL;
bond_para.error_handler = service_error_callback;
ASSERT_STATUS( ble_bondmngr_init( &bond_para ));
/* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
// ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
// NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
// ble_flash_on_radio_active_evt) );
return ERROR_NONE;
}
#endif // #if NEED_BOND_MANAGER
#if NEED_BOND_MANAGER /* disabled by default */
/**************************************************************************/
/*!
@brief
@param[in] nrf_error
@returns
*/
/**************************************************************************/
static void service_error_callback(uint32_t nrf_error)
{
ASSERT_STATUS_RET_VOID( nrf_error );
}
#endif // #if NEED_BOND_MANAGER
/**************************************************************************/
/*!
@brief Callback when an error occurs inside the SoftDevice
@param[in] line_num
@param[in] p-file_name
@returns
*/
/**************************************************************************/
void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
{
ASSERT(false, (void) 0);
}
/**************************************************************************/
/*!
@brief Handler for general errors above the SoftDevice layer.
Typically we can' recover from this so we do a reset.
@param[in] error_code
@param[in] line_num
@param[in] p-file_name
@returns
*/
/**************************************************************************/
void app_error_handler(uint32_t error_code,
uint32_t line_num,
const uint8_t *p_file_name)
{
ASSERT_STATUS_RET_VOID( error_code );
NVIC_SystemReset();
}

35
btle/btle.h Normal file
View file

@ -0,0 +1,35 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BTLE_H_
#define _BTLE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "common/common.h"
#include "ble_srv_common.h"
#include "ble.h"
error_t btle_init(void);
#ifdef __cplusplus
}
#endif
#endif // ifndef _BTLE_H_

46
btle/btle_advertising.cpp Normal file
View file

@ -0,0 +1,46 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/common.h"
#include "ble_advdata.h"
#include "btle.h"
/**************************************************************************/
/*!
@brief Starts the advertising process
@returns
*/
/**************************************************************************/
error_t btle_advertising_start(void)
{
ble_gap_adv_params_t adv_para = {0};
/* Set the default advertising parameters */
adv_para.type = BLE_GAP_ADV_TYPE_ADV_IND;
adv_para.p_peer_addr = NULL; /* Undirected advertising */
adv_para.fp = BLE_GAP_ADV_FP_ANY;
adv_para.p_whitelist = NULL;
adv_para.interval = (CFG_GAP_ADV_INTERVAL_MS * 8) / 5; /* Advertising
* interval in
* units of 0.625
* ms */
adv_para.timeout = CFG_GAP_ADV_TIMEOUT_S;
ASSERT_STATUS( sd_ble_gap_adv_start(&adv_para));
return ERROR_NONE;
}

24
btle/btle_advertising.h Normal file
View file

@ -0,0 +1,24 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BTLE_ADVERTISING_H_
#define _BTLE_ADVERTISING_H_
#include "common/common.h"
error_t btle_advertising_start(void);
#endif // ifndef _BTLE_ADVERTISING_H_

90
btle/btle_gap.cpp Normal file
View file

@ -0,0 +1,90 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/common.h"
#include "app_timer.h"
#include "ble_gap.h"
#include "ble_conn_params.h"
static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) ATTR_ALWAYS_INLINE ATTR_CONST;
static void error_callback(uint32_t nrf_error);
/**************************************************************************/
/*!
@brief Initialise GAP in the underlying SoftDevice
@returns
*/
/**************************************************************************/
error_t btle_gap_init(void)
{
ble_gap_conn_params_t gap_conn_params = {0};
gap_conn_params.min_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MIN_INTERVAL_MS); // in 1.25ms units
gap_conn_params.max_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MAX_INTERVAL_MS); // in 1.25ms unit
gap_conn_params.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY;
gap_conn_params.conn_sup_timeout = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit
ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) CFG_GAP_LOCAL_NAME, strlen(CFG_GAP_LOCAL_NAME)));
ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE));
ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params));
ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL));
/* Connection Parameters */
enum {
FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
NEXT_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
MAX_UPDATE_COUNT = 3
};
ble_conn_params_init_t cp_init = {0};
cp_init.p_conn_params = NULL;
cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY;
cp_init.next_conn_params_update_delay = NEXT_UPDATE_DELAY;
cp_init.max_conn_params_update_count = MAX_UPDATE_COUNT;
cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
cp_init.disconnect_on_fail = true;
cp_init.evt_handler = NULL;
cp_init.error_handler = error_callback;
ASSERT_STATUS ( ble_conn_params_init(&cp_init));
return ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Converts msecs to an integer representing 1.25ms units
@param[in] ms
The number of milliseconds to conver to 1.25ms units
@returns The number of 1.25ms units in the supplied number of ms
*/
/**************************************************************************/
static inline uint32_t msec_to_1_25msec(uint32_t interval_ms)
{
return (interval_ms * 4) / 5;
}
static void error_callback(uint32_t nrf_error)
{
ASSERT_STATUS_RET_VOID( nrf_error );
}

24
btle/btle_gap.h Normal file
View file

@ -0,0 +1,24 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BTLE_GAP_H_
#define _BTLE_GAP_H_
#include "common/common.h"
error_t btle_gap_init(void);
#endif // ifndef _BTLE_GAP_H_

View file

@ -0,0 +1,247 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "custom_helper.h"
/*
* The current version of the soft-device doesn't handle duplicate 128-bit UUIDs
* very well. It is therefore necessary to filter away duplicates before
* passing long UUIDs to sd_ble_uuid_vs_add(). The following types and data
* structures involved in maintaining a local cache of 128-bit UUIDs.
*/
typedef struct {
LongUUID_t uuid;
uint8_t type;
} converted_uuid_table_entry_t;
static const unsigned UUID_TABLE_MAX_ENTRIES = 8; /* This is the maximum number
* of 128-bit UUIDs with distinct bases that
* we expect to be in use; increase this
* limit if needed. */
static unsigned uuidTableEntries = 0; /* current usage of the table */
converted_uuid_table_entry_t convertedUUIDTable[UUID_TABLE_MAX_ENTRIES];
/**
* lookup the cache of previously converted 128-bit UUIDs to find a type value.
* @param uuid long UUID
* @param recoveredType the type field of the 3-byte nRF's uuid.
* @return true if a match is found.
*/
static bool
lookupConvertedUUIDTable(const LongUUID_t uuid, uint8_t *recoveredType)
{
unsigned i;
for (i = 0; i < uuidTableEntries; i++) {
if (memcmp(convertedUUIDTable[i].uuid,
uuid,
LENGTH_OF_LONG_UUID) == 0) {
*recoveredType = convertedUUIDTable[i].type;
return true;
}
}
return false;
}
static void
addToConvertedUUIDTable(const LongUUID_t uuid, uint8_t type)
{
if (uuidTableEntries == UUID_TABLE_MAX_ENTRIES) {
return; /* recovery needed; or at least the user should be
* warned about this fact.*/
}
memcpy(convertedUUIDTable[uuidTableEntries].uuid, uuid,LENGTH_OF_LONG_UUID);
convertedUUIDTable[uuidTableEntries].type = type;
uuidTableEntries++;
}
/**
* The nRF transport has its own 3-byte representation of a UUID. If the user-
* specified UUID is 128-bits wide, then the UUID base needs to be added to the
* soft-device and converted to a 3-byte handle before being used further. This
* function is responsible for this translation of user-specified UUIDs into
* nRF's representation.
*
* @param[in] uuid
* user-specified UUID
* @return nRF
* 3-byte UUID (containing a type and 16-bit UUID) representation
* to be used with SVC calls.
*/
ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid)
{
ble_uuid_t nordicUUID = {
.uuid = uuid.getShortUUID(),
.type = BLE_UUID_TYPE_UNKNOWN /* to be set below */
};
if (uuid.shortOrLong() == UUID::UUID_TYPE_SHORT) {
nordicUUID.type = BLE_UUID_TYPE_BLE;
} else {
if (!lookupConvertedUUIDTable(uuid.getBaseUUID(), &nordicUUID.type)) {
nordicUUID.type = custom_add_uuid_base(uuid.getBaseUUID());
addToConvertedUUIDTable(uuid.getBaseUUID(), nordicUUID.type);
}
}
return nordicUUID;
}
/**************************************************************************/
/*!
@brief Adds the base UUID to the custom service. All UUIDs used
by this service are based on this 128-bit UUID.
@note This UUID needs to be added to the SoftDevice stack before
adding the service's primary service via
'sd_ble_gatts_service_add'
@param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16)
@returns The UUID type.
A return value of 0 should be considered an error.
@retval 0x00 BLE_UUID_TYPE_UNKNOWN
@retval 0x01 BLE_UUID_TYPE_BLE
@retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN
@section EXAMPLE
@code
// Take note that bytes 2/3 are blank since these are used to identify
// the primary service and individual characteristics
#define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E"
uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE);
ASSERT(uuid_type > 0, ERROR_NOT_FOUND);
// We can now safely add the primary service and any characteristics
// for our custom service ...
@endcode
*/
/**************************************************************************/
uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base)
{
ble_uuid128_t base_uuid;
uint8_t uuid_type = 0;
/* Reverse the bytes since ble_uuid128_t is LSB */
for (uint8_t i = 0; i<16; i++) {
base_uuid.uuid128[i] = p_uuid_base[15 - i];
}
ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0);
return uuid_type;
}
/**************************************************************************/
/*!
*/
/**************************************************************************/
error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base,
ble_uuid_t *p_uuid)
{
LongUUID_t uuid_base_le;
/* Reverse the bytes since ble_uuid128_t is LSB */
for (uint8_t i = 0; i<16; i++) {
uuid_base_le[i] = p_uuid_base[15 - i];
}
ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid));
return ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Adds a new characteristic to the custom service, assigning
properties, a UUID add-on value, etc.
@param[in] service_handle
@param[in] p_uuid The 16-bit value to add to the base UUID
for this characteristic (normally >1
since 1 is typically used by the primary
service).
@param[in] char_props The characteristic properties, as
defined by ble_gatt_char_props_t
@param[in] max_length The maximum length of this characeristic
@param[in] p_char_handle
@returns
@retval ERROR_NONE Everything executed normally
*/
/**************************************************************************/
error_t custom_add_in_characteristic(uint16_t service_handle,
ble_uuid_t *p_uuid,
uint8_t properties,
uint8_t *p_data,
uint16_t min_length,
uint16_t max_length,
ble_gatts_char_handles_t *p_char_handle)
{
/* Characteristic metadata */
ble_gatts_attr_md_t cccd_md;
ble_gatt_char_props_t char_props;
memcpy(&char_props, &properties, 1);
if (char_props.notify || char_props.indicate) {
/* Notification requires cccd */
memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t));
cccd_md.vloc = BLE_GATTS_VLOC_STACK;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
}
ble_gatts_char_md_t char_md = {0};
char_md.char_props = char_props;
char_md.p_cccd_md =
(char_props.notify || char_props.indicate) ? &cccd_md : NULL;
/* Attribute declaration */
ble_gatts_attr_md_t attr_md = {0};
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.vlen = (min_length == max_length) ? 0 : 1;
if (char_props.read || char_props.notify || char_props.indicate) {
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
}
if (char_props.write) {
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
}
ble_gatts_attr_t attr_char_value = {0};
attr_char_value.p_uuid = p_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = min_length;
attr_char_value.max_len = max_length;
attr_char_value.p_value = p_data;
ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle,
&char_md,
&attr_char_value,
p_char_handle));
return ERROR_NONE;
}

View file

@ -0,0 +1,45 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _CUSTOM_HELPER_H_
#define _CUSTOM_HELPER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "common/common.h"
#include "ble.h"
#include "UUID.h"
uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base);
error_t custom_decode_uuid(uint8_t const *const p_uuid_base,
ble_uuid_t *p_uuid);
ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid);
error_t custom_add_in_characteristic(uint16_t service_handle,
ble_uuid_t *p_uuid,
uint8_t properties,
uint8_t *p_data,
uint16_t min_length,
uint16_t max_length,
ble_gatts_char_handles_t *p_char_handle);
#ifdef __cplusplus
}
#endif
#endif // ifndef _CUSTOM_HELPER_H_

103
common/ansi_escape.h Normal file
View file

@ -0,0 +1,103 @@
/**************************************************************************/
/*!
@file ansi_esc_code.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
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 its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''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 THE COPYRIGHT HOLDER 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.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
/** \file
* \brief TBD
*
* \note TBD
*/
/** \ingroup TBD
* \defgroup TBD
* \brief TBD
*
* @{
*/
#ifndef _ANSI_ESC_CODE_H_
#define _ANSI_ESC_CODE_H_
#ifdef __cplusplus
extern "C" {
#endif
#define CSI_CODE(seq) "\33[" seq
#define CSI_SGR(x) CSI_CODE(#x) "m"
//------------- Cursor movement -------------//
#define ANSI_CURSOR_UP(n) CSI_CODE(#n "A")
#define ANSI_CURSOR_DOWN(n) CSI_CODE(#n "B")
#define ANSI_CURSOR_FORWARD(n) CSI_CODE(#n "C")
#define ANSI_CURSOR_BACKWARD(n) CSI_CODE(#n "D")
#define ANSI_CURSOR_LINE_DOWN(n) CSI_CODE(#n "E")
#define ANSI_CURSOR_LINE_UP(n) CSI_CODE(#n "F")
#define ANSI_CURSOR_POSITION(n, m) CSI_CODE(#n ";" #m "H")
#define ANSI_ERASE_SCREEN(n) CSI_CODE(#n "J")
#define ANSI_ERASE_LINE(n) CSI_CODE(#n "K")
/** text color */
#define ANSI_TEXT_BLACK CSI_SGR(30)
#define ANSI_TEXT_RED CSI_SGR(31)
#define ANSI_TEXT_GREEN CSI_SGR(32)
#define ANSI_TEXT_YELLOW CSI_SGR(33)
#define ANSI_TEXT_BLUE CSI_SGR(34)
#define ANSI_TEXT_MAGENTA CSI_SGR(35)
#define ANSI_TEXT_CYAN CSI_SGR(36)
#define ANSI_TEXT_WHITE CSI_SGR(37)
#define ANSI_TEXT_DEFAULT CSI_SGR(39)
/** background color */
#define ANSI_BG_BLACK CSI_SGR(40)
#define ANSI_BG_RED CSI_SGR(41)
#define ANSI_BG_GREEN CSI_SGR(42)
#define ANSI_BG_YELLOW CSI_SGR(43)
#define ANSI_BG_BLUE CSI_SGR(44)
#define ANSI_BG_MAGENTA CSI_SGR(45)
#define ANSI_BG_CYAN CSI_SGR(46)
#define ANSI_BG_WHITE CSI_SGR(47)
#define ANSI_BG_DEFAULT CSI_SGR(49)
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_ANSI_ESC_CODE_H_ */
/** @} */

200
common/assertion.h Normal file
View file

@ -0,0 +1,200 @@
/**************************************************************************/
/*!
@file assertion.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, K. Townsend (microBuilder.eu)
All rights reserved.
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 its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''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 THE COPYRIGHT HOLDER 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.
*/
/**************************************************************************/
/** \file
* \brief TBD
*
* \note TBD
*/
/** \ingroup TBD
* \defgroup TBD
* \brief TBD
*
* @{
*/
#ifndef _ASSERTION_H_
#define _ASSERTION_H_
#include "projectconfig.h"
#ifdef __cplusplus
extern "C"
{
#endif
static inline void debugger_breakpoint(void) ATTR_ALWAYS_INLINE;
static inline void debugger_breakpoint(void)
{
#ifndef _TEST_
__asm("BKPT #0\n");
#endif
}
//--------------------------------------------------------------------+
// Compile-time Assert
//--------------------------------------------------------------------+
#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
#define _ASSERT_COUNTER __COUNTER__
#else
#define _ASSERT_COUNTER __LINE__
#endif
#define ASSERT_STATIC(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) }
//--------------------------------------------------------------------+
// Assert Helper
//--------------------------------------------------------------------+
//#ifndef _TEST_
// #define ASSERT_MESSAGE(format, ...)\
// _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
//#else
// #define ASSERT_MESSAGE(format, ...)\
// _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__)
//#endif
#if CFG_DEBUG == 3
#define ASSERT_MESSAGE(format, ...) debugger_breakpoint()
#elif CFG_DEBUG == 2
#define ASSERT_MESSAGE(format, ...) printf("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
#else
#define ASSERT_MESSAGE(format, ...)
#endif
#define ASSERT_ERROR_HANDLER(x, para) \
return (x)
#define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \
do{\
setup_statement;\
if (!(condition)) {\
ASSERT_MESSAGE(format, __VA_ARGS__);\
error_handler(error, handler_para);\
}\
}while(0)
#define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__)
//--------------------------------------------------------------------+
// error_t Status Assert TODO use ASSERT_DEFINE
//--------------------------------------------------------------------+
#define ASSERT_STATUS_MESSAGE(sts, message) \
ASSERT_DEFINE(error_t status = (error_t)(sts),\
ERROR_NONE == status, status, "%s: %s", ErrorStr[status], message)
#define ASSERT_STATUS(sts) \
ASSERT_DEFINE(error_t status = (error_t)(sts),\
ERROR_NONE == status, status, "error = %d", status)
#define ASSERT_STATUS_RET_VOID(sts) \
ASSERT_DEFINE(error_t status = (error_t)(sts),\
ERROR_NONE == status, (void) 0, "error = %d", status)
//--------------------------------------------------------------------+
// Logical Assert
//--------------------------------------------------------------------+
#define ASSERT(...) ASSERT_TRUE(__VA_ARGS__)
#define ASSERT_TRUE(condition , error) ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false")
#define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true")
//--------------------------------------------------------------------+
// Pointer Assert
//--------------------------------------------------------------------+
#define ASSERT_PTR(...) ASSERT_PTR_NOT_NULL(__VA_ARGS__)
#define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL")
#define ASSERT_PTR_NULL(pointer, error) ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL")
//--------------------------------------------------------------------+
// Integral Assert
//--------------------------------------------------------------------+
#define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \
ASSERT_DEFINE(\
uint32_t exp = (expected); uint32_t act = (actual),\
exp==act,\
error,\
"expected " type_format ", actual " type_format, exp, act)
#define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \
ASSERT_DEFINE(\
uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
(low <= act) && (act <= up),\
error,\
"expected within " type_format " - " type_format ", actual " type_format, low, up, act)
//--------------------------------------------------------------------+
// Integer Assert
//--------------------------------------------------------------------+
#define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__)
#define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__)
#define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__)
//--------------------------------------------------------------------+
// Hex Assert
//--------------------------------------------------------------------+
#define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__)
#define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__)
#define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__)
//--------------------------------------------------------------------+
// Bin Assert
//--------------------------------------------------------------------+
#define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d"
#define BIN8_PRINTF_CONVERT(byte) \
((byte) & 0x80 ? 1 : 0), \
((byte) & 0x40 ? 1 : 0), \
((byte) & 0x20 ? 1 : 0), \
((byte) & 0x10 ? 1 : 0), \
((byte) & 0x08 ? 1 : 0), \
((byte) & 0x04 ? 1 : 0), \
((byte) & 0x02 ? 1 : 0), \
((byte) & 0x01 ? 1 : 0)
#define ASSERT_BIN8(...) ASSERT_BIN8_EQUAL(__VA_ARGS__)
#define ASSERT_BIN8_EQUAL(expected, actual, error)\
ASSERT_DEFINE(\
uint8_t exp = (expected); uint8_t act = (actual),\
exp==act,\
error,\
"expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) )
#ifdef __cplusplus
}
#endif
#endif /* _ASSERTION_H_ */
/** @} */

96
common/binary.h Normal file
View file

@ -0,0 +1,96 @@
/**************************************************************************/
/*!
@file binary.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, K. Townsend (microBuilder.eu)
All rights reserved.
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 its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''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 THE COPYRIGHT HOLDER 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.
*/
/**************************************************************************/
/** \ingroup TBD
* \defgroup TBD
* \brief TBD
*
* @{
*/
#ifndef _BINARY_H_
#define _BINARY_H_
#ifdef __cplusplus
extern "C" {
#endif
/// n-th Bit
#define BIT(n) (1 << (n))
/// set n-th bit of x to 1
#define BIT_SET(x, n) ( (x) | BIT(n) )
/// clear n-th bit of x
#define BIT_CLR(x, n) ( (x) & (~BIT(n)) )
/// test n-th bit of x
#define BIT_TEST(x, n) ( (x) & BIT(n) )
#if defined(__GNUC__) && !defined(__CC_ARM) // keil does not support binary format
#define BIN8(x) ((uint8_t) (0b##x))
#define BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
#define BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
#else
// internal macro of B8, B16, B32
#define _B8__(x) (((x&0x0000000FUL)?1:0) \
+((x&0x000000F0UL)?2:0) \
+((x&0x00000F00UL)?4:0) \
+((x&0x0000F000UL)?8:0) \
+((x&0x000F0000UL)?16:0) \
+((x&0x00F00000UL)?32:0) \
+((x&0x0F000000UL)?64:0) \
+((x&0xF0000000UL)?128:0))
#define BIN8(d) ((uint8_t) _B8__(0x##d##UL))
#define BIN16(dmsb,dlsb) (((uint16_t)BIN8(dmsb)<<8) + BIN8(dlsb))
#define BIN32(dmsb,db2,db3,dlsb) \
(((uint32_t)BIN8(dmsb)<<24) \
+ ((uint32_t)BIN8(db2)<<16) \
+ ((uint32_t)BIN8(db3)<<8) \
+ BIN8(dlsb))
#endif
#ifdef __cplusplus
}
#endif
#endif /* _BINARY_H_ */
/** @} */

151
common/ble_error.h Normal file
View file

@ -0,0 +1,151 @@
/**************************************************************************/
/*!
@file ble_error.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, K. Townsend (microBuilder.eu)
All rights reserved.
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 its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''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 THE COPYRIGHT HOLDER 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.
*/
/**************************************************************************/
/** \file
* \brief Error Header
*
* \note TBD
*/
/** \ingroup Group_Common
* \defgroup Group_Error Error Codes
* @{
*/
#ifndef _BLE_ERROR_H_
#define _BLE_ERROR_H_
#include "projectconfig.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
/*=======================================================================
NORDIC GLOBAL ERRORS 0x0000 .. 0x00FF
-----------------------------------------------------------------------
Errors mapped from nrf_error.h
-----------------------------------------------------------------------*/
ERROR_NONE = 0x0000 , ///< Successful command
ERROR_SVC_HANDLER_MISSING = 0x0001 , ///< SVC handler is missing
ERROR_SOFTDEVICE_NOT_ENABLED = 0x0002 , ///< SoftDevice has not been enabled
ERROR_INTERNAL = 0x0003 , ///< Internal Error
ERROR_NO_MEM = 0x0004 , ///< No Memory for operation
ERROR_NOT_FOUND = 0x0005 , ///< Not found
ERROR_NOT_SUPPORTED = 0x0006 , ///< Not supported
ERROR_INVALID_PARAM = 0x0007 , ///< Invalid Parameter
ERROR_INVALID_STATE = 0x0008 , ///< Invalid state, operation disallowed in this state
ERROR_INVALID_LENGTH = 0x0009 , ///< Invalid Length
ERROR_INVALID_FLAGS = 0x000A , ///< Invalid Flags
ERROR_INVALID_DATA = 0x000B , ///< Invalid Data
ERROR_DATA_SIZE = 0x000C , ///< Data size exceeds limit
ERROR_TIMEOUT = 0x000D , ///< Operation timed out
ERROR_NULL = 0x000E , ///< Null Pointer
ERROR_FORBIDDEN = 0x000F , ///< Forbidden Operation
ERROR_INVALID_ADDR = 0x0010 , ///< Bad Memory Address
ERROR_BUSY = 0x0011 , ///< Busy
/*=======================================================================*/
ERROR_INVALIDPARAMETER = 0x0100 , /**< An invalid parameter value was provided */
ERROR_I2C_XFER_FAILED = 0x0101 , /**< an failed attempt to make I2C transfer */
/*=======================================================================
SIMPLE BINARY PROTOCOL ERRORS 0x0120 .. 0x013F
-----------------------------------------------------------------------
Errors relating to the simple binary protocol (/src//protocol)
-----------------------------------------------------------------------*/
ERROR_PROT_INVALIDMSGTYPE = 0x121, /**< Unexpected msg type encountered */
ERROR_PROT_INVALIDCOMMANDID = 0x122, /**< Unknown or out of range command ID */
ERROR_PROT_INVALIDPAYLOAD = 0x123, /**< Message payload has a problem (invalid len, etc.) */
/*=======================================================================*/
//------------- based on Nordic SDM nrf_error_sdm.h -------------//
ERROR_SDM_LFCLK_SOURCE_UNKNOWN = 0x1000 , ///< Unknown lfclk source
ERROR_SDM_INCORRECT_INTERRUPT_CONFIGURATION = 0x1001 , ///< Incorrect interrupt configuration (can be caused by using illegal priority levels, or having enabled SoftDevice interrupts)
ERROR_SDM_INCORRECT_CLENR0 = 0x1002 , ///< Incorrect CLENR0 (can be caused by erronous SoftDevice flashing)
//------------- based on Nordic SOC nrf_error_soc.h -------------//
/* Mutex Errors */
ERROR_SOC_MUTEX_ALREADY_TAKEN = 0x2000 , ///< Mutex already taken
/* NVIC errors */
ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE = 0x2001 , ///< NVIC interrupt not available
ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED = 0x2002 , ///< NVIC interrupt priority not allowed
ERROR_SOC_NVIC_SHOULD_NOT_RETURN = 0x2003 , ///< NVIC should not return
/* Power errors */
ERROR_SOC_POWER_MODE_UNKNOWN = 0x2004 , ///< Power mode unknown
ERROR_SOC_POWER_POF_THRESHOLD_UNKNOWN = 0x2005 , ///< Power POF threshold unknown
ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN = 0x2006 , ///< Power off should not return
/* Rand errors */
ERROR_SOC_RAND_NOT_ENOUGH_VALUES = 0x2007 , ///< RAND not enough values
/* PPI errors */
ERROR_SOC_PPI_INVALID_CHANNEL = 0x2008 , ///< Invalid PPI Channel
ERROR_SOC_PPI_INVALID_GROUP = 0x2009 , ///< Invalid PPI Group
//------------- based on Nordic STK (ble) ble_err.h -------------//
ERROR_BLE_INVALID_CONN_HANDLE = 0x3001 , /**< Invalid connection handle. */
ERROR_BLE_INVALID_ATTR_HANDLE = 0x3002 , /**< Invalid attribute handle. */
ERROR_BLE_NO_TX_BUFFERS = 0x3003 , /**< Buffer capacity exceeded. */
// L2CAP
ERROR_BLE_L2CAP_CID_IN_USE = 0x3100 , /**< CID already in use. */
// GAP
ERROR_BLE_GAP_UUID_LIST_MISMATCH = 0x3200 , /**< UUID list does not contain an integral number of UUIDs. */
ERROR_BLE_GAP_DISCOVERABLE_WITH_WHITELIST = 0x3201 , /**< Use of Whitelist not permitted with discoverable advertising. */
ERROR_BLE_GAP_INVALID_BLE_ADDR = 0x3202 , /**< The upper two bits of the address do not correspond to the specified address type. */
// GATTC
ERROR_BLE_GATTC_PROC_NOT_PERMITTED = 0x3300 ,
// GATTS
ERROR_BLEGATTS_INVALID_ATTR_TYPE = 0x3400 , /**< Invalid attribute type. */
ERROR_BLEGATTS_SYS_ATTR_MISSING = 0x3401 , /**< System Attributes missing. */
}error_t;
#ifdef __cplusplus
}
#endif
#endif /* _BLE_ERROR_H_ */
/** @} */

236
common/common.h Normal file
View file

@ -0,0 +1,236 @@
/**************************************************************************/
/*!
@file common.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, K. Townsend (microBuilder.eu)
All rights reserved.
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 its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''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 THE COPYRIGHT HOLDER 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.
*/
/**************************************************************************/
/** \defgroup Group_Common Common Files
* @{
*
* \defgroup Group_CommonH common.h
*
* @{
*/
#ifndef _COMMON_H_
#define _COMMON_H_
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------+
// INCLUDES
//--------------------------------------------------------------------+
//------------- Standard Header -------------//
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
//------------- General Header -------------//
#include "projectconfig.h"
#include "compiler.h"
#include "assertion.h"
#include "binary.h"
#include "ble_error.h"
//------------- MCU header -------------//
//#include "nrf.h"
//--------------------------------------------------------------------+
// TYPEDEFS
//--------------------------------------------------------------------+
typedef unsigned char byte_t;
typedef float float32_t;
typedef double float64_t;
//--------------------------------------------------------------------+
// MACROS
//--------------------------------------------------------------------+
#define STRING_(x) #x // stringify without expand
#define XSTRING_(x) STRING_(x) // expand then stringify
#define STRING_CONCAT_(a, b) a##b // concat without expand
#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
#define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
#define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff))
#define U16_TO_U8S_BE(u16) U16_HIGH_U8(u16), U16_LOW_U8(u16)
#define U16_TO_U8S_LE(u16) U16_LOW_U8(u16), U16_HIGH_U8(u16)
#define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
#define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
#define U32_B3_U8(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff))
#define U32_B4_U8(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB
#define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
#define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
//--------------------------------------------------------------------+
// INLINE FUNCTION
//--------------------------------------------------------------------+
#define memclr_(buffer, size) memset(buffer, 0, size)
//------------- Conversion -------------//
/// form an uint32_t from 4 x uint8_t
static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
{
return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
}
static inline uint8_t u16_high_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
static inline uint8_t u16_high_u8(uint16_t u16)
{
return (uint8_t) ((u16 >> 8) & 0x00ff);
}
static inline uint8_t u16_low_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
static inline uint8_t u16_low_u8(uint16_t u16)
{
return (uint8_t) (u16 & 0x00ff);
}
//------------- Min -------------//
static inline uint8_t min8_of(uint8_t x, uint8_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint8_t min8_of(uint8_t x, uint8_t y)
{
return (x < y) ? x : y;
}
static inline uint16_t min16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint16_t min16_of(uint16_t x, uint16_t y)
{
return (x < y) ? x : y;
}
static inline uint32_t min32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint32_t min32_of(uint32_t x, uint32_t y)
{
return (x < y) ? x : y;
}
//------------- Max -------------//
static inline uint32_t max32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint32_t max32_of(uint32_t x, uint32_t y)
{
return (x > y) ? x : y;
}
//------------- Align -------------//
static inline uint32_t align32 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint32_t align32 (uint32_t value)
{
return (value & 0xFFFFFFE0UL);
}
static inline uint32_t align16 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint32_t align16 (uint32_t value)
{
return (value & 0xFFFFFFF0UL);
}
static inline uint32_t align_n (uint32_t alignment, uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
static inline uint32_t align_n (uint32_t alignment, uint32_t value)
{
return value & (~(alignment-1));
}