00001 /* 00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico 00003 00004 This file is part of MeRMaID. 00005 00006 MeRMaID is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 MeRMaID is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with MeRMaID. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 00021 00022 #ifndef _DEVICE_HPP_ 00023 #define _DEVICE_HPP_ 00024 00025 00026 #include <ActiveObject.hpp> 00027 #include "DeviceConfigurator.hpp" 00028 #include "StreamOperationHandlerMethod.hpp" 00029 #include "DeviceConfigurator.hpp" 00030 00031 namespace mermaid 00032 { 00033 namespace support 00034 { 00035 namespace io 00036 { 00037 00038 using boost::shared_ptr; 00039 using mermaid::support::activeobject::ActiveObject; 00040 00041 /** 00042 * @Class Device Device.hpp "Device.hpp" 00043 * @Description Device class 00044 * 00045 * This is the class which represents a device. 00046 * @Author Joao Carreira 00047 */ 00048 class Device 00049 { 00050 public: 00051 00052 /** 00053 * @Description Constructor. 00054 * 00055 * @Argument expectedDataSize The size of the data to be read from the device. 00056 * @Argument fileDescriptor The device's file descriptor. 00057 * @Argument owner The ActiveObject of the service using the device. 00058 * @Argument deviceConfigurator The configurator of the file descriptor 00059 * @Author Joao Carreira 00060 */ 00061 Device (int expectedDataSize, int fileDescriptor, shared_ptr<ActiveObject> owner, shared_ptr<DeviceConfigurator> deviceConfigurator); 00062 virtual ~Device(); 00063 00064 /** 00065 * @Description Call the handler which processes read replies 00066 * 00067 * @Argument bytesRead The number of bytes read 00068 * @Argument requestedBytes The number of bytes requested to be read. 00069 * @Argument data The data which was read. 00070 * @Argument success Boolean value which tells if the operation was successful. 00071 * @Author Joao Carreira 00072 */ 00073 virtual void callReadHandler (size_t bytesRead, size_t requestedBytes, const char* data, bool success); 00074 00075 /** 00076 * @Description Call the handler which processes write replies 00077 * 00078 * @Argument bytesWritten The number of bytes written 00079 * @Argument requestedBytes The number of bytes requested to be written 00080 * @Argument data The data which was written 00081 * @Argument success Boolean value which tells if the operation was successful. 00082 * @Author Joao Carreira 00083 */ 00084 virtual void callWriteHandler (size_t bytesWritten, size_t requestedBytes, const char* data, bool success, shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerWrite); 00085 00086 /** 00087 * @Description Starts reading from the device and sets the callback method. 00088 * 00089 * @Argument streamOperationHandlerRead The handler which will receive read replies. 00090 * @Author Joao Carreira 00091 */ 00092 virtual void enableRead (shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerRead); 00093 00094 /** 00095 * @Description Enables write operations on this device 00096 * 00097 * @Author Joao Carreira 00098 */ 00099 virtual void enableWrite(); 00100 00101 /** 00102 * @Description Writes data to the device. 00103 * 00104 * @Argument bytesToWrite Number of bytes to be written 00105 * @Argument data Data to be written 00106 * @Author Joao Carreira 00107 */ 00108 virtual void write (size_t bytesToWrite, const char* data, shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerWrite); 00109 00110 /** 00111 * @Description File descriptor getter 00112 * 00113 * @Returns The device's file descriptor 00114 * @Author Joao Carreira 00115 */ 00116 virtual int getFileDescriptor() const { 00117 return fileDescriptor_; 00118 } 00119 00120 /** 00121 * @Description Expected data size getter 00122 * 00123 * @Returns The expected size (in bytes) of the read data 00124 * @Author Joao Carreira 00125 */ 00126 virtual int getExpectedDataSize() const { 00127 return expectedDataSize_; 00128 } 00129 00130 /** 00131 * @Description Closes the device 00132 * 00133 * @Author Joao Carreira 00134 */ 00135 virtual void close(); 00136 00137 /** 00138 * @Description Configures the device 00139 * 00140 * @Author Joao Carreira 00141 */ 00142 virtual void configureDevice (shared_ptr<DeviceConfigurator>); 00143 00144 private: 00145 int expectedDataSize_; 00146 int fileDescriptor_; 00147 00148 bool readEnabled_; 00149 bool writeEnabled_; 00150 00151 shared_ptr<DeviceConfigurator> deviceConfigurator_; 00152 shared_ptr<ActiveObject> owner_; 00153 00154 shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerRead_; 00155 // shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerWrite_; 00156 00157 }; // class Device 00158 00159 } // namespace io 00160 } // namespace support 00161 } // namespace mermaid 00162 00163 #endif //_DEVICE_HPP_ 00164