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
*/ */
@ -62,7 +61,7 @@
#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)
@ -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 = {
@ -208,6 +205,8 @@ 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
{ {
// 开启广播使能 // 开启广播使能
@ -346,6 +345,11 @@ 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);
} }
@ -453,7 +457,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;
logDebug("mtu exchange: %d", pMsgEvent->msg.exchangeMTUReq.clientRxMTU); logDebug("****mtu exchange: %d****", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
} }
break; break;
} }
@ -489,6 +493,7 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
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);
} }
} }
@ -576,8 +581,8 @@ static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t 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
{ {
@ -743,7 +748,7 @@ static void performPeriodicTask(void)
// { // {
// humi = 0; // humi = 0;
// } // }
peripheralChar4Notify(&RawData.buf[0], RawData.len); // peripheralChar4Notify(&RawData.buf[0], RawData.len);
} }
/********************************************************************* /*********************************************************************
@ -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