Jeon byungchul
/
aws-iot-example
this is fork and i will modify for STM32
Fork of AWS-test by
aws-iot/src/aws_iot_mqtt_client_yield.cpp@0:cd5404401c2f, 2017-04-12 (annotated)
- Committer:
- peyo
- Date:
- Wed Apr 12 14:07:09 2017 +0200
- Revision:
- 0:cd5404401c2f
- Child:
- 2:db59f45ad3d3
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
peyo |
0:cd5404401c2f | 1 | /* |
peyo |
0:cd5404401c2f | 2 | * Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
peyo |
0:cd5404401c2f | 3 | * |
peyo |
0:cd5404401c2f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
peyo |
0:cd5404401c2f | 5 | * You may not use this file except in compliance with the License. |
peyo |
0:cd5404401c2f | 6 | * A copy of the License is located at |
peyo |
0:cd5404401c2f | 7 | * |
peyo |
0:cd5404401c2f | 8 | * http://aws.amazon.com/apache2.0 |
peyo |
0:cd5404401c2f | 9 | * |
peyo |
0:cd5404401c2f | 10 | * or in the "license" file accompanying this file. This file is distributed |
peyo |
0:cd5404401c2f | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
peyo |
0:cd5404401c2f | 12 | * express or implied. See the License for the specific language governing |
peyo |
0:cd5404401c2f | 13 | * permissions and limitations under the License. |
peyo |
0:cd5404401c2f | 14 | */ |
peyo |
0:cd5404401c2f | 15 | |
peyo |
0:cd5404401c2f | 16 | // Based on Eclipse Paho. |
peyo |
0:cd5404401c2f | 17 | /******************************************************************************* |
peyo |
0:cd5404401c2f | 18 | * Copyright (c) 2014 IBM Corp. |
peyo |
0:cd5404401c2f | 19 | * |
peyo |
0:cd5404401c2f | 20 | * All rights reserved. This program and the accompanying materials |
peyo |
0:cd5404401c2f | 21 | * are made available under the terms of the Eclipse Public License v1.0 |
peyo |
0:cd5404401c2f | 22 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
peyo |
0:cd5404401c2f | 23 | * |
peyo |
0:cd5404401c2f | 24 | * The Eclipse Public License is available at |
peyo |
0:cd5404401c2f | 25 | * http://www.eclipse.org/legal/epl-v10.html |
peyo |
0:cd5404401c2f | 26 | * and the Eclipse Distribution License is available at |
peyo |
0:cd5404401c2f | 27 | * http://www.eclipse.org/org/documents/edl-v10.php. |
peyo |
0:cd5404401c2f | 28 | * |
peyo |
0:cd5404401c2f | 29 | * Contributors: |
peyo |
0:cd5404401c2f | 30 | * Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation |
peyo |
0:cd5404401c2f | 31 | *******************************************************************************/ |
peyo |
0:cd5404401c2f | 32 | |
peyo |
0:cd5404401c2f | 33 | /** |
peyo |
0:cd5404401c2f | 34 | * @file aws_iot_mqtt_client_yield.c |
peyo |
0:cd5404401c2f | 35 | * @brief MQTT client yield API definitions |
peyo |
0:cd5404401c2f | 36 | */ |
peyo |
0:cd5404401c2f | 37 | |
peyo |
0:cd5404401c2f | 38 | #ifdef __cplusplus |
peyo |
0:cd5404401c2f | 39 | extern "C" { |
peyo |
0:cd5404401c2f | 40 | #endif |
peyo |
0:cd5404401c2f | 41 | |
peyo |
0:cd5404401c2f | 42 | #include "aws_iot_mqtt_client_common_internal.h" |
peyo |
0:cd5404401c2f | 43 | |
peyo |
0:cd5404401c2f | 44 | /** |
peyo |
0:cd5404401c2f | 45 | * This is for the case when the aws_iot_mqtt_internal_send_packet Fails. |
peyo |
0:cd5404401c2f | 46 | */ |
peyo |
0:cd5404401c2f | 47 | static void _aws_iot_mqtt_force_client_disconnect(AWS_IoT_Client *pClient) { |
peyo |
0:cd5404401c2f | 48 | pClient->clientStatus.clientState = CLIENT_STATE_DISCONNECTED_ERROR; |
peyo |
0:cd5404401c2f | 49 | pClient->networkStack.disconnect(&(pClient->networkStack)); |
peyo |
0:cd5404401c2f | 50 | pClient->networkStack.destroy(&(pClient->networkStack)); |
peyo |
0:cd5404401c2f | 51 | } |
peyo |
0:cd5404401c2f | 52 | |
peyo |
0:cd5404401c2f | 53 | static IoT_Error_t _aws_iot_mqtt_handle_disconnect(AWS_IoT_Client *pClient) { |
peyo |
0:cd5404401c2f | 54 | IoT_Error_t rc; |
peyo |
0:cd5404401c2f | 55 | |
peyo |
0:cd5404401c2f | 56 | FUNC_ENTRY; |
peyo |
0:cd5404401c2f | 57 | |
peyo |
0:cd5404401c2f | 58 | rc = aws_iot_mqtt_disconnect(pClient); |
peyo |
0:cd5404401c2f | 59 | if(rc != SUCCESS) { |
peyo |
0:cd5404401c2f | 60 | // If the aws_iot_mqtt_internal_send_packet prevents us from sending a disconnect packet then we have to clean the stack |
peyo |
0:cd5404401c2f | 61 | _aws_iot_mqtt_force_client_disconnect(pClient); |
peyo |
0:cd5404401c2f | 62 | } |
peyo |
0:cd5404401c2f | 63 | |
peyo |
0:cd5404401c2f | 64 | if(NULL != pClient->clientData.disconnectHandler) { |
peyo |
0:cd5404401c2f | 65 | pClient->clientData.disconnectHandler(pClient, pClient->clientData.disconnectHandlerData); |
peyo |
0:cd5404401c2f | 66 | } |
peyo |
0:cd5404401c2f | 67 | |
peyo |
0:cd5404401c2f | 68 | /* Reset to 0 since this was not a manual disconnect */ |
peyo |
0:cd5404401c2f | 69 | pClient->clientStatus.clientState = CLIENT_STATE_DISCONNECTED_ERROR; |
peyo |
0:cd5404401c2f | 70 | FUNC_EXIT_RC(NETWORK_DISCONNECTED_ERROR); |
peyo |
0:cd5404401c2f | 71 | } |
peyo |
0:cd5404401c2f | 72 | |
peyo |
0:cd5404401c2f | 73 | |
peyo |
0:cd5404401c2f | 74 | static IoT_Error_t _aws_iot_mqtt_handle_reconnect(AWS_IoT_Client *pClient) { |
peyo |
0:cd5404401c2f | 75 | IoT_Error_t rc; |
peyo |
0:cd5404401c2f | 76 | |
peyo |
0:cd5404401c2f | 77 | FUNC_ENTRY; |
peyo |
0:cd5404401c2f | 78 | |
peyo |
0:cd5404401c2f | 79 | if(!has_timer_expired(&(pClient->reconnectDelayTimer))) { |
peyo |
0:cd5404401c2f | 80 | /* Timer has not expired. Not time to attempt reconnect yet. |
peyo |
0:cd5404401c2f | 81 | * Return attempting reconnect */ |
peyo |
0:cd5404401c2f | 82 | FUNC_EXIT_RC(NETWORK_ATTEMPTING_RECONNECT); |
peyo |
0:cd5404401c2f | 83 | } |
peyo |
0:cd5404401c2f | 84 | |
peyo |
0:cd5404401c2f | 85 | rc = NETWORK_PHYSICAL_LAYER_DISCONNECTED; |
peyo |
0:cd5404401c2f | 86 | if(NULL != pClient->networkStack.isConnected) { |
peyo |
0:cd5404401c2f | 87 | rc = pClient->networkStack.isConnected(&(pClient->networkStack)); |
peyo |
0:cd5404401c2f | 88 | } |
peyo |
0:cd5404401c2f | 89 | |
peyo |
0:cd5404401c2f | 90 | if(NETWORK_PHYSICAL_LAYER_CONNECTED == rc) { |
peyo |
0:cd5404401c2f | 91 | rc = aws_iot_mqtt_attempt_reconnect(pClient); |
peyo |
0:cd5404401c2f | 92 | if(NETWORK_RECONNECTED == rc) { |
peyo |
0:cd5404401c2f | 93 | rc = aws_iot_mqtt_set_client_state(pClient, CLIENT_STATE_CONNECTED_IDLE, |
peyo |
0:cd5404401c2f | 94 | CLIENT_STATE_CONNECTED_YIELD_IN_PROGRESS); |
peyo |
0:cd5404401c2f | 95 | if(SUCCESS != rc) { |
peyo |
0:cd5404401c2f | 96 | FUNC_EXIT_RC(rc); |
peyo |
0:cd5404401c2f | 97 | } |
peyo |
0:cd5404401c2f | 98 | FUNC_EXIT_RC(NETWORK_RECONNECTED); |
peyo |
0:cd5404401c2f | 99 | } |
peyo |
0:cd5404401c2f | 100 | } |
peyo |
0:cd5404401c2f | 101 | |
peyo |
0:cd5404401c2f | 102 | pClient->clientData.currentReconnectWaitInterval *= 2; |
peyo |
0:cd5404401c2f | 103 | |
peyo |
0:cd5404401c2f | 104 | if(AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL < pClient->clientData.currentReconnectWaitInterval) { |
peyo |
0:cd5404401c2f | 105 | FUNC_EXIT_RC(NETWORK_RECONNECT_TIMED_OUT_ERROR); |
peyo |
0:cd5404401c2f | 106 | } |
peyo |
0:cd5404401c2f | 107 | countdown_ms(&(pClient->reconnectDelayTimer), pClient->clientData.currentReconnectWaitInterval); |
peyo |
0:cd5404401c2f | 108 | FUNC_EXIT_RC(rc); |
peyo |
0:cd5404401c2f | 109 | } |
peyo |
0:cd5404401c2f | 110 | |
peyo |
0:cd5404401c2f | 111 | static IoT_Error_t _aws_iot_mqtt_keep_alive(AWS_IoT_Client *pClient) { |
peyo |
0:cd5404401c2f | 112 | IoT_Error_t rc = SUCCESS; |
peyo |
0:cd5404401c2f | 113 | TimerAWS timer; |
peyo |
0:cd5404401c2f | 114 | size_t serialized_len; |
peyo |
0:cd5404401c2f | 115 | |
peyo |
0:cd5404401c2f | 116 | FUNC_ENTRY; |
peyo |
0:cd5404401c2f | 117 | |
peyo |
0:cd5404401c2f | 118 | if(NULL == pClient) { |
peyo |
0:cd5404401c2f | 119 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
peyo |
0:cd5404401c2f | 120 | } |
peyo |
0:cd5404401c2f | 121 | |
peyo |
0:cd5404401c2f | 122 | if(0 == pClient->clientData.keepAliveInterval) { |
peyo |
0:cd5404401c2f | 123 | FUNC_EXIT_RC(SUCCESS); |
peyo |
0:cd5404401c2f | 124 | } |
peyo |
0:cd5404401c2f | 125 | |
peyo |
0:cd5404401c2f | 126 | if(!has_timer_expired(&pClient->pingTimer)) { |
peyo |
0:cd5404401c2f | 127 | FUNC_EXIT_RC(SUCCESS); |
peyo |
0:cd5404401c2f | 128 | } |
peyo |
0:cd5404401c2f | 129 | |
peyo |
0:cd5404401c2f | 130 | if(pClient->clientStatus.isPingOutstanding) { |
peyo |
0:cd5404401c2f | 131 | rc = _aws_iot_mqtt_handle_disconnect(pClient); |
peyo |
0:cd5404401c2f | 132 | FUNC_EXIT_RC(rc); |
peyo |
0:cd5404401c2f | 133 | } |
peyo |
0:cd5404401c2f | 134 | |
peyo |
0:cd5404401c2f | 135 | /* there is no ping outstanding - send one */ |
peyo |
0:cd5404401c2f | 136 | init_timer(&timer); |
peyo |
0:cd5404401c2f | 137 | |
peyo |
0:cd5404401c2f | 138 | countdown_ms(&timer, pClient->clientData.commandTimeoutMs); |
peyo |
0:cd5404401c2f | 139 | serialized_len = 0; |
peyo |
0:cd5404401c2f | 140 | rc = aws_iot_mqtt_internal_serialize_zero(pClient->clientData.writeBuf, pClient->clientData.writeBufSize, |
peyo |
0:cd5404401c2f | 141 | PINGREQ, &serialized_len); |
peyo |
0:cd5404401c2f | 142 | if(SUCCESS != rc) { |
peyo |
0:cd5404401c2f | 143 | FUNC_EXIT_RC(rc); |
peyo |
0:cd5404401c2f | 144 | } |
peyo |
0:cd5404401c2f | 145 | |
peyo |
0:cd5404401c2f | 146 | /* send the ping packet */ |
peyo |
0:cd5404401c2f | 147 | rc = aws_iot_mqtt_internal_send_packet(pClient, serialized_len, &timer); |
peyo |
0:cd5404401c2f | 148 | if(SUCCESS != rc) { |
peyo |
0:cd5404401c2f | 149 | //If sending a PING fails we can no longer determine if we are connected. In this case we decide we are disconnected and begin reconnection attempts |
peyo |
0:cd5404401c2f | 150 | rc = _aws_iot_mqtt_handle_disconnect(pClient); |
peyo |
0:cd5404401c2f | 151 | FUNC_EXIT_RC(rc); |
peyo |
0:cd5404401c2f | 152 | } |
peyo |
0:cd5404401c2f | 153 | |
peyo |
0:cd5404401c2f | 154 | pClient->clientStatus.isPingOutstanding = true; |
peyo |
0:cd5404401c2f | 155 | /* start a timer to wait for PINGRESP from server */ |
peyo |
0:cd5404401c2f | 156 | countdown_sec(&pClient->pingTimer, pClient->clientData.keepAliveInterval); |
peyo |
0:cd5404401c2f | 157 | |
peyo |
0:cd5404401c2f | 158 | FUNC_EXIT_RC(SUCCESS); |
peyo |
0:cd5404401c2f | 159 | } |
peyo |
0:cd5404401c2f | 160 | |
peyo |
0:cd5404401c2f | 161 | /** |
peyo |
0:cd5404401c2f | 162 | * @brief Yield to the MQTT client |
peyo |
0:cd5404401c2f | 163 | * |
peyo |
0:cd5404401c2f | 164 | * Called to yield the current thread to the underlying MQTT client. This time is used by |
peyo |
0:cd5404401c2f | 165 | * the MQTT client to manage PING requests to monitor the health of the TCP connection as |
peyo |
0:cd5404401c2f | 166 | * well as periodically check the socket receive buffer for subscribe messages. Yield() |
peyo |
0:cd5404401c2f | 167 | * must be called at a rate faster than the keepalive interval. It must also be called |
peyo |
0:cd5404401c2f | 168 | * at a rate faster than the incoming message rate as this is the only way the client receives |
peyo |
0:cd5404401c2f | 169 | * processing time to manage incoming messages. |
peyo |
0:cd5404401c2f | 170 | * This is the internal function which is called by the yield API to perform the operation. |
peyo |
0:cd5404401c2f | 171 | * Not meant to be called directly as it doesn't do validations or client state changes |
peyo |
0:cd5404401c2f | 172 | * |
peyo |
0:cd5404401c2f | 173 | * @param pClient Reference to the IoT Client |
peyo |
0:cd5404401c2f | 174 | * @param timeout_ms Maximum number of milliseconds to pass thread execution to the client. |
peyo |
0:cd5404401c2f | 175 | * |
peyo |
0:cd5404401c2f | 176 | * @return An IoT Error Type defining successful/failed client processing. |
peyo |
0:cd5404401c2f | 177 | * If this call results in an error it is likely the MQTT connection has dropped. |
peyo |
0:cd5404401c2f | 178 | * iot_is_mqtt_connected can be called to confirm. |
peyo |
0:cd5404401c2f | 179 | */ |
peyo |
0:cd5404401c2f | 180 | static IoT_Error_t _aws_iot_mqtt_internal_yield(AWS_IoT_Client *pClient, uint32_t timeout_ms) { |
peyo |
0:cd5404401c2f | 181 | IoT_Error_t yieldRc = SUCCESS; |
peyo |
0:cd5404401c2f | 182 | |
peyo |
0:cd5404401c2f | 183 | uint8_t packet_type; |
peyo |
0:cd5404401c2f | 184 | ClientState clientState; |
peyo |
0:cd5404401c2f | 185 | TimerAWS timer; |
peyo |
0:cd5404401c2f | 186 | init_timer(&timer); |
peyo |
0:cd5404401c2f | 187 | countdown_ms(&timer, timeout_ms); |
peyo |
0:cd5404401c2f | 188 | |
peyo |
0:cd5404401c2f | 189 | FUNC_ENTRY; |
peyo |
0:cd5404401c2f | 190 | |
peyo |
0:cd5404401c2f | 191 | // evaluate timeout at the end of the loop to make sure the actual yield runs at least once |
peyo |
0:cd5404401c2f | 192 | do { |
peyo |
0:cd5404401c2f | 193 | clientState = aws_iot_mqtt_get_client_state(pClient); |
peyo |
0:cd5404401c2f | 194 | if(CLIENT_STATE_PENDING_RECONNECT == clientState) { |
peyo |
0:cd5404401c2f | 195 | if(AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL < pClient->clientData.currentReconnectWaitInterval) { |
peyo |
0:cd5404401c2f | 196 | yieldRc = NETWORK_RECONNECT_TIMED_OUT_ERROR; |
peyo |
0:cd5404401c2f | 197 | break; |
peyo |
0:cd5404401c2f | 198 | } |
peyo |
0:cd5404401c2f | 199 | yieldRc = _aws_iot_mqtt_handle_reconnect(pClient); |
peyo |
0:cd5404401c2f | 200 | /* Network reconnect attempted, check if yield timer expired before |
peyo |
0:cd5404401c2f | 201 | * doing anything else */ |
peyo |
0:cd5404401c2f | 202 | continue; |
peyo |
0:cd5404401c2f | 203 | } |
peyo |
0:cd5404401c2f | 204 | |
peyo |
0:cd5404401c2f | 205 | yieldRc = aws_iot_mqtt_internal_cycle_read(pClient, &timer, &packet_type); |
peyo |
0:cd5404401c2f | 206 | if(SUCCESS == yieldRc) { |
peyo |
0:cd5404401c2f | 207 | yieldRc = _aws_iot_mqtt_keep_alive(pClient); |
peyo |
0:cd5404401c2f | 208 | } else { |
peyo |
0:cd5404401c2f | 209 | // SSL read and write errors are terminal, connection must be closed and retried |
peyo |
0:cd5404401c2f | 210 | if(NETWORK_SSL_READ_ERROR == yieldRc || NETWORK_SSL_READ_TIMEOUT_ERROR == yieldRc |
peyo |
0:cd5404401c2f | 211 | || NETWORK_SSL_WRITE_ERROR == yieldRc || NETWORK_SSL_WRITE_TIMEOUT_ERROR == yieldRc) { |
peyo |
0:cd5404401c2f | 212 | yieldRc = _aws_iot_mqtt_handle_disconnect(pClient); |
peyo |
0:cd5404401c2f | 213 | } |
peyo |
0:cd5404401c2f | 214 | } |
peyo |
0:cd5404401c2f | 215 | |
peyo |
0:cd5404401c2f | 216 | if(NETWORK_DISCONNECTED_ERROR == yieldRc) { |
peyo |
0:cd5404401c2f | 217 | pClient->clientData.counterNetworkDisconnected++; |
peyo |
0:cd5404401c2f | 218 | if(1 == pClient->clientStatus.isAutoReconnectEnabled) { |
peyo |
0:cd5404401c2f | 219 | yieldRc = aws_iot_mqtt_set_client_state(pClient, CLIENT_STATE_DISCONNECTED_ERROR, |
peyo |
0:cd5404401c2f | 220 | CLIENT_STATE_PENDING_RECONNECT); |
peyo |
0:cd5404401c2f | 221 | if(SUCCESS != yieldRc) { |
peyo |
0:cd5404401c2f | 222 | FUNC_EXIT_RC(yieldRc); |
peyo |
0:cd5404401c2f | 223 | } |
peyo |
0:cd5404401c2f | 224 | |
peyo |
0:cd5404401c2f | 225 | pClient->clientData.currentReconnectWaitInterval = AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL; |
peyo |
0:cd5404401c2f | 226 | countdown_ms(&(pClient->reconnectDelayTimer), pClient->clientData.currentReconnectWaitInterval); |
peyo |
0:cd5404401c2f | 227 | /* Depending on timer values, it is possible that yield timer has expired |
peyo |
0:cd5404401c2f | 228 | * Set to rc to attempting reconnect to inform client that autoreconnect |
peyo |
0:cd5404401c2f | 229 | * attempt has started */ |
peyo |
0:cd5404401c2f | 230 | yieldRc = NETWORK_ATTEMPTING_RECONNECT; |
peyo |
0:cd5404401c2f | 231 | } else { |
peyo |
0:cd5404401c2f | 232 | break; |
peyo |
0:cd5404401c2f | 233 | } |
peyo |
0:cd5404401c2f | 234 | } else if(SUCCESS != yieldRc) { |
peyo |
0:cd5404401c2f | 235 | break; |
peyo |
0:cd5404401c2f | 236 | } |
peyo |
0:cd5404401c2f | 237 | } while(!has_timer_expired(&timer)); |
peyo |
0:cd5404401c2f | 238 | |
peyo |
0:cd5404401c2f | 239 | FUNC_EXIT_RC(yieldRc); |
peyo |
0:cd5404401c2f | 240 | } |
peyo |
0:cd5404401c2f | 241 | |
peyo |
0:cd5404401c2f | 242 | /** |
peyo |
0:cd5404401c2f | 243 | * @brief Yield to the MQTT client |
peyo |
0:cd5404401c2f | 244 | * |
peyo |
0:cd5404401c2f | 245 | * Called to yield the current thread to the underlying MQTT client. This time is used by |
peyo |
0:cd5404401c2f | 246 | * the MQTT client to manage PING requests to monitor the health of the TCP connection as |
peyo |
0:cd5404401c2f | 247 | * well as periodically check the socket receive buffer for subscribe messages. Yield() |
peyo |
0:cd5404401c2f | 248 | * must be called at a rate faster than the keepalive interval. It must also be called |
peyo |
0:cd5404401c2f | 249 | * at a rate faster than the incoming message rate as this is the only way the client receives |
peyo |
0:cd5404401c2f | 250 | * processing time to manage incoming messages. |
peyo |
0:cd5404401c2f | 251 | * This is the outer function which does the validations and calls the internal yield above |
peyo |
0:cd5404401c2f | 252 | * to perform the actual operation. It is also responsible for client state changes |
peyo |
0:cd5404401c2f | 253 | * |
peyo |
0:cd5404401c2f | 254 | * @param pClient Reference to the IoT Client |
peyo |
0:cd5404401c2f | 255 | * @param timeout_ms Maximum number of milliseconds to pass thread execution to the client. |
peyo |
0:cd5404401c2f | 256 | * |
peyo |
0:cd5404401c2f | 257 | * @return An IoT Error Type defining successful/failed client processing. |
peyo |
0:cd5404401c2f | 258 | * If this call results in an error it is likely the MQTT connection has dropped. |
peyo |
0:cd5404401c2f | 259 | * iot_is_mqtt_connected can be called to confirm. |
peyo |
0:cd5404401c2f | 260 | */ |
peyo |
0:cd5404401c2f | 261 | IoT_Error_t aws_iot_mqtt_yield(AWS_IoT_Client *pClient, uint32_t timeout_ms) { |
peyo |
0:cd5404401c2f | 262 | IoT_Error_t rc, yieldRc; |
peyo |
0:cd5404401c2f | 263 | ClientState clientState; |
peyo |
0:cd5404401c2f | 264 | |
peyo |
0:cd5404401c2f | 265 | if(NULL == pClient || 0 == timeout_ms) { |
peyo |
0:cd5404401c2f | 266 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
peyo |
0:cd5404401c2f | 267 | } |
peyo |
0:cd5404401c2f | 268 | |
peyo |
0:cd5404401c2f | 269 | clientState = aws_iot_mqtt_get_client_state(pClient); |
peyo |
0:cd5404401c2f | 270 | /* Check if network was manually disconnected */ |
peyo |
0:cd5404401c2f | 271 | if(CLIENT_STATE_DISCONNECTED_MANUALLY == clientState) { |
peyo |
0:cd5404401c2f | 272 | FUNC_EXIT_RC(NETWORK_MANUALLY_DISCONNECTED); |
peyo |
0:cd5404401c2f | 273 | } |
peyo |
0:cd5404401c2f | 274 | |
peyo |
0:cd5404401c2f | 275 | /* If we are in the pending reconnect state, skip other checks. |
peyo |
0:cd5404401c2f | 276 | * Pending reconnect state is only set when auto-reconnect is enabled */ |
peyo |
0:cd5404401c2f | 277 | if(CLIENT_STATE_PENDING_RECONNECT != clientState) { |
peyo |
0:cd5404401c2f | 278 | /* Check if network is disconnected and auto-reconnect is not enabled */ |
peyo |
0:cd5404401c2f | 279 | if(!aws_iot_mqtt_is_client_connected(pClient)) { |
peyo |
0:cd5404401c2f | 280 | FUNC_EXIT_RC(NETWORK_DISCONNECTED_ERROR); |
peyo |
0:cd5404401c2f | 281 | } |
peyo |
0:cd5404401c2f | 282 | |
peyo |
0:cd5404401c2f | 283 | /* Check if client is idle, if not another operation is in progress and we should return */ |
peyo |
0:cd5404401c2f | 284 | if(CLIENT_STATE_CONNECTED_IDLE != clientState) { |
peyo |
0:cd5404401c2f | 285 | FUNC_EXIT_RC(MQTT_CLIENT_NOT_IDLE_ERROR); |
peyo |
0:cd5404401c2f | 286 | } |
peyo |
0:cd5404401c2f | 287 | |
peyo |
0:cd5404401c2f | 288 | rc = aws_iot_mqtt_set_client_state(pClient, CLIENT_STATE_CONNECTED_IDLE, |
peyo |
0:cd5404401c2f | 289 | CLIENT_STATE_CONNECTED_YIELD_IN_PROGRESS); |
peyo |
0:cd5404401c2f | 290 | if(SUCCESS != rc) { |
peyo |
0:cd5404401c2f | 291 | FUNC_EXIT_RC(rc); |
peyo |
0:cd5404401c2f | 292 | } |
peyo |
0:cd5404401c2f | 293 | } |
peyo |
0:cd5404401c2f | 294 | |
peyo |
0:cd5404401c2f | 295 | yieldRc = _aws_iot_mqtt_internal_yield(pClient, timeout_ms); |
peyo |
0:cd5404401c2f | 296 | |
peyo |
0:cd5404401c2f | 297 | if(NETWORK_DISCONNECTED_ERROR != yieldRc && NETWORK_ATTEMPTING_RECONNECT != yieldRc) { |
peyo |
0:cd5404401c2f | 298 | rc = aws_iot_mqtt_set_client_state(pClient, CLIENT_STATE_CONNECTED_YIELD_IN_PROGRESS, |
peyo |
0:cd5404401c2f | 299 | CLIENT_STATE_CONNECTED_IDLE); |
peyo |
0:cd5404401c2f | 300 | if(SUCCESS == yieldRc && SUCCESS != rc) { |
peyo |
0:cd5404401c2f | 301 | yieldRc = rc; |
peyo |
0:cd5404401c2f | 302 | } |
peyo |
0:cd5404401c2f | 303 | } |
peyo |
0:cd5404401c2f | 304 | |
peyo |
0:cd5404401c2f | 305 | FUNC_EXIT_RC(yieldRc); |
peyo |
0:cd5404401c2f | 306 | } |
peyo |
0:cd5404401c2f | 307 | |
peyo |
0:cd5404401c2f | 308 | #ifdef __cplusplus |
peyo |
0:cd5404401c2f | 309 | } |
peyo |
0:cd5404401c2f | 310 | #endif |
peyo |
0:cd5404401c2f | 311 |