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