libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Hgdc.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef LIBHOMEGEAR_BASE_HGDC_H
32 #define LIBHOMEGEAR_BASE_HGDC_H
33 
34 #include "../Systems/Packet.h"
35 #include "TcpSocket.h"
36 #include "../Encoding/BinaryRpc.h"
37 #include "../Encoding/RpcEncoder.h"
38 #include "../Encoding/RpcDecoder.h"
39 #include "../Output/Output.h"
40 #include "../IQueue.h"
41 
42 #include <condition_variable>
43 
44 namespace BaseLib {
45 
46 class Hgdc : public IQueue {
47  private:
48  struct RequestInfo {
49  std::mutex waitMutex;
50  std::condition_variable conditionVariable;
51  };
52  typedef std::shared_ptr<RequestInfo> PRequestInfo;
53 
54  struct RpcResponse {
55  std::atomic_bool finished{false};
56  int32_t packetId = 0;
57  BaseLib::PVariable response;
58  };
59 
60  typedef std::shared_ptr<RpcResponse> PRpcResponse;
61 
62  class QueueEntry : public BaseLib::IQueueEntry {
63  public:
64  std::string method;
65  BaseLib::PArray parameters;
66  };
67 
68  SharedObjects *_bl = nullptr;
69  uint16_t _port = 0;
70 
71  Output _out;
72  std::unique_ptr<TcpSocket> _tcpSocket;
73  std::unique_ptr<Rpc::BinaryRpc> _binaryRpc;
74  std::unique_ptr<Rpc::RpcEncoder> _rpcEncoder;
75  std::unique_ptr<Rpc::RpcDecoder> _rpcDecoder;
76 
77  std::atomic_bool _stopped{true};
78  std::atomic_bool _stopCallbackThread{true};
79  std::thread _listenThread;
80 
81  int32_t _currentEventHandlerId = 0;
82  std::mutex _packetReceivedEventHandlersMutex;
83  std::unordered_map<int64_t, std::list<std::pair<int32_t, std::function<void(int64_t, const std::string &, const std::vector<uint8_t> &)>>>> _packetReceivedEventHandlers;
84  std::mutex _moduleUpdateEventHandlersMutex;
85  std::unordered_map<int32_t, std::function<void(const BaseLib::PVariable &)>> _moduleUpdateEventHandlers;
86  std::mutex _reconnectedEventHandlersMutex;
87  std::unordered_map<int32_t, std::function<void()>> _reconnectedEventHandlers;
88 
89  // {{{ Needed for "invoke()"
90  std::mutex _requestInfoMutex;
91  std::map<pthread_t, PRequestInfo> _requestInfo;
92  std::mutex _packetIdMutex;
93  int32_t _currentPacketId = 0;
94  std::mutex _rpcResponsesMutex;
95  std::unordered_map<pthread_t, std::unordered_map<int32_t, PRpcResponse>> _rpcResponses;
96  // }}}
97 
98  void listen();
99  void processQueueEntry(int32_t index, std::shared_ptr<BaseLib::IQueueEntry> &entry) override;
100  public:
101  explicit Hgdc(SharedObjects *bl, uint16_t port);
102  ~Hgdc();
103 
104  void start();
105  void stop();
106 
107  PVariable invoke(const std::string &methodName, const PArray &parameters, int32_t timeout = 10000);
108 
109  int32_t registerPacketReceivedEventHandler(int64_t familyId, std::function<void(int64_t, const std::string &, const std::vector<uint8_t> &)> value);
110  void unregisterPacketReceivedEventHandler(int32_t eventHandlerId);
111  int32_t registerModuleUpdateEventHandler(std::function<void(const BaseLib::PVariable &)> value);
112  void unregisterModuleUpdateEventHandler(int32_t eventHandlerId);
113  int32_t registerReconnectedEventHandler(std::function<void()> value);
114  void unregisterReconnectedEventHandler(int32_t eventHandlerId);
115 
116  bool isMaster();
117  PVariable getModules(int64_t familyId = -1);
118  bool sendPacket(const std::string &serialNumber, const std::vector<uint8_t> &packet);
119  bool sendPacket(const std::string &serialNumber, const std::vector<char> &packet);
120  bool moduleReset(const std::string &serialNumber);
121  bool setMode(const std::string &serialNumber, uint8_t mode);
122 };
123 
124 }
125 
126 #endif //LIBHOMEGEAR_BASE_HGDC_H
bool sendPacket(const std::string &serialNumber, const std::vector< uint8_t > &packet)
Definition: Hgdc.cpp:378
This is the base library main class.
Definition: BaseLib.h:95
Definition: IQueue.h:41
void stop()
Definition: Hgdc.cpp:91
Definition: BaseLib.cpp:34
std::shared_ptr< Array > PArray
Definition: Variable.h:72
void unregisterReconnectedEventHandler(int32_t eventHandlerId)
Definition: Hgdc.cpp:184
Class to print output of different kinds to the standard and error output.
Definition: Output.h:54
void unregisterModuleUpdateEventHandler(int32_t eventHandlerId)
Definition: Hgdc.cpp:156
void start()
Definition: Hgdc.cpp:52
PVariable invoke(const std::string &methodName, const PArray &parameters, int32_t timeout=10000)
Definition: Hgdc.cpp:299
int32_t registerPacketReceivedEventHandler(int64_t familyId, std::function< void(int64_t, const std::string &, const std::vector< uint8_t > &)> value)
Definition: Hgdc.cpp:105
PVariable value
Definition: UiElements.h:217
std::shared_ptr< Variable > PVariable
Definition: PhysicalInterfaceSettings.h:41
bool isMaster()
Definition: Hgdc.cpp:468
void unregisterPacketReceivedEventHandler(int32_t eventHandlerId)
Definition: Hgdc.cpp:121
This class implements a queue after the producer-consumer paradigm.
Definition: IQueue.h:52
Hgdc(SharedObjects *bl, uint16_t port)
Definition: Hgdc.cpp:36
int32_t registerModuleUpdateEventHandler(std::function< void(const BaseLib::PVariable &)> value)
Definition: Hgdc.cpp:140
int32_t familyId
Definition: Licensing.h:380
int32_t registerReconnectedEventHandler(std::function< void()> value)
Definition: Hgdc.cpp:168
bool setMode(const std::string &serialNumber, uint8_t mode)
Definition: Hgdc.cpp:445
Definition: Hgdc.h:46
PVariable getModules(int64_t familyId=-1)
Definition: Hgdc.cpp:482
~Hgdc()
Definition: Hgdc.cpp:48
bool moduleReset(const std::string &serialNumber)
Definition: Hgdc.cpp:424