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