Instrument Neutral Distributed Interface INDI  1.5.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 
92  void connectDevice(const char *deviceName);
93 
97  void disconnectDevice(const char *deviceName);
98 
102  INDI::BaseDevice *getDevice(const char *deviceName);
103 
106  const std::vector<INDI::BaseDevice *> &getDevices() const { return cDevices; }
107 
123  bool getDevices(std::vector<INDI::BaseDevice *> &deviceList, uint16_t driverInterface);
124 
141  void setBLOBMode(BLOBHandling blobH, const char *dev, const char *prop = NULL);
142 
149  BLOBHandling getBLOBMode(const char *dev, const char *prop = NULL);
150 
151  // Update
152  static void *listenHelper(void *context);
153 
154  const char *getHost() { return cServer.c_str(); }
155  int getPort() { return cPort; }
156 
160  void sendNewText(const char *deviceName, const char *propertyName, const char *elementName, const char *text);
164  void sendNewNumber(const char *deviceName, const char *propertyName, const char *elementName, double value);
168  void sendNewSwitch(const char *deviceName, const char *propertyName, const char *elementName);
169 
171  void startBlob(const char *devName, const char *propName, const char *timestamp);
173  void sendOneBlob(IBLOB *bp);
175  void sendOneBlob(const char *blobName, unsigned int blobSize, const char *blobFormat, void *blobBuffer);
177  void finishBlob();
178 
184  void setVerbose(bool enable) { verbose = enable; }
185 
190  bool isVerbose() const { return verbose; }
191 
197  void setConnectionTimeout(uint32_t seconds, uint32_t microseconds)
198  {
199  timeout_sec = seconds;
200  timeout_us = microseconds;
201  }
202 
203  protected:
205  int dispatchCommand(XMLEle *root, char *errmsg);
206 
208  int deleteDevice(const char *devName, char *errmsg);
209 
211  int delPropertyCmd(XMLEle *root, char *errmsg);
212 
214  INDI::BaseDevice *findDev(const char *devName, char *errmsg);
216  INDI::BaseDevice *addDevice(XMLEle *dep, char *errmsg);
218  INDI::BaseDevice *findDev(XMLEle *root, int create, char *errmsg);
219 
221  int messageCmd(XMLEle *root, char *errmsg);
222 
228  virtual void newUniversalMessage(std::string message);
229 
230  private:
231  typedef struct
232  {
233  std::string device;
234  std::string property;
235  BLOBHandling blobMode;
236  } BLOBMode;
237 
238  BLOBMode *findBLOBMode(const std::string& device, const std::string& property);
239 
245  void setDriverConnection(bool status, const char *deviceName);
246 
250  void clear();
251 
252  std::thread *listen_thread=nullptr;
253 
254 #ifdef _WINDOWS
255  SOCKET sockfd;
256 #else
257  int sockfd;
258  int m_receiveFd;
259  int m_sendFd;
260 #endif
261 
262  // Listen to INDI server and process incoming messages
263  void listenINDI();
264 
265  void sendString(const char *fmt, ...);
266 
267  std::vector<INDI::BaseDevice *> cDevices;
268  std::vector<std::string> cDeviceNames;
269  std::vector<BLOBMode *> blobModes;
270 
271  std::string cServer;
272  unsigned int cPort;
273  bool sConnected;
274  bool verbose;
275 
276  // Parse & FILE buffers for IO
277 
278  LilXML *lillp; /* XML parser context */
279  uint32_t timeout_sec, timeout_us;
280 };
void setVerbose(bool enable)
setVerbose Set verbose mode
Definition: baseclient.h:184
const char * timestamp()
Create an ISO 8601 formatted time stamp. The format is YYYY-MM-DDTHH:MM:SS.
Definition: indicom.c:298
void setBLOBMode(BLOBHandling blobH, const char *dev, const char *prop=NULL)
Set Binary Large Object policy mode.
Definition: baseclient.cpp:933
static void * listenHelper(void *context)
Definition: baseclient.cpp:347
Definition: lilxml.c:90
void disconnectDevice(const char *deviceName)
Disconnect INDI driver.
Definition: baseclient.cpp:287
IndiDevice * device
Definition: indidevice.cpp:26
INDI::BaseDevice * findDev(const char *devName, char *errmsg)
Find and return a particular device.
Definition: baseclient.cpp:578
One Blob (Binary Large Object) descriptor.
Definition: indiapi.h:413
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:855
int deleteDevice(const char *devName, char *errmsg)
Remove device.
Definition: baseclient.cpp:557
bool isVerbose() const
isVerbose Is client in verbose mode?
Definition: baseclient.h:190
Class to provide basic INDI device functionality.
Definition: basedevice.h:41
Switch vector property descriptor.
Definition: indiapi.h:337
void sendNewSwitch(ISwitchVectorProperty *pp)
Send new Switch command to server.
Definition: baseclient.cpp:794
virtual ~BaseClient()
Definition: baseclient.cpp:68
int dispatchCommand(XMLEle *root, char *errmsg)
Dispatch command received from INDI server to respective devices handled by the client.
Definition: baseclient.cpp:488
BLOBHandling getBLOBMode(const char *dev, const char *prop=NULL)
getBLOBMode Get Binary Large Object policy mode IF set previously by setBLOBMode
Definition: baseclient.cpp:978
void sendNewText(ITextVectorProperty *pp)
Send new Text command to server.
Definition: baseclient.cpp:709
void startBlob(const char *devName, const char *propName, const char *timestamp)
Send opening tag for BLOB command to server.
Definition: baseclient.cpp:847
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:197
Number vector property descriptor.
Definition: indiapi.h:289
INDI::BaseDevice * getDevice(const char *deviceName)
Definition: baseclient.cpp:337
void sendNewNumber(INumberVectorProperty *pp)
Send new Number command to server.
Definition: baseclient.cpp:751
const std::vector< INDI::BaseDevice * > & getDevices() const
Definition: baseclient.h:106
bool connectServer()
Connect to INDI server.
Definition: baseclient.cpp:94
void watchDevice(const char *deviceName)
Add a device to the watch list.
Definition: baseclient.cpp:89
void setServer(const char *hostname, unsigned int port)
Set the server host name and port.
Definition: baseclient.cpp:83
bool disconnectServer()
Disconnect from INDI server.
Definition: baseclient.cpp:254
virtual void newUniversalMessage(std::string message)
newUniversalMessage Universal messages are sent from INDI server without a specific device...
Definition: baseclient.cpp:704
void connectDevice(const char *deviceName)
Connect to INDI driver.
Definition: baseclient.cpp:282
int delPropertyCmd(XMLEle *root, char *errmsg)
Delete property command.
Definition: baseclient.cpp:529
const char * getHost()
Definition: baseclient.h:154
int messageCmd(XMLEle *root, char *errmsg)
Definition: baseclient.cpp:659
Text vector property descriptor.
Definition: indiapi.h:216
Class to provide basic client functionality.
Definition: baseclient.h:54
INDI::BaseDevice * addDevice(XMLEle *dep, char *errmsg)
Add a new device.
Definition: baseclient.cpp:593
void finishBlob()
Send closing tag for BLOB command to server.
Definition: baseclient.cpp:928
Meditates event notification as generated by driver and passed to clients.
Definition: indibase.h:73