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] 15、Zigbee協議棧應用(一)——Zigbee協議棧介紹及簡單例子(長文,OSAL及Zigbee入門知識)...協議單例
- zigbee協議的缺點 zigbee的主要應用領域協議
- FM-RDS工廠/鋼鐵廠無線廣播-秦皇島佰工鋼鐵有限公司無線智慧廣播應用分析
- 煤礦無線廣播系統在晉煤趙莊煤礦的應用
- 9、如何理解應用層、傳輸層、網路層、鏈路層、物理層
- Swift應用案例 1.無限輪播Swift
- Zigbee解析: NWK層作用
- 無線應用安全剖析
- 無線技術應用
- 【Zigbee精華教程01】Zigbee無線組網技術入門建議與學習路線
- TinyXML應用例子XML
- Android底層字元傳遞給上層應用舉例Android字元
- 神經網路前向和後向傳播推導(二):全連線層神經網路
- OC RunLoop應用例子OOP
- TabStrip應用例子 (轉)
- vlc的應用:用vlc做單播,組播及點播伺服器伺服器
- 傳輸層協議、應用層、socket套接字、半連結池協議
- 正向傳播和反向傳播反向傳播
- 邊境城市無線應急廣播系統方案設計概要
- Laravel 廣播系統例子Laravel
- 教你E103-W10串列埠轉WiFi模組的無線透傳應用教程串列埠WiFi
- 雙層無線接入網路滿足不同無線需求
- 和數傳媒:區塊鏈的未來在於應用層區塊鏈
- 針對工業IoT應用的主要無線要求
- 駭客仿冒ChatGPT應用程式,傳播Windows、Android惡意軟體ChatGPTWindowsAndroid
- 無線感測器網路ZigBee與Z-Wave的標準之爭
- 多卡聚合智慧融合通訊裝置在無人機無線視訊傳輸應用無人機
- 用MDA和傳統方法開發一個三層架構應用的對比架構
- PRECOMPUTE_SUBQUERY應用的一個例子
- Windbg在應用層除錯漏洞時的應用除錯
- 泛聯智慧無線感知網路的校園應用
- 應用層協議協議
- 來聊聊 應用層
- 『無為則無心』Python函式 — 39、Python中異常的傳播Python函式
- Shell指令碼應用兩個例子指令碼
- 離線簡訊應用無法正常讀取
- 無線網路應用配置六步走
- 瞭解安卓架構(linux核心層、系統執行庫層、應用框架層、應用層)安卓架構Linux框架