【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 "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 * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
jksoft 0:0a2f634d3324 24 * @param count the number of topic filter strings in topicFilters
jksoft 0:0a2f634d3324 25 * @param topicFilters the array of topic filter strings to be used in the publish
jksoft 0:0a2f634d3324 26 * @return the length of buffer needed to contain the serialized version of the packet
jksoft 0:0a2f634d3324 27 */
jksoft 0:0a2f634d3324 28 int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
jksoft 0:0a2f634d3324 29 {
jksoft 0:0a2f634d3324 30 int i;
jksoft 0:0a2f634d3324 31 int len = 2; /* packetid */
jksoft 0:0a2f634d3324 32
jksoft 0:0a2f634d3324 33 for (i = 0; i < count; ++i)
jksoft 0:0a2f634d3324 34 len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
jksoft 0:0a2f634d3324 35 return len;
jksoft 0:0a2f634d3324 36 }
jksoft 0:0a2f634d3324 37
jksoft 0:0a2f634d3324 38
jksoft 0:0a2f634d3324 39 /**
jksoft 0:0a2f634d3324 40 * Serializes the supplied subscribe data into the supplied buffer, ready for sending
jksoft 0:0a2f634d3324 41 * @param buf the buffer into which the packet will be serialized
jksoft 0:0a2f634d3324 42 * @param buflen the length in bytes of the supplied bufferr
jksoft 0:0a2f634d3324 43 * @param dup integer - the MQTT dup flag
jksoft 0:0a2f634d3324 44 * @param packetid integer - the MQTT packet identifier
jksoft 0:0a2f634d3324 45 * @param count - number of members in the topicFilters and reqQos arrays
jksoft 0:0a2f634d3324 46 * @param topicFilters - array of topic filter names
jksoft 0:0a2f634d3324 47 * @param requestedQoSs - array of requested QoS
jksoft 0:0a2f634d3324 48 * @return the length of the serialized data. <= 0 indicates error
jksoft 0:0a2f634d3324 49 */
jksoft 0:0a2f634d3324 50 int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
jksoft 0:0a2f634d3324 51 MQTTString topicFilters[], int requestedQoSs[])
jksoft 0:0a2f634d3324 52 {
jksoft 0:0a2f634d3324 53 unsigned char *ptr = buf;
jksoft 0:0a2f634d3324 54 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 55 int rem_len = 0;
jksoft 0:0a2f634d3324 56 int rc = 0;
jksoft 0:0a2f634d3324 57 int i = 0;
jksoft 0:0a2f634d3324 58
jksoft 0:0a2f634d3324 59 FUNC_ENTRY;
jksoft 0:0a2f634d3324 60 if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
jksoft 0:0a2f634d3324 61 {
jksoft 0:0a2f634d3324 62 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 0:0a2f634d3324 63 goto exit;
jksoft 0:0a2f634d3324 64 }
jksoft 0:0a2f634d3324 65
jksoft 0:0a2f634d3324 66 header.byte = 0;
jksoft 0:0a2f634d3324 67 header.bits.type = SUBSCRIBE;
jksoft 0:0a2f634d3324 68 header.bits.dup = dup;
jksoft 0:0a2f634d3324 69 header.bits.qos = 1;
jksoft 0:0a2f634d3324 70 writeChar(&ptr, header.byte); /* write header */
jksoft 0:0a2f634d3324 71
jksoft 0:0a2f634d3324 72 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
jksoft 0:0a2f634d3324 73
jksoft 0:0a2f634d3324 74 writeInt(&ptr, packetid);
jksoft 0:0a2f634d3324 75
jksoft 0:0a2f634d3324 76 for (i = 0; i < count; ++i)
jksoft 0:0a2f634d3324 77 {
jksoft 0:0a2f634d3324 78 writeMQTTString(&ptr, topicFilters[i]);
jksoft 0:0a2f634d3324 79 writeChar(&ptr, requestedQoSs[i]);
jksoft 0:0a2f634d3324 80 }
jksoft 0:0a2f634d3324 81
jksoft 0:0a2f634d3324 82 rc = ptr - buf;
jksoft 0:0a2f634d3324 83 exit:
jksoft 0:0a2f634d3324 84 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 85 return rc;
jksoft 0:0a2f634d3324 86 }
jksoft 0:0a2f634d3324 87
jksoft 0:0a2f634d3324 88
jksoft 0:0a2f634d3324 89
jksoft 0:0a2f634d3324 90 /**
jksoft 0:0a2f634d3324 91 * Deserializes the supplied (wire) buffer into suback data
jksoft 0:0a2f634d3324 92 * @param packetid returned integer - the MQTT packet identifier
jksoft 0:0a2f634d3324 93 * @param maxcount - the maximum number of members allowed in the grantedQoSs array
jksoft 0:0a2f634d3324 94 * @param count returned integer - number of members in the grantedQoSs array
jksoft 0:0a2f634d3324 95 * @param grantedQoSs returned array of integers - the granted qualities of service
jksoft 0:0a2f634d3324 96 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 0:0a2f634d3324 97 * @param buflen the length in bytes of the data in the supplied buffer
jksoft 0:0a2f634d3324 98 * @return error code. 1 is success, 0 is failure
jksoft 0:0a2f634d3324 99 */
jksoft 0:0a2f634d3324 100 int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen)
jksoft 0:0a2f634d3324 101 {
jksoft 0:0a2f634d3324 102 MQTTHeader header = {0};
jksoft 0:0a2f634d3324 103 unsigned char* curdata = buf;
jksoft 0:0a2f634d3324 104 unsigned char* enddata = NULL;
jksoft 0:0a2f634d3324 105 int rc = 0;
jksoft 0:0a2f634d3324 106 int mylen;
jksoft 0:0a2f634d3324 107
jksoft 0:0a2f634d3324 108 FUNC_ENTRY;
jksoft 0:0a2f634d3324 109 header.byte = readChar(&curdata);
jksoft 0:0a2f634d3324 110 if (header.bits.type != SUBACK)
jksoft 0:0a2f634d3324 111 goto exit;
jksoft 0:0a2f634d3324 112
jksoft 0:0a2f634d3324 113 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
jksoft 0:0a2f634d3324 114 enddata = curdata + mylen;
jksoft 0:0a2f634d3324 115 if (enddata - curdata < 2)
jksoft 0:0a2f634d3324 116 goto exit;
jksoft 0:0a2f634d3324 117
jksoft 0:0a2f634d3324 118 *packetid = readInt(&curdata);
jksoft 0:0a2f634d3324 119
jksoft 0:0a2f634d3324 120 *count = 0;
jksoft 0:0a2f634d3324 121 while (curdata < enddata)
jksoft 0:0a2f634d3324 122 {
jksoft 0:0a2f634d3324 123 if (*count > maxcount)
jksoft 0:0a2f634d3324 124 {
jksoft 0:0a2f634d3324 125 rc = -1;
jksoft 0:0a2f634d3324 126 goto exit;
jksoft 0:0a2f634d3324 127 }
jksoft 0:0a2f634d3324 128 grantedQoSs[(*count)++] = readChar(&curdata);
jksoft 0:0a2f634d3324 129 }
jksoft 0:0a2f634d3324 130
jksoft 0:0a2f634d3324 131 rc = 1;
jksoft 0:0a2f634d3324 132 exit:
jksoft 0:0a2f634d3324 133 FUNC_EXIT_RC(rc);
jksoft 0:0a2f634d3324 134 return rc;
jksoft 0:0a2f634d3324 135 }
jksoft 0:0a2f634d3324 136
jksoft 0:0a2f634d3324 137