Ejemplo n.º 1
0
    def test_write_control_file(self):
        """
        Tests that `write_control_file` returns the expected exceptions, return values, or outputs.
        """

        # Branch 1: Tests that `write_control_file` raises the expected exception when `nuc_wmi.CONTROL_FILE` doesnt
        #           exist. Assumes we are testing on a system without the driver installed.
        with self.assertRaises((IOError, OSError)) as err:
            read_control_file()

        # Reset
        with open(self.control_file.name, 'w') as fout:
            fout.truncate()

        # Branch 2, 3: Tests that the number of bytes written to the control file are padded to 5 bytes, and that
        #              integer byte list is properly written to the control file.
        byte_list = [0x0D, 0x0E, 0x0A, 0x0D]
        expected_byte_string='0d 0e 0a 0d 00'

        write_control_file(byte_list, control_file=self.control_file.name)

        with open(self.control_file.name, 'r') as fin:
            written_byte_string = fin.read()

        self.assertEqual(expected_byte_string, written_byte_string)

        # Reset
        with open(self.control_file.name, 'w') as fout:
            fout.truncate()

        # Branch 4: Tests that an string byte list is properly written to the control file
        byte_list = [str(0x0D), str(0x0E), str(0x0A), str(0x0D)]
        expected_byte_string='0d 0e 0a 0d 00'

        write_control_file(byte_list, control_file=self.control_file.name)

        with open(self.control_file.name, 'r') as fin:
            written_byte_string = fin.read()

        self.assertEqual(expected_byte_string, written_byte_string)

        # Reset
        with open(self.control_file.name, 'w') as fout:
            fout.truncate()

        # Branch 5: Test that byte strings outside of the 0-255 value raise an exception
        byte_list = [0xFFF]

        with self.assertRaises(NucWmiError) as err:
            write_control_file(byte_list, control_file=self.control_file.name)

        self.assertEqual(str(err.exception), 'Error (NUC LED byte values must be 0-255)')
Ejemplo n.º 2
0
    def test_read_control_file(self):
        """
        Tests that `read_control_file` returns the expected exceptions, return values, or outputs.
        """

        # Branch 1: Test that `read_control_file` raises exception when `nuc_wmi.CONTROL_FILE` doesnt exist
        #           Assumes we are testing on a system without the driver installed.
        with self.assertRaises((IOError, OSError)) as err:
            read_control_file()

        # Reset
        with open(self.control_file.name, 'w') as fout:
            fout.truncate()

        # Branch 2: Test that `read_control_file` raise exception if less than 4 bytes are read
        with open(self.control_file.name, 'w') as fout:
            fout.write("00 00 00\n\x00")

        with self.assertRaises(NucWmiError) as byte_list_len_err:
            read_control_file(control_file=self.control_file.name)

        self.assertEqual(str(byte_list_len_err.exception),
                         'NUC WMI control file did not return an expected 4 bytes')

        # Reset
        with open(self.control_file.name, 'w') as fout:
            fout.truncate()

        # Branch 3: Test that overriding control file with existing file works
        with open(self.control_file.name, 'w') as fout:
            fout.write("0D 0E 0A 0D\n\x00")

        byte_list = (0x0D, 0x0E, 0x0A, 0x0D)

        self.assertEqual(read_control_file(control_file=self.control_file.name), byte_list)

        # Reset
        with open(self.control_file.name, 'w') as fout:
            fout.truncate()

        # Branch 4: Test that overriding control file with non existing file raises exception
        non_existent_file = NamedTemporaryFile()

        non_existent_file.close()

        with self.assertRaises((IOError, OSError)) as err:
            read_control_file(control_file=non_existent_file.name)

        # Branch 5: Test that exception is raised if NUC WMI returns a hex byte outside 0-255 range
        with open(self.control_file.name, 'w') as fout:
            fout.write("FFF 0E 0A 0D\n\x00")

        with self.assertRaises(NucWmiError) as err:
            read_control_file(control_file=self.control_file.name)

        self.assertEqual(str(err.exception), 'NUC WMI returned hex byte outside of 0-255 range')
Ejemplo n.º 3
0
def get_led_indicator_option(led_type, control_file=None):
    """
    Get the current indicator option for the LED type.

    Args:
      control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
      led_type: The LED type for which to retrieve the current indicator option.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    Returns:
       `nuc_wmi.LED_INDICATOR_OPTION` index of the current LED indicator option.
    """

    get_led_indicator_option_byte_list = [
        METHOD_ID,
        GET_LED_TYPE.index('get_led_indicator_option'),
        led_type
    ]

    write_control_file(get_led_indicator_option_byte_list, control_file=control_file)

    (
        error_code,
        led_indicator_option,
        reserved_byte_1,
        reserved_byte_2
    ) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(RETURN_ERROR[error_code])

    return led_indicator_option
Ejemplo n.º 4
0
def set_led_indicator_option(led_type, led_indicator_option, control_file=None):
    """
    Set the LED indicator option for the specified LED type,

    Args:
      control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
      led_indicator_option: The LED indicator option to set for the LED type.
      led_type: The LED type for which to set the LED indicator option.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    """

    set_led_indicator_option_byte_list = [METHOD_ID, led_type, led_indicator_option]

    write_control_file(set_led_indicator_option_byte_list, control_file=control_file)

    (
        error_code,
        reserved_byte_1,
        reserved_byte_2,
        reserved_byte_3
    ) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))
Ejemplo n.º 5
0
def wmi_interface_spec_compliance_version(control_file=None):
    """
    Returns the version for the WMI interface spec compliance.

    Args:
       control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    Returns:
       Tuple of two bytes representing the version number.
    """

    wmi_version_byte_list = [
        METHOD_ID,
        VERSION_TYPE.index('wmi_interface_spec_compliance_version')
    ]

    write_control_file(wmi_version_byte_list, control_file=control_file)

    (error_code, version_byte_1, version_byte_2,
     reserved_byte) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(
            RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))

    return tuple([version_byte_2, version_byte_1])
Ejemplo n.º 6
0
def query_leds(control_file=None):
    """
    List all LED types supported.

    Args:
      control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    Returns:
       List of available `nuc_wmi.LED_TYPE` indexes.
    """

    query_leds_byte_list = [METHOD_ID, QUERY_TYPE.index('query_leds')]

    write_control_file(query_leds_byte_list, control_file=control_file)

    # Bitmap [0:7], [8:15], [16:23]
    (error_code, led_type_bitmap_1, led_type_bitmap_2,
     led_type_bitmap_3) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(
            RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))

    led_type_bitmaps = [
        led_type_bitmap_3, led_type_bitmap_2, led_type_bitmap_1
    ]
    led_type_bitmap = byte_list_to_bitmap(led_type_bitmaps)[::-1]

    return [
        index for index, bit in enumerate(led_type_bitmap)
        if int(bit) and index < len(LED_TYPE['new'])
    ]
Ejemplo n.º 7
0
def get_led(led, control_file=None):
    """
    Get legacy LED state with regard to brightness, frequency, and color.

    Args:
       control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
       led: Selects the legacy LED to get the state for.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    Returns:
       Tuple of legacy brightness, frequency, and color of the select LED.
    """

    get_led_byte_list = [METHOD_ID, led]

    write_control_file(get_led_byte_list, control_file=control_file)

    (
        error_code,
        brightness,
        frequency,
        color
    ) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))

    return tuple([brightness, frequency, color])
Ejemplo n.º 8
0
def query_led_control_items(led_type, led_indicator_option, control_file=None):
    """
    Query the LED control items for the LED indicator option of the LED type.

    Args:
      control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
      led_indicator_option: The LED indicator option to use for the LED type when querying for the LED control items.
      led_type: The LED type for which to query the LED control items.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    Returns:
       List of available `nuc_wmi.CONTROL_ITEM` indexes for the specified LED type and LED indicator option.
    """

    led_color_type = query_led_color_type(led_type, control_file=control_file)

    query_led_control_item_byte_list = [
        METHOD_ID,
        QUERY_TYPE.index('query_led_control_items'), led_type,
        led_indicator_option
    ]

    write_control_file(query_led_control_item_byte_list,
                       control_file=control_file)

    # Bitmap [0:7], [8:15], [16:23]
    (error_code, led_control_item_bitmap_1, led_control_item_bitmap_2,
     led_control_item_bitmap_3) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(
            RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))

    led_control_item_bitmaps = [
        led_control_item_bitmap_3, led_control_item_bitmap_2,
        led_control_item_bitmap_1
    ]
    led_control_item_bitmap = byte_list_to_bitmap(
        led_control_item_bitmaps)[::-1]

    if led_indicator_option == LED_INDICATOR_OPTION_DISABLED or \
       CONTROL_ITEM[led_indicator_option][led_color_type] is None:
        return []

    return [
        index for index, bit in enumerate(led_control_item_bitmap) if int(bit)
        and index < len(CONTROL_ITEM[led_indicator_option][led_color_type])
    ]
Ejemplo n.º 9
0
def switch_led_type(led_color_group, control_file=None):
    """
    Switches the LED color group type.

    Args:
       led_color_group: The LED color group type to set.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    """

    switch_led_byte_list = [METHOD_ID, led_color_group]

    write_control_file(switch_led_byte_list, control_file=control_file)

    (error_code, reserved_byte_1, reserved_byte_2,
     reserved_byte_3) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(
            RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))
Ejemplo n.º 10
0
def get_led_control_item(led_type, led_indicator_option, control_item, control_file=None):
    """
    Get the current control item value for the control item of the indicator option for the specified LED type.

    Args:
      control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
      control_item: The control item of the specified LED type indicator option for which to retrieve the value.
      led_indicator_option: The indicator option for the specified LED type for which to retrieve the current control
                            item value.
      led_type: The LED type for which to retrieve the current control item.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    Returns:
       `nuc_wmi.CONTROL_ITEM` value for the control item of the indicator option for the specified LED type.
    """

    get_led_control_item_byte_list = [
        METHOD_ID,
        GET_LED_TYPE.index('get_led_control_item'),
        led_type,
        led_indicator_option,
        control_item
    ]

    write_control_file(get_led_control_item_byte_list, control_file=control_file)

    (
        error_code,
        control_item_value,
        reserved_byte_1,
        reserved_byte_2
    ) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))

    return control_item_value
Ejemplo n.º 11
0
def save_led_config(control_file=None):
    """
    Send a save LED configuration LED app notification.

    Args:
      control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    """

    notification_byte_list = [
        METHOD_ID, NOTIFICATION_TYPE.index('save_led_config')
    ]

    write_control_file(notification_byte_list, control_file=control_file)

    (error_code, reserved_byte_1, reserved_byte_2,
     reserved_byte_3) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(
            RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))
Ejemplo n.º 12
0
def query_led_color_type(led_type, control_file=None):
    """
    Query the LED color type for the LED type.

    Args:
      control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
      led_type: The LED type for which to query the LED color type.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    Returns:
      `nuc_wmi.LED_COLOR_TYPE` index of current LED color type.
    """

    query_led_color_byte_list = [
        METHOD_ID,
        QUERY_TYPE.index('query_led_color_type'), led_type
    ]

    write_control_file(query_led_color_byte_list, control_file=control_file)

    # Bitmap [0:7], [8:15], [16:23]
    (error_code, led_color_type_bitmap_1, led_color_type_bitmap_2,
     led_color_type_bitmap_3) = read_control_file(control_file=control_file)

    if error_code > 0:
        raise NucWmiError(
            RETURN_ERROR.get(error_code, 'Error (Unknown NUC WMI error code)'))

    led_color_type_bitmaps = [
        led_color_type_bitmap_3, led_color_type_bitmap_2,
        led_color_type_bitmap_1
    ]
    led_color_type_bitmap = byte_list_to_bitmap(led_color_type_bitmaps)

    return int(led_color_type_bitmap, 2)
Ejemplo n.º 13
0
def set_led(led, brightness, frequency, color, control_file=None):
    """
    Set LED state with regard to brightness, frequency, and color.

    Args:
       brightness: Controls the brightness level of the LED.
       color: Sets legacy RGB-color for LED.
       control_file: Sets the control file to use if provided, otherwise `nuc_wmi.CONTROL_FILE` is used.
       frequency: Sets the legacy LED frequency.
       led: Selects the legacy LED to set a state for.
    Exceptions:
       Raises `nuc_wmi.NucWmiError` exception if kernel module returns an error code,
       or if `read_control_file` or `write_control_file` raise an exception.
    """

    set_led_byte_list = [METHOD_ID, led, brightness, frequency, color]

    write_control_file(set_led_byte_list, control_file=control_file)

    (brightness_error, frequency_error, color_error,
     reserved_byte) = read_control_file(control_file=control_file)

    if brightness_error > 0:
        raise NucWmiError(
            RETURN_ERROR.get(brightness_error,
                             'Error (Unknown NUC WMI error code)'))

    if frequency_error > 0:
        raise NucWmiError(
            RETURN_ERROR.get(frequency_error,
                             'Error (Unknown NUC WMI error code)'))

    if color_error > 0:
        raise NucWmiError(
            RETURN_ERROR.get(color_error,
                             'Error (Unknown NUC WMI error code)'))