BLE_TYQ_BJQ_CH584M/APP/multiCentral.c

1370 lines
48 KiB
C
Raw Normal View History

2024-12-03 11:11:30 +08:00
/*********************************************************************
* INCLUDES
*/
#include "CONFIG.h"
#include "gattprofile.h"
#include "multiCentral.h"
2024-12-07 17:09:36 +08:00
#include "bsp_uart.h"
#include "log.h"
2024-12-13 19:07:00 +08:00
#include "bsp_flash.h"
2024-12-11 20:03:50 +08:00
#include "bsp_valve.h"
2024-12-07 17:09:36 +08:00
#undef LOG_ENABLE
#define LOG_ENABLE 1
2024-12-16 15:34:47 +08:00
#define MASTER_START_EVT (0X01 << 0)
#define MASTER_CONNECT_EVT (0X01 << 1)
#define MASTER_DISCONN_EVT (0X01 << 2)
#define MASTER_DETECT_EVT (0X01 << 3)
uint8_t MasterCtrTaskId;
2025-05-23 18:02:43 +08:00
uint8_t bt_conncet_cnt = 0;
2024-12-16 15:34:47 +08:00
2024-12-03 11:11:30 +08:00
/*********************************************************************
* MACROS
*/
// Length of bd addr as a string
2024-12-11 20:03:50 +08:00
#define B_ADDR_STR_LEN 15
2024-12-03 11:11:30 +08:00
/*********************************************************************
* CONSTANTS
*/
// Maximum number of scan responses
2024-12-16 15:34:47 +08:00
#define DEFAULT_MAX_SCAN_RES 1
2024-12-03 11:11:30 +08:00
2024-12-11 20:03:50 +08:00
// Scan duration in 0.625ms
2025-05-30 10:46:10 +08:00
#define DEFAULT_SCAN_DURATION (1600 * 3) // 2400*0.625 = 1500ms
2024-12-03 11:11:30 +08:00
// Connection min interval in 1.25ms
2024-12-16 19:22:58 +08:00
#define DEFAULT_MIN_CONNECTION_INTERVAL (80 * 4)
2024-12-03 11:11:30 +08:00
// Connection max interval in 1.25ms
2025-05-30 10:46:10 +08:00
#define DEFAULT_MAX_CONNECTION_INTERVAL 80 * 6
2024-12-03 11:11:30 +08:00
// Connection supervision timeout in 10ms
2024-12-16 19:22:58 +08:00
#define DEFAULT_CONNECTION_TIMEOUT (100 * 3)
2024-12-03 11:11:30 +08:00
// Discovey mode (limited, general, all)
2024-12-11 20:03:50 +08:00
#define DEFAULT_DISCOVERY_MODE DEVDISC_MODE_ALL
2024-12-03 11:11:30 +08:00
// TRUE to use active scan
2024-12-11 20:03:50 +08:00
#define DEFAULT_DISCOVERY_ACTIVE_SCAN TRUE
2024-12-03 11:11:30 +08:00
// TRUE to use white list during discovery
2024-12-11 20:03:50 +08:00
#define DEFAULT_DISCOVERY_WHITE_LIST FALSE
2024-12-03 11:11:30 +08:00
// TRUE to use high scan duty cycle when creating link
2024-12-13 19:07:00 +08:00
#define DEFAULT_LINK_HIGH_DUTY_CYCLE TRUE
2024-12-03 11:11:30 +08:00
// TRUE to use white list when creating link
2024-12-11 20:03:50 +08:00
#define DEFAULT_LINK_WHITE_LIST FALSE
2024-12-03 11:11:30 +08:00
// Default read RSSI period in 0.625ms
2024-12-16 15:34:47 +08:00
#define DEFAULT_RSSI_PERIOD 4800
2024-12-03 11:11:30 +08:00
// Minimum connection interval (units of 1.25ms)
2024-12-16 15:34:47 +08:00
#define DEFAULT_UPDATE_MIN_CONN_INTERVAL DEFAULT_MIN_CONNECTION_INTERVAL
2024-12-03 11:11:30 +08:00
// Maximum connection interval (units of 1.25ms)
2024-12-16 15:34:47 +08:00
#define DEFAULT_UPDATE_MAX_CONN_INTERVAL DEFAULT_MAX_CONNECTION_INTERVAL
2024-12-11 20:03:50 +08:00
2024-12-03 11:11:30 +08:00
// Slave latency to use parameter update
2024-12-16 19:22:58 +08:00
#define DEFAULT_UPDATE_SLAVE_LATENCY 0
2024-12-03 11:11:30 +08:00
// Supervision timeout value (units of 10ms)
2024-12-16 15:34:47 +08:00
#define DEFAULT_UPDATE_CONN_TIMEOUT 100 * 5
2024-12-03 11:11:30 +08:00
// Default passcode
2024-12-11 20:03:50 +08:00
#define DEFAULT_PASSCODE 0
2024-12-03 11:11:30 +08:00
// Default GAP pairing mode
2024-12-11 20:03:50 +08:00
#define DEFAULT_PAIRING_MODE GAPBOND_PAIRING_MODE_WAIT_FOR_REQ
2024-12-03 11:11:30 +08:00
// Default MITM mode (TRUE to require passcode or OOB when pairing)
2024-12-11 20:03:50 +08:00
#define DEFAULT_MITM_MODE TRUE
2024-12-03 11:11:30 +08:00
// Default bonding mode, TRUE to bond, max bonding 6 devices
2024-12-11 20:03:50 +08:00
#define DEFAULT_BONDING_MODE TRUE
2024-12-03 11:11:30 +08:00
// Default GAP bonding I/O capabilities
2024-12-11 20:03:50 +08:00
#define DEFAULT_IO_CAPABILITIES GAPBOND_IO_CAP_NO_INPUT_NO_OUTPUT
2024-12-03 11:11:30 +08:00
// Default service discovery timer delay in 0.625ms
2025-05-23 18:02:43 +08:00
#define DEFAULT_SVC_DISCOVERY_DELAY 2
2024-12-03 11:11:30 +08:00
// Default parameter update delay in 0.625ms
2024-12-16 15:34:47 +08:00
#define DEFAULT_PARAM_UPDATE_DELAY 1600
2024-12-03 11:11:30 +08:00
// Default read or write timer delay in 0.625ms
2024-12-11 20:03:50 +08:00
#define DEFAULT_READ_OR_WRITE_DELAY 1600
2024-12-03 11:11:30 +08:00
// Default write CCCD delay in 0.625ms
2024-12-11 20:03:50 +08:00
#define DEFAULT_WRITE_CCCD_DELAY 1600
2024-12-03 11:11:30 +08:00
// Establish link timeout in 0.625ms
2024-12-16 15:34:47 +08:00
#define ESTABLISH_LINK_TIMEOUT 1600 * 3
2024-12-03 11:11:30 +08:00
// Application states
enum
{
BLE_STATE_IDLE,
BLE_STATE_CONNECTING,
BLE_STATE_CONNECTED,
BLE_STATE_DISCONNECTING
};
// Discovery states
enum
{
BLE_DISC_STATE_IDLE, // Idle
BLE_DISC_STATE_SVC, // Service discovery
2024-12-11 20:03:50 +08:00
BLE_DISC_STATE_CHAR, // Characteristic discovery
BLE_DISC_STATE_CCCD, // client characteristic configuration discovery
2024-12-03 11:11:30 +08:00
};
2024-12-11 20:03:50 +08:00
2024-12-03 11:11:30 +08:00
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* GLOBAL VARIABLES
*/
/*********************************************************************
* EXTERNAL VARIABLES
*/
/*********************************************************************
* EXTERNAL FUNCTIONS
*/
/*********************************************************************
* LOCAL VARIABLES
*/
// Task ID for internal task/event processing
2024-12-13 19:07:00 +08:00
uint8_t centralTaskId;
2024-12-03 11:11:30 +08:00
// Number of scan results
static uint8_t centralScanRes;
// Scan result list
static gapDevRec_t centralDevList[DEFAULT_MAX_SCAN_RES];
// Peer device address
2024-12-13 19:07:00 +08:00
peerAddrDefItem_t PeerAddrDef[CENTRAL_MAX_CONNECTION] = {
// {0xb6, 0xb4, 0x8f, 0x10, 0x53, 0x5c},
2024-12-03 11:11:30 +08:00
};
// Connection item list
2024-12-11 20:03:50 +08:00
centralConnItem_t centralConnList[CENTRAL_MAX_CONNECTION];
2024-12-03 11:11:30 +08:00
// Value to write
2024-12-07 17:09:36 +08:00
static uint8_t centralCharVal = 0x01;
2024-12-03 11:11:30 +08:00
// Value read/write toggle
static uint8_t centralDoWrite = TRUE;
/*********************************************************************
* LOCAL FUNCTIONS
*/
2024-12-11 20:03:50 +08:00
static void centralProcessGATTMsg(gattMsgEvent_t *pMsg);
static void centralRssiCB(uint16_t connHandle, int8_t rssi);
static void centralEventCB(gapRoleEvent_t *pEvent);
static void centralHciMTUChangeCB(uint16_t connHandle, uint16_t maxTxOctets, uint16_t maxRxOctets);
static void centralPasscodeCB(uint8_t *deviceAddr, uint16_t connectionHandle,
uint8_t uiInputs, uint8_t uiOutputs);
static void centralPairStateCB(uint16_t connHandle, uint8_t state, uint8_t status);
2024-12-03 11:11:30 +08:00
static uint16_t connect0_ProcessEvent(uint8_t task_id, uint16_t events);
2024-12-11 20:03:50 +08:00
static void central_ProcessTMOSMsg(tmos_event_hdr_t *pMsg);
static void centralGATTDiscoveryEvent(uint8_t connItem, gattMsgEvent_t *pMsg);
static void centralConnIistStartDiscovery_0(void);
static void centralAddDeviceInfo(uint8_t *pAddr, uint8_t addrType);
static void centralInitConnItem(uint8_t task_id, centralConnItem_t *centralConnList);
static uint8_t centralAddrCmp(peerAddrDefItem_t *PeerAddrDef, uint8_t *addr);
int BSP_Master_Receive_Data(uint8_t task_id, uint8_t *data, uint16_t len);
2024-12-16 15:34:47 +08:00
uint16_t Master_ProcessEvent(uint8_t task_id, uint16_t events);
2024-12-03 11:11:30 +08:00
/*********************************************************************
* PROFILE CALLBACKS
*/
// GAP Role Callbacks
static gapCentralRoleCB_t centralRoleCB = {
centralRssiCB, // RSSI callback
centralEventCB, // Event callback
centralHciMTUChangeCB // MTU change callback
};
// Bond Manager Callbacks
static gapBondCBs_t centralBondCB = {
2024-12-11 20:03:50 +08:00
centralPasscodeCB,
centralPairStateCB};
2024-12-03 11:11:30 +08:00
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/*********************************************************************
* @fn Central_Init
*
* @brief Initialization function for the Central App Task.
* This is called during initialization and should contain
* any application specific initialization (ie. hardware
* initialization/setup, table initialization, power up
* notification).
*
* @param task_id - the ID assigned by TMOS. This ID should be
* used to send messages and set timers.
*
* @return none
*/
void Central_Init()
{
2024-12-11 20:03:50 +08:00
centralTaskId = TMOS_ProcessEventRegister(Central_ProcessEvent);
2024-12-03 11:11:30 +08:00
// Setup GAP
2024-12-11 20:03:50 +08:00
GAP_SetParamValue(TGAP_DISC_SCAN, DEFAULT_SCAN_DURATION);
GAP_SetParamValue(TGAP_CONN_EST_INT_MIN, DEFAULT_MIN_CONNECTION_INTERVAL);
GAP_SetParamValue(TGAP_CONN_EST_INT_MAX, DEFAULT_MAX_CONNECTION_INTERVAL);
GAP_SetParamValue(TGAP_CONN_EST_SUPERV_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
2024-12-03 11:11:30 +08:00
// Setup the GAP Bond Manager
{
2024-12-11 20:03:50 +08:00
uint32_t passkey = DEFAULT_PASSCODE;
uint8_t pairMode = DEFAULT_PAIRING_MODE;
uint8_t mitm = DEFAULT_MITM_MODE;
uint8_t ioCap = DEFAULT_IO_CAPABILITIES;
uint8_t bonding = DEFAULT_BONDING_MODE;
2024-12-03 11:11:30 +08:00
GAPBondMgr_SetParameter(GAPBOND_CENT_DEFAULT_PASSCODE, sizeof(uint32_t), &passkey);
GAPBondMgr_SetParameter(GAPBOND_CENT_PAIRING_MODE, sizeof(uint8_t), &pairMode);
GAPBondMgr_SetParameter(GAPBOND_CENT_MITM_PROTECTION, sizeof(uint8_t), &mitm);
GAPBondMgr_SetParameter(GAPBOND_CENT_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
GAPBondMgr_SetParameter(GAPBOND_CENT_BONDING_ENABLED, sizeof(uint8_t), &bonding);
}
// Init Connection Item
centralInitConnItem(centralTaskId, centralConnList);
// Initialize GATT Client
2024-12-11 20:03:50 +08:00
GATT_InitClient();
2024-12-16 15:34:47 +08:00
2024-12-03 11:11:30 +08:00
// Register to receive incoming ATT Indications/Notifications
GATT_RegisterForInd(centralTaskId);
// Setup a delayed profile startup
tmos_set_event(centralTaskId, START_DEVICE_EVT);
2024-12-16 15:34:47 +08:00
//用户自定义
MasterCtrTaskId = TMOS_ProcessEventRegister(Master_ProcessEvent);
tmos_set_event(MasterCtrTaskId, MASTER_START_EVT);
2024-12-03 11:11:30 +08:00
}
/*********************************************************************
* @fn centralInitConnItem
*
* @brief Init Connection Item
*
* @param task_id -
* centralConnList -
*
* @return NULL
*/
static void centralInitConnItem(uint8_t task_id, centralConnItem_t *centralConnList)
{
uint8_t connItem;
2024-12-11 20:03:50 +08:00
for (connItem = 0; connItem < CENTRAL_MAX_CONNECTION; connItem++)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
// 每个连接的任务通过taskID区分
centralConnList[connItem].taskID = TMOS_ProcessEventRegister(Central_ProcessEvent);
centralConnList[connItem].connHandle = GAP_CONNHANDLE_INIT;
centralConnList[connItem].state = BLE_STATE_IDLE;
centralConnList[connItem].discState = BLE_DISC_STATE_IDLE;
2024-12-03 11:11:30 +08:00
centralConnList[connItem].procedureInProgress = FALSE;
2024-12-11 20:03:50 +08:00
centralConnList[connItem].charHdl = 0;
centralConnList[connItem].svcStartHdl = 0;
centralConnList[connItem].svcEndHdl = 0;
centralConnList[connItem].cccHdl = 0;
2024-12-03 11:11:30 +08:00
}
}
/*********************************************************************
* @fn Central_ProcessEvent
*
* @brief Central Application Task event processor. This function
* is called to process all events for the task. Events
* include timers, messages and any other user defined events.
2024-12-11 20:03:50 +08:00
*
2024-12-03 11:11:30 +08:00
* @param task_id - The TMOS assigned task ID.
* @param events - events to process. This is a bit map and can
* contain more than one event.
*
* @return events not processed
*/
uint16_t Central_ProcessEvent(uint8_t task_id, uint16_t events)
{
2024-12-11 20:03:50 +08:00
if (events & SYS_EVENT_MSG)
2024-12-03 11:11:30 +08:00
{
uint8_t *pMsg;
2024-12-11 20:03:50 +08:00
if ((pMsg = tmos_msg_receive(centralTaskId)) != NULL)
2024-12-03 11:11:30 +08:00
{
central_ProcessTMOSMsg((tmos_event_hdr_t *)pMsg);
// Release the TMOS message
tmos_msg_deallocate(pMsg);
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
2024-12-11 20:03:50 +08:00
if (events & START_DEVICE_EVT)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
// Start the Device
2024-12-03 11:11:30 +08:00
GAPRole_CentralStartDevice(centralTaskId, &centralBondCB, &centralRoleCB);
return (events ^ START_DEVICE_EVT);
}
2024-12-11 20:03:50 +08:00
if (events & ESTABLISH_LINK_TIMEOUT_EVT)
2024-12-03 11:11:30 +08:00
{
GAPRole_TerminateLink(INVALID_CONNHANDLE);
return (events ^ ESTABLISH_LINK_TIMEOUT_EVT);
}
2024-12-11 20:03:50 +08:00
// 连接0的任务处理
if (task_id == centralConnList[CONNECT0_ITEM].taskID)
2024-12-03 11:11:30 +08:00
{
return connect0_ProcessEvent(task_id, events);
}
2024-12-11 20:03:50 +08:00
// 连接1的任务处理
else if (task_id == centralConnList[CONNECT1_ITEM].taskID)
2024-12-03 11:11:30 +08:00
{
}
2024-12-11 20:03:50 +08:00
// 连接2的任务处理
// else if(task_id == centralConnList[CONNECT2_ITEM].taskID)
// {
// }
2024-12-03 11:11:30 +08:00
// Discard unknown events
return 0;
}
/*********************************************************************
* @fn connect0_ProcessEvent
*
* @brief Process an incoming task message.
*
* @param pMsg - message to process
*
* @return none
*/
static uint16_t connect0_ProcessEvent(uint8_t task_id, uint16_t events)
{
// logDebug("connect0_ProcessEvent 0x%02x", events);
2024-12-11 20:03:50 +08:00
if (events & START_SVC_DISCOVERY_EVT)
2024-12-03 11:11:30 +08:00
{
// logDebug("START_SVC_DISCOVERY_EVT");
2024-12-16 19:22:58 +08:00
2024-12-03 11:11:30 +08:00
// start service discovery
2024-12-16 19:22:58 +08:00
// centralConnIistStartDiscovery_0();
logDebug("发现句柄配置\r\n");
centralConnList[CONNECT0_ITEM].charHdl = 0X23;
centralConnList[CONNECT0_ITEM].charHd2 = 0X26;
centralConnList[CONNECT0_ITEM].charHd3 = 0x29;
centralConnList[CONNECT0_ITEM].charHd4 = 0x2C;
centralConnList[CONNECT0_ITEM].procedureInProgress = FALSE;
2024-12-16 15:34:47 +08:00
2024-12-03 11:11:30 +08:00
return (events ^ START_SVC_DISCOVERY_EVT);
}
2024-12-11 20:03:50 +08:00
if (events & START_READ_OR_WRITE_EVT)
2024-12-03 11:11:30 +08:00
{
2024-12-16 15:34:47 +08:00
logDebug("START_READ_OR_WRITE_EVT");
2024-12-11 20:03:50 +08:00
if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (centralDoWrite)
2024-12-03 11:11:30 +08:00
{
// Do a write
attWriteReq_t req;
2024-12-11 20:03:50 +08:00
req.cmd = FALSE;
req.sig = FALSE;
2024-12-03 11:11:30 +08:00
req.handle = centralConnList[CONNECT0_ITEM].charHdl;
2024-12-11 20:03:50 +08:00
req.len = 1;
2024-12-03 11:11:30 +08:00
req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0);
2024-12-11 20:03:50 +08:00
if (req.pValue != NULL)
2024-12-03 11:11:30 +08:00
{
*req.pValue = centralCharVal;
2024-12-11 20:03:50 +08:00
if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS)
2024-12-03 11:11:30 +08:00
{
centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE;
2024-12-11 20:03:50 +08:00
centralDoWrite = !centralDoWrite;
2024-12-03 11:11:30 +08:00
tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_READ_OR_WRITE_EVT, DEFAULT_READ_OR_WRITE_DELAY);
}
else
{
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
}
}
}
else
{
// Do a read
attReadReq_t req;
req.handle = centralConnList[CONNECT0_ITEM].charHdl;
2024-12-11 20:03:50 +08:00
if (GATT_ReadCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS)
2024-12-03 11:11:30 +08:00
{
centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE;
2024-12-11 20:03:50 +08:00
centralDoWrite = !centralDoWrite;
2024-12-03 11:11:30 +08:00
}
}
}
return (events ^ START_READ_OR_WRITE_EVT);
}
2024-12-12 15:56:21 +08:00
if (events & CH2_READ_EVT)
{
logDebug("CH2_READ_EVT\r\n");
if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE)
{
// read
attReadReq_t req;
2024-12-03 11:11:30 +08:00
2024-12-12 15:56:21 +08:00
req.handle = centralConnList[CONNECT0_ITEM].charHd2;
if (GATT_ReadCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS)
{
centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE;
tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH2_READ_EVT, 160000);
}
}
return (events ^ CH2_READ_EVT);
}
2024-12-11 20:03:50 +08:00
if (events & CH3_WRITE_EVT)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("CH3_WRITE_EVT\r\n");
2024-12-11 20:03:50 +08:00
if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE)
2024-12-03 11:11:30 +08:00
{
2024-12-16 15:34:47 +08:00
// logDebug("centralConnList CH3_WRITE_EVT\n");
2024-12-11 20:03:50 +08:00
static uint8_t sendcnt = 0;
2024-12-03 11:11:30 +08:00
attWriteReq_t req;
2024-12-11 20:03:50 +08:00
req.cmd = FALSE;
req.sig = FALSE;
req.handle = centralConnList[CONNECT0_ITEM].charHd3;
2024-12-16 15:34:47 +08:00
// logDebug("handle:%x\r\n", req.handle);
2024-12-11 20:03:50 +08:00
req.len = 3;
2024-12-03 11:11:30 +08:00
req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0);
2024-12-11 20:03:50 +08:00
if (req.pValue != NULL)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("req.pValue != NULL\r\n");
2024-12-11 20:03:50 +08:00
req.pValue[0] = 0x11;
req.pValue[1] = 0x22;
req.pValue[2] = sendcnt++;
2024-12-03 11:11:30 +08:00
2024-12-11 20:03:50 +08:00
if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS)
2024-12-03 11:11:30 +08:00
{
centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE;
2024-12-12 15:56:21 +08:00
tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH3_WRITE_EVT, 3200);
logDebug("GATT_WriteCharValue\r\n");
2024-12-03 11:11:30 +08:00
}
else
{
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
}
}
}
2024-12-11 20:03:50 +08:00
return (events ^ CH3_WRITE_EVT);
2024-12-07 17:09:36 +08:00
}
2024-12-12 15:56:21 +08:00
if (events & CH4_NOTI_EVT)
2024-12-07 17:09:36 +08:00
{
// logDebug("CH4_NOTI_EVT\r\n");
2024-12-11 20:03:50 +08:00
if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE)
2024-12-07 17:09:36 +08:00
{
// Do a write
attWriteReq_t req;
2024-12-16 15:34:47 +08:00
logDebug("CH4_NOTI_EVT handle:%x\r\n", req.handle + 1);
2024-12-11 20:03:50 +08:00
req.cmd = FALSE;
req.sig = FALSE;
2024-12-16 15:34:47 +08:00
req.handle = centralConnList[CONNECT0_ITEM].charHd4 + 1;
2024-12-11 20:03:50 +08:00
req.len = 2;
2024-12-12 15:56:21 +08:00
req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0); // 内存申请
2024-12-11 20:03:50 +08:00
if (req.pValue != NULL)
2024-12-07 17:09:36 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("CH4_NOTI_EVT req.pValue != NULL\r\n");
2024-12-07 17:09:36 +08:00
req.pValue[0] = 1;
req.pValue[1] = 0;
2024-12-12 15:56:21 +08:00
if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS) // 发送
2024-12-07 17:09:36 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("GATT_WriteCharValue CH4_NOTI_EVT\r\n");
2024-12-11 20:03:50 +08:00
centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE;
2024-12-07 17:09:36 +08:00
}
else
{
2024-12-12 15:56:21 +08:00
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); // 释放内存
2024-12-07 17:09:36 +08:00
}
}
}
2024-12-12 15:56:21 +08:00
return (events ^ CH4_NOTI_EVT);
2024-12-07 17:09:36 +08:00
}
2024-12-12 15:56:21 +08:00
// if (events & START_WRITE_CCCD_EVT)
// {
// if (centralConnList[CONNECT0_ITEM].procedureInProgress == FALSE)
// {
// // Do a write
// attWriteReq_t req;
// req.cmd = FALSE;
// req.sig = FALSE;
// req.handle = centralConnList[CONNECT0_ITEM].cccHdl;
// req.len = 2;
// req.pValue = GATT_bm_alloc(centralConnList[CONNECT0_ITEM].connHandle, ATT_WRITE_REQ, req.len, NULL, 0);
// if (req.pValue != NULL)
// {
// req.pValue[0] = 1;
// req.pValue[1] = 0;
// if (GATT_WriteCharValue(centralConnList[CONNECT0_ITEM].connHandle, &req, centralTaskId) == SUCCESS)
// {
// centralConnList[CONNECT0_ITEM].procedureInProgress = TRUE;
// }
// else
// {
// GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
// }
// }
// }
// return (events ^ START_WRITE_CCCD_EVT);
// }
2024-12-11 20:03:50 +08:00
if (events & START_READ_RSSI_EVT)
2024-12-07 17:09:36 +08:00
{
2024-12-11 20:03:50 +08:00
GAPRole_ReadRssiCmd(centralConnList[CONNECT0_ITEM].connHandle);
tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_READ_RSSI_EVT, DEFAULT_RSSI_PERIOD);
2024-12-07 17:09:36 +08:00
return (events ^ START_READ_RSSI_EVT);
}
2024-12-12 15:56:21 +08:00
if (events & START_PARAM_UPDATE_EVT)
{
// start connect parameter update
GAPRole_UpdateLink(centralConnList[CONNECT0_ITEM].connHandle,
DEFAULT_UPDATE_MIN_CONN_INTERVAL,
DEFAULT_UPDATE_MAX_CONN_INTERVAL,
DEFAULT_UPDATE_SLAVE_LATENCY,
DEFAULT_UPDATE_CONN_TIMEOUT);
return (events ^ START_PARAM_UPDATE_EVT);
}
2024-12-07 17:09:36 +08:00
// Discard unknown events
return 0;
}
2024-12-03 11:11:30 +08:00
/*********************************************************************
* @fn central_ProcessTMOSMsg
*
* @brief Process an incoming task message.
*
* @param pMsg - message to process
*
* @return none
*/
static void central_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
{
2024-12-11 20:03:50 +08:00
switch (pMsg->event)
2024-12-03 11:11:30 +08:00
{
case GATT_MSG_EVENT:
centralProcessGATTMsg((gattMsgEvent_t *)pMsg);
break;
}
}
/*********************************************************************
* @fn centralProcessGATTMsg
*
* @brief Process GATT messages
*
* @return none
*/
static void centralProcessGATTMsg(gattMsgEvent_t *pMsg)
{
uint8_t connItem;
2024-12-11 20:03:50 +08:00
for (connItem = 0; connItem < CENTRAL_MAX_CONNECTION; connItem++)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (centralConnList[connItem].connHandle == pMsg->connHandle)
2024-12-03 11:11:30 +08:00
break;
}
2024-12-11 20:03:50 +08:00
if (connItem == CENTRAL_MAX_CONNECTION)
2024-12-03 11:11:30 +08:00
{
return;
// Should not go there
}
2024-12-11 20:03:50 +08:00
if (centralConnList[connItem].state != BLE_STATE_CONNECTED)
2024-12-03 11:11:30 +08:00
{
// In case a GATT message came after a connection has dropped,
// ignore the message
GATT_bm_free(&pMsg->msg, pMsg->method);
return;
}
2024-12-11 20:03:50 +08:00
if ((pMsg->method == ATT_EXCHANGE_MTU_RSP) ||
((pMsg->method == ATT_ERROR_RSP) &&
(pMsg->msg.errorRsp.reqOpcode == ATT_EXCHANGE_MTU_REQ)))
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (pMsg->method == ATT_ERROR_RSP)
2024-12-03 11:11:30 +08:00
{
uint8_t status = pMsg->msg.errorRsp.errCode;
2024-12-12 15:56:21 +08:00
logDebug("Exchange MTU Error: %x\r\n", status);
2024-12-03 11:11:30 +08:00
}
centralConnList[connItem].procedureInProgress = FALSE;
}
2024-12-11 20:03:50 +08:00
if (pMsg->method == ATT_MTU_UPDATED_EVENT)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("MTU: %x\r\n", pMsg->msg.mtuEvt.MTU);
2024-12-03 11:11:30 +08:00
}
2024-12-16 15:34:47 +08:00
if ((pMsg->method == ATT_READ_RSP) || ((pMsg->method == ATT_ERROR_RSP) && (pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ)))
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (pMsg->method == ATT_ERROR_RSP)
2024-12-03 11:11:30 +08:00
{
uint8_t status = pMsg->msg.errorRsp.errCode;
2024-12-12 15:56:21 +08:00
logDebug("Read Error: %x\r\n", status);
2024-12-03 11:11:30 +08:00
}
else
{
// After a successful read, display the read value
2024-12-12 15:56:21 +08:00
logDebug("Read rsp: ");
2024-12-16 15:34:47 +08:00
// logHexDumpAll(pMsg->msg.readRsp.pValue, pMsg->msg.readRsp.len);
2024-12-03 11:11:30 +08:00
}
centralConnList[connItem].procedureInProgress = FALSE;
}
2024-12-16 15:34:47 +08:00
else if ((pMsg->method == ATT_WRITE_RSP) || ((pMsg->method == ATT_ERROR_RSP) && (pMsg->msg.errorRsp.reqOpcode == ATT_WRITE_REQ)))
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (pMsg->method == ATT_ERROR_RSP == ATT_ERROR_RSP)
2024-12-03 11:11:30 +08:00
{
uint8_t status = pMsg->msg.errorRsp.errCode;
2024-12-12 15:56:21 +08:00
logDebug("Write Error: %x\r\n", status);
2024-12-03 11:11:30 +08:00
}
else
{
// After a succesful write, display the value that was written and increment value
2024-12-12 15:56:21 +08:00
logDebug("Write sent: %x\r\n", centralCharVal);
2024-12-03 11:11:30 +08:00
}
centralConnList[connItem].procedureInProgress = FALSE;
}
2024-12-11 20:03:50 +08:00
else if (pMsg->method == ATT_HANDLE_VALUE_NOTI)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("Noti: ");
if (connItem == CONNECT0_ITEM)
{
BSP_Master_Receive_Data(CONNECT0_ITEM, pMsg->msg.handleValueNoti.pValue, pMsg->msg.handleValueNoti.len);
}
2024-12-03 11:11:30 +08:00
}
2024-12-11 20:03:50 +08:00
else if (centralConnList[connItem].discState != BLE_DISC_STATE_IDLE)
2024-12-03 11:11:30 +08:00
{
centralGATTDiscoveryEvent(connItem, pMsg);
2024-12-16 15:34:47 +08:00
centralConnList[connItem].discState = BLE_DISC_STATE_IDLE;
2024-12-03 11:11:30 +08:00
}
GATT_bm_free(&pMsg->msg, pMsg->method);
}
/*********************************************************************
* @fn centralRssiCB
*
* @brief RSSI callback.
*
* @param connHandle - connection handle
* @param rssi - RSSI
*
* @return none
*/
static void centralRssiCB(uint16_t connHandle, int8_t rssi)
{
2024-12-12 15:56:21 +08:00
logDebug("RSSI -%d dB Conn - %x \r\n", -rssi, connHandle);
2024-12-03 11:11:30 +08:00
}
/*********************************************************************
* @fn centralHciMTUChangeCB
*
* @brief MTU changed callback.
*
* @param maxTxOctets - Max tx octets
* @param maxRxOctets - Max rx octets
*
* @return none
*/
static void centralHciMTUChangeCB(uint16_t connHandle, uint16_t maxTxOctets, uint16_t maxRxOctets)
{
2024-12-16 15:34:47 +08:00
logDebug("MTU changed: %d, %d\r\n", maxTxOctets, maxRxOctets);
// centralConnList[CONNECT0_ITEM].procedureInProgress = FALSE;
// tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH4_NOTI_EVT, 1600);
2024-12-03 11:11:30 +08:00
}
/*********************************************************************
* @fn centralEventCB
*
* @brief Central event callback function.
*
* @param pEvent - pointer to event structure
*
* @return none
*/
static void centralEventCB(gapRoleEvent_t *pEvent)
{
2024-12-11 20:03:50 +08:00
switch (pEvent->gap.opcode)
2024-12-03 11:11:30 +08:00
{
case GAP_DEVICE_INIT_DONE_EVENT:
{
2024-12-16 15:34:47 +08:00
GAPRole_CentralCancelDiscovery();
2024-12-16 20:47:16 +08:00
// logDebug("停止扫描,发起直连\r\n");
// tmos_start_task(MasterCtrTaskId, MASTER_CONNECT_EVT, 800);
2024-12-03 11:11:30 +08:00
}
break;
case GAP_DEVICE_INFO_EVENT:
{
// Add device to list
centralAddDeviceInfo(pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType);
}
break;
case GAP_DEVICE_DISCOVERY_EVENT:
{
2024-12-16 15:34:47 +08:00
Master_Connect();
logDebug("Connecting...\r\n");
2024-12-03 11:11:30 +08:00
}
break;
case GAP_LINK_ESTABLISHED_EVENT:
{
2024-12-11 20:03:50 +08:00
tmos_stop_task(centralTaskId, ESTABLISH_LINK_TIMEOUT_EVT);
if (pEvent->gap.hdr.status == SUCCESS)
2024-12-03 11:11:30 +08:00
{
uint8_t connItem;
2024-12-11 20:03:50 +08:00
// 查询是否有空余连接条目
for (connItem = 0; connItem < CENTRAL_MAX_CONNECTION; connItem++)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (centralConnList[connItem].connHandle == GAP_CONNHANDLE_INIT)
2024-12-03 11:11:30 +08:00
break;
}
2024-12-11 20:03:50 +08:00
if (connItem == CENTRAL_MAX_CONNECTION)
2024-12-03 11:11:30 +08:00
{
GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle);
2024-12-12 15:56:21 +08:00
logDebug("Connection max...\r\n");
2024-12-03 11:11:30 +08:00
}
else
{
2024-12-11 20:03:50 +08:00
centralConnList[connItem].state = BLE_STATE_CONNECTED;
2024-12-03 11:11:30 +08:00
centralConnList[connItem].connHandle = pEvent->linkCmpl.connectionHandle;
2024-12-12 15:56:21 +08:00
logDebug("Conn %x - Int %x \r\n", pEvent->linkCmpl.connectionHandle, pEvent->linkCmpl.connInterval);
2024-12-03 11:11:30 +08:00
2024-12-11 20:03:50 +08:00
// 连接0
if (connItem == CONNECT0_ITEM)
2024-12-03 11:11:30 +08:00
{
centralConnList[connItem].procedureInProgress = TRUE;
2024-12-16 15:34:47 +08:00
// update MTU
// attExchangeMTUReq_t req =
// {
// .clientRxMTU = BLE_BUFF_MAX_LEN - 4,
// };
// GATT_ExchangeMTU(pEvent->linkCmpl.connectionHandle, &req, centralTaskId);
// Initiate service discovery
2024-12-03 11:11:30 +08:00
tmos_start_task(centralConnList[connItem].taskID, START_SVC_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY);
2024-12-11 20:03:50 +08:00
// Initiate connect parameter update
2024-12-03 11:11:30 +08:00
tmos_start_task(centralConnList[connItem].taskID, START_PARAM_UPDATE_EVT, DEFAULT_PARAM_UPDATE_DELAY);
2024-12-11 20:03:50 +08:00
// Start RSSI polling
2024-12-03 11:11:30 +08:00
tmos_start_task(centralConnList[connItem].taskID, START_READ_RSSI_EVT, DEFAULT_RSSI_PERIOD);
2024-12-11 20:03:50 +08:00
}
2024-12-12 15:56:21 +08:00
logDebug("Connected...\r\n");
2024-12-03 11:11:30 +08:00
// See if need discover again
2024-12-11 20:03:50 +08:00
for (connItem = 0; connItem < CENTRAL_MAX_CONNECTION; connItem++)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (centralConnList[connItem].connHandle == GAP_CONNHANDLE_INIT)
2024-12-03 11:11:30 +08:00
break;
}
2024-12-11 20:03:50 +08:00
if (connItem < CENTRAL_MAX_CONNECTION)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("Discovering...\r\n");
2024-12-03 11:11:30 +08:00
centralScanRes = 0;
}
}
}
else
{
2024-12-12 15:56:21 +08:00
logDebug("Connect Failed...Reason:%X\r\n", pEvent->gap.hdr.status);
logDebug("Discovering...\r\n");
2024-12-03 11:11:30 +08:00
centralScanRes = 0;
}
}
break;
2024-12-11 20:03:50 +08:00
case GAP_LINK_TERMINATED_EVENT:
2024-12-03 11:11:30 +08:00
{
uint8_t connItem;
2024-12-11 20:03:50 +08:00
for (connItem = 0; connItem < CENTRAL_MAX_CONNECTION; connItem++)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (centralConnList[connItem].connHandle == pEvent->linkTerminate.connectionHandle)
2024-12-03 11:11:30 +08:00
break;
}
2024-12-11 20:03:50 +08:00
if (connItem == CENTRAL_MAX_CONNECTION)
2024-12-03 11:11:30 +08:00
{
// Should not go there
}
2024-12-12 15:56:21 +08:00
logDebug(" %x Disconnected...Reason:%x\r\n", centralConnList[connItem].connHandle, pEvent->linkTerminate.reason);
2024-12-11 20:03:50 +08:00
centralConnList[connItem].state = BLE_STATE_IDLE;
centralConnList[connItem].connHandle = GAP_CONNHANDLE_INIT;
centralConnList[connItem].discState = BLE_DISC_STATE_IDLE;
centralConnList[connItem].charHdl = 0;
2024-12-03 11:11:30 +08:00
centralConnList[connItem].procedureInProgress = FALSE;
2024-12-11 20:03:50 +08:00
centralScanRes = 0;
2024-12-03 11:11:30 +08:00
tmos_stop_task(centralConnList[connItem].taskID, START_READ_RSSI_EVT);
2024-12-12 15:56:21 +08:00
logDebug("Discovering...\r\n");
2025-04-24 10:33:19 +08:00
//更新阀门状态信息
if (bt_connect_flag == 1)
{
bt_connect_flag = 0;
valve_list.valve_data[0].valve_connct_status = FALSE;
valve_list.valve_data[0].valve_switch_status = FALSE;
valve_list.valve_data[0].valve_temp = FALSE;
2025-05-28 16:31:12 +08:00
BSP_Bt_Valve_Resp(kValveConncetStatus, valve_list.valve_data[0].valve_id, valve_list.valve_data[0].valve_mac, FALSE);//表示连接断开
2025-04-24 10:33:19 +08:00
}
2024-12-03 11:11:30 +08:00
}
break;
case GAP_LINK_PARAM_UPDATE_EVENT:
{
2024-12-12 15:56:21 +08:00
logDebug("Update %x - Int %x \r\n", pEvent->linkUpdate.connectionHandle, pEvent->linkUpdate.connInterval);
2024-12-03 11:11:30 +08:00
}
break;
default:
break;
}
}
/*********************************************************************
* @fn pairStateCB
*
* @brief Pairing state callback.
*
* @return none
*/
static void centralPairStateCB(uint16_t connHandle, uint8_t state, uint8_t status)
{
2024-12-11 20:03:50 +08:00
if (state == GAPBOND_PAIRING_STATE_STARTED)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("Connection %04x - Pairing started:%d\r\n", connHandle, status);
2024-12-03 11:11:30 +08:00
}
2024-12-11 20:03:50 +08:00
else if (state == GAPBOND_PAIRING_STATE_COMPLETE)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (status == SUCCESS)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("Connection %04x - Pairing success\r\n", connHandle);
2024-12-03 11:11:30 +08:00
}
else
{
2024-12-12 15:56:21 +08:00
logDebug("Connection %04x - Pairing fail\r\n", connHandle);
2024-12-03 11:11:30 +08:00
}
}
2024-12-11 20:03:50 +08:00
else if (state == GAPBOND_PAIRING_STATE_BONDED)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (status == SUCCESS)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("Connection %04x - Bonding success\r\n", connHandle);
2024-12-03 11:11:30 +08:00
}
}
2024-12-11 20:03:50 +08:00
else if (state == GAPBOND_PAIRING_STATE_BOND_SAVED)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (status == SUCCESS)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("Connection %04x - Bond save success\r\n", connHandle);
2024-12-03 11:11:30 +08:00
}
else
{
2024-12-12 15:56:21 +08:00
logDebug("Connection %04x - Bond save failed: %d\r\n", connHandle, status);
2024-12-03 11:11:30 +08:00
}
}
}
/*********************************************************************
* @fn centralPasscodeCB
*
* @brief Passcode callback.
*
* @return none
*/
static void centralPasscodeCB(uint8_t *deviceAddr, uint16_t connectionHandle,
uint8_t uiInputs, uint8_t uiOutputs)
{
uint32_t passcode;
// Create random passcode
passcode = tmos_rand();
passcode %= 1000000;
// Display passcode to user
2024-12-11 20:03:50 +08:00
if (uiOutputs != 0)
2024-12-03 11:11:30 +08:00
{
2024-12-12 15:56:21 +08:00
logDebug("Passcode:%06d\r\n", (int)passcode);
2024-12-03 11:11:30 +08:00
}
// Send passcode response
GAPBondMgr_PasscodeRsp(connectionHandle, SUCCESS, passcode);
}
/*********************************************************************
* @fn centralConnIistStartDiscovery_0
*
* @brief Start connection 0 service discovery.
*
* @return none
*/
static void centralConnIistStartDiscovery_0(void)
{
2024-12-16 19:22:58 +08:00
// uint8_t uuid[ATT_BT_UUID_SIZE] = {LO_UINT16(SIMPLEPROFILE_SERV_UUID),
// HI_UINT16(SIMPLEPROFILE_SERV_UUID)};
2024-12-03 11:11:30 +08:00
2024-12-16 19:22:58 +08:00
// // Initialize cached handles
// centralConnList[CONNECT0_ITEM].svcStartHdl = centralConnList[CONNECT0_ITEM].svcEndHdl = centralConnList[CONNECT0_ITEM].charHdl = 0;
2024-12-03 11:11:30 +08:00
2024-12-16 19:22:58 +08:00
// centralConnList[CONNECT0_ITEM].discState = BLE_DISC_STATE_SVC;
// GATT_DiscPrimaryServiceByUUID(centralConnList[CONNECT0_ITEM].connHandle,
// uuid,
// ATT_BT_UUID_SIZE,
// centralTaskId);
2024-12-03 11:11:30 +08:00
}
2024-12-07 17:09:36 +08:00
2024-12-03 11:11:30 +08:00
/*********************************************************************
* @fn centralGATTDiscoveryEvent
*
* @brief Process GATT discovery event
*
* @return none
*/
static void centralGATTDiscoveryEvent(uint8_t connItem, gattMsgEvent_t *pMsg)
{
attReadByTypeReq_t req;
2024-12-11 20:03:50 +08:00
// 连接0的枚举
if (connItem == CONNECT0_ITEM)
2024-12-03 11:11:30 +08:00
{
2024-12-16 15:34:47 +08:00
logDebug("发现句柄配置\r\n");
centralConnList[connItem].charHdl = 0X23;
centralConnList[connItem].charHd2 = 0X26;
centralConnList[connItem].charHd3 = 0x29;
centralConnList[connItem].charHd4 = 0x2C;
centralConnList[connItem].procedureInProgress = FALSE;
#if 0
2024-12-11 20:03:50 +08:00
if (centralConnList[connItem].discState == BLE_DISC_STATE_SVC)
2024-12-03 11:11:30 +08:00
{
// Service found, store handles
2024-12-11 20:03:50 +08:00
if (pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
pMsg->msg.findByTypeValueRsp.numInfo > 0)
2024-12-03 11:11:30 +08:00
{
centralConnList[connItem].svcStartHdl = ATT_ATTR_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0);
2024-12-11 20:03:50 +08:00
centralConnList[connItem].svcEndHdl = ATT_GRP_END_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0);
2024-12-03 11:11:30 +08:00
// Display Profile Service handle range
2024-12-12 15:56:21 +08:00
logDebug("Found Profile Service handle : %x ~ %x \r\n", centralConnList[connItem].svcStartHdl, centralConnList[connItem].svcEndHdl);
#if 0
2024-12-11 20:03:50 +08:00
for (uint16_t i = 0; i < pMsg->msg.readByGrpTypeRsp.numGrps; i++)
2024-12-07 17:09:36 +08:00
{
2024-12-11 20:03:50 +08:00
// uuid
printf("uuid = %x", BUILD_UINT16(
pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 4],
pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 5]));
// Primary Service UUID Length
printf("%02d bit x", pMsg->msg.readByGrpTypeRsp.len - 4);
// printf("att len = %d\n", pMsg->msg.readByGrpTypeRsp.len);
printf("start handle:%04x", BUILD_UINT16(
pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i],
pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 1]));
// Attribute End Group Handle
printf("end handle:%04x\r\n", BUILD_UINT16(
pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 2],
pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 3]));
2024-12-07 17:09:36 +08:00
}
2024-12-11 20:03:50 +08:00
#endif
2024-12-03 11:11:30 +08:00
}
// If procedure complete
2024-12-11 20:03:50 +08:00
if ((pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
pMsg->hdr.status == bleProcedureComplete) ||
(pMsg->method == ATT_ERROR_RSP))
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
// Discover characteristic
2024-12-16 15:34:47 +08:00
// centralConnList[connItem].discState = BLE_DISC_STATE_CHAR;
// uint8_t ret = GATT_DiscAllChars(centralConnList[connItem].connHandle, 0x01, 0xFFFF, centralTaskId);
// logDebug("GATT_DiscAllChars:%02x\r\n", ret);
2024-12-03 11:11:30 +08:00
}
}
2024-12-11 20:03:50 +08:00
else if (centralConnList[connItem].discState == BLE_DISC_STATE_CHAR)
2024-12-03 11:11:30 +08:00
{
2024-12-16 15:34:47 +08:00
2024-12-03 11:11:30 +08:00
// Characteristic found, store handle
2024-12-12 15:56:21 +08:00
if (pMsg->method == ATT_READ_BY_TYPE_RSP &&
pMsg->msg.readByTypeRsp.numPairs > 0)
2024-12-11 20:03:50 +08:00
{
2024-12-16 15:34:47 +08:00
#if 0
2024-12-11 20:03:50 +08:00
for (unsigned char i = 0; i < pMsg->msg.readByTypeRsp.numPairs; i++)
{
// characteristic properties
uint8_t char_properties = pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 2];
uint16_t char_value_handle = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 3],
2024-12-12 15:56:21 +08:00
pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 4]);
2024-12-11 20:03:50 +08:00
// characteristic uuid length
uint8_t char_uuid_length = pMsg->msg.readByGrpTypeRsp.len - 5;
// uuid
uint8_t *char_uuid = &(pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 5]);
2024-12-12 15:56:21 +08:00
logDebug("|-------------------------------------|");
logDebug("char_uuid\t:");
2024-12-11 20:03:50 +08:00
for (uint8_t i = 0; i < char_uuid_length; i++)
{
2024-12-12 15:56:21 +08:00
logDebug("%02x ", char_uuid[i]);
2024-12-11 20:03:50 +08:00
}
2024-12-12 15:56:21 +08:00
logDebug("char_value_handle\t:%04x\r\n", char_value_handle);
logDebug("char_uuid_length\t:%02d bit\r\n", char_uuid_length);
2024-12-11 20:03:50 +08:00
uint16_t char1uuid = SIMPLEPROFILE_CHAR1_UUID;
uint16_t char2uuid = SIMPLEPROFILE_CHAR2_UUID;
uint16_t char3uuid = SIMPLEPROFILE_CHAR3_UUID;
2024-12-12 15:56:21 +08:00
uint16_t char4uuid = SIMPLEPROFILE_CHAR4_UUID;
if (tmos_memcmp(char_uuid, &char1uuid, 2))
2024-12-11 20:03:50 +08:00
{
if (char_properties & (GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP))
{
2024-12-12 15:56:21 +08:00
centralConnList[connItem].charHdl = char_value_handle;
logDebug("char1 Write handle\t:%04x\r\n", char_value_handle);
2024-12-11 20:03:50 +08:00
centralConnList[connItem].procedureInProgress = FALSE;
2024-12-12 15:56:21 +08:00
// tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, START_READ_OR_WRITE_EVT, 1600);
2024-12-11 20:03:50 +08:00
}
}
else if (tmos_memcmp(char_uuid, &char2uuid, 2))
{
if (char_properties & (GATT_PROP_READ))
{
2024-12-12 15:56:21 +08:00
centralConnList[connItem].charHd2 = char_value_handle;
2024-12-11 20:03:50 +08:00
centralConnList[connItem].procedureInProgress = FALSE;
2024-12-12 15:56:21 +08:00
logDebug("char2 Write handle\t:%04x\r\n", char_value_handle);
// tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH2_READ_EVT, 2400);
2024-12-11 20:03:50 +08:00
}
}
2024-12-12 15:56:21 +08:00
else if (tmos_memcmp(char_uuid, &char3uuid, 2))
2024-12-11 20:03:50 +08:00
{
2024-12-12 15:56:21 +08:00
if (char_properties & (GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP))
{
centralConnList[connItem].charHd3 = char_value_handle;
logDebug("char3 Write handle\t:%04x\r\n", char_value_handle);
centralConnList[connItem].procedureInProgress = FALSE;
2024-12-13 19:07:00 +08:00
// tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH3_WRITE_EVT, 3200);
2024-12-12 15:56:21 +08:00
}
2024-12-11 20:03:50 +08:00
}
2024-12-12 15:56:21 +08:00
else if (tmos_memcmp(char_uuid, &char4uuid, 2))
2024-12-11 20:03:50 +08:00
{
2024-12-12 15:56:21 +08:00
if (char_properties & (GATT_PROP_NOTIFY))
{
centralConnList[connItem].charHd4 = char_value_handle + 1;
centralConnList[connItem].procedureInProgress = FALSE;
logDebug("char4 Write handle\t:%04x\r\n", char_value_handle);
tmos_start_task(centralConnList[CONNECT0_ITEM].taskID, CH4_NOTI_EVT, 1600);
}
2024-12-11 20:03:50 +08:00
}
}
2024-12-16 15:34:47 +08:00
#endif
2024-12-11 20:03:50 +08:00
}
2024-12-16 15:34:47 +08:00
}
#endif
2024-12-11 20:03:50 +08:00
}
2024-12-16 15:34:47 +08:00
// 连接1的枚举
// else if (connItem == CONNECT1_ITEM)
// {
// }
// 连接2的枚举
// else if(connItem == CONNECT2_ITEM)
// {
// }
2024-12-03 11:11:30 +08:00
}
/*********************************************************************
* @fn centralAddDeviceInfo
*
* @brief Add a device to the device discovery result list
*
* @return none
*/
static void centralAddDeviceInfo(uint8_t *pAddr, uint8_t addrType)
{
uint8_t i;
// If result count not at max
2024-12-11 20:03:50 +08:00
if (centralScanRes < DEFAULT_MAX_SCAN_RES)
2024-12-03 11:11:30 +08:00
{
// Check if device is already in scan results
2024-12-11 20:03:50 +08:00
for (i = 0; i < centralScanRes; i++)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (tmos_memcmp(pAddr, centralDevList[i].addr, B_ADDR_LEN))
2024-12-03 11:11:30 +08:00
{
return;
}
}
// Add addr to scan result list
tmos_memcpy(centralDevList[centralScanRes].addr, pAddr, B_ADDR_LEN);
centralDevList[centralScanRes].addrType = addrType;
// Increment scan result count
centralScanRes++;
// Display device addr
2024-12-12 15:56:21 +08:00
logDebug("Device %d - Addr %x %x %x %x %x %x \r\n", centralScanRes,
centralDevList[centralScanRes - 1].addr[0],
centralDevList[centralScanRes - 1].addr[1],
centralDevList[centralScanRes - 1].addr[2],
centralDevList[centralScanRes - 1].addr[3],
centralDevList[centralScanRes - 1].addr[4],
centralDevList[centralScanRes - 1].addr[5]);
2024-12-03 11:11:30 +08:00
}
}
/*********************************************************************
* @fn centralAddrCmp
*
* @brief none
*
* @return none
*/
static uint8_t centralAddrCmp(peerAddrDefItem_t *PeerAddrDef, uint8_t *addr)
{
uint8_t i;
2024-12-11 20:03:50 +08:00
for (i = 0; i < CENTRAL_MAX_CONNECTION; i++)
2024-12-03 11:11:30 +08:00
{
2024-12-11 20:03:50 +08:00
if (tmos_memcmp(PeerAddrDef[i].peerAddr, addr, B_ADDR_LEN))
2024-12-03 11:11:30 +08:00
break;
}
2024-12-11 20:03:50 +08:00
if (i == CENTRAL_MAX_CONNECTION)
2024-12-03 11:11:30 +08:00
{
return FALSE;
}
else
{
return TRUE;
}
}
/************************ endfile @ central **************************/
2024-12-12 15:56:21 +08:00
/*用户自定义文件*/
int BSP_Master_Send(centralConnItem_t *centralConnList, uint8_t id, uint8_t *Data, uint8_t Len)
2024-12-12 15:56:21 +08:00
{
int ret = 0;
2024-12-12 15:56:21 +08:00
attWriteReq_t req;
uint8_t res;
if (centralConnList[id].state == BLE_STATE_CONNECTED) // 检查连接状态是否为连接状态
{
if (centralConnList[id].procedureInProgress == FALSE)
2024-12-12 15:56:21 +08:00
{
req.cmd = FALSE;
req.sig = FALSE;
req.handle = centralConnList[id].charHd3;
req.len = Len;
req.pValue = GATT_bm_alloc(centralConnList[id].connHandle, ATT_WRITE_REQ, req.len, NULL, 0);
if (req.pValue != NULL)
2024-12-12 15:56:21 +08:00
{
tmos_memcpy(req.pValue, Data, Len);
res = GATT_WriteCharValue(centralConnList[id].connHandle, &req, centralTaskId);
if (res == SUCCESS)
{
logDebug("\n Master Write ok");
2024-12-16 15:34:47 +08:00
centralConnList[id].procedureInProgress = TRUE;
ret = 0;
}
else
{
2024-12-16 15:34:47 +08:00
logError("\n Master Write faild= %x", res);
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
ret = 1;
}
2024-12-12 15:56:21 +08:00
}
else
{
2024-12-16 15:34:47 +08:00
logError("\n alloc Faild");
ret = 2;
2024-12-12 15:56:21 +08:00
}
}
2024-12-13 19:07:00 +08:00
else
{
2024-12-16 15:34:47 +08:00
logError("procedureInProgress");
2024-12-13 19:07:00 +08:00
ret = 3;
}
}
else
{
2024-12-16 20:47:16 +08:00
logError("没有连接成功");
2024-12-13 19:07:00 +08:00
ret = 4;
2024-12-12 15:56:21 +08:00
}
return ret;
2024-12-12 15:56:21 +08:00
}
int BSP_Master_Receive_Data(uint8_t task_id, uint8_t *data, uint16_t len)
2024-12-12 15:56:21 +08:00
{
int ret = 0;
uint16_t index = 0;
uint8_t data_buf[64] = {0};
// 解析接收到的数据帧先寻找AA开头然后再找AA下一个字节其代表命令然后找到代表长度的值的长度的下一位其为校验码校验码后为结束码0x55
2024-12-12 15:56:21 +08:00
// 如果数据正确,则提取数据,不正确,则不处理
if (len < 4)
{ // 至少需要 4 个字节:起始码、长度、校验码、结束码
logError("BT 数据帧长度不足");
logHexDumpAll(data, len);
2024-12-13 19:07:00 +08:00
return -1;
2024-12-12 15:56:21 +08:00
}
while (index < len && data[index] != 0xAA) // 寻找起始码 0xAA
{
index++;
}
if (index >= len - 3) // 不够空间容纳长度、校验码和结束码
{
logError("BT 数据帧起始码错误");
logHexDumpAll(data, len);
2024-12-13 19:07:00 +08:00
return -2;
2024-12-12 15:56:21 +08:00
}
uint16_t datalength = data[index + 2]; // 读取数据长度
if (index + 3 + datalength + 1 >= len) // 检查数据长度是否合理 数据长度 + 校验码 + 结束码
2024-12-12 15:56:21 +08:00
{
logError("BT 数据帧长度错误");
logHexDumpAll(data, len);
2024-12-13 19:07:00 +08:00
return -3; //
2024-12-12 15:56:21 +08:00
}
uint8_t rx_sum = data[index + 3 + datalength]; // 读取校验码
uint8_t calculated_sum = _CheckSum(&data[index], datalength + 3); // 计算校验码
2024-12-12 15:56:21 +08:00
if (rx_sum != calculated_sum)
{
logError("BT 数据帧校验码错误 rx_sum = %02X, calculated_sum = %02X", rx_sum, calculated_sum);
logHexDumpAll(data, len);
2024-12-13 19:07:00 +08:00
return -4;
2024-12-12 15:56:21 +08:00
}
if (data[index + 3 + datalength + 1] != 0x55) // 检查结束码
2024-12-12 15:56:21 +08:00
{
logError("BT 数据帧结束码错误");
logHexDumpAll(data, len);
2024-12-13 19:07:00 +08:00
return -5;
2024-12-12 15:56:21 +08:00
}
// //有效数据长度
size_t data_len = (datalength + 5);
2024-12-16 15:34:47 +08:00
ret = data_len;
2025-05-23 18:02:43 +08:00
if (data[index + 1] == 0x02) // 0x02 代表了其是控制阀门响应
{
2024-12-16 15:34:47 +08:00
// logDebug("BT 控制阀门响应包");
if (data[index + datalength + 2] == 1) // 响应数据位
2024-12-13 19:07:00 +08:00
{
2025-05-23 18:02:43 +08:00
logInfo("BT 控制阀门成功");
2024-12-13 19:07:00 +08:00
}
else
{
logError("BT 控制阀门失败");
}
}
2024-12-12 19:38:49 +08:00
else if (data[index + 1] == kValveCmdRep) // 这个是代表了其是心跳数据包
{
2024-12-16 20:47:16 +08:00
logDebug("蓝牙从机心跳数据包:");
2024-12-16 15:34:47 +08:00
logHexDumpAll(data, data_len);
}
2024-12-16 15:34:47 +08:00
// 到这一步说明数据没问题,将接收到的数据通过中心任务发送出去
2024-12-13 19:07:00 +08:00
uint8_t *p_data;
p_data = tmos_msg_allocate(data_len);
if (p_data)
{
tmos_memcpy(p_data, data, data_len);
tmos_msg_send(BtRxTaskId, p_data);
tmos_start_task(BtRxTaskId, SYS_EVENT_MSG, 0);
}
2024-12-16 15:34:47 +08:00
2024-12-12 15:56:21 +08:00
return ret;
}
2024-12-13 19:07:00 +08:00
void Master_Connect(void)
{
2024-12-16 15:34:47 +08:00
uint8_t def_mac[6] = {0xFF};
uint8_t cnt = Flash_Get_Valve_Num();
2024-12-13 19:07:00 +08:00
logDebug("cnt:%d\r\n", cnt);
for (uint8_t i = 0; i < cnt; i++)
{
2024-12-16 15:34:47 +08:00
if (valve_list.valve_data[i].valve_id != 0)
2024-12-13 19:07:00 +08:00
{
2024-12-16 15:34:47 +08:00
logDebug("valve_id:%d\r\n", valve_list.valve_data[i].valve_id);
logHexDumpAll(valve_list.valve_data[i].valve_mac, 6);
2024-12-13 19:07:00 +08:00
GAPRole_CentralEstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE,
2024-12-16 15:34:47 +08:00
DEFAULT_LINK_WHITE_LIST,
ADDRTYPE_PUBLIC,
valve_list.valve_data[i].valve_mac);
2024-12-13 19:07:00 +08:00
tmos_start_task(centralTaskId, ESTABLISH_LINK_TIMEOUT_EVT, ESTABLISH_LINK_TIMEOUT);
logDebug("Connecting...\r\n");
}
}
}
2024-12-16 15:34:47 +08:00
void Master_DisConnect(void)
{
2024-12-16 19:22:58 +08:00
GAPRole_TerminateLink(centralConnList[0].connHandle);
centralConnList[0].state = BLE_STATE_IDLE;
2024-12-16 15:34:47 +08:00
logDebug("主动断开连接\r\n");
}
uint16_t Master_ProcessEvent(uint8_t task_id, uint16_t events)
{
int8_t ret = 0;
uint8_t *pMsg;
uint8_t master_buf[64] = {0};
if (events & MASTER_START_EVT)
{
if (centralConnList[0].state != BLE_STATE_CONNECTED)
{
2024-12-16 20:47:16 +08:00
if (Flash_Get_Valve_Num())//如果有设备,发起连接
2024-12-16 15:34:47 +08:00
{
2024-12-17 10:54:01 +08:00
tmos_start_task(task_id, MASTER_CONNECT_EVT, 1600);
2024-12-16 20:47:16 +08:00
}
2024-12-16 15:34:47 +08:00
}
2024-12-17 10:54:01 +08:00
tmos_start_task(task_id, MASTER_DETECT_EVT, 3200);
2024-12-16 15:34:47 +08:00
return (events ^ MASTER_START_EVT);
}
else if(events & MASTER_DETECT_EVT)
{
if (centralConnList[0].state != BLE_STATE_CONNECTED)
{
2024-12-16 20:47:16 +08:00
if (Flash_Get_Valve_Num())
{
logDebug("有设备,尝试连接\r\n");
2025-05-23 18:02:43 +08:00
bt_conncet_cnt++;
if (bt_conncet_cnt > 60)//如果多次重试都不行,直接重启设备
{
bt_conncet_cnt = 0;
PFIC_SystemReset();
}
2024-12-17 10:54:01 +08:00
tmos_start_task(task_id, MASTER_CONNECT_EVT, 1600);
2024-12-16 20:47:16 +08:00
}
2024-12-16 15:34:47 +08:00
}
2024-12-17 10:54:01 +08:00
tmos_start_task(task_id, MASTER_DETECT_EVT, 3200);
2024-12-16 15:34:47 +08:00
return (events ^ MASTER_DETECT_EVT);
}
else if (events & MASTER_CONNECT_EVT)
{
if (centralConnList[0].state != BLE_STATE_CONNECTED)
{
Master_Connect();
}
return (events ^ MASTER_CONNECT_EVT);
}
else if (events & MASTER_DISCONN_EVT)
{
Master_DisConnect();
return (events ^ MASTER_DISCONN_EVT);
}
return 0;
}