From b717c58d6856a00938ce1eb0ced9633a05367404 Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Sat, 22 May 2021 14:52:45 -0400 Subject: [PATCH] keyboard_comm: error out if unsupported protocol --- src/main/python/keyboard_comm.py | 13 +++++++++++++ src/main/python/main_window.py | 17 +++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/python/keyboard_comm.py b/src/main/python/keyboard_comm.py index 2bc3d10..7419316 100644 --- a/src/main/python/keyboard_comm.py +++ b/src/main/python/keyboard_comm.py @@ -12,6 +12,9 @@ from macro_action import SS_TAP_CODE, SS_DOWN_CODE, SS_UP_CODE, ActionText, Acti from unlocker import Unlocker from util import MSG_LEN, hid_send, chunks +SUPPORTED_VIA_PROTOCOL = [-1, 9] +SUPPORTED_VIAL_PROTOCOL = [-1, 0, 1, 2, 3] + CMD_VIA_GET_PROTOCOL_VERSION = 0x01 CMD_VIA_GET_KEYBOARD_VALUE = 0x02 CMD_VIA_SET_KEYBOARD_VALUE = 0x03 @@ -53,6 +56,10 @@ CMD_VIAL_LOCK = 0x08 BUFFER_FETCH_CHUNK = 28 +class ProtocolError(Exception): + pass + + def macro_deserialize_v1(data): """ Deserialize a single macro, protocol version 1 @@ -205,6 +212,10 @@ class Keyboard: data = self.usb_send(self.dev, struct.pack("B", CMD_VIA_GET_PROTOCOL_VERSION), retries=20) self.via_protocol = struct.unpack(">H", data[1:3])[0] + def check_protocol_version(self): + if self.via_protocol not in SUPPORTED_VIA_PROTOCOL or self.vial_protocol not in SUPPORTED_VIAL_PROTOCOL: + raise ProtocolError() + def reload_layout(self, sideload_json=None): """ Requests layout data from the current device """ @@ -235,6 +246,8 @@ class Keyboard: payload = json.loads(lzma.decompress(payload)) + self.check_protocol_version() + self.definition = payload if "vial" in payload: diff --git a/src/main/python/main_window.py b/src/main/python/main_window.py index f44f34f..4a24380 100644 --- a/src/main/python/main_window.py +++ b/src/main/python/main_window.py @@ -10,6 +10,7 @@ from urllib.request import urlopen from editor_container import EditorContainer from firmware_flasher import FirmwareFlasher +from keyboard_comm import ProtocolError from keymap_editor import KeymapEditor from keymaps import KEYMAPS from layout_editor import LayoutEditor @@ -216,12 +217,16 @@ class MainWindow(QMainWindow): self.current_device = self.devices[idx] if self.current_device is not None: - if self.current_device.sideload: - self.current_device.open(self.sideload_json) - elif self.current_device.via_stack: - self.current_device.open(self.via_stack_json["definitions"][self.current_device.via_id]) - else: - self.current_device.open(None) + try: + if self.current_device.sideload: + self.current_device.open(self.sideload_json) + elif self.current_device.via_stack: + self.current_device.open(self.via_stack_json["definitions"][self.current_device.via_id]) + else: + self.current_device.open(None) + except ProtocolError: + QMessageBox.warning(self, "", "Unsupported protocol version!\n" + "Please download latest Vial from https://get.vial.today/") if isinstance(self.current_device, VialKeyboard) \ and self.current_device.keyboard.keyboard_id in EXAMPLE_KEYBOARDS: