Drivers: Ensure serial numbers are uppercase

This prevents mismatches if the device sends a lowercase
serial number, which may happen due to an oversight in
production.
add-device-sharing
Paul Hollinsky 2022-05-28 14:41:41 -04:00
parent 00024990e9
commit 407ccccedd
4 changed files with 13 additions and 1 deletions

View File

@ -2,6 +2,7 @@
#include "icsneo/device/founddevice.h"
#include <mutex>
#include <vector>
#include <cctype>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
@ -166,9 +167,11 @@ void CDCACM::Find(std::vector<FoundDevice>& found) {
if(CFGetTypeID(serialProp) != CFStringGetTypeID())
continue;
// We can static cast here because we have verified the type to be a CFString
const std::string serial = CFStringToString(static_cast<CFStringRef>(serialProp));
std::string serial = CFStringToString(static_cast<CFStringRef>(serialProp));
if(serial.empty())
continue;
for(char& c : serial)
c = static_cast<char>(toupper(c));
device.serial[serial.copy(device.serial, sizeof(device.serial)-1)] = '\0';
// Add a factory to make the driver

View File

@ -5,6 +5,7 @@
#include <cstring>
#include <memory>
#include <utility>
#include <cctype>
#include <algorithm>
#include <libusb.h>
@ -24,6 +25,8 @@ void FTDI::Find(std::vector<FoundDevice>& found) {
FoundDevice d;
strncpy(d.serial, serial.c_str(), deviceSerialBufferLength - 1);
d.serial[deviceSerialBufferLength - 1] = '\0'; // strncpy does not write a null terminator if serial is too long
for(size_t i = 0; i < deviceSerialBufferLength - 1; i++)
d.serial[i] = toupper(serial[i]);
std::string devHandle = serial;
auto it = std::find(handles.begin(), handles.end(), devHandle);
size_t foundHandle = SIZE_MAX;

View File

@ -7,6 +7,7 @@
#include <fstream>
#include <map>
#include <algorithm>
#include <cctype>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
@ -67,6 +68,8 @@ public:
try {
std::ifstream reader(ss.str());
std::getline(reader, serial);
for (auto& c : serial)
c = toupper(c);
} catch(...) {
succeeded = false;
return;

View File

@ -9,6 +9,7 @@
#include <cwctype>
#include <algorithm>
#include <codecvt>
#include <cctype>
#include <limits>
#include <stdio.h>
@ -152,6 +153,8 @@ void VCP::Find(std::vector<FoundDevice>& found, std::vector<std::wstring> driver
if(serial.find_first_of('\\') != std::string::npos)
continue;
}
for(char& c : serial)
c = static_cast<char>(toupper(c));
strcpy_s(device.serial, sizeof(device.serial), serial.c_str());
// Serial number is saved, we want the COM port number now