Index: DialityCoreCanProtocol.py =================================================================== diff -u -r4a04642902b240b68710677ed9c745d78f338ef6 -r7e8f523c6ecb0a8fe78714e66507af7bc1e286cb --- DialityCoreCanProtocol.py (.../DialityCoreCanProtocol.py) (revision 4a04642902b240b68710677ed9c745d78f338ef6) +++ DialityCoreCanProtocol.py (.../DialityCoreCanProtocol.py) (revision 7e8f523c6ecb0a8fe78714e66507af7bc1e286cb) @@ -19,79 +19,358 @@ import can import math from time import sleep +import sys -class LongDialityPacketBuilder: +class DenaliMessage: + BYTE_ORDER = 'little' + START_BYTE = 0xA5 + + START_INDEX = 0 + MSG_SEQ_INDEX = 1 + MSG_ID_INDEX = 3 + + PAYLOAD_LENGTH_INDEX = 5 + PAYLOAD_START_INDEX = 6 + + PAYLOAD_LENGTH_FIRST_PACKET = 1 + HEADER_LENGTH = 6 + + PACKET_LENGTH = 8 + CRC_LENGTH = 1 + MAX_MSG_ID_NUMBER = 65535 + + MAX_NUMBER_OF_PAYLOAD_BYTES = 254 + + CRC_LIST = [ + 0, 49, 98, 83, 196, 245, 166, 151, 185, 136, 219, 234, 125, 76, 31, 46, + 67, 114, 33, 16, 135, 182, 229, 212, 250, 203, 152, 169, 62, 15, 92, 109, + 134, 183, 228, 213, 66, 115, 32, 17, 63, 14, 93, 108, 251, 202, 153, 168, + 197, 244, 167, 150, 1, 48, 99, 82, 124, 77, 30, 47, 184, 137, 218, 235, + 61, 12, 95, 110, 249, 200, 155, 170, 132, 181, 230, 215, 64, 113, 34, 19, + 126, 79, 28, 45, 186, 139, 216, 233, 199, 246, 165, 148, 3, 50, 97, 80, + 187, 138, 217, 232, 127, 78, 29, 44, 2, 51, 96, 81, 198, 247, 164, 149, + 248, 201, 154, 171, 60, 13, 94, 111, 65, 112, 35, 18, 133, 180, 231, 214, + 122, 75, 24, 41, 190, 143, 220, 237, 195, 242, 161, 144, 7, 54, 101, 84, + 57, 8, 91, 106, 253, 204, 159, 174, 128, 177, 226, 211, 68, 117, 38, 23, + 252, 205, 158, 175, 56, 9, 90, 107, 69, 116, 39, 22, 129, 176, 227, 210, + 191, 142, 221, 236, 123, 74, 25, 40, 6, 55, 100, 85, 194, 243, 160, 145, + 71, 118, 37, 20, 131, 178, 225, 208, 254, 207, 156, 173, 58, 11, 88, 105, + 4, 53, 102, 87, 192, 241, 162, 147, 189, 140, 223, 238, 121, 72, 27, 42, + 193, 240, 163, 146, 5, 52, 103, 86, 120, 73, 26, 43, 188, 141, 222, 239, + 130, 179, 224, 209, 70, 119, 36, 21, 59, 10, 89, 104, 255, 206, 157, 172 + ] + + @staticmethod + def buildBasicMessage(channel_id=0, message=None): + """ + build a message using channel_id and dialin message + \param channel_id: integer with channel id + \param message: array of int with message + \return: dictionary with channel_id and message keys + """ + if message is None: + message = [] + return {'channel_id': channel_id, 'message': message} + + @staticmethod + def buildMessage(channel_id=0, message_id=0, payload=None): + """ + buildPacket builds a Diality Packet + + \param channel_id: is an integer with channel ID + \param message_id: is an integer indicating request ID + \param payload: list with payload. It does not include length + + \return dictionary with channel_id and 8-byte padded message + """ + + if payload is None: + payload = [] + + message_list = [DenaliCanMessenger.START_BYTE] + + if 0 <= message_id <= DenaliMessage.MAX_MSG_ID_NUMBER: + # Add a zero seq # for dialin messages for now + seq_no = 0 + message_seq_in_bytes = seq_no.to_bytes(2, byteorder=DenaliMessage.BYTE_ORDER) + + message_list += [message_seq_in_bytes[0]] + message_list += [message_seq_in_bytes[1]] + + # Add message ID as unsigned 16-bit # + message_id_in_bytes = message_id.to_bytes(2, byteorder=DenaliMessage.BYTE_ORDER) + + message_list += [message_id_in_bytes[0]] + message_list += [message_id_in_bytes[1]] + + else: + + return [] + + # Check payload length + payload_length = len(payload) + + # if payload is larger than 255 return nothing + if payload_length <= DenaliMessage.MAX_NUMBER_OF_PAYLOAD_BYTES: + # payload has to be a list + message_list += [payload_length] + else: + return [] + + message_list += payload + + # Because CRC does not include first byte, then we pass a list with out it + message_list += [DenaliMessage.crc8(message_list[1:])] + + message_list = DenaliMessage.__padMessageWithZeros(message_list) + + return DenaliMessage.buildBasicMessage(channel_id=channel_id, message=message_list) + + + @staticmethod + def crc8(message_list): + """ + returns the calculated crc from a message list + \param message_list: is a list of integer numbers containing the message + \return: integer containing a unsigned byte + """ + crc = 0 + for byte in message_list: + unsigned_byte = byte ^ crc + crc = DenaliMessage.CRC_LIST[unsigned_byte] + + return crc + + @staticmethod + def __padMessageWithZeros(message): + """ + returns a packet padded with zeros that guarantees that the packet is a multiple of 8 bytes. + \param message: packet that may or may not be multiple of 8 bytes + + \return: packet that is 8-byte multiple + + """ + message_length = len(message) + + # message must be multiple of 8 + if message_length % DenaliMessage.PACKET_LENGTH != 0: + # We need to pad the message with trailing zeros + add_these_many_zeros = math.ceil(message_length / DenaliMessage.PACKET_LENGTH) * \ + DenaliMessage.PACKET_LENGTH - message_length + + message += [0] * add_these_many_zeros + + return message + + @staticmethod + def getCRC(message): + """ + gets the CRC in message + + \param message: Dialin complete message with CRC + + \return: CRC in message + """ + + crc_index = DenaliMessage.PAYLOAD_START_INDEX + DenaliMessage.getPayloadLength(message) + + return message['message'][crc_index] + + @staticmethod + def verifyCRC(message): + """ + verifies message CRC equals calculated message CRC + + \return: TRUE if CRC matches, FALSE otherwise + """ + + if message is None: + return False + else: + message_list = message['message'] + + message_length = DenaliMessage.PAYLOAD_START_INDEX + DenaliMessage.getPayloadLength(message) + calculated_crc = DenaliMessage.crc8(message_list[1:message_length]) + actual_crc = DenaliMessage.getCRC(message) + + return calculated_crc == actual_crc + + @staticmethod + def getChannelID(message): + """ + returns request ID from message + + \param message: dictionary with channel_id and message keys + \return: integer with channel id + + """ + + return message['channel_id'] + + @staticmethod + def getMessageID(message): + """ + returns request ID from packet + + \param message: complete Diality Packet + + \return: integer with request ID + + """ + msg_id_array = message['message'][DenaliMessage.MSG_ID_INDEX: + DenaliMessage.PAYLOAD_LENGTH_INDEX] + + return int.from_bytes(msg_id_array, byteorder=DenaliMessage.BYTE_ORDER) + + @staticmethod + def getPayloadLength(message): + """ + returns payload length from message + \param message: dictionary with channel_id and message keys + \return: a unsigned payload length + """ + return message['message'][DenaliMessage.PAYLOAD_LENGTH_INDEX] + + @staticmethod + def getPayload(message): + """ + returns payload array from message + \param message: dictionary with channel_id and message keys + \return: a payload array if exist + """ + + payload_length = DenaliMessage.getPayloadLength(message) + + if payload_length == 0: + return None + else: + return message['message'][DenaliMessage.PAYLOAD_START_INDEX:] + + @staticmethod + def getTotalPackets(message, is_array=True): + """ + returns the number of packets needed to transmit Denali Message + \param message: dictionary with channel_id and message keys or raw message + \param is_array: True if message is an array and not a dictionary + \return: number of packets + """ + the_message = message if is_array else message['message'] + + return math.ceil((the_message[DenaliMessage.PAYLOAD_LENGTH_INDEX] + + DenaliMessage.HEADER_LENGTH + DenaliMessage.CRC_LENGTH) / + DenaliMessage.PACKET_LENGTH) + + +class DenaliChannels: + # This are all the channels available + + hd_alarm_broadcast_ch_id = 0x001 + dg_alarm_broadcast_ch_id = 0x002 + ui_alarm_broadcast_ch_id = 0x004 + hd_to_dg_ch_id = 0x008 + dg_to_hd_ch_id = 0x010 + hd_to_ui_ch_id = 0x020 + hd_sync_broadcast_ch_id = 0x040 + dg_sync_broadcast_ch_id = 0x080 + ui_to_hd_ch_id = 0x100 + ui_sync_broadcast_ch_id = 0x200 + dialin_to_hd_ch_id = 0x400 + hd_to_dialin_ch_id = 0x401 + dialin_to_dg_ch_id = 0x402 + dg_to_dialin_ch_id = 0x403 + dialin_to_ui_ch_id = 0x404 + ui_to_dialin_ch_id = 0x405 + + +class LongDenaliMessageBuilder: + def __init__(self, can_message): """ - LongDialityPacketBuilder is a utility object that helps construct a diality packet + LongDialityMessageBuilder is a utility object that helps construct a diality message that is longer than 8 bytes. Basic principle is to construct an object with the - first 8 byte message which contains the length of the packet, and the later push the + first 8 byte message which contains the length of the message, and the later push the remaining messages. e.g., let's imagine a 3 message packet. - obj = LongDialityPacketBuilder(msg1) + obj = LongDialityMessageBuilder(msg1) - packet = obj.push(msg2), returns None + message = obj.push(msg2), returns None, message is not complete - packet = obj.push(msg3), return the packet which is the concatenation of msg1, msg2 and msg3 + message = obj.push(msg3), return the packet which is the concatenation of msg1, msg2 and msg3 - :param can_message: an 8 byte message needed to build a Diality packet. + \param can_message: an 8 byte message needed to build a diality message. - :returns object + \returns object """ self.__message = can_message - self.__number_of_can_message_needed = math.ceil((can_message[3] + 4) / 8) - self.__number_of_can_message_up_to_now = 1 + self.__number_of_can_packets_needed = DenaliMessage.getTotalPackets(can_message) + self.__number_of_can_packets_up_to_now = 1 - def push(self, can_message): + def push(self, can_message, first_packet=False): """ push appends the can_message to the current packet. - :param can_message: 8-byte message + \param can_message: 8-byte message - :return: None if the packet is not completed, otherwise returns the complete packet + \param first_packet: True if it is the first packet received + + \return: None if the packet is not completed, otherwise returns the complete packet """ - self.__message += can_message - self.__number_of_can_message_up_to_now += 1 + if first_packet: + self.__message = can_message + self.__number_of_can_packets_needed = DenaliMessage.getTotalPackets(can_message) + self.__number_of_can_packets_up_to_now = 1 - if self.__number_of_can_message_up_to_now == self.__number_of_can_message_needed: + else: + self.__message += can_message + self.__number_of_can_packets_up_to_now += 1 + + if self.__number_of_can_packets_up_to_now == self.__number_of_can_packets_needed: return_message = self.__message self.__message = None return return_message + else: return None -class DialityCanMessenger: - START_BYTE = 0xA5 +class DenaliCanMessenger: + START_BYTE = DenaliMessage.START_BYTE + DIALIN_MSG_RESP_TO = 1 # number of seconds to wait for a response to a sent command def __init__(self, can_interface='can0'): """ - DialityCanMesseger constructor + DenaliCanMessenger constructor - :param can_interface - string containing the can interface, e.g., 'can0" + \param can_interface - string containing the can interface, e.g., 'can0" - :returns DialityCanMessenger object + \returns DialityCanMessenger object """ - self.__canConnection = can.interface.Bus(can_interface, bustype='socketcan') + self.__bus = can.interfaces.socketcan.SocketcanBus(channel=can_interface) + self.__listener_buffer = can.BufferedReader() + self.__notifier = can.Notifier(self.__bus, [self.__listener_buffer]) self.__sendPacketRequestID = -1 self.__sendEvent = threading.Event() - self.__longPacketsBuilders = {} - self.__dialityPacket = None - self.__dialityResponsePacket = None - self.__dialityResponseChannelID = -1 + self.__longMessageBuilders = {} + self.__longMsgChannelIDSet = set() + self.__dialinMessageList = None + self.__lastSentMessage = None + self.__dialinCommandResponseMessage = None + self.__dialinResponseChannelID = -1 + self.__run = False self.__sync_response_dictionary = {} - if self.__canConnection is not None: - self.__serialListenerThread = threading.Thread(target=self.__listener) + if self.__bus is not None: + self.__serialListenerThread = threading.Thread(target=self.__listener, daemon=True) + else: self.__serialListenerThread = None print("Can connection is not valid") @@ -102,7 +381,7 @@ """ - if self.__canConnection is None: + if self.__bus is None: print("Cannot start can listener.") return else: @@ -116,12 +395,8 @@ """ self.__run = False - print("Can listener has stopped.") + print("\nCan listener has stopped.") - def __getRequestID(self, message): - - return int.from_bytes(message[1:3], byteorder='big', signed=False) - def __listener(self): """ listens for diality message on the can interface passed during construction. @@ -130,276 +405,236 @@ while self.__run: - message = self.__canConnection.recv(0.0) + message = self.__listener_buffer.get_message(0.0) - if message is not None and message.dlc == 8: + if message is not None: - # We have received a legit can message of 8 bytes + if message.dlc == DenaliMessage.PACKET_LENGTH: + # We have received a legit can message of 8 bytes + can_data = [b for b in message.data] + channel_id = message.arbitration_id + message_length = can_data[DenaliMessage.PAYLOAD_LENGTH_INDEX] - can_data = [b for b in message.data] - channel_id = message.arbitration_id - packet_length = can_data[3] + # if we are building a long message, then proceed to push it to the channel dictionary + if channel_id in self.__longMsgChannelIDSet: + self.__dialinMessageList = self.__longMessageBuilders[channel_id].push(can_data) - # We decide what to do with it + elif can_data[0] == DenaliMessage.START_BYTE and \ + message_length <= DenaliMessage.PAYLOAD_LENGTH_FIRST_PACKET: # This is a short packet + # This is the first time that we are building a message + self.__dialinMessageList = can_data # deliver the packet - if can_data[0] == self.START_BYTE and packet_length <= 4: # This is a short packet + elif can_data[0] == self.START_BYTE and \ + message_length > DenaliMessage.PAYLOAD_LENGTH_FIRST_PACKET: # Long packet start + # We are starting to build a long message, include it in the lonMsgChannelIDSet + self.__longMsgChannelIDSet.add(channel_id) - self.__dialityPacket = can_data # deliver the packet + if channel_id not in self.__longMessageBuilders.keys(): # if we don't have a builder. Create it! + self.__longMessageBuilders[channel_id] = LongDenaliMessageBuilder(can_data) + self.__dialinMessageList = None - elif can_data[0] == self.START_BYTE and packet_length > 4: # This is the start of a long packet + else: # if we do have a builder. This is the first time + self.__dialinMessageList = self.__longMessageBuilders[channel_id].push(can_data, first_packet=True) - if channel_id not in self.__longPacketsBuilders.keys(): # if we don't have a builder. Create it! + # Do we have a complete (long or short) Denali Message? + if self.__dialinMessageList is not None: + message_valid = True #assume true for now, set to false if CRC check fails below + complete_dialin_message = DenaliMessage.buildBasicMessage(channel_id=channel_id, + message=self.__dialinMessageList) + dialin_msg_id = DenaliMessage.getMessageID(complete_dialin_message) + dialin_ch_id = DenaliMessage.getChannelID(complete_dialin_message) - self.__longPacketsBuilders[channel_id] = __LongDialityPacketBuilder(can_data) - self.__dialityPacket = None + if dialin_ch_id in self.__longMsgChannelIDSet: + # We need to remove channel ID from the long message set + self.__longMsgChannelIDSet.remove(dialin_ch_id) - else: # this is the continuation of a long packet. A builder must have been created + # Need to verify CRC at this point + if DenaliMessage.verifyCRC(complete_dialin_message) is False: + # if verify is False, let's drop (ignore) this message + message_valid = False + dialin_ch_id = None + dialin_msg_id = None + sys.stderr.write("Incorrect CRC, received message: {}, crc: {}, calculated crc: {}\n".format( + self.__dialinMessageList, DenaliMessage.getCRC(complete_dialin_message), + DenaliMessage.crc8(self.__dialinMessageList))) - if channel_id in self.__longPacketsBuilders.keys(): - self.__dialityPacket = self.__longPacketBuilders[channel_id].push(can_data) + if message_valid == True: + # We first check if this is a response to a send request that is pending + if dialin_msg_id == self.__sendPacketRequestID: - # At this point we have a complete (long or short) Diality Packet + self.__dialinCommandResponseMessage = complete_dialin_message + self.__sendEvent.set() + self.__sendPacketRequestID = -1 - if self.__dialityPacket is not None: + # If it is not, this is a publication message and we need to call it's register function + elif dialin_ch_id in self.__sync_response_dictionary.keys() and \ + dialin_msg_id in self.__sync_response_dictionary[channel_id].keys(): - dialityPacketRequestID = DialityPacket.getRequestID(self.__dialityPacket) - self.__dialityPacket[0] = channel_id + self.__sync_response_dictionary[dialin_ch_id][dialin_msg_id](complete_dialin_message) - # We first check if this is a response to a send request that is pending + # Done with this message, let's get the next one + self.__dialinMessageList = None - if dialityPacketRequestID == self.__sendPacketRequestID: - - self.__dialityResponsePacket = self.__dialityPacket - self.__sendEvent.set() - self.__sendPacketRequestID = -1 - - # If it is not, this is a sync packet and we need to call it's register function - - elif channel_id in self.__sync_response_dictionary.keys() and \ - dialityPacketRequestID in self.__sync_response_dictionary[channel_id].keys(): - - self.__sync_response_dictionary[channel_id][dialityPacketRequestID](self.__dialityPacket) - - # Done with this packet, let's get the next one - - self.__dialityPacket = None - - else: - + else: #no new packets in receive buffer # We have received nothing, let's sleep 1 msec and let's check again - sleep(0.001) + sleep(0.01) - def registerSyncFunction(self, channel_id, request_id, function): + def registerReceivingPublicationFunction(self, channel_id, message_id, function): """ assign a function with packet parameter to an sync request id, e.g., def function(packet). - :param channel_id: can channel number where messages are received - :param request_id: Diality request ID in message - :param function: function reference + \param channel_id: can channel number where messages are received + \param message_id: Diality request ID in message + \param function: function reference """ # if the channel_id exist, we just update the dictionary for the channel_id if channel_id in self.__sync_response_dictionary.keys(): + self.__sync_response_dictionary[channel_id].update({message_id: function}) - self.__sync_response_dictionary[channel_id].update({request_id: function}) - # otherwise, we need to create the dictionary for the channel_id else: + self.__sync_response_dictionary[channel_id] = {message_id: function} - self.__sync_response_dictionary[channel_id] = {request_id: function} - - def send(self, channel_id, can_packet, time_out=1): + def send(self, built_message, time_out=DIALIN_MSG_RESP_TO): """ sends can_packet to channel_id. - :param channel_id: CAN channel ID - :param can_packet: list of integers with the Diality format - :param time_out: time it will wait for a response in seconds + \param built_message: message built using DialinMessage class + \param time_out: time it will wait for a response in seconds - :return: Diality Packet, it it times out it returns None + \returns: Diality Packet, it it times out it returns None """ - padded_can_packet = DialityPacket.padPacketWithZeros(can_packet) + msg_sent = False - padded_can_packet_length = padded_can_packet[3] + # keep trying to send message until we get a response + while msg_sent is not True: + self.__lastSentMessage = built_message - self.__sendPacketRequestID = DialityPacket.getRequestID(can_packet) + channel_id = DenaliMessage.getChannelID(built_message) - # A packet can be longer than 8 bytes, so we need to split it - # into 8 bytes messages. + padded_can_message_array = built_message['message'] - number_of_messages = math.ceil((padded_can_packet_length + 4) / 8) + self.__sendPacketRequestID = DenaliMessage.getMessageID(built_message) - # We are sending one message at a time on CAN + # A message can be longer than 8 bytes, so we need to split it + # into 8 bytes packets. - for n in range(number_of_messages): - message = padded_can_packet[n * 8:n * 8 + 8] + number_of_packets = DenaliMessage.getTotalPackets(padded_can_message_array) - # Sending one message at a time - msg = can.Message(arbitration_id=channel_id, - data=message, - is_extended_id=False) + # We are sending one message at a time on CAN - self.__canConnection.send(msg) + for n in range(number_of_packets): + packet = padded_can_message_array[n * DenaliMessage.PACKET_LENGTH: + (n + 1) * DenaliMessage.PACKET_LENGTH] - # Sending - self.__dialityResponsePacket = None + # Sending one packet at a time + packet = can.Message(arbitration_id=channel_id, + data=packet, + is_extended_id=False) - # After all message has been sent, we clear a flag - self.__sendEvent.clear() + #print(packet) +# self.__bus.send(packet, 0.01) + self.__bus.send(packet, 0) - # At this point, we sleep until the system times out or flag is set - didTimeOut = not self.__sendEvent.wait(time_out) + # Sending + self.__dialinCommandResponseMessage = None - # We are ready to send again. Reset request ID appropriately - self.__sendPacketRequestID = -1 + # After all message has been sent, we clear a flag + self.__sendEvent.clear() - # This value is None or it has a message depending of the listener - return self.__dialityResponsePacket + # At this point, we sleep until the system times out or flag is set + self.__sendEvent.wait(time_out) + if self.__dialinCommandResponseMessage is not None: + msg_sent = True + elif time_out == 0: + msg_sent = True + else: + print("No response. Re-sending message.") -class DialityPacket: + # We are ready to send again. Reset request ID appropriately + self.__sendPacketRequestID = -1 - @staticmethod - def buildPacket(request_id=0, cargo=[]): - """ - buildPacket builds a Diality Packet + # This value is None or it has a message depending of the listener + return self.__dialinCommandResponseMessage - :param request_id: is an integer indicating request ID - :param cargo: list with cargo. It does not include length - """ - - packet = [DialityCanMessenger.START_BYTE] - - if 0 <= request_id <= (2 ** 16 - 1): - # Make sure an int was passed - request_id_in_bytes = request_id.to_bytes(2, byteorder='big') - - packet += [request_id_in_bytes[0]] - packet += [request_id_in_bytes[1]] - - else: - - return [] - - # Check cargo length - cargo_length = len(cargo) - - # if cargo is larger than 255 return nothing - if cargo_length <= 255: - # cargo has to be a list - packet += [cargo_length] - else: - return [] - - packet += cargo - - return DialityPacket.padPacketWithZeros(packet) - - @staticmethod - def padPacketWithZeros(packet): - """ - returns a packet padded with zeros that guarantees that the packet is a multiple of 8 bytes. - :param packet: packet that may or may not be multiple of 8 bytes - - :return: packet that is 8-byte multiple - - """ - packet_length = len(packet) - - # message must be multiple of 8 - if packet_length % 8 != 0: - # We might need to patch the message with zeros - add_these_many_zeros = math.ceil(packet_length / 8) * 8 - packet_length - - packet += [0] * add_these_many_zeros - - return packet - - @staticmethod - def getRequestID(packet): - """ - returns request ID from packet - - :param packet: complete Diality Packet - - :return: integer with request ID - - """ - - return int.from_bytes(packet[1:3], byteorder='big') - - -def test_print_received_packet(packet, sync=False): - channel_id = packet[0] - packet[0] = 0xA5 - +def tst_print_received_packet(message, sync=False): + channel_id = message[0] + message[0] = DenaliMessage.START_BYTE introduction = "Received: " if sync: introduction = "Sync " + introduction - print(introduction, packet, " in channel: ", channel_id) + print(introduction, message, " in channel: ", channel_id) -def test_print_to_screen(packet): - if packet is None: +def tst_print_to_screen(message): + if message is None: print("Timeout!!!") else: - test_print_received_packet(packet) + tst_print_received_packet(message) -def test_function_for_sync(packet): - test_print_received_packet(packet, sync=True) +def tst_function_for_sync(message): + tst_print_received_packet(message, sync=True) -def test_print_sending_dg_board(channel_id, packet): - print("Sending to board: ", packet, " in channel: ", channel_id) +def tst_print_sending_dg_board(message): + print("Sending to board: ", message['message'], " in channel: ", message['channel_id']) -def test_print_sending_dg_sim(channel_id, packet): - print("Sending to DG simulator: ", packet, " in channel", channel_id, end=" ---> ") +def tst_print_sending_dg_sim(message): + print("Sending to DG simulator: ", message['message'], " in channel", message['channel_id'], end=" ---> ") if __name__ == "__main__": - test_messenger = DialityCanMessenger() - test_channel_id = 0x100 + test_messenger = DenaliCanMessenger() + test_channel_id = DenaliChannels.ui_to_hd_ch_id - test_received_channel_id = 0x20 - test_received_request_id = 0x100 + test_received_channel_id = DenaliChannels.hd_to_ui_ch_id + test_received_message_id = 0x100 - test_messenger.registerSyncFunction(test_received_channel_id, test_received_request_id, test_function_for_sync) + test_messenger.registerReceivingPublicationFunction(test_received_channel_id, test_received_message_id, + tst_function_for_sync) - test_dg_simulator_received_channel_id = 0x01 - test_dg_simulator_sync_req_id = 0x05 - test_dg_simulator_req_id = 0x03 + test_dg_simulator_received_channel_id = DenaliChannels.dialin_to_dg_ch_id + test_dg_simulator_sync_msg_id = 0x05 + test_dg_simulator_msg_id = 0x03 - test_messenger.registerSyncFunction(test_dg_simulator_received_channel_id, test_dg_simulator_sync_req_id, - test_function_for_sync) + test_messenger.registerReceivingPublicationFunction(test_dg_simulator_received_channel_id, + test_dg_simulator_sync_msg_id, + tst_function_for_sync) test_messenger.start() - test_packet = DialityPacket.buildPacket(0x100, [1]) - test_dg_packet = DialityPacket.buildPacket(test_dg_simulator_req_id, []) + test_msg = DenaliMessage.buildMessage(channel_id=1000, + message_id=0x01, + payload=[1]) + test_dg_msg = DenaliMessage.buildMessage(channel_id=test_dg_simulator_received_channel_id, + message_id=test_dg_simulator_msg_id, + payload=[]) sleep(3.0) - test_print_sending_dg_board(test_channel_id, test_packet) - test_response = test_messenger.send(test_channel_id, test_packet) - test_print_to_screen(test_response) + tst_print_sending_dg_board(test_msg) + test_response = test_messenger.send(test_msg) + tst_print_to_screen(test_response) sleep(3.0) - test_print_sending_dg_board(test_channel_id, test_packet) - test_response = test_messenger.send(test_channel_id, test_packet) - test_print_to_screen(test_response) + tst_print_sending_dg_board(test_msg) + test_response = test_messenger.send(test_msg) + tst_print_to_screen(test_response) sleep(3.0) - test_print_sending_dg_sim(test_dg_simulator_received_channel_id, test_dg_packet) - test_response = test_messenger.send(test_dg_simulator_received_channel_id, test_dg_packet) - test_print_to_screen(test_response) + tst_print_sending_dg_sim(test_dg_msg) + test_response = test_messenger.send(test_dg_msg) + tst_print_to_screen(test_response) sleep(3.0) - test_print_sending_dg_sim(test_dg_simulator_received_channel_id, test_dg_packet) - test_response = test_messenger.send(test_dg_simulator_received_channel_id, test_dg_packet) - test_print_to_screen(test_response) + tst_print_sending_dg_sim(test_dg_msg) + test_response = test_messenger.send(test_dg_msg) + tst_print_to_screen(test_response)