AT&T IoT / Mbed OS ATT_AWS_IoT_demo Featured

Dependencies:   SDFileSystem

Fork of ATT_AWS_IoT_demo by Anthony Phillips

IoT Starter Kit Powered by AWS Demo

This program demonstrates the AT&T IoT Starter Kit sending data directly into AWS IoT. It's explained and used in the Getting Started with the IoT Starter Kit Powered by AWS on starterkit.att.com.

What's required

  • AT&T IoT LTE Add-on (also known as the Cellular Shield)
  • NXP K64F - for programming
  • microSD card - used to store your AWS security credentials
  • AWS account
  • Python, locally installed

If you don't already have an IoT Starter Kit, you can purchase a kit here. The IoT Starter Kit Powered by AWS includes the LTE cellular shield, K64F, and a microSD card.

Committer:
ampembeng
Date:
Thu Dec 01 18:05:38 2016 +0000
Revision:
15:6f2798e45099
Initial commit.  Demo works with both the FRDM wired Ethernet and the Avnet Shield wireless modem.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ampembeng 15:6f2798e45099 1 /*******************************************************************************
ampembeng 15:6f2798e45099 2 * Copyright (c) 2014 IBM Corp.
ampembeng 15:6f2798e45099 3 *
ampembeng 15:6f2798e45099 4 * All rights reserved. This program and the accompanying materials
ampembeng 15:6f2798e45099 5 * are made available under the terms of the Eclipse Public License v1.0
ampembeng 15:6f2798e45099 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
ampembeng 15:6f2798e45099 7 *
ampembeng 15:6f2798e45099 8 * The Eclipse Public License is available at
ampembeng 15:6f2798e45099 9 * http://www.eclipse.org/legal/epl-v10.html
ampembeng 15:6f2798e45099 10 * and the Eclipse Distribution License is available at
ampembeng 15:6f2798e45099 11 * http://www.eclipse.org/org/documents/edl-v10.php.
ampembeng 15:6f2798e45099 12 *
ampembeng 15:6f2798e45099 13 * Contributors:
ampembeng 15:6f2798e45099 14 * Ian Craggs - initial API and implementation and/or initial documentation
ampembeng 15:6f2798e45099 15 *******************************************************************************/
ampembeng 15:6f2798e45099 16
ampembeng 15:6f2798e45099 17 #include "MQTTPacket.h"
ampembeng 15:6f2798e45099 18 #include "StackTrace.h"
ampembeng 15:6f2798e45099 19
ampembeng 15:6f2798e45099 20 #include <string.h>
ampembeng 15:6f2798e45099 21
ampembeng 15:6f2798e45099 22 /**
ampembeng 15:6f2798e45099 23 * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
ampembeng 15:6f2798e45099 24 * @param count the number of topic filter strings in topicFilters
ampembeng 15:6f2798e45099 25 * @param topicFilters the array of topic filter strings to be used in the publish
ampembeng 15:6f2798e45099 26 * @return the length of buffer needed to contain the serialized version of the packet
ampembeng 15:6f2798e45099 27 */
ampembeng 15:6f2798e45099 28 size_t MQTTSerialize_GetSubscribePacketLength(uint32_t count, MQTTString topicFilters[]) {
ampembeng 15:6f2798e45099 29 size_t i;
ampembeng 15:6f2798e45099 30 size_t len = 2; /* packetid */
ampembeng 15:6f2798e45099 31
ampembeng 15:6f2798e45099 32 for(i = 0; i < count; ++i) {
ampembeng 15:6f2798e45099 33 len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
ampembeng 15:6f2798e45099 34 }
ampembeng 15:6f2798e45099 35
ampembeng 15:6f2798e45099 36 return len;
ampembeng 15:6f2798e45099 37 }
ampembeng 15:6f2798e45099 38
ampembeng 15:6f2798e45099 39 /**
ampembeng 15:6f2798e45099 40 * Serializes the supplied subscribe data into the supplied buffer, ready for sending
ampembeng 15:6f2798e45099 41 * @param buf the buffer into which the packet will be serialized
ampembeng 15:6f2798e45099 42 * @param buflen the length in bytes of the supplied bufferr
ampembeng 15:6f2798e45099 43 * @param dup integer - the MQTT dup flag
ampembeng 15:6f2798e45099 44 * @param packetid integer - the MQTT packet identifier
ampembeng 15:6f2798e45099 45 * @param count - number of members in the topicFilters and reqQos arrays
ampembeng 15:6f2798e45099 46 * @param topicFilters - array of topic filter names
ampembeng 15:6f2798e45099 47 * @param requestedQoSs - array of requested QoS
ampembeng 15:6f2798e45099 48 * @return the length of the serialized data. <= 0 indicates error
ampembeng 15:6f2798e45099 49 */
ampembeng 15:6f2798e45099 50 MQTTReturnCode MQTTSerialize_subscribe(unsigned char *buf, size_t buflen,
ampembeng 15:6f2798e45099 51 unsigned char dup, uint16_t packetid, uint32_t count,
ampembeng 15:6f2798e45099 52 MQTTString topicFilters[], QoS requestedQoSs[],
ampembeng 15:6f2798e45099 53 uint32_t *serialized_len) {
ampembeng 15:6f2798e45099 54 unsigned char *ptr = buf;
ampembeng 15:6f2798e45099 55 MQTTHeader header = {0};
ampembeng 15:6f2798e45099 56 size_t rem_len = 0;
ampembeng 15:6f2798e45099 57 uint32_t i = 0;
ampembeng 15:6f2798e45099 58 MQTTReturnCode rc = MQTTPacket_InitHeader(&header, SUBSCRIBE, (QoS)1, dup, 0);
ampembeng 15:6f2798e45099 59 FUNC_ENTRY;
ampembeng 15:6f2798e45099 60 if(NULL == buf || NULL == serialized_len) {
ampembeng 15:6f2798e45099 61 FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
ampembeng 15:6f2798e45099 62 return MQTT_NULL_VALUE_ERROR;
ampembeng 15:6f2798e45099 63 }
ampembeng 15:6f2798e45099 64
ampembeng 15:6f2798e45099 65 if(MQTTPacket_len(rem_len = MQTTSerialize_GetSubscribePacketLength(count, topicFilters)) > buflen) {
ampembeng 15:6f2798e45099 66 FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
ampembeng 15:6f2798e45099 67 return MQTTPACKET_BUFFER_TOO_SHORT;
ampembeng 15:6f2798e45099 68 }
ampembeng 15:6f2798e45099 69
ampembeng 15:6f2798e45099 70 if(SUCCESS != rc) {
ampembeng 15:6f2798e45099 71 FUNC_EXIT_RC(rc);
ampembeng 15:6f2798e45099 72 return rc;
ampembeng 15:6f2798e45099 73 }
ampembeng 15:6f2798e45099 74 /* write header */
ampembeng 15:6f2798e45099 75 writeChar(&ptr, header.byte);
ampembeng 15:6f2798e45099 76
ampembeng 15:6f2798e45099 77 /* write remaining length */
ampembeng 15:6f2798e45099 78 ptr += MQTTPacket_encode(ptr, rem_len);
ampembeng 15:6f2798e45099 79
ampembeng 15:6f2798e45099 80 writePacketId(&ptr, packetid);
ampembeng 15:6f2798e45099 81
ampembeng 15:6f2798e45099 82 for(i = 0; i < count; ++i) {
ampembeng 15:6f2798e45099 83 writeMQTTString(&ptr, topicFilters[i]);
ampembeng 15:6f2798e45099 84 writeChar(&ptr, (unsigned char)requestedQoSs[i]);
ampembeng 15:6f2798e45099 85 }
ampembeng 15:6f2798e45099 86
ampembeng 15:6f2798e45099 87 *serialized_len = (uint32_t)(ptr - buf);
ampembeng 15:6f2798e45099 88
ampembeng 15:6f2798e45099 89 FUNC_EXIT_RC(SUCCESS);
ampembeng 15:6f2798e45099 90 return SUCCESS;
ampembeng 15:6f2798e45099 91 }
ampembeng 15:6f2798e45099 92
ampembeng 15:6f2798e45099 93 /**
ampembeng 15:6f2798e45099 94 * Deserializes the supplied (wire) buffer into suback data
ampembeng 15:6f2798e45099 95 * @param packetid returned integer - the MQTT packet identifier
ampembeng 15:6f2798e45099 96 * @param maxcount - the maximum number of members allowed in the grantedQoSs array
ampembeng 15:6f2798e45099 97 * @param count returned integer - number of members in the grantedQoSs array
ampembeng 15:6f2798e45099 98 * @param grantedQoSs returned array of integers - the granted qualities of service
ampembeng 15:6f2798e45099 99 * @param buf the raw buffer data, of the correct length determined by the remaining length field
ampembeng 15:6f2798e45099 100 * @param buflen the length in bytes of the data in the supplied buffer
ampembeng 15:6f2798e45099 101 * @return error code. 1 is success, 0 is failure
ampembeng 15:6f2798e45099 102 */
ampembeng 15:6f2798e45099 103 MQTTReturnCode MQTTDeserialize_suback(uint16_t *packetid, uint32_t maxcount,
ampembeng 15:6f2798e45099 104 uint32_t *count, QoS grantedQoSs[],
ampembeng 15:6f2798e45099 105 unsigned char *buf, size_t buflen) {
ampembeng 15:6f2798e45099 106 MQTTHeader header = {0};
ampembeng 15:6f2798e45099 107 unsigned char *curdata = buf;
ampembeng 15:6f2798e45099 108 unsigned char *enddata = NULL;
ampembeng 15:6f2798e45099 109 MQTTReturnCode decodeRc = FAILURE;
ampembeng 15:6f2798e45099 110 uint32_t decodedLen = 0;
ampembeng 15:6f2798e45099 111 uint32_t readBytesLen = 0;
ampembeng 15:6f2798e45099 112
ampembeng 15:6f2798e45099 113 FUNC_ENTRY;
ampembeng 15:6f2798e45099 114 if(NULL == packetid || NULL == count || NULL == grantedQoSs) {
ampembeng 15:6f2798e45099 115 FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
ampembeng 15:6f2798e45099 116 return MQTT_NULL_VALUE_ERROR;
ampembeng 15:6f2798e45099 117 }
ampembeng 15:6f2798e45099 118
ampembeng 15:6f2798e45099 119 /* SUBACK header size is 4 bytes for header and at least one byte for QoS payload
ampembeng 15:6f2798e45099 120 * Need at least a 5 bytes buffer. MQTT3.1.1 specification 3.9
ampembeng 15:6f2798e45099 121 */
ampembeng 15:6f2798e45099 122 if(5 > buflen) {
ampembeng 15:6f2798e45099 123 FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
ampembeng 15:6f2798e45099 124 return MQTTPACKET_BUFFER_TOO_SHORT;
ampembeng 15:6f2798e45099 125 }
ampembeng 15:6f2798e45099 126
ampembeng 15:6f2798e45099 127 header.byte = readChar(&curdata);
ampembeng 15:6f2798e45099 128 if (header.bits.type != SUBACK) {
ampembeng 15:6f2798e45099 129 FUNC_EXIT_RC(FAILURE);
ampembeng 15:6f2798e45099 130 return FAILURE;
ampembeng 15:6f2798e45099 131 }
ampembeng 15:6f2798e45099 132
ampembeng 15:6f2798e45099 133 /* read remaining length */
ampembeng 15:6f2798e45099 134 decodeRc = MQTTPacket_decodeBuf(curdata, &decodedLen, &readBytesLen);
ampembeng 15:6f2798e45099 135 if(decodeRc != SUCCESS) {
ampembeng 15:6f2798e45099 136 return decodeRc;
ampembeng 15:6f2798e45099 137 }
ampembeng 15:6f2798e45099 138
ampembeng 15:6f2798e45099 139 curdata += (readBytesLen);
ampembeng 15:6f2798e45099 140 enddata = curdata + decodedLen;
ampembeng 15:6f2798e45099 141 if (enddata - curdata < 2) {
ampembeng 15:6f2798e45099 142 FUNC_EXIT_RC(FAILURE);
ampembeng 15:6f2798e45099 143 return FAILURE;
ampembeng 15:6f2798e45099 144 }
ampembeng 15:6f2798e45099 145
ampembeng 15:6f2798e45099 146 *packetid = readPacketId(&curdata);
ampembeng 15:6f2798e45099 147
ampembeng 15:6f2798e45099 148 *count = 0;
ampembeng 15:6f2798e45099 149 while(curdata < enddata) {
ampembeng 15:6f2798e45099 150 if(*count > maxcount) {
ampembeng 15:6f2798e45099 151 FUNC_EXIT_RC(FAILURE);
ampembeng 15:6f2798e45099 152 return FAILURE;
ampembeng 15:6f2798e45099 153 }
ampembeng 15:6f2798e45099 154 grantedQoSs[(*count)++] = (QoS)readChar(&curdata);
ampembeng 15:6f2798e45099 155 }
ampembeng 15:6f2798e45099 156
ampembeng 15:6f2798e45099 157 FUNC_EXIT_RC(SUCCESS);
ampembeng 15:6f2798e45099 158 return SUCCESS;
ampembeng 15:6f2798e45099 159 }
ampembeng 15:6f2798e45099 160
ampembeng 15:6f2798e45099 161