Skip to content

Commit 83ec8d7

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 b53096c commit 83ec8d7

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
@@ -1452,29 +1452,57 @@ char* create_port_status_json(int port, int port_status, const struct descriptor
14521452
char *flags_json = create_status_flags_json(port_status);
14531453
char *hr_json = create_human_readable_json(port_status);
14541454

1455+
// For USB3 hubs, get link state and port speed capability
1456+
const char* link_state_str = NULL;
1457+
const char* port_speed_str = NULL;
1458+
if (port_status & USB_SS_PORT_STAT_POWER) {
1459+
// Check port speed capability
1460+
if ((port_status & USB_SS_PORT_STAT_SPEED) == USB_PORT_STAT_SPEED_5GBPS) {
1461+
port_speed_str = "5gbps";
1462+
}
1463+
1464+
int link_state = port_status & USB_PORT_STAT_LINK_STATE;
1465+
switch (link_state) {
1466+
case USB_SS_PORT_LS_U0: link_state_str = "U0"; break;
1467+
case USB_SS_PORT_LS_U1: link_state_str = "U1"; break;
1468+
case USB_SS_PORT_LS_U2: link_state_str = "U2"; break;
1469+
case USB_SS_PORT_LS_U3: link_state_str = "U3"; break;
1470+
case USB_SS_PORT_LS_SS_DISABLED: link_state_str = "SS.Disabled"; break;
1471+
case USB_SS_PORT_LS_RX_DETECT: link_state_str = "Rx.Detect"; break;
1472+
case USB_SS_PORT_LS_SS_INACTIVE: link_state_str = "SS.Inactive"; break;
1473+
case USB_SS_PORT_LS_POLLING: link_state_str = "Polling"; break;
1474+
case USB_SS_PORT_LS_RECOVERY: link_state_str = "Recovery"; break;
1475+
case USB_SS_PORT_LS_HOT_RESET: link_state_str = "HotReset"; break;
1476+
case USB_SS_PORT_LS_COMP_MOD: link_state_str = "Compliance"; break;
1477+
case USB_SS_PORT_LS_LOOPBACK: link_state_str = "Loopback"; break;
1478+
}
1479+
}
1480+
14551481
// Basic port info without device
14561482
if (!(port_status & USB_PORT_STAT_CONNECTION) || !dev) {
1457-
return mkjson(MKJSON_OBJ, 6,
1483+
return mkjson(MKJSON_OBJ, 7,
14581484
MKJSON_INT, "port", port,
14591485
MKJSON_STRING, "status", status_hex,
14601486
MKJSON_JSON_FREE, "flags", flags_json,
14611487
MKJSON_JSON_FREE, "human_readable", hr_json,
14621488
MKJSON_STRING, "speed", speed_str,
1463-
MKJSON_LLINT, "speed_bits", speed_bits
1489+
MKJSON_LLINT, "speed_bits", speed_bits,
1490+
link_state_str ? MKJSON_STRING : MKJSON_IGN_STRING, "link_state", link_state_str
14641491
);
14651492
}
14661493

14671494
// Port with device - add device info
14681495
struct libusb_device_descriptor desc;
14691496
if (libusb_get_device_descriptor(dev, &desc) != 0) {
14701497
// If we can't get descriptor, return basic info
1471-
return mkjson(MKJSON_OBJ, 6,
1498+
return mkjson(MKJSON_OBJ, 7,
14721499
MKJSON_INT, "port", port,
14731500
MKJSON_STRING, "status", status_hex,
14741501
MKJSON_JSON_FREE, "flags", flags_json,
14751502
MKJSON_JSON_FREE, "human_readable", hr_json,
14761503
MKJSON_STRING, "speed", speed_str,
1477-
MKJSON_LLINT, "speed_bits", speed_bits
1504+
MKJSON_LLINT, "speed_bits", speed_bits,
1505+
link_state_str ? MKJSON_STRING : MKJSON_IGN_STRING, "link_state", link_state_str
14781506
);
14791507
}
14801508

@@ -1497,13 +1525,15 @@ char* create_port_status_json(int port, int port_status, const struct descriptor
14971525
int is_mass_storage = is_mass_storage_device(dev);
14981526

14991527
// Return port with basic device info
1500-
return mkjson(MKJSON_OBJ, 14 + (is_mass_storage ? 1 : 0),
1528+
// Note: even when ignored, parameters still count towards total
1529+
return mkjson(MKJSON_OBJ, 16,
15011530
MKJSON_INT, "port", port,
15021531
MKJSON_STRING, "status", status_hex,
15031532
MKJSON_JSON_FREE, "flags", flags_json,
15041533
MKJSON_JSON_FREE, "human_readable", hr_json,
15051534
MKJSON_STRING, "speed", speed_str,
15061535
MKJSON_LLINT, "speed_bits", speed_bits,
1536+
link_state_str ? MKJSON_STRING : MKJSON_IGN_STRING, "link_state", link_state_str,
15071537
MKJSON_STRING, "vid", vendor_id,
15081538
MKJSON_STRING, "pid", product_id,
15091539
MKJSON_INT, "device_class", desc.bDeviceClass,
@@ -1578,7 +1608,10 @@ char* create_hub_json(struct hub_info* hub, int portmask)
15781608
libusb_get_port_number(udev) == port)
15791609
{
15801610
rc = get_device_description(udev, &ds);
1581-
if (rc == 0) break;
1611+
if (rc == 0) {
1612+
// Found the device
1613+
break;
1614+
}
15821615
}
15831616
}
15841617

0 commit comments

Comments
 (0)