Skip to content
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

initial update for -j json #575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

initial update for -j json #575

wants to merge 1 commit into from

Conversation

benroeder
Copy link

I have added cjson to the repo
This is my first pass of adding -j, there is more work to do, but wanted your opinion first before I carry on

I have added cjson to to the repo
This is my first pass of adding -j, there is more work to do
@benroeder benroeder mentioned this pull request Aug 2, 2024
Copy link
Owner

@mvp mvp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your submission @benroeder!

Overall great job, but it needs some work to move on with this - please read comments below.

But main points are:

  1. Consider using encoder-only json C library that is as small as possible, e.g. mkjson - it is 10x smaller than cJSON.
  2. Try making emitted json as small as possible: avoid emitting empty or false values. E.g. always emitting "indicator": false in list of flags is hardly useful. Try using shorter field names (but still make them understandable) - e.g. consider using nports instead of num_ports - this will make json smaller.
  3. Retain backwards compatibility as much as possible - e.g. existing code is using ppps for per-port power switching mode supported - consider using it instead of per-port.
  4. For json generating functions, avoid copying large blocks from old non-json functions.
  5. Try to make output json as small as possible and don't emit info that is rarely used - basically anything that is not related to current port power state. If you want it to be still available, consider adding --verbose/-V flag that would make it really verbose.

$(RM) $(PROGRAM).o $(PROGRAM).dSYM $(PROGRAM)
$(RM) $(OBJECTS) $(PROGRAM).dSYM $(PROGRAM)

.PHONY: all install clean
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing last newline

@@ -0,0 +1,300 @@
/*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cJSON supports both encoding and decoding, and is relatively large (96KB c+h combined).
Since we only need json encoder, perhaps we should use encoder only library? mkjson seems like great alternative (less than 10KB c+h combined).

@@ -18,6 +18,10 @@
#include <getopt.h>
#include <errno.h>
#include <ctype.h>
#include <stdbool.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need stdbool.h - commenting this out still works - please remove.

@@ -180,6 +184,38 @@ struct usb_port_status {
#define HUB_CHAR_TTTT 0x0060 /* TT Think Time mask */
#define HUB_CHAR_PORTIND 0x0080 /* per-port indicators (LEDs) */

/*
* USB Speed definitions
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please link source where you got these speed definitions from? Perhaps USB spec or something?

int is_mass_storage_device(struct libusb_device *dev)
{
struct libusb_config_descriptor *config;
int ret = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use rc instead of ret to be consistent with other code

const char* power_switching_mode;
switch (hub->lpsm) {
case HUB_CHAR_INDV_PORT_LPSM:
power_switching_mode = "per-port";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mode is already set to ppps in get_device_description, make it consistent?

power_switching_mode = "ganged";
break;
default:
power_switching_mode = "unknown";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is set to nops in get_device_description (no power switching).

struct libusb_device_handle* devh = NULL;
int rc = libusb_open(hub->dev, &devh);
if (rc == 0) {
for (int port = 1; port <= hub->nports; port++) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code has big copy/paste chunk from get_device_description(). Is there a way to avoid this? Ideally make it into one function?

int mask;
const char* name;
} flag_defs[] = {
{USB_PORT_STAT_CONNECTION, "connection"},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For brevity, I would not emit any flag that has false value - it would considerably reduce output size

}

// Helper function to determine port speed
void get_port_speed(int port_status, char** speed_str, int64_t* speed_bits)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

speed_bits should be speed_bps (bits per second). That should be in emitted json as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants