【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) ? 1 : 0)
jksoft 0:0a2f634d3324 22
jksoft 0:0a2f634d3324 23 /**
jksoft 0:0a2f634d3324 24 * Deserializes the supplied (wire) buffer into publish data
jksoft 0:0a2f634d3324 25 * @param dup returned integer - the MQTT dup flag
jksoft 0:0a2f634d3324 26 * @param qos returned integer - the MQTT QoS value
jksoft 0:0a2f634d3324 27 * @param retained returned integer - the MQTT retained flag
jksoft 0:0a2f634d3324 28 * @param packetid returned integer - the MQTT packet identifier
jksoft 0:0a2f634d3324 29 * @param topicName returned MQTTString - the MQTT topic in the publish
jksoft 0:0a2f634d3324 30 * @param payload returned byte buffer - the MQTT publish payload
jksoft 0:0a2f634d3324 31 * @param payloadlen returned integer - the length of the MQTT payload
jksoft 0:0a2f634d3324 32 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 0:0a2f634d3324 33 * @param buflen the length in bytes of the data in the supplied buffer
jksoft 0:0a2f634d3324 34 * @return error code. 1 is success
jksoft 0:0a2f634d3324 35 */
jksoft 0:0a2f634d3324 36 int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
jksoft 0:0a2f634d3324 37 unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
jksoft 0:0a2f634d3324 38 {
jksoft 0:0a2f634d3324 39 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 40 unsigned char* curdata = buf;
jksoft 0:0a2f634d3324 41 unsigned char* enddata = NULL;
jksoft 0:0a2f634d3324 42 int rc = 0;
jksoft 0:0a2f634d3324 43 int mylen = 0;
jksoft 0:0a2f634d3324 44
jksoft 0:0a2f634d3324 45 FUNC_ENTRY;
jksoft 0:0a2f634d3324 46 header.byte = readChar(&curdata);
jksoft 0:0a2f634d3324 47 if (header.bits.type != PUBLISH)
jksoft 0:0a2f634d3324 48 goto exit;
jksoft 0:0a2f634d3324 49 *dup = header.bits.dup;
jksoft 0:0a2f634d3324 50 *qos = header.bits.qos;
jksoft 0:0a2f634d3324 51 *retained = header.bits.retain;
jksoft 0:0a2f634d3324 52
jksoft 0:0a2f634d3324 53 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
jksoft 0:0a2f634d3324 54 enddata = curdata + mylen;
jksoft 0:0a2f634d3324 55
jksoft 0:0a2f634d3324 56 if (!readMQTTLenString(topicName, &curdata, enddata) ||
jksoft 0:0a2f634d3324 57 enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
jksoft 0:0a2f634d3324 58 goto exit;
jksoft 0:0a2f634d3324 59
jksoft 0:0a2f634d3324 60 if (*qos > 0)
jksoft 0:0a2f634d3324 61 *packetid = readInt(&curdata);
jksoft 0:0a2f634d3324 62
jksoft 0:0a2f634d3324 63 *payloadlen = enddata - curdata;
jksoft 0:0a2f634d3324 64 *payload = curdata;
jksoft 0:0a2f634d3324 65 rc = 1;
jksoft 0:0a2f634d3324 66 exit:
jksoft 0:0a2f634d3324 67 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 68 return rc;
jksoft 0:0a2f634d3324 69 }
jksoft 0:0a2f634d3324 70
jksoft 0:0a2f634d3324 71
jksoft 0:0a2f634d3324 72
jksoft 0:0a2f634d3324 73 /**
jksoft 0:0a2f634d3324 74 * Deserializes the supplied (wire) buffer into an ack
jksoft 0:0a2f634d3324 75 * @param packettype returned integer - the MQTT packet type
jksoft 0:0a2f634d3324 76 * @param dup returned integer - the MQTT dup flag
jksoft 0:0a2f634d3324 77 * @param packetid returned integer - the MQTT packet identifier
jksoft 0:0a2f634d3324 78 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 0:0a2f634d3324 79 * @param buflen the length in bytes of the data in the supplied buffer
jksoft 0:0a2f634d3324 80 * @return error code. 1 is success, 0 is failure
jksoft 0:0a2f634d3324 81 */
jksoft 0:0a2f634d3324 82 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
jksoft 0:0a2f634d3324 83 {
jksoft 0:0a2f634d3324 84 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 85 unsigned char* curdata = buf;
jksoft 0:0a2f634d3324 86 unsigned char* enddata = NULL;
jksoft 0:0a2f634d3324 87 int rc = 0;
jksoft 0:0a2f634d3324 88 int mylen;
jksoft 0:0a2f634d3324 89
jksoft 0:0a2f634d3324 90 FUNC_ENTRY;
jksoft 0:0a2f634d3324 91 header.byte = readChar(&curdata);
jksoft 0:0a2f634d3324 92 *dup = header.bits.dup;
jksoft 0:0a2f634d3324 93 *packettype = header.bits.type;
jksoft 0:0a2f634d3324 94
jksoft 0:0a2f634d3324 95 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
jksoft 0:0a2f634d3324 96 enddata = curdata + mylen;
jksoft 0:0a2f634d3324 97
jksoft 0:0a2f634d3324 98 if (enddata - curdata < 2)
jksoft 0:0a2f634d3324 99 goto exit;
jksoft 0:0a2f634d3324 100 *packetid = readInt(&curdata);
jksoft 0:0a2f634d3324 101
jksoft 0:0a2f634d3324 102 rc = 1;
jksoft 0:0a2f634d3324 103 exit:
jksoft 0:0a2f634d3324 104 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 105 return rc;
jksoft 0:0a2f634d3324 106 }
jksoft 0:0a2f634d3324 107