diff options
Diffstat (limited to 'platform/atm2/ATM22xx-x1x/lib')
28 files changed, 6894 insertions, 0 deletions
diff --git a/platform/atm2/ATM22xx-x1x/lib/app_bass/app_bass.h b/platform/atm2/ATM22xx-x1x/lib/app_bass/app_bass.h new file mode 100644 index 0000000..d70f89f --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/app_bass/app_bass.h @@ -0,0 +1,66 @@ +/** + ******************************************************************************* + * + * @file app_bass.h + * + * @brief Battery service for application layer + * + * Copyright (C) Atmosic 2020 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_APPBASS Battery service for application layer + * @ingroup ATM_BTFM_API + * @brief The battery service for application of ATM bluetooth framework + * + * This module contains the battery service interface for application layer. + * + * @{ + ******************************************************************************* + */ + +/* + * INCLUDE FILE + ******************************************************************************* + */ +#include "ble_bass.h" +#include "gadc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + ******************************************************************************* + * @brief Retrieve the parameters of battery service + * return Battery service parameters + ******************************************************************************* + */ +ble_bass_params_t const *app_bass_param(void); + +/** + ******************************************************************************* + * @brief Get battery level + * return Return the current battery level + ******************************************************************************* + */ +uint8_t app_bass_get_battery_lvl(void); + +/** + ******************************************************************************* + * @brief Send battery level + * @param[in] lvl Battery level. + ******************************************************************************* + */ +void app_bass_send_battery_lvl(uint8_t lvl); + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_APPBASS + diff --git a/platform/atm2/ATM22xx-x1x/lib/app_batt/app_batt.h b/platform/atm2/ATM22xx-x1x/lib/app_batt/app_batt.h new file mode 100644 index 0000000..2f52918 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/app_batt/app_batt.h @@ -0,0 +1,92 @@ +/** + ******************************************************************************* + * + * @file app_batt.h + * + * @brief battery procedure for application layer. + * + * Copyright (C) Atmosic 2022 + * + ******************************************************************************* + */ + +#pragma once + +/** + * @defgroup APP_BATT_PROC Battery free procedure for application layer. + * @ingroup ATM_BTFM_PROC + * @brief Battery free procedure that control HSC and booster. + * @{ + */ + +#include "arch.h" +#include "gadc.h" +#include "batt_model.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/// Bit definition for hiberation restart. +#define APP_BATT_HIB_RESTART_BIT (1 << 0) +/// Bit definition for auto timer +#define APP_BATT_AUTO_TIMER_BIT (1 << 1) + +#ifndef CFG_APP_FEATURE +#define CFG_APP_FEATURE (APP_BATT_HIB_RESTART_BIT | APP_BATT_AUTO_TIMER_BIT) +#endif + +#if (CFG_APP_FEATURE & APP_BATT_AUTO_TIMER_BIT) +/// The low battery percentage boundary. +#define APP_BATT_LOW_BATTERY_PERCENTAGE 30 +/// Time to poll capacity while battery is low. +#define APP_BATT_LOW_POLL_TIME_CS 10000 +/// Time to poll capacity while battery is high. +#define APP_BATT_HIGH_POLL_TIME_CS 60000 +#endif + +#if (CFG_APP_FEATURE & APP_BATT_HIB_RESTART_BIT) +/// Time to check while low battery in hibernation +#define APP_BATT_HIB_TIME_CS 60000 +#endif + +/// Callback functions to application. +typedef struct { + /// Start application. + void (*app_start)(void (*done)(void)); + /// Stop application. + void (*app_stop)(void (*done)(void)); + /// Update battery level. + void (*level_update)(uint8_t); +} app_batt_cbs_t; + +/** + ******************************************************************************* + * @brief Start application battery procedure. + * @param[in] cbs Callback to application. + ******************************************************************************* + */ +void app_batt_start(app_batt_cbs_t const *cbs); + +#if (CFG_APP_FEATURE & APP_BATT_AUTO_TIMER_BIT) +/** + ******************************************************************************* + * @brief Stop application battery procedure. + ******************************************************************************* + */ +void app_batt_stop(void); +#else +/** + ******************************************************************************* + * @brief Get the battery level. + * @return False if another operation is onging. + * @note The result will pass to level_update callback function. + ******************************************************************************* + */ +bool app_batt_get_level(void); +#endif +#ifdef __cplusplus +} +#endif + +/// @} APP_BATT_PROC diff --git a/platform/atm2/ATM22xx-x1x/lib/app_diss/app_diss.h b/platform/atm2/ATM22xx-x1x/lib/app_diss/app_diss.h new file mode 100644 index 0000000..b5611f4 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/app_diss/app_diss.h @@ -0,0 +1,39 @@ +/** + ******************************************************************************* + * + * @file app_diss.h + * + * @brief Device Information Service Configuration Setting + * + * Copyright (C) Atmosic 2020 + * + ******************************************************************************* + */ + +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_APPDISS Device information service for application layer + * @ingroup ATM_BTFM_API + * @brief The device information service for application of ATM bluetooth framework + * + * This module contains the device information service interface for application layer. + * + * @{ + ******************************************************************************* + */ +#include "app_config.h" +#include "ble_diss.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct diss_param const *app_dis_param(void); + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_APPBASS diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv.h b/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv.h new file mode 100644 index 0000000..b87aa75 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv.h @@ -0,0 +1,234 @@ +/** + ******************************************************************************* + * + * @file atm_adv.h + * + * @brief Atmosic BLE advertising procedure + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_PADV ADV procedures + * @ingroup ATM_BTFM_PROC + * @brief ATM bluetooth framework advertisement procedures + * + * This module contains the necessary procedure to manipulate ADV. + * + * @{ + ******************************************************************************* + */ + +#include "ble_gap.h" +#include "atm_debug.h" +#include "atm_adv_param.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ATM_INVALID_ADVIDX 0xFF + +/// ADV state. +typedef enum { + /// Advertising state machine is in idle state. + ATM_ADV_IDLE, + /// Advertising state machine is in creating state. + ATM_ADV_CREATING, + /// Successfully created, data not set. + ATM_ADV_CREATED, + /// ADV data is under setting. + ATM_ADV_ADVDATA_SETTING, + /// ADV data is set. + ATM_ADV_ADVDATA_DONE, + /// Scan data is under setting. + ATM_ADV_SCANDATA_SETTING, + /// Scan data is set. + ATM_ADV_SCANDATA_DONE, + /// Advertising state machine is in off state. + ATM_ADV_OFF, + /// Advertising state machine is in starting state. + ATM_ADV_STARTING, + /// Advertising state machine is in on state. + ATM_ADV_ON, + /// Advertising state machine is in stoping state. + ATM_ADV_STOPPING, + /// Advertising state machine is in deleting state. + ATM_ADV_DELETING, + /// Delete created advertising successfully. + ATM_ADV_DELETED +} atm_adv_state_t; + +/// Advertising callback function +typedef void (*atm_adv_cb_t)(atm_adv_state_t state, uint8_t act_idx, + ble_err_code_t status); + +/** + ******************************************************************************* + * @brief Register advertising callback function + * @param[in] cb Advertising callback function. + ******************************************************************************* + */ +void atm_adv_reg(atm_adv_cb_t cb); + +/** + ******************************************************************************* + * @brief Apply the advertising activity + * @param[in] create Advertising activity + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_create(atm_adv_create_t const *create); + +/** + ******************************************************************************* + * @brief Set the advertising data + * @param[in] act_idx Activity index + * @param[in] data Advertising data + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_set_adv_data(uint8_t act_idx, atm_adv_data_t const *data); + +/** + ******************************************************************************* + * @brief Set the scan response data + * @param[in] act_idx Activity index + * @param[in] data Scan response data + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_set_scan_data(uint8_t act_idx, atm_adv_data_t const *data); + +/** + ******************************************************************************* + * @brief Start advertising + * @param[in] act_idx Activity index + * @param[in] start Additional advertising parameters + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_start(uint8_t act_idx, atm_adv_start_t const *start); + +/** + ******************************************************************************* + * @brief Stop advertising + * @param[in] act_idx Activity index + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_stop(uint8_t act_idx); + +/** + ******************************************************************************* + * @brief Delete advertising + * @param[in] act_idx Activity index + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_delete(uint8_t act_idx); + +/** + ******************************************************************************* + * @brief Get advertising activity state + * @param[in] act_idx Activity index + * @return state value @ref atm_adv_state_t + ******************************************************************************* + */ +uint8_t atm_adv_get_state(uint8_t act_idx); + +/** + ******************************************************************************* + * @brief Check information element + * + * @param[in] payload The information element of adv. data or scan response data + * @param[in] len The length of payload + * @return true if data is correct + ******************************************************************************* + */ +__NONNULL(1) +bool atm_adv_check_adv_len_valid(uint8_t const *payload, uint16_t len); + +/** + ******************************************************************************* + * @brief Parameter sanity check for adv data + * @param[in] create Parameter setting for create adv. + * @param[in] adv Advertising data + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_check_adv_data(atm_adv_create_t const *create, + atm_adv_data_t const *adv); + +/** + ******************************************************************************* + * @brief Parameter sanity check for scan response data + * @param[in] create Parameter setting for create adv. + * @param[in] scan Scan response data + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_check_scan_resp_data(atm_adv_create_t const *create, + atm_adv_data_t const *scan); + +/** + ******************************************************************************* + * @brief Parameter sanity check for set adv. data or scan response data + * @param[in] create Parameter setting for create adv. + * @param[in] adv Advertising data + * @param[in] scan Scan response data + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +ble_err_code_t atm_adv_set_data_sanity(atm_adv_create_t const *create, + atm_adv_data_t const *adv, atm_adv_data_t const *scan); + +/** + ******************************************************************************* + * @brief Advertisement stopped indication callback function + * @param[in] param Indication information + ******************************************************************************* + */ +void atm_adv_stopped_ind(ble_gap_ind_stop_t const *param); + +/** + ******************************************************************************* + * @brief Convert data type from atm_adv_data_nvds_t to atm_adv_data_t + * @param[in] data Flash nvds data pointer reading from flash nvds + * @return data pointer using atm_adv_data_t data type + ******************************************************************************* + */ +__INLINE atm_adv_data_t *atm_adv_convert_nvds_data_type(atm_adv_data_nvds_t *data) +{ + return (atm_adv_data_t *)data; +} + +/** + ******************************************************************************* + * @brief Print advertising timeout parameter + * @param[in] create Parameter setting for create adv. + * @param[in] start Parameter setting for start adv. + * @return BLE_ERR_NO_ERROR for success @ref ble_err_code_t + ******************************************************************************* + */ +#ifdef ATM_DEBUG +ble_err_code_t atm_adv_timeout_param_print(atm_adv_create_t const *create, + atm_adv_start_t const *start); +#else +__INLINE uint8_t atm_adv_timeout_param_print(atm_adv_create_t const *create, + atm_adv_start_t const *start) +{ + return BLE_ERR_NO_ERROR; +} +#endif + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_PADV + diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv_param.h b/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv_param.h new file mode 100644 index 0000000..4ed9c6e --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv_param.h @@ -0,0 +1,182 @@ +/** + ******************************************************************************* + * + * @file atm_adv_param.h + * + * @brief Atmosic BLE advertising parameter + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_PADVP ADV parameters + * @ingroup ATM_BTFM_PROC + * @brief ATM bluetooth framework advertisement parameters + * + * This module contains the necessary function to provide predefined parameters + * for usage of ADV procedures @see atm_adv.h + * + * @{ + ******************************************************************************* + */ + +#include "atm_log.h" +#include "ble_gap.h" + +// Overwrite setting by application +#ifdef GAP_ADV_PARM_NAME +#include STR(GAP_ADV_PARM_NAME) +#endif + +// GAP_ADV_PARM_NAME can overwrite default parameter setting +#include "atm_adv_param_internal.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if CFG_ADV_CREATE_PARAM_CONST +#define __ATM_ADV_CREATE_PARAM_CONST const +#else +#define __ATM_ADV_CREATE_PARAM_CONST +#endif + +#if CFG_ADV_DATA_PARAM_CONST +#define __ATM_ADV_DATA_PARAM_CONST const +#else +#define __ATM_ADV_DATA_PARAM_CONST +#endif + +#if CFG_ADV_START_PARAM_CONST +#define __ATM_ADV_START_PARAM_CONST const +#else +#define __ATM_ADV_START_PARAM_CONST +#endif + + +/// Parameter for advertising data and scan response data +typedef struct { + /// Length of advertising data or scan response data + uint16_t len; + /// The content of advertising data or scan response data + uint8_t __ATM_ADV_DATA_PARAM_CONST *data; +} atm_adv_data_t; + +/// NVDS parameter for advertising data and scan response data +typedef struct { + /// Length of advertising data or scan response data + uint16_t len; + /// The content of advertising data or scan response data + uint8_t *data; +} atm_adv_data_nvds_t; + +/// Create an advertising activity command +typedef struct { + ble_own_addr_t own_addr_type; + ble_gapm_adv_create_param_t adv_param; +} atm_adv_create_t; + +/// Parameters of Start advertising +typedef ble_gapm_adv_param_t atm_adv_start_t; + +#ifndef CFG_GAP_ADV_MAX_INST +#define CFG_GAP_ADV_MAX_INST 1 +#endif + +/** + ******************************************************************************* + * @brief Get persistent advertising create parameter. + * @param[in] idx The index of persistent parameter. + ******************************************************************************* + */ +atm_adv_create_t __ATM_ADV_CREATE_PARAM_CONST *atm_adv_create_param_get(uint8_t idx); + +/** + ******************************************************************************* + * @brief Get persistent advertising start parameter. + * @param[in] idx The index of persistent parameter. + ******************************************************************************* + */ +atm_adv_start_t __ATM_ADV_START_PARAM_CONST *atm_adv_start_param_get(uint8_t idx); + +/** + ******************************************************************************* + * @brief Get persistent advertising data. + * @param[in] idx The index of persistent parameter. + ******************************************************************************* + */ +atm_adv_data_t __ATM_ADV_DATA_PARAM_CONST *atm_adv_advdata_param_get(uint8_t idx); + +/** + ******************************************************************************* + * @brief Get persistent scan response data. + * @param[in] idx The index of persistent parameter. + ******************************************************************************* + */ +atm_adv_data_t __ATM_ADV_DATA_PARAM_CONST *atm_adv_scandata_param_get(uint8_t idx); + +/** + ******************************************************************************* + * @brief Get advertising create parameters from Flash NVDS. + * @param[in] is_wurx True if wurx ADV. + * @param[in,out] create Create parameter + * @return true for success + ******************************************************************************* + */ +bool atm_adv_create_param_nvds(bool is_wurx, atm_adv_create_t *create); + +/** + ******************************************************************************* + * @brief Get advertising start parameters from Flash NVDS. + * @param[in,out] start Start parameter + * @return true for success + ******************************************************************************* + */ +bool atm_adv_start_param_nvds(atm_adv_start_t *start); + +/** + ******************************************************************************* + * @brief Get scan response data from Flash NVDS. + * @param[in,out] scan Scan response data. scan->data must be NULL. + * @return true for success, scan->data will be allocated dynamically + ******************************************************************************* + */ +bool atm_adv_scandata_param_nvds(atm_adv_data_nvds_t *scan); + +/** + ******************************************************************************* + * @brief Get advertising data from Flash NVDS. + * @param[in,out] adv Advertising data. adv->data must be NULL. + * @return true for success, adv->data will be allocated dynamically + ******************************************************************************* + */ +bool atm_adv_advdata_param_nvds(atm_adv_data_nvds_t *adv); + +/** + ******************************************************************************* + * @brief Get advertising data from Flash NVDS. + * @param[in,out] adv Advertising data/scan response data. adv->data must be NULL. + * @param[in] tag_id NVDS tag ID + * @return true for success, adv->data will be allocated dynamically + ******************************************************************************* + */ +__NONNULL(1) +bool atm_adv_data_param_nvds_id(atm_adv_data_nvds_t *adv, uint8_t tag_id); + +/** + ******************************************************************************* + * @brief Free advertising or scan response data reading from Flash NVDS + * @param[in] nvds_data Advertising or scan response data + ******************************************************************************* + */ +void atm_adv_data_param_nvds_free(atm_adv_data_nvds_t *nvds_data); + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_PADVP diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv_param_internal.h b/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv_param_internal.h new file mode 100644 index 0000000..0eee737 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_adv/atm_adv_param_internal.h @@ -0,0 +1,249 @@ +/** + ******************************************************************************* + * + * @file atm_adv_param_internal.h + * + * @brief Atmosic BLE advertising default parameter + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ + +// Please use GAP_ADV_PARM_NAME to overwrite the parameter setting by example +// Prevent to modify this framework global default parameter setting + +#pragma once +#include "atm_adv_param.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Default Create Parameter Setting +#define DEFAULT_CREATE_PARAM_CONST 1 +#define DEFAULT_OWNER_ADDR_TYPE BLE_OWN_STATIC_ADDR +#define DEFAULT_CREATE_TYPE ADV_TYPE_LEGACY +#define DEFAULT_CREATE_PROPERTY ADV_LEGACY_UNDIR_CONN_MASK +#define DEFAULT_CREATE_FILTER_POLICY FILTER_SCAN_ANY_CON_ANY +#define DEFAULT_CREATE_CHNL_MAP ADV_ALL_CHNLS +#define DEFAULT_CREATE_PRIM_PHY BLE_GAP_PHY_1MBPS +#define DEFAULT_CREATE_DISCOVERY_MODE ADV_MODE_GEN_DISC +#define DEFAULT_CREATE_INTERVAL_MIN (ADV_INTERVAL_MIN * 2) +#define DEFAULT_CREATE_INTERVAL_MAX (ADV_INTERVAL_MIN * 2) +#define DEFAULT_CREATE_MAX_TX_POWER 0 +#define DEFAULT_CREATE_SEC_MAX_SKIP 0 +#define DEFAULT_CREATE_SEC_PHY BLE_GAP_PHY_1MBPS +#define DEFAULT_CREATE_SEC_ADV_SID 0 +#define DEFAULT_CREATE_PERI_INTERVAL_MIN 80 +#define DEFAULT_CREATE_PERI_INTERVAL_MAX 80 +#define DEFAULT_CREATE_PEER_ADDR_TYPE 0 +#define DEFAULT_CREATE_PEER_ADDR 0 + +// Default Start Parameter Setting +#define DEFAULT_START_PARAM_CONST 1 +#define DEFAULT_START_DURATION 0 +#ifdef AUTO_TEST +#ifdef IS_FOR_SIM + #define DEFAULT_START_MAX_ADV_EVENT 8 +#else // IS_FOR_SIM + #define DEFAULT_START_MAX_ADV_EVENT 80 +#endif // IS_FOR_SIM +#else // AUTO_TEST + #define DEFAULT_START_MAX_ADV_EVENT 0 +#endif // AUTO_TEST + +// Default Data Parameter Setting +#define DEFAULT_DATA_PARAM_CONST 1 +#define DEFAULT_DATA_ADV_ENABLE 1 +#define DEFAULT_DATA_SCANRSP_ENABLE 1 +#define DEFAULT_DATA_ADV_PAYLOAD \ + 0x0c,0x09,'A','t','m','o','s','i','c','-','A','D','V' +#define DEFAULT_DATA_SCANRSP_PAYLOAD \ + 0x09,0xff,0x00,0x60,'R','W','-','B','L','E' + +/************************************/ +// parameters // +/************************************/ +/// --- ADVs Create Parameter Constant------- +/// To change at runtime, need to set 0 +#ifndef CFG_ADV_CREATE_PARAM_CONST +#define CFG_ADV_CREATE_PARAM_CONST DEFAULT_CREATE_PARAM_CONST +#endif + +/// -------- ADV0 Register Parameter------------ +#ifndef CFG_ADV0_OWNER_ADDR_TYPE +#define CFG_ADV0_OWNER_ADDR_TYPE DEFAULT_OWNER_ADDR_TYPE +#endif +#ifndef CFG_ADV0_CREATE_TYPE +#define CFG_ADV0_CREATE_TYPE DEFAULT_CREATE_TYPE +#endif +#ifndef CFG_ADV0_CREATE_PROPERTY +#define CFG_ADV0_CREATE_PROPERTY DEFAULT_CREATE_PROPERTY +#endif +#ifndef CFG_ADV0_CREATE_FILTER_POLICY +#define CFG_ADV0_CREATE_FILTER_POLICY DEFAULT_CREATE_FILTER_POLICY +#endif +#ifndef CFG_ADV0_CREATE_PEER_ADDR_TYPE +#define CFG_ADV0_CREATE_PEER_ADDR_TYPE DEFAULT_CREATE_PEER_ADDR_TYPE +#endif +#ifndef CFG_ADV0_CREATE_PEER_ADDR +#define CFG_ADV0_CREATE_PEER_ADDR DEFAULT_CREATE_PEER_ADDR +#endif +#ifndef CFG_ADV0_CREATE_CHNL_MAP +#define CFG_ADV0_CREATE_CHNL_MAP DEFAULT_CREATE_CHNL_MAP +#endif +#ifndef CFG_ADV0_CREATE_PRIM_PHY +#define CFG_ADV0_CREATE_PRIM_PHY DEFAULT_CREATE_PRIM_PHY +#endif +#ifndef CFG_ADV0_CREATE_DISCOVERY_MODE +#define CFG_ADV0_CREATE_DISCOVERY_MODE DEFAULT_CREATE_DISCOVERY_MODE +#endif +#ifndef CFG_ADV0_CREATE_INTERVAL_MIN +#define CFG_ADV0_CREATE_INTERVAL_MIN DEFAULT_CREATE_INTERVAL_MIN +#endif +#ifndef CFG_ADV0_CREATE_INTERVAL_MAX +#define CFG_ADV0_CREATE_INTERVAL_MAX DEFAULT_CREATE_INTERVAL_MAX +#endif +#ifndef CFG_ADV0_CREATE_MAX_TX_POWER +#define CFG_ADV0_CREATE_MAX_TX_POWER DEFAULT_CREATE_MAX_TX_POWER +#endif +#ifndef CFG_ADV0_CREATE_SEC_MAX_SKIP +#define CFG_ADV0_CREATE_SEC_MAX_SKIP DEFAULT_CREATE_SEC_MAX_SKIP +#endif +#ifndef CFG_ADV0_CREATE_SEC_PHY +#define CFG_ADV0_CREATE_SEC_PHY DEFAULT_CREATE_SEC_PHY +#endif +#ifndef CFG_ADV0_CREATE_SEC_ADV_SID +#define CFG_ADV0_CREATE_SEC_ADV_SID DEFAULT_CREATE_SEC_ADV_SID +#endif +#ifndef CFG_ADV0_CREATE_PERI_INTERVAL_MIN +#define CFG_ADV0_CREATE_PERI_INTERVAL_MIN DEFAULT_CREATE_PERI_INTERVAL_MIN +#endif +#ifndef CFG_ADV0_CREATE_PERI_INTERVAL_MAX +#define CFG_ADV0_CREATE_PERI_INTERVAL_MAX DEFAULT_CREATE_PERI_INTERVAL_MAX +#endif + +/// -------- ADV1 Register Parameter------------ +#ifndef CFG_ADV1_OWNER_ADDR_TYPE +#define CFG_ADV1_OWNER_ADDR_TYPE DEFAULT_OWNER_ADDR_TYPE +#endif +#ifndef CFG_ADV1_CREATE_TYPE +#define CFG_ADV1_CREATE_TYPE DEFAULT_CREATE_TYPE +#endif +#ifndef CFG_ADV1_CREATE_PROPERTY +#define CFG_ADV1_CREATE_PROPERTY DEFAULT_CREATE_PROPERTY +#endif +#ifndef CFG_ADV1_CREATE_FILTER_POLICY +#define CFG_ADV1_CREATE_FILTER_POLICY DEFAULT_CREATE_FILTER_POLICY +#endif +#ifndef CFG_ADV1_CREATE_PEER_ADDR_TYPE +#define CFG_ADV1_CREATE_PEER_ADDR_TYPE DEFAULT_CREATE_PEER_ADDR_TYPE +#endif +#ifndef CFG_ADV1_CREATE_PEER_ADDR +#define CFG_ADV1_CREATE_PEER_ADDR DEFAULT_CREATE_PEER_ADDR +#endif +#ifndef CFG_ADV1_CREATE_CHNL_MAP +#define CFG_ADV1_CREATE_CHNL_MAP DEFAULT_CREATE_CHNL_MAP +#endif +#ifndef CFG_ADV1_CREATE_PRIM_PHY +#define CFG_ADV1_CREATE_PRIM_PHY DEFAULT_CREATE_PRIM_PHY +#endif +#ifndef CFG_ADV1_CREATE_DISCOVERY_MODE +#define CFG_ADV1_CREATE_DISCOVERY_MODE DEFAULT_CREATE_DISCOVERY_MODE +#endif +#ifndef CFG_ADV1_CREATE_INTERVAL_MIN +#define CFG_ADV1_CREATE_INTERVAL_MIN DEFAULT_CREATE_INTERVAL_MIN +#endif +#ifndef CFG_ADV1_CREATE_INTERVAL_MAX +#define CFG_ADV1_CREATE_INTERVAL_MAX DEFAULT_CREATE_INTERVAL_MAX +#endif +#ifndef CFG_ADV1_CREATE_MAX_TX_POWER +#define CFG_ADV1_CREATE_MAX_TX_POWER DEFAULT_CREATE_MAX_TX_POWER +#endif +#ifndef CFG_ADV1_CREATE_SEC_MAX_SKIP +#define CFG_ADV1_CREATE_SEC_MAX_SKIP DEFAULT_CREATE_SEC_MAX_SKIP +#endif +#ifndef CFG_ADV1_CREATE_SEC_PHY +#define CFG_ADV1_CREATE_SEC_PHY DEFAULT_CREATE_SEC_PHY +#endif +#ifndef CFG_ADV1_CREATE_SEC_ADV_SID +#define CFG_ADV1_CREATE_SEC_ADV_SID DEFAULT_CREATE_SEC_ADV_SID +#endif +#ifndef CFG_ADV1_CREATE_PERI_INTERVAL_MIN +#define CFG_ADV1_CREATE_PERI_INTERVAL_MIN DEFAULT_CREATE_PERI_INTERVAL_MIN +#endif +#ifndef CFG_ADV1_CREATE_PERI_INTERVAL_MAX +#define CFG_ADV1_CREATE_PERI_INTERVAL_MAX DEFAULT_CREATE_PERI_INTERVAL_MAX +#endif + + +/// ------- ADV Start Parameter Constant---- +/// To change at runtime, need to set 0 +#ifndef CFG_ADV_START_PARAM_CONST +#define CFG_ADV_START_PARAM_CONST DEFAULT_START_PARAM_CONST +#endif + +/// ------- ADV0 Start Parameter---- +/// Advertising duration (in unit of 10ms). 0 means that advertising continues +/// until the host disables it +#ifndef CFG_ADV0_START_DURATION +#define CFG_ADV0_START_DURATION DEFAULT_START_DURATION +#endif +/// The maximum number of extended ad events that the controller should try to +/// send before terminating the extended adv. +/// Valid only if extended advertising +#ifndef CFG_ADV0_START_MAX_ADV_EVENT +#define CFG_ADV0_START_MAX_ADV_EVENT DEFAULT_START_MAX_ADV_EVENT +#endif + +/// ------- ADV1 Start Parameter---- +/// Advertising duration (in unit of 10ms). 0 means that advertising continues +/// until the host disable it +#ifndef CFG_ADV1_START_DURATION +#define CFG_ADV1_START_DURATION DEFAULT_START_DURATION +#endif +/// The maximum number of extended ad events that the controller should try to +/// send before terminating the extended adv. +/// Valid only if extended advertising +#ifndef CFG_ADV1_START_MAX_ADV_EVENT +#define CFG_ADV1_START_MAX_ADV_EVENT DEFAULT_START_MAX_ADV_EVENT +#endif + +/// ------- ADV Data Parameter Constant---- +/// To change at runtime, need to set 0 +#ifndef CFG_ADV_DATA_PARAM_CONST +#define CFG_ADV_DATA_PARAM_CONST DEFAULT_DATA_PARAM_CONST +#endif + +/// ------- ADV0 Set Data Parameter---- +#ifndef CFG_ADV0_DATA_ADV_ENABLE +#define CFG_ADV0_DATA_ADV_ENABLE DEFAULT_DATA_ADV_ENABLE +#endif +#ifndef CFG_ADV0_DATA_SCANRSP_ENABLE +#define CFG_ADV0_DATA_SCANRSP_ENABLE DEFAULT_DATA_SCANRSP_ENABLE +#endif +#ifndef CFG_ADV0_DATA_ADV_PAYLOAD +#define CFG_ADV0_DATA_ADV_PAYLOAD DEFAULT_DATA_ADV_PAYLOAD +#endif +#ifndef CFG_ADV0_DATA_SCANRSP_PAYLOAD +#define CFG_ADV0_DATA_SCANRSP_PAYLOAD DEFAULT_DATA_SCANRSP_PAYLOAD +#endif + +/// ------- ADV1 Set Data Parameter---- +#ifndef CFG_ADV1_DATA_ADV_ENABLE +#define CFG_ADV1_DATA_ADV_ENABLE DEFAULT_DATA_ADV_ENABLE +#endif +#ifndef CFG_ADV1_DATA_SCANRSP_ENABLE +#define CFG_ADV1_DATA_SCANRSP_ENABLE DEFAULT_DATA_SCANRSP_ENABLE +#endif +#ifndef CFG_ADV1_DATA_ADV_PAYLOAD +#define CFG_ADV1_DATA_ADV_PAYLOAD DEFAULT_DATA_ADV_PAYLOAD +#endif +#ifndef CFG_ADV1_DATA_SCANRSP_PAYLOAD +#define CFG_ADV1_DATA_SCANRSP_PAYLOAD DEFAULT_DATA_SCANRSP_PAYLOAD +#endif + +#ifdef __cplusplus +} +#endif diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_asm/atm_asm.h b/platform/atm2/ATM22xx-x1x/lib/atm_asm/atm_asm.h new file mode 100644 index 0000000..b730fcf --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_asm/atm_asm.h @@ -0,0 +1,154 @@ +/** + ******************************************************************************* + * + * @file atm_asm.h + * + * @brief Atmosic application state machine + * + * Copyright (C) Atmosic 2020-2023 + * + ******************************************************************************* + */ + +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_PASM Application state machine + * @ingroup ATM_BTFM_PROC + * @brief ATM bluetooth framework state machine tool + * + * This module contains the necessary function of state machine tool + * + * @{ + ******************************************************************************* + */ + +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * MACROS + ******************************************************************************* + */ +#define S_OP(s, op) (s), (op) + +/* + * TYPEDEFINE + ******************************************************************************* + */ + +#ifdef CFG_ASM_BITWISE +typedef uint32_t ASM_T; +#else +typedef uint8_t ASM_T; +#endif +typedef ASM_T ASM_S; +typedef ASM_T ASM_O; + +/** + * @brief Simple handler + */ +typedef void (*state_handler)(void); + +typedef struct { + /// state code + ASM_S state; + /// operation code + ASM_O operation; + /// next state + ASM_S next_state; + /// handler function for state_operation + state_handler handler; +} state_entry; +typedef struct { + /// state code + ASM_S pre_state; + /// operation code + ASM_O operation; + /// next state + ASM_S cur_state; +} atm_asm_trans; + +/* + * GLOBAL + ******************************************************************************* + */ +/** + * @brief Insert application state machine table into framework + * @param[in] idx instance index + * @param[in] s_tbl state machine table + * @param[in] s_tbl_size size of state machine table + */ +void atm_asm_init_table(uint8_t idx, const state_entry s_tbl[], + uint8_t s_tbl_size); + +/** + * @brief Set state + * @param[in] idx instance index + * @param[in] state state value + */ +void atm_asm_set_current_state(uint8_t idx, ASM_S state); + +/** + * @brief Get current state + * @param[in] idx instance index + * @return current state + */ +ASM_S atm_asm_get_current_state(uint8_t idx); + +/** + * @brief Update current state and operation code + * @param[in] idx instance index + * @param[in] state state value + * @param[in] op_code operation code + */ +void atm_asm_set_state_op(uint8_t idx, ASM_S state, ASM_O op_code); + +/** + * @brief Get latest transition + * @param[in] idx instance index + */ +atm_asm_trans atm_asm_get_latest_transition(uint8_t idx); + +/** + * @brief Move to handler function by operation code then update state + * @param[in] idx instance index + * @param[in] next_op move state to this + */ +void atm_asm_move(uint8_t idx, ASM_O next_op); + +/** + * @brief Set callback function to debug state machine transitions. + * @param[in] idx instance index + * @param[in] cb callback function + */ +void atm_asm_reg_state_change_cb( + uint8_t idx, void (*cb)(ASM_S last_s, ASM_O op, ASM_S next_s)); + +/** + ******************************************************************************* + * @brief Get ordinal number of ASM_O or ASM_S. + * @param code Specific state code or operation code. + * @return Ordinal number of this state. + ******************************************************************************* + */ +static inline uint8_t atm_asm_ordinal(ASM_T code) +{ +#ifdef CFG_ASM_BITWISE + return co_ctz(code); +#else + return code; +#endif +} + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_PASM + + diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_co_bt_defines.h b/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_co_bt_defines.h new file mode 100644 index 0000000..63efc92 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_co_bt_defines.h @@ -0,0 +1,25 @@ +/** + ******************************************************************************* + * + * @file atm_co_bt_defines.h + * + * @brief ATM bluetooth framework common Bluetooth defines. + * + * Copyright (C) Atmosic 2021 + * + ******************************************************************************* + */ + +#pragma once + +/** + ******************************************************************************* + * @addtogroup ATM_CO_BT_DEFINES + * @brief ATM bluetooth framework common Bluetooth defines. + * @{ + ******************************************************************************* + */ + +#define ATMOSIC_COMPANY_ID 0x240A + +/// @} ATM_CO_BT_DEFINES diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_co_utils.h b/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_co_utils.h new file mode 100644 index 0000000..36dab62 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_co_utils.h @@ -0,0 +1,89 @@ +/** + ******************************************************************************* + * + * @file atm_co_utils.h + * + * @brief ATM bluetooth framework common utility functions + * + * Copyright (C) Atmosic 2020 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_CO_UTILS Common utilities + * @ingroup ATM_BTFM + * @brief ATM bluetooth framework common utility functions + * + * This module contains the common utilities functions and macros + * used by the bluetooth framework API. + * + * @{ + ******************************************************************************* + */ + +#include "gap.h" +#include "co_utils.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/// Macro to create a for-loop for a co_list +#define FOR_CO_LIST(co_list, type, hdlr) \ + for (type hdlr = (type)co_list_pick(&co_list); hdlr; \ + hdlr = (type)co_list_next(&hdlr->hdr)) \ + +/// Macro to create a for-loop for pop co_list FIFO +#define FOR_CO_LIST_POP(co_list, type, hdlr) \ + for (type hdlr = (type)co_list_pop_front(&co_list); hdlr; \ + hdlr = (type)co_list_pop_front(&co_list)) \ + +/// Redefine struct gap_bdaddr to gap_bdaddr_t +typedef struct gap_bdaddr gap_bdaddr_t; + +/** + ******************************************************************************* + * @brief Convert data type from bd_addr_t to struct bd_addr + * @param[in] addr address to convert + * + * @return addr pointer using struct bd_addr data type + ******************************************************************************* + */ +__INLINE struct bd_addr const *atm_co_convert_bd_addr_type(bd_addr_t const *addr) +{ + return (struct bd_addr const *)addr; +} + +__INLINE bool atm_co_gap_addr_compare(gap_bdaddr_t const *addr1, + gap_bdaddr_t const *addr2) +{ + return addr1 && addr2 && (addr1->addr_type == addr2->addr_type) && + co_bdaddr_compare(atm_co_convert_bd_addr_type(&addr1->addr), + atm_co_convert_bd_addr_type(&addr2->addr)); +} +/** + * @brief Covert 16 bits data endian. + */ +#define ATM_CO_BSWAP16(val16) (((val16 << 8) & 0xFF00) | ((val16 >> 8) & 0xFF)) + +__INLINE int atm_co_popcount(uint32_t x) +{ +#ifdef __GNUC__ + return __builtin_popcount(x); +#else + int i = 0; + for (; x; x &= x - 1) { + i++; + } + return i; +#endif +} + +#ifdef __cplusplus +} +#endif + +/// @} ATM_CO_UTILS diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_ke_utils.h b/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_ke_utils.h new file mode 100644 index 0000000..e316fed --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_common/atm_ke_utils.h @@ -0,0 +1,70 @@ +/** + ******************************************************************************* + * + * @file atm_ke_utils.h + * + * @brief ATM bluetooth framework common utility functions + * + * Copyright (C) Atmosic 2020-2023 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @addtogroup ATM_CO_UTILS + * @brief ATM bluetooth framework common kernel functions + * + * This module contains the common kernel functions and macros + * used by the bluetooth framework API. + * + * @{ + ******************************************************************************* + */ +#include "rwip_config.h" +#include "co_version.h" +#include <string.h> +#include "ke_mem.h" +#include "ke_msg.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + ******************************************************************************* + * @brief Allocation of a block of memory and sets allocated memory to zero + * @param[in] nitems number of elements to be allocated. + * @param[in] size Size of the memory area that need to be allocated. + * @param[in] type Type of memory block + * + * @return A pointer to the allocated memory area. + ******************************************************************************* + */ +__INLINE void *atm_ke_calloc(uint32_t nitems, uint32_t size, uint8_t type) +{ + return memset(ke_malloc(nitems * size, type), 0, nitems * size); +} + +#if (RWBT_SW_VERSION_MAJOR < 11) +#define ATM_KE_MSG_CALLOC(id, dest, src, param_str) \ + KE_MSG_ALLOC(id, dest, src, param_str) + +#define ATM_KE_MSG_CALLOC_DYN(id, dest, src, param_str, length) \ + KE_MSG_ALLOC_DYN(id, dest, src, param_str, length) +#else // (RWBT_SW_VERSION_MAJOR < 11) +#define ATM_KE_MSG_CALLOC(id, dest, src, param_str) \ + (struct param_str *)memset(KE_MSG_ALLOC(id, dest, src, param_str), 0, \ + sizeof(struct param_str)) + +#define ATM_KE_MSG_CALLOC_DYN(id, dest, src, param_str, length) \ + (struct param_str *)memset(KE_MSG_ALLOC_DYN(id, dest, src, param_str, \ + length), 0, sizeof(struct param_str) + length) +#endif // (RWBT_SW_VERSION_MAJOR < 11) + +#ifdef __cplusplus +} +#endif + +/// @} ATM_CO_UTILS diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_debug/atm_debug.h b/platform/atm2/ATM22xx-x1x/lib/atm_debug/atm_debug.h new file mode 100644 index 0000000..452ef05 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_debug/atm_debug.h @@ -0,0 +1,238 @@ +/** + ******************************************************************************* + * + * @file atm_debug.h + * + * @brief Atmosic debug utility + * + * Copyright (C) Atmosic 2020-2023 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_PDBG DEBUG utility + * @ingroup ATM_BTFM_PROC + * @brief ATM bluetooth framework debug utility + * + * This module contains the necessary function for debugging. + * + * @{ + ******************************************************************************* + */ +#include "arch.h" +#include "atm_log.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * MACRO + ******************************************************************************* + */ + +#ifdef CFG_DBG +#define ATM_DEBUG +#endif + +/// All attribute off (@see ATM_VT_SGR) +#define ATM_DE_COLOR 0 +/// Highlight Block color +#define ATM_GG_BLACK 90 +/// Highlight Red color +#define ATM_GG_RED 91 +/// Highlight Green color +#define ATM_GG_GREEN 92 +/// Highlight Brown color +#define ATM_GG_BROWN 93 +/// Highlight Grand foreground blue +#define ATM_GG_BLUE 94 +/// Highlight Magenta color +#define ATM_GG_MAGENTA 95 +/// Highlight Cyan color +#define ATM_GG_CYAN 96 +/// Highlight White color +#define ATM_GG_WHITE 97 +/// Background Block color +#define ATM_BG_BLACK 40 +/// Background Red color +#define ATM_BG_RED 41 +/// Background Green color +#define ATM_BG_GREEN 42 +/// Background Brown color +#define ATM_BG_BROWN 43 +/// Background Blue color +#define ATM_BG_BLUE 44 +/// Background Magenta color +#define ATM_BG_MAGENTA 45 +/// Background Cyan color +#define ATM_BG_CYAN 46 +/// Background White color +#define ATM_BG_WHITE 47 +/// Foreground Block color +#define ATM_FG_BLACK 30 +/// Foreground Red color +#define ATM_FG_RED 31 +/// Foreground Green color +#define ATM_FG_GREEN 32 +/// Foreground Brown color +#define ATM_FG_BROWN 33 +/// Foreground Blue color +#define ATM_FG_BLUE 34 +/// Foreground Magenta color +#define ATM_FG_MAGENTA 35 +/// Foreground Cyan color +#define ATM_FG_CYAN 36 +/// Foreground White color +#define ATM_FG_WHITE 37 + +/// cursor position +#define ATM_VT_CUP(x,y) "\x1b["#x";"#y"H" +/// cursor up +#define ATM_VT_CUU(x) "\x1B["#x"A" +/// cursor down +#define ATM_VT_CUD(x) "\x1B["#x"B" +/// cursor forward +#define ATM_VT_CUF(x) "\x1B["#x"C" +/// cursor back +#define ATM_VT_CUB(x) "\x1B["#x"D" +/// device status report +#define ATM_VT_DSR "\x1B[6n" +/// save cursor position +#define ATM_VT_SCP "\x1B[6s" +/// restore cursor position +#define ATM_VT_RSP "\x1B[6u" +/// erase display and line +#define ATM_VT_ED "\x1B[2J" +#define ATM_VT_EL "\x1B[K" +/// set graphic rendition +#define ATM_VT_SGR "\x1B[%dm" +/// set modes +#define ATM_VT_SM "\x1B[=%dh" +/// reset modes +#define ATM_VT_RM "\x1B[=%dl" +/// graphic rendition +#define ATM_VT_GR(x) "\x1B["#x"m" +/// set default +#define ATM_VT_DE_COLOR "\x1B[0m" +/// set FG Red color +#define ATM_VT_FG_RED "\x1B[31m" +/// set FG Green color +#define ATM_VT_FG_GREEN "\x1B[32m" +/// set FG CYAN color +#define ATM_VT_FG_CYAN "\x1B[36m" +/// set FG Blue color +#define ATM_VT_FG_BLUE "\x1B[34m" +/// set FG Brown color +#define ATM_VT_FG_BROWN "\x1B[33m" +/// set FG White color +#define ATM_VT_FG_WHITE "\x1B[37m" +/// set FG MAGENTA color +#define ATM_VT_FG_MAGENTA "\x1B[35m" + +#define HEADER_COLOR(color, header, str) "\x1B[" STR(color) "m" header ATM_VT_DE_COLOR ": " str +#define STR_COLOR(header, color, str) header ": " "\x1B[" STR(color) "m" str ATM_VT_DE_COLOR + +// flag of hex print +typedef enum { + // all disabled + atm_debug_hp_none = 0, + // 16 columns output. If not set, output will be 8 columns. + atm_debug_hp_16col = (1 << 0), + // Additional ASCII output enable. + atm_debug_hp_ascii_en = (1 << 1), + // Indent enabled. 8 spaces. + atm_debug_hp_indent_en = (1 << 2), +} atm_debug_hp_flag_t; + +/* + * GLOBAL + ******************************************************************************* + */ +#if (KE_PROFILING) +/** + ******************************************************************************* + * @brief Get memory usage by memory type + * @param[in] mem_type memory type + * @return Used memory size + ******************************************************************************* + */ +uint16_t atm_debug_mem_usage_by_type(enum KE_MEM_HEAP mem_type); +#endif +/** + ******************************************************************************* + * @brief Get total memory size by memory type + * @param[in] mem_type memory type + * @return Total memory size value + ******************************************************************************* + */ +uint16_t atm_debug_mem_total_by_type(enum KE_MEM_HEAP mem_type); + +#ifdef ATM_DEBUG +/** + ******************************************************************************* + * @brief Print string + * @param[in] header Header string array + * @param[in] str Content string array + ******************************************************************************* + */ +#define atm_debug_normal(header, str, ...)\ + DEBUG_TRACE(header ": " str, ##__VA_ARGS__) + +/** + ******************************************************************************* + * @brief Print header string with color + * @param[in] color Color for header + * @param[in] header Header string array + * @param[in] str Content string format + ******************************************************************************* + */ +#define atm_debug_header_color(color, header, str, ...)\ + DEBUG_TRACE(HEADER_COLOR(color, header, str), ##__VA_ARGS__) + +/** + ******************************************************************************* + * @brief Print string with color + * @param[in] color Color for str + * @param[in] header Header string array + * @param[in] str Content string array + ******************************************************************************* + */ +#define atm_debug_str_color(header, color, str, ...)\ + DEBUG_TRACE(STR_COLOR(header, color, str), ##__VA_ARGS__) + +/** + ******************************************************************************* + * @brief Print memory usage + ******************************************************************************* + */ +void atm_debug_print_memory_usage(void); + +/** + ******************************************************************************* + * @brief Print hex output + * @param[in] flag Output type + * @param[in] data Data + * @param[in] len Data length + ******************************************************************************* + */ +void atm_debug_hex_print(atm_debug_hp_flag_t flag, void const *data, + uint16_t len); + +#else +#define atm_debug_normal(header, str, ...) DEBUG_TRACE_COND(0, header ": " str, ##__VA_ARGS__) +#define atm_debug_header_color(color, header, str, ...) DEBUG_TRACE_COND(0, HEADER_COLOR(color, header, str), ##__VA_ARGS__) +#define atm_debug_str_color(header, color, str, ...) DEBUG_TRACE_COND(0, STR_COLOR(header, color, str), ##__VA_ARGS__) +#define atm_debug_print_memory_usage() +#define atm_debug_hex_print(flag, data, len) +#endif + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_PDBG + diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap.h b/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap.h new file mode 100644 index 0000000..1e03c04 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap.h @@ -0,0 +1,500 @@ +/** + ******************************************************************************* + * + * @file atm_gap.h + * + * @brief Atmosic GAP procedure + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM Atmosic Framework + * @brief ATM bluetooth framework + * + * ATM bluetooth framework components. + * + * @defgroup ATM_BTFM_PROC Framework Procedure + * @ingroup ATM_BTFM + * @brief ATM bluetooth procedure + * Procedure layer which on top of API. + * + * @defgroup ATM_BTFM_API Framework Interface(API) + * @ingroup ATM_BTFM + * @brief ATM bluetooth API layer. + * + ******************************************************************************* + */ + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_GAP GAP procedures + * @ingroup ATM_BTFM_PROC + * @brief ATM bluetooth framework GAP procedures + * + * This module contains the necessary procedure to run the platform with the ATM + * bluetooth framework API. + * + * @{ + ******************************************************************************* + */ + +#include "ble_gap.h" +#include "gapc_task.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/// constant parameter +#ifndef CFG_GAP_PARAM_CONST +#define CFG_GAP_PARAM_CONST 1 +#endif + +#if CFG_GAP_PARAM_CONST +#define __ATM_GAP_PARAM_CONST const +#else +#define __ATM_GAP_PARAM_CONST +#endif + +/// Invalid activity index +#define ATM_INVALID_ACTIDX 0xFF + +/// Indicate that a connection has been established +typedef ble_gap_ind_con_est_t atm_connect_info_t; + +/// Link states +typedef enum { + /// Link is closed. + LINK_CLOSED, + /// Link is opened but not adopted yet. + LINK_CONNECTING, + /// Link is opened + LINK_CONNECTED, + /// Link is closing + LINK_CLOSING, +} atm_link_state_t; + +/// GAP appearance. +/// Sourced from: https://specificationrefs.bluetooth.com/assigned-values/ +/// Appearance Values.pdf +typedef enum { + /// Generic Unknown + ATM_GAP_APPEARANCE_UNKNOWN = 0, + /// Generic Phone + ATM_GAP_APPEARANCE_GENERIC_PHONE = 64, + /// Generic Computer + ATM_GAP_APPEARANCE_GENERIC_COMPUTER = 128, + /// Generic Watch + ATM_GAP_APPEARANCE_GENERIC_WATCH = 192, + /// Sports Watch + ATM_GAP_APPEARANCE_WATCH_SPORTS_WATCH = 193, + /// Generic Clock + ATM_GAP_APPEARANCE_GENERIC_CLOCK = 256, + /// Generic Display + ATM_GAP_APPEARANCE_GENERIC_DISPLAY = 320, + /// Generic Remote Control + ATM_GAP_APPEARANCE_GENERIC_REMOTE_CONTROL = 384, + /// Generic Eye-glasses + ATM_GAP_APPEARANCE_GENERIC_EYE_GLASSES = 448, + /// Generic Tag + ATM_GAP_APPEARANCE_GENERIC_TAG = 512, + /// Generic Keyring + ATM_GAP_APPEARANCE_GENERIC_KEYRING = 576, + /// Generic Media Player + ATM_GAP_APPEARANCE_GENERIC_MEDIA_PLAYER = 640, + /// Generic Barcode Scanner + ATM_GAP_APPEARANCE_GENERIC_BARCODE_SCANNER = 704, + /// Generic Thermometer + ATM_GAP_APPEARANCE_GENERIC_THERMOMETER = 768, + /// Ear Thermometer + ATM_GAP_APPEARANCE_THERMOMETER_EAR = 769, + /// Generic Heart Rate Sensor + ATM_GAP_APPEARANCE_GENERIC_HEART_RATE_SENSOR = 832, + /// Heart Rate Belt + ATM_GAP_APPEARANCE_HEART_RATE_SENSOR_HEART_RATE_BELT = 833, + /// Generic Blood Pressure + ATM_GAP_APPEARANCE_GENERIC_BLOOD_PRESSURE = 896, + /// Arm Blood Pressure + ATM_GAP_APPEARANCE_BLOOD_PRESSURE_ARM = 897, + /// Wrist Blood Pressure + ATM_GAP_APPEARANCE_BLOOD_PRESSURE_WRIST = 898, + /// Generic Human Interface Device + ATM_GAP_APPEARANCE_HUMAN_INTERFACE_DEVICE = 960, + /// Keyboard + ATM_GAP_APPEARANCE_KEYBOARD = 961, + /// Mouse + ATM_GAP_APPEARANCE_MOUSE = 962, + /// Joystick + ATM_GAP_APPEARANCE_JOYSTICK = 963, + /// Gamepad + ATM_GAP_APPEARANCE_GAMEPAD = 964, + /// Digitizer Tablet + ATM_GAP_APPEARANCE_DIGITIZER_TABLET = 965, + /// Card Reader + ATM_GAP_APPEARANCE_CARD_READER = 966, + /// Digital Pen + ATM_GAP_APPEARANCE_DIGITAL_PEN = 967, + /// Barcode Scanner + ATM_GAP_APPEARANCE_BARCODE_SCANNER = 968, + /// Generic Glucose Meter + ATM_GAP_APPEARANCE_GENERIC_GLUCOSE_METER = 1024, + /// Generic Running Walking Sensor + ATM_GAP_APPEARANCE_GENERIC_RUNNING_WALKING_SENSOR = 1088, + /// In-Shoe Running Walking Sensor + ATM_GAP_APPEARANCE_RUNNING_WALKING_SENSOR_IN_SHOE = 1089, + /// On-Shoe Running Walking Sensor + ATM_GAP_APPEARANCE_RUNNING_WALKING_SENSOR_ON_SHOE = 1090, + /// On-Hip Running Walking Sensor + ATM_GAP_APPEARANCE_RUNNING_WALKING_SENSOR_ON_HIP = 1091, + /// Generic Cycling + ATM_GAP_APPEARANCE_GENERIC_CYCLING = 1152, + /// Cycling Computer + ATM_GAP_APPEARANCE_CYCLING_CYCLING_COMPUTER = 1153, + /// Speed Sensor + ATM_GAP_APPEARANCE_CYCLING_SPEED_SENSOR = 1154, + /// Cadence Sensor + ATM_GAP_APPEARANCE_CYCLING_CADENCE_SENSOR = 1155, + /// Power Sensor + ATM_GAP_APPEARANCE_CYCLING_POWER_SENSOR = 1156, + /// Speed and Cadence Sensor + ATM_GAP_APPEARANCE_CYCLING_SPEED_AND_CADENCE_SENSOR = 1157, + /// Generic Pulse Oximeter + ATM_GAP_APPEARANCE_GENERIC_PULSE_OXIMETER = 3136, + /// Fingertip Pulse Oximeter + ATM_GAP_APPEARANCE_FINGERTIP = 3137, + /// Wrist Worn Pulse Oximeter + ATM_GAP_APPEARANCE_WRIST_WORN = 3138, + /// Generic Weight Scale + ATM_GAP_APPEARANCE_GENERIC_WEIGHT_SCALE = 3200, + /// Generic Personal Mobility Device + ATM_GAP_APPEARANCE_GENERIC_PERSONAL_MOBILITY_DEVICE = 3264, + /// Powered Wheelchair + ATM_GAP_APPEARANCE_POWERED_WHEELCHAIR = 3265, + /// Mobility Scooter + ATM_GAP_APPEARANCE_MOBILITY_SCOOTER = 3266, + /// Generic Continuous Glucose Monitor + ATM_GAP_APPEARANCE_GENERIC_CONTINUOUS_GLUCOSE_MONITOR = 3328, + /// Generic Insulin Pump + ATM_GAP_APPEARANCE_GENERIC_INSULIN_PUMP = 3392, + /// nsulin Pump, durable pump + ATM_GAP_APPEARANCE_INSULIN_PUMP_DURABLE_PUMP = 3393, + /// Insulin Pump, patch pump + ATM_GAP_APPEARANCE_INSULIN_PUMP_PATCH_PUMP = 3396, + /// Insulin Pen + ATM_GAP_APPEARANCE_INSULIN_PEN = 3400, + /// Generic Medication Delivery + ATM_GAP_APPEARANCE_GENERIC_MEDICATION_DELIVERY = 3456, + /// Generic Outdoor Sports Activity + ATM_GAP_APPEARANCE_GENERIC_OUTDOOR_SPORTS_ACTIVITY = 5184, + /// Location Display + ATM_GAP_APPEARANCE_LOCATION_DISPLAY_DEVICE = 5185, + /// Location and Navigation Display + ATM_GAP_APPEARANCE_LOCATION_AND_NAVIGATION_DISPLAY_DEVICE = 5186, + /// Location Pod + ATM_GAP_APPEARANCE_LOCATION_POD = 5187, + /// Location and Navigation Pod + ATM_GAP_APPEARANCE_LOCATION_AND_NAVIGATION_POD = 5188 +} atm_gap_appearance_t; + +/// ATM command complete callback prototype +/// @param[in] conidx Connection index. +/// @param[in] status Command status. +typedef void (*atm_gap_cmd_cb)(uint8_t conidx, ble_err_code_t status); + +/// GAP initialization parameters +typedef struct { + /// Device name string array. If NVDS exist, it will be overwritten. + uint8_t __ATM_GAP_PARAM_CONST *dev_name; + /// Length of dev_name. + uint8_t dev_name_len; + /// Device name array size. + uint8_t dev_name_max; + /// Device Appearance Icon + atm_gap_appearance_t appearance; + /// Slave preferred connection parameters + ble_gap_periph_pref_t periph_pref_params; + /// IRK assignment from application. Null if not used. + uint8_t const *app_irk; + /// Set device configuration + ble_gap_set_dev_config_t __ATM_GAP_PARAM_CONST *dev_config; + /// Own static random address + ble_bdaddr_t addr; +} atm_gap_param_t; + +/// GAP application callbacks +typedef struct { + /// Advertising report indication. + /// @param[in] ind Report information. + void (*ext_adv_ind)(ble_gap_ind_ext_adv_report_t const *ind); + /// Connection indication + /// @brief This function will be called after ACL connected. + /// Application should call @ref atm_gap_connect_accept or + /// @ref atm_gap_disconnect. + /// @param[in] conidx Connection index. + /// @param[in] param Connection parameters. + void (*conn_ind)(uint8_t conidx, atm_connect_info_t *param); + /// Disconnection indication + /// @brief This function will be called after ACL disconnected. + /// @param[in] conidx Connection index. + /// @param[in] param Disconnection information. + void (*disc_ind)(uint8_t conidx, ble_gap_ind_discon_t const *param); + /// Pairing request indication + /// @brief Pairing information request by pairing procedure. + /// After receiving this, application should call + /// @ref ble_gap_sec_pairing_rsp. + /// @param[in] conidx Connection index. + /// @param[in] auth_req Authentication request. + void (*pair_req_ind)(uint8_t conidx, enum gap_auth auth_req); + /// Security request indication + /// @brief Security request by slave. + /// @param[in] conidx Connection index. + /// @param[in] auth_req Authentication request. + void (*sec_req_ind)(uint8_t conidx, enum gap_auth auth_req); + /// Passkey indication + /// @brief Indication of passkey display or passkey input + /// After received this indication, application shall show number to user + /// (GAP_TK_DISPLAY) + /// or get input(GAP_TK_KEY_ENTRY) from user and call + /// @ref ble_gap_sec_passkey_rsp. + /// @param[in] type Decide display(GAP_TK_DISPLAY) or + /// input(GAP_TK_KEY_ENTRY, GAP_TK_KEY_OOB) + /// @param[in] conidx Connection index. + /// @param[in] passkey Random number generated by lower layer. It is used + /// when type is @ref GAP_TK_DISPLAY. + /// Application must show it to user and pass it as argument of + /// ble_gap_sec_passkey_rsp. If application want + /// to display with other number, then argument to ble_gap_sec_passkey_rsp + /// have to change as well. + void (*pair_passkey_ind)(enum gap_tk_type type, uint8_t conidx, + uint32_t passkey); + /// Numeric comparison indication + /// @brief Indication of numeric comparison received. + /// @param[in] conidx Connection index. + /// @param[in] number Number generated between pairing. This shall be + /// shown to user. + void (*pair_numeric_ind)(uint8_t conidx, uint32_t number); + /// Pairing result indication + /// @brief This function will be called after pairing finished. + /// @param[in] conidx Connection index. + /// @param[in] param Bonding information. + /// @note Link is encrypted when pairing is success. + void (*pair_ind)(uint8_t conidx, ble_gap_ind_le_pair_end_t const *param); + /// Connection parameter updated indication + /// @brief This function will be called when connection parameter changed + /// @param[in] conidx Connection index. + void (*conn_param_updated_ind)(uint8_t conidx); + /// Initialization confirmation. + /// @brief This function will be called when @ref atm_gap_start finished. + /// @param[in] status Status for @ref atm_gap_start. + void (*init_cfm)(ble_err_code_t status); + /// PHY update indication. + /// @brief This function will be called after PHY update. + /// @param[in] conidx Connection index. + /// @param[in] param PHY update information. + void (*phy_ind)(uint8_t conidx, ble_gap_le_phy_t const *param); + /// Encryption indication + /// @brief This function will be called after link encrypted. + /// It is invoked during reconnection if encryption is enabled. + /// @param[in] conidx Connection task index. + /// @param[in] param Encryption information. + /// @note This will not be called when link is enctyped at the time of + /// pairing. + void (*enc_ind)(uint8_t conidx, ble_gap_ind_encrypt_t const *param); + /// Link information indication + /// @brief This function will be called after @ref atm_gap_get_link_info + /// execution finished or information had been updated. + /// @param[in] status Information status. + /// @param[in] param Information data. + void (*linfo_ind)(ble_err_code_t status, ble_gap_link_info_t const *param); + /// Periodic sync established indication. + /// @param[in] ind sync established information. + void (*sync_est_ind)(struct gapm_sync_established_ind const *ind); + /// LE Set Data Length Indication + /// @param[in] conidx Connection index. + /// @param[in] ind Size of LE data packets negotiated over BLE Link. + void (*le_pkt_size_ind)(uint8_t conidx, ble_gap_ind_pkt_size_t const *ind); +} atm_gap_cbs_t; + +/// Parameter for connection parameter update process +typedef struct { + /// Confirmation of connection parameter update process + /// @param[in] conidx Connection index. + /// @param[in] status Status of result. + void (*param_nego_cfm)(uint8_t conidx, ble_err_code_t status); + /// force retry retry_times no master error happen + bool force_retry; + /// Max retries for connection parameter update. + uint8_t retry_times; + /// Time(centisec) for checking the parameter result. + uint16_t check_result; + /// Time(centisec) for delay start after calling atm_gap_connect_param_nego. + /// @note this setting could prevent chance of the LLCP collision + uint16_t delay; + /// Preferred connection parameter. + ble_gap_conn_param_t const *target; +} atm_gap_param_nego_t; + +/// Parameter for setting PHY. +typedef struct { + /// Callback of setting PHY. + atm_gap_cmd_cb cb; + /// TX PHYs. + ble_gap_phy_mode_t tx_phy; + /// RX PHYs. + ble_gap_phy_mode_t rx_phy; + /// PHY_Options. + ble_gap_phy_opt_t phy_opt; +} atm_gap_param_set_phy_t; +/* + * GLOBAL FUNCTION + * ***************************************************************************** + */ + +/** + ******************************************************************************* + * @brief Register profile + * @param[in] name String of profile identification. + * @param[in] parm Configured parameter of profile + ******************************************************************************* + */ +__NONNULL(1) +void atm_gap_prf_reg(char const *name, void const *parm); + +/** + ******************************************************************************* + * @brief Initialization BT system + * @param[in] init Parameter for initialization. + * @param[in] cbs Callbacks of application. + ******************************************************************************* + */ +__NONNULL_ALL +void atm_gap_start(atm_gap_param_t __ATM_GAP_PARAM_CONST *init, atm_gap_cbs_t const *cbs); + +/** + ******************************************************************************* + * @brief Get local name + * @param[out] len Length of name. + * @return Local name string. + ******************************************************************************* + */ +__NONNULL_ALL +uint8_t __ATM_GAP_PARAM_CONST *atm_gap_get_local_name(uint8_t *len); + +/** + ******************************************************************************* + * @brief Accept incoming connection. + * @param[in] conidx Connection index + ******************************************************************************* + */ +void atm_gap_connect_accept(uint8_t conidx); + +/** + ******************************************************************************* + * @brief Get connection parameters + * @param[in] conidx Connection index. + * @return Connection parameter information. + ******************************************************************************* + */ +atm_connect_info_t *atm_gap_get_connect_info(uint8_t conidx); + +/** + ******************************************************************************* + * @brief Get link state + * @param[in] conidx Connection index. + * @return Link state information. + ******************************************************************************* + */ +atm_link_state_t atm_gap_get_connect_state(uint8_t conidx); + +/** + ******************************************************************************* + * @brief Launch process of requesting new connection parameter + * @param[in] conidx Connection index. + * @param[in] param Parameter for process. + * @return True if it is allowed. Otherwise means an existing operation + * kis ongoing. + ******************************************************************************* + */ +__NONNULL(2) +bool atm_gap_connect_param_nego(uint8_t conidx, atm_gap_param_nego_t const *param); + +/** + ******************************************************************************* + * @brief Disconnect the link + * @param[in] conidx Connection index. + * @param[in] reason Reason for disconnection. Only allow error code of + * BLE_HCI_MODULE. + ******************************************************************************* + */ +void atm_gap_disconnect(uint8_t conidx, ble_err_code_t reason); + +/** + ******************************************************************************* + * @brief Disconnect all links + ******************************************************************************* + */ +void atm_gap_disconnect_all(void); + +/** + ******************************************************************************* + * @brief Set LE phy. mode + * @param[in] conidx Connection index. + * @param[in] param Parameter for setting. + ******************************************************************************* + */ +__NONNULL(2) +void atm_gap_set_phy(uint8_t conidx, atm_gap_param_set_phy_t const *param); + +/** + ******************************************************************************* + * @brief Get link information from peer + * @param[in] conidx Connection index. + * @param[in] type Information to get. + ******************************************************************************* + */ +void atm_gap_get_link_info(uint8_t conidx, ble_gap_link_info_type_t type); + +/** + ******************************************************************************* + * @brief Generate random address + * @param[in] addr_type Random address type + ******************************************************************************* + */ +void atm_gap_gen_rand_addr(ble_gapm_rand_addr_type_t addr_type); + +/** + ******************************************************************************* + * @brief Lower slave latency locally + * @param[in] conidx Connection index. + * @param[in] latency Target local latency. (in number of connection events) + ******************************************************************************* + */ +void atm_gap_lower_slave_latency_locally(uint8_t conidx, uint16_t latency); + +/** + ******************************************************************************* + * @brief Get random address + * @return Rand address + ******************************************************************************* + */ +ble_bdaddr_t __ATM_GAP_PARAM_CONST *atm_gap_get_rand_addr(void); + +/** + ******************************************************************************* + * @brief Print connection parameter + * @param[in] info Connection parameter information + ******************************************************************************* + */ +__NONNULL_ALL +void atm_gap_print_conn_param(atm_connect_info_t *info); + +#ifdef __cplusplus +} +#endif + +/// @} ATM_BTFM_GAP diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap_param.h b/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap_param.h new file mode 100644 index 0000000..b811124 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap_param.h @@ -0,0 +1,75 @@ +/** + ******************************************************************************* + * + * @file atm_gap_param.h + * + * @brief Header file - ATM bluetooth framework GAP parameters + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +#include "atm_log.h" +#include "atm_gap.h" + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_GAPP GAP parameters + * @ingroup ATM_BTFM_PROC + * @brief ATM bluetooth framework GAP parameters + * + * This module contains the necessary function to provide predefined parameters + * for usage of + * @ref atm_gap_start. + * + * @{ + ******************************************************************************* + */ + +// overwritten by application +#ifdef GAP_PARM_NAME +#include STR(GAP_PARM_NAME) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef CFG_GAP_MAX_LINKS +#define CFG_GAP_MAX_LINKS 1 +#endif + +// Max no. of LECB connections +#ifndef CFG_GAP_MAX_LECB +#define CFG_GAP_MAX_LECB 1 +#endif + +#ifndef CFG_GAP_TEST_TX_PWR_LVL +#define CFG_GAP_TEST_TX_PWR_LVL BLE_GAP_MAX_TEST_TX_PWR_LVL +#endif + +// Minimum length of connection event in 0.625 ms +#ifndef CFG_GAP_PARAM_UPDATE_CE_MIN +#define CFG_GAP_PARAM_UPDATE_CE_MIN 0 +#endif + +// Maximum length of connection event in 0.625 ms +#ifndef CFG_GAP_PARAM_UPDATE_CE_MAX +#define CFG_GAP_PARAM_UPDATE_CE_MAX 0 +#endif + +/** + ******************************************************************************* + * @brief Get persistent GAP start parameter. + * @return Pointer of GAP start parameter + ******************************************************************************* + */ +__ATM_GAP_PARAM_CONST atm_gap_param_t *atm_gap_param_get(void); + +#ifdef __cplusplus +} +#endif + +/// @} ATM_BTFM_GAPP diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap_param_internal.h b/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap_param_internal.h new file mode 100644 index 0000000..3e4bf04 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_gap/atm_gap_param_internal.h @@ -0,0 +1,123 @@ +/** + ******************************************************************************* + * + * @file atm_gap_param_internal.h + * + * @brief Header file - ATM bluetooth framework internal GAP parameters + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +#include "gapc_task.h" +#include "gapm.h" +#include "atm_gap.h" +#include "atm_gap_param.h" +#include "l2cc_pdu.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// -------------- GAP Basic Configure ----------------------- + +/// device name setting +#ifndef CFG_GAP_DEV_NAME +#define CFG_GAP_DEV_NAME "Atmosic BT" +#endif +#ifndef CFG_GAP_DNAME_MAX_LEN +#define CFG_GAP_DNAME_MAX_LEN 18 +#endif +#ifndef CFG_GAP_APP_IRK +#define CFG_GAP_APP_IRK NULL +#endif +/// Central, Peripheral, Observer, Broadcaster or All roles. (@see enum gap_role) +#ifndef CFG_GAP_ROLE +#define CFG_GAP_ROLE BLE_GAP_ROLE_PERIPHERAL +#endif +/// Appearance Icon +#ifndef CFG_GAP_APPEARANCE +// (@see atm_gap_appearance_t) +#define CFG_GAP_APPEARANCE ATM_GAP_APPEARANCE_UNKNOWN +#endif +#ifndef CFG_GAP_CONN_INT_MIN +#define CFG_GAP_CONN_INT_MIN 9 // in 1.25 ms +#endif +#ifndef CFG_GAP_CONN_INT_MAX +#define CFG_GAP_CONN_INT_MAX 9 // in 1.25 ms +#endif +#ifndef CFG_GAP_PERIPH_LATENCY +#define CFG_GAP_PERIPH_LATENCY 29 // in number of connection events +#endif +#ifndef CFG_GAP_CONN_TIMEOUT +#define CFG_GAP_CONN_TIMEOUT 500 // in unit of 10ms. Range: 100ms - 32s +#endif + +// -------------- Privacy Config ----------------------- +#ifndef CFG_RENEW_DURATION +#define CFG_RENEW_DURATION RPA_TO_DFT +#endif + +#ifndef CFG_GAP_PRIVACY_CFG +#define CFG_GAP_PRIVACY_CFG 0 +#endif +// -------------- Security Config ----------------------- +/// Pairing mode authorized (@see enum gapm_pairing_mode) +#ifndef CFG_GAP_PAIRING_MODE +#define CFG_GAP_PAIRING_MODE BLE_GAP_PAIRING_LEGACY +#endif + +// -------------- LE Data Length Extension -------------- +#ifndef CFG_GAP_MAX_TX_OCTETS +#define CFG_GAP_MAX_TX_OCTETS BLE_MAX_OCTETS +#endif +#ifndef CFG_GAP_MAX_TX_TIME +#define CFG_GAP_MAX_TX_TIME LE_MAX_TIME +#endif + +// --------------- L2CAP Configuration ------------------ +#ifndef CFG_GAP_MAX_LL_MTU +#define CFG_GAP_MAX_LL_MTU 160 +#endif +#ifndef CFG_GAP_MAX_LL_MPS +#define CFG_GAP_MAX_LL_MPS L2C_MIN_LE_MTUSIG +#endif + +// -------------- ATT Database Configure ---------------- +/// Attribute database configuration (@see enum gapm_att_cfg_flag) +#ifndef CFG_GAP_ATT_CFG +#define CFG_GAP_ATT_CFG 0 +#endif + +// --------Application assign static random address ------ +// CFG_GAP_OWN_STATIC_RANDOM_ADDR5 must follow the spec. that +// bit 6 and bit 7 must be 1 +#ifndef CFG_GAP_OWN_STATIC_RANDOM_ADDR5 +#define CFG_GAP_OWN_STATIC_RANDOM_ADDR5 0x00 +#endif + +#ifndef CFG_GAP_OWN_STATIC_RANDOM_ADDR4 +#define CFG_GAP_OWN_STATIC_RANDOM_ADDR4 0x00 +#endif + +#ifndef CFG_GAP_OWN_STATIC_RANDOM_ADDR3 +#define CFG_GAP_OWN_STATIC_RANDOM_ADDR3 0x00 +#endif + +#ifndef CFG_GAP_OWN_STATIC_RANDOM_ADDR2 +#define CFG_GAP_OWN_STATIC_RANDOM_ADDR2 0x00 +#endif + +#ifndef CFG_GAP_OWN_STATIC_RANDOM_ADDR1 +#define CFG_GAP_OWN_STATIC_RANDOM_ADDR1 0x00 +#endif + +#ifndef CFG_GAP_OWN_STATIC_RANDOM_ADDR0 +#define CFG_GAP_OWN_STATIC_RANDOM_ADDR0 0x00 +#endif + +#ifdef __cplusplus +} +#endif diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_log/atm_log.h b/platform/atm2/ATM22xx-x1x/lib/atm_log/atm_log.h new file mode 100644 index 0000000..eb541e0 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_log/atm_log.h @@ -0,0 +1,166 @@ +/** + ******************************************************************************* + * + * @file atm_log.h + * + * @brief Atmosic Log Facility + * + * Copyright (C) Atmosic 2020 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_LOG LOG utility + * @ingroup ATM_BTFM_PROC + * @brief ATM bluetooth framework log utility + * + * This module contains the macro for log. + * + * @{ + ******************************************************************************* + */ + +/* + * INCLUDES + * ***************************************************************************** + */ +#include "arch.h" + +#ifdef RTT_DBG +#include "SEGGER_RTT.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * MACRO + ******************************************************************************* + */ + +/// string +#define D_(d) #d +#define STR(d) D_(d) + +/// set default +#define ATM_LOG_DECOLOR "\x1B[0m" +/// set GG Red color +#define ATM_LOG_GRED "\x1B[91m" +/// set GG Green color +#define ATM_LOG_GGREEN "\x1B[92m" +/// set GG CYAN color +#define ATM_LOG_GCYAN "\x1B[96m" +/// set GG Blue color +#define ATM_LOG_GGBLUE "\x1B[94m" +/// set GG Brown color +#define ATM_LOG_GBROWN "\x1B[93m" +/// set GG White color +#define ATM_LOG_GWHITE "\x1B[97m" +/// set GG MAGENTA color +#define ATM_LOG_GMAGENTA "\x1B[95m" +/// set FG Red color +#define ATM_LOG_FRED "\x1B[31m" +/// set FG Green color +#define ATM_LOG_FGREEN "\x1B[32m" +/// set FG CYAN color +#define ATM_LOG_FCYAN "\x1B[36m" +/// set FG Blue color +#define ATM_LOG_FBLUE "\x1B[34m" +/// set FG Brown color +#define ATM_LOG_FBROWN "\x1B[33m" +/// set FG White color +#define ATM_LOG_FWHITE "\x1B[37m" +/// set FG MAGENTA color +#define ATM_LOG_FMAGENTA "\x1B[35m" + +/// LOG ERROR mask +#define ATM_LOG_E_MASK ((1 << 1) - 1) +/// LOG WARNING mask +#define ATM_LOG_W_MASK ((1 << 2) - 1) +/// LOG DEBUG mask +#define ATM_LOG_D_MASK ((1 << 3) - 1) +/// LOG NOTIFY mask +#define ATM_LOG_N_MASK ((1 << 4) - 1) +/// LOG VERBOSE mask +#define ATM_LOG_V_MASK ((1 << 5) - 1) + +#ifdef RTT_DBG +/// LOG ERROR color +#define ATM_LOG_E_C RTT_CTRL_TEXT_BRIGHT_RED +/// LOG WARNING color +#define ATM_LOG_W_C RTT_CTRL_TEXT_BRIGHT_MAGENTA +/// LOG NOTIFY color +#define ATM_LOG_N_C RTT_CTRL_TEXT_BRIGHT_BLUE +/// LOG DEBUG color +#define ATM_LOG_D_C RTT_CTRL_TEXT_BRIGHT_GREEN +/// LOG VERBOSE color +#define ATM_LOG_V_C RTT_CTRL_TEXT_BRIGHT_WHITE +#else +/// LOG ERROR color +#define ATM_LOG_E_C ATM_LOG_GRED +/// LOG WARNING color +#define ATM_LOG_W_C ATM_LOG_GMAGENTA +/// LOG NOTIFY color +#define ATM_LOG_N_C ATM_LOG_GGBLUE +/// LOG DEBUG color +#define ATM_LOG_D_C ATM_LOG_GGREEN +/// LOG VERBOSE color +#define ATM_LOG_V_C ATM_LOG_GWHITE +#endif + +/// Global debug log level +/// @note user could use -DATMLOG_GLOBAL_LEVEL=\<lvl\> to disable log below the +/// \<lvl\>. The \<lvl\> would be ATM_LOG_[E|W|N|D|V]_MASK. +#ifndef ATM_LOG_GLOBAL_LEVEL +#define ATM_LOG_GLOBAL_LEVEL ATM_LOG_V_MASK +#endif + +#if PLF_DEBUG +/// Module debug log setting +#define ATM_LOG_LOCAL_SETTING(name, level)\ +static const char M_NAME[] = {name};\ +static const uint32_t M_MASK = ATM_LOG_ ## level ## _MASK + +/// Module debug log setting dynamic level +#define ATM_LOG_LOCAL_SETTING_DYN_LV(name, level)\ +static char M_NAME[] = {name};\ +static uint32_t M_MASK = ATM_LOG_ ## level ## _MASK + +/// Module debug log +/// @note The module name(M_NAME) and module mask(M_MASK) needs to be defined +/// in source file. For example, #define M_NAME "adv". #define M_MASK V +#define ATM_LOG(MSK, fmt, ...) do {\ + if ((M_MASK & ATM_LOG_ ## MSK ## _MASK & ATM_LOG_GLOBAL_LEVEL) == \ + ATM_LOG_ ## MSK ## _MASK) {\ + DEBUG_TRACE("[%10.10s][" ATM_LOG_ ## MSK ## _C #MSK ATM_LOG_DECOLOR "]: " fmt ,\ + M_NAME, ##__VA_ARGS__);\ + }} while(false) +#else +#define ATM_LOG_LOCAL_SETTING(name, level) +#define ATM_LOG_LOCAL_SETTING_DYN_LV(name, level) +#define ATM_LOG(MSK, fmt, ...) do {\ + DEBUG_TRACE_COND(0, fmt, ##__VA_ARGS__);\ +} while (false) +#endif //PLF_DEBUG + +#ifdef AUTO_TEST +/// Negative unit test +#define ATM_LOG_NEGUT(fmt, ...) do { \ + DEBUG_TRACE("[NEGUT]: " fmt, ##__VA_ARGS__); \ +} while (0) +#else +#define ATM_LOG_NEGUT(fmt, ...) do {\ + DEBUG_TRACE_COND(0, fmt, ##__VA_ARGS__);\ +} while (0) +#endif + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_LOG + diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_prfs/atm_prfs.h b/platform/atm2/ATM22xx-x1x/lib/atm_prfs/atm_prfs.h new file mode 100644 index 0000000..1174519 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_prfs/atm_prfs.h @@ -0,0 +1,59 @@ +/** + ******************************************************************************* + * + * @file atm_prfs.h + * + * @brief Atmosic Profile Server + * + * Copyright (C) Atmosic 2020 + * + * + ******************************************************************************* + */ +#pragma once + +/** +******************************************************************************** + * @addtogroup ATM_PRFS Atmosic Profile Server + * @ingroup ATM_PRF + * @brief Atmosic Profile Server + * @{ +******************************************************************************** + */ + +/* + * INCLUDE FILE + ******************************************************************************* + */ +#include "prf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/// Max number of server task instances +#define ATM_PRFS_IDX_MAX (1) + +///The atm_prfs environment variable +struct atm_prfs_env_tag { + /// Profile environment + prf_env_t prf_env; + /// Extra context + void const *ctx; +}; + +/** + ******************************************************************************* + * @brief Retrieve Atmosic profile interface + * + * @return Atmosic profile interface + ******************************************************************************* + */ +const struct prf_task_cbs *atm_prfs_itf_get(void); + +#ifdef __cplusplus +} +#endif + +/// @} ATM_PRFS + diff --git a/platform/atm2/ATM22xx-x1x/lib/atm_prfs/atm_prfs_task.h b/platform/atm2/ATM22xx-x1x/lib/atm_prfs/atm_prfs_task.h new file mode 100644 index 0000000..b2ee146 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/atm_prfs/atm_prfs_task.h @@ -0,0 +1,79 @@ +/** + ******************************************************************************* + * + * @file atm_prfs_task.h + * + * @brief Atmosic Profile Server Task + * + * Copyright (C) Atmosic 2020 + * + * + ******************************************************************************* + */ +#pragma once + +/** +******************************************************************************** + * @addtogroup ATM_PRFSTASK Atmosic Profile Server Task + * @ingroup ATM_PRFS + * @brief Atmosic Profile Server Task + * @{ +******************************************************************************** + */ + +/* + * INCLUDE FILES + ******************************************************************************* + */ +#include "ke_task.h" +#include "gattc_task.h" +#include "atm_prfs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * TYPE DEFINITIONS + ******************************************************************************* + */ +/// Messages for Atmosic profile server (atm_prfs) +enum atm_prfs_msg_id { + /// Redirect the GATTC_ATT_INFO_REQ_IND message to upper layer + ATM_PRFS_GATTC_ATT_INFO_REQ_IND = + TASK_BUILD(TASK_TYPE_GET(GATTC_ATT_INFO_REQ_IND), TASK_ID_ATMPRFS), + /// Redirect the GATTC_WRITE_REQ_IND message to upper layer + ATM_PRFS_GATTC_WRITE_REQ_IND = + TASK_BUILD(TASK_TYPE_GET(GATTC_WRITE_REQ_IND), TASK_ID_ATMPRFS), + /// Redirect the GATTC_READ_REQ_IND message to upper layer + ATM_PRFS_GATTC_READ_REQ_IND = + TASK_BUILD(TASK_TYPE_GET(GATTC_READ_REQ_IND), TASK_ID_ATMPRFS), + /// Redirect the GATTC_CMP_EVT message to upper layer + ATM_PRFS_GATTC_CMP_EVT = + TASK_BUILD(TASK_TYPE_GET(GATTC_CMP_EVT), TASK_ID_ATMPRFS), +}; + +/** + ******************************************************************************* + * @brief Initialize atm_prfs task handlers + * + * @param[out] task_desc Task descriptor to fill + ******************************************************************************* + */ +void atm_prfs_task_init(struct ke_task_desc *task_desc); + +/** + ******************************************************************************* + * @brief Retrieve profile task after profile creating + * + * @return registed profile task + ******************************************************************************* + */ +uint16_t atm_prfs_get_profile_task(void); + +#ifdef __cplusplus +} +#endif + +/// @} ATM_PRFSTASK + diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_atmprfs/ble_atmprfs.h b/platform/atm2/ATM22xx-x1x/lib/ble_atmprfs/ble_atmprfs.h new file mode 100644 index 0000000..4cc7522 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_atmprfs/ble_atmprfs.h @@ -0,0 +1,347 @@ +/** + ******************************************************************************* + * + * @file ble_atmprfs.h + * + * @brief Atmosic Profile Server Middleware + * + * Copyright (C) Atmosic 2020 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_ATMPRFS Atmosic profile server API + * @ingroup ATM_BTFM_API + * @brief ATM bluetooth framework atmprfs API + * + * This module contains the necessary API to create service through Atmosic + * profile server + * + * @{ + ******************************************************************************* + */ + +/* + * INCLUDE + ******************************************************************************* + */ +#include "atm_prfs_task.h" +#include "gattc.h" +#include "ble_prf_itf.h" +#include "ble_gattc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * DEFINE + ******************************************************************************* + */ +/// The module name to register to SDK framework +#define BLE_ATMPRFS_MODULE_NAME "ble_atmprfs" +/// Invalid ATT index +#define BLE_ATMPRFS_INVALID_ATTIDX 0xFF +/// The maximum serial number of notification or indication +#define BLE_ATMPRFS_NTF_IND_MAX_SN 0xFFFF + +/// The element type in crateing profile +typedef enum { + /// Service + BLE_ATMPRFS_ATT_SVC, + /// Characteristic + BLE_ATMPRFS_ATT_CHAR, + /// The descriptor base index + BLE_ATMPRFS_ATT_DESC_BASE = ATT_DESC_CHAR_EXT_PROPERTIES, + /// CEOD (0x2900) + BLE_ATMPRFS_ATT_CHAR_EXT_PROP = BLE_ATMPRFS_ATT_DESC_BASE, + /// CUDD (0x2901) + BLE_ATMPRFS_ATT_CHAR_USR_DESC, + /// CCCD (0x2902) + BLE_ATMPRFS_ATT_CLI_CHAR_CFG, + /// SCCD (0x2903) + BLE_ATMPRFS_ATT_SERVER_CHAR_CFG, + /// SCCD (0x2904) + BLE_ATMPRFS_ATT_CHAR_PRES_FOMT, + /// CAFD (0x2905) + BLE_ATMPRFS_ATT_CHAR_AGGR_FOMT, + /// The user defined descriptor (128 bit) + BLE_ATMPRFS_ATT_USER_DESC +} ble_atmprfs_att_type_t; + +typedef const ble_prf_cbs_t *(ble_atmprfs_get_cbs_t)(void const *param); + +/* + * STRUCTURE DEFINITION + ****************************************************************************** + */ + +/// GATTC read request indicate +typedef uint8_t (*ble_atmprfs_gattc_read_req_ind_t)(uint8_t conidx, + uint8_t att_idx); +/// GATTC write request indicate +typedef uint8_t (*ble_atmprfs_gattc_write_req_ind_t)(uint8_t conidx, + uint8_t att_idx, uint8_t const *data, uint16_t size); +/// GATTC write confirmation indicate +typedef void (*ble_atmprfs_gattc_write_cfm_ind_t)(uint8_t conidx, + uint8_t att_idx); +/// GATTC ATT information request indicate +typedef uint8_t (*ble_atmprfs_gattc_att_info_req_ind_t)(uint8_t conidx, + uint8_t att_idx, uint16_t *length); + +/// The callback function for ble_atmprfs +typedef struct { + /// GATTC read request indicate + ble_atmprfs_gattc_read_req_ind_t read_req; + /// GATTC write request indicate + ble_atmprfs_gattc_write_req_ind_t write_req; + /// GATTC write confirmation indicate + ble_atmprfs_gattc_write_cfm_ind_t write_cfm; + /// GATTC ATT information request indicate + ble_atmprfs_gattc_att_info_req_ind_t att_info_req; +} ble_atmprfs_cbs_t; + +/* + * FUNCTION DECLARATION + ****************************************************************************** + */ + +/** + ******************************************************************************* + * @brief Add service + * + * @param[in] uuid Service UUID (128 bit) + * @param[in] sec_prop Security property (ble_att.h) + * @param[in] cb The callback function of register service + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_svc(const uint8_t uuid[ATT_UUID_128_LEN], + uint16_t sec_prop, ble_atmprfs_cbs_t const *cb); + +/** + ******************************************************************************* + * @brief Add 16-bit UUID service + * + * @param[in] uuid Service UUID (16 bit) + * @param[in] sec_prop Security property (ble_att.h) + * @param[in] cb The callback function of register service + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_svc_16(uint16_t uuid, uint16_t sec_prop, + ble_atmprfs_cbs_t const *cb); + +/** + ******************************************************************************* + * @brief Add characteristic + * + * @param[in] uuid Characteristic UUID (128 bit) + * @param[in] perm Permission (ble_att.h) + * @param[in] max_size The maximum size of characteristic + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_char(const uint8_t uuid[ATT_UUID_128_LEN], uint16_t perm, + uint16_t max_size); + +/** + ******************************************************************************* + * @brief Add 16-bit UUID characteristic + * + * @param[in] uuid Characteristic UUID (16 bit) + * @param[in] perm Permission (ble_att.h) + * @param[in] max_size The maximum size of characteristic + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_char_16(uint16_t uuid, uint16_t perm, + uint16_t max_size); + +/** + ******************************************************************************* + * @brief Add characteristic extended properties descriptor (0x2900) + * + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_char_ext_prop(void); + +/** + ******************************************************************************* + * @brief Add characteristic user description descriptor (0x2901) + * + * @param[in] perm Permission (ble_att.h) + * @param[in] max_size The maximum size of descriptor + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_char_user_desc(uint16_t perm, uint16_t max_size); + +/** + ******************************************************************************* + * @brief Add client characteristic configuration descriptor (0x2902) + * + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_client_char_cfg(void); + +/** + ******************************************************************************* + * @brief Add server characteristic configuration descriptor (0x2903) + * + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_server_char_cfg(void); + +/** + ******************************************************************************* + * @brief Add characteristic presentation format descriptor (0x2904) + * + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_char_pres_format(void); + +/** + ******************************************************************************* + * @brief Add characteristic aggregate format descriptor (0x2905) + * + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_char_aggr_format(uint16_t max_size); + +/** + ******************************************************************************* + * @brief Add user defined descriptor + * + * @param[in] uuid Descriptor UUID (128 bit) + * @param[in] perm Permission (ble_att.h) + * @param[in] max_size The maximum size of descriptor + * @return ATT index. Return BLE_ATMPRFS_INVALID_ATTIDX on failure + ******************************************************************************* + */ +uint8_t ble_atmprfs_add_desc_user_defined(const uint8_t uuid[ATT_UUID_128_LEN], + uint16_t perm, uint16_t max_size); + +/** + ******************************************************************************* + * @brief Active the service database in queue + * + * @return Success: ATT_ERR_NO_ERROR + ******************************************************************************* + */ +uint8_t ble_atmprfs_active_svc_db(void); + +/** + ******************************************************************************* + * @brief Apply data to ATT server when get GATTC read request + * + * @param[in] conidx Connection index + * @param[in] att_idx ATT index + * @param[out] data The data will send to remote + * @param[in] length The data length + * @return Success: ATT_ERR_NO_ERROR + ******************************************************************************* + */ +uint8_t ble_atmprfs_gattc_read_cfm(uint8_t conidx, uint8_t att_idx, + uint8_t const *data, uint16_t length); + +/** + ******************************************************************************* + * @brief Send notification to ATT client + * + * @param[in] conidx Connection index + * @param[in] att_idx ATT index + * @param[out] data The data will send to remote + * @param[in] length The data length + * @param[in] cb The complete event after sending notification + * @return Success: ATT_ERR_NO_ERROR + ******************************************************************************* + */ +uint8_t ble_atmprfs_gattc_send_ntf(uint8_t conidx, uint8_t att_idx, + uint8_t const *data, uint16_t length, ble_gattc_cmpl_cb_t cb); + +/** + ******************************************************************************* + * @brief Send indication to ATT client + * + * @param[in] conidx Connection index + * @param[in] att_idx ATT index + * @param[out] data The data will send to remote + * @param[in] length The data length + * @param[in] cb The complete event after sending indication + * @return Success: ATT_ERR_NO_ERROR + ******************************************************************************* + */ +uint8_t ble_atmprfs_gattc_send_ind(uint8_t conidx, uint8_t att_idx, + uint8_t const *data, uint16_t length, ble_gattc_cmpl_cb_t cb); + +/** + ******************************************************************************* + * @brief Retrieve the last sequence number after sending notification or + * indication + * + * @param[in] conidx Connection index + * @return sequence number + ******************************************************************************* + */ +uint16_t ble_atmprfs_get_ni_last_seq_num(uint8_t conidx); + +/** + ******************************************************************************* + * @brief Retrieve ATT handle by index + * + * @param[in] att_idx The ATT index in profile creating. + * @return ATT handle. Return ATT_INVALID_HDL if handle not exist. + ******************************************************************************* + */ +uint16_t ble_atmprfs_get_att_handle(uint8_t att_idx); + +/** + ******************************************************************************* + * @brief Get ATT index by ATT handle + * + * @param[in] handle The ATT handle + * @param[out] att_idx ATT index to fill + * @return Success: ATT_ERR_NO_ERROR, Fail: ATT_ERR_INVALID_HANDLE + ******************************************************************************* + */ +uint8_t ble_atmprfs_get_att_idx(uint16_t handle, uint8_t *att_idx); + +/** + ******************************************************************************* + * @brief Apply data to ATT server when get GATTC att info request + * + * @param[in] conidx Connection index + * @param[in] att_idx ATT index + * @param[in] length Length + * @return Success: ATT_ERR_NO_ERROR + ******************************************************************************* + */ +uint8_t ble_atmprfs_gattc_att_info_cfm(uint8_t conidx, uint8_t att_idx, + uint16_t length); + +/** + ******************************************************************************* + * @brief Enable/Disable service visibility during service discovery + * + * @param[in] att_idx ATT index + * @param[in] set 1 (service is visible) : 0 (service is not visible) + * @return Success: BLE_ERR_NO_ERROR + ******************************************************************************* + */ +ble_err_code_t ble_atmprfs_svc_visibility_set(uint8_t att_idx, bool set); + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_ATMPRFS + diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_bass/ble_bass.h b/platform/atm2/ATM22xx-x1x/lib/ble_bass/ble_bass.h new file mode 100644 index 0000000..97b7ad1 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_bass/ble_bass.h @@ -0,0 +1,88 @@ +/** + ******************************************************************************* + * + * @file ble_bass.h + * + * @brief BASS API and data structures + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_BASS Battery service procedures + * @ingroup ATM_BTFM_API + * @brief BLE bluetooth battery service procedures + * + * This module contains the necessary procedure to send the battery level. + * + * + * @{ + ******************************************************************************* + */ +/* + * INCLUDE FILES + ******************************************************************************* + */ +#include "ble_prf_itf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * DEFINITION + ******************************************************************************* + */ +#define BLE_BASS_MODULE_NAME "kbass" +#define BATT_LVL_HALF 50 // percent +#define BATT_LVL_MAX 100 // percent + +/* + * STRUCTURES DEFINITION + ******************************************************************************* + */ +/// BASS feature +typedef enum { + /// Support notification + BLE_BASS_FEATURE_NTF = 1, + /// Number of instance. MUST BE the final enumerator. Add new values above. + BLE_BASS_NB_BAS_INSTANCES = 2 +} ble_bass_feature_t; + +typedef struct { + /// Service security level + uint8_t sec_lvl; + /// Number of BAS to add + uint8_t bas_nb; + /// Features of each BAS instance. bit 0: support notification. + /// bit 1 to 7 reserved. + ble_bass_feature_t features[BLE_BASS_NB_BAS_INSTANCES]; + /// Callback to request update + void (*cb_upd_req_ind)(uint8_t bas_instance); +} ble_bass_params_t; + +typedef const ble_prf_cbs_t *(ble_bass_cbs_t)(ble_bass_params_t const *init); + +/* + * GLOBAL + ******************************************************************************* + */ + +/** + ******************************************************************************* + * @brief Send battery level information + * @param[in] bas_instance Connection index + * @param[in] pct_lvl Battery level percentage + ******************************************************************************* + */ +void ble_bass_send_lvl(uint8_t bas_instance, uint8_t pct_lvl); + +#ifdef __cplusplus +} +#endif + +/// @} ATM_BTFM_BASS diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_common/ble_common.h b/platform/atm2/ATM22xx-x1x/lib/ble_common/ble_common.h new file mode 100644 index 0000000..67a22b1 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_common/ble_common.h @@ -0,0 +1,74 @@ +/** + ******************************************************************************* + * + * @file ble_common.h + * + * @brief Common BLE definitions and utility + * + * Copyright (C) Atmosic 2022 + * + ******************************************************************************* + */ + +#pragma once + +#include <string.h> + +/// Address length +#define BLE_BDADDR_LEN 6 + +/// BD address type. +typedef enum { + /// Public address type + BLE_ADDR_PUBLIC, + /// Random address type + BLE_ADDR_RAND, +} ble_addr_type_t; + +/// BD address structure +typedef struct { + /// 6-byte array address value + uint8_t addr[BLE_BDADDR_LEN]; +} ble_bdaddr_t; + +/// Address information about a device address +typedef struct { + /// BD Address of device + ble_bdaddr_t addr; + /// Address type of the device + ble_addr_type_t type; +} ble_gap_bdaddr_t; + +/** + ******************************************************************************* + * @brief Check equivalence of two ble_bdaddr_t type data + * @param[in] addr1 first address to compare + * @param[in] addr2 second address to compare + * + * @return True if they equal. + ******************************************************************************* + */ +__NONNULL_ALL +__INLINE bool ble_bdaddr_compare(ble_bdaddr_t const *addr1, + ble_bdaddr_t const *addr2) +{ + return !memcmp(addr1, addr2, BLE_BDADDR_LEN); +} + +/** + ******************************************************************************* + * @brief Check equivalence of two ble_gap_bdaddr_t type data + * + * @param[in] addr1 first address to compare + * @param[in] addr2 second address to compare + * + * @return True if they equal. + ******************************************************************************* + */ +__NONNULL_ALL +__INLINE bool ble_gap_addr_compare(ble_gap_bdaddr_t const *addr1, + ble_gap_bdaddr_t const *addr2) +{ + return (addr1->type == addr2->type) && + ble_bdaddr_compare(&addr1->addr, &addr2->addr); +} diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_common/ble_err.h b/platform/atm2/ATM22xx-x1x/lib/ble_common/ble_err.h new file mode 100644 index 0000000..1094b72 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_common/ble_err.h @@ -0,0 +1,399 @@ +/** + ******************************************************************************* + * + * @file ble_err.h + * + * @brief Bluetooth framework common error code enums. + * + * Copyright (C) Atmosic 2022-2023 + * + ******************************************************************************* + */ + +#pragma once + +/// Error code size of module +#define BLE_ERR_CODE_BITS 8 + +/// Module of error codes +typedef enum { + BLE_HCI_MODULE, + BLE_L2C_MODULE, + BLE_ATT_MODULE, + BLE_SMP_MODULE, + BLE_GAP_MODULE, +} ble_module_type_t; + +/// Macro for module +#define BLE_MODULE(x) BLE_##x##_MODULE + +/// Macro for combining errors +#define BLE_ERR(x, y) ((BLE_MODULE(x) << BLE_ERR_CODE_BITS) | y) + +/// Macro for getting error type +#define BLE_ERR_TYPE(x) ((x) >> BLE_ERR_CODE_BITS) + +/// Error codes +/// @note This error code is mapped from the lower layer. Should not be changed. +typedef enum { + /// No error. + BLE_ERR_NO_ERROR = 0, + /// HCI Specific Error + /// Unknown HCI Command. + BLE_HCI_ERR_UNKNOWN_HCI_COMMAND = BLE_ERR(HCI, 0x01), + /// Unknown Connection Identifier. + BLE_HCI_ERR_UNKNOWN_CONNECTION_ID = BLE_ERR(HCI, 0x02), + /// Hardware Failure. + BLE_HCI_ERR_HARDWARE_FAILURE = BLE_ERR(HCI, 0x03), + /// Page Timeout. + BLE_HCI_ERR_PAGE_TIMEOUT = BLE_ERR(HCI, 0x04), + /// Authentication Failure. + BLE_HCI_ERR_AUTH_FAILURE = BLE_ERR(HCI, 0x05), + /// PIN or Key Missing. + BLE_HCI_ERR_PIN_MISSING = BLE_ERR(HCI, 0x06), + /// Memory Capacity Exceeded. + BLE_HCI_ERR_MEMORY_CAPA_EXCEED = BLE_ERR(HCI, 0x07), + /// Connection Timeout. + BLE_HCI_ERR_CON_TIMEOUT = BLE_ERR(HCI, 0x08), + /// Connection Limit Exceeded. + BLE_HCI_ERR_CON_LIMIT_EXCEED = BLE_ERR(HCI, 0x09), + /// Synchronous Connection Limit to a Device Exceeded. + BLE_HCI_ERR_SYNC_CON_LIMIT_DEV_EXCEED = BLE_ERR(HCI, 0x0A), + /// Connection Already Exists. + BLE_HCI_ERR_CON_ALREADY_EXISTS = BLE_ERR(HCI, 0x0B), + /// Command Disallowed. + BLE_HCI_ERR_COMMAND_DISALLOWED = BLE_ERR(HCI, 0x0C), + /// Connection Rejected Due To Limited Resources. + BLE_HCI_ERR_CONN_REJ_LIMITED_RESOURCES = BLE_ERR(HCI, 0x0D), + /// Connection Rejected Due To Security Reasons. + BLE_HCI_ERR_CONN_REJ_SECURITY_REASONS = BLE_ERR(HCI, 0x0E), + /// Connection Rejected due to Unacceptable BD_ADDR. + BLE_HCI_ERR_CONN_REJ_UNACCEPTABLE_BDADDR = BLE_ERR(HCI, 0x0F), + /// Connection Accept Timeout Exceeded. + BLE_HCI_ERR_CONN_ACCEPT_TIMEOUT_EXCEED = BLE_ERR(HCI, 0x10), + /// Unsupported Feature Or Parameter Value. + BLE_HCI_ERR_UNSUPPORTED = BLE_ERR(HCI, 0x11), + /// Invalid HCI Command Parameters. + BLE_HCI_ERR_INVALID_HCI_PARAM = BLE_ERR(HCI, 0x12), + /// Remote User Terminated Connection. + BLE_HCI_ERR_REMOTE_USER_TERM_CON = BLE_ERR(HCI, 0x13), + /// Remote Device Terminated Connection due to Low Resources. + BLE_HCI_ERR_REMOTE_DEV_TERM_LOW_RESOURCES = BLE_ERR(HCI, 0x14), + /// Remote Device Terminated Connection due to Power Off. + BLE_HCI_ERR_REMOTE_DEV_POWER_OFF = BLE_ERR(HCI, 0x15), + /// Connection Terminated By Local Host. + BLE_HCI_ERR_CON_TERM_BY_LOCAL_HOST = BLE_ERR(HCI, 0x16), + /// Repeated Attempts. + BLE_HCI_ERR_REPEATED_ATTEMPTS = BLE_ERR(HCI, 0x17), + /// Pairing Not Allowed. + BLE_HCI_ERR_PAIRING_NOT_ALLOWED = BLE_ERR(HCI, 0x18), + /// Unknown LMP PDU. + BLE_HCI_ERR_UNKNOWN_LMP_PDU = BLE_ERR(HCI, 0x19), + /// Unsupported Remote Feature. + BLE_HCI_ERR_UNSUPPORTED_REMOTE_FEATURE = BLE_ERR(HCI, 0x1A), + /// SCO Offset Rejected. + BLE_HCI_ERR_SCO_OFFSET_REJECTED = BLE_ERR(HCI, 0x1B), + /// SCO Interval Rejected. + BLE_HCI_ERR_SCO_INTERVAL_REJECTED = BLE_ERR(HCI, 0x1C), + /// SCO Air Mode Rejected. + BLE_HCI_ERR_SCO_AIR_MODE_REJECTED = BLE_ERR(HCI, 0x1D), + /// Invalid LMP Parameters / Invalid LL Parameters. + BLE_HCI_ERR_INVALID_LMP_PARAM = BLE_ERR(HCI, 0x1E), + /// Unspecified Error. + BLE_HCI_ERR_UNSPECIFIED_ERROR = BLE_ERR(HCI, 0x1F), + /// Unsupported LMP Parameter Value / Unsupported LL Parameter Value. + BLE_HCI_ERR_UNSUPPORTED_LMP_PARAM_VALUE = BLE_ERR(HCI, 0x20), + /// Role Change Not Allowed. + BLE_HCI_ERR_ROLE_CHANGE_NOT_ALLOWED = BLE_ERR(HCI, 0x21), + /// LMP Response Timeout / LL Response Timeout. + BLE_HCI_ERR_LMP_RSP_TIMEOUT = BLE_ERR(HCI, 0x22), + /// LMP Error Transaction Collision / LL Procedure Collision. + BLE_HCI_ERR_LMP_COLLISION = BLE_ERR(HCI, 0x23), + /// LMP PDU Not Allowed. + BLE_HCI_ERR_LMP_PDU_NOT_ALLOWED = BLE_ERR(HCI, 0x24), + /// Encryption Mode Not Acceptable. + BLE_HCI_ERR_ENC_MODE_NOT_ACCEPT = BLE_ERR(HCI, 0x25), + /// Link Key cannot be Changed. + BLE_HCI_ERR_LINK_KEY_CANT_CHANGE = BLE_ERR(HCI, 0x26), + /// Requested QoS Not Supported. + BLE_HCI_ERR_QOS_NOT_SUPPORTED = BLE_ERR(HCI, 0x27), + /// Instant Passed. + BLE_HCI_ERR_INSTANT_PASSED = BLE_ERR(HCI, 0x28), + /// Pairing With Unit Key Not Supported. + BLE_HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUP = BLE_ERR(HCI, 0x29), + /// Different Transaction Collision. + BLE_HCI_ERR_DIFF_TRANSACTION_COLLISION = BLE_ERR(HCI, 0x2A), + /// QoS Unacceptable Parameter. + BLE_HCI_ERR_QOS_UNACCEPTABLE_PARAM = BLE_ERR(HCI, 0x2C), + /// QoS Rejected. + BLE_HCI_ERR_QOS_REJECTED = BLE_ERR(HCI, 0x2D), + /// Channel Assessment Not Supported. + BLE_HCI_ERR_CHANNEL_CLASS_NOT_SUP = BLE_ERR(HCI, 0x2E), + /// Insufficient Security. + BLE_HCI_ERR_INSUFFICIENT_SECURITY = BLE_ERR(HCI, 0x2F), + /// Parameter Out Of Mandatory Range. + BLE_HCI_ERR_PARAM_OUT_OF_MAND_RANGE = BLE_ERR(HCI, 0x30), + /// Role Switch Pending. + BLE_HCI_ERR_ROLE_SWITCH_PEND = BLE_ERR(HCI, 0x32), + /// Reserved Slot Violation. + BLE_HCI_ERR_RESERVED_SLOT_VIOLATION = BLE_ERR(HCI, 0x34), + /// Role Switch Failed. + BLE_HCI_ERR_ROLE_SWITCH_FAIL = BLE_ERR(HCI, 0x35), + /// Extended Inquiry Response Too Large. + BLE_HCI_ERR_EIR_TOO_LARGE = BLE_ERR(HCI, 0x36), + /// Secure Simple Pairing Not Supported by Host. + BLE_HCI_ERR_SP_NOT_SUPPORTED_HOST = BLE_ERR(HCI, 0x37), + /// The Host Busy - Pairing. + BLE_HCI_ERR_HOST_BUSY_PAIRING = BLE_ERR(HCI, 0x38), + /// Connection Rejected due to No Suitable Channel Found. + BLE_HCI_ERR_CONN_REJ_NO_SUITABLE_CHANN_FOUND = BLE_ERR(HCI, 0x39), + /// Controller Busy. + BLE_HCI_ERR_CONTROLLER_BUSY = BLE_ERR(HCI, 0x3A), + /// Unacceptable Connection Parameters. + BLE_HCI_ERR_UNACCEPTABLE_CONN_PARAM = BLE_ERR(HCI, 0x3B), + /// Advertising Timeout. + BLE_HCI_ERR_ADV_TO = BLE_ERR(HCI, 0x3C), + /// Connection Terminated Due to MIC Failure. + BLE_HCI_ERR_TERMINATED_MIC_FAILURE = BLE_ERR(HCI, 0x3D), + /// Connection Failed to be Established / Synchronization Timeout. + BLE_HCI_ERR_CONN_FAILED_TO_BE_EST = BLE_ERR(HCI, 0x3E), + /// Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock + /// Dragging. + BLE_HCI_ERR_CCA_REJ_USE_CLOCK_DRAG = BLE_ERR(HCI, 0x40), + /// Type0 Submap Not Defined. + BLE_HCI_ERR_TYPE0_SUBMAP_NOT_DEFINED = BLE_ERR(HCI, 0x41), + /// Unknown Advertising Identifier. + BLE_HCI_ERR_UNKNOWN_ADVERTISING_ID = BLE_ERR(HCI, 0x42), + /// Limit Reached. + BLE_HCI_ERR_LIMIT_REACHED = BLE_ERR(HCI, 0x43), + /// Operation Cancelled by Host. + BLE_HCI_ERR_OPERATION_CANCELED_BY_HOST = BLE_ERR(HCI, 0x44), + /// Undefined error. + BLE_HCI_ERR_UNDEFINED = BLE_ERR(HCI, 0xFF), + + /// L2C Specific Error + /// Table 4.3: Reason code descriptions in L2CAP of Core Specification. + /// Command not understood. + BLE_L2C_ERR_NOT_UNDERSTOOD = BLE_ERR(L2C, 0x01), + /// Signaling MTU exceeded. + BLE_L2C_ERR_INVALID_MTU_EXCEED = BLE_ERR(L2C, 0x02), + /// Invalid CID in request. + BLE_L2C_ERR_INVALID_CID = BLE_ERR(L2C, 0x03), + /// Table 4.14: Result values for the L2CAP_CONNECTION_PARAMETER_UPDATE_RSP + /// packet. + /// Connection Parameters rejected. + BLE_L2C_ERR_CONN_PARA_REJECTED = BLE_ERR(L2C, 0x04), + /// Table 4.16: Result values for the L2CAP_LE_CREDIT_BASED_CONNECTION_RSP + /// packet in L2CAP of Core Specification. + /// Connection refused - SPSM not supported. + BLE_L2C_ERR_SPSM_NOT_SUPP = BLE_ERR(L2C, 0x05), + /// Connection refused - no resources available. + BLE_L2C_ERR_NO_RES_AVAIL = BLE_ERR(L2C, 0x06), + /// Connection refused - insufficient authentication. + BLE_L2C_ERR_INSUFF_AUTHEN = BLE_ERR(L2C, 0x07), + /// Connection refused - insufficient authorization. + BLE_L2C_ERR_INSUFF_AUTHOR = BLE_ERR(L2C, 0x08), + /// Connection refused - insufficient encryption key size. + BLE_L2C_ERR_INSUFF_ENC_KEY_SIZE = BLE_ERR(L2C, 0x09), + /// Connection refused - insufficient encryption. + BLE_L2C_ERR_INSUFF_ENC = BLE_ERR(L2C, 0x0A), + /// Connection refused – invalid Source CID. + BLE_L2C_ERR_INVALID_SOURCE_CID = BLE_ERR(L2C, 0x0B), + /// Connection refused – Source CID already allocated. + BLE_L2C_ERR_CID_ALREADY_ALLOC = BLE_ERR(L2C, 0x0C), + /// Connection refused – unacceptable parameters. + BLE_L2C_ERR_UNACCEPT_PARA = BLE_ERR(L2C, 0x0D), + /// Table 4.17: Result values for the L2CAP_CREDIT_BASED_CONNECTION_RSP + /// packet in L2CAP of Core Specification. + /// Connection pending – no further information available. + BLE_L2C_ERR_NO_FURTHER_INFO_AVAIL = BLE_ERR(L2C, 0x0E), + /// Connection pending – authentication pending. + BLE_L2C_ERR_AUTHEN_PEND = BLE_ERR(L2C, 0x0F), + /// Connection pending – authorization pending. + BLE_L2C_ERR_AUTHOR_PEND = BLE_ERR(L2C, 0x10), + /// Local L2CAP Specific Error + /// Invalid PDU length exceed MPS. + BLE_L2C_ERR_INVALID_MPS_EXCEED = BLE_ERR(L2C, 0x11), + /// Invalid PDU. + BLE_L2C_ERR_INVALID_PDU = BLE_ERR(L2C, 0x12), + /// Credit error, invalid number of credit received. + BLE_L2C_ERR_CREDIT_ERROR = BLE_ERR(L2C, 0x13), + /// Undefined error. + BLE_L2C_ERR_UNDEFINED = BLE_ERR(L2C, 0xFF), + + /// ATT Specific Error + /// Refer to Table 3.4: Error codes in ATT of Core Specification. + /// The attribute handle given was not valid on this server. + BLE_ATT_ERR_INVALID_HANDLE = BLE_ERR(ATT, 0x01), + /// The attribute cannot be read. + BLE_ATT_ERR_READ_NOT_PERMITTED = BLE_ERR(ATT, 0x02), + /// The attribute cannot be written. + BLE_ATT_ERR_WRITE_NOT_PERMITTED = BLE_ERR(ATT, 0x03), + /// The attribute PDU was invalid. + BLE_ATT_ERR_INVALID_PDU = BLE_ERR(ATT, 0x04), + /// The attribute requires authentication before it can be read or written. + BLE_ATT_ERR_INSUFF_AUTHEN = BLE_ERR(ATT, 0x05), + /// ATT Server does not support the request received from the client. + BLE_ATT_ERR_REQUEST_NOT_SUPPORTED = BLE_ERR(ATT, 0x06), + /// Offset specified was past the end of the attribute. + BLE_ATT_ERR_INVALID_OFFSET = BLE_ERR(ATT, 0x07), + /// The attribute requires authorization before it can be read or written. + BLE_ATT_ERR_INSUFF_AUTHOR = BLE_ERR(ATT, 0x08), + /// Too many prepare writes have been queued. + BLE_ATT_ERR_PREPARE_QUEUE_FULL = BLE_ERR(ATT, 0x09), + /// No attribute found within the given attribute handle range. + BLE_ATT_ERR_ATTRIBUTE_NOT_FOUND = BLE_ERR(ATT, 0x0A), + /// The attribute cannot be read using the ATT_READ_BLOB_REQ PDU. + BLE_ATT_ERR_ATTRIBUTE_NOT_LONG = BLE_ERR(ATT, 0x0B), + /// The Encryption Key Size used for encrypting this link is too short. + BLE_ATT_ERR_INSUFF_ENC_KEY_SIZE = BLE_ERR(ATT, 0x0C), + /// The attribute value length is invalid for the operation. + BLE_ATT_ERR_INVALID_ATTRIBUTE_VAL_LEN = BLE_ERR(ATT, 0x0D), + /// The attribute request that was requested has encountered an error that + /// was unlikely, and therefore could not be completed as requested. + BLE_ATT_ERR_UNLIKELY_ERR = BLE_ERR(ATT, 0x0E), + /// The attribute requires encryption before it can be read or written. + BLE_ATT_ERR_INSUFF_ENC = BLE_ERR(ATT, 0x0F), + /// The attribute type is not a supported grouping attribute as defined by a + /// higher layer specification. + BLE_ATT_ERR_UNSUPP_GRP_TYPE = BLE_ERR(ATT, 0x10), + /// Insufficient Resources to complete the request. + BLE_ATT_ERR_INSUFF_RESOURCE = BLE_ERR(ATT, 0x11), + /// The server requests the client to rediscover the database. + BLE_ATT_ERR_DATABASE_OUT_OF_SYNC = BLE_ERR(ATT, 0x12), + /// The attribute parameter value was not allowed. + BLE_ATT_ERR_VALUE_NOT_ALLOWED = BLE_ERR(ATT, 0x13), + + /// Application error code defined by a higher layer specification. + /// range 0x80 ~ 0x9F + BLE_ATT_ERR_APP_ERROR = BLE_ERR(ATT, 0x80), + /// Invalid parameter in request + BLE_ATT_ERR_APP_INVALID_PARAM = BLE_ERR(ATT, 0x81), + /// Inexistent handle for sending a read/write characteristic request + BLE_ATT_ERR_APP_INEXISTENT_HDL = BLE_ERR(ATT, 0x82), + /// Discovery stopped due to missing attribute according to specification + BLE_ATT_ERR_APP_STOP_DISC_CHAR_MISSING = BLE_ERR(ATT, 0x83), + /// Too many SVC instances found -> protocol violation + BLE_ATT_ERR_APP_MULTIPLE_SVC = BLE_ERR(ATT, 0x84), + /// Discovery stopped due to found attribute with incorrect properties + BLE_ATT_ERR_APP_STOP_DISC_WRONG_CHAR_PROP = BLE_ERR(ATT, 0x85), + /// Too many Char. instances found-> protocol violation + BLE_ATT_ERR_APP_MULTIPLE_CHAR = BLE_ERR(ATT, 0x86), + /// Attribute write not allowed + BLE_ATT_ERR_APP_NOT_WRITABLE = BLE_ERR(ATT, 0x87), + /// Attribute read not allowed + BLE_ATT_ERR_APP_NOT_READABLE = BLE_ERR(ATT, 0x88), + /// Request not allowed + BLE_ATT_ERR_APP_REQ_DISALLOWED = BLE_ERR(ATT, 0x89), + /// Notification Not Enabled + BLE_ATT_ERR_APP_NTF_DISABLED = BLE_ERR(ATT, 0x8A), + /// Indication Not Enabled + BLE_ATT_ERR_APP_IND_DISABLED = BLE_ERR(ATT, 0x8B), + /// Feature not supported by profile + BLE_ATT_ERR_APP_FEATURE_NOT_SUPPORTED = BLE_ERR(ATT, 0x8C), + /// Read value has an unexpected length + BLE_ATT_ERR_APP_UNEXPECTED_LEN = BLE_ERR(ATT, 0x8D), + /// Disconnection occurs + BLE_ATT_ERR_APP_DISCONNECTED = BLE_ERR(ATT, 0x8E), + /// Procedure Timeout + BLE_ATT_ERR_APP_PROC_TIMEOUT = BLE_ERR(ATT, 0x8F), + /// Permission set in service/attribute are invalid + BLE_ATT_ERR_APP_INVALID_PERM = BLE_ERR(ATT, 0x90), + + /// Common profile and service error codes. range 0xE0 ~ 0xFF + BLE_ATT_ERR_COMM_PRF_AND_SERVICE_ERR = BLE_ERR(ATT, 0xE0), + /// Write request rejected + BLE_ATT_ERR_PRF_WRITE_REQ_REJECTED = BLE_ERR(ATT, 0xFC), + /// Client characteristic configuration improperly configured + BLE_ATT_ERR_PRF_CCCD_IMPR_CONFIGURED = BLE_ERR(ATT, 0xFD), + /// Procedure already in progress + BLE_ATT_ERR_PRF_PROC_IN_PROGRESS = BLE_ERR(ATT, 0xFE), + /// Out of Range + BLE_ATT_ERR_PRF_OUT_OF_RANGE = BLE_ERR(ATT, 0xFF), + + /// SMP Specific Error + /// Refer to Table 3.7: Pairing Failed Reason Codes in SMP of Core + /// Specification. + /// The user input of pass key failed, for example, the user canceled the + /// operation. + BLE_SMP_ERR_PASSKEY_ENTRY_FAILED = BLE_ERR(SMP, 0x01), + /// The OOB Data is not available. + BLE_SMP_ERR_OOB_NOT_AVAILABLE = BLE_ERR(SMP, 0x02), + /// The pairing procedure cannot be performed as authentication requirements + /// cannot be met due to IO capabilities of one or both devices. + BLE_SMP_ERR_AUTH_REQ = BLE_ERR(SMP, 0x03), + /// The confirm value does not match the calculated confirm value. + BLE_SMP_ERR_CONF_VAL_FAILED = BLE_ERR(SMP, 0x04), + /// Pairing is not supported by the device. + BLE_SMP_ERR_PAIRING_NOT_SUPP = BLE_ERR(SMP, 0x05), + /// The resultant encryption key size is insufficient for the security + /// requirements of this device. + BLE_SMP_ERR_ENC_KEY_SIZE = BLE_ERR(SMP, 0x06), + /// The SMP command received is not supported on this device. + BLE_SMP_ERR_CMD_NOT_SUPPORTED = BLE_ERR(SMP, 0x07), + /// Pairing failed due to an unspecified reason. + BLE_SMP_ERR_UNSPECIFIED_REASON = BLE_ERR(SMP, 0x08), + /// Pairing or Authentication procedure is disallowed because too little + /// time has elapsed since last pairing request or security request. + BLE_SMP_ERR_REPEATED_ATTEMPTS = BLE_ERR(SMP, 0x09), + /// The command length is invalid or a parameter is outside of the specified + /// range. + BLE_SMP_ERR_INVALID_PARAM = BLE_ERR(SMP, 0x0A), + /// Indicates to the remote device that the DHKey Check value received + /// doesn't match the one calculated by the local device. + BLE_SMP_ERR_DHKEY_CHECK_FAILED = BLE_ERR(SMP, 0x0B), + /// Indicates that the confirm values in the numeric comparison protocol do + /// not match. + BLE_SMP_ERR_NUMERIC_COMPARISON_FAILED = BLE_ERR(SMP, 0x0C), + /// Indicates that the pairing over the LE transport failed due to a Pairing + /// Request sent over the BR/EDR transport in process. + BLE_SMP_ERR_BREDR_PAIRING_IN_PROGRESS = BLE_ERR(SMP, 0x0D), + /// Indicates that the BR/EDR Link Key generated on the BR/EDR transport + /// cannot be used to derive and distribute keys for the LE transport. + BLE_SMP_ERR_CROSS_TRANSPORT_KEY_GENERATION_NOT_ALLOWED = BLE_ERR(SMP, 0x0E), + /// Indicates that the device chose not to accept a distributed key. + BLE_SMP_ERR_KEY_REJECTED = BLE_ERR(SMP, 0x0F), + /// Local SMP Specific Error + /// The signature verification failed. + BLE_SMP_ERR_SIGN_VERIF_FAIL = BLE_ERR(SMP, 0x10), + /// The encryption procedure failed because the slave device didn't find the + /// LTK needed to start an encryption session. + BLE_SMP_ERR_ENC_KEY_MISSING = BLE_ERR(SMP, 0x11), + /// The encryption procedure failed because the slave device doesn't support + /// the encryption feature. + BLE_SMP_ERR_ENC_NOT_SUPPORTED = BLE_ERR(SMP, 0x12), + /// A timeout has occurred during the start encryption session. + BLE_SMP_ERR_ENC_TIMEOUT = BLE_ERR(SMP, 0x13), + /// Undefined error. + BLE_SMP_ERR_UNDEFINED = BLE_ERR(SMP, 0xFF), + + /// GAP Specific Error + /// Invalid parameters set. + BLE_GAP_ERR_INVALID_PARAM = BLE_ERR(GAP, 0x01), + /// Problem with protocol exchange, get unexpected response. + BLE_GAP_ERR_PROTOCOL_PROBLEM = BLE_ERR(GAP, 0x02), + /// Request not supported by software configuration. + BLE_GAP_ERR_NOT_SUPPORTED = BLE_ERR(GAP, 0x03), + /// Request not allowed in current state. + BLE_GAP_ERR_COMMAND_DISALLOWED = BLE_ERR(GAP, 0x04), + /// Requested operation canceled. + BLE_GAP_ERR_CANCELED = BLE_ERR(GAP, 0x05), + /// Requested operation timeout. + BLE_GAP_ERR_TIMEOUT = BLE_ERR(GAP, 0x06), + /// Link connection lost during operation. + BLE_GAP_ERR_DISCONNECTED = BLE_ERR(GAP, 0x07), + /// Search algorithm finished, but no result found. + BLE_GAP_ERR_NOT_FOUND = BLE_ERR(GAP, 0x08), + /// Request rejected by peer device. + BLE_GAP_ERR_REJECTED = BLE_ERR(GAP, 0x09), + /// Problem with privacy configuration. + BLE_GAP_ERR_PRIVACY_CFG_PB = BLE_ERR(GAP, 0x0A), + /// Duplicate or invalid advertising data. + BLE_GAP_ERR_ADV_DATA_INVALID = BLE_ERR(GAP, 0x0B), + /// Insufficient resources. + BLE_GAP_ERR_INSUFF_RESOURCES = BLE_ERR(GAP, 0x0C), + /// Unexpected error. + BLE_GAP_ERR_UNEXPECTED = BLE_ERR(GAP, 0x0D), + /// Undefined error. + BLE_GAP_ERR_UNDEFINED = BLE_ERR(GAP, 0xFF), +} ble_err_code_t; diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_diss/ble_diss.h b/platform/atm2/ATM22xx-x1x/lib/ble_diss/ble_diss.h new file mode 100644 index 0000000..9f09898 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_diss/ble_diss.h @@ -0,0 +1,78 @@ +/** + ******************************************************************************* + * + * @file ble_diss.h + * + * @brief Device Information Service Server Module + * + * Copyright (C) Atmosic 2020-2021 + * + ******************************************************************************* + */ + +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_DISS Device information service procedures + * @ingroup ATM_BTFM_API + * @brief BLE bluetooth device information service procedures + * + * This module contains the necessary procedure to send the device information. + * + * @{ + ******************************************************************************* + */ +/* + * INCLUDE FILES + ******************************************************************************* + */ +#include <stdint.h> +#include "ble_gap.h" +#include "ble_internal.h" +#include "ble_prf_itf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * DEFINITION + ******************************************************************************* + */ +#define BLE_DISS_MODULE_NAME "kdiss" + +/* + * GLOBAL VARIABLES DECLARATION + ******************************************************************************* + */ + +/* + * DEFINES + ******************************************************************************* + */ +struct diss_param { + /// Security Level : + /// 7 6 5 4 3 2 1 0 + /// +----+----+----+----+----+----+----+----+ + /// | Reserved |DIS | AUTH |EKS | MI | + /// +----+----+----+----+----+----+----+----+ + /// + /// - MI: 1 - Application task is a Multi-Instantiated task, 0 - Mono-Instantiated + /// Only applies for service - Ignored by collectors: + /// - EKS: Service needs a 16 bytes encryption key + /// - AUTH: 0 - Disable, 1 - Enable, 2 - Unauth, 3 - Auth + /// - DIS: Disable the service + uint8_t sec_lvl; + /// Supported Feature configuration + uint16_t feature; + void (*cb_value_req)(uint8_t, uint8_t const **, uint8_t *); +}; + +typedef const ble_prf_cbs_t *(ble_prf_get_cbs_t)(struct diss_param const *init); + +#ifdef __cplusplus +} +#endif + +/// @} ATM_BTFM_DISS diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_gap/ble_gap.h b/platform/atm2/ATM22xx-x1x/lib/ble_gap/ble_gap.h new file mode 100644 index 0000000..867123a --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_gap/ble_gap.h @@ -0,0 +1,2192 @@ +/** + ******************************************************************************* + * + * @file ble_gap.h + * + * @brief Header file - BLE GAP API. + * + * Copyright (C) Atmosic 2020-2023 + * + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_BLE_GAP GAP API + * @ingroup ATM_BTFM_API + * @brief ATM bluetooth framework GAP API + * + * This module contains the necessary API to deal with the GAP messages. + * + * @{ + ******************************************************************************* + */ + + +/* + * INCLUDE FILES + ******************************************************************************* + */ + +#include "co_bt.h" // Common BT Definitions +#include "gap.h" // GAP Definition +#include "gapm.h" +#include "gapc.h" +#include "ble_common.h" +#include "ble_err.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/// Key length +#define BLE_GAP_KEY_LEN 16 +/// Random number number +#define BLE_GAP_RAND_NB_LEN 8 +/// Maximum test TX power level +#define BLE_GAP_MAX_TEST_TX_PWR_LVL 0x7F +/// Minimum switching pattern length +#define BLE_GAP_MIN_SWITCHING_PATTERN_LEN 2 +/// Maximum switching pattern length +#define BLE_GAP_MAX_SWITCHING_PATTERN_LEN 0x4B +/// LE Feature Flags Length +#define BLE_GAP_LE_FEATS_LEN 8 +/// LE Channel map length +#define BLE_GAP_LE_CHNL_MAP_LEN 5 +/// Scanning window (in 625us slot) +#define BLE_SCAN_WINDOW_DFT 0x0010 +/// Scanning interval (in 625us slot) +#define BLE_SCAN_INTERVAL_DFT 0x0010 +/// Database hash length +#define BLE_GAP_DATABASE_HASH_LEN 16 + +/* + * TYPEDEFINE + ******************************************************************************* + */ + +/// Random Address type +typedef enum { + /// Static random address + BLE_GAP_STATIC_ADDR = 0xc0, + /// Private non-resolvable address + BLE_GAP_NON_RSLV_ADDR = 0x00, + /// Private resolvable address + BLE_GAP_RSLV_ADDR = 0x40, +} ble_gapm_rand_addr_type_t; + +/// BLE ADV data type +typedef enum { + /// Advertising data + BLE_GAPM_ADV_DATA, + /// Scan response data + BLE_GAPM_SCAN_RSP_DATA, + /// Periodic advertising data + BLE_GAPM_PERIOD_ADV_DATA +} ble_gapm_data_type_t; + +typedef enum { + /// Extended advertising report + BLE_GAP_REPORT_TYPE_ADV_EXT, + /// Legacy advertising report + BLE_GAP_REPORT_TYPE_ADV_LEG, + /// Extended scan response report + BLE_GAP_REPORT_TYPE_SCAN_RSP_EXT, + /// Legacy scan response report + BLE_GAP_REPORT_TYPE_SCAN_RSP_LEG, + /// Periodic advertising report + BLE_GAP_REPORT_TYPE_PER_ADV, +} ble_gap_adv_report_type_t; + +typedef enum { + /// Report Type (see ble_gap_adv_report_type_t) + BLE_GAP_REPORT_INFO_REPORT_TYPE_MASK = 0x07, + /// Report is complete + BLE_GAP_REPORT_INFO_COMPLETE_BIT = (1 << 3), + /// Connectable advertising + BLE_GAP_REPORT_INFO_CONN_ADV_BIT = (1 << 4), + /// Scannable advertising + BLE_GAP_REPORT_INFO_SCAN_ADV_BIT = (1 << 5), + /// Directed advertising + BLE_GAP_REPORT_INFO_DIR_ADV_BIT = (1 << 6), +} ble_gap_adv_report_info_t; + +/// Packet payload type for test mode +typedef enum { + /// PRBS9 sequence "11111111100000111101..." (in transmission order) + BLE_GAP_PKT_PLD_PRBS9, + /// Repeated "11110000" (in transmission order) + BLE_GAP_PKT_PLD_REPEATED_11110000, + /// Repeated "10101010" (in transmission order) + BLE_GAP_PKT_PLD_REPEATED_10101010, + /// PRBS15 sequence + BLE_GAP_PKT_PLD_PRBS15, + /// Repeated "11111111" (in transmission order) sequence + BLE_GAP_PKT_PLD_REPEATED_11111111, + /// Repeated "00000000" (in transmission order) sequence + BLE_GAP_PKT_PLD_REPEATED_00000000, + /// Repeated "00001111" (in transmission order) sequence + BLE_GAP_PKT_PLD_REPEATED_00001111, + /// Repeated "01010101" (in transmission order) sequence + BLE_GAP_PKT_PLD_REPEATED_01010101, +} ble_gap_pkt_pld_type_t; + +/// Enumeration of TX PHY values +typedef enum { + /// LE 1M PHY + BLE_GAP_TX_PHY_1MBPS = 1, + /// LE 2M PHY + BLE_GAP_TX_PHY_2MBPS = 2, + /// LE Coded PHY with S=8 data coding + BLE_GAP_TX_PHY_125KBPS = 3, + /// LE Coded PHY with S=2 data coding + BLE_GAP_TX_PHY_500KBPS = 4, +} ble_gap_tx_phy_val_t; + +/// Enumeration of RX PHY values +typedef enum { + /// LE 1M PHY + BLE_GAP_RX_PHY_1MBPS = 1, + /// LE 2M PHY + BLE_GAP_RX_PHY_2MBPS = 2, + /// LE Coded PHY + BLE_GAP_RX_PHY_CODED = 3, +} ble_gap_rx_phy_val_t; + +/// Enumeration of PHY values +typedef enum { + /// LE 1M PHY + BLE_GAP_PHY_1MBPS = 1, + /// LE 2M PHY + BLE_GAP_PHY_2MBPS = 2, + /// LE Coded PHY + BLE_GAP_PHY_CODED = 3, +} ble_gap_phy_val_t; + +/// Constant tone extension type +typedef enum { + /// AoA constant tone extension + BLE_GAP_CTE_AOA, + /// AoD constant tone extension with 1 us slots + BLE_GAP_CTE_AOD_1US, + /// AoD constant tone extension with 2 us slots + BLE_GAP_CTE_AOD_2US, +} ble_gap_cte_type_t; + +/// LE transmitter test parameters +typedef struct { + /// TX channel (range: 0x00 to 0x27) + uint8_t ch; + /// Length in bytes of payload data in each packet + uint8_t test_data_len; + /// Packet payload type + ble_gap_pkt_pld_type_t pkt_pld_type; + /// PHY + ble_gap_tx_phy_val_t phy; + /// CTE length + uint8_t cte_len; + /// CTE type + ble_gap_cte_type_t cte_type; + /// Transmit power level in dBm (0x7E: minimum | 0x7F: maximum | range: -127 + /// to +20) + int8_t tx_pwr_lvl; + /// The number of antenna IDs in the pattern (range: 0x02 to 0x4B) + uint8_t switching_pattern_len; + /// Antenna IDs + uint8_t antenna_id[__ARRAY_EMPTY]; +} ble_gap_test_tx_param_t; + +/// Modulation index +typedef enum { + /// Assume transmitter will have a standard modulation index + BLE_GAP_MODULATION_STANDARD, + /// Assume transmitter will have a stable modulation index + BLE_GAP_MODULATION_STABLE, +} ble_gap_mod_idx_t; + +/// Slot durations +typedef enum { + /// Switching and sampling slots are 1 μs each + BLE_GAP_SLOT_DUR_1US = 1, + /// Switching and sampling slots are 2 μs each + BLE_GAP_SLOT_DUR_2US, +} ble_gap_slot_dur_t; + +/// LE receiver test parameters +typedef struct { + /// RX channel (range: 0x00 to 0x27) + uint8_t ch; + /// PHY + ble_gap_rx_phy_val_t phy; + /// Modulation index + ble_gap_mod_idx_t mod_idx; + /// Expected CTE length + uint8_t exp_cte_len; + /// Expected CTE type + ble_gap_cte_type_t exp_cte_type; + /// Slot durations + ble_gap_slot_dur_t slot_dur; + /// The number of antenna IDs in the pattern (range: 0x02 to 0x4B) + uint8_t switching_pattern_len; + /// Antenna IDs + uint8_t antenna_id[__ARRAY_EMPTY]; +} ble_gap_test_rx_param_t; + +/// BLE ADV channel selection +typedef enum { + /// Channel 37 + ADV_37_CHNL = (1 << 0), + /// Channel 38 + ADV_38_CHNL = (1 << 1), + /// Channel 39 + ADV_39_CHNL = (1 << 2), + /// Channel 37, 38 and 39 + ADV_ALL_CHNLS = ADV_37_CHNL | ADV_38_CHNL | ADV_39_CHNL +} ble_adv_chnl_t; + +/// Advertising properties bit field bit value +typedef enum { + /// Indicate that advertising is connectable, reception of + /// CONNECT_IND or AUX_CONNECT_REQ PDUs is accepted. Not applicable + /// for periodic advertising. + ADV_PROP_CONNECTABLE_BIT = (1 << 0), + /// Indicate that advertising is scannable, reception of SCAN_REQ or + /// AUX_SCAN_REQ PDUs is accepted + ADV_PROP_SCANNABLE_BIT = (1 << 1), + /// Indicate that advertising targets a specific device. Only apply + /// in + /// following cases: + /// - Legacy advertising: if connectable + /// - Extended advertising: connectable or (non connectable and + /// non discoverable) + ADV_PROP_DIRECTED_BIT = (1 << 2), + /// Indicate that High Duty Cycle has to be used for advertising on + /// primary channel Apply only if created advertising is not an + /// extended advertising + ADV_PROP_HDC_BIT = (1 << 3), + /// Reserved for bit 4 + ADV_PROP_RESERVED_4_BIT = (1 << 4), + /// Enable anonymous mode. Device address won't appear in send PDUs + /// Valid only if created advertising is an extended advertising + ADV_PROP_ANONYMOUS_BIT = (1 << 5), + /// Include TX Power in the extended header of the advertising PDU. + /// Valid only if created advertising is not a legacy advertising + ADV_PROP_TX_PWR_BIT = (1 << 6), + /// Include TX Power in the periodic advertising PDU. + /// Valid only if created advertising is a periodic advertising + ADV_PROP_PER_TX_PWR_BIT = (1 << 7), + /// Indicate if application must be informed about received scan + /// requests PDUs + ADV_PROP_SCAN_REQ_NTF_EN_BIT = (1 << 8), +} ble_adv_prop_t; + +/// Leggacy advertising properties. Used when type is ADV_TYPE_LEGACY +enum adv_crt_leg_prop { + /// Non connectable non scannable advertising + ADV_LEGACY_NON_CONN_NON_SCAN_MASK, + /// Broadcast non scannable advertising - Discovery mode must be + /// Non Discoverable + ADV_LEGACY_BROADCAST_NON_SCAN_MASK = + ADV_LEGACY_NON_CONN_NON_SCAN_MASK, + /// Non connectable scannable advertising + ADV_LEGACY_NON_CONN_SCAN_MASK = ADV_PROP_SCANNABLE_BIT, + /// Broadcast non scannable advertising - Discovery mode must be Non + /// Discoverable + ADV_LEGACY_BROADCAST_SCAN_MASK = ADV_LEGACY_NON_CONN_SCAN_MASK, + /// Undirected connectable advertising + ADV_LEGACY_UNDIR_CONN_MASK = ADV_PROP_CONNECTABLE_BIT | + ADV_PROP_SCANNABLE_BIT, + /// Directed connectable advertising + ADV_LEGACY_DIR_CONN_MASK = ADV_PROP_DIRECTED_BIT | + ADV_PROP_CONNECTABLE_BIT, + /// Directed connectable with Low Duty Cycle + ADV_LEGACY_DIR_CONN_LDC_MASK = ADV_LEGACY_DIR_CONN_MASK, + /// Directed connectable with High Duty Cycle + ADV_LEGACY_DIR_CONN_HDC_MASK = ADV_LEGACY_DIR_CONN_MASK | + ADV_PROP_HDC_BIT, +}; + +/// Leggacy advertising properties. Used when type is ADV_TYPE_EXTENDED +enum adv_crt_ext_prop { + /// Non connectable non scannable extended advertising + ADV_EXT_NON_CONN_NON_SCAN_MASK, + /// Non connectable scannable extended advertising + ADV_EXT_NON_CONN_SCAN_MASK = ADV_PROP_SCANNABLE_BIT, + /// Non connectable scannable directed extended advertising + ADV_EXT_NON_CONN_SCAN_DIR_MASK = ADV_PROP_SCANNABLE_BIT | + ADV_PROP_DIRECTED_BIT, + /// Non connectable anonymous directed extended advertising + ADV_EXT_ANONYM_DIR_MASK = ADV_PROP_ANONYMOUS_BIT | + ADV_PROP_DIRECTED_BIT, + /// Undirected connectable extended advertising + ADV_EXT_UNDIR_CONN_MASK = ADV_PROP_CONNECTABLE_BIT, + /// Directed connectable extended advertising + ADV_EXT_DIR_CONN_MASK = ADV_PROP_CONNECTABLE_BIT | + ADV_PROP_DIRECTED_BIT, +}; + +/// Periodic advertising properties. Used when type is ADV_TYPE_PERIODIC +enum adv_crt_per_prop { + /// Undirected periodic advertising + ADV_PER_UNDIR_MASK, + /// Directed periodic advertising + ADV_PER_DIR_MASK = ADV_PROP_DIRECTED_BIT, +}; + +/// Advertising parameters for advertising creation +typedef struct { + /// Advertising type + enum adv_crt_type { + /// Legacy advertising + ADV_TYPE_LEGACY, + /// Extended advertising + ADV_TYPE_EXTENDED, + /// Periodic advertising + ADV_TYPE_PERIODIC, + } type; + + /// Advertising discovery mode + enum adv_crt_mode { + /// Mode in non-discov rable + ADV_MODE_NON_DISC, + /// Mode in general discoverable + ADV_MODE_GEN_DISC, + /// Mode in limited discoverable + ADV_MODE_LIM_DISC, + /// Broadcast mode without presence of AD_TYPE_FLAG in advertising data + ADV_MODE_BEACON, + /// Number of mode + ADV_MODE_MAX, + } disc_mode; + + /// Advertising property + /// This may come from OR operation of values of ble_adv_prop_t + /// with one of value from @see enum adv_crt_leg_prop, + /// @see enum adv_crt_ext_prop or @see enum adv_crt_per_prop. + uint16_t prop; + + /// Maximum power level at which the advertising packets have to be + /// transmitted (between -127 and 126 dBm) + int8_t max_tx_pwr; + /// Advertising filter policy + enum adv_crt_filter_policy { + /// Allow both scan and connection requests from anyone + FILTER_SCAN_ANY_CON_ANY, + /// Allow both scan req from White List devices only and connection req + /// from anyone + FILTER_SCAN_WLST_CON_ANY, + /// Allow both scan req from anyone and connection req from White List + /// devices only + FILTER_SCAN_ANY_CON_WLST, + /// Allow scan and connection requests from White List devices only + FILTER_SCAN_WLST_CON_WLST, + } filter_pol; + /// Peer address configuration (only used in case of directed advertising) + ble_gap_bdaddr_t peer_addr; + /// Configuration for advertising on primary channel + struct primary_config { + /// Minimum advertising interval (in unit of 625us). Must be greater + /// than 20ms + uint32_t adv_intv_min; + /// Maximum advertising interval (in unit of 625us). Must be greater + /// than 20ms + uint32_t adv_intv_max; + /// Bit field indicating the channel mapping. Bit number 0/1/2 is + /// channel number 37/38/39. + ble_adv_chnl_t chnl_map; + /// Note that LE 2M PHY is not allowed and that legacy advertising only + /// support LE 1M PHY + ble_gap_phy_val_t phy; + } prim_cfg; + /// Configuration for secondary advertising (valid only if advertising type + /// is ADV_TYPE_EXTENDED or ADV_TYPE_PERIODIC) + struct second_config { + /// Maximum number of advertising events the controller can skip before + /// sending the + /// AUX_ADV_IND packets. 0 means that AUX_ADV_IND PDUs shall be sent + /// prior each + /// advertising events + uint8_t max_skip; + /// Indicate on which PHY secondary advertising has to be performed + ble_gap_phy_val_t phy; + /// Advertising SID + uint8_t adv_sid; + } second_cfg; + /// Configuration for periodic advertising (valid only if advertising type + /// is ADV_TYPE_PERIODIC) + struct periodic_config { + /// Minimum advertising interval (in unit of 1.25ms). Must be greater + /// than 20ms + uint16_t adv_intv_min; + /// Maximum advertising interval (in unit of 1.25ms). Must be greater + /// than 20ms + uint16_t adv_intv_max; + } period_cfg; +} ble_gapm_adv_create_param_t; + +/// Generic key structure +typedef struct { + /// Key value MSB -> LSB + uint8_t key[BLE_GAP_KEY_LEN]; +} ble_gap_key_t; + +typedef struct { + /// Identity Resolving Key + ble_gap_key_t irk; + /// Device BD Identity Address + ble_gap_bdaddr_t addr; +} ble_gap_irk_t; + +/// Bit field use to select the preferred TX or RX LE PHY. 0 means no +/// preferences. +typedef enum { + /// No preferred PHY. + BLE_GAP_PHY_ANY = 0x00, + /// LE 1M PHY preferred for an active link. + BLE_GAP_PHY_LE_1MBPS = (1 << 0), + /// LE 2M PHY preferred for an active link. + BLE_GAP_PHY_LE_2MBPS = (1 << 1), + /// LE Coded PHY preferred for an active link. + BLE_GAP_PHY_LE_CODED = (1 << 2), +} ble_gap_phy_mode_t; + +/// Specify what rate Host prefers to use in transmission on coded PHY. refer to +/// HCI: 7.8.49 +typedef enum { + /// No preferred coding when transmitting on the LE Coded PHY. + BLE_GAP_PHY_OPT_NO_LE_CODED_TX_PREF, + /// Prefers that S=2 coding be used when transmitting on the LE Coded PHY. + BLE_GAP_PHY_OPT_S2_LE_CODED_TX_PREF, + /// Prefers that S=8 coding be used when transmitting on the LE Coded PHY. + BLE_GAP_PHY_OPT_S8_LE_CODED_TX_PREF, +} ble_gap_phy_opt_t; + +typedef enum { + /// No role set yet + BLE_GAP_ROLE_NONE = 0x00, + /// Observer role + BLE_GAP_ROLE_OBSERVER = 0x01, + /// Broadcaster role + BLE_GAP_ROLE_BROADCASTER = 0x02, + /// Master/Central role + BLE_GAP_ROLE_CENTRAL = (0x04 | BLE_GAP_ROLE_OBSERVER), + /// Peripheral/Slave role + BLE_GAP_ROLE_PERIPHERAL = (0x08 | BLE_GAP_ROLE_BROADCASTER), + /// Device has all role, both peripheral and central + BLE_GAP_ROLE_ALL = (BLE_GAP_ROLE_CENTRAL | BLE_GAP_ROLE_PERIPHERAL), +} ble_gap_role_t; + +/// Privacy configuration +typedef enum { + /// Identity address type. Public address:0, Static private random + /// address:1 + BLE_GAP_PRIV_CFG_PRIV_ADDR_BIT = (1 << 0), + /// Reserved + BLE_GAP_PRIV_CFG_RSVD = (1 << 1), + /// Enable controller privacy + BLE_GAP_PRIV_CFG_PRIV_EN_BIT = (1 << 2), +} ble_gap_priv_cfg_t; + +/// Pairing Mode +typedef enum { + /// No pairing authorized + BLE_GAP_PAIRING_DISABLE = 0, + /// Legacy pairing Authorized + BLE_GAP_PAIRING_LEGACY = (1 << 0), + /// Secure Connection pairing Authorized + BLE_GAP_PAIRING_SEC_CON = (1 << 1), +} ble_gap_pairing_mode_t; + +/// Device Attribute write permission requirement +typedef enum { + /// Disable write access + BLE_GAP_WRITE_DISABLE, + /// Enable write access - no authentication required + BLE_GAP_WRITE_NO_AUTH, + /// Write access requires unauthenticated link + BLE_GAP_WRITE_UNAUTH, + /// Write access requires authenticated link + BLE_GAP_WRITE_AUTH, + /// Write access requires secure connected link + BLE_GAP_WRITE_SEC_CON, +} ble_gap_write_att_perm_t; + +/// Attribute database configuration +/// 8 7 6 5 4 3 2 1 0 +/// +-----+----+-----+----+----+----+----+----+----+ +/// | MTU | SC | PCP | APP_PERM | NAME_PERM | +/// +-----+----+-----+----+----+----+----+----+----+ +/// 15 14 13 12 11 10 9 +/// +----+----+----+----+------+------+----+ +/// | RFU | RPAO | EATT | FE | +/// +----+----+----+----+------+------+----+ +/// - Bit [0-2] : Device Name write permission requirements for peer device. +/// (@see ble_gap_write_att_perm_t) +/// - Bit [3-5] : Device Appearance write permission requirements for peer +/// device (@see ble_gap_write_att_perm_t) +/// - Bit [6] : Peripheral Preferred Connection Parameters present. +/// - Bit [7] : Service change feature present in GATT attribute database. +/// - Bit [8] : Enable automatic MTU exchange at connection establishment. +/// - Bit [9] : Enable automatic client feature enable setup at connection +/// establishment. +/// - Bit [10] : Enable automatic establishment of Enhanced ATT bearers. +/// - Bit [11] : Enable presence of Resolvable private address only. +/// - Bit [12-15]: Reserve for future use. +typedef enum { + /// Device Name write permission requirements for peer device + /// (@see ble_gap_write_att_perm_t) + BLE_GAP_ATT_NAME_PERM_MASK = 0x0007, + BLE_GAP_ATT_NAME_PERM_POS = 0, + /// Device Appearance write permission requirements for peer device + /// (@see ble_gap_write_att_perm_t) + BLE_GAP_ATT_APPEARENCE_PERM_MASK = 0x0038, + BLE_GAP_ATT_APPEARENCE_PERM_POS = 3, + /// Peripheral Preferred Connection Parameters present in GAP attribute + /// database. + BLE_GAP_ATT_PERIPH_PREF_CON_PAR_EN_MASK = 0x0040, + BLE_GAP_ATT_PERIPH_PREF_CON_PAR_EN_POS = 6, + /// Service change feature present in GATT attribute database. + BLE_GAP_ATT_SVC_CHG_EN_MASK = 0x0080, + BLE_GAP_ATT_SVC_CHG_EN_POS = 7, + /// Automatic MTU exchange at connection establishment. + BLE_GAP_ATT_CLI_AUTO_MTU_EXCH_EN_MASK = 0x0100, + BLE_GAP_ATT_CLI_AUTO_MTU_EXCH_EN_POS = 8, + /// Automatic client feature enable + BLE_GAP_ATT_CLI_AUTO_FEAT_EN_MASK = 0x0200, + BLE_GAP_ATT_CLI_AUTO_FEAT_EN_POS = 9, + /// Automatic establishment of Enhanced ATT bearers. + BLE_GAP_ATT_CLI_AUTO_EATT_EN_MASK = 0x0400, + BLE_GAP_ATT_CLI_AUTO_EATT_POS = 10, + /// Presence of Resolvable private address only. + BLE_GAP_ATT_RSLV_PRIV_ADDR_ONLY_MASK = 0x0800, + BLE_GAP_ATT_RSLV_PRIV_ADDR_ONLY_POS = 11, +} ble_gap_att_cfg_flag_t; + +typedef struct { + /// Device Role: Central, Peripheral, Observer, Broadcaster or All roles. + ble_gap_role_t role; + + /// Privacy Config + /// Duration before regenerate device address when privacy is enabled. + /// unit:sec + uint16_t renew_dur; + /// Provided own static private random address + ble_bdaddr_t addr; + /// Device IRK used for resolvable random BD address generation (LSB first) + ble_gap_key_t irk; + /// Privacy configuration bit field + ble_gap_priv_cfg_t privacy_cfg; + + /// Security Config + /// Pairing mode authorized + ble_gap_pairing_mode_t pairing_mode; + + /// ATT Database Config + /// GAP service start handle + uint16_t gap_start_hdl; + /// GATT service start handle + uint16_t gatt_start_hdl; + /// Attribute database configuration + ble_gap_att_cfg_flag_t att_cfg; + + /// LE Data Length Extension + /// Suggested value for the Controller's maximum transmitted number of + /// payload octets to be used + uint16_t sugg_max_tx_octets; + /// Suggested value for the Controller's maximum packet transmission time to + /// be used (unit: micro sec) + uint16_t sugg_max_tx_time; + + /// L2CAP Configuration + /// Maximal MTU size + uint16_t max_mtu; + /// Maximal MPS Packet size + uint16_t max_mps; + /// Maximum number of LE Credit based connection that can be established + uint8_t max_nb_lecb; + + /// LE PHY Management + /// Preferred LE PHY for data transmission + ble_gap_phy_mode_t tx_pref_phy; + /// Preferred LE PHY for data reception + ble_gap_phy_mode_t rx_pref_phy; + + /// Other settings + /// RF TX Path Compensation value (from -128dB to 128dB, unit is 0.1dB) + uint16_t tx_path_comp; + /// RF RX Path Compensation value (from -128dB to 128dB, unit is 0.1dB) + uint16_t rx_path_comp; +} ble_gap_set_dev_config_t; + +/// BLE peer info type +typedef enum { + /// Retrieve name of peer device. + BLE_GAP_GET_PEER_NAME, + /// Retrieve peer device version info. + BLE_GAP_GET_PEER_VERSION, + /// Retrieve peer device features. + BLE_GAP_GET_PEER_FEATURES, + /// Retrieve connection RSSI. + BLE_GAP_GET_CON_RSSI, + /// Retrieve connection channel map. + BLE_GAP_GET_CON_CHANNEL_MAP, + /// Get peer device appearance. + BLE_GAP_GET_PEER_APPEARANCE, + /// Get peer device peripheral preferred parameters. + BLE_GAP_GET_PEER_PERIPH_PREF_PARAMS, + /// Get timer timeout value. + BLE_GAP_GET_LE_PING_TO, + /// Central address resolution supported. + BLE_GAP_GET_ADDR_RESOL_SUPP, + /// Retrieve PHY configuration of active link. + BLE_GAP_GET_PHY, + /// Retrieve channel selection algorithm. + BLE_GAP_GET_CHAN_SEL_ALGO, +} ble_gap_link_info_type_t; + +/// Peer irk structure +typedef struct { + /// irk information + ble_gap_irk_t irk; + /// Bonding Index + int idx; +} ble_gap_irk_idx_t; + +/// Resolving address command complete event data structure +typedef struct { + /// Index of irk + uint8_t index; + /// Number of irk + uint8_t nb; + /// Irk + ble_gap_irk_idx_t irk[__ARRAY_EMPTY]; +} resl_addr_t; + +/// Connection parameter +typedef struct { + /// Connection interval minimum + uint16_t intv_min; + /// Connection interval maximum + uint16_t intv_max; + /// Latency + uint16_t latency; + /// Supervision timeout + uint16_t time_out; +} ble_gap_conn_param_t; + +typedef struct { + /// GAP requested operation + uint8_t operation; + /// Status of the request + uint8_t status; +} gapm_cmp_evt_t; + +/// The list of names for registered gap event function. +typedef enum { + /// Handler in atm_gap. + GAP_EVT_NAME_ATM_GAP, + /// Handler in ble_gap_sec. + GAP_EVT_NAME_BLE_GAP_SEC, + /// Handler in test. + GAP_EVT_NAME_TEST, + /// Max number of this enumerations. + GAP_EVT_NAME_MAX, +} ble_gap_evt_reg_name_t; + +typedef struct { + /// 8-byte array for random number + uint8_t nb[BLE_GAP_RAND_NB_LEN]; +} ble_rand_nb_t; + +typedef struct ble_gap_ext_adv_report_ind { + /// Bit field info + ble_gap_adv_report_info_t info; + /// Transmitter device address + ble_gap_bdaddr_t trans_addr; + /// Target device address + /// Valid only for directed advertising report + ble_gap_bdaddr_t target_addr; + /// TX power (in dBm) + int8_t tx_pwr; + /// RSSI (between -127 and +20 dBm) + int8_t rssi; + /// Primary PHY (1:1M, 3:Coded) + uint8_t phy_prim; + /// Secondary PHY (0:No packet on secondary phy, 1:1M, 2:2M, 3:Coded) + uint8_t phy_second; + /// Advertising SID + /// Valid only for periodic advertising report + uint8_t adv_sid; + /// Periodic advertising interval (in unit of 1.25ms, min is 7.5ms) + /// Valid only for periodic advertising report + uint16_t period_adv_intv; + /// Report length + uint16_t length; + /// Report + uint8_t data[__ARRAY_EMPTY]; +} ble_gap_ind_ext_adv_report_t; + +/// Device name +typedef struct { + /// Name length + uint16_t value_length; + /// Name value (starting from offset to maximum length for BLE_GAP_DEV_NAME + /// request) + uint8_t value[__ARRAY_EMPTY]; +} ble_gap_dev_name_t; + +typedef struct { + uint8_t con_idx; + ble_gap_dev_name_t param; +} ble_gap_le_dev_name_t; + +typedef struct { + uint8_t con_idx; + /// Appearance Icon + uint16_t appearance; +} ble_gap_le_appearance_t; + +/// Peripheral preference parameters +typedef struct { + /// Connection interval minimum + uint16_t con_intv_min; + /// Connection interval maximum + uint16_t con_intv_max; + /// Peripheral latency + uint16_t periph_latency; + /// Connection supervision timeout multiplier + uint16_t con_timeout; +} ble_gap_periph_pref_t; + +typedef struct { + uint8_t con_idx; + ble_gap_periph_pref_t param; +} ble_gap_le_periph_pref_t; + +typedef struct { + uint8_t con_idx; + /// Resolvable Private address only + uint8_t rslv_priv_addr_only; +} ble_gap_le_rslv_priv_addr_only_t; + +/// Peer version info +typedef struct { + /// Manufacturer name + uint16_t comp_id; + /// LMP subversion + uint16_t lmp_subvers; + /// LMP version + uint8_t lmp_vers; +} ble_gap_peer_version_t; + +typedef struct { + uint8_t con_idx; + ble_gap_peer_version_t param; +} ble_gap_le_peer_version_t; + +/// Peer features info +typedef struct { + /// 8-byte array for LE features + uint8_t features[BLE_GAP_LE_FEATS_LEN]; +} ble_gap_peer_features_t; + +typedef struct { + uint8_t con_idx; + ble_gap_peer_features_t param; +} ble_gap_le_peer_features_t; + +/// Ongoing connection RSSI +typedef struct { + /// RSSI value + int8_t rssi; +} ble_gap_con_rssi_t; + +typedef struct { + uint8_t con_idx; + ble_gap_con_rssi_t param; +} ble_gap_le_con_rssi_t; + +/// Channel map structure +typedef struct { + /// 5-byte channel map array + uint8_t map[BLE_GAP_LE_CHNL_MAP_LEN]; +} ble_chnl_map_t; + +/// Ongoing connection Channel Map +typedef struct { + /// Channel map value + ble_chnl_map_t ch_map; +} ble_gap_con_channel_map_t; + +typedef struct { + uint8_t con_idx; + ble_gap_con_channel_map_t param; +} ble_gap_le_con_channel_map_t; + +/// LE Ping +typedef struct { + /// Authenticated payload timeout + uint16_t timeout; +} ble_gap_le_ping_to_val_t; + +typedef struct { + uint8_t con_idx; + ble_gap_le_ping_to_val_t param; +} ble_gap_le_le_ping_to_val_t; + +/// Active link PHY configuration. Triggered when configuration is read or +/// during an update. +typedef struct { + /// LE PHY for data transmission. + ble_gap_phy_val_t tx_phy; + /// LE PHY for data reception. + ble_gap_phy_val_t rx_phy; +} ble_gap_le_phy_t; + +typedef struct { + uint8_t con_idx; + ble_gap_le_phy_t param; +} ble_gap_le_le_phy_t; + +/// Channel selection algorithm +typedef struct { + /// Used channel selection algorithm + uint8_t chan_sel_algo; +} ble_gap_chan_sel_algo_t; + +typedef struct { + uint8_t con_idx; + ble_gap_chan_sel_algo_t param; +} ble_gap_le_chan_sel_algo_t; + +/// Response data of @ref ble_gapc_get_link_info +typedef struct { + /// Get information type. + ble_gap_link_info_type_t type; + union { + /// Data for BLE_GAP_GET_PEER_NAME type + ble_gap_le_dev_name_t name; + /// Data for BLE_GAP_GET_PEER_APPEARANCE type + ble_gap_le_appearance_t appearance; + /// Data for BLE_GAP_GET_PEER_PERIPH_PREF_PARAMS type + ble_gap_le_periph_pref_t periph_pref; + /// Data for BLE_GAP_GET_ADDR_RESOL_SUPP type + ble_gap_le_rslv_priv_addr_only_t rslv_priv_addr_only; + /// Data for BLE_GAP_GET_PEER_VERSION type + ble_gap_le_peer_version_t peer_ver; + /// Data for BLE_GAP_GET_PEER_FEATURES type + ble_gap_le_peer_features_t peer_feat; + /// Data for BLE_GAP_GET_CON_RSSI type + ble_gap_le_con_rssi_t con_rssi; + /// Data for BLE_GAP_GET_CON_CHANNEL_MAP type + ble_gap_le_con_channel_map_t con_chnl_map; + /// Data for BLE_GAP_GET_LE_PING_TO type + ble_gap_le_le_ping_to_val_t le_ping_to; + /// Data for BLE_GAP_GET_PHY type + ble_gap_le_le_phy_t le_phy; + /// Data for BLE_GAP_GET_CHAN_SEL_ALGO type + ble_gap_le_chan_sel_algo_t chan_sel_algo; + }; +} ble_gap_link_info_t; + +typedef struct { + /// Activity identifier + uint8_t actv_idx; + /// Activity stop reason + ble_err_code_t reason; + /// In case of periodic advertising, indicate if periodic advertising + /// has been stopped + uint8_t per_adv_stop; +} ble_gap_ind_stop_t; + +typedef struct { + /// Connection handle + uint16_t conhdl; + /// Connection interval + uint16_t con_interval; + /// Connection latency + uint16_t con_latency; + /// Supervision timeout + uint16_t sup_to; + /// Clock accuracy + uint8_t clk_accuracy; + /// Peer address type + uint8_t peer_addr_type; + /// Peer BT address + ble_bdaddr_t peer_addr; + /// Role of device in connection (0 = Central / 1 = Peripheral) + uint8_t role; +} ble_gap_ind_con_est_t; + +typedef struct { + /// Connection index + uint8_t conidx; + /// Connection parameter + ble_gap_ind_con_est_t param; +} ble_gap_ind_le_con_est_t; + +/// Long Term Key information +typedef struct { + /// Long Term Key + ble_gap_key_t ltk; + /// Encryption Diversifier + uint16_t ediv; + /// Random Number + ble_rand_nb_t randnb; + /// Encryption key size (7 to 16) + uint8_t key_size; +} ble_gap_ltk_t; + +typedef struct { + /// Connection index + uint8_t conidx; + /// Long Term Key information + ble_gap_ltk_t ltk; +} ble_gap_ind_le_ltk_t; + +typedef struct { + /// Connection index + uint8_t conidx; +} ble_gap_ind_le_repeated_attempt_t; + +/// Authentication mask +typedef enum { + /// No Flag set + BLE_GAP_AUTH_NONE = 0, + /// Bond authentication + BLE_GAP_AUTH_BOND = (1 << 0), + /// Man In the middle protection + BLE_GAP_AUTH_MITM = (1 << 2), + /// Secure Connection + BLE_GAP_AUTH_SEC_CON = (1 << 3), + /// Key Notification + BLE_GAP_AUTH_KEY_NOTIF = (1 << 4), +} ble_gap_auth_mask_t; + +/// Authentication Requirements +typedef enum { + /// No MITM No Bonding + BLE_GAP_AUTH_REQ_NO_MITM_NO_BOND = BLE_GAP_AUTH_NONE, + /// No MITM Bonding + BLE_GAP_AUTH_REQ_NO_MITM_BOND = BLE_GAP_AUTH_BOND, + /// MITM No Bonding + BLE_GAP_AUTH_REQ_MITM_NO_BOND = BLE_GAP_AUTH_MITM, + /// MITM and Bonding + BLE_GAP_AUTH_REQ_MITM_BOND = BLE_GAP_AUTH_MITM | BLE_GAP_AUTH_BOND, + /// SEC_CON, No MITM and No Bonding + BLE_GAP_AUTH_REQ_NO_MITM_SEC_CON_NO_BOND = BLE_GAP_AUTH_SEC_CON, + /// SEC_CON, No MITM and Bonding + BLE_GAP_AUTH_REQ_NO_MITM_SEC_CON_BOND = BLE_GAP_AUTH_SEC_CON | + BLE_GAP_AUTH_BOND, + /// SEC_CON, MITM and No Bonding + BLE_GAP_AUTH_REQ_MITM_SEC_CON_NO_BOND = BLE_GAP_AUTH_SEC_CON | + BLE_GAP_AUTH_MITM, + /// SEC_CON, MITM and Bonding + BLE_GAP_AUTH_REQ_MITM_SEC_CON_BOND = BLE_GAP_AUTH_SEC_CON | + BLE_GAP_AUTH_MITM | BLE_GAP_AUTH_BOND, + + /// Mask of authentication features without reserved flag + BLE_GAP_AUTH_REQ_MASK = 0x1F +} ble_gap_auth_t; + +typedef enum { + /// Unauthenticated pairing achieved but without bond data + /// (meaning-less for connection confirmation) + BLE_GAP_PAIRING_UNAUTH = BLE_GAP_AUTH_NONE, + /// Authenticated pairing achieved but without bond data + /// (meaning-less for connection confirmation) + BLE_GAP_PAIRING_AUTH = BLE_GAP_AUTH_MITM, + /// Secure connection pairing achieved but without bond data + /// (meaning-less for connection confirmation) + BLE_GAP_PAIRING_SECURE_CON = BLE_GAP_AUTH_SEC_CON | BLE_GAP_AUTH_MITM, + + /// No pairing performed with peer device + /// (meaning-less for connection confirmation) + BLE_GAP_PAIRING_NO_BOND = BLE_GAP_AUTH_NONE, + /// Peer device bonded through an unauthenticated pairing. + BLE_GAP_PAIRING_BOND_UNAUTH = BLE_GAP_AUTH_BOND, + /// Peer device bonded through an authenticated pairing. + BLE_GAP_PAIRING_BOND_AUTH = BLE_GAP_AUTH_MITM | BLE_GAP_AUTH_BOND, + /// Peer device bonded through a secure connection pairing pairing. + BLE_GAP_PAIRING_BOND_SECURE_CON = BLE_GAP_AUTH_SEC_CON | BLE_GAP_AUTH_MITM | + BLE_GAP_AUTH_BOND, + + /// Pairing with bond data present Bit + BLE_GAP_PAIRING_BOND_PRESENT_BIT = 0x01, + BLE_GAP_PAIRING_BOND_PRESENT_POS = 0x00, +} ble_gap_pairing_level_t; + +typedef struct { + /// Connection handle + uint8_t conidx; + /// Error code + ble_err_code_t reason; + /// Pairing security level. (Valid if reason == BLE_ERR_NO_ERROR) + ble_gap_pairing_level_t pairing_lvl; + /// LTK is exchanged. (Valid if reason == BLE_ERR_NO_ERROR) + bool ltk_present; +} ble_gap_ind_le_pair_end_t; + +typedef struct { + /// Connection handle + uint16_t conhdl; + /// Reason of disconnection + ble_err_code_t reason; +} ble_gap_ind_discon_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_discon_t param; +} ble_gap_ind_le_discon_t; + +/// Privacy mode type +typedef enum { + /// Network privacy mode + BLE_GAP_PRIV_TYPE_NETWORK, + /// Device privacy mode + BLE_GAP_PRIV_TYPE_DEVICE, +} ble_gap_priv_type_t; + +/// Resolving list device information +typedef struct { + /// Device identity + ble_gap_bdaddr_t addr; + /// Privacy Mode + ble_gap_priv_type_t priv_mode; + /// Peer IRK + uint8_t peer_irk[BLE_GAP_KEY_LEN]; + /// Local IRK + uint8_t local_irk[BLE_GAP_KEY_LEN]; +} ble_gap_ral_dev_info_t; + +typedef struct { + /// Connection interval used on this connection, unit:1.25 ms + uint16_t con_interval; + /// Peripheral latency for the connection in number of connection events, + /// range: 0x0000 to 0x01F3 + uint16_t con_latency; + /// Supervision timeout for this connection, unit: 10ms + uint16_t sup_to; +} ble_gap_ind_con_update_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_con_update_t param; +} ble_gap_ind_le_con_update_t; + +typedef enum { + /// Device name + BLE_GAP_DEV_NAME, + /// Device external appearance + BLE_GAP_DEV_APPEARANCE, + /// Peripheral preferred connection parameters + BLE_GAP_DEV_PERIPH_PREF_PARAMS, + /// Central address resolution supported + BLE_GAP_DEV_CTL_ADDR_RESOL, + /// Device database hash value + BLE_GAP_DEV_DB_HASH, + /// Resolvable private addresss as local addresss only + BLE_GAP_DEV_RSLV_PRIV_ADDR_ONLY, + /// Maximum device info parameter + BLE_GAP_DEV_INFO_MAX, +} ble_gap_dev_info_t; + +typedef union { + /// Device name + ble_gap_dev_name_t name; + /// Appearance Icon + uint16_t appearance; +} ble_gap_set_dev_info_t; + +typedef struct { + /// Requested device information + ble_gap_dev_info_t req_type; + /// Token value + uint16_t token; + /// Device information data + ble_gap_set_dev_info_t info; +} ble_gap_ind_set_dev_req_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_set_dev_req_t param; +} ble_gap_ind_le_set_dev_req_t; + +typedef struct { + /// true to accept connection parameters, false else. + bool accept; + /// Minimum length of connection event needed for this LE connection in + /// 0.625 ms. + uint16_t ce_len_min; + /// Maximum length of connection event needed for this LE connection in + /// 0.625 ms. + uint16_t ce_len_max; +} ble_gapc_param_update_cfm_t; + +typedef struct { + /// Connection interval minimum in 1.25 ms + uint16_t intv_min; + /// Connection interval maximum in 1.25 ms + uint16_t intv_max; + /// Latency + uint16_t latency; + /// Supervision timeout in 10 ms + uint16_t time_out; +} ble_gap_ind_param_update_req_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_param_update_req_t param; +} ble_gap_ind_le_param_update_req_t; + +typedef struct { + /// PHY updated + ble_gap_le_phy_t phy; +} ble_gap_ind_le_phy_t; + +typedef struct { + uint8_t con_idx; + ble_gap_ind_le_phy_t param; +} ble_gap_ind_le_le_phy_t; + +typedef struct { + /// Channel selection algorithm updated + ble_gap_chan_sel_algo_t algo; +} ble_gap_ind_chan_sel_algo_t; + +typedef struct { + uint8_t con_idx; + ble_gap_ind_chan_sel_algo_t param; +} ble_gap_ind_le_chan_sel_algo_t; + +/// Additional advertising parameters +typedef struct { + /// Advertising duration (in unit of 10ms). 0 means that advertising + /// continues until the host disable it + uint16_t duration; + /// Maximum number of extended advertising events the controller shall + /// attempt to send prior to terminating the extending advertising Valid + /// only if extended advertising + uint8_t max_adv_evt; +} ble_gapm_adv_param_t; + +/// Scan window operation parameters +typedef struct { + /// Scan interval + uint16_t scan_intv; + /// Scan window + uint16_t scan_wd; +} ble_gapm_scan_wd_op_param_t; + +/// Scanning types +typedef enum { + /// General discovery + BLE_GAPM_SCAN_TYPE_GEN_DISC, + /// Limited discovery + BLE_GAPM_SCAN_TYPE_LIM_DISC, + /// Observer + BLE_GAPM_SCAN_TYPE_OBSERVER, + /// Selective observer + BLE_GAPM_SCAN_TYPE_SEL_OBSERVER, + /// Connectable discovery + BLE_GAPM_SCAN_TYPE_CONN_DISC, + /// Selective connectable discovery + BLE_GAPM_SCAN_TYPE_SEL_CONN_DISC, +} ble_gapm_scan_type_t; + +/// Scanning properties bit field bit value +typedef enum { + /// Scan advertisement on the LE 1M PHY + BLE_GAPM_SCAN_PROP_PHY_1M_BIT = (1 << 0), + /// Scan advertisement on the LE Coded PHY + BLE_GAPM_SCAN_PROP_PHY_CODED_BIT = (1 << 1), + /// Active scan on LE 1M PHY (Scan Request PDUs may be sent) + BLE_GAPM_SCAN_PROP_ACTIVE_1M_BIT = (1 << 2), + /// Active scan on LE Coded PHY (Scan Request PDUs may be sent) + BLE_GAPM_SCAN_PROP_ACTIVE_CODED_BIT = (1 << 3), + /// Accept directed advertising packets if we use a RPA and target address + /// cannot be solved by the controller + BLE_GAPM_SCAN_PROP_ACCEPT_RPA_BIT = (1 << 4), + /// Filter truncated advertising or scan response reports + BLE_GAPM_SCAN_PROP_FILT_TRUNC_BIT = (1 << 5), +} ble_gapm_scan_prop_t; + +/// Filtering policy for duplicated packets +typedef enum { + /// Disable filtering of duplicated packets + BLE_GAPM_DUP_FILT_DIS, + /// Enable filtering of duplicated packets + BLE_GAPM_DUP_FILT_EN, + /// Enable filtering of duplicated packets, reset for each scan period + BLE_GAPM_DUP_FILT_EN_PERIOD, +} ble_gapm_dup_filter_pol_t; + +/// Scanning parameters +typedef struct { + /// Type of scanning to be started + ble_gapm_scan_type_t type; + /// Properties for the scan procedure + ble_gapm_scan_prop_t prop; + /// Duplicate packet filtering policy + ble_gapm_dup_filter_pol_t dup_filt_pol; + /// Reserved for future use + uint8_t rsvd; + /// Scan window opening parameters for LE 1M PHY + ble_gapm_scan_wd_op_param_t scan_param_1m; + /// Scan window opening parameters for LE Coded PHY + ble_gapm_scan_wd_op_param_t scan_param_coded; + /// Scan duration (in unit of 10ms). 0 means that the controller will scan + /// continuously until reception of a stop command from the application + uint16_t duration; + /// Scan period (in unit of 1.28s). Time interval between two consequent + /// starts of a scan duration by the controller. 0 means that the scan + /// procedure is not periodic + uint16_t period; +} ble_gapm_scan_param_t; + +/// Initiating types +typedef enum { + /// Direct connection establishment, establish a connection with an + /// indicated device + BLE_GAPM_INIT_TYPE_DIRECT_CONN_EST, + /// Automatic connection establishment, establish a connection with all + /// devices whose address is present in the white list + BLE_GAPM_INIT_TYPE_AUTO_CONN_EST, + /// Name discovery, Establish a connection with an indicated device in order + /// to read content of its Device Name characteristic. Connection is closed + /// once this operation is stopped. + BLE_GAPM_INIT_TYPE_NAME_DISC, +} ble_gapm_init_type_t; + +/// Initiating properties +typedef enum { + /// Scan connectable advertisements on the LE 1M PHY. Connection parameters + /// for the LE 1M PHY are provided + BLE_GAPM_INIT_PROP_1M_BIT = (1 << 0), + /// Connection parameters for the LE 2M PHY are provided + BLE_GAPM_INIT_PROP_2M_BIT = (1 << 1), + /// Scan connectable advertisements on the LE Coded PHY. Connection + /// parameters for the LE Coded PHY are provided + BLE_GAPM_INIT_PROP_CODED_BIT = (1 << 2), +} ble_gapm_init_prop_t; + +/// Connection parameters +typedef struct { + /// Minimum value for the connection interval (in unit of 1.25ms). Shall be + /// less than or equal to conn_intv_max value. Allowed range is 7.5ms to 4s. + uint16_t conn_intv_min; + /// Maximum value for the connection interval (in unit of 1.25ms). Shall be + /// greater than or equal to conn_intv_min value. Allowed range is 7.5ms to + /// 4s. + uint16_t conn_intv_max; + /// Slave latency. Number of events that can be missed by a connected slave + /// device + uint16_t conn_latency; + /// Link supervision timeout (in unit of 10ms). Allowed range is 100ms to + /// 32s + uint16_t supervision_to; + /// Recommended minimum duration of connection events (in unit of 625us) + uint16_t ce_len_min; + /// Recommended maximum duration of connection events (in unit of 625us) + uint16_t ce_len_max; +} ble_gapm_conn_param_t; + +/// Initiating parameters +typedef struct { + /// Initiating type + ble_gapm_init_type_t type; + /// Properties for the initiating procedure + ble_gapm_init_prop_t prop; + /// Timeout for automatic connection establishment (in unit of 10ms). Cancel + /// the procedure if not all indicated devices have been connected when the + /// timeout occurs. 0 means there is no timeout + uint16_t conn_to; + /// Scan window opening parameters for LE 1M PHY + ble_gapm_scan_wd_op_param_t scan_param_1m; + /// Scan window opening parameters for LE Coded PHY + ble_gapm_scan_wd_op_param_t scan_param_coded; + /// Connection parameters for LE 1M PHY + ble_gapm_conn_param_t conn_param_1m; + /// Connection parameters for LE 2M PHY + ble_gapm_conn_param_t conn_param_2m; + /// Connection parameters for LE Coded PHY + ble_gapm_conn_param_t conn_param_coded; + /// Address of peer device in case white list is not used for connection + ble_gap_bdaddr_t peer_addr; +} ble_gapm_init_param_t; + +/// Periodic advertising information +typedef struct { + /// Advertiser address information + ble_gap_bdaddr_t addr; + /// Advertising SID + uint8_t adv_sid; +} ble_gapm_period_adv_addr_t; + +/// Periodic synchronization types +typedef enum { + /// Do not use periodic advertiser list for synchronization. Use advertiser + /// information provided in the ble_gapm_start_per_sync(). + BLE_GAPM_PER_SYNC_TYPE_GENERAL, + /// Use periodic advertiser list for synchronization + BLE_GAPM_PER_SYNC_TYPE_SELECTIVE, + /// Use Periodic advertising sync transfer information send through + /// connection for synchronization + BLE_GAPM_PER_SYNC_TYPE_PAST, +} ble_gapm_per_sync_type_t; + +/// Bit field of enabled advertising reports +typedef enum { + /// Periodic advertising reports reception enabled + BLE_GAPM_REPORT_ADV_EN_BIT = 0x01, + BLE_GAPM_REPORT_ADV_EN_POS = 0, + /// BIG Info advertising reports reception enabled + BLE_GAPM_REPORT_BIGINFO_EN_BIT = 0x02, + BLE_GAPM_REPORT_BIGINFO_EN_POS = 1, + /// Periodic advertising reports reception enabled with duplicate filtering + /// enabled. + BLE_GAPM_REPORT_ADV_DUP_FIL_EN_BIT = 0x04, + BLE_GAPM_REPORT_ADV_DUP_FIL_EN_POS = 2, +} ble_gapm_report_en_bf_t; + +/// Constant Tone Extension sync filtering type +typedef enum { + /// Do not sync to packets with an AoA Constant Tone Extension + BLE_GAPM_CTE_NO_SYNC_WITH_AOA = (1 << 0), + /// Do not sync to packets with an AoD Constant Tone Extension with 1 us + /// slots + BLE_GAPM_CTE_NO_SYNC_WITH_AOD_1US_SLOT = (1 << 1), + /// Do not sync to packets with an AoD Constant Tone Extension with 2 us + /// slots + BLE_GAPM_CTE_NO_SYNC_WITH_AOD_2US_SLOT = (1 << 2), + /// Do not sync to packets with a type 3 Constant Tone Extension (currently + /// reserved for future use) + BLE_GAPM_CTE_NO_SYNC_WITH_TYPE_3 = (1 << 3), + /// Do not sync to packets without a Constant Tone Extension + BLE_GAPM_CTE_NO_SYNC_WITHOUT_CTE = (1 << 4), +} ble_gapm_sync_cte_type_t; + +/// Periodic synchronization parameters +typedef struct { + /// Number of periodic advertising that can be skipped after a successful + /// receive. Maximum authorized value is 499. + uint16_t skip; + /// Synchronization timeout for the periodic advertising (in unit of 10ms + /// between 100ms and 163.84s) + uint16_t sync_to; + /// Periodic synchronization type + ble_gapm_per_sync_type_t type; + /// Connection index used for periodic sync info reception (only valid for + /// BLE_GAPM_PER_SYNC_TYPE_PAST) + uint8_t conidx; + /// Address of advertiser with which synchronization has to be established + /// (used only if type is BLE_GAPM_PER_SYNC_TYPE_GENERAL) + ble_gapm_period_adv_addr_t adv_addr; + /// Bit field that contains list of reports that are enabled or not. + ble_gapm_report_en_bf_t report_en_bf; + /// Type of Constant Tone Extension device should sync on. + ble_gapm_sync_cte_type_t cte_type; +} ble_gapm_per_sync_param_t; + +/// Type of activities that can be created +typedef enum { + /// Advertising activity + BLE_GAPM_ACTV_TYPE_ADV, + /// Scanning activity + BLE_GAPM_ACTV_TYPE_SCAN, + /// Initiating activity + BLE_GAPM_ACTV_TYPE_INIT, + /// Periodic synchronization activity + BLE_GAPM_ACTV_TYPE_PER_SYNC, +} ble_gapm_actv_type_t; + +typedef struct { + /// The maximum Tx payload size + uint16_t max_tx_octets; + /// The maximum Tx time (unit: us) + uint16_t max_tx_time; + /// The maximum Rx payload size + uint16_t max_rx_octets; + /// The maximum Rx time (unit: us) + uint16_t max_rx_time; +} ble_gap_ind_pkt_size_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_pkt_size_t param; +} ble_gap_ind_le_pkt_size_t; + +typedef struct { + /// Requested information + ble_gap_dev_info_t req; + /// Token value that must be returned in confirmation + uint16_t token; + /// Device name data offset + uint16_t name_offset; + /// Maximum name length (starting from offset) + uint16_t max_name_length; +} ble_gap_ind_get_dev_info_req_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_get_dev_info_req_t param; +} ble_gap_ind_le_get_dev_info_req_t; + +typedef struct { + /// Encryption status + ble_err_code_t status; + /// Pairing security level + ble_gap_pairing_level_t pairing_lvl; +} ble_gap_ind_encrypt_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_encrypt_t param; +} ble_gap_ind_le_encrypt_t; + +typedef struct { + /// Pairing security level + ble_gap_pairing_level_t pairing_lvl; +} ble_gap_encrypt_t; + +typedef struct { + uint8_t conidx; + ble_gap_encrypt_t param; +} ble_gap_le_encrypt_t; + +typedef enum { + /// Passkey entry started + BLE_GAP_PASSKEY_ENTRY_STARTED, + /// Passkey digit entered + BLE_GAP_PASSKEY_DIGIT_ENTERED, + /// Passkey digit erased + BLE_GAP_PASSKEY_DIGIT_ERASED, + /// Passkey cleared + BLE_GAP_PASSKEY_CLEARED, + /// Passkey entry completed + BLE_GAP_PASSKEY_ENTRY_COMPLETED, +} ble_gap_notif_type_t; + +typedef struct { + /// Keypress notification type + ble_gap_notif_type_t keypress_notif_type; +} ble_gap_ind_keypress_notif_t; + +typedef struct { + uint8_t conidx; + ble_gap_ind_keypress_notif_t param; +} ble_gap_ind_le_keypress_notif_t; + +typedef struct { + /// Requested information + ble_gap_dev_info_t req; + /// Status of the confirmation + ble_err_code_t status; + /// Token value provided in request indication + uint16_t token; + /// Complete value length including offset + uint16_t complete_length; + union { + /// Data for BLE_GAP_DEV_NAME request + ble_gap_dev_name_t name; + /// Data for BLE_GAP_DEV_APPEARANCE request + uint16_t appearance; + /// Data for BLE_GAP_DEV_PERIPH_PREF_PARAMS request + ble_gap_periph_pref_t periph_pref; + /// Data for BLE_GAP_DEV_CTL_ADDR_RESOL request + uint8_t ctl_addr_resol; + /// Data for BLE_GAP_DEV_DB_HASH request + uint8_t hash[BLE_GAP_DATABASE_HASH_LEN]; + /// Data for BLE_GAP_DEV_RSLV_PRIV_ADDR_ONLY request + /// 0: Only Resolvable Private Addresses will be used as local addresses + /// after bonding; 1-255: Reserved for future use. + uint8_t rslv_priv_addr_only; + }; +} ble_gap_get_dev_info_cfm_t; + +typedef struct { + /// Internally used + uint8_t operation; + /// Status of the request + ble_err_code_t status; + /// Connection index + uint8_t con_idx; + union { + /// Data for operation (@see enum ble_gap_link_info_type_t) + ble_gap_link_info_t info; + /// Authentication. Data in callback of @ref ble_gap_sec_encrypt_req + ble_gap_le_encrypt_t encrypt; + }; +} ble_gapc_cmpl_t; + +typedef struct { + /// Internally used + uint8_t operation; + /// Status of the request + ble_err_code_t status; + /// Extra data + union { + /// Random number. Data in callback of @ref ble_gapm_gen_rand. + ble_rand_nb_t randnb; + /// Resolved addr + resl_addr_t resladr; + /// Activity index. Data in callback of @ref ble_gapm_stop_activity + uint8_t actv_stop_idx; + /// Generated address which is returned data of + /// @ref ble_gapm_gen_rand_addr + ble_bdaddr_t randadr; + /// Generated encrypted data which is returned data of + /// @ref ble_gapm_enc_block + uint8_t enc_result[BLE_GAP_KEY_LEN]; + /// Number of received packets. Data in callback of + /// @ref ble_gapm_le_test_stop + uint16_t nb_packet_received; + /// Periodic sync activity index which is returned data of + /// @ref ble_gapm_create_persync_activity + uint8_t persync_act_idx; + /// Scan activity index which is returned data of + /// @ref ble_gapm_create_scan_activity + uint8_t scan_act_idx; + /// Initiating activity index which is returned data of + /// @ref ble_gapm_create_init_activity + uint8_t init_act_idx; + /// ADV creation information. Data in callback of + /// @ref ble_gapm_create_adv_activity. + struct { + /// ADV identifier + uint8_t adv_idx; + /// Selected TX power for ADV activity + int8_t tx_pwr; + } adv_created; + }; +} ble_gapm_cmpl_t; + +/// Invalid connection index +#define BLE_GAP_INVALID_CONIDX 0xFF + +/// GAP event +typedef enum { + /// Indication of ADV stop + BLE_GAP_EID_IND_ADV_STOP, + /// Indication of Scan stop + BLE_GAP_EID_IND_SCAN_STOP, + /// Indication of initiating stop + BLE_GAP_EID_IND_INIT_STOP, + /// Indication of periodic sync stop + BLE_GAP_EID_IND_PER_SYNC_STOP, + /// Indication of extended advertising report + BLE_GAP_EID_IND_EXT_ADV_REPORT, + /// Indication of connection established + BLE_GAP_EID_IND_CON_EST, + /// Indication of connection disconnected + BLE_GAP_EID_IND_DISCON, + /// Indication of LTK + BLE_GAP_EID_IND_LTK, + /// Indication of connection parameter updated + BLE_GAP_EID_IND_CON_UPDATE, + /// Indication of set device information request + BLE_GAP_EID_IND_SET_DEV_INFO_REQ, + /// Pairing end + BLE_GAP_EID_IND_PAIR_END, + /// Indication of parameter updating request + BLE_GAP_EID_IND_PARAM_UPDATE_REQ, + /// Indication of LE PHY + BLE_GAP_EID_IND_LE_PHY, + /// Indication of channel selection algorithm + BLE_GAP_EID_IND_CHAN_SEL_ALGO, + /// Indication of LE packet size + BLE_GAP_EID_IND_PKT_SIZE, + /// Indication of getting device information request + BLE_GAP_EID_IND_GET_DEV_INFO_REQ, + /// Indication of encrypt + BLE_GAP_EID_IND_ENCRYPT, + /// Indicatin of repeated attempt + BLE_GAP_EID_IND_REPEATED_ATTEMPT, + /// Indicatin of keypress notification + BLE_GAP_EID_IND_KEYPRESS_NOTIFICATION, +} ble_gap_eid_t; + +typedef struct { + /// Event ID + ble_gap_eid_t eid; + union { + /// Data for BLE_GAP_EID_IND_ADV_STOP event. + ble_gap_ind_stop_t ind_adv_stop; + /// Data for BLE_GAP_EID_IND_SCAN_STOP event. + ble_gap_ind_stop_t ind_scan_stop; + /// Data for BLE_GAP_EID_IND_INIT_STOP event. + ble_gap_ind_stop_t ind_init_stop; + /// Data for BLE_GAP_EID_IND_PER_SYNC_STOP event. + ble_gap_ind_stop_t ind_per_sync_stop; + /// Data for BLE_GAP_EID_IND_EXT_ADV_REPORT event. + ble_gap_ind_ext_adv_report_t ind_ext_adv_rpt; + /// Data for BLE_GAP_EID_IND_CON_EST event. + ble_gap_ind_le_con_est_t ind_con_est; + /// Data for BLE_GAP_EID_IND_DISCON event. + ble_gap_ind_le_discon_t ind_discon; + /// Data for BLE_GAP_EID_IND_LTK event. + ble_gap_ind_le_ltk_t ind_ltk; + /// Data for BLE_GAP_EID_IND_CON_UPDATE event. + ble_gap_ind_le_con_update_t ind_con_update; + // Data for BLE_GAP_EID_IND_SET_DEV_INFO_REQ event. + ble_gap_ind_le_set_dev_req_t ind_set_dev_req; + /// Data for BLE_GAP_EDI_IND_PAIR_END + ble_gap_ind_le_pair_end_t ind_pair_end; + /// Data for BLE_GAP_EID_IND_PARAM_UPDATE_REQ event. + ble_gap_ind_le_param_update_req_t ind_param_update_req; + /// Data for BLE_GAP_EID_IND_LE_PHY event. + ble_gap_ind_le_le_phy_t ind_le_phy; + /// Data for BLE_GAP_EID_IND_CHAN_SEL_ALGO event. + ble_gap_ind_le_chan_sel_algo_t ind_sel_algo; + /// Data for BLE_GAP_EID_IND_PKT_SIZE event. + ble_gap_ind_le_pkt_size_t ind_pkt_size; + /// Data for BLE_GAP_EID_IND_GET_DEV_INFO_REQ event. + ble_gap_ind_le_get_dev_info_req_t ind_get_dev_info_req; + /// Data for BLE_GAP_EID_IND_ENCRYPT event. + ble_gap_ind_le_encrypt_t ind_encrypt; + /// Data for BLE_GAP_EID_IND_REPEATED_ATTEMPT event. + ble_gap_ind_le_repeated_attempt_t ind_repeated_attempt; + /// Data for BLE_GAP_EID_IND_KEYPRESS_NOTIFICATION event. + ble_gap_ind_le_keypress_notif_t ind_keypress_notif; + }; +} ble_gap_evt_t; + +/// Own BD address source of the device +typedef enum { + /// Public or Private Static Address + BLE_OWN_STATIC_ADDR, + /// Generated resolvable private random address + BLE_OWN_GEN_RSLV_ADDR, + /// Generated non-resolvable private random address + BLE_OWN_GEN_NON_RSLV_ADDR, +} ble_own_addr_t; + +/// GAPC command complete callback prototype +/// @param[in] conidx Connection index. +/// @param[in] parm Parameter returned. +/// @param[in] ctx Context provided from calling API. +typedef void (*gapc_cmpl_cb)(uint8_t conidx, struct gapc_cmp_evt const *parm, + void const *ctx); + +/// GAPC message +/// @param[in] id message id +/// @param[in] conidx The connection index +/// @param[in] is_bcast Specify the message is broadcast or not +/// @param[in] parm message parameter +typedef void (*gapc_ind)(enum gapc_msg_id id, uint8_t conidx, bool is_bcast, + void const *parm); + +/// GAPM message callback +/// @param[in] id message id +/// @param[in] parm message parameter +typedef void (*gapm_ind)(enum gapm_msg_id id, void const *parm); + +/// GAPM add profile callback prototype +/// @param[in] parm Parameter returned. +/// @param[in] ctx Context provided from calling API. +typedef void (*gapm_profile_add_cb)(void *parm, void const *ctx); + +// FIXME: please remove comment below after coverting process is done. +// Coverting mapping for process of cleaning up violation of abstraction: +//| incument | succeesor | +//|=====================+======================| +//| gapc_cmpl_cb | ble_gapc_cmd_cb | +//| gapm_cmp_evt_t | ble_gapm_cmpl_t | +//| struct gapc_cmp_evt | ble_gapc_cmpl_t | +//| ble_gapm_reg_ind | ble_gap_evt_func_reg | +//| ble_gapc_reg_ind | ble_gap_evt_func_reg | +//| gapc_ind | ble_gap_evt_fn | +//| gapm_ind | ble_gap_evt_fn | +// The incumbent functions(structs) would be totally replaced finally. + +/// GAPC command complete callback prototype +/// @param[in] conidx Connection index. +/// @param[in] parm Parameter returned. +/// @param[in] ctx Context provided from calling API. +typedef void (*ble_gapc_cmd_cb)(uint8_t conidx, ble_gapc_cmpl_t const *parm, + void const *ctx); + +/// GAPM command complete callback prototype +/// @param[in] parm Parameter returned. +/// @param[in] ctx Context provided from calling API. +typedef void (*ble_gapm_cmd_cb)(ble_gapm_cmpl_t const *parm, void const *ctx); + +/// GAP event callback function +/// @param[in] event Event parameter +/// @return True if processed. If return value is false, the event will be sent +/// to next registered function. +typedef bool (*ble_gap_evt_fn)(ble_gap_evt_t const *event); + +/* + * MACROS + ******************************************************************************* + */ + +/// Invalid bond index +#define BLE_GAP_INVALID_BOND (-1) + + +/* + * GLOBAL C API + ******************************************************************************* + */ +/** + ******************************************************************************* + * @brief Register ble gap event callback + * + * @param[in] pri Priority for service order. 0 is the highest. + * @param[in] name Name of this event func. + * @param[in] fn Event function + ******************************************************************************* + */ +void ble_gap_evt_func_reg(uint8_t pri, ble_gap_evt_reg_name_t name, + ble_gap_evt_fn fn); + +/** + ******************************************************************************* + * @brief Get gap event handler + * + * @param[in] name Name string for event handler. + * @return Event function. NULL if not found. + ******************************************************************************* + */ +ble_gap_evt_fn ble_gap_evt_func_get(ble_gap_evt_reg_name_t name); + +/** + ******************************************************************************* + * @brief Set device configuration + * + * @param[in] cfg device configuration + * @param[in] cb Complete callback function. + * @param[in] ctx Context data. + ******************************************************************************* + */ +__NONNULL(1) +void ble_gapm_set_device_config(ble_gap_set_dev_config_t const *cfg, + ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Remove gap event handler + * + * @param[in] name Name string for event handler. + * @return Removed callback function. NULL if not found. + ******************************************************************************* + */ +ble_gap_evt_fn ble_gap_evt_func_rm(ble_gap_evt_reg_name_t name); + +/** + ******************************************************************************* + * @brief Generate random number + * + * @param[in] cb Complete callback function. The random number will be return in + * randnb data field of ble_gapm_cmpl_t which is passed as a argument of cb. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapm_gen_rand(ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Set content of the white list + * @param[in] count Number of entries to be added in the list. 0 means that list + * content has to be cleared. + * @param[in] addrs List of entries to be added in the list. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + * @param[in] ctx Context data. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapm_set_white_list(uint8_t count, ble_gap_bdaddr_t const addrs[], + ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Set content of the resolving list + * @param[in] count Number of entries to be added in the list. 0 means that list + * content has to be cleared. + * @param[in] rals List of entries to be added in the list. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapm_set_resolving_list(uint8_t count, + ble_gap_ral_dev_info_t const rals[], ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Lower slave latency locally + * @param[in] conidx Connection index. + * @param[in] latency + * @param[in] cb Command complete callback + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapc_lower_slave_latency_locally(uint8_t conidx, uint16_t latency, + ble_gapc_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Set the preferred slave event duration (for slave role configuration) + * @param[in] conidx Connection index. + * @param[in] dur Preferred event duration that the controller should use on a + * connection (N * 0.625 ms) + * @param[in] single_tx Slave transmits a single packet per connection event + * (False/True) + * @param[in] cb Command complete callback + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapc_set_prefer_slave_event_duration(uint8_t conidx, uint16_t dur, + bool single_tx, ble_gapc_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Start TX direct test mode + * @param[in] param Test TX parameters. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + ******************************************************************************* + */ +__NONNULL(1) +void ble_gapm_le_test_tx(ble_gap_test_tx_param_t const *param, + ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Start RX direct test mode. + * @param[in] param Test RX parameters. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + ******************************************************************************* + */ +__NONNULL(1) +void ble_gapm_le_test_rx(ble_gap_test_rx_param_t const *param, + ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Stop the direct test mode. + * @param[in] cb Command complete callback. + ******************************************************************************* + */ +__NONNULL_ALL +void ble_gapm_le_test_stop(ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Resolve provided random address using array of Identity Resolution Key + * (IRK) exchanged. + * @param[in] addr Resolvable random address to solve. + * @param[in] nb Number of provided IRK. + * @param[in] irkarr peer IRK + * @param[in] cb Command complete callback. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapm_resolv_addr(ble_bdaddr_t addr, uint8_t nb, ble_gap_irk_idx_t irkarr[], + ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Generate random address + * @param[in] addr_type random address type + * @param[in] cb Command complete or indication callback. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapm_gen_rand_addr(ble_gapm_rand_addr_type_t addr_type, + ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Encrypt data + * @param[in] key 128 bit operand (key) + * @param[in] data 128 bit operand (data) + * @param[in] cb Command complete callback. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapm_enc_block(uint8_t const key[BLE_GAP_KEY_LEN], + uint8_t const data[BLE_GAP_KEY_LEN], ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Stop an activity + * @param[in] idx Activity identifier + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. + ******************************************************************************* + */ +void ble_gapm_stop_activity(uint8_t idx, void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Delete activity + * @param[in] idx Activity identifier. + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. + ******************************************************************************* + */ +void ble_gapm_delete_activity(uint8_t idx, void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Set advertising data + * @param[in] data_type ADV data type + * @param[in] idx Activity identifier. + * @param[in] len Length of ADV data. + * @param[in] data ADV data array. + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. + ***************************************************************************** + */ +void ble_gapm_set_adv_data(ble_gapm_data_type_t data_type, uint8_t idx, + uint16_t len, uint8_t const *data, void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Get link information + * @param[in] info Operation correspond to information. + * @param[in] conidx Connection index. + * @param[in] cb Complete callback function. NULL means that the caller does not + * need to be informed after the command completion. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapc_get_link_info(uint8_t conidx, ble_gap_link_info_type_t info, + ble_gapc_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Set phy. rate + * @param[in] conidx Connection index. + * @param[in] tx_phy Tx phy. + * @param[in] rx_phy Rx phy. + * @param[in] phy_opt Coded phy. opt. + * @param[in] cb Complete callback function. NULL means that the caller does not + * need to be informed after the command completion. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapc_set_phy(uint8_t conidx, ble_gap_phy_mode_t tx_phy, + ble_gap_phy_mode_t rx_phy, ble_gap_phy_opt_t phy_opt, ble_gapc_cmd_cb cb, + void const *ctx); + +/** + ******************************************************************************* + * @brief Update connection parameters + * @param[in] conidx Connection index. + * @param[in] param Connection parameters. + * @param[in] cb Complete callback function. + * @param[in] ctx Context data. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapc_update_param(uint8_t conidx, ble_gap_conn_param_t const *param, + ble_gapc_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Disconnect specific link + * @param[in] conidx Connection index. + * @param[in] reason Reason for disconnection. Only allow BLE_HCI_MODULE and + * BLE_SMP_MODULE type. + * @param[in] cb Complete callback function. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapc_disconnect(uint8_t conidx, ble_err_code_t reason, ble_gapc_cmd_cb cb, + void const *ctx); + +/** + ******************************************************************************* + * @brief Set device IRK + * @param[in] irk IRK array data. Size is BLE_GAP_KEY_LEN(16). + * @param[in] cb Complete callback function. + * @param[in] ctx Context data. + ******************************************************************************* + */ +__NONNULL(1) +void ble_gapm_set_irk(uint8_t const irk[BLE_GAP_KEY_LEN], ble_gapm_cmd_cb cb, + void const *ctx); + +/** + ******************************************************************************* + * @brief Set device information confirmation (corresponding to + * BLE_GAP_EID_IND_SET_DEV_INFO_REQ event) + * @param[in] conidx Connection index. + * @param[in] req_type Request type + * @param[in] status Status of the request + * @param[in] token Token value + ******************************************************************************* + */ +void ble_gapc_set_dev_info_cfm(uint8_t conidx, ble_gap_dev_info_t req_type, + ble_err_code_t status, uint16_t token); + +/** + ******************************************************************************* + * @brief Retrieve connection address information + * @param[in] conidx Connection index + * @param[out] addr Pointer to peer addr. + * @return True if connection exists. + ******************************************************************************* + */ +__NONNULL(2) +bool ble_gapc_get_peer_addr(uint8_t conidx, ble_gap_bdaddr_t *addr); + +/** + ******************************************************************************* + * @brief Confirm reception of BLE_GAP_EID_IND_PARAM_UPDATE_REQ event + * @param[in] conidx Connection index. + * @param[in] param Confirmation parameters. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapc_param_update_cfm(uint8_t conidx, + ble_gapc_param_update_cfm_t const *param); + +/** + ******************************************************************************* + * @brief Confirm reception of BLE_GAP_EID_IND_GET_DEV_INFO_REQ event + * @param[in] conidx Connection index. + * @param[in] param Confirmation parameters. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapc_get_dev_info_cfm(uint8_t conidx, + ble_gap_get_dev_info_cfm_t const *param); + +/** + ******************************************************************************* + * @brief Create periodic sync activity + * @param[in] own_addr_type Own address type + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. + ******************************************************************************* + */ +__NONNULL(3) +void ble_gapm_create_persync_activity(ble_own_addr_t own_addr_type, + void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Create scan activity + * @param[in] own_addr_type Own address type + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. + ******************************************************************************* + */ +__NONNULL(3) +void ble_gapm_create_scan_activity(ble_own_addr_t own_addr_type, + void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Create initiating activity + * @param[in] own_addr_type Own address type + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. + ******************************************************************************* + */ +__NONNULL(3) +void ble_gapm_create_init_activity(ble_own_addr_t own_addr_type, + void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Start advertising + * @param[in] idx Activity identifier. + * @param[in] dur Advertising duration (in unit of 10ms). 0 means that + * advertising continues until the application stop actively. + * @param[in] max_evt Maximum number of extended advertising events the + * controller shall attempt to send prior to terminating the extending + * advertising. Valid only if extended advertising. + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + ******************************************************************************* + */ +void ble_gapm_start_adv(uint8_t idx, uint16_t dur, uint8_t max_evt, + void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Start scanning + * @param[in] idx Activity identifier. + * @param[in] param Scanning parameters. + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapm_start_scan(uint8_t idx, ble_gapm_scan_param_t const *param, + void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Start Initiating + * @param[in] idx Activity identifier. + * @param[in] param Initiating parameters. + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapm_start_init(uint8_t idx, ble_gapm_init_param_t const *param, + void const *ctx, ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Start periodic synchronization + * @param[in] idx Activity identifier. + * @param[in] param Periodic synchronization parameters. + * @param[in] ctx Context data. + * @param[in] cb Command complete callback. NULL means that the caller does not + * need to be informed after the command completion. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapm_start_per_sync(uint8_t idx, + ble_gapm_per_sync_param_t const *param, void const *ctx, + ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Create ADV activity + * @param[in] own_addr_type Own address type + * @param[in] param Advertising parameters for advertising creation. + * @param[in] ctx Context data + * @param[in] cb Command complete callback. + ******************************************************************************* + */ +__NONNULL(2) +void ble_gapm_create_adv_activity(ble_own_addr_t own_addr_type, + ble_gapm_adv_create_param_t const *param, void const *ctx, + ble_gapm_cmd_cb cb); + +/** + ******************************************************************************* + * @brief Get advertising create parameters from Flash NVDS. + * @param[in] is_wurx True if the adv is wurx. + * @param[in,out] own_addr_type Own addr type. + * @param[in,out] create Create parameter + * @return true for success + ******************************************************************************* + */ +__NONNULL_ALL +bool ble_gapm_get_adv_create_nvds(bool is_wurx, ble_own_addr_t *own_addr_type, + ble_gapm_adv_create_param_t *create); + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_BLE_GAP + diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_gap/ble_internal.h b/platform/atm2/ATM22xx-x1x/lib/ble_gap/ble_internal.h new file mode 100644 index 0000000..6c04a8a --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_gap/ble_internal.h @@ -0,0 +1,221 @@ +/** + ******************************************************************************* + * + * @file ble_internal.h + * + * @brief Middleware GAP Internal function + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_BLE_INT GAP internal API + * @ingroup ATM_BTFM_API + * @brief ATM bluetooth framework GAP internal API + * + * This module contains the necessary API to deal with the GAP messages. + * + * @{ + ******************************************************************************* + */ + +#include "ble_task.h" +#include "ble_gap.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Internal C API + ******************************************************************************* + */ + +// BLE GAP Security environment structure +struct ble_gap_sec_env_tag { + uint16_t bonded_mask; + int8_t last_bonding; + uint8_t current_authreq; + ble_gap_bdaddr_t peer_addr; + struct gapc_irk peer_irk; + ble_gap_ltk_t ltk; + ble_gap_ltk_t peer_ltk; + bool peer_irk_valid; + bool loc_irk_valid; + uint8_t loc_irk[BLE_GAP_KEY_LEN]; +}; + +// BLE gapm profile task add extended parameters +typedef struct { + // service start handle + // 0 - Dynamic allocation + uint16_t start_hdl; +} ble_gapm_task_add_ex_t; + +/** + ******************************************************************************* + * @brief GAP API initialization + ******************************************************************************* + */ +void ble_gap_init(void); +/** + ******************************************************************************* + * @brief Reset controller + * After reset controller, user needs to @see ble_gapm_set_device_config again. + * @param[in] cb Complete callback function. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapm_reset_cmd(ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Create and add profile task + * + * @param[in] taskid Task identifier. + * @param[in] seclvl Security Level : + * 7 6 5 4 3 2 1 0 + * +----+----+----+----+----+----+----+----+ + * | Reserved |DIS | AUTH |EKS | MI | + * +----+----+----+----+----+----+----+----+ + * + * - MI: 1 - Application task is a Multi-Instantiated task, 0 - Mono-Instantiated + * Only applies for service - Ignored by collectors: + * - EKS: Service needs a 16 bytes encryption key + * - AUTH: 0 - Disable, 1 - Enable, 2 - Unauth, 3 - Auth + * - DIS: Disable the service + * @param[in] size Size of allocated profile configuration data. The storage + * will be passed to @p cbadd callback. + * @param[in] cbadd Profile added callback function. + * @param[in] cb Complete callback function. + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapm_profile_task_add(uint16_t taskid, uint8_t seclvl, uint16_t size, + gapm_profile_add_cb cbadd, ble_gapm_cmd_cb cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Create and add profile task with extended parameters + * + * @param[in] taskid Task identifier. + * @param[in] seclvl Security Level : + * 7 6 5 4 3 2 1 0 + * +----+----+----+----+----+----+----+----+ + * | Reserved |DIS | AUTH |EKS | MI | + * +----+----+----+----+----+----+----+----+ + * + * - MI: 1 - Application task is a Multi-Instantiated task, + * 0 - Mono-Instantiated Only applies for service - Ignored by collectors: + * - EKS: Service needs a 16 bytes encryption key + * - AUTH: 0 - Disable, 1 - Enable, 2 - Unauth, 3 - Auth + * - DIS: Disable the service + * @param[in] size Size of allocated profile configuration data. The storage + * will be passed to @p cbadd callback. + * @param[in] cbadd Profile added callback function. + * @param[in] cb Complete callback function. + * @param[in] ctx Context data. + * @param[in] ext Extended parameters + ******************************************************************************* + */ +void ble_gapm_profile_task_add_ex(uint16_t taskid, uint8_t seclvl, + uint16_t size, gapm_profile_add_cb cbadd, ble_gapm_cmd_cb cb, + void const *ctx, ble_gapm_task_add_ex_t const *ext); + +/** + ******************************************************************************* + * @brief Register GAPC message callback + * @param[in] cb Callback of GAPC message + ******************************************************************************* + */ +void ble_gapc_reg_ind(gapc_ind cb); + +/** + ******************************************************************************* + * @brief Register GAPM message callback + * @param[in] cb Callback of GAPM message + ******************************************************************************* + */ +void ble_gapm_reg_ind(gapm_ind cb); + +/** + ******************************************************************************* + * @brief Register GAPC message callback + * @param[in] cb Callback of GAPC security messages (GAPC_BOND_REQ_IND, + * GAPC_BOND_IND and GAPC_ENCRYPT_REQ_IND). + ******************************************************************************* + */ +void ble_gapc_sec_reg_ind(ke_msg_func_t cb); + +/** + ******************************************************************************* + * @brief Initialization + * @return Handler of messages. (GAPC_BOND_REQ_IND, GAPC_BOND_IND and + * GAPC_ENCRYPT_REQ_IND). + ******************************************************************************* + */ +ke_msg_func_t ble_gap_sec_init(void); + +/** + ******************************************************************************* + * @brief Get gapm handler + ******************************************************************************* + */ +gapm_ind ble_gapm_cb_get(void); + +/** + ******************************************************************************* + * @brief Get gapc handler + ******************************************************************************* + */ +gapc_ind ble_gapc_cb_get(void); + +/** + ******************************************************************************* + * @brief Add gapc command callback + * @param[in] cb Command complete callback. + * @param[in] op GAPC operation code + * @param[in] ctx Context data. + ******************************************************************************* + */ +void ble_gapc_add_cb(gapc_cmpl_cb cb, uint8_t op, void const *ctx); + +/** + ******************************************************************************* + * @brief Add gapc command callback + * @param[in] cb Command complete callback. + * @param[in] op GAPC operation code. + * @param[in] ctx Context data. + * @param[in] evt Additinal information needed in command complete. + ******************************************************************************* + */ +void ble_gapc_push_cb(ble_gapc_cmd_cb cb, uint8_t op, void const *ctx, + ble_gapc_cmpl_t *evt); + +/** + ******************************************************************************* + * @brief Allocate a memory of ble_gapc_cmpl_t with extra data length + * @param[in] extra_data_len The extra data length. + * @return Allocated memory pointer with extra data length. + ******************************************************************************* + */ +ble_gapc_cmpl_t *ble_gapc_add_cb_evt(uint32_t extra_data_len); + +/** + ******************************************************************************* + * @brief Call a callback event + * @param[in] id Event id. + * @param[in] evt Event. + ******************************************************************************* + */ +void ble_gap_evt_cb_call(ble_gap_eid_t id, ble_gap_evt_t *evt); + +#ifdef __cplusplus +} +#endif + +/// @} ATM_BTFM_BLE_INT diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_gattc/ble_gattc.h b/platform/atm2/ATM22xx-x1x/lib/ble_gattc/ble_gattc.h new file mode 100644 index 0000000..560ec95 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_gattc/ble_gattc.h @@ -0,0 +1,475 @@ +/** + ******************************************************************************* + * + * @file ble_gattc.h + * + * @brief Header File - BLE GATTC API + * + * Copyright (C) Atmosic 2020-2023 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_GATTC GATTC API + * @ingroup ATM_BTFM_API + * @brief ATM bluetooth framework GATTC API + * + * This module contains the necessary API to deal with the RW GATTC messages. + * + * @{ + ******************************************************************************* + */ + +/* + * INCLUDE + ******************************************************************************* + */ +#include "co_bt.h" +#include "gattc_task.h" +#include "ble_att.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * STRUCTURE DEFINITION + ******************************************************************************* + */ + +/// The characteristic information of GATTC_DISC_ALL_CHAR +typedef struct { + /// Element handle + uint16_t attr_hdl; + /// Pointer attribute handle to UUID + uint16_t pointer_hdl; + /// Properties + uint8_t prop; + /// UUID length + uint8_t uuid_len; + /// Characteristic UUID + uint8_t uuid[ATT_UUID_128_LEN]; +} ble_gattc_disc_all_char_ind_t; + +/// The data of GATTC_DISC_ALL_CHAR +typedef struct { + /// The maximum entries + uint16_t max_num; + /// Number of entries + uint16_t num; + /// Entry of one characteristic + ble_gattc_disc_all_char_ind_t chars[__ARRAY_EMPTY]; +} ble_gattc_disc_chars_t; + +/// The characteristic descriptor information of GATTC_DISC_DESC_CHAR +typedef struct { + /// Element handle + uint16_t attr_hdl; + /// UUID length + uint8_t uuid_len; + /// Descriptor UUID + uint8_t uuid[ATT_UUID_128_LEN]; +} ble_gattc_disc_char_desc_ind_t; + +/// Data for GATTC_DISC_DESC_CHAR +typedef struct { + /// The maximum entries + uint16_t max_num; + /// Number of entries + uint16_t num; + /// Entry of one characteristic descriptor + ble_gattc_disc_char_desc_ind_t descs[__ARRAY_EMPTY]; +} ble_gattc_disc_char_descs_t; + +/// The service information of GATTC_DISC_ALL_SVC +typedef struct { + /// Start handle + uint16_t start_hdl; + /// End handle + uint16_t end_hdl; + /// UUID length + uint8_t uuid_len; + /// Service UUID + uint8_t uuid[ATT_UUID_128_LEN]; +} ble_gattc_disc_all_svc_ind_t; + +/// Data of GATTC_DISC_ALL_SVC +typedef struct { + /// The maximum entries + uint16_t max_num; + /// Number of entries + uint8_t num; + /// Entry of one characteristic + ble_gattc_disc_all_svc_ind_t svcs[__ARRAY_EMPTY]; +} ble_gattc_disc_svcs_t; + +/// GATT task message +typedef enum gattc_msg_id ble_gattc_msg_id_t; +/// Discovery all characteristics indication structure +typedef struct gattc_disc_char_ind ble_gattc_disc_char_ind_t; +/// Discover service indication structure +typedef struct gattc_disc_svc_ind ble_gattc_disc_svc_ind_t; + +typedef struct { + /// Attribute handle + uint16_t handle; + /// Read offset + uint16_t offset; + /// Read length + uint16_t length; + /// Handle value + uint8_t value[__ARRAY_EMPTY]; +} ble_gattc_read_ind_t; + +/// GATT command complete event includes extra information +typedef struct { + /// GATT request type + uint8_t operation; + /// Status of the request + uint8_t status; + /// Operation sequence number - provided when operation is started + uint16_t seq_num; + /// Extra data + union { + /// Data for GATTC_DISC_BY_UUID_CHAR + ble_gattc_disc_char_ind_t disc_char; + /// Data for GATTC_DISC_ALL_CHAR + ble_gattc_disc_chars_t disc_chars; + /// Data for GATTC_DISC_DESC_CHAR + ble_gattc_disc_char_descs_t disc_descs; + /// Data for GATTC_DISC_BY_UUID_SVC + ble_gattc_disc_svc_ind_t disc_svc; + /// Data for GATTC_DISC_ALL_SVC + ble_gattc_disc_svcs_t disc_svcs; + /// Data for ble_gatt_read API + ble_gattc_read_ind_t read_ind; + }; +} ble_gattc_cmp_evt_ex_t; + +/// The operate code to add or remove callback (@ref ble_gattc_cb_apply) +typedef enum { + /// Add unsolicited message callback + BLE_GATTC_CB_ADD, + /// Remove unsolicited message callback + BLE_GATTC_CB_REMOVE +} ble_gattc_cb_op_t; + +/// The peer device triggers an event (notification) +typedef struct gattc_event_ind ble_gattc_event_ind_t; +/// Indicate that the ATT MTU has been updated (negotiated) +typedef struct gattc_mtu_changed_ind ble_gattc_mtu_changed_int_t; +/// The peer device triggers an event (indication) +typedef struct gattc_event_req_ind ble_gattc_event_req_ind_t; + +/// GATTC unsolicited callbacks for application +typedef struct { + /// MTU exchanged indication + /// @brief This function will be called after MTU changed. + /// @param[in] conidx Connection task index. + /// @param[in] mtu MTU exchange information. + void (*mtu_exchanged_ind)(uint8_t conidx, uint16_t mtu); + /// GATTC event indication + /// @brief This function will be called in GATTC event indication. + /// @param[in] conidx Connection task index. + /// @param[in] ind GATTC event data. + void (*event_ind)(uint8_t conidx, ble_gattc_event_ind_t const *ind); + /// GATTC event require indication + /// @brief This function will be called in GATTC event require indication. + /// @param[in] conidx Connection task index. + /// @param[in] ind GATTC event require data. + void (*event_req_ind)(uint8_t conidx, ble_gattc_event_req_ind_t const *ind); + /// Service changed Configuration indication + /// @brief This function will be called after CCCD written. + /// @param[in] conidx Connection task index. + /// @param[in] ind_cfg BLE_ATT_CCCD_IND: service changed indication is + /// enabled, BLE_ATT_CCCD_STOP_IND: service changed indication is disabled. + void (*svc_changed_cfg_ind)(uint8_t conidx, ble_att_cccd_val_t ind_cfg); +} ble_gattc_unsolicited_cbs_t; + +/// The ID of callback function (@ref ble_gattc_cb_apply) +typedef struct ble_gattc_unsolicited_ctx_s const *ble_gattc_cb_id_t; + +/* + * FUNCTION DECLARATION + ******************************************************************************* + */ + +/** + ******************************************************************************* + * GATTC command complete callback prototype + * + * @param[in] conidx Connection index + * @param[in] parm Parameter returned + * @param[in] ctx Context which provided from calling API + ******************************************************************************* + */ +typedef void (*ble_gattc_cmpl_cb_t)(uint8_t conidx, + ble_gattc_cmp_evt_ex_t const *param, void const *ctx); + +/** + ******************************************************************************* + * @brief Retrieve the GATTC message handler + * + * @return GATTC message handler + ******************************************************************************* + */ +const struct ble_subtask_handlers *ble_gattc_handler_get(void); + +/** + ******************************************************************************* + * @brief GATTC initialization + ******************************************************************************* + */ +void ble_gattc_init(void); + +/** + ******************************************************************************* + * @brief Add or remove the GATTC unsolicited message callback + * + * @param[in] op Operate code (@ref ble_gattc_cb_op_t) + * @param[in] cbs Callback of GATTC unsolicited message + * @return the ID of GATTC callback function if op code is ble_gattc_cb_add; + * otherwise, return NULL. + ******************************************************************************* + */ +__NONNULL(2) +ble_gattc_cb_id_t ble_gattc_cb_apply(ble_gattc_cb_op_t op, + ble_gattc_unsolicited_cbs_t const *cbs); + +/** + ******************************************************************************* + * @brief Get gattc unsolicited callback + * + * @param[in] id The ID of GATTC callback function + * @return GATTC callback function identify + ******************************************************************************* + */ +__NONNULL_ALL +ble_gattc_unsolicited_cbs_t const *ble_gattc_cb_get(ble_gattc_cb_id_t const id); + +/** + ******************************************************************************* + * @brief MTU exchange + * + * @param[in] conidx Connection index + * @param[in] cb Command complete or indication callback + ******************************************************************************* + */ +void ble_gattc_mtu_exchange(uint8_t conidx, ble_gattc_cmpl_cb_t cb); + +/** + ******************************************************************************* + * @brief The specific characteristic discovery + * + * @param[in] conidx Connection index + * @param[in] uuid UUID searched - LSB first + * @param[in] uuid_len UUID length (2, 4, or 16 bytes) + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_disc_svc(uint8_t conidx, uint8_t const *uuid, uint8_t uuid_len, + ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Discover service by handle range + * + * @param[in] conidx Connection index + * @param[in] shdl Start handle + * @param[in] ehdl End handle + * @param[in] max_num The maximum services could be discovered + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_disc_svcs(uint8_t conidx, uint16_t shdl, uint16_t ehdl, + uint16_t max_num, ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Characteristic discovery + * + * @param[in] conidx Connection index + * @param[in] uuid UUID searched - LSB first + * @param[in] uuid_len UUID length (2, 4, or 16 bytes) + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_disc_char(uint8_t conidx, uint8_t const *uuid, uint8_t uuid_len, + ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Discover all characteristics + * + * @param[in] conidx Connection index + * @param[in] shdl Start handle + * @param[in] ehdl End handle + * @param[in] cb Command complete or indication callback + * @param[in] max_num The maximum characteristics could be discovered + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_disc_chars(uint8_t conidx, uint16_t shdl, uint16_t ehdl, + uint16_t max_num, ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Characteristic descriptor discovery + * + * @param[in] conidx Connection index + * @param[in] shdl Start handle + * @param[in] ehdl End handle + * @param[in] max_num The maximum descriptors could be discovered + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_disc_desc_char(uint8_t conidx, uint16_t shdl, uint16_t ehdl, + uint16_t max_num, ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Characteristic write + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] dat Data + * @param[in] len Data length + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_write(uint8_t conidx, uint16_t hdl, uint8_t const *dat, + uint16_t len, ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Characteristic read + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] offset Start offset in data payload + * @param[in] len Data length. (0 = read all) + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_read(uint8_t conidx, uint16_t hdl, uint16_t offset, uint16_t len, + ble_gattc_cmpl_cb_t cb, void const *ctx); +/** + ******************************************************************************* + * @brief Characteristic write without response + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] dat Data + * @param[in] len Data length + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_write_no_resp(uint8_t conidx, uint16_t hdl, uint8_t const *dat, + uint16_t len, ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Characteristic read confirm + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] dat Data + * @param[in] length Data length + ******************************************************************************* + */ +void ble_gattc_read_cfm(uint8_t conidx, uint16_t hdl, uint8_t const *dat, + uint16_t length); + +/** + ******************************************************************************* + * @brief Characteristic write confirm + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] status Status + ******************************************************************************* + */ +void ble_gattc_write_cfm(uint8_t conidx, uint16_t hdl, uint8_t status); + +/** + ******************************************************************************* + * @brief Characteristic notification + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] dat Data + * @param[in] ntf_size Data length + * @param[in] sn Sequence number + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_send_ntf(uint8_t conidx, uint16_t hdl, uint8_t const *dat, + uint16_t ntf_size, uint16_t sn, ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Characteristic indication + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] dat Data + * @param[in] ind_size Data length + * @param[in] sn Seq number + * @param[in] cb Command complete or indication callback + * @param[in] ctx Context data + ******************************************************************************* + */ +void ble_gattc_send_ind(uint8_t conidx, uint16_t hdl, uint8_t const *dat, + uint16_t ind_size, uint16_t sn, ble_gattc_cmpl_cb_t cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Characteristic att info confirm + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + * @param[in] length Length + ******************************************************************************* + */ +void ble_gattc_att_info_cfm(uint8_t conidx, uint16_t hdl, uint16_t length); + +/** + ******************************************************************************* + * @brief Characteristic att event requirement indication confirm + * + * @param[in] conidx Connection index + * @param[in] hdl ATT handle + ******************************************************************************* + */ +void ble_gattc_event_req_ind_cfm(uint8_t conidx, uint16_t hdl); + +/** + ******************************************************************************* + * @brief Send service changed notification + * + * @param[in] conidx Connection index + * @param[in] start_hdl Staring handle + * @param[in] end_hdl Ending handle + * @param[in] cb Callback function (NULL for no callback) + ******************************************************************************* + */ +void ble_gattc_svc_changed(uint8_t conidx, uint16_t start_hdl, uint16_t end_hdl, + ble_gattc_cmpl_cb_t cb); + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_GATTC + diff --git a/platform/atm2/ATM22xx-x1x/lib/ble_hogpd/ble_hogpd.h b/platform/atm2/ATM22xx-x1x/lib/ble_hogpd/ble_hogpd.h new file mode 100644 index 0000000..cc79c39 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/ble_hogpd/ble_hogpd.h @@ -0,0 +1,395 @@ +/** + ******************************************************************************* + * + * @file ble_hogpd.h + * + * @brief HID Over GATT Profile Device Middleware + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_HOGPD HID Over GATT Profile Device API + * @ingroup ATM_BTFM_API + * @brief ATM bluetooth framework HOGP API + * + * This module contains the necessary device API for HID over GATT profile + * + * @{ + ******************************************************************************* + */ +#include <stdbool.h> +#include "ble_prf_itf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/// The module name to register to SDK framework +#define BLE_HOGPD_MODULE_NAME "khogpd" + +/// Maximal number of HIDS +#define BLE_HOGPD_NB_HIDS_INST_MAX 2 +/// Maximal number of Report Char. +#define BLE_HOGPD_NB_REPORT_INST_MAX 5 + +/// The states of the HID device +typedef enum { + /// Device is disabled (Service not added in DB) + BLE_HOGPD_DISABLED, + /// Device is idle (Service added but profile not enabled) + BLE_HOGPD_IDLE, + /// Device is enabled (Device is connected and the profile is enabled) + BLE_HOGPD_ENABLED, + /// Device is ready (Device can sent the report) + BLE_HOGPD_READY, + /// Mark for last + BLE_HOGPD_STATE_MAX, +} ble_hogpd_state_t; + +/// Report Char. Configuration Flag Values +typedef enum { + /// Input Report + BLE_HOGPD_CFG_REPORT_IN = 0x01, + /// Output Report + BLE_HOGPD_CFG_REPORT_OUT = 0x02, + /// HOGPD_CFG_REPORT_FEAT can be used as a mask to check Report type + /// Feature Report + BLE_HOGPD_CFG_REPORT_FEAT = 0x03, + /// Input report with Write capabilities + BLE_HOGPD_CFG_REPORT_WR = 0x10, +} ble_hogpd_report_cfg_t; + +/// Features Flag Values +typedef enum { + /// Keyboard Device + BLE_HOGPD_CFG_KEYBOARD = 0x01, + /// Mouse Device + BLE_HOGPD_CFG_MOUSE = 0x02, + /// Protocol Mode present + BLE_HOGPD_CFG_PROTO_MODE = 0x04, + /// Extended Reference Present + BLE_HOGPD_CFG_MAP_EXT_REF = 0x08, + /// Boot Keyboard Report write capability + BLE_HOGPD_CFG_BOOT_KB_WR = 0x10, + /// Boot Mouse Report write capability + BLE_HOGPD_CFG_BOOT_MOUSE_WR = 0x20, + /// Valid Feature mask + BLE_HOGPD_CFG_MASK = 0x3F, + /// Report Notification Enabled + BLE_HOGPD_CFG_REPORT_NTF_EN = 0x40, +} ble_hogpd_cfg_t; + +/// Type of reports +typedef enum { + /// The Report characteristic + BLE_HOGPD_REPORT, + /// The Report Map characteristic + BLE_HOGPD_REPORT_MAP, + /// Boot Keyboard Input Report + BLE_HOGPD_BOOT_KEYBOARD_INPUT_REPORT, + /// Boot Keyboard Output Report + BLE_HOGPD_BOOT_KEYBOARD_OUTPUT_REPORT, + /// Boot Mouse Input Report + BLE_HOGPD_BOOT_MOUSE_INPUT_REPORT, +} ble_hogpd_report_type_t; + +/// HID Information bit values +typedef enum { + /// Device capable of providing wake-up signal to a HID host + BLE_HIDS_REMOTE_WAKE_CAPABLE = 0x01, + /// Normally connectable support bit + BLE_HIDS_NORM_CONNECTABLE = 0x02, +} ble_hogp_info_bit_t; + +/// HID Control Point Characteristic value keys +enum ble_hogp_ctnl_pt { + /// Suspend + BLE_HOGP_CTNL_PT_SUSPEND, + /// Exit suspend + BLE_HOGP_CTNL_PT_EXIT_SUSPEND, +}; + +/// Protocol Mode Char. value Keys +enum ble_hogp_boot_prot_mode { + /// Boot Protocol Mode + BLE_HOGP_BOOT_PROTOCOL_MODE, + /// Report Protocol Mode + BLE_HOGP_REPORT_PROTOCOL_MODE, +}; + +/* + * STRUCTURE DEFINITION + ******************************************************************************* + */ + +/// HID Information structure +typedef struct { + /// bcdHID + uint16_t bcdHID; + /// bCountryCode + uint8_t bCountryCode; + /// Flags + ble_hogp_info_bit_t flags; +} ble_hids_hid_info_t; + +/// External Report Reference +typedef struct { + /// Included Service Handle + uint16_t inc_svc_hdl; + /// Characteristic UUID + uint16_t rep_ref_uuid; +} ble_hogpd_ext_ref_t; + +/// Database Creation Service Instance Configuration structure +typedef struct { + /// Service Features + ble_hogpd_cfg_t svc_features; + /// Number of Report Char. instances to add in the database + uint8_t report_nb; + /// Report Char. Configuration + ble_hogpd_report_cfg_t report_char_cfg[BLE_HOGPD_NB_REPORT_INST_MAX]; + /// Report id number + uint8_t report_id[BLE_HOGPD_NB_REPORT_INST_MAX]; + /// HID Information Char. Values + ble_hids_hid_info_t hid_info; + /// External Report Reference + ble_hogpd_ext_ref_t ext_ref; +} ble_hogpd_hids_cfg_t; + +typedef struct { + /// Number of HIDS to add + uint8_t hids_nb; + /// Initial configuration for each HIDS instance + ble_hogpd_hids_cfg_t cfg[BLE_HOGPD_NB_HIDS_INST_MAX]; +} ble_hogpd_db_cfg_t; + +/// The ble_hogpd peer information +typedef struct { + /// State of module + ble_hogpd_state_t state; + /// Notification CCC mask + uint16_t ntf_mask; +} ble_hogpd_peer_info_t; + +/// The ble_hogpd report info +typedef struct { + /// Connection index + uint8_t conidx; + /// HIDS Instance + uint8_t hid_idx; + /// Report type + ble_hogpd_report_type_t rpt_type; + /// Report index + uint8_t rpt_idx; +} ble_hogpd_report_info_t; + +/// The ble_hogpd report +typedef struct { + /// Report Info + ble_hogpd_report_info_t info; + /// Report length + uint16_t rpt_len; + /// Report data + uint8_t const *data; +} ble_hogpd_report_t; + +/// The ble_hogpd report read request +typedef struct { + /// Report info + ble_hogpd_report_info_t info; + /// Token value that must be returned in confirmation + uint16_t token; + /// Data offset requested for read value + uint16_t offset; + /// Maximum data length is response value (starting from offset) + uint16_t max_length; +} ble_hogpd_report_read_req_t; + +/// The ble_hogpd report write request +typedef struct { + /// Token value that must be returned in confirmation + uint16_t token; + /// Report info + ble_hogpd_report_info_t info; + /// Report length + uint16_t rpt_len; + /// Report data + uint8_t const *data; +} ble_hogpd_report_write_req_t; + +/// The ble_hogpd report read confirm +typedef struct { + /// Report info + ble_hogpd_report_info_t info; + /// Status of read request. Success: BLE_ERR_NO_ERROR + ble_err_code_t status; + /// Token value + uint16_t token; + /// Report length + uint16_t rpt_len; + /// Report data + uint8_t const *data; +} ble_hogpd_report_read_cfm_t; + +/// The ble_hogpd report write confirm +typedef struct { + /// Token value + uint16_t token; + /// Status of read request. Success: BLE_ERR_NO_ERROR + ble_err_code_t status; + /// Report Info + ble_hogpd_report_info_t info; +} ble_hogpd_report_write_cfm_t; + +typedef void (*ble_hogpd_ntf_ind_t)(uint8_t conidx, uint16_t rpt_idx_mask); +typedef void (*ble_hogpd_state_ind_t)(uint8_t conidx, ble_hogpd_state_t last); +typedef void (*ble_hogpd_report_read_req_cb_t)( + ble_hogpd_report_read_req_t const *req); +typedef void (*ble_hogpd_report_write_req_cb_t)( + ble_hogpd_report_write_req_t const *req); +typedef void (*ble_hogpd_report_cmp_cb_t)(uint8_t conidx, ble_err_code_t status, + void const *ctx); + +typedef struct { + /// Number of report could be sent + uint8_t nb_report; + /// Database config + ble_hogpd_db_cfg_t db_cfg; + /// Report map + uint8_t const *report_map[BLE_HOGPD_NB_HIDS_INST_MAX]; + /// Report map size + uint16_t report_map_len[BLE_HOGPD_NB_HIDS_INST_MAX]; + /// The mask of cccd enable for HID ready + uint16_t ccc_rdy_mask; + /// Notification state changed + ble_hogpd_state_ind_t state_ind; + /// Report read request + ble_hogpd_report_read_req_cb_t report_read_req; + /// Report write request + ble_hogpd_report_write_req_cb_t report_write_req; + /// Service start handle + /// 0: dynamically allocated in Attribute database + uint16_t start_hdl; + /// Number of peers supported + uint8_t nb_peer; +} ble_hogpd_param_t; + +typedef ble_prf_cbs_t const *(ble_hogpd_cbs_t)(ble_hogpd_param_t const *init); + +/** + ******************************************************************************* + * @brief Send HID report + * + * @param[in] rpt Report + * @param[in] cmp_cb Report complete callback. NULL if the user does not want to + * be notified after report event complete. + * @param[in] ctx User defined context + * @return Success: true + ******************************************************************************* + */ +__NONNULL(1) +bool ble_hogpd_send_report(ble_hogpd_report_t const *rpt, + ble_hogpd_report_cmp_cb_t cmp_cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Send report read confirm + * + * @param[in] cfm Report read confirm + ******************************************************************************* + */ +__NONNULL_ALL +void ble_hogpd_send_report_read_cfm(ble_hogpd_report_read_cfm_t const *cfm); + +/** + ******************************************************************************* + * @brief Send report write confirm + * + * @param[in] cfm Report write confirm + ******************************************************************************* + */ +__NONNULL_ALL +void ble_hogpd_send_report_write_cfm(ble_hogpd_report_write_cfm_t const *cfm); + +/** + ******************************************************************************* + * @brief Claim report buffer. + * + * Application uses this function to claim a buffer from ble_hogpd layer and + * calls ble_hogpd_report_send to send it out. + * + * @param[in] info Report Info + * @param[in] rpt_len Report Length + * @param[in] cmp_cb Report complete callback. NULL if the user does not want to + * be notified after report event complete. + * @param[in] ctx Application context. + * @return HOGPD report buffer context. + ******************************************************************************* + */ +__NONNULL(1) +void *ble_hogpd_report_claim(ble_hogpd_report_info_t const *info, + uint16_t rpt_len, ble_hogpd_report_cmp_cb_t cmp_cb, void const *ctx); + +/** + ******************************************************************************* + * @brief Get report data payload + * + * @param[in] rpt_buf_ctx HOGPD report buffer context + * @return Pointer of the report payload + ******************************************************************************* + */ +__NONNULL_ALL +uint8_t *ble_hogpd_report_get_report_payload(void *rpt_buf_ctx); + +/** + ******************************************************************************* + * @brief Get report length from the report buffer + * + * @param[in] rpt_buf_ctx HOGPD report buffer context + * @return Report length + ******************************************************************************* + */ +__NONNULL_ALL +uint16_t ble_hogpd_report_get_report_length(void const *rpt_buf_ctx); + +/** + ******************************************************************************* + * @brief Set report length to the report buffer + * + * @param[in] rpt_buf_ctx HOGPD report buffer context + * @param[in] new_len Report length. Must not longer than the claimed length + ******************************************************************************* + */ +__NONNULL(1) +void ble_hogpd_report_set_report_length(void *rpt_buf_ctx, uint16_t new_len); + +/** + ******************************************************************************* + * @brief Send report to peer. + * + * @param[in] rpt_buf_ctx HOGPD report buffer context + ******************************************************************************* + */ +__NONNULL_ALL +void ble_hogpd_report_send(void const *rpt_buf_ctx); + +/** + ******************************************************************************* + * @brief Get module information + * + * @return Current ble_hogpd peer information + ******************************************************************************* + */ +ble_hogpd_peer_info_t const *ble_hogpd_get_peer_info(void); + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_HOGPD + diff --git a/platform/atm2/ATM22xx-x1x/lib/prf/ble_att.h b/platform/atm2/ATM22xx-x1x/lib/prf/ble_att.h new file mode 100644 index 0000000..07ee444 --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/prf/ble_att.h @@ -0,0 +1,126 @@ +/** + ******************************************************************************* + * + * @file ble_att.h + * + * @brief BLE ATT + * + * Copyright (C) Atmosic 2020 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_ATT BLE ATT define + * @ingroup ATM_BTFM_API + * @brief ATM bluetooth framework ATT defines + * + * @{ + ******************************************************************************* + */ + +/* + * INCLUDE FILE + ****************************************************************************** + */ +#include "rwip_config.h" +#include "att.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * DEFINE + ****************************************************************************** + */ + +/// No security requirements +#define BLE_SEC_PROP_NO_SECURITY (PERM(SVC_AUTH, NO_AUTH)) +/// Unauthenticated no MITM protection +#define BLE_SEC_PROP_UNAUTH (PERM(SVC_AUTH, UNAUTH)) +/// Authenticated MITM protection +#define BLE_SEC_PROP_AUTH (PERM(SVC_AUTH, AUTH)) +/// LE Secure Connections pairing +#define BLE_SEC_PROP_LE_SC (PERM(SVC_AUTH, SEC_CON)) + +#define BLE_ATT_RD PERM(RD, ENABLE) +/// No security requirements - Read +#define BLE_ATT_READ_NO_SECURITY (BLE_ATT_RD | PERM(RP, NO_AUTH)) +/// Unauthenticated no MITM protection - Read +#define BLE_ATT_READ_UNAUTH (BLE_ATT_RD | PERM(RP, UNAUTH)) +/// Authenticated MITM protection - Read +#define BLE_ATT_READ_AUTH (BLE_ATT_RD | PERM(RP, AUTH)) +/// LE Secure Connections pairing - Read +#define BLE_ATT_READ_LE_SC (BLE_ATT_RD | PERM(RP, SEC_CON)) + +#define BLE_ATT_WC PERM(WRITE_COMMAND, ENABLE) +/// No security requirements - Write command +#define BLE_ATT_WRITE_COMMAND_NO_SECURITY (BLE_ATT_WC | PERM(WP, NO_AUTH)) +/// Unauthenticated no MITM protection - Write command +#define BLE_ATT_WRITE_COMMAND_UNAUTH (BLE_ATT_WC | PERM(WP, UNAUTH)) +/// Authenticated MITM protection - Write command +#define BLE_ATT_WRITE_COMMAND_AUTH (BLE_ATT_WC | PERM(WP, AUTH)) +/// LE Secure Connections pairing - Write command +#define BLE_ATT_WRITE_COMMAND_LE_SC (BLE_ATT_WC | PERM(WP, SEC_CON)) + +#define BLE_ATT_WR PERM(WRITE_REQ, ENABLE) +/// No security requirements - Write request +#define BLE_ATT_WRITE_REQ_NO_SECURITY (BLE_ATT_WR | PERM(WP, NO_AUTH)) +/// Unauthenticated no MITM protection - Write request +#define BLE_ATT_WRITE_REQ_UNAUTH (BLE_ATT_WR | PERM(WP, UNAUTH)) +/// Authenticated MITM protection - Write request +#define BLE_ATT_WRITE_REQ_AUTH (BLE_ATT_WR | PERM(WP, AUTH)) +/// LE Secure Connections pairing - Write request +#define BLE_ATT_WRITE_REQ_LE_SC (BLE_ATT_WR | PERM(WP, SEC_CON)) + +#define BLE_ATT_WS PERM(WRITE_SIGNED, ENABLE) +/// No security requirements - Write signed +#define BLE_ATT_WRITE_SIGNED_NO_SECURITY (BLE_ATT_WS | PERM(WP, NO_AUTH)) +/// Unauthenticated no MITM protection - Write signed +#define BLE_ATT_WRITE_SIGNED_UNAUTH (BLE_ATT_WS | PERM(WP, UNAUTH)) +/// Authenticated MITM protection - Write signed +#define BLE_ATT_WRITE_SIGNED_AUTH (BLE_ATT_WS | PERM(WP, AUTH)) +/// LE Secure Connections pairing - Write signed +#define BLE_ATT_WRITE_SIGNED_LE_SC (BLE_ATT_WS | PERM(WP, SEC_CON)) + +#define BLE_ATT_NTF PERM(NTF, ENABLE) +/// No security requirements - Notification +#define BLE_ATT_NTF_NO_SECURITY (BLE_ATT_NTF | PERM(NP, NO_AUTH)) +/// Unauthenticated no MITM protection - Notification +#define BLE_ATT_NTF_UNAUTH (BLE_ATT_NTF | PERM(NP, UNAUTH)) +/// Authenticated MITM protection - Notification +#define BLE_ATT_NTF_AUTH (BLE_ATT_NTF | PERM(NP, AUTH)) +/// LE Secure Connections pairing - Notification +#define BLE_ATT_NTF_LE_SC (BLE_ATT_NTF | PERM(NP, SEC_CON)) + +#define BLE_ATT_IND PERM(IND, ENABLE) +/// No security requirements - Indication +#define BLE_ATT_IND_NO_SECURITY (BLE_ATT_IND | PERM(IP, NO_AUTH)) +/// Unauthenticated no MITM protection - Indication +#define BLE_ATT_IND_UNAUTH (BLE_ATT_IND | PERM(IP, UNAUTH)) +/// Authenticated MITM protection - Indication +#define BLE_ATT_IND_AUTH (BLE_ATT_IND | PERM(IP, AUTH)) +/// LE Secure Connections pairing - Indication +#define BLE_ATT_IND_LE_SC (BLE_ATT_IND | PERM(IP, SEC_CON)) + +/// Client Characteristic Configuration Codes +typedef enum { + /// Stop notification + BLE_ATT_CCCD_STOP_NTF = ATT_CCC_STOP_NTFIND, + /// Stop indication + BLE_ATT_CCCD_STOP_IND = ATT_CCC_STOP_NTFIND, + /// Start notification + BLE_ATT_CCCD_NTF = ATT_CCC_START_NTF, + /// Start indication + BLE_ATT_CCCD_IND = ATT_CCC_START_IND +} ble_att_cccd_val_t; + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_ATT + diff --git a/platform/atm2/ATM22xx-x1x/lib/prf/ble_prf_itf.h b/platform/atm2/ATM22xx-x1x/lib/prf/ble_prf_itf.h new file mode 100644 index 0000000..056c15a --- /dev/null +++ b/platform/atm2/ATM22xx-x1x/lib/prf/ble_prf_itf.h @@ -0,0 +1,59 @@ +/** + ******************************************************************************* + * + * @file ble_prf_itf.h + * + * @brief BLE profile interface + * + * Copyright (C) Atmosic 2020-2022 + * + ******************************************************************************* + */ +#pragma once + +/** + ******************************************************************************* + * @defgroup ATM_BTFM_PRFITF Profile interface + * @ingroup ATM_BTFM_API + * @brief ATM bluetooth framework profile interface + * + * This module contains the profile interface definitioin for middleware and + * application layer. + * + * @{ + ******************************************************************************* + */ + +/* + * INCLUDE FILE + ******************************************************************************* + */ +#include "ble_gap.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * STRUCTURE + ******************************************************************************* + */ +/// The profile interface callback API for middleware and application layer. +typedef struct { + /// @brief Profile initialization. + /// @details This function will be called before BT controller reset. + void (*init_op)(void); + /// @brief Profile start. + /// @details This function will be called after BT controller reset. + /// Profile shall add service in this function if needed. + /// @param[in] cb Callback needed be called after starting duties completed. + /// @param[in] ctxt Context data. + void (*start_op)(ble_gapm_cmd_cb cb, void const *ctxt); +} ble_prf_cbs_t; + +#ifdef __cplusplus +} +#endif + +///@} ATM_BTFM_PRFITF + |