Skip to content

Commit 9b5efa0

Browse files
committed
Simplify code that powers on/off
* Simplified code that powers on/off. * Fixed minor bug for repeat counter (always once to power off, and opt_repeat times to power on). * Only warn about USB permissions if no compatible hubs are detected. Old behavior would always warn, even if permissions were missing for any hub (even unsupported ones).
1 parent b99a080 commit 9b5efa0

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

uhubctl.c

+13-15
Original file line numberDiff line numberDiff line change
@@ -877,15 +877,15 @@ static int usb_find_hubs()
877877
hub_phys_count++;
878878
}
879879
}
880+
if (perm_ok == 0 && hub_phys_count == 0) {
880881
#ifdef __gnu_linux__
881-
if (perm_ok == 0 && geteuid() != 0) {
882-
fprintf(stderr,
883-
"There were permission problems while accessing USB.\n"
884-
"Follow https://git.io/JIB2Z for a fix!\n"
885-
);
886-
}
882+
if (geteuid() != 0) {
883+
fprintf(stderr,
884+
"There were permission problems while accessing USB.\n"
885+
"Follow https://git.io/JIB2Z for a fix!\n"
886+
);
887+
}
887888
#endif
888-
if (perm_ok == 0 && hub_phys_count == 0) {
889889
return LIBUSB_ERROR_ACCESS;
890890
}
891891
return hub_phys_count;
@@ -1060,18 +1060,16 @@ int main(int argc, char *argv[])
10601060
int port_status = get_port_status(devh, port);
10611061
int power_mask = hubs[i].super_speed ? USB_SS_PORT_STAT_POWER
10621062
: USB_PORT_STAT_POWER;
1063+
int powered_on = port_status & power_mask;
10631064
if (opt_action == POWER_TOGGLE) {
1064-
request = (port_status & power_mask) ? LIBUSB_REQUEST_CLEAR_FEATURE : LIBUSB_REQUEST_SET_FEATURE;
1065+
request = powered_on ? LIBUSB_REQUEST_CLEAR_FEATURE
1066+
: LIBUSB_REQUEST_SET_FEATURE;
10651067
}
1066-
if (k == 0 && !(port_status & power_mask) && (opt_action != POWER_TOGGLE))
1068+
if (k == 0 && !powered_on && opt_action != POWER_TOGGLE)
10671069
continue;
1068-
if (k == 1 && (port_status & power_mask))
1070+
if (k == 1 && powered_on)
10691071
continue;
1070-
int repeat = 1;
1071-
if (k == 0)
1072-
repeat = opt_repeat;
1073-
if (!(port_status & ~power_mask))
1074-
repeat = 1;
1072+
int repeat = powered_on ? opt_repeat : 1;
10751073
while (repeat-- > 0) {
10761074
rc = libusb_control_transfer(devh,
10771075
LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_OTHER,

0 commit comments

Comments
 (0)