This commit is contained in:
stark1898y 2024-12-15 19:04:05 +08:00
parent 218f048b23
commit 4271e8c6ff
5 changed files with 168 additions and 126 deletions

View File

@ -13,6 +13,7 @@
"shell_port.h": "c", "shell_port.h": "c",
"atomic": "cpp", "atomic": "cpp",
"cstddef": "cpp", "cstddef": "cpp",
"type_traits": "cpp" "type_traits": "cpp",
"stdarg.h": "c"
} }
} }

View File

@ -33,7 +33,6 @@
#undef LOG_TAG #undef LOG_TAG
#define LOG_TAG "peripheral" #define LOG_TAG "peripheral"
/********************************************************************* /*********************************************************************
* MACROS * MACROS
*/ */
@ -43,40 +42,40 @@
*/ */
// How often to perform periodic event // How often to perform periodic event
#define SBP_PERIODIC_EVT_PERIOD (160 * 10 * 5) // (160 = 100ms) #define SBP_PERIODIC_EVT_PERIOD (160 * 10 * 5) // (160 = 100ms)
// How often to perform read rssi event // How often to perform read rssi event
#define SBP_READ_RSSI_EVT_PERIOD (1600 * 3) // (160 = 100ms) #define SBP_READ_RSSI_EVT_PERIOD (1600 * 3) // (160 = 100ms)
// Parameter update delay // Parameter update delay
#define SBP_PARAM_UPDATE_DELAY (1600 * 4) #define SBP_PARAM_UPDATE_DELAY (1600 * 4)
// PHY update delay // PHY update delay
#define SBP_PHY_UPDATE_DELAY (1600 * 2) #define SBP_PHY_UPDATE_DELAY (1600 * 2)
// What is the advertising interval when device is discoverable (units of 625us, 80=50ms) // What is the advertising interval when device is discoverable (units of 625us, 80=50ms)
#define DEFAULT_ADVERTISING_INTERVAL (160 * 1) #define DEFAULT_ADVERTISING_INTERVAL (160 * 1)
// Limited discoverable mode advertises for 30.72s, and then stops // Limited discoverable mode advertises for 30.72s, and then stops
// General discoverable mode advertises indefinitely // General discoverable mode advertises indefinitely
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL #define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
// Minimum connection interval (units of 1.25ms, 6=7.5ms) // Minimum connection interval (units of 1.25ms, 6=7.5ms)
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL (80 * 5) // (80 = 100ms) #define DEFAULT_DESIRED_MIN_CONN_INTERVAL (80 * 9) // (80 = 100ms)
// Maximum connection interval (units of 1.25ms, 100=125ms) // Maximum connection interval (units of 1.25ms, 100=125ms)
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL (80 * 10) #define DEFAULT_DESIRED_MAX_CONN_INTERVAL (80 * 10)
// Slave latency to use parameter update // Slave latency to use parameter update
#define DEFAULT_DESIRED_SLAVE_LATENCY 3 #define DEFAULT_DESIRED_SLAVE_LATENCY 3
// Supervision timeout value (units of 10ms, 100=1s) // Supervision timeout value (units of 10ms, 100=1s)
#define DEFAULT_DESIRED_CONN_TIMEOUT (100 * 5) #define DEFAULT_DESIRED_CONN_TIMEOUT (100 * 5)
// Company Identifier: WCH // Company Identifier: WCH
#define WCH_COMPANY_ID 0x07D7 #define WCH_COMPANY_ID 0x07D7
#define MAC_NAME "TYQ-93:B4:8F:10:53:5C" #define MAC_NAME "TYQ-93:B4:8F:10:53:5C"
// TYQ-93:B4:8F:10:53:5C // TYQ-93:B4:8F:10:53:5C
#define MAC_NAME_LEN 22 #define MAC_NAME_LEN 22
@ -135,8 +134,7 @@ static uint8_t advertData[] = {
0x03, // length of this data 0x03, // length of this data
GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
LO_UINT16(SIMPLEPROFILE_SERV_UUID), LO_UINT16(SIMPLEPROFILE_SERV_UUID),
HI_UINT16(SIMPLEPROFILE_SERV_UUID) HI_UINT16(SIMPLEPROFILE_SERV_UUID)};
};
// GAP GATT Attributes // GAP GATT Attributes
static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "BLE_TYQ"; static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "BLE_TYQ";
@ -166,8 +164,7 @@ static void peripheralChar4Notify(uint8_t *pValue, uint16_t len);
static gapRolesCBs_t Peripheral_PeripheralCBs = { static gapRolesCBs_t Peripheral_PeripheralCBs = {
peripheralStateNotificationCB, // Profile State Change Callbacks peripheralStateNotificationCB, // Profile State Change Callbacks
peripheralRssiCB, // When a valid RSSI is read from controller (not used by application) peripheralRssiCB, // When a valid RSSI is read from controller (not used by application)
peripheralParamUpdateCB peripheralParamUpdateCB};
};
// Broadcast Callbacks // Broadcast Callbacks
static gapRolesBroadcasterCBs_t Broadcaster_BroadcasterCBs = { static gapRolesBroadcasterCBs_t Broadcaster_BroadcasterCBs = {
@ -178,7 +175,7 @@ static gapRolesBroadcasterCBs_t Broadcaster_BroadcasterCBs = {
// GAP Bond Manager Callbacks // GAP Bond Manager Callbacks
static gapBondCBs_t Peripheral_BondMgrCBs = { static gapBondCBs_t Peripheral_BondMgrCBs = {
NULL, // Passcode callback (not used by application) NULL, // Passcode callback (not used by application)
NULL, // Pairing / Bonding state Callback (not used by application) NULL, // Pairing / Bonding state Callback (not used by application)
NULL // oob callback NULL // oob callback
}; };
@ -208,13 +205,15 @@ void Peripheral_Init()
{ {
Peripheral_TaskID = TMOS_ProcessEventRegister(Peripheral_ProcessEvent); Peripheral_TaskID = TMOS_ProcessEventRegister(Peripheral_ProcessEvent);
GATT_InitClient();
// Setup the GAP Peripheral Role Profile // Setup the GAP Peripheral Role Profile
{ {
//开启广播使能 // 开启广播使能
uint8_t initial_advertising_enable = TRUE; uint8_t initial_advertising_enable = TRUE;
//最小连接间隔 // 最小连接间隔
uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
//最大连接间隔 // 最大连接间隔
uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
// Set the GAP Role Parameters // Set the GAP Role Parameters
@ -246,10 +245,10 @@ void Peripheral_Init()
// Setup the GAP Bond Manager // Setup the GAP Bond Manager
{ {
uint32_t passkey = 0; // passkey "000000" uint32_t passkey = 0; // passkey "000000"
uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
uint8_t mitm = TRUE; uint8_t mitm = TRUE;
uint8_t bonding = TRUE; uint8_t bonding = TRUE;
uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
GAPBondMgr_SetParameter(GAPBOND_PERI_DEFAULT_PASSCODE, sizeof(uint32_t), &passkey); GAPBondMgr_SetParameter(GAPBOND_PERI_DEFAULT_PASSCODE, sizeof(uint32_t), &passkey);
GAPBondMgr_SetParameter(GAPBOND_PERI_PAIRING_MODE, sizeof(uint8_t), &pairMode); GAPBondMgr_SetParameter(GAPBOND_PERI_PAIRING_MODE, sizeof(uint8_t), &pairMode);
GAPBondMgr_SetParameter(GAPBOND_PERI_MITM_PROTECTION, sizeof(uint8_t), &mitm); GAPBondMgr_SetParameter(GAPBOND_PERI_MITM_PROTECTION, sizeof(uint8_t), &mitm);
@ -305,10 +304,10 @@ void Peripheral_Init()
*/ */
static void peripheralInitConnItem(peripheralConnItem_t *peripheralConnList) static void peripheralInitConnItem(peripheralConnItem_t *peripheralConnList)
{ {
peripheralConnList->connHandle = GAP_CONNHANDLE_INIT; peripheralConnList->connHandle = GAP_CONNHANDLE_INIT;
peripheralConnList->connInterval = 0; peripheralConnList->connInterval = 0;
peripheralConnList->connSlaveLatency = 0; peripheralConnList->connSlaveLatency = 0;
peripheralConnList->connTimeout = 0; peripheralConnList->connTimeout = 0;
} }
/********************************************************************* /*********************************************************************
@ -328,11 +327,11 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
{ {
// VOID task_id; // TMOS required parameter that isn't used in this function // VOID task_id; // TMOS required parameter that isn't used in this function
if(events & SYS_EVENT_MSG) if (events & SYS_EVENT_MSG)
{ {
uint8_t *pMsg; uint8_t *pMsg;
if((pMsg = tmos_msg_receive(Peripheral_TaskID)) != NULL) if ((pMsg = tmos_msg_receive(Peripheral_TaskID)) != NULL)
{ {
Peripheral_ProcessTMOSMsg((tmos_event_hdr_t *)pMsg); Peripheral_ProcessTMOSMsg((tmos_event_hdr_t *)pMsg);
// Release the TMOS message // Release the TMOS message
@ -342,17 +341,22 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
return (events ^ SYS_EVENT_MSG); return (events ^ SYS_EVENT_MSG);
} }
if(events & SBP_START_DEVICE_EVT) if (events & SBP_START_DEVICE_EVT)
{ {
// Start the Device // Start the Device
GAPRole_PeripheralStartDevice(Peripheral_TaskID, &Peripheral_BondMgrCBs, &Peripheral_PeripheralCBs); GAPRole_PeripheralStartDevice(Peripheral_TaskID, &Peripheral_BondMgrCBs, &Peripheral_PeripheralCBs);
// Update MTU
// attExchangeMTUReq_t req = {.clientRxMTU = BLE_BUFF_MAX_LEN - 4,};
// GATT_ExchangeMTU(peripheralConnList.connHandle, &req, Peripheral_TaskID);
// Peripheral_SetMacName();
return (events ^ SBP_START_DEVICE_EVT); return (events ^ SBP_START_DEVICE_EVT);
} }
if(events & SBP_PERIODIC_EVT) if (events & SBP_PERIODIC_EVT)
{ {
// Restart timer // Restart timer
if(SBP_PERIODIC_EVT_PERIOD) if (SBP_PERIODIC_EVT_PERIOD)
{ {
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD); tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
} }
@ -361,7 +365,7 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
return (events ^ SBP_PERIODIC_EVT); return (events ^ SBP_PERIODIC_EVT);
} }
if(events & SBP_PARAM_UPDATE_EVT) if (events & SBP_PARAM_UPDATE_EVT)
{ {
// Send connect param update request // Send connect param update request
GAPRole_PeripheralConnParamUpdateReq(peripheralConnList.connHandle, GAPRole_PeripheralConnParamUpdateReq(peripheralConnList.connHandle,
@ -374,16 +378,16 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
return (events ^ SBP_PARAM_UPDATE_EVT); return (events ^ SBP_PARAM_UPDATE_EVT);
} }
if(events & SBP_PHY_UPDATE_EVT) if (events & SBP_PHY_UPDATE_EVT)
{ {
// start phy update // start phy update
logDebug("PHY Update %x...", GAPRole_UpdatePHY(peripheralConnList.connHandle, 0, logDebug("PHY Update %x...", GAPRole_UpdatePHY(peripheralConnList.connHandle, 0,
GAP_PHY_BIT_LE_2M, GAP_PHY_BIT_LE_2M, 0)); GAP_PHY_BIT_LE_2M, GAP_PHY_BIT_LE_2M, 0));
return (events ^ SBP_PHY_UPDATE_EVT); return (events ^ SBP_PHY_UPDATE_EVT);
} }
if(events & SBP_READ_RSSI_EVT) if (events & SBP_READ_RSSI_EVT)
{ {
GAPRole_ReadRssiCmd(peripheralConnList.connHandle); GAPRole_ReadRssiCmd(peripheralConnList.connHandle);
tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD); tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
@ -405,13 +409,13 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
*/ */
static void Peripheral_ProcessGAPMsg(gapRoleEvent_t *pEvent) static void Peripheral_ProcessGAPMsg(gapRoleEvent_t *pEvent)
{ {
switch(pEvent->gap.opcode) switch (pEvent->gap.opcode)
{ {
case GAP_SCAN_REQUEST_EVENT: case GAP_SCAN_REQUEST_EVENT:
{ {
logDebug("Receive scan req from %x %x %x %x %x %x ..", pEvent->scanReqEvt.scannerAddr[0], logDebug("Receive scan req from %x %x %x %x %x %x ..", pEvent->scanReqEvt.scannerAddr[0],
pEvent->scanReqEvt.scannerAddr[1], pEvent->scanReqEvt.scannerAddr[2], pEvent->scanReqEvt.scannerAddr[3], pEvent->scanReqEvt.scannerAddr[1], pEvent->scanReqEvt.scannerAddr[2], pEvent->scanReqEvt.scannerAddr[3],
pEvent->scanReqEvt.scannerAddr[4], pEvent->scanReqEvt.scannerAddr[5]); pEvent->scanReqEvt.scannerAddr[4], pEvent->scanReqEvt.scannerAddr[5]);
break; break;
} }
@ -437,7 +441,7 @@ static void Peripheral_ProcessGAPMsg(gapRoleEvent_t *pEvent)
*/ */
static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg) static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
{ {
switch(pMsg->event) switch (pMsg->event)
{ {
case GAP_MSG_EVENT: case GAP_MSG_EVENT:
{ {
@ -450,10 +454,10 @@ static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
gattMsgEvent_t *pMsgEvent; gattMsgEvent_t *pMsgEvent;
pMsgEvent = (gattMsgEvent_t *)pMsg; pMsgEvent = (gattMsgEvent_t *)pMsg;
if(pMsgEvent->method == ATT_MTU_UPDATED_EVENT) if (pMsgEvent->method == ATT_MTU_UPDATED_EVENT)
{ {
peripheralMTU = pMsgEvent->msg.exchangeMTUReq.clientRxMTU; peripheralMTU = pMsgEvent->msg.exchangeMTUReq.clientRxMTU;
logDebug("mtu exchange: %d", pMsgEvent->msg.exchangeMTUReq.clientRxMTU); logDebug("****mtu exchange: %d****", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
} }
break; break;
} }
@ -477,18 +481,19 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
gapEstLinkReqEvent_t *event = (gapEstLinkReqEvent_t *)pEvent; gapEstLinkReqEvent_t *event = (gapEstLinkReqEvent_t *)pEvent;
// See if already connected // See if already connected
if(peripheralConnList.connHandle != GAP_CONNHANDLE_INIT) if (peripheralConnList.connHandle != GAP_CONNHANDLE_INIT)
{ {
GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle); GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle);
logDebug("Connection max..."); logDebug("Connection max...");
} }
else else
{ {
peripheralConnList.connHandle = event->connectionHandle; peripheralConnList.connHandle = event->connectionHandle;
peripheralConnList.connInterval = event->connInterval; peripheralConnList.connInterval = event->connInterval;
peripheralConnList.connSlaveLatency = event->connLatency; peripheralConnList.connSlaveLatency = event->connLatency;
peripheralConnList.connTimeout = event->connTimeout; peripheralConnList.connTimeout = event->connTimeout;
peripheralMTU = ATT_MTU_SIZE; peripheralMTU = ATT_MTU_SIZE;
// peripheralMTU = BLE_BUFF_MAX_LEN;
// Set timer for periodic event // Set timer for periodic event
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD); tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
@ -498,7 +503,7 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
// Start read rssi // Start read rssi
tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD); tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
logDebug("Conn %x - Int %x ", event->connectionHandle, event->connInterval); logDebug("Conn %x - Int %x= %.1fms", event->connectionHandle, event->connInterval, event->connInterval * 1.25);
} }
} }
@ -515,12 +520,12 @@ static void Peripheral_LinkTerminated(gapRoleEvent_t *pEvent)
{ {
gapTerminateLinkEvent_t *event = (gapTerminateLinkEvent_t *)pEvent; gapTerminateLinkEvent_t *event = (gapTerminateLinkEvent_t *)pEvent;
if(event->connectionHandle == peripheralConnList.connHandle) if (event->connectionHandle == peripheralConnList.connHandle)
{ {
peripheralConnList.connHandle = GAP_CONNHANDLE_INIT; peripheralConnList.connHandle = GAP_CONNHANDLE_INIT;
peripheralConnList.connInterval = 0; peripheralConnList.connInterval = 0;
peripheralConnList.connSlaveLatency = 0; peripheralConnList.connSlaveLatency = 0;
peripheralConnList.connTimeout = 0; peripheralConnList.connTimeout = 0;
tmos_stop_task(Peripheral_TaskID, SBP_PERIODIC_EVT); tmos_stop_task(Peripheral_TaskID, SBP_PERIODIC_EVT);
tmos_stop_task(Peripheral_TaskID, SBP_READ_RSSI_EVT); tmos_stop_task(Peripheral_TaskID, SBP_READ_RSSI_EVT);
@ -570,14 +575,14 @@ static void peripheralRssiCB(uint16_t connHandle, int8_t rssi)
static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval, static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
uint16_t connSlaveLatency, uint16_t connTimeout) uint16_t connSlaveLatency, uint16_t connTimeout)
{ {
if(connHandle == peripheralConnList.connHandle) if (connHandle == peripheralConnList.connHandle)
{ {
peripheralConnList.connInterval = connInterval; peripheralConnList.connInterval = connInterval;
peripheralConnList.connSlaveLatency = connSlaveLatency; peripheralConnList.connSlaveLatency = connSlaveLatency;
peripheralConnList.connTimeout = connTimeout; peripheralConnList.connTimeout = connTimeout;
logDebug("peripheralParamUpdateCB (connHandle)%x - (connInterval) %x- (connSlaveLatency) %x - (connTimeout) %x\n" logDebug("ParamUpdateCB (Handle)%x - (connInt 0x%x=%.1fms) - (connSlaveLatency) %x - (connTimeout 0x%x=%dms) "
, connHandle, connInterval, connSlaveLatency, connTimeout); , connHandle, connInterval, connInterval * 1.25, connSlaveLatency, connTimeout * 10, connTimeout * 10);
} }
else else
{ {
@ -596,27 +601,27 @@ static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
*/ */
static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent) static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent)
{ {
switch(newState & GAPROLE_STATE_ADV_MASK) switch (newState & GAPROLE_STATE_ADV_MASK)
{ {
case GAPROLE_STARTED: case GAPROLE_STARTED:
logDebug("Initialized.."); logDebug("Initialized..");
break; break;
case GAPROLE_ADVERTISING: case GAPROLE_ADVERTISING:
if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT) if (pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
{ {
Peripheral_LinkTerminated(pEvent); Peripheral_LinkTerminated(pEvent);
logDebug("Disconnected.. Reason:%x", pEvent->linkTerminate.reason); logDebug("Disconnected.. Reason:%x", pEvent->linkTerminate.reason);
logDebug("Advertising.."); logDebug("Advertising..");
} }
else if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT) else if (pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
{ {
logDebug("Advertising.."); logDebug("Advertising..");
} }
break; break;
case GAPROLE_CONNECTED: case GAPROLE_CONNECTED:
if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT) if (pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
{ {
Peripheral_LinkEstablished(pEvent); Peripheral_LinkEstablished(pEvent);
logDebug("Connected.."); logDebug("Connected..");
@ -624,25 +629,25 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven
break; break;
case GAPROLE_CONNECTED_ADV: case GAPROLE_CONNECTED_ADV:
if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT) if (pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
{ {
logDebug("Connected Advertising.."); logDebug("Connected Advertising..");
} }
break; break;
case GAPROLE_WAITING: case GAPROLE_WAITING:
if(pEvent->gap.opcode == GAP_END_DISCOVERABLE_DONE_EVENT) if (pEvent->gap.opcode == GAP_END_DISCOVERABLE_DONE_EVENT)
{ {
logDebug("Waiting for advertising.."); logDebug("Waiting for advertising..");
} }
else if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT) else if (pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
{ {
Peripheral_LinkTerminated(pEvent); Peripheral_LinkTerminated(pEvent);
logDebug("Disconnected.. Reason:%x", pEvent->linkTerminate.reason); logDebug("Disconnected.. Reason:%x", pEvent->linkTerminate.reason);
} }
else if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT) else if (pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
{ {
if(pEvent->gap.hdr.status != SUCCESS) if (pEvent->gap.hdr.status != SUCCESS)
{ {
logDebug("Waiting for advertising.."); logDebug("Waiting for advertising..");
} }
@ -689,14 +694,14 @@ static void performPeriodicTask(void)
// peripheralChar4Notify(notiData, 5); // peripheralChar4Notify(notiData, 5);
// counter++; // counter++;
#if 1 #if 1
// VBAT // VBAT
uint32_t CountBat = 0; uint32_t CountBat = 0;
uint16_t adcBuff[40]; uint16_t adcBuff[40];
uint8_t i = 0; uint8_t i = 0;
// uint32_t countadc = 0; // uint32_t countadc = 0;
uint16_t min_number = 0; uint16_t min_number = 0;
uint16_t max_number = 0; uint16_t max_number = 0;
ADC_InterBATSampInit(); ADC_InterBATSampInit();
for (i = 0; i < 20; i++) for (i = 0; i < 20; i++)
@ -736,14 +741,14 @@ static void performPeriodicTask(void)
GXHTC3C_Sleep(); GXHTC3C_Sleep();
TsRawFrameData RawData; TsRawFrameData RawData;
// static uint8_t humi; // static uint8_t humi;
// humi++; // humi++;
BSP_VALVE_Generate_Data(&RawData, 0, CountBat/100, temp, humi); BSP_VALVE_Generate_Data(&RawData, 0, CountBat / 100, temp, humi);
// if (humi > 99) // if (humi > 99)
// { // {
// humi = 0; // humi = 0;
// } // }
peripheralChar4Notify(&RawData.buf[0], RawData.len); // peripheralChar4Notify(&RawData.buf[0], RawData.len);
} }
/********************************************************************* /*********************************************************************
@ -759,17 +764,17 @@ static void performPeriodicTask(void)
static void peripheralChar4Notify(uint8_t *pValue, uint16_t len) static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
{ {
attHandleValueNoti_t noti; attHandleValueNoti_t noti;
if(len > (peripheralMTU - 3)) if (len > (peripheralMTU - 3))
{ {
logDebug("Too large noti"); logDebug("Too large noti");
return; return;
} }
noti.len = len; noti.len = len;
noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0); noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
if(noti.pValue) if (noti.pValue)
{ {
tmos_memcpy(noti.pValue, pValue, noti.len); tmos_memcpy(noti.pValue, pValue, noti.len);
if(simpleProfile_Notify(peripheralConnList.connHandle, &noti) != SUCCESS) if (simpleProfile_Notify(peripheralConnList.connHandle, &noti) != SUCCESS)
{ {
GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI); GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI);
} }
@ -791,7 +796,7 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
{ {
// tmos_event_hdr_t *test_message; // tmos_event_hdr_t *test_message;
TsRawFrameData RawData; TsRawFrameData RawData;
switch(paramID) switch (paramID)
{ {
case SIMPLEPROFILE_CHAR1: case SIMPLEPROFILE_CHAR1:
{ {
@ -818,11 +823,11 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
tmos_memcpy(newValue, pValue, len); tmos_memcpy(newValue, pValue, len);
logDebug("CHAR3 Start"); logDebug("CHAR3 Start");
logHexDumpAll(newValue, len); logHexDumpAll(newValue, len);
// for (uint8_t i = 0; i < len; i++) // for (uint8_t i = 0; i < len; i++)
// { // {
// logDebug("%02x ", newValue[i]); // logDebug("%02x ", newValue[i]);
// } // }
#if 1 #if 1
TsFrameData *HostFrameData = BSP_VAVLE_GetFrameData(newValue, len); TsFrameData *HostFrameData = BSP_VAVLE_GetFrameData(newValue, len);
if (HostFrameData != NULL) if (HostFrameData != NULL)
{ {
@ -831,37 +836,37 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
// HR_ProcessData(HostFrameData); // HR_ProcessData(HostFrameData);
switch (HostFrameData->cmd) switch (HostFrameData->cmd)
{ {
case kCmdCfg: case kCmdCfg:
// uint8_t data_buf[64] = {0}; // uint8_t data_buf[64] = {0};
// tmos_memset(data_buf, 0, sizeof(data_buf)); // tmos_memset(data_buf, 0, sizeof(data_buf));
// 处理数据帧 // 处理数据帧
logDebug("kCmdCfg"); logDebug("kCmdCfg");
break; break;
case kCmdCloseVavle: case kCmdCloseVavle:
logDebug("kCmdCloseVavle"); logDebug("kCmdCloseVavle");
BSP_VALVE_Generate_ValveResponse(&RawData, kCmdCloseVavle, 1); BSP_VALVE_Generate_ValveResponse(&RawData, kCmdCloseVavle, 1);
peripheralChar4Notify((uint8_t*)&RawData.buf[0], RawData.len); peripheralChar4Notify((uint8_t *)&RawData.buf[0], RawData.len);
BSP_UART1_TxLoop(); BSP_UART1_TxLoop();
break; break;
case kCmdOpenVavle: case kCmdOpenVavle:
logDebug("kCmdOpenVavle"); logDebug("kCmdOpenVavle");
BSP_VALVE_Generate_ValveResponse(&RawData, kCmdOpenVavle, 1); BSP_VALVE_Generate_ValveResponse(&RawData, kCmdOpenVavle, 1);
peripheralChar4Notify((uint8_t*)&RawData.buf[0], RawData.len); peripheralChar4Notify((uint8_t *)&RawData.buf[0], RawData.len);
BSP_UART1_TxLoop(); BSP_UART1_TxLoop();
break; break;
default: default:
logError("无效的命令"); logError("无效的命令");
// logHexDumpAll(data, len); // logHexDumpAll(data, len);
break; break;
} }
} }
else else
{ {
logError("数据帧解析失败"); logError("数据帧解析失败");
} }
tmos_msg_deallocate((uint8_t*)HostFrameData); tmos_msg_deallocate((uint8_t *)HostFrameData);
HostFrameData = NULL; HostFrameData = NULL;
#endif #endif
logDebug("profile ChangeCB CHAR3.. End"); logDebug("profile ChangeCB CHAR3.. End");
break; break;
} }
@ -883,16 +888,46 @@ void Peripheral_UpdateAdvertData(uint8_t adType, uint8_t dataLen, uint8_t *pAdve
GAP_UpdateAdvertisingData(Peripheral_TaskID, adType, dataLen, pAdvertData); GAP_UpdateAdvertisingData(Peripheral_TaskID, adType, dataLen, pAdvertData);
} }
#include "stdio.h"
#include "stdarg.h"
// TODO:应用加入广播数据更新部分状态数据
void Peripheral_SetMacName(void) void Peripheral_SetMacName(void)
{ {
GetMACAddress(MacAddr); uint8_t mac_addr[6];
GetMACAddress(mac_addr);
// 手机APP显示是倒序的[5-0],不是[0-5] // 手机APP显示是倒序的[5-0],不是[0-5]
logDebug("MacAddr: %02X:%02X:%02X:%02X:%02X:%02X", MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]); logDebug("MacAddr: %02X:%02X:%02X:%02X:%02X:%02X", mac_addr[0], mac_addr[1]
, mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
// GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData); // GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
// // 更新扫描响应体 uint8_t scan_rsp_data[128];
// GAP_UpdateAdvertisingData(Peripheral_TaskID, FALSE, dataLen, pAdvertData); uint8_t len = 0;
} uint8_t name_len;
// complete name
scan_rsp_data[1] = GAP_ADTYPE_LOCAL_NAME_COMPLETE;
name_len = sprintf(&scan_rsp_data[2], "TYQ-%02X:%02X:%02X:%02X:%02X:%02X", mac_addr[0], mac_addr[1]
, mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
len = name_len + 1;
scan_rsp_data[0] = len;
logDebug("len:%d", len);
// connection interval range
scan_rsp_data[len++] = 0x05;
scan_rsp_data[len++] = LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL);
scan_rsp_data[len++] = HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL);
scan_rsp_data[len++] = LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL);
scan_rsp_data[len++] = HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL);
// Tx power level
scan_rsp_data[len++] = 0x02;
scan_rsp_data[len++] = GAP_ADTYPE_POWER_LEVEL;
scan_rsp_data[len] = 0;
logDebug("len:%d", len);
// logDebug("scan_rsp_data:");
logHexDumpAll(scan_rsp_data, len);
// // 更新扫描响应体
GAP_UpdateAdvertisingData(Peripheral_TaskID, FALSE, scan_rsp_data, len);
}

View File

@ -208,6 +208,12 @@ int main(void)
// 手机APP显示是倒序的[5-0],不是[0-5] // 手机APP显示是倒序的[5-0],不是[0-5]
logDebug("MacAddr: %02X:%02X:%02X:%02X:%02X:%02X", MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]); logDebug("MacAddr: %02X:%02X:%02X:%02X:%02X:%02X", MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]);
// extern void Peripheral_SetMacName(void);
// Peripheral_SetMacName();
// while(1)
// {
// DelayMs(1000);
// }
HAL_Init(); HAL_Init();
GAPRole_PeripheralInit(); GAPRole_PeripheralInit();
Peripheral_Init(); Peripheral_Init();

View File

@ -156,11 +156,11 @@ void CH59x_BLEInit(void)
{ {
uint8_t MacAddr[6]; uint8_t MacAddr[6];
GetMACAddress(MacAddr); GetMACAddress(MacAddr);
// for(i = 0; i < 6; i++) for(i = 0; i < 6; i++)
// { {
// cfg.MacAddr[i] = MacAddr[i]; // 使用芯片mac地址 cfg.MacAddr[i] = MacAddr[i]; // 使用芯片mac地址
// PRINT("MacAddr[%d]: %x\n", i, MacAddr[i]); // PRINT("MacAddr[%d]: %x\n", i, MacAddr[i]);
// } }
} }
#endif #endif
if(!cfg.MEMAddr || cfg.MEMLen < 4 * 1024) if(!cfg.MEMAddr || cfg.MEMLen < 4 * 1024)

View File

@ -127,7 +127,7 @@
#define BLE_MEMHEAP_SIZE (1024*7) #define BLE_MEMHEAP_SIZE (1024*7)
#endif #endif
#ifndef BLE_BUFF_MAX_LEN #ifndef BLE_BUFF_MAX_LEN
#define BLE_BUFF_MAX_LEN 251 #define BLE_BUFF_MAX_LEN 251 // MTU https://www.cnblogs.com/gscw/p/17896209.html
#endif #endif
#ifndef BLE_BUFF_NUM #ifndef BLE_BUFF_NUM
#define BLE_BUFF_NUM 5 #define BLE_BUFF_NUM 5