def test_read_addr_bytes_exception(self, mock_smbus):
        """
        Test the routine with an exception raised whilst reading each address and byte values
        """
        #BUG: Need to check the exception raised

        self.log.debug("[TEST] test_read_addr_bytes_exception")
        mock_smbus().read_byte_data.side_effect = [
            Exception('IOError'), 0x26, 0x27
        ]
        calls = [call(0x45, 0x12), call(0x45, 0x13), call(0x45, 0x14)]
        comms = i2c_comms()
        result = comms.read_data_bytes(0x45, 0x12, 3)
        mock_smbus().read_byte_data.assert_has_calls(calls)
        self.assertEqual(result, [])

        mock_smbus().read_byte_data.side_effect = [
            0x26, Exception('IOError'), 0x27
        ]
        calls = [call(0x45, 0x12), call(0x45, 0x13), call(0x45, 0x14)]
        comms = i2c_comms()
        result = comms.read_data_bytes(0x45, 0x12, 3)
        mock_smbus().read_byte_data.assert_has_calls(calls)
        self.assertEqual(result, [])

        mock_smbus().read_byte_data.side_effect = [
            0x26, 0x27, Exception('IOError')
        ]
        calls = [call(0x45, 0x12), call(0x45, 0x13), call(0x45, 0x14)]
        comms = i2c_comms()
        result = comms.read_data_bytes(0x45, 0x12, 3)
        mock_smbus().read_byte_data.assert_has_calls(calls)
        self.assertEqual(result, [])
def main():
    """
    This routine controls the programming of the iCof ID-IoT EEPROM utilising the functions that already exist
    in the 2 classes associated with the project
    Key Steps
        - Menu to choose the sensor to use
        - Reprogram the chip
        - Write a map version
        - re-generate the checksum
    
    """

    global log
    # Create a logger with the name of the function
    logging.config.dictConfig(LOG_CFG)
    log = logging.getLogger()
    log.info("[ID_IoT] Logging started")

    # Load the data from the EEPROM on the ID-Iot chip
    i2c_connection = i2c_comms()
    eeprom_data = cls_EEPROM.ID_IoT(i2c_connection, read_chip=False)

    selected = choose_chip()  # returns the record number

    write_connection_data(eeprom_data, selected)

    write_map_version(eeprom_data)

    return
    def test_init_fails(self, mock_smbus):
        """
        test the instationisation of the module
        """
        self.log.debug("[TEST] test_init_fails")
        mock_smbus.side_effect = Exception('FileNotFoundError')

        with self.assertRaises(SystemExit):
            result = i2c_comms()  # returns the object i2c_comms
    def test_init(self, mock_smbus):
        """
        test the instationisation of the module
        """
        self.log.debug("[TEST] test_init")
        # need to mock _open_port
        result = i2c_comms()  # returns the object i2c_comms

        self.assertIsInstance(result, object)
    def test_write_addr_bytes_exception(self, mock_smbus):
        """
        Test the routine with an exception raised whilst writing each address and byte values
        """
        #BUG: Need to check the exception raised
        self.log.debug("[TEST] test_write_addr_bytes_exception")
        mock_smbus().write_byte_data.side_effect = Exception('IOError')
        comms = i2c_comms()
        result = comms.write_data_bytes(0x45, 0x12, [0x33, 0x34, 0x35])
        mock_smbus().write_byte_data.assert_called_with(0x45, 0x12, 0x33)
        self.assertEqual(result, False)

        mock_smbus().write_byte_data.side_effect = [True, Exception('IOError')]
        calls = [call(0x45, 0x12, 0x33), call(0x45, 0x13, 0x34)]
        comms = i2c_comms()
        result = comms.write_data_bytes(0x45, 0x12, [0x33, 0x34, 0x35])
        mock_smbus().write_byte_data.assert_has_calls(calls)
        self.assertEqual(result, False)
def SetupSensor():
    """
    This function performs the following
    - Connect to the ID_IoT chip and retive all the infromation
    - load the correct icog file
    - Transfer the configuration information into the loaded icog
    - set the global variable icog to the required icog
    returns the instance of the icog and the eeprom
    """

    # Load the data from the EEPROM on the ID-Iot chip
    i2c_connection = i2c_comms()
    eeprom = cls_EEPROM.ID_IoT(i2c_connection)

    # Load the correct sensor file
    icog_file = eeprom.ReturnSensorCommsFile()
    gbl_log.info("[CTRL] Loading the iCog Comms file:%s" % icog_file)

    try:
        # This doesn't initialise the iCog, just loads it
        imported_icog = importlib.import_module(icog_file)
    except:
        gbl_log.critical(
            "[CTRL] Importing of the iCog file:%s failed, contact support" %
            icog_file)
        gbl_log.exception("[CTRL] Start Routine Exception Data")
        sys.exit()
    gbl_log.info("[CTRL] Loading of the iCog file:%s succeeded (%s)" %
                 (icog_file, imported_icog))

    # Open the right bus connection to work with the icog connected
    reqd_bus = eeprom.ReturnBusType()
    if reqd_bus == SS.SPI:
        icog_connection = SPi_comms()
    elif reqd_bus == SS.SERIAL:
        icog_connection = Serial_Comms()
    elif reqd_bus == SS.I2C:
        icog_connection = i2c_connection
    else:
        gbl_log.critical(
            "[CTRL] Required Connection bus:%s is not supported, contact Support"
            % reqd_bus)
        sys.exit()
    gbl_log.info("[CTRL] Required Connection bus:%s loaded" % icog_connection)

    # Retrieve Calibration Data and pass it to the iCog
    calib_data = eeprom.ReturnCalibrationData()

    gbl_log.debug("[CTRL] calibration data being passed into iCog:%s" %
                  calib_data)
    # Initialise the iCog
    icog = imported_icog.iCog(icog_connection, calib_data)
    gbl_log.debug("[CTRL] Initialised icog:%s" % icog)

    return (icog, eeprom)
    def test_write_addr_byte_valid(self, mock_smbus):
        """
        Test the routine with a valid address and byte values
        """
        self.log.debug("[TEST] test_write_addr_byte_valid")
        mock_smbus().write_byte.return_value = True

        comms = i2c_comms()
        result = comms.write_data_byte(0x60, 0x10, 0x80)
        mock_smbus().write_byte_data.assert_called_with(0x60, 0x10, 0x80)
        self.assertEqual(result, True)
 def test_repeated_start_set(self):
     """
     Check the repeated start setting is correct
     """
     self.log.debug("[TEST] test_repeated_start_set")
     result = ''
     comms = i2c_comms()
     response = comms.repeated_start(repeated=True)
     with open('/sys/module/i2c_bcm2708/parameters/combined', 'r') as check:
         result = check.read(1)
     self.assertEqual(result, 'Y')
     self.assertTrue(response)
 def test_read_addr_bytes_valid(self, mock_smbus):
     """
     Test the routine with a valid range of bytes
     Request a range and check
     """
     self.log.debug("[TEST] test_read_addr_bytes_valid")
     mock_smbus().read_byte_data.side_effect = [0x26, 0x27, 0x28]
     calls = [call(0x45, 0x12), call(0x45, 0x13), call(0x45, 0x14)]
     comms = i2c_comms()
     result = comms.read_data_bytes(0x45, 0x12, 3)
     mock_smbus().read_byte_data.assert_has_calls(calls)
     self.assertEqual(result, [0x26, 0x27, 0x28])
    def test_write_addr_byte_exception(self, mock_smbus):
        """
        Test the routine with an exception raised
        """
        #BUG: Need to check the exception raised

        self.log.debug("[TEST] test_write_addr_byte_exception")
        mock_smbus().write_byte.side_effect = Exception('IOError')

        comms = i2c_comms()
        result = comms.write_data_byte(0x50, 0x16, 0xff)
        mock_smbus().write_byte_data.assert_called_with(0x50, 0x16, 0xff)

        self.assertEqual(result, True)
 def test_write_addr_bytes_valid(self, mock_smbus):
     """
     Test the routine with a valid range of bytes
     Request a range and check
     """
     self.log.debug("[TEST] test_write_addr_bytes_valid")
     mock_smbus().write_byte_data.side_effect = [True, True, True]
     calls = [
         call(0x45, 0x12, 0x20),
         call(0x45, 0x13, 0x21),
         call(0x45, 0x14, 0x22)
     ]
     comms = i2c_comms()
     result = comms.write_data_bytes(0x45, 0x12, [0x20, 0x21, 0x22])
     mock_smbus().write_byte_data.assert_has_calls(calls)
     self.assertEqual(result, True)