24 #include <unordered_map>
26 #include <condition_variable>
29 #include "UniSetObject.h"
30 #include "modbus/ModbusTypes.h"
31 #include "modbus/ModbusServerSlot.h"
32 #include "modbus/ModbusTCPServer.h"
33 #include "modbus/ModbusTCPServerSlot.h"
34 #include "PassiveTimer.h"
37 #include "SMInterface.h"
38 #include "SharedMemory.h"
41 #include "ThreadCreator.h"
42 #include "LogServer.h"
43 #include "LogAgregator.h"
47 #define vmonit( var ) vmon.add( #var, var )
317 static std::shared_ptr<MBSlave>
init_mbslave(
int argc,
const char*
const* argv,
319 const std::string& prefix =
"mbs" );
322 static void help_print(
int argc,
const char*
const* argv );
324 static const int NoSafetyState = -1;
344 ModbusRTU::RegID regID;
349 vtype(VTypes::vtUnknown),
355 friend std::ostream& operator<<( std::ostream& os,
IOProperty& p );
361 typedef std::vector<IOProperty> BitSensorMap;
371 friend std::ostream& operator<<( std::ostream& os,
BitRegProperty& p );
372 friend std::ostream& operator<<( std::ostream& os,
BitRegProperty* p );
375 inline long getAskCount()
380 inline std::shared_ptr<LogAgregator> getLogAggregator()
384 inline std::shared_ptr<DebugStream> log()
394 ModbusRTU::mbErrCode
readCoilStatus( ModbusRTU::ReadCoilMessage& query,
395 ModbusRTU::ReadCoilRetMessage& reply );
397 ModbusRTU::mbErrCode
readInputStatus( ModbusRTU::ReadInputStatusMessage& query,
398 ModbusRTU::ReadInputStatusRetMessage& reply );
402 ModbusRTU::ReadOutputRetMessage& reply );
406 ModbusRTU::ReadInputRetMessage& reply );
409 ModbusRTU::mbErrCode
forceSingleCoil( ModbusRTU::ForceSingleCoilMessage& query,
410 ModbusRTU::ForceSingleCoilRetMessage& reply );
414 ModbusRTU::ForceCoilsRetMessage& reply );
419 ModbusRTU::WriteOutputRetMessage& reply );
423 ModbusRTU::WriteSingleOutputRetMessage& reply );
430 ModbusRTU::mbErrCode
setDateTime( ModbusRTU::SetDateTimeMessage& query,
431 ModbusRTU::SetDateTimeRetMessage& reply );
434 ModbusRTU::mbErrCode
remoteService( ModbusRTU::RemoteServiceMessage& query,
435 ModbusRTU::RemoteServiceRetMessage& reply );
437 ModbusRTU::mbErrCode fileTransfer( ModbusRTU::FileTransferMessage& query,
438 ModbusRTU::FileTransferRetMessage& reply );
440 ModbusRTU::mbErrCode diagnostics( ModbusRTU::DiagnosticMessage& query,
441 ModbusRTU::DiagnosticRetMessage& reply );
443 ModbusRTU::mbErrCode read4314( ModbusRTU::MEIMessageRDI& query,
444 ModbusRTU::MEIMessageRetRDI& reply );
449 virtual ModbusRTU::mbErrCode
checkRegister( ModbusRTU::ModbusData reg, ModbusRTU::ModbusData& val )
451 return ModbusRTU::erNoError;
456 typedef std::map<ModbusRTU::RegID, IOProperty> RegMap;
458 typedef std::unordered_map<ModbusRTU::ModbusAddr, RegMap> IOMap;
464 typedef std::list<IOBase> ThresholdList;
465 ThresholdList thrlist;
467 std::shared_ptr<ModbusServerSlot> mbslot;
468 std::unordered_set<ModbusRTU::ModbusAddr>
vaddr;
469 std::string default_mbaddr = {
"" };
471 xmlNode* cnode = { 0 };
472 std::string s_field = {
"" };
473 std::string s_fvalue = {
"" };
474 int default_mbfunc = {0};
476 std::shared_ptr<SMInterface> shm;
480 void askSensors( UniversalIO::UIOCommand cmd );
482 virtual void execute_rtu();
483 virtual void execute_tcp();
484 virtual void updateStatistics();
485 virtual void updateTCPStatistics();
486 virtual void updateThresholds();
487 virtual void postReceiveEvent( ModbusRTU::mbErrCode res );
493 virtual void sigterm(
int signo )
override;
494 virtual void finalThread();
496 virtual void initIterators();
497 bool initItem( UniXML::iterator& it );
498 bool readItem(
const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
500 void readConfiguration();
501 bool check_item( UniXML::iterator& it );
503 ModbusRTU::mbErrCode real_write( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData val,
const int fn = 0 );
504 ModbusRTU::mbErrCode real_write( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData* dat,
size_t& i,
size_t count,
const int fn = 0 );
505 ModbusRTU::mbErrCode real_read( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData& val,
const int fn = 0 );
506 ModbusRTU::mbErrCode much_real_read( RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData* dat,
size_t count,
const int fn = 0 );
507 ModbusRTU::mbErrCode much_real_write(RegMap& rmap,
const ModbusRTU::ModbusData regOKOK, ModbusRTU::ModbusData* dat,
size_t count,
const int fn = 0 );
509 ModbusRTU::mbErrCode real_read_it( RegMap& rmap, RegMap::iterator& it, ModbusRTU::ModbusData& val );
510 ModbusRTU::mbErrCode real_bitreg_read_it( std::shared_ptr<BitRegProperty>& bp, ModbusRTU::ModbusData& val );
511 ModbusRTU::mbErrCode real_read_prop( IOProperty* p, ModbusRTU::ModbusData& val );
513 ModbusRTU::mbErrCode real_write_it(RegMap& rmap, RegMap::iterator& it, ModbusRTU::ModbusData* dat,
size_t& i,
size_t count );
514 ModbusRTU::mbErrCode real_bitreg_write_it( std::shared_ptr<BitRegProperty>& bp,
const ModbusRTU::ModbusData val );
515 ModbusRTU::mbErrCode real_write_prop(IOProperty* p, ModbusRTU::ModbusData* dat,
size_t& i,
size_t count );
518 timeout_t initPause = { 3000 };
520 std::shared_ptr< ThreadCreator<MBSlave> > thr;
522 std::mutex mutexStartNotify;
523 std::condition_variable startNotifyEvent;
525 PassiveTimer ptHeartBeat;
527 long maxHeartBeat = { 10 };
528 IOController::IOStateList::iterator itHeartBeat;
531 IOController::IOStateList::iterator itAskCount;
534 IOController::IOStateList::iterator itRespond;
536 bool respond_invert = {
false };
538 PassiveTimer ptTimeout;
539 long askCount = { 0 };
541 std::atomic_bool activated = {
false };
542 std::atomic_bool cancelled = {
false };
543 timeout_t activateTimeout = { 20000 };
544 bool pingOK = {
false };
545 timeout_t wait_msec = { 3000 };
548 bool mbregFromID = {0};
549 bool checkMBFunc = {0};
550 bool noMBFuncOptimize = {0};
552 int getOptimizeWriteFunction(
const int fn );
554 typedef std::unordered_map<int, std::string> FileList;
556 std::string prefix = {
"" };
557 std::string prop_prefix = {
"" };
559 ModbusRTU::ModbusData
buf[ModbusRTU::MAXLENPACKET / 2 + 1];
564 typedef std::unordered_map<int, std::string> MEIValMap;
565 typedef std::unordered_map<int, MEIValMap> MEIObjIDMap;
566 typedef std::unordered_map<int, MEIObjIDMap> MEIDevIDMap;
570 std::shared_ptr<LogAgregator> loga;
571 std::shared_ptr<DebugStream> mblog;
572 std::shared_ptr<LogServer> logserv;
573 std::string logserv_host = {
""};
574 int logserv_port = {0};
576 std::string mbtype = {
"" };
580 void initTCPClients( UniXML::iterator confnode );
583 timeout_t updateStatTime = { 4000 };
584 ModbusTCPServer::Sessions
sess;
585 std::mutex sessMutex;
586 size_t sessMaxNum = { 5 };
587 std::shared_ptr<ModbusTCPServerSlot> tcpserver;
597 std::string iaddr = {
"" };
600 IOController::IOStateList::iterator respond_it;
601 bool invert = {
false };
603 timeout_t tout = { 2000 };
605 long askCount = { 0 };
607 IOController::IOStateList::iterator askcount_it;
609 inline void initIterators(
const std::shared_ptr<SMInterface>& shm )
611 shm->initIterator( respond_it );
612 shm->initIterator( askcount_it );
615 const std::string getShortInfo()
const;
618 typedef std::unordered_map<std::string, ClientInfo> ClientsMap;
622 IOController::IOStateList::iterator sesscount_it;
624 std::atomic_bool tcpCancelled = {
true };
629 #endif // _MBSlave_H_