forked from SeeedJP/ReButtonApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReButtonClient.cpp
More file actions
286 lines (236 loc) · 7.59 KB
/
ReButtonClient.cpp
File metadata and controls
286 lines (236 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <Arduino.h>
#include <ReButton.h>
#include "ReButtonClient.h"
#include <DevkitDPSClient.h>
static const char* GLOBAL_DEVICE_ENDPOINT = "global.azure-devices-provisioning.net";
static void ConnectionStateCallbackFunc(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* context)
{
ReButtonClient* client = (ReButtonClient*)context;
if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED && reason == IOTHUB_CLIENT_CONNECTION_OK)
{
Serial.println("ConnectionStateCallbackFunc() : Connected");
client->SetConnected(true);
}
else
{
Serial.printf("ConnectionStateCallbackFunc() : Disconnected (result:%d reason:%d)\n", result, reason);
client->SetConnected(false);
}
}
static void SendEventCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* context)
{
ReButtonClient* client = (ReButtonClient*)context;
if (result == IOTHUB_CLIENT_CONFIRMATION_OK)
{
Serial.println("SendEventCallback() : Message sent to Azure IoT Hub");
client->SetMessageSent();
}
else
{
Serial.println("SendEventCallback() : Failed to send message to Azure IoT Hub");
}
client->DestroyMessage();
}
static void DeviceTwinCallbackFunc(DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size, void* context)
{
ReButtonClient* client = (ReButtonClient*)context;
Serial.printf("DeviceTwinCallbackFunc() \n%.*s\n", size, payLoad);
client->DeviceTwinUpdateCallbackInvoke(update_state, payLoad, size);
}
static void DeviceTwinReportCallbackFunc(int result, void* context)
{
ReButtonClient* client = (ReButtonClient*)context;
Serial.printf("DeviceTwinReportCallbackFunc() : %d\n", result);
client->SetDeviceTwinReported();
}
ReButtonClient::ReButtonClient()
{
_ClientHandle = NULL;
_MessageHandle = NULL;
_DeviceTwinUpdateCallbackFunc = NULL;
_Connected = false;
_MessageSent = false;
_DeviceTwinReported = false;
}
ReButtonClient::~ReButtonClient()
{
Disconnect();
}
void ReButtonClient::DoWork()
{
IoTHubClient_LL_DoWork(_ClientHandle);
}
bool ReButtonClient::IsAllEventsSent()
{
IOTHUB_CLIENT_STATUS status;
if ((IoTHubClient_LL_GetSendStatus(_ClientHandle, &status) == IOTHUB_CLIENT_OK) && (status == IOTHUB_CLIENT_SEND_STATUS_BUSY))
{
return false;
}
else
{
return true;
}
}
void ReButtonClient::DeviceTwinUpdateCallbackInvoke(DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size)
{
if (_DeviceTwinUpdateCallbackFunc != NULL) _DeviceTwinUpdateCallbackFunc(update_state, payLoad, size);
}
bool ReButtonClient::Connect(DeviceTwinUpdateCallback callback)
{
_DeviceTwinUpdateCallbackFunc = callback;
if (platform_init() != 0)
{
Serial.println("Failed to initialize the platform.");
return false;
}
String iotHubConnectionString;
if (strlen(Config.IoTHubConnectionString) >= 1)
{
iotHubConnectionString = Config.IoTHubConnectionString;
}
else if (strlen(Config.IoTHubConnectionString) <= 0 && strlen(Config.ScopeId) >= 1 && strlen(Config.DeviceId) >= 1 && strlen(Config.SasKey) >= 1)
{
Serial.println("Device provisioning...");
char uds[strlen(Config.SasKey) + 1];
strcpy(uds, Config.SasKey);
DevkitDPSSetLogTrace(false);
DevkitDPSSetAuthType(DPS_AUTH_SYMMETRIC_KEY);
if (!DevkitDPSClientStart(GLOBAL_DEVICE_ENDPOINT, Config.ScopeId, Config.DeviceId, uds, NULL, 0))
{
Serial.println("DPS client for GroupSAS has failed.");
return false;
}
iotHubConnectionString = "HostName=";
iotHubConnectionString += DevkitDPSGetIoTHubURI();
iotHubConnectionString += ";DeviceId=";
iotHubConnectionString += DevkitDPSGetDeviceID();
iotHubConnectionString += ";SharedAccessKey=";
iotHubConnectionString += Config.SasKey;
}
_ClientHandle = IoTHubClient_LL_CreateFromConnectionString(iotHubConnectionString.c_str(), MQTT_Protocol);
if (_ClientHandle == NULL)
{
Serial.println("ERROR: iotHubClientHandle is NULL!");
return false;
}
// set options
// https://github.com/Azure/azure-iot-sdk-c/blob/master/doc/Iothub_sdk_options.md
if (IoTHubClient_LL_SetOption(_ClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
{
Serial.println("Failed to set option \"TrustedCerts\"");
return false;
}
bool bLog = true;
if (IoTHubClient_LL_SetOption(_ClientHandle, "logtrace", &bLog) != IOTHUB_CLIENT_OK)
{
Serial.println("Failed to set option \"logtrace\"");
return false;
}
int connect_timeout = 30;
if (IoTHubClient_LL_SetOption(_ClientHandle, "connect_timeout", &connect_timeout) != IOTHUB_CLIENT_OK)
{
Serial.println("Failed to set option \"TrustedCerts\"");
return false;
}
int keepAlive = 10;
if (IoTHubClient_LL_SetOption(_ClientHandle, "keepalive", &keepAlive) != IOTHUB_CLIENT_OK)
{
Serial.println("Failed to set option \"keepalive\"");
return false;
}
if (IoTHubClient_LL_SetOption(_ClientHandle, "product_info", "ReButton") != IOTHUB_CLIENT_OK)
{
Serial.println("Failed to set option \"product_info\"");
return false;
}
// Connection status change callback
if (IoTHubClient_LL_SetConnectionStatusCallback(_ClientHandle, ConnectionStateCallbackFunc, this) != IOTHUB_CLIENT_OK)
{
Serial.println("IoTHubClient_LL_SetConnectionStatusCallback failed");
return false;
}
if (IoTHubClient_LL_SetDeviceTwinCallback(_ClientHandle, DeviceTwinCallbackFunc, this) != IOTHUB_CLIENT_OK)
{
Serial.printf("IoTHubClient_LL_SetDeviceTwinCallback failed");
return false;
}
Serial.println("InitializeIoTHubConnection() : Exit");
return true;
}
void ReButtonClient::Disconnect()
{
if (_ClientHandle != NULL)
{
IoTHubClient_LL_Destroy(_ClientHandle);
}
}
bool ReButtonClient::IsConnected() const
{
return _Connected;
}
void ReButtonClient::SetConnected(bool connect)
{
_Connected = connect;
}
bool ReButtonClient::SendMessageAsync(const char* payload)
{
if (_MessageHandle != NULL) return false;
_MessageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)payload, strlen(payload));
if (_MessageHandle == NULL)
{
Serial.println("SendMessageAsync() : Message Handle = NULL!");
return false;
}
//MAP_HANDLE propMap = IoTHubMessage_Properties(_MessageHandle);
//time_t now_utc = time(NULL); // utc time
//MAP_RESULT res = Map_AddOrUpdate(propMap, "timestamp", ctime(&now_utc));
//if (res != MAP_OK)
//{
// Serial.println("Adding timestampe property failed");
//}
IoTHubMessage_SetContentEncodingSystemProperty(_MessageHandle, "utf-8");
IoTHubMessage_SetContentTypeSystemProperty(_MessageHandle, "application%2fjson");
MAP_HANDLE propMap = IoTHubMessage_Properties(_MessageHandle);
Map_AddOrUpdate(propMap, "productId", Config.ProductId);
Serial.printf("SendMessageAsync() \n%s\r\n", payload);
if (IoTHubClient_LL_SendEventAsync(_ClientHandle, _MessageHandle, SendEventCallback, this) != IOTHUB_CLIENT_OK)
{
DestroyMessage();
Serial.println("SendMessageAsync() : Failed to hand over the message to IoTHubClient.");
return false;
}
Serial.println("SendMessageAsync() : IoTHubClient accepted the message for delivery.");
return true;
}
bool ReButtonClient::IsMessageSent() const
{
return _MessageSent;
}
void ReButtonClient::SetMessageSent()
{
_MessageSent = true;
}
void ReButtonClient::DestroyMessage()
{
IoTHubMessage_Destroy(_MessageHandle);
_MessageHandle = NULL;
}
bool ReButtonClient::DeviceTwinReport(const char* jsonString)
{
_DeviceTwinReported = false;
if (IoTHubClient_LL_SendReportedState(_ClientHandle, (unsigned char*)jsonString, strlen(jsonString), DeviceTwinReportCallbackFunc, this) != IOTHUB_CLIENT_OK)
{
Serial.println("DeviceTwinReport() : Failed");
return false;
}
return true;
}
bool ReButtonClient::IsDeviceTwinReported() const
{
return _DeviceTwinReported;
}
void ReButtonClient::SetDeviceTwinReported()
{
_DeviceTwinReported = true;
}