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 /**
icraggs 1:7fa362fa563f 24 * Deserializes the supplied (wire) buffer into advertise data
icraggs 1:7fa362fa563f 25 * @param gatewayid the returned gateway id
icraggs 1:7fa362fa563f 26 * @param duration the returned duration - the time interval until the next advertise will be sent
icraggs 1:7fa362fa563f 27 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 1:7fa362fa563f 28 * @param buflen the length in bytes of the data in the supplied buffer
icraggs 1:7fa362fa563f 29 * @return error code. 1 is success
icraggs 1:7fa362fa563f 30 */
icraggs 1:7fa362fa563f 31 int MQTTSNDeserialize_advertise(unsigned char* gatewayid, unsigned short* duration, unsigned char* buf, int buflen)
icraggs 1:7fa362fa563f 32 {
icraggs 1:7fa362fa563f 33 unsigned char* curdata = buf;
icraggs 1:7fa362fa563f 34 unsigned char* enddata = NULL;
icraggs 1:7fa362fa563f 35 int rc = 0;
icraggs 1:7fa362fa563f 36 int mylen = 0;
icraggs 1:7fa362fa563f 37
icraggs 1:7fa362fa563f 38 FUNC_ENTRY;
icraggs 1:7fa362fa563f 39 curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */
icraggs 1:7fa362fa563f 40 enddata = buf + mylen;
icraggs 1:7fa362fa563f 41 if (enddata - curdata > buflen)
icraggs 1:7fa362fa563f 42 goto exit;
icraggs 1:7fa362fa563f 43
icraggs 1:7fa362fa563f 44 if (MQTTSNPacket_readChar(&curdata) != MQTTSN_ADVERTISE)
icraggs 1:7fa362fa563f 45 goto exit;
icraggs 1:7fa362fa563f 46
icraggs 1:7fa362fa563f 47 *gatewayid = MQTTSNPacket_readChar(&curdata);
icraggs 1:7fa362fa563f 48 *duration = MQTTSNPacket_readInt(&curdata);
icraggs 1:7fa362fa563f 49
icraggs 1:7fa362fa563f 50 rc = 1;
icraggs 1:7fa362fa563f 51 exit:
icraggs 1:7fa362fa563f 52 FUNC_EXIT_RC(rc);
icraggs 1:7fa362fa563f 53 return rc;
icraggs 1:7fa362fa563f 54 }
icraggs 1:7fa362fa563f 55
icraggs 1:7fa362fa563f 56
icraggs 0:c524a894b5e8 57
icraggs 1:7fa362fa563f 58 /**
icraggs 1:7fa362fa563f 59 * Serializes the supplied searchgw data into the supplied buffer, ready for sending
icraggs 1:7fa362fa563f 60 * @param buf the buffer into which the packet will be serialized
icraggs 1:7fa362fa563f 61 * @param buflen the length in bytes of the supplied buffer
icraggs 1:7fa362fa563f 62 * @param radius the broadcast radius of this message
icraggs 1:7fa362fa563f 63 * @return the length of the serialized data. <= 0 indicates error
icraggs 1:7fa362fa563f 64 */
icraggs 1:7fa362fa563f 65 int MQTTSNSerialize_searchgw(unsigned char* buf, int buflen, unsigned char radius)
icraggs 1:7fa362fa563f 66 {
icraggs 1:7fa362fa563f 67 unsigned char *ptr = buf;
icraggs 1:7fa362fa563f 68 int len = 0;
icraggs 1:7fa362fa563f 69 int rc = 0;
icraggs 1:7fa362fa563f 70
icraggs 1:7fa362fa563f 71 FUNC_ENTRY;
icraggs 1:7fa362fa563f 72 if ((len = MQTTSNPacket_len(2)) > buflen)
icraggs 1:7fa362fa563f 73 {
icraggs 1:7fa362fa563f 74 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 1:7fa362fa563f 75 goto exit;
icraggs 1:7fa362fa563f 76 }
icraggs 1:7fa362fa563f 77 ptr += MQTTSNPacket_encode(ptr, len); /* write length */
icraggs 1:7fa362fa563f 78 MQTTSNPacket_writeChar(&ptr, MQTTSN_SEARCHGW); /* write message type */
icraggs 1:7fa362fa563f 79
icraggs 1:7fa362fa563f 80 MQTTSNPacket_writeChar(&ptr, radius);
icraggs 1:7fa362fa563f 81
icraggs 1:7fa362fa563f 82 rc = ptr - buf;
icraggs 1:7fa362fa563f 83 exit:
icraggs 1:7fa362fa563f 84 FUNC_EXIT_RC(rc);
icraggs 1:7fa362fa563f 85 return rc;
icraggs 1:7fa362fa563f 86
icraggs 1:7fa362fa563f 87 }
icraggs 1:7fa362fa563f 88
icraggs 1:7fa362fa563f 89
icraggs 1:7fa362fa563f 90 /**
icraggs 1:7fa362fa563f 91 * Deserializes the supplied (wire) buffer into gwinfo data
icraggs 1:7fa362fa563f 92 * @param gatewayid the returned gateway id
icraggs 1:7fa362fa563f 93 * @param gatewayaddress_len the optional returned length of the gateway address (0 if none)
icraggs 1:7fa362fa563f 94 * @param gatewayaddress the optional returned gateway address (set to NULL if none)
icraggs 1:7fa362fa563f 95 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 1:7fa362fa563f 96 * @param buflen the length in bytes of the data in the supplied buffer
icraggs 1:7fa362fa563f 97 * @return error code. 1 is success
icraggs 1:7fa362fa563f 98 */
icraggs 1:7fa362fa563f 99 int MQTTSNDeserialize_gwinfo(unsigned char* gatewayid, unsigned short* gatewayaddress_len,
icraggs 1:7fa362fa563f 100 unsigned char** gatewayaddress, unsigned char* buf, int buflen)
icraggs 1:7fa362fa563f 101 {
icraggs 1:7fa362fa563f 102 unsigned char* curdata = buf;
icraggs 1:7fa362fa563f 103 unsigned char* enddata = NULL;
icraggs 1:7fa362fa563f 104 int rc = 0;
icraggs 1:7fa362fa563f 105 int mylen = 0;
icraggs 1:7fa362fa563f 106
icraggs 1:7fa362fa563f 107 FUNC_ENTRY;
icraggs 1:7fa362fa563f 108 curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */
icraggs 1:7fa362fa563f 109 enddata = buf + mylen;
icraggs 1:7fa362fa563f 110 if (enddata - curdata > buflen)
icraggs 1:7fa362fa563f 111 goto exit;
icraggs 1:7fa362fa563f 112
icraggs 1:7fa362fa563f 113 if (MQTTSNPacket_readChar(&curdata) != MQTTSN_GWINFO)
icraggs 1:7fa362fa563f 114 goto exit;
icraggs 1:7fa362fa563f 115
icraggs 1:7fa362fa563f 116 *gatewayid = MQTTSNPacket_readChar(&curdata);
icraggs 1:7fa362fa563f 117
icraggs 1:7fa362fa563f 118 *gatewayaddress_len = enddata - curdata;
icraggs 1:7fa362fa563f 119 *gatewayaddress = (gatewayaddress_len > 0) ? curdata : NULL;
icraggs 1:7fa362fa563f 120
icraggs 1:7fa362fa563f 121 rc = 1;
icraggs 1:7fa362fa563f 122 exit:
icraggs 1:7fa362fa563f 123 FUNC_EXIT_RC(rc);
icraggs 1:7fa362fa563f 124 return rc;
icraggs 1:7fa362fa563f 125 }
icraggs 1:7fa362fa563f 126
icraggs 1:7fa362fa563f 127