-
Notifications
You must be signed in to change notification settings - Fork 246
Add JSON support for uhubctl - addresses all PR #575 feedback #575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benroeder
wants to merge
14
commits into
mvp:master
Choose a base branch
from
benroeder:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a2f3932
initial update for -j
benroeder 3543bc4
Replace cJSON with lightweight mkjson library
benroeder a877048
Add missing JSON fields
benroeder 3a39f48
Add serial numbers to JSON output
benroeder f866aa1
Replace cJSON with mkjson and fix JSON output issues
benroeder 4c5bfaa
Add JSON usage examples and documentation
benroeder b5e4747
Replace mkjson with forked submodule
benroeder 1d181c2
Update mkjson submodule to latest security fixes
benroeder f3c4c7e
Add JSON support for uhubctl
benroeder d3b5abe
Remove unused create_interfaces_json function
benroeder 5e369cd
Refactor to eliminate duplicate device descriptor parsing
benroeder 094673e
Extract USB device enumeration into helper function
benroeder 88b8aca
Extract hex ID formatting into helper function
benroeder 99a082e
Extract USB version formatting into helper function
benroeder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ PKG_CONFIG ?= pkg-config | |
|
||
CC ?= gcc | ||
CFLAGS ?= -g -O0 | ||
CFLAGS += -Wall -Wextra -Wno-zero-length-array -std=c99 -pedantic | ||
CFLAGS += -Wall -Wextra -Wno-zero-length-array -std=c99 -pedantic -I. | ||
GIT_VERSION := $(shell git describe --match "v[0-9]*" --abbrev=8 --dirty --tags | cut -c2-) | ||
ifeq ($(GIT_VERSION),) | ||
GIT_VERSION := $(shell cat VERSION) | ||
|
@@ -37,17 +37,22 @@ else | |
endif | ||
|
||
PROGRAM = uhubctl | ||
|
||
.PHONY: all install clean | ||
SOURCES = $(PROGRAM).c mkjson.c | ||
OBJECTS = $(SOURCES:.c=.o) | ||
|
||
all: $(PROGRAM) | ||
|
||
$(PROGRAM): $(PROGRAM).c | ||
$(CC) $(CPPFLAGS) $(CFLAGS) [email protected] -o $@ $(LDFLAGS) | ||
$(PROGRAM): $(OBJECTS) | ||
$(CC) $(CPPFLAGS) $(CFLAGS) $(OBJECTS) -o $@ $(LDFLAGS) | ||
|
||
%.o: %.c | ||
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ | ||
|
||
install: | ||
$(INSTALL_DIR) $(DESTDIR)$(sbindir) | ||
$(INSTALL_PROGRAM) $(PROGRAM) $(DESTDIR)$(sbindir) | ||
|
||
clean: | ||
$(RM) $(PROGRAM).o $(PROGRAM).dSYM $(PROGRAM) | ||
$(RM) $(OBJECTS) $(PROGRAM).dSYM $(PROGRAM) | ||
|
||
.PHONY: all install clean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# uhubctl JSON Output Examples | ||
|
||
This directory contains examples of how to use uhubctl's JSON output feature (`-j` flag) to programmatically work with USB hub information. | ||
|
||
## Requirements | ||
|
||
- uhubctl compiled with JSON support | ||
- jq (command-line JSON processor) - install with: | ||
- macOS: `brew install jq` | ||
- Ubuntu/Debian: `sudo apt-get install jq` | ||
- RedHat/Fedora: `sudo yum install jq` | ||
|
||
## Running the Examples | ||
|
||
```bash | ||
./json_usage_examples.sh | ||
``` | ||
|
||
## JSON Output Format | ||
|
||
The JSON output provides complete information about all USB hubs and connected devices: | ||
|
||
```json | ||
{ | ||
"hubs": [ | ||
{ | ||
"location": "3-1", | ||
"description": "Hub description string", | ||
"hub_info": { | ||
"vid": "0x05e3", | ||
"pid": "0x0608", | ||
"usb_version": "2.00", | ||
"nports": 4, | ||
"ppps": "ppps" | ||
}, | ||
"ports": [ | ||
{ | ||
"port": 1, | ||
"status": "0x0103", | ||
"flags": { | ||
"connection": true, | ||
"enable": true, | ||
"power": true | ||
}, | ||
"human_readable": { | ||
"connection": "Device is connected", | ||
"enable": "Port is enabled", | ||
"power": "Port power is enabled" | ||
}, | ||
"speed": "USB2.0 High Speed 480Mbps", | ||
"speed_bps": 480000000, | ||
"vid": "0x0781", | ||
"pid": "0x5567", | ||
"vendor": "SanDisk", | ||
"product": "Cruzer Blade", | ||
"device_class": 0, | ||
"class_name": "Mass Storage", | ||
"usb_version": "2.00", | ||
"device_version": "1.00", | ||
"nconfigs": 1, | ||
"serial": "4C530001234567891234", | ||
"is_mass_storage": true, | ||
"interfaces": [...], | ||
"description": "0781:5567 SanDisk Cruzer Blade 4C530001234567891234" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Common Use Cases | ||
|
||
### 1. Find Device by Serial Number | ||
```bash | ||
SERIAL="4C530001234567891234" | ||
uhubctl -j | jq -r --arg s "$SERIAL" '.hubs[] | . as $h | .ports[] | select(.serial == $s) | "uhubctl -l \($h.location) -p \(.port) -a cycle"' | ||
``` | ||
|
||
### 2. List All Mass Storage Devices | ||
```bash | ||
uhubctl -j | jq -r '.hubs[].ports[] | select(.is_mass_storage == true) | .description' | ||
``` | ||
|
||
### 3. Find Empty Ports | ||
```bash | ||
uhubctl -j | jq -r '.hubs[] | . as $h | .ports[] | select(.vid == null) | "Hub \($h.location) Port \(.port)"' | ||
``` | ||
|
||
### 4. Generate Control Commands for Device Type | ||
```bash | ||
# Power off all FTDI devices | ||
uhubctl -j | jq -r '.hubs[] | . as $h | .ports[] | select(.vendor == "FTDI") | "uhubctl -l \($h.location) -p \(.port) -a off"' | bash | ||
``` | ||
|
||
### 5. Monitor for Device Changes | ||
```bash | ||
# Save baseline | ||
uhubctl -j > baseline.json | ||
|
||
# Later, check what changed | ||
uhubctl -j | jq -r --argjson old "$(cat baseline.json)" ' | ||
. as $new | | ||
$old.hubs[].ports[] as $op | | ||
$new.hubs[] | . as $h | | ||
.ports[] | | ||
select(.port == $op.port and $h.location == $op.hub_location and .vid != $op.vid) | | ||
"Change at \($h.location):\(.port)"' | ||
``` | ||
|
||
## JSON Fields Reference | ||
|
||
### Hub Object | ||
- `location` - Hub location (e.g., "3-1", "1-1.4") | ||
- `description` - Full hub description string | ||
- `hub_info` - Parsed hub information | ||
- `vid` - Vendor ID in hex | ||
- `pid` - Product ID in hex | ||
- `usb_version` - USB version string | ||
- `nports` - Number of ports | ||
- `ppps` - Power switching mode | ||
- `ports` - Array of port objects | ||
|
||
### Port Object | ||
- `port` - Port number | ||
- `status` - Raw status value in hex | ||
- `flags` - Boolean flags (only true values included) | ||
- `human_readable` - Human-readable flag descriptions | ||
- `speed` - Speed description string | ||
- `speed_bps` - Speed in bits per second (numeric) | ||
- `vid`, `pid` - Device vendor/product IDs | ||
- `vendor`, `product` - Device vendor/product names | ||
- `serial` - Device serial number | ||
- `device_class` - USB device class code | ||
- `class_name` - Device class name | ||
- `is_mass_storage` - Boolean flag for mass storage devices | ||
- `interfaces` - Array of interface descriptors | ||
|
||
### USB3-Specific Fields | ||
- `port_speed` - Link speed (e.g., "5gbps") | ||
- `link_state` - USB3 link state (e.g., "U0", "U3") | ||
|
||
## Tips | ||
|
||
1. Use `jq -r` for raw output (no quotes) | ||
2. Use `select()` to filter results | ||
3. Use `. as $var` to save context when diving into nested objects | ||
4. Use `// "default"` to provide default values for missing fields | ||
5. Combine with shell scripts for automation |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would not be needed if we keep mkjson local copy