forked from ARMmbed/mbed-os-example-for-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
166 lines (149 loc) · 5.69 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
#include "mbed.h"
#include "mbed_trace.h"
#include "mbedtls/debug.h"
#include "aws_credentials.h"
extern "C" {
// sdk initialization
#include "iot_init.h"
// mqtt methods
#include "iot_mqtt.h"
}
// debugging facilities
#define TRACE_GROUP "Main"
static Mutex trace_mutex;
static void trace_mutex_lock()
{
trace_mutex.lock();
}
static void trace_mutex_unlock()
{
trace_mutex.unlock();
}
extern "C" void aws_iot_puts(const char *msg) {
trace_mutex_lock();
puts(msg);
trace_mutex_unlock();
}
#define MQTT_TIMEOUT_MS 15000
// subscription event handler
static void on_message_received(void * pCallbackContext, IotMqttCallbackParam_t *pCallbackParam) {
auto wait_sem = static_cast<Semaphore*>(pCallbackContext);
char* payload = (char*)pCallbackParam->u.message.info.pPayload;
auto payloadLen = pCallbackParam->u.message.info.payloadLength;
tr_debug("from topic:%s; msg: %.*s", pCallbackParam->u.message.info.pTopicName, payloadLen, payload);
if (strncmp("Warning", payload, 7) != 0) {
tr_info("Hello %.*s !", payloadLen, payload);
wait_sem->release();
}
}
int main()
{
mbed_trace_mutex_wait_function_set( trace_mutex_lock ); // only if thread safety is needed
mbed_trace_mutex_release_function_set( trace_mutex_unlock ); // only if thread safety is needed
mbed_trace_init();
tr_info("Connecting to the network...");
auto eth = NetworkInterface::get_default_instance();
if (eth == NULL) {
tr_error("No Network interface found.");
return -1;
}
auto ret = eth->connect();
if (ret != 0) {
tr_error("Connection error: %x", ret);
return -1;
}
tr_info("MAC: %s", eth->get_mac_address());
tr_info("Connection Success");
// demo :
// - Init sdk
if (!IotSdk_Init()) {
tr_error("AWS Sdk: failed to initialize IotSdk");
return -1;
}
auto init_status = IotMqtt_Init();
if (init_status != IOT_MQTT_SUCCESS) {
tr_error("AWS Sdk: Failed to initialize IotMqtt with %u", init_status);
return -1;
}
// - Connect to mqtt broker
IotMqttNetworkInfo_t network_info = IOT_MQTT_NETWORK_INFO_INITIALIZER;
network_info.pNetworkInterface = aws::get_iot_network_interface();
// create nework connection
network_info.createNetworkConnection = true;
network_info.u.setup.pNetworkServerInfo = {
.hostname = MBED_CONF_APP_AWS_ENDPOINT,
.port = 8883
};
network_info.u.setup.pNetworkCredentialInfo = {
.rootCA = aws::credentials::rootCA,
.clientCrt = aws::credentials::clientCrt,
.clientKey = aws::credentials::clientKey
};
IotMqttConnectInfo_t connect_info = IOT_MQTT_CONNECT_INFO_INITIALIZER;
connect_info.awsIotMqttMode = true; // we are connecting to aws servers
connect_info.pClientIdentifier = MBED_CONF_APP_AWS_CLIENT_IDENTIFIER;
connect_info.clientIdentifierLength = strlen(MBED_CONF_APP_AWS_CLIENT_IDENTIFIER);
IotMqttConnection_t connection = IOT_MQTT_CONNECTION_INITIALIZER;
auto connect_status = IotMqtt_Connect(&network_info, &connect_info, /* timeout ms */ MQTT_TIMEOUT_MS, &connection);
if (connect_status != IOT_MQTT_SUCCESS) {
tr_error("AWS Sdk: Connection to the MQTT broker failed with %u", connect_status);
return -1;
}
// - Subscribe to sdkTest/sub
// On message
// - Display on the console: "Hello %s", message
/* Set the members of the subscription. */
static const char topic[] = MBED_CONF_APP_AWS_MQTT_TOPIC;
Semaphore wait_sem {/* count */ 0, /* max_count */ 1};
IotMqttSubscription_t subscription = IOT_MQTT_SUBSCRIPTION_INITIALIZER;
subscription.qos = IOT_MQTT_QOS_1;
subscription.pTopicFilter = topic;
subscription.topicFilterLength = strlen(topic);
subscription.callback.function = on_message_received;
subscription.callback.pCallbackContext = &wait_sem;
/* Subscribe to the topic using the blocking SUBSCRIBE
* function. */
auto sub_status = IotMqtt_SubscribeSync(connection, &subscription,
/* subscription count */ 1, /* flags */ 0,
/* timeout ms */ MQTT_TIMEOUT_MS );
if (sub_status != IOT_MQTT_SUCCESS) {
tr_error("AWS Sdk: Subscribe failed with : %u", sub_status);
}
/* Set the members of the publish info. */
IotMqttPublishInfo_t publish = IOT_MQTT_PUBLISH_INFO_INITIALIZER;
publish.qos = IOT_MQTT_QOS_1;
publish.pTopicName = topic;
publish.topicNameLength = strlen(topic);
publish.retryLimit = 3;
publish.retryMs = 1000;
for (uint32_t i = 0; i < 10; i++) {
// - for i in 0..9
// - wait up to 1 sec
// - if no message received Publish: "You have %d sec remaining to say hello...", 10-i
// - other wise, exit
if (wait_sem.try_acquire_for(1000)) {
break;
}
/* prepare the message */
static char message[64];
snprintf(message, 64, "Warning: Only %lu seconde(s) left to say your name !", 10 - i);
publish.pPayload = message;
publish.payloadLength = strlen(message);
/* Publish the message. */
tr_info("sending warning message: %s", message);
auto pub_status = IotMqtt_PublishSync(connection, &publish,
/* flags */ 0, /* timeout ms */ MQTT_TIMEOUT_MS);
if (pub_status != IOT_MQTT_SUCCESS) {
tr_warning("AWS Sdk: failed to publish message with %u.", pub_status);
}
}
/* Close the MQTT connection. */
IotMqtt_Disconnect(connection, 0);
IotMqtt_Cleanup();
IotSdk_Cleanup();
tr_info("Done");
while (true) {
ThisThread::sleep_for(1000);
}
return 0;
}