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