pip micro disable piintrospection piwaitevent
PIP_NO_SOCET
This commit is contained in:
@@ -18,9 +18,11 @@
|
||||
*/
|
||||
#include "pikbdlistener.h"
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "piliterals.h"
|
||||
#include "piwaitevent_p.h"
|
||||
#ifndef MICRO_PIP
|
||||
|
||||
# include "piincludes_p.h"
|
||||
# include "piliterals.h"
|
||||
# include "piwaitevent_p.h"
|
||||
// clang-format off
|
||||
#ifndef WINDOWS
|
||||
# include <termios.h>
|
||||
@@ -49,7 +51,7 @@ bool PIKbdListener::exiting;
|
||||
PIKbdListener * PIKbdListener::_object = 0;
|
||||
|
||||
|
||||
#ifndef WINDOWS
|
||||
# ifndef WINDOWS
|
||||
// unix
|
||||
const PIKbdListener::EscSeq PIKbdListener::esc_seq[] = {
|
||||
{"OA", PIKbdListener::UpArrow, 0, 0, 1},
|
||||
@@ -130,22 +132,22 @@ void setupTerminal(bool on) {
|
||||
printf(on ? "h" : "l");
|
||||
fflush(0);
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PIKbdListener)
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
void *hIn, *hOut;
|
||||
DWORD smode, tmode;
|
||||
CONSOLE_SCREEN_BUFFER_INFO sbi;
|
||||
#else
|
||||
# else
|
||||
struct termios sterm, tterm;
|
||||
#endif
|
||||
#ifdef WINDOWS
|
||||
# endif
|
||||
# ifdef WINDOWS
|
||||
DWORD
|
||||
#else
|
||||
# else
|
||||
int
|
||||
#endif
|
||||
# endif
|
||||
ret;
|
||||
PIWaitEvent event;
|
||||
PRIVATE_DEFINITION_END(PIKbdListener)
|
||||
@@ -154,13 +156,13 @@ PRIVATE_DEFINITION_END(PIKbdListener)
|
||||
PIKbdListener::PIKbdListener(KBFunc slot, void * _d, bool startNow): PIThread() {
|
||||
setName("keyboard_listener"_a);
|
||||
_object = this;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
PRIVATE->hIn = GetStdHandle(STD_INPUT_HANDLE);
|
||||
PRIVATE->hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
GetConsoleMode(PRIVATE->hIn, &PRIVATE->smode);
|
||||
#else
|
||||
# else
|
||||
tcgetattr(0, &PRIVATE->sterm);
|
||||
#endif
|
||||
# endif
|
||||
ret_func = slot;
|
||||
kbddata_ = _d;
|
||||
dbl_interval = 400;
|
||||
@@ -178,10 +180,10 @@ PIKbdListener::~PIKbdListener() {
|
||||
|
||||
|
||||
void PIKbdListener::begin() {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
GetConsoleMode(PRIVATE->hIn, &PRIVATE->tmode);
|
||||
SetConsoleMode(PRIVATE->hIn, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS);
|
||||
#else
|
||||
# else
|
||||
struct termios term;
|
||||
tcgetattr(0, &term);
|
||||
term.c_lflag &= ~(ECHO | ICANON);
|
||||
@@ -189,11 +191,11 @@ void PIKbdListener::begin() {
|
||||
PRIVATE->tterm = term;
|
||||
tcsetattr(0, TCSANOW, &term);
|
||||
setupTerminal(true);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
PIKbdListener::KeyModifiers getModifiers(DWORD v, bool * shift = 0) {
|
||||
PIKbdListener::KeyModifiers ret;
|
||||
bool ctrl = v & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED);
|
||||
@@ -214,7 +216,7 @@ PIKbdListener::MouseButtons getButtons(DWORD v) {
|
||||
if (v & FROM_LEFT_2ND_BUTTON_PRESSED) ret |= PIKbdListener::MouseMiddle;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
|
||||
void PIKbdListener::readKeyboard() {
|
||||
@@ -222,7 +224,7 @@ void PIKbdListener::readKeyboard() {
|
||||
ke.modifiers = 0;
|
||||
char rc[8];
|
||||
piZeroMemory(rc, 8);
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
INPUT_RECORD ir;
|
||||
ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret));
|
||||
switch (ir.EventType) {
|
||||
@@ -406,7 +408,7 @@ void PIKbdListener::readKeyboard() {
|
||||
} break;
|
||||
default: piMSleep(10); return;
|
||||
}
|
||||
#else
|
||||
# else
|
||||
tcsetattr(0, TCSANOW, &PRIVATE->tterm);
|
||||
if (!PRIVATE->event.wait(0)) return;
|
||||
PRIVATE->ret = read(0, rc, 8);
|
||||
@@ -533,7 +535,7 @@ void PIKbdListener::readKeyboard() {
|
||||
cout << endl;*/
|
||||
}
|
||||
if (ke.key == 0 && PRIVATE->ret > 1) ke.key = PIChar::fromSystem(rc).unicode16Code();
|
||||
#endif
|
||||
# endif
|
||||
if ((rc[0] == '\n' || rc[0] == '\r') && PRIVATE->ret == 1) ke.key = Return;
|
||||
if (exit_enabled && ke.key == exit_key) {
|
||||
PIKbdListener::exiting = true;
|
||||
@@ -560,30 +562,32 @@ bool PIKbdListener::stopAndWait(PISystemTime timeout) {
|
||||
|
||||
void PIKbdListener::end() {
|
||||
// cout << "list end" << endl;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
SetConsoleMode(PRIVATE->hIn, PRIVATE->smode);
|
||||
#else
|
||||
# else
|
||||
tcsetattr(0, TCSANOW, &PRIVATE->sterm);
|
||||
setupTerminal(false);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void PIKbdListener::setActive(bool yes) {
|
||||
is_active = yes;
|
||||
if (is_active) {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
SetConsoleMode(PRIVATE->hIn, PRIVATE->tmode);
|
||||
#else
|
||||
# else
|
||||
tcsetattr(0, TCSANOW, &PRIVATE->tterm);
|
||||
setupTerminal(true);
|
||||
#endif
|
||||
# endif
|
||||
} else {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
SetConsoleMode(PRIVATE->hIn, PRIVATE->smode);
|
||||
#else
|
||||
# else
|
||||
tcsetattr(0, TCSANOW, &PRIVATE->sterm);
|
||||
setupTerminal(false);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif // MICRO_PIP
|
||||
|
||||
@@ -26,15 +26,19 @@
|
||||
#ifndef PIKBDLISTENER_H
|
||||
#define PIKBDLISTENER_H
|
||||
|
||||
#include "pithread.h"
|
||||
#include "pitime.h"
|
||||
#include "pibase.h"
|
||||
|
||||
#define WAIT_FOR_EXIT \
|
||||
while (!PIKbdListener::exiting) \
|
||||
piMSleep(PIP_MIN_MSLEEP * 5); \
|
||||
if (PIKbdListener::instance()) { \
|
||||
if (!PIKbdListener::instance()->stopAndWait(PISystemTime::fromSeconds(1))) PIKbdListener::instance()->terminate(); \
|
||||
}
|
||||
#ifndef MICRO_PIP
|
||||
|
||||
# include "pithread.h"
|
||||
# include "pitime.h"
|
||||
|
||||
# define WAIT_FOR_EXIT \
|
||||
while (!PIKbdListener::exiting) \
|
||||
piMSleep(PIP_MIN_MSLEEP * 5); \
|
||||
if (PIKbdListener::instance()) { \
|
||||
if (!PIKbdListener::instance()->stopAndWait(PISystemTime::fromSeconds(1))) PIKbdListener::instance()->terminate(); \
|
||||
}
|
||||
|
||||
|
||||
class PIP_EXPORT PIKbdListener: public PIThread {
|
||||
@@ -231,7 +235,7 @@ private:
|
||||
void run() override { readKeyboard(); }
|
||||
void end() override;
|
||||
|
||||
#ifndef WINDOWS
|
||||
# ifndef WINDOWS
|
||||
struct PIP_EXPORT EscSeq {
|
||||
const char * seq;
|
||||
int key;
|
||||
@@ -251,7 +255,7 @@ private:
|
||||
};
|
||||
|
||||
static const EscSeq esc_seq[];
|
||||
#endif
|
||||
# endif
|
||||
|
||||
PRIVATE_DECLARATION(PIP_EXPORT)
|
||||
KBFunc ret_func;
|
||||
@@ -305,4 +309,5 @@ REGISTER_PIVARIANTSIMPLE(PIKbdListener::KeyEvent)
|
||||
REGISTER_PIVARIANTSIMPLE(PIKbdListener::MouseEvent)
|
||||
REGISTER_PIVARIANTSIMPLE(PIKbdListener::WheelEvent)
|
||||
|
||||
#endif // MICRO_PIP
|
||||
#endif // PIKBDLISTENER_H
|
||||
|
||||
@@ -18,18 +18,19 @@
|
||||
*/
|
||||
|
||||
#include "piwaitevent_p.h"
|
||||
#ifdef WINDOWS
|
||||
#ifndef MICRO_PIP
|
||||
# ifdef WINDOWS
|
||||
// # ifdef _WIN32_WINNT
|
||||
// # undef _WIN32_WINNT
|
||||
// # define _WIN32_WINNT 0x0600
|
||||
// # endif
|
||||
# include <synchapi.h>
|
||||
#else
|
||||
# include <errno.h>
|
||||
# include <fcntl.h>
|
||||
# include <sys/ioctl.h>
|
||||
#endif
|
||||
#include "pistring.h"
|
||||
# include <synchapi.h>
|
||||
# else
|
||||
# include <errno.h>
|
||||
# include <fcntl.h>
|
||||
# include <sys/ioctl.h>
|
||||
# endif
|
||||
# include "pistring.h"
|
||||
|
||||
|
||||
PIWaitEvent::~PIWaitEvent() {
|
||||
@@ -39,12 +40,12 @@ PIWaitEvent::~PIWaitEvent() {
|
||||
|
||||
void PIWaitEvent::create() {
|
||||
destroy();
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
event = CreateEventA(NULL, TRUE, FALSE, NULL);
|
||||
if (!event) {
|
||||
piCout << "Error with CreateEventA:" << errorString();
|
||||
}
|
||||
#else
|
||||
# else
|
||||
for (int i = 0; i < 3; ++i)
|
||||
piZeroMemory(fds[i]);
|
||||
if (::pipe(pipe_fd) < 0) {
|
||||
@@ -53,34 +54,34 @@ void PIWaitEvent::create() {
|
||||
fcntl(pipe_fd[ReadEnd], F_SETFL, O_NONBLOCK);
|
||||
fcntl(pipe_fd[WriteEnd], F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void PIWaitEvent::destroy() {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
if (event) {
|
||||
CloseHandle(event);
|
||||
event = NULL;
|
||||
}
|
||||
#else
|
||||
# else
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
if (pipe_fd[i] != 0) {
|
||||
::close(pipe_fd[i]);
|
||||
pipe_fd[i] = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
bool PIWaitEvent::wait(int fd, CheckRole role) {
|
||||
if (!isCreate()) return false;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
DWORD ret = WaitForSingleObjectEx(event, INFINITE, TRUE);
|
||||
ResetEvent(event);
|
||||
if (ret == WAIT_IO_COMPLETION || ret == WAIT_FAILED) return false;
|
||||
#else
|
||||
# else
|
||||
if (fd == -1) return false;
|
||||
int nfds = piMaxi(pipe_fd[ReadEnd], fd) + 1;
|
||||
int fd_index = role;
|
||||
@@ -97,18 +98,18 @@ bool PIWaitEvent::wait(int fd, CheckRole role) {
|
||||
if (sr == EBADF || sr == EINTR) return false;
|
||||
if (FD_ISSET(fd, &(fds[CheckExeption]))) return true;
|
||||
return FD_ISSET(fd, &(fds[fd_index]));
|
||||
#endif
|
||||
# endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool PIWaitEvent::sleep(int us) {
|
||||
if (!isCreate()) return false;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
DWORD ret = WaitForSingleObjectEx(event, us / 1000, TRUE);
|
||||
ResetEvent(event);
|
||||
return ret == WAIT_TIMEOUT;
|
||||
#else
|
||||
# else
|
||||
int nfds = pipe_fd[ReadEnd] + 1;
|
||||
FD_ZERO(&(fds[CheckRead]));
|
||||
FD_SET(pipe_fd[ReadEnd], &(fds[CheckRead]));
|
||||
@@ -120,34 +121,36 @@ bool PIWaitEvent::sleep(int us) {
|
||||
while (::read(pipe_fd[ReadEnd], &buf, sizeof(buf)) > 0)
|
||||
;
|
||||
return ret == 0;
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void PIWaitEvent::interrupt() {
|
||||
if (!isCreate()) return;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
SetEvent(event);
|
||||
#else
|
||||
# else
|
||||
auto _r = ::write(pipe_fd[WriteEnd], "", 1);
|
||||
NO_UNUSED(_r);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
bool PIWaitEvent::isCreate() const {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
return event;
|
||||
#else
|
||||
# else
|
||||
return pipe_fd[ReadEnd] != 0;
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void * PIWaitEvent::getEvent() const {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
return event;
|
||||
#else
|
||||
# else
|
||||
return nullptr;
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
#endif // MICRO_PIP
|
||||
|
||||
@@ -20,7 +20,9 @@
|
||||
#ifndef PIWAITEVENT_P_H
|
||||
#define PIWAITEVENT_P_H
|
||||
|
||||
#include "pibase.h"
|
||||
#ifndef MICRO_PIP
|
||||
|
||||
# include "pibase.h"
|
||||
// clang-format off
|
||||
#ifdef WINDOWS
|
||||
# include <stdarg.h>
|
||||
@@ -52,17 +54,18 @@ public:
|
||||
void * getEvent() const; // WINDOWS only
|
||||
|
||||
private:
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
void * event = nullptr;
|
||||
#else
|
||||
# else
|
||||
int pipe_fd[2] = {0, 0};
|
||||
fd_set fds[3];
|
||||
enum {
|
||||
ReadEnd = 0,
|
||||
WriteEnd = 1
|
||||
};
|
||||
#endif
|
||||
# endif
|
||||
};
|
||||
|
||||
|
||||
#endif // MICRO_PIP
|
||||
#endif // PIWAITEVENT_P_H
|
||||
|
||||
@@ -19,10 +19,12 @@
|
||||
|
||||
#include "piintrospection_server_p.h"
|
||||
|
||||
#include "pichunkstream.h"
|
||||
#include "piinit.h"
|
||||
#include "piobject.h"
|
||||
#include "pisysteminfo.h"
|
||||
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
|
||||
# include "pichunkstream.h"
|
||||
# include "piinit.h"
|
||||
# include "piobject.h"
|
||||
# include "pisysteminfo.h"
|
||||
|
||||
|
||||
const uint PIIntrospection::sign = 0x0F1C2B3A;
|
||||
@@ -111,9 +113,9 @@ PIByteArray PIIntrospection::packContainers() {
|
||||
PIByteArray ret;
|
||||
PIVector<PIIntrospectionContainers::TypeInfo> data;
|
||||
PIIntrospectionContainers * p = 0;
|
||||
#ifdef PIP_INTROSPECTION
|
||||
# ifdef PIP_INTROSPECTION
|
||||
p = PIINTROSPECTION_CONTAINERS->p;
|
||||
#endif
|
||||
# endif
|
||||
if (p) {
|
||||
data = p->getInfo();
|
||||
}
|
||||
@@ -131,9 +133,9 @@ void PIIntrospection::unpackContainers(PIByteArray & ba, PIVector<PIIntrospectio
|
||||
PIByteArray PIIntrospection::packThreads() {
|
||||
PIByteArray ret;
|
||||
PIIntrospectionThreads * p = 0;
|
||||
#ifdef PIP_INTROSPECTION
|
||||
# ifdef PIP_INTROSPECTION
|
||||
p = PIINTROSPECTION_THREADS->p;
|
||||
#endif
|
||||
# endif
|
||||
if (p) {
|
||||
p->mutex.lock();
|
||||
PIMap<PIThread *, PIIntrospectionThreads::ThreadInfo> & tm(p->threads);
|
||||
@@ -170,3 +172,5 @@ void PIIntrospection::unpackObjects(PIByteArray & ba, PIVector<PIIntrospection::
|
||||
objects.clear();
|
||||
ba >> objects;
|
||||
}
|
||||
|
||||
#endif // #if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "piintrospection_threads_p.h"
|
||||
#include "pisystemmonitor.h"
|
||||
|
||||
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
|
||||
class PIP_EXPORT PIIntrospection {
|
||||
public:
|
||||
@@ -168,4 +169,5 @@ BINARY_STREAM_READ(PIIntrospection::ObjectInfo) {
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // #if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
#endif // PIINTROSPECTION_SERVER_P_H
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "piintrospection_threads_p.h"
|
||||
|
||||
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
|
||||
PIIntrospectionThreads::ThreadInfo::ThreadInfo() {
|
||||
id = delay = 0;
|
||||
@@ -78,3 +79,5 @@ void PIIntrospectionThreads::threadRunDone(PIThread * t, ullong us) {
|
||||
ThreadInfo & ti(threads[t]);
|
||||
ti.run_us = (ti.run_us * 0.8) + (us * 0.2); /// WARNING
|
||||
}
|
||||
|
||||
#endif // #if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
|
||||
@@ -20,6 +20,10 @@
|
||||
#ifndef PIINTROSPECTION_THREADS_P_H
|
||||
#define PIINTROSPECTION_THREADS_P_H
|
||||
|
||||
#include "pibase.h"
|
||||
|
||||
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
|
||||
#include "pimap.h"
|
||||
#include "pithread.h"
|
||||
|
||||
@@ -68,4 +72,5 @@ BINARY_STREAM_READ(PIIntrospectionThreads::ThreadInfo) {
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // #if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
|
||||
#endif // PIINTROSPECTION_THREADS_P_H
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
#include "pipropertystorage.h"
|
||||
#include "piwaitevent_p.h"
|
||||
#if !defined(WINDOWS) && !defined(MAC_OS) && !defined(MICRO_PIP)
|
||||
#if !defined(WINDOWS) && !defined(MAC_OS) && !defined(PIP_NO_SOCKET)
|
||||
# define PIP_CAN
|
||||
#endif
|
||||
#ifdef PIP_CAN
|
||||
@@ -39,25 +39,29 @@
|
||||
|
||||
REGISTER_DEVICE(PICAN)
|
||||
|
||||
|
||||
#ifdef PIP_CAN
|
||||
PRIVATE_DEFINITION_START(PICAN)
|
||||
PIWaitEvent event;
|
||||
PRIVATE_DEFINITION_END(PICAN)
|
||||
|
||||
#endif
|
||||
|
||||
PICAN::PICAN(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode) {
|
||||
setThreadedReadBufferSize(256);
|
||||
setPath(path);
|
||||
#ifdef PIP_CAN
|
||||
can_id = 0;
|
||||
sock = 0;
|
||||
PRIVATE->event.create();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
PICAN::~PICAN() {
|
||||
stopAndWait();
|
||||
close();
|
||||
#ifdef PIP_CAN
|
||||
PRIVATE->event.destroy();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -164,7 +168,9 @@ int PICAN::readedCANID() const {
|
||||
|
||||
|
||||
void PICAN::interrupt() {
|
||||
#ifdef PIP_CAN
|
||||
PRIVATE->event.interrupt();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,64 +18,65 @@
|
||||
*/
|
||||
#include "piethernet.h"
|
||||
|
||||
#include "piconfig.h"
|
||||
#include "piconstchars.h"
|
||||
#include "piincludes_p.h"
|
||||
#include "piliterals.h"
|
||||
#include "pipropertystorage.h"
|
||||
#include "pisysteminfo.h"
|
||||
#include "pitranslator.h"
|
||||
// clang-format off
|
||||
#ifdef QNX
|
||||
# include <arpa/inet.h>
|
||||
# include <fcntl.h>
|
||||
# include <hw/nicinfo.h>
|
||||
# include <ifaddrs.h>
|
||||
# include <net/if.h>
|
||||
# include <net/if_dl.h>
|
||||
# include <netdb.h>
|
||||
# include <netinet/in.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <sys/socket.h>
|
||||
# include <sys/time.h>
|
||||
# include <sys/types.h>
|
||||
# ifdef BLACKBERRY
|
||||
# include <netinet/in.h>
|
||||
# else
|
||||
# include <sys/dcmd_io-net.h>
|
||||
# endif
|
||||
# define ip_mreqn ip_mreq
|
||||
# define imr_address imr_interface
|
||||
#else
|
||||
# ifdef WINDOWS
|
||||
# include <io.h>
|
||||
# include <winsock2.h>
|
||||
# include <iphlpapi.h>
|
||||
# include <psapi.h>
|
||||
# include <ws2tcpip.h>
|
||||
# define ip_mreqn ip_mreq
|
||||
# define imr_address imr_interface
|
||||
# else
|
||||
# include <fcntl.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <netinet/in.h>
|
||||
# include <netinet/tcp.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/socket.h>
|
||||
# include <netdb.h>
|
||||
# include <net/if.h>
|
||||
# if !defined(ANDROID) && !defined(LWIP)
|
||||
# include <ifaddrs.h>
|
||||
# endif
|
||||
# ifdef LWIP
|
||||
# include <lwip/sockets.h>
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
// clang-format on
|
||||
#include "piwaitevent_p.h"
|
||||
#ifndef PIP_NO_SOCKET
|
||||
|
||||
#include <errno.h>
|
||||
# include "piconfig.h"
|
||||
# include "piconstchars.h"
|
||||
# include "piincludes_p.h"
|
||||
# include "piliterals.h"
|
||||
# include "pipropertystorage.h"
|
||||
# include "pisysteminfo.h"
|
||||
# include "pitranslator.h"
|
||||
|
||||
# ifdef QNX
|
||||
# include <arpa/inet.h>
|
||||
# include <fcntl.h>
|
||||
# include <hw/nicinfo.h>
|
||||
# include <ifaddrs.h>
|
||||
# include <net/if.h>
|
||||
# include <net/if_dl.h>
|
||||
# include <netdb.h>
|
||||
# include <netinet/in.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <sys/socket.h>
|
||||
# include <sys/time.h>
|
||||
# include <sys/types.h>
|
||||
# ifdef BLACKBERRY
|
||||
# include <netinet/in.h>
|
||||
# else
|
||||
# include <sys/dcmd_io-net.h>
|
||||
# endif
|
||||
# define ip_mreqn ip_mreq
|
||||
# define imr_address imr_interface
|
||||
# else
|
||||
# ifdef WINDOWS
|
||||
# include <io.h>
|
||||
# include <iphlpapi.h>
|
||||
# include <psapi.h>
|
||||
# include <winsock2.h>
|
||||
# include <ws2tcpip.h>
|
||||
# define ip_mreqn ip_mreq
|
||||
# define imr_address imr_interface
|
||||
# else
|
||||
# include <arpa/inet.h>
|
||||
# include <fcntl.h>
|
||||
# include <net/if.h>
|
||||
# include <netdb.h>
|
||||
# include <netinet/in.h>
|
||||
# include <netinet/tcp.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <sys/socket.h>
|
||||
# if !defined(ANDROID) && !defined(LWIP)
|
||||
# include <ifaddrs.h>
|
||||
# endif
|
||||
# ifdef LWIP
|
||||
# include <lwip/sockets.h>
|
||||
# endif
|
||||
# endif
|
||||
# endif
|
||||
# include "piwaitevent_p.h"
|
||||
|
||||
# include <errno.h>
|
||||
|
||||
|
||||
/** \class PIEthernet piethernet.h
|
||||
@@ -100,11 +101,11 @@
|
||||
*
|
||||
* */
|
||||
|
||||
#ifndef WINDOWS
|
||||
# ifndef WINDOWS
|
||||
PIString getSockAddr(sockaddr * s) {
|
||||
return s == 0 ? PIString() : PIStringAscii(inet_ntoa(((sockaddr_in *)s)->sin_addr));
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
|
||||
REGISTER_DEVICE(PIEthernet)
|
||||
@@ -196,11 +197,11 @@ void PIEthernet::construct() {
|
||||
setMulticastTTL(1);
|
||||
server_thread_.setData(this);
|
||||
server_thread_.setName("_S.tcpserver"_a);
|
||||
#ifdef MICRO_PIP
|
||||
# ifdef LWIP
|
||||
setThreadedReadBufferSize(512);
|
||||
#else
|
||||
# else
|
||||
setThreadedReadBufferSize(64_KiB);
|
||||
#endif
|
||||
# endif
|
||||
// setPriority(piHigh);
|
||||
}
|
||||
|
||||
@@ -304,9 +305,9 @@ bool PIEthernet::openDevice() {
|
||||
PRIVATE->addr_.sin_addr.s_addr = INADDR_ANY;
|
||||
else
|
||||
PRIVATE->addr_.sin_addr.s_addr = addr_r.ip();
|
||||
#ifdef QNX
|
||||
# ifdef QNX
|
||||
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
||||
#endif
|
||||
# endif
|
||||
// piCout << "bind to" << (params[PIEthernet::Broadcast] ? "255.255.255.255" : ip_) << ":" << port_ << " ...";
|
||||
int tries = 0;
|
||||
while ((bind(sock, (sockaddr *)&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == -1) && (tries < 2)) {
|
||||
@@ -379,14 +380,14 @@ void PIEthernet::applyBuffers() {
|
||||
|
||||
void PIEthernet::applyTimeout(int fd, int opt, PISystemTime tm) {
|
||||
if (fd == 0) return;
|
||||
// piCoutObj << "setReadIsBlocking" << yes;
|
||||
#ifdef WINDOWS
|
||||
// piCoutObj << "setReadIsBlocking" << yes;
|
||||
# ifdef WINDOWS
|
||||
DWORD _tm = tm.toMilliseconds();
|
||||
#else
|
||||
# else
|
||||
timeval _tm;
|
||||
_tm.tv_sec = tm.seconds;
|
||||
_tm.tv_usec = tm.nanoseconds / 1000;
|
||||
#endif
|
||||
# endif
|
||||
ethSetsockopt(fd, SOL_SOCKET, opt, &_tm, sizeof(_tm));
|
||||
}
|
||||
|
||||
@@ -411,30 +412,30 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
return true;
|
||||
}
|
||||
addr_r.set(path());
|
||||
#ifndef LWIP
|
||||
# ifndef LWIP
|
||||
struct ip_mreqn mreq;
|
||||
#else
|
||||
# else
|
||||
struct ip_mreq mreq;
|
||||
#endif
|
||||
# endif
|
||||
piZeroMemory(mreq);
|
||||
#ifdef LINUX
|
||||
# ifdef LINUX
|
||||
// mreq.imr_address.s_addr = INADDR_ANY;
|
||||
/*PIEthernet::InterfaceList il = interfaces();
|
||||
const PIEthernet::Interface * ci = il.getByAddress(addr_r.ipString());
|
||||
if (ci != 0) mreq.imr_ifindex = ci->index;*/
|
||||
#endif
|
||||
# endif
|
||||
if (params[PIEthernet::Broadcast])
|
||||
#ifndef LWIP
|
||||
# ifndef LWIP
|
||||
mreq.imr_address.s_addr = INADDR_ANY;
|
||||
#else
|
||||
# else
|
||||
mreq.imr_interface.s_addr = INADDR_ANY;
|
||||
#endif
|
||||
# endif
|
||||
else
|
||||
#ifndef LWIP
|
||||
# ifndef LWIP
|
||||
mreq.imr_address.s_addr = addr_r.ip();
|
||||
#else
|
||||
# else
|
||||
mreq.imr_interface.s_addr = addr_r.ip();
|
||||
#endif
|
||||
# endif
|
||||
|
||||
// piCout << "join group" << group << "ip" << ip_ << "with index" << mreq.imr_ifindex << "socket" << sock;
|
||||
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
||||
@@ -457,24 +458,24 @@ bool PIEthernet::leaveMulticastGroup(const PIString & group) {
|
||||
return false;
|
||||
}
|
||||
addr_r.set(path());
|
||||
#ifndef LWIP
|
||||
# ifndef LWIP
|
||||
struct ip_mreqn mreq;
|
||||
#else
|
||||
# else
|
||||
struct ip_mreq mreq;
|
||||
#endif
|
||||
# endif
|
||||
piZeroMemory(mreq);
|
||||
if (params[PIEthernet::Broadcast])
|
||||
#ifndef LWIP
|
||||
# ifndef LWIP
|
||||
mreq.imr_address.s_addr = INADDR_ANY;
|
||||
#else
|
||||
# else
|
||||
mreq.imr_interface.s_addr = INADDR_ANY;
|
||||
#endif
|
||||
# endif
|
||||
else
|
||||
#ifndef LWIP
|
||||
# ifndef LWIP
|
||||
mreq.imr_address.s_addr = addr_r.ip();
|
||||
#else
|
||||
# else
|
||||
mreq.imr_interface.s_addr = addr_r.ip();
|
||||
#endif
|
||||
# endif
|
||||
mreq.imr_multiaddr.s_addr = inet_addr(group.dataAscii());
|
||||
if (ethSetsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) {
|
||||
piCoutObj << "Can`t leave multicast group" << group << "," << ethErrorString();
|
||||
@@ -498,9 +499,9 @@ bool PIEthernet::connect(bool threaded) {
|
||||
PRIVATE->addr_.sin_port = htons(addr_r.port());
|
||||
PRIVATE->addr_.sin_addr.s_addr = addr_r.ip();
|
||||
PRIVATE->addr_.sin_family = AF_INET;
|
||||
#ifdef QNX
|
||||
# ifdef QNX
|
||||
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
||||
#endif
|
||||
# endif
|
||||
connecting_ = true;
|
||||
connected_ = connectTCP();
|
||||
connecting_ = false;
|
||||
@@ -535,9 +536,9 @@ bool PIEthernet::listen(bool threaded) {
|
||||
PRIVATE->addr_.sin_port = htons(addr_r.port());
|
||||
PRIVATE->addr_.sin_addr.s_addr = addr_r.ip();
|
||||
PRIVATE->addr_.sin_family = AF_INET;
|
||||
#ifdef QNX
|
||||
# ifdef QNX
|
||||
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
||||
#endif
|
||||
# endif
|
||||
opened_ = false;
|
||||
int tries = 0;
|
||||
while ((bind(sock, (sockaddr *)&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == -1) && (tries < 2)) {
|
||||
@@ -661,9 +662,9 @@ ssize_t PIEthernet::readDevice(void * read_to, ssize_t max_size) {
|
||||
PRIVATE->addr_.sin_port = htons(addr_r.port());
|
||||
PRIVATE->addr_.sin_addr.s_addr = addr_r.ip();
|
||||
PRIVATE->addr_.sin_family = AF_INET;
|
||||
#ifdef QNX
|
||||
# ifdef QNX
|
||||
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
||||
#endif
|
||||
# endif
|
||||
// piCoutObj << "connect to " << path() << "...";
|
||||
connected_ = connectTCP();
|
||||
// piCoutObj << "connect to " << path() << connected_;
|
||||
@@ -678,7 +679,7 @@ ssize_t PIEthernet::readDevice(void * read_to, ssize_t max_size) {
|
||||
}
|
||||
if (!connected_) return -1;
|
||||
errorClear();
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
{
|
||||
long wr = waitForEvent(PRIVATE->event, FD_READ | FD_CLOSE);
|
||||
switch (wr) {
|
||||
@@ -694,34 +695,34 @@ ssize_t PIEthernet::readDevice(void * read_to, ssize_t max_size) {
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
#else
|
||||
# else
|
||||
if (PRIVATE->event.wait(sock)) {
|
||||
errorClear();
|
||||
rs = ethRecv(sock, read_to, max_size);
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
// piCoutObj << "readed" << rs;
|
||||
if (rs <= 0) {
|
||||
lerr = ethErrorCore();
|
||||
// piCoutObj << "readed" << rs << "error" << lerr;
|
||||
|
||||
// async normal returns
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
if (lerr == WSAEWOULDBLOCK) {
|
||||
#else
|
||||
# else
|
||||
if (lerr == EWOULDBLOCK || lerr == EAGAIN || lerr == EINTR) {
|
||||
#endif
|
||||
# endif
|
||||
// piCoutObj << "Ignore would_block" << lerr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// if no disconnect on timeout
|
||||
if (!params[DisonnectOnTimeout]) {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
if (lerr == WSAETIMEDOUT) {
|
||||
#else
|
||||
# else
|
||||
if (lerr == ETIMEDOUT) {
|
||||
#endif
|
||||
# endif
|
||||
// piCoutObj << "Ignore read timeout";
|
||||
return -1;
|
||||
}
|
||||
@@ -745,7 +746,7 @@ ssize_t PIEthernet::readDevice(void * read_to, ssize_t max_size) {
|
||||
case UDP: {
|
||||
piZeroMemory(PRIVATE->raddr_);
|
||||
// piCoutObj << "read from" << path() << "...";
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
long wr = waitForEvent(PRIVATE->event, FD_READ | FD_CLOSE);
|
||||
switch (wr) {
|
||||
case FD_READ:
|
||||
@@ -758,9 +759,9 @@ ssize_t PIEthernet::readDevice(void * read_to, ssize_t max_size) {
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
#else
|
||||
# else
|
||||
rs = ethRecvfrom(sock, read_to, max_size, 0, (sockaddr *)&PRIVATE->raddr_);
|
||||
#endif
|
||||
# endif
|
||||
// piCoutObj << "read from" << path() << rs << "bytes";
|
||||
if (rs > 0) {
|
||||
addr_lr.set(uint(PRIVATE->raddr_.sin_addr.s_addr), ntohs(PRIVATE->raddr_.sin_port));
|
||||
@@ -793,11 +794,11 @@ ssize_t PIEthernet::writeDevice(const void * data, ssize_t max_size) {
|
||||
return ethSendto(sock_s,
|
||||
data,
|
||||
max_size,
|
||||
#ifndef WINDOWS
|
||||
# ifndef WINDOWS
|
||||
isOptionSet(BlockingWrite) ? 0 : MSG_DONTWAIT
|
||||
#else
|
||||
# else
|
||||
0
|
||||
#endif
|
||||
# endif
|
||||
,
|
||||
(sockaddr *)&PRIVATE->saddr_,
|
||||
sizeof(PRIVATE->saddr_));
|
||||
@@ -809,9 +810,9 @@ ssize_t PIEthernet::writeDevice(const void * data, ssize_t max_size) {
|
||||
PRIVATE->addr_.sin_port = htons(addr_r.port());
|
||||
PRIVATE->addr_.sin_addr.s_addr = addr_r.ip();
|
||||
PRIVATE->addr_.sin_family = AF_INET;
|
||||
#ifdef QNX
|
||||
# ifdef QNX
|
||||
PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_);
|
||||
#endif
|
||||
# endif
|
||||
// piCoutObj << "connect to " << ip << ":" << port_;
|
||||
connected_ = connectTCP();
|
||||
if (!connected_) piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString();
|
||||
@@ -847,11 +848,11 @@ ssize_t PIEthernet::writeDevice(const void * data, ssize_t max_size) {
|
||||
int sr = ::send(sock, remain_data, remain_size, 0);
|
||||
if (sr < 0) {
|
||||
int err = ethErrorCore();
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
if (err == WSAEWOULDBLOCK) {
|
||||
#else
|
||||
# else
|
||||
if (err == EAGAIN || err == EWOULDBLOCK) {
|
||||
#endif
|
||||
# endif
|
||||
piMinSleep();
|
||||
// piCoutObj << "wait for write";
|
||||
continue;
|
||||
@@ -910,30 +911,30 @@ void PIEthernet::server_func(void * eth) {
|
||||
}
|
||||
sockaddr_in client_addr;
|
||||
socklen_t slen = sizeof(client_addr);
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
long wr = ce->waitForEvent(ce->PRIVATEWB->event, FD_ACCEPT | FD_CLOSE);
|
||||
if (wr != FD_ACCEPT) {
|
||||
piMSleep(10);
|
||||
return;
|
||||
}
|
||||
#else
|
||||
# else
|
||||
if (!ce->PRIVATEWB->event.wait(ce->sock)) {
|
||||
piMSleep(10);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
// piCout << "server" << "accept ...";
|
||||
int s = accept(ce->sock, (sockaddr *)&client_addr, &slen);
|
||||
// piCout << "server" << "accept done" << ethErrorString();
|
||||
if (s == -1) {
|
||||
int lerr = ethErrorCore();
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
if (lerr == WSAETIMEDOUT) {
|
||||
#elif defined(ANDROID)
|
||||
# elif defined(ANDROID)
|
||||
if ((lerr == EAGAIN || lerr == EINTR)) {
|
||||
#else
|
||||
# else
|
||||
if (lerr == EAGAIN) {
|
||||
#endif
|
||||
# endif
|
||||
piMSleep(10);
|
||||
return;
|
||||
}
|
||||
@@ -969,7 +970,7 @@ void PIEthernet::setType(Type t, bool reopen) {
|
||||
bool PIEthernet::connectTCP() {
|
||||
::connect(sock, (sockaddr *)&(PRIVATE->addr_), sizeof(PRIVATE->addr_));
|
||||
// piCout << errorString();
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
long wr = waitForEvent(PRIVATE->event, FD_CONNECT | FD_CLOSE);
|
||||
switch (wr) {
|
||||
case FD_CONNECT:
|
||||
@@ -977,7 +978,7 @@ bool PIEthernet::connectTCP() {
|
||||
return ethIsWriteable(sock);
|
||||
default: break;
|
||||
}
|
||||
#else
|
||||
# else
|
||||
if (PRIVATE->event.wait(sock, PIWaitEvent::CheckWrite)) {
|
||||
if (ethIsWriteable(sock))
|
||||
return true;
|
||||
@@ -986,12 +987,12 @@ bool PIEthernet::connectTCP() {
|
||||
init();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
long PIEthernet::waitForEvent(PIWaitEvent & event, long mask) {
|
||||
if (!event.isCreate() || sock < 0) return 0;
|
||||
if (WSAEventSelect(sock, event.getEvent(), mask) == SOCKET_ERROR) {
|
||||
@@ -1008,7 +1009,7 @@ long PIEthernet::waitForEvent(PIWaitEvent & event, long mask) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
|
||||
|
||||
bool PIEthernet::configureDevice(const void * e_main, const void * e_parent) {
|
||||
@@ -1118,7 +1119,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
Interface ci;
|
||||
ci.index = -1;
|
||||
ci.mtu = 1500;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
int ret = 0;
|
||||
ulong ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
||||
PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO));
|
||||
@@ -1169,10 +1170,10 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
}
|
||||
}
|
||||
if (pAdapterInfo) HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
||||
#else
|
||||
# ifdef MICRO_PIP
|
||||
# else
|
||||
# ifdef ANDROID
|
||||
# ifdef LWIP
|
||||
# else
|
||||
# ifdef ANDROID
|
||||
struct ifconf ifc;
|
||||
int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
ifc.ifc_len = 256;
|
||||
@@ -1200,7 +1201,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
il << ci;
|
||||
}
|
||||
delete ifc.ifc_buf;
|
||||
# else
|
||||
# else
|
||||
struct ifaddrs *ret, *cif = 0;
|
||||
int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
if (getifaddrs(&ret) == 0) {
|
||||
@@ -1218,8 +1219,8 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
ci.address = getSockAddr(cif->ifa_addr);
|
||||
ci.netmask = getSockAddr(cif->ifa_netmask);
|
||||
ci.mac.clear();
|
||||
# ifdef QNX
|
||||
# ifndef BLACKBERRY
|
||||
# ifdef QNX
|
||||
# ifndef BLACKBERRY
|
||||
int fd = ::open((PIString("/dev/io-net/") + ci.name).dataAscii(), O_RDONLY);
|
||||
if (fd != 0) {
|
||||
nic_config_t nic;
|
||||
@@ -1227,9 +1228,9 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
::close(fd);
|
||||
ci.mac = macFromBytes(PIByteArray(nic.permanent_address, 6));
|
||||
}
|
||||
# endif
|
||||
# else
|
||||
# ifdef MAC_OS
|
||||
# endif
|
||||
# else
|
||||
# ifdef MAC_OS
|
||||
PIString req = PISystemInfo::instance()->ifconfigPath + " " + ci.name + " | grep ether";
|
||||
FILE * fp = popen(req.dataAscii(), "r");
|
||||
if (fp != 0) {
|
||||
@@ -1240,7 +1241,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
}
|
||||
pclose(fp);
|
||||
}
|
||||
# else
|
||||
# else
|
||||
if (s != -1) {
|
||||
struct ifreq ir;
|
||||
memset(&ir, 0, sizeof(ir));
|
||||
@@ -1252,8 +1253,8 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
ci.mtu = ir.ifr_mtu;
|
||||
}
|
||||
}
|
||||
# endif
|
||||
# endif
|
||||
# endif
|
||||
ci.flags = 0;
|
||||
if (cif->ifa_flags & IFF_UP) ci.flags |= PIEthernet::ifActive;
|
||||
if (cif->ifa_flags & IFF_RUNNING) ci.flags |= PIEthernet::ifRunning;
|
||||
@@ -1274,18 +1275,18 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
piCout << "[PIEthernet]"
|
||||
<< "Can`t get interfaces: %1"_tr("PIEthernet").arg(errorString());
|
||||
if (s != -1) ::close(s);
|
||||
# endif
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
return il;
|
||||
}
|
||||
|
||||
|
||||
PINetworkAddress PIEthernet::interfaceAddress(const PIString & interface_) {
|
||||
#if defined(WINDOWS) || defined(MICRO_PIP)
|
||||
# if defined(WINDOWS) || defined(LWIP)
|
||||
piCout << "[PIEthernet] Not implemented, use \"PIEthernet::allAddresses\" or \"PIEthernet::interfaces\" instead";
|
||||
return PINetworkAddress();
|
||||
#else
|
||||
# else
|
||||
struct ifreq ifr;
|
||||
piZeroMemory(ifr);
|
||||
strcpy(ifr.ifr_name, interface_.dataAscii());
|
||||
@@ -1294,7 +1295,7 @@ PINetworkAddress PIEthernet::interfaceAddress(const PIString & interface_) {
|
||||
::close(s);
|
||||
struct sockaddr_in * sa = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
return PINetworkAddress(uint(sa->sin_addr.s_addr));
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
@@ -1317,16 +1318,16 @@ PIVector<PINetworkAddress> PIEthernet::allAddresses() {
|
||||
// System wrap
|
||||
|
||||
int PIEthernet::ethErrorCore() {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
return WSAGetLastError();
|
||||
#else
|
||||
# else
|
||||
return errno;
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
PIString PIEthernet::ethErrorString() {
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
char * msg = nullptr;
|
||||
int err = WSAGetLastError();
|
||||
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
@@ -1343,18 +1344,18 @@ PIString PIEthernet::ethErrorString() {
|
||||
} else
|
||||
ret += '?';
|
||||
return ret;
|
||||
#else
|
||||
# else
|
||||
return errorString();
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
int PIEthernet::ethRecv(int sock, void * buf, int size, int flags) {
|
||||
if (sock < 0) return -1;
|
||||
return recv(sock,
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
(char *)
|
||||
#endif
|
||||
# endif
|
||||
buf,
|
||||
size,
|
||||
flags);
|
||||
@@ -1363,29 +1364,29 @@ int PIEthernet::ethRecv(int sock, void * buf, int size, int flags) {
|
||||
|
||||
int PIEthernet::ethRecvfrom(int sock, void * buf, int size, int flags, sockaddr * addr) {
|
||||
if (sock < 0) return -1;
|
||||
#ifdef QNX
|
||||
# ifdef QNX
|
||||
return recv(sock, buf, size, flags);
|
||||
#else
|
||||
# else
|
||||
socklen_t len = sizeof(sockaddr);
|
||||
return recvfrom(sock,
|
||||
# ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
(char *)
|
||||
# endif
|
||||
# endif
|
||||
buf,
|
||||
size,
|
||||
flags,
|
||||
addr,
|
||||
&len);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
int PIEthernet::ethSendto(int sock, const void * buf, int size, int flags, sockaddr * addr, int addr_len) {
|
||||
if (sock < 0) return -1;
|
||||
return sendto(sock,
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
(const char *)
|
||||
#endif
|
||||
# endif
|
||||
buf,
|
||||
size,
|
||||
flags,
|
||||
@@ -1399,26 +1400,26 @@ void PIEthernet::ethClosesocket(int sock, bool shutdown) {
|
||||
if (sock < 0) return;
|
||||
if (shutdown)
|
||||
::shutdown(sock,
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
SD_BOTH);
|
||||
closesocket(sock);
|
||||
#else
|
||||
# else
|
||||
SHUT_RDWR);
|
||||
::close(sock);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
int PIEthernet::ethSetsockopt(int sock, int level, int optname, const void * optval, int optlen) {
|
||||
if (sock < 0) return -1;
|
||||
auto ret = setsockopt(sock,
|
||||
level,
|
||||
optname,
|
||||
#ifdef WINDOWS
|
||||
(char *)
|
||||
#endif
|
||||
optval,
|
||||
optlen);
|
||||
level,
|
||||
optname,
|
||||
# ifdef WINDOWS
|
||||
(char *)
|
||||
# endif
|
||||
optval,
|
||||
optlen);
|
||||
if (ret != 0) piCout << "setsockopt error:" << ethErrorString();
|
||||
return ret;
|
||||
}
|
||||
@@ -1426,11 +1427,11 @@ int PIEthernet::ethSetsockopt(int sock, int level, int optname, const void * opt
|
||||
|
||||
int PIEthernet::ethSetsockoptInt(int sock, int level, int optname, int value) {
|
||||
if (sock < 0) return -1;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
DWORD
|
||||
#else
|
||||
# else
|
||||
int
|
||||
#endif
|
||||
# endif
|
||||
so = value;
|
||||
return ethSetsockopt(sock, level, optname, &so, sizeof(so));
|
||||
}
|
||||
@@ -1438,11 +1439,11 @@ int PIEthernet::ethSetsockoptInt(int sock, int level, int optname, int value) {
|
||||
|
||||
int PIEthernet::ethSetsockoptBool(int sock, int level, int optname, bool value) {
|
||||
if (sock < 0) return -1;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
BOOL
|
||||
#else
|
||||
# else
|
||||
int
|
||||
#endif
|
||||
# endif
|
||||
so = (value ? 1 : 0);
|
||||
return ethSetsockopt(sock, level, optname, &so, sizeof(so));
|
||||
}
|
||||
@@ -1450,12 +1451,12 @@ int PIEthernet::ethSetsockoptBool(int sock, int level, int optname, bool value)
|
||||
|
||||
void PIEthernet::ethNonblocking(int sock) {
|
||||
if (sock < 0) return;
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
u_long mode = 1;
|
||||
ioctlsocket(sock, FIONBIO, &mode);
|
||||
#else
|
||||
# else
|
||||
fcntl(sock, F_SETFL, O_NONBLOCK);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
@@ -1471,7 +1472,7 @@ bool PIEthernet::ethIsWriteable(int sock) {
|
||||
timeout.tv_sec = timeout.tv_usec = 0;
|
||||
::select(fds, nullptr, &fd_test, nullptr, &timeout);
|
||||
return FD_ISSET(sock, &fd_test);*/
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
fd_set fd_test;
|
||||
FD_ZERO(&fd_test);
|
||||
FD_SET(sock, &fd_test);
|
||||
@@ -1479,10 +1480,12 @@ bool PIEthernet::ethIsWriteable(int sock) {
|
||||
timeout.tv_sec = timeout.tv_usec = 0;
|
||||
::select(0, nullptr, &fd_test, nullptr, &timeout);
|
||||
return FD_ISSET(sock, &fd_test);
|
||||
#else
|
||||
# else
|
||||
int ret = 0;
|
||||
socklen_t len = sizeof(ret);
|
||||
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&ret, &len);
|
||||
return ret == 0;
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
#endif // PIP_NO_SOCKET
|
||||
|
||||
@@ -26,14 +26,17 @@
|
||||
#ifndef PIETHERNET_H
|
||||
#define PIETHERNET_H
|
||||
|
||||
|
||||
#include "piiodevice.h"
|
||||
#include "pinetworkaddress.h"
|
||||
|
||||
#ifdef ANDROID
|
||||
#ifndef PIP_NO_SOCKET
|
||||
|
||||
# ifdef ANDROID
|
||||
struct
|
||||
#else
|
||||
# else
|
||||
class
|
||||
#endif
|
||||
# endif
|
||||
sockaddr;
|
||||
|
||||
class PIP_EXPORT PIEthernet: public PIIODevice {
|
||||
@@ -439,7 +442,7 @@ public:
|
||||
//! \}
|
||||
//! \ioparams
|
||||
//! \{
|
||||
#ifdef DOXYGEN
|
||||
# ifdef DOXYGEN
|
||||
//! \brief read ip, default ""
|
||||
string ip;
|
||||
|
||||
@@ -460,7 +463,7 @@ public:
|
||||
|
||||
//! \brief time-to-live for multicast, default 1
|
||||
int multicastTTL;
|
||||
#endif
|
||||
# endif
|
||||
//! \}
|
||||
|
||||
protected:
|
||||
@@ -509,9 +512,9 @@ private:
|
||||
static void server_func(void * eth);
|
||||
void setType(Type t, bool reopen = true);
|
||||
bool connectTCP();
|
||||
#ifdef WINDOWS
|
||||
# ifdef WINDOWS
|
||||
long waitForEvent(PIWaitEvent & event, long mask);
|
||||
#endif
|
||||
# endif
|
||||
|
||||
static int ethErrorCore();
|
||||
static PIString ethErrorString();
|
||||
@@ -536,4 +539,5 @@ inline bool operator!=(const PIEthernet::Interface & v0, const PIEthernet::Inter
|
||||
return (v0.name != v1.name || v0.address != v1.address || v0.netmask != v1.netmask);
|
||||
}
|
||||
|
||||
#endif // PIP_NO_SOCKET
|
||||
#endif // PIETHERNET_H
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
# include <utime.h>
|
||||
#endif
|
||||
#define S_IFHDN 0x40
|
||||
#if defined(QNX) || defined(ANDROID) || defined(FREERTOS)
|
||||
#if defined(QNX) || defined(ANDROID) || defined(MICRO_PIP)
|
||||
# define _fopen_call_ fopen
|
||||
# define _fseek_call_ fseek
|
||||
# define _ftell_call_ ftell
|
||||
|
||||
@@ -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, ×) == -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
|
||||
|
||||
@@ -55,6 +55,10 @@
|
||||
# ifndef FREERTOS
|
||||
# define PIP_NO_THREADS
|
||||
# endif
|
||||
# ifndef LWIP
|
||||
# define PIP_NO_SOCKET
|
||||
# endif
|
||||
# define PISERIAL_NO_PINS
|
||||
#endif
|
||||
#ifndef WINDOWS
|
||||
# ifndef QNX
|
||||
|
||||
Reference in New Issue
Block a user