This commit is contained in:
stark1898y 2024-12-16 15:36:45 +08:00
parent 6e1a1dc3ed
commit 5889762ed0
4 changed files with 110 additions and 72 deletions

View File

@ -33,6 +33,8 @@
#undef LOG_TAG #undef LOG_TAG
#define LOG_TAG "peripheral" #define LOG_TAG "peripheral"
volatile uint8_t mtu_flag = 0;
/********************************************************************* /*********************************************************************
* MACROS * MACROS
*/ */
@ -51,7 +53,7 @@
#define SBP_PARAM_UPDATE_DELAY (1600 * 1) #define SBP_PARAM_UPDATE_DELAY (1600 * 1)
// PHY update delay // PHY update delay
#define SBP_PHY_UPDATE_DELAY (1600 * 1) #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 * 10) #define DEFAULT_ADVERTISING_INTERVAL (160 * 10)
@ -205,8 +207,6 @@ 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
{ {
// 开启广播使能 // 开启广播使能
@ -291,6 +291,9 @@ void Peripheral_Init()
// Setup a delayed profile startup // Setup a delayed profile startup
tmos_set_event(Peripheral_TaskID, SBP_START_DEVICE_EVT); tmos_set_event(Peripheral_TaskID, SBP_START_DEVICE_EVT);
// https://www.cnblogs.com/ZYL-FS/p/18061479
GATT_InitClient();
} }
/********************************************************************* /*********************************************************************
@ -345,10 +348,6 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
{ {
// 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);
} }
@ -457,6 +456,7 @@ static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_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;
mtu_flag = 1;
logDebug("****mtu exchange: %d****", pMsgEvent->msg.exchangeMTUReq.clientRxMTU); logDebug("****mtu exchange: %d****", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
} }
break; break;
@ -492,8 +492,8 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
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 - 4; // peripheralMTU = BLE_BUFF_MAX_LEN - 4;
// 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);
@ -503,6 +503,10 @@ 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);
// https://www.cnblogs.com/ZYL-FS/p/17759138.html
// 使能通知
enable_notify(peripheralConnList.connHandle, ENABLE);
logDebug("Conn %x - Int %x= %.1fms", event->connectionHandle, event->connInterval, event->connInterval * 1.25); logDebug("Conn %x - Int %x= %.1fms", event->connectionHandle, event->connInterval, event->connInterval * 1.25);
} }
} }
@ -535,6 +539,8 @@ static void Peripheral_LinkTerminated(gapRoleEvent_t *pEvent)
// uint8_t init_adv_channel_map = GAP_ADVCHAN_ALL ; // uint8_t init_adv_channel_map = GAP_ADVCHAN_ALL ;
// GAPRole_SetParameter( GAPROLE_ADV_CHANNEL_MAP, sizeof( uint8_t ), &init_adv_channel_map); // GAPRole_SetParameter( GAPROLE_ADV_CHANNEL_MAP, sizeof( uint8_t ), &init_adv_channel_map);
mtu_flag = 0;
uint8_t advertising_enable = TRUE; uint8_t advertising_enable = TRUE;
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertising_enable); GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertising_enable);
} }
@ -625,6 +631,12 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven
{ {
Peripheral_LinkEstablished(pEvent); Peripheral_LinkEstablished(pEvent);
logDebug("Connected.."); logDebug("Connected..");
// 在蓝牙建立连接之后,调用 GATT ExchangeMTU 来修改 MTU 的值
// Update MTU
attExchangeMTUReq_t req = {.clientRxMTU = BLE_BUFF_MAX_LEN - 4,};
GATT_ExchangeMTU(peripheralConnList.connHandle, &req, Peripheral_TaskID);
// Peripheral_SetMacName();
} }
break; break;
@ -687,14 +699,10 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven
*/ */
static void performPeriodicTask(void) static void performPeriodicTask(void)
{ {
// static uint8_t counter = 0; if (mtu_flag)
// uint8_t notiData[SIMPLEPROFILE_CHAR4_LEN] = {0x11, 0x22, 0x33, 0x44, 0}; {
// notiData[4] = counter; // 温度
// // peripheralChar4Notify(notiData, SIMPLEPROFILE_CHAR4_LEN); uint32_t countadc = 0;
// peripheralChar4Notify(notiData, 5);
// counter++;
#if 1
// VBAT // VBAT
uint32_t CountBat = 0; uint32_t CountBat = 0;
uint16_t adcBuff[40]; uint16_t adcBuff[40];
@ -722,7 +730,7 @@ static void performPeriodicTask(void)
logDebug("min_number = %d, max_number = %d", min_number, max_number); logDebug("min_number = %d, max_number = %d", min_number, max_number);
CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值 CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值
logDebug("AverageCountBat = %d", CountBat); logDebug("AverageCountBat = %d", CountBat);
#endif
int ret; int ret;
@ -749,6 +757,7 @@ static void performPeriodicTask(void)
// humi = 0; // humi = 0;
// } // }
peripheralChar4Notify(&RawData.buf[0], RawData.len); peripheralChar4Notify(&RawData.buf[0], RawData.len);
}
} }
/********************************************************************* /*********************************************************************
@ -779,8 +788,11 @@ static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
logError("Noti Err"); logError("Noti Err");
GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI); GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI);
} }
else
{
logDebug("Noti Success"); logDebug("Noti Success");
} }
}
} }
/********************************************************************* /*********************************************************************
@ -879,6 +891,9 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
} }
} }
#if 0 #if 0

View File

@ -44,18 +44,18 @@ void app_task_handler(TeAppEvtType app_evt_type)
switch(app_evt_type) switch(app_evt_type)
{ {
case kKeyShort:{ case kKeyShort:{
logDebug("button short press\r\n"); logDebug("button short press");
break; break;
} }
case kKeyLong: case kKeyLong:
logDebug("button long press\r\n"); logDebug("button long press");
break; break;
case kKeyRelease: case kKeyRelease:
// BSP_KEY_EnterLowpower(); // BSP_KEY_EnterLowpower();
DelayMs(10); // DelayMs(10);
BSP_RequestSleep(); BSP_RequestSleep();
logDebug("gpio relase;BSP_RequestSleep\r\n"); logDebug("gpio relase;BSP_RequestSleep");
break; break;
default: default:

View File

@ -726,5 +726,25 @@ static void simpleProfile_HandleConnStatusCB(uint16_t connHandle, uint8_t change
} }
} }
//定义使能通知函数connection_handle连接句柄表示要进行操作的连接
uint8_t enable_notify(uint16_t connection_handle, uint8_t enable)
{
uint16_t cccd = 0; //定义一个变量cccd特征的配置值
//判断是否使能通知
if(enable) //使能
{
cccd |= GATT_CLIENT_CFG_NOTIFY; //将cccd置为使能通知的值
}
else
{
cccd &= ~GATT_CLIENT_CFG_NOTIFY; //不使能将cccd置为禁止通知的值
}
return GATTServApp_WriteCharCfg( connection_handle, simpleProfileChar4Config, cccd); //将特征配置值写入到连接句柄,以实现通知的使能或禁用
}
/********************************************************************* /*********************************************************************
*********************************************************************/ *********************************************************************/

View File

@ -125,6 +125,9 @@ extern bStatus_t SimpleProfile_GetParameter(uint8_t param, void *value);
*/ */
extern bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti); extern bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti);
extern uint8_t enable_notify(uint16_t connection_handle, uint8_t enable);
/********************************************************************* /*********************************************************************
*********************************************************************/ *********************************************************************/