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