Skip to content

Commit

Permalink
Add a custom timeout option the the device discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Matevz Morato committed Feb 24, 2025
1 parent 6615e07 commit 72132e2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
build/
build*/

#clangd
.cache/*

#git
*.orig
*_REMOTE_*
*_LOCAL_*
*_BACKUP_*
*_BASE_*
*_BASE_*
4 changes: 3 additions & 1 deletion include/XLink/XLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ XLinkError_t XLinkFindFirstSuitableDevice(const deviceDesc_t in_deviceRequiremen
* @param[in,out] out_foundDevicesPtr - pointer to array with all found devices descriptions
* @param[out] devicesArraySize - size of out_foundDevicesPtr
* @param[out] out_foundDevicesCount - amount of found devices
* @param[in] timeoutMs - for how long to search for
* @return Status code of the operation: X_LINK_SUCCESS (0) for success
*/
XLinkError_t XLinkFindAllSuitableDevices(const deviceDesc_t in_deviceRequirements,
deviceDesc_t *out_foundDevicesPtr,
const unsigned int devicesArraySize,
unsigned int *out_foundDevicesCount);
unsigned int *out_foundDevicesCount,
int timeoutMs);

/**
* @brief Returns all Myriad devices description which meets the requirements
Expand Down
2 changes: 1 addition & 1 deletion include/XLink/XLinkPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ xLinkPlatformErrorCode_t XLinkPlatformInit(XLinkGlobalHandler_t* globalHandler);
*/
xLinkPlatformErrorCode_t XLinkPlatformFindDevices(const deviceDesc_t in_deviceRequirements,
deviceDesc_t* out_foundDevices, unsigned sizeFoundDevices,
unsigned *out_amountOfFoundDevices);
unsigned *out_amountOfFoundDevices, int timeoutMs);
xLinkPlatformErrorCode_t XLinkPlatformFindDevicesDynamic(const deviceDesc_t in_deviceRequirements,
deviceDesc_t* out_foundDevices, unsigned sizeFoundDevices,
unsigned *out_amountOfFoundDevices, int timeoutMs, bool (*cb)(deviceDesc_t*, unsigned int));
Expand Down
1 change: 1 addition & 0 deletions include/XLink/XLinkPublicDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C"
#endif
#define XLINK_MAX_PACKETS_PER_STREAM 64
#define XLINK_NO_RW_TIMEOUT 0xFFFFFFFF
#define XLINK_DEVICE_DEFAULT_SEARCH_TIMEOUT_MS 500


typedef enum {
Expand Down
13 changes: 7 additions & 6 deletions src/pc/PlatformDeviceSearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ static xLinkPlatformErrorCode_t getPCIeDeviceName(int index,
deviceDesc_t* out_foundDevice);
static xLinkPlatformErrorCode_t getTcpIpDevices(const deviceDesc_t in_deviceRequirements,
deviceDesc_t* out_foundDevices, int sizeFoundDevices,
unsigned int *out_amountOfFoundDevices);
unsigned int *out_amountOfFoundDevices,
int timeoutMs);

#if defined(__unix__)
static xLinkPlatformErrorCode_t getLocalShdmemDevices(const deviceDesc_t in_deviceRequirements,
Expand All @@ -51,7 +52,7 @@ static xLinkPlatformErrorCode_t getLocalShdmemDevices(const deviceDesc_t in_devi

xLinkPlatformErrorCode_t XLinkPlatformFindDevices(const deviceDesc_t in_deviceRequirements,
deviceDesc_t* out_foundDevices, unsigned sizeFoundDevices,
unsigned int *out_amountOfFoundDevices) {
unsigned int *out_amountOfFoundDevices, int timeoutMs) {
memset(out_foundDevices, 0, sizeFoundDevices * sizeof(deviceDesc_t));
xLinkPlatformErrorCode_t USB_rc;
xLinkPlatformErrorCode_t PCIe_rc;
Expand All @@ -77,7 +78,7 @@ xLinkPlatformErrorCode_t XLinkPlatformFindDevices(const deviceDesc_t in_deviceRe
if(!XLinkIsProtocolInitialized(in_deviceRequirements.protocol)) {
return X_LINK_PLATFORM_DRIVER_NOT_LOADED+in_deviceRequirements.protocol;
}
return getTcpIpDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, out_amountOfFoundDevices);
return getTcpIpDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, out_amountOfFoundDevices, timeoutMs);

#if defined(__unix__)
case X_LINK_LOCAL_SHDMEM:
Expand Down Expand Up @@ -140,7 +141,7 @@ xLinkPlatformErrorCode_t XLinkPlatformFindDevices(const deviceDesc_t in_deviceRe
// Try find TCPIP device
if(XLinkIsProtocolInitialized(X_LINK_TCP_IP)) {
numFoundDevices = 0;
TCPIP_rc = getTcpIpDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, &numFoundDevices);
TCPIP_rc = getTcpIpDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, &numFoundDevices, timeoutMs);
*out_amountOfFoundDevices += numFoundDevices;
out_foundDevices += numFoundDevices;
// Found enough devices, return
Expand Down Expand Up @@ -302,7 +303,7 @@ xLinkPlatformErrorCode_t getPCIeDeviceName(int index,

xLinkPlatformErrorCode_t getTcpIpDevices(const deviceDesc_t in_deviceRequirements,
deviceDesc_t* out_foundDevices, int sizeFoundDevices,
unsigned int *out_amountOfFoundDevices)
unsigned int *out_amountOfFoundDevices, int timeoutMs)
{
ASSERT_XLINK_PLATFORM(out_foundDevices);
ASSERT_XLINK_PLATFORM(out_amountOfFoundDevices);
Expand All @@ -321,7 +322,7 @@ xLinkPlatformErrorCode_t getTcpIpDevices(const deviceDesc_t in_deviceRequirement
return X_LINK_PLATFORM_DEVICE_NOT_FOUND;
}

return tcpip_get_devices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, out_amountOfFoundDevices);
return tcpip_get_devices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, out_amountOfFoundDevices, timeoutMs);
}


Expand Down
4 changes: 2 additions & 2 deletions src/pc/protocols/tcpip_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ xLinkPlatformErrorCode_t tcpip_close_search_context(void* ctx)


// TODO(themarpe) - duplicate until further tested
xLinkPlatformErrorCode_t tcpip_get_devices(const deviceDesc_t in_deviceRequirements, deviceDesc_t* devices, size_t devices_size, unsigned int* device_count)
xLinkPlatformErrorCode_t tcpip_get_devices(const deviceDesc_t in_deviceRequirements, deviceDesc_t* devices, size_t devices_size, unsigned int* device_count, int timeout_ms)
{
// Name signifies ip in TCP_IP protocol case
const char* target_ip = in_deviceRequirements.name;
Expand Down Expand Up @@ -790,7 +790,7 @@ xLinkPlatformErrorCode_t tcpip_get_devices(const deviceDesc_t in_deviceRequireme

num_devices_match++;
}
} while(std::chrono::steady_clock::now() - t1 < DEVICE_DISCOVERY_RES_TIMEOUT);
} while(std::chrono::steady_clock::now() - t1 < std::chrono::milliseconds(timeout_ms));

tcpip_close_socket(sock);

Expand Down
3 changes: 2 additions & 1 deletion src/pc/protocols/tcpip_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ tcpipHostError_t tcpip_close_socket(TCPIP_SOCKET socket);
* @param[in] devices_size Size of devices array
* @param[out] device_count Total device IP address obtained
* @param[in] target_ip Target IP address to be checked
* @param[in] timeout_ms Timeout in milliseconds
* @retval TCPIP_HOST_ERROR Failed to get network interface informations
* @retval TCPIP_HOST_SUCCESS Received all device IP address available
*/
xLinkPlatformErrorCode_t tcpip_get_devices(const deviceDesc_t in_deviceRequirements, deviceDesc_t* devices, size_t devices_size, unsigned int* device_count);
xLinkPlatformErrorCode_t tcpip_get_devices(const deviceDesc_t in_deviceRequirements, deviceDesc_t* devices, size_t devices_size, unsigned int* device_count, int timeout_ms);

xLinkPlatformErrorCode_t tcpip_create_search_context(void** pctx, const deviceDesc_t in_deviceRequirements);
xLinkPlatformErrorCode_t tcpip_perform_search(void* ctx, deviceDesc_t* devices, size_t devices_size, unsigned int* device_count);
Expand Down
7 changes: 4 additions & 3 deletions src/shared/XLinkDevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ XLinkError_t XLinkFindFirstSuitableDevice(const deviceDesc_t in_deviceRequiremen

xLinkPlatformErrorCode_t rc;
unsigned numFoundDevices = 0;
rc = XLinkPlatformFindDevices(in_deviceRequirements, out_foundDevice, 1, &numFoundDevices);
rc = XLinkPlatformFindDevices(in_deviceRequirements, out_foundDevice, 1, &numFoundDevices, XLINK_DEVICE_DEFAULT_SEARCH_TIMEOUT_MS);
if(numFoundDevices <= 0){
return X_LINK_DEVICE_NOT_FOUND;
}
Expand All @@ -236,13 +236,14 @@ XLinkError_t XLinkFindFirstSuitableDevice(const deviceDesc_t in_deviceRequiremen
XLinkError_t XLinkFindAllSuitableDevices(const deviceDesc_t in_deviceRequirements,
deviceDesc_t *out_foundDevicesPtr,
const unsigned int devicesArraySize,
unsigned int* out_foundDevicesCount) {
unsigned int* out_foundDevicesCount,
int timeoutMs) {
XLINK_RET_IF(out_foundDevicesPtr == NULL);
XLINK_RET_IF(devicesArraySize <= 0);
XLINK_RET_IF(out_foundDevicesCount == NULL);

xLinkPlatformErrorCode_t rc;
rc = XLinkPlatformFindDevices(in_deviceRequirements, out_foundDevicesPtr, devicesArraySize, out_foundDevicesCount);
rc = XLinkPlatformFindDevices(in_deviceRequirements, out_foundDevicesPtr, devicesArraySize, out_foundDevicesCount, timeoutMs);

return parsePlatformError(rc);
}
Expand Down

0 comments on commit 72132e2

Please sign in to comment.