SMBIOS Library
CmosRW.cpp
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
3  *
4  * Copyright (C) 2005 Dell Inc.
5  * by Michael Brown <Michael_E_Brown@dell.com>
6  * Licensed under the Open Software License version 2.1
7  *
8  * Alternatively, you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License,
11  * or (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  */
18 
19 // compat header should always be first header if including system headers
20 #define LIBSMBIOS_SOURCE
21 #include "smbios/compat.h"
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
25 #include "CmosRWImpl.h"
26 
27 using namespace std;
28 
29 namespace cmos
30 {
31 
32  //
33  // NON-MEMBER FUNCTIONS
34  //
35  void readByteArray ( const ICmosRW &cmos, u32 indexPort, u32 dataPort, u32 offset, u8 * target, u32 count)
36  {
37  for (u32 i = 0; i < count; ++i)
38  {
39  target[i] = cmos.readByte (indexPort, dataPort, offset + i);
40  }
41  }
42 
43  void writeByteArray ( const ICmosRW &cmos, u32 indexPort, u32 dataPort, u32 offset, const u8 * source, u32 count)
44  {
45  const Suppressable *s = dynamic_cast<const Suppressable *>(&cmos);
46  if(s)
48  for (u32 i = 0; i < count; ++i)
49  {
50  cmos.writeByte (indexPort, dataPort, offset + i, source[i]);
51  }
52  if(s)
53  s->resumeNotification();
54  }
55 
56 
57 
58  //
59  // Suppressable
60  //
61  // This class is used to supress ->notify() calls inside an Observable
62  // class. This lets us do many operations that may cause spurious
63  // notifications. This would also probably let us do some simple
64  // transaction-like operations.
65  //
66  Suppressable::Suppressable()
67  : suppressNotify(false)
68  {}
69 
71  {}
72 
74  {
75  suppressNotify = sup;
76  }
77 
78  void Suppressable::resumeNotification(bool doNotify) const
79  {
80  const observer::IObservable *o = dynamic_cast<const observer::IObservable *>(this);
81  if(o && doNotify)
82  o->notify();
83 
84  suppressNotify = false;
85  }
86 
88  {
89  return suppressNotify;
90  }
91 
92  //
93  // ICmosRW functions
94  //
96  {}
97 
99  {}
100 
101  //
102  // CmosRWFile functions
103  //
104 
105  // REGULAR CONSTRUCTOR
106  CmosRWFile::CmosRWFile ( const string &File )
107  :ICmosRW(), Suppressable(), fileName (File)
108  {}
109 
110  // DESTRUCTOR
112  {}
113 
115  {}
116 
117  // TODO: need to throw exception on problem with file
118  //
119  u8 CmosRWFile::readByte (u32 indexPort, u32 dataPort, u32 offset) const
120  {
121  u8 retval = 0xFF;
122  u32 realOffset = indexPort * 256 + offset;
123  (void) dataPort; // unused
124  string errMessage("Could not open CMOS file(" + fileName + ") for reading: ");
125 
126  FILE *fh = fopen (fileName.c_str (), "rb");
127  if( !fh )
128  throw smbios::InternalErrorImpl(errMessage + strerror(errno));
129 
130  fseek (fh, static_cast<long>(realOffset), SEEK_SET);
131  size_t numBytes = fread (&retval, 1, sizeof (retval), fh); // only used in unit tests, so isnt critical
132  fclose (fh);
133  if (numBytes != sizeof(retval))
134  throw std::exception(); // short read. there isnt really a good exception to throw here.
135 
136  return retval;
137  }
138 
139  // TODO: need to throw exception on problem with file
140  //
141  void CmosRWFile::writeByte (u32 indexPort, u32 dataPort, u32 offset, u8 byte) const
142  {
143  //cout << "w(" << offset << ")";
144  u32 realOffset = indexPort * 256 + offset;
145  (void) dataPort; // unused
146  string errMessage("Could not open CMOS file(" + fileName + ") for writing: ");
147 
148  FILE *fh = fopen (fileName.c_str (), "r+b");
149  if( !fh )
150  throw smbios::InternalErrorImpl(errMessage + strerror(errno));
151 
152  fseek (fh, static_cast<long>(realOffset), SEEK_SET);
153  fwrite (&byte, 1, sizeof (byte), fh);
154  fclose (fh);
155  fflush(NULL);
156 
157  if(! isNotifySuppressed() )
158  {
159  // writers are responsible for only writing changed values
160  // otherwise we get to see how fast our OS can do an
161  // infinite loop. :-)
162  notify();
163  }
164  return;
165  }
166 
167 
168 }