323 static std::shared_ptr<MBSlave>
init_mbslave(
int argc,
const char*
const* argv,
325 const std::string& prefix =
"mbs" );
327 static void help_print(
int argc,
const char*
const* argv );
329 static const int NoSafetyState = -1;
338 std::string amode2str( AccessMode m );
351 ModbusRTU::RegID regID;
356 vtype(VTypes::vtUnknown),
362 friend std::ostream& operator<<( std::ostream& os,
IOProperty& p );
368 typedef std::vector<IOProperty> BitSensorMap;
378 friend std::ostream& operator<<( std::ostream& os,
BitRegProperty& p );
379 friend std::ostream& operator<<( std::ostream& os,
BitRegProperty* p );
382 inline long getConnCount()
387 inline std::shared_ptr<LogAgregator> getLogAggregator()
391 inline std::shared_ptr<DebugStream> log()
398#ifndef DISABLE_REST_API
400 virtual Poco::JSON::Object::Ptr httpHelp(
const Poco::URI::QueryParameters& p )
override;
401 virtual Poco::JSON::Object::Ptr httpRequest(
const std::string& req,
const Poco::URI::QueryParameters& p )
override;
407 ModbusRTU::mbErrCode
readCoilStatus( ModbusRTU::ReadCoilMessage& query,
408 ModbusRTU::ReadCoilRetMessage& reply );
410 ModbusRTU::mbErrCode
readInputStatus( ModbusRTU::ReadInputStatusMessage& query,
411 ModbusRTU::ReadInputStatusRetMessage& reply );
415 ModbusRTU::ReadOutputRetMessage& reply );
419 ModbusRTU::ReadInputRetMessage& reply );
422 ModbusRTU::mbErrCode
forceSingleCoil( ModbusRTU::ForceSingleCoilMessage& query,
423 ModbusRTU::ForceSingleCoilRetMessage& reply );
427 ModbusRTU::ForceCoilsRetMessage& reply );
432 ModbusRTU::WriteOutputRetMessage& reply );
436 ModbusRTU::WriteSingleOutputRetMessage& reply );
443 ModbusRTU::mbErrCode
setDateTime( ModbusRTU::SetDateTimeMessage& query,
444 ModbusRTU::SetDateTimeRetMessage& reply );
447 ModbusRTU::mbErrCode
remoteService( ModbusRTU::RemoteServiceMessage& query,
448 ModbusRTU::RemoteServiceRetMessage& reply );
450 ModbusRTU::mbErrCode fileTransfer( ModbusRTU::FileTransferMessage& query,
451 ModbusRTU::FileTransferRetMessage& reply );
453 ModbusRTU::mbErrCode diagnostics( ModbusRTU::DiagnosticMessage& query,
454 ModbusRTU::DiagnosticRetMessage& reply );
456 ModbusRTU::mbErrCode read4314( ModbusRTU::MEIMessageRDI& query,
457 ModbusRTU::MEIMessageRetRDI& reply );
461 typedef std::map<ModbusRTU::RegID, IOProperty> RegMap;
463 typedef std::unordered_map<ModbusRTU::ModbusAddr, RegMap> IOMap;
469 typedef std::list<IOBase> ThresholdList;
470 ThresholdList thrlist;
472 std::shared_ptr<ModbusServerSlot> mbslot;
473 std::unordered_set<ModbusRTU::ModbusAddr>
vaddr;
474 std::string default_mbaddr = {
"" };
476 xmlNode* cnode = { 0 };
477 std::string s_field = {
"" };
478 std::string s_fvalue = {
"" };
479 int default_mbfunc = {0};
481 std::shared_ptr<SMInterface> shm;
486 void askSensors( UniversalIO::UIOCommand cmd );
488 virtual void execute_rtu();
489 virtual void execute_tcp();
490 virtual void updateStatistics();
491 virtual void updateTCPStatistics();
492 virtual void updateThresholds();
493 virtual void postReceiveEvent( ModbusRTU::mbErrCode res );
500 virtual void finalThread();
507 uniset::timeout_t checkExchangeTime = { 10000 };
509 virtual void initIterators();
510 bool initItem( UniXML::iterator& it );
511 bool readItem(
const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
513 void readConfiguration();
514 bool check_item( UniXML::iterator& it );
516 ModbusRTU::mbErrCode real_write( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData val,
const int fn = 0 );
517 ModbusRTU::mbErrCode real_write( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData* dat,
size_t& i,
size_t count,
const int fn = 0 );
518 ModbusRTU::mbErrCode real_read( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData& val,
const int fn = 0 );
519 ModbusRTU::mbErrCode much_real_read( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData* dat,
size_t count,
const int fn = 0 );
520 ModbusRTU::mbErrCode much_real_write(RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData* dat,
size_t count,
const int fn = 0 );
522 ModbusRTU::mbErrCode real_read_it( RegMap& rmap, RegMap::iterator& it, ModbusRTU::ModbusData& val );
523 ModbusRTU::mbErrCode real_bitreg_read_it( std::shared_ptr<BitRegProperty>& bp, ModbusRTU::ModbusData& val );
524 ModbusRTU::mbErrCode real_read_prop( IOProperty* p, ModbusRTU::ModbusData& val );
526 ModbusRTU::mbErrCode real_write_it(RegMap& rmap, RegMap::iterator& it, ModbusRTU::ModbusData* dat,
size_t& i,
size_t count );
527 ModbusRTU::mbErrCode real_bitreg_write_it( std::shared_ptr<BitRegProperty>& bp,
const ModbusRTU::ModbusData val );
528 ModbusRTU::mbErrCode real_write_prop(IOProperty* p, ModbusRTU::ModbusData* dat,
size_t& i,
size_t count );
530#ifndef DISABLE_REST_API
532 Poco::JSON::Object::Ptr request_regs(
const std::string& req,
const Poco::URI::QueryParameters& p );
533 Poco::JSON::Object::Ptr get_regs(ModbusRTU::ModbusAddr addr,
const RegMap& rmap,
const std::vector<std::string>& q_regs );
534 Poco::JSON::Object::Ptr get_reginfo(
const IOProperty& prop );
537 timeout_t initPause = { 3000 };
539 std::unique_ptr< ThreadCreator<MBSlave> > thr;
541 std::mutex mutexStartNotify;
542 std::condition_variable startNotifyEvent;
544 PassiveTimer ptHeartBeat;
546 long maxHeartBeat = { 10 };
547 IOController::IOStateList::iterator itHeartBeat;
550 IOController::IOStateList::iterator itAskCount;
553 IOController::IOStateList::iterator itRespond;
555 bool respond_invert = {
false };
557 PassiveTimer ptTimeout;
558 long connCount = { 0 };
559 long restartTCPServerCount = { 0 };
561 std::atomic_bool activated = {
false };
562 std::atomic_bool canceled = {
false };
563 timeout_t activateTimeout = { 20000 };
564 bool smPingOK = {
false };
565 timeout_t wait_msec = { 3000 };
568 bool mbregFromID = {0};
569 bool checkMBFunc = {0};
570 bool noMBFuncOptimize = {0};
572 int getOptimizeWriteFunction(
const int fn );
574 typedef std::unordered_map<int, std::string> FileList;
576 std::string prefix = {
"" };
577 std::string prop_prefix = {
"" };
579 ModbusRTU::ModbusData
buf[ModbusRTU::MAXLENPACKET / 2 + 1];
584 typedef std::unordered_map<int, std::string> MEIValMap;
585 typedef std::unordered_map<int, MEIValMap> MEIObjIDMap;
586 typedef std::unordered_map<int, MEIObjIDMap> MEIDevIDMap;
590 std::shared_ptr<LogAgregator> loga;
591 std::shared_ptr<DebugStream> mblog;
592 std::shared_ptr<LogServer> logserv;
593 std::string logserv_host = {
""};
594 int logserv_port = {0};
596 std::string mbtype = {
"" };
600 void initTCPClients( UniXML::iterator confnode );
604 timeout_t updateStatTime = { 4000 };
605 ModbusTCPServer::Sessions
sess;
606 std::mutex sessMutex;
607 size_t sessMaxNum = { 5 };
608 std::shared_ptr<ModbusTCPServerSlot> tcpserver;
615 ptTimeout.setTiming(0);
618 std::string iaddr = {
"" };
621 IOController::IOStateList::iterator respond_it;
622 bool invert = {
false };
624 timeout_t tout = { 2000 };
626 long askCount = { 0 };
628 IOController::IOStateList::iterator askcount_it;
630 inline void initIterators(
const std::shared_ptr<SMInterface>& shm )
632 shm->initIterator( respond_it );
633 shm->initIterator( askcount_it );
636 const std::string getShortInfo()
const;
639 typedef std::unordered_map<std::string, ClientInfo> ClientsMap;
643 IOController::IOStateList::iterator sesscount_it;
645 std::atomic_bool tcpCancelled = {
true };
647 bool tcpBreakIfFailRun = {
false };
648 timeout_t tcpRepeatCreateSocketPause = { 30000 };