libsmbios_c library
|
00001 // vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c: 00002 /* 00003 * Copyright (C) 2005 Dell Inc. 00004 * by Michael Brown <Michael_E_Brown@dell.com> 00005 * Licensed under the Open Software License version 2.1 00006 * 00007 * Alternatively, you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published 00009 * by the Free Software Foundation; either version 2 of the License, 00010 * or (at your option) any later version. 00011 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00015 * See the GNU General Public License for more details. 00016 */ 00017 00018 00019 #ifndef SMBIOSINTERFACE_H 00020 #define SMBIOSINTERFACE_H 00021 00022 // compat header should always be first header 00023 #include "smbios/compat.h" 00024 00025 #include <cstdlib> // Provides size_t and NULL 00026 #include <iostream> 00027 #include <map> 00028 #include <memory> 00029 00030 // types.h should be first user-defined header. 00031 #include "smbios/types.h" 00032 #include "smbios/IFactory.h" 00033 #include "smbios/IException.h" 00034 #include "smbios/SmbiosLowLevel.h" 00035 00036 // abi_prefix should be last header included before declarations 00037 #include "smbios/config/abi_prefix.hpp" 00038 00039 namespace smbios 00040 { 00041 // Exception Classes 00042 DECLARE_EXCEPTION( SmbiosException ); 00043 DECLARE_EXCEPTION_EX( ParameterException, smbios, SmbiosException ); 00044 DECLARE_EXCEPTION_EX( ParseException, smbios, SmbiosException ); 00045 DECLARE_EXCEPTION_EX( StringUnavailable, smbios, SmbiosException ); 00046 DECLARE_EXCEPTION_EX( DataOutOfBounds, smbios, SmbiosException ); 00047 DECLARE_EXCEPTION_EX( ItemNotFound, smbios, SmbiosException ); 00048 00049 00050 //forward declarations... defined 'for real' below... 00051 class ISmbiosTable; 00052 class ISmbiosItem; 00053 class SmbiosTableIterator; 00054 class ConstSmbiosTableIterator; 00055 00057 00071 class SmbiosFactory : public virtual factory::IFactory 00072 { 00073 public: 00075 00085 static SmbiosFactory *getFactory(); 00086 virtual ~SmbiosFactory() throw(); 00087 00089 00095 virtual ISmbiosTable *getSingleton() = 0; 00096 00098 00107 virtual ISmbiosTable *makeNew() = 0; 00108 protected: 00110 SmbiosFactory(); 00111 }; 00112 00114 00117 class ISmbiosTable 00118 { 00119 public: 00120 // Std container typedefs. Everybody expects to 00121 // say 'iterator' or 'const_iterator' 00122 typedef SmbiosTableIterator iterator; 00123 typedef ConstSmbiosTableIterator const_iterator; 00124 00125 // CONSTRUCTORS, DESTRUCTOR, and ASSIGNMENT 00126 ISmbiosTable(); 00127 // Interface class: no default or copy constructor 00128 virtual ~ISmbiosTable (); 00129 00130 // ITERATORS 00131 // 00133 00146 virtual iterator begin () = 0; 00148 00149 virtual const_iterator begin () const = 0; 00150 00152 00153 virtual iterator end () = 0; 00154 00156 00158 virtual const_iterator end () const = 0; 00159 00161 00176 virtual iterator operator[]( const int ) = 0; 00177 00179 00180 virtual const_iterator operator[]( const int ) const = 0; 00181 00182 // MEMBERS 00184 00190 virtual void rawMode(bool m = true) const = 0; 00191 00193 00210 virtual void clearItemCache() const = 0; 00211 00213 virtual int getNumberOfEntries () const = 0; // used by unit-test code 00215 // Used by the validateBios code. 00216 virtual smbiosLowlevel::smbios_table_entry_point getTableEPS() const = 0; 00217 00218 friend class SmbiosTableIteratorBase; 00219 protected: 00220 virtual const ISmbiosItem & getSmbiosItem (const u8 *current) const = 0; 00221 virtual ISmbiosItem & getSmbiosItem (const u8 *current) = 0; 00222 virtual const u8 * nextSmbiosStruct ( const u8 * current = 0) const = 0; 00223 00225 //output table information. 00229 virtual std::ostream & streamify(std::ostream & cout ) const = 0; 00230 friend std::ostream & operator << (std::ostream & cout, const ISmbiosTable & item); 00231 00232 private: 00233 explicit ISmbiosTable(const ISmbiosTable &); 00234 void operator =( const ISmbiosTable & ); 00235 }; 00236 00238 00241 class ISmbiosItem 00242 { 00243 public: 00245 virtual ~ISmbiosItem (); 00246 ISmbiosItem(); 00247 00248 virtual std::auto_ptr<const ISmbiosItem> clone() const = 0; 00249 virtual std::auto_ptr<ISmbiosItem> clone() = 0; 00250 00256 virtual u8 getType() const = 0; 00257 00263 virtual u8 getLength() const = 0; 00264 00270 virtual u16 getHandle() const = 0; 00271 00296 virtual void getData( unsigned int offset, u8 *out, size_t size ) const = 0; 00297 00298 virtual const u8* getBufferCopy(size_t &length) const = 0; 00299 00301 // The validateBios.cpp calls this function. 00302 virtual size_t getBufferSize() const = 0; 00303 00308 virtual const char *getStringByStringNumber (u8) const = 0; 00309 00310 enum { 00311 FIELD_LEN_BYTE=1, 00312 FIELD_LEN_WORD=2, 00313 FIELD_LEN_DWORD=4, 00314 FIELD_LEN_QWORD=8 00315 }; 00316 00317 protected: 00323 virtual std::ostream & streamify( std::ostream & cout ) const = 0; 00324 friend std::ostream & operator << (std::ostream & cout, const ISmbiosItem & item); 00325 }; 00326 00327 u8 getItemType(const ISmbiosItem &item); 00328 u8 getItemLength(const ISmbiosItem &item); 00329 u16 getItemHandle(const ISmbiosItem &item); 00330 00331 u8 getU8_FromItem(const ISmbiosItem &item, unsigned int offset); 00332 u16 getU16_FromItem(const ISmbiosItem &item, unsigned int offset); 00333 u32 getU32_FromItem(const ISmbiosItem &item, unsigned int offset); 00334 u64 getU64_FromItem(const ISmbiosItem &item, unsigned int offset); 00335 const char *getString_FromItem(const ISmbiosItem &item, unsigned int offset); 00336 void *getBits_FromItem(const ISmbiosItem &item, unsigned int offset, void *out, unsigned int lsb=0, unsigned int msb=0 ); 00337 bool isBitSet(const ISmbiosItem *itemPtr, unsigned int offset, unsigned int bitToTest); 00338 00339 template <class R> 00340 R &getData(const ISmbiosItem &item, unsigned int offset, R &out) 00341 { 00342 item.getData(offset, &out, sizeof(R)); 00343 return out; 00344 } 00345 00347 00354 class SmbiosTableIteratorBase 00355 { 00356 public: 00357 typedef std::forward_iterator_tag iterator_category; 00358 typedef std::ptrdiff_t difference_type; 00359 00360 00361 explicit SmbiosTableIteratorBase(const ISmbiosTable * initialTable = 0, int typeToMatch = -1 ); 00362 SmbiosTableIteratorBase &operator=(const SmbiosTableIteratorBase&); 00363 virtual ~SmbiosTableIteratorBase() throw(); 00364 bool operator == (const SmbiosTableIteratorBase &other) const; 00365 bool operator != (const SmbiosTableIteratorBase &other) const; 00366 void incrementIterator (); 00367 const ISmbiosItem & dereference () const; 00368 ISmbiosItem & dereference (); 00369 00370 void reset(); 00371 bool eof(); 00372 00373 protected: 00374 int matchType; 00375 const ISmbiosTable * table; 00376 const u8 * current; 00377 }; 00378 00379 class SmbiosTableIterator: 00380 public SmbiosTableIteratorBase, 00381 public std::iterator < std::forward_iterator_tag, ISmbiosItem > 00382 { 00383 public: 00384 typedef ISmbiosItem value_type; 00385 typedef value_type& reference; 00386 typedef value_type* pointer; 00387 00388 virtual ~SmbiosTableIterator() throw(); 00389 explicit SmbiosTableIterator(ISmbiosTable * initialTable = 0, int typeToMatch = -1 ); 00390 reference operator * (); 00391 pointer operator -> (); 00392 SmbiosTableIterator & operator ++ (); // ++Prefix 00393 const SmbiosTableIterator operator ++ (int); //Postfix++ 00394 }; 00395 00396 class ConstSmbiosTableIterator: 00397 public SmbiosTableIteratorBase, 00398 public std::iterator < std::forward_iterator_tag, const ISmbiosItem > 00399 { 00400 public: 00401 typedef const ISmbiosItem value_type; 00402 typedef value_type& reference; 00403 typedef value_type* pointer; 00404 00405 virtual ~ConstSmbiosTableIterator() throw(); 00406 explicit ConstSmbiosTableIterator(const ISmbiosTable * initialTable = 0, int typeToMatch = -1 ); 00407 ConstSmbiosTableIterator &operator=(const SmbiosTableIteratorBase&); 00408 00409 reference operator * () const; 00410 pointer operator -> () const; 00411 ConstSmbiosTableIterator & operator ++ (); // ++Prefix 00412 const ConstSmbiosTableIterator operator ++ (int); //Postfix++ 00413 }; 00414 00415 // 00416 // Non-member functions 00417 // 00418 std::ostream & operator << (std::ostream & cout, const ISmbiosTable & item); 00419 std::ostream & operator << (std::ostream & cout, const ISmbiosItem & item); 00420 00421 } 00422 00423 00424 // always should be last thing in header file 00425 #include "smbios/config/abi_suffix.hpp" 00426 00427 #endif /* SMBIOSINTERFACE_H */