/************************************************************************** * * Copyright (c) 2024-2024 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file Messaging.c * * @author (last) Sean * @date (last) 30-Jul-2024 * * @author (original) Sean * @date (original) 30-Jul-2024 * ***************************************************************************/ #include #include // For memcpy() #include "reg_system.h" // Used to access system register to reset processor on request #include "Compatible.h" #include "Messaging.h" #include "OperationModes.h" #include "Utilities.h" /** * @addtogroup Messaging * @{ */ // ********** private definitions ********** #define MAX_MSGS_BLOCKED_FOR_XMIT 8 ///< Maximum number of messages to block transmission for. #pragma pack(push,1) /// Payload record structure for block message transmission request. typedef struct { U16 blockedMessages[ MAX_MSGS_BLOCKED_FOR_XMIT ]; ///< Blocked messages. } BLOCKED_MSGS_DATA_T; #pragma pack(pop) /// Array of CAN communication buffers to respond on (aligned with enum Comm_Buffers). static const COMM_BUFFER_T tdResponseBuffers[ NUM_OF_COMM_BUFFERS ] = { COMM_BUFFER_NOT_USED, ///< CAN message boxes start at 1 so we will not use this buffer COMM_BUFFER_NOT_USED, ///< Buffer for outgoing TD alarm messages so no response buffer COMM_BUFFER_OUT_CAN_TD_ALARM, ///< Buffer for responding to incoming DD alarm messages COMM_BUFFER_OUT_CAN_TD_ALARM, ///< Buffer for responding to incoming RO alarm messages COMM_BUFFER_OUT_CAN_TD_ALARM, ///< Buffer for responding to incoming UI alarm messages COMM_BUFFER_NOT_USED, ///< Buffer for outgoing TD to DD messages so no response buffer COMM_BUFFER_OUT_CAN_TD_2_DD, ///< Buffer for responding to incoming DD to HD messages COMM_BUFFER_NOT_USED, ///< Buffer for outgoing TD to UI messages so no response buffer COMM_BUFFER_OUT_CAN_TD_2_UI, ///< Buffer for responding to incoming UI to HD messages COMM_BUFFER_NOT_USED, ///< Buffer for outgoing TD broadcast messages so no response buffer COMM_BUFFER_OUT_CAN_TD_BROADCAST, ///< Buffer for responding to incoming DD broadcast messages COMM_BUFFER_OUT_CAN_TD_BROADCAST, ///< Buffer for responding to incoming RO broadcast messages COMM_BUFFER_OUT_CAN_TD_BROADCAST, ///< Buffer for responding to incoming UI broadcast messages COMM_BUFFER_OUT_CAN_PC, ///< Buffer for responding to incoming PC to TD messages COMM_BUFFER_NOT_USED, ///< Buffer for outgoing HD to PC messages so no response buffer }; typedef BOOL (*MsgFuncPtr)( MESSAGE_T* ); /// Message handling function lookup table static const U16 MSG_FUNCTION_HANDLER_LOOKUP[] = { MSG_ID_TESTER_LOGIN_REQUEST, MSG_ID_TD_SOFTWARE_RESET_REQUEST }; /// Message handling function table static const MsgFuncPtr MSG_FUNCTION_HANDLERS[] = { &handleTesterLogInRequest, &handleTDSoftwareResetRequest }; #define NUM_OF_FUNCTION_HANDLERS (sizeof(MSG_FUNCTION_HANDLERS) / sizeof(MsgFuncPtr)) // ********** private data ********** static BOOL testerLoggedIn = FALSE; ///< Flag indicates whether an external tester (connected PC) has sent a valid login message. static volatile U16 nextSeqNo = 1; ///< Value of sequence number to use for next transmitted message. /// List of message IDs that are requested not to be transmitted. static BLOCKED_MSGS_DATA_T blockedMessagesForXmit = { 0, 0, 0, 0, 0, 0, 0, 0 }; // ********** private function prototypes ********** static MsgFuncPtr getMsgHandler( U16 msgID ); static BOOL sendTestAckResponseMsg( MSG_ID_T msgID, BOOL ack ); static BOOL sendAckResponseMsg( MSG_ID_T msgID, COMM_BUFFER_T buffer, BOOL ack ); /*********************************************************************//** * @brief * The serializeMessage function serializes a given message into a given * array of bytes. A sequence # is added to the message here and the ACK * bit of the sequence # is set if ACK is required per parameter. A sync byte * is inserted at the beginning of the message and an 8-bit CRC is appended to * the end of the message. The message is queued for transmission in the given buffer. * @details Inputs: blockedMessagesForXmit * @details Outputs: given data array populated with serialized message data and queued for transmit. * @param msg message to serialize * @param buffer outgoing buffer that message should be queued in * @param ackReq is an acknowledgement from receiver required? * @return size (in bytes) of serialized message populated in given data array. *************************************************************************/ U32 serializeMessage( MESSAGE_T msg, COMM_BUFFER_T buffer, BOOL ackReq ) { BOOL result = 0; BOOL blocked = FALSE; U32 msgSize = 0; U32 sizeMod, sizePad; U32 i; U08 crc; U08 data[ MAX_ACK_MSG_SIZE ]; // Byte array to populate with message data // Check to see if tester has requested this message not be transmited if ( TRUE == isTestingActivated() ) { U32 i; for ( i = 0; i < MAX_MSGS_BLOCKED_FOR_XMIT; i++ ) { if ( msg.hdr.msgID == blockedMessagesForXmit.blockedMessages[ i ] ) { blocked = TRUE; break; } } } // Serialize and queue message for transmission unless this message is blocked if ( blocked != TRUE ) { // Prefix data with message sync byte data[ msgSize++ ] = MESSAGE_SYNC_BYTE; // Set sequence # and ACK bit (unless this is an ACK to a received message) if ( msg.hdr.msgID != MSG_ID_ACK_MESSAGE_THAT_REQUIRES_ACK ) { // Thread protect next sequence # access & increment _disable_IRQ(); msg.hdr.seqNo = nextSeqNo; nextSeqNo = INC_WRAP( nextSeqNo, MIN_MSG_SEQ_NO, MAX_MSG_SEQ_NO ); _enable_IRQ(); if ( TRUE == ackReq ) { msg.hdr.seqNo *= -1; } } // Calculate message CRC crc = crc8( (U08*)(&msg), sizeof( MESSAGE_HEADER_T ) + msg.hdr.payloadLen ); // Serialize message header data memcpy( &data[ msgSize ], &( msg.hdr ), sizeof( MESSAGE_HEADER_T ) ); msgSize += sizeof( MESSAGE_HEADER_T ); // Serialize message payload (only used bytes per payloadLen field) memcpy( &data[ msgSize ], &( msg.payload ), msg.hdr.payloadLen ); msgSize += msg.hdr.payloadLen; // Add 8-bit CRC data[ msgSize++ ] = crc; // Pad with zero bytes to get length a multiple of CAN_MESSAGE_PAYLOAD_SIZE (8) sizeMod = msgSize % CAN_MESSAGE_PAYLOAD_SIZE; sizePad = ( sizeMod == 0 ? 0 : CAN_MESSAGE_PAYLOAD_SIZE - sizeMod ); for ( i = 0; i < sizePad; i++ ) { data[ msgSize++ ] = 0; } #ifndef _RELEASE_ // if ( getSoftwareConfigStatus( SW_CONFIG_DISABLE_ACK_ERRORS ) != SW_CONFIG_ENABLE_VALUE ) #endif { // If ACK required, add to pending ACK list if ( TRUE == ackReq ) { if ( FALSE == addMsgToPendingACKList( &msg, buffer, data, msgSize ) ) { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_MSG_PENDING_ACK_LIST_FULL, (U32)(msg.hdr.msgID) ) } } } // Add serialized message data to appropriate out-going comm buffer result = addToCommBuffer( buffer, data, msgSize ); } else { result = TRUE; // If message blocked, return successful transmission } return result; } /*********************************************************************//** * @brief * The sendACKMsg function constructs and queues for transmit an ACK message * for a given received message. * @details Inputs: none * @details Outputs: ACK message queued for transmit on broadcast CAN channel. * @param message message to send an ACK for * @return TRUE if ACK message queued successfully, FALSE if not *************************************************************************/ BOOL sendACKMsg( MESSAGE_T *message ) { BOOL result; MESSAGE_T msg; // Create a message record blankMessage( &msg ); // Send ACK back with same seq. #, but w/o ACK bit msg.hdr.seqNo = message->hdr.seqNo * -1; // ACK messages always have this ID msg.hdr.msgID = MSG_ID_ACK_MESSAGE_THAT_REQUIRES_ACK; // ACK messages always have no payload msg.hdr.payloadLen = 0; // Serialize and queue the message for transmit on broadcast channel result = serializeMessage( msg, COMM_BUFFER_OUT_CAN_TD_BROADCAST, ACK_NOT_REQUIRED ); return result; } /*********************************************************************//** * @brief * The sendAckResponseMsg function constructs a simple response * message for a handled message and queues it for transmit on the * appropriate CAN channel. * @details Inputs: none * @details Outputs: response message constructed and queued for transmit. * @param msgID ID of handled message that we are responding to * @param buffer outgoing buffer that message should be queued in * @param ack TRUE if test message was handled successfully, FALSE if not * @return TRUE if response message successfully queued for transmit, FALSE if not *************************************************************************/ static BOOL sendAckResponseMsg( MSG_ID_T msgID, COMM_BUFFER_T buffer, BOOL ack ) { BOOL result; MESSAGE_T msg; ACK_RESPONSE_PAYLOAD_T cmd; cmd.acknowledgement = ack; // Create a message record blankMessage( &msg ); msg.hdr.msgID = msgID; msg.hdr.payloadLen = sizeof( ACK_RESPONSE_PAYLOAD_T ); memcpy( &msg.payload, &cmd, sizeof( ACK_RESPONSE_PAYLOAD_T ) ); // Serialize the message (w/ sync, CRC, and appropriate CAN padding) and add serialized message data to appropriate comm buffer result = serializeMessage( msg, buffer, ACK_NOT_REQUIRED ); return result; } /*********************************************************************//** * @brief * The getMsgHandler function finds the appropriate handler function * for the given message. * @details Inputs: MSG_FUNCTION_HANDLER_LOOKUP[], MSG_FUNCTION_HANDLERS[] * @details Outputs: none * @param msgID ID of message to find handler function for * @return pointer to appropriate function to handle given message *************************************************************************/ static MsgFuncPtr getMsgHandler( U16 msgID ) { U32 i; MsgFuncPtr func = 0; // Search for the index associated with the given override command message ID and then use index to get the handling function for ( i = 0; i < NUM_OF_FUNCTION_HANDLERS; i++ ) { if ( MSG_FUNCTION_HANDLER_LOOKUP[i] == msgID ) { func = MSG_FUNCTION_HANDLERS[i]; break; } } return func; } /*********************************************************************//** * @brief * The handleIncomingMessage function calls the appropriate handler function * for the given message. * @details Inputs: none * @details Outputs: Appropriate message handler function called * @param message Incoming message to handle * @return none *************************************************************************/ void handleIncomingMessage( MESSAGE_T *message ) { BOOL ack = FALSE; // assume we will NAK until we have successful handling of message COMM_BUFFER_T respBuffer = tdResponseBuffers[ message->in_buffer ]; // if Dialin message, ensure Dialin is logged in before processing it if ( ( message->hdr.msgID <= MSG_ID_FIRST_TD_TESTER_MESSAGE ) || ( TRUE == isTestingActivated() ) ) { MsgFuncPtr msgFuncPtr; // Find the appropriate message handling function to call msgFuncPtr = getMsgHandler( message->hdr.msgID ); // Ensure a handler is found before calling it if ( msgFuncPtr != 0 ) { // Call the appropriate message handling function ack = (*msgFuncPtr)( message ); } } // ACK/NAK request if ( message->hdr.msgID < MSG_ID_FIRST_TD_TESTER_MESSAGE ) { if ( respBuffer != COMM_BUFFER_NOT_USED ) { sendAckResponseMsg( (MSG_ID_T)message->hdr.msgID, respBuffer, ack ); } } else { sendTestAckResponseMsg( (MSG_ID_T)message->hdr.msgID, ack ); } } // *********************************************************************** // ***************** Message Handling Helper Functions ******************** // *********************************************************************** // *********************************************************************** // ***************** Message Sending Helper Functions ******************** // *********************************************************************** /*********************************************************************//** * @brief * The sendEvent function constructs an TD event message to the UI and * queues the msg for transmit on the appropriate CAN channel. * @details Inputs: none * @details Outputs: TD event msg constructed and queued. * @param event Enumeration of event type that occurred * @param dat1 First data associated with event * @param dat2 Second data associated with event * @return TRUE if msg successfully queued for transmit, FALSE if not *************************************************************************/ BOOL sendEvent( TD_EVENT_ID_T event, EVENT_DATA_T dat1, EVENT_DATA_T dat2 ) { BOOL result; MESSAGE_T msg; EVENT_PAYLOAD_T eventStruct; eventStruct.event = (U32)event; eventStruct.dataType1 = (U32)dat1.dataType; eventStruct.data1 = dat1.data; eventStruct.dataType2 = (U32)dat2.dataType; eventStruct.data2 = dat2.data; // Create a message record blankMessage( &msg ); msg.hdr.msgID = MSG_ID_TD_EVENT; // The payload length is the event ID, 2 event datas and the events data types for each of the event data msg.hdr.payloadLen = sizeof( EVENT_PAYLOAD_T ); memcpy( &msg.payload, &eventStruct, sizeof( EVENT_PAYLOAD_T ) ); // Serialize the message (w/ sync, CRC, and appropriate CAN padding) and add serialized message data to appropriate comm buffer result = serializeMessage( msg, COMM_BUFFER_OUT_CAN_TD_2_UI, ACK_NOT_REQUIRED ); return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The isTestingActivated function determines whether a tester has successfully * logged in to activate testing functionality. * @details Inputs: testerLoggedIn * @details Outputs: none * @return TRUE if a tester has logged in to activate testing, FALSE if not *************************************************************************/ BOOL isTestingActivated( void ) { return testerLoggedIn; } /*********************************************************************//** * @brief * The setTesterStatusToLoggedOut function sets the status of the tester to * logged out. * @details Inputs: none * @details Outputs: testerLoggedIn * @return none *************************************************************************/ void setTesterStatusToLoggedOut( void ) { testerLoggedIn = FALSE; } /*********************************************************************//** * @brief * The sendTestAckResponseMsg function constructs a simple response * message for a handled test message and queues it for transmit on the * appropriate UART channel. * @details Inputs: none * @details Outputs: response message constructed and queued for transmit. * @param msgID ID of handled message that we are responding to * @param ack TRUE if test message was handled successfully, FALSE if not * @return TRUE if response message successfully queued for transmit, FALSE if not *************************************************************************/ static BOOL sendTestAckResponseMsg( MSG_ID_T msgID, BOOL ack ) { BOOL result; MESSAGE_T msg; // Create a message record blankMessage( &msg ); msg.hdr.msgID = msgID; msg.hdr.payloadLen = sizeof( U08 ); msg.payload[ 0 ] = (U08)ack; // Serialize the message (w/ sync, CRC, and appropriate CAN padding) and add serialized message data to appropriate comm buffer result = serializeMessage( msg, COMM_BUFFER_OUT_CAN_PC, ACK_NOT_REQUIRED ); return result; } /*********************************************************************//** * @brief * The handleTesterLogInRequest function handles a request to login as a * tester. * @details Inputs: none * @details Outputs: message handled * @param message a pointer to the message to handle * @return TRUE if log in successful, FALSE if not *************************************************************************/ BOOL handleTesterLogInRequest( MESSAGE_T *message ) { // Verify pass code // TODO - placeholder - how do we want to authenticate tester? if ( ( 3 == message->hdr.payloadLen ) && ( 0x31 == message->payload[ 0 ] ) && ( 0x32 == message->payload[ 1 ] ) && ( 0x33 == message->payload[ 2 ] ) ) { testerLoggedIn = TRUE; checkInFromUI(); // Allow tasks to begin normal processing when tester has logged in // Set the dialin checkin time stamp until the first checkin message is received from dailin setDialinCheckInTimeStamp(); } else { testerLoggedIn = FALSE; } sendTestAckResponseMsg( (MSG_ID_T)message->hdr.msgID, testerLoggedIn ); return testerLoggedIn; } /*********************************************************************//** * @brief * The handleTDSoftwareResetRequest function handles a request to reset the * TD firmware processor. * @details Inputs: none * @details Outputs: message handled * @param message a pointer to the message to handle * @return FALSE if reset command rejected (won't return if reset successful) *************************************************************************/ BOOL handleTDSoftwareResetRequest( MESSAGE_T *message ) { BOOL result = FALSE; // Verify payload length if ( 0 == message->hdr.payloadLen ) { // S/w reset of processor result = TRUE; // Reset will prevent this from getting transmitted though #ifndef _VECTORCAST_ systemREG1->SYSECR = (0x2) << 14; // Reset processor #endif } sendTestAckResponseMsg( (MSG_ID_T)message->hdr.msgID, result ); return result; } /**@}*/