![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
V.06 11/3
Dependencies: FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts
Fork of ATT_AWS_IoT_demo by
AWS_openssl/aws_iot_src/protocol/mqtt/aws_iot_embedded_client_wrapper/aws_iot_mqtt_embedded_client_wrapper.cpp@29:f71a0be59b99, 2017-11-03 (annotated)
- Committer:
- jilee
- Date:
- Fri Nov 03 20:28:02 2017 +0000
- Revision:
- 29:f71a0be59b99
- Parent:
- 15:6f2798e45099
v.06 11/03/2016
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ampembeng | 15:6f2798e45099 | 1 | /* |
ampembeng | 15:6f2798e45099 | 2 | * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
ampembeng | 15:6f2798e45099 | 3 | * |
ampembeng | 15:6f2798e45099 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
ampembeng | 15:6f2798e45099 | 5 | * You may not use this file except in compliance with the License. |
ampembeng | 15:6f2798e45099 | 6 | * A copy of the License is located at |
ampembeng | 15:6f2798e45099 | 7 | * |
ampembeng | 15:6f2798e45099 | 8 | * http://aws.amazon.com/apache2.0 |
ampembeng | 15:6f2798e45099 | 9 | * |
ampembeng | 15:6f2798e45099 | 10 | * or in the "license" file accompanying this file. This file is distributed |
ampembeng | 15:6f2798e45099 | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
ampembeng | 15:6f2798e45099 | 12 | * express or implied. See the License for the specific language governing |
ampembeng | 15:6f2798e45099 | 13 | * permissions and limitations under the License. |
ampembeng | 15:6f2798e45099 | 14 | */ |
ampembeng | 15:6f2798e45099 | 15 | #include "timer_interface.h" |
ampembeng | 15:6f2798e45099 | 16 | #include "aws_iot_mqtt_interface.h" |
ampembeng | 15:6f2798e45099 | 17 | #include "MQTTClient.h" |
ampembeng | 15:6f2798e45099 | 18 | #include "aws_iot_config.h" |
ampembeng | 15:6f2798e45099 | 19 | #include "aws_iot_log.h" |
ampembeng | 15:6f2798e45099 | 20 | |
jilee | 29:f71a0be59b99 | 21 | extern char *aws_iot_mqtt_host; |
jilee | 29:f71a0be59b99 | 22 | |
ampembeng | 15:6f2798e45099 | 23 | static Client c; |
ampembeng | 15:6f2798e45099 | 24 | |
ampembeng | 15:6f2798e45099 | 25 | static iot_disconnect_handler clientDisconnectHandler; |
ampembeng | 15:6f2798e45099 | 26 | |
ampembeng | 15:6f2798e45099 | 27 | static unsigned char writebuf[AWS_IOT_MQTT_TX_BUF_LEN]; |
ampembeng | 15:6f2798e45099 | 28 | static unsigned char readbuf[AWS_IOT_MQTT_RX_BUF_LEN]; |
ampembeng | 15:6f2798e45099 | 29 | |
ampembeng | 15:6f2798e45099 | 30 | const MQTTConnectParams MQTTConnectParamsDefault = { |
ampembeng | 15:6f2798e45099 | 31 | .enableAutoReconnect = 0, |
jilee | 29:f71a0be59b99 | 32 | //.pHostURL = AWS_IOT_MQTT_HOST, |
jilee | 29:f71a0be59b99 | 33 | .pHostURL = aws_iot_mqtt_host, |
ampembeng | 15:6f2798e45099 | 34 | .port = AWS_IOT_MQTT_PORT, |
ampembeng | 15:6f2798e45099 | 35 | .pRootCALocation = NULL, |
ampembeng | 15:6f2798e45099 | 36 | .pDeviceCertLocation = NULL, |
ampembeng | 15:6f2798e45099 | 37 | .pDevicePrivateKeyLocation = NULL, |
ampembeng | 15:6f2798e45099 | 38 | .pClientID = NULL, |
ampembeng | 15:6f2798e45099 | 39 | .pUserName = NULL, |
ampembeng | 15:6f2798e45099 | 40 | .pPassword = NULL, |
ampembeng | 15:6f2798e45099 | 41 | .MQTTVersion = MQTT_3_1_1, |
ampembeng | 15:6f2798e45099 | 42 | .KeepAliveInterval_sec = 10, |
ampembeng | 15:6f2798e45099 | 43 | .isCleansession = true, |
ampembeng | 15:6f2798e45099 | 44 | .isWillMsgPresent = false, |
ampembeng | 15:6f2798e45099 | 45 | .will={.pTopicName = NULL, .pMessage = NULL, .isRetained = false, .qos = QOS_0}, |
ampembeng | 15:6f2798e45099 | 46 | .mqttCommandTimeout_ms = 1000, |
ampembeng | 15:6f2798e45099 | 47 | .tlsHandshakeTimeout_ms = 2000, |
ampembeng | 15:6f2798e45099 | 48 | .isSSLHostnameVerify = true, |
ampembeng | 15:6f2798e45099 | 49 | .disconnectHandler = NULL |
ampembeng | 15:6f2798e45099 | 50 | }; |
ampembeng | 15:6f2798e45099 | 51 | |
ampembeng | 15:6f2798e45099 | 52 | const MQTTPublishParams MQTTPublishParamsDefault={ |
ampembeng | 15:6f2798e45099 | 53 | .pTopic = NULL, |
ampembeng | 15:6f2798e45099 | 54 | .MessageParams = {.qos = QOS_0, .isRetained=false, .isDuplicate = false, .id = 0, .pPayload = NULL, .PayloadLen = 0} |
ampembeng | 15:6f2798e45099 | 55 | }; |
ampembeng | 15:6f2798e45099 | 56 | const MQTTSubscribeParams MQTTSubscribeParamsDefault={ |
ampembeng | 15:6f2798e45099 | 57 | .pTopic = NULL, |
ampembeng | 15:6f2798e45099 | 58 | .qos = QOS_0, |
ampembeng | 15:6f2798e45099 | 59 | .mHandler = NULL |
ampembeng | 15:6f2798e45099 | 60 | }; |
ampembeng | 15:6f2798e45099 | 61 | const MQTTCallbackParams MQTTCallbackParamsDefault={ |
ampembeng | 15:6f2798e45099 | 62 | .pTopicName = NULL, |
ampembeng | 15:6f2798e45099 | 63 | .TopicNameLen = 0, |
ampembeng | 15:6f2798e45099 | 64 | .MessageParams = {.qos = QOS_0, .isRetained=false, .isDuplicate = false, .id = 0, .pPayload = NULL, .PayloadLen = 0} |
ampembeng | 15:6f2798e45099 | 65 | }; |
ampembeng | 15:6f2798e45099 | 66 | const MQTTMessageParams MQTTMessageParamsDefault={ |
ampembeng | 15:6f2798e45099 | 67 | .qos = QOS_0, |
ampembeng | 15:6f2798e45099 | 68 | .isRetained=false, |
ampembeng | 15:6f2798e45099 | 69 | .isDuplicate = false, |
ampembeng | 15:6f2798e45099 | 70 | .id = 0, |
ampembeng | 15:6f2798e45099 | 71 | .pPayload = NULL, |
ampembeng | 15:6f2798e45099 | 72 | .PayloadLen = 0 |
ampembeng | 15:6f2798e45099 | 73 | }; |
ampembeng | 15:6f2798e45099 | 74 | const MQTTwillOptions MQTTwillOptionsDefault={ |
ampembeng | 15:6f2798e45099 | 75 | .pTopicName = NULL, |
ampembeng | 15:6f2798e45099 | 76 | .pMessage = NULL, |
ampembeng | 15:6f2798e45099 | 77 | .isRetained = false, |
ampembeng | 15:6f2798e45099 | 78 | .qos = QOS_0 |
ampembeng | 15:6f2798e45099 | 79 | }; |
ampembeng | 15:6f2798e45099 | 80 | |
ampembeng | 15:6f2798e45099 | 81 | #define GETLOWER4BYTES 0x0FFFFFFFF |
ampembeng | 15:6f2798e45099 | 82 | void pahoMessageCallback(MessageData* md) { |
ampembeng | 15:6f2798e45099 | 83 | MQTTMessage* message = md->message; |
ampembeng | 15:6f2798e45099 | 84 | MQTTCallbackParams params; |
ampembeng | 15:6f2798e45099 | 85 | |
ampembeng | 15:6f2798e45099 | 86 | // early exit if we do not have a valid callback pointer |
ampembeng | 15:6f2798e45099 | 87 | if (md->applicationHandler == NULL) { |
ampembeng | 15:6f2798e45099 | 88 | return; |
ampembeng | 15:6f2798e45099 | 89 | } |
ampembeng | 15:6f2798e45099 | 90 | |
ampembeng | 15:6f2798e45099 | 91 | if (NULL != md->topicName->lenstring.data) { |
ampembeng | 15:6f2798e45099 | 92 | params.pTopicName = md->topicName->lenstring.data; |
ampembeng | 15:6f2798e45099 | 93 | params.TopicNameLen = (uint16_t)(md->topicName->lenstring.len); |
ampembeng | 15:6f2798e45099 | 94 | } |
ampembeng | 15:6f2798e45099 | 95 | if (NULL != message) { |
ampembeng | 15:6f2798e45099 | 96 | params.MessageParams.PayloadLen = message->payloadlen & GETLOWER4BYTES; |
ampembeng | 15:6f2798e45099 | 97 | params.MessageParams.pPayload = (char*) message->payload; |
ampembeng | 15:6f2798e45099 | 98 | params.MessageParams.isDuplicate = message->dup; |
ampembeng | 15:6f2798e45099 | 99 | params.MessageParams.qos = (QoSLevel)message->qos; |
ampembeng | 15:6f2798e45099 | 100 | params.MessageParams.isRetained = message->retained; |
ampembeng | 15:6f2798e45099 | 101 | params.MessageParams.id = message->id; |
ampembeng | 15:6f2798e45099 | 102 | } |
ampembeng | 15:6f2798e45099 | 103 | |
ampembeng | 15:6f2798e45099 | 104 | ((iot_message_handler)(md->applicationHandler))(params); |
ampembeng | 15:6f2798e45099 | 105 | } |
ampembeng | 15:6f2798e45099 | 106 | |
ampembeng | 15:6f2798e45099 | 107 | void pahoDisconnectHandler(void) { |
ampembeng | 15:6f2798e45099 | 108 | if(NULL != clientDisconnectHandler) { |
ampembeng | 15:6f2798e45099 | 109 | clientDisconnectHandler(); |
ampembeng | 15:6f2798e45099 | 110 | } |
ampembeng | 15:6f2798e45099 | 111 | } |
ampembeng | 15:6f2798e45099 | 112 | |
ampembeng | 15:6f2798e45099 | 113 | static bool isPowerCycle = true; |
ampembeng | 15:6f2798e45099 | 114 | |
ampembeng | 15:6f2798e45099 | 115 | IoT_Error_t aws_iot_mqtt_connect(MQTTConnectParams *pParams) { |
ampembeng | 15:6f2798e45099 | 116 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 117 | MQTTReturnCode pahoRc = SUCCESS; |
ampembeng | 15:6f2798e45099 | 118 | |
ampembeng | 15:6f2798e45099 | 119 | if(NULL == pParams || NULL == pParams->pClientID || NULL == pParams->pHostURL) { |
ampembeng | 15:6f2798e45099 | 120 | return NULL_VALUE_ERROR; |
ampembeng | 15:6f2798e45099 | 121 | } |
ampembeng | 15:6f2798e45099 | 122 | |
ampembeng | 15:6f2798e45099 | 123 | TLSConnectParams TLSParams; |
ampembeng | 15:6f2798e45099 | 124 | TLSParams.DestinationPort = pParams->port; |
ampembeng | 15:6f2798e45099 | 125 | TLSParams.pDestinationURL = pParams->pHostURL; |
ampembeng | 15:6f2798e45099 | 126 | TLSParams.pDeviceCertLocation = pParams->pDeviceCertLocation; |
ampembeng | 15:6f2798e45099 | 127 | TLSParams.pDevicePrivateKeyLocation = pParams->pDevicePrivateKeyLocation; |
ampembeng | 15:6f2798e45099 | 128 | TLSParams.pRootCALocation = pParams->pRootCALocation; |
ampembeng | 15:6f2798e45099 | 129 | TLSParams.timeout_ms = pParams->tlsHandshakeTimeout_ms; |
ampembeng | 15:6f2798e45099 | 130 | TLSParams.ServerVerificationFlag = pParams->isSSLHostnameVerify; |
ampembeng | 15:6f2798e45099 | 131 | |
ampembeng | 15:6f2798e45099 | 132 | DEBUG("...subscribe"); |
ampembeng | 15:6f2798e45099 | 133 | // This implementation assumes you are not going to switch between cleansession 1 to 0 |
ampembeng | 15:6f2798e45099 | 134 | // As we don't have a default subscription handler support in the MQTT client every time a device power cycles it has to re-subscribe to let the MQTT client to pass the message up to the application callback. |
ampembeng | 15:6f2798e45099 | 135 | // The default message handler will be implemented in the future revisions. |
ampembeng | 15:6f2798e45099 | 136 | if(pParams->isCleansession || isPowerCycle){ |
ampembeng | 15:6f2798e45099 | 137 | pahoRc = MQTTClient(&c, (unsigned int)(pParams->mqttCommandTimeout_ms), writebuf, |
ampembeng | 15:6f2798e45099 | 138 | AWS_IOT_MQTT_TX_BUF_LEN, readbuf, AWS_IOT_MQTT_RX_BUF_LEN, |
ampembeng | 15:6f2798e45099 | 139 | pParams->enableAutoReconnect, iot_tls_init, &TLSParams); |
ampembeng | 15:6f2798e45099 | 140 | if(SUCCESS != pahoRc) { |
ampembeng | 15:6f2798e45099 | 141 | return CONNECTION_ERROR; |
ampembeng | 15:6f2798e45099 | 142 | } |
ampembeng | 15:6f2798e45099 | 143 | isPowerCycle = false; |
ampembeng | 15:6f2798e45099 | 144 | } |
ampembeng | 15:6f2798e45099 | 145 | |
ampembeng | 15:6f2798e45099 | 146 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
ampembeng | 15:6f2798e45099 | 147 | |
ampembeng | 15:6f2798e45099 | 148 | DEBUG("...MQTTVersion"); |
ampembeng | 15:6f2798e45099 | 149 | data.willFlag = pParams->isWillMsgPresent; |
ampembeng | 15:6f2798e45099 | 150 | // compatible type for MQTT_Ver_t |
ampembeng | 15:6f2798e45099 | 151 | switch (pParams->MQTTVersion) { |
ampembeng | 15:6f2798e45099 | 152 | case MQTT_3_1: |
ampembeng | 15:6f2798e45099 | 153 | data.MQTTVersion = (unsigned char) (3); |
ampembeng | 15:6f2798e45099 | 154 | break; |
ampembeng | 15:6f2798e45099 | 155 | case MQTT_3_1_1: |
ampembeng | 15:6f2798e45099 | 156 | data.MQTTVersion = (unsigned char) (4); |
ampembeng | 15:6f2798e45099 | 157 | break; |
ampembeng | 15:6f2798e45099 | 158 | default: |
ampembeng | 15:6f2798e45099 | 159 | data.MQTTVersion = (unsigned char) (4); // default MQTT version = 3.1.1 |
ampembeng | 15:6f2798e45099 | 160 | } |
ampembeng | 15:6f2798e45099 | 161 | |
ampembeng | 15:6f2798e45099 | 162 | // register our disconnect handler, save customer's handler |
ampembeng | 15:6f2798e45099 | 163 | setDisconnectHandler(&c, pahoDisconnectHandler); |
ampembeng | 15:6f2798e45099 | 164 | clientDisconnectHandler = pParams->disconnectHandler; |
ampembeng | 15:6f2798e45099 | 165 | |
ampembeng | 15:6f2798e45099 | 166 | data.clientID.cstring = pParams->pClientID; |
ampembeng | 15:6f2798e45099 | 167 | data.username.cstring = pParams->pUserName; |
ampembeng | 15:6f2798e45099 | 168 | data.password.cstring = pParams->pPassword; |
ampembeng | 15:6f2798e45099 | 169 | data.will.topicName.cstring = (char*)pParams->will.pTopicName; |
ampembeng | 15:6f2798e45099 | 170 | data.will.message.cstring = (char*)pParams->will.pMessage; |
ampembeng | 15:6f2798e45099 | 171 | data.will.qos = (enum QoS)pParams->will.qos; |
ampembeng | 15:6f2798e45099 | 172 | data.will.retained = pParams->will.isRetained; |
ampembeng | 15:6f2798e45099 | 173 | data.keepAliveInterval = pParams->KeepAliveInterval_sec; |
ampembeng | 15:6f2798e45099 | 174 | data.cleansession = pParams->isCleansession; |
ampembeng | 15:6f2798e45099 | 175 | |
ampembeng | 15:6f2798e45099 | 176 | DEBUG("...MQTTConnect"); |
ampembeng | 15:6f2798e45099 | 177 | pahoRc = MQTTConnect(&c, &data); |
ampembeng | 15:6f2798e45099 | 178 | if(MQTT_NETWORK_ALREADY_CONNECTED_ERROR == pahoRc) { |
ampembeng | 15:6f2798e45099 | 179 | rc = NETWORK_ALREADY_CONNECTED; |
ampembeng | 15:6f2798e45099 | 180 | } else if(SUCCESS != pahoRc) { |
ampembeng | 15:6f2798e45099 | 181 | rc = CONNECTION_ERROR; |
ampembeng | 15:6f2798e45099 | 182 | } |
ampembeng | 15:6f2798e45099 | 183 | |
ampembeng | 15:6f2798e45099 | 184 | return rc; |
ampembeng | 15:6f2798e45099 | 185 | } |
ampembeng | 15:6f2798e45099 | 186 | |
ampembeng | 15:6f2798e45099 | 187 | IoT_Error_t aws_iot_mqtt_subscribe(MQTTSubscribeParams *pParams) { |
ampembeng | 15:6f2798e45099 | 188 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 189 | |
ampembeng | 15:6f2798e45099 | 190 | if (0 != MQTTSubscribe(&c, pParams->pTopic, (enum QoS)pParams->qos, pahoMessageCallback, (void (*)(void))(pParams->mHandler))) { |
ampembeng | 15:6f2798e45099 | 191 | rc = SUBSCRIBE_ERROR; |
ampembeng | 15:6f2798e45099 | 192 | } |
ampembeng | 15:6f2798e45099 | 193 | return rc; |
ampembeng | 15:6f2798e45099 | 194 | } |
ampembeng | 15:6f2798e45099 | 195 | |
ampembeng | 15:6f2798e45099 | 196 | IoT_Error_t aws_iot_mqtt_publish(MQTTPublishParams *pParams) { |
ampembeng | 15:6f2798e45099 | 197 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 198 | |
ampembeng | 15:6f2798e45099 | 199 | MQTTMessage Message; |
ampembeng | 15:6f2798e45099 | 200 | Message.dup = pParams->MessageParams.isDuplicate; |
ampembeng | 15:6f2798e45099 | 201 | Message.id = pParams->MessageParams.id; |
ampembeng | 15:6f2798e45099 | 202 | Message.payload = pParams->MessageParams.pPayload; |
ampembeng | 15:6f2798e45099 | 203 | Message.payloadlen = pParams->MessageParams.PayloadLen; |
ampembeng | 15:6f2798e45099 | 204 | Message.qos = (enum QoS)pParams->MessageParams.qos; |
ampembeng | 15:6f2798e45099 | 205 | Message.retained = pParams->MessageParams.isRetained; |
ampembeng | 15:6f2798e45099 | 206 | |
ampembeng | 15:6f2798e45099 | 207 | if(0 != MQTTPublish(&c, pParams->pTopic, &Message)){ |
ampembeng | 15:6f2798e45099 | 208 | rc = PUBLISH_ERROR; |
ampembeng | 15:6f2798e45099 | 209 | } |
ampembeng | 15:6f2798e45099 | 210 | |
ampembeng | 15:6f2798e45099 | 211 | return rc; |
ampembeng | 15:6f2798e45099 | 212 | } |
ampembeng | 15:6f2798e45099 | 213 | |
ampembeng | 15:6f2798e45099 | 214 | IoT_Error_t aws_iot_mqtt_unsubscribe(char *pTopic) { |
ampembeng | 15:6f2798e45099 | 215 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 216 | |
ampembeng | 15:6f2798e45099 | 217 | if(0 != MQTTUnsubscribe(&c, pTopic)){ |
ampembeng | 15:6f2798e45099 | 218 | rc = UNSUBSCRIBE_ERROR; |
ampembeng | 15:6f2798e45099 | 219 | } |
ampembeng | 15:6f2798e45099 | 220 | return rc; |
ampembeng | 15:6f2798e45099 | 221 | } |
ampembeng | 15:6f2798e45099 | 222 | |
ampembeng | 15:6f2798e45099 | 223 | IoT_Error_t aws_iot_mqtt_disconnect() { |
ampembeng | 15:6f2798e45099 | 224 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 225 | |
ampembeng | 15:6f2798e45099 | 226 | if(0 != MQTTDisconnect(&c)){ |
ampembeng | 15:6f2798e45099 | 227 | rc = DISCONNECT_ERROR; |
ampembeng | 15:6f2798e45099 | 228 | } |
ampembeng | 15:6f2798e45099 | 229 | |
ampembeng | 15:6f2798e45099 | 230 | return rc; |
ampembeng | 15:6f2798e45099 | 231 | } |
ampembeng | 15:6f2798e45099 | 232 | |
ampembeng | 15:6f2798e45099 | 233 | IoT_Error_t aws_iot_mqtt_yield(int timeout) { |
ampembeng | 15:6f2798e45099 | 234 | MQTTReturnCode pahoRc = MQTTYield(&c, timeout); |
ampembeng | 15:6f2798e45099 | 235 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 236 | if(MQTT_NETWORK_RECONNECTED == pahoRc){ |
ampembeng | 15:6f2798e45099 | 237 | INFO("aws_iot_mqtt_yield() RECONNECT_SUCCESSFUL"); |
ampembeng | 15:6f2798e45099 | 238 | rc = RECONNECT_SUCCESSFUL; |
ampembeng | 15:6f2798e45099 | 239 | } else if(SUCCESS == pahoRc){ |
ampembeng | 15:6f2798e45099 | 240 | //INFO("aws_iot_mqtt_yield() NONE_ERROR"); |
ampembeng | 15:6f2798e45099 | 241 | rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 242 | } else if(MQTT_NULL_VALUE_ERROR == pahoRc) { |
ampembeng | 15:6f2798e45099 | 243 | INFO("aws_iot_mqtt_yield() NULL_VALUE_ERROR"); |
ampembeng | 15:6f2798e45099 | 244 | rc = NULL_VALUE_ERROR; |
ampembeng | 15:6f2798e45099 | 245 | } else if(MQTT_NETWORK_DISCONNECTED_ERROR == pahoRc) { |
ampembeng | 15:6f2798e45099 | 246 | INFO("aws_iot_mqtt_yield() NETWORK_DISCONNECTED"); |
ampembeng | 15:6f2798e45099 | 247 | rc = NETWORK_DISCONNECTED; |
ampembeng | 15:6f2798e45099 | 248 | } else if(MQTT_RECONNECT_TIMED_OUT == pahoRc) { |
ampembeng | 15:6f2798e45099 | 249 | INFO("aws_iot_mqtt_yield() NETWORK_RECONNECT_TIMED_OUT"); |
ampembeng | 15:6f2798e45099 | 250 | rc = NETWORK_RECONNECT_TIMED_OUT; |
ampembeng | 15:6f2798e45099 | 251 | } else if(MQTT_ATTEMPTING_RECONNECT == pahoRc) { |
ampembeng | 15:6f2798e45099 | 252 | INFO("aws_iot_mqtt_yield() NETWORK_ATTEMPTING_RECONNECT"); |
ampembeng | 15:6f2798e45099 | 253 | rc = NETWORK_ATTEMPTING_RECONNECT; |
ampembeng | 15:6f2798e45099 | 254 | } else if(MQTT_BUFFER_RX_MESSAGE_INVALID == pahoRc){ |
ampembeng | 15:6f2798e45099 | 255 | INFO("aws_iot_mqtt_yield() RX_MESSAGE_INVALID"); |
ampembeng | 15:6f2798e45099 | 256 | rc = RX_MESSAGE_INVALID; |
ampembeng | 15:6f2798e45099 | 257 | } else if(MQTTPACKET_BUFFER_TOO_SHORT == pahoRc){ |
ampembeng | 15:6f2798e45099 | 258 | INFO("aws_iot_mqtt_yield() RX_MESSAGE_BIGGER_THAN_MQTT_RX_BUF"); |
ampembeng | 15:6f2798e45099 | 259 | rc = RX_MESSAGE_BIGGER_THAN_MQTT_RX_BUF; |
ampembeng | 15:6f2798e45099 | 260 | } else { |
ampembeng | 15:6f2798e45099 | 261 | INFO("aws_iot_mqtt_yield() YIELD_ERROR"); |
ampembeng | 15:6f2798e45099 | 262 | rc = YIELD_ERROR; |
ampembeng | 15:6f2798e45099 | 263 | } |
ampembeng | 15:6f2798e45099 | 264 | |
ampembeng | 15:6f2798e45099 | 265 | return rc; |
ampembeng | 15:6f2798e45099 | 266 | } |
ampembeng | 15:6f2798e45099 | 267 | |
ampembeng | 15:6f2798e45099 | 268 | IoT_Error_t aws_iot_mqtt_attempt_reconnect() { |
ampembeng | 15:6f2798e45099 | 269 | MQTTReturnCode pahoRc = MQTTAttemptReconnect(&c); |
ampembeng | 15:6f2798e45099 | 270 | IoT_Error_t rc = RECONNECT_SUCCESSFUL; |
ampembeng | 15:6f2798e45099 | 271 | if(MQTT_NETWORK_RECONNECTED == pahoRc){ |
ampembeng | 15:6f2798e45099 | 272 | rc = RECONNECT_SUCCESSFUL; |
ampembeng | 15:6f2798e45099 | 273 | } else if(MQTT_NULL_VALUE_ERROR == pahoRc) { |
ampembeng | 15:6f2798e45099 | 274 | rc = NULL_VALUE_ERROR; |
ampembeng | 15:6f2798e45099 | 275 | } else if(MQTT_NETWORK_DISCONNECTED_ERROR == pahoRc) { |
ampembeng | 15:6f2798e45099 | 276 | rc = NETWORK_DISCONNECTED; |
ampembeng | 15:6f2798e45099 | 277 | } else if(MQTT_RECONNECT_TIMED_OUT == pahoRc) { |
ampembeng | 15:6f2798e45099 | 278 | rc = NETWORK_RECONNECT_TIMED_OUT; |
ampembeng | 15:6f2798e45099 | 279 | } else if(MQTT_NETWORK_ALREADY_CONNECTED_ERROR == pahoRc) { |
ampembeng | 15:6f2798e45099 | 280 | rc = NETWORK_ALREADY_CONNECTED; |
ampembeng | 15:6f2798e45099 | 281 | } else { |
ampembeng | 15:6f2798e45099 | 282 | rc = GENERIC_ERROR; |
ampembeng | 15:6f2798e45099 | 283 | } |
ampembeng | 15:6f2798e45099 | 284 | |
ampembeng | 15:6f2798e45099 | 285 | return rc; |
ampembeng | 15:6f2798e45099 | 286 | } |
ampembeng | 15:6f2798e45099 | 287 | |
ampembeng | 15:6f2798e45099 | 288 | IoT_Error_t aws_iot_mqtt_autoreconnect_set_status(bool value) { |
ampembeng | 15:6f2798e45099 | 289 | MQTTReturnCode rc = setAutoReconnectEnabled(&c, (uint8_t) value); |
ampembeng | 15:6f2798e45099 | 290 | |
ampembeng | 15:6f2798e45099 | 291 | if(MQTT_NULL_VALUE_ERROR == rc) { |
ampembeng | 15:6f2798e45099 | 292 | return NULL_VALUE_ERROR; |
ampembeng | 15:6f2798e45099 | 293 | } |
ampembeng | 15:6f2798e45099 | 294 | |
ampembeng | 15:6f2798e45099 | 295 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 296 | } |
ampembeng | 15:6f2798e45099 | 297 | |
ampembeng | 15:6f2798e45099 | 298 | bool aws_iot_is_mqtt_connected(void) { |
ampembeng | 15:6f2798e45099 | 299 | return MQTTIsConnected(&c); |
ampembeng | 15:6f2798e45099 | 300 | } |
ampembeng | 15:6f2798e45099 | 301 | |
ampembeng | 15:6f2798e45099 | 302 | bool aws_iot_is_autoreconnect_enabled(void) { |
ampembeng | 15:6f2798e45099 | 303 | return MQTTIsAutoReconnectEnabled(&c); |
ampembeng | 15:6f2798e45099 | 304 | } |
ampembeng | 15:6f2798e45099 | 305 | |
ampembeng | 15:6f2798e45099 | 306 | void aws_iot_mqtt_init(MQTTClient_t *pClient){ |
ampembeng | 15:6f2798e45099 | 307 | pClient->connect = aws_iot_mqtt_connect; |
ampembeng | 15:6f2798e45099 | 308 | pClient->disconnect = aws_iot_mqtt_disconnect; |
ampembeng | 15:6f2798e45099 | 309 | pClient->isConnected = aws_iot_is_mqtt_connected; |
ampembeng | 15:6f2798e45099 | 310 | pClient->reconnect = aws_iot_mqtt_attempt_reconnect; |
ampembeng | 15:6f2798e45099 | 311 | pClient->publish = aws_iot_mqtt_publish; |
ampembeng | 15:6f2798e45099 | 312 | pClient->subscribe = aws_iot_mqtt_subscribe; |
ampembeng | 15:6f2798e45099 | 313 | pClient->unsubscribe = aws_iot_mqtt_unsubscribe; |
ampembeng | 15:6f2798e45099 | 314 | pClient->yield = aws_iot_mqtt_yield; |
ampembeng | 15:6f2798e45099 | 315 | pClient->isAutoReconnectEnabled = aws_iot_is_autoreconnect_enabled; |
ampembeng | 15:6f2798e45099 | 316 | pClient->setAutoReconnectStatus = aws_iot_mqtt_autoreconnect_set_status; |
ampembeng | 15:6f2798e45099 | 317 | } |
ampembeng | 15:6f2798e45099 | 318 | |
ampembeng | 15:6f2798e45099 | 319 |