00001 /*************************************************************************** 00002 LOW_device.h - description 00003 ------------------- 00004 begin : Sat Jul 6 2002 00005 copyright : (C) 2002 by Harald Roelle 00006 email : roelle@informatik.uni-muenchen.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef LOW_DEVICE_H 00019 #define LOW_DEVICE_H 00020 00021 00022 #include <map> 00023 00024 #include "LOW_link.h" 00025 #include "LOW_deviceIDRaw.h" 00026 #include "LOW_deviceID.h" 00027 #include "LOW_exception.h" 00028 00029 class LOW_netSegment; // forward declaration needed to resolve circular definitions. 00030 00031 00032 /** Abstract base class for all 1-Wire devices. 00033 00034 Any device class representing a concrete 1-Wire device must inherit from this class. 00035 00036 This class is thread-safe. 00037 00038 @author Harald Roelle 00039 */ 00040 class LOW_device { 00041 00042 //======================================================================================= 00043 public: 00044 00045 //===================================================================================== 00046 // 00047 // exceptions 00048 // 00049 00050 /** Exception base class for all exceptions thrown by LOW_device. */ 00051 class_DERIVE_FROM_EXCEPTION( device_error, LOW_exception); 00052 00053 /** Exception class indicating a mismatch of expected/required family code. */ 00054 class_DERIVE_FROM_EXCEPTION( familyMismatch_error, device_error); 00055 00056 /** Exception class indicating that the requested or any device could not be found. 00057 This exception is also used by many other classes. 00058 */ 00059 class_DERIVE_FROM_EXCEPTION( noDevice_error, device_error); 00060 00061 /** Exception class indicating that an illegal dynamic type cast on a device occured. 00062 This exception is also used by many other classes. 00063 */ 00064 class_DERIVE_FROM_EXCEPTION( illegalCast_error, device_error); 00065 00066 00067 //===================================================================================== 00068 // 00069 // constants 00070 // 00071 00072 /** Pseudo family code for selecting any/all device types. */ 00073 static const LOW_deviceIDRaw::devFamCode_t anyDev_famCode = 0x00; 00074 00075 /** Pseudo family code for unknown device types. */ 00076 static const LOW_deviceIDRaw::devFamCode_t unknownDev_famCode = 0xff; 00077 00078 /** Family code of this base class equals the one for any device type. 00079 <B>Note:</B> Subclasses must override this constant to return their specific family code. 00080 */ 00081 static const LOW_deviceIDRaw::devFamCode_t familyCode = anyDev_famCode; 00082 00083 /** Family name of this base class. 00084 <B>Note:</B> Subclasses must override this constant to return their specific family name. 00085 */ 00086 static const std::string familyName; 00087 00088 static const owCommand_t MatchROM_COMMAND = 0x55; /**< 1-Wire command byte constant */ 00089 static const owCommand_t ReadROM_COMMAND = 0x33; /**< 1-Wire command byte constant */ 00090 static const owCommand_t SkipROM_COMMAND = 0xcc; /**< 1-Wire command byte constant */ 00091 static const owCommand_t SearchROM_COMMAND = 0xf0; /**< 1-Wire command byte constant */ 00092 static const owCommand_t SearchAlarmROM_COMMAND = 0xec; /**< 1-Wire command byte constant */ 00093 00094 00095 //===================================================================================== 00096 // 00097 // type definitions 00098 // 00099 00100 typedef std::vector<LOW_device*> devPtrVec_t; /**< Vector type of class device pointers. */ 00101 typedef std::map<LOW_deviceID,LOW_device*> deviceMap_t; /**< Map type of devices with LOW_deviceID as key. */ 00102 00103 00104 //===================================================================================== 00105 // 00106 // classes 00107 // 00108 00109 /** Locking class to ensure exclusive access to a device. 00110 00111 The class is intended to be used in a "locking is creation" design pattern. 00112 On creation an exclusive loxk is optained for the device, and on destruction 00113 the lock is released. 00114 Implemented by obtaining a LOW_link::commLock for the link the device is on. 00115 */ 00116 class linkLock : public LOW_link::commLock 00117 { 00118 public: 00119 /** Obtain the lock. 00120 00121 @param inDev Reference to the device to obtain the lock for. 00122 */ 00123 linkLock( const LOW_device &inDev); 00124 00125 /** Release the lock. */ 00126 ~linkLock(); 00127 }; 00128 00129 00130 //===================================================================================== 00131 // 00132 // constructors 00133 // 00134 00135 /** Destructor. 00136 Deregisters the device from its LOW_netSegment. 00137 */ 00138 virtual ~LOW_device(); 00139 00140 00141 //===================================================================================== 00142 // 00143 // methods 00144 // 00145 00146 /** Get the device's family code. 00147 <B>Note:</B> Subclasses must implement this method to return their specific family code. 00148 @return Family name of the device. 00149 */ 00150 virtual const LOW_deviceIDRaw::devFamCode_t getFamilyCode() const { return familyCode; }; 00151 00152 /** Get the device's family name. 00153 <B>Note:</B> Subclasses must implement this method to return a clear text 00154 name of their family. 00155 @return Family name of the device. 00156 */ 00157 virtual const std::string getFamilyName() const { return familyName; }; 00158 00159 /** Get the device's ROM ID. 00160 @return ROM ID of the device. 00161 */ 00162 virtual LOW_deviceID getID() const; 00163 00164 /** Get the network segment the device is on. 00165 @return Network segment the device is on. 00166 */ 00167 virtual LOW_netSegment& getNetSegment() const; 00168 00169 /** Shortcut method to verify the presence of the device on it's network segment. 00170 @see LOW_netSegment::verifyDevice() 00171 */ 00172 virtual bool verifyDevice( const bool inOnlyAlarm = false, const bool inDoReset = true) const; 00173 00174 00175 00176 //======================================================================================= 00177 protected: 00178 00179 //===================================================================================== 00180 // 00181 // friends 00182 // 00183 00184 friend class linkLock; /**< Needed to grant access to the protected getLink() method. */ 00185 00186 00187 //===================================================================================== 00188 // 00189 // attributes 00190 // 00191 00192 const LOW_deviceID ID; /**< 1-Wire ROM ID of the device. */ 00193 LOW_netSegment &netSegment; /**< Network segment where the device is located. */ 00194 00195 00196 //===================================================================================== 00197 // 00198 // constructors 00199 // 00200 00201 /** Base constructor for devices. 00202 00203 <B>Note:</B> In asymmetry to the destructor no action regarding the 00204 network segment's device maps is done here. This is already 00205 performed by LOW_netSegment. 00206 00207 @param inSegment Reference to the network segment the device is on. 00208 @param inDevID Reference to device's ID. 00209 @param inFamCode Expected familiy code for the device. 00210 @throw familyMismatch_error Thrown when <I>inFamCode</I> and familiy code 00211 of <I>inDevID</I> don't match. 00212 */ 00213 LOW_device( LOW_netSegment &inSegment, const LOW_deviceID &inDevID, 00214 const LOW_deviceIDRaw::devFamCode_t inFamCode); 00215 00216 00217 //===================================================================================== 00218 // 00219 // static methods 00220 // 00221 00222 /** Static method for creating new concrete device objects. 00223 This method is de-facto virtual as no implementation is given here 00224 and so it must be implemented individually by every subclass. 00225 The returned object must be dynamically allocated (i.e. it must 00226 be disposable by the delete operator). 00227 00228 @param inNetSegment Reference to the network segment the device is on. 00229 @param inDevID Reference to the device's ID. 00230 @returns The new dynamically created device instance. 00231 */ 00232 static LOW_device* new_Instance( LOW_netSegment &inNetSegment, const LOW_deviceID &inDevID); 00233 00234 00235 //===================================================================================== 00236 // 00237 // methods 00238 // 00239 00240 /** Get the link the device is on. 00241 @return Link the device is on. 00242 */ 00243 virtual LOW_link& getLink() const; 00244 00245 /** Shortcut for issuing a matchROM command for a device. 00246 Calls the corresponding method in LOW_netSegment. 00247 */ 00248 virtual void cmd_MatchROM() const; 00249 00250 }; 00251 00252 00253 #endif