📄 znwk.h
字号:
#ifndef _ZNWK_H_
#define _ZNWK_H_
#include "zigbee.h"
#include "zMAC.h"
//randam value
#define RANDOM_LSB (TCNT1L)
#define RANDOM_MSB (TCNT1H)
//路由路径
/////////////////////////////////////////
#define MAX_NODE 5
typedef struct _PATH_INFO
{
BYTE src;
BYTE dst;
BYTE metric;
BYTE path[MAX_NODE-2];
}PATH_INFO;
extern PATH_INFO path_info[MAX_NODE];
PATH_INFO nwk_get_path_info(BYTE dst);
/////////////////////////////////////////
// NWK_FRAMECON_LSB
#define NWK_FRAME_TYPE_MASK (0x03)
#define NWK_FRAME_DATA (0x00)
#define NWK_FRAME_CMD (0x01)
#define NWKIsData() ((nwkCurrentFrame.frameCONLSB.Val & NWK_FRAME_TYPE_MASK) == NWK_FRAME_DATA )
#define NWKIsCommand() ((nwkCurrentFrame.frameCONLSB.Val & NWK_FRAME_TYPE_MASK) == NWK_FRAME_CMD)
#define NWK_PROT_VER_MASK (0x3C)
#define NWK_PROT_VER (0x01 << 2)
#define NWK_DISCOVER_ROUTE_MASK (1<<6)
// NWK_FRAMECON_MSB
#define NWK_SECURITY_MASK (0x02)
#define NWK_SECURITY (0x01 << 1)
// NWK task states
typedef enum _SM_NWK
{
SM_NWK_ENERGY_SCAN,
SM_NWK_ENERGY_SCAN_WAIT,
SM_NWK_ACTIVE_SCAN_WAIT,
SM_NWK_FORMED,
SM_NWK_NEXT_CHANNEL
} SM_NWK;
// NWK module flags.
typedef union _NWK_FLAGS
{
struct
{
unsigned int bIsNWKDiscovered:1;
} bits;
BYTE Val;
} NWK_FLAGS;
extern NWK_FLAGS _NWKFlags;
// Defs for use by stack upper layer only.
typedef union NWK_FRAMECON_LSB
{
struct
{
unsigned int type:2;
unsigned int version:4;
unsigned int discover:1;
unsigned int :1;
} bits;
BYTE Val;
} NWK_FRAMECON_LSB;
typedef union NWK_FRAMECON_MSB
{
struct
{
unsigned int :1;
unsigned int security:1;
unsigned int :6;
} bits;
BYTE Val;
} NWK_FRAMECON_MSB;
typedef struct _NWK_HEADER
{
NWK_FRAMECON_LSB frameCONLSB;
NWK_FRAMECON_MSB frameCONMSB;
SHORT_ADDR destAddr;
SHORT_ADDR srcAddr;
BYTE broadcastRadius;
BYTE broadcastSequence;
union
{
struct
{
unsigned int bIsInProcess:1;
} bits;
BYTE Val;
} Flags;
} NWK_HEADER;
extern NWK_HEADER nwkCurrentFrame;
extern BYTE* NWKPayload;
extern BYTE* NWKPayloadHead;
extern BYTE NWKPayloadLength;
extern BYTE NWKPayloadLengthSave;
#define NWK_ISR() MACISR()
#define NWKEnable() MACEnable()
#define NWKIsIdle() MACIsIdle()
#define NWKLeave() MACStartDisassociation()
#define NWKIsLeaveComplete() MACIsDisassociationComplete()
#define NWKUpdateAddressInfo() MACUpdateAddressInfo()
#define NWKPutLongAddress(x) MACPutLongAddress(x)
#define NWKPutShortAddress(x) MACPutShortAddress(x)
#define NWKIsAddressAssigned() MACIsAddressAssigned()
#define NWKFrameIsAcked(x) MACFrameIsAcked(x)
#define NWKFrameIsTimedOut(x) MACFrameIsTimedOut(x)
#define NWKFrameRemove(x) MACFrameRemove(x)
/*********************************************************************
* Function: void NWKInit(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
void NWKInit(void);
/*********************************************************************
* Function: BOOL NWKTask(void)
*
* PreCondition: None
*
* Input: TRUE if task has finished its task
* FALSE, if otherwise
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: This function must be called frequently to
* keep NWK layer going.
********************************************************************/
BOOL NWKTask(void);
/*********************************************************************
* Function: void NWKStartDiscovery(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Starts network discovery process
*
* Note: Available for coorindator only.
********************************************************************/
void NWKStartDiscovery(void);
/*********************************************************************
* Function: BOOL NWKIsDiscoveryComplete(void)
*
* PreCondition: NWKStartDiscovery() is called
*
* Input: None
*
* Output: TRUE, if complete
* FALSE otherwise
* Use GetZError() to check for error.
*
* Side Effects: None
*
* Overview: Performs network discovery process.
*
* Note: None
********************************************************************/
BOOL NWKIsDiscoveryComplete(void);
/*********************************************************************
* Function: BOOL NWKIsDiscovered(void)
*
* PreCondition: NWKStartDiscovery() is called
*
* Input: None
*
* Output: TRUE, if one network is discovered
* FALSE otherwise
* Use GetZError() to check for error.
*
* Side Effects: None
*
* Overview: Returns result of previously initiated NWKDiscovery
* process
*
* Note: None
********************************************************************/
#define NWKIsDiscovered() (_NWKFlags.bits.bIsNWKDiscovered)
#if defined(I_AM_COORDINATOR)
/*********************************************************************
* Function: void NWKCoordInit(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: As part of coordinator init procedure, this function
* initiates the discovery for another coordinator
* operating in this radio sphere.
*
* Note: None
********************************************************************/
#define NWKCoordInit() NWKStartDiscovery()
/*********************************************************************
* Function: BOOL NWKIsCoordInitComplete(void)
*
* PreCondition: NWKCoordInit() is already called
*
* Input: None
*
* Output: TRUE is Coordinator initialization is complete.
* FALSE, if otherwise
*
* Side Effects: None
*
* Overview: Performs coordinator initialization state machine
* tasks.
*
* Note: None
********************************************************************/
BOOL NWKIsCoordInitComplete(void);
/*********************************************************************
* Function: void NWKForm(void)
*
* PreCondition: NWKDiscovery is successfully completed.
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Selects PANID and establishes network
*
* Note: Available to coordinators only.
********************************************************************/
void NWKForm(void);
/*********************************************************************
* Macro: BOOL NWKIsFormed(void)
*
* PreCondition: NWKForm is successfully completed.
*
* Input: None
*
* Output: TRUE, if a network is established
* FALSE, otherwise
*
* Side Effects: None
*
* Overview: Performs NWK formation state machine
*
* Note: Available to coordinators only.
********************************************************************/
#define NWKIsFormed() MACIsNetworkEstablished()
/*********************************************************************
* Macro: void NWKPermitJoining(void)
*
* PreCondition: NWKIsFormed() = TRUE
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Permits new nodes to join with this network.
*
* Note: Available to coordinators only.
********************************************************************/
#define NWKPermitJoining() MACSetAssociationPermit(TRUE)
/*********************************************************************
* Macro: void NWKDisableJoining(void)
*
* PreCondition: NWKIsFormed() = TRUE
*
* Input: None
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -