Example #1
0
 def task(self, args):
     device = self.find_matching_device(args)
     device.open()
     protocol.enter_bootloader(device)
     device.close()
Example #2
0
    def programDeviceHandler(self, device_path):
        target_device = self.tryOpenDevicePath(device_path)

        if target_device == None:
            self.abort_update(target_device)
            return

        programmingMode = self.fileSelectorWidget.getProgramingInfo()

        if is_bootloader_device(
                target_device
        ) and programmingMode != FileSelector.ScopeFirmware:
            error_msg_box(
                "Can only upload firmware while bootloader is running. "
                "Either reset it, or upload a firmware hex instead")
            self.abort_update(target_device)
            return

        if programmingMode == FileSelector.ScopeLayout:
            self.statusBar().showMessage("Started updating layout",
                                         timeout=STATUS_BAR_TIMEOUT)

            layout_file = self.fileSelectorWidget.getLayoutFile()

            if layout_file == '':
                error_msg_box("No layout file given.")
                self.abort_update(target_device)
                return
            else:
                pass

            layout_json_obj = None
            with open(layout_file) as file_obj:
                try:
                    layout_json_obj = yaml.safe_load(file_obj.read())
                except Exception as err:
                    error_msg_box("Syntax error in yaml file: " + str(err))
                    self.abort_update(target_device)
                    return

            device_info = protocol.get_device_info(target_device)
            layout_data, settings_data = self.process_layout(
                layout_json_obj, layout_file, device_info.id)
            if layout_data == None or settings_data == None:
                return

            protocol.update_layout_section(target_device, layout_data)
            protocol.update_settings_section(target_device,
                                             settings_data,
                                             keep_rf=True)
            protocol.reset_device(target_device)

            self.statusBar().showMessage("Finished updating layout",
                                         timeout=STATUS_BAR_TIMEOUT)
        elif programmingMode == FileSelector.ScopeDevice:
            layout_file = self.fileSelectorWidget.getRFLayoutFile()
            rf_file = self.fileSelectorWidget.getRFFile()
            target_id = self.fileSelectorWidget.getTargetID()

            self.statusBar().showMessage("Started updating RF settings",
                                         timeout=STATUS_BAR_TIMEOUT)

            if layout_file == '':
                error_msg_box("No layout file given.")
                self.abort_update(target_device)
                return
            elif rf_file == '':
                error_msg_box("No RF settings file given.")
                self.abort_update(target_device)
                return
            elif target_id == None:
                error_msg_box("No device id file given.")
                self.abort_update(target_device)
                return

            layout_json_obj = None
            rf_json_obj = None
            with open(layout_file) as file_obj:
                try:
                    layout_json_obj = yaml.safe_load(file_obj.read())
                except Exception as err:
                    error_msg_box("Syntax error in yaml file: " + str(err))
                    self.abort_update(target_device)
                    return
            with open(rf_file) as file_obj:
                try:
                    rf_json_obj = yaml.safe_load(file_obj.read())
                except Exception as err:
                    error_msg_box("Syntax error in yaml file: " + str(err))
                    self.abort_update(target_device)
                    return

            try:
                settings_gen = layout.parser.SettingsGenerator(
                    layout_json_obj, rf_json_obj)
            except ParseError as err:
                error_msg_box("Error Generating RF settings data: " + str(err))
                self.abort_update(target_device)
                return

            layout_data = settings_gen.gen_layout_section(target_id)
            settings_data = settings_gen.gen_settings_section(target_id)

            protocol.update_settings_section(target_device, settings_data)
            protocol.update_layout_section(target_device, layout_data)
            protocol.reset_device(target_device)

            self.statusBar().showMessage("Finished updating RF settings",
                                         timeout=STATUS_BAR_TIMEOUT)

        elif programmingMode == FileSelector.ScopeFirmware:
            fw_file = self.fileSelectorWidget.getFirmwareFile()

            self.statusBar().showMessage("Starting update firmware",
                                         timeout=STATUS_BAR_TIMEOUT)

            if fw_file == '':
                error_msg_box("No firmware file given.")
            else:

                if is_xusb_bootloader_device(target_device):
                    self.program_xusb_boot_firmware_hex(target_device, fw_file)
                elif is_keyplus_device(target_device):
                    try:
                        serial_num = target_device.serial_number
                        boot_vid, boot_pid = protocol.enter_bootloader(
                            target_device)

                        self.bootloaderProgramTimer = QTimer()
                        self.bootloaderProgramTimer.setInterval(3000)
                        self.bootloaderProgramTimer.setSingleShot(True)
                        self.bootloaderProgramTimer.timeout.connect(
                            lambda: self.programFirmwareHex(
                                boot_vid, boot_pid, serial_num, fw_file))
                        self.bootloaderProgramTimer.start()
                    except (easyhid.HIDException,
                            protocol.KBProtocolException):
                        error_msg_box(
                            "Programming hex file failed: '{}'".format(
                                fw_file))
        else:
            try:
                target_device.close()
            except:
                pass
            raise Exception("Unimplementend programming mode")
Example #3
0
    def programDeviceHandler(self, device_path):
        target_device = self.tryOpenDevicePath(device_path)

        if target_device == None:
            self.abort_update(target_device)
            return

        programmingMode = self.fileSelectorWidget.getProgramingInfo()

        if is_bootloader_device(
                target_device
        ) and programmingMode != FileSelector.ScopeFirmware:
            error_msg_box("The device's bootloader is running. "
                          "Choose 'Update Firmware' from the drop down box "
                          "to flash new firmware, or reset it to use to run "
                          "the currently loaded firmware (if any).")
            self.abort_update(target_device)
            return

        if programmingMode == FileSelector.ScopeLayout:
            target_device.close()
            kb = self.tryOpenDevicePath2(device_path)

            if kb == None:
                return

            self.statusBar().showMessage("Started updating layout",
                                         timeout=STATUS_BAR_TIMEOUT)

            layout_file = self.fileSelectorWidget.getLayoutFile()

            if layout_file == '':
                error_msg_box("No layout file given.")
                return

            try:
                kp_layout = KeyplusLayout()
                warnings = []
                kp_layout.from_yaml_file(layout_file, warnings=warnings)
                device_target = kb.get_device_target()
                settings_data = kp_layout.build_settings_section(device_target)
                layout_data = kp_layout.build_layout_section(device_target)
            except (KeyplusError, IOError) as err:
                error_msg_box(str(err))
                return

            print('#' * 80)
            print("settings_data:", type(settings_data), settings_data)
            print('#' * 80)
            print("layout_data:", type(layout_data), layout_data)
            print('#' * 80)
            hexdump.hexdump(bytes(settings_data))
            hexdump.hexdump(bytes(layout_data))

            with kb:
                old_name = copy.copy(kb.name)
                kb.update_settings_section(settings_data, keep_rf=True)
                kb.update_layout_section(layout_data)
                if old_name != kb.name:
                    kb.reset(reset_type=RESET_TYPE_HARDWARE)
                    needs_label_update = True
                else:
                    kb.reset(reset_type=RESET_TYPE_SOFTWARE)
                    needs_label_update = False

            if needs_label_update:
                for widget in self.deviceListWidget.deviceWidgets:
                    try:
                        widget.updateLabel()
                    except easyhid.HIDException:
                        pass

            if warnings != []:
                error_msg_box(
                    "The device was programmed successfully, but some "
                    "non-critical errors were encountered:\n" +
                    "\n".join([str(warn) for warn in warnings]),
                    title="Warnings",
                )

            self.statusBar().showMessage("Finished updating layout",
                                         timeout=STATUS_BAR_TIMEOUT)
        elif programmingMode == FileSelector.ScopeDevice:
            target_device.close()
            kb = self.tryOpenDevicePath2(device_path)
            if kb == None:
                return

            layout_file = self.fileSelectorWidget.getRFLayoutFile()
            rf_file = self.fileSelectorWidget.getRFFile()
            target_id = self.fileSelectorWidget.getTargetID()

            self.statusBar().showMessage("Started updating RF settings",
                                         timeout=STATUS_BAR_TIMEOUT)

            if layout_file == '':
                error_msg_box("No layout file given.")
                self.abort_update(target_device)
                return
            elif rf_file == '':
                error_msg_box("No RF settings file given.")
                self.abort_update(target_device)
                return
            elif target_id == None:
                error_msg_box("No device id file given.")
                self.abort_update(target_device)
                return

            try:
                kp_layout = KeyplusLayout()
                warnings = []
                kp_layout.from_yaml_file(layout_file,
                                         rf_file,
                                         warnings=warnings)
                device_target = kb.get_device_target()
                device_target.device_id = target_id
                settings_data = kp_layout.build_settings_section(device_target)
                layout_data = kp_layout.build_layout_section(device_target)
            except (KeyplusError, IOError) as err:
                error_msg_box(str(err))
                self.abort_update(target_device)
                return

            with kb:
                old_name = copy.copy(kb.name)
                kb.update_settings_section(settings_data, keep_rf=False)
                kb.update_layout_section(layout_data)
                if old_name != kb.name:
                    kb.reset(reset_type=RESET_TYPE_HARDWARE)
                    needs_label_update = True
                else:
                    kb.reset(reset_type=RESET_TYPE_SOFTWARE)
                    needs_label_update = False

            if needs_label_update:
                for widget in self.deviceListWidget.deviceWidgets:
                    try:
                        widget.updateLabel()
                    except easyhid.HIDException:
                        pass

            if warnings != []:
                error_msg_box(
                    "The device was programmed successfully, but some "
                    "non-critical errors were encountered:\n" +
                    "\n".join([str(warn) for warn in warnings]),
                    title="Warnings",
                )

            self.statusBar().showMessage("Finished updating RF settings",
                                         timeout=STATUS_BAR_TIMEOUT)

        elif programmingMode == FileSelector.ScopeFirmware:
            fw_file = self.fileSelectorWidget.getFirmwareFile()

            self.statusBar().showMessage("Starting update firmware",
                                         timeout=STATUS_BAR_TIMEOUT)

            if fw_file == '':
                error_msg_box("No firmware file given.")
            else:

                if is_xusb_bootloader_device(target_device):
                    self.program_xusb_boot_firmware_hex(target_device, fw_file)
                elif is_kp_boot_device(target_device):
                    self.program_kp_boot_32u4_firmware_hex(
                        target_device, fw_file)
                elif is_keyplus_device(target_device):
                    try:
                        serial_num = target_device.serial_number
                        boot_vid, boot_pid = protocol.enter_bootloader(
                            target_device)

                        self.bootloaderProgramTimer = QTimer()
                        self.bootloaderProgramTimer.setInterval(3000)
                        self.bootloaderProgramTimer.setSingleShot(True)
                        self.bootloaderProgramTimer.timeout.connect(
                            lambda: self.programFirmwareHex(
                                boot_vid, boot_pid, serial_num, fw_file))
                        self.bootloaderProgramTimer.start()
                    except (easyhid.HIDException,
                            protocol.KBProtocolException):
                        error_msg_box(
                            "Programming hex file failed: '{}'".format(
                                fw_file))
                else:
                    error_msg_box("This bootloader is currently unsupported")
        else:
            try:
                target_device.close()
            except:
                pass
            raise Exception("Unimplementend programming mode")