Skip to content

Commit

Permalink
Handle "location" support for Edgeport devices
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Oct 24, 2023
1 parent 8a4a0f7 commit 10bcf48
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 17 deletions.
55 changes: 46 additions & 9 deletions src/main/c/Windows/SerialPort_Windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SerialPort_Windows.c
*
* Created on: Feb 25, 2012
* Last Updated on: Oct 17, 2023
* Last Updated on: Oct 24, 2023
* Author: Will Hedgecock
*
* Copyright (C) 2012-2023 Fazecast, Inc.
Expand Down Expand Up @@ -258,18 +258,55 @@ static void enumeratePorts(void)
if (locationString && SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_LOCATION_INFORMATION, NULL, (BYTE*)locationString, locationLength, NULL))
{
locationString[(locationLength / sizeof(wchar_t)) - 1] = 0;
if (wcsstr(locationString, L"Hub"))
if (wcsstr(locationString, L"Hub_#"))
hubNumber = _wtoi(wcschr(wcsstr(locationString, L"Hub"), L'#') + 1);
if ((portNumber == -1) && wcsstr(locationString, L"Port"))
if (portNumber == -1)
{
wchar_t *portString = wcschr(wcsstr(locationString, L"Port"), L'#') + 1;
if (portString)
if (wcsstr(locationString, L"Port_#"))
{
wchar_t *end = wcschr(portString, L'.');
if (end)
*end = L'\0';
wchar_t *portString = wcschr(wcsstr(locationString, L"Port"), L'#') + 1;
if (portString)
{
wchar_t *end = wcschr(portString, L'.');
if (end)
*end = L'\0';
portNumber = _wtoi(portString);
}
}
else if (wcsstr(locationString, L"Port_") && wcschr(locationString, L'-') && wcschr(locationString, L']'))
{
wchar_t *portString = wcschr(wcsstr(locationString, L"Port"), L'_') + 1;
if (portString)
{
wchar_t *end = wcschr(portString, L' ');
if (end)
*end = L'\0';
portNumber = _wtoi(portString);
if (end)
*end = L' ';
}
if (hubNumber == -1)
{
wchar_t *hubString = wcsrchr(locationString, L'-') + 1;
if (hubString)
{
wchar_t *end = wcschr(hubString, L']');
if (end)
*end = L'\0';
hubNumber = _wtoi(hubString);
if (end)
*end = L']';
}
}
if (!serialNumberString && wcschr(locationString, L'[') && wcschr(locationString, L']'))
{
serialNumberString = (wchar_t*)malloc(32*sizeof(wchar_t));
wchar_t *serialBegin = wcschr(locationString, L'[') + 1;
wchar_t *serialEnd = wcspbrk(serialBegin, L"-]");
*serialEnd = L'\0';
wcscpy_s(serialNumberString, 32, serialBegin);
}
}
portNumber = _wtoi(portString);
}
}
if (locationString)
Expand Down
53 changes: 45 additions & 8 deletions src/test/c/testEnumerateWindows.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,55 @@ static void enumeratePorts(void)
if (locationString && SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_LOCATION_INFORMATION, NULL, (BYTE*)locationString, locationLength, NULL))
{
locationString[(locationLength / sizeof(wchar_t)) - 1] = 0;
if (wcsstr(locationString, L"Hub"))
if (wcsstr(locationString, L"Hub_#"))
hubNumber = _wtoi(wcschr(wcsstr(locationString, L"Hub"), L'#') + 1);
if ((portNumber == -1) && wcsstr(locationString, L"Port"))
if (portNumber == -1)
{
wchar_t *portString = wcschr(wcsstr(locationString, L"Port"), L'#') + 1;
if (portString)
if (wcsstr(locationString, L"Port_#"))
{
wchar_t *end = wcschr(portString, L'.');
if (end)
*end = L'\0';
wchar_t *portString = wcschr(wcsstr(locationString, L"Port"), L'#') + 1;
if (portString)
{
wchar_t *end = wcschr(portString, L'.');
if (end)
*end = L'\0';
portNumber = _wtoi(portString);
}
}
else if (wcsstr(locationString, L"Port_") && wcschr(locationString, L'-') && wcschr(locationString, L']'))
{
wchar_t *portString = wcschr(wcsstr(locationString, L"Port"), L'_') + 1;
if (portString)
{
wchar_t *end = wcschr(portString, L' ');
if (end)
*end = L'\0';
portNumber = _wtoi(portString);
if (end)
*end = L' ';
}
if (hubNumber == -1)
{
wchar_t *hubString = wcsrchr(locationString, L'-') + 1;
if (hubString)
{
wchar_t *end = wcschr(hubString, L']');
if (end)
*end = L'\0';
hubNumber = _wtoi(hubString);
if (end)
*end = L']';
}
}
if (!serialNumberString && wcschr(locationString, L'[') && wcschr(locationString, L']'))
{
serialNumberString = (wchar_t*)malloc(32*sizeof(wchar_t));
wchar_t *serialBegin = wcschr(locationString, L'[') + 1;
wchar_t *serialEnd = wcspbrk(serialBegin, L"-]");
*serialEnd = L'\0';
wcscpy_s(serialNumberString, 32, serialBegin);
}
}
portNumber = _wtoi(portString);
}
}
if (locationString)
Expand Down

0 comments on commit 10bcf48

Please sign in to comment.