Index: App/Services/SystemComm.c =================================================================== diff -u -r833095dbbe2b21a989b05f48bd7ddc390ad964cb -r83de6e3e3de4767fd50713e18b0bd75a06479fc7 --- App/Services/SystemComm.c (.../SystemComm.c) (revision 833095dbbe2b21a989b05f48bd7ddc390ad964cb) +++ App/Services/SystemComm.c (.../SystemComm.c) (revision 83de6e3e3de4767fd50713e18b0bd75a06479fc7) @@ -21,6 +21,7 @@ #include "Common.h" #include "Buttons.h" #include "MsgQueues.h" +#include "SystemCommMessages.h" #include "SystemComm.h" // ***************************** TEST CODE ****************************** @@ -32,16 +33,6 @@ // ********** private definitions ********** -#define MESSAGE_SYNC_BYTE 0xA5 -#define CAN_MESSAGE_CARGO_SIZE 8 - -typedef enum Msg_IDs -{ - MSG_ID_TEST = 0, - MSG_ID_OFF_BUTTON_PRESS, - NUM_OF_MSG_IDS -} MSG_ID_T; - #define NUM_OF_CAN_OUT_BUFFERS 4 // # of CAN buffers for transmit #define NUM_OF_CAN_IN_BUFFERS 6 // # of CAN buffers for receiving @@ -70,11 +61,10 @@ static COMM_BUFFER_T findNextHighestPriorityCANPacketToTransmit( void ); static void transmitNextCANPacket( void ); static void transmitPendingUARTData( void ); -static U32 serializeMessage( MESSAGE_T msg, U08 *data ); static void processIncomingData( void ); static U32 parseMessageFromBuffer( U08 *data, U32 len ); -static void consumeBufferDataBeforeSync( COMM_BUFFER_T buffer ); +static void consumeBufferPaddingBeforeSync( COMM_BUFFER_T buffer ); static void processReceivedMessages( void ); static void processReceivedMessage( MESSAGE_T *message ); @@ -230,52 +220,7 @@ // TODO - implement } -/************************************************************************* - * @brief serializeMessage - * The serializeMessage function serializes a given message into a given \n - * array of bytes. A sync byte is inserted at the beginning of the message \n - * and an 8-bit CRC is appended to the end of the message. The given array \n - * must be large enough to hold the message + 1 sync byte and 1 CRC byte and \n - * up to 7 CAN padding bytes. - * @details - * Inputs : none - * Outputs : given data array populated with serialized message data. - * @param msg : message to serialize - * @param data : byte array to populate with message data - * @return size (in bytes) of serialized message populated in given data array. - *************************************************************************/ -static U32 serializeMessage( MESSAGE_T msg, U08 *data ) -{ - U32 msgSize = 0; - U32 sizeMod, sizePad; - U32 i; - // prefix data with message sync byte - //data[msgSize++] = MESSAGE_SYNC_BYTE; - - // serialize message header data - memcpy( &data[msgSize], &(msg.hdr), sizeof(MESSAGE_HEADER_T) ); - msgSize += sizeof(MESSAGE_HEADER_T); - - // serialize message cargo (only used bytes per cargoLen field) - memcpy( &data[msgSize], &(msg.cargo), msg.hdr.cargoLen ); - msgSize += msg.hdr.cargoLen; - - // TODO - calculate 8-bit CRC - data[msgSize++] = 0; // TODO - s/b 8-bit CRC when calc is available - - // pad with zero bytes to get length a multiple of CAN_MESSAGE_CARGO_SIZE (8) - sizeMod = msgSize % CAN_MESSAGE_CARGO_SIZE; - sizePad = ( sizeMod == 0 ? 0 : CAN_MESSAGE_CARGO_SIZE - sizeMod ); - for ( i = 0; i < sizePad; i++ ) - { - data[msgSize++] = 0; - } - - return msgSize; -} - - /************************************************************************* ********************** RECEIVE SUPPORT FUNCTIONS ************************* *************************************************************************/ @@ -328,9 +273,11 @@ { U32 numOfBytesInBuffer; - //consumeBufferDataBeforeSync( CAN_IN_BUFFERS[i] ); // TODO - call when sync implemented + // since messages can have CAN padding left unconsumed by last get, get padding out of buffer + consumeBufferPaddingBeforeSync( CAN_IN_BUFFERS[i] ); + // do we have enough bytes in buffer for smallest message? numOfBytesInBuffer = numberOfBytesInCommBuffer( CAN_IN_BUFFERS[i] ); - if ( numOfBytesInBuffer >= CAN_MESSAGE_CARGO_SIZE ) + if ( numOfBytesInBuffer >= MESSAGE_OVERHEAD_SIZE ) { U32 bytesPeeked = peekFromCommBuffer( CAN_IN_BUFFERS[i], data, MIN(numOfBytesInBuffer,(sizeof(MESSAGE_WRAPPER_T)+1)) ); U32 msgSize = parseMessageFromBuffer( data, bytesPeeked ); @@ -352,36 +299,35 @@ addToMsgQueue( MSG_Q_IN_CAN, &rcvMsg ); } } - - -// MESSAGE_WRAPPER_T rcvMsg; -// U08 *dataPtr = data; -// -// getFromCommBuffer( CAN_IN_BUFFERS[i], data, CAN_MESSAGE_CARGO_SIZE ); -// blankMessageInWrapper( &rcvMsg ); -// memcpy( &(rcvMsg.msg.hdr), dataPtr, sizeof(MESSAGE_HEADER_T) ); -// dataPtr += sizeof(MESSAGE_HEADER_T); -// memcpy( &(rcvMsg.msg.cargo), dataPtr, rcvMsg.msg.hdr.cargoLen ); -// dataPtr += rcvMsg.msg.hdr.cargoLen; -// rcvMsg.crc = *dataPtr; -// addToMsgQueue( MSG_Q_IN_CAN, &rcvMsg ); } } } /************************************************************************* - * @brief consumeBufferDataBeforeSync - * The consumeBufferDataBeforeSync function removes any bytes in a given \n + * @brief consumeBufferPaddingBeforeSync + * The consumeBufferPaddingBeforeSync function removes any bytes in a given \n * buffer that lie before a sync byte. * @details * Inputs : none * Outputs : none * @param msg : buffer : the comm buffer to process * @return none *************************************************************************/ -static void consumeBufferDataBeforeSync( COMM_BUFFER_T buffer ) +static void consumeBufferPaddingBeforeSync( COMM_BUFFER_T buffer ) { - // TODO - implement + U08 data; + U32 numOfBytesInBuffer = numberOfBytesInCommBuffer( buffer ); + + // consume bytes out of buffer 1 at a time until we find the sync byte or it's empty + while ( numOfBytesInBuffer > 0 ) + { + getFromCommBuffer( buffer, &data, 1 ); + if ( MESSAGE_SYNC_BYTE == data ) + { + break; // we found a sync - we're done + } + numOfBytesInBuffer = numberOfBytesInCommBuffer( buffer ); + } } /************************************************************************* @@ -405,21 +351,19 @@ for ( i = 0; i < len; i++ ) { // find sync byte - //if ( MESSAGE_SYNC_BYTE == data[i] ) - if (1) // TODO - find sync byte when implemented + if ( MESSAGE_SYNC_BYTE == data[i] ) { - U32 pos = i; // + 1; // TODO - add one when sync byte implemented + U32 pos = i + 1; // skip past sync byte implemented U32 remSize = len - pos; - U32 minSize = sizeof(MESSAGE_HEADER_T) + sizeof(U08); // if a minimum sized msg would fit in remaining - if ( remSize >= minSize ) + if ( remSize >= MESSAGE_OVERHEAD_SIZE ) { cargoSize = data[pos+sizeof(U16)]; - msgSize = minSize + cargoSize; + msgSize = MESSAGE_OVERHEAD_SIZE + cargoSize; if ( msgSize <= remSize ) { - result = msgSize; + result = msgSize; // we found a complete message of this size } } @@ -477,7 +421,7 @@ switch ( message->hdr.msgID ) { case MSG_ID_OFF_BUTTON_PRESS: - userConfirmOffButton( message->cargo[0] ); + handleOffButtonConfirmMsgFromUI( message ); // ***************************** TEST CODE ****************************** // TODO - remove later #if 1 @@ -491,41 +435,3 @@ break; } } - - -/************************************************************************* -**************** SUITE OF SEND MESSAGE FUNCTIONS BELOW ******************* -*************************************************************************/ - -/************************************************************************* - * @brief sendOffButtonMsgToUI - * The sendOffButtonMsgToUI function constructs an off button msg to the UI \n - * and queues the msg for transmit on the appropriate CAN channel. - * @details - * Inputs : none - * Outputs : Off button msg constructed and queued. - * @param none - * @return TRUE if msg successfully queued for transmit, FALSE if not - *************************************************************************/ -BOOL sendOffButtonMsgToUI( void ) -{ - BOOL result; - MESSAGE_T msg; - U32 msgSize; - U08 data[sizeof(MESSAGE_WRAPPER_T)+1+CAN_MESSAGE_CARGO_SIZE]; // must hold full (wrapped) message + sync + any CAN padding - U08 *dataPtr = data; - - // create a message record - blankMessage( &msg ); - msg.hdr.msgID = MSG_ID_OFF_BUTTON_PRESS; - msg.hdr.cargoLen = 0; - - // serialize the message (w/ sync, CRC, and appropriate CAN padding) - msgSize = serializeMessage( msg, data ); - - // add serialized message data to appropriate comm buffer - result = addToCommBuffer( COMM_BUFFER_OUT_CAN_HD_2_UI, dataPtr, msgSize ); - - return result; -} -