From 7134348789c4425da6cc18d2f2708d7b3b3a062d Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 11:53:44 +0200 Subject: [PATCH 01/13] Added X_LINK_USB_EP define. --- include/XLink/XLinkPublicDefines.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/XLink/XLinkPublicDefines.h b/include/XLink/XLinkPublicDefines.h index 69545c1..f665813 100644 --- a/include/XLink/XLinkPublicDefines.h +++ b/include/XLink/XLinkPublicDefines.h @@ -62,6 +62,7 @@ typedef enum{ X_LINK_PCIE, X_LINK_IPC, X_LINK_TCP_IP, + X_LINK_USB_EP, X_LINK_NMB_OF_PROTOCOLS, X_LINK_ANY_PROTOCOL } XLinkProtocol_t; From 7cce347f6d65cd33f4209da55ddc25c44dddc489 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 11:54:07 +0200 Subject: [PATCH 02/13] Added reading and writing functions in PlatformData.c --- src/pc/PlatformData.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pc/PlatformData.c b/src/pc/PlatformData.c index 874c2ae..4e632e6 100644 --- a/src/pc/PlatformData.c +++ b/src/pc/PlatformData.c @@ -79,7 +79,7 @@ int XLinkPlatformWrite(xLinkDeviceHandle_t *deviceHandle, void *data, int size) if(!XLinkIsProtocolInitialized(deviceHandle->protocol)) { return X_LINK_PLATFORM_DRIVER_NOT_LOADED+deviceHandle->protocol; } - + switch (deviceHandle->protocol) { case X_LINK_USB_VSC: case X_LINK_USB_CDC: @@ -91,6 +91,9 @@ int XLinkPlatformWrite(xLinkDeviceHandle_t *deviceHandle, void *data, int size) case X_LINK_TCP_IP: return tcpipPlatformWrite(deviceHandle->xLinkFD, data, size); + case X_LINK_USB_EP: + return usbEpPlatformWrite(deviceHandle->xLinkFD, data, size); + default: return X_LINK_PLATFORM_INVALID_PARAMETERS; } @@ -113,6 +116,9 @@ int XLinkPlatformRead(xLinkDeviceHandle_t *deviceHandle, void *data, int size) case X_LINK_TCP_IP: return tcpipPlatformRead(deviceHandle->xLinkFD, data, size); + case X_LINK_USB_EP: + return usbEpPlatformRead(deviceHandle->xLinkFD, data, size); + default: return X_LINK_PLATFORM_INVALID_PARAMETERS; } From d0197a9412f81966acb77cb9a242a8bd413903bd Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 11:54:52 +0200 Subject: [PATCH 03/13] USB EP Protocol. --- src/pc/protocols/usb_host_ep.cpp | 194 +++++++++++++++++++++++++++++++ src/pc/protocols/usb_host_ep.h | 24 ++++ 2 files changed, 218 insertions(+) create mode 100644 src/pc/protocols/usb_host_ep.cpp create mode 100644 src/pc/protocols/usb_host_ep.h diff --git a/src/pc/protocols/usb_host_ep.cpp b/src/pc/protocols/usb_host_ep.cpp new file mode 100644 index 0000000..0174f93 --- /dev/null +++ b/src/pc/protocols/usb_host_ep.cpp @@ -0,0 +1,194 @@ +// project +#include +#define MVLOG_UNIT_NAME xLinkUsb + +#include "XLink/XLinkLog.h" +#include "XLink/XLinkPlatform.h" +#include "XLink/XLinkPublicDefines.h" +#include "usb_host_ep.h" +#include "../PlatformDeviceFd.h" + +#include +#include +#include + +#include + +#include + +/* Vendor ID */ +#define VENDOR_ID 0x03e7 + +/* Product ID */ +#define PRODUCT_ID 0x1234 + +/* Interface number for ffs.xlink */ +#define INTERFACE_XLINK 2 + +/* Base ndpoint address used for output */ +#define ENDPOINT_OUT_BASE 0x01 + +/* Base endpoint address used for input */ +#define ENDPOINT_IN_BASE 0x81 + +/* Transfer timeout */ +#define TIMEOUT 2000 + +static int usbFdRead, usbFdWrite; +static bool isServer; + +static libusb_context *ctx = NULL; +static libusb_device_handle *dev_handle = NULL; + +int usbEpInitialize() { + int error; + + /* Initialize libusb */ + libusb_init(&ctx); + + return 0; +} + +int usbEpPlatformConnect(const char *devPathRead, const char *devPathWrite, void **fd) +{ + int error; + isServer = false; + + /* Get our device */ + dev_handle = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID); + if (dev_handle == NULL) { + libusb_exit(ctx); + + error = LIBUSB_ERROR_NO_DEVICE; + return error; + } + + /* Not strictly necessary, but it is better to use it, + * as we're using kernel modules together with our interfaces + */ + error = libusb_set_auto_detach_kernel_driver(dev_handle, 1); + if (error != LIBUSB_SUCCESS) { + libusb_exit(ctx); + + return error; + } + + /* Now we claim our ffs interfaces */ + error = libusb_claim_interface(dev_handle, INTERFACE_XLINK); + if (error != LIBUSB_SUCCESS) { + libusb_exit(ctx); + + return error; + } + + /* We get the first EP_OUT and EP_IN for our interfaces + * In the way we initialized our usb-gadget on our device + * We know ncm is claiming the first 2 interfaces + */ + usbFdWrite = ENDPOINT_OUT_BASE + 1; /* +1 because NCM claims 1 OUT endpoint */ + usbFdRead = ENDPOINT_IN_BASE + 2; /* +2 because NCM claims 2 IN endpoints */ + + *fd = createPlatformDeviceFdKey((void*) (uintptr_t) usbFdRead); + + return 0; +} + +int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void **fd) +{ + isServer = true; + + char outPath[256]; + strcpy(outPath, devPathWrite); + strcat(outPath, "/ep1"); + + char inPath[256]; + strcpy(inPath, devPathWrite); + strcat(inPath, "/ep2"); + + int outfd = open(outPath, O_WRONLY); + int infd = open(inPath, O_RDONLY); + + if(outfd < 0 || infd < 0) { + return -1; + } + + usbFdRead = infd; + usbFdWrite = outfd; + + *fd = createPlatformDeviceFdKey((void*) (uintptr_t) usbFdRead); + + return 0; +} + + +int usbEpPlatformClose(void *fdKey) +{ + int error; + + if (isServer) { + if (usbFdRead != -1){ + close(usbFdRead); + usbFdRead = -1; + } + + if (usbFdWrite != -1){ + close(usbFdWrite); + usbFdWrite = -1; + } + } else { + error = libusb_release_interface(dev_handle, INTERFACE_XLINK); + if (error != LIBUSB_SUCCESS) { + libusb_exit(ctx); + + return error; + } + + /* Release the device and exit */ + libusb_close(dev_handle); + } + + libusb_exit(ctx); + + error = EXIT_SUCCESS; + + return EXIT_SUCCESS; +} + +int usbEpPlatformRead(void *fdKey, void *data, int size) +{ + int rc = 0; + + if (isServer) { + if(usbFdRead < 0) + { + return -1; + } + + rc = read(usbFdRead, data, size); + } else { + rc = libusb_bulk_transfer(dev_handle, usbFdRead, (unsigned char*)data, size, &rc, TIMEOUT); + } + + return rc; +} + +int usbEpPlatformWrite(void *fdKey, void *data, int size) +{ + int rc = 0; + + if (isServer) { + if(usbFdWrite < 0) + { + return -1; + } + + rc = write(usbFdWrite, data, size); + + } else { + rc = libusb_bulk_transfer(dev_handle, usbFdWrite, (unsigned char*)data, size, &rc, TIMEOUT); + } + + return rc; +} + + diff --git a/src/pc/protocols/usb_host_ep.h b/src/pc/protocols/usb_host_ep.h new file mode 100644 index 0000000..658819d --- /dev/null +++ b/src/pc/protocols/usb_host_ep.h @@ -0,0 +1,24 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "XLink/XLink.h" +#include "XLink/XLinkPlatform.h" + +int usbEpInitialize(); + +int usbEpPlatformConnect(const char *devPathRead, const char *devPathWrite, void **fd); +int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void **fd); +int usbEpPlatformClose(void *fd); + +int usbEpPlatformRead(void *fd, void *data, int size); +int usbEpPlatformWrite(void *fd, void *data, int size); + +#ifdef __cplusplus +} +#endif From 7f2f6fe303325c0cedf135a7b669280f573c1b3b Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 11:55:12 +0200 Subject: [PATCH 04/13] Added examples for USB EP. --- examples/CMakeLists.txt | 8 ++++- examples/xlink_usb_client.cpp | 54 ++++++++++++++++++++++++++++++ examples/xlink_usb_server.cpp | 62 +++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 examples/xlink_usb_client.cpp create mode 100644 examples/xlink_usb_server.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b645e7a..9f97c79 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -43,4 +43,10 @@ add_example(boot_firmware boot_firmware.cpp) add_example(xlink_server xlink_server.cpp) # Server example -add_example(xlink_server2 xlink_server2.cpp) \ No newline at end of file +add_example(xlink_server2 xlink_server2.cpp) + +# Server example +add_example(xlink_usb_server xlink_usb_server.cpp) + +# Client example +add_example(xlink_usb_client xlink_usb_client.cpp) diff --git a/examples/xlink_usb_client.cpp b/examples/xlink_usb_client.cpp new file mode 100644 index 0000000..35c6f1e --- /dev/null +++ b/examples/xlink_usb_client.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "XLink/XLink.h" +#include "XLink/XLinkPublicDefines.h" +#include "XLink/XLinkLog.h" + +// Common constants +const uint8_t DUMMY_DATA[1024*128] = {}; + +int main(int argc, char** argv) { + XLinkGlobalHandler_t gHandler; + XLinkInitialize(&gHandler); + + mvLogDefaultLevelSet(MVLOG_ERROR); + + deviceDesc_t deviceDesc; + strcpy(deviceDesc.name, "usbdev"); + deviceDesc.protocol = X_LINK_USB_EP; + + printf("Device name: %s\n", deviceDesc.name); + + XLinkHandler_t handler; + handler.devicePath = deviceDesc.name; + handler.protocol = deviceDesc.protocol; + auto connRet = XLinkConnect(&handler); + printf("Connection returned: %s\n", XLinkErrorToStr(connRet)); + if(connRet != X_LINK_SUCCESS) { + return -1; + } + + auto s = XLinkOpenStream(handler.linkId, "test_0", sizeof(DUMMY_DATA) * 2); + if(s == INVALID_STREAM_ID){ + printf("Open stream failed...\n"); + } else { + printf("Open stream OK - id: 0x%08X\n", s); + } + + auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s)); + assert(w == X_LINK_SUCCESS); + + return 0; +} diff --git a/examples/xlink_usb_server.cpp b/examples/xlink_usb_server.cpp new file mode 100644 index 0000000..16c277b --- /dev/null +++ b/examples/xlink_usb_server.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "XLink/XLink.h" +#include "XLink/XLinkPublicDefines.h" +#include "XLink/XLinkLog.h" + +// Common constants +constexpr static auto NUM_STREAMS = 16; +constexpr static auto NUM_PACKETS = 120; +const uint8_t DUMMY_DATA[1024*128] = {}; +XLinkGlobalHandler_t xlinkGlobalHandler = {}; + +// Server +// + +int main(int argc, const char** argv){ + + xlinkGlobalHandler.protocol = X_LINK_USB_EP; + + // Initialize and suppress XLink logs + mvLogDefaultLevelSet(MVLOG_ERROR); + auto status = XLinkInitialize(&xlinkGlobalHandler); + if(X_LINK_SUCCESS != status) { + throw std::runtime_error("Couldn't initialize XLink"); + } + + XLinkHandler_t handler; + handler.devicePath = "/dev/usb-ffs/xlink"; + handler.protocol = X_LINK_USB_EP; + XLinkServer(&handler, "eps", X_LINK_BOOTED, X_LINK_MYRIAD_X); + + + // loop through streams + auto s = XLinkOpenStream(0, "test_0", sizeof(DUMMY_DATA) * 2); + assert(s != INVALID_STREAM_ID); + + // auto w = XLinkWriteData2(s, (uint8_t*) &s, sizeof(s/2), ((uint8_t*) &s) + sizeof(s/2), sizeof(s) - sizeof(s/2)); + // assert(w == X_LINK_SUCCESS); + + auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s)); + assert(w == X_LINK_SUCCESS); + + + streamPacketDesc_t p; + w = XLinkReadMoveData(s, &p); + assert(w == X_LINK_SUCCESS); + XLinkDeallocateMoveData(p.data, p.length); + + return 0; +} From efb454dcec018a4a5a023df8b5f764ef10bc1f95 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 11:55:49 +0200 Subject: [PATCH 05/13] Added initialization, connecting and closing for USB EP. --- src/pc/PlatformDeviceControl.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/pc/PlatformDeviceControl.c b/src/pc/PlatformDeviceControl.c index 855edc9..a6fbc77 100644 --- a/src/pc/PlatformDeviceControl.c +++ b/src/pc/PlatformDeviceControl.c @@ -8,6 +8,7 @@ #include "XLinkPlatform.h" #include "XLinkPlatformErrorUtils.h" #include "usb_host.h" +#include "usb_host_ep.h" #include "pcie_host.h" #include "tcpip_host.h" #include "XLinkStringUtils.h" @@ -82,17 +83,22 @@ xLinkPlatformErrorCode_t XLinkPlatformInit(XLinkGlobalHandler_t* globalHandler) { // Set that all protocols are initialized at first for(int i = 0; i < X_LINK_NMB_OF_PROTOCOLS; i++) { - xlinkSetProtocolInitialized(i, 1); + xlinkSetProtocolInitialized(i, 1); } // check for failed initialization; LIBUSB_SUCCESS = 0 if (usbInitialize(globalHandler->options) != 0) { - xlinkSetProtocolInitialized(X_LINK_USB_VSC, 0); + xlinkSetProtocolInitialized(X_LINK_USB_VSC, 0); + } + + // Initialize the USB EPs if present + if(usbEpInitialize() != 0) { + xlinkSetProtocolInitialized(X_LINK_USB_EP, 0); } // Initialize tcpip protocol if necessary if(tcpip_initialize() != TCPIP_HOST_SUCCESS) { - xlinkSetProtocolInitialized(X_LINK_TCP_IP, 0); + xlinkSetProtocolInitialized(X_LINK_TCP_IP, 0); } return X_LINK_PLATFORM_SUCCESS; @@ -170,6 +176,7 @@ xLinkPlatformErrorCode_t XLinkPlatformConnect(const char* devPathRead, const cha if(!XLinkIsProtocolInitialized(protocol)) { return X_LINK_PLATFORM_DRIVER_NOT_LOADED+protocol; } + switch (protocol) { case X_LINK_USB_VSC: case X_LINK_USB_CDC: @@ -181,6 +188,9 @@ xLinkPlatformErrorCode_t XLinkPlatformConnect(const char* devPathRead, const cha case X_LINK_TCP_IP: return tcpipPlatformConnect(devPathRead, devPathWrite, fd); + case X_LINK_USB_EP: + return usbEpPlatformConnect(devPathRead, devPathWrite, fd); + default: return X_LINK_PLATFORM_INVALID_PARAMETERS; } @@ -192,6 +202,9 @@ xLinkPlatformErrorCode_t XLinkPlatformServer(const char* devPathRead, const char case X_LINK_TCP_IP: return tcpipPlatformServer(devPathRead, devPathWrite, fd); + case X_LINK_USB_EP: + return usbEpPlatformServer(devPathRead, devPathWrite, fd); + default: return X_LINK_PLATFORM_INVALID_PARAMETERS; } @@ -239,6 +252,9 @@ xLinkPlatformErrorCode_t XLinkPlatformCloseRemote(xLinkDeviceHandle_t* deviceHan case X_LINK_TCP_IP: return tcpipPlatformClose(deviceHandle->xLinkFD); + + case X_LINK_USB_EP: + return usbEpPlatformClose(deviceHandle->xLinkFD); default: return X_LINK_PLATFORM_INVALID_PARAMETERS; From 93d3f20484292a61f8baa6c1ff1cc271b4ecf2be Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 11:56:17 +0200 Subject: [PATCH 06/13] Added XLinkProtocolToStr for X_LINK_USB_EP --- src/shared/XLinkDevice.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/shared/XLinkDevice.c b/src/shared/XLinkDevice.c index 4f08964..6f1ecc9 100644 --- a/src/shared/XLinkDevice.c +++ b/src/shared/XLinkDevice.c @@ -212,6 +212,7 @@ XLinkError_t XLinkServerOnly(XLinkHandler_t* handler) link->peerState = XLINK_UP; link->hostClosedFD = 0; handler->linkId = link->id; + return X_LINK_SUCCESS; } @@ -686,6 +687,7 @@ const char* XLinkProtocolToStr(XLinkProtocol_t val) { case X_LINK_PCIE: return "X_LINK_PCIE"; case X_LINK_IPC: return "X_LINK_IPC"; case X_LINK_TCP_IP: return "X_LINK_TCP_IP"; + case X_LINK_USB_EP: return "X_LINK_USB_EP"; case X_LINK_NMB_OF_PROTOCOLS: return "X_LINK_NMB_OF_PROTOCOLS"; case X_LINK_ANY_PROTOCOL: return "X_LINK_ANY_PROTOCOL"; default: @@ -745,4 +747,4 @@ const char* XLinkPCIEBootloaderToStr(XLinkPCIEBootloader val) { // ------------------------------------ // Helpers implementation. End. -// ------------------------------------ \ No newline at end of file +// ------------------------------------ From 659c8e394d55a6289f20f8d0ebf271b2116e4f7f Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 15:05:10 +0200 Subject: [PATCH 07/13] Added missing header --- src/pc/PlatformData.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pc/PlatformData.c b/src/pc/PlatformData.c index 4e632e6..557d965 100644 --- a/src/pc/PlatformData.c +++ b/src/pc/PlatformData.c @@ -57,6 +57,8 @@ extern int usbFdWrite; extern int usbFdRead; #endif /*USE_USB_VSC*/ +#include "usb_host_ep.h" + // ------------------------------------ // Wrappers declaration. Begin. // ------------------------------------ From d2a709bcbdbc10fe888df8bd160f836f778d017e Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 15:16:06 +0200 Subject: [PATCH 08/13] Fixed issue of missing headers and the impossibility of using EPs on WIN32 --- src/pc/protocols/usb_host_ep.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/pc/protocols/usb_host_ep.cpp b/src/pc/protocols/usb_host_ep.cpp index 0174f93..a227203 100644 --- a/src/pc/protocols/usb_host_ep.cpp +++ b/src/pc/protocols/usb_host_ep.cpp @@ -8,9 +8,12 @@ #include "usb_host_ep.h" #include "../PlatformDeviceFd.h" +#if not defined(_WIN32) #include -#include #include +#endif + +#include #include @@ -95,6 +98,9 @@ int usbEpPlatformConnect(const char *devPathRead, const char *devPathWrite, void int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void **fd) { +#if defined(_WIN32) + return X_LINK_ERROR; +#else isServer = true; char outPath[256]; @@ -118,6 +124,7 @@ int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void *fd = createPlatformDeviceFdKey((void*) (uintptr_t) usbFdRead); return 0; +#endif } From 4427adedfa1c3e873b461b36a48ef8e212de9442 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 19 Jul 2023 15:22:16 +0200 Subject: [PATCH 09/13] Added checks for Win32 in all usb EP functions. --- src/pc/protocols/usb_host_ep.cpp | 71 ++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/src/pc/protocols/usb_host_ep.cpp b/src/pc/protocols/usb_host_ep.cpp index a227203..ffe58fd 100644 --- a/src/pc/protocols/usb_host_ep.cpp +++ b/src/pc/protocols/usb_host_ep.cpp @@ -133,25 +133,29 @@ int usbEpPlatformClose(void *fdKey) int error; if (isServer) { - if (usbFdRead != -1){ - close(usbFdRead); - usbFdRead = -1; - } - - if (usbFdWrite != -1){ - close(usbFdWrite); - usbFdWrite = -1; - } +#if defined(_WIN32) + return X_LINK_ERROR; +#else + if (usbFdRead != -1){ + close(usbFdRead); + usbFdRead = -1; + } + + if (usbFdWrite != -1){ + close(usbFdWrite); + usbFdWrite = -1; + } +#endif } else { - error = libusb_release_interface(dev_handle, INTERFACE_XLINK); - if (error != LIBUSB_SUCCESS) { - libusb_exit(ctx); + error = libusb_release_interface(dev_handle, INTERFACE_XLINK); + if (error != LIBUSB_SUCCESS) { + libusb_exit(ctx); - return error; - } + return error; + } - /* Release the device and exit */ - libusb_close(dev_handle); + /* Release the device and exit */ + libusb_close(dev_handle); } libusb_exit(ctx); @@ -166,14 +170,17 @@ int usbEpPlatformRead(void *fdKey, void *data, int size) int rc = 0; if (isServer) { - if(usbFdRead < 0) - { - return -1; - } - - rc = read(usbFdRead, data, size); + if(usbFdRead < 0) + { + return -1; + } +#if defined(_WIN32) + return X_LINK_ERROR; +#else + rc = read(usbFdRead, data, size); +#endif } else { - rc = libusb_bulk_transfer(dev_handle, usbFdRead, (unsigned char*)data, size, &rc, TIMEOUT); + rc = libusb_bulk_transfer(dev_handle, usbFdRead, (unsigned char*)data, size, &rc, TIMEOUT); } return rc; @@ -184,15 +191,17 @@ int usbEpPlatformWrite(void *fdKey, void *data, int size) int rc = 0; if (isServer) { - if(usbFdWrite < 0) - { - return -1; - } - - rc = write(usbFdWrite, data, size); - + if(usbFdWrite < 0) + { + return -1; + } +#if defined(_WIN32) + return X_LINK_ERROR; +#else + rc = write(usbFdWrite, data, size); +#endif } else { - rc = libusb_bulk_transfer(dev_handle, usbFdWrite, (unsigned char*)data, size, &rc, TIMEOUT); + rc = libusb_bulk_transfer(dev_handle, usbFdWrite, (unsigned char*)data, size, &rc, TIMEOUT); } return rc; From 558d1c1ac6cfb57b6ca74b2f02382ebfc252a2f5 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Thu, 27 Jul 2023 13:14:53 +0200 Subject: [PATCH 10/13] Semi-successful client/server with usb-ep --- examples/xlink_usb_client.cpp | 3 ++- examples/xlink_usb_server.cpp | 3 +-- src/pc/protocols/usb_host.cpp | 20 ++++++++++++---- src/pc/protocols/usb_host_ep.cpp | 40 ++++++++++++++++++++++++-------- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/examples/xlink_usb_client.cpp b/examples/xlink_usb_client.cpp index 35c6f1e..09cbe65 100644 --- a/examples/xlink_usb_client.cpp +++ b/examples/xlink_usb_client.cpp @@ -26,7 +26,8 @@ int main(int argc, char** argv) { mvLogDefaultLevelSet(MVLOG_ERROR); deviceDesc_t deviceDesc; - strcpy(deviceDesc.name, "usbdev"); + strcpy(deviceDesc.name, "1.4"); +// deviceDesc.protocol = X_LINK_USB_VSC; deviceDesc.protocol = X_LINK_USB_EP; printf("Device name: %s\n", deviceDesc.name); diff --git a/examples/xlink_usb_server.cpp b/examples/xlink_usb_server.cpp index 16c277b..b118787 100644 --- a/examples/xlink_usb_server.cpp +++ b/examples/xlink_usb_server.cpp @@ -37,11 +37,10 @@ int main(int argc, const char** argv){ } XLinkHandler_t handler; - handler.devicePath = "/dev/usb-ffs/xlink"; + handler.devicePath = "/dev/usb-ffs/depthai_device"; handler.protocol = X_LINK_USB_EP; XLinkServer(&handler, "eps", X_LINK_BOOTED, X_LINK_MYRIAD_X); - // loop through streams auto s = XLinkOpenStream(0, "test_0", sizeof(DUMMY_DATA) * 2); assert(s != INVALID_STREAM_ID); diff --git a/src/pc/protocols/usb_host.cpp b/src/pc/protocols/usb_host.cpp index d4174db..87d303a 100644 --- a/src/pc/protocols/usb_host.cpp +++ b/src/pc/protocols/usb_host.cpp @@ -34,10 +34,13 @@ static constexpr std::chrono::milliseconds DEFAULT_CONNECT_TIMEOUT{20000}; static constexpr std::chrono::milliseconds DEFAULT_SEND_FILE_TIMEOUT{10000}; static constexpr auto USB1_CHUNKSZ = 64; -static constexpr int USB_ENDPOINT_IN = 0x81; -static constexpr int USB_ENDPOINT_OUT = 0x01; +//static constexpr int USB_ENDPOINT_IN = 0x81; +static constexpr int USB_ENDPOINT_IN = 0x01; +//static constexpr int USB_ENDPOINT_OUT = 0x01; +static constexpr int USB_ENDPOINT_OUT = 0x81; -static constexpr int XLINK_USB_DATA_TIMEOUT = 0; +//static constexpr int XLINK_USB_DATA_TIMEOUT = 0; +static constexpr int XLINK_USB_DATA_TIMEOUT = 1000; static unsigned int bulk_chunklen = DEFAULT_CHUNKSZ; static int write_timeout = DEFAULT_WRITE_TIMEOUT; @@ -237,6 +240,7 @@ extern "C" xLinkPlatformErrorCode_t refLibusbDeviceByName(const char* name, libu // Check path only std::string devicePath = getLibusbDevicePath(devs[i]); + // Check if compare with name std::string requiredName(name); if(requiredName.length() > 0 && requiredName == devicePath){ @@ -850,7 +854,6 @@ int usbPlatformConnect(const char *devPathRead, const char *devPathWrite, void * return 0; #endif /*USE_LINK_JTAG*/ #else - libusb_device_handle* usbHandle = nullptr; xLinkPlatformErrorCode_t ret = usbLinkOpen(devPathWrite, usbHandle); @@ -866,6 +869,7 @@ int usbPlatformConnect(const char *devPathRead, const char *devPathWrite, void * #endif /*USE_USB_VSC*/ + return 0; } @@ -921,6 +925,7 @@ int usbPlatformBootFirmware(const deviceDesc_t* deviceDesc, const char* firmware int usb_read(libusb_device_handle *f, void *data, size_t size) { + printf("requested read. %d\r\n", size); const int chunk_size = DEFAULT_CHUNKSZ; while(size > 0) { @@ -928,6 +933,7 @@ int usb_read(libusb_device_handle *f, void *data, size_t size) if(ss > chunk_size) ss = chunk_size; int rc = libusb_bulk_transfer(f, USB_ENDPOINT_IN,(unsigned char *)data, ss, &bt, XLINK_USB_DATA_TIMEOUT); + printf("result of transfer: %d\r\n", rc); if(rc) return rc; data = ((char *)data) + bt; @@ -938,6 +944,9 @@ int usb_read(libusb_device_handle *f, void *data, size_t size) int usb_write(libusb_device_handle *f, const void *data, size_t size) { + printf("requested write. %d\r\n", size); + int bt, ss = (int)size; + const int chunk_size = DEFAULT_CHUNKSZ; while(size > 0) { @@ -945,6 +954,7 @@ int usb_write(libusb_device_handle *f, const void *data, size_t size) if(ss > chunk_size) ss = chunk_size; int rc = libusb_bulk_transfer(f, USB_ENDPOINT_OUT, (unsigned char *)data, ss, &bt, XLINK_USB_DATA_TIMEOUT); + printf("result of transfer: %d\r\n", rc); if(rc) return rc; data = (char *)data + bt; @@ -1021,6 +1031,7 @@ int usbPlatformWrite(void *fdKey, void *data, int size) { return -1; } + while(byteCount < size) { int toWrite = (PACKET_LENGTH && (size - byteCount > PACKET_LENGTH)) \ @@ -1035,6 +1046,7 @@ int usbPlatformWrite(void *fdKey, void *data, int size) byteCount += toWrite; unsigned char acknowledge; int rc; + rc = read(usbFdWrite, &acknowledge, sizeof(acknowledge)); if ( rc < 0) diff --git a/src/pc/protocols/usb_host_ep.cpp b/src/pc/protocols/usb_host_ep.cpp index ffe58fd..490fbb5 100644 --- a/src/pc/protocols/usb_host_ep.cpp +++ b/src/pc/protocols/usb_host_ep.cpp @@ -26,7 +26,7 @@ #define PRODUCT_ID 0x1234 /* Interface number for ffs.xlink */ -#define INTERFACE_XLINK 2 +#define INTERFACE_XLINK 0 /* Base ndpoint address used for output */ #define ENDPOINT_OUT_BASE 0x01 @@ -35,7 +35,7 @@ #define ENDPOINT_IN_BASE 0x81 /* Transfer timeout */ -#define TIMEOUT 2000 +#define TIMEOUT 200 static int usbFdRead, usbFdWrite; static bool isServer; @@ -88,8 +88,8 @@ int usbEpPlatformConnect(const char *devPathRead, const char *devPathWrite, void * In the way we initialized our usb-gadget on our device * We know ncm is claiming the first 2 interfaces */ - usbFdWrite = ENDPOINT_OUT_BASE + 1; /* +1 because NCM claims 1 OUT endpoint */ - usbFdRead = ENDPOINT_IN_BASE + 2; /* +2 because NCM claims 2 IN endpoints */ + usbFdWrite = ENDPOINT_OUT_BASE; + usbFdRead = ENDPOINT_IN_BASE; *fd = createPlatformDeviceFdKey((void*) (uintptr_t) usbFdRead); @@ -111,8 +111,8 @@ int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void strcpy(inPath, devPathWrite); strcat(inPath, "/ep2"); - int outfd = open(outPath, O_WRONLY); - int infd = open(inPath, O_RDONLY); + int outfd = open(outPath, O_RDWR); + int infd = open(inPath, O_RDWR); if(outfd < 0 || infd < 0) { return -1; @@ -170,6 +170,7 @@ int usbEpPlatformRead(void *fdKey, void *data, int size) int rc = 0; if (isServer) { + printf("Server read requested: %d\n", size); if(usbFdRead < 0) { return -1; @@ -178,12 +179,21 @@ int usbEpPlatformRead(void *fdKey, void *data, int size) return X_LINK_ERROR; #else rc = read(usbFdRead, data, size); + + printf("Amount read data: %d\n", rc); #endif } else { - rc = libusb_bulk_transfer(dev_handle, usbFdRead, (unsigned char*)data, size, &rc, TIMEOUT); + int amount; + printf("Client read requested: %d\n", size); + rc = libusb_bulk_transfer(dev_handle, usbFdRead, (unsigned char*)data, size, &amount, TIMEOUT); + + printf("Amount read data: %d\n", amount); } - return rc; + printf("Read return code: %d.\n", rc); + + if(rc < 0) return rc; + return 0; } int usbEpPlatformWrite(void *fdKey, void *data, int size) @@ -191,6 +201,7 @@ int usbEpPlatformWrite(void *fdKey, void *data, int size) int rc = 0; if (isServer) { + printf("Server write requested: %d\n", size); if(usbFdWrite < 0) { return -1; @@ -199,12 +210,21 @@ int usbEpPlatformWrite(void *fdKey, void *data, int size) return X_LINK_ERROR; #else rc = write(usbFdWrite, data, size); + + printf("Amount written data: %d\n", rc); #endif } else { - rc = libusb_bulk_transfer(dev_handle, usbFdWrite, (unsigned char*)data, size, &rc, TIMEOUT); + int amount; + printf("Client write requested: %d\n", size); + rc = libusb_bulk_transfer(dev_handle, usbFdWrite, (unsigned char*)data, size, &amount, TIMEOUT); + + printf("Amount written data: %d\n", amount); } + +printf("Write return code: %d.\n", rc); - return rc; + if(rc < 0) return rc; + return 0; } From 6165959e8634d68c35f5d0c045d4edccb6e1eef7 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Tue, 1 Aug 2023 12:28:45 +0200 Subject: [PATCH 11/13] Working implementation with USB_EP as server and USB_VSC as client. --- examples/xlink_usb_client.cpp | 33 ++++-- examples/xlink_usb_server.cpp | 41 +++++--- src/pc/protocols/usb_host.cpp | 13 +-- src/pc/protocols/usb_host_ep.cpp | 171 +++++-------------------------- 4 files changed, 82 insertions(+), 176 deletions(-) diff --git a/examples/xlink_usb_client.cpp b/examples/xlink_usb_client.cpp index 09cbe65..741df45 100644 --- a/examples/xlink_usb_client.cpp +++ b/examples/xlink_usb_client.cpp @@ -18,17 +18,24 @@ // Common constants const uint8_t DUMMY_DATA[1024*128] = {}; +XLinkGlobalHandler_t xlinkGlobalHandler = {}; +// Client int main(int argc, char** argv) { + if (argc != 2) { + printf("Usage: xlink_usb_client [name of device]\n"); + exit(1); + } + XLinkGlobalHandler_t gHandler; XLinkInitialize(&gHandler); - + mvLogDefaultLevelSet(MVLOG_ERROR); deviceDesc_t deviceDesc; - strcpy(deviceDesc.name, "1.4"); -// deviceDesc.protocol = X_LINK_USB_VSC; - deviceDesc.protocol = X_LINK_USB_EP; + strcpy(deviceDesc.name, argv[1]); + deviceDesc.protocol = X_LINK_USB_VSC; + // deviceDesc.protocol = X_LINK_USB_EP; printf("Device name: %s\n", deviceDesc.name); @@ -37,11 +44,12 @@ int main(int argc, char** argv) { handler.protocol = deviceDesc.protocol; auto connRet = XLinkConnect(&handler); printf("Connection returned: %s\n", XLinkErrorToStr(connRet)); + if(connRet != X_LINK_SUCCESS) { return -1; } - auto s = XLinkOpenStream(handler.linkId, "test_0", sizeof(DUMMY_DATA) * 2); + auto s = XLinkOpenStream(handler.linkId, "test_0", sizeof(DUMMY_DATA)); if(s == INVALID_STREAM_ID){ printf("Open stream failed...\n"); } else { @@ -49,7 +57,20 @@ int main(int argc, char** argv) { } auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s)); - assert(w == X_LINK_SUCCESS); + if (w == X_LINK_SUCCESS) { + printf("Write successful: 0x%08X\n", w); + } else { + printf("Write failed...\n"); + } + + streamPacketDesc_t p; + auto r = XLinkReadMoveData(s, &p); + if (r == X_LINK_SUCCESS) { + printf("Read successful: 0x%08X\n", w); + } else { + printf("Read failed...\n"); + } + XLinkDeallocateMoveData(p.data, p.length); return 0; } diff --git a/examples/xlink_usb_server.cpp b/examples/xlink_usb_server.cpp index b118787..c371da2 100644 --- a/examples/xlink_usb_server.cpp +++ b/examples/xlink_usb_server.cpp @@ -17,16 +17,11 @@ #include "XLink/XLinkLog.h" // Common constants -constexpr static auto NUM_STREAMS = 16; -constexpr static auto NUM_PACKETS = 120; const uint8_t DUMMY_DATA[1024*128] = {}; XLinkGlobalHandler_t xlinkGlobalHandler = {}; // Server -// - int main(int argc, const char** argv){ - xlinkGlobalHandler.protocol = X_LINK_USB_EP; // Initialize and suppress XLink logs @@ -39,23 +34,35 @@ int main(int argc, const char** argv){ XLinkHandler_t handler; handler.devicePath = "/dev/usb-ffs/depthai_device"; handler.protocol = X_LINK_USB_EP; - XLinkServer(&handler, "eps", X_LINK_BOOTED, X_LINK_MYRIAD_X); + auto serverRet = XLinkServer(&handler, "eps", X_LINK_BOOTED, X_LINK_MYRIAD_X); + printf("Connection returned: %s\n", XLinkErrorToStr(serverRet)); // loop through streams - auto s = XLinkOpenStream(0, "test_0", sizeof(DUMMY_DATA) * 2); - assert(s != INVALID_STREAM_ID); - - // auto w = XLinkWriteData2(s, (uint8_t*) &s, sizeof(s/2), ((uint8_t*) &s) + sizeof(s/2), sizeof(s) - sizeof(s/2)); - // assert(w == X_LINK_SUCCESS); + auto s = XLinkOpenStream(0, "test_0", sizeof(DUMMY_DATA)); + if(s == INVALID_STREAM_ID){ + printf("Open stream failed...\n"); + } else { + printf("Open stream OK - id: 0x%08X\n", s); + } auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s)); - assert(w == X_LINK_SUCCESS); - - + if (w == X_LINK_SUCCESS) { + printf("Write successful: 0x%08X\n", w); + } else { + printf("Write failed...\n"); + } + streamPacketDesc_t p; - w = XLinkReadMoveData(s, &p); - assert(w == X_LINK_SUCCESS); + auto r = XLinkReadMoveData(s, &p); + if (r == X_LINK_SUCCESS) { + printf("Read successful: 0x%08X\n", w); + } else { + printf("Read failed...\n"); + } XLinkDeallocateMoveData(p.data, p.length); - + + // Wait to make sure we caugth up to all requests + std::this_thread::sleep_for(std::chrono::seconds(2)); + return 0; } diff --git a/src/pc/protocols/usb_host.cpp b/src/pc/protocols/usb_host.cpp index 87d303a..167c757 100644 --- a/src/pc/protocols/usb_host.cpp +++ b/src/pc/protocols/usb_host.cpp @@ -34,13 +34,10 @@ static constexpr std::chrono::milliseconds DEFAULT_CONNECT_TIMEOUT{20000}; static constexpr std::chrono::milliseconds DEFAULT_SEND_FILE_TIMEOUT{10000}; static constexpr auto USB1_CHUNKSZ = 64; -//static constexpr int USB_ENDPOINT_IN = 0x81; -static constexpr int USB_ENDPOINT_IN = 0x01; -//static constexpr int USB_ENDPOINT_OUT = 0x01; -static constexpr int USB_ENDPOINT_OUT = 0x81; +static constexpr int USB_ENDPOINT_IN = 0x81; +static constexpr int USB_ENDPOINT_OUT = 0x01; -//static constexpr int XLINK_USB_DATA_TIMEOUT = 0; -static constexpr int XLINK_USB_DATA_TIMEOUT = 1000; +static constexpr int XLINK_USB_DATA_TIMEOUT = 0; static unsigned int bulk_chunklen = DEFAULT_CHUNKSZ; static int write_timeout = DEFAULT_WRITE_TIMEOUT; @@ -925,7 +922,6 @@ int usbPlatformBootFirmware(const deviceDesc_t* deviceDesc, const char* firmware int usb_read(libusb_device_handle *f, void *data, size_t size) { - printf("requested read. %d\r\n", size); const int chunk_size = DEFAULT_CHUNKSZ; while(size > 0) { @@ -933,7 +929,6 @@ int usb_read(libusb_device_handle *f, void *data, size_t size) if(ss > chunk_size) ss = chunk_size; int rc = libusb_bulk_transfer(f, USB_ENDPOINT_IN,(unsigned char *)data, ss, &bt, XLINK_USB_DATA_TIMEOUT); - printf("result of transfer: %d\r\n", rc); if(rc) return rc; data = ((char *)data) + bt; @@ -944,7 +939,6 @@ int usb_read(libusb_device_handle *f, void *data, size_t size) int usb_write(libusb_device_handle *f, const void *data, size_t size) { - printf("requested write. %d\r\n", size); int bt, ss = (int)size; const int chunk_size = DEFAULT_CHUNKSZ; @@ -954,7 +948,6 @@ int usb_write(libusb_device_handle *f, const void *data, size_t size) if(ss > chunk_size) ss = chunk_size; int rc = libusb_bulk_transfer(f, USB_ENDPOINT_OUT, (unsigned char *)data, ss, &bt, XLINK_USB_DATA_TIMEOUT); - printf("result of transfer: %d\r\n", rc); if(rc) return rc; data = (char *)data + bt; diff --git a/src/pc/protocols/usb_host_ep.cpp b/src/pc/protocols/usb_host_ep.cpp index 490fbb5..458f0af 100644 --- a/src/pc/protocols/usb_host_ep.cpp +++ b/src/pc/protocols/usb_host_ep.cpp @@ -14,95 +14,24 @@ #endif #include - -#include - #include -/* Vendor ID */ -#define VENDOR_ID 0x03e7 - -/* Product ID */ -#define PRODUCT_ID 0x1234 - -/* Interface number for ffs.xlink */ -#define INTERFACE_XLINK 0 - -/* Base ndpoint address used for output */ -#define ENDPOINT_OUT_BASE 0x01 - -/* Base endpoint address used for input */ -#define ENDPOINT_IN_BASE 0x81 - -/* Transfer timeout */ -#define TIMEOUT 200 - static int usbFdRead, usbFdWrite; -static bool isServer; - -static libusb_context *ctx = NULL; -static libusb_device_handle *dev_handle = NULL; int usbEpInitialize() { - int error; - - /* Initialize libusb */ - libusb_init(&ctx); - return 0; } int usbEpPlatformConnect(const char *devPathRead, const char *devPathWrite, void **fd) { - int error; - isServer = false; - - /* Get our device */ - dev_handle = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID); - if (dev_handle == NULL) { - libusb_exit(ctx); - - error = LIBUSB_ERROR_NO_DEVICE; - return error; - } - - /* Not strictly necessary, but it is better to use it, - * as we're using kernel modules together with our interfaces - */ - error = libusb_set_auto_detach_kernel_driver(dev_handle, 1); - if (error != LIBUSB_SUCCESS) { - libusb_exit(ctx); - - return error; - } - - /* Now we claim our ffs interfaces */ - error = libusb_claim_interface(dev_handle, INTERFACE_XLINK); - if (error != LIBUSB_SUCCESS) { - libusb_exit(ctx); - - return error; - } - - /* We get the first EP_OUT and EP_IN for our interfaces - * In the way we initialized our usb-gadget on our device - * We know ncm is claiming the first 2 interfaces - */ - usbFdWrite = ENDPOINT_OUT_BASE; - usbFdRead = ENDPOINT_IN_BASE; - - *fd = createPlatformDeviceFdKey((void*) (uintptr_t) usbFdRead); - - return 0; + return X_LINK_ERROR; } int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void **fd) { #if defined(_WIN32) - return X_LINK_ERROR; + return X_LINK_ERROR; #else - isServer = true; - char outPath[256]; strcpy(outPath, devPathWrite); strcat(outPath, "/ep1"); @@ -120,7 +49,7 @@ int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void usbFdRead = infd; usbFdWrite = outfd; - + *fd = createPlatformDeviceFdKey((void*) (uintptr_t) usbFdRead); return 0; @@ -132,35 +61,19 @@ int usbEpPlatformClose(void *fdKey) { int error; - if (isServer) { #if defined(_WIN32) - return X_LINK_ERROR; + return X_LINK_ERROR; #else - if (usbFdRead != -1){ - close(usbFdRead); - usbFdRead = -1; - } - - if (usbFdWrite != -1){ - close(usbFdWrite); - usbFdWrite = -1; - } -#endif - } else { - error = libusb_release_interface(dev_handle, INTERFACE_XLINK); - if (error != LIBUSB_SUCCESS) { - libusb_exit(ctx); - - return error; - } - - /* Release the device and exit */ - libusb_close(dev_handle); + if (usbFdRead != -1){ + close(usbFdRead); + usbFdRead = -1; } - libusb_exit(ctx); - - error = EXIT_SUCCESS; + if (usbFdWrite != -1){ + close(usbFdWrite); + usbFdWrite = -1; + } +#endif return EXIT_SUCCESS; } @@ -169,62 +82,34 @@ int usbEpPlatformRead(void *fdKey, void *data, int size) { int rc = 0; - if (isServer) { - printf("Server read requested: %d\n", size); - if(usbFdRead < 0) - { - return -1; - } + if(usbFdRead < 0) + { + return -1; + } #if defined(_WIN32) - return X_LINK_ERROR; + return X_LINK_ERROR; #else - rc = read(usbFdRead, data, size); - - printf("Amount read data: %d\n", rc); -#endif - } else { - int amount; - printf("Client read requested: %d\n", size); - rc = libusb_bulk_transfer(dev_handle, usbFdRead, (unsigned char*)data, size, &amount, TIMEOUT); - - printf("Amount read data: %d\n", amount); - } + rc = read(usbFdRead, data, size); - printf("Read return code: %d.\n", rc); - - if(rc < 0) return rc; - return 0; +#endif + return rc; } int usbEpPlatformWrite(void *fdKey, void *data, int size) { int rc = 0; - if (isServer) { - printf("Server write requested: %d\n", size); - if(usbFdWrite < 0) - { - return -1; - } + if(usbFdWrite < 0) + { + return -1; + } #if defined(_WIN32) - return X_LINK_ERROR; + return X_LINK_ERROR; #else - rc = write(usbFdWrite, data, size); - - printf("Amount written data: %d\n", rc); -#endif - } else { - int amount; - printf("Client write requested: %d\n", size); - rc = libusb_bulk_transfer(dev_handle, usbFdWrite, (unsigned char*)data, size, &amount, TIMEOUT); - - printf("Amount written data: %d\n", amount); - } - -printf("Write return code: %d.\n", rc); + rc = write(usbFdWrite, data, size); - if(rc < 0) return rc; - return 0; +#endif + return rc; } From a6f2d391611413207965082ea1517fe8a2a1890b Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 2 Aug 2023 10:59:04 +0200 Subject: [PATCH 12/13] Using now fdKeys and getting Read and Write paths from the user in USB EP --- src/pc/protocols/usb_host_ep.cpp | 111 +++++++++++++++++++------------ 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/src/pc/protocols/usb_host_ep.cpp b/src/pc/protocols/usb_host_ep.cpp index 458f0af..6baa4e0 100644 --- a/src/pc/protocols/usb_host_ep.cpp +++ b/src/pc/protocols/usb_host_ep.cpp @@ -11,12 +11,14 @@ #if not defined(_WIN32) #include #include -#endif #include #include -static int usbFdRead, usbFdWrite; +struct fdPair { + int usbFdRead; + int usbFdWrite; +}; int usbEpInitialize() { return 0; @@ -29,87 +31,112 @@ int usbEpPlatformConnect(const char *devPathRead, const char *devPathWrite, void int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void **fd) { -#if defined(_WIN32) - return X_LINK_ERROR; -#else - char outPath[256]; - strcpy(outPath, devPathWrite); - strcat(outPath, "/ep1"); - - char inPath[256]; - strcpy(inPath, devPathWrite); - strcat(inPath, "/ep2"); + fdPair *pair = new fdPair; - int outfd = open(outPath, O_RDWR); - int infd = open(inPath, O_RDWR); + pair->usbFdRead = -1; + pair->usbFdWrite = -1; - if(outfd < 0 || infd < 0) { - return -1; + if(devPathRead != NULL) + { + pair->usbFdRead = open(devPathRead, O_RDONLY); } - usbFdRead = infd; - usbFdWrite = outfd; + if(devPathWrite != NULL) + { + pair->usbFdWrite = open(devPathWrite, O_WRONLY); + } - *fd = createPlatformDeviceFdKey((void*) (uintptr_t) usbFdRead); + *fd = createPlatformDeviceFdKey((void*)pair); return 0; -#endif } int usbEpPlatformClose(void *fdKey) { + fdPair *pair; + getPlatformDeviceFdFromKey(fdKey, (void**)&pair); + int error; -#if defined(_WIN32) - return X_LINK_ERROR; -#else - if (usbFdRead != -1){ - close(usbFdRead); - usbFdRead = -1; + if (pair->usbFdRead > -1) { + close(pair->usbFdRead); + pair->usbFdRead = -1; } - if (usbFdWrite != -1){ - close(usbFdWrite); - usbFdWrite = -1; + if (pair->usbFdWrite > -1) { + close(pair->usbFdWrite); + pair->usbFdWrite = -1; } -#endif + + destroyPlatformDeviceFdKey(fdKey); + delete pair; return EXIT_SUCCESS; } int usbEpPlatformRead(void *fdKey, void *data, int size) { + fdPair *pair; + getPlatformDeviceFdFromKey(fdKey, (void**)&pair); + int rc = 0; - if(usbFdRead < 0) + if(pair->usbFdRead < 0) { return -1; } -#if defined(_WIN32) - return X_LINK_ERROR; -#else - rc = read(usbFdRead, data, size); -#endif + rc = read(pair->usbFdRead, data, size); + return rc; } int usbEpPlatformWrite(void *fdKey, void *data, int size) { + fdPair *pair; + getPlatformDeviceFdFromKey(fdKey, (void**)&pair); + int rc = 0; - if(usbFdWrite < 0) + if(pair->usbFdWrite < 0) { return -1; } -#if defined(_WIN32) - return X_LINK_ERROR; -#else - rc = write(usbFdWrite, data, size); -#endif + rc = write(pair->usbFdWrite, data, size); return rc; } +#else +int usbEpInitialize() { + return X_LINK_ERROR; +} + +int usbEpPlatformConnect(const char *devPathRead, const char *devPathWrite, void **fd) +{ + return X_LINK_ERROR; +} + +int usbEpPlatformServer(const char *devPathRead, const char *devPathWrite, void **fd) +{ + return X_LINK_ERROR; +} + +int usbEpPlatformClose(void *fdKey) +{ + return X_LINK_ERROR; +} + +int usbEpPlatformRead(void *fdKey, void *data, int size) +{ + return X_LINK_ERROR; +} + +int usbEpPlatformWrite(void *fdKey, void *data, int size) +{ + return X_LINK_ERROR; +} + +#endif From d4fffc3b7a8e62c1ef9c118a2746af1503ab29bc Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 2 Aug 2023 10:59:17 +0200 Subject: [PATCH 13/13] Adapted examples to USB_EP. --- examples/xlink_usb_client.cpp | 16 ++++++++-------- examples/xlink_usb_server.cpp | 13 +++---------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/examples/xlink_usb_client.cpp b/examples/xlink_usb_client.cpp index 741df45..f46656c 100644 --- a/examples/xlink_usb_client.cpp +++ b/examples/xlink_usb_client.cpp @@ -56,21 +56,21 @@ int main(int argc, char** argv) { printf("Open stream OK - id: 0x%08X\n", s); } - auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s)); - if (w == X_LINK_SUCCESS) { - printf("Write successful: 0x%08X\n", w); - } else { - printf("Write failed...\n"); - } - streamPacketDesc_t p; auto r = XLinkReadMoveData(s, &p); if (r == X_LINK_SUCCESS) { - printf("Read successful: 0x%08X\n", w); + printf("Read successful: 0x%08X\n", r); } else { printf("Read failed...\n"); } XLinkDeallocateMoveData(p.data, p.length); + auto w = XLinkWriteData(s, (uint8_t*) &s, sizeof(s)); + if (w == X_LINK_SUCCESS) { + printf("Write successful: 0x%08X\n", w); + } else { + printf("Write failed...\n"); + } + return 0; } diff --git a/examples/xlink_usb_server.cpp b/examples/xlink_usb_server.cpp index c371da2..db0a0d3 100644 --- a/examples/xlink_usb_server.cpp +++ b/examples/xlink_usb_server.cpp @@ -32,7 +32,9 @@ int main(int argc, const char** argv){ } XLinkHandler_t handler; - handler.devicePath = "/dev/usb-ffs/depthai_device"; + // Write is ep1, read is ep2 + handler.devicePath = "/dev/usb-ffs/depthai_device/ep1"; + handler.devicePath2 = "/dev/usb-ffs/depthai_device/ep2"; handler.protocol = X_LINK_USB_EP; auto serverRet = XLinkServer(&handler, "eps", X_LINK_BOOTED, X_LINK_MYRIAD_X); printf("Connection returned: %s\n", XLinkErrorToStr(serverRet)); @@ -52,15 +54,6 @@ int main(int argc, const char** argv){ printf("Write failed...\n"); } - streamPacketDesc_t p; - auto r = XLinkReadMoveData(s, &p); - if (r == X_LINK_SUCCESS) { - printf("Read successful: 0x%08X\n", w); - } else { - printf("Read failed...\n"); - } - XLinkDeallocateMoveData(p.data, p.length); - // Wait to make sure we caugth up to all requests std::this_thread::sleep_for(std::chrono::seconds(2));