Release of version 1.1.1

This commit is contained in:
bhadrip
2016-04-02 00:20:22 +00:00
parent 76e9dddd11
commit 453a88a5cf
54 changed files with 213 additions and 167 deletions

9
CHANGELOG.md Executable file → Normal file
View File

@@ -1,8 +1,15 @@
#Change Log
## [1.1.1](https://github.com/aws/aws-iot-device-sdk-embedded-C/releases/tag/v1.1.1) (April 1,2016)
Bugfixes/Improvements:
- Removing the Executable bit from all the files in the repository. Fixing [this](https://github.com/aws/aws-iot-device-sdk-embedded-C/issues/14) issue
- Refactoring MQTT client to remove declaration after statement warnings
- Fixing [this](https://forums.aws.amazon.com/thread.jspa?threadID=222467&tstart=0) bug
## [1.1.0](https://github.com/aws/aws-iot-device-sdk-embedded-C/releases/tag/v1.1.0) (February 10,2016)
Features:
- Auto Reconnect and Resubscribe
Bugfixes/Improvements:
- MQTT buffer handling incase of bigger message
- Large timeout values converted to seconds and milliseconds

0
LICENSE.txt Executable file → Normal file
View File

0
NOTICE.txt Executable file → Normal file
View File

47
PortingGuide.md Executable file → Normal file
View File

@@ -1,13 +1,13 @@
#Porting Guide
##Scope
The scope of this document is to provide instructions to modify the provided source files and functions in of this SDK to run in a variety of embedded Cbased environments (e.g. real-time OS, embedded Linux) and to be adjusted to use a specific TLS implementation as available with specific hardware platforms.
The scope of this document is to provide instructions to modify the provided source files and functions in this SDK to run in a variety of embedded Cbased environments (e.g. real-time OS, embedded Linux) and to be adjusted to use a specific TLS implementation as available with specific hardware platforms.
##Contents of the SDK
The SDK ported for linux can be downloaded from the below links.
* [OpenSSL](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_openssl-1.1.0.tar)
* [mbedTLS from ARM](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_mbedtls-1.1.0.tar)
* [OpenSSL](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_openssl-1.1.1.tar)
* [mbedTLS from ARM](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_mbedtls-1.1.1.tar)
The C-code files of this SDK are delivered via the following directory structure (see comment behind folder name for an explanation of its content).
@@ -31,7 +31,7 @@ Current SDK Directory Layout (mbedTLS)
All makefiles in this SDK were configured using the documented folder structure above, so moving or renaming folders will require modifications to makefiles.
##Explanation of folders and their content
`iot_src` : This directory contains the SDK source code including wrappers around the MQTT library, device shadow code and utilities.
`aws_iot_src` : This directory contains the SDK source code including wrappers around the MQTT library, device shadow code and utilities.
`aws_mqtt_embedded_client_lib` : The source code for the Embedded C MQTT client. This client is a modified version of the [Eclipse Paho](http://www.eclipse.org/paho/clients/c/embedded/) Embedded C client. The modifications include improved keep alive handling (callback on disconnect), a fix for unsubscribe functionality, buffer protection against too large MQTT messages and additional callback context to allow for a better layered architecture of the AWS IoT SDK.
@@ -96,24 +96,47 @@ Clean up the connection
The TLS library generally provides the API for the underlying TCP socket.
###Sample Porting:
Marvell has ported the SDK to its IoT Starter kit. [These](https://github.com/marvell-iot/aws_starter_sdk/tree/master/wmsdk/external/aws_iot/aws_iot_src/protocol/mqtt/aws_iot_embedded_client_wrapper/platform_wmsdk) files are example implementations of the above mentioned functions.
##Time source for certificate validation
As part of the TLS handshake the device (client) needs to validate the server certificate which includes validation of the certificate lifetime requiring that the device is aware of the actual time. Devices should be equipped with a real time clock or should be able to obtain the current time via NTP. Bypassing validation of the lifetime of a certificate is not recommended as it exposes the device to a security vulnerability, as it will still accept server certificates even when they have already expired.
##Integration into operating system
###Single-Threaded implementation
The single threaded implementation implies that the sample application code (SDK + MQTT client) is called periodically by the firmware application running on the main thread. This is done by calling the function `iot_mqtt_yield` (in the simple pub-sub example) and by calling `iot_shadow_yield()` (in the device shadow example). In both cases the keep-alive time is set to 10 seconds. This means that the yield functions need to be called at a minimum frequency of once every 10 seconds. Note however that the `iot_mqtt_yield()` function takes care of reading incoming MQTT messages from the IoT service as well and hence should be called more frequently depending on the timing requirements of an application. All incoming messages can only be processed at the frequency at which `yield` is called.
The single threaded implementation implies that the sample application code (SDK + MQTT client) is called periodically by the firmware application running on the main thread. This is done by calling the function `aws_iot_mqtt_yield` (in the simple pub-sub example) and by calling `aws_iot_shadow_yield()` (in the device shadow example). In both cases the keep-alive time is set to 10 seconds. This means that the yield functions need to be called at a minimum frequency of once every 10 seconds. Note however that the `iot_mqtt_yield()` function takes care of reading incoming MQTT messages from the IoT service as well and hence should be called more frequently depending on the timing requirements of an application. All incoming messages can only be processed at the frequency at which `yield` is called.
###Multi-Threaded implementation
In the simple multithreaded case the yield() function can be moved to a background thread. Ensure this task runs at the frequency described above. In this case, depending on the OS mechanism, a message queue or mailbox could be used to proxy incoming MQTT messages from the callback to the worker task responsible for responding to or dispatching messages. A similar mechanism could be employed to queue publish messages from threads into a publish queue that are processed by a publishing task.
In the simple multithreaded case the `yield` function can be moved to a background thread. Ensure this task runs at the frequency described above. In this case, depending on the OS mechanism, a message queue or mailbox could be used to proxy incoming MQTT messages from the callback to the worker task responsible for responding to or dispatching messages. A similar mechanism could be employed to queue publish messages from threads into a publish queue that are processed by a publishing task. Ensure a synchronization primitive like mutex is used, as the library is not thread safe.
##Sample applications
The sample apps in this SDK provide a working implementation for either openSSL or mbedTLS, meaning that the function calls explained above are already implemented for these environments.
The sample apps in this SDK provide a working implementation for either openSSL or mbedTLS, meaning that the function calls explained above are already implemented for these TLS libraries for linux.
###Memory Requirements
Building the SDK shadow example using the Keil ARM toolchain on a Windows box.<br>
Target is an ARM Cortex M4.<br>
Code: ~8kb code+const<br>
RAM: ~12kb<br>
These numbers are with TLS and TCP/IP code stubbed out. This is just the SDK.
These numbers do not include TLS and TCP/IP code. This is just the AWS IoT SDK.
####Marvell Example
The following sizes are of AWS IoT sample compiled for [Marvell AWS IoT](https://github.com/marvell-iot/aws_starter_sdk) platform.
#####Size of Certificates
- Private key - 1733 bytes
- Signed Certificate from AWS IoT - 1225 bytes
- Root CA - 1680 bytes
#####Size of SDK with MQTT subscribe publish sample
All sizes are in bytes
| Name | Text | RO Data | Data | BSS | Common | Total|
|---|---|---|---|---|---|---|
|MQTT Buffer: 512 bytes | 4436 | 304 | 1 | 1200 | 0 | 5941 |
|MQTT Buffer: 256 bytes | 4436 | 304 | 1 | 688 | 0 | 5429 |
#####Size of SDK with Shadow sample
All sizes are in bytes
| Name | Text | RO Data | Data | BSS | Common | Total|
|---|---|---|---|---|---|---|
|Shadow Sample with default `aws_iot_config.h` values | 9016 | 618 | 2 | 7792 | 0 | 17428 |
| Shadow Sample with reduced JSON keys and less number of message handling at any given time | 9020 | 618 | 2 | 5322 | 0 | 14962 |

4
README.md Executable file → Normal file
View File

@@ -27,8 +27,8 @@ Ensure you understand the AWS IoT platform and create the necessary certificates
In order to quickly get started with the AWS IoT platform, we have ported the SDK for POSIX type Operating Systems like Ubuntu, OS X and RHEL. The porting of the SDK happens at the TLS layer, and for the MQTT protocol. The SDK is configured for two TLS libraries and can be built out of the box with *GCC* using *make utility*. The tarballs can be downloaded from the below links.
* [OpenSSL](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_openssl-1.1.0.tar)
* [mbedTLS from ARM](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_mbedtls-1.1.0.tar)
* [OpenSSL](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_openssl-1.1.1.tar)
* [mbedTLS from ARM](https://s3.amazonaws.com/aws-iot-device-sdk-embedded-c/linux_mqtt_mbedtls-1.1.1.tar)
##Installation
This section explains the individual steps to retrieve the necessary files and be able to build your first application using the AWS IoT device SDK for embedded C.

0
aws_iot_src/protocol/mqtt/aws_iot_mqtt_interface.h Executable file → Normal file
View File

0
aws_iot_src/shadow/aws_iot_shadow.c Executable file → Normal file
View File

2
aws_iot_src/shadow/aws_iot_shadow_actions.c Executable file → Normal file
View File

@@ -38,7 +38,7 @@ IoT_Error_t iot_shadow_action(MQTTClient_t *pClient, const char *pThingName, Sha
isCallbackPresent = true;
}
char extractedClientToken[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE];
char extractedClientToken[MAX_SIZE_CLIENT_ID_WITH_SEQUENCE];
isClientTokenPresent = extractClientToken(pJsonDocumentToBeSent, extractedClientToken);
if (isClientTokenPresent && isCallbackPresent) {

0
aws_iot_src/shadow/aws_iot_shadow_actions.h Executable file → Normal file
View File

0
aws_iot_src/shadow/aws_iot_shadow_interface.h Executable file → Normal file
View File

0
aws_iot_src/shadow/aws_iot_shadow_json.c Executable file → Normal file
View File

0
aws_iot_src/shadow/aws_iot_shadow_json.h Executable file → Normal file
View File

0
aws_iot_src/shadow/aws_iot_shadow_json_data.h Executable file → Normal file
View File

0
aws_iot_src/shadow/aws_iot_shadow_key.h Executable file → Normal file
View File

4
aws_iot_src/shadow/aws_iot_shadow_records.c Executable file → Normal file
View File

@@ -167,7 +167,7 @@ static int AckStatusCallback(MQTTCallbackParams params) {
int32_t tokenCount;
int32_t i;
void *pJsonHandler;
char temporaryClientToken[MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE];
char temporaryClientToken[MAX_SIZE_CLIENT_ID_WITH_SEQUENCE];
if (params.MessageParams.PayloadLen > SHADOW_MAX_SIZE_OF_RX_BUFFER) {
return GENERIC_ERROR;
@@ -412,7 +412,7 @@ void addToAckWaitList(uint8_t indexAckWaitList, const char *pThingName, ShadowAc
const char *pExtractedClientToken, fpActionCallback_t callback, void *pCallbackContext,
uint32_t timeout_seconds) {
AckWaitList[indexAckWaitList].callback = callback;
strncpy(AckWaitList[indexAckWaitList].clientTokenID, pExtractedClientToken, MAX_SIZE_CLIENT_TOKEN_CLIENT_SEQUENCE);
strncpy(AckWaitList[indexAckWaitList].clientTokenID, pExtractedClientToken, MAX_SIZE_CLIENT_ID_WITH_SEQUENCE);
strncpy(AckWaitList[indexAckWaitList].thingName, pThingName, MAX_SIZE_OF_THING_NAME);
AckWaitList[indexAckWaitList].pCallbackContext = pCallbackContext;
AckWaitList[indexAckWaitList].action = action;

0
aws_iot_src/shadow/aws_iot_shadow_records.h Executable file → Normal file
View File

0
aws_iot_src/utils/aws_iot_error.h Executable file → Normal file
View File

0
aws_iot_src/utils/aws_iot_json_utils.c Executable file → Normal file
View File

0
aws_iot_src/utils/aws_iot_json_utils.h Executable file → Normal file
View File

0
aws_iot_src/utils/aws_iot_log.h Executable file → Normal file
View File

2
aws_iot_src/utils/aws_iot_version.h Executable file → Normal file
View File

@@ -39,7 +39,7 @@
/**
* @brief PATCH version when backwards-compatible bug fixes are made.
*/
#define VERSION_PATCH 0
#define VERSION_PATCH 1
/**
* @brief TAG is an (optional) tag appended to the version if a more descriptive verion is needed.
*/

0
aws_iot_src/utils/jsmn.c Executable file → Normal file
View File

0
aws_iot_src/utils/jsmn.h Executable file → Normal file
View File

View File

@@ -30,13 +30,13 @@ uint16_t getNextPacketId(Client *c) {
}
MQTTReturnCode sendPacket(Client *c, uint32_t length, Timer *timer) {
int32_t sentLen = 0;
uint32_t sent = 0;
if(NULL == c || NULL == timer) {
return MQTT_NULL_VALUE_ERROR;
}
int32_t sentLen = 0;
uint32_t sent = 0;
if(length >= c->bufSize) {
return MQTTPACKET_BUFFER_TOO_SHORT;
}
@@ -81,14 +81,14 @@ MQTTReturnCode MQTTClient(Client *c, uint32_t commandTimeoutMs,
size_t readBufSize, uint8_t enableAutoReconnect,
networkInitHandler_t networkInitHandler,
TLSConnectParams *tlsConnectParams) {
uint32_t i;
MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
if(NULL == c || NULL == tlsConnectParams || NULL == buf || NULL == readbuf
|| NULL == networkInitHandler) {
return MQTT_NULL_VALUE_ERROR;
}
uint32_t i;
MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
for(i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
c->messageHandlers[i].topicFilter = NULL;
c->messageHandlers[i].fp = NULL;
@@ -111,7 +111,6 @@ MQTTReturnCode MQTTClient(Client *c, uint32_t commandTimeoutMs,
copyMQTTConnectData(&(c->options), &default_options);
c->networkInitHandler = networkInitHandler;
c->tlsConnectParams.DestinationPort = tlsConnectParams->DestinationPort;
c->tlsConnectParams.pDestinationURL = tlsConnectParams->pDestinationURL;
c->tlsConnectParams.pDeviceCertLocation = tlsConnectParams->pDeviceCertLocation;
@@ -127,15 +126,15 @@ MQTTReturnCode MQTTClient(Client *c, uint32_t commandTimeoutMs,
}
MQTTReturnCode decodePacket(Client *c, uint32_t *value, uint32_t timeout) {
if(NULL == c || NULL == value) {
return MQTT_NULL_VALUE_ERROR;
}
unsigned char i;
uint32_t multiplier = 1;
uint32_t len = 0;
const uint32_t MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
if(NULL == c || NULL == value) {
return MQTT_NULL_VALUE_ERROR;
}
*value = 0;
do {
@@ -160,16 +159,17 @@ MQTTReturnCode decodePacket(Client *c, uint32_t *value, uint32_t timeout) {
}
MQTTReturnCode readPacket(Client *c, Timer *timer, uint8_t *packet_type) {
if(NULL == c || NULL == timer) {
return MQTT_NULL_VALUE_ERROR;
}
MQTTHeader header = {0};
uint32_t len = 0;
uint32_t rem_len = 0;
uint32_t total_bytes_read = 0;
uint32_t bytes_to_be_read = 0;
int32_t ret_val = 0;
int32_t ret_val = 0;
MQTTReturnCode rc;
if(NULL == c || NULL == timer) {
return MQTT_NULL_VALUE_ERROR;
}
/* 1. read the header byte. This has the packet type in it */
if(1 != c->networkStack.mqttread(&(c->networkStack), c->readbuf, 1, left_ms(timer))) {
@@ -182,7 +182,7 @@ MQTTReturnCode readPacket(Client *c, Timer *timer, uint8_t *packet_type) {
len = 1;
/* 2. read the remaining length. This is variable in itself */
MQTTReturnCode rc = decodePacket(c, &rem_len, (uint32_t)left_ms(timer));
rc = decodePacket(c, &rem_len, (uint32_t)left_ms(timer));
if(SUCCESS != rc) {
return rc;
}
@@ -223,13 +223,17 @@ MQTTReturnCode readPacket(Client *c, Timer *timer, uint8_t *packet_type) {
// # can only be at end
// + and # can only be next to separator
char isTopicMatched(char *topicFilter, MQTTString *topicName) {
char *curf = NULL;
char *curn = NULL;
char *curn_end = NULL;
if(NULL == topicFilter || NULL == topicName) {
return MQTT_NULL_VALUE_ERROR;
}
char *curf = topicFilter;
char *curn = topicName->lenstring.data;
char *curn_end = curn + topicName->lenstring.len;
curf = topicFilter;
curn = topicName->lenstring.data;
curn_end = curn + topicName->lenstring.len;
while(*curf && (curn < curn_end)) {
if(*curn == '/' && *curf != '/') {
@@ -256,13 +260,13 @@ char isTopicMatched(char *topicFilter, MQTTString *topicName) {
}
MQTTReturnCode deliverMessage(Client *c, MQTTString *topicName, MQTTMessage *message) {
uint32_t i;
MessageData md;
if(NULL == c || NULL == topicName || NULL == message) {
return MQTT_NULL_VALUE_ERROR;
}
uint32_t i;
MessageData md;
// we have to find the right message handler - indexed by topic
for(i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
if((c->messageHandlers[i].topicFilter != 0)
@@ -287,11 +291,13 @@ MQTTReturnCode deliverMessage(Client *c, MQTTString *topicName, MQTTMessage *mes
}
MQTTReturnCode handleDisconnect(Client *c) {
MQTTReturnCode rc;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
MQTTReturnCode rc = MQTTDisconnect(c);
rc = MQTTDisconnect(c);
if(rc != SUCCESS){
// If the sendPacket prevents us from sending a disconnect packet then we have to clean the stack
MQTTForceDisconnect(c);
@@ -307,6 +313,8 @@ MQTTReturnCode handleDisconnect(Client *c) {
}
MQTTReturnCode MQTTAttemptReconnect(Client *c) {
MQTTReturnCode rc = MQTT_ATTEMPTING_RECONNECT;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
@@ -315,8 +323,6 @@ MQTTReturnCode MQTTAttemptReconnect(Client *c) {
return MQTT_NETWORK_ALREADY_CONNECTED_ERROR;
}
MQTTReturnCode rc = MQTT_ATTEMPTING_RECONNECT;
/* Ignoring return code. failures expected if network is disconnected */
rc = MQTTConnect(c, NULL);
@@ -334,6 +340,9 @@ MQTTReturnCode MQTTAttemptReconnect(Client *c) {
}
MQTTReturnCode handleReconnect(Client *c) {
int8_t isPhysicalLayerConnected = 1;
MQTTReturnCode rc = MQTT_NETWORK_RECONNECTED;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
@@ -344,12 +353,10 @@ MQTTReturnCode handleReconnect(Client *c) {
return MQTT_ATTEMPTING_RECONNECT;
}
int8_t isPhysicalLayerConnected = 1;
if(NULL != c->networkStack.isConnected) {
isPhysicalLayerConnected = (int8_t)c->networkStack.isConnected(&(c->networkStack));
}
MQTTReturnCode rc = MQTT_NETWORK_RECONNECTED;
if(isPhysicalLayerConnected) {
rc = MQTTAttemptReconnect(c);
if(MQTT_NETWORK_RECONNECTED == rc) {
@@ -367,6 +374,10 @@ MQTTReturnCode handleReconnect(Client *c) {
}
MQTTReturnCode keepalive(Client *c) {
MQTTReturnCode rc = SUCCESS;
Timer timer;
uint32_t serialized_len = 0;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
@@ -384,11 +395,8 @@ MQTTReturnCode keepalive(Client *c) {
}
/* there is no ping outstanding - send one */
MQTTReturnCode rc = SUCCESS;
Timer timer;
InitTimer(&timer);
countdown_ms(&timer, c->commandTimeoutMs);
uint32_t serialized_len = 0;
rc = MQTTSerialize_pingreq(c->buf, c->bufSize, &serialized_len);
if(SUCCESS != rc) {
return rc;
@@ -454,12 +462,13 @@ MQTTReturnCode handlePubrec(Client *c, Timer *timer) {
uint16_t packet_id;
unsigned char dup, type;
MQTTReturnCode rc;
uint32_t len;
rc = MQTTDeserialize_ack(&type, &dup, &packet_id, c->readbuf, c->readBufSize);
if(SUCCESS != rc) {
return rc;
}
uint32_t len;
rc = MQTTSerialize_ack(c->buf, c->bufSize, PUBREL, 0, packet_id, &len);
if(SUCCESS != rc) {
return rc;
@@ -476,12 +485,13 @@ MQTTReturnCode handlePubrec(Client *c, Timer *timer) {
}
MQTTReturnCode cycle(Client *c, Timer *timer, uint8_t *packet_type) {
MQTTReturnCode rc;
if(NULL == c || NULL == timer) {
return MQTT_NULL_VALUE_ERROR;
}
/* read the socket, see what work is due */
MQTTReturnCode rc = readPacket(c, timer, packet_type);
rc = readPacket(c, timer, packet_type);
if(MQTT_NOTHING_TO_READ == rc) {
/* Nothing to read, not a cycle failure */
return SUCCESS;
@@ -523,6 +533,10 @@ MQTTReturnCode cycle(Client *c, Timer *timer, uint8_t *packet_type) {
}
MQTTReturnCode MQTTYield(Client *c, uint32_t timeout_ms) {
MQTTReturnCode rc = SUCCESS;
Timer timer;
uint8_t packet_type;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
@@ -537,10 +551,7 @@ MQTTReturnCode MQTTYield(Client *c, uint32_t timeout_ms) {
return MQTT_NETWORK_DISCONNECTED_ERROR;
}
MQTTReturnCode rc = SUCCESS;
Timer timer;
InitTimer(&timer);
uint8_t packet_type;
countdown_ms(&timer, timeout_ms);
while(!expired(&timer)) {
@@ -579,12 +590,12 @@ MQTTReturnCode MQTTYield(Client *c, uint32_t timeout_ms) {
/* only used in single-threaded mode where one command at a time is in process */
MQTTReturnCode waitfor(Client *c, uint8_t packet_type, Timer *timer) {
MQTTReturnCode rc = FAILURE;
uint8_t read_packet_type = 0;
if(NULL == c || NULL == timer) {
return MQTT_NULL_VALUE_ERROR;
}
MQTTReturnCode rc = FAILURE;
uint8_t read_packet_type = 0;
do {
if(expired(timer)) {
/* we timed out */
@@ -602,16 +613,16 @@ MQTTReturnCode waitfor(Client *c, uint8_t packet_type, Timer *timer) {
}
MQTTReturnCode MQTTConnect(Client *c, MQTTPacket_connectData *options) {
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
Timer connect_timer;
MQTTReturnCode connack_rc = FAILURE;
char sessionPresent = 0;
uint32_t len = 0;
MQTTReturnCode rc = FAILURE;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
InitTimer(&connect_timer);
countdown_ms(&connect_timer, c->commandTimeoutMs);
@@ -682,6 +693,15 @@ uint32_t GetFreeMessageHandlerIndex(Client *c) {
MQTTReturnCode MQTTSubscribe(Client *c, const char *topicFilter, QoS qos,
messageHandler messageHandler, pApplicationHandler_t applicationHandler) {
MQTTReturnCode rc = FAILURE;
Timer timer;
uint32_t len = 0;
uint32_t indexOfFreeMessageHandler;
uint32_t count = 0;
QoS grantedQoS[3] = {QOS0, QOS0, QOS0};
uint16_t packetId;
MQTTString topic = MQTTString_initializer;
if(NULL == c || NULL == topicFilter
|| NULL == messageHandler || NULL == applicationHandler) {
return MQTT_NULL_VALUE_ERROR;
@@ -691,15 +711,6 @@ MQTTReturnCode MQTTSubscribe(Client *c, const char *topicFilter, QoS qos,
return MQTT_NETWORK_DISCONNECTED_ERROR;
}
MQTTReturnCode rc = FAILURE;
Timer timer;
uint32_t len = 0;
uint32_t indexOfFreeMessageHandler;
uint32_t count = 0;
QoS grantedQoS[3] = {QOS0, QOS0, QOS0};
uint16_t packetId;
MQTTString topic = MQTTString_initializer;
topic.cstring = (char *)topicFilter;
InitTimer(&timer);
@@ -744,6 +755,15 @@ MQTTReturnCode MQTTSubscribe(Client *c, const char *topicFilter, QoS qos,
}
MQTTReturnCode MQTTResubscribe(Client *c) {
MQTTReturnCode rc = FAILURE;
Timer timer;
uint32_t len = 0;
uint32_t count = 0;
QoS grantedQoS[3] = {QOS0, QOS0, QOS0};
uint16_t packetId;
uint32_t existingSubCount = 0;
uint32_t itr = 0;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
@@ -752,14 +772,7 @@ MQTTReturnCode MQTTResubscribe(Client *c) {
return MQTT_NETWORK_DISCONNECTED_ERROR;
}
MQTTReturnCode rc = FAILURE;
Timer timer;
uint32_t len = 0;
uint32_t count = 0;
QoS grantedQoS[3] = {QOS0, QOS0, QOS0};
uint16_t packetId;
uint32_t existingSubCount = GetFreeMessageHandlerIndex(c);
uint32_t itr = 0;
existingSubCount = GetFreeMessageHandlerIndex(c);
for(itr = 0; itr < existingSubCount; itr++) {
MQTTString topic = MQTTString_initializer;
@@ -797,21 +810,23 @@ MQTTReturnCode MQTTResubscribe(Client *c) {
}
MQTTReturnCode MQTTUnsubscribe(Client *c, const char *topicFilter) {
MQTTReturnCode rc = FAILURE;
Timer timer;
MQTTString topic = MQTTString_initializer;
uint32_t len = 0;
uint32_t i = 0;
uint16_t packet_id;
if(NULL == c || NULL == topicFilter) {
return MQTT_NULL_VALUE_ERROR;
}
topic.cstring = (char *)topicFilter;
if(!c->isConnected) {
return MQTT_NETWORK_DISCONNECTED_ERROR;
}
MQTTReturnCode rc = FAILURE;
Timer timer;
MQTTString topic = MQTTString_initializer;
topic.cstring = (char *)topicFilter;
uint32_t len = 0;
uint32_t i = 0;
InitTimer(&timer);
countdown_ms(&timer, c->commandTimeoutMs);
@@ -831,7 +846,6 @@ MQTTReturnCode MQTTUnsubscribe(Client *c, const char *topicFilter) {
return rc;
}
uint16_t packet_id;
rc = MQTTDeserialize_unsuback(&packet_id, c->readbuf, c->readBufSize);
if(SUCCESS != rc) {
return rc;
@@ -851,17 +865,8 @@ MQTTReturnCode MQTTUnsubscribe(Client *c, const char *topicFilter) {
}
MQTTReturnCode MQTTPublish(Client *c, const char *topicName, MQTTMessage *message) {
if(NULL == c || NULL == topicName || NULL == message) {
return MQTT_NULL_VALUE_ERROR;
}
if(!c->isConnected) {
return MQTT_NETWORK_DISCONNECTED_ERROR;
}
Timer timer;
MQTTString topic = MQTTString_initializer;
topic.cstring = (char *)topicName;
uint32_t len = 0;
uint8_t waitForAck = 0;
uint8_t packetType = PUBACK;
@@ -869,6 +874,16 @@ MQTTReturnCode MQTTPublish(Client *c, const char *topicName, MQTTMessage *messag
unsigned char dup, type;
MQTTReturnCode rc = FAILURE;
if(NULL == c || NULL == topicName || NULL == message) {
return MQTT_NULL_VALUE_ERROR;
}
topic.cstring = (char *)topicName;
if(!c->isConnected) {
return MQTT_NETWORK_DISCONNECTED_ERROR;
}
InitTimer(&timer);
countdown_ms(&timer, c->commandTimeoutMs);
@@ -917,6 +932,11 @@ static void MQTTForceDisconnect(Client *c){
}
MQTTReturnCode MQTTDisconnect(Client *c) {
MQTTReturnCode rc = FAILURE;
/* We might wait for incomplete incoming publishes to complete */
Timer timer;
uint32_t serialized_len = 0;
if(NULL == c) {
return MQTT_NULL_VALUE_ERROR;
}
@@ -926,10 +946,6 @@ MQTTReturnCode MQTTDisconnect(Client *c) {
return MQTT_NETWORK_DISCONNECTED_ERROR;
}
MQTTReturnCode rc = FAILURE;
/* We might wait for incomplete incoming publishes to complete */
Timer timer;
uint32_t serialized_len = 0;
rc = MQTTSerialize_disconnect(c->buf, c->bufSize, &serialized_len);
if(SUCCESS != rc) {
return rc;

View File

@@ -26,9 +26,9 @@
* @return MQTTReturnCode indicating function execution status
*/
size_t MQTTSerialize_GetConnectLength(MQTTPacket_connectData *options) {
size_t len = 0;
FUNC_ENTRY;
size_t len = 0;
/* variable depending on MQTT or MQIsdp */
if(3 == options->MQTTVersion) {
len = 12;
@@ -65,24 +65,24 @@ size_t MQTTSerialize_GetConnectLength(MQTTPacket_connectData *options) {
MQTTReturnCode MQTTSerialize_connect(unsigned char *buf, size_t buflen,
MQTTPacket_connectData *options,
uint32_t *serialized_len) {
unsigned char *ptr = buf;
MQTTHeader header = {0};
MQTTConnectFlags flags = {0};
size_t len = 0;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, CONNECT, QOS0, 0, 0);
FUNC_ENTRY;
if(NULL == buf || NULL == options || NULL == serialized_len) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
return MQTT_NULL_VALUE_ERROR;
}
unsigned char *ptr = buf;
MQTTHeader header = {0};
MQTTConnectFlags flags = {0};
size_t len = 0;
len = MQTTSerialize_GetConnectLength(options);
if(MQTTPacket_len(len) > buflen) {
FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, CONNECT, QOS0, 0, 0);
if(SUCCESS != rc) {
FUNC_EXIT_RC(rc);
return rc;
@@ -149,6 +149,14 @@ MQTTReturnCode MQTTSerialize_connect(unsigned char *buf, size_t buflen,
MQTTReturnCode MQTTDeserialize_connack(unsigned char *sessionPresent,
MQTTReturnCode *connack_rc,
unsigned char *buf, size_t buflen) {
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
MQTTReturnCode rc = FAILURE;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
MQTTConnackFlags flags = {0};
unsigned char connack_rc_char;
FUNC_ENTRY;
if(NULL == sessionPresent || NULL == connack_rc || NULL == buf) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
@@ -163,14 +171,6 @@ MQTTReturnCode MQTTDeserialize_connack(unsigned char *sessionPresent,
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
MQTTReturnCode rc = FAILURE;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
MQTTConnackFlags flags = {0};
header.byte = readChar(&curdata);
if(CONNACK != header.bits.type) {
FUNC_EXIT_RC(FAILURE);
@@ -193,7 +193,8 @@ MQTTReturnCode MQTTDeserialize_connack(unsigned char *sessionPresent,
flags.all = readChar(&curdata);
*sessionPresent = flags.bits.sessionpresent;
unsigned char connack_rc_char = readChar(&curdata);
connack_rc_char = readChar(&curdata);
switch(connack_rc_char) {
case CONNACK_CONNECTION_ACCEPTED:
*connack_rc = MQTT_CONNACK_CONNECTION_ACCEPTED;
@@ -233,6 +234,10 @@ MQTTReturnCode MQTTDeserialize_connack(unsigned char *sessionPresent,
MQTTReturnCode MQTTSerialize_zero(unsigned char *buf, size_t buflen,
unsigned char packetType,
uint32_t *serialized_length) {
MQTTHeader header = {0};
unsigned char *ptr = buf;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, packetType, QOS0, 0, 0);
FUNC_ENTRY;
if(NULL == buf || NULL == serialized_length) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
@@ -245,10 +250,6 @@ MQTTReturnCode MQTTSerialize_zero(unsigned char *buf, size_t buflen,
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTHeader header = {0};
unsigned char *ptr = buf;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, packetType, QOS0, 0, 0);
if(SUCCESS != rc) {
FUNC_EXIT_RC(rc);
return rc;

View File

@@ -35,6 +35,13 @@ MQTTReturnCode MQTTDeserialize_publish(unsigned char *dup, QoS *qos,
unsigned char *retained, uint16_t *packetid,
MQTTString* topicName, unsigned char **payload,
uint32_t *payloadlen, unsigned char *buf, size_t buflen) {
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
MQTTReturnCode rc = FAILURE;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
FUNC_ENTRY;
if(NULL == dup || NULL == qos || NULL == retained || NULL == packetid) {
FUNC_EXIT_RC(FAILURE);
@@ -52,13 +59,6 @@ MQTTReturnCode MQTTDeserialize_publish(unsigned char *dup, QoS *qos,
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
MQTTReturnCode rc = FAILURE;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
header.byte = readChar(&curdata);
if(PUBLISH != header.bits.type) {
FUNC_EXIT_RC(FAILURE);
@@ -107,6 +107,12 @@ MQTTReturnCode MQTTDeserialize_publish(unsigned char *dup, QoS *qos,
MQTTReturnCode MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup,
uint16_t *packetid, unsigned char *buf,
size_t buflen) {
MQTTReturnCode rc = FAILURE;
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
FUNC_ENTRY;
if(NULL == packettype || NULL == dup || NULL == packetid || NULL == buf) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
@@ -119,13 +125,6 @@ MQTTReturnCode MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTReturnCode rc = FAILURE;
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
header.byte = readChar(&curdata);
*dup = header.bits.dup;
*packettype = header.bits.type;
@@ -148,4 +147,5 @@ MQTTReturnCode MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup
FUNC_EXIT_RC(SUCCESS);
return SUCCESS;
}
}

View File

@@ -56,23 +56,23 @@ MQTTReturnCode MQTTSerialize_publish(unsigned char *buf, size_t buflen, uint8_t
QoS qos, uint8_t retained, uint16_t packetid,
MQTTString topicName, unsigned char *payload, size_t payloadlen,
uint32_t *serialized_len) {
unsigned char *ptr = buf;
MQTTHeader header = {0};
size_t rem_len = 0;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, PUBLISH, qos, dup, retained);
FUNC_ENTRY;
if(NULL == buf || NULL == payload || NULL == serialized_len) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
return MQTT_NULL_VALUE_ERROR;
}
unsigned char *ptr = buf;
MQTTHeader header = {0};
size_t rem_len = 0;
rem_len = MQTTSerialize_GetPublishLength(qos, topicName, payloadlen);
if(MQTTPacket_len(rem_len) > buflen) {
FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, PUBLISH, qos, dup, retained);
if(SUCCESS != rc) {
FUNC_EXIT_RC(rc);
return rc;
@@ -108,15 +108,17 @@ MQTTReturnCode MQTTSerialize_publish(unsigned char *buf, size_t buflen, uint8_t
MQTTReturnCode MQTTSerialize_ack(unsigned char *buf, size_t buflen,
unsigned char type, uint8_t dup, uint16_t packetid,
uint32_t *serialized_len) {
MQTTHeader header = {0};
unsigned char *ptr = buf;
QoS requestQoS = (PUBREL == type) ? QOS1 : QOS0;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, type, requestQoS, dup, 0);
FUNC_ENTRY;
if(NULL == buf || serialized_len == NULL) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
return MQTT_NULL_VALUE_ERROR;
}
MQTTHeader header = {0};
unsigned char *ptr = buf;
/* Minimum byte length required by ACK headers is
* 2 for fixed and 2 for variable part */
if(4 > buflen) {
@@ -124,8 +126,6 @@ MQTTReturnCode MQTTSerialize_ack(unsigned char *buf, size_t buflen,
return MQTTPACKET_BUFFER_TOO_SHORT;
}
QoS requestQoS = (PUBREL == type) ? QOS1 : QOS0;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, type, requestQoS, dup, 0);
if(SUCCESS != rc) {
FUNC_EXIT_RC(rc);
return rc;

View File

@@ -51,23 +51,22 @@ MQTTReturnCode MQTTSerialize_subscribe(unsigned char *buf, size_t buflen,
unsigned char dup, uint16_t packetid, uint32_t count,
MQTTString topicFilters[], QoS requestedQoSs[],
uint32_t *serialized_len) {
unsigned char *ptr = buf;
MQTTHeader header = {0};
size_t rem_len = 0;
uint32_t i = 0;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, SUBSCRIBE, 1, dup, 0);
FUNC_ENTRY;
if(NULL == buf || NULL == serialized_len) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
return MQTT_NULL_VALUE_ERROR;
}
unsigned char *ptr = buf;
MQTTHeader header = {0};
size_t rem_len = 0;
uint32_t i = 0;
if(MQTTPacket_len(rem_len = MQTTSerialize_GetSubscribePacketLength(count, topicFilters)) > buflen) {
FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, SUBSCRIBE, 1, dup, 0);
if(SUCCESS != rc) {
FUNC_EXIT_RC(rc);
return rc;
@@ -104,19 +103,19 @@ MQTTReturnCode MQTTSerialize_subscribe(unsigned char *buf, size_t buflen,
MQTTReturnCode MQTTDeserialize_suback(uint16_t *packetid, uint32_t maxcount,
uint32_t *count, QoS grantedQoSs[],
unsigned char *buf, size_t buflen) {
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
MQTTReturnCode decodeRc = FAILURE;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
FUNC_ENTRY;
if(NULL == packetid || NULL == count || NULL == grantedQoSs) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
return MQTT_NULL_VALUE_ERROR;
}
MQTTHeader header = {0};
unsigned char *curdata = buf;
unsigned char *enddata = NULL;
MQTTReturnCode decodeRc = FAILURE;
uint32_t decodedLen = 0;
uint32_t readBytesLen = 0;
/* SUBACK header size is 4 bytes for header and at least one byte for QoS payload
* Need at least a 5 bytes buffer. MQTT3.1.1 specification 3.9
*/

View File

@@ -51,24 +51,24 @@ MQTTReturnCode MQTTSerialize_unsubscribe(unsigned char* buf, size_t buflen,
uint8_t dup, uint16_t packetid,
uint32_t count, MQTTString topicFilters[],
uint32_t *serialized_len) {
unsigned char *ptr = buf;
MQTTHeader header = {0};
size_t rem_len = 0;
uint32_t i = 0;
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, UNSUBSCRIBE, 1, dup, 0);
FUNC_ENTRY;
if(NULL == buf || NULL == serialized_len) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
return MQTT_NULL_VALUE_ERROR;
}
unsigned char *ptr = buf;
MQTTHeader header = {0};
size_t rem_len = 0;
uint32_t i = 0;
rem_len = MQTTSerialize_GetUnsubscribePacketLength(count, topicFilters);
if(MQTTPacket_len(rem_len) > buflen) {
FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
return MQTTPACKET_BUFFER_TOO_SHORT;
}
MQTTReturnCode rc = MQTTPacket_InitHeader(&header, UNSUBSCRIBE, 1, dup, 0);
if(SUCCESS != rc) {
FUNC_EXIT_RC(rc);
return rc;
@@ -98,16 +98,16 @@ MQTTReturnCode MQTTSerialize_unsubscribe(unsigned char* buf, size_t buflen,
* @return MQTTReturnCode indicating function execution status
*/
MQTTReturnCode MQTTDeserialize_unsuback(uint16_t *packetid, unsigned char *buf, size_t buflen) {
unsigned char type = 0;
unsigned char dup = 0;
MQTTReturnCode rc = FAILURE;
FUNC_ENTRY;
if(NULL == packetid || NULL == buf) {
FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
return MQTT_NULL_VALUE_ERROR;
}
unsigned char type = 0;
unsigned char dup = 0;
MQTTReturnCode rc = FAILURE;
rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
if(SUCCESS == rc && UNSUBACK != type) {
rc = FAILURE;

View File

View File

0
sample_apps/shadow_sample/aws_iot_config.h Executable file → Normal file
View File

0
sample_apps/shadow_sample/shadow_sample.c Executable file → Normal file
View File

View File

View File

View File

View File

View File

View File

0
sample_apps/subscribe_publish_sample/aws_iot_config.h Executable file → Normal file
View File

View File