-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.cpp
137 lines (120 loc) · 3.62 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
/*
* Copyright (c) 2020-2021 Arm Limited
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "mbed-trace/mbed_trace.h"
#include "rtos/Mutex.h"
#include "rtos/Thread.h"
#include "rtos/ThisThread.h"
#include "AWSClient/AWSClient.h"
#include "aws_credentials.h"
extern "C" {
#include "core_json.h"
}
#define TRACE_GROUP "Main"
// Implemented by the two demos
void on_message_callback(
const char *topic,
uint16_t topic_length,
const void *payload,
size_t payload_length
);
void demo();
rtos::Mutex connection_mutex;
// Task to process MQTT responses at a regular interval
static void process_responses()
{
AWSClient &client = AWSClient::getInstance();
while (true) {
connection_mutex.lock(); // avoid AWSClient::disconnect() while processing
if (!client.isConnected()) {
connection_mutex.unlock();
break;
}
if (client.processResponses() != MBED_SUCCESS) {
tr_error("AWSClient::processResponses() failed");
}
connection_mutex.unlock();
rtos::ThisThread::sleep_for(10ms);
}
}
int main()
{
// "goto" requires early initialization of variables
AWSClient &client = AWSClient::getInstance();
rtos::Thread process_thread;
AWSClient::TLSCredentials_t credentials;
int ret;
mbed_trace_init();
tr_info("Connecting to the network...");
auto network = NetworkInterface::get_default_instance();
if (network == NULL) {
tr_error("No network interface found");
goto end;
}
ret = network->connect();
if (ret != 0) {
tr_error("Connection error: %x", ret);
goto end;
}
tr_info("MAC: %s", network->get_mac_address());
tr_info("Connection Success");
// Set credentials
credentials.clientCrt = aws::credentials::clientCrt;
credentials.clientCrtLen = sizeof(aws::credentials::clientCrt);
credentials.clientKey = aws::credentials::clientKey;
credentials.clientKeyLen = sizeof(aws::credentials::clientKey);
credentials.rootCrtMain = aws::credentials::rootCA;
credentials.rootCrtMainLen = sizeof(aws::credentials::rootCA);
// Initialize client
ret = client.init(
on_message_callback,
credentials
);
if (ret != MBED_SUCCESS) {
tr_error("AWSClient::init() failed");
goto disconnect;
}
// Connect to AWS IoT Core
ret = client.connect(
network,
credentials,
MBED_CONF_APP_AWS_ENDPOINT,
MBED_CONF_APP_AWS_CLIENT_IDENTIFIER
);
if (ret != MBED_SUCCESS) {
tr_error("AWSClient::connect() failed");
goto disconnect;
}
// Start a background thread to process MQTT
ret = process_thread.start(process_responses);
if (ret != osOK) {
tr_error("Failed to start thread to process MQTT");
goto disconnect;
}
// Run a demo depending on the configuration in mbed_app.json:
// * demo_mqtt.cpp if aws-client.shadow is unset or false
// * demo_shadow.cpp if aws-client.shadow is true
demo();
disconnect:
if (client.isConnected()) {
connection_mutex.lock();
ret = client.disconnect();
connection_mutex.unlock();
if (ret != MBED_SUCCESS) {
tr_error("AWS::disconnect() failed");
}
}
// The process thread should finish on disconnection
ret = process_thread.join();
if (ret != osOK) {
tr_error("Failed to join thread");
}
ret = network->disconnect();
if (ret != MBED_SUCCESS) {
tr_error("NetworkInterface::disconnect() failed");
}
end:
tr_info("End of the demo");
}