328 lines
8.7 KiB
C
328 lines
8.7 KiB
C
/**
|
|
****************************************************************************************
|
|
*
|
|
* @file app_task.h
|
|
*
|
|
* @brief Header file - APPTASK.
|
|
*
|
|
* Copyright (C) RivieraWaves 2009-2015
|
|
*
|
|
*
|
|
****************************************************************************************
|
|
*/
|
|
|
|
#ifndef APP_TASK_H_
|
|
#define APP_TASK_H_
|
|
|
|
/**
|
|
****************************************************************************************
|
|
* @addtogroup APPTASK Task
|
|
* @ingroup APP
|
|
* @brief Routes ALL messages to/from APP block.
|
|
*
|
|
* The APPTASK is the block responsible for bridging the final application with
|
|
*the RWBLE software host stack. It communicates with the different modules of
|
|
*the BLE host, i.e. @ref SMP, @ref GAP and @ref GATT.
|
|
*
|
|
* @{
|
|
****************************************************************************************
|
|
*/
|
|
|
|
#include "co_bt_defines.h"
|
|
#include "dbg.h"
|
|
#include "gapm_int.h"
|
|
#include "gatt_msg_int.h" // GATTC Definitions
|
|
#include "ke_task.h" // Kernel Task
|
|
#include "rwip_task.h" // Task definitions
|
|
#include "xc_gap_api.h"
|
|
#include <stdbool.h>
|
|
#include <stdint.h> // Standard Integer Definition
|
|
#include <stdio.h>
|
|
|
|
#if (NVDS_SUPPORT)
|
|
#include "nvds.h"
|
|
#endif // (NVDS_SUPPORT)
|
|
|
|
#define APP_HANDLERS(subtask) \
|
|
{ \
|
|
&subtask##_msg_handler_list[0], ARRAY_LEN(subtask##_msg_handler_list) \
|
|
}
|
|
|
|
/// Default Device Name
|
|
#define APP_DFLT_DEVICE_NAME ("HPW421")
|
|
#define APP_DFLT_DEVICE_NAME_LEN (sizeof(APP_DFLT_DEVICE_NAME))//must not be greater than APP_DEVICE_NAME_MAX_LEN
|
|
#define APP_SINGLEOTA_DEVICE_NAME "OTA_TEST"
|
|
#define APP_SINGLEOTA_DEVICE_NAME_LEN (sizeof(APP_SINGLEOTA_DEVICE_NAME))
|
|
/// Maximal length of the Device Name value
|
|
#define APP_DEVICE_NAME_MAX_LEN (18)
|
|
#define APP_MAX_TX_POWER (0)
|
|
// Advertising channel map - 37, 38, 39
|
|
#define APP_ADV_CHMAP (0x07)
|
|
// Advertising minimum interval - 40ms (64*0.625ms)
|
|
#define APP_ADV_INT_MIN (64)
|
|
// Advertising maximum interval - 40ms (64*0.625ms)
|
|
#define APP_ADV_INT_MAX (64)
|
|
#define ADV_DATA_MAX_LENGTH 28
|
|
|
|
#define MANUFACTURER_DATA "\x09\xFF\x32\x60\x47\x99"
|
|
#define MANUFACTURER_DATA_LEN (sizeof(MANUFACTURER_DATA)-1)//10
|
|
|
|
/// Advertising duration (in unit of 10ms). 0 means that advertising continues
|
|
/// until the host disable it
|
|
#define ADV_DURATION 0
|
|
|
|
#define APP_SCAN_INTERVAL (0xa0)
|
|
#define APP_SCAN_WINDOW (0x50)
|
|
|
|
#define APP_CONN_EST_TIME_OUT (0)
|
|
#define APP_CONN_SCNA_INTV (32)
|
|
#define APP_CONN_SCAN_WD (20)
|
|
#define APP_CONN_INTV_MIN (80)
|
|
#define APP_CONN_INV_MAX (80)
|
|
#define APP_CONN_LATENCY (0)
|
|
#define APP_CONN_TIME_OUT (500)
|
|
|
|
/// Number of APP Task Instances
|
|
#define APP_IDX_MAX 1
|
|
#define DEV_APPEARANCE 0
|
|
|
|
#define BLE_UAPDATA_MIN_INTVALUE 8
|
|
#define BLE_UAPDATA_MAX_INTVALUE 10
|
|
#define BLE_UAPDATA_LATENCY 0
|
|
#define BLE_UAPDATA_TIMEOUT 200
|
|
|
|
#define INVALID_DATA 0xFF
|
|
|
|
#define TAGET_DEVICE_ADDR ("\x12\x34\x56\x78\x90\xAB")
|
|
#define APP_ADDR_MAXLEN 6
|
|
#define APP_NAME_MAXLEN 21
|
|
struct ADV_INFO{
|
|
uint8_t addr[APP_ADDR_MAXLEN];
|
|
uint8_t name_length;
|
|
uint8_t name[APP_NAME_MAXLEN];
|
|
};
|
|
/// Application environment structure
|
|
struct app_env_tag
|
|
{
|
|
/// Connection handle
|
|
uint16_t conhdl;
|
|
/// Connection Index
|
|
uint8_t conidx;
|
|
/// Current advertising state (@see enum app_adv_state)
|
|
uint8_t adv_state;
|
|
/// Current advertising state (@see enum app_adv_state)
|
|
uint8_t scan_state;
|
|
/// Current advertising state (@see enum app_adv_state)
|
|
uint8_t init_state;
|
|
/// Next expected operation completed event
|
|
uint8_t adv_op;
|
|
|
|
/// Last initialized profile
|
|
uint8_t next_svc;
|
|
|
|
/// Bonding status
|
|
bool bonded;
|
|
|
|
/// Device Name length
|
|
uint8_t dev_name_len;
|
|
/// Device Name
|
|
uint8_t dev_name[APP_DEVICE_NAME_MAX_LEN];
|
|
|
|
/// Local device IRK
|
|
uint8_t loc_irk[KEY_LEN];
|
|
|
|
/// Secure Connections on current link
|
|
bool sec_con_enabled;
|
|
|
|
/// Counter used to generate IRK
|
|
uint8_t rand_cnt;
|
|
|
|
/// Demonstration type length
|
|
uint8_t demo_type_len;
|
|
/// Demonstration type
|
|
uint8_t demo_type;
|
|
/// GATT user local identifier
|
|
uint8_t user_lid;
|
|
uint8_t adv_actv_idx;
|
|
uint8_t slave_conidx;
|
|
bool slave_connected;
|
|
};
|
|
|
|
#if (NVDS_SUPPORT)
|
|
/// List of Application NVDS TAG identifiers
|
|
enum app_nvds_tag
|
|
{
|
|
/// Device Name
|
|
NVDS_TAG_DEVICE_NAME = 0x02,
|
|
NVDS_LEN_DEVICE_NAME = 62,
|
|
|
|
/// BD Address
|
|
NVDS_TAG_BD_ADDRESS = 0x01,
|
|
NVDS_LEN_BD_ADDRESS = 6,
|
|
|
|
/// Local device Identity resolving key
|
|
NVDS_TAG_LOC_IRK = 0xA0,
|
|
NVDS_LEN_LOC_IRK = KEY_LEN,
|
|
|
|
#if (BLE_APP_PRF)
|
|
/// BLE Application Advertising data
|
|
NVDS_TAG_APP_BLE_ADV_DATA = 0x0B,
|
|
NVDS_LEN_APP_BLE_ADV_DATA = 32,
|
|
|
|
/// BLE Application Scan response data
|
|
NVDS_TAG_APP_BLE_SCAN_RESP_DATA = 0x0C,
|
|
NVDS_LEN_APP_BLE_SCAN_RESP_DATA = 32,
|
|
|
|
/// Mouse Sample Rate
|
|
NVDS_TAG_MOUSE_SAMPLE_RATE = 0x38,
|
|
NVDS_LEN_MOUSE_SAMPLE_RATE = 1,
|
|
|
|
/// Peripheral Bonded
|
|
NVDS_TAG_PERIPH_BONDED = 0x39,
|
|
NVDS_LEN_PERIPH_BONDED = 1,
|
|
|
|
/// Mouse NTF Cfg
|
|
NVDS_TAG_MOUSE_NTF_CFG = 0x3A,
|
|
NVDS_LEN_MOUSE_NTF_CFG = 2,
|
|
|
|
/// Mouse Timeout value
|
|
NVDS_TAG_MOUSE_TIMEOUT = 0x3B,
|
|
NVDS_LEN_MOUSE_TIMEOUT = 2,
|
|
|
|
/// Peer Device BD Address
|
|
NVDS_TAG_PEER_BD_ADDRESS = 0x3C,
|
|
NVDS_LEN_PEER_BD_ADDRESS = 7,
|
|
|
|
/// Mouse Energy Safe
|
|
NVDS_TAG_MOUSE_ENERGY_SAFE = 0x3D,
|
|
NVDS_LEN_MOUSE_SAFE_ENERGY = 2,
|
|
|
|
/// EDIV (2bytes), RAND NB (8bytes), LTK (16 bytes), Key Size (1 byte)
|
|
NVDS_TAG_LTK = 0x3E,
|
|
NVDS_LEN_LTK = 28,
|
|
|
|
/// PAIRING
|
|
NVDS_TAG_PAIRING = 0x3F,
|
|
NVDS_LEN_PAIRING = 54,
|
|
|
|
/// Audio mode 0 task
|
|
NVDS_TAG_AM0_FIRST = 0x90,
|
|
NVDS_TAG_AM0_LAST = 0x9F,
|
|
|
|
/// Peer device Resolving identity key (+identity address)
|
|
NVDS_TAG_PEER_IRK = 0xA1,
|
|
NVDS_LEN_PEER_IRK = sizeof(struct gapc_irk),
|
|
|
|
#endif //(BLE_APP_PRF)
|
|
};
|
|
#endif // (NVDS_SUPPORT)
|
|
|
|
/// Advertising state machine
|
|
enum app_adv_state
|
|
{
|
|
/// Advertising activity does not exists
|
|
APP_ADV_STATE_IDLE = 0,
|
|
#if BLE_APP_PRF
|
|
/// Creating advertising activity
|
|
APP_ADV_STATE_CREATING,
|
|
/// Setting advertising data
|
|
APP_ADV_STATE_SETTING_ADV_DATA,
|
|
/// Setting scan response data
|
|
APP_ADV_STATE_SETTING_SCAN_RSP_DATA,
|
|
|
|
/// Advertising activity created
|
|
APP_ADV_STATE_CREATED,
|
|
/// Starting advertising activity
|
|
APP_ADV_STATE_STARTING,
|
|
/// Advertising activity started
|
|
APP_ADV_STATE_STARTED,
|
|
/// Stopping advertising activity
|
|
APP_ADV_STATE_STOPPING,
|
|
APP_ADV_STATE_STOPPED,
|
|
#endif //(BLE_APP_PRF)
|
|
};
|
|
|
|
/// Scanning state machine
|
|
enum app_scan_state
|
|
{
|
|
APP_SCAN_STATE_IDLE = 0,
|
|
APP_SCAN_STATE_CREATING,
|
|
APP_SCAN_STATE_CREATED,
|
|
APP_SCAN_STATE_STARTING,
|
|
APP_SCAN_STATE_STARTED,
|
|
APP_SCAN_STATE_STOPPING,
|
|
APP_SCAN_STATE_STOPPED,
|
|
};
|
|
|
|
/// initting state machine
|
|
enum app_init_state
|
|
{
|
|
APP_INIT_STATE_IDLE = 0,
|
|
APP_INIT_STATE_CREATING,
|
|
APP_INIT_STATE_CREATED,
|
|
APP_INIT_STATE_STARTING,
|
|
APP_INIT_STATE_STARTED,
|
|
APP_INIT_STATE_STOPPING,
|
|
APP_INIT_STATE_STOPPED,
|
|
};
|
|
|
|
/// States of APP task
|
|
enum app_state
|
|
{
|
|
/// Initialization state
|
|
APP_INIT,
|
|
/// Database create state
|
|
APP_CREATE_DB,
|
|
/// Ready State
|
|
APP_READY,
|
|
/// Connected state
|
|
APP_CONNECTED,
|
|
|
|
/// Number of defined states.
|
|
APP_STATE_MAX
|
|
};
|
|
|
|
/// APP Task messages
|
|
/*@TRACE*/
|
|
enum app_msg_id
|
|
{
|
|
APP_DUMMY_MSG = TASK_FIRST_MSG(TASK_ID_APP),
|
|
|
|
#if (BLE_APP_PRF)
|
|
#if (BLE_APP_HT)
|
|
/// Timer used to refresh the temperature measurement value
|
|
APP_HT_MEAS_INTV_TIMER,
|
|
#endif //(BLE_APP_HT)
|
|
|
|
#if (BLE_APP_HID)
|
|
/// Timer used to disconnect the moue if no activity is detecter
|
|
APP_HID_MOUSE_TIMEOUT_TIMER,
|
|
#endif //(BLE_APP_HID)
|
|
#endif //(BLE_APP_PRF)
|
|
|
|
#if(SLAVE_SEND_TEST_DATA)
|
|
SLAVE_SEND_TEST_DATA_TIMER,
|
|
#endif //(SLAVE_SEND_TEST_DATA)
|
|
};
|
|
|
|
struct app_subtask_handlers
|
|
{
|
|
/// Pointer to the message handler table
|
|
const struct ke_msg_handler *p_msg_handler_tab;
|
|
/// Number of messages handled
|
|
uint16_t msg_cnt;
|
|
};
|
|
|
|
|
|
/**
|
|
****************************************************************************************
|
|
* @brief Initialize the BLE demo application.
|
|
****************************************************************************************
|
|
*/
|
|
extern struct app_env_tag app_env;
|
|
void app_init(void);
|
|
struct app_env_tag *get_app_env(void);
|
|
/// @} APPTASK
|
|
|
|
#endif // APP_TASK_H_
|