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

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

Committer:
jksoft
Date:
Thu Feb 09 07:26:57 2017 +0000
Revision:
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 /**
jksoft 0:0a2f634d3324 24 * Deserializes the supplied (wire) buffer into subscribe data
jksoft 0:0a2f634d3324 25 * @param dup integer returned - the MQTT dup flag
jksoft 0:0a2f634d3324 26 * @param packetid integer returned - the MQTT packet identifier
jksoft 0:0a2f634d3324 27 * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
jksoft 0:0a2f634d3324 28 * @param count - number of members in the topicFilters and requestedQoSs arrays
jksoft 0:0a2f634d3324 29 * @param topicFilters - array of topic filter names
jksoft 0:0a2f634d3324 30 * @param requestedQoSs - array of requested QoS
jksoft 0:0a2f634d3324 31 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 0:0a2f634d3324 32 * @param buflen the length in bytes of the data in the supplied buffer
jksoft 0:0a2f634d3324 33 * @return the length of the serialized data. <= 0 indicates error
jksoft 0:0a2f634d3324 34 */
jksoft 0:0a2f634d3324 35 int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
jksoft 0:0a2f634d3324 36 int requestedQoSs[], unsigned char* buf, int buflen)
jksoft 0:0a2f634d3324 37 {
jksoft 0:0a2f634d3324 38 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 39 unsigned char* curdata = buf;
jksoft 0:0a2f634d3324 40 unsigned char* enddata = NULL;
jksoft 0:0a2f634d3324 41 int rc = -1;
jksoft 0:0a2f634d3324 42 int mylen = 0;
jksoft 0:0a2f634d3324 43
jksoft 0:0a2f634d3324 44 FUNC_ENTRY;
jksoft 0:0a2f634d3324 45 header.byte = readChar(&curdata);
jksoft 0:0a2f634d3324 46 if (header.bits.type != SUBSCRIBE)
jksoft 0:0a2f634d3324 47 goto exit;
jksoft 0:0a2f634d3324 48 *dup = header.bits.dup;
jksoft 0:0a2f634d3324 49
jksoft 0:0a2f634d3324 50 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
jksoft 0:0a2f634d3324 51 enddata = curdata + mylen;
jksoft 0:0a2f634d3324 52
jksoft 0:0a2f634d3324 53 *packetid = readInt(&curdata);
jksoft 0:0a2f634d3324 54
jksoft 0:0a2f634d3324 55 *count = 0;
jksoft 0:0a2f634d3324 56 while (curdata < enddata)
jksoft 0:0a2f634d3324 57 {
jksoft 0:0a2f634d3324 58 if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
jksoft 0:0a2f634d3324 59 goto exit;
jksoft 0:0a2f634d3324 60 if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
jksoft 0:0a2f634d3324 61 goto exit;
jksoft 0:0a2f634d3324 62 requestedQoSs[*count] = readChar(&curdata);
jksoft 0:0a2f634d3324 63 (*count)++;
jksoft 0:0a2f634d3324 64 }
jksoft 0:0a2f634d3324 65
jksoft 0:0a2f634d3324 66 rc = 1;
jksoft 0:0a2f634d3324 67 exit:
jksoft 0:0a2f634d3324 68 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 69 return rc;
jksoft 0:0a2f634d3324 70 }
jksoft 0:0a2f634d3324 71
jksoft 0:0a2f634d3324 72
jksoft 0:0a2f634d3324 73 /**
jksoft 0:0a2f634d3324 74 * Serializes the supplied suback data into the supplied buffer, ready for sending
jksoft 0:0a2f634d3324 75 * @param buf the buffer into which the packet will be serialized
jksoft 0:0a2f634d3324 76 * @param buflen the length in bytes of the supplied buffer
jksoft 0:0a2f634d3324 77 * @param packetid integer - the MQTT packet identifier
jksoft 0:0a2f634d3324 78 * @param count - number of members in the grantedQoSs array
jksoft 0:0a2f634d3324 79 * @param grantedQoSs - array of granted QoS
jksoft 0:0a2f634d3324 80 * @return the length of the serialized data. <= 0 indicates error
jksoft 0:0a2f634d3324 81 */
jksoft 0:0a2f634d3324 82 int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
jksoft 0:0a2f634d3324 83 {
jksoft 0:0a2f634d3324 84 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 85 int rc = -1;
jksoft 0:0a2f634d3324 86 unsigned char *ptr = buf;
jksoft 0:0a2f634d3324 87 int i;
jksoft 0:0a2f634d3324 88
jksoft 0:0a2f634d3324 89 FUNC_ENTRY;
jksoft 0:0a2f634d3324 90 if (buflen < 2 + count)
jksoft 0:0a2f634d3324 91 {
jksoft 0:0a2f634d3324 92 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 0:0a2f634d3324 93 goto exit;
jksoft 0:0a2f634d3324 94 }
jksoft 0:0a2f634d3324 95 header.byte = 0;
jksoft 0:0a2f634d3324 96 header.bits.type = SUBACK;
jksoft 0:0a2f634d3324 97 writeChar(&ptr, header.byte); /* write header */
jksoft 0:0a2f634d3324 98
jksoft 0:0a2f634d3324 99 ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
jksoft 0:0a2f634d3324 100
jksoft 0:0a2f634d3324 101 writeInt(&ptr, packetid);
jksoft 0:0a2f634d3324 102
jksoft 0:0a2f634d3324 103 for (i = 0; i < count; ++i)
jksoft 0:0a2f634d3324 104 writeChar(&ptr, grantedQoSs[i]);
jksoft 0:0a2f634d3324 105
jksoft 0:0a2f634d3324 106 rc = ptr - buf;
jksoft 0:0a2f634d3324 107 exit:
jksoft 0:0a2f634d3324 108 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 109 return rc;
jksoft 0:0a2f634d3324 110 }
jksoft 0:0a2f634d3324 111
jksoft 0:0a2f634d3324 112