ME910 support

Dependents:   5_Dragonfly_Cayenne_Sprint_IKS01A1

Fork of Cayenne-MQTT-mbed-MTSAS by Peter Ferland

Committer:
pferland
Date:
Thu Sep 07 16:10:53 2017 +0000
Revision:
25:4bae4d3efe12
Parent:
0:09ef59d2d0f7
Child:
22:0dbabcc6e7b2
Replaced domain name with ip address

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:09ef59d2d0f7 1 /*******************************************************************************
jburhenn 0:09ef59d2d0f7 2 * Copyright (c) 2014 IBM Corp.
jburhenn 0:09ef59d2d0f7 3 *
jburhenn 0:09ef59d2d0f7 4 * All rights reserved. This program and the accompanying materials
jburhenn 0:09ef59d2d0f7 5 * are made available under the terms of the Eclipse Public License v1.0
jburhenn 0:09ef59d2d0f7 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
jburhenn 0:09ef59d2d0f7 7 *
jburhenn 0:09ef59d2d0f7 8 * The Eclipse Public License is available at
jburhenn 0:09ef59d2d0f7 9 * http://www.eclipse.org/legal/epl-v10.html
jburhenn 0:09ef59d2d0f7 10 * and the Eclipse Distribution License is available at
jburhenn 0:09ef59d2d0f7 11 * http://www.eclipse.org/org/documents/edl-v10.php.
jburhenn 0:09ef59d2d0f7 12 *
jburhenn 0:09ef59d2d0f7 13 * Contributors:
jburhenn 0:09ef59d2d0f7 14 * Ian Craggs - initial API and implementation and/or initial documentation
jburhenn 0:09ef59d2d0f7 15 *******************************************************************************/
jburhenn 0:09ef59d2d0f7 16
jburhenn 0:09ef59d2d0f7 17 #include "MQTTPacket.h"
jburhenn 0:09ef59d2d0f7 18
jburhenn 0:09ef59d2d0f7 19 #include <string.h>
jburhenn 0:09ef59d2d0f7 20
jburhenn 0:09ef59d2d0f7 21 /**
jburhenn 0:09ef59d2d0f7 22 * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
jburhenn 0:09ef59d2d0f7 23 * @param count the number of topic filter strings in topicFilters
jburhenn 0:09ef59d2d0f7 24 * @param topicFilters the array of topic filter strings to be used in the publish
jburhenn 0:09ef59d2d0f7 25 * @return the length of buffer needed to contain the serialized version of the packet
jburhenn 0:09ef59d2d0f7 26 */
jburhenn 0:09ef59d2d0f7 27 int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
jburhenn 0:09ef59d2d0f7 28 {
jburhenn 0:09ef59d2d0f7 29 int i;
jburhenn 0:09ef59d2d0f7 30 int len = 2; /* packetid */
jburhenn 0:09ef59d2d0f7 31
jburhenn 0:09ef59d2d0f7 32 for (i = 0; i < count; ++i)
jburhenn 0:09ef59d2d0f7 33 len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
jburhenn 0:09ef59d2d0f7 34 return len;
jburhenn 0:09ef59d2d0f7 35 }
jburhenn 0:09ef59d2d0f7 36
jburhenn 0:09ef59d2d0f7 37
jburhenn 0:09ef59d2d0f7 38 /**
jburhenn 0:09ef59d2d0f7 39 * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
jburhenn 0:09ef59d2d0f7 40 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jburhenn 0:09ef59d2d0f7 41 * @param buflen the length in bytes of the data in the supplied buffer
jburhenn 0:09ef59d2d0f7 42 * @param dup integer - the MQTT dup flag
jburhenn 0:09ef59d2d0f7 43 * @param packetid integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 44 * @param count - number of members in the topicFilters array
jburhenn 0:09ef59d2d0f7 45 * @param topicFilters - array of topic filter names
jburhenn 0:09ef59d2d0f7 46 * @return the length of the serialized data. <= 0 indicates error
jburhenn 0:09ef59d2d0f7 47 */
jburhenn 0:09ef59d2d0f7 48 int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
jburhenn 0:09ef59d2d0f7 49 int count, MQTTString topicFilters[])
jburhenn 0:09ef59d2d0f7 50 {
jburhenn 0:09ef59d2d0f7 51 unsigned char *ptr = buf;
jburhenn 0:09ef59d2d0f7 52 MQTTHeader header = {0};
jburhenn 0:09ef59d2d0f7 53 int rem_len = 0;
jburhenn 0:09ef59d2d0f7 54 int rc = -1;
jburhenn 0:09ef59d2d0f7 55 int i = 0;
jburhenn 0:09ef59d2d0f7 56
jburhenn 0:09ef59d2d0f7 57 if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
jburhenn 0:09ef59d2d0f7 58 {
jburhenn 0:09ef59d2d0f7 59 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jburhenn 0:09ef59d2d0f7 60 goto exit;
jburhenn 0:09ef59d2d0f7 61 }
jburhenn 0:09ef59d2d0f7 62
jburhenn 0:09ef59d2d0f7 63 header.byte = 0;
jburhenn 0:09ef59d2d0f7 64 header.bits.type = UNSUBSCRIBE_MSG;
jburhenn 0:09ef59d2d0f7 65 header.bits.dup = dup;
jburhenn 0:09ef59d2d0f7 66 header.bits.qos = 1;
jburhenn 0:09ef59d2d0f7 67 writeChar(&ptr, header.byte); /* write header */
jburhenn 0:09ef59d2d0f7 68
jburhenn 0:09ef59d2d0f7 69 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
jburhenn 0:09ef59d2d0f7 70
jburhenn 0:09ef59d2d0f7 71 writeInt(&ptr, packetid);
jburhenn 0:09ef59d2d0f7 72
jburhenn 0:09ef59d2d0f7 73 for (i = 0; i < count; ++i)
jburhenn 0:09ef59d2d0f7 74 writeMQTTString(&ptr, topicFilters[i]);
jburhenn 0:09ef59d2d0f7 75
jburhenn 0:09ef59d2d0f7 76 rc = ptr - buf;
jburhenn 0:09ef59d2d0f7 77 exit:
jburhenn 0:09ef59d2d0f7 78 return rc;
jburhenn 0:09ef59d2d0f7 79 }
jburhenn 0:09ef59d2d0f7 80
jburhenn 0:09ef59d2d0f7 81
jburhenn 0:09ef59d2d0f7 82 /**
jburhenn 0:09ef59d2d0f7 83 * Deserializes the supplied (wire) buffer into unsuback data
jburhenn 0:09ef59d2d0f7 84 * @param packetid returned integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 85 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jburhenn 0:09ef59d2d0f7 86 * @param buflen the length in bytes of the data in the supplied buffer
jburhenn 0:09ef59d2d0f7 87 * @return error code. 1 is success, 0 is failure
jburhenn 0:09ef59d2d0f7 88 */
jburhenn 0:09ef59d2d0f7 89 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
jburhenn 0:09ef59d2d0f7 90 {
jburhenn 0:09ef59d2d0f7 91 unsigned char type = 0;
jburhenn 0:09ef59d2d0f7 92 unsigned char dup = 0;
jburhenn 0:09ef59d2d0f7 93 int rc = 0;
jburhenn 0:09ef59d2d0f7 94
jburhenn 0:09ef59d2d0f7 95 rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
jburhenn 0:09ef59d2d0f7 96 if (type == UNSUBACK_MSG)
jburhenn 0:09ef59d2d0f7 97 rc = 1;
jburhenn 0:09ef59d2d0f7 98 return rc;
jburhenn 0:09ef59d2d0f7 99 }
jburhenn 0:09ef59d2d0f7 100
jburhenn 0:09ef59d2d0f7 101