source: BAORadio/libindi/libindi/libs/indibase/indidevice.cpp @ 642

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

-Alignement des antennes
-Version 0.0.9 de libindi

File size: 11.5 KB
Line 
1/*******************************************************************************
2  Copyright(c) 2010 Gerry Rozema. All rights reserved.
3
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License as published by the Free
6  Software Foundation; either version 2 of the License, or (at your option)
7  any later version.
8
9  This program is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  more details.
13
14  You should have received a copy of the GNU General Public License along with
15  this program; if not, write to the Free Software Foundation, Inc., 59
16  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18  The full GNU General Public License is included in this distribution in the
19  file called LICENSE.
20*******************************************************************************/
21
22#include "IndiDevice.h"
23
24#include <string.h>
25
26IndiDevice *device=NULL;
27
28
29/**************************************************************************************
30**
31***************************************************************************************/
32void ISGetProperties (const char *dev)
33{
34    //fprintf(stderr,"Enter ISGetProperties '%s'\n",dev);
35    if(device==NULL) {
36        //IDLog("Create device for %s\n",dev);
37        device=_create_device();
38        if(dev != NULL) {
39            //fprintf(stderr,"Calling setDeviceName %s\n",dev);
40            device->setDeviceName(dev);
41            //fprintf(stderr,"deviceName() returns  %s\n",device->deviceName());
42            //fprintf(stderr,"getDefaultName() returns  %s\n",device->getDefaultName());
43        } else {
44            //device->setDeviceName("junker");
45            device->setDeviceName(device->getDefaultName());
46        }
47        device->addConfigurationControl();
48        device->init_properties();
49    }
50    device->ISGetProperties(dev);
51}
52
53/**************************************************************************************
54**
55***************************************************************************************/
56void ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
57{
58    //fprintf(stderr,"Enter ISNewSwitch %s\n",dev);
59
60    //ISInit();
61    //fprintf(stderr,"Calling Device->IsNewSwitch for device %0x\n",(void *)device);
62    device->ISNewSwitch(dev, name, states, names, n);
63}
64
65/**************************************************************************************
66**
67***************************************************************************************/
68void ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
69{
70    //fprintf(stderr,"Enter ISNewText\n");
71    //ISInit();
72    device->ISNewText(dev, name, texts, names, n);
73}
74
75/**************************************************************************************
76**
77***************************************************************************************/
78void ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
79{
80    //fprintf(stderr,"OutsideClass::Enter ISNewNumber\n");
81    //ISInit();
82    device->ISNewNumber(dev, name, values, names, n);
83}
84
85/**************************************************************************************
86**
87***************************************************************************************/
88void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
89{
90  INDI_UNUSED(dev);
91  INDI_UNUSED(name);
92  INDI_UNUSED(sizes);
93  INDI_UNUSED(blobsizes);
94  INDI_UNUSED(blobs);
95  INDI_UNUSED(formats);
96  INDI_UNUSED(names);
97  INDI_UNUSED(n);
98}
99
100/**************************************************************************************
101**
102***************************************************************************************/
103void ISSnoopDevice (XMLEle *root)
104{
105    return device->ISSnoopDevice(root);
106}
107
108void timerfunc(void *t)
109{
110    //fprintf(stderr,"Got a timer hit with %x\n",t);
111    if(t==device) {
112        //  this was for my device
113        //  but we dont have a way of telling
114        //  WHICH timer was hit :(
115        device->TimerHit();
116    }
117    return;
118}
119
120
121IndiDevice::IndiDevice()
122{
123    //ctor
124    Connected=false;
125}
126
127IndiDevice::~IndiDevice()
128{
129    //dtor
130}
131
132
133int IndiDevice::init_properties()
134{
135    //  All devices should have a connection switch defined
136    //  so lets create it
137
138    //IDLog("IndiDevice::init_properties()  MyDev=%s\n",deviceName());
139    IUFillSwitch(&ConnectionS[0],"CONNECT","Connect",ISS_OFF);
140    IUFillSwitch(&ConnectionS[1],"DISCONNECT","Disconnect",ISS_ON);
141    IUFillSwitchVector(&ConnectionSV,ConnectionS,2,deviceName(),"CONNECTION","Connection","Main Control",IP_RW,ISR_1OFMANY,60,IPS_IDLE);
142
143    return 0;
144}
145
146bool IndiDevice::DeleteProperty(char *n)
147{
148    IDDelete(deviceName(),n,NULL);
149    return true;
150}
151
152void IndiDevice::ISGetProperties(const char *dev)
153{
154
155    //  Now lets send the ones we have defined
156    //IDLog("IndiDevice::ISGetProperties %s\n",dev);
157    IDDefSwitch (&ConnectionSV, NULL);
158
159    if(Connected) UpdateProperties();   //  If already connected, send the rest
160    //  And now get the default driver to send what it wants to send
161    INDI::DefaultDriver::ISGetProperties(dev);
162
163
164}
165
166/**************************************************************************************
167** Process Text properties
168***************************************************************************************/
169bool IndiDevice::ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
170{
171
172    //  And this base class doesn't actually process anything
173    return INDI::DefaultDriver::ISNewText(dev,name,texts,names,n);
174}
175
176/**************************************************************************************
177**  Process Numbers
178***************************************************************************************/
179bool IndiDevice::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
180{
181
182    //  Our base class doesn't actually do any processing
183    return INDI::DefaultDriver::ISNewNumber(dev,name,values,names,n);
184}
185
186/**************************************************************************************
187**  Process switches
188***************************************************************************************/
189bool IndiDevice::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
190{
191
192    //  Ok, lets Process any switches we actually handle here
193    if(strcmp(dev,deviceName())==0) {
194        //  it's for this device
195
196        if(strcmp(name,ConnectionSV.name)==0) {
197                bool rc;
198                //IDLog("IndiDevice Switch %s\n",names[x]);
199
200                IUUpdateSwitch(&ConnectionSV,states,names,n);
201
202                if(ConnectionS[0].s==ISS_ON) {
203                    if(!Connected) {
204                        rc=Connect();
205                        if(rc) {
206                            ConnectionSV.s=IPS_OK;
207                            //setConnected(true,"calling setconnected");
208                            //ConnectionS[0].s=ISS_ON;
209                            //ConnectionS[1].s=ISS_OFF;
210                            Connected=true;
211                        } else {
212                            //ConnectionS[0].s=ISS_OFF;
213                            //ConnectionS[1].s=ISS_ON;
214                            ConnectionSV.s=IPS_ALERT;
215                            Connected=false;
216                        }
217                    }
218                    UpdateProperties();
219                    IDSetSwitch(&ConnectionSV,NULL);
220                } else {
221                    if(Connected) rc=Disconnect();
222                    //ConnectionS[0].s=ISS_OFF;
223                    //ConnectionS[1].s=ISS_ON;
224                    ConnectionSV.s=IPS_IDLE;
225                    Connected=false;
226                    UpdateProperties();
227                    IDSetSwitch(&ConnectionSV,NULL);
228                }
229
230                /*
231                if(strcmp(names[x],"CONNECT")==0) {
232                    //  We are being requested to make a physical connection to the device
233                    rc=Connect();
234                    if(rc) {
235                        //  Connection Succeeded
236                        ConnectionSV.s=IPS_OK;
237                        Connected=true;
238
239                    } else {
240                        //  Connection Failed
241                        ConnectionSV.s=IPS_ALERT;
242                        Connected=false;
243                    }
244                    IUUpdateSwitch(&ConnectionSV,states,names,n);
245                    IDSetSwitch(&ConnectionSV,NULL);
246                    IDLog("Connect ccalling update properties\n");
247                    UpdateProperties();
248                    //return true;
249                }
250                if(strcmp(names[x],"DISCONNECT")==0) {
251                    //  We are being told to disconnect from the device
252                    rc=Disconnect();
253                    if(rc) {
254                        ConnectionSV.s=IPS_IDLE;
255                    } else {
256                        ConnectionSV.s=IPS_ALERT;
257                    }
258                    Connected=false;
259                    IDLog("Disconnect calling update properties\n");
260                    UpdateProperties();
261                    //  And now lets tell everybody how it went
262                    IUUpdateSwitch(&ConnectionSV,states,names,n);
263                    IDSetSwitch(&ConnectionSV,NULL);
264                    return true;
265                }
266                */
267            //}
268        }
269    }
270
271
272    // let the default driver have a crack at it
273    return INDI::DefaultDriver::ISNewSwitch(dev, name, states, names, n);
274}
275
276//  This is a helper function
277//  that just encapsulates the Indi way into our clean c++ way of doing things
278int IndiDevice::SetTimer(int t)
279{
280    return IEAddTimer(t,timerfunc,this);
281}
282
283//  Just another helper to help encapsulate indi into a clean class
284void IndiDevice::RemoveTimer(int t)
285{
286    IERmTimer(t);
287    return;
288}
289//  This is just a placeholder
290//  This function should be overriden by child classes if they use timers
291//  So we should never get here
292void IndiDevice::TimerHit()
293{
294    return;
295}
296
297bool IndiDevice::Connect()
298{
299    //  We dont actually implement a device here
300    //  So we cannot connect to it
301IDLog("IndiDevice Connect, we should never get here\n");
302    IDMessage(deviceName(),"IndiDevice:: has no device attached....");
303    return false;
304}
305
306bool IndiDevice::Disconnect()
307{
308    //  Since we cannot connect, we cant disconnect either
309    IDMessage(deviceName(),"IndiDevice:: has no device to detach....");
310    return false;
311}
312
313bool IndiDevice::UpdateProperties()
314{
315    //  The base device has no properties to update
316    return true;
317}
318
319void IndiDevice::ISSnoopDevice (XMLEle *root)
320{
321      INDI_UNUSED(root);
322}
323
324bool IndiDevice::SaveConfig()
325{
326
327    // FIXME REMOVE THIS
328    return true;
329
330    char err[MAXRBUF];
331    FILE *fp;
332    //int rc;
333
334    fp=IUGetConfigFP(NULL,deviceName(),err);
335    if(fp != NULL) {
336        IUSaveConfigTag(fp,0);
337        //IUSaveConfigText(fp,&FilterNameTV);
338        //  Tell child classes to write any items they want
339        //  into persistent storage
340        WritePersistentConfig(fp);
341        IUSaveConfigTag(fp,1);
342        fclose(fp);
343        //IDMessage(deviceName(),"Configuration Saved\n");
344        return true;
345    } else {
346        IDMessage(deviceName(),"Save config failed\n");
347    }
348    return false;
349}
350
351bool IndiDevice::WritePersistentConfig(FILE *f)
352{
353    return false;
354}
355
356bool IndiDevice::LoadConfig()
357{
358    char err[MAXRBUF];
359    int rc;
360
361    rc=IUReadConfig(NULL,deviceName(),err);
362    if(rc==0) return true;
363    return false;
364}
Note: See TracBrowser for help on using the repository browser.