Blame view

src/ModbusConnections.h 4.99 KB
46785270   Peter M. Groen   Setting up workin...
1
2
3
4
5
6
7
8
9
10
11
  /****************************************************************************
   * Copyright (c) 2022 Priva B.V.
   ****************************************************************************/
  #pragma once
  
  // Flexblox
  #include "IModbusAdapter.h"
  #include "ModbusAdapter.h"
  
  // std
  #include <memory>
b85a3e4a   Peter M. Groen   Setting up workin...
12
  #include <mutex>
46785270   Peter M. Groen   Setting up workin...
13
14
15
  #include <string>
  #include <unordered_map>
  
0df27b07   Peter M. Groen   Added doxygen com...
16
  /// Easy replacement of template construction
b85a3e4a   Peter M. Groen   Setting up workin...
17
18
  using AdapterList = std::vector<std::shared_ptr<IModbusAdapter>>;
  
91724967   Peter M. Groen   Fix whitespace
19
  /*!
0df27b07   Peter M. Groen   Added doxygen com...
20
21
   * \brief The ModbusConnections class conatins a list of all modbus connections, Serial and TCP.
   *        To access a specific connection, use its portname and / or endpoint to return it's interface.
91724967   Peter M. Groen   Fix whitespace
22
   */
46785270   Peter M. Groen   Setting up workin...
23
24
25
26
  class ModbusConnections
  {
  public:
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
27
       * \brief Default CTor
46785270   Peter M. Groen   Setting up workin...
28
29
30
31
       */
      explicit ModbusConnections();
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
32
       * \brief Default DTor
46785270   Peter M. Groen   Setting up workin...
33
34
35
36
       */
      virtual ~ModbusConnections();
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
37
38
39
40
41
       * \brief Create a modbus connection as described by the connection object.
       * \param config    - The connection object describing the connection in detail.
       * \return  The result of the creation.
       *          True    = succesful.
       *          False   = Failed.
46785270   Peter M. Groen   Setting up workin...
42
       */
91724967   Peter M. Groen   Fix whitespace
43
      bool CreateConnection( const ConnectionConfig &config );
46785270   Peter M. Groen   Setting up workin...
44
45
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
46
47
48
49
50
       * \brief Remove the connection described by its portname and/or endpoint.
       * \param portName  - The portname as its enum
       * \param endpoint  - combination of ip-address and portname in endpoint-format
       *                    ( tcp://<ip_address>:<portNumber> )
       *                    ( i.e. tcp://127.0.0.1:501 )
46785270   Peter M. Groen   Setting up workin...
51
52
       * \return
       */
91724967   Peter M. Groen   Fix whitespace
53
      bool DeleteConnection( const ConnectionPort portName, const std::string &endpoint = std::string() );
46785270   Peter M. Groen   Setting up workin...
54
55
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
56
       * \brief Give the number of registered serial and tcp-connections combined.
46785270   Peter M. Groen   Setting up workin...
57
58
59
60
       */
      int ConnectionCount();
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
61
62
63
64
65
66
       * \brief Get the connection give by its parameters.
       * \param   portName    - The portname by its enum
       * \param   endpoint    - combination of ip-address and portname in endpoint-format
       *                          ( tcp://<ip_address>:<portNumber> )
       *                          ( i.e. tcp://127.0.0.1:501 )
       * \return  Valid Pointer to the Modbus Connection Interface. nullptr if the connection wasn't found.
46785270   Peter M. Groen   Setting up workin...
67
68
69
70
71
       */
      std::shared_ptr<IModbusAdapter> getConnection( const ConnectionPort portName, const std::string &endpoint = std::string() );
  
      // Convenient functions
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
72
73
74
75
76
       * \brief Get the connection given by its parameters. If applicable, ipaddress and portnumber will be used to create the endpoint.
       * \param   portName    - The portname by its enum
       * \param   ipAddress   - The ipaddress of the TCP-connection
       * \param   tcpPortNumber   - The portnumber of the TCP-connection
       * \return  Valid Pointer to the Modbus Connection Interface. nullptr if the connection wasn't found.
46785270   Peter M. Groen   Setting up workin...
77
78
79
       */
      std::shared_ptr<IModbusAdapter> getConnection( const ConnectionPort portName, const std::string &ipAddress, int tcpPortNumber );
  
b85a3e4a   Peter M. Groen   Setting up workin...
80
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
81
       * \brief Returns a list of all registered connections by their interfaces. This is a mix of Serial and TCP-connections.
b85a3e4a   Peter M. Groen   Setting up workin...
82
83
84
       */
      AdapterList getConnections();
  
46785270   Peter M. Groen   Setting up workin...
85
86
  private:
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
87
88
89
90
91
       * \brief Check if a connection already exist.
       * \param portName  - The portName by its enum
       * \param endpoint  - The endpoint this connection was registered with.
       * \return  A valid pointer to the Interface if the connection exist. If the connection is unknown, it will return a nullptr.
       *          shared_ptr can manifest themselves as booleans, so "!ptr" is sufficient to check.
46785270   Peter M. Groen   Setting up workin...
92
93
94
95
       */
      std::shared_ptr<IModbusAdapter> connectionExist( const ConnectionPort portName, const std::string &endpoint = std::string() );
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
96
97
98
99
       * \brief Create the TCP endpoint based on the connections IP-address and portnumber
       * \param ipAddress     - The ipAddress as string.
       * \param portNumber    - The portnumber as integer.
       * \return  The endpoint in format : tcp://<ip_address>:<portNumber> i.e. tcp://127.0.0.1:501
46785270   Peter M. Groen   Setting up workin...
100
101
102
103
       */
      std::string createEndPoint( const std::string &ipAddress, int portNumber );
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
104
105
106
       * \brief Create the TCP endpoint based on the connections IP-address and portnumber extracted from the connection configuration.
       * \param config - The configuration object used to create the connection.
       * \return  The endpoint in format : tcp://<ip_address>:<portNumber> i.e. tcp://127.0.0.1:501
46785270   Peter M. Groen   Setting up workin...
107
108
109
110
       */
      std::string createEndPoint( const ConnectionConfig &config );
  
  private:
b85a3e4a   Peter M. Groen   Setting up workin...
111
112
      std::mutex m_mapSerialMutex;                                                        ///< Mutex protecting the serialmap
      std::mutex m_mapTcpMutex;                                                           ///< Mutex protecting the TCPmap
46785270   Peter M. Groen   Setting up workin...
113
114
115
      std::unordered_map<ConnectionPort, std::shared_ptr<IModbusAdapter>> m_mapSerial;    ///< Unordered map holding the Modbus connections By PortName
      std::unordered_map<std::string, std::shared_ptr<IModbusAdapter>>    m_mapTcp;       ///< Unordered map holding the Modbus connections By tcp://endpoint:port
  };