 BLE 數(shu)據接發流程
							時間(jian):2018-08-16      來源:未知
							BLE 數(shu)據接發流程
							時間(jian):2018-08-16      來源:未知 
							BLE概述
BLE分為兩(liang)部分:控制器和主機。對于(yu)4.0以前的藍牙,這兩(liang)部分是分開(kai)(kai)的。所有profile(用來(lai)定(ding)義設備或組件的角色)和應用都建構在GAP或GATT之(zhi)上。下面由結構圖的底(di)層組件開(kai)(kai)始介(jie)紹。
  
協議梭(suo)的(de)(de)實(shi)現(xian)方式采(cai)用(yong)(yong)分層(ceng)的(de)(de)思想(xiang),控制器部分包括物理(li)層(ceng)、鏈路層(ceng)、主機(ji)控制接口層(ceng)主機(ji)部分包括邏輯(ji)鏈路控制及(ji)自(zi)適應協議層(ceng)、安全(quan)管(guan)理(li)層(ceng)、屬性協議層(ceng)、通用(yong)(yong)訪問(wen)配置文件層(ceng)、通用(yong)(yong)屬性配置文件層(ceng);上層(ceng)可以調用(yong)(yong)下層(ceng)提供的(de)(de)函數來實(shi)現(xian)需要的(de)(de)功能。
PHY層:1Mbps自適應跳頻(pin)GFSK(高斯頻(pin)移鍵控(kong)),運(yun)行在(zai)免(mian)證的(de)2.4GHz。
LL層為(wei)RF控制器(射頻(pin)),控制設(she)備(bei)處于準備(bei)(standby)、廣播、監聽(ting)/掃(sao)描(scan)、初始化、連接,這五(wu)種狀態中任一種。
HCI層:為(wei)接(jie)口層,向(xiang)上(shang)為(wei)主(zhu)機(ji)提供軟件應(ying)用程(cheng)序接(jie)口(API),對外(wai)為(wei)外(wai)部硬件控(kong)制接(jie)口,可以通過串(chuan)口、SPI、USB來實現設備控(kong)制。
L2CAP層(ceng):提供數據封裝(zhuang)服務
SM層(加密);提供(gong)配對和密匙分發,實現安全(quan)連(lian)接和數據交換
ATT層:負(fu)責(ze)數據檢索(suo)
GATT層(ceng):出納負責處理向上(shang)與應用打交道(dao),而GATT負責向下把檢索(suo)任(ren)務子進(jin)程交給ATT層(ceng)去做,其關鍵工(gong)作(zuo)是把為檢索(suo)工(gong)作(zuo)提(ti)供合適的profile結構,而profile由檢索(suo)關鍵詞(characteristics)組成。
GAP層(ceng):向上提供應用程序(xu)接口(kou),向下管理各層(ceng)的(de)相應的(de)功能,尤(you)其是指示LL層(ceng)的(de)五種狀(zhuang)態切換,指導SM層(ceng)做(zuo)好加密工作。
BLE 啟動流程
在IAR 工程的左側有很多文件(jian)夾(jia),如(ru)APP 、HAL、OSAL、 PROFILES等(deng),如(ru)圖(tu)下(xia)圖(tu)所示, 這(zhe)些(xie)文件(jian)夾(jia)下(xia)面包含了很多源代碼,這(zhe)種(zhong)實現方式(shi)與(yu)藍(lan)牙4.0 BLE 協議的分層思想(xiang)是(shi)相對應的,盡量(liang)將實現某些(xie)功能的函數放在同一個文件(jian)夾(jia)下(xia)。
  
圖 BLE 4.0 工程列表
下面從main()函數(shu)入(ru)手,看看main函數(shu)都(dou)做(zuo)啦哪些工作(zuo)。下找(zhao)到simpleBLECentral_Main.c文件(jian)中的main函數(shu)入(ru)口。代碼(ma)如下:
int main(void)
{
/* Initialize hardware */
HAL_BOARD_INIT();
// Initialize board I/O
InitBoard( OB_COLD );
/* Initialze the HAL driver */
HalDriverInit();
/* Initialize NV system */
osal_snv_init();
/* Initialize LL */
/* Initialize the operating system */
osal_init_system();
/* Enable interrupts */
HAL_ENABLE_INTERRUPTS();
// Final board initialization
InitBoard( OB_READY );
#if defined ( POWER_SAVING )
osal_pwrmgr_device( PWRMGR_BATTERY );
#endif
/* Start OSAL */
osal_start_system(); // No Return from here
return 0;
}
在main函數(shu)(shu)中大部分是(shi)對(dui)CC2540的初始化和對(dui)無(wu)線網(wang)絡協議(yi)的配(pei)置。主要的就是(shi)osal_start_system()函數(shu)(shu)的處理,這是(shi)重點,只有執(zhi)行這個函數(shu)(shu),藍(lan)牙BLE的協議(yi)棧才(cai)是(shi)真正的運(yun)行。
為(wei)下函數為(wei)任務啟(qi)動過程(cheng)。實現(xian)不停的(de)查看事件表(biao),如(ru)果有事件發生(sheng)就(jiu)調用相(xiang)應的(de)事件處(chu)理函數。
void osal_run_system( void )
{
uint8 idx = 0;
#ifndef HAL_BOARD_CC2538
osalTimeUpdate();
#endif
Hal_ProcessPoll();
do {
if (tasksEvents[idx]) // Task is highest priority that is ready.
{
break;
}
} while (++idx < tasksCnt);
if (idx < tasksCnt)
{
uint16 events;
halIntState_t intState;
HAL_ENTER_CRITICAL_SECTION(intState);
events = tasksEvents[idx];
tasksEvents[idx] = 0; // Clear the Events for this task.
HAL_EXIT_CRITICAL_SECTION(intState);
activeTaskID = idx;
events = (tasksArr[idx])( idx, events );
activeTaskID = TASK_NO_TASK;
HAL_ENTER_CRITICAL_SECTION(intState);
tasksEvents[idx] |= events; // Add back unprocessed events to the current task.
HAL_EXIT_CRITICAL_SECTION(intState);
}
#if defined( POWER_SAVING )
else // Complete pass through all task events with no activity?
{
osal_pwrmgr_powerconserve(); // Put the processor/system into sleep
}
#endif
/* Yield in case cooperative scheduling is being used. */
#if defined (configUSE_PREEMPTION) && (configUSE_PREEMPTION == 0)
{
osal_task_yield();
}
#endif
}
事件處理函數
如(ru)果有事(shi)(shi)件(jian)調用SimpleBLEPeripheral_ProcessEvent()函數處(chu)理相(xiang)應(ying)的消息事(shi)(shi)件(jian)。在simpleBLECentral.c文(wen)件(jian)中。
uint16 SimpleBLECentral_ProcessEvent( uint8 task_id, uint16 events )
{
VOID task_id; // OSAL required parameter that isn't used in this function
if ( events & SYS_EVENT_MSG )
{
uint8 *pMsg;
if ( (pMsg = osal_msg_receive( simpleBLETaskId )) != NULL )
{
simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
// Release the OSAL message
VOID osal_msg_deallocate( pMsg );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
if ( events & START_DEVICE_EVT )
{
// Start the Device
VOID GAPCentralRole_StartDevice( (gapCentralRoleCB_t *) &simpleBLERoleCB );
// Register with bond manager after starting device
GAPBondMgr_Register( (gapBondCBs_t *) &simpleBLEBondCB );
//自(zi)動(dong)開始搜索
if ( !simpleBLEScanning & simpleBLEScanRes == 0 )
{
simpleBLEScanning = TRUE;
simpleBLEScanRes = 0;
GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
DEFAULT_DISCOVERY_ACTIVE_SCAN,
DEFAULT_DISCOVERY_WHITE_LIST );
LCD_WRITE_STRING( "Scanning...", HAL_LCD_LINE_1 );
}
else
{
LCD_WRITE_STRING( "No Scan", HAL_LCD_LINE_1 );
}
return ( events ^ START_DEVICE_EVT );
}
if ( events & START_DISCOVERY_EVT )
{
simpleBLECentralStartDiscovery( );
return ( events ^ START_DISCOVERY_EVT );
}
// Discard unknown events
return 0;
}
接收數據
本函(han)數主要是處理無線的數據(ju),如把(ba)接收到的數據(ju)打(da)印到串口終端(duan)。打(da)印函(han)數如以下代碼中(zhong)的紅(hong)(hong)色字(zi)體(ti)部(bu)分。注意紅(hong)(hong)色部(bu)分。
static void simpleBLECentralProcessGATTMsg( gattMsgEvent_t *pMsg )
{
if ( simpleBLEState != BLE_STATE_CONNECTED )
{
// In case a GATT message came after a connection has dropped,
// ignore the message
return;
}
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;
LCD_WRITE_STRING_VALUE( "Read Error", status, 10, HAL_LCD_LINE_1 );
}
else
{
// After a successful read, display the read value
uint8 valueRead = pMsg->msg.readRsp.value[0];
LCD_WRITE_STRING_VALUE( "Read rsp:", valueRead, 10, HAL_LCD_LINE_1 );
}
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;
LCD_WRITE_STRING_VALUE( "Write Error", status, 10, HAL_LCD_LINE_1 );
}
else
{
// After a succesful write, display the value that was written and increment value
//LCD_WRITE_STRING_VALUE( "Write sent:", simpleBLECharVal++, 10, HAL_LCD_LINE_1 );
simpleBLEChar6DoWrite = TRUE;
}
simpleBLEProcedureInProgress = FALSE;
}
else if ( simpleBLEDiscState != BLE_DISC_STATE_IDLE )
{
simpleBLEGATTDiscoveryEvent( pMsg );
}
else if ( ( pMsg->method == ATT_HANDLE_VALUE_NOTI ) ) //通知
{
if( pMsg->msg.handleValueNoti.handle == 0x0038) //CHAR7的通(tong)知 串口(kou)打印
{
if(pMsg->msg.handleValueNoti.value[0]>=15)
{
NPI_WriteTransport(&pMsg->msg.handleValueNoti.value[1],14 );
NPI_WriteTransport("...\n",4 );
}
else
{
NPI_WriteTransport(&pMsg->msg.handleValueNoti.value[1],pMsg->msg.handleValueNoti.value[0] );
}
}
}
}
發送數據
從(cong)串口讀取數據存入buf[]緩沖區,然后調(diao)用(yong)協議棧(zhan)函數GATT_WriteCharValue()發送出去(qu)。
NpiSerialCallback()函(han)數為串口回調函(han)數,當串口有(you)數據時(shi)(shi)就會調用此(ci)函(han)數,并且(qie)同時(shi)(shi)會產生串口事件HAL_UART_RX_TIMEOUT。
static void NpiSerialCallback( uint8 port, uint8 events )
{
(void)port;
uint8 numBytes = 0;
uint8 buf[128];
if (events & HAL_UART_RX_TIMEOUT) //串(chuan)口有(you)數(shu)據(ju)
{
numBytes = NPI_RxBufLen(); //讀出(chu)串口(kou)緩(huan)沖區(qu)有多(duo)少字(zi)節
if(numBytes)
{
if ( ( simpleBLEState == BLE_STATE_CONNECTED ) && ( simpleBLECharHd6 != 0 ) ) //已連接并獲取完(wan)CHAR6的Handle就寫CHAR6
{
if(simpleBLEChar6DoWrite) //寫入成功后再寫入
{
attWriteReq_t AttReq;
if ( numBytes >= SIMPLEPROFILE_CHAR6_LEN ) buf[0] = SIMPLEPROFILE_CHAR6_LEN-1;
else buf[0] = numBytes;
NPI_ReadTransport(&buf[1],buf[0]); //從串口讀出數據
AttReq.handle = simpleBLECharHd6;
ttReq.len = SIMPLEPROFILE_CHAR6_LEN;
AttReq.sig = 0;
AttReq.cmd = 0;
osal_memcpy(AttReq.value,buf,SIMPLEPROFILE_CHAR6_LEN);
GATT_WriteCharValue( 0, &AttReq, simpleBLETaskId );
simpleBLEChar6DoWrite = FALSE;
}
}
else
{
NPI_WriteTransport("Not Ready\n", 10 );
NPI_ReadTransport(buf,numBytes); //釋放串(chuan)口數(shu)據
}
}
}
}
BLE Central串口通信協議
在上位機上顯示終端的實時(shi)信息(xi),必(bi)須有相應的傳輸協議(yi),協議(yi)里含有終端的相應信息(xi)。
制定協議如下:
( 1 ) 串口(kou)打印數據(ju)協議(yi)信息:
21 B 01 00 54 00 00 01 3E 14 EB
21 協議頭’!’ B:BLE 01 00:模塊源節(jie)點(dian)(dian)地址(zhi)(zhi) 54:類型 00 00 01:數據(ju)/設(she)備狀(zhuang)態 3E 14:父節(jie)點(dian)(dian)地址(zhi)(zhi) EB:校驗
如果第(di)2數據(ju)類型為’B’,則代表所連接的設備是BLE。
( 2 ) 協(xie)調節點(dian)串(chuan)口發送的(de)信息:
# C B f 00 01 01
“# C”控(kong)制終端(duan)的協(xie)議頭 B:BLE 00 01:終端(duan)節(jie)點(dian)地址 01 控(kong)制命(ming)令(1:開(kai)/0:關(guan))

