Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTSNSearchServer.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 * Serializes the supplied advertise data into the supplied buffer, ready for sending 00025 * @param buf the buffer into which the packet will be serialized 00026 * @param buflen the length in bytes of the supplied buffer 00027 * @param radius the broadcast radius of this message 00028 * @param duration - the time interval until the next advertise will be sent 00029 * @return the length of the serialized data. <= 0 indicates error 00030 */ 00031 int MQTTSNSerialize_advertise(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short duration) 00032 { 00033 unsigned char *ptr = buf; 00034 int len = 0; 00035 int rc = 0; 00036 00037 FUNC_ENTRY; 00038 if ((len = MQTTSNPacket_len(4)) > buflen) 00039 { 00040 rc = MQTTSNPACKET_BUFFER_TOO_SHORT; 00041 goto exit; 00042 } 00043 ptr += MQTTSNPacket_encode(ptr, len); /* write length */ 00044 writeChar(&ptr, MQTTSN_ADVERTISE); /* write message type */ 00045 00046 writeChar(&ptr, gatewayid); 00047 writeInt(&ptr, duration); 00048 00049 rc = ptr - buf; 00050 exit: 00051 FUNC_EXIT_RC(rc); 00052 return rc; 00053 } 00054 00055 00056 /** 00057 * Deserializes the supplied (wire) buffer into searchgw data 00058 * @param radius the returned broadcast radius of this message 00059 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00060 * @param buflen the length in bytes of the data in the supplied buffer 00061 * @return error code. 1 is success 00062 */ 00063 int MQTTSNDeserialize_searchgw(unsigned char* radius, unsigned char* buf, int buflen) 00064 { 00065 unsigned char* curdata = buf; 00066 unsigned char* enddata = NULL; 00067 int rc = 0; 00068 int mylen = 0; 00069 00070 FUNC_ENTRY; 00071 curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */ 00072 enddata = buf + mylen; 00073 if (enddata - curdata > buflen) 00074 goto exit; 00075 00076 if (readChar(&curdata) != MQTTSN_SEARCHGW) 00077 goto exit; 00078 00079 *radius = readChar(&curdata); 00080 00081 rc = 1; 00082 exit: 00083 FUNC_EXIT_RC(rc); 00084 return rc; 00085 } 00086 00087 00088 /** 00089 * Serializes the supplied gwinfo data into the supplied buffer, ready for sending 00090 * @param buf the buffer into which the packet will be serialized 00091 * @param buflen the length in bytes of the supplied buffer 00092 * @param gatewayid the gateway id 00093 * @param gatewayaddress_len the optional length of the gateway address (0 if none) 00094 * @param gatewayaddress the optional gateway address (NULL if none) 00095 * @return the length of the serialized data. <= 0 indicates error 00096 */ 00097 int MQTTSNSerialize_gwinfo(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short gatewayaddress_len, 00098 unsigned char* gatewayaddress) 00099 { 00100 unsigned char *ptr = buf; 00101 int len = 0; 00102 int rc = 0; 00103 00104 FUNC_ENTRY; 00105 if ((len = MQTTSNPacket_len(2 + gatewayaddress_len)) > buflen) 00106 { 00107 rc = MQTTSNPACKET_BUFFER_TOO_SHORT; 00108 goto exit; 00109 } 00110 ptr += MQTTSNPacket_encode(ptr, len); /* write length */ 00111 writeChar(&ptr, MQTTSN_GWINFO); /* write message type */ 00112 00113 writeChar(&ptr, gatewayid); 00114 if (gatewayaddress_len > 0 && gatewayaddress != NULL) 00115 { 00116 memcpy(ptr, gatewayaddress, gatewayaddress_len); 00117 ptr += gatewayaddress_len; 00118 } 00119 00120 rc = ptr - buf; 00121 exit: 00122 FUNC_EXIT_RC(rc); 00123 return rc; 00124 00125 } 00126 00127 00128
Generated on Thu Jul 14 2022 12:58:42 by
