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