【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/

Dependents:   mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn

Committer:
jksoft
Date:
Mon Mar 26 04:49:20 2018 +0000
Revision:
13:61e0cc093180
Parent:
0:0a2f634d3324
???????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:0a2f634d3324 1 /*******************************************************************************
jksoft 0:0a2f634d3324 2 * Copyright (c) 2014 IBM Corp.
jksoft 0:0a2f634d3324 3 *
jksoft 0:0a2f634d3324 4 * All rights reserved. This program and the accompanying materials
jksoft 0:0a2f634d3324 5 * are made available under the terms of the Eclipse Public License v1.0
jksoft 0:0a2f634d3324 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
jksoft 0:0a2f634d3324 7 *
jksoft 0:0a2f634d3324 8 * The Eclipse Public License is available at
jksoft 0:0a2f634d3324 9 * http://www.eclipse.org/legal/epl-v10.html
jksoft 0:0a2f634d3324 10 * and the Eclipse Distribution License is available at
jksoft 0:0a2f634d3324 11 * http://www.eclipse.org/org/documents/edl-v10.php.
jksoft 0:0a2f634d3324 12 *
jksoft 0:0a2f634d3324 13 * Contributors:
jksoft 0:0a2f634d3324 14 * Ian Craggs - initial API and implementation and/or initial documentation
jksoft 0:0a2f634d3324 15 *******************************************************************************/
jksoft 0:0a2f634d3324 16
jksoft 0:0a2f634d3324 17 #include "MQTTPacket.h"
jksoft 0:0a2f634d3324 18 #include "StackTrace.h"
jksoft 0:0a2f634d3324 19
jksoft 0:0a2f634d3324 20 #include <string.h>
jksoft 0:0a2f634d3324 21
jksoft 0:0a2f634d3324 22 /**
jksoft 0:0a2f634d3324 23 * Determines the length of the MQTT connect packet that would be produced using the supplied connect options.
jksoft 0:0a2f634d3324 24 * @param options the options to be used to build the connect packet
jksoft 0:0a2f634d3324 25 * @return the length of buffer needed to contain the serialized version of the packet
jksoft 0:0a2f634d3324 26 */
jksoft 0:0a2f634d3324 27 int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
jksoft 0:0a2f634d3324 28 {
jksoft 0:0a2f634d3324 29 int len = 0;
jksoft 0:0a2f634d3324 30
jksoft 0:0a2f634d3324 31 FUNC_ENTRY;
jksoft 0:0a2f634d3324 32
jksoft 0:0a2f634d3324 33 if (options->MQTTVersion == 3)
jksoft 0:0a2f634d3324 34 len = 12; /* variable depending on MQTT or MQIsdp */
jksoft 0:0a2f634d3324 35 else if (options->MQTTVersion == 4)
jksoft 0:0a2f634d3324 36 len = 10;
jksoft 0:0a2f634d3324 37
jksoft 0:0a2f634d3324 38 len += MQTTstrlen(options->clientID)+2;
jksoft 0:0a2f634d3324 39 if (options->willFlag)
jksoft 0:0a2f634d3324 40 len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2;
jksoft 0:0a2f634d3324 41 if (options->username.cstring || options->username.lenstring.data)
jksoft 0:0a2f634d3324 42 len += MQTTstrlen(options->username)+2;
jksoft 0:0a2f634d3324 43 if (options->password.cstring || options->password.lenstring.data)
jksoft 0:0a2f634d3324 44 len += MQTTstrlen(options->password)+2;
jksoft 0:0a2f634d3324 45
jksoft 0:0a2f634d3324 46 FUNC_EXIT_RC(len);
jksoft 0:0a2f634d3324 47 return len;
jksoft 0:0a2f634d3324 48 }
jksoft 0:0a2f634d3324 49
jksoft 0:0a2f634d3324 50
jksoft 0:0a2f634d3324 51 /**
jksoft 0:0a2f634d3324 52 * Serializes the connect options into the buffer.
jksoft 0:0a2f634d3324 53 * @param buf the buffer into which the packet will be serialized
jksoft 0:0a2f634d3324 54 * @param len the length in bytes of the supplied buffer
jksoft 0:0a2f634d3324 55 * @param options the options to be used to build the connect packet
jksoft 0:0a2f634d3324 56 * @return serialized length, or error if 0
jksoft 0:0a2f634d3324 57 */
jksoft 0:0a2f634d3324 58 int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options)
jksoft 0:0a2f634d3324 59 {
jksoft 0:0a2f634d3324 60 unsigned char *ptr = buf;
jksoft 0:0a2f634d3324 61 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 62 MQTTConnectFlags flags = {0};
jksoft 0:0a2f634d3324 63 int len = 0;
jksoft 0:0a2f634d3324 64 int rc = -1;
jksoft 0:0a2f634d3324 65
jksoft 0:0a2f634d3324 66 FUNC_ENTRY;
jksoft 0:0a2f634d3324 67 if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
jksoft 0:0a2f634d3324 68 {
jksoft 0:0a2f634d3324 69 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 0:0a2f634d3324 70 goto exit;
jksoft 0:0a2f634d3324 71 }
jksoft 0:0a2f634d3324 72
jksoft 0:0a2f634d3324 73 header.byte = 0;
jksoft 0:0a2f634d3324 74 header.bits.type = CONNECT;
jksoft 0:0a2f634d3324 75 writeChar(&ptr, header.byte); /* write header */
jksoft 0:0a2f634d3324 76
jksoft 0:0a2f634d3324 77 ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
jksoft 0:0a2f634d3324 78
jksoft 0:0a2f634d3324 79 if (options->MQTTVersion == 4)
jksoft 0:0a2f634d3324 80 {
jksoft 0:0a2f634d3324 81 writeCString(&ptr, "MQTT");
jksoft 0:0a2f634d3324 82 writeChar(&ptr, (char) 4);
jksoft 0:0a2f634d3324 83 }
jksoft 0:0a2f634d3324 84 else
jksoft 0:0a2f634d3324 85 {
jksoft 0:0a2f634d3324 86 writeCString(&ptr, "MQIsdp");
jksoft 0:0a2f634d3324 87 writeChar(&ptr, (char) 3);
jksoft 0:0a2f634d3324 88 }
jksoft 0:0a2f634d3324 89
jksoft 0:0a2f634d3324 90 flags.all = 0;
jksoft 0:0a2f634d3324 91 flags.bits.cleansession = options->cleansession;
jksoft 0:0a2f634d3324 92 flags.bits.will = (options->willFlag) ? 1 : 0;
jksoft 0:0a2f634d3324 93 if (flags.bits.will)
jksoft 0:0a2f634d3324 94 {
jksoft 0:0a2f634d3324 95 flags.bits.willQoS = options->will.qos;
jksoft 0:0a2f634d3324 96 flags.bits.willRetain = options->will.retained;
jksoft 0:0a2f634d3324 97 }
jksoft 0:0a2f634d3324 98
jksoft 0:0a2f634d3324 99 if (options->username.cstring || options->username.lenstring.data)
jksoft 0:0a2f634d3324 100 flags.bits.username = 1;
jksoft 0:0a2f634d3324 101 if (options->password.cstring || options->password.lenstring.data)
jksoft 0:0a2f634d3324 102 flags.bits.password = 1;
jksoft 0:0a2f634d3324 103
jksoft 0:0a2f634d3324 104 writeChar(&ptr, flags.all);
jksoft 0:0a2f634d3324 105 writeInt(&ptr, options->keepAliveInterval);
jksoft 0:0a2f634d3324 106 writeMQTTString(&ptr, options->clientID);
jksoft 0:0a2f634d3324 107 if (options->willFlag)
jksoft 0:0a2f634d3324 108 {
jksoft 0:0a2f634d3324 109 writeMQTTString(&ptr, options->will.topicName);
jksoft 0:0a2f634d3324 110 writeMQTTString(&ptr, options->will.message);
jksoft 0:0a2f634d3324 111 }
jksoft 0:0a2f634d3324 112 if (flags.bits.username)
jksoft 0:0a2f634d3324 113 writeMQTTString(&ptr, options->username);
jksoft 0:0a2f634d3324 114 if (flags.bits.password)
jksoft 0:0a2f634d3324 115 writeMQTTString(&ptr, options->password);
jksoft 0:0a2f634d3324 116
jksoft 0:0a2f634d3324 117 rc = ptr - buf;
jksoft 0:0a2f634d3324 118
jksoft 0:0a2f634d3324 119 exit: FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 120 return rc;
jksoft 0:0a2f634d3324 121 }
jksoft 0:0a2f634d3324 122
jksoft 0:0a2f634d3324 123
jksoft 0:0a2f634d3324 124 /**
jksoft 0:0a2f634d3324 125 * Deserializes the supplied (wire) buffer into connack data - return code
jksoft 0:0a2f634d3324 126 * @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
jksoft 0:0a2f634d3324 127 * @param connack_rc returned integer value of the connack return code
jksoft 0:0a2f634d3324 128 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 0:0a2f634d3324 129 * @param len the length in bytes of the data in the supplied buffer
jksoft 0:0a2f634d3324 130 * @return error code. 1 is success, 0 is failure
jksoft 0:0a2f634d3324 131 */
jksoft 0:0a2f634d3324 132 int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen)
jksoft 0:0a2f634d3324 133 {
jksoft 0:0a2f634d3324 134 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 135 unsigned char* curdata = buf;
jksoft 0:0a2f634d3324 136 unsigned char* enddata = NULL;
jksoft 0:0a2f634d3324 137 int rc = 0;
jksoft 0:0a2f634d3324 138 int mylen;
jksoft 0:0a2f634d3324 139 MQTTConnackFlags flags = {0};
jksoft 0:0a2f634d3324 140
jksoft 0:0a2f634d3324 141 FUNC_ENTRY;
jksoft 0:0a2f634d3324 142 header.byte = readChar(&curdata);
jksoft 0:0a2f634d3324 143 if (header.bits.type != CONNACK)
jksoft 0:0a2f634d3324 144 goto exit;
jksoft 0:0a2f634d3324 145
jksoft 0:0a2f634d3324 146 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
jksoft 0:0a2f634d3324 147 enddata = curdata + mylen;
jksoft 0:0a2f634d3324 148 if (enddata - curdata < 2)
jksoft 0:0a2f634d3324 149 goto exit;
jksoft 0:0a2f634d3324 150
jksoft 0:0a2f634d3324 151 flags.all = readChar(&curdata);
jksoft 0:0a2f634d3324 152 *sessionPresent = flags.bits.sessionpresent;
jksoft 0:0a2f634d3324 153 *connack_rc = readChar(&curdata);
jksoft 0:0a2f634d3324 154
jksoft 0:0a2f634d3324 155 rc = 1;
jksoft 0:0a2f634d3324 156 exit:
jksoft 0:0a2f634d3324 157 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 158 return rc;
jksoft 0:0a2f634d3324 159 }
jksoft 0:0a2f634d3324 160
jksoft 0:0a2f634d3324 161
jksoft 0:0a2f634d3324 162
jksoft 0:0a2f634d3324 163 /**
jksoft 0:0a2f634d3324 164 * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
jksoft 0:0a2f634d3324 165 * @param buf the buffer into which the packet will be serialized
jksoft 0:0a2f634d3324 166 * @param buflen the length in bytes of the supplied buffer, to avoid overruns
jksoft 0:0a2f634d3324 167 * @param packettype the message type
jksoft 0:0a2f634d3324 168 * @return serialized length, or error if 0
jksoft 0:0a2f634d3324 169 */
jksoft 0:0a2f634d3324 170 int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype)
jksoft 0:0a2f634d3324 171 {
jksoft 0:0a2f634d3324 172 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 173 int rc = -1;
jksoft 0:0a2f634d3324 174 unsigned char *ptr = buf;
jksoft 0:0a2f634d3324 175
jksoft 0:0a2f634d3324 176 FUNC_ENTRY;
jksoft 0:0a2f634d3324 177 if (buflen < 2)
jksoft 0:0a2f634d3324 178 {
jksoft 0:0a2f634d3324 179 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 0:0a2f634d3324 180 goto exit;
jksoft 0:0a2f634d3324 181 }
jksoft 0:0a2f634d3324 182 header.byte = 0;
jksoft 0:0a2f634d3324 183 header.bits.type = packettype;
jksoft 0:0a2f634d3324 184 writeChar(&ptr, header.byte); /* write header */
jksoft 0:0a2f634d3324 185
jksoft 0:0a2f634d3324 186 ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
jksoft 0:0a2f634d3324 187 rc = ptr - buf;
jksoft 0:0a2f634d3324 188 exit:
jksoft 0:0a2f634d3324 189 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 190 return rc;
jksoft 0:0a2f634d3324 191 }
jksoft 0:0a2f634d3324 192
jksoft 0:0a2f634d3324 193
jksoft 0:0a2f634d3324 194 /**
jksoft 0:0a2f634d3324 195 * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
jksoft 0:0a2f634d3324 196 * @param buf the buffer into which the packet will be serialized
jksoft 0:0a2f634d3324 197 * @param buflen the length in bytes of the supplied buffer, to avoid overruns
jksoft 0:0a2f634d3324 198 * @return serialized length, or error if 0
jksoft 0:0a2f634d3324 199 */
jksoft 0:0a2f634d3324 200 int MQTTSerialize_disconnect(unsigned char* buf, int buflen)
jksoft 0:0a2f634d3324 201 {
jksoft 0:0a2f634d3324 202 return MQTTSerialize_zero(buf, buflen, DISCONNECT);
jksoft 0:0a2f634d3324 203 }
jksoft 0:0a2f634d3324 204
jksoft 0:0a2f634d3324 205
jksoft 0:0a2f634d3324 206 /**
jksoft 0:0a2f634d3324 207 * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
jksoft 0:0a2f634d3324 208 * @param buf the buffer into which the packet will be serialized
jksoft 0:0a2f634d3324 209 * @param buflen the length in bytes of the supplied buffer, to avoid overruns
jksoft 0:0a2f634d3324 210 * @return serialized length, or error if 0
jksoft 0:0a2f634d3324 211 */
jksoft 0:0a2f634d3324 212 int MQTTSerialize_pingreq(unsigned char* buf, int buflen)
jksoft 0:0a2f634d3324 213 {
jksoft 0:0a2f634d3324 214 return MQTTSerialize_zero(buf, buflen, PINGREQ);
jksoft 0:0a2f634d3324 215 }