pip micro disable piintrospection piwaitevent

PIP_NO_SOCET
This commit is contained in:
2025-10-18 11:20:18 +03:00
parent 4885623492
commit cf89d77981
14 changed files with 506 additions and 459 deletions

View File

@@ -19,38 +19,37 @@
#include "piserial.h"
#include "piconfig.h"
#include "pidir.h"
#include "piincludes_p.h"
#include "pipropertystorage.h"
#include "pitime.h"
#include "pitranslator.h"
#include "piwaitevent_p.h"
#ifndef MICRO_PIP
#include <errno.h>
# include "piconfig.h"
# include "pidir.h"
# include "piincludes_p.h"
# include "pipropertystorage.h"
# include "pitime.h"
# include "pitranslator.h"
# include "piwaitevent_p.h"
#if defined(MICRO_PIP)
# define PISERIAL_NO_PINS
#endif
#if defined(PISERIAL_NO_PINS) || defined(WINDOWS)
# define TIOCM_LE 1
# define TIOCM_DTR 4
# define TIOCM_RTS 7
# define TIOCM_CTS 8
# define TIOCM_ST 3
# define TIOCM_SR 2
# define TIOCM_CAR 1
# define TIOCM_RNG 9
# define TIOCM_DSR 6
#endif
#ifdef WINDOWS
# ifndef INITGUID
# define INITGUID
# include <guiddef.h>
# undef INITGUID
# else
# include <guiddef.h>
# include <errno.h>
# if defined(PISERIAL_NO_PINS) || defined(WINDOWS)
# define TIOCM_LE 1
# define TIOCM_DTR 4
# define TIOCM_RTS 7
# define TIOCM_CTS 8
# define TIOCM_ST 3
# define TIOCM_SR 2
# define TIOCM_CAR 1
# define TIOCM_RNG 9
# define TIOCM_DSR 6
# endif
# ifdef WINDOWS
# ifndef INITGUID
# define INITGUID
# include <guiddef.h>
# undef INITGUID
# else
# include <guiddef.h>
# endif
// clang-format off
# include <ntddmodm.h>
# include <winreg.h>
@@ -59,89 +58,89 @@
# include <cfgmgr32.h>
# include <setupapi.h>
// clang-format on
# define B50 50
# define B75 75
# define B110 110
# define B300 300
# define B600 600
# define B1200 1200
# define B2400 2400
# define B4800 4800
# define B9600 9600
# define B14400 14400
# define B19200 19200
# define B38400 38400
# define B57600 57600
# define B115200 115200
# define B230400 230400
# define B460800 460800
# define B500000 500000
# define B576000 576000
# define B921600 921600
# define B1000000 1000000
# define B1152000 1152000
# define B1500000 1500000
# define B2000000 2000000
# define B2500000 2500000
# define B3000000 3000000
# define B3500000 3500000
# define B4000000 4000000
#else
# include <fcntl.h>
# include <sys/ioctl.h>
# include <termios.h>
# ifndef B50
# define B50 0000001
# define B50 50
# define B75 75
# define B110 110
# define B300 300
# define B600 600
# define B1200 1200
# define B2400 2400
# define B4800 4800
# define B9600 9600
# define B14400 14400
# define B19200 19200
# define B38400 38400
# define B57600 57600
# define B115200 115200
# define B230400 230400
# define B460800 460800
# define B500000 500000
# define B576000 576000
# define B921600 921600
# define B1000000 1000000
# define B1152000 1152000
# define B1500000 1500000
# define B2000000 2000000
# define B2500000 2500000
# define B3000000 3000000
# define B3500000 3500000
# define B4000000 4000000
# else
# include <fcntl.h>
# include <sys/ioctl.h>
# include <termios.h>
# ifndef B50
# define B50 0000001
# endif
# ifndef B75
# define B75 0000002
# endif
# ifndef B230400
# define B230400 0010003
# endif
# ifndef B460800
# define B460800 0010004
# endif
# ifndef B500000
# define B500000 0010005
# endif
# ifndef B576000
# define B576000 0010006
# endif
# ifndef B921600
# define B921600 0010007
# endif
# ifndef B1000000
# define B1000000 0010010
# endif
# ifndef B1152000
# define B1152000 0010011
# endif
# ifndef B1500000
# define B1500000 0010012
# endif
# ifndef B2000000
# define B2000000 0010013
# endif
# ifndef B2500000
# define B2500000 0010014
# endif
# ifndef B3000000
# define B3000000 0010015
# endif
# ifndef B3500000
# define B3500000 0010016
# endif
# ifndef B4000000
# define B4000000 0010017
# endif
# endif
# ifndef B75
# define B75 0000002
# ifndef CRTSCTS
# define CRTSCTS 020000000000
# endif
# ifndef B230400
# define B230400 0010003
# ifdef LINUX
# include <linux/serial.h>
# endif
# ifndef B460800
# define B460800 0010004
# endif
# ifndef B500000
# define B500000 0010005
# endif
# ifndef B576000
# define B576000 0010006
# endif
# ifndef B921600
# define B921600 0010007
# endif
# ifndef B1000000
# define B1000000 0010010
# endif
# ifndef B1152000
# define B1152000 0010011
# endif
# ifndef B1500000
# define B1500000 0010012
# endif
# ifndef B2000000
# define B2000000 0010013
# endif
# ifndef B2500000
# define B2500000 0010014
# endif
# ifndef B3000000
# define B3000000 0010015
# endif
# ifndef B3500000
# define B3500000 0010016
# endif
# ifndef B4000000
# define B4000000 0010017
# endif
#endif
#ifndef CRTSCTS
# define CRTSCTS 020000000000
#endif
#ifdef LINUX
# include <linux/serial.h>
#endif
//! \class PISerial piserial.h
@@ -177,16 +176,16 @@ REGISTER_DEVICE(PISerial)
PRIVATE_DEFINITION_START(PISerial)
PIWaitEvent event;
#ifdef WINDOWS
# ifdef WINDOWS
PIWaitEvent event_write;
DCB desc, sdesc;
HANDLE hCom = nullptr;
DWORD readed = 0, mask = 0;
OVERLAPPED overlap, overlap_write;
#else
# else
termios desc, sdesc;
uint readed = 0;
#endif
# endif
PRIVATE_DEFINITION_END(PISerial)
@@ -214,9 +213,9 @@ PISerial::~PISerial() {
stopAndWait();
close();
PRIVATE->event.destroy();
#ifdef WINDOWS
# ifdef WINDOWS
PRIVATE->event_write.destroy();
#endif
# endif
}
@@ -347,7 +346,7 @@ bool PISerial::setBreak(bool enabled) {
piCoutObj << "sendBreak error: \"" << path() << "\" is not opened!";
return false;
}
#ifdef WINDOWS
# ifdef WINDOWS
if (enabled) {
if (!SetCommBreak(PRIVATE->hCom)) {
piCoutObj << "setBreak error: " << errorString();
@@ -363,14 +362,14 @@ bool PISerial::setBreak(bool enabled) {
return true;
}
}
#else
# else
if (ioctl(fd, enabled ? TIOCSBRK : TIOCCBRK) < 0) {
piCoutObj << "setBreak error: " << errorString();
return false;
} else {
return true;
}
#endif
# endif
return false;
}
@@ -380,8 +379,8 @@ bool PISerial::setBit(int bit, bool on, const PIString & bname) {
piCoutObj << "setBit" << bname << " error: \"" << path() << "\" is not opened!";
return false;
}
#ifndef PISERIAL_NO_PINS
# ifdef WINDOWS
# ifndef PISERIAL_NO_PINS
# ifdef WINDOWS
static int bit_map_on[] = {0, 0, 0, 0, SETDTR, 0, 0, SETRTS, 0, 0, 0};
static int bit_map_off[] = {0, 0, 0, 0, CLRDTR, 0, 0, CLRRTS, 0, 0, 0};
int action = (on ? bit_map_on : bit_map_off)[bit];
@@ -392,14 +391,14 @@ bool PISerial::setBit(int bit, bool on, const PIString & bname) {
}
return true;
}
# else
# else
if (ioctl(fd, on ? TIOCMBIS : TIOCMBIC, &bit) < 0) {
piCoutObj << "setBit" << bname << " error: " << errorString();
return false;
}
return true;
# endif
# endif
#endif
piCoutObj << "setBit" << bname << " doesn`t implemented, sorry :-(";
return false;
}
@@ -410,23 +409,23 @@ bool PISerial::isBit(int bit, const PIString & bname) const {
piCoutObj << "isBit" << bname << " error: \"" << path() << "\" is not opened!";
return false;
}
#ifndef PISERIAL_NO_PINS
# ifdef WINDOWS
# else
# ifndef PISERIAL_NO_PINS
# ifdef WINDOWS
# else
int ret = 0;
if (ioctl(fd, TIOCMGET, &ret) < 0) piCoutObj << "isBit" << bname << " error: " << errorString();
return ret & bit;
# endif
# endif
#endif
piCoutObj << "isBit" << bname << " doesn`t implemented, sorry :-(";
return false;
}
void PISerial::flush() {
#ifndef WINDOWS
# ifndef WINDOWS
if (fd != -1) tcflush(fd, TCIOFLUSH);
#endif
# endif
}
@@ -441,9 +440,9 @@ int PISerial::convertSpeed(PISerial::Speed speed) {
case S2400: return B2400;
case S4800: return B4800;
case S9600: return B9600;
#ifdef WINDOWS
# ifdef WINDOWS
case S14400: return B14400;
#endif
# endif
case S19200: return B19200;
case S38400: return B38400;
case S57600: return B57600;
@@ -463,13 +462,13 @@ int PISerial::convertSpeed(PISerial::Speed speed) {
case S4000000: return B4000000;
default: break;
}
#ifdef WINDOWS
# ifdef WINDOWS
piCoutObj << "Warning: Custom speed %1"_tr("PISerial").arg((int)speed);
return (int)speed;
#else
# else
piCoutObj << "Warning: Unknown speed %1, using 115200"_tr("PISerial").arg((int)speed);
return B115200;
#endif
# endif
}
@@ -672,9 +671,9 @@ bool PISerial::send(const void * data, int size) {
void PISerial::interrupt() {
// piCoutObj << "interrupt";
PRIVATE->event.interrupt();
#ifdef WINDOWS
# ifdef WINDOWS
PRIVATE->event_write.interrupt();
#endif
# endif
}
@@ -696,7 +695,7 @@ bool PISerial::openDevice() {
}
}
if (p.isEmpty()) return false;
#ifdef WINDOWS
# ifdef WINDOWS
DWORD ds = 0, sm = 0;
if (isReadable()) {
ds |= GENERIC_READ;
@@ -714,7 +713,7 @@ bool PISerial::openDevice() {
return false;
}
fd = 0;
#else
# else
int om = 0;
switch (mode()) {
case PIIODevice::ReadOnly: om = O_RDONLY; break;
@@ -729,12 +728,12 @@ bool PISerial::openDevice() {
tcgetattr(fd, &PRIVATE->desc);
PRIVATE->sdesc = PRIVATE->desc;
// piCoutObj << "Initialized " << p;
#endif
# endif
applySettings();
PRIVATE->event.create();
#ifdef WINDOWS
# ifdef WINDOWS
PRIVATE->event_write.create();
#endif
# endif
return true;
}
@@ -745,28 +744,28 @@ bool PISerial::closeDevice() {
stopThreadedRead();
}
if (fd != -1) {
#ifdef WINDOWS
# ifdef WINDOWS
SetCommState(PRIVATE->hCom, &PRIVATE->sdesc);
SetCommMask(PRIVATE->hCom, PRIVATE->mask);
// piCoutObj << "close" <<
CloseHandle(PRIVATE->hCom);
PRIVATE->hCom = 0;
#else
# else
tcsetattr(fd, TCSANOW, &PRIVATE->sdesc);
::close(fd);
#endif
# endif
fd = -1;
}
PRIVATE->event.destroy();
#ifdef WINDOWS
# ifdef WINDOWS
PRIVATE->event_write.destroy();
#endif
# endif
return true;
}
void PISerial::applySettings() {
#ifdef WINDOWS
# ifdef WINDOWS
if (fd == -1) return;
setTimeouts();
GetCommMask(PRIVATE->hCom, &PRIVATE->mask);
@@ -792,7 +791,7 @@ void PISerial::applySettings() {
piCoutObj << "Unable to set comm state for \"%1\""_tr("PISerial").arg(path());
return;
}
#else
# else
if (fd == -1) return;
tcgetattr(fd, &PRIVATE->desc);
PRIVATE->desc.c_oflag = PRIVATE->desc.c_lflag = PRIVATE->desc.c_cflag = 0;
@@ -826,12 +825,12 @@ void PISerial::applySettings() {
piCoutObj << "Can`t set attributes for \"%1\""_tr("PISerial").arg(path());
return;
}
#endif
# endif
}
void PISerial::setTimeouts() {
#ifdef WINDOWS
# ifdef WINDOWS
COMMTIMEOUTS times;
if (isOptionSet(BlockingRead)) {
times.ReadIntervalTimeout = MAXDWORD;
@@ -845,9 +844,9 @@ void PISerial::setTimeouts() {
times.WriteTotalTimeoutConstant = isOptionSet(BlockingWrite) ? 0 : 1;
times.WriteTotalTimeoutMultiplier = 0;
if (SetCommTimeouts(PRIVATE->hCom, &times) == -1) piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
#else
# else
fcntl(fd, F_SETFL, isOptionSet(BlockingRead) ? 0 : O_NONBLOCK);
#endif
# endif
}
@@ -866,7 +865,7 @@ void PISerial::setTimeouts() {
//!
//! \~\sa \a readData(), \a readString()
ssize_t PISerial::readDevice(void * read_to, ssize_t max_size) {
#ifdef WINDOWS
# ifdef WINDOWS
if (!canRead()) return -1;
if (sending) return -1;
// piCoutObj << "read ..." << PRIVATE->hCom << max_size;
@@ -896,7 +895,7 @@ ssize_t PISerial::readDevice(void * read_to, ssize_t max_size) {
return -1;
// piCoutObj << "read" << (PRIVATE->readed) << errorString();
return PRIVATE->readed;
#else
# else
if (!canRead()) return -1;
if (isOptionSet(PIIODevice::BlockingRead)) {
if (!PRIVATE->event.wait(fd)) return -1;
@@ -911,7 +910,7 @@ ssize_t PISerial::readDevice(void * read_to, ssize_t max_size) {
}
}
return ret;
#endif
# endif
}
@@ -920,7 +919,7 @@ ssize_t PISerial::writeDevice(const void * data, ssize_t max_size) {
// piCoutObj << "Can`t write to uninitialized COM";
return -1;
}
#ifdef WINDOWS
# ifdef WINDOWS
DWORD wrote(0);
// piCoutObj << "send ..." << max_size;// << ": " << PIString((char*)data, max_size);
sending = true;
@@ -932,11 +931,11 @@ ssize_t PISerial::writeDevice(const void * data, ssize_t max_size) {
}
sending = false;
// piCoutObj << "send ok" << wrote;// << " bytes in " << path();
#else
# else
ssize_t wrote;
wrote = ::write(fd, data, max_size);
if (isOptionSet(BlockingWrite)) tcdrain(fd);
#endif
# endif
return (ssize_t)wrote;
// piCoutObj << "Error while sending";
}
@@ -1061,9 +1060,9 @@ void PISerial::configureFromVariantDevice(const PIPropertyStorage & d) {
PIVector<int> PISerial::availableSpeeds() {
PIVector<int> spds;
spds << 50 << 75 << 110 << 300 << 600 << 1200 << 2400 << 4800 << 9600 <<
#ifdef WINDOWS
# ifdef WINDOWS
14400 <<
#endif
# endif
19200 << 38400 << 57600 << 115200 << 230400 << 460800 << 500000 << 576000 << 921600 << 1000000 << 1152000 << 1500000 << 2000000
<< 2500000 << 3000000 << 3500000 << 4000000;
return spds;
@@ -1079,7 +1078,7 @@ PIStringList PISerial::availableDevices(bool test) {
}
#ifdef WINDOWS
# ifdef WINDOWS
PIString devicePortName(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInfoData) {
PIString ret;
const HKEY key = SetupDiOpenDevRegKey(deviceInfoSet, deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
@@ -1147,13 +1146,13 @@ bool parseID(PIString str, PISerial::DeviceInfo & di) {
if (i > 0) di.pID = str.mid(i + 4, 4).toInt(16);
return (di.vID > 0) && (di.pID > 0);
}
#endif
# endif
PIVector<PISerial::DeviceInfo> PISerial::availableDevicesInfo(bool test) {
PIVector<DeviceInfo> ret;
DeviceInfo di;
#ifdef WINDOWS
# ifdef WINDOWS
static const GUID guids[] = {GUID_DEVINTERFACE_MODEM, GUID_DEVINTERFACE_COMPORT};
static const int guids_cnt = sizeof(guids) / sizeof(GUID);
for (int i = 0; i < guids_cnt; ++i) {
@@ -1182,12 +1181,12 @@ PIVector<PISerial::DeviceInfo> PISerial::availableDevicesInfo(bool test) {
}
SetupDiDestroyDeviceInfoList(dis);
}
#else
# ifndef ANDROID
# else
# ifndef ANDROID
PIStringList prefixes;
# ifdef QNX
# ifdef QNX
prefixes << "ser";
# else
# else
prefixes << "ttyS"
<< "ttyO"
<< "ttyUSB"
@@ -1198,14 +1197,14 @@ PIVector<PISerial::DeviceInfo> PISerial::availableDevicesInfo(bool test) {
<< "ttyAMA"
<< "rfcomm"
<< "ircomm";
# ifdef FREE_BSD
# ifdef FREE_BSD
prefixes << "cu";
# endif
# ifdef MAC_OS
# endif
# ifdef MAC_OS
prefixes.clear();
prefixes << "cu."
<< "tty.";
# endif
# endif
PIFile file_prefixes("/proc/tty/drivers", PIIODevice::ReadOnly);
if (file_prefixes.open()) {
PIString fc = PIString::fromAscii(file_prefixes.readAll()), line, cpref;
@@ -1226,18 +1225,18 @@ PIVector<PISerial::DeviceInfo> PISerial::availableDevicesInfo(bool test) {
}
prefixes.removeDuplicates();
}
# endif
# endif
PIDir dir("/dev");
PIVector<PIFile::FileInfo> de = dir.entries();
# ifdef LINUX
# ifdef LINUX
char linkbuf[1024];
# endif
# endif
for (const auto & e: de) { // TODO changes in FileInfo
for (const auto & p: prefixes) {
if (e.name().startsWith(p)) {
di = DeviceInfo();
di.path = e.path;
# ifdef LINUX
di = DeviceInfo();
di.path = e.path;
# ifdef LINUX
ssize_t lsz = readlink(("/sys/class/tty/" + e.name()).dataAscii(), linkbuf, 1024);
if (lsz > 0) {
PIString fpath = "/sys/class/tty/" + PIString(linkbuf, lsz) + "/";
@@ -1253,16 +1252,16 @@ PIVector<PISerial::DeviceInfo> PISerial::availableDevicesInfo(bool test) {
if (di.pID > 0) break;
}
}
# endif
# endif
ret << di;
}
}
}
# endif
# endif
#endif
if (test) {
for (int i = 0; i < ret.size_s(); ++i) {
#ifdef WINDOWS
# ifdef WINDOWS
void * hComm = CreateFileA(ret[i].path.dataAscii(),
GENERIC_READ,
FILE_SHARE_READ,
@@ -1271,31 +1270,31 @@ PIVector<PISerial::DeviceInfo> PISerial::availableDevicesInfo(bool test) {
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE) {
#else
# else
int fd = ::open(ret[i].path.dataAscii(), O_NOCTTY | O_RDONLY);
if (fd == -1) {
#endif
# endif
ret.remove(i);
--i;
continue;
}
bool rok = true;
#ifndef WINDOWS
# ifndef WINDOWS
int void_ = 0;
fcntl(fd, F_SETFL, O_NONBLOCK);
if (::read(fd, &void_, 1) == -1) rok = errno != EIO;
#endif
# endif
if (!rok) {
ret.remove(i);
--i;
continue;
}
#ifdef WINDOWS
# ifdef WINDOWS
CloseHandle(hComm);
#else
# else
::close(fd);
#endif
# endif
}
}
return ret;
@@ -1309,12 +1308,14 @@ void PISerial::optionsChanged() {
void PISerial::threadedReadBufferSizeChanged() {
if (!isOpened()) return;
#if defined(LINUX)
# if defined(LINUX)
serial_struct ss;
ioctl(fd, TIOCGSERIAL, &ss);
// piCoutObj << "b" << ss.xmit_fifo_size;
ss.xmit_fifo_size = piMaxi(threadedReadBufferSize(), 4096);
ioctl(fd, TIOCSSERIAL, &ss);
// piCoutObj << "a" << ss.xmit_fifo_size;
#endif
# endif
}
#endif // MICRO_PIP