supla-device
Loading...
Searching...
No Matches
supla_srpc.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_PROTOCOL_SUPLA_SRPC_H_
5#define SRC_SUPLA_PROTOCOL_SUPLA_SRPC_H_
6
7#include <stddef.h>
8#include <supla-common/proto.h>
9
10#include "protocol_layer.h"
11
12#define SUPLA_SRPC_CALCFG_RESULT_DONT_REPLY (-1)
13#define SUPLA_SRPC_CALCFG_RESULT_PENDING (-2)
14
15namespace Supla {
16
17class Client;
18
19namespace Device {
20class ChannelConflictResolver;
21class RemoteDeviceConfig;
22} // namespace Device
23
24namespace Protocol {
25
26struct CalCfgResultPendingItem;
27
29 public:
30 friend class SuplaSrpc;
31 void set(int16_t channelNo,
32 int32_t receiverId,
33 int32_t command,
34 uint32_t timeoutMs = 0);
35 void clear(int16_t channelNo, int32_t command = -1);
36 void clearTimeout(int16_t channelNo, int32_t command = -1);
37 void clearAll();
38 bool empty() const;
39 CalCfgResultPendingItem *get(int16_t channelNo, int32_t command = -1);
40
41 protected:
44 CalCfgResultPendingItem *first = nullptr;
45};
46
47class SuplaSrpc : public ProtocolLayer {
48 public:
49 static bool isSuplaSSLEnabled;
50
51 explicit SuplaSrpc(SuplaDeviceClass *sdc, int version = 23);
52 ~SuplaSrpc();
53
54 void setNetworkClient(Supla::Client *newClient);
55
56 void onInit() override;
57 bool onLoadConfig() override;
58 bool verifyConfig() override;
59 bool isEnabled() override;
60 void disconnect() override;
61 bool iterate(uint32_t _millis) override;
62 bool isNetworkRestartRequested() override;
63 uint32_t getConnectionFailTime() override;
64 ConnectionError getConnectionError() const override;
65 bool isRegisteredAndReady() override;
66 void initClient();
67
68 void sendActionTrigger(uint8_t channelNumber, uint32_t actionId) override;
69 void sendRegisterNotification(
70 TDS_RegisterPushNotification *notification) override;
71 bool sendNotification(int context,
72 const char *title,
73 const char *message,
74 int soundId) override;
75 void sendSubdeviceDetails(TDS_SubdeviceDetails *subdeviceDetails) override;
76 void getUserLocaltime() override;
77 void sendChannelValueChanged(uint8_t channelNumber,
78 int8_t *value,
79 uint8_t offline,
80 uint32_t validityTimeSec) override;
81 void sendExtendedChannelValueChanged(
82 uint8_t channelNumber, TSuplaChannelExtendedValue *value) override;
83
84 void getChannelConfig(uint8_t channelNumber, uint8_t configType) override;
85 bool setChannelConfig(uint8_t channelNumber,
86 _supla_int_t channelFunction,
87 void *channelConfig,
88 int size,
89 uint8_t configType) override;
90
91 bool setDeviceConfig(TSDS_SetDeviceConfig *deviceConfig) override;
92 bool setInitialCaption(uint8_t channelNumber, const char *caption) override;
93 void sendRemainingTimeValue(uint8_t channelNumber,
94 uint32_t timeMs,
95 uint8_t state,
96 int32_t senderId) override;
97 void sendRemainingTimeValue(uint8_t channelNumber,
98 uint32_t remainingTime,
99 unsigned char state[SUPLA_CHANNELVALUE_SIZE],
100 int32_t senderId,
101 bool useSecondsInsteadOfMs) override;
102
103 void onVersionError(TSDC_SuplaVersionError *versionError);
104 void onRegisterResult(TSD_SuplaRegisterDeviceResult *register_device_result);
105 void onRegisterResultB(
106 TSD_SuplaRegisterDeviceResult_B *registerDeviceResultB);
107 void onDeviceSyncDone();
108 void onSetActivityTimeoutResult(TSDC_SuplaSetActivityTimeoutResult *result);
109 void setActivityTimeout(uint32_t activityTimeoutSec);
110 uint32_t getActivityTimeout();
111 void updateLastResponseTime();
112 void updateLastSentTime();
113 void onGetUserLocaltimeResult(TSDC_UserLocalTimeResult *result);
114 void sendChannelStateResult(int32_t receiverId, uint8_t channelNo) override;
115
116 void setServerPort(int value);
117 void setVersion(int value);
118 bool hasWriteFailure() const;
119 void markWriteFailure();
120 void setSuplaCACert(const char *);
121 void setSupla3rdPartyCACert(const char *);
122 const char *getSuplaCACert() const;
123 const char *getSupla3rdPartyCACert() const;
124 void setLowLevelDebugLogs(bool value);
125 bool isUpdatePending() override;
126 void handleDeviceConfig(TSDS_SetDeviceConfig *deviceConfig);
127 void handleSetDeviceConfigResult(TSDS_SetDeviceConfigResult *result);
128
129 Supla::Client *client = nullptr;
130 CalCfgResultPending calCfgResultPending;
131 void sendPendingCalCfgResult(int16_t channelNo,
132 int32_t result,
133 int32_t command,
134 int dataSize = 0,
135 void *data = nullptr);
136 void sendPendingCalCfgResultForCommand(int16_t channelNo,
137 int32_t result,
138 int32_t pendingCommand,
139 int32_t responseCommand,
140 int dataSize = 0,
141 void *data = nullptr);
142 void sendCalCfgResult(int32_t receiverId,
143 int16_t channelNo,
144 int32_t result,
145 int32_t command,
146 int dataSize = 0,
147 void *data = nullptr);
148
149 void clearPendingCalCfgResult(int16_t channelNo, int32_t command = -1);
150 void clearPendingCalCfgTimeout(int16_t channelNo, int32_t command = -1);
151
152 static const char *configResultToCStr(int result);
153
154 void setChannelConflictResolver(
156
157#if SUPLA_SRPC_PACKET_LOG_ENABLED && !defined(SUPLA_DISABLE_LOGS)
158 static void onPacketSent(void *srpcHandle,
159 unsigned _supla_int_t callId,
160 void *data,
161 unsigned _supla_int_t dataSize,
162 void *userParam);
163 static void onPacketReceived(void *srpcHandle,
164 unsigned _supla_int_t callId,
165 void *data,
166 unsigned _supla_int_t dataSize,
167 void *userParam);
168 void logSrpcPacket(bool send, int callId, const uint8_t *buf, size_t size);
169 static const char *callIdToName(int callId);
170 static bool isSensitiveCallId(int callId);
171#endif
172
173 protected:
174 bool ping();
175 void scheduleReconnect(uint32_t now);
176 void initializeSrpc();
177 void deinitializeSrpc();
178 void addLastStateAdError(char *buf);
179
180 uint8_t version = 0;
181 uint8_t activityTimeoutS = 30;
182 uint8_t securityLevel = 0;
183 int8_t registered = 0;
184 bool requestNetworkRestart = false;
185 bool enabled = true;
186 bool setDeviceConfigReceivedAfterRegistration = false;
187 bool deviceSyncDoneReceived = false;
188 bool firstConnectionAttempt = true;
189 bool adErrorLogged = false;
190 bool writeFailure = false;
191 uint8_t autodiscoverRetryCounter = 0;
192 uint16_t connectionFailCounter = 0;
193 uint8_t reconnectAttemptCounter = 0;
194
195 uint32_t lastPingTimeMs = 0;
196 uint32_t waitForIterate = 0;
197 uint32_t lastIterateTime = 0;
198 uint32_t lastResponseMs = 0;
199 uint32_t lastSentMs = 0;
200
201 int port = -1;
202
203 const char *suplaCACert = nullptr;
204 const char *supla3rdPartyCACert = nullptr;
205 const char *selectedCertificate = nullptr;
206 void *srpc = nullptr;
207 Supla::Device::ChannelConflictResolver *channelConflictResolver = nullptr;
208
209 private:
210 void handlePendingCalCfgTimeouts(uint32_t _millis);
211 Supla::Device::RemoteDeviceConfig *remoteDeviceConfig = nullptr;
212 bool versionErrorDisconnectPending = false;
213};
214} // namespace Protocol
215
216// Method passed to SRPC as a callback to read raw data from network interface
217_supla_int_t dataRead(void *buf, _supla_int_t count, void *sdc);
218// Method passed to SRPC as a callback to write raw data to network interface
219_supla_int_t dataWrite(void *buf, _supla_int_t count, void *sdc);
220// Method passed to SRPC as a callback to handle response from Supla server
221void messageReceived(void *srpc,
222 unsigned _supla_int_t rrId,
223 unsigned _supla_int_t callType,
224 void *userParam,
225 unsigned char protoVersion);
226
227} // namespace Supla
228
229#endif // SRC_SUPLA_PROTOCOL_SUPLA_SRPC_H_
Definition SuplaDevice.h:167
Definition client.h:24
Definition channel_conflict_resolver.h:18
Definition remote_device_config.h:28
Definition supla_srpc.h:28
Definition protocol_layer.h:18
Definition supla_srpc.h:47
Definition supla_srpc.cpp:30
Definition proto.h:3954
Definition proto.h:3980
Definition proto.h:726
Definition proto.h:2658
Definition proto.h:2920
Definition proto.h:2908
Definition proto.h:1197
Definition proto.h:1184
Definition proto.h:799