9 def __init__(self, vendor_id, product_id):
10 self.vendor_id = vendor_id
11 self.product_id = product_id
21 def find_device(self):
22 self.dev = usb.core.find(idVendor=self.vendor_id, idProduct=self.product_id)
24 raise ValueError(f"Device {self.vendor_id}:{self.product_id} not found\nAre you sure it is connected?")
26 for config in self.dev:
28 if usb.util.find_descriptor(config, custom_match=lambda d: (d.bInterfaceClass == 0x02 or d.bInterfaceClass == 0xFF)):
29 print("Found serial device with 0x02 | 0xFF")
30 if self.dev.is_kernel_driver_active(intf.bInterfaceNumber):
31 print("Found kernel driver, detaching ... ")
32 self.dev.detach_kernel_driver(intf.bInterfaceNumber)
34 if (ep.bmAttributes == 0x02):
35 if ep.bEndpointAddress == self.ep_in:
38 elif ep.bEndpointAddress == self.ep_out:
43 raise ValueError("Device does not have a serial interface")
45 def establish_connection(self):
46 for i in range(self.max_tries):
48 self.tx_ep.write(bytes([0x00]), self.timeout_ms)
49 ret = self.rx_ep.read(1, self.timeout_ms)
53 except usb.core.USBError as e:
54 print(f"Timeout: retry #{i}", e)
57 def confirm_connection(self):
58 for i in range(self.max_tries):
60 self.tx_ep.write(bytes([0x55]), self.timeout_ms)
61 ret = self.rx_ep.read(1, self.timeout_ms)
65 except usb.core.USBError as e:
66 print(f"Timeout: retry #{i}", e)
69 def authenticate_connection(self):
70 raise Exception("Not implemented")
72 def send_data(self, packed_data):
73 if (self.tx_ep == None):
76 self.tx_ep.write(packed_data, self.timeout_ms)
77 except usb.core.USBError as e:
78 print(f"Timeout: error", e)
82 # packets are length 7, except for a read package
83 def recv_data(self, exp_len):
84 if (exp_len > MAX_TRANSFER_SIZE):
85 raise ValueError(f"length package {exp_len} over max transfer size")
86 if (self.rx_ep == None):
89 msg = self.rx_ep.read(exp_len, self.timeout_ms)
90 except usb.core.USBError as e:
91 print(f"Timeout: error", e)
96 #communicator = RAConnect(vendor_id=0x1a86, product_id=0x7523)
98 #communicator.send_data(b'\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA')
99 #communicator.recv_data(7)
101 #if not communicator.establish_connection():
102 # print("Cannot connect")
105 #if not communicator.confirm_connection():
106 # print("Failed to confirm boot code")