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
 
@@ -23,105 +23,106 @@ void serial0_setup() {
 
	//PORTD |= (1<<PD1);
 
	
 
	/* Enable receiver and transmitter */
 
	UCSR0B |= (1<<RXEN0)|(1<<TXEN0); // Enable rx/tx
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01); // - 1 stop bit
 
	
 
	/* Set baud rate */
 
	UBRR0H = (unsigned char)(USART0_BAUD_PRESCALE>>8);
 
	UBRR0L = (unsigned char)USART0_BAUD_PRESCALE;
 
	
 
	UCSR0B |= (1 << RXCIE0); // Enable interrupt
 
	
 
	//UCSR0A |= (1<<RXC0);
 
 
 
}
 
 
/*
 
uint8_t test = 0;
 
ISR(USART0__RX_vect)
 
{
 
	led_on(POWER);
 
	test = UDR0;
 
}*/
 
 
void serial1_setup() {
 
	/* Set baud rate */
 
	UBRR1H = (unsigned char)(USART1_BAUD_PRESCALE>>8);
 
	UBRR1L = (unsigned char)USART1_BAUD_PRESCALE;
 
	/* Enable receiver and transmitter */
 
	UCSR1B = (1<<RXEN1)|(1<<TXEN1);
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR1C = (3<<UCSZ10); // - 1 stop bit
 
}
 
 
void serial0_sendChar( unsigned char byte )
 
{
 
	while (!(UCSR0A & (1<<UDRE0)));
 
	UDR0 = byte;
 
}
 
 
void serial1_sendChar( unsigned char byte )
 
{
 
	while (!(UCSR1A & (1<<UDRE1)));
 
	UDR1 = byte;
 
}
 
 
unsigned char serial0_readChar()
 
{
 
	while(!(UCSR0A &(1<<RXC0)));
 
	return UDR0;
 
}
 
 
unsigned char serial1_readChar()
 
{
 
	while(!(UCSR1A &(1<<RXC1)));
 
	return UDR1;
 
}
 
 
void serial0_sendString(const char* stringPtr){
 
	while(*stringPtr != 0x00)
 
	{
 
		serial0_sendChar(*stringPtr);
 
		stringPtr++;
 
	}
 
}
 
 
void serial1_sendString(const char* stringPtr){
 
	while(*stringPtr != 0x00)
 
	{
 
		serial1_sendChar(*stringPtr);
 
		stringPtr++;
 
	}
 
}
 
 
 
void serial_sendCommand( char moduleID, char sensorID, char* data )
 
{
 
	char checkSum = 0x00; //initialize checksum
 
	
 
	serial0_sendChar('['); //bracket indicates start of command
 
	serial0_sendChar(moduleID);
 
	checkSum+=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
 
@@ -56,140 +56,155 @@ static void setParserState(uint8_t state
 
		checksumCalc = 0;
 
	}
 
	
 
	// Every time we change state, we have parsed a byte
 
	bufferParsePosition = (bufferParsePosition + 1) % BUFFER_SIZE;
 
}
 
 
// Receive data on USART
 
 
char buffmeh[16];
 
 
ISR(USART0__RX_vect)
 
{
 
	led_on(POWER);
 
	buffer[bufferDataPosition % BUFFER_SIZE] = UDR0;
 
	bufferDataPosition = (bufferDataPosition + 1) % BUFFER_SIZE;
 
	/*sprintf(buffmeh, "bdp: %d, bpp: %d \r\n", bufferDataPosition, bufferParsePosition);
 
	serial0_sendString((buffmeh)); */
 
}
 
 
 
 
#define DEBUG
 
 
// Parse data from circular buffer
 
int serparser_parse(void)
 
{
 
	
 
	char byte;
 
 
	// Process first command (if any) on the circular buffer
 
	while(bufferDataPosition != bufferParsePosition)
 
	{
 
		byte = buffer[bufferParsePosition];
 
		
 
		// Reset 
 
		if(parserState == STATE_RESET)
 
		{
 
			if(byte == '[') // Start of frame; keep parsing
 
			{
 
				#ifdef DEBUG
 
				serial0_sendString("start ok\r\n");
 
				#endif
 
				setParserState(STATE_GETID);
 
			}
 
			else // Not start of frame, reset
 
			{
 
				#ifdef DEBUG
 
				serial0_sendString("not start\r\n");
 
				#endif
 
				setParserState(STATE_RESET);
 
				return PARSERESULT_NODATA; // no valid start bit; better luck next time. run the function the next time around the main loop.
 
			}
 
		}
 
		
 
		// Get destination module ID
 
		else if(parserState == STATE_GETID)
 
		{
 
			if(byte == MODULE_ID) // Message intended for this module; keep parsing
 
			{
 
				#ifdef DEBUG
 
				serial0_sendString("dest ok\r\n");
 
				#endif
 
				checksumCalc += byte;
 
				setParserState(STATE_GETDATATYPE);
 
			}
 
			else // Transmission is intended for another module; reset
 
			{
 
				#ifdef DEBUG
 
				serial0_sendString("bad dest\r\n");
 
				#endif
 
				setParserState(STATE_RESET);
 
			}
 
		}
 
		
 
		// Get payload type ID
 
		else if(parserState == STATE_GETDATATYPE)
 
		{
 
			#ifdef DEBUG
 
			serial0_sendString("type ok\r\n");
 
			#endif
 
			receivedDataType = byte; // Store the type of data receiving
 
			checksumCalc += byte;
 
			setParserState(STATE_GETDATA);
 
		}
 
		
 
		// 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) {
 
					#ifdef DEBUG
 
					serial0_sendString("data ovf\r\n");
 
					#endif
 
					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
 
/*
 
 * Master Firmware: Serial Parser
 
 *
 
 * Wireless Observational Modular Aerial Network
 
 * 
 
 * Ethan Zonca
 
 * Matthew Kanning
 
 * Kyle Ripperger
 
 * Matthew Kroening
 
 *
 
 */
 
 
 
#ifndef SERPARSER_H_
 
#define SERPARSER_H_
 
 
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)