ME910 support

Dependents:   5_Dragonfly_Cayenne_Sprint_IKS01A1

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

Committer:
jburhenn
Date:
Fri Oct 07 17:21:45 2016 +0000
Revision:
0:09ef59d2d0f7
Child:
22:0dbabcc6e7b2
Initial commit.

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 * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
jburhenn 0:09ef59d2d0f7 16 *******************************************************************************/
jburhenn 0:09ef59d2d0f7 17
jburhenn 0:09ef59d2d0f7 18 #include "MQTTPacket.h"
jburhenn 0:09ef59d2d0f7 19
jburhenn 0:09ef59d2d0f7 20 #include <string.h>
jburhenn 0:09ef59d2d0f7 21
jburhenn 0:09ef59d2d0f7 22
jburhenn 0:09ef59d2d0f7 23 /**
jburhenn 0:09ef59d2d0f7 24 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
jburhenn 0:09ef59d2d0f7 25 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
jburhenn 0:09ef59d2d0f7 26 * @param topicName the topic name to be used in the publish
jburhenn 0:09ef59d2d0f7 27 * @param payloadlen the length of the payload to be sent
jburhenn 0:09ef59d2d0f7 28 * @return the length of buffer needed to contain the serialized version of the packet
jburhenn 0:09ef59d2d0f7 29 */
jburhenn 0:09ef59d2d0f7 30 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
jburhenn 0:09ef59d2d0f7 31 {
jburhenn 0:09ef59d2d0f7 32 int len = 0;
jburhenn 0:09ef59d2d0f7 33
jburhenn 0:09ef59d2d0f7 34 len += 2 + MQTTstrlen(topicName) + payloadlen;
jburhenn 0:09ef59d2d0f7 35 if (qos > 0)
jburhenn 0:09ef59d2d0f7 36 len += 2; /* packetid */
jburhenn 0:09ef59d2d0f7 37 return len;
jburhenn 0:09ef59d2d0f7 38 }
jburhenn 0:09ef59d2d0f7 39
jburhenn 0:09ef59d2d0f7 40
jburhenn 0:09ef59d2d0f7 41 /**
jburhenn 0:09ef59d2d0f7 42 * Serializes the supplied publish data into the supplied buffer, ready for sending
jburhenn 0:09ef59d2d0f7 43 * @param buf the buffer into which the packet will be serialized
jburhenn 0:09ef59d2d0f7 44 * @param buflen the length in bytes of the supplied buffer
jburhenn 0:09ef59d2d0f7 45 * @param dup integer - the MQTT dup flag
jburhenn 0:09ef59d2d0f7 46 * @param qos integer - the MQTT QoS value
jburhenn 0:09ef59d2d0f7 47 * @param retained integer - the MQTT retained flag
jburhenn 0:09ef59d2d0f7 48 * @param packetid integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 49 * @param topicName MQTTString - the MQTT topic in the publish
jburhenn 0:09ef59d2d0f7 50 * @param payload byte buffer - the MQTT publish payload
jburhenn 0:09ef59d2d0f7 51 * @param payloadlen integer - the length of the MQTT payload
jburhenn 0:09ef59d2d0f7 52 * @return the length of the serialized data. <= 0 indicates error
jburhenn 0:09ef59d2d0f7 53 */
jburhenn 0:09ef59d2d0f7 54 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
jburhenn 0:09ef59d2d0f7 55 MQTTString topicName, unsigned char* payload, int payloadlen)
jburhenn 0:09ef59d2d0f7 56 {
jburhenn 0:09ef59d2d0f7 57 unsigned char *ptr = buf;
jburhenn 0:09ef59d2d0f7 58 MQTTHeader header = {0};
jburhenn 0:09ef59d2d0f7 59 int rem_len = 0;
jburhenn 0:09ef59d2d0f7 60 int rc = 0;
jburhenn 0:09ef59d2d0f7 61
jburhenn 0:09ef59d2d0f7 62 if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
jburhenn 0:09ef59d2d0f7 63 {
jburhenn 0:09ef59d2d0f7 64 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jburhenn 0:09ef59d2d0f7 65 goto exit;
jburhenn 0:09ef59d2d0f7 66 }
jburhenn 0:09ef59d2d0f7 67
jburhenn 0:09ef59d2d0f7 68 header.bits.type = PUBLISH_MSG;
jburhenn 0:09ef59d2d0f7 69 header.bits.dup = dup;
jburhenn 0:09ef59d2d0f7 70 header.bits.qos = qos;
jburhenn 0:09ef59d2d0f7 71 header.bits.retain = retained;
jburhenn 0:09ef59d2d0f7 72 writeChar(&ptr, header.byte); /* write header */
jburhenn 0:09ef59d2d0f7 73
jburhenn 0:09ef59d2d0f7 74 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
jburhenn 0:09ef59d2d0f7 75
jburhenn 0:09ef59d2d0f7 76 writeMQTTString(&ptr, topicName);
jburhenn 0:09ef59d2d0f7 77
jburhenn 0:09ef59d2d0f7 78 if (qos > 0)
jburhenn 0:09ef59d2d0f7 79 writeInt(&ptr, packetid);
jburhenn 0:09ef59d2d0f7 80
jburhenn 0:09ef59d2d0f7 81 memcpy(ptr, payload, payloadlen);
jburhenn 0:09ef59d2d0f7 82 ptr += payloadlen;
jburhenn 0:09ef59d2d0f7 83
jburhenn 0:09ef59d2d0f7 84 rc = ptr - buf;
jburhenn 0:09ef59d2d0f7 85
jburhenn 0:09ef59d2d0f7 86 exit:
jburhenn 0:09ef59d2d0f7 87 return rc;
jburhenn 0:09ef59d2d0f7 88 }
jburhenn 0:09ef59d2d0f7 89
jburhenn 0:09ef59d2d0f7 90
jburhenn 0:09ef59d2d0f7 91
jburhenn 0:09ef59d2d0f7 92 /**
jburhenn 0:09ef59d2d0f7 93 * Serializes the ack packet into the supplied buffer.
jburhenn 0:09ef59d2d0f7 94 * @param buf the buffer into which the packet will be serialized
jburhenn 0:09ef59d2d0f7 95 * @param buflen the length in bytes of the supplied buffer
jburhenn 0:09ef59d2d0f7 96 * @param type the MQTT packet type
jburhenn 0:09ef59d2d0f7 97 * @param dup the MQTT dup flag
jburhenn 0:09ef59d2d0f7 98 * @param packetid the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 99 * @return serialized length, or error if 0
jburhenn 0:09ef59d2d0f7 100 */
jburhenn 0:09ef59d2d0f7 101 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
jburhenn 0:09ef59d2d0f7 102 {
jburhenn 0:09ef59d2d0f7 103 MQTTHeader header = {0};
jburhenn 0:09ef59d2d0f7 104 int rc = 0;
jburhenn 0:09ef59d2d0f7 105 unsigned char *ptr = buf;
jburhenn 0:09ef59d2d0f7 106
jburhenn 0:09ef59d2d0f7 107 if (buflen < 4)
jburhenn 0:09ef59d2d0f7 108 {
jburhenn 0:09ef59d2d0f7 109 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jburhenn 0:09ef59d2d0f7 110 goto exit;
jburhenn 0:09ef59d2d0f7 111 }
jburhenn 0:09ef59d2d0f7 112 header.bits.type = packettype;
jburhenn 0:09ef59d2d0f7 113 header.bits.dup = dup;
jburhenn 0:09ef59d2d0f7 114 header.bits.qos = (packettype == PUBREL_MSG) ? 1 : 0;
jburhenn 0:09ef59d2d0f7 115 writeChar(&ptr, header.byte); /* write header */
jburhenn 0:09ef59d2d0f7 116
jburhenn 0:09ef59d2d0f7 117 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
jburhenn 0:09ef59d2d0f7 118 writeInt(&ptr, packetid);
jburhenn 0:09ef59d2d0f7 119 rc = ptr - buf;
jburhenn 0:09ef59d2d0f7 120 exit:
jburhenn 0:09ef59d2d0f7 121 return rc;
jburhenn 0:09ef59d2d0f7 122 }
jburhenn 0:09ef59d2d0f7 123
jburhenn 0:09ef59d2d0f7 124
jburhenn 0:09ef59d2d0f7 125 /**
jburhenn 0:09ef59d2d0f7 126 * Serializes a puback packet into the supplied buffer.
jburhenn 0:09ef59d2d0f7 127 * @param buf the buffer into which the packet will be serialized
jburhenn 0:09ef59d2d0f7 128 * @param buflen the length in bytes of the supplied buffer
jburhenn 0:09ef59d2d0f7 129 * @param packetid integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 130 * @return serialized length, or error if 0
jburhenn 0:09ef59d2d0f7 131 */
jburhenn 0:09ef59d2d0f7 132 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
jburhenn 0:09ef59d2d0f7 133 {
jburhenn 0:09ef59d2d0f7 134 return MQTTSerialize_ack(buf, buflen, PUBACK_MSG, 0, packetid);
jburhenn 0:09ef59d2d0f7 135 }
jburhenn 0:09ef59d2d0f7 136
jburhenn 0:09ef59d2d0f7 137
jburhenn 0:09ef59d2d0f7 138 /**
jburhenn 0:09ef59d2d0f7 139 * Serializes a pubrel packet into the supplied buffer.
jburhenn 0:09ef59d2d0f7 140 * @param buf the buffer into which the packet will be serialized
jburhenn 0:09ef59d2d0f7 141 * @param buflen the length in bytes of the supplied buffer
jburhenn 0:09ef59d2d0f7 142 * @param dup integer - the MQTT dup flag
jburhenn 0:09ef59d2d0f7 143 * @param packetid integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 144 * @return serialized length, or error if 0
jburhenn 0:09ef59d2d0f7 145 */
jburhenn 0:09ef59d2d0f7 146 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
jburhenn 0:09ef59d2d0f7 147 {
jburhenn 0:09ef59d2d0f7 148 return MQTTSerialize_ack(buf, buflen, PUBREL_MSG, dup, packetid);
jburhenn 0:09ef59d2d0f7 149 }
jburhenn 0:09ef59d2d0f7 150
jburhenn 0:09ef59d2d0f7 151
jburhenn 0:09ef59d2d0f7 152 /**
jburhenn 0:09ef59d2d0f7 153 * Serializes a pubrel packet into the supplied buffer.
jburhenn 0:09ef59d2d0f7 154 * @param buf the buffer into which the packet will be serialized
jburhenn 0:09ef59d2d0f7 155 * @param buflen the length in bytes of the supplied buffer
jburhenn 0:09ef59d2d0f7 156 * @param packetid integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 157 * @return serialized length, or error if 0
jburhenn 0:09ef59d2d0f7 158 */
jburhenn 0:09ef59d2d0f7 159 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
jburhenn 0:09ef59d2d0f7 160 {
jburhenn 0:09ef59d2d0f7 161 return MQTTSerialize_ack(buf, buflen, PUBCOMP_MSG, 0, packetid);
jburhenn 0:09ef59d2d0f7 162 }
jburhenn 0:09ef59d2d0f7 163
jburhenn 0:09ef59d2d0f7 164