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 | |
---|
26 | IndiDevice *device=NULL; |
---|
27 | |
---|
28 | |
---|
29 | /************************************************************************************** |
---|
30 | ** |
---|
31 | ***************************************************************************************/ |
---|
32 | void 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 | ***************************************************************************************/ |
---|
56 | void 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 | ***************************************************************************************/ |
---|
68 | void 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 | ***************************************************************************************/ |
---|
78 | void 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 | ***************************************************************************************/ |
---|
88 | void 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 | ***************************************************************************************/ |
---|
103 | void ISSnoopDevice (XMLEle *root) |
---|
104 | { |
---|
105 | return device->ISSnoopDevice(root); |
---|
106 | } |
---|
107 | |
---|
108 | void 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 | |
---|
121 | IndiDevice::IndiDevice() |
---|
122 | { |
---|
123 | //ctor |
---|
124 | Connected=false; |
---|
125 | } |
---|
126 | |
---|
127 | IndiDevice::~IndiDevice() |
---|
128 | { |
---|
129 | //dtor |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | int 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 | |
---|
146 | bool IndiDevice::DeleteProperty(char *n) |
---|
147 | { |
---|
148 | IDDelete(deviceName(),n,NULL); |
---|
149 | return true; |
---|
150 | } |
---|
151 | |
---|
152 | void 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 | ***************************************************************************************/ |
---|
169 | bool 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 | ***************************************************************************************/ |
---|
179 | bool 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 | ***************************************************************************************/ |
---|
189 | bool 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 |
---|
278 | int 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 |
---|
284 | void 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 |
---|
292 | void IndiDevice::TimerHit() |
---|
293 | { |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | bool IndiDevice::Connect() |
---|
298 | { |
---|
299 | // We dont actually implement a device here |
---|
300 | // So we cannot connect to it |
---|
301 | IDMessage(deviceName(),"IndiDevice:: has no device attached...."); |
---|
302 | return false; |
---|
303 | } |
---|
304 | |
---|
305 | bool IndiDevice::Disconnect() |
---|
306 | { |
---|
307 | // Since we cannot connect, we cant disconnect either |
---|
308 | IDMessage(deviceName(),"IndiDevice:: has no device to detach...."); |
---|
309 | return false; |
---|
310 | } |
---|
311 | |
---|
312 | bool IndiDevice::UpdateProperties() |
---|
313 | { |
---|
314 | // The base device has no properties to update |
---|
315 | return true; |
---|
316 | } |
---|
317 | |
---|
318 | void IndiDevice::ISSnoopDevice (XMLEle *root) |
---|
319 | { |
---|
320 | INDI_UNUSED(root); |
---|
321 | } |
---|
322 | |
---|
323 | bool IndiDevice::SaveConfig() |
---|
324 | { |
---|
325 | |
---|
326 | // FIXME REMOVE THIS |
---|
327 | return true; |
---|
328 | |
---|
329 | char err[MAXRBUF]; |
---|
330 | FILE *fp; |
---|
331 | //int rc; |
---|
332 | |
---|
333 | fp=IUGetConfigFP(NULL,deviceName(),err); |
---|
334 | if(fp != NULL) { |
---|
335 | IUSaveConfigTag(fp,0); |
---|
336 | //IUSaveConfigText(fp,&FilterNameTV); |
---|
337 | // Tell child classes to write any items they want |
---|
338 | // into persistent storage |
---|
339 | WritePersistentConfig(fp); |
---|
340 | IUSaveConfigTag(fp,1); |
---|
341 | fclose(fp); |
---|
342 | //IDMessage(deviceName(),"Configuration Saved\n"); |
---|
343 | return true; |
---|
344 | } else { |
---|
345 | IDMessage(deviceName(),"Save config failed\n"); |
---|
346 | } |
---|
347 | return false; |
---|
348 | } |
---|
349 | |
---|
350 | bool IndiDevice::WritePersistentConfig(FILE *f) |
---|
351 | { |
---|
352 | return false; |
---|
353 | } |
---|
354 | |
---|
355 | bool IndiDevice::LoadConfig() |
---|
356 | { |
---|
357 | char err[MAXRBUF]; |
---|
358 | int rc; |
---|
359 | |
---|
360 | rc=IUReadConfig(NULL,deviceName(),err); |
---|
361 | if(rc==0) return true; |
---|
362 | return false; |
---|
363 | } |
---|