supla-device
Loading...
Searching...
No Matches
modbus_client_handler.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_MODBUS_MODBUS_CLIENT_HANDLER_H_
20#define SRC_SUPLA_MODBUS_MODBUS_CLIENT_HANDLER_H_
21
22#include <stdint.h>
23#include <stddef.h>
24
25namespace Supla {
26
27namespace Modbus {
28 enum class Result {
29 OK,
30 INVALID_STATE,
31 INVALID_REGISTER_ADDRESS,
32 };
33
34 enum class Access{
35 READ,
36 WRITE,
37 };
38} // namespace Modbus
39
40class ModbusClientHandler {
41 public:
42 ModbusClientHandler();
43
44 virtual ~ModbusClientHandler();
45 // RW holding register
46 static Supla::Modbus::Result HoldingProcessRequest(
47 uint16_t address,
48 uint16_t nRegs,
49 uint8_t *regBuffer,
50 Supla::Modbus::Access access);
51 // RO input register
52 static Supla::Modbus::Result InputProcessRequest(uint16_t address,
53 uint16_t nRegs,
54 uint8_t *regBuffer);
55 // RW coils
56 static Supla::Modbus::Result CoilsProcessRequest(
57 uint16_t address,
58 uint16_t nRegs,
59 uint8_t *regBuffer,
60 Supla::Modbus::Access access);
61 // RO discrete register
62 static Supla::Modbus::Result DiscreteProcessRequest(uint16_t address,
63 uint16_t nRegs,
64 uint8_t *regBuffer);
65
66 static bool IsInputSupported();
67 static bool IsDiscreteSupported();
68 static bool IsCoilsSupported();
69 static bool IsHoldingSupported();
70
71 virtual bool isInputSupported();
72 virtual bool isDiscreteSupported();
73 virtual bool isCoilsSupported();
74 virtual bool isHoldingSupported();
75
76 virtual bool holdingRespondsToAddress(uint16_t address, uint16_t nRegs);
77 virtual Supla::Modbus::Result holdingProcessRequest(
78 uint16_t address,
79 uint16_t nRegs,
80 uint8_t *regBuffer,
81 Supla::Modbus::Access access);
82
83 virtual bool inputRespondsToAddress(uint16_t address, uint16_t nRegs);
84 virtual Supla::Modbus::Result inputProcessRequest(uint16_t address,
85 uint16_t nRegs,
86 uint8_t *regBuffer);
87
88 virtual bool coilsRespondsToAddress(uint16_t address, uint16_t nRegs);
89 virtual Supla::Modbus::Result coilsProcessRequest(
90 uint16_t address,
91 uint16_t nRegs,
92 uint8_t *regBuffer,
93 Supla::Modbus::Access access);
94
95 virtual bool discreteRespondsToAddress(uint16_t address, uint16_t nRegs);
96 virtual Supla::Modbus::Result discreteProcessRequest(uint16_t address,
97 uint16_t nRegs,
98 uint8_t *regBuffer);
99
100 protected:
101 void storeBigEndian(uint64_t value,
102 uint8_t *regBuffer,
103 uint8_t registerOffset,
104 uint8_t registerCount);
105 uint16_t modbusAddressOffset = 0;
106 uint16_t usedRegistersCount = 0;
107
108 private:
109 static ModbusClientHandler *GetHoldingHandler(uint16_t address,
110 uint16_t nRegs);
111 static ModbusClientHandler *GetInputHandler(uint16_t address,
112 uint16_t nRegs);
113 static ModbusClientHandler *GetDiscreteHandler(uint16_t address,
114 uint16_t nRegs);
115 static ModbusClientHandler *GetCoilsHandler(uint16_t address,
116 uint16_t nRegs);
117 static ModbusClientHandler *first;
118 ModbusClientHandler *next = nullptr;
119};
120
121} // namespace Supla
122#endif // SRC_SUPLA_MODBUS_MODBUS_CLIENT_HANDLER_H_