Demo application for using the AT&T IoT Starter Kit Powered by AWS.

Dependencies:   SDFileSystem

Fork of ATT_AWS_IoT_demo by Anthony Phillips

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTUnsubscribeClient.cpp Source File

MQTTUnsubscribeClient.cpp

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 #include "MQTTPacket.h"
00018 #include "StackTrace.h"
00019 
00020 #include <string.h>
00021 
00022 /**
00023   * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
00024   * @param count the number of topic filter strings in topicFilters
00025   * @param topicFilters the array of topic filter strings to be used in the publish
00026   * @return the length of buffer needed to contain the serialized version of the packet
00027   */
00028 size_t MQTTSerialize_GetUnsubscribePacketLength(uint32_t count, MQTTString topicFilters[]) {
00029     size_t i;
00030     size_t len = 2; /* packetid */
00031 
00032     for(i = 0; i < count; ++i) {
00033         len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
00034     }
00035 
00036     return len;
00037 }
00038 
00039 /**
00040   * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
00041   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00042   * @param buflen the length in bytes of the data in the supplied buffer
00043   * @param dup integer - the MQTT dup flag
00044   * @param packetid integer - the MQTT packet identifier
00045   * @param count - number of members in the topicFilters array
00046   * @param topicFilters - array of topic filter names
00047   * @param serialized_len - the length of the serialized data
00048   * @return MQTTReturnCode indicating function execution status
00049   */
00050 MQTTReturnCode MQTTSerialize_unsubscribe(unsigned char* buf, size_t buflen,
00051                                    uint8_t dup, uint16_t packetid,
00052                                    uint32_t count, MQTTString topicFilters[],
00053                                    uint32_t *serialized_len) {
00054         unsigned char *ptr = buf;
00055         MQTTHeader header = {0};
00056         size_t rem_len = 0;
00057         uint32_t i = 0;
00058         MQTTReturnCode rc = MQTTPacket_InitHeader(&header, UNSUBSCRIBE, (QoS)1, dup, 0);
00059 
00060     FUNC_ENTRY;
00061     if(NULL == buf || NULL == serialized_len) {
00062         FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
00063         return MQTT_NULL_VALUE_ERROR;
00064     }
00065 
00066     rem_len = MQTTSerialize_GetUnsubscribePacketLength(count, topicFilters);
00067     if(MQTTPacket_len(rem_len) > buflen) {
00068         FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
00069         return MQTTPACKET_BUFFER_TOO_SHORT;
00070     }
00071 
00072     if(SUCCESS != rc) {
00073         FUNC_EXIT_RC(rc);
00074         return rc;
00075     }
00076     writeChar(&ptr, header.byte); /* write header */
00077 
00078     ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */
00079 
00080     writePacketId(&ptr, packetid);
00081 
00082     for(i = 0; i < count; ++i) {
00083         writeMQTTString(&ptr, topicFilters[i]);
00084     }
00085 
00086     *serialized_len = (uint32_t)(ptr - buf);
00087 
00088     FUNC_EXIT_RC(SUCCESS);
00089     return SUCCESS;
00090 }
00091 
00092 
00093 /**
00094   * Deserializes the supplied (wire) buffer into unsuback data
00095   * @param packetid returned integer - the MQTT packet identifier
00096   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00097   * @param buflen the length in bytes of the data in the supplied buffer
00098   * @return MQTTReturnCode indicating function execution status
00099   */
00100 MQTTReturnCode MQTTDeserialize_unsuback(uint16_t *packetid, unsigned char *buf, size_t buflen) {
00101         unsigned char type = 0;
00102         unsigned char dup = 0;
00103         MQTTReturnCode rc = FAILURE;
00104 
00105     FUNC_ENTRY;
00106     if(NULL == packetid || NULL == buf) {
00107         FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
00108         return MQTT_NULL_VALUE_ERROR;
00109     }
00110 
00111     rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
00112     if(SUCCESS == rc && UNSUBACK != type) {
00113         rc = FAILURE;
00114     }
00115 
00116     FUNC_EXIT_RC(rc);
00117     return rc;
00118 }
00119 
00120