diff --git a/CMakeLists.txt b/CMakeLists.txt index be6655b..9c66624 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,10 +3,10 @@ project(dicremote C) set(CMAKE_C_STANDARD 90) -set(MAIN_SOURCES main.c list_devices.c device.c scsi.c hex2bin.c) +set(MAIN_SOURCES main.c list_devices.c device.c scsi.c hex2bin.c usb.c) if("${CMAKE_SYSTEM}" MATCHES "Linux") - set(PLATFORM_SOURCES linux/list_devices.c linux/linux.h linux/device.c linux/scsi.c) + set(PLATFORM_SOURCES linux/list_devices.c linux/linux.h linux/device.c linux/scsi.c linux/usb.c) endif() add_executable(dicremote ${MAIN_SOURCES} ${PLATFORM_SOURCES}) diff --git a/dicmote.h b/dicmote.h index 88fdc08..b2d117b 100644 --- a/dicmote.h +++ b/dicmote.h @@ -418,5 +418,13 @@ int32_t GetSdhciRegisters(const char* devicePath, uint32_t* cid_len, uint32_t* ocr_len, uint32_t* scr_len); +uint8_t GetUsbData(const char* devicePath, + uint16_t* descLen, + char* descriptors, + uint16_t* idVendor, + uint16_t* idProduct, + char* manufacturer, + char* product, + char* serial); #endif diff --git a/linux/linux.h b/linux/linux.h index 1263d96..ba4b737 100644 --- a/linux/linux.h +++ b/linux/linux.h @@ -44,4 +44,12 @@ int32_t linux_get_sdhci_registers(const char* devicePath, uint32_t* cid_len, uint32_t* ocr_len, uint32_t* scr_len); +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); #endif // DICREMOTE_LINUX_H diff --git a/linux/usb.c b/linux/usb.c new file mode 100644 index 0000000..586d0a7 --- /dev/null +++ b/linux/usb.c @@ -0,0 +1,170 @@ +/* + * This file is part of the DiscImageChef Remote Server. + * Copyright (c) 2019 Natalia Portillo. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "linux.h" + +#include +#include +#include +#include +#include + +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) +{ + char* devPath; + char tmpPath[4096]; + char resolvedLink[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); + + if(strncmp(devicePath, "/dev/sd", 7) != 0 && strncmp(devicePath, "/dev/sr", 7) != 0 && + strncmp(devicePath, "/dev/st", 7) != 0) + return 0; + + devPath = (char*)devicePath + 5; + + snprintf(tmpPath, 4096, "/sys/block/%s", devPath); + + if(stat(tmpPath, &sb) != 0 || !S_ISDIR(sb.st_mode)) { return 0; } + + len = readlink(tmpPath, resolvedLink, 4096); + + if(len == 0) return 0; + + memset(tmpPath, 0, 4096); + snprintf(tmpPath, 4096, "/sys%s", resolvedLink + 2); + memcpy(resolvedLink, tmpPath, 4096); + + while(strstr(resolvedLink, "usb") != NULL) + { + rchr = strrchr(resolvedLink, '/'); + + if(rchr == NULL) break; + + *rchr = '\0'; + + if(strlen(resolvedLink) == 0) break; + + snprintf(tmpPath, 4096, "%s/descriptors", resolvedLink); + if(access(tmpPath, R_OK) != 0) found = 0; + memset(tmpPath, 0, 4096); + + snprintf(tmpPath, 4096, "%s/idProduct", resolvedLink); + if(access(tmpPath, R_OK) != 0) found = 0; + memset(tmpPath, 0, 4096); + + snprintf(tmpPath, 4096, "%s/idVendor", resolvedLink); + if(access(tmpPath, R_OK) != 0) found = 0; + memset(tmpPath, 0, 4096); + + if(!found) continue; + + snprintf(tmpPath, 4096, "%s/descriptors", resolvedLink); + file = fopen(tmpPath, "r"); + + if(file == NULL) break; + + *descLen = (uint16_t)fread(descriptors, 4096, 1, file); + + fclose(file); + + memset(tmpPath, 0, 4096); + snprintf(tmpPath, 4096, "%s/idProduct", resolvedLink); + + if(access(tmpPath, R_OK) == 0) + { + file = fopen(tmpPath, "r"); + if(file != NULL) + { + fscanf(file, "%4hx", idProduct); + fclose(file); + } + } + + memset(tmpPath, 0, 4096); + snprintf(tmpPath, 4096, "%s/idVendor", resolvedLink); + + if(access(tmpPath, R_OK) == 0) + { + file = fopen(tmpPath, "r"); + if(file != NULL) + { + fscanf(file, "%4hx", idVendor); + fclose(file); + } + } + + memset(tmpPath, 0, 4096); + snprintf(tmpPath, 4096, "%s/manufacturer", resolvedLink); + + if(access(tmpPath, R_OK) == 0) + { + file = fopen(tmpPath, "r"); + if(file != NULL) + { + fread(manufacturer, 256, 1, file); + fclose(file); + } + } + + memset(tmpPath, 0, 4096); + snprintf(tmpPath, 4096, "%s/product", resolvedLink); + + if(access(tmpPath, R_OK) == 0) + { + file = fopen(tmpPath, "r"); + if(file != NULL) + { + fread(product, 256, 1, file); + fclose(file); + } + } + + memset(tmpPath, 0, 4096); + snprintf(tmpPath, 4096, "%s/serial", resolvedLink); + + if(access(tmpPath, R_OK) == 0) + { + file = fopen(tmpPath, "r"); + if(file != NULL) + { + fread(serial, 256, 1, file); + fclose(file); + } + } + + break; + } + + return descLen != 0; +} diff --git a/usb.c b/usb.c new file mode 100644 index 0000000..be0b924 --- /dev/null +++ b/usb.c @@ -0,0 +1,38 @@ +/* + * This file is part of the DiscImageChef Remote Server. + * Copyright (c) 2019 Natalia Portillo. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#if defined(__linux__) && !defined(__ANDROID__) +#include "linux/linux.h" +#endif + +uint8_t GetUsbData(const char* devicePath, + uint16_t* descLen, + char* descriptors, + uint16_t* idVendor, + uint16_t* idProduct, + char* manufacturer, + char* product, + char* serial) +{ +#if defined(__linux__) && !defined(__ANDROID__) + return linux_get_usb_data(devicePath, descLen, descriptors, idVendor, idProduct, manufacturer, product, serial); +#else + return 0; +#endif +}