Instrument Neutral Distributed Interface INDI  1.9.2
indiproperty.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2011 Jasem Mutlaq. All rights reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 *******************************************************************************/
18 
19 #include "indiproperty.h"
20 #include "indiproperty_p.h"
21 
22 #include <cstdlib>
23 #include <cstring>
24 
25 namespace INDI
26 {
27 
29  : property(property)
30  , type(property ? type : INDI_UNKNOWN)
31  , registered(property != nullptr)
32 { }
33 
35  : property(property)
36  , type(property ? INDI_TEXT : INDI_UNKNOWN)
37  , registered(property != nullptr)
38 { }
39 
41  : property(property)
42  , type(property ? INDI_NUMBER : INDI_UNKNOWN)
43  , registered(property != nullptr)
44 { }
45 
47  : property(property)
48  , type(property ? INDI_SWITCH : INDI_UNKNOWN)
49  , registered(property != nullptr)
50 { }
51 
53  : property(property)
54  , type(property ? INDI_LIGHT : INDI_UNKNOWN)
55  , registered(property != nullptr)
56 { }
57 
59  : property(property)
60  , type(property ? INDI_BLOB : INDI_UNKNOWN)
61  , registered(property != nullptr)
62 { }
63 
64 #ifdef INDI_PROPERTY_BACKWARD_COMPATIBILE
66 {
67  return this;
68 }
69 
71 {
72  return this;
73 }
74 
75 Property::operator INDI::Property *()
76 {
77  D_PTR(Property);
78  return isValid() ? &d->self : nullptr;
79 }
80 
81 Property::operator const INDI::Property *() const
82 {
83  D_PTR(const Property);
84  return isValid() ? &d->self : nullptr;
85 }
86 #endif
87 
88 #define PROPERTY_CASE(CODE) \
89  switch (d->property != nullptr ? d->type : INDI_UNKNOWN) \
90  { \
91  case INDI_NUMBER: { auto property = static_cast<PropertyView<INumber> *>(d->property); CODE } break; \
92  case INDI_TEXT: { auto property = static_cast<PropertyView<IText> *>(d->property); CODE } break; \
93  case INDI_SWITCH: { auto property = static_cast<PropertyView<ISwitch> *>(d->property); CODE } break; \
94  case INDI_LIGHT: { auto property = static_cast<PropertyView<ILight> *>(d->property); CODE } break; \
95  case INDI_BLOB: { auto property = static_cast<PropertyView<IBLOB> *>(d->property); CODE } break; \
96  default:; \
97  }
98 
100 {
101  // Only delete properties if they were created dynamically via the buildSkeleton
102  // function. Other drivers are responsible for their own memory allocation.
103  if (property == nullptr || !dynamic)
104  return;
105 
106  auto d = this;
107  PROPERTY_CASE( delete property; )
108 }
109 
111  : d_ptr(new PropertyPrivate(nullptr, INDI_UNKNOWN))
112 { }
113 
114 Property::Property(void *property, INDI_PROPERTY_TYPE type)
115  : d_ptr(new PropertyPrivate(property, type))
116 { }
117 
118 Property::Property(INumberVectorProperty *property)
119  : d_ptr(new PropertyPrivate(property))
120 { }
121 
123  : d_ptr(new PropertyPrivate(property))
124 { }
125 
127  : d_ptr(new PropertyPrivate(property))
128 { }
129 
131  : d_ptr(new PropertyPrivate(property))
132 { }
133 
135  : d_ptr(new PropertyPrivate(property))
136 { }
137 
139 { }
140 
142  : d_ptr(&dd)
143 { }
144 
145 Property::Property(std::shared_ptr<PropertyPrivate> dd)
146  : d_ptr(dd)
147 { }
148 
149 void Property::setProperty(void *p)
150 {
151  D_PTR(Property);
152  d->type = p ? d->type : INDI_UNKNOWN;
153  d->registered = p != nullptr;
154  d->property = p;
155 }
156 
158 {
159  D_PTR(Property);
160  d->type = t;
161 }
162 
163 void Property::setRegistered(bool r)
164 {
165  D_PTR(Property);
166  d->registered = r;
167 }
168 
169 void Property::setDynamic(bool dyn)
170 {
171  D_PTR(Property);
172  d->dynamic = dyn;
173 }
174 
176 {
177  D_PTR(Property);
178  d->baseDevice = idp;
179 }
180 
181 void *Property::getProperty() const
182 {
183  D_PTR(const Property);
184  return d->property;
185 }
186 
188 {
189  D_PTR(const Property);
190  return d->property != nullptr ? d->type : INDI_UNKNOWN;
191 }
192 
193 const char *Property::getTypeAsString() const
194 {
195  switch (getType())
196  {
197  case INDI_NUMBER: return "INDI_NUMBER";
198  case INDI_SWITCH: return "INDI_SWITCH";
199  case INDI_TEXT: return "INDI_TEXT";
200  case INDI_LIGHT: return "INDI_LIGHT";
201  case INDI_BLOB: return "INDI_BLOB";
202  case INDI_UNKNOWN: return "INDI_UNKNOWN";
203  }
204  return "INDI_UNKNOWN";
205 }
206 
207 bool Property::getRegistered() const
208 {
209  D_PTR(const Property);
210  return d->registered;
211 }
212 
213 bool Property::isDynamic() const
214 {
215  D_PTR(const Property);
216  return d->dynamic;
217 }
218 
219 BaseDevice *Property::getBaseDevice() const
220 {
221  D_PTR(const Property);
222  return d->baseDevice;
223 }
224 
225 void Property::setName(const char *name)
226 {
227  D_PTR(Property);
228  PROPERTY_CASE( property->setName(name); )
229 }
230 
231 void Property::setLabel(const char *label)
232 {
233  D_PTR(Property);
234  PROPERTY_CASE( property->setLabel(label); )
235 }
236 
237 void Property::setGroupName(const char *group)
238 {
239  D_PTR(Property);
240  PROPERTY_CASE( property->setGroupName(group); )
241 }
242 
243 void Property::setDeviceName(const char *device)
244 {
245  D_PTR(Property);
246  PROPERTY_CASE( property->setDeviceName(device); )
247 }
248 
249 void Property::setTimestamp(const char *timestamp)
250 {
251  D_PTR(Property);
252  PROPERTY_CASE( property->setTimestamp(timestamp); )
253 }
254 
255 void Property::setState(IPState state)
256 {
257  D_PTR(Property);
258  PROPERTY_CASE( property->setState(state); )
259 }
260 
261 void Property::setPermission(IPerm permission)
262 {
263  D_PTR(Property);
264  PROPERTY_CASE( property->setPermission(permission); )
265 }
266 
267 void Property::setTimeout(double timeout)
268 {
269  D_PTR(Property);
270  PROPERTY_CASE( property->setTimeout(timeout); )
271 }
272 
273 const char *Property::getName() const
274 {
275  D_PTR(const Property);
276  PROPERTY_CASE( return property->getName(); )
277  return nullptr;
278 }
279 
280 const char *Property::getLabel() const
281 {
282  D_PTR(const Property);
283  PROPERTY_CASE( return property->getLabel(); )
284  return nullptr;
285 }
286 
287 const char *Property::getGroupName() const
288 {
289  D_PTR(const Property);
290  PROPERTY_CASE( return property->getGroupName(); )
291  return nullptr;
292 }
293 
294 const char *Property::getDeviceName() const
295 {
296  D_PTR(const Property);
297  PROPERTY_CASE( return property->getDeviceName(); )
298  return nullptr;
299 }
300 
301 const char *Property::getTimestamp() const
302 {
303  D_PTR(const Property);
304  PROPERTY_CASE( return property->getTimestamp(); )
305  return nullptr;
306 }
307 
308 IPState Property::getState() const
309 {
310  D_PTR(const Property);
311  PROPERTY_CASE( return property->getState(); )
312  return IPS_ALERT;
313 }
314 
315 const char *Property::getStateAsString() const
316 {
317  return pstateStr(getState());
318 }
319 
321 {
322  D_PTR(const Property);
323  PROPERTY_CASE( return property->getPermission(); )
324  return IP_RO;
325 }
326 
327 bool Property::isEmpty() const
328 {
329  D_PTR(const Property);
330  PROPERTY_CASE( return property->isEmpty(); )
331  return true;
332 }
333 
334 bool Property::isValid() const
335 {
336  D_PTR(const Property);
337  return d->type != INDI_UNKNOWN;
338 }
339 
340 bool Property::isNameMatch(const char *otherName) const
341 {
342  D_PTR(const Property);
343  PROPERTY_CASE( return property->isNameMatch(otherName); )
344  return false;
345 }
346 
347 bool Property::isNameMatch(const std::string &otherName) const
348 {
349  D_PTR(const Property);
350  PROPERTY_CASE( return property->isNameMatch(otherName); )
351  return false;
352 }
353 
354 bool Property::isLabelMatch(const char *otherLabel) const
355 {
356  D_PTR(const Property);
357  PROPERTY_CASE( return property->isLabelMatch(otherLabel); )
358  return false;
359 }
360 
361 bool Property::isLabelMatch(const std::string &otherLabel) const
362 {
363  D_PTR(const Property);
364  PROPERTY_CASE( return property->isLabelMatch(otherLabel); )
365  return false;
366 }
367 
369 {
370  D_PTR(const Property);
371  if (d->type == INDI_NUMBER)
372  return static_cast<PropertyView<INumber>*>(d->property);
373 
374  return nullptr;
375 }
376 
378 {
379  D_PTR(const Property);
380  if (d->type == INDI_TEXT)
381  return static_cast<PropertyView<IText>*>(d->property);
382 
383  return nullptr;
384 }
385 
387 {
388  D_PTR(const Property);
389  if (d->type == INDI_LIGHT)
390  return static_cast<PropertyView<ILight>*>(d->property);
391 
392  return nullptr;
393 }
394 
396 {
397  D_PTR(const Property);
398  if (d->type == INDI_SWITCH)
399  return static_cast<PropertyView<ISwitch>*>(d->property);
400 
401  return nullptr;
402 }
403 
405 {
406  D_PTR(const Property);
407  if (d->type == INDI_BLOB)
408  return static_cast<PropertyView<IBLOB>*>(d->property);
409 
410  return nullptr;
411 }
412 
413 void Property::save(FILE *fp) const
414 {
415  D_PTR(const Property);
416  PROPERTY_CASE( property->save(fp); )
417 }
418 
419 void Property::apply(const char *format, ...) const
420 {
421  D_PTR(const Property);
422  va_list ap;
423  va_start(ap, format);
424  PROPERTY_CASE( property->vapply(format, ap); )
425  va_end(ap);
426 }
427 
428 void Property::define(const char *format, ...) const
429 {
430  D_PTR(const Property);
431  va_list ap;
432  va_start(ap, format);
433  PROPERTY_CASE( property->vdefine(format, ap); )
434  va_end(ap);
435 }
436 
437 }
INDI_UNKNOWN
@ INDI_UNKNOWN
Definition: indidriver.c:62
INDI::Property::operator->
INDI::Property * operator->()
Definition: indiproperty.cpp:81
INDI_BLOB
@ INDI_BLOB
Definition: indidriver.c:61
INDI::Property::setProperty
void setProperty(void *)
Definition: indiproperty.cpp:165
IP_RO
@ IP_RO
Definition: indiapi.h:183
PROPERTY_CASE
#define PROPERTY_CASE(CODE)
Definition: indiproperty.cpp:104
INDI::Property::getRegistered
bool getRegistered() const
Definition: indiproperty.cpp:223
IPState
IPState
Property state.
Definition: indiapi.h:158
INDI::Property::getName
const char * getName() const
Definition: indiproperty.cpp:289
_ILightVectorProperty
Light vector property descriptor.
Definition: indiapi.h:415
INDI::Property::getState
IPState getState() const
Definition: indiproperty.cpp:324
INDI::Property::setGroupName
void setGroupName(const char *groupName)
Definition: indiproperty.cpp:253
INDI::Property::getDeviceName
const char * getDeviceName() const
Definition: indiproperty.cpp:310
INDI::Property::getProperty
void * getProperty() const
Definition: indiproperty.cpp:197
IPS_ALERT
@ IPS_ALERT
Definition: indiapi.h:163
INDI::Property::getTypeAsString
const char * getTypeAsString() const
Definition: indiproperty.cpp:209
_IBLOBVectorProperty
BLOB (Binary Large Object) vector property descriptor.
Definition: indiapi.h:469
INDI::Property::setTimestamp
void setTimestamp(const char *timestamp)
Definition: indiproperty.cpp:265
timestamp
const char * timestamp()
Create an ISO 8601 formatted time stamp. The format is YYYY-MM-DDTHH:MM:SS.
Definition: indicom.c:334
INDI::Property::isLabelMatch
bool isLabelMatch(const char *otherLabel) const
Definition: indiproperty.cpp:370
INDI::Property::getNumber
INDI::PropertyView< INumber > * getNumber() const
Definition: indiproperty.cpp:384
INDI_PROPERTY_TYPE
INDI_PROPERTY_TYPE
Definition: indibasetypes.h:22
INDI::PropertyPrivate::property
void * property
Definition: indiproperty_p.h:55
INDI::Property::getTimestamp
const char * getTimestamp() const
Definition: indiproperty.cpp:317
INDI::Property::setBaseDevice
void setBaseDevice(BaseDevice *idp)
Definition: indiproperty.cpp:191
INDI::Property::setType
void setType(INDI_PROPERTY_TYPE t)
Definition: indiproperty.cpp:173
INDI::Property::~Property
~Property()
Definition: indiproperty.cpp:154
INDI::Property::getPermission
IPerm getPermission() const
Definition: indiproperty.cpp:336
INDI::Property::apply
void void void apply() const
Definition: indiproperty.h:109
INDI::Property::isValid
bool isValid() const
Definition: indiproperty.cpp:350
INDI::Property
Provides generic container for INDI properties.
Definition: indiproperty.h:43
_ITextVectorProperty
Text vector property descriptor.
Definition: indiapi.h:244
INDI::Property::setLabel
void setLabel(const char *label)
Definition: indiproperty.cpp:247
INDI::Property::getBaseDevice
BaseDevice * getBaseDevice() const
Definition: indiproperty.cpp:235
device
hid_device * device
Definition: activefocuser_utils.cpp:92
type
__le16 type
Definition: pwc-ioctl.h:2
_INumberVectorProperty
Number vector property descriptor.
Definition: indiapi.h:317
INDI::Property::getBLOB
INDI::PropertyView< IBLOB > * getBLOB() const
Definition: indiproperty.cpp:420
INDI::Property::setState
void setState(IPState state)
Definition: indiproperty.cpp:271
pstateStr
const char * pstateStr(IPState s)
Definition: indicom.c:1194
INDI_NUMBER
@ INDI_NUMBER
Definition: indidriver.c:57
INDI_LIGHT
@ INDI_LIGHT
Definition: indidriver.c:60
INDI::PropertyPrivate::dynamic
bool dynamic
Definition: indiproperty_p.h:59
INDI::Property::setDynamic
void setDynamic(bool d)
Definition: indiproperty.cpp:185
INDI::PropertyPrivate::~PropertyPrivate
virtual ~PropertyPrivate()
Definition: indiproperty.cpp:115
INDI_TEXT
@ INDI_TEXT
Definition: indidriver.c:59
INDI::Property::getStateAsString
const char * getStateAsString() const
Definition: indiproperty.cpp:331
INDI::Property::getText
INDI::PropertyView< IText > * getText() const
Definition: indiproperty.cpp:393
Property
Definition: indiserver.c:100
INDI::Property::setDeviceName
void setDeviceName(const char *deviceName)
Definition: indiproperty.cpp:259
INDI::Property::getLabel
const char * getLabel() const
Definition: indiproperty.cpp:296
INDI::PropertyPrivate
Definition: indiproperty_p.h:52
IPerm
IPerm
Permission hint, with respect to client.
Definition: indiapi.h:181
INDI::PropertyView< INumber >
INDI::Property::getLight
INDI::PropertyView< ILight > * getLight() const
Definition: indiproperty.cpp:402
INDI::PropertyPrivate::PropertyPrivate
PropertyPrivate(void *property, INDI_PROPERTY_TYPE type)
Definition: indiproperty.cpp:44
INDI::Property::isNameMatch
bool isNameMatch(const char *otherName) const
Definition: indiproperty.cpp:356
INDI::Property::setTimeout
void setTimeout(double timeout)
Definition: indiproperty.cpp:283
INDI::Property::isDynamic
bool isDynamic() const
Definition: indiproperty.cpp:229
INDI::Property::getType
INDI_PROPERTY_TYPE getType() const
Definition: indiproperty.cpp:203
indiproperty.h
INDI::Property::getGroupName
const char * getGroupName() const
Definition: indiproperty.cpp:303
INDI_SWITCH
@ INDI_SWITCH
Definition: indidriver.c:58
INDI::Property::Property
Property()
Definition: indiproperty.cpp:126
INDI
Namespace to encapsulate INDI client, drivers, and mediator classes.
Definition: AlignmentSubsystemForClients.cpp:11
INDI::Property::setRegistered
void setRegistered(bool r)
Definition: indiproperty.cpp:179
indiproperty_p.h
INDI::Property::save
void save(FILE *fp) const
Definition: indiproperty.cpp:429
INDI::Property::isEmpty
bool isEmpty() const
Definition: indiproperty.cpp:343
INDI::Property::setPermission
void setPermission(IPerm permission)
Definition: indiproperty.cpp:277
INDI::BaseDevice
Class to provide basic INDI device functionality.
Definition: basedevice.h:45
INDI::Property::getSwitch
INDI::PropertyView< ISwitch > * getSwitch() const
Definition: indiproperty.cpp:411
INDI::Property::define
void define() const
Definition: indiproperty.h:110
_ISwitchVectorProperty
Switch vector property descriptor.
Definition: indiapi.h:365
INDI::Property::setName
void setName(const char *name)
Definition: indiproperty.cpp:241