попытки сделать crsf, не взлетела часть с пультом
This commit is contained in:
64
lib/port/win/poller.cpp
Normal file
64
lib/port/win/poller.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "port/poller.h"
|
||||
#include <windows.h>
|
||||
#include <stdexcept>
|
||||
|
||||
bool poller::PollObject::isPollIn() const { return revents != 0; }
|
||||
bool poller::PollObject::isPollOut() const { return true; } // для Windows драйверов обычно всегда можно писать
|
||||
bool poller::PollObject::isPollHup() const {
|
||||
// UART: проверка ClearCommError
|
||||
if (hCom != INVALID_HANDLE_VALUE) {
|
||||
DWORD errors;
|
||||
if (!ClearCommError(hCom, &errors, nullptr)) {
|
||||
DWORD err = GetLastError();
|
||||
if (err == ERROR_INVALID_HANDLE || err == ERROR_FILE_NOT_FOUND)
|
||||
return true; // COM порт физически пропал
|
||||
}
|
||||
if (errors & CE_RXOVER) return true; // overflow тоже можно трактовать как проблему
|
||||
}
|
||||
|
||||
// UDP: проверка ошибки сокета через SO_ERROR
|
||||
if (sock != INVALID_SOCKET) {
|
||||
int err = 0;
|
||||
int len = sizeof(err);
|
||||
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == 0) {
|
||||
if (err != 0) return true; // есть ошибка на сокете
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
poller::PollObject::~PollObject() = default;
|
||||
|
||||
poller::PollWrapper::PollWrapper() = default;
|
||||
|
||||
void poller::PollWrapper::loop(int timeoutMs) {
|
||||
// собираем все события
|
||||
std::vector<HANDLE> handles;
|
||||
for (auto& obj : objects) {
|
||||
#ifdef _WIN32
|
||||
if (!obj) continue;
|
||||
// UART HANDLE или UDP событие
|
||||
if (obj->winHandle)
|
||||
handles.push_back(obj->winHandle);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (handles.empty()) return;
|
||||
|
||||
DWORD waitTime = (timeoutMs < 0) ? INFINITE : static_cast<DWORD>(timeoutMs);
|
||||
DWORD rc = WaitForMultipleObjects(static_cast<DWORD>(handles.size()),
|
||||
handles.data(),
|
||||
FALSE, // ждем любого события
|
||||
waitTime);
|
||||
if (rc == WAIT_FAILED)
|
||||
throw std::runtime_error("WaitForMultipleObjects failed");
|
||||
|
||||
// Определяем, какой объект сработал
|
||||
int idx = rc - WAIT_OBJECT_0;
|
||||
if (idx >= 0 && idx < static_cast<int>(handles.size())) {
|
||||
auto& obj = objects[idx];
|
||||
obj->revents = 1; // простая метка, что событие произошло
|
||||
}
|
||||
}
|
||||
|
||||
poller::PollWrapper::~PollWrapper() = default;
|
||||
71
lib/port/win/uart.cpp
Normal file
71
lib/port/win/uart.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "port/poller.h"
|
||||
#include "port/uart.h"
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
|
||||
drivers::UartDriver::UartDriver(const std::string& path, int baud) {
|
||||
hCom = CreateFileA(path.c_str(),
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0, nullptr,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED,
|
||||
nullptr);
|
||||
if (hCom == INVALID_HANDLE_VALUE)
|
||||
throw std::runtime_error("Cannot open UART port");
|
||||
|
||||
// Настройка DCB
|
||||
DCB dcb{};
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
if (!GetCommState(hCom, &dcb))
|
||||
throw std::runtime_error("GetCommState failed");
|
||||
|
||||
dcb.BaudRate = baud;
|
||||
dcb.ByteSize = 8;
|
||||
dcb.Parity = EVENPARITY; // 8E2
|
||||
dcb.StopBits = TWOSTOPBITS;
|
||||
|
||||
if (!SetCommState(hCom, &dcb))
|
||||
throw std::runtime_error("SetCommState failed");
|
||||
|
||||
// Настройка событий
|
||||
winHandle = CreateEvent(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!winHandle) throw std::runtime_error("CreateEvent failed");
|
||||
|
||||
if (!SetCommMask(hCom, EV_RXCHAR))
|
||||
throw std::runtime_error("SetCommMask failed");
|
||||
|
||||
// Первый вызов WaitCommEvent в асинхронном режиме
|
||||
memset(&overlapped, 0, sizeof(overlapped));
|
||||
overlapped.hEvent = winHandle;
|
||||
DWORD dummy;
|
||||
WaitCommEvent(hCom, &dummy, &overlapped);
|
||||
}
|
||||
bool drivers::UartDriver::writeData(std::span<const uint8_t> data) {
|
||||
DWORD written;
|
||||
OVERLAPPED ov{};
|
||||
ov.hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
|
||||
|
||||
bool ok = WriteFile(hCom, data.data(), (DWORD)data.size(), &written, &ov) ||
|
||||
GetOverlappedResult(hCom, &ov, &written, TRUE);
|
||||
CloseHandle(ov.hEvent);
|
||||
return ok && written == data.size();
|
||||
}
|
||||
size_t drivers::UartDriver::readChunk(std::vector<uint8_t>& out) {
|
||||
out.resize(512);
|
||||
DWORD read = 0;
|
||||
OVERLAPPED ov{};
|
||||
ov.hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
|
||||
bool ok = ReadFile(hCom, out.data(), (DWORD)out.size(), &read, &ov) ||
|
||||
GetOverlappedResult(hCom, &ov, &read, TRUE);
|
||||
CloseHandle(ov.hEvent);
|
||||
out.resize(read);
|
||||
return read;
|
||||
}
|
||||
drivers::UartDriver::~UartDriver() {
|
||||
if (hCom != INVALID_HANDLE_VALUE) CloseHandle(hCom);
|
||||
if (winHandle) CloseHandle(winHandle);
|
||||
}
|
||||
|
||||
66
lib/port/win/udp.cpp
Normal file
66
lib/port/win/udp.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
// File: src/port/win/udp_driver_win.cpp
|
||||
#include "pollobject.h"
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
|
||||
#pragma comment(lib, "Ws2_32.lib")
|
||||
|
||||
namespace drivers {
|
||||
|
||||
class UdpDriver : public poller::PollObject {
|
||||
public:
|
||||
explicit UdpDriver(uint16_t port) {
|
||||
WSADATA wsa;
|
||||
WSAStartup(MAKEWORD(2,2), &wsa);
|
||||
|
||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (sock == INVALID_SOCKET) throw std::runtime_error("Cannot create socket");
|
||||
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if (bind(sock, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR)
|
||||
throw std::runtime_error("Bind failed");
|
||||
|
||||
winHandle = WSACreateEvent();
|
||||
if (!winHandle) throw std::runtime_error("WSACreateEvent failed");
|
||||
|
||||
WSAEventSelect(sock, winHandle, FD_READ);
|
||||
}
|
||||
|
||||
bool sendTo(std::span<const uint8_t> data, const std::string& addrStr, uint16_t port) {
|
||||
sockaddr_in dest{};
|
||||
dest.sin_family = AF_INET;
|
||||
dest.sin_port = htons(port);
|
||||
inet_pton(AF_INET, addrStr.c_str(), &dest.sin_addr);
|
||||
|
||||
int ret = sendto(sock, (const char*)data.data(), (int)data.size(), 0,
|
||||
(sockaddr*)&dest, sizeof(dest));
|
||||
return ret == (int)data.size();
|
||||
}
|
||||
|
||||
bool recvPacket(std::vector<uint8_t>& out) {
|
||||
out.resize(512);
|
||||
sockaddr_in src{};
|
||||
int len = sizeof(src);
|
||||
int ret = recvfrom(sock, (char*)out.data(), (int)out.size(), MSG_PEEK,
|
||||
(sockaddr*)&src, &len);
|
||||
if (ret <= 0) return false; // ничего нет или ошибка
|
||||
out.resize(ret);
|
||||
return true;
|
||||
}
|
||||
|
||||
~UdpDriver() override {
|
||||
if (sock != INVALID_SOCKET) closesocket(sock);
|
||||
if (winHandle) WSACloseEvent(winHandle);
|
||||
WSACleanup();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace drivers
|
||||
Reference in New Issue
Block a user