def load_file(self, firmware_file):
        """Starts to upload the firmware.

        Args:
            firmware_file (:class:`blue_st_sdk.firmware_upgrade.utils.firmware_file.FirmwareFile`):
                Firmware file.

        Raises:
            :exc:`OSError` if the file is not found or is inaccessible.
            :exc:`ValueError` if the firmware file can not be read properly.
        """
        try:
            # Setting the firmware file.
            self._firmware_file = firmware_file

            # Computing the CRC of the firmware file content.
            self._firmware_crc = self._firmware_file.get_crc_32()

            # Creating the command to start the firmware upgrade.
            command = bytearray(self.FIRMWARE_UPGRADE_COMMAND) \
                + bytearray(LittleEndian.uint32_to_bytes(
                    self._firmware_file.get_size())) \
                + bytearray(LittleEndian.uint32_to_bytes(
                    self._firmware_crc))
        except (OSError, ValueError) as e:
            raise e

        # Opening the firmware file to send data packets.
        self._firmware_fd = self._firmware_file.open()

        # Starting the firmware upgrade.
        self._loading_file_status = LoadingFileStatus.CRC_CHECK
        self._firmware_upgrade_console._debug_console.write(command)
Ejemplo n.º 2
0
    def on_stdout_receive(self, debug_console, message):
        """Called whenever a message is received on the standard output.

        A message is received on the standard output in two cases:
          + When the device sends back the CRC received from the gateway.
          + When the device sends an ACK or a NACK message after receiving the
            firmware file and comparing the CRC computed on it with the CRC
            previously received from the gateway.

        Args:
            debug_console (object): Console that sends the message.
            message (str): The message received on the stdout console.

        Raises:
            :exc:`NotImplementedError` if the method has not been implemented.
        """
        if self._loading_file_status == LoadingFileStatus.CRC_CHECK:
            # Check whether the message received from the node contains the same
            # CRC code that we have sent.
            if message.encode('ISO-8859-1') != \
                LittleEndian.uint32_to_bytes(self._firmware_crc):
                self._on_load_error(FirmwareUpgradeError.TRANSMISSION_ERROR)

            # Sending firmware in blocks of packets.
            self._loading_file_status = LoadingFileStatus.ACK_CHECK
            self._number_of_packets_received = 0
            while self._firmware_file.get_size() - self._bytes_sent > 0:
                if not self._send_block():
                    self._on_load_error(
                        FirmwareUpgradeError.CORRUPTED_FILE_ERROR)
                    break

        elif self._loading_file_status == LoadingFileStatus.ACK_CHECK:
            # Transfer completed.
            #self._timeout.cancel()
            # Python 2.
            #if message.encode('ISO-8859-1').lower() == self.ACK_MSG.lower():
            # Python 3.
            if message.lower() == self.ACK_MSG.lower():
                self._on_load_complete()
            else:
                self._on_load_error(FirmwareUpgradeError.CORRUPTED_FILE_ERROR)