WIP: Pi Pico support #193

Draft
andrey wants to merge 6 commits from pico_sdk into master
37 changed files with 834 additions and 512 deletions
+165
View File
@@ -0,0 +1,165 @@
# PIP Codebase Guide for AGENTS
## Build Commands
### Basic Build
```bash
# Configure with CMake (release build)
cmake -B build -j16
# Build the project
cmake --build build -j16
# Install (default system location)
cmake --build build --target install -j16
# Local install (bin/lib/include in build directory)
cmake -B build -DLOCAL=ON -j16
```
### With Tests
```bash
cmake -B build -DTESTS=ON -j16
cmake --build build -j16
cd build && ctest
```
### Build Only
```bash
cmake --build build -j16
```
### Run Single Test
```bash
# After building with TESTS=ON
./build/tests/math/pip_math_test --gtest_filter="Vector2DTest.defaultConstructor*"
# Or use ctest
ctest -R math -V
# List all tests
ctest -N
```
### Build Options
- `TESTS=ON` - Build tests (requires Google Test)
- `COVERAGE=ON` - Build with coverage instrumentation
- `ICU=ON` - Enable ICU support for string conversion
- `STD_IOSTREAM=ON` - Enable std::iostream operators
- `INTROSPECTION=ON` - Build with introspection support
- `PIP_BUILD_*` - Enable/disable modules (e.g., `PIP_BUILD_CRYPT=OFF`)
### Code Generation Tools
- `pip_cmg` - Code model generator (auto-generates code from comments)
- `pip_rc` - Resources compiler (embeds resources into C++ code)
- `pip_tr` - Translation tool (compiles .btf translation files)
### Linting & Formatting
- **clang-format**: Run `clang-format -i <file>` or `find . -name "*.cpp" -o -name "*.h" | xargs clang-format -i`
- **Format config**: `.clang-format` (ColumnLimit: 140, IndentWidth: 4, BraceWrapping: Attach)
- **Includes sorting**: Enabled (CaseSensitive, SortIncludes: true)
- **Macro blocks**: `PRIVATE_DEFINITION_START/END`, `STATIC_INITIALIZER_BEGIN/END`, `DECLARE_UNIT_CLASS_BEGIN/END`
- **No Cursor/Copilot rules** found in this repository
## Code Style Guidelines
### General
- **Language Standard**: C++11 (CMAKE_CXX_STANDARD 11)
- **File Encoding**: UTF-8
- **Line Endings**: Unix (LF)
- **Column Limit**: 140 characters
### Naming Conventions
- **Classes**: `PascalCase` (e.g., `PIObject`, `PIString`)
- **Functions/Methods**: `camelCase` (e.g., `toString()`, `isEmpty()`)
- **Variables**: `camelCase` (e.g., `rowCount`, `isReady`)
- **Constants**: `kPrefixCamelCase` (e.g., `kMaxSize`)
- **Macros**: `UPPER_CASE` (e.g., `PIP_EXPORT`, `NO_COPY_CLASS`)
- **Types**: `PascalCase` with `p` prefix for internal/private (e.g., `PIString_p.h`)
### File Naming
- Headers: `pi<name>.h` (e.g., `piobject.h`, `pistring.h`)
- Private headers: `pi<name>_p.h` (e.g., `piobject_p.h`)
- Test files: `test<name>.cpp` (e.g., `testpivector2d.cpp`)
### Comments & Documentation
- Use Doxygen-style comments for all public APIs
- Include `\~english` and `\~russian` translations
- Group related classes/modules with `//! \defgroup`
- Use `\ingroup` to assign files to groups
- Example:
```cpp
//! \file piobject.h
//! \ingroup Core
//! \~\brief
//! \~english Base object
//! \~russian Базовый класс
```
### Includes
- Order: System headers → PIP headers → Local headers
- Sort alphabetically within groups
- Use `#pragma once` or include guards
- PIP includes use `#include "pi<name>.h"`
### Formatting (clang-format)
- **Indentation**: 4 spaces (no tabs for code)
- **Braces**: Attach style (`if (x) {`)
- **Pointers/References**: `Type* ptr`, `Type& ref`
- **Templates**: Always break before `>` in nested templates
- **Empty Lines**: Max 2 consecutive empty lines
- **Namespace**: No indentation (`Namespace { ... }`)
### Error Handling
- Use `PIString` for error messages
- Return bool for success/failure
- Use `assert()` for debug-only checks
- Avoid exceptions (RTTI disabled in some builds)
### Memory Management
- Prefer stack allocation
- Use `NO_COPY_CLASS(MyClass)` to disable copy
- Implement proper move semantics when needed
- Use `std::unique_ptr`/`std::shared_ptr` sparingly
### Macros
- PIMETA(...) - Add metadata for code model generator
- PIP_EXPORT - Export/import symbols for DLLs
- NO_COPY_CLASS - Disable copy constructor and operator=
- PRIVATE_DECLARATION - Private implementation macro
## Testing
- Framework: Google Test (fetched automatically when TESTS=ON)
- Test files: `tests/<module>/test<name>.cpp`
- Use `pip_test(module)` macro in `tests/CMakeLists.txt`
- Test discovery via `gtest_discover_tests()`
### Running Tests
```bash
# Run all tests
ctest
# Run specific test suite
ctest -R math -V
# List all available tests
ctest -N
# Run single test with gtest filter
./build/tests/math/pip_math_test --gtest_filter="Vector2DTest.defaultConstructor*"
```
## Module Structure
```
libs/
├── main/ # Core modules
│ ├── core/ # Base types, PIObject, PIString
│ ├── thread/ # Threading primitives
│ ├── math/ # Math functions, vectors, matrices
│ └── ...
└── <module>/ # Feature modules (fftw, crypt, compress, etc.)
```
## Additional Notes
- Project uses custom CMake macros from `cmake/` directory
- Version format: `MAJOR.MINOR.REVISION` (e.g., 5.6.0)
- All files have LGPL license header
- Support for multiple platforms: Windows, Linux, QNX, Android, Apple
+35 -15
View File
@@ -70,6 +70,8 @@ option(INTROSPECTION "Build with introspection" OFF)
option(TESTS "Build tests" OFF)
option(TESTS_RUN "Run tests before install step" OFF)
option(COVERAGE "Build project with coverage info" OFF)
option(PIP_NO_FILESYSTEM "Disable filesystem support" OFF)
option(PIP_NO_THREADS "Disable threading support" OFF)
option(PIP_FFTW_F "Support fftw module for float" ON)
option(PIP_FFTW_L "Support fftw module for long double" ON)
option(PIP_FFTW_Q "Support fftw module for quad double" OFF)
@@ -223,11 +225,26 @@ if (TESTS)
add_subdirectory(tests)
endif()
if(PIP_FREERTOS)
add_definitions(-DPIP_FREERTOS)
if(PIP_MICRO)
add_definitions(-DMICRO_PIP)
set(ICU OFF)
set(LOCAL ON)
endif()
if(PIP_FREERTOS)
add_definitions(-DPIP_FREERTOS)
endif()
if(DEFINED PICO_BOARD)
add_definitions(-DPICO_SDK)
set(PIP_NO_FILESYSTEM ON CACHE BOOL "" FORCE)
message(STATUS "Building PIP for Pi Pico SDK ${PICO_SDK_VERSION_STRING}")
endif()
if(PIP_NO_FILESYSTEM)
add_definitions(-DPIP_NO_FILESYSTEM)
endif()
if(PIP_NO_THREADS)
add_definitions(-DPIP_NO_THREADS)
endif()
# Check Bessel functions
set(CMAKE_REQUIRED_INCLUDES math.h)
@@ -331,7 +348,7 @@ if ((NOT DEFINED SHSTKPROJECT) AND (DEFINED ANDROID_PLATFORM))
#message("${ANDROID_NDK}/sysroot/usr/include")
endif()
if(NOT PIP_FREERTOS)
if(NOT PIP_MICRO)
if(WIN32)
if(${C_COMPILER} STREQUAL "cl.exe")
else()
@@ -352,7 +369,7 @@ if(NOT PIP_FREERTOS)
endif()
endif()
set(PIP_LIBS)
if(PIP_FREERTOS)
if(PIP_MICRO)
set(PIP_LIBS ${LIBS_MAIN})
else()
foreach(LIB_ ${LIBS_MAIN})
@@ -369,11 +386,11 @@ else()
if (NOT DEFINED ANDROID_PLATFORM)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
endif()
if(DEFINED ENV{QNX_HOST} OR PIP_FREERTOS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth-32")
endif()
endif()
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS}")
if(DEFINED ENV{QNX_HOST} OR PIP_MICRO)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth-32")
endif()
set(PCRE2_BUILD_PCRE2_8 OFF CACHE BOOL "" FORCE)
set(PCRE2_BUILD_PCRE2_16 ON CACHE BOOL "" FORCE)
@@ -411,7 +428,7 @@ endif()
if (NOT CROSSTOOLS)
if (NOT PIP_FREERTOS)
if (NOT PIP_MICRO)
if (PIP_BUILD_CONSOLE)
pip_module(console "" "PIP console support" "" "" "")
@@ -649,9 +666,12 @@ if (NOT CROSSTOOLS)
endif()
if (PIP_BUILD_IO_UTILS)
pip_module(io_utils "pip_crypt" "PIP I/O support" "" "" " (+crypt)")
if(PIP_BUILD_CRYPT)
pip_module(io_utils "pip_crypt" "PIP I/O support" "" "" " (+crypt)")
else()
pip_module(io_utils "" "PIP I/O support" "" "" "")
endif()
endif()
endif()
endif()
@@ -659,7 +679,7 @@ string(REPLACE ";" "," PIP_EXPORTS_STR "${PIP_EXPORTS}")
target_compile_definitions(pip PRIVATE "PICODE_DEFINES=\"${PIP_EXPORTS_STR}\"")
if(NOT PIP_FREERTOS)
if(NOT PIP_MICRO)
# Auxiliary
if (NOT CROSSTOOLS)
@@ -736,7 +756,7 @@ if(NOT LOCAL)
install(TARGETS ${PIP_MODULES} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
endif()
else()
if(NOT PIP_FREERTOS)
if(NOT PIP_MICRO)
if(WIN32)
install(TARGETS ${PIP_MODULES} RUNTIME DESTINATION bin)
install(TARGETS ${PIP_MODULES} ARCHIVE DESTINATION lib)
@@ -764,7 +784,7 @@ endif()
#
# Build Documentation
#
if ((NOT PIP_FREERTOS) AND (NOT CROSSTOOLS))
if ((NOT PIP_MICRO) AND (NOT CROSSTOOLS))
include(PIPDocumentation)
find_package(Doxygen)
if(DOXYGEN_FOUND)
@@ -833,7 +853,7 @@ message(" Type : ${CMAKE_BUILD_TYPE}")
if (NOT LOCAL)
message(" Install: \"${CMAKE_INSTALL_PREFIX}\"")
else()
if(NOT PIP_FREERTOS)
if(NOT PIP_MICRO)
message(" Install: local \"bin\", \"lib\" and \"include\"")
endif()
endif()
@@ -869,7 +889,7 @@ message(" Utilites:")
foreach(_util ${PIP_UTILS_LIST})
message(" * ${_util}")
endforeach()
if(NOT PIP_FREERTOS)
if(NOT PIP_MICRO)
message("")
message(" Using libraries:")
foreach(LIB_ ${LIBS_STATUS})
+1 -1
View File
@@ -72,7 +72,7 @@ if (NOT BUILDING_PIP)
find_library(PTHREAD_LIBRARY pthread)
find_library(UTIL_LIBRARY util)
set(_PIP_ADD_LIBS_ ${PTHREAD_LIBRARY} ${UTIL_LIBRARY})
if((NOT DEFINED ENV{QNX_HOST}) AND (NOT APPLE) AND (NOT PIP_FREERTOS))
if((NOT DEFINED ENV{QNX_HOST}) AND (NOT APPLE) AND (NOT PIP_MICRO))
find_library(RT_LIBRARY rt)
list(APPEND _PIP_ADD_LIBS_ ${RT_LIBRARY})
endif()
+1
View File
@@ -17,6 +17,7 @@ list(APPEND COMPONENT_ADD_INCLUDEDIRS "../libs/main/thread")
set(COMPONENT_PRIV_REQUIRES pthread lwip freertos vfs spi_flash libsodium)
register_component()
set(PIP_FREERTOS ON)
set(PIP_MICRO ON)
set(LIB OFF)
set(INCLUDE_DIRS ${IDF_INCLUDE_DIRECTORIES})
list(APPEND INCLUDE_DIRS $ENV{IDF_PATH}/components/newlib/platform_include)
+15 -13
View File
@@ -113,7 +113,7 @@ bool PISystemMonitor::startOnProcess(int pID, PISystemTime interval) {
}
# endif
# else
PRIVATE->hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pID_);
PRIVATE->hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pID_);
if (PRIVATE->hProc == 0) {
piCoutObj << "Can`t open process with ID = %1, %2!"_tr("PISystemMonitor").arg(pID_).arg(errorString());
return false;
@@ -178,6 +178,9 @@ PISystemTime uint64toST(uint64_t v) {
void PISystemMonitor::run() {
cur_tm.clear();
tbid.clear();
ProcessStats tstat;
tstat.ID = pID_;
#ifndef PIP_NO_THREADS
__PIThreadCollection * pitc = __PIThreadCollection::instance();
pitc->lock();
PIVector<PIThread *> tv = pitc->threads();
@@ -185,16 +188,14 @@ void PISystemMonitor::run() {
if (t->isPIObject()) tbid[t->tid()] = t->name();
pitc->unlock();
// piCout << tbid.keys().toType<uint>();
ProcessStats tstat;
tstat.ID = pID_;
#ifdef MICRO_PIP
# ifdef FREERTOS
for (auto * t: tv)
if (t->isPIObject()) gatherThread(t->tid());
#else
# ifndef WINDOWS
# else // FREERTOS
# ifndef WINDOWS
double delay_ms = delay_.toMilliseconds();
tbid[pID_] = "main";
# ifdef MAC_OS
# ifdef MAC_OS
rusage_info_current ru;
proc_pid_rusage(pID_, RUSAGE_INFO_CURRENT, (rusage_info_t *)&ru);
// piCout << PISystemTime(((uint*)&(ru.ri_user_time))[1], ((uint*)&(ru.ri_user_time))[0]);
@@ -210,7 +211,7 @@ void PISystemMonitor::run() {
tstat.cpu_load_user = 100.f * (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev).toMilliseconds() / delay_ms;
cycle = 0;
// piCout << (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev).toMilliseconds() / delay_ms;
# else
# else // MAC_OS
PRIVATE->file.seekToBegin();
PIString str = PIString::fromAscii(PRIVATE->file.readAll());
int si = str.find('(') + 1, fi = 0, cc = 1;
@@ -264,8 +265,8 @@ void PISystemMonitor::run() {
if (i.flags[PIFile::FileInfo::Dot] || i.flags[PIFile::FileInfo::DotDot]) continue;
gatherThread(i.name().toInt());
}
# endif
# else
# endif // MAC_OS
# else // WINDOWS
if (GetProcessMemoryInfo(PRIVATE->hProc, &PRIVATE->mem_cnt, sizeof(PRIVATE->mem_cnt)) != 0) {
tstat.physical_memsize = PRIVATE->mem_cnt.WorkingSetSize;
}
@@ -315,8 +316,9 @@ void PISystemMonitor::run() {
tstat.cpu_load_user = 0.f;
}
PRIVATE->tm.reset();
# endif
#endif
# endif // WINDOWS
# endif // FREERTOS
#endif // PIP_NO_THREADS
tstat.cpu_load_system = piClampf(tstat.cpu_load_system, 0.f, 100.f);
tstat.cpu_load_user = piClampf(tstat.cpu_load_user, 0.f, 100.f);
@@ -352,7 +354,7 @@ void PISystemMonitor::gatherThread(llong id) {
#ifdef MICRO_PIP
ts.name = tbid.value(id, "<PIThread>");
#else
ts.name = tbid.value(id, "<non-PIThread>");
ts.name = tbid.value(id, "<non-PIThread>");
# ifndef WINDOWS
PIFile f(PRIVATE->proc_dir + "task/" + PIString::fromNumber(id) + "/stat");
// piCout << f.path();
+35 -31
View File
@@ -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
+15 -10
View File
@@ -25,19 +25,23 @@
#ifndef PIKBDLISTENER_H
#define PIKBDLISTENER_H
#include "pithread.h"
#include "pitime.h"
#include "pibase.h"
#ifndef MICRO_PIP
# include "pithread.h"
# include "pitime.h"
//! \relatesalso PIKbdListener
//! \~\brief
//! \~english Waits until the active listener captures the configured exit key and then stops it.
//! \~russian Ожидает, пока активный слушатель перехватит настроенную клавишу выхода, и затем останавливает его.
#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(); \
}
# 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(); \
}
//! \~\ingroup Console
@@ -303,7 +307,7 @@ private:
void run() override { readKeyboard(); }
void end() override;
#ifndef WINDOWS
# ifndef WINDOWS
struct PIP_EXPORT EscSeq {
const char * seq;
int key;
@@ -323,7 +327,7 @@ private:
};
static const EscSeq esc_seq[];
#endif
# endif
PRIVATE_DECLARATION(PIP_EXPORT)
KBFunc ret_func;
@@ -377,4 +381,5 @@ REGISTER_PIVARIANTSIMPLE(PIKbdListener::KeyEvent)
REGISTER_PIVARIANTSIMPLE(PIKbdListener::MouseEvent)
REGISTER_PIVARIANTSIMPLE(PIKbdListener::WheelEvent)
#endif // MICRO_PIP
#endif // PIKBDLISTENER_H
+4 -2
View File
@@ -224,9 +224,11 @@ extern char ** environ;
# endif
# ifdef MICRO_PIP
# define __PIP_TYPENAME__(T) "?"
# define __PIP_TYPENAME__(T) "?"
# elif defined(__GXX_RTTI__) || defined(__RTTI__)
# define __PIP_TYPENAME__(T) typeid(T).name()
# else
# define __PIP_TYPENAME__(T) typeid(T).name()
# define __PIP_TYPENAME__(T) "?"
# endif
# ifdef CC_GCC
+28 -2
View File
@@ -31,13 +31,14 @@
#include "pibase.h"
#ifndef MICRO_PIP
#ifndef PIP_NO_THREADS
# include "piincludes.h"
class PIFile;
class PIStringList;
class PIInit;
class PIP_EXPORT __PIInit_Initializer__ {
public:
@@ -49,6 +50,31 @@ public:
static __PIInit_Initializer__ __piinit_initializer__;
#ifdef MICRO_PIP
#ifndef PIINIT_MICRO_STUB_DEFINED
#define PIINIT_MICRO_STUB_DEFINED
int __PIInit_Initializer__::count_ = 0;
PIInit * __PIInit_Initializer__::__instance__ = nullptr;
__PIInit_Initializer__::__PIInit_Initializer__() {
count_++;
if (count_ > 1) return;
__instance__ = nullptr;
}
__PIInit_Initializer__::~__PIInit_Initializer__() {
count_--;
if (count_ > 0) return;
if (__instance__ != nullptr) {
__instance__ = nullptr;
}
}
#endif
#endif
//! \~\ingroup Core
//! \~\brief
//! \~english Library initialization singleton and build information access point.
@@ -96,5 +122,5 @@ private:
};
#endif // MICRO_PIP
#endif // PIP_NO_THREADS
#endif // PIINIT_H
+1
View File
@@ -21,6 +21,7 @@
#include "piconditionvar.h"
#include "pithread.h"
#include "pitime.h"
#ifndef MICRO_PIP
# include "pifile.h"
# include "piiostream.h"
+32 -29
View File
@@ -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
+7 -4
View File
@@ -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
+9 -3
View File
@@ -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
}
+2
View File
@@ -16,6 +16,7 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIP_NO_FILESYSTEM
#include "pidir.h"
#include "piincludes_p.h"
@@ -610,3 +611,4 @@ void PIDir::CurrentDirOverrider::save(const PIFile::FileInfo & info) {
else
PIDir::setCurrent(PIDir::current().path() + PIDir::separator + p);
}
#endif // PIP_NO_FILESYSTEM
+4
View File
@@ -30,6 +30,7 @@
#include "piregularexpression.h"
#ifndef PIP_NO_FILESYSTEM
//! \~\ingroup IO
//! \~\brief
//! \~english Local directory.
@@ -217,8 +218,10 @@ private:
PIString path_, scan_;
};
#endif // PIP_NO_FILESYSTEM
#ifndef PIP_NO_FILESYSTEM
inline bool operator<(const PIFile::FileInfo & v0, const PIFile::FileInfo & v1) {
return (v0.path < v1.path);
}
@@ -242,6 +245,7 @@ inline PICout operator<<(PICout s, const PIDir & v) {
s.restoreControls();
return s;
}
#endif // PIP_NO_FILESYSTEM
#endif // PIDIR_H
+182 -179
View File
@@ -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;
@@ -536,9 +537,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)) {
@@ -662,9 +663,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_;
@@ -679,7 +680,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) {
@@ -695,34 +696,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;
}
@@ -746,7 +747,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:
@@ -759,9 +760,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));
@@ -794,11 +795,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_));
@@ -810,9 +811,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();
@@ -848,11 +849,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;
@@ -911,30 +912,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;
}
@@ -970,7 +971,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:
@@ -978,7 +979,7 @@ bool PIEthernet::connectTCP() {
return ethIsWriteable(sock);
default: break;
}
#else
# else
if (PRIVATE->event.wait(sock, PIWaitEvent::CheckWrite)) {
if (ethIsWriteable(sock))
return true;
@@ -987,12 +988,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) {
@@ -1009,7 +1010,7 @@ long PIEthernet::waitForEvent(PIWaitEvent & event, long mask) {
}
return 0;
}
#endif
# endif
bool PIEthernet::configureDevice(const void * e_main, const void * e_parent) {
@@ -1119,7 +1120,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));
@@ -1170,10 +1171,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;
@@ -1201,7 +1202,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) {
@@ -1219,8 +1220,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;
@@ -1228,9 +1229,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) {
@@ -1241,7 +1242,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
}
pclose(fp);
}
# else
# else
if (s != -1) {
struct ifreq ir;
memset(&ir, 0, sizeof(ir));
@@ -1253,8 +1254,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;
@@ -1275,18 +1276,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());
@@ -1295,7 +1296,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
}
@@ -1318,16 +1319,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,
@@ -1344,18 +1345,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);
@@ -1364,29 +1365,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,
@@ -1400,26 +1401,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;
}
@@ -1427,11 +1428,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));
}
@@ -1439,11 +1440,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));
}
@@ -1451,12 +1452,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
}
@@ -1472,7 +1473,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);
@@ -1480,10 +1481,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
+11 -7
View File
@@ -25,14 +25,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;
//! \~\ingroup IO
@@ -593,7 +596,7 @@ public:
//! \}
//! \ioparams
//! \{
#ifdef DOXYGEN
# ifdef DOXYGEN
//! \~english Read IP address, default ""
//! \~russian IP-адрес чтения, по умолчанию ""
@@ -623,7 +626,7 @@ public:
//! \~russian TTL multicast-пакетов, по умолчанию 1
int multicastTTL;
#endif
# endif
//! \}
protected:
@@ -676,9 +679,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();
@@ -703,4 +706,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
+3 -1
View File
@@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIP_NO_FILESYSTEM
#include "pifile.h"
#include "pidir.h"
@@ -45,7 +46,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
@@ -635,3 +636,4 @@ int PIFile::writeAll(const PIString & path, const PIByteArray & data) {
f.clear();
return f.write(data.data(), data.size_s());
}
#endif // PIP_NO_FILESYSTEM
+4
View File
@@ -31,6 +31,7 @@
#include "pipropertystorage.h"
#ifndef PIP_NO_FILESYSTEM
//! \~\ingroup IO
//! \~\brief
//! \~english Local file.
@@ -361,8 +362,10 @@ private:
llong _size = -1;
PIString prec_str;
};
#endif // PIP_NO_FILESYSTEM
#ifndef PIP_NO_FILESYSTEM
//! \relatesalso PICout
//! \~english Output operator to \a PICout.
//! \~russian Оператор вывода в \a PICout.
@@ -395,5 +398,6 @@ BINARY_STREAM_READ(PIFile::FileInfo) {
v.perm_group.raw >> v.perm_other.raw;
return s;
}
#endif // PIP_NO_FILESYSTEM
#endif // PIFILE_H
+188 -187
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
+8 -2
View File
@@ -153,8 +153,14 @@
#ifdef PIP_FREERTOS
# define FREERTOS
#endif
#if defined(FREERTOS) || defined(PLATFORMIO)
# define MICRO_PIP
#ifdef MICRO_PIP
# ifndef FREERTOS
# define PIP_NO_THREADS
# endif
# ifndef LWIP
# define PIP_NO_SOCKET
# endif
# define PISERIAL_NO_PINS
#endif
#ifndef WINDOWS
# ifndef QNX
@@ -41,7 +41,11 @@ template<typename... Args>
class Function: public FunctionBase {
public:
uint formatHash() override {
#if defined(__GXX_RTTI__) || defined(__RTTI__)
static uint ret = PIConstChars(typeid(std::function<void(Args...)>).name()).hash();
#else
static uint ret = 0;
#endif
return ret;
}
std::function<bool(Args...)> func;
+29 -6
View File
@@ -7,12 +7,21 @@
# include "pifile.h"
# include "piiostream.h"
# include <fcntl.h>
# include <linux/input-event-codes.h>
# include <linux/input.h>
# include <sys/ioctl.h>
# include <sys/time.h>
# include <unistd.h>
# ifdef LINUX
# include <fcntl.h>
# include <linux/input-event-codes.h>
# include <linux/input.h>
# include <sys/ioctl.h>
# include <sys/time.h>
# include <unistd.h>
# else
// Stubs for embedded/non-Linux builds
# define EV_SYN 0
# define EV_KEY 1
# define EV_REL 2
# define EV_ABS 3
# define EVIOCGABS(_v) 0
# endif
#else
// clang-format off
# undef _WIN32_WINNT
@@ -399,6 +408,7 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
ullong bits = readFile(hd_i.path + file).toULLong(16);
// piCout<< PICoutManipulators::Bin << abs;
if (bits > 0) {
#ifdef LINUX
int fd = ::open(dev.path.dataAscii(), O_RDONLY);
if (fd < 0) {
// piCout << "Warning: can`t open" << dev.path << errorString();
@@ -423,6 +433,19 @@ PIVector<PIHIDeviceInfo> PIHIDevice::allDevices(bool try_open) {
}
}
if (fd >= 0) ::close(fd);
#else
// Stub implementation for non-Linux builds
PIHIDeviceInfo::AxisInfo ai;
ai.is_relative = is_relative;
ai.min = 0;
ai.max = 1024;
for (int bit = 0; bit < 64; ++bit) {
if (checkBit(bits, bit, PIString::fromNumber(bit))) {
ai.data_index = bit;
ret << ai;
}
}
#endif
}
return ret;
};
+2
View File
@@ -23,6 +23,7 @@
# define _WIN32_WINNT 0x0600
#endif
#ifndef PIP_NO_THREADS
#include "piconditionvar.h"
#include "piincludes_p.h"
#ifdef WINDOWS
@@ -175,3 +176,4 @@ void PIConditionVariable::notifyAll() {
pthread_cond_broadcast(&PRIVATE->nativeHandle);
#endif
}
#endif // PIP_NO_THREADS
+3 -1
View File
@@ -28,6 +28,8 @@
#include "pimutex.h"
#include "pisystemtime.h"
#ifndef PIP_NO_THREADS
//! \~\ingroup Thread
//! \~\brief
//! \~english Condition variable used together with external %PIMutex.
@@ -173,5 +175,5 @@ private:
PRIVATE_DECLARATION(PIP_EXPORT)
};
#endif // PIP_NO_THREADS
#endif // PICONDITIONVAR_H
+2
View File
@@ -107,6 +107,7 @@
//! \}
#ifndef PIP_NO_THREADS
#include "pimutex.h"
#include "piincludes_p.h"
@@ -228,3 +229,4 @@ void PIMutex::destroy() {
pthread_mutex_destroy(&(PRIVATE->mutex));
#endif
}
#endif // PIP_NO_THREADS
+2 -1
View File
@@ -28,6 +28,7 @@
#include "piinit.h"
#ifndef PIP_NO_THREADS
//! \~\ingroup Thread
//! \~\brief
//! \~english Mutex for mutual exclusion between threads.
@@ -93,5 +94,5 @@ private:
bool cond;
};
#endif // PIP_NO_THREADS
#endif // PIMUTEX_H
+2
View File
@@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIP_NO_THREADS
#include "pithread.h"
#include "piincludes_p.h"
@@ -1086,3 +1087,4 @@ bool PIThread::_waitForFinish(PISystemTime max_tm) {
#endif
return false;
}
#endif // PIP_NO_THREADS
+2 -1
View File
@@ -43,6 +43,7 @@
class PIThread;
#ifndef PIP_NO_THREADS
#ifndef MICRO_PIP
class PIIntrospectionThreads;
@@ -339,5 +340,5 @@ private:
void setThreadName();
};
#endif // PIP_NO_THREADS
#endif // PITHREAD_H
+2
View File
@@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIP_NO_THREADS
#include "pitimer.h"
#include "piconditionvar.h"
@@ -315,3 +316,4 @@ void PITimer::stop() {
thread->stop();
event.notifyAll();
}
#endif // PIP_NO_THREADS
+2 -1
View File
@@ -44,6 +44,7 @@
class PIThread;
#ifndef PIP_NO_THREADS
//! \~\ingroup Thread
//! \~\brief
//! \~english Periodic timer that emits ticks from an internal worker thread.
@@ -246,5 +247,5 @@ protected:
PIConditionVariable event;
};
#endif // PIP_NO_THREADS
#endif // PITIMER_H
+7 -7
View File
@@ -26,6 +26,8 @@
# include "pisystemtests.h"
#elif defined(ARDUINO)
# include <Arduino.h>
#elif defined(PICO_SDK)
# include "hardware/time.h"
#endif
#ifdef MICRO_PIP
# include <sys/time.h>
@@ -48,15 +50,13 @@
void piUSleep(int usecs) {
if (usecs <= 0) return;
#ifdef WINDOWS
// printf("Sleep %d\n", usecs / 1000);
if (usecs > 0) Sleep(usecs / 1000);
// printf("Sleep end");
#else
# ifdef FREERTOS
Sleep(usecs / 1000);
#elif defined(FREERTOS)
vTaskDelay(usecs / 1000 / portTICK_PERIOD_MS);
# else
#elif defined(PICO_SDK)
sleep_us(usecs);
#else
usecs -= PISystemTests::usleep_offset_us;
if (usecs > 0) usleep(usecs);
# endif
#endif
}
+5 -1
View File
@@ -54,12 +54,16 @@ public:
}
#ifdef MICRO_PIP
PIString typeName() const final {
static PIString ret(PIVariant(T()).typeName());
static PIString ret(PIVariant::fromValue<T>(T()).typeName());
return ret;
}
#else
PIString typeName() const final {
#if defined(__GXX_RTTI__) || defined(__RTTI__)
static PIString ret(typeid(T).name());
#else
static PIString ret("unknown");
#endif
return ret;
}
#endif