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.
MQTTSubscribeClient.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 "MQTTPacket.h" 00018 #include "StackTrace.h" 00019 00020 #include <string.h> 00021 00022 /** 00023 * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters 00024 * @param count the number of topic filter strings in topicFilters 00025 * @param topicFilters the array of topic filter strings to be used in the publish 00026 * @return the length of buffer needed to contain the serialized version of the packet 00027 */ 00028 int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) 00029 { 00030 int i; 00031 int len = 2; /* packetid */ 00032 00033 for (i = 0; i < count; ++i) 00034 len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ 00035 return len; 00036 } 00037 00038 00039 /** 00040 * Serializes the supplied subscribe data into the supplied buffer, ready for sending 00041 * @param buf the buffer into which the packet will be serialized 00042 * @param buflen the length in bytes of the supplied bufferr 00043 * @param dup integer - the MQTT dup flag 00044 * @param packetid integer - the MQTT packet identifier 00045 * @param count - number of members in the topicFilters and reqQos arrays 00046 * @param topicFilters - array of topic filter names 00047 * @param requestedQoSs - array of requested QoS 00048 * @return the length of the serialized data. <= 0 indicates error 00049 */ 00050 int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, 00051 MQTTString topicFilters[], int requestedQoSs[]) 00052 { 00053 unsigned char *ptr = buf; 00054 MQTTHeader header = {0}; 00055 int rem_len = 0; 00056 int rc = 0; 00057 int i = 0; 00058 00059 FUNC_ENTRY; 00060 if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) 00061 { 00062 rc = MQTTPACKET_BUFFER_TOO_SHORT; 00063 goto exit; 00064 } 00065 00066 header.byte = 0; 00067 header.bits.type = SUBSCRIBE; 00068 header.bits.dup = dup; 00069 header.bits.qos = 1; 00070 writeChar(&ptr, header.byte); /* write header */ 00071 00072 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 00073 00074 writeInt(&ptr, packetid); 00075 00076 for (i = 0; i < count; ++i) 00077 { 00078 writeMQTTString(&ptr, topicFilters[i]); 00079 writeChar(&ptr, requestedQoSs[i]); 00080 } 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 suback data 00092 * @param packetid returned integer - the MQTT packet identifier 00093 * @param maxcount - the maximum number of members allowed in the grantedQoSs array 00094 * @param count returned integer - number of members in the grantedQoSs array 00095 * @param grantedQoSs returned array of integers - the granted qualities of service 00096 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00097 * @param buflen the length in bytes of the data in the supplied buffer 00098 * @return error code. 1 is success, 0 is failure 00099 */ 00100 int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen) 00101 { 00102 MQTTHeader header = {0}; 00103 unsigned char* curdata = buf; 00104 unsigned char* enddata = NULL; 00105 int rc = 0; 00106 int mylen; 00107 00108 FUNC_ENTRY; 00109 header.byte = readChar(&curdata); 00110 if (header.bits.type != SUBACK) 00111 goto exit; 00112 00113 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 00114 enddata = curdata + mylen; 00115 if (enddata - curdata < 2) 00116 goto exit; 00117 00118 *packetid = readInt(&curdata); 00119 00120 *count = 0; 00121 while (curdata < enddata) 00122 { 00123 if (*count > maxcount) 00124 { 00125 rc = -1; 00126 goto exit; 00127 } 00128 grantedQoSs[(*count)++] = readChar(&curdata); 00129 } 00130 00131 rc = 1; 00132 exit: 00133 FUNC_EXIT_RC(rc); 00134 return rc; 00135 } 00136 00137
Generated on Thu Jul 14 2022 12:58:42 by
