UniSet  2.6.0
IOControl.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // -----------------------------------------------------------------------------
17 #ifndef IOControl_H_
18 #define IOControl_H_
19 // -----------------------------------------------------------------------------
20 #include <vector>
21 #include <memory>
22 #include <deque>
23 #include <string>
24 #include "UniXML.h"
25 #include "PassiveTimer.h"
26 #include "Trigger.h"
27 #include "IONotifyController.h"
28 #include "UniSetObject.h"
29 #include "Mutex.h"
30 #include "MessageType.h"
31 #include "ComediInterface.h"
32 #include "DigitalFilter.h"
33 #include "Calibration.h"
34 #include "SMInterface.h"
35 #include "SingleProcess.h"
36 #include "IOController.h"
37 #include "IOBase.h"
38 #include "SharedMemory.h"
39 #include "LogServer.h"
40 #include "DebugStream.h"
41 #include "LogAgregator.h"
42 // -------------------------------------------------------------------------
43 namespace uniset
44 {
45  // -----------------------------------------------------------------------------
184  // -----------------------------------------------------------------------------
187  class CardList:
188  public std::vector<ComediInterface*>
189  {
190  public:
191 
192  explicit CardList( size_t size ) : std::vector<ComediInterface * >(size) { }
193 
194  ~CardList()
195  {
196  for( size_t i = 0; i < size(); i++ )
197  delete (*this)[i];
198  }
199 
200  inline ComediInterface* getCard(int ncard) const
201  {
202  if( ncard >= 0 && ncard < (int)size() )
203  return (*this)[ncard];
204 
205  return NULL;
206  }
207 
208  };
209 
225  class IOControl:
226  public UniSetObject
227  {
228  public:
229  IOControl( uniset::ObjectId id, uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& shm = nullptr, int numcards = 2, const std::string& prefix = "io" );
230  virtual ~IOControl();
231 
233  static std::shared_ptr<IOControl> init_iocontrol( int argc, const char* const* argv,
234  uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& ic = nullptr,
235  const std::string& prefix = "io" );
237  static void help_print( int argc, const char* const* argv );
238 
240  struct IOInfo:
241  public IOBase
242  {
243  // т.к. IOBase содержит rwmutex с запрещённым конструктором копирования
244  // приходится здесь тоже объявлять разрешенными только операции "перемещения"
245  IOInfo( const IOInfo& r ) = delete;
246  IOInfo& operator=(const IOInfo& r) = delete;
247  IOInfo( IOInfo&& r ) = default;
248  IOInfo& operator=(IOInfo&& r) = default;
249 
250  IOInfo():
251  subdev(DefaultSubdev), channel(DefaultChannel),
252  ncard(-1),
253  aref(0),
254  range(0),
255  lamp(false),
256  no_testlamp(false),
257  enable_testmode(false),
258  disable_testmode(false)
259  {}
260 
261 
262  int subdev;
263  int channel;
264  int ncard;
272  int aref;
273 
280  int range;
281 
282  bool lamp;
283  bool no_testlamp;
287  friend std::ostream& operator<<(std::ostream& os, IOInfo& inf );
288  };
289 
290  struct IOPriority
291  {
292  IOPriority(size_t p, size_t i):
293  priority(p), index(i) {}
294 
295  size_t priority;
296  size_t index;
297  };
298 
300  {
301  tmNone = 0,
302  tmOffPoll = 1,
307  };
308 
309  void execute();
310 
311  protected:
312 
313  void iopoll();
314  void ioread( IOInfo* it );
315  void check_testlamp();
316  void check_testmode();
317  void blink();
318 
319  // действия при завершении работы
320  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
321  virtual void askSensors( UniversalIO::UIOCommand cmd );
322  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
323  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
324  virtual void sigterm( int signo ) override;
325  virtual bool activateObject() override;
326 
327  // начальная инициализация выходов
328  void initOutputs();
329 
330  // инициализация карты (каналов в/в)
331  void initIOCard();
332 
333  // чтение файла конфигурации
334  void readConfiguration();
335  bool initIOItem( UniXML::iterator& it );
336  bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
337  void buildCardsList();
338 
339  void waitSM();
340 
341  xmlNode* confnode = { 0 };
343  int polltime = { 150 };
345  bool noCards = { false };
346 
347  typedef std::vector<IOInfo> IOMap;
348  IOMap iomap;
350  typedef std::deque<IOPriority> PIOMap;
351  PIOMap pmap;
353  unsigned int maxItem = { 0 };
354  unsigned int maxHalf = { 0 };
355  int filtersize = { 0 };
356  float filterT = { 0.0 };
357 
358  std::string s_field;
359  std::string s_fvalue;
360 
361  std::shared_ptr<SMInterface> shm;
363  std::string prefix;
364 
365  typedef std::list<IOInfo*> BlinkList;
366 
367  void addBlink( IOInfo* it, BlinkList& lst );
368  void delBlink( IOInfo* it, BlinkList& lst );
369  void blink( BlinkList& lst, bool& bstate );
370 
371  // обычное мигание
372  BlinkList lstBlink;
373  PassiveTimer ptBlink;
374  bool blink_state = { false };
375 
376  // мигание с двойной частотой
377  BlinkList lstBlink2;
378  PassiveTimer ptBlink2;
379  bool blink2_state = { false };
380 
381  // мигание с тройной частотой
382  BlinkList lstBlink3;
383  PassiveTimer ptBlink3;
384  bool blink3_state = { false };
385 
386  uniset::ObjectId testLamp_s = { uniset::DefaultObjectId };
387  Trigger trTestLamp;
388  bool isTestLamp = { false };
389  IOController::IOStateList::iterator itTestLamp;
390 
391  PassiveTimer ptHeartBeat;
392  uniset::ObjectId sidHeartBeat;
393  int maxHeartBeat = { 10 };
394  IOController::IOStateList::iterator itHeartBeat;
395 
396  bool force = { false };
397  bool force_out = { false };
398  timeout_t smReadyTimeout = { 15000 };
399  int defCardNum = { -1 };
400  int maxCardNum = { 10 };
402  std::mutex iopollMutex;
403  std::atomic_bool activated = { false };
404  bool readconf_ok = { false };
405  int activateTimeout;
406  uniset::ObjectId sidTestSMReady = { uniset::DefaultObjectId };
407  std::atomic_bool term = { false };
408 
409  uniset::ObjectId testMode_as = { uniset::DefaultObjectId };
410  IOController::IOStateList::iterator itTestMode;
411  long testmode = { false };
412  long prev_testmode = { false };
413 
414  std::shared_ptr<LogAgregator> loga;
415  std::shared_ptr<DebugStream> iolog;
416  std::shared_ptr<LogServer> logserv;
417  std::string logserv_host = {""};
418  int logserv_port = {0};
419 
420  private:
421  };
422  // --------------------------------------------------------------------------
423 } // end of namespace uniset
424 // -----------------------------------------------------------------------------
425 #endif // IOControl_H_
426 // -----------------------------------------------------------------------------