server/module.c

00001 /*
00002  * Copyright Ian Burnett 2005, 2006.
00003  *
00004  * This file is part of Ian's Interactive LCD controller (IILC).
00005  * 
00006  * IILC is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU General Public License as published by the Free
00008  * Software Foundation; either version 2 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * IILC is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00014  * for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with IILC; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <errno.h>
00024 #include <string.h>
00025 
00026 #if defined(_WIN32)
00027 #   include "pthread_w32.h"
00028 #else
00029 #   include <pthread.h>
00030 #   include <dlfcn.h>
00031 #endif
00032 
00033 #include "trace.h"
00034 #include "module.h"
00035 
00036 /* Each platform has its own way of dealing with dynamic libraries */
00037 #if defined(_WIN32)
00038 #   define LIB_NAME_PREFIX
00039 #   define LIB_NAME_SUFFIX ".dll"
00040 #else
00041 #   define LIB_NAME_PREFIX "lib"
00042 #   define LIB_NAME_SUFFIX ".so"
00043 #endif
00044 
00045 
00046 #define IILC_MODULE MODULE_TYPE_APP
00047 
00048 
00049 /* ------------------------------------------------------------------------ *
00050  *                                                                          *
00051  * Module creation / destruction routines                                   *
00052  *                                                                          *
00053  * ------------------------------------------------------------------------ */
00054 
00055 
00056 
00057 
00058 int moduleInit(const char * moduleName, LPMODULE * ppModule)
00059 {
00060     int rc = 0;
00061     char libraryName[512];
00062     char * dlError = NULL;
00063 
00064     LPMODULE          pModule  = NULL;
00065     LPMODULE_FN_ARRAY pFnArray = NULL;
00066 
00067     IILC_TRACE_ENTRY(IILC_MODULE, T_moduleInit);
00068     traceDebug(IILC_TRACE_CONTEXT, "moduleName %p, ppModule %p", moduleName, ppModule);
00069 
00070     if (ppModule == NULL) {
00071         traceError(IILC_TRACE_CONTEXT, "ppModule == NULL");
00072         goto end_of_function;
00073     }
00074 
00075     traceDebug(IILC_TRACE_CONTEXT, "Initialising module name %s", moduleName);
00076 
00077     /* Create main module memory */
00078     pModule = malloc(sizeof(MODULE));
00079     if (pModule == NULL) {
00080         goto end_of_function;
00081     }
00082 
00083     /* Initialise the module data */
00084     pModule->libraryHandle = NULL;
00085     pModule->moduleContext = NULL;
00086     pModule->pfnCreate     = NULL;
00087     pModule->pfnDestroy    = NULL;
00088     pModule->pFnArray      = NULL;
00089 
00090     /* All the other functions */
00091     pFnArray = calloc(1, sizeof(MODULE_FN_ARRAY));
00092     
00093     /* A mutex used to serialise comms with the module */
00094     rc = pthread_mutex_init(&(pModule->mtxModule), NULL);
00095     if (rc) {
00096         traceError(IILC_TRACE_CONTEXT, "Error creating module mutex: %s", strerror(errno));
00097         goto end_of_function;
00098     }
00099 
00100     /* Create the full module name */
00101     sprintf(libraryName, LIB_NAME_PREFIX "%s" LIB_NAME_SUFFIX, moduleName);
00102 
00103     /* Dynamically load the library */
00104 #if defined(_WIN32)
00105     pModule->libraryHandle = LoadLibrary(libraryName);
00106 #else
00107     pModule->libraryHandle = dlopen(libraryName, RTLD_NOW);
00108     dlError = dlerror();
00109 #endif
00110 
00111     if (pModule->libraryHandle == NULL) {
00112         traceError(IILC_TRACE_CONTEXT, "Library load failed: %s", dlError);
00113         rc = ERR_INVALID_CONFIG;
00114         goto end_of_function;
00115     }
00116 
00117 #if defined(_WIN32)
00118 #else
00119     dlerror();
00120     pModule->pfnCreate = (pfnModuleCreate) dlsym(pModule->libraryHandle, "ModuleCreate");
00121     dlError = dlerror();
00122 #endif
00123 
00124     if (pModule->pfnCreate == NULL) {
00125         traceError(IILC_TRACE_CONTEXT, "Failed loading module create function: %s", dlError);
00126         goto end_of_function;
00127     }
00128 
00129 #if defined(_WIN32)
00130 #else
00131     dlerror();
00132     pModule->pfnDestroy = (pfnModuleDestroy) dlsym(pModule->libraryHandle, "ModuleDestroy");
00133     dlError = dlerror();
00134 #endif
00135 
00136     if (pModule->pfnDestroy == NULL) {
00137         traceError(IILC_TRACE_CONTEXT, "Failed loading module destroy function: %s", dlError);
00138         goto end_of_function;
00139     }
00140 
00141     /* Assign local var into the main module ptr */
00142     pModule->pFnArray = pFnArray;
00143     
00144     /* Now call the module init routine */
00145     (pModule->pfnCreate)(1, &(pModule->moduleContext), pModule->pFnArray);
00146 
00147     *ppModule = pModule;
00148 
00149 end_of_function:
00150 
00151     /* Tear down if things go wrong */
00152     if (rc) {
00153 
00154 
00155     }
00156 
00157     IILC_TRACE_EXIT_RC(rc);
00158     return rc;
00159 }
00160 
00161 
00162 int moduleUnload(LPMODULE * ppModule)
00163 {
00164     LPMODULE pModule = *ppModule;
00165 
00166     /* Call destroy on the module first */
00167     if (pModule->pfnDestroy != NULL) {
00168         (pModule->pfnDestroy)(pModule->moduleContext);
00169     }
00170 
00171     /* Delete the mutex */
00172     pthread_mutex_destroy(&(pModule->mtxModule));
00173 
00174     /* Release the function array memory */
00175     free(pModule->pFnArray);
00176 
00177     /* Unload the library */
00178 #if defined(_WIN32)
00179     FreeLibrary(pModule->libraryHandle);
00180 #else
00181     dlclose(pModule->libraryHandle);
00182 #endif
00183 
00184     /* Release the module structure memory */
00185     free(pModule);
00186 
00187     *ppModule = NULL;
00188 
00189     return 0;
00190 }
00191 
00192 int moduleReportTemp(LPMODULE pModule, unsigned int sensor_id, double degrees_c, const char * pszName)
00193 {
00194     int rc = 0;
00195 
00196     if (pModule != NULL && pModule->pFnArray->pfnTempReport != NULL) {
00197         (pModule->pFnArray->pfnTempReport)(pModule->moduleContext, sensor_id, degrees_c, pszName);
00198     }
00199 
00200     return rc;
00201 }
00202 
00203 
00204 int moduleReportFan(LPMODULE pModule, unsigned int fan_id, unsigned int rpm, const char * pszName)
00205 {
00206     int rc = 0;
00207 
00208     if (pModule != NULL && pModule->pFnArray->pfnFanSpeedReport != NULL) {
00209         (pModule->pFnArray->pfnFanSpeedReport)(pModule->moduleContext, fan_id, rpm, pszName);
00210     }
00211 
00212     return rc;
00213 }
00214 
00215 
00216 /*
00217 int moduleGetDisplayData(LPMODULE pModule, LPSCREEN pScreen, unsigned int row, char ** buf)
00218 {
00219     int rc = 0;
00220 
00221     if (pModule == NULL) {
00222         rc = -1;
00223         goto end_of_function;
00224     }
00225 
00226     * Lock the module *
00227 
00228     * Invoke the function *
00229     if (pModule->pFnArray->pfnTextData != NULL) {
00230         //rc = (pModule->pFnArray->pfnTextData)(pModule->moduleContext, pScreen, row, buf);
00231     }
00232     else {
00233         *buf[0] = '\0';
00234         rc = -1;
00235     }
00236 
00237     * Unlock the module *
00238 
00239 
00240 end_of_function:
00241 
00242     return rc;
00243 }
00244 */
00245 
00246 
00247 int moduleGetLEDStatus(LPMODULE pModule, unsigned int led_id,
00248                        LPCOLOUR pColourOn, LPCOLOUR pColourOff,
00249                        unsigned int * dutyCycle, unsigned int * freq)
00250 {
00251     int rc = 0;
00252 
00253     if (pModule == NULL) {
00254         rc = -1;
00255         goto end_of_function;
00256     }
00257 
00258     if (pModule->pFnArray->pfnReadLEDStatus == NULL) {
00259         rc = -1;
00260         goto end_of_function;
00261     }
00262 
00263     /* Lock the module */
00264 
00265     /* Invoke the function */
00266     //rc = (pModule->pFnArray->pfnReadLEDStatus)(pModule->moduleContext,
00267       //  led_id, pColourOn, pColourOff, dutyCycle, freq);
00268 
00269     /* Unlock the module */
00270 
00271 
00272 end_of_function:
00273 
00274     return rc;
00275 }
00276 
00277 
00278 
00279 int moduleGetFanPower(LPMODULE pModule, int fan_id, unsigned int * pFanPower)
00280 {
00281     int rc = 0;
00282 
00283     if (pModule == NULL) {
00284         rc = -1;
00285         goto end_of_function;
00286     }
00287 
00288     /* Lock the module */
00289     pthread_mutex_lock(&(pModule->mtxModule));
00290 
00291     /* Invoke the function */
00292     //rc = (pModule->pFnArray->pfnReadFanPower)(pModule->moduleContext, fan_id, pFanPower);
00293 
00294     /* Unlock the module */
00295     pthread_mutex_unlock(&(pModule->mtxModule));
00296 
00297 
00298 end_of_function:
00299 
00300     return rc;
00301 }
00302 
00303 
00304 int moduleRender(LPMODULE pModule, int nRow, int nWidth, char * pBuffer)
00305 {
00306     int rc = 0;
00307 
00308     if (pModule->pFnArray->pfnTextData != NULL) {
00309         (pModule->pFnArray->pfnTextData)(pModule->moduleContext, nRow, nWidth, pBuffer);
00310     }
00311     else {
00312         sprintf(pBuffer, "[NO FN]%.*s", nWidth - 7, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
00313     }
00314 
00315     return rc;
00316 }

Generated on Mon Jul 17 01:36:11 2006 for IILC by  doxygen 1.4.6