Jim Flynn / MQTT
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTFormat.c Source File

MQTTFormat.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 "StackTrace.h"
00018 #include "MQTTPacket.h"
00019 
00020 #include <string.h>
00021 
00022 
00023 const char* MQTTPacket_names[] =
00024 {
00025     "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
00026     "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
00027     "PINGREQ", "PINGRESP", "DISCONNECT"
00028 };
00029 
00030 
00031 const char* MQTTPacket_getName(unsigned short packetid)
00032 {
00033     return MQTTPacket_names[packetid];
00034 }
00035 
00036 
00037 int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data)
00038 {
00039     int strindex = 0;
00040 
00041     strindex = snprintf(strbuf, strbuflen,
00042             "CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
00043             (int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data,
00044             (int)data->cleansession, data->keepAliveInterval);
00045     if (data->willFlag)
00046         strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
00047                 ", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
00048                 data->will.qos, data->will.retained,
00049                 data->will.topicName.lenstring.len, data->will.topicName.lenstring.data,
00050                 data->will.message.lenstring.len, data->will.message.lenstring.data);
00051     if (data->username.lenstring.data && data->username.lenstring.len > 0)
00052         strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
00053                 ", user name %.*s", data->username.lenstring.len, data->username.lenstring.data);
00054     if (data->password.lenstring.data && data->password.lenstring.len > 0)
00055         strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
00056                 ", password %.*s", data->password.lenstring.len, data->password.lenstring.data);
00057     return strindex;
00058 }
00059 
00060 
00061 int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent)
00062 {
00063     int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc);
00064     return strindex;
00065 }
00066 
00067 
00068 int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
00069         unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen)
00070 {
00071     int strindex = snprintf(strbuf, strbuflen,
00072                 "PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
00073                 dup, qos, retained, packetid,
00074                 (topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
00075                 payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
00076     return strindex;
00077 }
00078 
00079 
00080 int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
00081 {
00082     int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid);
00083     if (dup)
00084         strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
00085     return strindex;
00086 }
00087 
00088 
00089 int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
00090         MQTTString topicFilters[], int requestedQoSs[])
00091 {
00092     return snprintf(strbuf, strbuflen,
00093         "SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
00094         dup, packetid, count,
00095         topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
00096         requestedQoSs[0]);
00097 }
00098 
00099 
00100 int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs)
00101 {
00102     return snprintf(strbuf, strbuflen,
00103         "SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]);
00104 }
00105 
00106 
00107 int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
00108         int count, MQTTString topicFilters[])
00109 {
00110     return snprintf(strbuf, strbuflen,
00111                     "UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
00112                     dup, packetid, count,
00113                     topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
00114 }
00115 
00116 
00117 #if defined(MQTT_CLIENT)
00118 char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
00119 {
00120     int index = 0;
00121     int rem_length = 0;
00122     MQTTHeader header = {0};
00123 
00124     header.byte = buf[index++];
00125     index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
00126 
00127     switch (header.bits.type)
00128     {
00129 
00130     case CONNACK:
00131     {
00132         unsigned char sessionPresent, connack_rc;
00133         if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
00134             MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent);
00135     }
00136     break;
00137     case PUBLISH:
00138     {
00139         unsigned char dup, retained, *payload;
00140         unsigned short packetid;
00141         int qos, payloadlen;
00142         MQTTString topicName = MQTTString_initializer;
00143         if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
00144                 &payload, &payloadlen, buf, buflen) == 1)
00145             MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
00146                     topicName, payload, payloadlen);
00147     }
00148     break;
00149     case PUBACK:
00150     case PUBREC:
00151     case PUBREL:
00152     case PUBCOMP:
00153     {
00154         unsigned char packettype, dup;
00155         unsigned short packetid;
00156         if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
00157             MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
00158     }
00159     break;
00160     case SUBACK:
00161     {
00162         unsigned short packetid;
00163         int maxcount = 1, count = 0;
00164         int grantedQoSs[1];
00165         if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
00166             MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs);
00167     }
00168     break;
00169     case UNSUBACK:
00170     {
00171         unsigned short packetid;
00172         if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
00173             MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid);
00174     }
00175     break;
00176     case PINGREQ:
00177     case PINGRESP:
00178     case DISCONNECT:
00179         snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
00180         break;
00181     }
00182     return strbuf;
00183 }
00184 #endif
00185 
00186 #if defined(MQTT_SERVER)
00187 char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
00188 {
00189     int index = 0;
00190     int rem_length = 0;
00191     MQTTHeader header = {0};
00192 
00193     header.byte = buf[index++];
00194     index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
00195 
00196     switch (header.bits.type)
00197     {
00198     case CONNECT:
00199     {
00200         MQTTPacket_connectData data;
00201         if ((MQTTDeserialize_connect(&data, buf, buflen)) == 1)
00202             MQTTStringFormat_connect(strbuf, strbuflen, &data);
00203     }
00204     break;
00205     case PUBLISH:
00206     {
00207         unsigned char dup, retained, *payload;
00208         unsigned short packetid;
00209         int qos, payloadlen;
00210         MQTTString topicName = MQTTString_initializer;
00211         if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
00212                 &payload, &payloadlen, buf, buflen) == 1)
00213             MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
00214                     topicName, payload, payloadlen);
00215     }
00216     break;
00217     case PUBACK:
00218     case PUBREC:
00219     case PUBREL:
00220     case PUBCOMP:
00221     {
00222         unsigned char packettype, dup;
00223         unsigned short packetid;
00224         if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
00225             MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
00226     }
00227     break;
00228     case SUBSCRIBE:
00229     {
00230         unsigned char dup;
00231         unsigned short packetid;
00232         int maxcount = 1, count = 0;
00233         MQTTString topicFilters[1];
00234         int requestedQoSs[1];
00235         if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
00236                 topicFilters, requestedQoSs, buf, buflen) == 1)
00237             MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);;
00238     }
00239     break;
00240     case UNSUBSCRIBE:
00241     {
00242         unsigned char dup;
00243         unsigned short packetid;
00244         int maxcount = 1, count = 0;
00245         MQTTString topicFilters[1];
00246         if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
00247             MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters);
00248     }
00249     break;
00250     case PINGREQ:
00251     case PINGRESP:
00252     case DISCONNECT:
00253         snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
00254         break;
00255     }
00256     strbuf[strbuflen] = '\0';
00257     return strbuf;
00258 }
00259 #endif