source: BAORadio/libindi/libindi/libs/indibase/defaultdriver.cpp @ 504

Last change on this file since 504 was 504, checked in by frichard, 13 years ago

-Version 0.8 de libini
-Formule de Marc
-Nouvelles fonctionnalités (goto nom-de l'objet etc...)

File size: 15.2 KB
Line 
1#include <stdlib.h>
2#include <string.h>
3#include <errno.h>
4#include <zlib.h>
5
6#include "defaultdriver.h"
7#include "indicom.h"
8#include "base64.h"
9
10void timerfunc(void *t)
11{
12    //fprintf(stderr,"Got a timer hit with %x\n",t);
13    INDI::DefaultDriver *devPtr = static_cast<INDI::DefaultDriver *> (t);
14    if (devPtr != NULL)
15    {
16        //  this was for my device
17        //  but we dont have a way of telling
18        //  WHICH timer was hit :(
19        devPtr->TimerHit();
20    }
21    return;
22}
23
24
25INDI::DefaultDriver::DefaultDriver()
26{
27    pDebug = false;
28    pSimulation = false;
29
30    //switchPtr conSw(new ISwitchVectorProperty);
31    ConnectionSP = new ISwitchVectorProperty;
32
33    IUFillSwitch(&ConnectionS[0],"CONNECT","Connect",ISS_OFF);
34    IUFillSwitch(&ConnectionS[1],"DISCONNECT","Disconnect",ISS_ON);
35    IUFillSwitchVector(ConnectionSP,ConnectionS,2,deviceName(),"CONNECTION","Connection","Main Control",IP_RW,ISR_1OFMANY,60,IPS_IDLE);
36
37    registerProperty(ConnectionSP, INDI_SWITCH);
38
39}
40
41bool INDI::DefaultDriver::loadConfig()
42{
43    char errmsg[MAXRBUF];
44    bool pResult = false;
45
46    pResult = IUReadConfig(NULL, deviceID, errmsg) == 0 ? true : false;
47
48   if (pResult)
49       IDMessage(deviceID, "Configuration successfully loaded.");
50
51   IUSaveDefaultConfig(NULL, NULL, deviceID);
52
53   return pResult;
54}
55
56bool INDI::DefaultDriver::saveConfig()
57{
58    //std::vector<orderPtr>::iterator orderi;
59
60    std::map< boost::shared_ptr<void>, INDI_TYPE>::iterator orderi;
61
62    ISwitchVectorProperty *svp=NULL;
63    INumberVectorProperty *nvp=NULL;
64    ITextVectorProperty   *tvp=NULL;
65    IBLOBVectorProperty   *bvp=NULL;
66    char errmsg[MAXRBUF];
67    FILE *fp = NULL;
68
69    fp = IUGetConfigFP(NULL, deviceID, errmsg);
70
71    if (fp == NULL)
72    {
73        IDMessage(deviceID, "Error saving configuration. %s", errmsg);
74        return false;
75    }
76
77    IUSaveConfigTag(fp, 0);
78
79    for (orderi = pAll.begin(); orderi != pAll.end(); orderi++)
80    {
81
82        switch (orderi->second)
83        {
84        case INDI_NUMBER:
85             nvp = static_cast<INumberVectorProperty *>((orderi->first).get());
86             IDLog("Trying to save config for number %s\n", nvp->name);
87             IUSaveConfigNumber(fp, nvp);
88             break;
89        case INDI_TEXT:
90             tvp = static_cast<ITextVectorProperty *>((orderi->first).get());
91             IUSaveConfigText(fp, tvp);
92             break;
93        case INDI_SWITCH:
94             svp = static_cast<ISwitchVectorProperty *>((orderi->first).get());
95             /* Never save CONNECTION property. Don't save switches with no switches on if the rule is one of many */
96             if (!strcmp(svp->name, "CONNECTION") || (svp->r == ISR_1OFMANY && !IUFindOnSwitch(svp)))
97                 continue;
98             IUSaveConfigSwitch(fp, svp);
99             break;
100        case INDI_BLOB:
101             bvp = static_cast<IBLOBVectorProperty *>((orderi->first).get());
102             IUSaveConfigBLOB(fp, bvp);
103             break;
104        }
105    }
106
107    IUSaveConfigTag(fp, 1);
108
109    fclose(fp);
110
111    IUSaveDefaultConfig(NULL, NULL, deviceID);
112
113    IDMessage(deviceID, "Configuration successfully saved.");
114
115    return true;
116}
117
118bool INDI::DefaultDriver::loadDefaultConfig()
119{
120    char configDefaultFileName[MAXRBUF];
121    char errmsg[MAXRBUF];
122    bool pResult = false;
123
124    if (getenv("INDICONFIG"))
125        snprintf(configDefaultFileName, MAXRBUF, "%s.default", getenv("INDICONFIG"));
126    else
127        snprintf(configDefaultFileName, MAXRBUF, "%s/.indi/%s_config.xml.default", getenv("HOME"), deviceID);
128
129    if (pDebug)
130        IDLog("Requesting to load default config with: %s\n", configDefaultFileName);
131
132    pResult = IUReadConfig(configDefaultFileName, deviceID, errmsg) == 0 ? true : false;
133
134    if (pResult)
135        IDMessage(deviceID, "Default configuration loaded.");
136    else
137        IDMessage(deviceID, "Error loading default configuraiton. %s", errmsg);
138
139    return pResult;
140}
141
142bool INDI::DefaultDriver::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
143{
144
145    // ignore if not ours //
146    if (strcmp (dev, deviceID))
147        return false;
148
149    ISwitchVectorProperty *svp = getSwitch(name);
150
151    if (!svp)
152        return false;
153
154     if(!strcmp(svp->name,ConnectionSP->name))
155     {
156        bool rc;
157       
158      for (int i=0; i < n; i++)
159      {
160        if ( !strcmp(names[i], "CONNECT") && (states[i] == ISS_ON))
161        {
162            // If not connected, attempt to connect
163            if (isConnected() == false)
164            {
165                rc = Connect();
166
167                // If connection is successful, set it thus
168                if (rc)
169                  setConnected(true);
170                else
171                  setConnected(false, IPS_ALERT);
172
173                updateProperties();
174            }
175            else
176                // Just tell client we're connected yes
177                setConnected(true);
178        }
179        else if ( !strcmp(names[i], "DISCONNECT") && (states[i] == ISS_ON))
180        {
181            // If connected, then true to disconnect.
182            if (isConnected() == true)
183                rc = Disconnect();
184            else
185                rc = true;
186
187            if (rc)
188                setConnected(false, IPS_IDLE);
189            else
190                setConnected(true, IPS_ALERT);
191
192            updateProperties();
193        }
194    }
195
196        return true;
197    }
198
199
200    if (!strcmp(svp->name, "DEBUG"))
201    {
202        IUUpdateSwitch(svp, states, names, n);
203        ISwitch *sp = IUFindOnSwitch(svp);
204        if (!sp)
205            return false;
206
207        if (!strcmp(sp->name, "ENABLE"))
208            setDebug(true);
209        else
210            setDebug(false);
211        return true;
212    }
213
214    if (!strcmp(svp->name, "SIMULATION"))
215    {
216        IUUpdateSwitch(svp, states, names, n);
217        ISwitch *sp = IUFindOnSwitch(svp);
218        if (!sp)
219            return false;
220
221        if (!strcmp(sp->name, "ENABLE"))
222            setSimulation(true);
223        else
224            setSimulation(false);
225        return true;
226    }
227
228    if (!strcmp(svp->name, "CONFIG_PROCESS"))
229    {
230        IUUpdateSwitch(svp, states, names, n);
231        ISwitch *sp = IUFindOnSwitch(svp);
232        IUResetSwitch(svp);
233        bool pResult = false;
234        if (!sp)
235            return false;
236
237        if (!strcmp(sp->name, "CONFIG_LOAD"))
238            pResult = loadConfig();
239        else if (!strcmp(sp->name, "CONFIG_SAVE"))
240            pResult = saveConfig();
241        else if (!strcmp(sp->name, "CONFIG_DEFAULT"))
242            pResult = loadDefaultConfig();
243
244        if (pResult)
245            svp->s = IPS_OK;
246        else
247            svp->s = IPS_ALERT;
248
249        IDSetSwitch(svp, NULL);
250        return true;
251    }
252
253    return false;
254
255}
256
257void INDI::DefaultDriver::addDebugControl()
258{
259    /**************************************************************************/
260    DebugSP = getSwitch("DEBUG");
261    if (!DebugSP)
262    {
263        switchPtr debSw(new ISwitchVectorProperty);
264        DebugSP = debSw.get();
265        IUFillSwitch(&DebugS[0], "ENABLE", "Enable", ISS_OFF);
266        IUFillSwitch(&DebugS[1], "DISABLE", "Disable", ISS_ON);
267        IUFillSwitchVector(DebugSP, DebugS, NARRAY(DebugS), deviceID, "DEBUG", "Debug", "Options", IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
268        pAll[debSw] = INDI_SWITCH;
269    }
270    else
271    {
272        ISwitch *sp = IUFindSwitch(DebugSP, "ENABLE");
273        if (sp)
274            if (sp->s == ISS_ON)
275                pDebug = true;
276    }
277    /**************************************************************************/
278
279}
280
281void INDI::DefaultDriver::addSimulationControl()
282{
283    /**************************************************************************/
284    SimulationSP = getSwitch("SIMULATION");
285    if (!SimulationSP)
286    {
287        switchPtr simSw(new ISwitchVectorProperty);
288        SimulationSP = simSw.get();
289        IUFillSwitch(&SimulationS[0], "ENABLE", "Enable", ISS_OFF);
290        IUFillSwitch(&SimulationS[1], "DISABLE", "Disable", ISS_ON);
291        IUFillSwitchVector(SimulationSP, SimulationS, NARRAY(SimulationS), deviceID, "SIMULATION", "Simulation", "Options", IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
292        pAll[simSw] = INDI_SWITCH;
293    }
294    else
295    {
296        ISwitch *sp = IUFindSwitch(SimulationSP, "ENABLE");
297        if (sp)
298            if (sp->s == ISS_ON)
299                pSimulation = true;
300    }
301}
302
303void INDI::DefaultDriver::addConfigurationControl()
304{
305    /**************************************************************************/
306    ConfigProcessSP = getSwitch("CONFIG_PROCESS");
307    if (!ConfigProcessSP)
308    {
309        switchPtr configSw(new ISwitchVectorProperty);
310        ConfigProcessSP = configSw.get();
311        IUFillSwitch(&ConfigProcessS[0], "CONFIG_LOAD", "Load", ISS_OFF);
312        IUFillSwitch(&ConfigProcessS[1], "CONFIG_SAVE", "Save", ISS_OFF);
313        IUFillSwitch(&ConfigProcessS[2], "CONFIG_DEFAULT", "Default", ISS_OFF);
314        IUFillSwitchVector(ConfigProcessSP, ConfigProcessS, NARRAY(ConfigProcessS), deviceID, "CONFIG_PROCESS", "Configuration", "Options", IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
315        pAll[configSw] = INDI_SWITCH;
316    }
317    /**************************************************************************/
318
319}
320
321void INDI::DefaultDriver::addAuxControls()
322{
323   addDebugControl();
324   addSimulationControl();
325   addConfigurationControl();
326}
327
328void INDI::DefaultDriver::setDebug(bool enable)
329{
330    if (!DebugSP)
331        return;
332
333    if (pDebug == enable)
334    {
335        DebugSP->s = IPS_OK;
336        IDSetSwitch(DebugSP, NULL);
337        return;
338    }
339
340    IUResetSwitch(DebugSP);
341
342    if (enable)
343    {
344        ISwitch *sp = IUFindSwitch(DebugSP, "ENABLE");
345        if (sp)
346        {
347            sp->s = ISS_ON;
348            IDMessage(deviceID, "Debug is enabled.");
349        }
350    }
351    else
352    {
353        ISwitch *sp = IUFindSwitch(DebugSP, "DISABLE");
354        if (sp)
355        {
356            sp->s = ISS_ON;
357            IDMessage(deviceID, "Debug is disabled.");
358        }
359    }
360
361    pDebug = enable;
362    DebugSP->s = IPS_OK;
363    IDSetSwitch(DebugSP, NULL);
364
365}
366
367void INDI::DefaultDriver::setSimulation(bool enable)
368{
369    if (!SimulationSP)
370        return;
371
372   if (pSimulation == enable)
373   {
374       SimulationSP->s = IPS_OK;
375       IDSetSwitch(SimulationSP, NULL);
376       return;
377   }
378
379   IUResetSwitch(SimulationSP);
380
381   if (enable)
382   {
383       ISwitch *sp = IUFindSwitch(SimulationSP, "ENABLE");
384       if (sp)
385       {
386           IDMessage(deviceID, "Simulation is enabled.");
387           sp->s = ISS_ON;
388       }
389   }
390   else
391   {
392       ISwitch *sp = IUFindSwitch(SimulationSP, "DISABLE");
393       if (sp)
394       {
395           sp->s = ISS_ON;
396           IDMessage(deviceID, "Simulation is disabled.");
397       }
398   }
399
400   pSimulation = enable;
401   SimulationSP->s = IPS_OK;
402   IDSetSwitch(SimulationSP, NULL);
403
404}
405
406bool INDI::DefaultDriver::isDebug()
407{
408  return pDebug;
409}
410
411bool INDI::DefaultDriver::isSimulation()
412{
413 return pSimulation;
414}
415
416void INDI::DefaultDriver::ISGetProperties (const char *dev)
417{
418    std::map< boost::shared_ptr<void>, INDI_TYPE>::iterator orderi;
419    static int isInit = 0;
420
421    //fprintf(stderr,"Enter ISGetProperties '%s'\n",dev);
422    if(isInit == 0)
423    {
424        if(dev != NULL)
425             setDeviceName(dev);
426        else
427        {
428            char *envDev = getenv("INDIDEV");
429            if (envDev != NULL)
430                setDeviceName(envDev);
431            else
432               setDeviceName(getDefaultName());
433        }
434
435        strncpy(ConnectionSP->device, deviceName(), MAXINDIDEVICE);
436        initProperties();
437        addConfigurationControl();
438
439        isInit = 1;
440    }
441
442    for (orderi = pAll.begin(); orderi != pAll.end(); orderi++)
443    {
444        switch (orderi->second)
445        {
446        case INDI_NUMBER:
447             IDDefNumber(static_cast<INumberVectorProperty *>((orderi->first).get()) , NULL);
448             break;
449        case INDI_TEXT:
450             IDDefText(static_cast<ITextVectorProperty *>((orderi->first).get()) , NULL);
451             break;
452        case INDI_SWITCH:
453             IDDefSwitch(static_cast<ISwitchVectorProperty *>((orderi->first).get()) , NULL);
454             break;
455        case INDI_LIGHT:
456             IDDefLight(static_cast<ILightVectorProperty *>((orderi->first).get()) , NULL);
457             break;
458        case INDI_BLOB:
459             IDDefBLOB(static_cast<IBLOBVectorProperty *>((orderi->first).get()) , NULL);
460             break;
461        }
462    }
463}
464
465void INDI::DefaultDriver::resetProperties()
466{
467    /*std::vector<numberPtr>::const_iterator numi;
468    std::vector<switchPtr>::const_iterator switchi;
469    std::vector<textPtr>::const_iterator texti;
470    std::vector<lightPtr>::const_iterator lighti;
471    std::vector<blobPtr>::const_iterator blobi;
472
473    for ( numi = pNumbers.begin(); numi != pNumbers.end(); numi++)
474    {
475        (*numi)->s = IPS_IDLE;
476        IDSetNumber( (*numi).get(), NULL);
477    }
478
479   for ( switchi = pSwitches.begin(); switchi != pSwitches.end(); switchi++)
480   {
481       (*switchi)->s = IPS_IDLE;
482       IDSetSwitch( (*switchi).get(), NULL);
483   }
484
485   for ( texti = pTexts.begin(); texti != pTexts.end(); texti++)
486   {
487      (*texti)->s = IPS_IDLE;
488      IDSetText( (*texti).get(), NULL);
489   }
490
491   for ( lighti = pLights.begin(); lighti != pLights.end(); lighti++)
492   {
493       (*lighti)->s = IPS_IDLE;
494       IDSetLight( (*lighti).get(), NULL);
495   }
496
497   for ( blobi = pBlobs.begin(); blobi != pBlobs.end(); blobi++)
498   {
499       (*blobi)->s = IPS_IDLE;
500       IDSetBLOB( (*blobi).get(), NULL);
501   }*/
502}
503
504void INDI::DefaultDriver::setConnected(bool status, IPState state, const char *msg)
505{
506    ISwitch *sp = NULL;
507    ISwitchVectorProperty *svp = getSwitch("CONNECTION");
508    if (!svp)
509        return;
510
511    IUResetSwitch(svp);
512
513    // Connect
514    if (status)
515    {
516        sp = IUFindSwitch(svp, "CONNECT");
517        if (!sp)
518            return;
519        sp->s = ISS_ON;
520    }
521    // Disconnect
522    else
523    {
524        sp = IUFindSwitch(svp, "DISCONNECT");
525        if (!sp)
526            return;
527        sp->s = ISS_ON;
528    }
529
530    svp->s = state;
531
532    IDSetSwitch(svp, msg, NULL);
533}
534
535//  This is a helper function
536//  that just encapsulates the Indi way into our clean c++ way of doing things
537int INDI::DefaultDriver::SetTimer(int t)
538{
539    return IEAddTimer(t,timerfunc,this);
540}
541
542//  Just another helper to help encapsulate indi into a clean class
543void INDI::DefaultDriver::RemoveTimer(int t)
544{
545    IERmTimer(t);
546    return;
547}
548
549//  This is just a placeholder
550//  This function should be overriden by child classes if they use timers
551//  So we should never get here
552void INDI::DefaultDriver::TimerHit()
553{
554    return;
555}
556
557
558bool INDI::DefaultDriver::updateProperties()
559{
560    //  The base device has no properties to update
561    return true;
562}
563
564
565bool INDI::DefaultDriver::initProperties()
566{
567   return true;
568}
569
570bool INDI::DefaultDriver::deleteProperty(const char *propertyName)
571{
572    removeProperty(propertyName);
573    IDDelete(deviceName(), propertyName ,NULL);
574    return true;
575}
576
577void INDI::DefaultDriver::defineNumber(INumberVectorProperty *nvp)
578{
579    registerProperty(nvp, INDI_NUMBER);
580    IDDefNumber(nvp, NULL);
581}
582
583void INDI::DefaultDriver::defineText(ITextVectorProperty *tvp)
584{
585    registerProperty(tvp, INDI_TEXT);
586    IDDefText(tvp, NULL);
587}
588
589void INDI::DefaultDriver::defineSwitch(ISwitchVectorProperty *svp)
590{
591    registerProperty(svp, INDI_SWITCH);
592    IDDefSwitch(svp, NULL);
593}
594
595void INDI::DefaultDriver::defineLight(ILightVectorProperty *lvp)
596{
597    registerProperty(lvp, INDI_LIGHT);
598    IDDefLight(lvp, NULL);
599}
600
601void INDI::DefaultDriver::defineBLOB(IBLOBVectorProperty *bvp)
602{
603    registerProperty(bvp, INDI_BLOB);
604    IDDefBLOB(bvp, NULL);
605}
Note: See TracBrowser for help on using the repository browser.