Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

Level.cc

Go to the documentation of this file.
00001 /*
00002 
00003     Level server
00004     Copyright (C) 2000-2001 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <signal.h>
00026 #include <math.h>
00027 #include <float.h>
00028 #include <unistd.h>
00029 #include <sys/time.h>
00030 
00031 #include "Level.hh"
00032 
00033 
00034 static bool bDebug = false;
00035 static bool bDaemon = false;
00036 clLevel *Level;
00037 
00038 
00039 void SigHandler (int iSigNum)
00040 {
00041     switch (iSigNum)
00042     {
00043         case SIGSEGV:
00044             if (bDebug)
00045             {
00046                 printf("Oops, received SIGSEGV, crash...\n");
00047                 abort();
00048             }
00049             else exit(-1);
00050             break;
00051     }
00052 }
00053 
00054 
00055 int main (int argc, char *argv[])
00056 {
00057     int iRetVal;
00058 
00059     signal(SIGPIPE, SIG_IGN);
00060     signal(SIGFPE, SIG_IGN);
00061     signal(SIGSEGV, SigHandler);
00062     if (argc >= 2)
00063     {
00064         if (strcmp(argv[1], "--debug") == 0)
00065         {
00066             bDebug = true;
00067         }
00068         if (strcmp(argv[1], "-D") == 0)
00069         {
00070             bDaemon = true;
00071         }
00072     }
00073     if (bDebug) printf("level: Started\n");
00074     Level = new clLevel(3, 4);
00075     iRetVal = Level->Main(argc, argv);
00076     delete Level;
00077     if (bDebug) printf("level: Exit = %i\n", iRetVal);
00078     return iRetVal;
00079 }
00080 
00081 
00082 bool clLevel::GetCfg ()
00083 {
00084     CfgFile.SetFileName(SD_CFGFILE);
00085     if (!CfgFile.GetStr("LocalSocket", cpStreamSocket)) return false;
00086     CfgFile.SetFileName(LEVEL_CFGFILE);
00087     if (!CfgFile.GetInt("FilterSize", &lFilterSize))
00088         lFilterSize = LEVEL_DEF_FILTER_SIZE;
00089     return true;
00090 }
00091 
00092 
00093 bool clLevel::GetRequest ()
00094 {
00095     char cpReqMsg[GLOBAL_HEADER_LEN];
00096 
00097     if (!SOpRequest.ReadSelect(LEVEL_1STREQ_TIMEOUT)) return false;
00098     if (SOpRequest.ReadN(cpReqMsg, GLOBAL_HEADER_LEN) < GLOBAL_HEADER_LEN)
00099         return false;
00100     Msg.GetRequest(cpReqMsg, &sRequest);
00101     return true;
00102 }
00103 
00104 
00105 bool clLevel::ConnectStream ()
00106 {
00107     int iSockH;
00108     stRawDataReq sDataReq;
00109 
00110     iSockH = SClient.Connect(cpStreamSocket);
00111     if (iSockH < 0) return false;
00112     SOpRaw.SetHandle(iSockH);
00113     if (!SOpRaw.ReadSelect(LEVEL_RAW1ST_TIMEOUT)) return false;
00114     if (SOpRaw.ReadN(&sRawHdr, sizeof(sRawHdr)) < (int) sizeof(sRawHdr))
00115         return false;
00116     sDataReq.iChannel = sRequest.iChannel;
00117     if (SOpRaw.WriteN(&sDataReq, sizeof(sDataReq)) < (int) sizeof(sDataReq))
00118         return false;
00119     return true;
00120 }
00121 
00122 
00123 bool clLevel::Initialize ()
00124 {
00125     float fLowCorner;
00126     float fHighCorner;
00127     GDT *fpNullPtr = NULL;
00128     
00129     lRawDataCount = lFilterSize / 2l;
00130     lRawBufSize = lRawDataCount * sizeof(GDT);
00131     RawBuf.Size(lRawBufSize);
00132     lSampleCount = (long) 
00133         (sRawHdr.dSampleRate * sRequest.fIntegrationTime + 0.5F);
00134     if (lSampleCount <= 0) 
00135         lSampleCount = 1l;
00136     DataBuf.Size(lSampleCount * sizeof(GDT));
00137     sResult.fIntegrationTime = 
00138         (float) lSampleCount / (float) sRawHdr.dSampleRate;
00139     if (bDebug) 
00140     {
00141         printf("level: Integration time: %f -> %f\n", 
00142             sRequest.fIntegrationTime, sResult.fIntegrationTime);
00143     }
00144     // Initialize filter
00145     Filter.Initialize(lFilterSize, fpNullPtr);
00146     if (sRequest.fLowFrequency < sRequest.fHighFrequency)
00147     {
00148         fLowCorner = sRequest.fLowFrequency;
00149         fHighCorner = sRequest.fHighFrequency;
00150         Filter.DesignBP(&fLowCorner, &fHighCorner, (GDT) sRawHdr.dSampleRate);
00151         if (bDebug)
00152         {
00153             printf("level: Lower corner %.2f -> %.2f\n", 
00154                 sRequest.fLowFrequency, fLowCorner);
00155             printf("level: Higher corner %.2f -> %.2f\n",
00156                 sRequest.fHighFrequency, fHighCorner);
00157         }
00158     }
00159     if (bDebug)
00160     {
00161         printf("level: Algorithm: ");
00162         switch (sRequest.iAlgorithm)
00163         {
00164             case MSG_LEVEL_ALG_PEAK:
00165                 printf("Peak level\n");
00166                 break;
00167             case MSG_LEVEL_ALG_RMS:
00168                 printf("RMS\n");
00169                 break;
00170             case MSG_LEVEL_ALG_MEAN:
00171                 printf("Mean\n");
00172                 break;
00173             case MSG_LEVEL_ALG_MEDIAN:
00174                 printf("Median\n");
00175                 break;
00176             case MSG_LEVEL_ALG_STDDEV:
00177                 printf("Standard deviation\n");
00178                 break;
00179             default:
00180                 printf("Unknown\n");
00181         }
00182     }
00183     return true;
00184 }
00185 
00186 
00187 bool clLevel::ReceiveData ()
00188 {
00189     GDT fTempPL;
00190     GDT *fpRaw;
00191 
00192     fpRaw = RawBuf;
00193     if (SOpRaw.ReadSelect(LEVEL_TIMEOUT))
00194     {
00195         if (SOpRaw.ReadN(fpRaw, lRawBufSize) < lRawBufSize)
00196             return false;
00197         fTempPL = DSP.PeakLevel(fpRaw, lRawDataCount);
00198         if (fTempPL > fResPeakLevel) fResPeakLevel = fTempPL;
00199         Filter.Put(fpRaw, lRawDataCount);
00200     }
00201     else return false;
00202     return true;
00203 }
00204 
00205 
00206 bool clLevel::Process ()
00207 {
00208     GDT fStdDev;
00209     GDT fMean;
00210     GDT *fpData;
00211 
00212     fpData = DataBuf;
00213     gettimeofday(&sResult.sTimeStamp, NULL);
00214     switch (sRequest.iAlgorithm)
00215     {
00216         case MSG_LEVEL_ALG_PEAK:
00217             sResult.fResult = DSP.PeakLevel(fpData, lSampleCount);
00218             break;
00219         case MSG_LEVEL_ALG_RMS:
00220             sResult.fResult = 20.0 * log10(DSP.RMS(fpData, lSampleCount));
00221             break;
00222         case MSG_LEVEL_ALG_MEAN:
00223             DSP.Abs(fpData, lSampleCount);
00224             sResult.fResult = 20.0 * log10(DSP.Mean(fpData, lSampleCount));
00225             break;
00226         case MSG_LEVEL_ALG_MEDIAN:
00227             DSP.Abs(fpData, lSampleCount);
00228             sResult.fResult = 20.0 * log10(DSP.Median(fpData, lSampleCount));
00229             break;
00230         case MSG_LEVEL_ALG_STDDEV:
00231             DSP.Abs(fpData, lSampleCount);
00232             DSP.StdDev(&fStdDev, &fMean, fpData, lSampleCount);
00233             sResult.fResult = 20.0 * log10(fStdDev);
00234             break;
00235         default:
00236             sResult.fResult = 0;
00237     }
00238     return true;
00239 }
00240 
00241 
00242 bool clLevel::SendResult ()
00243 {
00244     char cpMsgBuf[GLOBAL_HEADER_LEN];
00245 
00246     Msg.SetResult(cpMsgBuf, &sResult);
00247     if (SOpResult.WriteN(cpMsgBuf, GLOBAL_HEADER_LEN) < GLOBAL_HEADER_LEN)
00248         return false;
00249     sResult.fPeakLevel = 0;
00250     return true;
00251 }
00252 
00253 
00254 clLevel::clLevel (int iInHandle, int iOutHandle)
00255 {
00256     bRun = true;
00257     SOpRequest.SetHandle(iInHandle);
00258     SOpResult.SetHandle(iOutHandle);
00259 }
00260 
00261 
00262 clLevel::~clLevel ()
00263 {
00264 }
00265 
00266 
00267 int clLevel::Main (int iArgC, char *cpaArgV[])
00268 {
00269     GDT *fpData;
00270 
00271     if (!GetCfg())
00272     {
00273         if (bDebug) printf("level: Error reading configuration\n");
00274         return 1;
00275     }
00276     if (!GetRequest())
00277     {
00278         if (bDebug) printf("level: Unable to receive request message\n");
00279         return 1;
00280     }
00281     if (!ConnectStream())
00282     {
00283         if (bDebug) printf("level: Unable to connect to streamdist\n");
00284         return 1;
00285     }
00286     if (!Initialize())
00287     {
00288         if (bDebug) printf("level: Initialization failed\n");
00289         return 1;
00290     }
00291     fpData = DataBuf;
00292     while (bRun)
00293     {
00294         if (!ReceiveData()) 
00295         {
00296             if (bDebug) printf("level: ReceiveData() returned error\n");
00297             return 2;
00298         }
00299         while (Filter.Get(fpData, lSampleCount) && bRun)
00300         {
00301             if (!Process())
00302             {
00303                 if (bDebug) printf("level: Process() returned error\n");
00304                 return 2;
00305             }
00306             if (!SendResult())
00307             {
00308                 if (bDebug) 
00309                 {
00310                     printf(
00311                         "level: SendResult() returned error (disconnect?)\n");
00312                     bRun = false;
00313                 }
00314             }
00315         }
00316     }
00317     return 0;
00318 }
00319 

Generated on Sun Oct 26 19:11:21 2003 for HASAS by doxygen 1.3.3