forked from coisme/Mbed-to-Google-Cloud-IoT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
319 lines (272 loc) · 10.6 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
* Copyright (c) 2014, 2015 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Ian Craggs - make sure QoS2 processing works, and add device headers
*******************************************************************************/
/**
This is a sample program to illustrate the use of the MQTT Client library
on the mbed platform. The Client class requires two classes which mediate
access to system interfaces for networking and timing. As long as these two
classes provide the required public programming interfaces, it does not matter
what facilities they use underneath. In this program, they use the mbed
system libraries.
*/
#define MQTTCLIENT_QOS1 0
#define MQTTCLIENT_QOS2 0
#include "TLSSocket.h"
#include "MQTTClientMbedOs.h"
#include "MQTT_server_setting.h"
#include "mbed-trace/mbed_trace.h"
#include "mbed_events.h"
#include "NTPClient.h"
#include "jwtgen.h"
#include "mbedtls/error.h"
#define MQTT_MAX_CONNECTIONS 5
#define MQTT_MAX_PACKET_SIZE 1024
#define TIME_JWT_EXP (60*60*24) // 24 hours (MAX)
// LED on/off - This could be different among boards
#define LED_ON 0
#define LED_OFF 1
#include <string>
/* Flag to be set when a message needs to be published, i.e. BUTTON is pushed. */
static volatile bool isPublish = false;
/* Flag to be set when received a message from the server. */
static volatile bool isMessageArrived = false;
/* Buffer size for a receiving message. */
const int MESSAGE_BUFFER_SIZE = 1024;
/* Buffer for a receiving message. */
char messageBuffer[MESSAGE_BUFFER_SIZE];
// Function prototypes
int getPassword(char *buf, size_t buf_size);
void handleMqttMessage(MQTT::MessageData& md);
void handleButtonRise();
int main(int argc, char* argv[])
{
mbed_trace_init();
const float version = 0.1;
DigitalOut led_red(LED_RED, LED_OFF);
DigitalOut led_green(LED_GREEN, LED_OFF);
DigitalOut led_blue(LED_BLUE, LED_OFF);
NetworkInterface* network = NULL;
TLSSocket *socket = new TLSSocket;
MQTTClient* mqttClient = NULL;
printf("Mbed to Google IoT Cloud: version is %.2f\r\n", version);
printf("\r\n");
// Turns on green LED to indicate processing initialization process
led_green = LED_ON;
printf("Opening network interface...\r\n");
{
network = NetworkInterface::get_default_instance();
if (!network) {
printf("Error! No network inteface found.\n");
return -1;
}
printf("Connecting to network\n");
nsapi_size_or_error_t ret = network->connect();
if (ret) {
printf("Unable to connect! returned %d\n", ret);
return -1;
}
}
printf("Network interface opened successfully.\r\n");
printf("\r\n");
/* NTP - Get the current time. Required to generate JWT. */
NTPClient ntp(network);
ntp.set_server("time.google.com", 123);
time_t now = ntp.get_timestamp();
set_time(now);
/* JWT - Set JWT as password. */
const int buf_size = 1024;
char* password = new char[buf_size];
if(getPassword(password, buf_size) != 0) {
printf("ERROR: Failed to generate JWT.\r\n");
return -1;
}
printf("JWT:\r\n%s\r\n\r\n", password);
/* Establish a network connection. */
printf("Connecting to host %s:%d ...\r\n", MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
{
nsapi_error_t ret = socket->open(network);
if (ret != NSAPI_ERROR_OK) {
printf("Could not open socket! Returned %d\n", ret);
return -1;
}
ret = socket->set_root_ca_cert(SSL_CA_PEM);
if (ret != NSAPI_ERROR_OK) {
printf("Could not set ca cert! Returned %d\n", ret);
return -1;
}
ret = socket->set_client_cert_key(SSL_CLIENT_CERT_PEM, SSL_CLIENT_PRIVATE_KEY_PEM);
if (ret != NSAPI_ERROR_OK) {
printf("Could not set keys! Returned %d\n", ret);
return -1;
}
ret = socket->connect(MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
if (ret != NSAPI_ERROR_OK) {
printf("Could not connect! Returned %d\n", ret);
return -1;
}
}
printf("Connection established.\r\n");
printf("\r\n");
// Generates MQTT cliend ID from user's parameter in MQTT_server_setting.h
std::string mqtt_client_id =
std::string("projects/") + GOOGLE_PROJECT_ID + "/locations/"
+ GOOGLE_REGION + "/registries/" + GOOGLE_REGISTRY + "/devices/" + GOOGLE_DEVICE_ID;
/* Establish a MQTT connection. */
printf("MQTT client is connecting to the service ...\r\n");
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 4; // 3 = 3.1 4 = 3.1.1
data.clientID.cstring = (char *)mqtt_client_id.c_str();
data.username.cstring = (char *)"ignored";
data.password.cstring = (char *)password;
mqttClient = new MQTTClient(socket);
int rc = mqttClient->connect(data);
if (rc != MQTT::SUCCESS) {
printf("ERROR: rc from MQTT connect is %d\r\n", rc);
return -1;
}
}
printf("Client connected.\r\n");
printf("\r\n");
// Network initialization done. Turn off the green LED
led_green = LED_OFF;
// Generates topic names from user's setting in MQTT_server_setting.h
std::string mqtt_topic_pub =
std::string("/devices/") + GOOGLE_DEVICE_ID + "/events";
std::string mqtt_topic_sub =
std::string("/devices/") + GOOGLE_DEVICE_ID + "/config";
/* Subscribe a topic. */
bool isSubscribed = false;
printf("Client is trying to subscribe a topic \"%s\".\r\n", mqtt_topic_sub.c_str());
{
int rc = mqttClient->subscribe(mqtt_topic_sub.c_str(), MQTT::QOS0, handleMqttMessage);
if (rc != MQTT::SUCCESS) {
printf("ERROR: rc from MQTT subscribe is %d\r\n", rc);
return -1;
}
isSubscribed = true;
}
printf("Client has subscribed a topic \"%s\".\r\n", mqtt_topic_sub.c_str());
printf("\r\n");
// Enable button 1 for publishing a message.
InterruptIn btn1 = InterruptIn(BUTTON1);
btn1.rise(handleButtonRise);
printf("To send a packet, push the button 1 on your board.\r\n");
// Compose a message for the first time.
std::string initStr = std::string("{\"cloudRegion\":\"") + GOOGLE_REGION + "\",\"deviceId\":\""
+ GOOGLE_DEVICE_ID + "\",\"registryId\":\"" + GOOGLE_REGISTRY + "\",\"hops\":1}";
char* buf = (char*)initStr.c_str();
/* Main loop */
while(1) {
/* Client is disconnected. */
if(!mqttClient->isConnected()){
break;
}
/* Waits a message and handles keepalive. */
if(mqttClient->yield(100) != MQTT::SUCCESS) {
break;
}
/* Received a message. */
if(isMessageArrived) {
isMessageArrived = false;
printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer);
// Save the message to resend when the button is pushed.
// If the received message contains the word "hops", use it.
// Otherwise ignore.
if(strstr(messageBuffer, "hops") != NULL) {
buf = messageBuffer;
}
}
/* Button is pushed - publish a message. */
if(isPublish) {
isPublish = false;
static unsigned int id = 0;
static unsigned int count = 0;
count++;
// When sending a message, blue LED lights.
led_blue = LED_ON;
MQTT::Message message;
message.retained = false;
message.dup = false;
message.payload = (void*)buf;
message.qos = MQTT::QOS0;
message.id = id++;
message.payloadlen = strlen(buf);
// Publish a message.
printf("\r\nPublishing message to the topic %s:\r\n%s\r\n", mqtt_topic_pub.c_str(), buf);
int rc = mqttClient->publish(mqtt_topic_pub.c_str(), message);
if(rc != MQTT::SUCCESS) {
printf("ERROR: rc from MQTT publish is %d\r\n", rc);
}
printf("Message published.\r\n");
led_blue = LED_OFF;
}
}
printf("The client has disconnected.\r\n");
if(mqttClient) {
if(isSubscribed) {
mqttClient->unsubscribe(mqtt_topic_sub.c_str());
mqttClient->setMessageHandler(mqtt_topic_sub.c_str(), 0);
}
if(mqttClient->isConnected())
mqttClient->disconnect();
delete mqttClient;
}
if(socket) {
socket->close();
delete socket;
}
if(network) {
network->disconnect();
// network is not created by new.
}
// Turn on the red LED when the program is done.
led_red = LED_ON;
}
/*
* Creates a password string (JWT) to connect Google Cloud IoT Core.
* @param buf Pointer to the buffer stores password string followed by terminate character, '\0'.
* @param buf_size Size of the buffer in bytes.
* @return 0 when success, otherwise != 0
*/
int getPassword(char *buf, size_t buf_size) {
int ret = -1;
size_t len = 0;
time_t now = time(NULL);
if(JwtGenerator::getJwt(buf, buf_size - 1, &len, SSL_CLIENT_PRIVATE_KEY_PEM,
GOOGLE_PROJECT_ID, now, now + TIME_JWT_EXP) == JwtGenerator::SUCCESS) {
buf[len] = '\0';
ret = 0;
}
return ret;
}
/*
* Callback function called when a message arrived from server.
*/
void handleMqttMessage(MQTT::MessageData& md)
{
// Copy payload to the buffer.
MQTT::Message &message = md.message;
memcpy(messageBuffer, message.payload, message.payloadlen);
messageBuffer[message.payloadlen] = '\0';
isMessageArrived = true;
}
/*
* Callback function called when button is pushed.
*/
void handleButtonRise() {
isPublish = true;
}