QByteArray NodeIdentificationIndicator::getFrameData() {
	QByteArray frameData;
	frameData += getFrameType();
	frameData += getSourceAddress64();
	frameData += getSourceAddress16();
	frameData += getReceiveOptions();
	frameData += getRemoteAddress16();
	frameData += getRemoteAddress64();
	frameData += getNIString();
	frameData += (char)0x00;
	frameData += getParentAddress16();
	frameData += getDeviceType();
	frameData += getSourceEvent();
	frameData += getProfileID();
	frameData += getManufacturerID();
	frameData += getDeviceTypeID();
	frameData += getRSSI();
	return frameData;
}
/**
  * Attempt to queue a buffer received by the radio hardware, if sufficient space is available.
  *
  * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if a replacement receiver buffer
  *         could not be allocated (either by policy or memory exhaustion).
  */
int MicroBitRadio::queueRxBuf()
{
    if (rxBuf == NULL)
        return MICROBIT_INVALID_PARAMETER;

    if (queueDepth >= MICROBIT_RADIO_MAXIMUM_RX_BUFFERS)
        return MICROBIT_NO_RESOURCES;

    // Store the received RSSI value in the frame
    rxBuf->rssi = getRSSI();

    // Ensure that a replacement buffer is available before queuing.
    FrameBuffer *newRxBuf = new FrameBuffer();

    if (newRxBuf == NULL)
        return MICROBIT_NO_RESOURCES;

    // We add to the tail of the queue to preserve causal ordering.
    rxBuf->next = NULL;

    if (rxQueue == NULL)
    {
        rxQueue = rxBuf;
    }
    else
    {
        FrameBuffer *p = rxQueue;
        while (p->next != NULL)
            p = p->next;

        p->next = rxBuf;
    }

    // Increase our received packet count
    queueDepth++;

    // Allocate a new buffer for the receiver hardware to use. the old on will be passed on to higher layer protocols/apps.
    rxBuf = newRxBuf;

    return MICROBIT_OK;
}
Beispiel #3
0
void PDCallback (void)
/****************************************************************************/
{
    MyByte8T       status;
    MyByte8T       reg[2];

	/*
	 * use 1 led to indicate message reception on the devBoard
	 * The led will stay on for a 50 ms.
     */
#   ifdef CONFIG_TRAFFIC_LED
    TRIGGER_LED_RX ();
#   endif /* CONFIG_TRAFFIC_LED */

	/*
	 * read the crc2 status register
	 */
    NTRXSPIReadByte (NA_RxCrc2Stat_O, &status);
    rxIrq = 0;

    /* check if data is valid */
    if ((status & (1 << NA_RxCrc2Stat_B)) != 0)
    {
        NTRXSetIndexReg (0);
        /* read source address */
        NTRXSPIRead (NA_RamRxSrcAddr_O, upMsg.addr, 6);
        /* read length plus additionl bits */
        NTRXSPIRead (NA_RamRxLength_O, reg, 2);

        rState = reg[1]>>5;

        /* read destination address */
#		ifdef CONFIG_NTRX_SNIFFER
        NTRXSPIRead (NA_RamRxDstAddr_O, upMsg.rxAddr, 6);
		upMsg.count++;
		upMsg.extBits = rState;
		upMsg.frameType = (status & 0x0f);
#		endif

		upMsg.len = reg[0];
        if (upMsg.len > PHY_PACKET_SIZE)
        {
		    /* restart receiver */
			if (phyPIB.rxState == PHY_RX_ON)
			{
    			NTRXSPIWriteByte (NA_RxCmdStart_O, ntrxShadowReg[NA_RxCmdStart_O]
									| (1 << NA_RxCmdStart_B)
									| (0x03 << NA_RxBufferCmd_LSB));
			}
        }
        else
        {

			if (buffSwapped == TRUE)
			{
				buffSwapped = FALSE;
				/* SWAP BUFFER for receive*/
				ntrxShadowReg[NA_SwapBbBuffers_O] &= ~(1 << NA_SwapBbBuffers_B);
				NTRXSPIWriteByte (NA_SwapBbBuffers_O, ntrxShadowReg[NA_SwapBbBuffers_O]);

				NTRXSetIndexReg (3);
			}
			else
			{
				buffSwapped = TRUE;
				/* SWAP BUFFER for receive*/
				ntrxShadowReg[NA_SwapBbBuffers_O] |= (1 << NA_SwapBbBuffers_B);
				NTRXSPIWriteByte (NA_SwapBbBuffers_O, ntrxShadowReg[NA_SwapBbBuffers_O]);

				NTRXSetIndexReg (2);
			}

			tiPhyRxTimeout_once = FALSE;
			tiPhyRxTimeout = hwclock() + phyPIB.phyRxTimeout;

			/*
			 * restart receiver and than read rx buffer. This is ok because we use
			 * buffer swapping.
			 */
			if (phyPIB.rxState == PHY_RX_ON)
			{
    			NTRXSPIWriteByte (NA_RxCmdStart_O, ntrxShadowReg[NA_RxCmdStart_O]
									| (1 << NA_RxCmdStart_B)
									| (0x03 << NA_RxBufferCmd_LSB));
			}

            NTRXSPIRead ((MyByte8T)(NA_RamRxBuffer_O & 0xFF), upMsg.data, upMsg.len);
			NTRXSetIndexReg (0);
#ifdef		CONFIG_NTRX_SNIFFER
			upMsg.value = 0xff;
			upMsg.prim = PD_DATA_INDICATION;
			SendMsgUp (&upMsg);
#else
			/*
			 * if address matching off, ignore rangingstates
			 * this path is used for normal data reception
			 * ranging is handled in the else path
			 */
			if (((ntrxShadowReg[NA_RxAddrMode_O] & (1<<NA_RxAddrMode_B)) == 0) ||
				(lState == RANGING_READY && rState == RANGING_READY))
			{
				upMsg.value = 0xff;
				upMsg.prim = PD_DATA_INDICATION;
				SendMsgUp (&upMsg);
			}
			else if((lState == RANGING_READY) && (rState == RANGING_START || rState == RANGING_FAST_START))
			{
				memcpy(rDest, upMsg.addr, 6);

				RangingCallback_Rx(upMsg.data, upMsg.len);

				if(rState == RANGING_START)
				{
					lState = RANGING_ANSWER1;
					/* send ranging packet */
					RangingMode(RANGING_ANSWER1, rDest);
				}else if (rState == RANGING_FAST_START)
				{
					lState = RANGING_FAST_ANSWER1;
					/* send ranging packet */
					RangingMode(RANGING_FAST_ANSWER1, rDest);
				}
			}
			else if((memcmp(rDest, upMsg.addr, 6) == 0) && lState == RANGING_ANSWER1 && rState == RANGING_ANSWER1)
			{
				/* received ranging data to RangingCallback_Rx
				 * (without protocol header stuff)
				 */
				RangingCallback_Rx(upMsg.data, upMsg.len);
				lState = RANGING_ANSWER2;
			}
			else if((memcmp(rDest, upMsg.addr, 6) == 0) &&
					((lState == RANGING_ANSWER2 && rState == RANGING_ANSWER2) ||
					(lState == RANGING_FAST_ANSWER1 && rState == RANGING_FAST_ANSWER1)))
			{
				/* ranging was successfull, stop timeout */
				NTRXStopBbTimer();

				/* received ranging data to RangingCallback_Rx
				 *(without protocol header stuff)
				 */
				RangingCallback_Rx(upMsg.data, upMsg.len);

				/* calculate the distance */
				rangingPIB.distance = getDistance();
				rangingPIB.rssi = getRSSI();

				if (rangingPIB.distance < 0.0)
					rangingPIB.error = STAT_RANGING_VALUE_ERROR;

				upMsg.value = 0xff;
				memcpy(upMsg.data, (MyByte8T*) &rangingPIB, sizeof(RangingPIB));
				upMsg.len  = sizeof(RangingPIB);

				if (rState == RANGING_ANSWER2)
					upMsg.prim = PD_RANGING_INDICATION;
				else if (rState == RANGING_FAST_ANSWER1)
					upMsg.prim = PD_RANGING_FAST_INDICATION;

				/* set ready for new ranging */
				lState = RANGING_READY;
				SendMsgUp (&upMsg);
			}
#			endif
        }
    }