diff --git a/README.md b/README.md index bd7d7b4..330bc55 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ device prototyping on a linux host. The code in **src/** is a C implementation of a C++ version I did back in 2016. -The C++ version is still available in the **cpp/** folderc, but it will probably not be updated... +The C++ version is still available in the **cpp_version** branch in the **cpp/** folderc, but it will probably not be updated... ## Requirements diff --git a/cpp/Makefile b/cpp/Makefile deleted file mode 100644 index b99acf8..0000000 --- a/cpp/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -all: hidmouse bulkio - -hidmouse: - make -f Makefile.hidmouse -j - -bulkio: - make -f Makefile.bulkio -j - -clean: - make -f Makefile.hidmouse clean - -clang-format: - find src | grep -E "\.h$$|\.cc$$" | xargs clang-format -i diff --git a/cpp/Makefile.bulkio b/cpp/Makefile.bulkio deleted file mode 100644 index 7fac4fc..0000000 --- a/cpp/Makefile.bulkio +++ /dev/null @@ -1,12 +0,0 @@ -PROG=BulkIO - -include common-src.mk - -SRC_FILES += BulkIO.cc - -include build-rules.mk - -all: $(PROG) - -clean: - rm -rf $(OBJDIR) diff --git a/cpp/Makefile.hidmouse b/cpp/Makefile.hidmouse deleted file mode 100644 index d7706c1..0000000 --- a/cpp/Makefile.hidmouse +++ /dev/null @@ -1,12 +0,0 @@ -PROG=HidMouse - -include common-src.mk - -SRC_FILES += HidMouse.cc - -include build-rules.mk - -all: $(PROG) - -clean: - rm -rf $(OBJDIR) diff --git a/cpp/README.md b/cpp/README.md deleted file mode 100644 index 54d5faf..0000000 --- a/cpp/README.md +++ /dev/null @@ -1,61 +0,0 @@ -# usbip-server - -usbip is a part of the linux kernel (kernel version >= 3.17) and is used to export USB devices over TCP network from one computer (server) to another (client) -The usbip protocol can also be used to create a playground for USB stack and/or USB device development. - -The goal for this project is to implement a usbip-server that can handle USB devices in a VSP (Virtual System Prototype) environment. - -## Requirements -- linux kernel >= 3.17 -- package linux-tools-generic [apt-get install linux-tools-generic] - -## Disclaimer - -!!! WARNING !!! - -Playing around with kernel modules and Virtual USB devices can cause the kernel to hang! - -!!! WARNING !!! - -## Build C++ version -- cd usbip-server/cpp -- make - -## Start - -#### HidMouse -- .x86/HidMouse -vv - -#### BulkIO -- .x86/BulkIO -vv - -###### Test Tool -- sudo ./tools/test_bulkio.py - -## Attach Virtual Device - -### Load Module -- sudo modprobe vhci-hcd - -### List Devices -- usbip list -r **host** - -### Attach Virtual USB Device -- sudo usbip attach -b 1-1 -r **host** - -### Detach Virtual USB Device -- sudo usbip detach -p 0 - -## Current State -- Handles 1 (one) device over the usbip protocol -- USB Stack handles a single interrupt endpoint (at least) -- A HID Mouse Device is implemented -- A BulkIO Device is implemented - -## Star History - -[![Star History Chart](https://api.star-history.com/svg?repos=freand76/usbip-server&type=Date)](https://star-history.com/#freand76/usbip-server&Date) - -## Todo -- Handle several virtual USB devices in the UsbIpServer -- Implement fully functional USB stack diff --git a/cpp/build-rules.mk b/cpp/build-rules.mk deleted file mode 100644 index 17c5407..0000000 --- a/cpp/build-rules.mk +++ /dev/null @@ -1,52 +0,0 @@ -CPP=g++ - -LDFLAGS= -CFLAGS= - -CFLAGS+=-pedantic - -# Developer flags (gdb) -CFLAGS+=-ggdb - -# Developer flags (sanitize) -CFLAGS+=-fsanitize=address -LDFLAGS+=-lasan - -CFLAGS+=-O2 -std=c++11 -Werror -Wextra -Wall -I. -CFLAGS+=-I./src/server \ - -I./src/usb \ - -I./src/device \ - -I./src/util \ - -I./src/log \ - -I./src/app - -LDFLAGS+=-lpthread - - -OBJDIR=.x86 -$(shell mkdir -p $(OBJDIR)) - -OBJ_FILES=$(addprefix $(OBJDIR)/,$(SRC_FILES:.cc=.o)) - -PROG:=$(addprefix $(OBJDIR)/,$(PROG)) - -$(OBJDIR)/%.o : src/server/%.cc - $(CPP) -c $(CFLAGS) $^ -o $@ - -$(OBJDIR)/%.o : src/usb/%.cc - $(CPP) -c $(CFLAGS) $^ -o $@ - -$(OBJDIR)/%.o : src/device/%.cc - $(CPP) -c $(CFLAGS) $^ -o $@ - -$(OBJDIR)/%.o : src/util/%.cc - $(CPP) -c $(CFLAGS) $^ -o $@ - -$(OBJDIR)/%.o : src/log/%.cc - $(CPP) -c $(CFLAGS) $^ -o $@ - -$(OBJDIR)/%.o : src/app/%.cc - $(CPP) -c $(CFLAGS) $^ -o $@ - -$(PROG) : $(OBJ_FILES) - $(CPP) $(LDFLAGS) $^ -o $@ $(LDFLAGS) diff --git a/cpp/common-src.mk b/cpp/common-src.mk deleted file mode 100644 index e10836b..0000000 --- a/cpp/common-src.mk +++ /dev/null @@ -1,13 +0,0 @@ -SRC_FILES = \ - UsbIpServer.cc \ - UsbIpDevice.cc \ - UsbDevice.cc \ - UsbConfiguration.cc \ - UsbInterface.cc \ - UsbEndpoint.cc \ - UsbString.cc \ - UsbUtil.cc \ - NetworkUtil.cc \ - Verbose.cc - - diff --git a/cpp/src/app/AppBase.h b/cpp/src/app/AppBase.h deleted file mode 100644 index b5112ea..0000000 --- a/cpp/src/app/AppBase.h +++ /dev/null @@ -1,95 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef APP_BASE_H -#define APP_BASE_H - -#include "Verbose.h" -#include "UsbIpServer.h" - -using namespace Verbose; - -class AppBase { - public: - bool HandleArguments(int argc, char *argv[]) { - if (findArgument("-h", argc, argv) || findArgument("--help", argc, argv)) { - printf("Usage:\n"); - printf("-h / --help - Help text\n"); - printf("-v - Verbose [INFO]\n"); - printf("-vv - Verbose [DEBUG]\n"); - printf("\n"); - return false; - } - - if (findArgument("-v", argc, argv)) { - SetVerboseLevel(LEVEL_INFO); - } - - if (findArgument("-vv", argc, argv)) { - SetVerboseLevel(LEVEL_DEBUG); - } - - INFO("Command: %s", argv[0]); - for (int idx = 1; idx < argc; idx++) { - DEBUG(" arg%d = %s", idx, argv[idx]); - } - - return true; - } - - void AddDevice(UsbDevice *dev, string path, string busId, int busNum, int devNum, enum usb_device_speed speed) { - usbIpServer.AddDevice(dev, path, busId, busNum, devNum, speed); - } - - bool Init() { - if (usbIpServer.Init()) { - INFO("Init OK"); - return true; - } - return false; - } - - bool Start() { - printf("Starting server\n"); - if (!usbIpServer.StartServer()) { - ERROR("Could not start server"); - return false; - } - return true; - } - - void Stop() { - printf("Stopping server:\n"); - if (usbIpServer.ConnectedClients()) { - printf("\n"); - printf(" - Press Ctrl-C a couple of times if you do not want\n"); - printf(" to wait for all clients to disconnect\n\n"); - } - usbIpServer.StopServer(); - } - - private: - static bool findArgument(const char *arg, int argc, char *argv[]) { - for (int idx = 0; idx < argc; idx++) { - if (strcmp(arg, argv[idx]) == 0) { - return true; - } - } - return false; - } - - UsbIpServer usbIpServer; -}; - -#endif // APP_BASE_H diff --git a/cpp/src/device/BulkIO.cc b/cpp/src/device/BulkIO.cc deleted file mode 100644 index 595bd7a..0000000 --- a/cpp/src/device/BulkIO.cc +++ /dev/null @@ -1,97 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include -#include -#include "BulkIO.h" -#include "UsbUtil.h" -#include "AppBase.h" -#include "Verbose.h" - -using namespace UsbUtil; -using namespace Verbose; - -int BulkInputEndpoint::Data(uint8_t *dataIn, uint8_t *dataOut, int length) { - (void)dataIn; - static int outputIdx = 0; - sprintf((char *)dataOut, "OutputData (%d)", outputIdx++); - printf("USB Bulk %d bytes requested from device\n", length); - printf(" - Sending data [%s]\n", (char *)dataOut); - return strlen((char *)dataOut); -} - -int BulkOutputEndpoint::Data(uint8_t *dataIn, uint8_t *dataOut, int length) { - (void)dataOut; - printf("%d:%s\n", length, dataIn); - printf("USB Bulk device received %d bytes\n", length); - printf(" - Hex Bytes: "); - for (int idx = 0; idx < length; idx++) { - printf("0x%.2x, ", dataIn[idx]); - } - printf("\n"); - printf(" - String: %s\n", (char *)dataIn); - return 0; -} - -static volatile int keepRunning = 3; - -void intHandler(int) { - static int panicCounter = 0; - if (keepRunning == 3) { - fprintf(stderr, "\nCtrl-C received, Do it 3 times if you really want to quit\n"); - } else { - fprintf(stderr, "\n"); - } - fflush(stderr); - - if (keepRunning > 0) { - keepRunning--; - } else { - panicCounter++; - if (panicCounter == 5) { - exit(0); - } - } -} - -int main(int argc, char *argv[]) { - AppBase app; - if (!app.HandleArguments(argc, argv)) { - exit(0); - } - - signal(SIGINT, intHandler); - - if (!app.Init()) { - return EXIT_FAILURE; - } - - BulkIO bulkIO(0x00fa, 0xc001, 0x1234); - app.AddDevice(&bulkIO, "My First Virtual BulkIO", "1-1", 2, 3, USB_SPEED_FULL); - - if (!app.Start()) { - return EXIT_FAILURE; - } - - while (keepRunning > 0) { - usleep(500 * 1000); - if (bulkIO.IsConnected()) { - INFO("- Connected"); - } - } - - app.Stop(); - - return EXIT_SUCCESS; -} diff --git a/cpp/src/device/BulkIO.h b/cpp/src/device/BulkIO.h deleted file mode 100644 index dd60b84..0000000 --- a/cpp/src/device/BulkIO.h +++ /dev/null @@ -1,51 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include "UsbDevice.h" -#include "UsbConfiguration.h" -#include "UsbInterface.h" -#include "UsbEndpoint.h" - -const char *stringArray[3] = { - "Bulk Manufacturer", - "Bulk Device Name", - "ABC123", -}; - -class BulkInputEndpoint : public UsbEndpoint { - public: - BulkInputEndpoint() : UsbEndpoint(0x81, 2, 64, 255){}; - int Data(uint8_t *inData, uint8_t *outBuffer, int length); -}; - -class BulkOutputEndpoint : public UsbEndpoint { - public: - BulkOutputEndpoint() : UsbEndpoint(0x01, 2, 64, 255){}; - int Data(uint8_t *inData, uint8_t *outBuffer, int length); -}; - -class BulkIO : public UsbDevice { - public: - BulkIO(uint16_t vid, uint16_t pid, uint16_t bcdVer) : UsbDevice(vid, pid, bcdVer, 0, 0, 0, 1, configurationList, &usbString, 1, 2, 3){}; - - private: - BulkInputEndpoint inputEndpoint; - BulkOutputEndpoint outputEndpoint; - UsbEndpoint *endpointList[2] = {&inputEndpoint, &outputEndpoint}; - UsbInterface interface = {0, 0xff, 0, 0, 2, endpointList}; - UsbInterface *interfaceList[1] = {&interface}; - UsbConfiguration config = {11, 1, interfaceList}; - UsbConfiguration *configurationList[1] = {&config}; - UsbString usbString = {3, stringArray}; -}; diff --git a/cpp/src/device/HidMouse.cc b/cpp/src/device/HidMouse.cc deleted file mode 100644 index d149a5f..0000000 --- a/cpp/src/device/HidMouse.cc +++ /dev/null @@ -1,173 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include -#include -#include "HidMouse.h" -#include "UsbUtil.h" -#include "AppBase.h" - -using namespace UsbUtil; - -/* Debug */ -#include "Verbose.h" -using namespace Verbose; - -#define MOUSE_REPORT_ID 100 - -int HidMouseInterface::GenerateInterfaceDescriptor(uint8_t *buffer, int offset) { - int pos = offset; - pos += UsbInterface::GenerateInterfaceDescriptor(buffer, pos); - int rSize = GenerateHIDReportDescriptor(NULL, 0); - - pos += SetUint(9, buffer, pos, 1); - pos += SetUint(0x21, buffer, pos, 1); - pos += SetUint(0x110, buffer, pos, 2); - pos += SetUint(0, buffer, pos, 1); - pos += SetUint(1, buffer, pos, 1); - pos += SetUint(0x22, buffer, pos, 1); - pos += SetUint(rSize, buffer, pos, 2); - - return pos - offset; -} - -static uint8_t reportDescriptorHID[] = {0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x01, // COLLECTION (Application) - 0x09, 0x01, // USAGE (Pointer) - 0xa1, 0x00, // COLLECTION (Physical) - 0x85, MOUSE_REPORT_ID, // REPORT_ID - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x03, // USAGE_MAXIMUM (Button 3) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x95, 0x03, // REPORT_COUNT (3) - 0x75, 0x01, // REPORT_SIZE (1) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0x95, 0x01, // REPORT_COUNT (1) - 0x75, 0x05, // REPORT_SIZE (5) - 0x81, 0x03, // INPUT (Cnst,Var,Abs) - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x02, // REPORT_COUNT (2) - 0x81, 0x06, // INPUT (Data,Var,Rel) - 0xc0, // END_COLLECTION - 0xc0}; - -int HidMouseInterface::GenerateHIDReportDescriptor(uint8_t *buffer, int offset) { - int pos = offset; - for (unsigned int idx = 0; idx < sizeof(reportDescriptorHID); idx++) { - pos += SetUint(reportDescriptorHID[idx], buffer, pos, 1); - } - return pos - offset; -} - -int HidMouseInterface::GetDescriptor(uint8_t *setup, uint8_t *data, uint8_t *replyBuffer, int bufLength) { - uint8_t bDescriptorType = setup[3]; - - if (bDescriptorType == 0x22) { - return GenerateHIDReportDescriptor(replyBuffer, 0); - } - - return UsbInterface::GetDescriptor(setup, data, replyBuffer, bufLength); -} - -int HidMouseEndpoint::Data(uint8_t *inData, uint8_t *outBuffer, int length) { - (void)inData; - (void)length; - - outBuffer[0] = MOUSE_REPORT_ID; - outBuffer[1] = event.but; - outBuffer[2] = event.x; - outBuffer[3] = event.y; - - event.but = 0; - event.x = 0; - event.y = 0; - event.free = true; - - return 4; -} - -bool HidMouse::Move(int but, int x, int y) { - if (endpoint.event.free) { - endpoint.event.but = but; - endpoint.event.x = x; - endpoint.event.y = y; - endpoint.event.free = false; - return true; - } - return false; -} - -static volatile int keepRunning = 3; - -void intHandler(int) { - static int panicCounter = 0; - if (keepRunning == 3) { - fprintf(stderr, "\nCtrl-C received, Do it 3 times if you really want to quit\n"); - } else { - fprintf(stderr, "\n"); - } - fflush(stderr); - - if (keepRunning > 0) { - keepRunning--; - } else { - panicCounter++; - if (panicCounter == 5) { - exit(0); - } - } -} - -int main(int argc, char *argv[]) { - AppBase app; - if (!app.HandleArguments(argc, argv)) { - exit(0); - } - - signal(SIGINT, intHandler); - - if (!app.Init()) { - return EXIT_FAILURE; - } - - HidMouse mouse(0x00fa, 0xc001, 0x1234); - app.AddDevice(&mouse, "My First Virtual Mouse", "1-1", 2, 3, USB_SPEED_FULL); - - if (!app.Start()) { - return EXIT_FAILURE; - } - - while (keepRunning > 0) { - usleep(500 * 1000); - if (mouse.IsConnected()) { - if (!mouse.Move(0, 20, 20)) { - INFO("- Mouse event-queue overflow\n"); - } else { - INFO("- Mouse move"); - } - } - } - - app.Stop(); - - return EXIT_SUCCESS; -} diff --git a/cpp/src/device/HidMouse.h b/cpp/src/device/HidMouse.h deleted file mode 100644 index 3c1f060..0000000 --- a/cpp/src/device/HidMouse.h +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include "UsbDevice.h" -#include "UsbConfiguration.h" -#include "UsbInterface.h" -#include "UsbEndpoint.h" - -typedef struct { - bool free; - int but; - int x; - int y; -} MouseEvent_t; - -const char *stringArray[5] = { - "Mouse Manufacturer", "Automatic Mouse Mover", "Mouse-1337", "Mouse Configuration", "Mouse Interface", -}; - -class HidMouseEndpoint : public UsbEndpoint { - public: - HidMouseEndpoint() : UsbEndpoint(0x81, 3, 64, 255){}; - int Data(uint8_t *inData, uint8_t *outBuffer, int length); - MouseEvent_t event = {true, 0, 0, 0}; -}; - -class HidMouseInterface : public UsbInterface { - public: - HidMouseInterface(uint8_t bInterfaceNumber, uint8_t bNumEndpoints, UsbEndpoint **endpointArray, uint8_t iInterface) - : UsbInterface(bInterfaceNumber, 3, 1, 2, bNumEndpoints, endpointArray, iInterface){}; - int GenerateInterfaceDescriptor(uint8_t *buffer, int offset); - int GetDescriptor(uint8_t *setup, uint8_t *data, uint8_t *replyBuffer, int bufLength); - - private: - int GenerateHIDReportDescriptor(uint8_t *buffer, int offset); -}; - -class HidMouse : public UsbDevice { - public: - HidMouse(uint16_t vid, uint16_t pid, uint16_t bcdVer) : UsbDevice(vid, pid, bcdVer, 0, 0, 0, 1, configurationList, &usbString, 1, 2, 3){}; - - bool Move(int but, int x, int y); - - private: - HidMouseEndpoint endpoint; - UsbEndpoint *endpointList[1] = {&endpoint}; - HidMouseInterface interface = {0, 1, endpointList, 5}; - UsbInterface *interfaceList[1] = {&interface}; - UsbConfiguration config = {0x77, 1, interfaceList, 4}; - UsbConfiguration *configurationList[1] = {&config}; - UsbString usbString = {5, stringArray}; -}; diff --git a/cpp/src/log/Verbose.cc b/cpp/src/log/Verbose.cc deleted file mode 100644 index 89c78ed..0000000 --- a/cpp/src/log/Verbose.cc +++ /dev/null @@ -1,108 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include "Verbose.h" -#include -#include - -namespace Verbose { - static VerboseLogLevel_t logLevel = LEVEL_ERROR; - - void SetVerboseLevel(VerboseLogLevel_t level) { - logLevel = level; - } - - FILE *TYPE(VerboseLogLevel_t level) { - if (logLevel < level) return NULL; - - FILE *fd = NULL; - switch (level) { - case LEVEL_DEBUG: - fd = stdout; - std::fprintf(fd, "[DEBUG] "); - break; - case LEVEL_INFO: - fd = stdout; - std::fprintf(fd, "[INFO] "); - break; - case LEVEL_ERROR: - fd = stderr; - std::fprintf(fd, "[ERROR] "); - break; - } - return fd; - } - - void TEXT(VerboseLogLevel_t level, const char *format, va_list arglist) { - FILE *fd = TYPE(level); - if (fd == NULL) return; - - std::vfprintf(fd, format, arglist); - std::fprintf(fd, "\n"); - std::fflush(fd); - } - - void VECTOR(VerboseLogLevel_t level, const char *name, uint8_t *vector, int size) { - FILE *fd = TYPE(level); - if (fd == NULL) return; - - std::fprintf(fd, "%s (%d):", name, size); - for (int idx = 0; idx < size; idx++) { - if ((idx % 16) == 0) { - std::fprintf(fd, "\n"); - TYPE(level); - } - if ((idx % 16) == 8) { - std::fprintf(fd, " "); - } - std::fprintf(fd, "%.2x ", vector[idx]); - } - std::fprintf(fd, "\n"); - std::fflush(fd); - } - - void ERROR(const char *format, ...) { - va_list arglist; - va_start(arglist, format); - TEXT(LEVEL_ERROR, format, arglist); - va_end(arglist); - } - - void INFO(const char *format, ...) { - va_list arglist; - va_start(arglist, format); - TEXT(LEVEL_INFO, format, arglist); - va_end(arglist); - } - - void DEBUG(const char *format, ...) { - va_list arglist; - va_start(arglist, format); - TEXT(LEVEL_DEBUG, format, arglist); - va_end(arglist); - } - - void ERROR_VECTOR(const char *name, uint8_t *vector, int size) { - VECTOR(LEVEL_ERROR, name, vector, size); - } - - void INFO_VECTOR(const char *name, uint8_t *vector, int size) { - VECTOR(LEVEL_INFO, name, vector, size); - } - - void DEBUG_VECTOR(const char *name, uint8_t *vector, int size) { - VECTOR(LEVEL_DEBUG, name, vector, size); - } - -} // namespace Verbose diff --git a/cpp/src/log/Verbose.h b/cpp/src/log/Verbose.h deleted file mode 100644 index 6c854d6..0000000 --- a/cpp/src/log/Verbose.h +++ /dev/null @@ -1,32 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef VERBOSE_H -#define VERBOSE_H - -#include - -namespace Verbose { - typedef enum { LEVEL_ERROR = 0, LEVEL_INFO, LEVEL_DEBUG } VerboseLogLevel_t; - - void SetVerboseLevel(VerboseLogLevel_t level); - void ERROR(const char *format, ...); - void ERROR_VECTOR(const char *name, uint8_t *vector, int size); - void INFO(const char *format, ...); - void INFO_VECTOR(const char *name, uint8_t *vector, int size); - void DEBUG(const char *format, ...); - void DEBUG_VECTOR(const char *name, uint8_t *vector, int size); -}; // namespace Verbose - -#endif // VERBOSE_H diff --git a/cpp/src/server/UsbIpDevice.cc b/cpp/src/server/UsbIpDevice.cc deleted file mode 100644 index 59cc7ac..0000000 --- a/cpp/src/server/UsbIpDevice.cc +++ /dev/null @@ -1,85 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include - -#include "UsbIpDevice.h" -#include "UsbDevice.h" -#include "UsbConfiguration.h" -#include "UsbInterface.h" -#include "NetworkUtil.h" - -using namespace NetworkUtil; - -UsbIpDevice::UsbIpDevice(UsbDevice *d, string path, string busId, int busNum, int devNum, enum usb_device_speed speed) { - this->d = d; - memset(this->pathStrBuf, 0, sizeof(this->pathStrBuf)); - if (path.length() < sizeof(this->pathStrBuf)) { - strcpy(this->pathStrBuf, path.c_str()); - } - memset(this->busIdStrBuf, 0, sizeof(this->busIdStrBuf)); - if (busId.length() < sizeof(this->busIdStrBuf)) { - strcpy(this->busIdStrBuf, busId.c_str()); - } - this->busNum = busNum; - this->devNum = devNum; - this->speed = speed; -} - -int UsbIpDevice::FillDeviceData(uint8_t *buffer, int offset, bool withInterfaces) { - int pos = offset; - - pos += CopyString(this->pathStrBuf, sizeof(this->pathStrBuf), buffer, pos); - pos += CopyString(this->busIdStrBuf, sizeof(this->busIdStrBuf), buffer, pos); - pos += SetUint(busNum, buffer, pos, 4); /* */ - pos += SetUint(devNum, buffer, pos, 4); /* */ - pos += SetUint(speed, buffer, pos, 4); /* */ - - pos += SetUint(d->idVendor, buffer, pos, 2); /* */ - pos += SetUint(d->idProduct, buffer, pos, 2); /* */ - pos += SetUint(d->bcdDevice, buffer, pos, 2); /* */ - pos += SetUint(d->bDeviceClass, buffer, pos, 1); /* */ - pos += SetUint(d->bDeviceSubClass, buffer, pos, 1); /* */ - pos += SetUint(d->bDeviceProtocol, buffer, pos, 1); /* */ - pos += SetUint(d->bConfigurationValue, buffer, pos, 1); /* */ - pos += SetUint(d->bNumConfigurations, buffer, pos, 1); /* */ - pos += SetUint(d->configurationArray[0]->bNumInterfaces, buffer, pos, 1); /* */ - - if (withInterfaces) { - for (int idx = 0; idx < d->configurationArray[0]->bNumInterfaces; idx++) { - UsbInterface *interface = d->configurationArray[0]->interfaceArray[idx]; - pos += SetUint(interface->bInterfaceClass, buffer, pos, 1); /* InterfaceClass */ - pos += SetUint(interface->bInterfaceSubClass, buffer, pos, 1); /* InterfaceSubClass*/ - pos += SetUint(interface->bInterfaceProtocol, buffer, pos, 1); /* InterfaceProtocol */ - pos += SetUint(0, buffer, pos, 1); /* Padding */ - } - } - - return pos - offset; -} - -int UsbIpDevice::CopyString(char *str, int length, uint8_t *buffer, int offset) { - if (buffer != NULL) { - memcpy(&buffer[offset], str, length); - } - return length; -} - -int UsbIpDevice::TxRx(int endPoint, uint8_t *setupBuffer, uint8_t *dataBuffer, uint8_t *outBuffer, int outBufferLength) { - return d->TxRx(endPoint, setupBuffer, dataBuffer, outBuffer, outBufferLength); -} - -void UsbIpDevice::Disconnect() { - d->Disconnect(); -} diff --git a/cpp/src/server/UsbIpDevice.h b/cpp/src/server/UsbIpDevice.h deleted file mode 100644 index 90348b0..0000000 --- a/cpp/src/server/UsbIpDevice.h +++ /dev/null @@ -1,50 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USBIP_DEVICE_H -#define USBIP_DEVICE_H - -#include -#include - -#include "UsbDevice.h" - -#define USBIP_PATH_STR_LENGTH 256 -#define USBIP_BUSID_STR_LENGTH 32 - -using namespace std; - -class UsbIpDevice { - public: - UsbIpDevice(UsbDevice *d = NULL, string path = "", string busId = "", int busNum = 0, int devNum = 0, - enum usb_device_speed speed = USB_SPEED_HIGH); - - int FillDeviceData(uint8_t *buffer, int offset, bool withInterfaces); - - int TxRx(int endPoint, uint8_t *setupBuffer, uint8_t *dataBuffer, uint8_t *outBuffer, int outBufferLength); - - void Disconnect(); - - private: - int CopyString(char *str, int length, uint8_t *buffer, int offset); - - UsbDevice *d; - int busNum; - int devNum; - int speed; - char pathStrBuf[USBIP_PATH_STR_LENGTH]; - char busIdStrBuf[USBIP_BUSID_STR_LENGTH]; -}; - -#endif // USBIP_DEVICE_H diff --git a/cpp/src/server/UsbIpServer.cc b/cpp/src/server/UsbIpServer.cc deleted file mode 100644 index 139ebc4..0000000 --- a/cpp/src/server/UsbIpServer.cc +++ /dev/null @@ -1,431 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include -#include -#include -#include -#include -#include - -#include - -#include "Verbose.h" -#include "UsbIpServer.h" -#include "NetworkUtil.h" - -using namespace Verbose; -using namespace NetworkUtil; - -#define USBIP_OP_HEAD_SIZE 8 -#define USBIP_CMD_HEAD_SIZE 48 -#define USBIP_VERSION 0x0111 -#define USBIP_MAX_PACKET_SIZE 4096 - -static void milliSleep(int ms) { - usleep(ms * 1000); -} - -UsbIpServer::UsbIpServer(int tcpPort) { - this->tcpPort = tcpPort; - this->serverSocketFd = -1; - this->serverWorkerActive = false; - this->killServerWorker = false; - this->activeClients = 0; - this->serverThread = NULL; -} - -UsbIpServer::~UsbIpServer() { - if (serverSocketFd >= 0) { - close(serverSocketFd); - } - - for (unsigned int idx = 0; idx < connectionThreads.size(); idx++) { - connectionThreads[idx]->join(); - delete connectionThreads[idx]; - } -} - -void UsbIpServer::AddDevice(UsbDevice *dev, string path, string busId, int busNum, int devNum, enum usb_device_speed speed) { - usbIpDevice = UsbIpDevice(dev, path, busId, busNum, devNum, speed); -} - -bool UsbIpServer::Init() { - struct sockaddr_in serverAddress; - - serverSocketFd = socket(AF_INET, SOCK_STREAM, 0); - if (serverSocketFd < 0) { - ERROR("Could not open server socket"); - return false; - } - - memset(&serverAddress, 0, sizeof(serverAddress)); - serverAddress.sin_family = AF_INET; - serverAddress.sin_addr.s_addr = INADDR_ANY; - serverAddress.sin_port = htons(tcpPort); - - if (bind(serverSocketFd, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) { - close(serverSocketFd); - serverSocketFd = -1; - ERROR("Could not bind server socket"); - return false; - } - - listen(serverSocketFd, 16); - - int flags = fcntl(serverSocketFd, F_GETFL, 0); - if (flags < 0) { - close(serverSocketFd); - serverSocketFd = -1; - ERROR("Could not get socket flags"); - return false; - } - flags |= O_NONBLOCK; - if (fcntl(serverSocketFd, F_SETFL, flags) != 0) { - close(serverSocketFd); - serverSocketFd = -1; - ERROR("Could not set socket flags"); - return false; - } - - return true; -} - -bool UsbIpServer::StartServer() { - serverThread = new std::thread(&UsbIpServer::ServerWorker, this); - if (serverThread == NULL) { - return false; - } - - while (!serverWorkerActive) { - milliSleep(100); - } - - return true; -} - -bool UsbIpServer::ConnectedClients() { - return (activeClients > 0); -} - -void UsbIpServer::StopServer() { - if (serverWorkerActive) { - killServerWorker = true; - int oldActioveClients = -1; - while ((serverWorkerActive) || (activeClients > 0)) { - if (activeClients != oldActioveClients) { - INFO("Active socket clients %d", activeClients); - oldActioveClients = activeClients; - } - milliSleep(500); - } - serverThread->join(); - delete serverThread; - serverThread = NULL; - } else { - ERROR("Cannot stop inactive server..."); - } -} - -void UsbIpServer::ServerWorker() { - serverWorkerActive = true; - while (!killServerWorker) { - struct sockaddr_in clientAddress; - socklen_t clientAddressLength = sizeof(clientAddress); - int clientSocketFd = accept(serverSocketFd, (struct sockaddr *)&clientAddress, &clientAddressLength); - - if (clientSocketFd < 0) { - milliSleep(500); - continue; - } - - INFO("Incoming socket connection"); - if (!StartConnectionThread(clientSocketFd)) { - close(clientSocketFd); - } - } - serverWorkerActive = false; -} - -bool UsbIpServer::StartConnectionThread(int clientSocketFd) { - std::thread *connectionThread = new std::thread(&UsbIpServer::ConnectionWorker, this, clientSocketFd); - if (connectionThread == NULL) { - return false; - } - - connectionThreads.push_back(connectionThread); - return true; -} - -unsigned int UsbIpServer::TcpRead(int clientSocketFd, uint8_t *buffer, unsigned int readSize) { - unsigned int readBytes = 0; - DEBUG("TCP Read %d", readSize); - do { - int rBytes = read(clientSocketFd, &buffer[readBytes], readSize - readBytes); - if (rBytes == 0) { - break; - } - readBytes += rBytes; - } while (readBytes < readSize); - - return readBytes; -} - -void UsbIpServer::ConnectionWorker(int clientSocketFd) { - uint8_t headerBuffer[USBIP_CMD_HEAD_SIZE]; - bool deviceImported = false; - - activeClients++; - INFO("Client connect"); - while (1) { - /* Read exactly 8 bytes */ - unsigned int headSize = USBIP_OP_HEAD_SIZE; - if (deviceImported) { - headSize = USBIP_CMD_HEAD_SIZE; - } - - unsigned int readBytes = TcpRead(clientSocketFd, headerBuffer, headSize); - if (readBytes == 0) { - break; - } else if (readBytes < headSize) { - ERROR("Received strange or no data over TCP/IP, closing connection"); - break; - } - - int txSize = 0; - uint8_t *txBuffer = NULL; - /* Start Protocol handler */ - if (deviceImported) { - txBuffer = UsbIpCommandHandler(clientSocketFd, headerBuffer, txSize); - } else { - uint16_t protocolVersion = GetUint(headerBuffer, 0, 2); - if (protocolVersion == USBIP_VERSION) { - /* manage command list/attach */ - unsigned int manageCmd = GetUint(headerBuffer, 2, 2); - switch (manageCmd) { - case 0x8005: - /* OP_REQ_DEVLIST */ - txBuffer = UsbIpReplyDeviceList(txSize); - break; - case 0x8003: - txBuffer = UsbIpReplyImport(clientSocketFd, txSize); - deviceImported = (txBuffer != NULL); - break; - default: - ERROR("Unknown command: %.4x", manageCmd); - break; - } - } else { - ERROR("Unknown version: %.4x", protocolVersion); - } - } - - /* Transmit Reply */ - if (txBuffer == NULL) { - ERROR("Nothing to transmit?, closing connection"); - break; - } - - /* Transmit data over TCP/IP socket */ - DEBUG("TCP Write %d", txSize); - int wSize = write(clientSocketFd, txBuffer, txSize); - delete[] txBuffer; - - /* OK TCP/IP transmission? */ - if (wSize != txSize) { - ERROR("Did not write full packet to socket, closing connection"); - break; - } - } - - /* Disconnect & Close socket */ - INFO("Device disconnected"); - usbIpDevice.Disconnect(); - INFO("Client socket closed"); - close(clientSocketFd); - activeClients--; -} - -uint8_t *UsbIpServer::UsbIpReplyDeviceList(int &txSize) { - int replySize = 12; - replySize += usbIpDevice.FillDeviceData(NULL, replySize, true); - - INFO("ReplySize = %d", replySize); - - uint8_t *txBuffer = new uint8_t[replySize]; - if (txBuffer != NULL) { - memset(txBuffer, 0, replySize); - - int pos = 0; - pos += SetUint(USBIP_VERSION, txBuffer, pos, 2); /* Version */ - pos += SetUint(0x0005, txBuffer, pos, 2); /* Reply */ - pos += SetUint(0, txBuffer, pos, 4); /* Status */ - pos += SetUint(1, txBuffer, pos, 4); /* N devices */ - pos += usbIpDevice.FillDeviceData(txBuffer, pos, true); - txSize = replySize; - } - return txBuffer; -} - -uint8_t *UsbIpServer::UsbIpReplyImport(int clientSocketFd, int &txSize) { - uint8_t busIdBuffer[USBIP_BUSID_STR_LENGTH]; - if (TcpRead(clientSocketFd, busIdBuffer, sizeof(busIdBuffer)) != sizeof(busIdBuffer)) { - return NULL; - } - - int replySize = 8; - replySize += usbIpDevice.FillDeviceData(NULL, replySize, false); - - uint8_t *txBuffer = new uint8_t[replySize]; - if (txBuffer != NULL) { - memset(txBuffer, 0, replySize); - - std::string busId((char *)busIdBuffer); - INFO("Attach Device: %s", busId.c_str()); - - int pos = 0; - pos += SetUint(USBIP_VERSION, txBuffer, pos, 2); /* Version */ - pos += SetUint(0x0003, txBuffer, pos, 2); /* Reply */ - pos += SetUint(0, txBuffer, pos, 4); /* Status */ - - pos += usbIpDevice.FillDeviceData(txBuffer, pos, false); - txSize = replySize; - } - return txBuffer; -} - -uint8_t *UsbIpServer::UsbIpCommandHandler(int clientSocketFd, uint8_t *cmdHeadBuffer, int &txSize) { - uint32_t command = GetUint(cmdHeadBuffer, 0, 4); - switch (command) { - case 0x00000001: - /* USBIP_CMD_SUBMIT */ - return UsbIpHandleURB(clientSocketFd, cmdHeadBuffer, txSize); - case 0x00000002: - /* USBIP_CMD_UNLINK */ - return UsbIpUnlinkURB(cmdHeadBuffer, txSize); - default: - ERROR("Unkown UsbIp command %.4x", command); - break; - } - return NULL; -} - -uint8_t *UsbIpServer::UsbIpUnlinkURB(uint8_t *cmdHeadBuffer, int &txSize) { - uint32_t sequenceNumber = GetUint(cmdHeadBuffer, 0x04, 4); /* 0x04 seqnum */ - uint32_t devId = GetUint(cmdHeadBuffer, 0x08, 4); /* 0x08 devid */ - uint32_t unlinkSeqNum = GetUint(cmdHeadBuffer, 0x14, 4); /* 0x14 unlink seqnum */ - DEBUG("Unlink URB %d:%d", sequenceNumber, unlinkSeqNum); - - uint8_t *txBuffer = new uint8_t[USBIP_CMD_HEAD_SIZE]; - if (txBuffer != NULL) { - memset(txBuffer, 0, USBIP_CMD_HEAD_SIZE); - SetUint(4, txBuffer, 0x00, 4); /* 0x00 reply */ - SetUint(sequenceNumber, txBuffer, 0x04, 4); /* 0x04 Seq */ - SetUint(devId, txBuffer, 0x08, 4); /* 0x08 devid */ - SetUint(0, txBuffer, 0x0c, 4); /* 0x0c direction */ - SetUint(0, txBuffer, 0x10, 4); /* 0x10 EP */ - SetUint(0, txBuffer, 0x14, 4); /* 0x14 status */ - txSize = USBIP_CMD_HEAD_SIZE; - } - return txBuffer; -} - -uint8_t *UsbIpServer::UsbIpHandleURB(int clientSocketFd, uint8_t *cmdHeadBuffer, int &txSize) { - uint32_t sequenceNumber = GetUint(cmdHeadBuffer, 0x04, 4); /* 0x04 seqnum */ - uint32_t devId = GetUint(cmdHeadBuffer, 0x08, 4); /* 0x08 devid */ - uint32_t direction = GetUint(cmdHeadBuffer, 0x0c, 4); /* 0x0c direction */ - uint32_t ep = GetUint(cmdHeadBuffer, 0x10, 4); /* 0x10 ep */ - uint32_t transferFlags = GetUint(cmdHeadBuffer, 0x14, 4); /* 0x14 transfer_flags */ - uint32_t transferLength = GetUint(cmdHeadBuffer, 0x18, 4); /* 0x18 transfer_length */ - // uint32_t startFrame = GetUint(cmdHeadBuffer, 0x1c, 4); /* 0x1c - // start_frame */ - uint32_t isoPackets = GetUint(cmdHeadBuffer, 0x20, 4); /* 0x20 number_of_packets */ - uint32_t interval = GetUint(cmdHeadBuffer, 0x24, 4); /* 0x24 intervale */ - - DEBUG(" URB: DevId %.8x, Flags %.4x", devId, transferFlags); - DEBUG(" URB: EP%d, transfer_length %d", ep, transferLength); - DEBUG(" URB: ISO Packets %d, interval %d", isoPackets, interval); - DEBUG_VECTOR("USB Setup", &cmdHeadBuffer[0x28], 8); - - /* Dynamic create rxData in case it is needed */ - uint8_t usbSetup[8]; - memcpy(usbSetup, &cmdHeadBuffer[0x28], sizeof(usbSetup)); - - int ioEp = ep; - uint8_t *rxData = (uint8_t *)malloc(transferLength); - - if (rxData == NULL) { - return NULL; - } - - /* Read incoming data if available */ - if (direction == 0) { - if (TcpRead(clientSocketFd, rxData, transferLength) != transferLength) { - free(rxData); - return NULL; - } - } else { - if (ioEp > 0) { - ioEp = ioEp | 0x80; - } - } - - uint8_t *txBuffer = new uint8_t[USBIP_CMD_HEAD_SIZE + USBIP_MAX_PACKET_SIZE]; - uint8_t *usbReply = &txBuffer[USBIP_CMD_HEAD_SIZE]; - int usbIpStatus = 0; - unsigned int actualLength = 0; - if (txBuffer != NULL) { - memset(txBuffer, 0, USBIP_CMD_HEAD_SIZE + USBIP_MAX_PACKET_SIZE); - int status = usbIpDevice.TxRx(ioEp, usbSetup, rxData, usbReply, transferLength); - if (status < 0) { - switch (status) { - case EP_STALL: - usbIpStatus = EP_STALL; - DEBUG("USB Stall"); - break; - default: - ERROR("USB Error %d", status); - break; - } - } else { - actualLength = status; - } - - if (actualLength > transferLength) { - actualLength = transferLength; - DEBUG("Trunc package %d", actualLength); - } - DEBUG_VECTOR("USB Reply", usbReply, actualLength); - - SetUint(3, txBuffer, 0x00, 4); /* 0x00 reply */ - SetUint(sequenceNumber, txBuffer, 0x04, 4); /* 0x04 Seq */ - SetUint(devId, txBuffer, 0x08, 4); /* 0x08 devid */ - SetUint(0, txBuffer, 0x0c, 4); /* 0x0c direction */ - SetUint(ep, txBuffer, 0x10, 4); /* 0x10 EP */ - SetUint(usbIpStatus, txBuffer, 0x14, 4); /* 0x14 status */ - SetUint(actualLength, txBuffer, 0x18, 4); /* 0x18 actual_length */ - SetUint(0, txBuffer, 0x1c, 4); /* 0x1c start_frame */ - SetUint(0, txBuffer, 0x20, 4); /* 0x20 number_of_packets */ - SetUint(0, txBuffer, 0x24, 4); /* 0x24 error_count */ - - if (direction == 0) { - txSize = USBIP_CMD_HEAD_SIZE; - } else { - txSize = USBIP_CMD_HEAD_SIZE + actualLength; - } - } - - free(rxData); - return txBuffer; -} diff --git a/cpp/src/server/UsbIpServer.h b/cpp/src/server/UsbIpServer.h deleted file mode 100644 index 4eac560..0000000 --- a/cpp/src/server/UsbIpServer.h +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USB_IP_DRIVER_H -#define USB_IP_DRIVER_H - -#include -#include -#include -#include - -#include "UsbDevice.h" -#include "UsbIpDevice.h" - -using namespace std; - -class UsbIpServer { - public: - UsbIpServer(int tcpPort = 3240); - ~UsbIpServer(); - void AddDevice(UsbDevice *dev, string path, string busId, int busNum, int devNum, enum usb_device_speed speed); - bool Init(); - bool StartServer(); - bool ConnectedClients(); - void StopServer(); - void ServerWorker(); - bool StartConnectionThread(int clientSocketFd); - void ConnectionWorker(int clientSocketFd); - - private: - unsigned int TcpRead(int clientSocketFd, uint8_t *buffer, unsigned int readSize); - - uint8_t *UsbIpReplyDeviceList(int &txSize); - uint8_t *UsbIpReplyImport(int clientSocketFd, int &txSize); - uint8_t *UsbIpCommandHandler(int clientSocketFd, uint8_t *cmdHeadBuffer, int &txSize); - uint8_t *UsbIpUnlinkURB(uint8_t *cmdHeadBuffer, int &txSize); - uint8_t *UsbIpHandleURB(int clientSocketFd, uint8_t *cmdHeadBuffer, int &txSize); - - std::thread *serverThread; - std::vector connectionThreads; - - int serverSocketFd; - bool serverWorkerActive; - bool killServerWorker; - int activeClients; - int tcpPort; - - UsbIpDevice usbIpDevice; -}; - -#endif // USB_IP_DRIVER_H diff --git a/cpp/src/usb/UsbConfiguration.cc b/cpp/src/usb/UsbConfiguration.cc deleted file mode 100644 index 15e6466..0000000 --- a/cpp/src/usb/UsbConfiguration.cc +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include -#include "UsbDevice.h" -#include "UsbConfiguration.h" -#include "UsbUtil.h" - -using namespace UsbUtil; - -UsbConfiguration::UsbConfiguration(uint8_t bConfigurationValue, uint8_t bNumInterfaces, UsbInterface **interfaceArray, uint8_t iConfiguration, - uint8_t bmAttributes, uint8_t bMaxPower) { - this->bNumInterfaces = bNumInterfaces; - this->bConfigurationValue = bConfigurationValue; - this->iConfiguration = iConfiguration; - this->bmAttributes = bmAttributes; - this->bMaxPower = bMaxPower; - this->interfaceArray = interfaceArray; -} - -int UsbConfiguration::GenerateConfigurationDescriptor(uint8_t *buffer, int offset) { - int pos = offset; - - pos += SetUint(9, buffer, pos, 1); - pos += SetUint(2, buffer, pos, 1); - pos += SetUint(0, buffer, pos, 2); /* total size will be filed later */ - pos += SetUint(bNumInterfaces, buffer, pos, 1); - pos += SetUint(bConfigurationValue, buffer, pos, 1); - pos += SetUint(iConfiguration, buffer, pos, 1); - pos += SetUint(bmAttributes, buffer, pos, 1); - pos += SetUint(bMaxPower, buffer, pos, 1); - - for (int idx = 0; idx < bNumInterfaces; idx++) { - pos += interfaceArray[idx]->GenerateConfigurationDescriptor(buffer, offset + pos); - } - - /* Set total size */ - SetUint(pos, buffer, 2, 2); - return pos - offset; -} - -UsbEndpoint *UsbConfiguration::GetEndpoint(uint8_t endpointAddress) { - for (int idx = 0; idx < bNumInterfaces; idx++) { - UsbEndpoint *endpoint = interfaceArray[idx]->GetEndpoint(endpointAddress); - if (endpoint != NULL) { - return endpoint; - } - } - return NULL; -} diff --git a/cpp/src/usb/UsbConfiguration.h b/cpp/src/usb/UsbConfiguration.h deleted file mode 100644 index 95b239c..0000000 --- a/cpp/src/usb/UsbConfiguration.h +++ /dev/null @@ -1,38 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USB_CONFIGURATION_H -#define USB_CONFIGURATION_H - -#include -#include "UsbInterface.h" -#include "UsbEndpoint.h" - -class UsbConfiguration { - public: - UsbConfiguration(uint8_t bConfigurationValue, uint8_t bNumInterfaces, UsbInterface **interfaceArray, uint8_t iConfiguration = 0, - uint8_t bmAttributes = 0xc0, uint8_t bMaxPower = 100); - - int GenerateConfigurationDescriptor(uint8_t *buffer, int offset); - UsbEndpoint *GetEndpoint(uint8_t endpointAddress); - - uint8_t bNumInterfaces; - uint8_t bConfigurationValue; - uint8_t iConfiguration; - uint8_t bmAttributes; - uint8_t bMaxPower; - UsbInterface **interfaceArray; -}; - -#endif // USB_CONFIGURATION_H diff --git a/cpp/src/usb/UsbDevice.cc b/cpp/src/usb/UsbDevice.cc deleted file mode 100644 index 28cf871..0000000 --- a/cpp/src/usb/UsbDevice.cc +++ /dev/null @@ -1,202 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include "UsbDevice.h" -#include "UsbInterface.h" -#include "UsbUtil.h" -#include "Verbose.h" - -#define USB_VERSION 0x0110 - -using namespace Verbose; -using namespace UsbUtil; - -UsbDevice::UsbDevice(uint16_t idVendor, uint16_t idProduct, uint16_t bcdDevice, uint8_t bDeviceClass, uint8_t bDeviceSubClass, - uint8_t bDeviceProtocol, uint8_t bNumConfigurations, UsbConfiguration **configurationArray, UsbString *usbString, - uint8_t iManufacturer, uint8_t iProduct, uint8_t iSerialNumber) { - this->deviceConnected = false; - this->idVendor = idVendor; - this->idProduct = idProduct; - this->bcdDevice = bcdDevice; - this->bDeviceClass = bDeviceClass; - this->bDeviceSubClass = bDeviceSubClass; - this->bDeviceProtocol = bDeviceProtocol; - this->bNumConfigurations = bNumConfigurations; - this->configurationArray = configurationArray; - this->usbString = usbString; - this->iManufacturer = iManufacturer; - this->iProduct = iProduct; - this->iSerialNumber = iSerialNumber; -} - -int UsbDevice::TxRx(uint8_t endpoint, uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - if (endpoint == 0) { - /* Control */ - uint8_t bmRequestType = usbSetup[0]; - int bfRecipient = bmRequestType & 0x05; - - switch (bfRecipient) { - case 0: - return DeviceRequest(usbSetup, dataIn, dataOut, transferLength); - case 1: - return InterfaceRequest(usbSetup, dataIn, dataOut, transferLength); - default: - DEBUG("Unknown bfRecipient %d", bfRecipient); - } - return EP_STALL; - } - - if (dataOut == NULL) { - return transferLength; - } - - DEBUG("Get Endpoint %.2x", endpoint); - return configurationArray[0]->GetEndpoint(endpoint)->Data(dataIn, dataOut, transferLength); -} - -void UsbDevice::Disconnect() { - deviceConnected = false; -} - -int UsbDevice::DeviceRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - uint8_t bmRequestType = usbSetup[0]; - int bfDataDirection = bmRequestType & 0x80; - if (bfDataDirection) { - /* OutRequest */ - return OutRequest(usbSetup, dataIn, dataOut, transferLength); - } else { - /* InRequest */ - return InRequest(usbSetup, dataIn, dataOut, transferLength); - } -} - -int UsbDevice::OutRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - uint8_t bRequest = usbSetup[1]; - DEBUG("UsbDevice: OutRequest %.2x", bRequest); - - int packetSize = 0; - - if (bRequest == 0x00) { - SetUint(0x0001, dataOut, 0, 2); - packetSize = 2; - } else if (bRequest == 0x06) { - packetSize = GetDescriptor(usbSetup, dataIn, dataOut, transferLength); - } else { - ERROR("Unknown bRequest: %.2x", bRequest); - } - - return packetSize; -} - -int UsbDevice::GetDeviceDescriptor(uint8_t *buffer, int transferLength) { - int pos = 0; - pos += SetUint(18, buffer, pos, 1); - pos += SetUint(1, buffer, pos, 1); - pos += SetUint(USB_VERSION, buffer, pos, 2); - pos += SetUint(bDeviceClass, buffer, pos, 1); - pos += SetUint(bDeviceSubClass, buffer, pos, 1); - pos += SetUint(bDeviceProtocol, buffer, pos, 1); - if (transferLength < 64) { - pos += SetUint(transferLength, buffer, pos, 1); - } else { - pos += SetUint(64, buffer, pos, 1); - } - pos += SetUint(idVendor, buffer, pos, 2); - pos += SetUint(idProduct, buffer, pos, 2); - pos += SetUint(bcdDevice, buffer, pos, 2); - pos += SetUint(iManufacturer, buffer, pos, 1); - pos += SetUint(iProduct, buffer, pos, 1); - pos += SetUint(iSerialNumber, buffer, pos, 1); - pos += SetUint(bNumConfigurations, buffer, pos, 1); - - return pos; -} - -int UsbDevice::GetDeviceQualifier(uint8_t *buffer) { - int pos = 0; - pos += SetUint(10, buffer, pos, 1); - pos += SetUint(6, buffer, pos, 1); - pos += SetUint(USB_VERSION, buffer, pos, 2); - pos += SetUint(bDeviceClass, buffer, pos, 1); - pos += SetUint(bDeviceSubClass, buffer, pos, 1); - pos += SetUint(bDeviceProtocol, buffer, pos, 1); - pos += SetUint(64, buffer, pos, 1); - pos += SetUint(0, buffer, pos, 1); - pos += SetUint(0, buffer, pos, 1); - return pos; -} -int UsbDevice::GetDescriptor(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - (void)dataIn; - uint8_t bDescriptorType = usbSetup[3]; - uint8_t bDescriptorIndex = usbSetup[2]; - - switch (bDescriptorType) { - case 0x01: - return GetDeviceDescriptor(dataOut, transferLength); - case 0x02: - /* ConfigurationDescriptor */ - // FIXME Handle EP size better!!! - // [ 5787.496570] usb 8-1: Using ep0 maxpacket: 8 - // [ 5787.497786] usb 8-1: config 1 interface 0 altsetting 0 bulk - // endpoint 0x81 has invalid maxpacket 64 [ 5787.497792] usb 8-1: config - // 1 interface 1 altsetting 0 bulk endpoint 0x82 has invalid maxpacket - // 64 - if (bDescriptorIndex < bNumConfigurations) { - return configurationArray[bDescriptorIndex]->GenerateConfigurationDescriptor(dataOut, 0); - } - return EP_STALL; - case 0x03: - /* String Descriptor */ - if (usbString != NULL) { - return usbString->GetStringDescriptor(bDescriptorIndex, dataOut, 0); - } - return EP_STALL; - case 0x06: - return GetDeviceQualifier(dataOut); - default: - ERROR("Unknown bDescriptorType:bDescriptorIndexbRequest %.2x:%.2x", bDescriptorType, bDescriptorIndex); - break; - } - return EP_STALL; -} - -int UsbDevice::InRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - (void)dataIn; - (void)dataOut; - (void)transferLength; - - uint8_t bRequest = usbSetup[1]; - DEBUG("UsbDevice: InRequest %.2x", bRequest); - switch (bRequest) { - case 0x09: { - /* Set Configuration */ - int configrationValue = GetUint(usbSetup, 2, 2); - DEBUG("UsbDevice: Set Configuration: %d", configrationValue); - DEBUG("Device Connected"); - deviceConnected = true; - return 0; - } - } - - return EP_STALL; -} - -int UsbDevice::InterfaceRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - uint8_t bInterfaceIndex = usbSetup[2]; - DEBUG("UsbDevice: InterfaceRequest %.2x", bInterfaceIndex); - if (bInterfaceIndex < configurationArray[0]->bNumInterfaces) { - return configurationArray[0]->interfaceArray[bInterfaceIndex]->InterfaceRequest(usbSetup, dataIn, dataOut, transferLength); - } - return EP_STALL; -} diff --git a/cpp/src/usb/UsbDevice.h b/cpp/src/usb/UsbDevice.h deleted file mode 100644 index e21cd9b..0000000 --- a/cpp/src/usb/UsbDevice.h +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USB_DEVICE_H -#define USB_DEVICE_H - -#include -#include -#include "UsbConfiguration.h" -#include "UsbString.h" - -#define EP_STALL (-32) - -class UsbDevice { - public: - UsbDevice(uint16_t idVendor, uint16_t idProduct, uint16_t bcdDevice, uint8_t bDeviceClass, uint8_t bDeviceSubClass, uint8_t bDeviceProtocol, - uint8_t bNumConfigurations, UsbConfiguration **configurationArray, UsbString *usbString = NULL, uint8_t iManufacturer = 0, - uint8_t iProduct = 0, uint8_t iSerialNumber = 0); - - int TxRx(uint8_t endpoint, uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength); - int DeviceRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength); - int InterfaceRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength); - - int OutRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength); - int InRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength); - - int GetDescriptor(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength); - int GetDeviceDescriptor(uint8_t *buffer, int transferLength); - int GetDeviceQualifier(uint8_t *buffer); - - void Disconnect(); - bool IsConnected() { - return deviceConnected; - }; - - bool deviceConnected; - uint16_t idVendor; - uint16_t idProduct; - uint16_t bcdDevice; - uint8_t bDeviceClass; - uint8_t bDeviceSubClass; - uint8_t bDeviceProtocol; - uint8_t bConfigurationValue; - uint8_t bNumConfigurations; - UsbConfiguration **configurationArray; - UsbString *usbString; - uint8_t iManufacturer; - uint8_t iProduct; - uint8_t iSerialNumber; -}; - -#endif // USB_DEVICE_H diff --git a/cpp/src/usb/UsbEndpoint.cc b/cpp/src/usb/UsbEndpoint.cc deleted file mode 100644 index a03316a..0000000 --- a/cpp/src/usb/UsbEndpoint.cc +++ /dev/null @@ -1,38 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include "UsbEndpoint.h" -#include "UsbUtil.h" - -using namespace UsbUtil; - -UsbEndpoint::UsbEndpoint(uint8_t bEndpointAddress, uint8_t bmAttributes, uint16_t wMaxPacketSize, uint8_t bInterval) { - this->bEndpointAddress = bEndpointAddress; - this->bmAttributes = bmAttributes; - this->wMaxPacketSize = wMaxPacketSize; - this->bInterval = bInterval; -} - -int UsbEndpoint::GenerateConfigurationDescriptor(uint8_t *buffer, int offset) { - int pos = offset; - - pos += SetUint(7, buffer, pos, 1); - pos += SetUint(5, buffer, pos, 1); - pos += SetUint(bEndpointAddress, buffer, pos, 1); - pos += SetUint(bmAttributes, buffer, pos, 1); - pos += SetUint(wMaxPacketSize, buffer, pos, 2); - pos += SetUint(bInterval, buffer, pos, 1); - - return pos - offset; -} diff --git a/cpp/src/usb/UsbEndpoint.h b/cpp/src/usb/UsbEndpoint.h deleted file mode 100644 index 8f5fd8c..0000000 --- a/cpp/src/usb/UsbEndpoint.h +++ /dev/null @@ -1,32 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USB_ENDPOINT_H -#define USB_ENDPOINT_H - -#include - -class UsbEndpoint { - public: - UsbEndpoint(uint8_t bEndpointAddress, uint8_t bmAttributes, uint16_t wMaxPacketSize, uint8_t bInterval); - int GenerateConfigurationDescriptor(uint8_t *buffer, int offset); - virtual int Data(uint8_t *dataIn, uint8_t *dataOut, int transferLength) = 0; - - uint8_t bEndpointAddress; - uint8_t bmAttributes; - uint16_t wMaxPacketSize; - uint8_t bInterval; -}; - -#endif // USB_ENDPOINT_H diff --git a/cpp/src/usb/UsbInterface.cc b/cpp/src/usb/UsbInterface.cc deleted file mode 100644 index f5f6c24..0000000 --- a/cpp/src/usb/UsbInterface.cc +++ /dev/null @@ -1,118 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include "UsbDevice.h" -#include "UsbEndpoint.h" -#include "UsbInterface.h" -#include "UsbUtil.h" - -using namespace UsbUtil; - -#include "Verbose.h" -using namespace Verbose; - -UsbInterface::UsbInterface(uint8_t bInterfaceNumber, uint8_t bInterfaceClass, uint8_t bInterfaceSubClass, uint8_t bInterfaceProtocol, - uint8_t bNumEndpoints, UsbEndpoint **endpointArray, uint8_t iInterface, uint8_t bAlternateSetting) { - this->bInterfaceNumber = bInterfaceNumber; - this->bAlternateSetting = bAlternateSetting; - this->bNumEndpoints = bNumEndpoints; - this->bInterfaceClass = bInterfaceClass; - this->bInterfaceSubClass = bInterfaceSubClass; - this->bInterfaceProtocol = bInterfaceProtocol; - this->iInterface = iInterface; - this->endpointArray = endpointArray; -} - -int UsbInterface::GenerateInterfaceDescriptor(uint8_t *buffer, int offset) { - int pos = offset; - - pos += SetUint(9, buffer, pos, 1); - pos += SetUint(4, buffer, pos, 1); - pos += SetUint(bInterfaceNumber, buffer, pos, 1); - pos += SetUint(bAlternateSetting, buffer, pos, 1); - pos += SetUint(bNumEndpoints, buffer, pos, 1); - pos += SetUint(bInterfaceClass, buffer, pos, 1); - pos += SetUint(bInterfaceSubClass, buffer, pos, 1); - pos += SetUint(bInterfaceProtocol, buffer, pos, 1); - pos += SetUint(iInterface, buffer, pos, 1); - - return pos - offset; -} - -int UsbInterface::GenerateConfigurationDescriptor(uint8_t *buffer, int offset) { - int pos = offset; - pos += GenerateInterfaceDescriptor(buffer, pos); - - for (int idx = 0; idx < bNumEndpoints; idx++) { - pos += endpointArray[idx]->GenerateConfigurationDescriptor(buffer, pos); - } - return pos - offset; -} - -int UsbInterface::InterfaceRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - uint8_t bmRequestType = usbSetup[0]; - int bfDataDirection = bmRequestType & 0x80; - if (bfDataDirection) { - /* OutRequest */ - return OutRequest(usbSetup, dataIn, dataOut, transferLength); - } else { - /* InRequest */ - return InRequest(usbSetup, dataIn, dataOut, transferLength); - } - - return EP_STALL; -} - -int UsbInterface::OutRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - uint8_t bRequest = usbSetup[1]; - DEBUG("UsbInterface: OutRequest %.2x", bRequest); - - if (bRequest == 0x06) { - return GetDescriptor(usbSetup, dataIn, dataOut, transferLength); - } - return EP_STALL; -} - -int UsbInterface::GetDescriptor(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - (void)usbSetup; - (void)dataIn; - (void)dataOut; - (void)transferLength; - ERROR("Unkown GetDescriptor UsbInterface"); - return EP_STALL; -} - -int UsbInterface::InRequest(uint8_t *usbSetup, uint8_t *dataIn, uint8_t *dataOut, int transferLength) { - (void)dataIn; - (void)dataOut; - (void)transferLength; - - uint8_t bRequest = usbSetup[1]; - if (bRequest == 0x0a) { - /* stall this request */ - return EP_STALL; - } - - ERROR_VECTOR("Unkown InRequest UsbInterface", usbSetup, 8); - return EP_STALL; -} - -UsbEndpoint *UsbInterface::GetEndpoint(uint8_t endpointAddress) { - for (int idx = 0; idx < bNumEndpoints; idx++) { - if ((endpointArray[idx]->bEndpointAddress) == endpointAddress) { - return endpointArray[idx]; - } - } - return NULL; -} diff --git a/cpp/src/usb/UsbInterface.h b/cpp/src/usb/UsbInterface.h deleted file mode 100644 index 1b64c0d..0000000 --- a/cpp/src/usb/UsbInterface.h +++ /dev/null @@ -1,44 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USB_INTERFACE_H -#define USB_INTERFACE_H - -#include -#include "UsbEndpoint.h" - -class UsbInterface { - public: - UsbInterface(uint8_t bInterfaceNumber, uint8_t bInterfaceClass, uint8_t bInterfaceSubClass, uint8_t bInterfaceProtocol, uint8_t bNumEndpoints, - UsbEndpoint **endpointArray, uint8_t iInterface = 0, uint8_t bAlternateSetting = 0); - - int GenerateConfigurationDescriptor(uint8_t *buffer, int offset); - virtual int GenerateInterfaceDescriptor(uint8_t *buffer, int offset); - virtual int InterfaceRequest(uint8_t *usbSetup, uint8_t *data, uint8_t *replyBuffer, int transferLength); - virtual int OutRequest(uint8_t *usbSetup, uint8_t *data, uint8_t *replyBuffer, int transferLength); - virtual int GetDescriptor(uint8_t *usbSetup, uint8_t *data, uint8_t *replyBuffer, int transferLength); - virtual int InRequest(uint8_t *usbSetup, uint8_t *data, uint8_t *replyBuffer, int transferLength); - UsbEndpoint *GetEndpoint(uint8_t endpointAddress); - - uint8_t bInterfaceNumber; - uint8_t bAlternateSetting; - uint8_t bNumEndpoints; - uint8_t bInterfaceClass; - uint8_t bInterfaceSubClass; - uint8_t bInterfaceProtocol; - uint8_t iInterface; - UsbEndpoint **endpointArray; -}; - -#endif // USB_INTERFACE_H diff --git a/cpp/src/usb/UsbString.cc b/cpp/src/usb/UsbString.cc deleted file mode 100644 index 37dca4d..0000000 --- a/cpp/src/usb/UsbString.cc +++ /dev/null @@ -1,46 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include "UsbString.h" -#include "UsbDevice.h" -#include "UsbUtil.h" - -using namespace UsbUtil; - -int UsbString::GetStringDescriptor(uint8_t bDescriptorIndex, uint8_t *buffer, int offset) { - int pos = offset; - if (bDescriptorIndex == 0) { - /* Reply with language string */ - pos += SetUint(4, buffer, pos, 1); - pos += SetUint(3, buffer, pos, 1); - pos += SetUint(0x0409, buffer, pos, 2); - return pos; - } else if (bDescriptorIndex <= bNumStrings) { - /* Initialize length to 0 */ - pos += SetUint(0, buffer, pos, 1); - pos += SetUint(3, buffer, pos, 1); - /* Copy ASCII string to UniCode string */ - uint8_t *stringPtr = (uint8_t *)stringArray[bDescriptorIndex - 1]; - do { - int c = *stringPtr++; - pos += SetUint(c, buffer, pos, 2); - } while (*stringPtr != 0); - - /* Set Length */ - SetUint(pos, buffer, 0, 1); - return pos; - } - - return EP_STALL; -} diff --git a/cpp/src/usb/UsbString.h b/cpp/src/usb/UsbString.h deleted file mode 100644 index a5d85c7..0000000 --- a/cpp/src/usb/UsbString.h +++ /dev/null @@ -1,31 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USB_STRING_H -#define USB_STRING_H - -#include - -class UsbString { - public: - UsbString(uint8_t bNumStrings, const char **stringArray) { - this->bNumStrings = bNumStrings; - this->stringArray = stringArray; - } - int GetStringDescriptor(uint8_t bDescriptorIndex, uint8_t *buffer, int offset); - uint8_t bNumStrings; - const char **stringArray; -}; - -#endif // USB_STRING_H diff --git a/cpp/src/util/NetworkUtil.cc b/cpp/src/util/NetworkUtil.cc deleted file mode 100644 index 39b753e..0000000 --- a/cpp/src/util/NetworkUtil.cc +++ /dev/null @@ -1,43 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include -#include "NetworkUtil.h" - -namespace NetworkUtil { - unsigned int GetUint(uint8_t *buffer, int offset, int byteWidth) { - unsigned int res = 0; - for (int idx = 0; idx < byteWidth; idx++) { - res = res << 8; - res = res | buffer[offset + idx]; - } - return res; - }; - - int SetUint(unsigned int value, uint8_t *buffer, int offset, int byteWidth) { - if (buffer != NULL) { - for (int idx = byteWidth - 1; idx >= 0; idx--) { - buffer[offset + idx] = value >> ((byteWidth - idx - 1) * 8); - } - } - return byteWidth; - }; - - int AddData(uint8_t *data, uint8_t *buffer, int offset, int len) { - if (buffer != NULL) { - memcpy(&buffer[offset], data, len); - } - return len; - } -}; // namespace NetworkUtil diff --git a/cpp/src/util/NetworkUtil.h b/cpp/src/util/NetworkUtil.h deleted file mode 100644 index c73b753..0000000 --- a/cpp/src/util/NetworkUtil.h +++ /dev/null @@ -1,26 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef NETWORK_UTIL_H -#define NETWORK_UTIL_H - -#include - -namespace NetworkUtil { - unsigned int GetUint(uint8_t *buffer, int offset, int byteWidth); - int SetUint(unsigned int value, uint8_t *buffer, int offset, int byteWidth); - int AddData(uint8_t *data, uint8_t *buffer, int offset, int len); -}; // namespace NetworkUtil - -#endif // NETWORK_UTIL_H diff --git a/cpp/src/util/UsbUtil.cc b/cpp/src/util/UsbUtil.cc deleted file mode 100644 index a1cbb85..0000000 --- a/cpp/src/util/UsbUtil.cc +++ /dev/null @@ -1,39 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#include -#include "UsbUtil.h" - -namespace UsbUtil { - unsigned int GetUint(uint8_t *buffer, int offset, int byteWidth) { - unsigned int res = 0; - for (int idx = 0; idx < byteWidth; idx++) { - unsigned int val = buffer[offset + idx]; - res = res >> 8; - res = res | (val << (8 * (byteWidth - 1))); - } - return res; - }; - - int SetUint(unsigned int value, uint8_t *buffer, int offset, int byteWidth) { - if (buffer != NULL) { - int val = value; - for (int idx = 0; idx < byteWidth; idx++) { - buffer[offset + idx] = val & 0xff; - val = val >> 8; - } - } - return byteWidth; - } -} // namespace UsbUtil diff --git a/cpp/src/util/UsbUtil.h b/cpp/src/util/UsbUtil.h deleted file mode 100644 index 2be08e1..0000000 --- a/cpp/src/util/UsbUtil.h +++ /dev/null @@ -1,25 +0,0 @@ -/******************************************************* - usbip-server - a platform for USB device prototyping - - Fredrik Andersson - Copyright 2016, All Rights Reserved. - - This software may be used by anyone for any reason so - long as the copyright notice in the source files - remains intact. - - code repository located at: - http://github.com/freand76/usbip-server -********************************************************/ - -#ifndef USB_UTIL_H -#define USB_UTIL_H - -#include - -namespace UsbUtil { - unsigned int GetUint(uint8_t *buffer, int offset, int byteWidth); - int SetUint(unsigned int value, uint8_t *buffer, int offset, int byteWidth); -}; // namespace UsbUtil - -#endif // USB_UTIL_H