⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 协调器app文件.c

📁 包含两个终端设备、一个协调器设备和一个简单的上位机程序。 在freescake的codewarrior下编程
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************
* BeeApp.c
*
* 
*
*****************************************************************************/

#include "BeeStack_Globals.h"
#include "BeeStackConfiguration.h"
#include "BeeStackParameters.h"
#include "AppZdoInterface.h"
#include "TS_Interface.h"
#include "TMR_Interface.h"
#include "AppAfInterface.h"
#include "FunctionLib.h"
#include "PublicConst.h"
#include "keyboard.h"
#include "Display.h"
#include "led.h"
#include "EndPointConfig.h"
#include "BeeApp.h"
#include "BeeAppInit.h"
#include "NVM_Interface.h"
#include "BeeStackInterface.h"
#include "HaProfile.h"
#include "ZdpManager.h"
#include "ASL_ZdpInterface.h"
#include "ASL_UserInterface.h"
#include "ASL_ZCLInterface.h"
#include "UART_Interface.h"
#include "ZDOStateMachineHandler.h"
#include "ZclGeneral.h"


/******************************************************************************
*******************************************************************************
* Public memory declarations
*******************************************************************************
******************************************************************************/

extern anchor_t gAppDataConfirmQueue;
extern anchor_t gAppDataIndicationQueue;

/******************************************************************************
*******************************************************************************
* Private macros
*******************************************************************************
******************************************************************************/

#ifndef NumberOfElements
/* Number of elements in an array. */
#define NumberOfElements(array)     ((sizeof(array) / (sizeof(array[0]))))
#endif

/* Maximum size of a datagram sent from one side of the wireless uart to the */
/* other side. */
//#define mMaxDatagramLen_c   ApsmeGetMaxAsduLength(0)
#define mMaxDatagramLen_c  9

#define mDataClusterId_c    0x1200      /* Data. */
#define mAckClusterId_c     0x1201      /* Acknowledgements. */

/* Duration of the baud rate display. */
#define mBaudRateDisplayDuration_c          TmrMilliseconds(2000)

/* When a datagram is sent OTA, there's a chance that the destination may */
/* never respond. In reliable mode, a timer is used to notice the lack of */
/* response and to retry. It may or may not make sense to limit the number */
/* of retries. For now, keep it simple, and retry forever. */
/* Since polling end devices may be on a 4 second cycle, set the timer to */
/* something longer than 4 seconds. */
#define mTxZigBeeDataRetryTimerDuration_c  TmrMilliseconds(1000)
//TmrSeconds(5)

/* In block mode, the last block will usually not be a full datagram. */
/* Whenever a byte is received from the UART, a timer is started. */
/* If it expires before it is reset by the arrival of another byte, the */
/* datagram is sent, even if it is not full. Note that this must be long */
/* enough to allow for a full byte transmission time at the slowest baud */
/* rate. */
#define mUartRxTimeout_c                    TmrMilliseconds(10)
//#define mUartRxTimeout_c                    TmrSeconds(3)

#define mTxZigBeeThrottleDuration_c         TmrMilliseconds(12)

/******************************************************************************
*******************************************************************************
* Private type definitions
*******************************************************************************
******************************************************************************/

/* Events for the user application, to be used for the BeeAppUpdateDevice */
enum {
	gOnEvent_c,
	gOffEvent_c,
	gToggleEvent_c
};

/* Data sent from one side ot the wireless uart to the other side. */
typedef uint8_t sequenceNumber_t;

typedef struct structuredDatagram_tag {
  sequenceNumber_t sequenceNumber;
  unsigned char payload[mMaxDatagramLen_c - sizeof(sequenceNumber_t)];
} structuredDatagram_t;

/* A full sized datagram. */
typedef union datagram_tag {
  unsigned char raw[sizeof(structuredDatagram_t)];
  structuredDatagram_t structured;
} datagram_t;

typedef enum {
  mBufStatusFree_c = 0,                 /* Not being used. Must be == 0. */
  mBufStatusInProgress_c,               /* Being filled/emptied. */
  mBufStatusReadyToSend_c,
  mBufStatusBusy_c                      /* Waiting for an Rx/Tx to complete. */
} bufferStatus_t;

typedef struct buffer_tag {
  bufferStatus_t status;
  index_t len;
  datagram_t data;
} buffer_t;

typedef struct structuredAck_tag {
  sequenceNumber_t sequenceNumber;
  bool_t ack;                           /* T = ack, F = nak. */
} structuredAck_t;

/* A very small datagram, used only for ACKs. */
typedef union ackDatagram_tag {
  unsigned char raw[sizeof(structuredAck_t)];
  structuredAck_t structured;
} ackDatagram_t;

/* ACKs are small, and fixed size. */
typedef struct ackBuffer_tag {
  bufferStatus_t status;
  ackDatagram_t data;
} ackBuffer_t;

/******************************************************************************
*******************************************************************************
* Private memory declarations
*******************************************************************************
******************************************************************************/



/**************************************/

/* Timer IDs. */

static tmrTimerID_t mUartRxTimerID;
static tmrTimerID_t mBaudRateDisplayTimerID;
static tmrTimerID_t mTxZigBeeDataRetryTimerID;
static tmrTimerID_t mTxZigBeeThrottleTimerID;
static tmrTimerID_t heartbeatTimerID;


/* This flag will be TRUE when it has been at least a minimum time since the */
/* last ZigBee Tx. */
static bool_t mTxZigBeeThrottleFlag = TRUE;

/**************************************/
 static uint8_t  queryflag=0x55;





/* Buffers. There are four data transfer points: to and from the UART, and */
/* to and from the remote node via ZigBee. The UART driver already has a */
/* buffer for data received from the serial port, so we only need buffers */
/* for three of the four transfer types. */

/* Data to be sent to the remote ZigBee node. */
static sequenceNumber_t curZbTxSequenceNumber = 0;
static buffer_t zbTxDataBuffer;

/* ACK or NAK to be sent to the remote ZigBee node. */
static ackBuffer_t zbTxAckBuffer;

/* Data to be sent to the UART. */
static buffer_t uartTxDataBuffer;


 
     zbNwkAddr_t  aDestAddress = {0x00,0x00};
 
     zbNwkAddr_t  anwkaddress;
  
     uint8_t  nwkaddress[2];
      
     zbNwkAddr_t bindfornwk[2];
 
  static  uint8_t test1=0;
  static  uint8_t test2=0;
  
   uint8_t retrycounter=0;
/**************************************/

/* There are three different mode settings:
 *      char vs. block mode,
 *      reliable vs. unreliable mode, and
 *      loopback mode.
 *
 * Char mode sends data received from the serial port immediately, without
 * waiting to accumulate a full datagram. Char mode is intended mostly for
 * keyboard repeaters.
 *
 * Block mode sends full datagrams, with a timeout to send a final, partial
 * datagram. Block mode is intended for file transfers.
 *
 * Reliable mode checks for ACKs from the remote node, and resends datagrams
 * if necessary. Reliable mode is intended for any data transfer where
 * reliablity is important.
 *
 * Unreliable mode ignores ACKs from the remote node, and never resends
 * datagrams. Unreliable mode is intended for data transfers like encoded
 * voice, where it is better to drop data than to delay it with retries.
 *
 * LoopBack mode sends data received from the UART back out the UART, and
 * sends data received OTA back out OTA.
 */

static bool_t mInCharMode     = FALSE;       /* T = char mode, F = block mode. */
static bool_t mInReliableMode = FALSE;   /* T = reliable, F = unreliable. */
static bool_t mInLoopBackMode = FALSE;  /* T = loopback mode, F otherwise. */

/**************************************/

/* Table of supported baud rates. The default is maBaudRateTable[0]. */
static index_t mCurBaudRateIndex = 5;
static UartBaudRate_t const maBaudRateTable[] = {
  gUARTBaudRate1200_c,                  /* 0 */
  gUARTBaudRate2400_c,                  /* 1 */
  gUARTBaudRate4800_c,                  /* 2 */
  gUARTBaudRate9600_c,                  /* 3 */
  gUARTBaudRate19200_c,                 /* 4 */
  gUARTBaudRate38400_c                  /* 5 */
};

/**************************************/

/* The UI sometimes overrides the normal LED display with another value for */
/* a short time, and then returns to the previous display. The normal state */
/* of the LEDs is kept in shadow registers. These can be changed at any time, */
/* even if when the normal display is overridden, without losing their state. */
static bool_t mIsIntegerDisplayedOnLEDs = FALSE;
static struct ledState_tag {
  LED_t number;
  LedState_t state;
} maLedShadowStateTable[] = {
  {LED1, gLedOff_c},
  {LED2, gLedOff_c},
  {LED3, gLedOff_c},
  {LED4, gLedOff_c},
};

/******************************************************************************
*******************************************************************************
* Private prototypes
*******************************************************************************
******************************************************************************/

/* ProcessConfirmData() is only needed if if APS confirms are used. */
static void ProcessConfirmData(void);

static void DisplayLedInteger(index_t value);
static void FlushAllBuffers(void);
static void RestoreLedShadowState(tmrTimerID_t timerID);
static void RxUartData(void);
static void RxZigBeeData(void);
static void SetAllLeds(index_t value);
static void SetOneLed(LED_t ledNumber, LedState_t state);
static bool_t SendUartDataToUart(unsigned char byte);
static bool_t SendUartDataToZigBee(unsigned char byte);
static bool_t SendZigBeeDataToUart(apsdeToAfMessage_t *pMsg);
static bool_t SendZigBeeDataToZigBee(apsdeToAfMessage_t *pMsg);
static void UartRxCallBack(void);
static void UartRxTimeout(tmrTimerID_t timerID);
static void UartTxCallBack(unsigned char const *pBuf);
static void TxUartData(void);
static void TxZigBeeAck(void);
static void TxZigBeeData(void);
static void TxZigBeeDataRetryTimerCallBack(tmrTimerID_t timerID);
static void TxZigBeeThrottleTimerCallBack(tmrTimerID_t timerID);
static void heartbeatTimerCallBack(tmrTimerID_t timerID);



/******************************************************************************
*******************************************************************************
* Public memory definitions
*******************************************************************************
******************************************************************************/

zbEndPoint_t appEndPoint;

/* This data set contains app layer variables to be preserved across resets */
NvDataItemDescription_t const gaNvAppDataSet[] = {
  {&gBeeStackParameters,    sizeof(beeStackParameters_t)},  /* BeeStackSpecific */
  {&gZclCommonAttr,         sizeof(zclCommonAttr_t)},       /* scenes, location, etc... */
  {&gAslData,               sizeof(ASL_Data_t)},            /* state of ASL */
  /* insert any user data for NVM here.... */
  {NULL, 0}       /* Required end-of-table marker. */
};

/******************************************************************************
*******************************************************************************
* Public functions
*******************************************************************************
******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -