1. 程式人生 > >zigbee無線傳播的例子 應用層

zigbee無線傳播的例子 應用層

為了做一個課題,不得已要使用上zigbee 的技術上去了,這次是有點急功近利,沒有把cc2530板上面的實驗全部做完,就開始做zigbee 上面的實驗,剛開始的時候看的也是雲裡來霧裡去的,但是研究一段時間後,也慢慢的有了一些感悟,我就把我的寫到了程式碼中去了。一個小例子。

#include "OSAL.h"
#include "ZGlobals.h"
#include "AF.h"
#include "aps_groups.h"
#include "ZDApp.h"

#include "SampleApp.h"
#include "SampleAppHw.h"

#include "OnBoard.h"
/* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" /********************************************************************* * MACROS */ /********************************************************************* * CONSTANTS */ /********************************************************************* * TYPEDEFS */
/********************************************************************* * GLOBAL VARIABLES */ uint8 LedState = 0; uint8 AppTitle[] = "ALD2530 Group"; //應用程式名稱 // This list should be filled with Application specific Cluster IDs. const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] = { SAMPLEAPP_PERIODIC_CLUSTERID, SAMPLEAPP_FLASH_CLUSTERID }; //簡單的描述
const SimpleDescriptionFormat_t SampleApp_SimpleDesc = { SAMPLEAPP_ENDPOINT, // int Endpoint; SAMPLEAPP_PROFID, // uint16 AppProfId[2]; SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2]; SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4; SAMPLEAPP_FLAGS, // int AppFlags:4; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList, // uint8 *pAppInClusterList; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList // uint8 *pAppInClusterList; }; // This is the Endpoint/Interface description. It is defined here, but // filled-in in SampleApp_Init(). Another way to go would be to fill // in the structure here and make it a "const" (in code space). The // way it's defined in this sample app it is define in RAM. endPointDesc_t SampleApp_epDesc; /********************************************************************* * EXTERNAL VARIABLES */ /********************************************************************* * EXTERNAL FUNCTIONS */ /********************************************************************* * LOCAL VARIABLES */ uint8 SampleApp_TaskID; // Task ID for internal task/event processing // This variable will be received when // SampleApp_Init() is called. devStates_t SampleApp_NwkState; uint8 SampleApp_TransID; // This is the unique message ID (counter) afAddrType_t SampleApp_Periodic_DstAddr; afAddrType_t SampleApp_Flash_DstAddr; aps_Group_t SampleApp_Group; uint8 SampleAppPeriodicCounter = 0; uint8 SampleAppFlashCounter = 0; /********************************************************************* * LOCAL FUNCTIONS */ //按鍵處理 void SampleApp_HandleKeys( uint8 shift, uint8 keys ); //訊息處理 void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); //週期 void SampleApp_SendPeriodicMessage( void ); void SampleApp_SendFlashMessage( uint16 flashTime ); /********************************************************************* * NETWORK LAYER CALLBACKS */ /********************************************************************* * PUBLIC FUNCTIONS */ /********************************************************************* * @fn SampleApp_Init * * @brief Initialization function for the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; //初始化 網路的狀態為初始化 //為後面的網路改變的時候 做出迴應 SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; // Device hardware initialization can be added here or in main() (Zmain.c). // If the hardware is application specific - add it here. // If the hardware is other parts of the device add it in main(). //判斷該裝置是協調器 還是 路由器 #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, // the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to // start the device. ZDOInitDevice(0); #endif // Setup for the periodic message's destination address //設定傳送資料的方式和目的地址定址模式 // Broadcast to everyone //設定 週期傳送的地址 //傳送模式為廣播模式 SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast; //端點 指定端點號 SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //地址 指定目的網路地址為廣播地址 SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; // Setup for the flash command's destination address - Group 1 SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; //組定址 SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;//指定端點號 SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; //組號1 //SampleApp_Flash_DstAddr.addr.shortAddr = 0x0002; //組號2 // Fill out the endpoint description. 定義本裝置用來通訊的APS層端點描述符 SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;//端點號 SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;//簡單描述 SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); // Register for all key events - This app will handle all key events RegisterForKeys( SampleApp_TaskID ); // By default, all devices start out in Group 1 SampleApp_Group.ID = 0x0001;//組號 //SampleApp_Group.ID = 0x0002; osal_memcpy( SampleApp_Group.name, "Group 1", 7 );//設定為組名 aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); #endif } /********************************************************************* * @fn SampleApp_ProcessEvent * * @brief Generic Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return none 處理事務的東西 */ uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; (void)task_id; // Intentionally unreferenced parameter //得到屬於本系統的訊號 if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { // Received when a key is pressed //當按鍵觸發的時候 case KEY_CHANGE: SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; // Received when a messages is received (OTA) for this endpoint case AF_INCOMING_MSG_CMD: SampleApp_MessageMSGCB( MSGpkt ); break; // Received whenever the device changes state in the network //當網路的狀態發生變化的時候 case ZDO_STATE_CHANGE: SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if ( (SampleApp_NwkState == DEV_ZB_COORD) || (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) ) { // Start sending the periodic message in a regular interval. //osal_start_timerEx( SampleApp_TaskID, // SAMPLEAPP_SEND_PERIODIC_MSG_EVT, // SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); } else { // Device is no longer in the network } break; default: break; } // Release the memory osal_msg_deallocate( (uint8 *)MSGpkt ); // Next - if one is available MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } // Send a message out - This event is generated by a timer // (setup in SampleApp_Init()). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) { // Send the periodic message SampleApp_SendPeriodicMessage(); // Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); // return unprocessed events return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); } // Discard unknown events return 0; } /********************************************************************* * Event Generation Functions */ /********************************************************************* * @fn SampleApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ) { (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_6 ) { #if defined(ZDO_COORDINATOR) //協調器進行傳送 資料 SampleApp_SendFlashMessage(1); #else //路由器和終端才傳送資料 // SampleApp_SendFlashMessage(0); //以組播方式發資料 #endif } if ( keys & HAL_KEY_SW_1 ) { /* The Flashr Command is sent to Group 1. * This key toggles this device in and out of group 1. * If this device doesn't belong to group 1, this application * will not receive the Flash command sent to group 1. */ aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); if ( grp ) { // Remove from the group aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); } else { // Add to the flash group aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); } } } /********************************************************************* * LOCAL FUNCTIONS */ /********************************************************************* * @fn SampleApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return non 接受到請求然後進行處理請求 */ void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint8 data; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID: break; //快速的一個 case SAMPLEAPP_FLASH_CLUSTERID: //得到資料 data = (uint8)pkt->cmd.Data[0]; //得到的資料如果為0 則燈光置為滅 //如果得到的資料為其他 Led2 亮 if(data == 0) HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); else HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); break; } } /********************************************************************* * @fn SampleApp_SendPeriodicMessage * * @brief Send the periodic message. * * @param none * * @return none */ void SampleApp_SendPeriodicMessage( void ) { if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 1, (uint8*)&SampleAppPeriodicCounter, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { // Error occurred in request to send. } } /********************************************************************* * @fn SampleApp_SendFlashMessage * * @brief Send the flash message to group 1. * * @param flashTime - in milliseconds * * @return none */ void SampleApp_SendFlashMessage( uint16 flashTime ) { LedState = ~LedState; //傳送了Led狀態 if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,//傳送目的地址+端點地址和傳送模式 SAMPLEAPP_FLASH_CLUSTERID,//源(答覆或確認)終端的描述(比如作業系統中任務ID等)源EP 1, // 傳送資料長度 &LedState,// 傳送資料緩衝區 &SampleApp_TransID, // 任務ID號 AF_DISCV_ROUTE, // 有效位掩碼的傳送選項 AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )//傳送跳數,通常設定為AF_DEFAULT_RADIUS { if(LedState == 0) HalLedSet(HAL_LED_1, HAL_LED_MODE_ON); else HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF); } else { // Error occurred in request to send. } }

相關推薦

zigbee無線傳播例子 應用

為了做一個課題,不得已要使用上zigbee 的技術上去了,這次是有點急功近利,沒有把cc2530板上面的實驗全部做完,就開始做zigbee 上面的實驗,剛開始的時候看的也是雲裡來霧裡去的,但是研究一段時間後,也慢慢的有了一些感悟,我就把我的寫到了程式碼中去了。一

淺析ZigBee應用開發之終端結點

終端節點在zigbee的開發中,主要有兩個功能 <1>將採集到的資訊傳送給協調器(coordinator) <2>接受協調器傳送過來的命令,並執行 以sample app為例,簡單的廣播方式傳送資訊為例 當執行程式,作業系統啟動,註冊好應

應用拒絕服務攻擊

數據庫 coo bsp 源地址 http post 方法 fields 實現 exp DOS:Denial Of Service DDOS:Distributed Denial Of Service(利用僵屍網絡——由“肉雞”組成,發起 DoS 攻擊) 常見的 DDOS 攻

應用協議及ip地址劃分

ip地址 技術 log 1-1 應用 blog -1 應用層協議 cnblogs 1、應用層協議 2、ip地址 3、子網劃分及超網合並 應用層協議及ip地址劃分

TCP-IP之應用協議

傳輸協議 images gateway 1-1 大學 表示 進制 技術 dom 應用層協議是多種多樣的,比如 DNS、FTP、Telnet、SMTP、HTTP、RIP、NFS 一、DNS DNS (Domain Name Service 域名服務) 協議基於 UDP,使用

Linux 網卡驅動學習(六)(應用、tcp 、ip 、設備和驅動作用解析)

local acc 每次 letter auto sizeof style article inode 本文將介紹網絡連接建立的過程、收發包流程,以及當中應用層、tcp層、ip層、設備層和驅動層各層發揮的作用。 1、應用層 對於使用socket進行網絡連接的serv

計算機網絡之應用_part -1

之間 使用 程序 對象 請求 spa enter 應用程序 iptv 應用層協議原理 一、網絡應用程序體系結構   網絡應用程序體系結構是由程序研發者設計的,規定了如何在各種端系統中組織該應用程序,主要流行的有兩種: 1.客戶--服務器體系結構:   有一個總是打開的主機(

計算機網絡之應用_part -3

目錄樹 align 傳輸協議 之間 服務器 徘徊 本地 文件 系統 應用層協議之文件傳輸協議:FTP   在一個典型的FTP會話中,用戶坐在一臺主機(本地主機)前面,向一臺遠程主機傳輸(或者接受來自遠程主機的)文件。為使用戶能訪問它的遠程賬戶,用戶必須提供一個用戶標識和口令

第9章 應用(4)_超文本傳輸協議HTTP

span 關閉連接 多圖 帳戶 通過 從服務器 -668 傳輸協議 分享 5. 超文本傳輸協議HTTP 5.1 統一資源定位符URL (1)URL的一般形式:<協議>://<主機>:<端口>/<路徑>   ①協議後面必須寫上“

第9章 應用(5)_文件傳輸協議FTP

public 被動模式 更多 保留 允許 服務器端 磁盤 stat 命令 6. 文件傳輸協議FTP 6.1 FTP主動和被動模式 (1)FTP協議   ①與其他協議不同,FTP協議在客戶端訪問FTP服務器時需要建立兩個TCP連接。一個用來傳輸FTP命令,一個用來傳輸數據。

計算機網絡 chapter 6 應用

檢索 而不是 撰寫 cnblogs 計算 chapter 數據庫系統 通信 過多 第六章 應用層 每個應用層協議都是為了解決某一類應用問題,而問題的解決又往往是通過位於不同主機中的多個應用進程之間的通信和協同工作來完成的。應用層的具體內容就是規定應用進程在通信時所遵循的協議

Zigbee技術入門教程-01】Zigbee無線組網技術入門的學習路線

自組織 問題 作者 項目 建築 企業 color 基本概念 基礎 【Zigbee技術入門教程-01】Zigbee無線組網技術入門的學習路線 廣東職業技術學院 歐浩源 一、引言 在物聯網技術應用的知識體系中,Zigbee無線組網技術是非常重要的一環,也是大家感覺比較難

計算機網絡【八】:應用 【轉】

tac 都是 文件共享 編寫 .net pos 遠程 等待 src 轉自:http://blog.chinaunix.net/uid-26275986-id-4110819.html 今天我們來快速地瀏覽一下傳輸層之上的應用層所使用的協議,下面將簡要地列出應用

android4.2 telephone應用分析

popup 控件 set strong post ndis nag tracking ldb InCallScreen.java 復制通話界面的顯示和提供人機交互的接口。進入該界面第一個調用ONCreate。否則進入OnNewIntent,該配置文件是inc

應用/安全/傳輸如何進行協議選型?

c++ 應用層 什麽 保密 技術人 ccf ima 詳情 server 系統設計,協議先行。 大部分技術人沒有接觸協議的設計細節,更多的是使用已有協議進行應用層的編碼,例如: (1)使用http作為載體,設計get/post/cookie參數 (2)使用dubbo框架,而不

qt的應用主要是大型3d,vr,管理軟件和器械嵌入軟件(有上千個下一代軟件黑科技項目是qt的,美國宇航局,歐洲宇航局,超級戰艦DDG1000)

高級 研發 eclips 人在 但是 tel engine ack 運行 作者:Nebula.Trek鏈接:https://www.zhihu.com/question/24316868/answer/118944490來源:知乎著作權歸作者所有。商業轉載請聯系作者獲得授權

CDD應用架構學習總結

.cn src width hand sta ray del mrp 分享 怎麽樣用context,把數據、view和業務串起來的? 例如:聊天頁面,輸入框view產生的“hello”文本,直接通過context傳遞到BusinessObject進行處理,生成的新消息mes

計算機網絡基礎(五)——傳輸應用

協議 傳輸 應用層 一、傳輸層傳輸層的作用傳輸層提供端到端的連接(IP層提供點到點的連接),為應用層的數據的傳輸進行保駕護航。傳輸層的協議--TCP(Transmission Control Protocol) 傳輸控制協議。該協議是可靠的、面向連接的協議,可以實現長時間的、穩定的傳輸大量數據。

Netfilter/Iptables Layer7 應用過濾策略部署

netfilter/iptables layer7 應用層過濾策略部署Netfilter/Iptables Layer7 應用層過濾策略部署環境:內核版本:Linux version 2.6.32-431.el6.x86_64 iptables版本:iptables v1.4.7 gcc版本:

實驗八 應用協議Ⅱ-FTP協議分析

erl .cn 圖片 ftp連接 ext es2017 用戶 ftp服務 發送 實驗八 應用層協議Ⅱ-FTP協議分析 一、實驗目的 1.掌握FTP協議的實現原理。 2.了解控制通道和數據通道。 二、實驗內容 用WareShark