示例#1
0
 def query(self, data, timeout=None):
     if self.simulator:
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         s.connect(self.sock_settings)
         s.send((data+'\r\n').encode('utf-8'))
         s.setblocking(False)
         res = ""
         t0 = time.time()
         while not ("\r\n" in res):
             try:
                 raw = s.recv(10)
                 res += raw.decode("utf-8")
             except Exception as e:
                 time.sleep(0.1)
             if timeout is not None and time.time() > t0+timeout:
                 s.close()
                 raise DeviceBusyError("Timeout")
         s.close()
         res = res[:-2]
     else:
         self.ser.timeout=timeout
         self.ser.open()
         self.ser.write((data+'\r').encode('utf-8'))
         res = self.ser.read_until(b'\r').decode('utf-8')[:-1]
         self.ser.close()
     if res == 'user cancel':
         raise ActionCanceledError("User didn't confirm action")
     if "error" in res:
         raise BadArgumentError(res)
     return res
示例#2
0
 def func(*args, **kwargs):
     try:
         return f(*args, **kwargs)
     except ValueError as e:
         raise BadArgumentError(str(e))
     except JadeError as e:
         if e.code == -32000:  # CBOR_RPC_USER_CANCELLED
             raise ActionCanceledError("{} canceled by user".format(
                 f.__name__))
         elif e.code == -32602:  # CBOR_RPC_BAD_PARAMETERS
             raise BadArgumentError(e.message)
         elif e.code == -32603:  # CBOR_RPC_INTERNAL_ERROR
             raise DeviceFailureError(e.message)
         elif e.code == -32002:  # CBOR_RPC_HW_LOCKED
             raise DeviceConnectionError("Device is locked")
         elif e.code == -32003:  # CBOR_RPC_NETWORK_MISMATCH
             raise DeviceConnectionError("Network/chain selection error")
         elif e.code in [
                 -32600,
                 -32601,
                 -32001,
         ]:  # CBOR_RPC_INVALID_REQUEST, CBOR_RPC_UNKNOWN_METHOD, CBOR_RPC_PROTOCOL_ERROR
             raise DeviceConnectionError("Messaging/communiciation error")
         else:
             raise e
示例#3
0
 def func(*args: Any, **kwargs: Any) -> Any:
     try:
         return f(*args, **kwargs)
     except ValueError as e:
         raise BadArgumentError(str(e))
     except Cancelled:
         raise ActionCanceledError("{} canceled".format(f.__name__))
     except USBErrorNoDevice:
         raise DeviceConnectionError("Device disconnected")
示例#4
0
 def query(self, data: str, timeout: Optional[float] = None) -> str:
     """Send a text-based query to the device and get back the response"""
     res = self.dev.query(data, timeout)
     if res == "error: User cancelled":
         raise ActionCanceledError("User didn't confirm action")
     elif res.startswith("error: Unknown command"):
         raise UnavailableActionError(res[7:])
     elif res.startswith("error: "):
         raise BadArgumentError(res[7:])
     return res
示例#5
0
 def func(*args, **kwargs):  # type: ignore
     """ Wraps f, mapping exceptions. """
     try:
         return f(*args, **kwargs)
     except UserAbortException:
         raise ActionCanceledError("{} canceled".format(f.__name__))
     except Bitbox02Exception as exc:
         if exc.code in (ERR_GENERIC, ERR_INVALID_INPUT):
             raise BitBox02Error(str(exc))
         raise exc
     except FirmwareVersionOutdatedException as exc:
         raise DeviceNotReadyError(str(exc))
示例#6
0
 def func(*args, **kwargs):
     try:
         return f(*args, **kwargs)
     except ValueError as e:
         raise BadArgumentError(str(e))
     except BTChipException as e:
         if e.sw in bad_args:
             raise BadArgumentError("Bad argument")
         elif e.sw == 0x6F00:  # BTCHIP_SW_TECHNICAL_PROBLEM
             raise DeviceFailureError(e.message)
         elif e.sw == 0x6FAA:  # BTCHIP_SW_HALTED
             raise DeviceConnectionError("Device is asleep")
         elif e.sw in cancels:
             raise ActionCanceledError("{} canceled".format(f.__name__))
         else:
             raise e
示例#7
0
 def func(*args: Any, **kwargs: Any) -> Any:
     try:
         return f(*args, **kwargs)
     except ValueError as e:
         raise BadArgumentError(str(e))
     except JadeError as e:
         if e.code == JadeError.USER_CANCELLED:
             raise ActionCanceledError(f"{f.__name__} canceled by user")
         elif e.code == JadeError.BAD_PARAMETERS:
             raise BadArgumentError(e.message)
         elif e.code == JadeError.INTERNAL_ERROR:
             raise DeviceFailureError(e.message)
         elif e.code == JadeError.HW_LOCKED:
             raise DeviceConnectionError("Device is locked")
         elif e.code == JadeError.NETWORK_MISMATCH:
             raise DeviceConnectionError("Network/chain selection error")
         elif e.code in [
             JadeError.INVALID_REQUEST,
             JadeError.UNKNOWN_METHOD,
             JadeError.PROTOCOL_ERROR,
         ]:
             raise DeviceConnectionError("Messaging/communiciation error")
         else:
             raise e