Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

LOW_netSegment.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           LOW_netSegment.cpp  -  description
00003                              -------------------
00004     begin                : Sun Jul 7 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 #include "LOW_netSegment.h"
00019 #include "LOW_deviceFactory.h"
00020 
00021 
00022 
00023 //=====================================================================================
00024 //
00025 // static attributes
00026 //
00027 
00028 __LOW_SYNCHRONIZE_INIT_PROTECTED_LOCK__(LOW_netSegment)
00029 
00030 
00031 //=====================================================================================
00032 //
00033 // constructors
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>();  //find all devices on bus
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 // static methods
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   /** @warning  By now only a single segment is supported
00071       @todo Add support for multi-segment links
00072    */
00073   segments.push_back( new LOW_netSegment( inLink));
00074 
00075   return segments;
00076 }
00077 
00078 
00079 //=====================================================================================
00080 //
00081 // operator overloading
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 // public methods
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 // protected methods
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 // protected addressing/searching methods
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() /* init with 0 */, 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   //@todo CHECK NEW IMPLEMENTATION
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 // private active map / graveyard management
00239 //
00240 
00241 void LOW_netSegment::buryDevice( const LOW_device *inDev)
00242 {
00243   __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00244 
00245   // look in alive map, if not present simply return
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   // theDev.bury();
00256 }
00257 
00258 
00259 void LOW_netSegment::revitalizeDevice( const LOW_device *inDev)
00260 {
00261   __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00262 
00263   // look in graveyard, if not present simply return
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   // theDev.revitalize();
00274 }
00275 
00276 
00277 LOW_device* LOW_netSegment::addDevice( const LOW_deviceID inDevID)
00278 {
00279   __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00280 
00281   // look in alive map, if present simply return device
00282   LOW_device::deviceMap_t::iterator foundAlive = aliveDevMap.find( inDevID);
00283   if ( foundAlive != aliveDevMap.end() ) {
00284     return foundAlive->second;
00285   }
00286     
00287   // look in graveyard, if present revitalize it and return it
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   // now it must be really new, so create it, add it to alive map and return it
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   // look in alive map, if present delete it and return
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   // look in graveyard, if present delete it and return
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 

Generated on Tue Feb 3 11:30:26 2004 for OneWireLibrary++ by doxygen 1.3.2