Instrument Neutral Distributed Interface INDI  1.6.0
baseclient.h
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 #pragma once
20 
21 #include "indiapi.h"
22 #include "indibase.h"
23 
24 #include <string>
25 #include <vector>
26 
27 #include <thread>
28 
29 #ifdef _WINDOWS
30 #include <WinSock2.h>
31 #endif
32 
33 #define MAXRBUF 2048
34 
55 {
56  public:
57  BaseClient();
58  virtual ~BaseClient();
59 
64  void setServer(const char *hostname, unsigned int port);
65 
73  void watchDevice(const char *deviceName);
74 
80  bool connectServer();
81 
87  bool disconnectServer();
88 
89  bool isServerConnected() const;
90 
94  void connectDevice(const char *deviceName);
95 
99  void disconnectDevice(const char *deviceName);
100 
104  INDI::BaseDevice *getDevice(const char *deviceName);
105 
108  const std::vector<INDI::BaseDevice *> &getDevices() const { return cDevices; }
109 
125  bool getDevices(std::vector<INDI::BaseDevice *> &deviceList, uint16_t driverInterface);
126 
143  void setBLOBMode(BLOBHandling blobH, const char *dev, const char *prop = NULL);
144 
151  BLOBHandling getBLOBMode(const char *dev, const char *prop = NULL);
152 
153  // Update
154  static void *listenHelper(void *context);
155 
156  const char *getHost() { return cServer.c_str(); }
157  int getPort() { return cPort; }
158 
162  void sendNewText(const char *deviceName, const char *propertyName, const char *elementName, const char *text);
166  void sendNewNumber(const char *deviceName, const char *propertyName, const char *elementName, double value);
170  void sendNewSwitch(const char *deviceName, const char *propertyName, const char *elementName);
171 
173  void startBlob(const char *devName, const char *propName, const char *timestamp);
175  void sendOneBlob(IBLOB *bp);
177  void sendOneBlob(const char *blobName, unsigned int blobSize, const char *blobFormat, void *blobBuffer);
179  void finishBlob();
180 
186  void setVerbose(bool enable) { verbose = enable; }
187 
192  bool isVerbose() const { return verbose; }
193 
199  void setConnectionTimeout(uint32_t seconds, uint32_t microseconds)
200  {
201  timeout_sec = seconds;
202  timeout_us = microseconds;
203  }
204 
205  protected:
207  int dispatchCommand(XMLEle *root, char *errmsg);
208 
210  int deleteDevice(const char *devName, char *errmsg);
211 
213  int delPropertyCmd(XMLEle *root, char *errmsg);
214 
216  INDI::BaseDevice *findDev(const char *devName, char *errmsg);
218  INDI::BaseDevice *addDevice(XMLEle *dep, char *errmsg);
220  INDI::BaseDevice *findDev(XMLEle *root, int create, char *errmsg);
221 
223  int messageCmd(XMLEle *root, char *errmsg);
224 
230  virtual void newUniversalMessage(std::string message);
231 
232  private:
233  typedef struct
234  {
235  std::string device;
236  std::string property;
237  BLOBHandling blobMode;
238  } BLOBMode;
239 
240  BLOBMode *findBLOBMode(const std::string& device, const std::string& property);
241 
247  void setDriverConnection(bool status, const char *deviceName);
248 
252  void clear();
253 
254  std::thread *listen_thread=nullptr;
255 
256 #ifdef _WINDOWS
257  SOCKET sockfd;
258 #else
259  int sockfd;
260  int m_receiveFd;
261  int m_sendFd;
262 #endif
263 
264  // Listen to INDI server and process incoming messages
265  void listenINDI();
266 
267  void sendString(const char *fmt, ...);
268 
269  std::vector<INDI::BaseDevice *> cDevices;
270  std::vector<std::string> cDeviceNames;
271  std::vector<BLOBMode *> blobModes;
272 
273  std::string cServer;
274  unsigned int cPort;
275  bool sConnected;
276  bool verbose;
277 
278  // Parse & FILE buffers for IO
279 
280  LilXML *lillp; /* XML parser context */
281  uint32_t timeout_sec, timeout_us;
282 };
void setVerbose(bool enable)
setVerbose Set verbose mode
Definition: baseclient.h:186
const char * timestamp()
Create an ISO 8601 formatted time stamp. The format is YYYY-MM-DDTHH:MM:SS.
Definition: indicom.c:300
void setBLOBMode(BLOBHandling blobH, const char *dev, const char *prop=NULL)
Set Binary Large Object policy mode.
Definition: baseclient.cpp:947
static void * listenHelper(void *context)
Definition: baseclient.cpp:356
Definition: lilxml.c:90
void disconnectDevice(const char *deviceName)
Disconnect INDI driver.
Definition: baseclient.cpp:296
INDI::BaseDevice * findDev(const char *devName, char *errmsg)
Find and return a particular device.
Definition: baseclient.cpp:592
One Blob (Binary Large Object) descriptor.
Definition: indiapi.h:423
void sendOneBlob(IBLOB *bp)
Send ONE blob content to server. The BLOB data in raw binary format and will be converted to base64 a...
Definition: baseclient.cpp:869
int deleteDevice(const char *devName, char *errmsg)
Remove device.
Definition: baseclient.cpp:571
bool isVerbose() const
isVerbose Is client in verbose mode?
Definition: baseclient.h:192
Class to provide basic INDI device functionality.
Definition: basedevice.h:44
Switch vector property descriptor.
Definition: indiapi.h:347
void sendNewSwitch(ISwitchVectorProperty *pp)
Send new Switch command to server.
Definition: baseclient.cpp:808
virtual ~BaseClient()
Definition: baseclient.cpp:69
int dispatchCommand(XMLEle *root, char *errmsg)
Dispatch command received from INDI server to respective devices handled by the client.
Definition: baseclient.cpp:497
BLOBHandling getBLOBMode(const char *dev, const char *prop=NULL)
getBLOBMode Get Binary Large Object policy mode IF set previously by setBLOBMode
Definition: baseclient.cpp:992
bool isServerConnected() const
Definition: baseclient.cpp:286
void sendNewText(ITextVectorProperty *pp)
Send new Text command to server.
Definition: baseclient.cpp:723
void startBlob(const char *devName, const char *propName, const char *timestamp)
Send opening tag for BLOB command to server.
Definition: baseclient.cpp:861
BLOBHandling
How drivers handle BLOBs incoming from snooping drivers.
Definition: indidevapi.h:292
Constants and Data structure definitions for the interface to the reference INDI C API implementation...
void setConnectionTimeout(uint32_t seconds, uint32_t microseconds)
setConnectionTimeout Set connection timeout. By default it is 3 seconds.
Definition: baseclient.h:199
Number vector property descriptor.
Definition: indiapi.h:299
INDI::BaseDevice * getDevice(const char *deviceName)
Definition: baseclient.cpp:346
void sendNewNumber(INumberVectorProperty *pp)
Send new Number command to server.
Definition: baseclient.cpp:765
const std::vector< INDI::BaseDevice * > & getDevices() const
Definition: baseclient.h:108
bool connectServer()
Connect to INDI server.
Definition: baseclient.cpp:95
void watchDevice(const char *deviceName)
Add a device to the watch list.
Definition: baseclient.cpp:90
void setServer(const char *hostname, unsigned int port)
Set the server host name and port.
Definition: baseclient.cpp:84
bool disconnectServer()
Disconnect from INDI server.
Definition: baseclient.cpp:255
virtual void newUniversalMessage(std::string message)
newUniversalMessage Universal messages are sent from INDI server without a specific device...
Definition: baseclient.cpp:718
void connectDevice(const char *deviceName)
Connect to INDI driver.
Definition: baseclient.cpp:291
int delPropertyCmd(XMLEle *root, char *errmsg)
Delete property command.
Definition: baseclient.cpp:538
const char * getHost()
Definition: baseclient.h:156
int messageCmd(XMLEle *root, char *errmsg)
Definition: baseclient.cpp:673
Text vector property descriptor.
Definition: indiapi.h:226
Class to provide basic client functionality.
Definition: baseclient.h:54
INDI::BaseDevice * addDevice(XMLEle *dep, char *errmsg)
Add a new device.
Definition: baseclient.cpp:607
void finishBlob()
Send closing tag for BLOB command to server.
Definition: baseclient.cpp:942
Meditates event notification as generated by driver and passed to clients.
Definition: indibase.h:77