-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.cpp
196 lines (162 loc) · 6.28 KB
/
main.cpp
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
/*
* Copyright (c) 2020 Arm Limited
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "rtos/ThisThread.h"
#include "NTPClient.h"
#include "certs.h"
#include "iothub.h"
#include "iothub_client_options.h"
#include "iothub_device_client.h"
#include "iothub_message.h"
#include "azure_c_shared_utility/shared_util_options.h"
#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/tickcounter.h"
#include "azure_c_shared_utility/xlogging.h"
#include "iothubtransportmqtt.h"
#include "azure_cloud_credentials.h"
/**
* This example sends and receives messages to and from Azure IoT Hub.
* The API usages are based on Azure SDK's official iothub_convenience_sample.
*/
// Global symbol referenced by the Azure SDK's port for Mbed OS, via "extern"
NetworkInterface *_defaultSystemNetwork;
static bool message_received = false;
static void on_connection_status(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context)
{
if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED) {
LogInfo("Connected to IoT Hub");
} else {
LogError("Connection failed, reason: %s", MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONNECTION_STATUS_REASON, reason));
}
}
static IOTHUBMESSAGE_DISPOSITION_RESULT on_message_received(IOTHUB_MESSAGE_HANDLE message, void* user_context)
{
LogInfo("Message received from IoT Hub");
const unsigned char *data_ptr;
size_t len;
if (IoTHubMessage_GetByteArray(message, &data_ptr, &len) != IOTHUB_MESSAGE_OK) {
LogError("Failed to extract message data, please try again on IoT Hub");
return IOTHUBMESSAGE_ABANDONED;
}
message_received = true;
LogInfo("Message body: %.*s", len, data_ptr);
return IOTHUBMESSAGE_ACCEPTED;
}
static void on_message_sent(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
if (result == IOTHUB_CLIENT_CONFIRMATION_OK) {
LogInfo("Message sent successfully");
} else {
LogInfo("Failed to send message, error: %s",
MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
}
}
void demo() {
bool trace_on = MBED_CONF_APP_IOTHUB_CLIENT_TRACE;
tickcounter_ms_t interval = 100;
IOTHUB_CLIENT_RESULT res;
LogInfo("Initializing IoT Hub client");
IoTHub_Init();
IOTHUB_DEVICE_CLIENT_HANDLE client_handle = IoTHubDeviceClient_CreateFromConnectionString(
azure_cloud::credentials::iothub_connection_string,
MQTT_Protocol
);
if (client_handle == nullptr) {
LogError("Failed to create IoT Hub client handle");
goto cleanup;
}
// Enable SDK tracing
res = IoTHubDeviceClient_SetOption(client_handle, OPTION_LOG_TRACE, &trace_on);
if (res != IOTHUB_CLIENT_OK) {
LogError("Failed to enable IoT Hub client tracing, error: %d", res);
goto cleanup;
}
// Enable static CA Certificates defined in the SDK
res = IoTHubDeviceClient_SetOption(client_handle, OPTION_TRUSTED_CERT, certificates);
if (res != IOTHUB_CLIENT_OK) {
LogError("Failed to set trusted certificates, error: %d", res);
goto cleanup;
}
// Process communication every 100ms
res = IoTHubDeviceClient_SetOption(client_handle, OPTION_DO_WORK_FREQUENCY_IN_MS, &interval);
if (res != IOTHUB_CLIENT_OK) {
LogError("Failed to set communication process frequency, error: %d", res);
goto cleanup;
}
// set incoming message callback
res = IoTHubDeviceClient_SetMessageCallback(client_handle, on_message_received, nullptr);
if (res != IOTHUB_CLIENT_OK) {
LogError("Failed to set message callback, error: %d", res);
goto cleanup;
}
// Set connection/disconnection callback
res = IoTHubDeviceClient_SetConnectionStatusCallback(client_handle, on_connection_status, nullptr);
if (res != IOTHUB_CLIENT_OK) {
LogError("Failed to set connection status callback, error: %d", res);
goto cleanup;
}
// Send ten message to the cloud (one per second)
// or until we receive a message from the cloud
IOTHUB_MESSAGE_HANDLE message_handle;
char message[80];
for (int i = 0; i < 10; ++i) {
if (message_received) {
// If we have received a message from the cloud, don't send more messeges
break;
}
sprintf(message, "%d messages left to send, or until we receive a reply", 10 - i);
LogInfo("Sending: \"%s\"", message);
message_handle = IoTHubMessage_CreateFromString(message);
if (message_handle == nullptr) {
LogError("Failed to create message");
goto cleanup;
}
res = IoTHubDeviceClient_SendEventAsync(client_handle, message_handle, on_message_sent, nullptr);
IoTHubMessage_Destroy(message_handle); // message already copied into the SDK
if (res != IOTHUB_CLIENT_OK) {
LogError("Failed to send message event, error: %d", res);
goto cleanup;
}
ThisThread::sleep_for(1s);
}
// If the user didn't manage to send a cloud-to-device message earlier,
// let's wait until we receive one
while (!message_received) {
// Continue to receive messages in the communication thread
// which is internally created and maintained by the Azure SDK.
sleep();
}
cleanup:
IoTHubDeviceClient_Destroy(client_handle);
IoTHub_Deinit();
}
int main() {
LogInfo("Connecting to the network");
_defaultSystemNetwork = NetworkInterface::get_default_instance();
if (_defaultSystemNetwork == nullptr) {
LogError("No network interface found");
return -1;
}
int ret = _defaultSystemNetwork->connect();
if (ret != 0) {
LogError("Connection error: %d", ret);
return -1;
}
LogInfo("Connection success, MAC: %s", _defaultSystemNetwork->get_mac_address());
LogInfo("Getting time from the NTP server");
NTPClient ntp(_defaultSystemNetwork);
ntp.set_server("time.google.com", 123);
time_t timestamp = ntp.get_timestamp();
if (timestamp < 0) {
LogError("Failed to get the current time, error: %ld", timestamp);
return -1;
}
LogInfo("Time: %s", ctime(×tamp));
set_time(timestamp);
LogInfo("Starting the Demo");
demo();
LogInfo("The demo has ended");
return 0;
}