当前位置: > 华清远见教育科技集团 > 嵌入式学习 > 讲师博文 > BitCloud协议解析之——网络
BitCloud协议解析之——网络
时间:2016-12-12作者:华清远见

1.1 网络建立与加入

(1)首先在Main函数中,会有这样的语句:

for(;;)
        {
                SYS_RunTask();
        }

通过SYS_RunTask函数,系统会调用BitCloud协议栈中的用户应用程序,即APL_TaskHandler()。

(2)
        void APL_TaskHandler(void)
        {
                if (appStatePosted)
                {
                        appStatePosted = false;
                        appTaskHandler();
                }
                if (appSubStatePosted)
                {
                        appSubStatePosted = false;
                        appSubtaskHandler();
                }
        }

(3)按照流程程序调用appTaskHandler();该函数中对各种状态进行判断执行相应操作。

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)硬件初始化函数

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)网络初始化

static void initNetwork(void)
        {
                CS_WriteParameter(CS_DEVICE_TYPE_ID, &deviceType);//将设备在网络中的角色配置为终端设备。
                if (DEV_TYPE_ENDDEVICE == deviceType)
                        {
                                bool rxOnWhenIdleFlag = false;
                                CS_WriteParameter(CS_RX_ON_WHEN_IDLE_ID, &rxOnWhenIdleFlag);
                        }
        }

// 命令请求参数的配置

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;

// 数据块请求的配置


                  

然后调用startNerwork()启动搜寻网络。

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);
        }

定时器由static HAL_AppTimer_t blinkTimer;进行声明,当时间到达时,blinkTimerFired函数中对LED灯的状态进行改变。这样就达到在搜寻网络的过程中LED灯闪烁的效果,直到网络加入成功。

下层关于加入网络的回应通过ZDO_StartNetworkConf回调函数完成。

static void ZDO_StartNetworkConf(ZDO_StartNetworkConf_t* conf)
        {
                HAL_StopAppTimer(&blinkTimer); //首先关掉定时器
                if (ZDO_SUCCESS_STATUS == conf->status)//如果加入网络成功
                        {
                                restartStateMachines(APP_JOINED_STATE);//设置网络状态为已加入
                                LED_Clear(APP_LED_0); //关闭LED网络指示灯
  &nbsnbsp;                             //注册终端节点
                                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 数据传输中网络体现

2.2.1 网络角色的设置

在所有ZigBee网络中,共有三种网络角色:协调器、路由设备、终端设备。

要想让节点能够加入到ZigBee网络之中,在初始化网络的时候需要给节点分配网络角色。例如:

static void initNetwork(void)
        {
                DeviceType_t deviceType = DEVICE_TYPE_COORDINATOR;//设置为协调器
                // DeviceType_t deviceType = APP_SENDER_DEVICE_TYPE ;
                CS_WriteParameter(CS_DEVICE_TYPE_ID, &deviceType);
                ...
        }

需要注意的是,在整个ZigBee网络之中,有且只能有一个协调器用于创建一个ZigBee网络。然后通过CS_WriteParameter(CS_DEVICE_TYPE_ID, &deviceType);将参数写入文件。

2.2.2 网络参数的使用

在进行数据发送之前,首先要弄清楚的是被发送数据的目的网络地址。在程序中通过 apsCommandReq.dstAddress.shortAddress = imageReceiving.currentRequest->address;进行设置。然后通信双方通过相同的endpoint进行操作。

发表评论
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)