【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 "StackTrace.h"
jksoft 0:0a2f634d3324 18 #include "MQTTPacket.h"
jksoft 0:0a2f634d3324 19 #include <string.h>
jksoft 0:0a2f634d3324 20
jksoft 0:0a2f634d3324 21 #define min(a, b) ((a < b) ? a : b)
jksoft 0:0a2f634d3324 22
jksoft 0:0a2f634d3324 23
jksoft 0:0a2f634d3324 24 /**
jksoft 0:0a2f634d3324 25 * Validates MQTT protocol name and version combinations
jksoft 0:0a2f634d3324 26 * @param protocol the MQTT protocol name as an MQTTString
jksoft 0:0a2f634d3324 27 * @param version the MQTT protocol version number, as in the connect packet
jksoft 0:0a2f634d3324 28 * @return correct MQTT combination? 1 is true, 0 is false
jksoft 0:0a2f634d3324 29 */
jksoft 0:0a2f634d3324 30 int MQTTPacket_checkVersion(MQTTString* protocol, int version)
jksoft 0:0a2f634d3324 31 {
jksoft 0:0a2f634d3324 32 int rc = 0;
jksoft 0:0a2f634d3324 33
jksoft 0:0a2f634d3324 34 if (version == 3 && memcmp(protocol->lenstring.data, "MQIdsp",
jksoft 0:0a2f634d3324 35 min(6, protocol->lenstring.len)) == 0)
jksoft 0:0a2f634d3324 36 rc = 1;
jksoft 0:0a2f634d3324 37 else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT",
jksoft 0:0a2f634d3324 38 min(4, protocol->lenstring.len)) == 0)
jksoft 0:0a2f634d3324 39 rc = 1;
jksoft 0:0a2f634d3324 40 return rc;
jksoft 0:0a2f634d3324 41 }
jksoft 0:0a2f634d3324 42
jksoft 0:0a2f634d3324 43
jksoft 0:0a2f634d3324 44 /**
jksoft 0:0a2f634d3324 45 * Deserializes the supplied (wire) buffer into connect data structure
jksoft 0:0a2f634d3324 46 * @param data the connect data structure to be filled out
jksoft 0:0a2f634d3324 47 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 0:0a2f634d3324 48 * @param len the length in bytes of the data in the supplied buffer
jksoft 0:0a2f634d3324 49 * @return error code. 1 is success, 0 is failure
jksoft 0:0a2f634d3324 50 */
jksoft 0:0a2f634d3324 51 int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len)
jksoft 0:0a2f634d3324 52 {
jksoft 0:0a2f634d3324 53 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 54 MQTTConnectFlags flags = {0};
jksoft 0:0a2f634d3324 55 unsigned char* curdata = buf;
jksoft 0:0a2f634d3324 56 unsigned char* enddata = &buf[len];
jksoft 0:0a2f634d3324 57 int rc = 0;
jksoft 0:0a2f634d3324 58 MQTTString Protocol;
jksoft 0:0a2f634d3324 59 int version;
jksoft 0:0a2f634d3324 60 int mylen = 0;
jksoft 0:0a2f634d3324 61
jksoft 0:0a2f634d3324 62 FUNC_ENTRY;
jksoft 0:0a2f634d3324 63 header.byte = readChar(&curdata);
jksoft 0:0a2f634d3324 64 if (header.bits.type != CONNECT)
jksoft 0:0a2f634d3324 65 goto exit;
jksoft 0:0a2f634d3324 66
jksoft 0:0a2f634d3324 67 curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
jksoft 0:0a2f634d3324 68
jksoft 0:0a2f634d3324 69 if (!readMQTTLenString(&Protocol, &curdata, enddata) ||
jksoft 0:0a2f634d3324 70 enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
jksoft 0:0a2f634d3324 71 goto exit;
jksoft 0:0a2f634d3324 72
jksoft 0:0a2f634d3324 73 version = (int)readChar(&curdata); /* Protocol version */
jksoft 0:0a2f634d3324 74 /* If we don't recognize the protocol version, we don't parse the connect packet on the
jksoft 0:0a2f634d3324 75 * basis that we don't know what the format will be.
jksoft 0:0a2f634d3324 76 */
jksoft 0:0a2f634d3324 77 if (MQTTPacket_checkVersion(&Protocol, version))
jksoft 0:0a2f634d3324 78 {
jksoft 0:0a2f634d3324 79 flags.all = readChar(&curdata);
jksoft 0:0a2f634d3324 80 data->cleansession = flags.bits.cleansession;
jksoft 0:0a2f634d3324 81 data->keepAliveInterval = readInt(&curdata);
jksoft 0:0a2f634d3324 82 if (!readMQTTLenString(&data->clientID, &curdata, enddata))
jksoft 0:0a2f634d3324 83 goto exit;
jksoft 0:0a2f634d3324 84 if (flags.bits.will)
jksoft 0:0a2f634d3324 85 {
jksoft 0:0a2f634d3324 86 data->willFlag = 1;
jksoft 0:0a2f634d3324 87 data->will.qos = flags.bits.willQoS;
jksoft 0:0a2f634d3324 88 data->will.retained = flags.bits.willRetain;
jksoft 0:0a2f634d3324 89 if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) ||
jksoft 0:0a2f634d3324 90 !readMQTTLenString(&data->will.message, &curdata, enddata))
jksoft 0:0a2f634d3324 91 goto exit;
jksoft 0:0a2f634d3324 92 }
jksoft 0:0a2f634d3324 93 if (flags.bits.username)
jksoft 0:0a2f634d3324 94 {
jksoft 0:0a2f634d3324 95 if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata))
jksoft 0:0a2f634d3324 96 goto exit; /* username flag set, but no username supplied - invalid */
jksoft 0:0a2f634d3324 97 if (flags.bits.password &&
jksoft 0:0a2f634d3324 98 (enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata)))
jksoft 0:0a2f634d3324 99 goto exit; /* password flag set, but no password supplied - invalid */
jksoft 0:0a2f634d3324 100 }
jksoft 0:0a2f634d3324 101 else if (flags.bits.password)
jksoft 0:0a2f634d3324 102 goto exit; /* password flag set without username - invalid */
jksoft 0:0a2f634d3324 103 rc = 1;
jksoft 0:0a2f634d3324 104 }
jksoft 0:0a2f634d3324 105 exit:
jksoft 0:0a2f634d3324 106 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 107 return rc;
jksoft 0:0a2f634d3324 108 }
jksoft 0:0a2f634d3324 109
jksoft 0:0a2f634d3324 110
jksoft 0:0a2f634d3324 111 /**
jksoft 0:0a2f634d3324 112 * Serializes the connack packet into the supplied buffer.
jksoft 0:0a2f634d3324 113 * @param buf the buffer into which the packet will be serialized
jksoft 0:0a2f634d3324 114 * @param buflen the length in bytes of the supplied buffer
jksoft 0:0a2f634d3324 115 * @param connack_rc the integer connack return code to be used
jksoft 0:0a2f634d3324 116 * @param sessionPresent the MQTT 3.1.1 sessionPresent flag
jksoft 0:0a2f634d3324 117 * @return serialized length, or error if 0
jksoft 0:0a2f634d3324 118 */
jksoft 0:0a2f634d3324 119 int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent)
jksoft 0:0a2f634d3324 120 {
jksoft 0:0a2f634d3324 121 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 122 int rc = 0;
jksoft 0:0a2f634d3324 123 unsigned char *ptr = buf;
jksoft 0:0a2f634d3324 124 MQTTConnackFlags flags = {0};
jksoft 0:0a2f634d3324 125
jksoft 0:0a2f634d3324 126 FUNC_ENTRY;
jksoft 0:0a2f634d3324 127 if (buflen < 2)
jksoft 0:0a2f634d3324 128 {
jksoft 0:0a2f634d3324 129 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 0:0a2f634d3324 130 goto exit;
jksoft 0:0a2f634d3324 131 }
jksoft 0:0a2f634d3324 132 header.byte = 0;
jksoft 0:0a2f634d3324 133 header.bits.type = CONNACK;
jksoft 0:0a2f634d3324 134 writeChar(&ptr, header.byte); /* write header */
jksoft 0:0a2f634d3324 135
jksoft 0:0a2f634d3324 136 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
jksoft 0:0a2f634d3324 137
jksoft 0:0a2f634d3324 138 flags.all = 0;
jksoft 0:0a2f634d3324 139 flags.bits.sessionpresent = sessionPresent;
jksoft 0:0a2f634d3324 140 writeChar(&ptr, flags.all);
jksoft 0:0a2f634d3324 141 writeChar(&ptr, connack_rc);
jksoft 0:0a2f634d3324 142
jksoft 0:0a2f634d3324 143 rc = ptr - buf;
jksoft 0:0a2f634d3324 144 exit:
jksoft 0:0a2f634d3324 145 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 146 return rc;
jksoft 0:0a2f634d3324 147 }
jksoft 0:0a2f634d3324 148