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:
Thu Feb 26 15:59:36 2015 +0000
Revision:
0:c524a894b5e8
Child:
1:7fa362fa563f
First version that works nicely :-)

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