mbed OS5に対応したMilkcocoaライブラリのテストバージョンです。

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
jksoft
Date:
Tue Jan 24 13:41:36 2017 +0000
Revision:
24:6ba1245bf049
??????????

Who changed what in which revision?

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