git-svn-id: svn://db.shs.com.ru/pip@284 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-11-30 12:21:53 +00:00
parent e66e78ac16
commit 8b72323dd1
25 changed files with 254 additions and 227 deletions

View File

@@ -63,6 +63,14 @@ public:
ReadWrite /*! Device can both read and write */ = 0x03
};
//! \brief Options for PIIODevice, works with some devices
enum DeviceOption {
BlockingRead /*! \a read block until data is received, default off */ = 0x01,
BlockingWrite /*! \a write block until data is sent, default off */ = 0x02
};
typedef PIFlags<DeviceOption> DeviceOptions;
PIIODevice(const PIString & path, DeviceMode mode = ReadWrite);
virtual ~PIIODevice();
@@ -71,7 +79,19 @@ public:
//! Set open mode of device
void setMode(DeviceMode m) {mode_ = m;}
//! Current device options
DeviceOptions options() const {return options_;}
//! Current device option "o" state
bool isOptionSet(DeviceOption o) const {return options_[o];}
//! Set device options
void setOptions(DeviceOptions o);
//! Set device option "o" to "yes" and return previous state
bool setOption(DeviceOption o, bool yes = true);
//! Current path of device
PIString path() const {return property(PIStringAscii("path")).toString();}
@@ -168,22 +188,22 @@ public:
void stop(bool wait = false) {stopThreadedRead(); stopThreadedWrite(); if (wait) while (write_thread.isRunning() || isRunning()) msleep(1);}
//! Reimplement this function to read from your device
virtual int read(void * read_to, int max_size) {piCoutObj << "\"read\" is not implemented!"; return -2;}
//! Read from device maximum "max_size" bytes to "read_to"
int read(void * read_to, int max_size) {return readDevice(read_to, max_size);}
//! Reimplement this function to write to your device
virtual int write(const void * data, int max_size) {piCoutObj << "\"write\" is not implemented!"; return -2;}
//! Read from device maximum "max_size" bytes and return them as PIByteArray
PIByteArray read(int max_size) {buffer_in.resize(max_size); int ret = read(buffer_in.data(), max_size); if (ret < 0) return PIByteArray(); return buffer_in.resized(ret);}
PIByteArray read(int max_size) {buffer_in.resize(max_size); int ret = readDevice(buffer_in.data(), max_size); if (ret < 0) return PIByteArray(); return buffer_in.resized(ret);}
//! Write maximum "max_size" bytes of "data" to device
int write(const void * data, int max_size) {return writeDevice(data, max_size);}
//! Write "data" to device
int write(const PIByteArray & data) {return writeDevice(data.data(), data.size_s());}
//! Read from device for "timeout_ms" milliseconds and return readed data as PIByteArray. Timeout should to be greater than 0
PIByteArray readForTime(double timeout_ms);
//! Write "data" to device
int write(const PIByteArray & data) {return write(data.data(), data.size_s());}
//! Add task to threaded write queue and return task ID
ullong writeThreaded(const void * data, int max_size) {return writeThreaded(PIByteArray(data, uint(max_size)));}
@@ -200,15 +220,20 @@ public:
virtual PIString fullPathPrefix() const {return PIString();}
//! Reimplement to construct full unambiguous string, describes this device, default returns \a fullPathPrefix() + "://" + \a path()
virtual PIString constructFullPath() const {return fullPathPrefix() + "://" + path();}
PIString constructFullPath() const;
//! Reimplement to configure your device with parameters of full unambiguous string. Default implementation does nothing
void configureFromFullPath(const PIString & full_path);
//! \brief Try to determine suitable device, create new one, configure it with \a configureFromFullPath() and returns it.
//! \details To function \a configureFromFullPath() "full_path" passed without \a fullPathPrefix() + "://".
//! See \ref PIIODevice_sec7
static PIIODevice * createFromFullPath(const PIString & full_path);
static PIString normalizeFullPath(const PIString & full_path);
static void splitFullPath(PIString fpwm, PIString * full_path, DeviceMode * mode = 0, DeviceOptions * opts = 0);
EVENT_HANDLER(bool, open) {if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
EVENT_HANDLER1(bool, open, const PIString &, _path) {setPath(_path); if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
@@ -262,7 +287,7 @@ public:
//! \brief Raise if read thread succesfull read some data
//! \fn void threadedWriteEvent(ullong id, int written_size)
//! \brief Raise if write thread succesfull write some data of task with ID "id"
//! \brief Raise if write thread successfull write some data of task with ID "id"
//! \}
//! \ioparams
@@ -292,19 +317,31 @@ protected:
//! Reimplement to close device, inverse return value will be set to "opened_" variable
virtual bool closeDevice() {return true;} // use path_, type_, opened_, init_ variables
//! Reimplement this function to read from your device
virtual int readDevice(void * read_to, int max_size) {piCoutObj << "\"read\" is not implemented!"; return -2;}
//! Reimplement this function to write to your device
virtual int writeDevice(const void * data, int max_size) {piCoutObj << "\"write\" is not implemented!"; return -2;}
//! Function executed when thread read some data, default implementation execute external slot "ret_func_"
virtual bool threadedRead(uchar * readed, int size);
//! Reimplement to construct full unambiguous string, describes this device. Default implementation returns \a path()
virtual PIString constructFullPathDevice() const {return path();}
//! Reimplement to configure your device with parameters of full unambiguous string. Default implementation does nothing
virtual void configureFromFullPath(const PIString & full_path) {;}
virtual void configureFromFullPathDevice(const PIString & full_path) {;}
//! Reimplement to apply new device options
virtual void optionsChanged() {;}
void terminate();
DeviceMode mode_;
DeviceOptions options_;
ReadRetFunc ret_func_;
bool opened_;
void * ret_data_;
@@ -314,6 +351,7 @@ private:
EVENT_HANDLER(void, write_func);
virtual PIIODevice * copy() const {return 0;}
PIString fullPathOptions() const;
void _init();
void begin();
void run();