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