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

📄 协调器app文件.c

📁 包含两个终端设备、一个协调器设备和一个简单的上位机程序。 在freescake的codewarrior下编程
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Note that the entire buffer received OTA is sent to the UART, all at */
/* once, with no blocking or deblocking. This is simpler: one ZigBee */
/* buffer == one UART buffer. */
/* Return TRUE if successful. */
static bool_t SendZigBeeDataToUart(apsdeToAfMessage_t *pMsg) {
  zbApsdeDataIndication_t *pIndication;

  if (uartTxDataBuffer.status != mBufStatusFree_c) {
    return FALSE;
  }

  pIndication = &(pMsg->msgData.dataIndication);
  uartTxDataBuffer.status = mBufStatusReadyToSend_c;
  uartTxDataBuffer.len = pIndication->asduLength - sizeof(sequenceNumber_t);

  /* Don't send, or count, the sequence number we received. */
  FLib_MemCpy(uartTxDataBuffer.data.structured.payload,
              pIndication->pAsdu + sizeof(sequenceNumber_t),
              uartTxDataBuffer.len);

  TxUartData();
  return TRUE;
}                                       /* SendZigBeeDataToUart() */


/* Given a binary integer, set all of the LEDs to on or off accordingly. */
/* Does not handle toggling or flashing. Sets the actual LED state, not the */
/* shadow copy. */
static void SetAllLeds(index_t value) {
  ASL_SetLed(LED1, ((value & 0x01) ? gLedOn_c : gLedOff_c));
  ASL_SetLed(LED2, ((value & 0x02) ? gLedOn_c : gLedOff_c));
  ASL_SetLed(LED3, ((value & 0x04) ? gLedOn_c : gLedOff_c));
  ASL_SetLed(LED4, ((value & 0x08) ? gLedOn_c : gLedOff_c));
}                                       /* SetAllLeds() */

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

/* Change the shadow copy of the LED state. If an integer is being */
/* displayed, it takes priority, so this change will not be written to the */
/* LEDs yet. */
/* Otherwise, display the change on the LED. */
static void SetOneLed(LED_t ledNumber, LedState_t state) {
  index_t i;

  for (i = 0; i < NumberOfElements(maLedShadowStateTable); ++i) {
    if (maLedShadowStateTable[i].number == ledNumber) {
      maLedShadowStateTable[i].state = state;

      if (!mIsIntegerDisplayedOnLEDs) {
        ASL_SetLed(ledNumber, state);
      }
    }
  }
}                                       /* SetOneLed() */

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

/* Send the contents of the UART transmit buffer out via the UART. */
static void TxUartData(void) 
{



  if (uartTxDataBuffer.status != mBufStatusReadyToSend_c)
   {
    return;
   }

  /* Status will be set back to mBufStatusFree_c by UartTxCallBack(). */
  uartTxDataBuffer.status = mBufStatusBusy_c;

  if (!UartX_Transmit(uartTxDataBuffer.data.structured.payload,
                      uartTxDataBuffer.len,
                      UartTxCallBack)) 
  {

    /* The UART driver is out of buffer slots. Set an event to insure */
    /* this code gets run again next time the application task is called, */
    /* and give up for now. */
    uartTxDataBuffer.status = mBufStatusReadyToSend_c;
    TS_SendEvent(gAppTaskID, mAppRetryUartDataTx_c);
    return;
  }                                     /* if (!UartX_Transmit( ... */

  /* The send succeeded, or at least the UART driver accepted */
  /* the buffer to be sent. */
 // SetOneLed(LED4, gLedToggle_c);
}                                       /* TxUartData() */

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

/* Let the other side know that a datagram has been received. */
static void TxZigBeeAck(void) {
  afAddrInfo_t afAddrInfo;

  if (!mTxZigBeeThrottleFlag) {
    TS_SendEvent(gAppTaskID, mAppRetryZigBeeAckTx_c);
    return;
  }

  FLib_MemSet(&afAddrInfo, 0, sizeof(afAddrInfo));

  /* Don't care about afAddrInfo.dstAddr or afAddrInfo.dstEndPoint in */
  /* indirect mode. */
  /* For direct mode, dstAddr.aNwkAddr = 0 for Zc, 1 for Zr (temporary, */
  /* for testing). */
  /* afAddrInfo.dstAddr.aNwkAddr = 0; */
  /* afAddrInfo.dstEndPoint = 0; */

  afAddrInfo.aClusterId[0] = (mDataClusterId_c & 0xFF);
  afAddrInfo.aClusterId[1] = (mDataClusterId_c >> 8);
  afAddrInfo.srcEndPoint = appEndPoint;
  //afAddrInfo.dstAddrMode = gZbAddrModeIndirect_c;
  afAddrInfo.dstAddrMode = gZbAddrMode16Bit_c;
  
  switch(zbTxDataBuffer.data.structured.payload[1]) 
  {
    case 0xad:
    Copy2Bytes(afAddrInfo.dstAddr.aNwkAddr, bindfornwk[0]);break;
   
    case 0xbd: 
    Copy2Bytes(afAddrInfo.dstAddr.aNwkAddr, bindfornwk[1]);break;
    
    default:break;
  }

  afAddrInfo.dstEndPoint = gZbBroadcastEndPoint_c;
//Set2Bytes(addrInfo.aClusterId, gZclClusterIdentify_c);
//addrInfo.srcEndPoint = srcEndPoint;
  /* Turn on security and leave it on. Harmless In a non-secure network. */
  afAddrInfo.txOptions = 0;
  afAddrInfo.radiusCounter = afDefaultRadius_c;

  if (AF_DataRequest(&afAddrInfo,
                     sizeof(zbTxAckBuffer.data.raw),
                     zbTxAckBuffer.data.raw,
                     NULL) != gZbSuccess_c) {
    /* The send failed. Set an event to insure this code gets */
    /* run next time the application task is called, and give */
    /* up for now. */
    TS_SendEvent(gAppTaskID, mAppRetryZigBeeAckTx_c);
    return;
  }                                     /* if (AF_DataRequest( ... */



  /* Don't try to send too often. */
  mTxZigBeeThrottleFlag = FALSE;
  TMR_StartSingleShotTimer(mTxZigBeeThrottleTimerID,
                           mTxZigBeeThrottleDuration_c,
                           TxZigBeeThrottleTimerCallBack);
}                                       /* TxZigBeeAck() */

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

/* Send the contents of the ZigBee transmit buffer OTA to the remote node. */
void TxZigBeeData(void) {
  afAddrInfo_t afAddrInfo;
   //LED3TOGGLE;
  if (!mTxZigBeeThrottleFlag) {
    TS_SendEvent(gAppTaskID, mAppRetryZigBeeDataTx_c);
    return;
  }

  /* Don't need to be interrupted just to come back here again. */
  TMR_StopTimer(mTxZigBeeDataRetryTimerID);

  if (zbTxDataBuffer.status != mBufStatusReadyToSend_c) {
    return;
  }
  
  FLib_MemSet(&afAddrInfo, 0, sizeof(afAddrInfo));

  /* Don't care about afAddrInfo.dstAddr or afAddrInfo.dstEndPoint in */
  /* indirect mode. */
  /* For direct mode, dstAddr.aNwkAddr = 0 for Zc, 1 for Zr (temporary, */
  /* for testing). */
  /* afAddrInfo.dstAddr.aNwkAddr = 0; */

  afAddrInfo.aClusterId[0] = (mDataClusterId_c & 0xFF);
  afAddrInfo.aClusterId[1] = (mDataClusterId_c >> 8);
  afAddrInfo.srcEndPoint = appEndPoint;
  //afAddrInfo.dstAddrMode = gZbAddrModeIndirect_c;
  afAddrInfo.dstAddrMode = gZbAddrMode16Bit_c;
 // Copy2Bytes(afAddrInfo.dstAddr.aNwkAddr, anwkaddress);
   switch(zbTxDataBuffer.data.structured.payload[1]) 
  {
    case 0xad:
    Copy2Bytes(afAddrInfo.dstAddr.aNwkAddr, bindfornwk[1]);break;
   
    case 0xbd: 
    Copy2Bytes(afAddrInfo.dstAddr.aNwkAddr, bindfornwk[1]);break;
    
    default:break;
  }
  //gaBroadcastAddress);
  afAddrInfo.dstEndPoint = gZbBroadcastEndPoint_c;
//Set2Bytes(addrInfo.aClusterId, gZclClusterIdentify_c);
//addrInfo.srcEndPoint = srcEndPoint;
  /* Turn on security and leave it on. Harmless In a non-secure network. */
  afAddrInfo.txOptions = 0;
  afAddrInfo.radiusCounter = afDefaultRadius_c;

  /* The Wireless UART application on the remote node will ack or nak this */
  /* datagram (regardless of the reliability mode setting), so a confirm id */
  /* is not needed here. */
  AF_DataRequest(&afAddrInfo,
                     zbTxDataBuffer.len,
                     zbTxDataBuffer.data.raw,
                     NULL) ;
  
  /* The send succeeded, at least to the point where the local */
  /* stack has accepted the data to be transmitted. */
   
 
    zbTxDataBuffer.status = mBufStatusFree_c;
    zbTxDataBuffer.len = 0;
    mTxZigBeeThrottleFlag = FALSE;
    TMR_StartSingleShotTimer(mTxZigBeeThrottleTimerID,
                           mTxZigBeeThrottleDuration_c,
                           TxZigBeeThrottleTimerCallBack);
}                                       /* TxZigBeeData() */

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



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

static void TxZigBeeThrottleTimerCallBack(tmrTimerID_t timerID) 
{
  (void) timerID;
  mTxZigBeeThrottleFlag = TRUE;
}                                       /* TzZigBeeThrottleTimerCallBack() */

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

/* When a datagram is sent OTA in reliable mode, a timer is started. */
/* When an ack or nak is received for that datagram, the timer is stopped. */
/* If the timer expires, the remote node apparently has not received the */
/* datagram, so set an event for ourselves to send it again. */
static void TxZigBeeDataRetryTimerCallBack(tmrTimerID_t timerID) {
  (void) timerID;
  zbTxDataBuffer.status = mBufStatusReadyToSend_c;
  TS_SendEvent(gAppTaskID, mAppRetryZigBeeDataTx_c);
}   


                                    /* TxZigBeeDataRetryTimerCallBack() */
                                    
                                    
                                    
 static void heartbeatTimerCallBack(tmrTimerID_t timerID) 
{
  /*(void) timerID;
  uartTxDataBuffer.status = mBufStatusReadyToSend_c;
   uartTxDataBuffer.len = 5;

    FLib_MemCpy(uartTxDataBuffer.data.structured.payload,
             heartbeat,
             uartTxDataBuffer.len);
      
   TxUartData();     */
   (void) timerID;
    //SIMRS=0xff;
     // LED5TOGGLE;
    //  LED3TOGGLE;
} 

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

/* A byte is available from the serial port. */
static void UartRxCallBack(void) {
  TS_SendEvent(gAppTaskID, mAppRxFromUart_c);
}                                       /* UartRxCallBack() */

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

/* The last byte received from the UART was long enough ago that it is now */
/* time to send any outstanding data, even if the buffer(s) are not full. */
static void UartRxTimeout(tmrTimerID_t timerID) {
  (void) timerID;

  if (mInLoopBackMode) {
    if (uartTxDataBuffer.status != mBufStatusInProgress_c) {
      return;
    }

    uartTxDataBuffer.status = mBufStatusReadyToSend_c;
    TxUartData();
  } else {
    if (zbTxDataBuffer.status != mBufStatusInProgress_c) {
      return;
    }

    zbTxDataBuffer.status = mBufStatusReadyToSend_c;
    zbTxDataBuffer.data.structured.sequenceNumber = curZbTxSequenceNumber++;
    ++zbTxDataBuffer.len;                 /* Count the sequence number. */
    TxZigBeeData();
  }
}                                       /* UartRxTimeout() */

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

/* The UART has finished transmitting a buffer out through the serial port. */
/* pBuf is the start of the data that the UART driver sent, not the address */
/* of the buffer_t that contains it. */
static void UartTxCallBack(unsigned char const *pBuf) {
  (void) pBuf;
  uartTxDataBuffer.status = mBufStatusFree_c;
  uartTxDataBuffer.len = 0;
}                                       /* UartTxCallBack() */

⌨️ 快捷键说明

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