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 | #ifndef INDIBASECLIENT_H |
---|
20 | #define INDIBASECLIENT_H |
---|
21 | |
---|
22 | #include <vector> |
---|
23 | #include <map> |
---|
24 | #include <string> |
---|
25 | |
---|
26 | #include "indiapi.h" |
---|
27 | #include "indidevapi.h" |
---|
28 | #include "indibase.h" |
---|
29 | |
---|
30 | #define MAXRBUF 2048 |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | /** |
---|
35 | * \class INDI::BaseClient |
---|
36 | \brief Class to provide basic client functionality. |
---|
37 | |
---|
38 | BaseClient enables accelerated development of INDI Clients by providing a framework that facilitates communication, device |
---|
39 | handling, and event notification. By subclassing BaseClient, clients can quickly connect to an INDI server, and query for |
---|
40 | a set of INDI::BaseDriver devices, and read and write properties seamlessly. Event driven programming is possible due to |
---|
41 | notifications upon reception of new devices or properties. |
---|
42 | |
---|
43 | \attention All notifications functions defined in INDI::BaseMediator must be implemented in the client class even if |
---|
44 | they are not used because these are pure virtual functions. |
---|
45 | |
---|
46 | \see <a href=http://indilib.org/index.php?title=Learn_how_to_write_INDI_clients>INDI Client Tutorial</a> for more details. |
---|
47 | \author Jasem Mutlaq |
---|
48 | |
---|
49 | */ |
---|
50 | class INDI::BaseClient : public INDI::BaseMediator |
---|
51 | { |
---|
52 | public: |
---|
53 | enum { INDI_DEVICE_NOT_FOUND=-1, INDI_PROPERTY_INVALID=-2, INDI_PROPERTY_DUPLICATED = -3, INDI_DISPATCH_ERROR=-4 }; |
---|
54 | //typedef boost::shared_ptr<INDI::BaseDriver> devicePtr; |
---|
55 | |
---|
56 | BaseClient(); |
---|
57 | ~BaseClient(); |
---|
58 | |
---|
59 | /** \brief Set the server host name and port |
---|
60 | \param hostname INDI server host name or IP address. |
---|
61 | \param port INDI server port. |
---|
62 | */ |
---|
63 | void setServer(const char * hostname, unsigned int port); |
---|
64 | |
---|
65 | /** \brief Add a device to the watch list. |
---|
66 | |
---|
67 | A client may select to receive notifications of only a specific device or a set of devices. |
---|
68 | If the client encounters any of the devices set via this function, it will create a corresponding |
---|
69 | INDI::BaseDriver object to handle them. If no devices are watched, then all devices owned by INDI server |
---|
70 | will be created and handled. |
---|
71 | */ |
---|
72 | void watchDevice(const char * deviceName); |
---|
73 | |
---|
74 | |
---|
75 | /** \brief Connect to INDI server. |
---|
76 | |
---|
77 | \returns True if the connection is successful, false otherwise. |
---|
78 | \note This function blocks until connection is either successull or unsuccessful. |
---|
79 | */ |
---|
80 | bool connectServer(); |
---|
81 | |
---|
82 | /** \brief Disconnect from INDI server. |
---|
83 | |
---|
84 | Disconnects from INDI servers. Any devices previously created will be deleted and memory cleared. |
---|
85 | \return True if disconnection is successful, false otherwise. |
---|
86 | */ |
---|
87 | bool disconnectServer(); |
---|
88 | |
---|
89 | /** \brief Connect/Disconnect to INDI driver |
---|
90 | \param status If true, the client will attempt to turn on CONNECTION property within the driver (i.e. turn on the device). |
---|
91 | Otherwise, CONNECTION will be turned off. |
---|
92 | \param deviceName Name of the device to connect to. |
---|
93 | */ |
---|
94 | void setDriverConnection(bool status, const char *deviceName); |
---|
95 | |
---|
96 | /** \param deviceName Name of device to search for in the list of devices owned by INDI server, |
---|
97 | \returns If \e deviceName exists, it returns an instance of the device. Otherwise, it returns NULL. |
---|
98 | */ |
---|
99 | INDI::BaseDriver * getDriver(const char * deviceName); |
---|
100 | |
---|
101 | /** \returns Returns a vector of all devices created in the client. |
---|
102 | */ |
---|
103 | const vector<INDI::BaseDriver *> & getDrivers() const { return cDevices; } |
---|
104 | |
---|
105 | /** \brief Set Binary Large Object policy mode |
---|
106 | |
---|
107 | Set the BLOB handling mode for the client. The client may either recieve: |
---|
108 | <ul> |
---|
109 | <li>Only BLOBS</li> |
---|
110 | <li>BLOBs mixed with normal messages</li> |
---|
111 | <li>Normal messages only, no BLOBs</li> |
---|
112 | </ul> |
---|
113 | |
---|
114 | If \e dev and \e prop are supplied, then the BLOB handling policy is set for this particular device and property. |
---|
115 | if \e prop is NULL, then the BLOB policy applies to the whole device. |
---|
116 | |
---|
117 | |
---|
118 | \param blobH BLOB handling policy |
---|
119 | \param dev name of device, required. |
---|
120 | \param prop name of property, optional. |
---|
121 | */ |
---|
122 | void setBLOBMode(BLOBHandling blobH, const char *dev, const char *prop = NULL); |
---|
123 | |
---|
124 | // Update |
---|
125 | static void * listenHelper(void *context); |
---|
126 | |
---|
127 | protected: |
---|
128 | |
---|
129 | /** \brief Dispatch command received from INDI server to respective devices handled by the client */ |
---|
130 | int dispatchCommand(XMLEle *root, char* errmsg); |
---|
131 | |
---|
132 | /** \brief Remove device */ |
---|
133 | int removeDevice( const char * devName, char * errmsg ); |
---|
134 | |
---|
135 | /** \brief Delete property command */ |
---|
136 | int delPropertyCmd (XMLEle *root, char * errmsg); |
---|
137 | |
---|
138 | /** \brief Find and return a particular device */ |
---|
139 | INDI::BaseDriver * findDev( const char * devName, char * errmsg); |
---|
140 | /** \brief Add a new device */ |
---|
141 | INDI::BaseDriver * addDevice (XMLEle *dep, char * errmsg); |
---|
142 | /** \brief Find a device, and if it doesn't exist, create it if create is set to 1 */ |
---|
143 | INDI::BaseDriver * findDev (XMLEle *root, int create, char * errmsg); |
---|
144 | |
---|
145 | /** Process messages */ |
---|
146 | int messageCmd (XMLEle *root, char * errmsg); |
---|
147 | /** Process messages */ |
---|
148 | void checkMsg (XMLEle *root, INDI::BaseDriver *dp); |
---|
149 | /** Process messages */ |
---|
150 | void doMsg (XMLEle *msg, INDI::BaseDriver *dp); |
---|
151 | |
---|
152 | /** \brief Send new Text command to server */ |
---|
153 | void sendNewText (ITextVectorProperty *pp); |
---|
154 | /** \brief Send new Number command to server */ |
---|
155 | void sendNewNumber (INumberVectorProperty *pp); |
---|
156 | /** \brief Send new Switch command to server */ |
---|
157 | void sendNewSwitch (ISwitchVectorProperty *pp, ISwitch *lp); |
---|
158 | /** \brief Send opening tag for BLOB command to server */ |
---|
159 | void startBlob( const char *devName, const char *propName, const char *timestamp); |
---|
160 | /** \brief Send ONE blob content to server */ |
---|
161 | void sendOneBlob( const char *blobName, unsigned int blobSize, const char *blobFormat, unsigned char * blobBuffer); |
---|
162 | /** \brief Send closing tag for BLOB command to server */ |
---|
163 | void finishBlob(); |
---|
164 | |
---|
165 | private: |
---|
166 | |
---|
167 | // Listen to INDI server and process incoming messages |
---|
168 | void listenINDI(); |
---|
169 | |
---|
170 | // Thread for listenINDI() |
---|
171 | pthread_t listen_thread; |
---|
172 | |
---|
173 | vector<INDI::BaseDriver *> cDevices; |
---|
174 | vector<string> cDeviceNames; |
---|
175 | |
---|
176 | string cServer; |
---|
177 | unsigned int cPort; |
---|
178 | bool sConnected; |
---|
179 | |
---|
180 | // Parse & FILE buffers for IO |
---|
181 | int sockfd; |
---|
182 | LilXML *lillp; /* XML parser context */ |
---|
183 | FILE *svrwfp; /* FILE * to talk to server */ |
---|
184 | |
---|
185 | }; |
---|
186 | |
---|
187 | #endif // INDIBASECLIENT_H |
---|