File: indidevice.cpp

package info (click to toggle)
libindi 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,232 kB
  • sloc: ansic: 17,921; cpp: 17,821; xml: 260; makefile: 12
file content (364 lines) | stat: -rw-r--r-- 11,824 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*******************************************************************************
  Copyright(c) 2010 Gerry Rozema. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  The full GNU General Public License is included in this distribution in the
  file called LICENSE.
*******************************************************************************/

#include "IndiDevice.h"

#include <string.h>

IndiDevice *device=NULL;


/**************************************************************************************
**
***************************************************************************************/
void ISGetProperties (const char *dev)
{
    //fprintf(stderr,"Enter ISGetProperties '%s'\n",dev);
    if(device==NULL) {
        //IDLog("Create device for %s\n",dev);
        device=_create_device();
        if(dev != NULL) {
            //fprintf(stderr,"Calling setDeviceName %s\n",dev);
            device->setDeviceName(dev);
            //fprintf(stderr,"deviceName() returns  %s\n",device->deviceName());
            //fprintf(stderr,"getDefaultName() returns  %s\n",device->getDefaultName());
        } else {
            //device->setDeviceName("junker");
            device->setDeviceName(device->getDefaultName());
        }
        device->addConfigurationControl();
        device->init_properties();
    }
    device->ISGetProperties(dev);
}

/**************************************************************************************
**
***************************************************************************************/
void ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
{
    //fprintf(stderr,"Enter ISNewSwitch %s\n",dev);

    //ISInit();
    //fprintf(stderr,"Calling Device->IsNewSwitch for device %0x\n",(void *)device);
    device->ISNewSwitch(dev, name, states, names, n);
}

/**************************************************************************************
**
***************************************************************************************/
void ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
{
    //fprintf(stderr,"Enter ISNewText\n");
    //ISInit();
    device->ISNewText(dev, name, texts, names, n);
}

/**************************************************************************************
**
***************************************************************************************/
void ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
{
    //fprintf(stderr,"OutsideClass::Enter ISNewNumber\n");
    //ISInit();
    device->ISNewNumber(dev, name, values, names, n);
}

/**************************************************************************************
**
***************************************************************************************/
void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
{
  INDI_UNUSED(dev);
  INDI_UNUSED(name);
  INDI_UNUSED(sizes);
  INDI_UNUSED(blobsizes);
  INDI_UNUSED(blobs);
  INDI_UNUSED(formats);
  INDI_UNUSED(names);
  INDI_UNUSED(n);
}

/**************************************************************************************
**
***************************************************************************************/
void ISSnoopDevice (XMLEle *root)
{
    return device->ISSnoopDevice(root);
}

void timerfunc(void *t)
{
    //fprintf(stderr,"Got a timer hit with %x\n",t);
    if(t==device) {
        //  this was for my device
        //  but we dont have a way of telling
        //  WHICH timer was hit :(
        device->TimerHit();
    }
    return;
}


IndiDevice::IndiDevice()
{
    //ctor
    Connected=false;
}

IndiDevice::~IndiDevice()
{
    //dtor
}


int IndiDevice::init_properties()
{
    //  All devices should have a connection switch defined
    //  so lets create it

    //IDLog("IndiDevice::init_properties()  MyDev=%s\n",deviceName());
    IUFillSwitch(&ConnectionS[0],"CONNECT","Connect",ISS_OFF);
    IUFillSwitch(&ConnectionS[1],"DISCONNECT","Disconnect",ISS_ON);
    IUFillSwitchVector(&ConnectionSV,ConnectionS,2,deviceName(),"CONNECTION","Connection","Main Control",IP_RW,ISR_1OFMANY,60,IPS_IDLE);

    return 0;
}

bool IndiDevice::DeleteProperty(char *n)
{
    IDDelete(deviceName(),n,NULL);
    return true;
}

void IndiDevice::ISGetProperties(const char *dev)
{

    //  Now lets send the ones we have defined
    //IDLog("IndiDevice::ISGetProperties %s\n",dev);
    IDDefSwitch (&ConnectionSV, NULL);

    if(Connected) UpdateProperties();   //  If already connected, send the rest
    //  And now get the default driver to send what it wants to send
    INDI::DefaultDriver::ISGetProperties(dev);


}

/**************************************************************************************
** Process Text properties
***************************************************************************************/
bool IndiDevice::ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
{

    //  And this base class doesn't actually process anything
    return INDI::DefaultDriver::ISNewText(dev,name,texts,names,n);
}

/**************************************************************************************
**  Process Numbers
***************************************************************************************/
bool IndiDevice::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
{

    //  Our base class doesn't actually do any processing
    return INDI::DefaultDriver::ISNewNumber(dev,name,values,names,n);
}

/**************************************************************************************
**  Process switches
***************************************************************************************/
bool IndiDevice::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
{

    //  Ok, lets Process any switches we actually handle here
    if(strcmp(dev,deviceName())==0) {
        //  it's for this device

        if(strcmp(name,ConnectionSV.name)==0) {
                bool rc;
                //IDLog("IndiDevice Switch %s\n",names[x]);

                IUUpdateSwitch(&ConnectionSV,states,names,n);

                if(ConnectionS[0].s==ISS_ON) {
                    if(!Connected) {
                        rc=Connect();
                        if(rc) {
                            ConnectionSV.s=IPS_OK;
                            //setConnected(true,"calling setconnected");
                            //ConnectionS[0].s=ISS_ON;
                            //ConnectionS[1].s=ISS_OFF;
                            Connected=true;
                        } else {
                            //ConnectionS[0].s=ISS_OFF;
                            //ConnectionS[1].s=ISS_ON;
                            ConnectionSV.s=IPS_ALERT;
                            Connected=false;
                        }
                    }
                    UpdateProperties();
                    IDSetSwitch(&ConnectionSV,NULL);
                } else {
                    if(Connected) rc=Disconnect();
                    //ConnectionS[0].s=ISS_OFF;
                    //ConnectionS[1].s=ISS_ON;
                    ConnectionSV.s=IPS_IDLE;
                    Connected=false;
                    UpdateProperties();
                    IDSetSwitch(&ConnectionSV,NULL);
                }

                /*
                if(strcmp(names[x],"CONNECT")==0) {
                    //  We are being requested to make a physical connection to the device
                    rc=Connect();
                    if(rc) {
                        //  Connection Succeeded
                        ConnectionSV.s=IPS_OK;
                        Connected=true;

                    } else {
                        //  Connection Failed
                        ConnectionSV.s=IPS_ALERT;
                        Connected=false;
                    }
                    IUUpdateSwitch(&ConnectionSV,states,names,n);
                    IDSetSwitch(&ConnectionSV,NULL);
                    IDLog("Connect ccalling update properties\n");
                    UpdateProperties();
                    //return true;
                }
                if(strcmp(names[x],"DISCONNECT")==0) {
                    //  We are being told to disconnect from the device
                    rc=Disconnect();
                    if(rc) {
                        ConnectionSV.s=IPS_IDLE;
                    } else {
                        ConnectionSV.s=IPS_ALERT;
                    }
                    Connected=false;
                    IDLog("Disconnect calling update properties\n");
                    UpdateProperties();
                    //  And now lets tell everybody how it went
                    IUUpdateSwitch(&ConnectionSV,states,names,n);
                    IDSetSwitch(&ConnectionSV,NULL);
                    return true;
                }
                */
            //}
        }
    }


    // let the default driver have a crack at it
    return INDI::DefaultDriver::ISNewSwitch(dev, name, states, names, n);
}

//  This is a helper function
//  that just encapsulates the Indi way into our clean c++ way of doing things
int IndiDevice::SetTimer(int t)
{
    return IEAddTimer(t,timerfunc,this);
}

//  Just another helper to help encapsulate indi into a clean class
void IndiDevice::RemoveTimer(int t)
{
    IERmTimer(t);
    return;
}
//  This is just a placeholder
//  This function should be overriden by child classes if they use timers
//  So we should never get here
void IndiDevice::TimerHit()
{
    return;
}

bool IndiDevice::Connect()
{
    //  We dont actually implement a device here
    //  So we cannot connect to it
IDLog("IndiDevice Connect, we should never get here\n");
    IDMessage(deviceName(),"IndiDevice:: has no device attached....");
    return false;
}

bool IndiDevice::Disconnect()
{
    //  Since we cannot connect, we cant disconnect either
    IDMessage(deviceName(),"IndiDevice:: has no device to detach....");
    return false;
}

bool IndiDevice::UpdateProperties()
{
    //  The base device has no properties to update
    return true;
}

void IndiDevice::ISSnoopDevice (XMLEle *root)
{
      INDI_UNUSED(root);
}

bool IndiDevice::SaveConfig()
{

    // FIXME REMOVE THIS
    return true;

    char err[MAXRBUF];
    FILE *fp;
    //int rc;

    fp=IUGetConfigFP(NULL,deviceName(),err);
    if(fp != NULL) {
        IUSaveConfigTag(fp,0);
        //IUSaveConfigText(fp,&FilterNameTV);
        //  Tell child classes to write any items they want
        //  into persistent storage
        WritePersistentConfig(fp);
        IUSaveConfigTag(fp,1);
        fclose(fp);
        //IDMessage(deviceName(),"Configuration Saved\n");
        return true;
    } else {
        IDMessage(deviceName(),"Save config failed\n");
    }
    return false;
}

bool IndiDevice::WritePersistentConfig(FILE *f)
{
    return false;
}

bool IndiDevice::LoadConfig()
{
    char err[MAXRBUF];
    int rc;

    rc=IUReadConfig(NULL,deviceName(),err);
    if(rc==0) return true;
    return false;
}