1
+ /*
2
+ * Copyright (c) 2020-2021 Arm Limited
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ #if !MBED_CONF_AWS_CLIENT_SHADOW
7
+
8
+ #include " mbed.h"
9
+ #include " mbed-trace/mbed_trace.h"
10
+ #include " rtos/ThisThread.h"
11
+ #include " AWSClient/AWSClient.h"
12
+
13
+ extern " C" {
14
+ #include " core_json.h"
15
+ }
16
+
17
+ #define TRACE_GROUP " Main"
18
+
19
+ static bool reply_received = false ;
20
+
21
+ // Callback when a MQTT message has been added to the topic
22
+ void on_message_callback (
23
+ const char *topic,
24
+ uint16_t topic_length,
25
+ const void *payload,
26
+ size_t payload_length)
27
+ {
28
+ char *json_value;
29
+ size_t value_length;
30
+ auto ret = JSON_Search ((char *)payload, payload_length, " sender" , strlen (" sender" ), &json_value, &value_length);
31
+ if (ret == JSONSuccess && (strncmp (json_value, " device" , strlen (" device" )) == 0 )) {
32
+ tr_info (" Message sent successfully" );
33
+ } else {
34
+ ret = JSON_Search ((char *)payload, payload_length, " message" , strlen (" message" ), &json_value, &value_length);
35
+ if (ret == JSONSuccess) {
36
+ reply_received = true ;
37
+ tr_info (" Message received from the cloud: \" %.*s\" " , value_length, json_value);
38
+ } else {
39
+ tr_error (" Failed to extract message from the payload: \" %.*s\" " , payload_length, (const char *) payload);
40
+ }
41
+ }
42
+ }
43
+
44
+ void demo ()
45
+ {
46
+ AWSClient &client = AWSClient::getInstance ();
47
+
48
+ // Subscribe to the topic
49
+ const char topic[] = MBED_CONF_APP_AWS_MQTT_TOPIC;
50
+ int ret = client.subscribe (topic, strlen (topic));
51
+ if (ret != MBED_SUCCESS) {
52
+ tr_error (" AWSClient::subscribe() failed" );
53
+ return ;
54
+ }
55
+
56
+ // Send ten message to the cloud (one per second)
57
+ // Stop when we receive a cloud-to-device message
58
+ char payload[128 ];
59
+ for (int i = 0 ; i < 10 ; i++) {
60
+ if (reply_received) {
61
+ // If we have received a message from the cloud, don't send more messeges
62
+ break ;
63
+ }
64
+
65
+ // The MQTT protocol does not distinguish between senders,
66
+ // so we add a "sender" attribute to the payload
67
+ const char base_message[] = " messages left to send, or until we receive a reply" ;
68
+ sprintf (payload, " {\n "
69
+ " \" sender\" : \" device\" ,\n "
70
+ " \" message\" : \" %d %s\"\n "
71
+ " }" ,
72
+ 10 - i, base_message);
73
+ tr_info (" Publishing \" %d %s\" to topic \" %s\" " , 10 - i, base_message, topic);
74
+ ret = client.publish (
75
+ topic,
76
+ strlen (topic),
77
+ payload,
78
+ strlen (payload)
79
+ );
80
+ if (ret != MBED_SUCCESS) {
81
+ tr_error (" AWSClient::publish() failed" );
82
+ goto unsubscribe;
83
+ }
84
+
85
+ rtos::ThisThread::sleep_for (1s);
86
+ }
87
+
88
+ // If the user didn't manage to send a cloud-to-device message earlier,
89
+ // let's wait until we receive one
90
+ while (!reply_received) {
91
+ // Continue to receive messages in the communication thread
92
+ // which is internally created and maintained by the Azure SDK.
93
+ sleep ();
94
+ }
95
+
96
+ unsubscribe:
97
+ // Unsubscribe from the topic
98
+ ret = client.unsubscribe (topic, strlen (topic));
99
+ if (ret != MBED_SUCCESS) {
100
+ tr_error (" AWSClient::unsubscribe() failed" );
101
+ }
102
+ }
103
+
104
+ #endif // !MBED_CONF_AWS_CLIENT_SHADOW
0 commit comments