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