mirror of
https://github.com/aaru-dps/aaruremote.git
synced 2025-12-16 19:24:37 +00:00
General refactor.
This commit is contained in:
126
linux/usb.c
126
linux/usb.c
@@ -23,114 +23,114 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
uint8_t linux_get_usb_data(const char* devicePath,
|
||||
uint16_t* descLen,
|
||||
char* descriptors,
|
||||
uint16_t* idVendor,
|
||||
uint16_t* idProduct,
|
||||
char* manufacturer,
|
||||
char* product,
|
||||
char* serial)
|
||||
uint8_t LinuxGetUsbData(const char* device_path,
|
||||
uint16_t* desc_len,
|
||||
char* descriptors,
|
||||
uint16_t* id_vendor,
|
||||
uint16_t* id_product,
|
||||
char* manufacturer,
|
||||
char* product,
|
||||
char* serial)
|
||||
{
|
||||
char* devPath;
|
||||
char tmpPath[4096];
|
||||
char resolvedLink[4096];
|
||||
char* dev_path;
|
||||
char tmp_path[4096];
|
||||
char resolved_link[4096];
|
||||
struct stat sb;
|
||||
ssize_t len;
|
||||
char* rchr;
|
||||
int found = 1;
|
||||
FILE* file;
|
||||
|
||||
*descLen = 0;
|
||||
*idVendor = 0;
|
||||
*idProduct = 0;
|
||||
memset(tmpPath, 0, 4096);
|
||||
memset(resolvedLink, 0, 4096);
|
||||
*desc_len = 0;
|
||||
*id_vendor = 0;
|
||||
*id_product = 0;
|
||||
memset(tmp_path, 0, 4096);
|
||||
memset(resolved_link, 0, 4096);
|
||||
|
||||
if(strncmp(devicePath, "/dev/sd", 7) != 0 && strncmp(devicePath, "/dev/sr", 7) != 0 &&
|
||||
strncmp(devicePath, "/dev/st", 7) != 0)
|
||||
if(strncmp(device_path, "/dev/sd", 7) != 0 && strncmp(device_path, "/dev/sr", 7) != 0 &&
|
||||
strncmp(device_path, "/dev/st", 7) != 0)
|
||||
return 0;
|
||||
|
||||
devPath = (char*)devicePath + 5;
|
||||
dev_path = (char*)device_path + 5;
|
||||
|
||||
snprintf(tmpPath, 4096, "/sys/block/%s", devPath);
|
||||
snprintf(tmp_path, 4096, "/sys/block/%s", dev_path);
|
||||
|
||||
if(stat(tmpPath, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
|
||||
if(stat(tmp_path, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; }
|
||||
|
||||
len = readlink(tmpPath, resolvedLink, 4096);
|
||||
len = readlink(tmp_path, resolved_link, 4096);
|
||||
|
||||
if(len == 0) return 0;
|
||||
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmpPath, 4096, "/sys%s", resolvedLink + 2);
|
||||
memcpy(resolvedLink, tmpPath, 4096);
|
||||
memset(tmp_path, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "/sys%s", resolved_link + 2);
|
||||
memcpy(resolved_link, tmp_path, 4096);
|
||||
|
||||
while(strstr(resolvedLink, "usb") != NULL)
|
||||
while(strstr(resolved_link, "usb") != NULL)
|
||||
{
|
||||
found = 1;
|
||||
rchr = strrchr(resolvedLink, '/');
|
||||
rchr = strrchr(resolved_link, '/');
|
||||
|
||||
if(rchr == NULL) break;
|
||||
|
||||
*rchr = '\0';
|
||||
|
||||
if(strlen(resolvedLink) == 0) break;
|
||||
if(strlen(resolved_link) == 0) break;
|
||||
|
||||
snprintf(tmpPath, 4096, "%s/descriptors", resolvedLink);
|
||||
if(access(tmpPath, R_OK) != 0) found = 0;
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/descriptors", resolved_link);
|
||||
if(access(tmp_path, R_OK) != 0) found = 0;
|
||||
memset(tmp_path, 0, 4096);
|
||||
|
||||
snprintf(tmpPath, 4096, "%s/idProduct", resolvedLink);
|
||||
if(access(tmpPath, R_OK) != 0) found = 0;
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/idProduct", resolved_link);
|
||||
if(access(tmp_path, R_OK) != 0) found = 0;
|
||||
memset(tmp_path, 0, 4096);
|
||||
|
||||
snprintf(tmpPath, 4096, "%s/idVendor", resolvedLink);
|
||||
if(access(tmpPath, R_OK) != 0) found = 0;
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/idVendor", resolved_link);
|
||||
if(access(tmp_path, R_OK) != 0) found = 0;
|
||||
memset(tmp_path, 0, 4096);
|
||||
|
||||
if(!found) continue;
|
||||
|
||||
snprintf(tmpPath, 4096, "%s/descriptors", resolvedLink);
|
||||
file = fopen(tmpPath, "r");
|
||||
snprintf(tmp_path, 4096, "%s/descriptors", resolved_link);
|
||||
file = fopen(tmp_path, "r");
|
||||
|
||||
if(file == NULL) break;
|
||||
|
||||
*descLen = (uint16_t)fread(descriptors, 4096, 1, file);
|
||||
*desc_len = (uint16_t)fread(descriptors, 4096, 1, file);
|
||||
|
||||
fclose(file);
|
||||
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmpPath, 4096, "%s/idProduct", resolvedLink);
|
||||
memset(tmp_path, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/idProduct", resolved_link);
|
||||
|
||||
if(access(tmpPath, R_OK) == 0)
|
||||
if(access(tmp_path, R_OK) == 0)
|
||||
{
|
||||
file = fopen(tmpPath, "r");
|
||||
file = fopen(tmp_path, "r");
|
||||
if(file != NULL)
|
||||
{
|
||||
fscanf(file, "%4hx", idProduct);
|
||||
fscanf(file, "%4hx", id_product);
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmpPath, 4096, "%s/idVendor", resolvedLink);
|
||||
memset(tmp_path, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/idVendor", resolved_link);
|
||||
|
||||
if(access(tmpPath, R_OK) == 0)
|
||||
if(access(tmp_path, R_OK) == 0)
|
||||
{
|
||||
file = fopen(tmpPath, "r");
|
||||
file = fopen(tmp_path, "r");
|
||||
if(file != NULL)
|
||||
{
|
||||
fscanf(file, "%4hx", idVendor);
|
||||
fscanf(file, "%4hx", id_vendor);
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmpPath, 4096, "%s/manufacturer", resolvedLink);
|
||||
memset(tmp_path, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/manufacturer", resolved_link);
|
||||
|
||||
if(access(tmpPath, R_OK) == 0)
|
||||
if(access(tmp_path, R_OK) == 0)
|
||||
{
|
||||
file = fopen(tmpPath, "r");
|
||||
file = fopen(tmp_path, "r");
|
||||
if(file != NULL)
|
||||
{
|
||||
fread(manufacturer, 256, 1, file);
|
||||
@@ -138,12 +138,12 @@ uint8_t linux_get_usb_data(const char* devicePath,
|
||||
}
|
||||
}
|
||||
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmpPath, 4096, "%s/product", resolvedLink);
|
||||
memset(tmp_path, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/product", resolved_link);
|
||||
|
||||
if(access(tmpPath, R_OK) == 0)
|
||||
if(access(tmp_path, R_OK) == 0)
|
||||
{
|
||||
file = fopen(tmpPath, "r");
|
||||
file = fopen(tmp_path, "r");
|
||||
if(file != NULL)
|
||||
{
|
||||
fread(product, 256, 1, file);
|
||||
@@ -151,12 +151,12 @@ uint8_t linux_get_usb_data(const char* devicePath,
|
||||
}
|
||||
}
|
||||
|
||||
memset(tmpPath, 0, 4096);
|
||||
snprintf(tmpPath, 4096, "%s/serial", resolvedLink);
|
||||
memset(tmp_path, 0, 4096);
|
||||
snprintf(tmp_path, 4096, "%s/serial", resolved_link);
|
||||
|
||||
if(access(tmpPath, R_OK) == 0)
|
||||
if(access(tmp_path, R_OK) == 0)
|
||||
{
|
||||
file = fopen(tmpPath, "r");
|
||||
file = fopen(tmp_path, "r");
|
||||
if(file != NULL)
|
||||
{
|
||||
fread(serial, 256, 1, file);
|
||||
@@ -167,5 +167,5 @@ uint8_t linux_get_usb_data(const char* devicePath,
|
||||
break;
|
||||
}
|
||||
|
||||
return *descLen != 0;
|
||||
return *desc_len != 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user