попытки сделать crsf, не взлетела часть с пультом
This commit is contained in:
51
lib/port/unix/poller.cpp
Normal file
51
lib/port/unix/poller.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "port/poller.h"
|
||||
|
||||
#include <sys/poll.h>
|
||||
#include <algorithm>
|
||||
|
||||
bool poller::PollObject::isPollIn() const { return revents & POLLIN; }
|
||||
bool poller::PollObject::isPollOut() const { return revents & POLLOUT; }
|
||||
bool poller::PollObject::isPollHup() const { return revents & POLLHUP; }
|
||||
poller::PollObject::~PollObject() = default;
|
||||
|
||||
poller::PollWrapper::PollWrapper() = default;
|
||||
|
||||
void poller::PollWrapper::loop(int timeoutMs) {
|
||||
if (this->objects.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// проверяем, что нет объектов с fd < 0, удаляем такие объекты если они есть
|
||||
for (size_t index = 0; index < this->objects.size();) {
|
||||
if (this->objects[index]->fd < 0) {
|
||||
this->objects.erase(this->objects.begin() + static_cast<ssize_t>(index));
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
const auto qsize = this->objects.size();
|
||||
|
||||
// массив для вызова poll
|
||||
pollfd pollFds[qsize];
|
||||
|
||||
// заполняем данные для poll
|
||||
for (size_t i = 0; i < qsize; i++) {
|
||||
pollFds[i].revents = 0;
|
||||
pollFds[i].fd = this->objects[i]->fd;
|
||||
pollFds[i].events = this->objects[i]->events;
|
||||
}
|
||||
|
||||
// выполняем poll
|
||||
poll(pollFds, qsize, timeoutMs);
|
||||
|
||||
// проверяем события
|
||||
for (size_t i = 0; i < qsize; i++) {
|
||||
objects[i]->revents = pollFds[i].revents;
|
||||
// if (pollFds[i].revents != 0) {
|
||||
// objects[i]->callback();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
poller::PollWrapper::~PollWrapper() = default;
|
||||
60
lib/port/unix/uart.cpp
Normal file
60
lib/port/unix/uart.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "port/uart.h"
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <asm/termbits.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <atomic>
|
||||
#include <span>
|
||||
|
||||
|
||||
drivers::UartDriver::UartDriver(const std::string& path, int baud) {
|
||||
fd = open(path.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
if (fd < 0) throw std::runtime_error(std::string("UartDriver open error ") + path + ": " + std::strerror(errno));
|
||||
|
||||
struct termios2 tio{};
|
||||
if (ioctl(fd, TCGETS2, &tio) != 0) {
|
||||
close(fd);
|
||||
throw std::runtime_error(std::string("UartDriver setup error: ") + std::strerror(errno));
|
||||
}
|
||||
// 8bit
|
||||
tio.c_cflag &= ~CSIZE;
|
||||
tio.c_cflag |= CS8;
|
||||
// baud rate
|
||||
tio.c_ispeed = baud;
|
||||
tio.c_ospeed = baud;
|
||||
// other
|
||||
tio.c_iflag |= (INPCK|IGNBRK|IGNCR|ISTRIP);
|
||||
tio.c_cflag &= ~CBAUD;
|
||||
tio.c_cflag |= (BOTHER|CREAD|CLOCAL);
|
||||
|
||||
if (ioctl(fd, TCSETS2, &tio) != 0) {
|
||||
close(fd);
|
||||
throw std::runtime_error(std::string("UartDriver setup error: ") + std::strerror(errno));
|
||||
}
|
||||
|
||||
events = POLLIN;
|
||||
}
|
||||
bool drivers::UartDriver::writeData(std::span<const uint8_t> data) {
|
||||
if (fd < 0) return false;
|
||||
auto w = write(fd, data.data(), data.size());
|
||||
return w == data.size();
|
||||
}
|
||||
size_t drivers::UartDriver::readChunk(std::vector<uint8_t>& out) {
|
||||
if (fd < 0) return 0;
|
||||
out.resize(1024);
|
||||
auto r = read(fd, out.data(), out.size());
|
||||
if (r <= 0) return 0;
|
||||
out.resize(r);
|
||||
return static_cast<size_t>(r);
|
||||
}
|
||||
drivers::UartDriver::~UartDriver() {
|
||||
close(fd);
|
||||
}
|
||||
55
lib/port/unix/udp.cpp
Normal file
55
lib/port/unix/udp.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "port/udp.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <poll.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "crsf.h"
|
||||
|
||||
|
||||
drivers::UdpDriver::UdpDriver(uint16_t port) {
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
throw std::runtime_error(std::string("UdpDriver open error: ") + std::strerror(errno));
|
||||
}
|
||||
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if (bind(fd, (sockaddr*)&addr, sizeof(addr)) < 0) if (fd < 0) {
|
||||
close(fd);
|
||||
throw std::runtime_error(std::string("UdpDriver bind error: ") + std::strerror(errno));
|
||||
}
|
||||
|
||||
events = POLLIN;
|
||||
}
|
||||
bool drivers::UdpDriver::sendTo(std::span<const uint8_t> data, const std::string& addr, uint16_t port) {
|
||||
if (fd < 0) return false;
|
||||
|
||||
sockaddr_in dst{};
|
||||
dst.sin_family = AF_INET;
|
||||
dst.sin_port = htons(port);
|
||||
inet_aton(addr.c_str(), &dst.sin_addr);
|
||||
|
||||
auto w = sendto(fd, data.data(), data.size(), 0, (sockaddr*)&dst, sizeof(dst));
|
||||
return w == data.size();
|
||||
}
|
||||
bool drivers::UdpDriver::recvPacket(std::vector<uint8_t>& out) {
|
||||
out.resize(crsf::CRSF_MAX_FRAME_SIZE);
|
||||
ssize_t received = recvfrom(fd, out.data(), out.size(), MSG_DONTWAIT, nullptr, nullptr);
|
||||
|
||||
if (received > 0) {
|
||||
out.resize(received);
|
||||
return true;
|
||||
}
|
||||
out.clear();
|
||||
|
||||
return false;
|
||||
}
|
||||
drivers::UdpDriver::~UdpDriver() {
|
||||
close(fd);
|
||||
}
|
||||
Reference in New Issue
Block a user