1.1 網(wǎng)絡(luò)建立與加入
(1)首先在Main函數(shù)中,會(huì)有這樣的語句:
for(;;)
{
SYS_RunTask();
}
通過SYS_RunTask函數(shù),系統(tǒng)會(huì)調(diào)用BitCloud協(xié)議棧中的用戶應(yīng)用程序,即APL_TaskHandler()。
(2)
void APL_TaskHandler(void)
{
if (appStatePosted)
{
appStatePosted = false;
appTaskHandler();
}
if (appSubStatePosted)
{
appSubStatePosted = false;
appSubtaskHandler();
}
}
(3)按照流程程序調(diào)用appTaskHandler();該函數(shù)中對(duì)各種狀態(tài)進(jìn)行判斷執(zhí)行相應(yīng)操作。
static void appTaskHandler(void)
{
switch (appState)
{
case APP_INITIAL_STATE:
initHardware();
initNetwork();
break;
case APP_JOINING_STATE:
startNetwork();
break;
case APP_JOINED_STATE:
break;
case APP_LEAVE_STATE:
leaveNetwork();
break;
}
}
(3.9)硬件初始化函數(shù)
static void initHardware(void)
{
#ifdef BOARD_SAM3S_PIRRD
// Register interrupts
HAL_InstallInterruptVector(SysTick_IRQn, SysTick_Handler);
// Configure LEDS
LED_Configure(APP_LED_0);
// Configure SRAM
BOARD_ConfigureSram();
// Configure system tick timer
TimeTick_Configure(BOARD_MCK);
// Configure LCD
appLCD_Init();
#else
// Configure LEDS
BSP_OpenLeds();
// Configure communication medium
appMediumInit();
#endif // BOARD_SAM3S_PIRRD
}
(4)網(wǎng)絡(luò)初始化
static void initNetwork(void)
{
CS_WriteParameter(CS_DEVICE_TYPE_ID, &deviceType);//將設(shè)備在網(wǎng)絡(luò)中的角色配置為終端設(shè)備。
if (DEV_TYPE_ENDDEVICE == deviceType)
{
bool rxOnWhenIdleFlag = false;
CS_WriteParameter(CS_RX_ON_WHEN_IDLE_ID, &rxOnWhenIdleFlag);
}
}
// 命令請(qǐng)求參數(shù)的配置
apsCommandReq.dstAddrMode = APS_SHORT_ADDRESS;
apsCommandReq.dstAddress.shortAddress = 0;
apsCommandReq.profileId = APP_PROFILE_ID;
apsCommandReq.dstEndpoint = APP_CONTROL_ENDPOINT;
apsCommandReq.clusterId = APP_CLUSTER_ID;
apsCommandReq.srcEndpoint = APP_CONTROL_ENDPOINT;
apsCommandReq.asdu = (uint8_t *)&appCommandFrame.command;
apsCommandReq.asduLength = 0;
apsCommandReq.txOptions.acknowledgedTransmission = 1;
apsCommandReq.radius = 0;
apsCommandReq.APS_DataConf = apsCommandReqConf;
// 數(shù)據(jù)塊請(qǐng)求的配置


然后調(diào)用startNerwork()啟動(dòng)搜尋網(wǎng)絡(luò)。
static void startNetwork(void)
{
blinkTimer.interval = BLINK_TIMER_INTERVAL;
blinkTimer.mode = TIMER_REPEAT_MODE;
blinkTimer.callback = blinkTimerFired;
HAL_StartAppTimer(&blinkTimer);
startNetworkReq.ZDO_StartNetworkConf = ZDO_StartNetworkConf;
ZDO_StartNetworkReq(&startNetworkReq);
}
定時(shí)器由static HAL_AppTimer_t blinkTimer;進(jìn)行聲明,當(dāng)時(shí)間到達(dá)時(shí),blinkTimerFired函數(shù)中對(duì)LED燈的狀態(tài)進(jìn)行改變。這樣就達(dá)到在搜尋網(wǎng)絡(luò)的過程中LED燈閃爍的效果,直到網(wǎng)絡(luò)加入成功。
下層關(guān)于加入網(wǎng)絡(luò)的回應(yīng)通過ZDO_StartNetworkConf回調(diào)函數(shù)完成。
static void ZDO_StartNetworkConf(ZDO_StartNetworkConf_t* conf)
{
HAL_StopAppTimer(&blinkTimer); //首先關(guān)掉定時(shí)器
if (ZDO_SUCCESS_STATUS == conf->status)//如果加入網(wǎng)絡(luò)成功
{
restartStateMachines(APP_JOINED_STATE);//設(shè)置網(wǎng)絡(luò)狀態(tài)為已加入
LED_Clear(APP_LED_0); //關(guān)閉LED網(wǎng)絡(luò)指示燈
&nbsnbsp; //注冊(cè)終端節(jié)點(diǎn)
registerControlEndpointReq.simpleDescriptor = &controlEndpoint;
registerControlEndpointReq.APS_DataInd = APS_DataIndControl;
APS_RegisterEndpointReq(®isterControlEndpointReq);
registerDataEndpointReq.simpleDescriptor = &dataEndpoint;
registerDataEndpointReq.APS_DataInd = APS_DataIndData;
APS_RegisterEndpointReq(®isterDataEndpointReq);
HAL_StartAppTimer(&nwkInfoTimer);
}
else
setState(APP_JOINING_STATE);
}
2.2 數(shù)據(jù)傳輸中網(wǎng)絡(luò)體現(xiàn)
2.2.1 網(wǎng)絡(luò)角色的設(shè)置
在所有ZigBee網(wǎng)絡(luò)中,共有三種網(wǎng)絡(luò)角色:協(xié)調(diào)器、路由設(shè)備、終端設(shè)備。
要想讓節(jié)點(diǎn)能夠加入到ZigBee網(wǎng)絡(luò)之中,在初始化網(wǎng)絡(luò)的時(shí)候需要給節(jié)點(diǎn)分配網(wǎng)絡(luò)角色。例如:
static void initNetwork(void)
{
DeviceType_t deviceType = DEVICE_TYPE_COORDINATOR;//設(shè)置為協(xié)調(diào)器
// DeviceType_t deviceType = APP_SENDER_DEVICE_TYPE ;
CS_WriteParameter(CS_DEVICE_TYPE_ID, &deviceType);
...
}
需要注意的是,在整個(gè)ZigBee網(wǎng)絡(luò)之中,有且只能有一個(gè)協(xié)調(diào)器用于創(chuàng)建一個(gè)ZigBee網(wǎng)絡(luò)。然后通過CS_WriteParameter(CS_DEVICE_TYPE_ID, &deviceType);將參數(shù)寫入文件。
2.2.2 網(wǎng)絡(luò)參數(shù)的使用
在進(jìn)行數(shù)據(jù)發(fā)送之前,首先要弄清楚的是被發(fā)送數(shù)據(jù)的目的網(wǎng)絡(luò)地址。在程序中通過
apsCommandReq.dstAddress.shortAddress = imageReceiving.currentRequest->address;進(jìn)行設(shè)置。然后通信雙方通過相同的endpoint進(jìn)行操作。