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