Example #1
0
    def monitor_update(self):
        ''' updates the monitor connected to this ecu
            
            Input:    -
            Output:   monitor_list    RefList    list of MonitorInputs
        '''
        # register Monitoring tags to track
        #G().register_eventline_tags(self._tags)

        items_1 = len(
            self.transp_lay.datalink_lay.controller.receive_buffer.items)
        items_2 = self.transp_lay.datalink_lay.transmit_buffer_size

        G().mon(
            self.monitor_list,
            MonitorInput(items_1, MonitorTags.BT_ECU_RECEIVE_BUFFER,
                         self._ecu_id, self.sim_env.now))
        G().mon(
            self.monitor_list,
            MonitorInput(items_2, MonitorTags.BT_ECU_TRANSMIT_BUFFER,
                         self._ecu_id, self.sim_env.now))

        self.monitor_list.clear_on_access(
        )  # on the next access the list will be cleared
        return self.monitor_list.get()
Example #2
0
    def receive_msg(self):

        while True:

            # receive from lower layer
            [message_id, message_data
             ] = yield self.sim_env.process(self.transp_lay.receive_msg())

            # receiver information
            print("\n\nRECEIVER\nTime: " + str(self.sim_env.now) +
                  "--Communication Layer: \nI am ECU " + self._ecu_id +
                  "\nReceived message:\n - ID: " + str(message_id))

            # Assume it takes 0.5 seconds to e.g. decrypt this message
            uid = uuid.uuid4()
            # BEFORE PROCESSING
            print("\nECU " + str(self._ecu_id) +
                  "Time before message received: " + str(self.sim_env.now))
            G().mon(
                self.monitor_list,
                MonitorInput([], "AUTH_RECEIVE_TIME_BEFORE_DECRYPTION",
                             self._ecu_id, self.sim_env.now, 123, message_id,
                             message_data.get(), message_data.padded_size, 432,
                             uid))
            ''' Perform asymmetric decryption of the incoming message using its public key, e.g. lasting 0.5 seconds'''
            # get the public key of the sender
            senders_public_key = PublicKeyManager().get_key(
                message_data.sender_id)
            # use this key for decryption - also checks if this key is still valid
            received_cipher_message = message_data.get()
            clear_message = encryption_tools.asy_decrypt(
                received_cipher_message, senders_public_key, self.sim_env.now)
            [received_timestamp, received_hashed_message,
             received_message] = clear_message
            yield self.sim_env.timeout(0.5)
            ''' Perform symmetric decryption, e.g. takes 0.02 seconds'''
            [received_symmetric_key,
             received_symmetrically_encrypted_message] = received_message
            received_clear_message = encryption_tools.sym_decrypt(
                received_symmetrically_encrypted_message,
                received_symmetric_key)
            yield self.sim_env.timeout(0.02)
            ''' Verify received hash of the message, e.g. takes 0.01 second '''
            hashed_message = HashedMessage(str(received_message),
                                           HashMechEnum.MD5)
            is_same_hash = hashed_message.same_hash(received_hashed_message)
            yield self.sim_env.timeout(0.01)

            # AFTER PROCESSING
            print("\nECU " + str(self._ecu_id) +
                  "Time after message received: " + str(self.sim_env.now))
            G().mon(
                self.monitor_list,
                MonitorInput([], "AUTH_RECEIVE_TIME_AFTER_DECRYPTION",
                             self._ecu_id, self.sim_env.now, 123, message_id,
                             message_data.get(), message_data.padded_size, 432,
                             uid))

        # push to higher layer
        return [message_id, received_clear_message]
 def receive_msg(self):
     
     while True:
                     
         # receive from lower layer
         [message_id, message_data] = yield self.sim_env.process(self.transp_lay.receive_msg())        
     
         # receiver information    
         print("\n\nRECEIVER\nTime: "+str(self.sim_env.now)+"--Communication Layer: \nI am ECU " + self._ecu_id + "\nReceived message:\n - ID: " + str(message_id))
         
         # Assume it takes 0.5 seconds to e.g. decrypt this message
         uid = uuid.uuid4()
         # BEFORE PROCESSING
         print("\nECU "+ str(self._ecu_id) +"Time before message received: "+ str(self.sim_env.now))
         G().mon(self.monitor_list, MonitorInput([], "AUTH_RECEIVE_TIME_BEFORE_DECRYPTION", self._ecu_id, self.sim_env.now, 123, message_id, message_data.get(), message_data.padded_size, 432, uid))           
         
         ''' receive and verify certificate '''
         [received_message, received_certificate] = message_data.get()
         # verify certificate - which works here as they all have the same CA authority that signed the certificate
         certificate_valid = encryption_tools.certificate_trustworthy(received_certificate, self.my_root_certificates, self.sim_env.now)
     
         
         # AFTER PROCESSING
         print("\nECU "+ str(self._ecu_id) +"Time after message received: "+ str(self.sim_env.now))            
         G().mon(self.monitor_list, MonitorInput([], "AUTH_RECEIVE_TIME_AFTER_DECRYPTION", self._ecu_id, self.sim_env.now, 123, message_id, message_data.get(), message_data.padded_size, 432, uid))
         
     # push to higher layer
     return [message_id, message_data]
Example #4
0
 def push_constellation(self, ecu_groups, busses, bus_connections):
     ''' this method receives the initial constellation of the 
         environment and publishes it to the handlers 
     
         Input:  ecu_groups        list        list of lists: [[ecu_list, ecu_spec],[ecu_list, ecu_spec],...]
                 busses            list        list of lists: [[bus_list, ecu_spec],[bus_list, ecu_spec],...]
                 bus_connections   list        list of lists [[bus_id, ecu_id], [bus_id, ecu_id],...]
         Output: -
     '''        
     # set eventline handlers
     for ecu in ecu_groups: 
         try:
             General().register_eventline_tags(ecu[0][0].ecuSW.comm_mod._tags)
         except:
             pass
     
     # push the constellation
     push_list = [MonitorInput([ecu_groups, busses, bus_connections], MonitorTags.CONSELLATION_INFORMATION, \
                         None, 0, None, None, None, None, None, None)]
     self._input_handler.publish(push_list, RefList())        
         
     # push initial ecu ids
     
     push_list = MonitorInput(APICore()._ecu_list_from_groups(ecu_groups), MonitorTags.ECU_ID_LIST, \
                         None, 0, None, None, None, None, None, None)
     self._input_handler.publish(push_list, RefList())   
Example #5
0
    def monitor_update(self):
        ''' returns the input for the monitor 
        
            Input:    -
            Output:   monitor_list    list    List of MonitorInput objects
        '''

        lst = []

        for i in range(len(self._connected_busses)):

            try:
                # buffer information
                items_1 = len(self._datalink_layer[i].controller.
                              receive_buffer.get_bytes())
                items_2 = len(self._datalink_layer[i].controller.
                              transmit_buffer.get_bytes())

                lst.append(
                    MonitorInput(
                        items_1, MonitorTags.BT_ECU_RECEIVE_BUFFER,
                        "BUS (%s) GW_%s" %
                        (self._connected_busses[i].comp_id, self._ecu_id),
                        self.sim_env.now))
                lst.append(
                    MonitorInput(
                        items_2, MonitorTags.BT_ECU_TRANSMIT_BUFFER,
                        "BUS (%s) GW_%s" %
                        (self._connected_busses[i].comp_id, self._ecu_id),
                        self.sim_env.now))
            except:
                pass

        return lst
Example #6
0
    def _monitor_transmission_start(self):
        ''' notes the start time when this message was put on the bus
            
            Input:  -
            Output: -
        '''
        # extract information
        uid = uuid.uuid4()
        tag = MonitorTags.CB_PROCESSING_MESSAGE
        c_id = self.comp_id
        sender_id = self.current_message.sender_id
        msg_id = self.current_message.message_identifier
        msg_uid = self.current_message.data.unique_id
        data = self.current_message.data.get()

        # extract further information
        msg = self.current_message
        size = self.current_message_length_bit / 8
        self.current_message.data.unique_id = msg_uid

        # send to monitor
        G().mon(
            self.monitor_list,
            MonitorInput(data, tag, c_id, self.sim_env.now, sender_id, msg_id,
                         msg, size, msg_id, uid.hex))
        return data, c_id, sender_id, msg_id, msg, size, uid
Example #7
0
 def _monitor_transmission_end(self, mon_out):
     ''' notes the end time when this message was put on the bus
         
         Input:  -
         Output: -
     '''
     G().mon(self.monitor_list, MonitorInput(mon_out[0], MonitorTags.CB_DONE_PROCESSING_MESSAGE, \
                                             mon_out[1], self.sim_env.now, mon_out[2], mon_out[3], \
                                             mon_out[4], mon_out[5], -1, mon_out[6].hex))
Example #8
0
    def receive_msg(self):

        while True:

            # receive from lower layer
            [message_id, message_data
             ] = yield self.sim_env.process(self.transp_lay.receive_msg())

            # receiver information
            print("\n\nRECEIVER\nTime: " + str(self.sim_env.now) +
                  "--Communication Layer: \nI am ECU " + self._ecu_id +
                  "\nReceived message:\n - ID: " + str(message_id) +
                  "\n - Content: " + message_data.get())

            # Assume it takes 0.5 seconds to e.g. decrypt this message
            uid = uuid.uuid4()
            # BEFORE PROCESSING
            print("\nECU " + str(self._ecu_id) +
                  "Time before message received: " + str(self.sim_env.now))
            G().mon(
                self.monitor_list,
                MonitorInput([], "AUTH_RECEIVE_TIME_BEFORE_DECRYPTION",
                             self._ecu_id, self.sim_env.now, 123, message_id,
                             message_data.get(), message_data.padded_size, 432,
                             uid))

            yield self.sim_env.timeout(0.5)

            # AFTER PROCESSING
            print("\nECU " + str(self._ecu_id) +
                  "Time after message received: " + str(self.sim_env.now))
            G().mon(
                self.monitor_list,
                MonitorInput([], "AUTH_RECEIVE_TIME_AFTER_DECRYPTION",
                             self._ecu_id, self.sim_env.now, 123, message_id,
                             message_data.get(), message_data.padded_size, 432,
                             uid))

        # push to higher layer
        return [message_id, message_data]
Example #9
0
    def send_msg(self, sender_id, message_id, message):
        # Sender information
        print("\n\nSENDER - \nTime: " + str(self.sim_env.now) +
              "--Communication Layer: \nI am ECU " + sender_id +
              "\nSending message:\n - ID: " + str(message_id) +
              "\n - Content: " + message.get())

        # Message to be send
        print("\nSize of the message we want to send: " +
              str(message.padded_size))
        print("\nContent of the message: " + str(message.get()))

        # Assume it takes 0.2 seconds to e.g. encrypt this message
        uid = uuid.uuid4()
        # BEFORE PROCESSING
        print("\nECU " + str(self._ecu_id) + "Time before message sent: " +
              str(self.sim_env.now))
        G().mon(
            self.monitor_list,
            MonitorInput([], "AUTH_SEND_TIME_BEFORE_ENCRYPTION",
                         self._ecu_id, self.sim_env.now, 123, message_id,
                         message.get(), message.padded_size, 432, uid))

        yield self.sim_env.timeout(0.2)

        # AFTER PROCESSING
        G().mon(
            self.monitor_list,
            MonitorInput([], "AUTH_SEND_TIME_AFTER_ENCRYPTION",
                         self._ecu_id, self.sim_env.now, 123, message_id,
                         message.get(), message.padded_size, 432, uid))
        print("\nECU " + str(self._ecu_id) + "Time after message sent: " +
              str(self.sim_env.now))

        # Send message - here send your message with your message_id
        yield self.sim_env.process(
            self.transp_lay.send_msg(sender_id, message_id, message))
Example #10
0
    def send_msg(self, sender_id, message_id, message):
        # Sender information

        print("\n\nSENDER - \nTime: " + str(self.sim_env.now) +
              "--Communication Layer: \nI am ECU " + sender_id +
              "\nSending message:\n - ID: " + str(message_id) +
              "\n - Content: " + message.get())

        # Message to be send
        print("\nSize of the message we want to send: " +
              str(message.padded_size))
        print("\nContent of the message: " + str(message.get()))

        # Assume it takes 0.2 seconds to e.g. encrypt this message
        uid = uuid.uuid4()
        # BEFORE PROCESSING
        print("\nECU " + str(self._ecu_id) + "Time before message sent: " +
              str(self.sim_env.now))
        G().mon(
            self.monitor_list,
            MonitorInput([], "AUTH_SEND_TIME_BEFORE_ENCRYPTION",
                         self._ecu_id, self.sim_env.now, 123, message_id,
                         message.get(), message.padded_size, 432, uid))
        ''' Perform symmetric encryption (and key generation): send the message encrypted with a created key
        which e.g. takes 0.01 second'''
        algorithm = SymAuthMechEnum.AES
        key_length = AuKeyLengthEnum.bit_128
        algorithm_mode = SymAuthMechEnum.CBC
        sym_key = encryption_tools.sym_get_key(algorithm, key_length,
                                               algorithm_mode)
        # encrypt the message with the symmetric key we just created
        clear_message = message.get()
        cipher = encryption_tools.sym_encrypt(clear_message, sym_key)
        sym_cipher_message = [sym_key, cipher]
        yield self.sim_env.timeout(0.01)
        ''' Hash the message we want to send and send the hash with the message, e.g. takes 0.01 second '''
        hashed_message = HashedMessage(str(sym_cipher_message),
                                       HashMechEnum.MD5)
        yield self.sim_env.timeout(0.01)
        ''' Perform asymmetric encryption: Encrypt my private key which takes e.g. 0.2 seconds'''
        timestamp = self.sim_env.now
        encrypted_size = 50  # byte - usualy calculated from the size of the original message and the encryption algorithm
        clear_text_of_message = [timestamp, hashed_message, sym_cipher_message]
        cipher_message = encryption_tools.asy_encrypt(clear_text_of_message,
                                                      self.priv_key)
        cipher_message.valid_till = timestamp + 5  # add optional validity (e.g. 5 seconds)
        wrapped_cipher_message = SegData(cipher_message, encrypted_size)
        yield self.sim_env.timeout(0.2)

        # AFTER PROCESSING
        G().mon(
            self.monitor_list,
            MonitorInput([], "AUTH_SEND_TIME_AFTER_ENCRYPTION",
                         self._ecu_id, self.sim_env.now, 123, message_id,
                         message.get(), message.padded_size, 432, uid))
        print("\nECU " + str(self._ecu_id) + "Time after message sent: " +
              str(self.sim_env.now))

        # Send message - here send your message with your message_id
        yield self.sim_env.process(
            self.transp_lay.send_msg(sender_id, message_id,
                                     wrapped_cipher_message))