MagAO-X
Operations Applications Utilities Source
All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros Modules Pages
indiDriver.hpp
Go to the documentation of this file.
1 /** \file indiDriver.hpp
2  * \brief MagAO-X INDI Driver Wrapper
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  * History:
6  * - 2018-05-26 created by JRM
7  */
8 
9 #ifndef app_indiDriver_hpp
10 #define app_indiDriver_hpp
11 
12 #include "../../INDI/libcommon/IndiDriver.hpp"
13 #include "../../INDI/libcommon/IndiElement.hpp"
14 
15 #include "../../INDI/libcommon/IndiClient.hpp"
16 
17 #include "MagAOXApp.hpp"
18 
19 namespace MagAOX
20 {
21 namespace app
22 {
23 
24 template<class _parentT>
25 class indiDriver : public pcf::IndiDriver
26 {
27 public:
28 
29  ///The parent MagAOX app.
30  typedef _parentT parentT;
31 
32 protected:
33 
34  ///This objects parent class
35  parentT * m_parent {nullptr};
36 
37  ///An INDI Client is used to send commands to other drivers.
38  pcf::IndiClient * m_outGoing {nullptr};
39 
40  ///The IP address of the server for the INDI Client connection
41  std::string m_serverIPAddress {"127.0.01"};
42 
43  ///The port of the server for the INDI Client connection
44  int m_serverPort {7624};
45 
46 private:
47 
48  /// Flag to hold the status of this connection.
49  bool m_good {true};
50 
51 public:
52 
53  /// Public c'tor
54  /** Call pcf::IndiDriver c'tor, and then opens the FIFOs specified
55  * by parent. If this fails, then m_good is set to false.
56  * test this with good().
57  */
58  indiDriver( parentT * parent,
59  const std::string &szName,
60  const std::string &szDriverVersion,
61  const std::string &szProtocolVersion
62  );
63 
64  /// D'tor, deletes the IndiClient pointer.
65  ~indiDriver();
66 
67  /// Get the value of the good flag.
68  /**
69  * \returns the value of m_good, true or false.
70  */
71  bool good(){ return m_good;}
72 
73  // override callbacks
74  virtual void handleDefProperty( const pcf::IndiProperty &ipRecv );
75 
76  virtual void handleGetProperties( const pcf::IndiProperty &ipRecv );
77 
78  virtual void handleNewProperty( const pcf::IndiProperty &ipRecv );
79 
80  virtual void handleSetProperty( const pcf::IndiProperty &ipRecv );
81 
82  /// Define the execute virtual function. This runs the processIndiRequests function in this thread, and does not return.
83  virtual void execute(void);
84 
85  /// Define the update virt. func. here so the uptime message isn't sent
86  virtual void update();
87 
88  /// Send a newProperty command to another INDI driver
89  /** Uses the IndiClient member of this class, which is initialized the first time if necessary.
90  *
91  * \returns 0 on success
92  * \returns -1 on any errors (which are logged).
93  */
94  virtual int sendNewProperty( const pcf::IndiProperty &ipRecv );
95 
96 };
97 
98 template<class parentT>
100  const std::string &szName,
101  const std::string &szDriverVersion,
102  const std::string &szProtocolVersion
103  ) : pcf::IndiDriver(szName, szDriverVersion, szProtocolVersion)
104 {
105  m_parent = parent;
106 
107  int fd;
108 
109  errno = 0;
110  fd = open( parent->driverInName().c_str(), O_RDWR);
111  if(fd < 0)
112  {
113  m_good = false;
114  return;
115  }
116  setInputFd(fd);
117 
118  errno = 0;
119  fd = open( parent->driverOutName().c_str(), O_RDWR);
120  if(fd < 0)
121  {
122  m_good = false;
123  return;
124  }
125  setOutputFd(fd);
126 
127  // Open the ctrl fifo and write a single byte to it to trigger a restart
128  // of the xindidriver process.
129  // This allows indiserver to refresh everything.
130  errno = 0;
131  fd = open( parent->driverCtrlName().c_str(), O_RDWR);
132  if(fd < 0)
133  {
134  m_good = false;
135  return;
136  }
137  char c = 0;
138  write(fd, &c, 1);
139  close(fd);
140 }
141 
142 template<class parentT>
144 {
145  if(m_outGoing) delete m_outGoing;
146 
147 }
148 template<class parentT>
149 void indiDriver<parentT>::handleDefProperty( const pcf::IndiProperty &ipRecv )
150 {
151  if(m_parent) m_parent->handleDefProperty(ipRecv);
152 }
153 
154 template<class parentT>
155 void indiDriver<parentT>::handleGetProperties( const pcf::IndiProperty &ipRecv )
156 {
157  if(m_parent) m_parent->handleGetProperties(ipRecv);
158 }
159 
160 template<class parentT>
161 void indiDriver<parentT>::handleNewProperty( const pcf::IndiProperty &ipRecv )
162 {
163  if(m_parent) m_parent->handleNewProperty(ipRecv);
164 }
165 
166 template<class parentT>
167 void indiDriver<parentT>::handleSetProperty( const pcf::IndiProperty &ipRecv )
168 {
169  if(m_parent) m_parent->handleSetProperty(ipRecv);
170 }
171 
172 template<class parentT>
174 {
175  processIndiRequests(false);
176 }
177 
178 template<class parentT>
180 {
181  return;
182 }
183 
184 template<class parentT>
185 int indiDriver<parentT>::sendNewProperty( const pcf::IndiProperty &ipRecv )
186 {
187  if( m_outGoing == nullptr)
188  {
189  try
190  {
191  m_outGoing = new pcf::IndiClient(m_serverIPAddress, m_serverPort);
192  }
193  catch(...)
194  {
195  parentT::template log<logger::software_error>({__FILE__, __LINE__, "Exception thrown while creating IndiClient connection"});
196  return -1;
197  }
198 
199  if(m_outGoing == nullptr)
200  {
201  parentT::template log<logger::software_error>({__FILE__, __LINE__, "Failed to allocate IndiClient connection"});
202  return -1;
203  }
204  }
205 
206  try
207  {
208  m_outGoing->sendNewProperty(ipRecv);
209  }
210  catch(...)
211  {
212  parentT::template log<logger::software_error>({__FILE__, __LINE__, "Exception from IndiClient::sendNewProperty"});
213  return -1;
214  }
215 
216  return 0;
217 }
218 
219 } //namespace app
220 } //namespace MagAOX
221 
222 #endif //app_magAOXIndiDriver_hpp
std::string driverInName()
Get the INDI input FIFO file name.
Definition: MagAOXApp.hpp:1776
bool m_good
Flag to hold the status of this connection.
Definition: indiDriver.hpp:49
indiDriver(parentT *parent, const std::string &szName, const std::string &szDriverVersion, const std::string &szProtocolVersion)
Public c&#39;tor.
Definition: indiDriver.hpp:99
virtual void handleDefProperty(const pcf::IndiProperty &ipRecv)
Definition: indiDriver.hpp:149
std::string driverOutName()
Get the INDI output FIFO file name.
Definition: MagAOXApp.hpp:1782
virtual void update()
Define the update virt. func. here so the uptime message isn&#39;t sent.
Definition: indiDriver.hpp:179
virtual void handleGetProperties(const pcf::IndiProperty &ipRecv)
Definition: indiDriver.hpp:155
virtual void handleNewProperty(const pcf::IndiProperty &ipRecv)
Definition: indiDriver.hpp:161
virtual void handleSetProperty(const pcf::IndiProperty &ipRecv)
Definition: indiDriver.hpp:167
std::string m_serverIPAddress
The IP address of the server for the INDI Client connection.
Definition: indiDriver.hpp:41
virtual int sendNewProperty(const pcf::IndiProperty &ipRecv)
Send a newProperty command to another INDI driver.
Definition: indiDriver.hpp:185
int m_serverPort
The port of the server for the INDI Client connection.
Definition: indiDriver.hpp:44
The base-class for MagAO-X applications.
Definition: MagAOXApp.hpp:68
bool good()
Get the value of the good flag.
Definition: indiDriver.hpp:71
~indiDriver()
D&#39;tor, deletes the IndiClient pointer.
Definition: indiDriver.hpp:143
std::string driverCtrlName()
Get the INDI control FIFO file name.
Definition: MagAOXApp.hpp:1788
_parentT parentT
The parent MagAOX app.
Definition: indiDriver.hpp:30
parentT * m_parent
This objects parent class.
Definition: indiDriver.hpp:35
virtual void execute(void)
Define the execute virtual function. This runs the processIndiRequests function in this thread...
Definition: indiDriver.hpp:173
pcf::IndiClient * m_outGoing
An INDI Client is used to send commands to other drivers.
Definition: indiDriver.hpp:38