Beispiel #1
0
bool MrimMessages::handlePacket(class MrimPacket& packet)
{
    switch (packet.msgType()) {
    case MRIM_CS_MESSAGE_STATUS:
        handleMessageStatus(packet);
        break;
    case MRIM_CS_MESSAGE_ACK:
        handleMessageAck(packet);
        break;
	case MRIM_CS_OFFLINE_MESSAGE_ACK:
		handleOfflineMessageAck(packet);
		break;
    default:
		return false;
    }
    return true;
}
Beispiel #2
0
__interrupt void USCI0RX_ISR(void) {

	unsigned int i, checksumHolder;

	//Look for delimiter
	if (UCA0RXBUF == XBEE_START_DELIMITER) {
		//Reset variables
		xbeeObject.bufferPosition = 0;
		xbeeObject.xbeeFrameType = 0;
		xbeeObject.xbeeMessageLength = 0;
		xbeeObject.xbeeSenderAddr = 0;
		xbeeObject.xbeeSenderNodeAddr = 0;
	} //if()

	//Check for gibberish (ie. No delimter yet, but we have characters)
	if (xbeeObject.bufferPosition == -1) {
		return;
	} //if()

	//Check for a full buffer
	if (xbeeObject.bufferPosition == MAX_XBEE_BUFFER_LEN) return;

	//Add data to buffer
	xbeeObject.bufferSpace[xbeeObject.bufferPosition] = UCA0RXBUF;
	xbeeObject.bufferPosition++;

	//Pull message length
	if (xbeeObject.bufferPosition == 3) {
		xbeeObject.xbeeMessageLength = (xbeeObject.bufferSpace[1] << 8) + xbeeObject.bufferSpace[2];	//Frame 1-2: Message Length
	} //if()

	//Check for end of message & handle it (necessary to prevent data overwrite)
	if (xbeeObject.bufferPosition == xbeeObject.xbeeMessageLength + 4) {

		//Prevent this message from being read again
		xbeeObject.bufferPosition = -1;

		//Calculate the checksum
		checksumHolder = 0;
		for(i=3; i<xbeeObject.xbeeMessageLength + 4; i++) {
			checksumHolder += xbeeObject.bufferSpace[i];
			checksumHolder &= 0x00FF;
		} //for()

		//Check for invalid checksum
		if (checksumHolder != 0x00FF) return;

		xbeeObject.xbeeFrameType = xbeeObject.bufferSpace[3]; //Frame 3: Frame Type

		/* CAUTION -- SIGNIFICANT IN-LINE/NON-OPTIMAL CODE FOR NEXT BLOCK OF CODE */

		//Handle any configuration messages
		if(xbeeObject.xbeeFrameType == AT_CMD_RESPONSE) {
			//Filter out response types
			if ((xbeeObject.bufferSpace[5] << 8) + xbeeObject.bufferSpace[6] == ATCMD_HP) {
				//Response with Preamble ID  -- check to see if it's okay
				if (xbeeObject.bufferSpace[7] == 0x00) { //status OK
					//Okay, now send the next configuration for the Network ID
					/* Initialize network information #2 */
					sendByte(0x7E);
					sendByte(0x00);
					sendByte(0x06);
					sendByte(0x09);
					sendByte(0xB6);
					sendByte(0x49);
					sendByte(0x44);
					sendByte(0x20);
					sendByte(0x14);
					sendByte(0x7F);
				} else {
					//Try sending the configuration message again
					/* Initialize network information */
					sendByte(0x7E);
					sendByte(0x00);
					sendByte(0x05);
					sendByte(0x09);
					sendByte(0xB5);
					sendByte(0x48);
					sendByte(0x50);
					sendByte(0x04);
					sendByte(0xA5);
				} //if-else()

			} else if((xbeeObject.bufferSpace[5] << 8) + xbeeObject.bufferSpace[6] == ATCMD_ID) {
				//Response with Network ID -- check to see if it's okay
				if (xbeeObject.bufferSpace[7] == 0x00) { //status OK
					//Okay, now send the write command
					sendByte(0x7E);
					sendByte(0x00);
					sendByte(0x04);
					sendByte(0x09);
					sendByte(0xB7);
					sendByte(0x57);
					sendByte(0x52);
					sendByte(0x96);
				} else {
					//Try sending the configuration message again
					/* Initialize network information #2 */
					sendByte(0x7E);
					sendByte(0x00);
					sendByte(0x06);
					sendByte(0x09);
					sendByte(0xB6);
					sendByte(0x49);
					sendByte(0x44);
					sendByte(0x20);
					sendByte(0x14);
					sendByte(0x7F);
				} //if-else()

			} else if((xbeeObject.bufferSpace[5] << 8) + xbeeObject.bufferSpace[6] == ATCMD_WR) {
				//Response with WR status -- check to see if it's okay
				if (xbeeObject.bufferSpace[7] == 0x00) { //status OK
					//Okay, now send the apply changes command
					sendByte(0x7E);
					sendByte(0x00);
					sendByte(0x04);
					sendByte(0x09);
					sendByte(0xB8);
					sendByte(0x41);
					sendByte(0x43);
					sendByte(0xBA);
				} else {
					//Try sending the write configuration message again
					sendByte(0x7E);
					sendByte(0x00);
					sendByte(0x04);
					sendByte(0x09);
					sendByte(0xB7);
					sendByte(0x57);
					sendByte(0x52);
					sendByte(0x96);
				} //if-else()
			} else if((xbeeObject.bufferSpace[5] << 8) + xbeeObject.bufferSpace[6] == ATCMD_AC) {
				//Response with AC status -- check to see if it's okay
				if (xbeeObject.bufferSpace[7] != 0x00) { //status not OK
					//Try sending the apply changes configuration message again
					sendByte(0x7E);
					sendByte(0x00);
					sendByte(0x04);
					sendByte(0x09);
					sendByte(0xB8);
					sendByte(0x41);
					sendByte(0x43);
					sendByte(0xBA);
				} //if-else()
			} //if-else()

			return;
		} else if (xbeeObject.xbeeFrameType != RX_INDICATOR) {
			//Not a configuration message and not a typical data message -- exit
			return;
		} //if-else()

		/* END IN-LINE CODING EYE SORE */

		//Terminate the buffer (eliminates the checksum from the buffer in the process)
		xbeeObject.bufferSpace[xbeeObject.xbeeMessageLength + 3] = '\0';

		xbeeObject.xbeeSenderAddr = xbeeObject.bufferSpace[4];
		xbeeObject.xbeeSenderAddr <<= 8;

		xbeeObject.xbeeSenderAddr += xbeeObject.bufferSpace[5];
		xbeeObject.xbeeSenderAddr <<= 8;

		xbeeObject.xbeeSenderAddr += xbeeObject.bufferSpace[6];
		xbeeObject.xbeeSenderAddr <<= 8;

		xbeeObject.xbeeSenderAddr += xbeeObject.bufferSpace[7];
		xbeeObject.xbeeSenderAddr <<= 8;

		xbeeObject.xbeeSenderAddr += xbeeObject.bufferSpace[8];
		xbeeObject.xbeeSenderAddr <<= 8;

		xbeeObject.xbeeSenderAddr += xbeeObject.bufferSpace[9];
		xbeeObject.xbeeSenderAddr <<= 8;

		xbeeObject.xbeeSenderAddr += xbeeObject.bufferSpace[10];
		xbeeObject.xbeeSenderAddr <<= 8;

		xbeeObject.xbeeSenderAddr += xbeeObject.bufferSpace[11];

#if EXFIL_NODE

		//Activate LED
		P1OUT ^= BIT0;//Toggle LED

		xbeeObject.xbeeSenderNodeAddr = addXbee(xbeeObject.xbeeSenderAddr);

		if (xbeeObject.bufferSpace[RX_DATASTART] == NETWORK_NODE_REQ_INT) {
			addQueuedNode(xbeeObject.xbeeSenderNodeAddr, NULL);
		} else {
			//Is this xbee the next in line?
			if (!exfilObject.topExfilQueue ||
					exfilObject.topExfilQueue->node_number != xbeeObject.xbeeSenderNodeAddr) {
				__no_operation();
				return;
			} //if()

			//Is the radio busy?
			//if (!isHAMReady()) return;

			//If this branch is taken, a node is trying to send its
			//	message twice.
			if (exfilObject.topExfilQueue->message) {
				return;
			} //if()

			//Copy the message into a buffer
			exfilObject.topExfilQueue->message = malloc(sizeof(char) * (strlen(&xbeeObject.bufferSpace[15]) + 1) );
			strcpy(exfilObject.topExfilQueue->message, &xbeeObject.bufferSpace[15]);

			exfilObject.topExfilQueue->status = EXFIL_ACCEPT_ACK;

			/*
			//Add the message to the radio and reply if successful
			if (sendHAMString(exfilObject.topExfilQueue->message, xbeeObject.xbeeSenderNodeAddr)) {
				exfilObject.topExfilQueue->status = EXFIL_ACCEPT_ACK_SENT;
			} else {
				//Not successful, delete it
				exfilObject.topExfilQueue->status = EXFIL_FIN;
			} //if-else()
			*/
		} //if()
#else
		//Check to make sure this message came from the exfil node
		if (xbeeObject.xbeeSenderAddr != EXFIL_XBEE_ADDR) return;

		switch(xbeeObject.bufferSpace[RX_DATASTART]) {
		case NETWORK_EX_REQ_ACK_INT:
			handleMessageReqAck();
			break;
		case NETWORK_EX_APPROVAL_INT:
			handleMessageApproval();
			break;
		case NETWORK_EX_MSG_ACK_INT:
			handleMessageAck();
			break;
		default: break;
		} //switch()
#endif
	} //if()

} //USCI0RX_ISR()