Skip to content

Commit b99a080

Browse files
authored
Added feature toggle mode (#305)
Added feature toggle mode Co-authored-by: Tobias Weber <[email protected]>
1 parent 6ac502a commit b99a080

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ You can control the power on a USB port(s) like this:
166166
uhubctl -a off -p 2
167167

168168
This means operate on default smart hub and turn power off (`-a off`, or `-a 0`)
169-
on port 2 (`-p 2`). Supported actions are `off`/`on`/`cycle` (or `0`/`1`/`2`).
169+
on port 2 (`-p 2`). Supported actions are `off`/`on`/`cycle`/`toggle` (or `0`/`1`/`2`/`3`).
170170
`cycle` means turn power off, wait some delay (configurable with `-d`) and turn it back on.
171171
Ports can be comma separated list, and may use `-` for ranges e.g. `2`, or `2,4`, or `2-5`, or `1-2,5-8`.
172172

uhubctl.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void sleep_ms(int milliseconds)
7575
#define POWER_OFF 0
7676
#define POWER_ON 1
7777
#define POWER_CYCLE 2
78+
#define POWER_TOGGLE 3
7879

7980
#define MAX_HUB_CHAIN 8 /* Per USB 3.0 spec max hub chain is 7 */
8081

@@ -248,7 +249,7 @@ static int print_usage()
248249
"Without options, show status for all smart hubs.\n"
249250
"\n"
250251
"Options [defaults in brackets]:\n"
251-
"--action, -a - action to off/on/cycle (0/1/2) for affected ports.\n"
252+
"--action, -a - action to off/on/cycle/toggle (0/1/2/3) for affected ports.\n"
252253
"--ports, -p - ports to operate on [all hub ports].\n"
253254
"--location, -l - limit hub by location [all smart hubs].\n"
254255
"--level -L - limit hub by location level (e.g. a-b.c is level 3).\n"
@@ -942,6 +943,9 @@ int main(int argc, char *argv[])
942943
if (!strcasecmp(optarg, "cycle") || !strcasecmp(optarg, "2")) {
943944
opt_action = POWER_CYCLE;
944945
}
946+
if (!strcasecmp(optarg, "toggle") || !strcasecmp(optarg, "3")) {
947+
opt_action = POWER_TOGGLE;
948+
}
945949
break;
946950
case 'd':
947951
opt_delay = atof(optarg);
@@ -1029,6 +1033,9 @@ int main(int argc, char *argv[])
10291033
continue;
10301034
if (k == 1 && opt_action == POWER_KEEP)
10311035
continue;
1036+
// if toggle requested, do it only once when `k == 0`
1037+
if (k == 1 && opt_action == POWER_TOGGLE)
1038+
continue;
10321039
int i;
10331040
for (i=0; i<hub_count; i++) {
10341041
if (hubs[i].actionable == 0)
@@ -1053,7 +1060,10 @@ int main(int argc, char *argv[])
10531060
int port_status = get_port_status(devh, port);
10541061
int power_mask = hubs[i].super_speed ? USB_SS_PORT_STAT_POWER
10551062
: USB_PORT_STAT_POWER;
1056-
if (k == 0 && !(port_status & power_mask))
1063+
if (opt_action == POWER_TOGGLE) {
1064+
request = (port_status & power_mask) ? LIBUSB_REQUEST_CLEAR_FEATURE : LIBUSB_REQUEST_SET_FEATURE;
1065+
}
1066+
if (k == 0 && !(port_status & power_mask) && (opt_action != POWER_TOGGLE))
10571067
continue;
10581068
if (k == 1 && (port_status & power_mask))
10591069
continue;

0 commit comments

Comments
 (0)