一. central流程分析
1. 初始化的过程
osal_init_system
-->osalInitTasks
--> SimpleBLECentral_Init( taskID );
--> //设置自定义事件START_DEVICE_EVT
osal_set_event( simpleBLETaskId, START_DEVICE_EVT );
osal_start_system
--> 死循环调用osal_run_system
--> SimpleBLECentral_ProcessEvent //这是一个用户自定义处理函数,可以处理所有用户自定义事件
SimpleBLECentral_ProcessEvent
{
SYS_EVENT_MSG --> 按键与gatt事件
START_DEVICE_EVT
-->处理在初始化时osal_set_event设置的事件
-->并注册两个回调,但只关心 simpleBLECentralEventCB
-->产生一个GAP_DEVICE_INIT_DONE_EVENT事件
START_DISCOVERY_EVT
}
2. 当有按键按下时:
SimpleBLECentral_ProcessEvent会收到SYS_EVENT_MSG
-->simpleBLECentral_ProcessOSALMsg //区分按键与gatt事件
--> simpleBLECentral_HandleKeys //进行按键处理
{
//上: 未连接则进行或停止扫描,己连接则进行数据读写
//左:
//右: UpdateLink
//中: 未连接则建立连接;己连接则中断连接
//下: 如果己连接,输出rssi
}
2.1 状态回调函数simpleBLECentralEventCB
simpleBLECentralEventCB
{
GAP_DEVICE_INIT_DONE_EVENT: //START_DEVICE_EVT完成后会产生
GAP_DEVICE_INFO_EVENT : //主机收到一个从机的广播包时产生
GAP_DEVICE_DISCOVERY_EVENT: //主机扫描结束时产生
GAP_LINK_ESTABLISHED_EVENT: //建立连接后产生
GAP_LINK_TERMINATED_EVENT : //结束连接后产生
GAP_LINK_PARAM_UPDATE_EVENT
}
3. 当己连接时
SimpleBLECentral_ProcessEvent会收到SYS_EVENT_MSG
-->simpleBLECentral_ProcessOSALMsg //区分按键与gatt事件
--> simpleBLEGATTDiscoveryEvent //进行按键处理
simpleBLEGATTDiscoveryEvent
{
}
二.代码分析
2.1 初始化跟
SimpleBLEPeripheral 的初始化基本类似,
osal_init_system
-->osalInitTasks
--> SimpleBLECentral_Init( taskID );
在APP->simpleBLECentral.c中,功能:
a.配置GAP,GAP Bond manager的参数
b.初始化GATT client
-
void SimpleBLECentral_Init( uint8 task_id )
-
{
-
simpleBLETaskId = task_id;
-
-
//串口初始化
-
UartInit();
-
// Setup Central Profile
-
{
-
uint8 scanRes = DEFAULT_MAX_SCAN_RES;
-
GAPCentralRole_SetParameter ( GAPCENTRALROLE_MAX_SCAN_RES, sizeof( uint8 ), &scanRes );
-
}
-
-
// Setup GAP
-
GAP_SetParamValue( TGAP_GEN_DISC_SCAN, DEFAULT_SCAN_DURATION );
-
GAP_SetParamValue( TGAP_LIM_DISC_SCAN, DEFAULT_SCAN_DURATION );
-
GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, (uint8 *) simpleBLEDeviceName );
-
-
// Setup the GAP Bond Manager
-
{
-
uint32 passkey = DEFAULT_PASSCODE;
-
uint8 pairMode = DEFAULT_PAIRING_MODE;
-
uint8 mitm = DEFAULT_MITM_MODE;
-
uint8 ioCap = DEFAULT_IO_CAPABILITIES;
-
uint8 bonding = DEFAULT_BONDING_MODE;
-
GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof( uint32 ), &passkey );
-
GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof( uint8 ), &pairMode );
-
GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof( uint8 ), &mitm );
-
GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof( uint8 ), &ioCap );
-
GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof( uint8 ), &bonding );
-
}
-
-
//初始化GATT client
-
VOID GATT_InitClient();
-
-
// Register to receive incoming ATT Indications/Notifications
-
GATT_RegisterForInd( simpleBLETaskId );
-
-
// Initialize GATT attributes
-
GGS_AddService( GATT_ALL_SERVICES ); // GAP
-
GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes
-
-
//注册按键事件,只有在这儿注册了才能触发
-
RegisterForKeys( simpleBLETaskId );
-
-
//关led灯
-
HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );
-
-
//设置事件START_DEVICE_EVT,这个事件是用户自定义的,所以在用户自定义task中
-
osal_set_event( simpleBLETaskId, START_DEVICE_EVT );
-
}
2.2 进入用户自定义处理函数中
osal_start_system
--> 死循环调用osal_run_system //这个到下面这个再看一下
--> SimpleBLECentral_ProcessEvent //因为这个函数加到了tasksArr中,所以每次系统loop都会调用到
-
uint16 SimpleBLECentral_ProcessEvent( uint8 task_id, uint16 events )
-
{
-
if ( events & SYS_EVENT_MSG )
-
{
-
uint8 *pMsg;
-
if ( (pMsg = osal_msg_receive( simpleBLETaskId )) != NULL )
-
{
-
simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
-
VOID osal_msg_deallocate( pMsg );
-
}
-
return (events ^ SYS_EVENT_MSG);
-
}
-
//事件START_DEVICE_EVT的处理,开机启动主机
-
if ( events & START_DEVICE_EVT )
-
{
-
VOID GAPCentralRole_StartDevice( (gapCentralRoleCB_t *) &simpleBLERoleCB );
-
//向bond manager注册回调
-
GAPBondMgr_Register( (gapBondCBs_t *) &simpleBLEBondCB );
-
return ( events ^ START_DEVICE_EVT );
-
}
-
-
if ( events & START_DISCOVERY_EVT )
-
{ //按中键建立连接后,在建立连接的事件回调函数simpleBLECentralEventCB,在处理时加的时间定时器产生
-
simpleBLECentralStartDiscovery(); //这个函数用来获取gatt server的句柄
-
return ( events ^ START_DISCOVERY_EVT );
-
}
-
return 0;
-
}
这儿注册了回调函数
simpleBLERoleCB,这实际上是个函数数组,包含两个函数
simpleBLECentralRssiCB, // RSSI callback,这个会打印rssi信息,不关心
simpleBLECentralEventCB // Event callback
-
static void simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
-
{
-
switch ( pEvent->gap.opcode )
-
{
-
case GAP_DEVICE_INIT_DONE_EVENT: //START_DEVICE_EVT事件处理完,设备初始化成功就会产生
-
{
-
}
-
break;
-
//在扫描过程中每发现一个设备就会产生
-
case GAP_DEVICE_INFO_EVENT:
-
{ //这个事件是按下key_up键的处理函数执行完后会产生
-
if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
-
{
-
//此时pEvent->deviceInfo.pEvtData就是Peripheral发的advertData,len就是advertData的长度
-
//下面这个函数就是解析Peripheral发送的advertData这个字符串,找到与SIMPLEPROFILE_SERV_UUID配匹的项
-
if ( simpleBLEFindSvcUuid( SIMPLEPROFILE_SERV_UUID, pEvent->deviceInfo.pEvtData, pEvent->deviceInfo.dataLen ) )
-
{
-
//然后把Peripheral的设备地址存到simpleBLEDevList中
-
simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
-
}
-
}
-
}
-
break;
-
-
case GAP_DEVICE_DISCOVERY_EVENT:
-
{
-
simpleBLEScanning = FALSE;
-
if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE )
-
{
-
simpleBLEScanRes = pEvent->discCmpl.numDevs;
-
osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList, (sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );
-
}
-
simpleBLEScanIdx = simpleBLEScanRes;
-
}
-
break;
-
case GAP_LINK_ESTABLISHED_EVENT:
-
{ //这个事件是按下中键的处理函数执行完后会产生
-
if ( pEvent->gap.hdr.status == SUCCESS )
-
{
-
simpleBLEState = BLE_STATE_CONNECTED;
-
simpleBLEConnHandle = pEvent->linkCmpl.connectionHandle;
-
simpleBLEProcedureInProgress = TRUE;
-
-
if ( simpleBLECharHdl == 0 )
-
{ //定时器函数,产生一个START_DISCOVERY_EVT事件,会在SimpleBLECentral_ProcessEvent中处理
-
osal_start_timerEx( simpleBLETaskId, START_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY );
-
}
-
}
-
else
-
{
-
simpleBLEState = BLE_STATE_IDLE;
-
simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
-
simpleBLERssi = FALSE;
-
simpleBLEDiscState = BLE_DISC_STATE_IDLE;
-
}
-
}
-
break;
-
-
case GAP_LINK_TERMINATED_EVENT:
-
{
-
simpleBLEState = BLE_STATE_IDLE;
-
simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
-
simpleBLERssi = FALSE;
-
simpleBLEDiscState = BLE_DISC_STATE_IDLE;
-
simpleBLECharHdl = 0;
-
simpleBLEProcedureInProgress = FALSE;
-
}
-
break;
-
-
case GAP_LINK_PARAM_UPDATE_EVENT:
-
{
-
}
-
break;
-
-
default:
-
break;
-
}
-
}
在这个事件的处理函数中
-
static void simpleBLECentral_ProcessOSALMsg( osal_event_hdr_t *pMsg )
-
{
-
switch ( pMsg->event )
-
{
-
case KEY_CHANGE: //只处理按键事件
-
simpleBLECentral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );
-
break;
-
-
case GATT_MSG_EVENT: //与GATT事件
-
simpleBLECentralProcessGATTMsg( (gattMsgEvent_t *) pMsg );
-
break;
-
}
-
}
按键事件
-
static void simpleBLECentral_HandleKeys( uint8 shift, uint8 keys )
-
{
-
(void)shift; // Intentionally unreferenced parameter
-
-
if ( keys & HAL_KEY_UP ) //如果是向上的按键
-
{
-
if (没有建立连接)
-
{
-
GAPCentralRole_StartDiscovery(); //进行扫描,每扫到一个设备就会产生GAP_DEVICE_INFO_EVENT
-
再按一次GAPCentralRole_CancelDiscovery(); //停止扫描
-
}
-
else if (己建立连接)
-
GATT_WriteCharValue(); 或者 GATT_ReadCharValue();
-
}
-
-
-
if ( keys & HAL_KEY_LEFT ) //如果是向左的按键
-
{ //key_up按下时就simpleBLEScanning=true;当扫描到设备时simpleBLEScanRes++
-
if ( !simpleBLEScanning && simpleBLEScanRes > 0 )
-
{
-
simpleBLEScanIdx++;
-
if ( simpleBLEScanIdx >= simpleBLEScanRes )
-
{
-
simpleBLEScanIdx = 0;
-
}
-
}
-
}
-
-
if ( keys & HAL_KEY_RIGHT )
-
{
-
// Connection update
-
if ( simpleBLEState == BLE_STATE_CONNECTED )
-
{
-
GAPCentralRole_UpdateLink( simpleBLEConnHandle,
-
DEFAULT_UPDATE_MIN_CONN_INTERVAL,
-
DEFAULT_UPDATE_MAX_CONN_INTERVAL,
-
DEFAULT_UPDATE_SLAVE_LATENCY,
-
DEFAULT_UPDATE_CONN_TIMEOUT );
-
}
-
}
-
-
if ( keys & HAL_KEY_CENTER )
-
{ //中键建立或断开连接
-
if ( simpleBLEState == BLE_STATE_IDLE )
-
{
-
if ( simpleBLEScanRes > 0 )
-
{ //未连接则建立连接
-
peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
-
addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
-
simpleBLEState = BLE_STATE_CONNECTING;
-
GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE, DEFAULT_LINK_WHITE_LIST, addrType, peerAddr );
-
}
-
}
-
else if ( simpleBLEState == BLE_STATE_CONNECTING || simpleBLEState == BLE_STATE_CONNECTED )
-
{ //己连接,则断开连接
-
simpleBLEState = BLE_STATE_DISCONNECTING;
-
gStatus = GAPCentralRole_TerminateLink( simpleBLEConnHandle );
-
}
-
}
-
-
if ( keys & HAL_KEY_DOWN )
-
{
-
// Start or cancel RSSI polling
-
if ( simpleBLEState == BLE_STATE_CONNECTED )
-
{
-
if ( !simpleBLERssi )
-
{
-
simpleBLERssi = TRUE;
-
GAPCentralRole_StartRssi( simpleBLEConnHandle, DEFAULT_RSSI_PERIOD );
-
}
-
else
-
{
-
simpleBLERssi = FALSE;
-
GAPCentralRole_CancelRssi( simpleBLEConnHandle );
-
-
LCD_WRITE_STRING( "RSSI Cancelled", HAL_LCD_LINE_1 );
-
}
-
}
-
}
-
}
当建立连接后会收到GATTMsg
SimpleBLECentral_ProcessEvent //系统接收到SYS_EVENT_MSG
--> simpleBLECentral_ProcessOSALMsg //区分按键与gatt
--> simpleBLECentralProcessGATTMsg //对于gatt事件
-
static void simpleBLECentralProcessGATTMsg( gattMsgEvent_t *pMsg )
-
{
-
if ( simpleBLEState != BLE_STATE_CONNECTED )
-
{ //未连接,直接返回
-
return;
-
}
-
//gatt中read与write完成之后,会到这儿,对结果进行处理
-
//有错的报错,没有错的打印read,write的value
-
if ( ( pMsg->method == ATT_READ_RSP ) || (( pMsg->method == ATT_ERROR_RSP ) && ( pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ ) ) )
-
{
-
if ( pMsg->method == ATT_ERROR_RSP )
-
uint8 status = pMsg->msg.errorRsp.errCode;
-
else
-
uint8 valueRead = pMsg->msg.readRsp.value[0];
-
simpleBLEProcedureInProgress = FALSE;
-
}
-
else if ( ( pMsg->method == ATT_WRITE_RSP ) || ( ( pMsg->method == ATT_ERROR_RSP ) && ( pMsg->msg.errorRsp.reqOpcode == ATT_WRITE_REQ ) ) )
-
{
-
if ( pMsg->method == ATT_ERROR_RSP == ATT_ERROR_RSP )
-
uint8 status = pMsg->msg.errorRsp.errCode;
-
else
-
printV( "Write sent:", simpleBLECharVal++, 10);
-
simpleBLEProcedureInProgress = FALSE;
-
}
-
else if ( simpleBLEDiscState != BLE_DISC_STATE_IDLE )
-
{ //非read,write的事件就是系统产生的GATT discovery event
-
simpleBLEGATTDiscoveryEvent( pMsg );
-
}
-
}
-
static void simpleBLEGATTDiscoveryEvent( gattMsgEvent_t *pMsg )
-
{
-
attReadByTypeReq_t req;
-
-
if ( simpleBLEDiscState == BLE_DISC_STATE_SVC )
-
{
-
if ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->msg.findByTypeValueRsp.numInfo > 0 )
-
{
-
simpleBLESvcStartHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle;
-
simpleBLESvcEndHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle;
-
}
-
-
if ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->hdr.status == bleProcedureComplete ) ||
-
( pMsg->method == ATT_ERROR_RSP ) )
-
{
-
if ( simpleBLESvcStartHdl != 0 )
-
{
-
simpleBLEDiscState = BLE_DISC_STATE_CHAR;
-
req.startHandle = simpleBLESvcStartHdl;
-
req.endHandle = simpleBLESvcEndHdl;
-
req.type.len = ATT_BT_UUID_SIZE;
-
req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID);
-
req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID);
-
//发送获取charsimpleBLECharHdl的命令,成功后会产生一个GATT_MSG_EVENT
-
//其type=ATT_READ_BY_TYPE_RSP,即到了下面的这个else if
-
GATT_ReadUsingCharUUID( simpleBLEConnHandle, &req, simpleBLETaskId );
-
}
-
}
-
}
-
else if ( simpleBLEDiscState == BLE_DISC_STATE_CHAR )
-
{ //上面的GATT_ReadUsingCharUUID成功后,产生一个ATT_READ_BY_TYPE_RSP
-
//那么就可以获取simpleBLECharHdl了=0x25
-
if ( pMsg->method == ATT_READ_BY_TYPE_RSP && pMsg->msg.readByTypeRsp.numPairs > 0 )
-
{
-
simpleBLECharHdl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0], pMsg->msg.readByTypeRsp.dataList[1] );
-
simpleBLEProcedureInProgress = FALSE;
-
}
-
simpleBLEDiscState = BLE_DISC_STATE_IDLE;
-
}
-
}
阅读(9085) | 评论(0) | 转发(0) |