Changeset - 61c1fe267ce2
[Not reviewed]
default
0 3 0
ethanzonca@CL-SEC241-08.cedarville.edu - 12 years ago 2012-12-03 18:57:03
ethanzonca@CL-SEC241-08.cedarville.edu
Resolved FS#29 - Serial checksum may match special character
3 files changed with 32 insertions and 15 deletions:
0 comments (0 inline, 0 general)
master/master/lib/serial.c
Show inline comments
 
@@ -107,21 +107,22 @@ void serial_sendCommand( char moduleID, 
 
	
 
	serial0_sendChar(sensorID);
 
	checkSum+=sensorID;
 
	
 
	// send data, null-terminated
 
	while(*data != 0x00)
 
	{
 
		serial0_sendChar(*data);
 
		checkSum += *data;
 
		data++;
 
	}
 
	
 
	serial0_sendChar(checkSum);
 
	
 
	serial0_sendChar(']'); //bracket indicates end of command
 
	serial0_sendChar(checkSum); // checksum moved behind bracket to solve bug FS#29
 
}
 
 
void serial_sendResponseData()
 
{
 
	
 
}
 
master/master/lib/serparser.c
Show inline comments
 
@@ -140,37 +140,29 @@ int serparser_parse(void)
 
		}
 
		
 
		// Get payload data
 
		else if(parserState == STATE_GETDATA)
 
		{		
 
			if (byte == ']') // End of frame
 
			{
 
				#ifdef DEBUG
 
				serial0_sendString("eof ok\r\n");
 
				sprintf(buffmeh, "recvd %d bytes data\r\n", dataLength);
 
				serial0_sendString((buffmeh));
 
				#endif
 
				if(bufferParsePosition == bufferDataPosition) 
 
				{
 
					// We are at the end of the line. No more data to parse.
 
					setParserState(STATE_RESET);
 
					return PARSERESULT_PARSEOK;
 
				}
 
				else 
 
				{
 
					setParserState(STATE_RESET);
 
					// we could choose to keep parsing now, or parse the next message next loop around (better idea).
 
					// return now so we hit it the next time around
 
					return PARSERESULT_PARSEOK;
 
				}
 
				
 
				setParserState(STATE_GETCHECKSUM);
 
				// Checksum is now after the close bracket to solve bug FS#29		
 
				
 
 
			}
 
			else // Still receiving data
 
			{
 
				#ifdef DEBUG
 
				serial0_sendString("data ok\r\n");
 
				#endif
 
				receivedPayload[dataLength] = byte;
 
				dataLength++;
 
				checksumCalc += byte;
 
				
 
				// Data buffer overrun protection
 
				if(dataLength > MAX_PAYLOAD_LEN) {
 
@@ -180,16 +172,39 @@ int serparser_parse(void)
 
					setParserState(STATE_RESET);
 
					return PARSERESULT_FAIL;
 
				}
 
				else {
 
					// Set state. MUST call even though state is maintained to update parse position
 
					setParserState(STATE_GETDATA);
 
					return PARSERESULT_STILLPARSING;
 
				}
 
				
 
			}
 
 
		}
 
		else if(STATE_GETCHECKSUM)
 
		{
 
			// TODO: Compare checksums
 
			if(byte == checksumCalc) {
 
				serial0_sendString("checksum ok\r\n");
 
			}
 
			else {
 
				serial0_sendString("checksum fail\r\n");
 
			}
 
			if(bufferParsePosition == bufferDataPosition)
 
			{
 
				// We are at the end of the line. No more data to parse.
 
				setParserState(STATE_RESET);
 
				return PARSERESULT_PARSEOK;
 
			}
 
			else
 
			{
 
				setParserState(STATE_RESET);
 
				// we could choose to keep parsing now, or parse the next message next loop around (better idea).
 
				// return now so we hit it the next time around
 
				return PARSERESULT_PARSEOK;
 
			}
 
		}			
 
	}
 
	return PARSERESULT_NODATA;
 
	
 
}
 
\ No newline at end of file
master/master/lib/serparser.h
Show inline comments
 
@@ -19,19 +19,20 @@ enum parseResults
 
	PARSERESULT_FAIL = 0,
 
	PARSERESULT_NODATA,
 
	PARSERESULT_STILLPARSING,
 
	PARSERESULT_PARSEOK,
 
};
 
 
// Parser states
 
enum parseStates
 
{
 
	STATE_RESET = 0,
 
	STATE_GETID,
 
	STATE_GETDATATYPE,
 
	STATE_GETDATA
 
	STATE_GETDATA,
 
	STATE_GETCHECKSUM,
 
};
 
 
// Prototypes
 
int serparser_parse(void);
 
 
#endif /* SERPARSER_H_ */
 
\ No newline at end of file
0 comments (0 inline, 0 general)