18#include <radio_tool/radio/serial_radio_factory.hpp>
19#include <radio_tool/radio/radio.hpp>
20#include <radio_tool/radio/ailunce_radio.hpp>
21#include <radio_tool/util.hpp>
28#define WIN32_LEAN_AND_MEAN 1
32namespace fs = std::filesystem;
39 std::function<bool(
const std::string&)> SupportsDevice;
43const std::vector<DeviceMapper> Drivers = {
44 {AilunceRadio::SupportsDevice, AilunceRadio::Create} };
46auto SerialRadioFactory::ListDevices(
const uint16_t& idx_offset)
const ->
const std::vector<RadioInfo*>
48 auto ret = std::vector<RadioInfo*>();
51 [&ret, idx_offset](
const std::string& port,
const uint16_t& idx)
53 for (
auto& driver : Drivers)
55 auto fnOpen = [&driver, port]()
57 return driver.CreateOperations(port);
60 if (driver.SupportsDevice(port))
70auto SerialRadioFactory::OpDeviceList(std::function<
void(
const std::string&,
const uint16_t&)> fn)
const ->
void
72 HKEY comKey =
nullptr;
73 auto openResult = RegOpenKeyExA(HKEY_LOCAL_MACHINE, (LPSTR)
"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ | KEY_WOW64_64KEY, &comKey);
74 if (openResult != ERROR_SUCCESS)
76 throw std::runtime_error(
"Failed to enumerate serial ports");
79 constexpr auto BufferSize = 1024L;
81 char name[BufferSize] = {}, value[BufferSize] = {};
84 auto nameSize = BufferSize;
85 auto valueSize = BufferSize;
86 memset(name, 0, BufferSize);
87 memset(value, 0, BufferSize);
89 auto readResult = RegEnumValueA(comKey, idx, name, (LPDWORD)&nameSize, NULL, NULL, (LPBYTE)value, (LPDWORD)&valueSize);
90 if (readResult == ERROR_SUCCESS)
92 fn(std::string(value), (uint16_t)idx);
94 else if (readResult == ERROR_NO_MORE_ITEMS)
100 throw std::runtime_error(
"Error reading registory key values");
109auto SerialRadioFactory::OpDeviceList(std::function<
void(
const std::string&,
const uint16_t&)> op)
const ->
void
112 auto p = fs::path(
"/dev");
114 for (
const auto& de : fs::directory_iterator(p))
116 auto stem = de.path().stem().string();
117 if (stem ==
"tty" || stem ==
"cu")
119 op(de.path().string(), idx++);
124 auto p = fs::path(
"/dev/serial/by-id");
131 for (
auto& de : fs::directory_iterator(p))
133 if (fs::is_symlink(de.symlink_status()))
135 auto canonical_path = fs::canonical(de);
136 op(canonical_path.string(), idx++);