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

Smi_Linux.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 // compat header should always be first header if including system headers
00020 #define LIBSMBIOS_SOURCE
00021 #include "smbios/compat.h"
00022 
00023 #include <sys/file.h>
00024 
00025 #include "SmiImpl.h"
00026 
00027 using namespace std;
00028 
00029 #define SMI_DATA_FILE       "/sys/devices/platform/dcdbas/smi_data"
00030 #define SMI_PHYS_ADDR_FILE  "/sys/devices/platform/dcdbas/smi_data_buf_phys_addr"
00031 #define SMI_DO_REQUEST_FILE "/sys/devices/platform/dcdbas/smi_request"
00032 #define SMI_BUF_SIZE_FILE   "/sys/devices/platform/dcdbas/smi_data_buf_size"
00033 
00034 //#define SMI_DATA_FILE       "/tmp/smi_data"
00035 //#define SMI_PHYS_ADDR_FILE  "/tmp/smi_data_buf_phys_addr"
00036 //#define SMI_DO_REQUEST_FILE "/tmp/smi_request"
00037 
00038 struct smiLinuxPrivateData
00039 {
00040     FILE *fh_data;
00041     FILE *fh_doReq;
00042 };
00043 
00044 namespace smi
00045 {
00046     SmiArchStrategy::SmiArchStrategy()
00047     {
00048         privateData = new smiLinuxPrivateData;
00049         memset(privateData, 0, sizeof(smiLinuxPrivateData));
00050     }
00051 
00052     SmiArchStrategy::~SmiArchStrategy()
00053     {
00054         smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
00055 
00056         if(tmpPrivPtr->fh_data)
00057             fclose(tmpPrivPtr->fh_data);
00058 
00059         if(tmpPrivPtr->fh_doReq)
00060             fclose(tmpPrivPtr->fh_doReq);
00061 
00062         delete tmpPrivPtr;
00063         privateData = 0;
00064     }
00065 
00066     void SmiArchStrategy::lock()
00067     {
00068         smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
00069 
00070 
00071         tmpPrivPtr->fh_data = fopen(SMI_DATA_FILE, "r+b");
00072         if( ! tmpPrivPtr->fh_data )
00073             throw smbios::InternalErrorImpl("Could not open file " SMI_DATA_FILE ". Check that dcdbas driver is properly loaded.");
00074 
00075         tmpPrivPtr->fh_doReq = fopen(SMI_DO_REQUEST_FILE, "wb");
00076         if( ! tmpPrivPtr->fh_doReq)
00077             throw smbios::InternalErrorImpl("Could not open file " SMI_DO_REQUEST_FILE ". Check that dcdbas driver is properly loaded.");
00078 
00079         flock( fileno(tmpPrivPtr->fh_data), LOCK_EX );
00080 
00081         fseek(tmpPrivPtr->fh_doReq, 0L, 0);
00082         fwrite("0", 1, 1, tmpPrivPtr->fh_doReq);
00083         fseek(tmpPrivPtr->fh_doReq, 0L, 0);
00084     }
00085 
00086     size_t SmiArchStrategy::getPhysicalBufferBaseAddress()
00087     {
00088         const int bufSize=63;
00089         char tmpBuf[bufSize+1] = {0,};
00090         size_t retval = 0;
00091 
00092         fflush(NULL);
00093 
00094         FILE *fh = fopen(SMI_PHYS_ADDR_FILE, "rb");
00095         if( ! fh )
00096             throw smbios::InternalErrorImpl("Could not open file " SMI_PHYS_ADDR_FILE ". Check that dcdbas driver is properly loaded.");
00097 
00098         fseek(fh, 0L, 0);
00099         fread(tmpBuf, 1, bufSize, fh);
00100         fclose(fh);
00101         fh=0;
00102 
00103         retval = strtoll(tmpBuf, NULL, 16);
00104 
00105         return retval;
00106     }
00107 
00108     void SmiArchStrategy::setSize(int newSize)
00109     {
00110         const int bufSize=63;
00111         char tmpBuf[bufSize+1] = {0,};
00112 
00113         fflush(NULL);
00114 
00115         FILE *fh = fopen(SMI_BUF_SIZE_FILE, "w+b");
00116         if( ! fh )
00117             throw smbios::InternalErrorImpl("Could not open file " SMI_BUF_SIZE_FILE ". Check that dcdbas driver is properly loaded.");
00118 
00119         snprintf(tmpBuf, bufSize, "%d", newSize);
00120         fwrite(tmpBuf, 1, bufSize, fh);
00121         fclose(fh);
00122 
00123         fflush(NULL);
00124         fh=0;
00125     }
00126 
00127     void SmiArchStrategy::addInputBuffer(u8 *buffer, size_t size)
00128     {
00129         smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
00130         fwrite(buffer,  1,  size,  tmpPrivPtr->fh_data);
00131     }
00132 
00133     void SmiArchStrategy::getResultBuffer(u8 *buffer, size_t size)
00134     {
00135         smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
00136         fflush(NULL);
00137         fread(buffer,  1,  size,  tmpPrivPtr->fh_data);
00138     }
00139 
00140 
00141     void SmiArchStrategy::execute()
00142     {
00143         smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
00144         fflush(NULL);
00145         fwrite("1", 1, 1, tmpPrivPtr->fh_doReq);
00146         fflush(NULL);
00147         fseek(tmpPrivPtr->fh_data, 0L, 0);
00148     }
00149 
00150     void SmiArchStrategy::finish()
00151     {
00152         smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
00153         flock( fileno(tmpPrivPtr->fh_data), LOCK_UN );
00154         fclose(tmpPrivPtr->fh_doReq);
00155         fclose(tmpPrivPtr->fh_data);
00156 
00157         tmpPrivPtr->fh_doReq=0;
00158         tmpPrivPtr->fh_data=0;
00159     }
00160 }
00161 

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