Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop sever usb #70

Open
wants to merge 13 commits into
base: develop_server
Choose a base branch
from
Open
8 changes: 7 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
76 changes: 76 additions & 0 deletions examples/xlink_usb_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <cstdio>
#include <string>
#include <vector>
#include <array>
#include <thread>
#include <stdexcept>
#include <iostream>
#include <cassert>
#include <chrono>
#include <thread>
#include <algorithm>
#include <cstring>
#include <atomic>

#include "XLink/XLink.h"
#include "XLink/XLinkPublicDefines.h"
#include "XLink/XLinkLog.h"

// 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, argv[1]);
deviceDesc.protocol = X_LINK_USB_VSC;
// 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));
if(s == INVALID_STREAM_ID){
printf("Open stream failed...\n");
} else {
printf("Open stream OK - id: 0x%08X\n", s);
}

streamPacketDesc_t p;
auto r = XLinkReadMoveData(s, &p);
if (r == X_LINK_SUCCESS) {
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;
}
61 changes: 61 additions & 0 deletions examples/xlink_usb_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <cstdio>
#include <string>
#include <vector>
#include <array>
#include <thread>
#include <stdexcept>
#include <iostream>
#include <cassert>
#include <chrono>
#include <thread>
#include <algorithm>
#include <cstring>
#include <atomic>

#include "XLink/XLink.h"
#include "XLink/XLinkPublicDefines.h"
#include "XLink/XLinkLog.h"

// Common constants
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;
// 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));

// loop through streams
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));
if (w == X_LINK_SUCCESS) {
printf("Write successful: 0x%08X\n", w);
} else {
printf("Write failed...\n");
}

// Wait to make sure we caugth up to all requests
std::this_thread::sleep_for(std::chrono::seconds(2));

return 0;
}
1 change: 1 addition & 0 deletions include/XLink/XLinkPublicDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/pc/PlatformData.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ extern int usbFdWrite;
extern int usbFdRead;
#endif /*USE_USB_VSC*/

#include "usb_host_ep.h"

// ------------------------------------
// Wrappers declaration. Begin.
// ------------------------------------
Expand All @@ -79,7 +81,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:
Expand All @@ -91,6 +93,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;
}
Expand All @@ -113,6 +118,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;
}
Expand Down
22 changes: 19 additions & 3 deletions src/pc/PlatformDeviceControl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/pc/protocols/usb_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,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){
Expand Down Expand Up @@ -850,7 +851,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);

Expand All @@ -866,6 +866,7 @@ int usbPlatformConnect(const char *devPathRead, const char *devPathWrite, void *

#endif /*USE_USB_VSC*/


return 0;
}

Expand Down Expand Up @@ -938,6 +939,8 @@ 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)
{
int bt, ss = (int)size;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required?


const int chunk_size = DEFAULT_CHUNKSZ;
while(size > 0)
{
Expand Down Expand Up @@ -1021,6 +1024,7 @@ int usbPlatformWrite(void *fdKey, void *data, int size)
{
return -1;
}

while(byteCount < size)
{
int toWrite = (PACKET_LENGTH && (size - byteCount > PACKET_LENGTH)) \
Expand All @@ -1035,6 +1039,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)
Expand Down
Loading