Low level MQTTSN packet library, part of the Eclipse Paho project: http://eclipse.org/paho
Dependents: MQTTSN sara-n200-hello-mqtt-sn MQTTSN_2
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 MQTTSNPacket_writeChar(&ptr, MQTTSN_ADVERTISE); /* write message type */ 00045 00046 MQTTSNPacket_writeChar(&ptr, gatewayid); 00047 MQTTSNPacket_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 += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ 00072 enddata = buf + mylen; 00073 if (enddata - curdata > buflen) 00074 goto exit; 00075 00076 if (MQTTSNPacket_readChar(&curdata) != MQTTSN_SEARCHGW) 00077 goto exit; 00078 00079 *radius = MQTTSNPacket_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 MQTTSNPacket_writeChar(&ptr, MQTTSN_GWINFO); /* write message type */ 00112 00113 MQTTSNPacket_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 00129
Generated on Tue Jul 12 2022 20:35:37 by 1.7.2