29.04.2014 - Version 0.4.0_prealpha. PICodeParser, namespace PICodeInfo, new tool "pip_cmg" in dir "code_model_generator". New feature in PIIODevice - "createFromFullPath", all parameters of all I/O devices now works with PIObjects`s properties.
This commit is contained in:
118
piiodevice.cpp
118
piiodevice.cpp
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Abstract input/output device
|
||||
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -53,7 +53,7 @@
|
||||
* PIIODevice have internal buffer for threaded read, and \a threadedRead() function
|
||||
* receive pointer to this buffer in first argument. You can adjust size of this buffer
|
||||
* by function \a setThreadedReadBufferSize() \n
|
||||
* Default size of this buffer is 4096 bytes
|
||||
* Default size of this buffer is 4096 bytes.
|
||||
*
|
||||
* \section PIIODevice_sec5 Reopen
|
||||
* When threaded read is begin its call \a open() if device is closed. While threaded
|
||||
@@ -70,11 +70,42 @@
|
||||
* function \a configureDevice().
|
||||
* \n Each ancestor of %PIIODevice reimlements \a configureDevice() function to be able
|
||||
* to be confured from configuration file. This parameters described at section
|
||||
* "Configurable parameters" in the class reference. Usage example:
|
||||
* "Configurable parameters" in the class reference. \n Usage example:
|
||||
* \snippet piiodevice.cpp configure
|
||||
* Implement example:
|
||||
* Implementation example:
|
||||
* \snippet piiodevice.cpp configureDevice
|
||||
*
|
||||
* \section PIIODevice_sec7 Creating devices by unambiguous string
|
||||
* There are some virtual functions to describe child class without its declaration.
|
||||
* \n \a fullPathPrefix() should returns unique prefix of device
|
||||
* \n \a constructFullPath() should returns full unambiguous string, contains prefix and all device parameters
|
||||
* \n \a configureFromFullPath() provide configuring device from full unambiguous string without prefix and "://"
|
||||
* \n Macro PIIODEVICE should be used instead of PIOBJECT
|
||||
* \n Macro REGISTER_DEVICE should be used after definition of class, i.e. at the last line of *.cpp file
|
||||
* \n \n If custom I/O device corresponds there rules, it can be returned by function \a createFromFullPath().
|
||||
* \n Each PIP I/O device has custom unambiguous string description:
|
||||
* * PIFile: "file://<path>"
|
||||
* * PIBinaryLog: "binlog://<logDir>[:<filePrefix>][:<defaultID>]"
|
||||
* * PISerial: "ser://<device>:<speed(50|...|115200)>[:<dataBitsCount(6|7|8)>][:<parity(N|E|O)>][:<stopBits(1|2)>]"
|
||||
* * PIEthernet: "eth://<type(UDP|TCP)>:<readIP>:<readPort>[:<multicast(mcast:<ip>)>]"
|
||||
* * PIUSB: "usb://<vid>:<pid>[:<deviceNumber>][:<readEndpointNumber>][:<writeEndpointNumber>]"
|
||||
* \n \n Examples:
|
||||
* * PIFile: "file://../text.txt"
|
||||
* * PIBinaryLog: "binlog://../logs/:mylog_:1"
|
||||
* * PISerial: "ser:///dev/ttyUSB0:9600:8:N:1", equivalent "ser:///dev/ttyUSB0:9600"
|
||||
* * PIEthernet: "eth://TCP:127.0.0.1:16666", "eth://UDP:192.168.0.5:16666:mcast:234.0.2.1:mcast:234.0.2.2"
|
||||
* * PIUSB: "usb://0bb4:0c86:1:1:2"
|
||||
* \n \n
|
||||
* So, custom I/O device can be created with next call:
|
||||
* \code{cpp}
|
||||
* // creatring devices
|
||||
* PISerial * ser = (PISerial * )PIIODevice::createFromFullPath("ser://COM1:115200");
|
||||
* PIEthernet * eth = (PIEthernet * )PIIODevice::createFromFullPath("eth://UDP:127.0.0.1:4001");
|
||||
* // examine devices
|
||||
* piCout << ser << ser->properties();
|
||||
* piCout << eth << eth->properties();
|
||||
* \endcode
|
||||
*
|
||||
* \section PIIODevice_ex0 Example
|
||||
* \snippet piiodevice.cpp 0
|
||||
*/
|
||||
@@ -82,38 +113,18 @@
|
||||
|
||||
PIIODevice::PIIODevice(): PIThread() {
|
||||
mode_ = ReadOnly;
|
||||
opened_ = init_ = thread_started_ = false;
|
||||
reopen_enabled_ = raise_threaded_read_ = true;
|
||||
reopen_timeout_ = 1000;
|
||||
ret_func_ = 0;
|
||||
ret_data_ = 0;
|
||||
tri = 0;
|
||||
buffer_tr.resize(4096);
|
||||
CONNECT2(void, void * , int, &timer, timeout, this, check_start);
|
||||
CONNECT(void, &write_thread, started, this, write_func);
|
||||
created();
|
||||
init();
|
||||
_init();
|
||||
setPath(PIString());
|
||||
}
|
||||
|
||||
|
||||
/*! \brief Constructs a PIIODevice with path and mode
|
||||
* \param path path to device
|
||||
* \param type mode for open
|
||||
* \param initNow init or not in constructor */
|
||||
PIIODevice::PIIODevice(const PIString & path, PIIODevice::DeviceMode type, bool initNow): PIThread() {
|
||||
path_ = path;
|
||||
mode_ = type;
|
||||
opened_ = init_ = thread_started_ = false;
|
||||
reopen_enabled_ = raise_threaded_read_ = true;
|
||||
reopen_timeout_ = 1000;
|
||||
ret_func_ = 0;
|
||||
ret_data_ = 0;
|
||||
tri = 0;
|
||||
buffer_tr.resize(4096);
|
||||
CONNECT2(void, void * , int, &timer, timeout, this, check_start);
|
||||
CONNECT(void, &write_thread, started, this, write_func);
|
||||
created();
|
||||
if (initNow) init();
|
||||
* \param type mode for open */
|
||||
PIIODevice::PIIODevice(const PIString & path, PIIODevice::DeviceMode mode): PIThread() {
|
||||
mode_ = mode;
|
||||
_init();
|
||||
setPath(path);
|
||||
}
|
||||
|
||||
|
||||
@@ -127,6 +138,20 @@ PIIODevice::~PIIODevice() {
|
||||
}
|
||||
|
||||
|
||||
void PIIODevice::_init() {
|
||||
opened_ = init_ = thread_started_ = false;
|
||||
raise_threaded_read_ = true;
|
||||
ret_func_ = 0;
|
||||
ret_data_ = 0;
|
||||
tri = 0;
|
||||
setReopenEnabled(true);
|
||||
setReopenTimeout(1000);
|
||||
setThreadedReadBufferSize(4096);
|
||||
CONNECT2(void, void * , int, &timer, timeout, this, check_start);
|
||||
CONNECT(void, &write_thread, started, this, write_func);
|
||||
}
|
||||
|
||||
|
||||
void PIIODevice::check_start(void * data, int delim) {
|
||||
//cout << "check " << tread_started_ << endl;
|
||||
if (open()) {
|
||||
@@ -176,7 +201,7 @@ void PIIODevice::begin() {
|
||||
return;
|
||||
}
|
||||
//init();
|
||||
if (!timer.isRunning() && reopen_enabled_) timer.start(reopen_timeout_);
|
||||
if (!timer.isRunning() && isReopenEnabled()) timer.start(reopenTimeout());
|
||||
}
|
||||
|
||||
|
||||
@@ -206,13 +231,14 @@ PIByteArray PIIODevice::readForTime(double timeout_ms) {
|
||||
PIByteArray str;
|
||||
if (timeout_ms <= 0.) return str;
|
||||
int ret;
|
||||
uchar td[threadedReadBufferSize()];
|
||||
uchar * td = new uchar[threadedReadBufferSize()];
|
||||
timer.reset();
|
||||
while (timer.elapsed_m() < timeout_ms) {
|
||||
ret = read(td, threadedReadBufferSize());
|
||||
if (ret <= 0) msleep(1);
|
||||
else str.append(td, ret);
|
||||
}
|
||||
delete td;
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -237,16 +263,30 @@ bool PIIODevice::configure(const PIString & config_file, const PIString & sectio
|
||||
PIConfig::Entry * ep = 0;
|
||||
if (parent_section) ep = em.parent();
|
||||
if (ep != 0) {
|
||||
setReopenEnabled(ep->getValue("reopenEnabled", reopen_enabled_, &ex));
|
||||
if (!ex) setReopenEnabled(em.getValue("reopenEnabled", reopen_enabled_));
|
||||
setReopenTimeout(ep->getValue("reopenTimeout", reopen_timeout_, &ex));
|
||||
if (!ex) setReopenTimeout(em.getValue("reopenTimeout", reopen_timeout_));
|
||||
setReopenEnabled(ep->getValue("reopenEnabled", isReopenEnabled(), &ex));
|
||||
if (!ex) setReopenEnabled(em.getValue("reopenEnabled", isReopenEnabled()));
|
||||
setReopenTimeout(ep->getValue("reopenTimeout", reopenTimeout(), &ex));
|
||||
if (!ex) setReopenTimeout(em.getValue("reopenTimeout", reopenTimeout()));
|
||||
setThreadedReadBufferSize(ep->getValue("threadedReadBufferSize", buffer_tr.size_s(), &ex));
|
||||
if (!ex) setThreadedReadBufferSize(em.getValue("threadedReadBufferSize", buffer_tr.size_s()));
|
||||
} else {
|
||||
setReopenEnabled(em.getValue("reopenEnabled", reopen_enabled_));
|
||||
setReopenTimeout(em.getValue("reopenTimeout", reopen_timeout_));
|
||||
setReopenEnabled(em.getValue("reopenEnabled", isReopenEnabled()));
|
||||
setReopenTimeout(em.getValue("reopenTimeout", reopenTimeout()));
|
||||
setThreadedReadBufferSize(em.getValue("threadedReadBufferSize", buffer_tr.size_s()));
|
||||
}
|
||||
return configureDevice(&em, ep);
|
||||
}
|
||||
|
||||
|
||||
PIIODevice * PIIODevice::createFromFullPath(const PIString & full_path) {
|
||||
PIString prefix = full_path.left(full_path.find(":"));
|
||||
if (prefix.isEmpty()) return 0;
|
||||
PIVector<const PIObject * > rd(PICollection::groupElements("__PIIODevices__"));
|
||||
piForeachC (PIObject * d, rd)
|
||||
if (prefix == ((const PIIODevice * )d)->fullPathPrefix()) {
|
||||
PIIODevice * nd = ((const PIIODevice * )d)->copy();
|
||||
if (nd) nd->configureFromFullPath(full_path.mid(prefix.length() + 3));
|
||||
return nd;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user