Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

CmosRW.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
00003  *
00004  * Copyright (C) 2005 Dell Inc.
00005  *  by Michael Brown <Michael_E_Brown@dell.com>
00006  * Licensed under the Open Software License version 2.1
00007  *
00008  * Alternatively, you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published
00010  * by the Free Software Foundation; either version 2 of the License,
00011  * or (at your option) any later version.
00012 
00013  * This program is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  */
00018 
00019 #define LIBSMBIOS_SOURCE
00020 #include "CmosRWImpl.h"
00021 
00022 using namespace std;
00023 
00024 namespace cmos
00025 {
00026 
00027     //
00028     // NON-MEMBER FUNCTIONS
00029     //
00030     void readByteArray ( const ICmosRW &cmos, u32 indexPort, u32 dataPort, u32 offset, u8 * target, u32 count)
00031     {
00032         for (u32 i = 0; i < count; ++i)
00033         {
00034             target[i] = cmos.readByte (indexPort, dataPort, offset + i);
00035         }
00036     }
00037 
00038     void writeByteArray ( const ICmosRW &cmos, u32 indexPort, u32 dataPort, u32 offset, const u8 * source, u32 count)
00039     {
00040         const Suppressable *s = dynamic_cast<const Suppressable *>(&cmos);
00041         if(s)
00042             s->suppressNotification();
00043         for (u32 i = 0; i < count; ++i)
00044         {
00045             cmos.writeByte (indexPort, dataPort, offset + i, source[i]);
00046         }
00047         if(s)
00048             s->resumeNotification();
00049     }
00050 
00051 
00052 
00053     //
00054     // Suppressable
00055     //
00056     // This class is used to supress ->notify() calls inside an Observable
00057     // class. This lets us do many operations that may cause spurious
00058     // notifications. This would also probably let us do some simple
00059     // transaction-like operations.
00060     //
00061     Suppressable::Suppressable()
00062             : suppressNotify(false)
00063     {}
00064 
00065     Suppressable::~Suppressable()
00066     {}
00067 
00068     void Suppressable::suppressNotification(bool sup) const
00069     {
00070         suppressNotify = sup;
00071     }
00072 
00073     void Suppressable::resumeNotification(bool doNotify) const
00074     {
00075         const observer::IObservable *o = dynamic_cast<const observer::IObservable *>(this);
00076         if(o && doNotify)
00077             o->notify();
00078 
00079         suppressNotify = false;
00080     }
00081 
00082     bool Suppressable::isNotifySuppressed() const
00083     {
00084         return suppressNotify;
00085     }
00086 
00087     //
00088     // ICmosRW functions
00089     //
00090     ICmosRW::ICmosRW()
00091     {}
00092 
00093     ICmosRW::~ICmosRW()
00094     {}
00095 
00096     //
00097     // CmosRWFile functions
00098     //
00099 
00100     // REGULAR CONSTRUCTOR
00101     CmosRWFile::CmosRWFile ( const string &File )
00102             :ICmosRW(), Suppressable(), fileName (File)
00103     {}
00104 
00105     // DESTRUCTOR
00106     CmosRWFile::~CmosRWFile()
00107     {}
00108 
00109     CmosRWIo::~CmosRWIo()
00110     {}
00111 
00112     // TODO: need to throw exception on problem with file
00113     //
00114     u8 CmosRWFile::readByte (u32 indexPort, u32 dataPort, u32 offset) const
00115     {
00116         u8 retval = 0xFF;
00117         u32 realOffset = indexPort * 256 + offset;
00118         (void) dataPort; // unused
00119 
00120         FILE *fh = fopen (fileName.c_str (), "rb");
00121         if( !fh )
00122             throw smbios::InternalErrorImpl("Internal Error: could not cmos file for reading.");
00123 
00124         fseek (fh, static_cast<long>(realOffset), SEEK_SET);
00125         fread (&retval, 1, sizeof (retval), fh);
00126         fclose (fh);
00127         return retval;
00128     }
00129 
00130     // TODO: need to throw exception on problem with file
00131     //
00132     void CmosRWFile::writeByte (u32 indexPort, u32 dataPort, u32 offset, u8 byte) const
00133     {
00134         //cout << "w(" << offset << ")";
00135         u32 realOffset = indexPort * 256 + offset;
00136         (void) dataPort; // unused
00137 
00138         FILE *fh = fopen (fileName.c_str (), "r+b");
00139         if( !fh )
00140             throw smbios::InternalErrorImpl("Internal Error: could not cmos file for writing.");
00141 
00142         fseek (fh, static_cast<long>(realOffset), SEEK_SET);
00143         fwrite (&byte, 1, sizeof (byte), fh);
00144         fclose (fh);
00145         fflush(NULL);
00146 
00147         if(! isNotifySuppressed() )
00148         {
00149             // writers are responsible for only writing changed values
00150             // otherwise we get to see how fast our OS can do an
00151             // infinite loop. :-)
00152             notify();
00153         }
00154         return;
00155     }
00156 
00157 
00158 }

Generated on Wed Apr 11 16:25:10 2007 for SMBIOS Library by doxygen 1.3.5