Skip to content

Commit 1090d9d

Browse files
committed
remove check for port status on toggling ports
also fix parsing of acroname bitmask
1 parent e9e0bb6 commit 1090d9d

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

unit-tests/py/rspy/acroname.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,19 @@ def is_port_enabled(self, port ):
139139
:param port: port number;
140140
:return: True if Acroname enabled this port, False otherwise
141141
"""
142-
return self.port_state( port ) != "Disabled"
142+
return ("Disabled" not in self.port_state( port ))
143143

144144

145145
def port_state(self, port ):
146146
if port < 0 or port > 7:
147147
raise ValueError( "port number must be [0-7]" )
148148
#
149149
status = self.hub.usb.getPortState( port )
150-
#log.d("getPortState for port", port ,"return", port_state_bitmask_to_string( status.value ))
151-
return self.port_state_bitmask_to_string( status.value )
150+
#log.d("getPortState for port", port ,"return", port_state_bitmask_to_strings( status.value ))
151+
return self.port_state_bitmask_to_strings( status.value )
152152

153153

154-
def port_state_bitmask_to_string(self, bitmask ):
154+
def port_state_bitmask_to_strings(self, bitmask ):
155155
"""
156156
https://acroname.com/reference/devices/usbhub3p/entities/usb.html#usb-port-state
157157
Bit | Port State: Result Bitwise Description
@@ -170,14 +170,27 @@ def port_state_bitmask_to_string(self, bitmask ):
170170
23 | Device Attached
171171
24:31 | Reserved
172172
"""
173+
if bitmask < 0 or bitmask > 0xFFFFFFFF: # 32 bits bitmask
174+
raise ValueError("bitmask must be within 0 and 0xFFFFFFFF (inclusive)")
175+
def bit_on(x):
176+
return bitmask & (1 << x)
177+
res = ["Enabled" if bit_on(0) else "Disabled"]
178+
if bit_on(1):
179+
res.append("USB2 Data Enabled")
180+
if bit_on(3):
181+
res.append("USB3 Data Enabled")
182+
if bit_on(11):
183+
res.append("USB2 Device Attached")
184+
if bit_on(12):
185+
res.append("USB3 Device Attached")
186+
if bit_on(19):
187+
res.append("USB Error Flag")
188+
if bit_on(20):
189+
res.append("USB2 Boost Enabled")
190+
if bit_on(23):
191+
res.append("Device Attached")
192+
return res
173193

174-
if not (bitmask & 0x1): # Port is disabled by Acroname - last bit off indicates Vbus is off - port is disabled
175-
return "Disabled"
176-
if bitmask == 11: # Port is enabled but no device was detected
177-
return "Disconnected"
178-
if bitmask > 100: # Normally we hope it will cover "Device Attached" use cases (Note, can also be turn on when 'USB Error Flag' is on...but we havn't seen that )
179-
return "OK"
180-
return "Unknown Error ({})".format( bitmask )
181194

182195

183196
def enable_ports(self, ports = None, disable_other_ports = False, sleep_on_change = 0 ):
@@ -193,23 +206,21 @@ def enable_ports(self, ports = None, disable_other_ports = False, sleep_on_chan
193206
for port in self.all_ports():
194207
#
195208
if ports is None or port in ports:
196-
if not self.is_port_enabled( port ):
197-
# log.d( "enabling port", port)
198-
action_result = self.hub.usb.setPortEnable( port )
199-
if action_result != brainstem.result.Result.NO_ERROR:
200-
result = False
201-
log.e("Failed to enable port", port)
202-
else:
203-
changed = True
209+
# log.d( "enabling port", port)
210+
action_result = self.hub.usb.setPortEnable( port )
211+
if action_result != brainstem.result.Result.NO_ERROR:
212+
result = False
213+
log.e("Failed to enable port", port)
214+
else:
215+
changed = True
204216
#
205217
elif disable_other_ports:
206-
if self.is_port_enabled( port ):
207-
# log.d("disabling port", port)
208-
action_result = self.hub.usb.setPortDisable( port )
209-
if action_result != brainstem.result.Result.NO_ERROR:
210-
result = False
211-
else:
212-
changed = True
218+
# log.d("disabling port", port)
219+
action_result = self.hub.usb.setPortDisable( port )
220+
if action_result != brainstem.result.Result.NO_ERROR:
221+
result = False
222+
else:
223+
changed = True
213224
#
214225
if changed and sleep_on_change:
215226
signals.register_signal_handlers()
@@ -229,14 +240,13 @@ def disable_ports(self, ports = None, sleep_on_change = 0 ):
229240
changed = False
230241
for port in self.all_ports():
231242
if ports is None or port in ports:
232-
if self.is_port_enabled( port ):
233-
# log.d("disabling port", port)
234-
action_result = self.hub.usb.setPortDisable( port )
235-
if action_result != brainstem.result.Result.NO_ERROR:
236-
result = False
237-
log.e("Failed to disable port", port)
238-
else:
239-
changed = True
243+
# log.d("disabling port", port)
244+
action_result = self.hub.usb.setPortDisable( port )
245+
if action_result != brainstem.result.Result.NO_ERROR:
246+
result = False
247+
log.e("Failed to disable port", port)
248+
else:
249+
changed = True
240250
if changed and sleep_on_change:
241251
signals.register_signal_handlers()
242252
import time

unit-tests/py/rspy/combined_hub.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def disable_ports(self, ports=None, sleep_on_change=0):
127127

128128
success = True
129129
for name, hub in self.hubs.items():
130+
log.d("Disabling ports on hub", name, ":", targets[name])
130131
ok = hub.disable_ports(targets[name], sleep_on_change)
131132
success = success and ok
132133
return success

0 commit comments

Comments
 (0)