Skip to content

Commit 2af7088

Browse files
committed
wip comms module testing
1 parent fec81f7 commit 2af7088

File tree

2 files changed

+414
-13
lines changed

2 files changed

+414
-13
lines changed

src/nrfcredstore/comms.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import coloredlogs, logging
2020
import re
2121
import platform
22+
from typing import Tuple, List, Union
2223

2324
logger = logging.getLogger(__name__)
2425

@@ -102,7 +103,7 @@ def parser_add_comms_args(parser):
102103

103104

104105
# returns a list of printable name, serial number and serial port for connected Nordic boards
105-
def get_connected_nordic_boards():
106+
def get_connected_nordic_boards() -> List[Tuple[str, Union[str, int], serial.Serial]]:
106107
if platform.system() == 'Darwin':
107108
ports = sorted(list_ports.comports(), key=lambda x: x.device)
108109
else:
@@ -122,13 +123,13 @@ def get_connected_nordic_boards():
122123

123124

124125
# returns a list of SEGGER J-Link serial numbers as int
125-
def get_connected_jlinks():
126+
def get_connected_jlinks() -> List[int]:
126127
with LowLevel.API(LowLevel.DeviceFamily.UNKNOWN) as api:
127128
return api.enum_emu_snr() or []
128129

129130

130131
# for a serial device, return the serial number
131-
def extract_serial_number_from_serial_device(dev):
132+
def extract_serial_number_from_serial_device(dev : serial.Serial) -> Union[str, int, None]:
132133
hwid = dev.hwid
133134
# Get serial number from hwid, because port.serial_number is not always available
134135
serial = [x[4:] for x in hwid.split(" ") if x.startswith("SER=")]
@@ -141,7 +142,7 @@ def extract_serial_number_from_serial_device(dev):
141142

142143
return serial
143144

144-
def extract_product_name_from_serial_device(dev):
145+
def extract_product_name_from_serial_device(dev : serial.Serial) -> str:
145146
for pattern, name, main_port in usb_patterns:
146147
if f"SER={pattern}" in dev.hwid:
147148
return name
@@ -150,7 +151,7 @@ def extract_product_name_from_serial_device(dev):
150151
return text
151152
return ''
152153

153-
def extract_product_name_from_jlink_serial(serial):
154+
def extract_product_name_from_jlink_serial(serial : str) -> str:
154155
serial = f"{serial:012}"
155156
for pattern, name, main_port in usb_patterns:
156157
if pattern in serial:
@@ -159,14 +160,14 @@ def extract_product_name_from_jlink_serial(serial):
159160

160161

161162
# find the main port for a device if it's a Nordic board
162-
def get_port_index(dev):
163+
def get_port_index(dev : serial.Serial) -> Union[int, None]:
163164
for pattern, name, main_port in usb_patterns:
164165
if f"SER={pattern}" in dev.hwid:
165166
return main_port
166167
return None
167168

168169

169-
def select_jlink(jlinks, list_all):
170+
def select_jlink(jlinks : List[int], list_all: bool) -> int:
170171
if len(jlinks) == 0:
171172
raise Exception("No J-Link device found")
172173
if len(jlinks) == 1:
@@ -194,7 +195,7 @@ def select_jlink(jlinks, list_all):
194195
return answer["serial"]
195196

196197

197-
def select_device_by_serial(serial_number, list_all):
198+
def select_device_by_serial(serial_number : Union[str, int], list_all : bool) -> Tuple[serial.Serial, Union[str, int]]:
198199
serial_devices = [
199200
x
200201
for x in list_ports.comports()
@@ -221,7 +222,7 @@ def select_device_by_serial(serial_number, list_all):
221222

222223

223224
# returns serial_port, serial_number of selected device
224-
def select_device(rtt, serial_number, port, list_all):
225+
def select_device(rtt : bool, serial_number : Union[None, int, str], port : Union[None, serial.Serial], list_all : bool) -> Tuple[serial.Serial, Union[str, int]]:
225226
if type(serial_number) == str and serial_number.isdigit():
226227
serial_number = int(serial_number)
227228

@@ -247,7 +248,7 @@ def select_device(rtt, serial_number, port, list_all):
247248
logger.warning(
248249
f"Given Serial number {serial_number} does not match device serial number {extracted_serial_number}"
249250
)
250-
return (serial_devices[0], serial_number)
251+
return (serial_devices[0], extracted_serial_number)
251252

252253
if serial_number:
253254
# Often, there are multiple serial ports for a device, so we need to find the right one

0 commit comments

Comments
 (0)