![](/media/cache/profiles/5f55d0baa59f4bc1dc393149183f1492.jpg.50x50_q85.jpg)
Changes to enabled on-line compiler
src/aws_iot_mqtt_client.c@0:082731ede69f, 2018-05-30 (annotated)
- Committer:
- JMF
- Date:
- Wed May 30 20:59:51 2018 +0000
- Revision:
- 0:082731ede69f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:082731ede69f | 1 | /* |
JMF | 0:082731ede69f | 2 | * Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
JMF | 0:082731ede69f | 3 | * |
JMF | 0:082731ede69f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
JMF | 0:082731ede69f | 5 | * You may not use this file except in compliance with the License. |
JMF | 0:082731ede69f | 6 | * A copy of the License is located at |
JMF | 0:082731ede69f | 7 | * |
JMF | 0:082731ede69f | 8 | * http://aws.amazon.com/apache2.0 |
JMF | 0:082731ede69f | 9 | * |
JMF | 0:082731ede69f | 10 | * or in the "license" file accompanying this file. This file is distributed |
JMF | 0:082731ede69f | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
JMF | 0:082731ede69f | 12 | * express or implied. See the License for the specific language governing |
JMF | 0:082731ede69f | 13 | * permissions and limitations under the License. |
JMF | 0:082731ede69f | 14 | */ |
JMF | 0:082731ede69f | 15 | |
JMF | 0:082731ede69f | 16 | // Based on Eclipse Paho. |
JMF | 0:082731ede69f | 17 | /******************************************************************************* |
JMF | 0:082731ede69f | 18 | * Copyright (c) 2014 IBM Corp. |
JMF | 0:082731ede69f | 19 | * |
JMF | 0:082731ede69f | 20 | * All rights reserved. This program and the accompanying materials |
JMF | 0:082731ede69f | 21 | * are made available under the terms of the Eclipse Public License v1.0 |
JMF | 0:082731ede69f | 22 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
JMF | 0:082731ede69f | 23 | * |
JMF | 0:082731ede69f | 24 | * The Eclipse Public License is available at |
JMF | 0:082731ede69f | 25 | * http://www.eclipse.org/legal/epl-v10.html |
JMF | 0:082731ede69f | 26 | * and the Eclipse Distribution License is available at |
JMF | 0:082731ede69f | 27 | * http://www.eclipse.org/org/documents/edl-v10.php. |
JMF | 0:082731ede69f | 28 | * |
JMF | 0:082731ede69f | 29 | * Contributors: |
JMF | 0:082731ede69f | 30 | * Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation |
JMF | 0:082731ede69f | 31 | *******************************************************************************/ |
JMF | 0:082731ede69f | 32 | |
JMF | 0:082731ede69f | 33 | /** |
JMF | 0:082731ede69f | 34 | * @file aws_iot_mqtt_client.c |
JMF | 0:082731ede69f | 35 | * @brief MQTT client API definitions |
JMF | 0:082731ede69f | 36 | */ |
JMF | 0:082731ede69f | 37 | |
JMF | 0:082731ede69f | 38 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 39 | extern "C" { |
JMF | 0:082731ede69f | 40 | #endif |
JMF | 0:082731ede69f | 41 | |
JMF | 0:082731ede69f | 42 | #include <string.h> |
JMF | 0:082731ede69f | 43 | |
JMF | 0:082731ede69f | 44 | #include "aws_iot_log.h" |
JMF | 0:082731ede69f | 45 | #include "aws_iot_mqtt_client_interface.h" |
JMF | 0:082731ede69f | 46 | #include "aws_iot_version.h" |
JMF | 0:082731ede69f | 47 | |
JMF | 0:082731ede69f | 48 | #if !DISABLE_METRICS |
JMF | 0:082731ede69f | 49 | #define SDK_METRICS_LEN 25 |
JMF | 0:082731ede69f | 50 | #define SDK_METRICS_TEMPLATE "?SDK=C&Version=%d.%d.%d" |
JMF | 0:082731ede69f | 51 | static char pUsernameTemp[SDK_METRICS_LEN] = {0}; |
JMF | 0:082731ede69f | 52 | #endif |
JMF | 0:082731ede69f | 53 | |
JMF | 0:082731ede69f | 54 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 55 | #include "threads_interface.h" |
JMF | 0:082731ede69f | 56 | #endif |
JMF | 0:082731ede69f | 57 | |
JMF | 0:082731ede69f | 58 | const IoT_Client_Init_Params iotClientInitParamsDefault = IoT_Client_Init_Params_initializer; |
JMF | 0:082731ede69f | 59 | const IoT_MQTT_Will_Options iotMqttWillOptionsDefault = IoT_MQTT_Will_Options_Initializer; |
JMF | 0:082731ede69f | 60 | const IoT_Client_Connect_Params iotClientConnectParamsDefault = IoT_Client_Connect_Params_initializer; |
JMF | 0:082731ede69f | 61 | |
JMF | 0:082731ede69f | 62 | ClientState aws_iot_mqtt_get_client_state(AWS_IoT_Client *pClient) { |
JMF | 0:082731ede69f | 63 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 64 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 65 | return CLIENT_STATE_INVALID; |
JMF | 0:082731ede69f | 66 | } |
JMF | 0:082731ede69f | 67 | |
JMF | 0:082731ede69f | 68 | FUNC_EXIT_RC(pClient->clientStatus.clientState); |
JMF | 0:082731ede69f | 69 | } |
JMF | 0:082731ede69f | 70 | |
JMF | 0:082731ede69f | 71 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 72 | IoT_Error_t aws_iot_mqtt_client_lock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex) { |
JMF | 0:082731ede69f | 73 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 74 | IoT_Error_t threadRc = FAILURE; |
JMF | 0:082731ede69f | 75 | |
JMF | 0:082731ede69f | 76 | if(NULL == pClient || NULL == pMutex){ |
JMF | 0:082731ede69f | 77 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 78 | } |
JMF | 0:082731ede69f | 79 | |
JMF | 0:082731ede69f | 80 | if(false == pClient->clientData.isBlockOnThreadLockEnabled) { |
JMF | 0:082731ede69f | 81 | threadRc = aws_iot_thread_mutex_trylock(pMutex); |
JMF | 0:082731ede69f | 82 | } else { |
JMF | 0:082731ede69f | 83 | threadRc = aws_iot_thread_mutex_lock(pMutex); |
JMF | 0:082731ede69f | 84 | /* Should never return Error because the above request blocks until lock is obtained */ |
JMF | 0:082731ede69f | 85 | } |
JMF | 0:082731ede69f | 86 | |
JMF | 0:082731ede69f | 87 | if(AWS_SUCCESS != threadRc) { |
JMF | 0:082731ede69f | 88 | FUNC_EXIT_RC(threadRc); |
JMF | 0:082731ede69f | 89 | } |
JMF | 0:082731ede69f | 90 | |
JMF | 0:082731ede69f | 91 | FUNC_EXIT_RC(AWS_SUCCESS); |
JMF | 0:082731ede69f | 92 | } |
JMF | 0:082731ede69f | 93 | |
JMF | 0:082731ede69f | 94 | IoT_Error_t aws_iot_mqtt_client_unlock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex) { |
JMF | 0:082731ede69f | 95 | if(NULL == pClient || NULL == pMutex) { |
JMF | 0:082731ede69f | 96 | return NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 97 | } |
JMF | 0:082731ede69f | 98 | IOT_UNUSED(pClient); |
JMF | 0:082731ede69f | 99 | return aws_iot_thread_mutex_unlock(pMutex); |
JMF | 0:082731ede69f | 100 | } |
JMF | 0:082731ede69f | 101 | #endif |
JMF | 0:082731ede69f | 102 | |
JMF | 0:082731ede69f | 103 | IoT_Error_t aws_iot_mqtt_set_client_state(AWS_IoT_Client *pClient, ClientState expectedCurrentState, |
JMF | 0:082731ede69f | 104 | ClientState newState) { |
JMF | 0:082731ede69f | 105 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 106 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 107 | IoT_Error_t threadRc = FAILURE; |
JMF | 0:082731ede69f | 108 | #endif |
JMF | 0:082731ede69f | 109 | |
JMF | 0:082731ede69f | 110 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 111 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 112 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 113 | } |
JMF | 0:082731ede69f | 114 | |
JMF | 0:082731ede69f | 115 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 116 | rc = aws_iot_mqtt_client_lock_mutex(pClient, &(pClient->clientData.state_change_mutex)); |
JMF | 0:082731ede69f | 117 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 118 | return rc; |
JMF | 0:082731ede69f | 119 | } |
JMF | 0:082731ede69f | 120 | #endif |
JMF | 0:082731ede69f | 121 | if(expectedCurrentState == aws_iot_mqtt_get_client_state(pClient)) { |
JMF | 0:082731ede69f | 122 | pClient->clientStatus.clientState = newState; |
JMF | 0:082731ede69f | 123 | rc = AWS_SUCCESS; |
JMF | 0:082731ede69f | 124 | } else { |
JMF | 0:082731ede69f | 125 | rc = MQTT_UNEXPECTED_CLIENT_STATE_ERROR; |
JMF | 0:082731ede69f | 126 | } |
JMF | 0:082731ede69f | 127 | |
JMF | 0:082731ede69f | 128 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 129 | threadRc = aws_iot_mqtt_client_unlock_mutex(pClient, &(pClient->clientData.state_change_mutex)); |
JMF | 0:082731ede69f | 130 | if(AWS_SUCCESS == rc && AWS_SUCCESS != threadRc) { |
JMF | 0:082731ede69f | 131 | rc = threadRc; |
JMF | 0:082731ede69f | 132 | } |
JMF | 0:082731ede69f | 133 | #endif |
JMF | 0:082731ede69f | 134 | |
JMF | 0:082731ede69f | 135 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 136 | } |
JMF | 0:082731ede69f | 137 | |
JMF | 0:082731ede69f | 138 | IoT_Error_t aws_iot_mqtt_set_connect_params(AWS_IoT_Client *pClient, IoT_Client_Connect_Params *pNewConnectParams) { |
JMF | 0:082731ede69f | 139 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 140 | if(NULL == pClient || NULL == pNewConnectParams) { |
JMF | 0:082731ede69f | 141 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 142 | } |
JMF | 0:082731ede69f | 143 | |
JMF | 0:082731ede69f | 144 | pClient->clientData.options.isWillMsgPresent = pNewConnectParams->isWillMsgPresent; |
JMF | 0:082731ede69f | 145 | pClient->clientData.options.MQTTVersion = pNewConnectParams->MQTTVersion; |
JMF | 0:082731ede69f | 146 | pClient->clientData.options.pClientID = pNewConnectParams->pClientID; |
JMF | 0:082731ede69f | 147 | pClient->clientData.options.clientIDLen = pNewConnectParams->clientIDLen; |
JMF | 0:082731ede69f | 148 | #if !DISABLE_METRICS |
JMF | 0:082731ede69f | 149 | if (0 == strlen(pUsernameTemp)) { |
JMF | 0:082731ede69f | 150 | snprintf(pUsernameTemp, SDK_METRICS_LEN, SDK_METRICS_TEMPLATE, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); |
JMF | 0:082731ede69f | 151 | } |
JMF | 0:082731ede69f | 152 | pClient->clientData.options.pUsername = (char*)&pUsernameTemp[0]; |
JMF | 0:082731ede69f | 153 | pClient->clientData.options.usernameLen = strlen(pUsernameTemp); |
JMF | 0:082731ede69f | 154 | #else |
JMF | 0:082731ede69f | 155 | pClient->clientData.options.pUsername = pNewConnectParams->pUsername; |
JMF | 0:082731ede69f | 156 | pClient->clientData.options.usernameLen = pNewConnectParams->usernameLen; |
JMF | 0:082731ede69f | 157 | #endif |
JMF | 0:082731ede69f | 158 | pClient->clientData.options.pPassword = pNewConnectParams->pPassword; |
JMF | 0:082731ede69f | 159 | pClient->clientData.options.passwordLen = pNewConnectParams->passwordLen; |
JMF | 0:082731ede69f | 160 | pClient->clientData.options.will.pTopicName = pNewConnectParams->will.pTopicName; |
JMF | 0:082731ede69f | 161 | pClient->clientData.options.will.topicNameLen = pNewConnectParams->will.topicNameLen; |
JMF | 0:082731ede69f | 162 | pClient->clientData.options.will.pMessage = pNewConnectParams->will.pMessage; |
JMF | 0:082731ede69f | 163 | pClient->clientData.options.will.msgLen = pNewConnectParams->will.msgLen; |
JMF | 0:082731ede69f | 164 | pClient->clientData.options.will.qos = pNewConnectParams->will.qos; |
JMF | 0:082731ede69f | 165 | pClient->clientData.options.will.isRetained = pNewConnectParams->will.isRetained; |
JMF | 0:082731ede69f | 166 | pClient->clientData.options.keepAliveIntervalInSec = pNewConnectParams->keepAliveIntervalInSec; |
JMF | 0:082731ede69f | 167 | pClient->clientData.options.isCleanSession = pNewConnectParams->isCleanSession; |
JMF | 0:082731ede69f | 168 | |
JMF | 0:082731ede69f | 169 | FUNC_EXIT_RC(AWS_SUCCESS); |
JMF | 0:082731ede69f | 170 | } |
JMF | 0:082731ede69f | 171 | |
JMF | 0:082731ede69f | 172 | IoT_Error_t aws_iot_mqtt_free(AWS_IoT_Client *pClient) |
JMF | 0:082731ede69f | 173 | { |
JMF | 0:082731ede69f | 174 | IoT_Error_t rc = AWS_SUCCESS; |
JMF | 0:082731ede69f | 175 | |
JMF | 0:082731ede69f | 176 | if (NULL == pClient) { |
JMF | 0:082731ede69f | 177 | rc = NULL_VALUE_ERROR; |
JMF | 0:082731ede69f | 178 | }else |
JMF | 0:082731ede69f | 179 | { |
JMF | 0:082731ede69f | 180 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 181 | if (rc == AWS_SUCCESS) |
JMF | 0:082731ede69f | 182 | { |
JMF | 0:082731ede69f | 183 | rc = aws_iot_thread_mutex_destroy(&(pClient->clientData.state_change_mutex)); |
JMF | 0:082731ede69f | 184 | } |
JMF | 0:082731ede69f | 185 | |
JMF | 0:082731ede69f | 186 | if (rc == AWS_SUCCESS) |
JMF | 0:082731ede69f | 187 | { |
JMF | 0:082731ede69f | 188 | rc = aws_iot_thread_mutex_destroy(&(pClient->clientData.tls_read_mutex)); |
JMF | 0:082731ede69f | 189 | }else{ |
JMF | 0:082731ede69f | 190 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.tls_read_mutex)); |
JMF | 0:082731ede69f | 191 | } |
JMF | 0:082731ede69f | 192 | |
JMF | 0:082731ede69f | 193 | if (rc == AWS_SUCCESS) |
JMF | 0:082731ede69f | 194 | { |
JMF | 0:082731ede69f | 195 | rc = aws_iot_thread_mutex_destroy(&(pClient->clientData.tls_write_mutex)); |
JMF | 0:082731ede69f | 196 | }else{ |
JMF | 0:082731ede69f | 197 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.tls_write_mutex)); |
JMF | 0:082731ede69f | 198 | } |
JMF | 0:082731ede69f | 199 | #endif |
JMF | 0:082731ede69f | 200 | } |
JMF | 0:082731ede69f | 201 | |
JMF | 0:082731ede69f | 202 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 203 | } |
JMF | 0:082731ede69f | 204 | |
JMF | 0:082731ede69f | 205 | IoT_Error_t aws_iot_mqtt_init(AWS_IoT_Client *pClient, IoT_Client_Init_Params *pInitParams) { |
JMF | 0:082731ede69f | 206 | uint32_t i; |
JMF | 0:082731ede69f | 207 | IoT_Error_t rc; |
JMF | 0:082731ede69f | 208 | IoT_Client_Connect_Params default_options = IoT_Client_Connect_Params_initializer; |
JMF | 0:082731ede69f | 209 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 210 | |
JMF | 0:082731ede69f | 211 | if(NULL == pClient || NULL == pInitParams || NULL == pInitParams->pHostURL || 0 == pInitParams->port || |
JMF | 0:082731ede69f | 212 | NULL == pInitParams->pRootCALocation || NULL == pInitParams->pDevicePrivateKeyLocation || |
JMF | 0:082731ede69f | 213 | NULL == pInitParams->pDeviceCertLocation) { |
JMF | 0:082731ede69f | 214 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 215 | } |
JMF | 0:082731ede69f | 216 | |
JMF | 0:082731ede69f | 217 | for(i = 0; i < AWS_IOT_MQTT_NUM_SUBSCRIBE_HANDLERS; ++i) { |
JMF | 0:082731ede69f | 218 | pClient->clientData.messageHandlers[i].topicName = NULL; |
JMF | 0:082731ede69f | 219 | pClient->clientData.messageHandlers[i].pApplicationHandler = NULL; |
JMF | 0:082731ede69f | 220 | pClient->clientData.messageHandlers[i].pApplicationHandlerData = NULL; |
JMF | 0:082731ede69f | 221 | pClient->clientData.messageHandlers[i].qos = QOS0; |
JMF | 0:082731ede69f | 222 | } |
JMF | 0:082731ede69f | 223 | |
JMF | 0:082731ede69f | 224 | pClient->clientData.packetTimeoutMs = pInitParams->mqttPacketTimeout_ms; |
JMF | 0:082731ede69f | 225 | pClient->clientData.commandTimeoutMs = pInitParams->mqttCommandTimeout_ms; |
JMF | 0:082731ede69f | 226 | pClient->clientData.writeBufSize = AWS_IOT_MQTT_TX_BUF_LEN; |
JMF | 0:082731ede69f | 227 | pClient->clientData.readBufSize = AWS_IOT_MQTT_RX_BUF_LEN; |
JMF | 0:082731ede69f | 228 | pClient->clientData.counterNetworkDisconnected = 0; |
JMF | 0:082731ede69f | 229 | pClient->clientData.disconnectHandler = pInitParams->disconnectHandler; |
JMF | 0:082731ede69f | 230 | pClient->clientData.disconnectHandlerData = pInitParams->disconnectHandlerData; |
JMF | 0:082731ede69f | 231 | pClient->clientData.nextPacketId = 1; |
JMF | 0:082731ede69f | 232 | |
JMF | 0:082731ede69f | 233 | pClient->clientData.packetTimeoutMs = pInitParams->mqttPacketTimeout_ms; |
JMF | 0:082731ede69f | 234 | /* Initialize default connection options */ |
JMF | 0:082731ede69f | 235 | rc = aws_iot_mqtt_set_connect_params(pClient, &default_options); |
JMF | 0:082731ede69f | 236 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 237 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 238 | } |
JMF | 0:082731ede69f | 239 | |
JMF | 0:082731ede69f | 240 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 241 | pClient->clientData.isBlockOnThreadLockEnabled = pInitParams->isBlockOnThreadLockEnabled; |
JMF | 0:082731ede69f | 242 | rc = aws_iot_thread_mutex_init(&(pClient->clientData.state_change_mutex)); |
JMF | 0:082731ede69f | 243 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 244 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 245 | } |
JMF | 0:082731ede69f | 246 | rc = aws_iot_thread_mutex_init(&(pClient->clientData.tls_read_mutex)); |
JMF | 0:082731ede69f | 247 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 248 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.state_change_mutex)); |
JMF | 0:082731ede69f | 249 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 250 | } |
JMF | 0:082731ede69f | 251 | rc = aws_iot_thread_mutex_init(&(pClient->clientData.tls_write_mutex)); |
JMF | 0:082731ede69f | 252 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 253 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.tls_read_mutex)); |
JMF | 0:082731ede69f | 254 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.state_change_mutex)); |
JMF | 0:082731ede69f | 255 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 256 | } |
JMF | 0:082731ede69f | 257 | #endif |
JMF | 0:082731ede69f | 258 | |
JMF | 0:082731ede69f | 259 | pClient->clientStatus.isPingOutstanding = 0; |
JMF | 0:082731ede69f | 260 | pClient->clientStatus.isAutoReconnectEnabled = pInitParams->enableAutoReconnect; |
JMF | 0:082731ede69f | 261 | |
JMF | 0:082731ede69f | 262 | rc = iot_tls_init(&(pClient->networkStack), pInitParams->pRootCALocation, pInitParams->pDeviceCertLocation, |
JMF | 0:082731ede69f | 263 | pInitParams->pDevicePrivateKeyLocation, pInitParams->pHostURL, pInitParams->port, |
JMF | 0:082731ede69f | 264 | pInitParams->tlsHandshakeTimeout_ms, pInitParams->isSSLHostnameVerify); |
JMF | 0:082731ede69f | 265 | |
JMF | 0:082731ede69f | 266 | if(AWS_SUCCESS != rc) { |
JMF | 0:082731ede69f | 267 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 268 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.tls_read_mutex)); |
JMF | 0:082731ede69f | 269 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.state_change_mutex)); |
JMF | 0:082731ede69f | 270 | (void)aws_iot_thread_mutex_destroy(&(pClient->clientData.tls_write_mutex)); |
JMF | 0:082731ede69f | 271 | #endif |
JMF | 0:082731ede69f | 272 | pClient->clientStatus.clientState = CLIENT_STATE_INVALID; |
JMF | 0:082731ede69f | 273 | FUNC_EXIT_RC(rc); |
JMF | 0:082731ede69f | 274 | } |
JMF | 0:082731ede69f | 275 | |
JMF | 0:082731ede69f | 276 | init_timer(&(pClient->pingTimer)); |
JMF | 0:082731ede69f | 277 | init_timer(&(pClient->reconnectDelayTimer)); |
JMF | 0:082731ede69f | 278 | |
JMF | 0:082731ede69f | 279 | pClient->clientStatus.clientState = CLIENT_STATE_INITIALIZED; |
JMF | 0:082731ede69f | 280 | |
JMF | 0:082731ede69f | 281 | FUNC_EXIT_RC(AWS_SUCCESS); |
JMF | 0:082731ede69f | 282 | } |
JMF | 0:082731ede69f | 283 | |
JMF | 0:082731ede69f | 284 | uint16_t aws_iot_mqtt_get_next_packet_id(AWS_IoT_Client *pClient) { |
JMF | 0:082731ede69f | 285 | return pClient->clientData.nextPacketId = (uint16_t) ((MAX_PACKET_ID == pClient->clientData.nextPacketId) ? 1 : ( |
JMF | 0:082731ede69f | 286 | pClient->clientData.nextPacketId + 1)); |
JMF | 0:082731ede69f | 287 | } |
JMF | 0:082731ede69f | 288 | |
JMF | 0:082731ede69f | 289 | bool aws_iot_mqtt_is_client_connected(AWS_IoT_Client *pClient) { |
JMF | 0:082731ede69f | 290 | bool isConnected; |
JMF | 0:082731ede69f | 291 | |
JMF | 0:082731ede69f | 292 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 293 | |
JMF | 0:082731ede69f | 294 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 295 | IOT_WARN(" Client is null! "); |
JMF | 0:082731ede69f | 296 | FUNC_EXIT_RC(false); |
JMF | 0:082731ede69f | 297 | } |
JMF | 0:082731ede69f | 298 | |
JMF | 0:082731ede69f | 299 | switch(pClient->clientStatus.clientState) { |
JMF | 0:082731ede69f | 300 | case CLIENT_STATE_INVALID: |
JMF | 0:082731ede69f | 301 | case CLIENT_STATE_INITIALIZED: |
JMF | 0:082731ede69f | 302 | case CLIENT_STATE_CONNECTING: |
JMF | 0:082731ede69f | 303 | isConnected = false; |
JMF | 0:082731ede69f | 304 | break; |
JMF | 0:082731ede69f | 305 | case CLIENT_STATE_CONNECTED_IDLE: |
JMF | 0:082731ede69f | 306 | case CLIENT_STATE_CONNECTED_YIELD_IN_PROGRESS: |
JMF | 0:082731ede69f | 307 | case CLIENT_STATE_CONNECTED_PUBLISH_IN_PROGRESS: |
JMF | 0:082731ede69f | 308 | case CLIENT_STATE_CONNECTED_SUBSCRIBE_IN_PROGRESS: |
JMF | 0:082731ede69f | 309 | case CLIENT_STATE_CONNECTED_UNSUBSCRIBE_IN_PROGRESS: |
JMF | 0:082731ede69f | 310 | case CLIENT_STATE_CONNECTED_RESUBSCRIBE_IN_PROGRESS: |
JMF | 0:082731ede69f | 311 | case CLIENT_STATE_CONNECTED_WAIT_FOR_CB_RETURN: |
JMF | 0:082731ede69f | 312 | isConnected = true; |
JMF | 0:082731ede69f | 313 | break; |
JMF | 0:082731ede69f | 314 | case CLIENT_STATE_DISCONNECTING: |
JMF | 0:082731ede69f | 315 | case CLIENT_STATE_DISCONNECTED_ERROR: |
JMF | 0:082731ede69f | 316 | case CLIENT_STATE_DISCONNECTED_MANUALLY: |
JMF | 0:082731ede69f | 317 | case CLIENT_STATE_PENDING_RECONNECT: |
JMF | 0:082731ede69f | 318 | default: |
JMF | 0:082731ede69f | 319 | isConnected = false; |
JMF | 0:082731ede69f | 320 | break; |
JMF | 0:082731ede69f | 321 | } |
JMF | 0:082731ede69f | 322 | |
JMF | 0:082731ede69f | 323 | FUNC_EXIT_RC(isConnected); |
JMF | 0:082731ede69f | 324 | } |
JMF | 0:082731ede69f | 325 | |
JMF | 0:082731ede69f | 326 | bool aws_iot_is_autoreconnect_enabled(AWS_IoT_Client *pClient) { |
JMF | 0:082731ede69f | 327 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 328 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 329 | IOT_WARN(" Client is null! "); |
JMF | 0:082731ede69f | 330 | FUNC_EXIT_RC(false); |
JMF | 0:082731ede69f | 331 | } |
JMF | 0:082731ede69f | 332 | |
JMF | 0:082731ede69f | 333 | FUNC_EXIT_RC(pClient->clientStatus.isAutoReconnectEnabled); |
JMF | 0:082731ede69f | 334 | } |
JMF | 0:082731ede69f | 335 | |
JMF | 0:082731ede69f | 336 | IoT_Error_t aws_iot_mqtt_autoreconnect_set_status(AWS_IoT_Client *pClient, bool newStatus) { |
JMF | 0:082731ede69f | 337 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 338 | if(NULL == pClient) { |
JMF | 0:082731ede69f | 339 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 340 | } |
JMF | 0:082731ede69f | 341 | pClient->clientStatus.isAutoReconnectEnabled = newStatus; |
JMF | 0:082731ede69f | 342 | FUNC_EXIT_RC(AWS_SUCCESS); |
JMF | 0:082731ede69f | 343 | } |
JMF | 0:082731ede69f | 344 | |
JMF | 0:082731ede69f | 345 | IoT_Error_t aws_iot_mqtt_set_disconnect_handler(AWS_IoT_Client *pClient, iot_disconnect_handler pDisconnectHandler, |
JMF | 0:082731ede69f | 346 | void *pDisconnectHandlerData) { |
JMF | 0:082731ede69f | 347 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 348 | if(NULL == pClient || NULL == pDisconnectHandler) { |
JMF | 0:082731ede69f | 349 | FUNC_EXIT_RC(NULL_VALUE_ERROR); |
JMF | 0:082731ede69f | 350 | } |
JMF | 0:082731ede69f | 351 | |
JMF | 0:082731ede69f | 352 | pClient->clientData.disconnectHandler = pDisconnectHandler; |
JMF | 0:082731ede69f | 353 | pClient->clientData.disconnectHandlerData = pDisconnectHandlerData; |
JMF | 0:082731ede69f | 354 | FUNC_EXIT_RC(AWS_SUCCESS); |
JMF | 0:082731ede69f | 355 | } |
JMF | 0:082731ede69f | 356 | |
JMF | 0:082731ede69f | 357 | uint32_t aws_iot_mqtt_get_network_disconnected_count(AWS_IoT_Client *pClient) { |
JMF | 0:082731ede69f | 358 | return pClient->clientData.counterNetworkDisconnected; |
JMF | 0:082731ede69f | 359 | } |
JMF | 0:082731ede69f | 360 | |
JMF | 0:082731ede69f | 361 | void aws_iot_mqtt_reset_network_disconnected_count(AWS_IoT_Client *pClient) { |
JMF | 0:082731ede69f | 362 | pClient->clientData.counterNetworkDisconnected = 0; |
JMF | 0:082731ede69f | 363 | } |
JMF | 0:082731ede69f | 364 | |
JMF | 0:082731ede69f | 365 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 366 | } |
JMF | 0:082731ede69f | 367 | #endif |
JMF | 0:082731ede69f | 368 |