rllib  1
rldataacquisition.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           rldataacquisition.cpp  -  description
00003                              -------------------
00004     begin                : Mon Sep 03 2007
00005     copyright            : (C) 2007 by pvbrowser
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include "rldataacquisition.h"
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <ctype.h>
00020 
00021 rlDataAcquisition::rlDataAcquisition(const char *mailbox, const char *shared_memory, long shared_memory_size)
00022 {
00023   mbx = new rlMailbox(mailbox);
00024   shm = new rlSharedMemory(shared_memory,shared_memory_size);
00025   shmheader = (SHM_HEADER *) shm->getUserAdr();
00026   shmvalues = ((const char *)shmheader) + sizeof(SHM_HEADER);
00027   iCurrent = -1;
00028 }
00029 
00030 rlDataAcquisition::~rlDataAcquisition()
00031 {
00032   delete mbx;
00033   delete shm;
00034 }
00035 
00036 const char *rlDataAcquisition::stringValue(const char *variable)
00037 {
00038   int value_offset, delta_index, nmax, i;
00039   const char *cptr;
00040 
00041   if(shmheader == NULL) return "DAQ_ERROR: shmheader==NULL";
00042   if(strcmp(shmheader->ident,"daq") != 0) return "DAQ_ERROR: shmheader->ident is false";
00043   value_offset = shmheader->maxItemNameLength + 1;
00044   delta_index  = value_offset + shmheader->maxNameLength + 1;
00045   nmax         = shmheader->numItems;
00046 
00047   cptr = shmvalues;
00048   for(i=0; i<nmax; i++)
00049   {
00050     if(strcmp(cptr,variable) == 0)
00051     {
00052       return cptr + value_offset;
00053     }
00054     cptr += delta_index;
00055   }
00056 
00057   return "DAQ_ERROR: variable not found in shared memory";
00058 }
00059 
00060 int rlDataAcquisition::intValue(const char *variable)
00061 {
00062   const char *cptr;
00063   int ret;
00064 
00065   cptr = stringValue(variable);
00066   if(isdigit(*cptr))
00067   {
00068     ret = DAQ_ERROR;
00069     sscanf(cptr,"%d",&ret);
00070     return ret;
00071   }
00072   return DAQ_ERROR;
00073 }
00074 
00075 float rlDataAcquisition::floatValue(const char *variable)
00076 {
00077   const char *cptr;
00078   float ret;
00079 
00080   cptr = stringValue(variable);
00081   if(isdigit(*cptr) || *cptr == '-')
00082   {
00083     ret = DAQ_ERROR;
00084     sscanf(cptr,"%f",&ret);
00085     return ret;
00086   }
00087   return DAQ_ERROR;
00088 }
00089 
00090 int rlDataAcquisition::writeStringValue(const char *variable, const char *value)
00091 {
00092   mbx->printf("%s,%s\n",variable,value);
00093   return 0;
00094 }
00095 
00096 int rlDataAcquisition::writeIntValue(const char *variable, int value)
00097 {
00098   mbx->printf("%s,%d\n",variable,value);
00099   return 0;
00100 }
00101 
00102 int rlDataAcquisition::writeFloatValue(const char *variable, float value)
00103 {
00104   mbx->printf("%s,%f\n",variable,value);
00105   return 0;
00106 }
00107 
00108 int rlDataAcquisition::readErrorCount()
00109 {
00110   if(shmheader == NULL) return DAQ_ERROR;
00111   return shmheader->readErrorCount;
00112 }
00113 
00114 int rlDataAcquisition::writeErrorCount()
00115 {
00116   if(shmheader == NULL) return DAQ_ERROR;
00117   return shmheader->writeErrorCount;
00118 }
00119 
00120 int rlDataAcquisition::lifeCounter()
00121 {
00122   if(shmheader == NULL) return DAQ_ERROR;
00123   return shmheader->lifeCounter;
00124 }
00125 
00126 const char *rlDataAcquisition::firstVariable()
00127 {
00128   const char *cptr;
00129 
00130   if(shmheader == NULL) return "DAQ_ERROR";
00131   if(strcmp(shmheader->ident,"daq") != 0) return "DAQ_ERROR";
00132   cptr = shmvalues;
00133   iCurrent = 1;
00134   return cptr;
00135 }
00136 
00137 const char *rlDataAcquisition::nextVariable()
00138 {
00139   int value_offset, delta_index, nmax, i;
00140   const char *cptr;
00141 
00142   if(iCurrent < 0) return NULL;
00143   if(shmheader == NULL) return "DAQ_ERROR";
00144   if(strcmp(shmheader->ident,"daq") != 0) return "DAQ_ERROR";
00145   value_offset = shmheader->maxItemNameLength + 1;
00146   delta_index  = value_offset + shmheader->maxNameLength + 1;
00147   nmax         = shmheader->numItems;
00148 
00149   cptr = shmvalues;
00150   for(i=0; i<nmax; i++)
00151   {
00152     if(i == iCurrent)
00153     {
00154       iCurrent++;
00155       return cptr;
00156     }
00157     cptr += delta_index;
00158   }
00159 
00160   iCurrent = -1;
00161   return NULL;
00162 }
00163 
00164 int rlDataAcquisition::shmStatus()
00165 {
00166   if(shmheader == NULL) return DAQ_ERROR;
00167   if(shm->status == rlSharedMemory::OK) return 0;
00168   return DAQ_ERROR;
00169 }
00170 
00171 int rlDataAcquisition::shmKey()
00172 {
00173   if(shm == NULL) return -1;
00174   return shm->shmKey();
00175 }
00176 
00177 int rlDataAcquisition::shmId()
00178 {
00179   if(shm == NULL) return -1;
00180   return shm->shmId();
00181 }
00182 
00183