Skip to content

Commit a877048

Browse files
committed
Add missing JSON fields
- Fix missing device descriptions by correcting mkjson parameter count - Add USB3 link states (U0, U1, U2, Rx.Detect, etc.) to JSON output - Fix USB Full Speed detection for USB 2.0 devices These changes ensure JSON output includes all information shown in text output.
1 parent 3543bc4 commit a877048

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

uhubctl.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,29 +1545,57 @@ char* create_port_status_json(int port, int port_status, const struct descriptor
15451545
char *flags_json = create_status_flags_json(port_status);
15461546
char *hr_json = create_human_readable_json(port_status);
15471547

1548+
// For USB3 hubs, get link state and port speed capability
1549+
const char* link_state_str = NULL;
1550+
const char* port_speed_str = NULL;
1551+
if (port_status & USB_SS_PORT_STAT_POWER) {
1552+
// Check port speed capability
1553+
if ((port_status & USB_SS_PORT_STAT_SPEED) == USB_PORT_STAT_SPEED_5GBPS) {
1554+
port_speed_str = "5gbps";
1555+
}
1556+
1557+
int link_state = port_status & USB_PORT_STAT_LINK_STATE;
1558+
switch (link_state) {
1559+
case USB_SS_PORT_LS_U0: link_state_str = "U0"; break;
1560+
case USB_SS_PORT_LS_U1: link_state_str = "U1"; break;
1561+
case USB_SS_PORT_LS_U2: link_state_str = "U2"; break;
1562+
case USB_SS_PORT_LS_U3: link_state_str = "U3"; break;
1563+
case USB_SS_PORT_LS_SS_DISABLED: link_state_str = "SS.Disabled"; break;
1564+
case USB_SS_PORT_LS_RX_DETECT: link_state_str = "Rx.Detect"; break;
1565+
case USB_SS_PORT_LS_SS_INACTIVE: link_state_str = "SS.Inactive"; break;
1566+
case USB_SS_PORT_LS_POLLING: link_state_str = "Polling"; break;
1567+
case USB_SS_PORT_LS_RECOVERY: link_state_str = "Recovery"; break;
1568+
case USB_SS_PORT_LS_HOT_RESET: link_state_str = "HotReset"; break;
1569+
case USB_SS_PORT_LS_COMP_MOD: link_state_str = "Compliance"; break;
1570+
case USB_SS_PORT_LS_LOOPBACK: link_state_str = "Loopback"; break;
1571+
}
1572+
}
1573+
15481574
// Basic port info without device
15491575
if (!(port_status & USB_PORT_STAT_CONNECTION) || !dev) {
1550-
return mkjson(MKJSON_OBJ, 6,
1576+
return mkjson(MKJSON_OBJ, 7,
15511577
MKJSON_INT, "port", port,
15521578
MKJSON_STRING, "status", status_hex,
15531579
MKJSON_JSON_FREE, "flags", flags_json,
15541580
MKJSON_JSON_FREE, "human_readable", hr_json,
15551581
MKJSON_STRING, "speed", speed_str,
1556-
MKJSON_LLINT, "speed_bits", speed_bits
1582+
MKJSON_LLINT, "speed_bits", speed_bits,
1583+
link_state_str ? MKJSON_STRING : MKJSON_IGN_STRING, "link_state", link_state_str
15571584
);
15581585
}
15591586

15601587
// Port with device - add device info
15611588
struct libusb_device_descriptor desc;
15621589
if (libusb_get_device_descriptor(dev, &desc) != 0) {
15631590
// If we can't get descriptor, return basic info
1564-
return mkjson(MKJSON_OBJ, 6,
1591+
return mkjson(MKJSON_OBJ, 7,
15651592
MKJSON_INT, "port", port,
15661593
MKJSON_STRING, "status", status_hex,
15671594
MKJSON_JSON_FREE, "flags", flags_json,
15681595
MKJSON_JSON_FREE, "human_readable", hr_json,
15691596
MKJSON_STRING, "speed", speed_str,
1570-
MKJSON_LLINT, "speed_bits", speed_bits
1597+
MKJSON_LLINT, "speed_bits", speed_bits,
1598+
link_state_str ? MKJSON_STRING : MKJSON_IGN_STRING, "link_state", link_state_str
15711599
);
15721600
}
15731601

@@ -1590,13 +1618,15 @@ char* create_port_status_json(int port, int port_status, const struct descriptor
15901618
int is_mass_storage = is_mass_storage_device(dev);
15911619

15921620
// Return port with basic device info
1593-
return mkjson(MKJSON_OBJ, 14 + (is_mass_storage ? 1 : 0),
1621+
// Note: even when ignored, parameters still count towards total
1622+
return mkjson(MKJSON_OBJ, 16,
15941623
MKJSON_INT, "port", port,
15951624
MKJSON_STRING, "status", status_hex,
15961625
MKJSON_JSON_FREE, "flags", flags_json,
15971626
MKJSON_JSON_FREE, "human_readable", hr_json,
15981627
MKJSON_STRING, "speed", speed_str,
15991628
MKJSON_LLINT, "speed_bits", speed_bits,
1629+
link_state_str ? MKJSON_STRING : MKJSON_IGN_STRING, "link_state", link_state_str,
16001630
MKJSON_STRING, "vid", vendor_id,
16011631
MKJSON_STRING, "pid", product_id,
16021632
MKJSON_INT, "device_class", desc.bDeviceClass,
@@ -1671,7 +1701,10 @@ char* create_hub_json(struct hub_info* hub, int portmask)
16711701
libusb_get_port_number(udev) == port)
16721702
{
16731703
rc = get_device_description(udev, &ds);
1674-
if (rc == 0) break;
1704+
if (rc == 0) {
1705+
// Found the device
1706+
break;
1707+
}
16751708
}
16761709
}
16771710

0 commit comments

Comments
 (0)