Low level MQTTSN packet library, part of the Eclipse Paho project: http://eclipse.org/paho

Dependents:   MQTTSN sara-n200-hello-mqtt-sn MQTTSN_2

The master source for this project is held at: https://github.com/eclipse/paho.mqtt-sn.embedded-c

Committer:
icraggs
Date:
Wed Jan 06 14:19:27 2016 +0000
Revision:
1:7fa362fa563f
Parent:
0:c524a894b5e8
Internal function name changes to avoid name clashes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:7fa362fa563f 1 /*******************************************************************************
icraggs 1:7fa362fa563f 2 * Copyright (c) 2014 IBM Corp.
icraggs 1:7fa362fa563f 3 *
icraggs 1:7fa362fa563f 4 * All rights reserved. This program and the accompanying materials
icraggs 1:7fa362fa563f 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:7fa362fa563f 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:7fa362fa563f 7 *
icraggs 1:7fa362fa563f 8 * The Eclipse Public License is available at
icraggs 1:7fa362fa563f 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:7fa362fa563f 10 * and the Eclipse Distribution License is available at
icraggs 1:7fa362fa563f 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:7fa362fa563f 12 *
icraggs 1:7fa362fa563f 13 * Contributors:
icraggs 1:7fa362fa563f 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 1:7fa362fa563f 15 *******************************************************************************/
icraggs 1:7fa362fa563f 16
icraggs 1:7fa362fa563f 17 #include "MQTTSNPacket.h"
icraggs 1:7fa362fa563f 18 #include "StackTrace.h"
icraggs 1:7fa362fa563f 19
icraggs 1:7fa362fa563f 20 #include <string.h>
icraggs 1:7fa362fa563f 21
icraggs 1:7fa362fa563f 22 /**
icraggs 1:7fa362fa563f 23 * Determines the length of the MQTTSN subscribe packet that would be produced using the supplied parameters,
icraggs 1:7fa362fa563f 24 * excluding length
icraggs 1:7fa362fa563f 25 * @param topicName the topic name to be used in the publish
icraggs 1:7fa362fa563f 26 * @return the length of buffer needed to contain the serialized version of the packet
icraggs 1:7fa362fa563f 27 */
icraggs 1:7fa362fa563f 28 int MQTTSNSerialize_subscribeLength(MQTTSN_topicid* topicFilter)
icraggs 1:7fa362fa563f 29 {
icraggs 1:7fa362fa563f 30 int len = 4;
icraggs 1:7fa362fa563f 31
icraggs 1:7fa362fa563f 32 if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL)
icraggs 1:7fa362fa563f 33 len += topicFilter->data.long_.len;
icraggs 1:7fa362fa563f 34 else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT || topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED)
icraggs 1:7fa362fa563f 35 len += 2;
icraggs 1:7fa362fa563f 36
icraggs 1:7fa362fa563f 37 return len;
icraggs 1:7fa362fa563f 38 }
icraggs 1:7fa362fa563f 39
icraggs 1:7fa362fa563f 40
icraggs 1:7fa362fa563f 41 /**
icraggs 1:7fa362fa563f 42 * Serializes the supplied subscribe data into the supplied buffer, ready for sending
icraggs 1:7fa362fa563f 43 * @param buf the buffer into which the packet will be serialized
icraggs 1:7fa362fa563f 44 * @param buflen the length in bytes of the supplied buffer
icraggs 1:7fa362fa563f 45 * @param dup integer - the MQTT-SN dup flag
icraggs 1:7fa362fa563f 46 * @param qos integer - the MQTT-SN QoS value
icraggs 1:7fa362fa563f 47 * @param packetid integer - the MQTT-SN packet identifier
icraggs 1:7fa362fa563f 48 * @param topic MQTTSN_topicid - the MQTT-SN topic in the subscribe
icraggs 1:7fa362fa563f 49 * @return the length of the serialized data. <= 0 indicates error
icraggs 1:7fa362fa563f 50 */
icraggs 1:7fa362fa563f 51 int MQTTSNSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned short packetid,
icraggs 1:7fa362fa563f 52 MQTTSN_topicid* topicFilter)
icraggs 1:7fa362fa563f 53 {
icraggs 1:7fa362fa563f 54 unsigned char *ptr = buf;
icraggs 1:7fa362fa563f 55 MQTTSNFlags flags;
icraggs 1:7fa362fa563f 56 int len = 0;
icraggs 1:7fa362fa563f 57 int rc = 0;
icraggs 1:7fa362fa563f 58
icraggs 1:7fa362fa563f 59 FUNC_ENTRY;
icraggs 1:7fa362fa563f 60 if ((len = MQTTSNPacket_len(MQTTSNSerialize_subscribeLength(topicFilter))) > buflen)
icraggs 1:7fa362fa563f 61 {
icraggs 1:7fa362fa563f 62 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 1:7fa362fa563f 63 goto exit;
icraggs 1:7fa362fa563f 64 }
icraggs 1:7fa362fa563f 65 ptr += MQTTSNPacket_encode(ptr, len); /* write length */
icraggs 1:7fa362fa563f 66 MQTTSNPacket_writeChar(&ptr, MQTTSN_SUBSCRIBE); /* write message type */
icraggs 0:c524a894b5e8 67
icraggs 1:7fa362fa563f 68 flags.all = 0;
icraggs 1:7fa362fa563f 69 flags.bits.dup = dup;
icraggs 1:7fa362fa563f 70 flags.bits.QoS = qos;
icraggs 1:7fa362fa563f 71 flags.bits.topicIdType = topicFilter->type;
icraggs 1:7fa362fa563f 72 MQTTSNPacket_writeChar(&ptr, flags.all);
icraggs 1:7fa362fa563f 73
icraggs 1:7fa362fa563f 74 MQTTSNPacket_writeInt(&ptr, packetid);
icraggs 1:7fa362fa563f 75
icraggs 1:7fa362fa563f 76 /* now the topic id or name */
icraggs 1:7fa362fa563f 77 if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL) /* means long topic name */
icraggs 1:7fa362fa563f 78 {
icraggs 1:7fa362fa563f 79 memcpy(ptr, topicFilter->data.long_.name, topicFilter->data.long_.len);
icraggs 1:7fa362fa563f 80 ptr += topicFilter->data.long_.len;
icraggs 1:7fa362fa563f 81 }
icraggs 1:7fa362fa563f 82 else if (topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED)
icraggs 1:7fa362fa563f 83 MQTTSNPacket_writeInt(&ptr, topicFilter->data.id);
icraggs 1:7fa362fa563f 84 else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT)
icraggs 1:7fa362fa563f 85 {
icraggs 1:7fa362fa563f 86 MQTTSNPacket_writeChar(&ptr, topicFilter->data.short_name[0]);
icraggs 1:7fa362fa563f 87 MQTTSNPacket_writeChar(&ptr, topicFilter->data.short_name[1]);
icraggs 1:7fa362fa563f 88 }
icraggs 1:7fa362fa563f 89
icraggs 1:7fa362fa563f 90 rc = ptr - buf;
icraggs 1:7fa362fa563f 91 exit:
icraggs 1:7fa362fa563f 92 FUNC_EXIT_RC(rc);
icraggs 1:7fa362fa563f 93 return rc;
icraggs 1:7fa362fa563f 94
icraggs 1:7fa362fa563f 95 }
icraggs 1:7fa362fa563f 96
icraggs 1:7fa362fa563f 97
icraggs 1:7fa362fa563f 98 /**
icraggs 1:7fa362fa563f 99 * Deserializes the supplied (wire) buffer into suback data
icraggs 1:7fa362fa563f 100 * @param qos the returned qos
icraggs 1:7fa362fa563f 101 * @param topicid returned if "accepted" the value which will be used by the gateway in subsequent PUBLISH packets
icraggs 1:7fa362fa563f 102 * @param packetid returned - the same value as the one contained in the corresponding SUBSCRIBE
icraggs 1:7fa362fa563f 103 * @param returncode returned - "accepted" or rejection reason
icraggs 1:7fa362fa563f 104 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 1:7fa362fa563f 105 * @param buflen the length in bytes of the data in the supplied buffer
icraggs 1:7fa362fa563f 106 * @return error code. 1 is success
icraggs 1:7fa362fa563f 107 */
icraggs 1:7fa362fa563f 108 int MQTTSNDeserialize_suback(int* qos, unsigned short* topicid, unsigned short* packetid,
icraggs 1:7fa362fa563f 109 unsigned char* returncode, unsigned char* buf, int buflen)
icraggs 1:7fa362fa563f 110 {
icraggs 1:7fa362fa563f 111 MQTTSNFlags flags;
icraggs 1:7fa362fa563f 112 unsigned char* curdata = buf;
icraggs 1:7fa362fa563f 113 unsigned char* enddata = NULL;
icraggs 1:7fa362fa563f 114 int rc = 0;
icraggs 1:7fa362fa563f 115 int mylen = 0;
icraggs 1:7fa362fa563f 116
icraggs 1:7fa362fa563f 117 FUNC_ENTRY;
icraggs 1:7fa362fa563f 118 curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */
icraggs 1:7fa362fa563f 119 enddata = buf + mylen;
icraggs 1:7fa362fa563f 120 if (enddata - curdata > buflen)
icraggs 1:7fa362fa563f 121 goto exit;
icraggs 1:7fa362fa563f 122
icraggs 1:7fa362fa563f 123 if (MQTTSNPacket_readChar(&curdata) != MQTTSN_SUBACK)
icraggs 1:7fa362fa563f 124 goto exit;
icraggs 1:7fa362fa563f 125
icraggs 1:7fa362fa563f 126 flags.all = MQTTSNPacket_readChar(&curdata);
icraggs 1:7fa362fa563f 127 *qos = flags.bits.QoS;
icraggs 1:7fa362fa563f 128
icraggs 1:7fa362fa563f 129 *topicid = MQTTSNPacket_readInt(&curdata);
icraggs 1:7fa362fa563f 130 *packetid = MQTTSNPacket_readInt(&curdata);
icraggs 1:7fa362fa563f 131 *returncode = MQTTSNPacket_readChar(&curdata);
icraggs 1:7fa362fa563f 132
icraggs 1:7fa362fa563f 133 rc = 1;
icraggs 1:7fa362fa563f 134 exit:
icraggs 1:7fa362fa563f 135 FUNC_EXIT_RC(rc);
icraggs 1:7fa362fa563f 136 return rc;
icraggs 1:7fa362fa563f 137 }
icraggs 1:7fa362fa563f 138
icraggs 1:7fa362fa563f 139
icraggs 1:7fa362fa563f 140
icraggs 1:7fa362fa563f 141