TYH TEST FOR UPLOAD

Dependencies:   mbed-aws-TYH

Committer:
doudoutang
Date:
Wed Sep 16 07:00:15 2020 +0000
Revision:
1:3471acaa4817
Parent:
0:fae049daf5a1
Y

Who changed what in which revision?

UserRevisionLine numberNew contents of line
doudoutang 0:fae049daf5a1 1 /*
doudoutang 0:fae049daf5a1 2 * Copyright (c) 2020 Arm Limited
doudoutang 0:fae049daf5a1 3 * SPDX-License-Identifier: Apache-2.0
doudoutang 0:fae049daf5a1 4 */
doudoutang 0:fae049daf5a1 5
doudoutang 0:fae049daf5a1 6 #include "mbed.h"
doudoutang 0:fae049daf5a1 7 #include "mbed_trace.h"
doudoutang 0:fae049daf5a1 8 #include "mbedtls/debug.h"
doudoutang 0:fae049daf5a1 9 #include "aws_credentials.h"
doudoutang 0:fae049daf5a1 10
doudoutang 0:fae049daf5a1 11 extern "C" {
doudoutang 0:fae049daf5a1 12 // sdk initialization
doudoutang 0:fae049daf5a1 13 #include "iot_init.h"
doudoutang 0:fae049daf5a1 14 // mqtt methods
doudoutang 0:fae049daf5a1 15 #include "iot_mqtt.h"
doudoutang 0:fae049daf5a1 16 }
doudoutang 0:fae049daf5a1 17
doudoutang 0:fae049daf5a1 18 // debugging facilities
doudoutang 0:fae049daf5a1 19 #define TRACE_GROUP "Main"
doudoutang 0:fae049daf5a1 20 static Mutex trace_mutex;
doudoutang 0:fae049daf5a1 21 static void trace_mutex_lock()
doudoutang 0:fae049daf5a1 22 {
doudoutang 0:fae049daf5a1 23 trace_mutex.lock();
doudoutang 0:fae049daf5a1 24 }
doudoutang 0:fae049daf5a1 25 static void trace_mutex_unlock()
doudoutang 0:fae049daf5a1 26 {
doudoutang 0:fae049daf5a1 27 trace_mutex.unlock();
doudoutang 0:fae049daf5a1 28 }
doudoutang 0:fae049daf5a1 29 extern "C" void aws_iot_puts(const char *msg) {
doudoutang 0:fae049daf5a1 30 trace_mutex_lock();
doudoutang 0:fae049daf5a1 31 puts(msg);
doudoutang 0:fae049daf5a1 32 trace_mutex_unlock();
doudoutang 0:fae049daf5a1 33 }
doudoutang 0:fae049daf5a1 34
doudoutang 0:fae049daf5a1 35 #define MQTT_TIMEOUT_MS 15000
doudoutang 0:fae049daf5a1 36
doudoutang 0:fae049daf5a1 37 // subscription event handler
doudoutang 0:fae049daf5a1 38 static void on_message_received(void * pCallbackContext, IotMqttCallbackParam_t *pCallbackParam) {
doudoutang 0:fae049daf5a1 39 auto wait_sem = static_cast<Semaphore*>(pCallbackContext);
doudoutang 0:fae049daf5a1 40 char* payload = (char*)pCallbackParam->u.message.info.pPayload;
doudoutang 0:fae049daf5a1 41 auto payloadLen = pCallbackParam->u.message.info.payloadLength;
doudoutang 0:fae049daf5a1 42 tr_debug("from topic:%s; msg: %.*s", pCallbackParam->u.message.info.pTopicName, payloadLen, payload);
doudoutang 0:fae049daf5a1 43
doudoutang 0:fae049daf5a1 44 if (strncmp("Warning", payload, 7) != 0) {
doudoutang 0:fae049daf5a1 45 tr_info("Hello %.*s !", payloadLen, payload);
doudoutang 0:fae049daf5a1 46 wait_sem->release();
doudoutang 0:fae049daf5a1 47 }
doudoutang 0:fae049daf5a1 48 }
doudoutang 0:fae049daf5a1 49 int main()
doudoutang 0:fae049daf5a1 50 {
doudoutang 0:fae049daf5a1 51 mbed_trace_mutex_wait_function_set( trace_mutex_lock ); // only if thread safety is needed
doudoutang 0:fae049daf5a1 52 mbed_trace_mutex_release_function_set( trace_mutex_unlock ); // only if thread safety is needed
doudoutang 0:fae049daf5a1 53 mbed_trace_init();
doudoutang 0:fae049daf5a1 54
doudoutang 0:fae049daf5a1 55 tr_info("Connecting to the network...");
doudoutang 0:fae049daf5a1 56 auto eth = NetworkInterface::get_default_instance();
doudoutang 0:fae049daf5a1 57 if (eth == NULL) {
doudoutang 0:fae049daf5a1 58 tr_error("No Network interface found.");
doudoutang 0:fae049daf5a1 59 return -1;
doudoutang 0:fae049daf5a1 60 }
doudoutang 0:fae049daf5a1 61 auto ret = eth->connect();
doudoutang 0:fae049daf5a1 62 if (ret != 0) {
doudoutang 0:fae049daf5a1 63 tr_error("Connection error: %x", ret);
doudoutang 0:fae049daf5a1 64 return -1;
doudoutang 0:fae049daf5a1 65 }
doudoutang 0:fae049daf5a1 66 tr_info("MAC: %s", eth->get_mac_address());
doudoutang 0:fae049daf5a1 67 tr_info("Connection Success");
doudoutang 0:fae049daf5a1 68
doudoutang 0:fae049daf5a1 69 // demo :
doudoutang 0:fae049daf5a1 70 // - Init sdk
doudoutang 0:fae049daf5a1 71 if (!IotSdk_Init()) {
doudoutang 0:fae049daf5a1 72 tr_error("AWS Sdk: failed to initialize IotSdk");
doudoutang 0:fae049daf5a1 73 return -1;
doudoutang 0:fae049daf5a1 74 }
doudoutang 0:fae049daf5a1 75 auto init_status = IotMqtt_Init();
doudoutang 0:fae049daf5a1 76 if (init_status != IOT_MQTT_SUCCESS) {
doudoutang 0:fae049daf5a1 77 tr_error("AWS Sdk: Failed to initialize IotMqtt with %u", init_status);
doudoutang 0:fae049daf5a1 78 return -1;
doudoutang 0:fae049daf5a1 79 }
doudoutang 0:fae049daf5a1 80 // - Connect to mqtt broker
doudoutang 0:fae049daf5a1 81 IotMqttNetworkInfo_t network_info = IOT_MQTT_NETWORK_INFO_INITIALIZER;
doudoutang 0:fae049daf5a1 82 network_info.pNetworkInterface = aws::get_iot_network_interface();
doudoutang 0:fae049daf5a1 83 // create nework connection
doudoutang 0:fae049daf5a1 84 network_info.createNetworkConnection = true;
doudoutang 0:fae049daf5a1 85 network_info.u.setup.pNetworkServerInfo = {
doudoutang 0:fae049daf5a1 86 .hostname = MBED_CONF_APP_AWS_ENDPOINT,
doudoutang 0:fae049daf5a1 87 .port = 8883
doudoutang 0:fae049daf5a1 88 };
doudoutang 0:fae049daf5a1 89 network_info.u.setup.pNetworkCredentialInfo = {
doudoutang 0:fae049daf5a1 90 .rootCA = aws::credentials::rootCA,
doudoutang 0:fae049daf5a1 91 .clientCrt = aws::credentials::clientCrt,
doudoutang 0:fae049daf5a1 92 .clientKey = aws::credentials::clientKey
doudoutang 0:fae049daf5a1 93 };
doudoutang 0:fae049daf5a1 94
doudoutang 0:fae049daf5a1 95 IotMqttConnectInfo_t connect_info = IOT_MQTT_CONNECT_INFO_INITIALIZER;
doudoutang 0:fae049daf5a1 96 connect_info.awsIotMqttMode = true; // we are connecting to aws servers
doudoutang 0:fae049daf5a1 97 connect_info.pClientIdentifier = MBED_CONF_APP_AWS_CLIENT_IDENTIFIER;
doudoutang 0:fae049daf5a1 98 connect_info.clientIdentifierLength = strlen(MBED_CONF_APP_AWS_CLIENT_IDENTIFIER);
doudoutang 0:fae049daf5a1 99
doudoutang 0:fae049daf5a1 100 IotMqttConnection_t connection = IOT_MQTT_CONNECTION_INITIALIZER;
doudoutang 0:fae049daf5a1 101 auto connect_status = IotMqtt_Connect(&network_info, &connect_info, /* timeout ms */ MQTT_TIMEOUT_MS, &connection);
doudoutang 0:fae049daf5a1 102 if (connect_status != IOT_MQTT_SUCCESS) {
doudoutang 0:fae049daf5a1 103 tr_error("AWS Sdk: Connection to the MQTT broker failed with %u", connect_status);
doudoutang 0:fae049daf5a1 104 return -1;
doudoutang 0:fae049daf5a1 105 }
doudoutang 0:fae049daf5a1 106 // - Subscribe to sdkTest/sub
doudoutang 0:fae049daf5a1 107 // On message
doudoutang 0:fae049daf5a1 108 // - Display on the console: "Hello %s", message
doudoutang 0:fae049daf5a1 109 /* Set the members of the subscription. */
doudoutang 0:fae049daf5a1 110 static const char topic[] = MBED_CONF_APP_AWS_MQTT_TOPIC;
doudoutang 0:fae049daf5a1 111 Semaphore wait_sem {/* count */ 0, /* max_count */ 1};
doudoutang 0:fae049daf5a1 112
doudoutang 0:fae049daf5a1 113 IotMqttSubscription_t subscription = IOT_MQTT_SUBSCRIPTION_INITIALIZER;
doudoutang 0:fae049daf5a1 114 subscription.qos = IOT_MQTT_QOS_1;
doudoutang 0:fae049daf5a1 115 subscription.pTopicFilter = topic;
doudoutang 0:fae049daf5a1 116 subscription.topicFilterLength = strlen(topic);
doudoutang 0:fae049daf5a1 117 subscription.callback.function = on_message_received;
doudoutang 0:fae049daf5a1 118 subscription.callback.pCallbackContext = &wait_sem;
doudoutang 0:fae049daf5a1 119
doudoutang 0:fae049daf5a1 120 /* Subscribe to the topic using the blocking SUBSCRIBE
doudoutang 0:fae049daf5a1 121 * function. */
doudoutang 0:fae049daf5a1 122 auto sub_status = IotMqtt_SubscribeSync(connection, &subscription,
doudoutang 0:fae049daf5a1 123 /* subscription count */ 1, /* flags */ 0,
doudoutang 0:fae049daf5a1 124 /* timeout ms */ MQTT_TIMEOUT_MS );
doudoutang 0:fae049daf5a1 125 if (sub_status != IOT_MQTT_SUCCESS) {
doudoutang 0:fae049daf5a1 126 tr_error("AWS Sdk: Subscribe failed with : %u", sub_status);
doudoutang 0:fae049daf5a1 127 }
doudoutang 0:fae049daf5a1 128
doudoutang 0:fae049daf5a1 129 /* Set the members of the publish info. */
doudoutang 0:fae049daf5a1 130 IotMqttPublishInfo_t publish = IOT_MQTT_PUBLISH_INFO_INITIALIZER;
doudoutang 0:fae049daf5a1 131 publish.qos = IOT_MQTT_QOS_1;
doudoutang 0:fae049daf5a1 132 publish.pTopicName = topic;
doudoutang 0:fae049daf5a1 133 publish.topicNameLength = strlen(topic);
doudoutang 0:fae049daf5a1 134 publish.retryLimit = 3;
doudoutang 0:fae049daf5a1 135 publish.retryMs = 1000;
doudoutang 0:fae049daf5a1 136 for (uint32_t i = 0; i < 10; i++) {
doudoutang 0:fae049daf5a1 137 // - for i in 0..9
doudoutang 0:fae049daf5a1 138 // - wait up to 1 sec
doudoutang 0:fae049daf5a1 139 // - if no message received Publish: "You have %d sec remaining to say hello...", 10-i
doudoutang 0:fae049daf5a1 140 // - other wise, exit
doudoutang 0:fae049daf5a1 141 if (wait_sem.try_acquire_for(1000)) {
doudoutang 0:fae049daf5a1 142 break;
doudoutang 0:fae049daf5a1 143 }
doudoutang 0:fae049daf5a1 144
doudoutang 0:fae049daf5a1 145 /* prepare the message */
doudoutang 0:fae049daf5a1 146 static char message[64];
doudoutang 0:fae049daf5a1 147 snprintf(message, 64, "Warning: Only %lu second(s) left to say your name !", 10 - i);
doudoutang 0:fae049daf5a1 148 publish.pPayload = message;
doudoutang 0:fae049daf5a1 149 publish.payloadLength = strlen(message);
doudoutang 0:fae049daf5a1 150
doudoutang 0:fae049daf5a1 151 /* Publish the message. */
doudoutang 0:fae049daf5a1 152 tr_info("sending warning message: %s", message);
doudoutang 0:fae049daf5a1 153 auto pub_status = IotMqtt_PublishSync(connection, &publish,
doudoutang 0:fae049daf5a1 154 /* flags */ 0, /* timeout ms */ MQTT_TIMEOUT_MS);
doudoutang 0:fae049daf5a1 155 if (pub_status != IOT_MQTT_SUCCESS) {
doudoutang 0:fae049daf5a1 156 tr_warning("AWS Sdk: failed to publish message with %u.", pub_status);
doudoutang 0:fae049daf5a1 157 }
doudoutang 0:fae049daf5a1 158 }
doudoutang 0:fae049daf5a1 159
doudoutang 0:fae049daf5a1 160 /* Close the MQTT connection. */
doudoutang 0:fae049daf5a1 161 IotMqtt_Disconnect(connection, 0);
doudoutang 0:fae049daf5a1 162
doudoutang 0:fae049daf5a1 163 IotMqtt_Cleanup();
doudoutang 0:fae049daf5a1 164 IotSdk_Cleanup();
doudoutang 0:fae049daf5a1 165
doudoutang 0:fae049daf5a1 166 tr_info("Done");
doudoutang 0:fae049daf5a1 167 while (true) {
doudoutang 0:fae049daf5a1 168 ThisThread::sleep_for(1000);
doudoutang 0:fae049daf5a1 169 }
doudoutang 0:fae049daf5a1 170 return 0;
doudoutang 0:fae049daf5a1 171 }