Skip to content

Commit

Permalink
devices/usb: Add USB interfaces and device icons
Browse files Browse the repository at this point in the history
* devices/usb: Added support for USB interfaces

* devices/usb: added icons for usb devices

* tabs -> spaces
  • Loading branch information
ocerman authored and lpereira committed Nov 30, 2018
1 parent b52f767 commit 5c32c60
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 7 deletions.
69 changes: 67 additions & 2 deletions hardinfo/usb_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,35 @@
#include "hardinfo.h"
#include "usb_util.h"

usbi *usbi_new() {
return g_new0(usbi, 1);
}

void usbi_free(usbi *s) {
if (s) {
g_free(s->if_class_str);
g_free(s->if_subclass_str);
g_free(s->if_protocol_str);
g_free(s);
}
}

void usbi_list_free(usbi *s) {
usbi *n;
while(s != NULL) {
n = s->next;
usbi_free(s);
s = n;
}
}

usbd *usbd_new() {
return g_new0(usbd, 1);
}

void usbd_free(usbd *s) {
if (s) {
usbi_list_free(s->if_list);
g_free(s->vendor);
g_free(s->product);
g_free(s->usb_version);
Expand Down Expand Up @@ -63,6 +86,20 @@ static int usbd_list_append(usbd *l, usbd *n) {
return c;
}

void usbd_append_interface(usbd *dev, usbi *new_if){
usbi *curr_if;
if (dev->if_list == NULL){
dev->if_list = new_if;
return;
}

curr_if = dev->if_list;
while(curr_if->next != NULL) {
curr_if = curr_if->next;
}
curr_if->next = new_if;
}

int usbd_list_count(usbd *s) {
return usbd_list_append(s, NULL);
}
Expand All @@ -79,6 +116,7 @@ static gboolean usb_get_device_lsusb(int bus, int dev, usbd *s) {
gboolean spawned;
gchar *out, *err, *p, *l, *t, *next_nl;
gchar *lsusb_cmd = g_strdup_printf("lsusb -s %d:%d -v", bus, dev);
usbi *curr_if = NULL; // do not free

s->bus = bus;
s->dev = dev;
Expand All @@ -94,6 +132,8 @@ static gboolean usb_get_device_lsusb(int bus, int dev, usbd *s) {
while(next_nl = strchr(p, '\n')) {
strend(p, '\n');
g_strstrip(p);

// device info
if (l = lsusb_line_value(p, "idVendor")) {
s->vendor_id = strtol(l, NULL, 0);
if (t = strchr(l, ' '))
Expand All @@ -116,9 +156,34 @@ static gboolean usb_get_device_lsusb(int bus, int dev, usbd *s) {
s->dev_subclass = atoi(l);
if (t = strchr(l, ' '))
s->dev_subclass_str = g_strdup(t + 1);

// interface info
} else if (l = lsusb_line_value(p, "bInterfaceNumber")) {
curr_if = usbi_new();
curr_if->if_number = atoi(l);
usbd_append_interface(s, curr_if);
} else if (l = lsusb_line_value(p, "bInterfaceClass")) {
if (curr_if != NULL){
curr_if->if_class = atoi(l);
if (t = strchr(l, ' '))
curr_if->if_class_str = g_strdup(t + 1);
}
} else if (l = lsusb_line_value(p, "bInterfaceSubClass")) {
if (curr_if != NULL){
curr_if->if_subclass = atoi(l);
if (t = strchr(l, ' '))
curr_if->if_subclass_str = g_strdup(t + 1);
}
} else if (l = lsusb_line_value(p, "bInterfaceProtocol")) {
if (curr_if != NULL){
curr_if->if_protocol = atoi(l);
if (t = strchr(l, ' '))
curr_if->if_protocol_str = g_strdup(t + 1);
}
}
/* TODO: speed_mbs
* WISHLIST: interfaces, drivers */

/* TODO: speed_mbs, improve interfaces
* WISHLIST: drivers */
p = next_nl + 1;
}
g_free(out);
Expand Down
1 change: 1 addition & 0 deletions includes/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ extern gchar *sensors;
extern gchar *storage_icons;
extern gchar *storage_list;
extern gchar *usb_list;
extern gchar *usb_icons;
extern GHashTable *memlabels;
extern GHashTable *_pci_devices;
extern GHashTable *sensor_compute;
Expand Down
14 changes: 14 additions & 0 deletions includes/usb_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,24 @@ typedef struct usbd {
int speed_mbs; /* TODO: */

gboolean user_scan; /* not scanned as root */
struct usbi *if_list;

struct usbd *next;
} usbd;

/* another linked list */
typedef struct usbi {
int if_number;
int if_class;
int if_subclass;
int if_protocol;
char *if_class_str;
char *if_subclass_str;
char *if_protocol_str;

struct usbi *next;
} usbi;

usbd *usb_get_device_list();
int usbd_list_count(usbd *);
void usbd_list_free(usbd *);
Expand Down
2 changes: 1 addition & 1 deletion modules/devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ gchar *callback_usb()
return g_strdup_printf("%s"
"[$ShellParam$]\n"
"ViewType=1\n"
"ReloadInterval=5000\n", usb_list);
"ReloadInterval=5000\n%s", usb_list, usb_icons);

}

Expand Down
115 changes: 111 additions & 4 deletions modules/devices/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,87 @@
#include "usb_util.h"

gchar *usb_list = NULL;
gchar *usb_icons = NULL;

#define UNKIFNULL_AC(f) (f != NULL) ? f : _("(Unknown)");
#define IARR_END -2
#define IARR_ANY -1

static struct {
int class;
char *icon;
} usb_class_icons[] = {
{ 0x1, "audio"}, /* Audio */
{ 0x2, "modem"}, /* Communications and CDC Control */
{ 0x3, "inputdevices"}, /* HID (Human Interface Device) */
{ 0x6, "camera-photo"}, /* Still Imaging */
{ 0x7, "printer"}, /* Printer */
{ 0x8, "media-removable"}, /* Mass storage */
{ 0x9, "module"}, /* Hub */
{ 0xe, "camera-web"}, /* Video */
{IARR_END, NULL}
};

static struct {
int class, subclass, protocol;
char *icon;
} usb_type_icons[] = {
{ 0x2, 0x6, IARR_ANY, "network-interface"}, /* Ethernet Networking Control Model */
{ 0x3, 0x1, 0x1, "keyboard"}, /* Keyboard */
{ 0x3, 0x1, 0x2, "mouse"}, /* Mouse */
{0xe0, 0x1, 0x1, "bluetooth"}, /* Bluetooth Programming Interface */
{IARR_END, IARR_END, IARR_END, NULL},
};

static const char* get_class_icon(int class){
int i = 0;
while (usb_class_icons[i].class != IARR_END) {
if (usb_class_icons[i].class == class) {
return usb_class_icons[i].icon;
}
i++;
}
return NULL;
}

static const char* get_usbif_icon(const usbi *usbif) {
int i = 0;
while (usb_type_icons[i].class != IARR_END) {
if (usb_type_icons[i].class == usbif->if_class && usb_type_icons[i].subclass == usbif->if_subclass &&
(usb_type_icons[i].protocol == IARR_ANY || usb_type_icons[i].protocol == usbif->if_protocol)) {

return usb_type_icons[i].icon;
}
i++;
}

return get_class_icon(usbif->if_class);
}

static const char* get_usbdev_icon(const usbd *u) {
const char * icon = NULL;
usbi *curr_if;

curr_if = u->if_list;
while (icon == NULL && curr_if != NULL){
icon = get_usbif_icon(curr_if);
curr_if = curr_if->next;
}

if (icon == NULL){
icon = get_class_icon(u->dev_class);
}

return icon;
}

static void _usb_dev(const usbd *u) {
gchar *name, *key, *v_str, *str;
gchar *name, *key, *v_str, *label, *str;
gchar *product, *vendor, *dev_class_str, *dev_subclass_str; /* don't free */
gchar *if_class_str, *if_subclass_str, *if_protocol_str; /* don't free */
gchar *interfaces = strdup("");
usbi *i;
const char* icon;

vendor = UNKIFNULL_AC(u->vendor);
product = UNKIFNULL_AC(u->product);
Expand All @@ -37,8 +112,11 @@ static void _usb_dev(const usbd *u) {

name = g_strdup_printf("%s %s", vendor, product);
key = g_strdup_printf("USB%03d:%03d:%03d", u->bus, u->dev, 0);
label = g_strdup_printf("%03d:%03d", u->bus, u->dev);
icon = get_usbdev_icon(u);

usb_list = h_strdup_cprintf("$%s$%03d:%03d=%s\n", usb_list, key, u->bus, u->dev, name);
usb_list = h_strdup_cprintf("$%s$%s=%s\n", usb_list, key, label, name);
usb_icons = h_strdup_cprintf("Icon$%s$%s=%s.png\n", usb_icons, key, label, icon ? icon: "usb");

const gchar *v_url = vendor_get_url(vendor);
const gchar *v_name = vendor_get_name(vendor);
Expand All @@ -48,6 +126,27 @@ static void _usb_dev(const usbd *u) {
v_str = g_strdup_printf("%s", vendor );
}

if (u->if_list != NULL) {
i = u->if_list;
while (i != NULL){
if_class_str = UNKIFNULL_AC(i->if_class_str);
if_subclass_str = UNKIFNULL_AC(i->if_subclass_str);
if_protocol_str = UNKIFNULL_AC(i->if_protocol_str);

interfaces = h_strdup_cprintf("[%s %d]\n"
/* Class */ "%s=[%d] %s\n"
/* Sub-class */ "%s=[%d] %s\n"
/* Protocol */ "%s=[%d] %s\n",
interfaces,
_("Interface"), i->if_number,
_("Class"), i->if_class, if_class_str,
_("Sub-class"), i->if_subclass, if_subclass_str,
_("Protocol"), i->if_protocol, if_protocol_str
);
i = i->next;
}
}

str = g_strdup_printf("[%s]\n"
/* Product */ "%s=[0x%04x] %s\n"
/* Manufacturer */ "%s=[0x%04x] %s\n"
Expand All @@ -58,7 +157,8 @@ static void _usb_dev(const usbd *u) {
/* Dev Version */ "%s=%s\n"
"[%s]\n"
/* Bus */ "%s=%03d\n"
/* Device */ "%s=%03d\n",
/* Device */ "%s=%03d\n"
/* Interfaces */ "%s",
_("Device Information"),
_("Product"), u->product_id, product,
_("Vendor"), u->vendor_id, v_str,
Expand All @@ -69,14 +169,17 @@ static void _usb_dev(const usbd *u) {
_("Device Version"), u->device_version,
_("Connection"),
_("Bus"), u->bus,
_("Device"), u->dev
_("Device"), u->dev,
interfaces
);

moreinfo_add_with_prefix("DEV", key, str); /* str now owned by morinfo */

g_free(v_str);
g_free(name);
g_free(key);
g_free(label);
g_free(interfaces);
}

void __scan_usb(void) {
Expand All @@ -89,6 +192,10 @@ void __scan_usb(void) {
moreinfo_del_with_prefix("DEV:USB");
g_free(usb_list);
}
if (usb_icons){
g_free(usb_icons);
usb_icons = NULL;
}
usb_list = g_strdup_printf("[%s]\n", _("USB Devices"));

if (c > 0) {
Expand Down
Binary file added pixmaps/camera-photo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pixmaps/camera-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pixmaps/media-removable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c32c60

Please sign in to comment.