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

📄 aps.c

📁 zigbee在cc2430下的演示程序。
💻 C
📖 第 1 页 / 共 3 页
字号:
  case APS_STATE_ZEP_WAIT_RXSTART:
		  if (zepBusy()) break;
                  //start the zepFSM RX
                  zepState = ZEP_STATE_RX_START;
                  apsState = APS_STATE_ZEP_WAIT_RXFINISH;
                  goto apsFSM_start2;



  case APS_STATE_ZEP_WAIT_RXFINISH:
		  if (zepBusy()) break;
                  //finished.
                  //indicate RX is over, release APS RX FSM
                  aps_pib.flags.bits.zepRxInProgress = 0;
                  //release the APS FSM also
                  apsState = APS_STATE_IDLE;
                  break;

#ifdef LRWPAN_COORDINATOR
	 case APS_STATE_INJECT_INDIRECT:
           //wait for RX to become idle
           if (apsRxState != APS_RXSTATE_IDLE) break;
           //inject packet into RX FSM
           if (apsRxBuffFull()) {
             //will not be able to copy buffer into indirect space
             a_aps_service.status = LRWPAN_STATUS_INDIRECT_BUFFER_FULL;
           }else {
             apsInjectIndirectPacket();
           }
		   aps_pib.flags.bits.IsUsrBufferFree = 1;
           apsState = APS_STATE_IDLE;
           goto apsFSM_start;

#endif

#ifdef LRWPAN_COORDINATOR
	 case APS_STATE_INDIRECT_GETDST:
		   rxPtr = &aps_pib.rxBuff[aps_pib.rxTail];
           //get the next bind destination for this src endpoint, cluster
           if (!evbResolveBind(&rxPtr->dstEP, &rxPtr->dstSADDR)) {
             //at this point, we have finished with the indirect transmit.
             //lets free the original packet, and continue
             apsFreeRxPacket(TRUE);
             apsState = APS_STATE_IDLE;
           } else {
             //we have destination
             //see if we are sending to ourself
             if (rxPtr->dstSADDR == macGetShortAddr()) {
               apsState = APS_STATE_INDIRECT_LOOPBACK;
             }else {
               apsState = APS_STATE_INDIRECT_TX;
             }
             goto apsFSM_start;
           }
           break;

	 case APS_STATE_INDIRECT_LOOPBACK:
           if (apsRxBusy()) break;  //wait until RX buffer free
           //copy to RX buffer
		   rxPtr = &aps_pib.rxBuff[aps_pib.rxTail];
           halUtilMemCopy((BYTE *)&a_aps_rx_data,(BYTE *)rxPtr, sizeof(APS_RX_DATA));
           //do user callback
           usrRxPacketCallback();
           apsState = APS_STATE_INDIRECT_GETDST;
           goto apsFSM_start;


  case APS_STATE_INDIRECT_TX:
    //have a destination for our indirect TX. Lets do it.
    if (phyTxLocked()) break;
    phyGrabTxLock();
	rxPtr = &aps_pib.rxBuff[aps_pib.rxTail];
	a_aps_tx_data.aps_fcf = rxPtr->aps_fcf;
    a_aps_tx_data.tsn = rxPtr->tsn;
    a_aps_tx_data.af_fcf = rxPtr->af_fcf;
    a_aps_tx_data.dstEP = rxPtr->dstEP;
    a_aps_tx_data.dstMode = APS_DSTMODE_SHORT;
    a_aps_tx_data.dstSADDR = rxPtr->dstSADDR;
    a_aps_tx_data.cluster = rxPtr->cluster;
    a_aps_tx_data.usrPlen = rxPtr->usrPlen;
    a_aps_tx_data.usrPload = rxPtr->usrPload;
    phy_pib.currentTxFlen = 0;  //set frame length to zero, build from scratch
    apsTxData(TRUE);
    apsState = APS_STATE_INDIRECT_TX_WAIT;
    break;

    //wait for last indirect TX to finish
  case APS_STATE_INDIRECT_TX_WAIT:
    if (nwkBusy()) break;
    //at this point we have a status, but can't do much about it.
    //either it went through or it did not.
    //release the TX buffer lock before exiting.
    phyReleaseTxLock();
    //loop around, and see if there is another destination for this indirect packet
    apsState = APS_STATE_INDIRECT_GETDST;
    goto apsFSM_start;

#endif	


	 default:  break;


  }//end switch(apsState)
 HAL_SUSPEND(0);  //for WIN32
}

#ifdef LRWPAN_COORDINATOR
//inject this packet into stack as if it has been received
//so that the binding can be resolved.
static void apsInjectIndirectPacket(void){
  BYTE *dst;

	 //allocate some heap space for this data
	 a_aps_rx_data.orgpkt.data = MemAlloc(a_aps_tx_data.usrPlen);
         if (!a_aps_rx_data.orgpkt.data ) {
           //can't even get started, return
           a_aps_service.status = LRWPAN_STATUS_HEAPFULL;
           return;
         }
         //copy user payload into new space
         dst = a_aps_rx_data.orgpkt.data;
         a_aps_rx_data.usrPlen = a_aps_tx_data.usrPlen;  //save len
         while(a_aps_tx_data.usrPlen){
           *dst = *a_aps_tx_data.usrPload;  //copy data
           a_aps_tx_data.usrPload++;
           dst++;
           a_aps_tx_data.usrPlen--;
         }
         //set up rest of rx data
         a_aps_rx_data.cluster = a_aps_tx_data.cluster;
         a_aps_rx_data.af_fcf = a_aps_tx_data.af_fcf;
         a_aps_rx_data.srcEP = a_aps_tx_data.srcEP;
         a_aps_rx_data.srcSADDR = a_aps_tx_data.srcSADDR;
         a_aps_rx_data.usrPload = a_aps_rx_data.orgpkt.data;

         //this packet has arrived at the coordinator
         a_aps_rx_data.dstSADDR = 0;
         //ensure that the submode bit is a '1'
         a_aps_rx_data.aps_fcf = a_aps_tx_data.aps_fcf |APS_FRM_INDIRECT_SUBMODE_MASK ;

         //copy data into indirect buffer space
         apsRxBuffAdd(&a_aps_rx_data);

         //set the RX FSM to the pending state
         aps_pib.flags.bits.indirectPending = 1;
         apsRxState = APS_RXSTATE_RESOLVE_INDIRECT;

         a_aps_service.status = LRWPAN_STATUS_SUCCESS;

         //at this point, we have simulated this packet being
         //received by the stack. When we return, the mainFSM
         //will process it, resolve the indirect binding, and re-transmit

}
#endif


//Add the AF and APS headers, then send it to NWK
//the AF header probably should be a seperate layer,
//but will place it here since we are only handling MSG frames,
//reduces the depth of the stack.

void apsTxData(BOOL copy_payload) {

  BYTE *src;

  //if currentTxFlen is zero, we need to build the frame, else, it is
  // a retransmission
  if (phy_pib.currentTxFlen == 0) {
    //assume that the frame is just now being built.
    //use temporary space for building frame
    if (copy_payload){
      //copy user payload into tmpTxBuff space
      //if userPlen is 0, nothing is copied into the payload area
      phy_pib.currentTxFrm = &tmpTxBuff[LRWPAN_MAX_FRAME_SIZE];
      //get a pointer to the end of the payload
      src = a_aps_tx_data.usrPload + a_aps_tx_data.usrPlen;
      phy_pib.currentTxFlen = a_aps_tx_data.usrPlen;
      //now copy the user payload to the frame
      while (phy_pib.currentTxFlen) {
        src--;                //decrement to first src location with data
        phy_pib.currentTxFrm--;     //decrement to free location
        phy_pib.currentTxFlen--;    //decrement length
        *(phy_pib.currentTxFrm) = *src;
      }
    } else {
      //assume that TXBuff already has the payload, the ZEP
      //commands build their payload in this space
      //point currentTxFrm to this payload
      phy_pib.currentTxFrm = &tmpTxBuff[LRWPAN_MAX_FRAME_SIZE] - a_aps_tx_data.usrPlen;
    }
    //restore length
    phy_pib.currentTxFlen = a_aps_tx_data.usrPlen;

    if (APS_IS_DATA(a_aps_tx_data.aps_fcf)) {
      //DATA frame
      //Build AF header.
      //ONLY MSG FRAMES ARE SUPPORTED, so all we need to write is the
      //length of user payload
      --phy_pib.currentTxFrm; phy_pib.currentTxFlen++;
      *phy_pib.currentTxFrm = a_aps_tx_data.usrPlen;

      //sequence number
      --phy_pib.currentTxFrm; phy_pib.currentTxFlen++;
      *phy_pib.currentTxFrm = a_aps_tx_data.tsn;

      //AF frame control
      --phy_pib.currentTxFrm; phy_pib.currentTxFlen++;
      *phy_pib.currentTxFrm = a_aps_tx_data.af_fcf;
    }


    if (APS_GET_FRM_DLVRMODE(a_aps_tx_data.aps_fcf) == APS_FRM_DLVRMODE_INDIRECT){
      //this is indirect packet
#ifdef LRWPAN_COORDINATOR
      //TX packet from coordinator, ensure that the submode bit is a '0'

      a_aps_tx_data.aps_fcf = a_aps_tx_data.aps_fcf & ~APS_FRM_INDIRECT_SUBMODE_MASK ;
      //the dstSADDR has already been filled in during the binding resolution, copy to nwk
      a_nwk_tx_data.dstSADDR = a_aps_tx_data.dstSADDR;

#else
      //the destination for indirect packets is the coordinator
      a_nwk_tx_data.dstSADDR = 0;
      //ensure that the submode bit is a '1'
      a_aps_tx_data.aps_fcf = a_aps_tx_data.aps_fcf |APS_FRM_INDIRECT_SUBMODE_MASK ;
#endif
    } else {
      //copy destination address

      a_nwk_tx_data.dstSADDR = a_aps_tx_data.dstSADDR;
      a_nwk_tx_data.dstLADDR = a_aps_tx_data.dstLADDR;
     }



    //Build APS header.
    //SRC Endpoint
    if (!(((APS_GET_FRM_DLVRMODE(a_aps_tx_data.aps_fcf))==APS_FRM_DLVRMODE_INDIRECT) &&
          (!APS_GET_FRM_INDIRECT_SUBMODE(a_aps_tx_data.aps_fcf)))){
            //SRC endpoint is only omitted if INDIRECT frame and
            //indirect sub-mode bit is a '0'
            --phy_pib.currentTxFrm;phy_pib.currentTxFlen++;
            *phy_pib.currentTxFrm=a_aps_tx_data.srcEP;
          }

    //profile ID
    if ((APS_GET_FRM_TYPE(a_aps_tx_data.aps_fcf) == APS_FRM_TYPE_DATA)||
        (APS_GET_FRM_TYPE(a_aps_tx_data.aps_fcf) == APS_FRM_TYPE_ACK)) {
          //insert the profile ID, this hardcoded by the configuration
          --phy_pib.currentTxFrm;phy_pib.currentTxFlen++;
          *phy_pib.currentTxFrm= (BYTE) ((LRWPAN_APP_PROFILE) >> 8);
          --phy_pib.currentTxFrm;phy_pib.currentTxFlen++;
          *phy_pib.currentTxFrm = (BYTE)LRWPAN_APP_PROFILE ;
        }
    //cluster ID
    if (APS_GET_FRM_TYPE(a_aps_tx_data.aps_fcf) == APS_FRM_TYPE_DATA) {
      --phy_pib.currentTxFrm;phy_pib.currentTxFlen++;
      *phy_pib.currentTxFrm= a_aps_tx_data.cluster;
    }


    //Destination EP
    if (!(((APS_GET_FRM_DLVRMODE(a_aps_tx_data.aps_fcf))==APS_FRM_DLVRMODE_INDIRECT) &&
          (APS_GET_FRM_INDIRECT_SUBMODE(a_aps_tx_data.aps_fcf)))){
            //DST endpoint is only omitted if INDIRECT frame and
            //indirect sub-mode bit is a '1'
            --phy_pib.currentTxFrm;phy_pib.currentTxFlen++;
            *phy_pib.currentTxFrm=a_aps_tx_data.dstEP;
          }

    //frame control
    --phy_pib.currentTxFrm;phy_pib.currentTxFlen++;
    *phy_pib.currentTxFrm=a_aps_tx_data.aps_fcf;



    //setup call to network layer
    //use the SRC address passed in by the aps layer
    //will be the SADDR of the originating node for this message
    a_nwk_tx_data.srcSADDR = a_aps_tx_data.srcSADDR;

    //now set the network bytes
    //since we are using tree routing, the Route Discovery is always suppressed.
    a_nwk_tx_data.radius = LRWPAN_NWK_MAX_RADIUS;
    a_nwk_tx_data.fcflsb = NWK_FRM_TYPE_DATA | NWK_PROTOCOL | NWK_SUPPRESS_ROUTE_DISCOVER ;

    //Send via the network layer
    a_nwk_service.cmd = LRWPAN_SVC_NWK_GENERIC_TX;

    // at this point, we will attempt a TX
    if (APS_GET_FRM_ACKREQ(a_aps_tx_data.aps_fcf)){
      //need an ACK back. set ackPending bit, start timer.
      aps_pib.flags.bits.ackPending = 1;
      aps_pib.tx_start_time = halGetMACTimer();
	  //lets compute our Ack Wait duration
	  //aps_pib.apscAckWaitDuration
	  aps_pib.apsAckWaitMultiplier = nwkGetHopsToDest(a_nwk_tx_data.dstSADDR);
	  aps_pib.apsAckWaitMultiplierCntr = aps_pib.apsAckWaitMultiplier;
    }
    else aps_pib.flags.bits.ackPending = 0;
    apsSetTxBusy();
    aps_pib.currentAckRetries = aps_pib.apscMaxFrameRetries; //set retry count
    apsTxFSM_status = LRWPAN_STATUS_APS_INPROGRESS;

    //we need to remember this offset in case of a retry, as we
    //will have to reset the flen to this point
    a_aps_tx_data.aps_flen = phy_pib.currentTxFlen;
    a_aps_tx_data.aps_ptr = phy_pib.currentTxFrm;

  }

  nwkDoService();

}


//handle RX of packets at APS level
static void apsRxFSM(void) {


apsRxFSM_start:

  switch(apsRxState) {
                case APS_RXSTATE_IDLE:
                   break;
		case APS_RXSTATE_START:
                  //we have a packet, lets check it out.

                  if (APS_IS_RSV(a_aps_rx_data.aps_fcf)) {
                    //unknown packet type
                    DEBUG_STRING(DBG_INFO,"APS: Received APS RSV packet, discarding.\n");
                    MemFree(a_aps_rx_data.orgpkt.data);
                    apsRxState = APS_RXSTATE_IDLE;
                    break;
                  }
                  if ((APS_GET_FRM_DLVRMODE(a_aps_rx_data.aps_fcf) == APS_FRM_DLVRMODE_BCAST) ||
                      (APS_GET_FRM_DLVRMODE(a_aps_rx_data.aps_fcf) == APS_FRM_DLVRMODE_RSV)){
                        //Delivery mode not handled.
                        DEBUG_STRING(DBG_INFO,"APS: Received APS packet with BCAST or RSV delivery mode, discarding.\n");
                        MemFree(a_aps_rx_data.orgpkt.data);
                        apsRxState = APS_RXSTATE_IDLE;
                        break;
                      }

                  //parse this and see what to do with it.
                  if (APS_IS_CMD(a_aps_rx_data.aps_fcf)) {
                    //currently don't handle CMD packets. Discard.
                    DEBUG_STRING(DBG_INFO,"APS: Received APS CMD packet, discarding.\n");
                    //NWK,MAC resource already free; need to free the MEM resource
                    MemFree(a_aps_rx_data.orgpkt.data);
                    apsRxState = APS_RXSTATE_IDLE;
                    break;
                  }
                  //have a DATA or ACK packet, lets do something.
                  //parse it to figure out what it is.
                  apsParseHdr(a_aps_rx_data.orgpkt.data + a_aps_rx_data.apsOffset);
                  if (APS_IS_ACK(a_aps_rx_data.aps_fcf)) {
                    if (!aps_pib.flags.bits.ackPending) {
                      //not currently expecting an ACK so discard
                      DEBUG_STRING(DBG_INFO,"APS: Received unexpected ACK, discarding.\n");
                    }else {
                      //lets see if this is our ack.
                      if (apsCheckAck()) {
                        DEBUG_STRING(DBG_INFO,"APS: Received APS ack\n");
                        //this is our ACK, clear the ackPending bit
                        aps_pib.flags.bits.ackPending = 0;
                      } else {
                        DEBUG_STRING(DBG_INFO,"APS: Received ACK, did not match expected.\n");
                      }

⌨️ 快捷键说明

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