00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "LOW_netSegment.h"
00019 #include "LOW_deviceFactory.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028 __LOW_SYNCHRONIZE_INIT_PROTECTED_LOCK__(LOW_netSegment)
00029
00030
00031
00032
00033
00034
00035
00036 LOW_netSegment::LOW_netSegment( LOW_link &inLink) :
00037 segmentID( LOW_objectIDFactory::getNewObjectID()),
00038 link( inLink)
00039 {
00040 __LOW_SYNCHRONIZE_METHOD_WRITE__
00041
00042 hasExternalPower = link.getHasExternalPower();
00043
00044 searchDevices<LOW_device>();
00045 }
00046
00047
00048 LOW_netSegment::~LOW_netSegment()
00049 {
00050 for( LOW_device::deviceMap_t::const_iterator a=aliveDevMap.begin(); a!=aliveDevMap.end(); ++a)
00051 delete a->second;
00052
00053 for( LOW_device::deviceMap_t::const_iterator a=graveyardMap.begin(); a!=graveyardMap.end(); ++a)
00054 delete a->second;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064 LOW_netSegment::netSegPtrVec_t LOW_netSegment::newSegmentsFromLink( LOW_link &inLink)
00065 {
00066 __LOW_SYNCHRONIZE_STATIC_WRITE__
00067
00068 netSegPtrVec_t segments;
00069
00070
00071
00072
00073 segments.push_back( new LOW_netSegment( inLink));
00074
00075 return segments;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 bool LOW_netSegment::operator==(LOW_netSegment &inSegment) const
00085 {
00086 __LOW_SYNCHRONIZE_METHOD_READ_WEAK__
00087
00088 return (segmentID==inSegment.segmentID);
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098 LOW_link& LOW_netSegment::getLink()
00099 {
00100 __LOW_SYNCHRONIZE_METHOD_READ_WEAK__
00101
00102 return link;
00103 }
00104
00105
00106 bool LOW_netSegment::getHasExternalPower() const
00107 {
00108 __LOW_SYNCHRONIZE_METHOD_READ_WEAK__
00109
00110 return hasExternalPower;
00111 }
00112
00113
00114 bool LOW_netSegment::verifyDevice( const LOW_deviceID inDevID, const bool inOnlyAlarm, const bool inDoReset)
00115 {
00116 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00117
00118 bool isPresent = cmd_SearchROMVerify( inDevID, inOnlyAlarm, inDoReset);
00119
00120 if ( !inOnlyAlarm ) {
00121 if ( isPresent ) {
00122 addDevice( inDevID);
00123 }
00124 else {
00125 addDevice( inDevID);
00126 try {
00127 LOW_device *theDev = getDevice<LOW_device>( inDevID);
00128 revitalizeDevice( theDev);
00129 }
00130 catch ( LOW_exception ex) {
00131 return false;
00132 }
00133 }
00134 }
00135
00136 return isPresent;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 void LOW_netSegment::unregisterDevice( const LOW_device *inDev)
00147 {
00148 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00149
00150 removeDevice( inDev);
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 void LOW_netSegment::cmd_MatchROM( const LOW_device *inDevice) const
00161 {
00162 __LOW_SYNCHRONIZE_METHOD_READ_WEAK__
00163
00164 LOW_link::commLock lock( link);
00165
00166 if ( link.resetBus() == false )
00167 throw noDevice_error( "Reset indicated no devices", __FILE__, __LINE__);
00168
00169 byteVec_t outVec;
00170
00171 outVec.push_back( static_cast<uint8_t>( LOW_device::MatchROM_COMMAND));
00172
00173 byteVec_t id = inDevice->getID().getRomIDVec();
00174 outVec.insert( outVec.end(), id.begin(), id.end());
00175
00176 link.writeData( outVec);
00177 }
00178
00179
00180 LOW_deviceID LOW_netSegment::cmd_ReadROM() const
00181 {
00182 __LOW_SYNCHRONIZE_METHOD_READ__
00183
00184 LOW_link::commLock lock( link);
00185
00186 if ( link.resetBus() == false )
00187 throw noDevice_error( "Reset indicated no devices", __FILE__, __LINE__);
00188
00189 link.writeData( LOW_device::ReadROM_COMMAND);
00190
00191 byteVec_t idBytes = byteVec_t( sizeof( LOW_deviceIDRaw::devRomID_t));
00192
00193 link.readData( idBytes);
00194
00195 return LOW_deviceID( idBytes);
00196 }
00197
00198
00199 void LOW_netSegment::cmd_SkipROM() const
00200 {
00201 __LOW_SYNCHRONIZE_METHOD_READ__
00202
00203 LOW_link::commLock lock( link);
00204
00205 if ( link.resetBus() == false )
00206 throw noDevice_error( "Reset indicated no devices", __FILE__, __LINE__);
00207
00208 link.writeData( LOW_device::SkipROM_COMMAND);
00209 }
00210
00211
00212 LOW_deviceID::deviceIDVec_t LOW_netSegment::cmd_SearchROM( const bool inOnlyAlarm,
00213 const LOW_deviceIDRaw::devFamCode_t inFamCode) const
00214 {
00215 __LOW_SYNCHRONIZE_METHOD_READ_WEAK__
00216
00217 return link.searchDevices( inOnlyAlarm, LOW_deviceIDRaw() , inFamCode, true);
00218 }
00219
00220
00221 bool LOW_netSegment::cmd_SearchROMVerify( const LOW_deviceID inDevID, const bool inOnlyAlarm, const bool inDoReset) const
00222 {
00223 __LOW_SYNCHRONIZE_METHOD_READ_WEAK__
00224
00225
00226 LOW_deviceID::deviceIDVec_t results = link.searchDevices( inOnlyAlarm, inDevID, LOW_device::anyDev_famCode, inDoReset);
00227
00228 if ( results.size()>0 && results[0]==inDevID )
00229 return true;
00230 else
00231 return false;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241 void LOW_netSegment::buryDevice( const LOW_device *inDev)
00242 {
00243 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00244
00245
00246 LOW_device::deviceMap_t::iterator foundAlive = aliveDevMap.find( inDev->getID());
00247 if ( foundAlive == aliveDevMap.end() ) {
00248 return;
00249 }
00250
00251 LOW_device *theDev = foundAlive->second;
00252 aliveDevMap.erase( foundAlive);
00253
00254 graveyardMap[theDev->getID()] = theDev;
00255
00256 }
00257
00258
00259 void LOW_netSegment::revitalizeDevice( const LOW_device *inDev)
00260 {
00261 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00262
00263
00264 LOW_device::deviceMap_t::iterator foundGraveyard = graveyardMap.find( inDev->getID());
00265 if ( foundGraveyard == graveyardMap.end() ) {
00266 return;
00267 }
00268
00269 LOW_device *theDev = foundGraveyard->second;
00270 graveyardMap.erase( foundGraveyard);
00271
00272 aliveDevMap[theDev->getID()] = theDev;
00273
00274 }
00275
00276
00277 LOW_device* LOW_netSegment::addDevice( const LOW_deviceID inDevID)
00278 {
00279 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00280
00281
00282 LOW_device::deviceMap_t::iterator foundAlive = aliveDevMap.find( inDevID);
00283 if ( foundAlive != aliveDevMap.end() ) {
00284 return foundAlive->second;
00285 }
00286
00287
00288 LOW_device::deviceMap_t::iterator foundGraveyard = graveyardMap.find( inDevID);
00289 if ( foundGraveyard != graveyardMap.end() ) {
00290 LOW_device *theDev = foundGraveyard->second;
00291 revitalizeDevice( theDev);
00292 return theDev;
00293 }
00294
00295
00296 LOW_device* newDev = LOW_deviceFactory::new_SpecificDevice( *this, inDevID);
00297 aliveDevMap[inDevID] = newDev;
00298 return newDev;
00299 }
00300
00301
00302 void LOW_netSegment::removeDevice( const LOW_device *inDev)
00303 {
00304 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00305
00306 const LOW_deviceID devID = inDev->getID();
00307
00308
00309 LOW_device::deviceMap_t::iterator foundAlive = aliveDevMap.find( inDev->getID());
00310 if ( foundAlive != aliveDevMap.end() ) {
00311 aliveDevMap.erase( foundAlive);
00312 return;
00313 }
00314
00315
00316 LOW_device::deviceMap_t::iterator foundGraveyard = graveyardMap.find( inDev->getID());
00317 if ( foundGraveyard != graveyardMap.end() ) {
00318 graveyardMap.erase( foundGraveyard);
00319 return;
00320 }
00321 }
00322