Andrew Reed / Mbed OS CITY1082-i2c_master_wifi_mqtt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSNSearchClient.c Source File

MQTTSNSearchClient.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 #include "MQTTSNPacket.h"
00018 #include "StackTrace.h"
00019 
00020 #include <string.h>
00021 
00022 
00023 /**
00024   * Deserializes the supplied (wire) buffer into advertise data
00025   * @param gatewayid the returned gateway id
00026   * @param duration the returned duration - the time interval until the next advertise will be sent
00027   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00028   * @param buflen the length in bytes of the data in the supplied buffer
00029   * @return error code.  1 is success
00030   */
00031 int MQTTSNDeserialize_advertise(unsigned char* gatewayid, unsigned short* duration, unsigned char* buf, int buflen)
00032 {
00033     unsigned char* curdata = buf;
00034     unsigned char* enddata = NULL;
00035     int rc = 0;
00036     int mylen = 0;
00037 
00038     FUNC_ENTRY;
00039     curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */
00040     enddata = buf + mylen;
00041     if (enddata - curdata > buflen)
00042         goto exit;
00043 
00044     if (readChar(&curdata) != MQTTSN_ADVERTISE)
00045         goto exit;
00046 
00047     *gatewayid = readChar(&curdata);
00048     *duration = readInt(&curdata);
00049 
00050     rc = 1;
00051 exit:
00052     FUNC_EXIT_RC(rc);
00053     return rc;
00054 }
00055 
00056 
00057 
00058 /**
00059   * Serializes the supplied searchgw data into the supplied buffer, ready for sending
00060   * @param buf the buffer into which the packet will be serialized
00061   * @param buflen the length in bytes of the supplied buffer
00062   * @param radius the broadcast radius of this message
00063   * @return the length of the serialized data.  <= 0 indicates error
00064   */
00065 int MQTTSNSerialize_searchgw(unsigned char* buf, int buflen, unsigned char radius)
00066 {
00067     unsigned char *ptr = buf;
00068     int len = 0;
00069     int rc = 0;
00070 
00071     FUNC_ENTRY;
00072     if ((len = MQTTSNPacket_len(2)) > buflen)
00073     {
00074         rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
00075         goto exit;
00076     }
00077     ptr += MQTTSNPacket_encode(ptr, len);   /* write length */
00078     writeChar(&ptr, MQTTSN_SEARCHGW);      /* write message type */
00079 
00080     writeChar(&ptr, radius);
00081 
00082     rc = ptr - buf;
00083 exit:
00084     FUNC_EXIT_RC(rc);
00085     return rc;
00086 
00087 }
00088 
00089 
00090 /**
00091   * Deserializes the supplied (wire) buffer into gwinfo data
00092   * @param gatewayid the returned gateway id
00093   * @param gatewayaddress_len the optional returned length of the gateway address (0 if none)
00094   * @param gatewayaddress the optional returned gateway address (set to NULL if none)
00095   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00096   * @param buflen the length in bytes of the data in the supplied buffer
00097   * @return error code.  1 is success
00098   */
00099 int MQTTSNDeserialize_gwinfo(unsigned char* gatewayid, unsigned short* gatewayaddress_len,
00100         unsigned char** gatewayaddress, unsigned char* buf, int buflen)
00101 {
00102     unsigned char* curdata = buf;
00103     unsigned char* enddata = NULL;
00104     int rc = 0;
00105     int mylen = 0;
00106 
00107     FUNC_ENTRY;
00108     curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */
00109     enddata = buf + mylen;
00110     if (enddata - curdata > buflen)
00111         goto exit;
00112 
00113     if (readChar(&curdata) != MQTTSN_GWINFO)
00114         goto exit;
00115 
00116     *gatewayid = readChar(&curdata);
00117 
00118     *gatewayaddress_len = enddata - curdata;
00119     *gatewayaddress = (gatewayaddress_len > 0) ? curdata : NULL;
00120 
00121     rc = 1;
00122 exit:
00123     FUNC_EXIT_RC(rc);
00124     return rc;
00125 }
00126