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 unsubscribe 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_unsubscribeLength(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]); /* length + topic*/
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 unsubscribe data into the supplied buffer, ready for sending
jksoft 24:6ba1245bf049 41 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 24:6ba1245bf049 42 * @param buflen the length in bytes of the data in the supplied buffer
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 array
jksoft 24:6ba1245bf049 46 * @param topicFilters - array of topic filter names
jksoft 24:6ba1245bf049 47 * @return the length of the serialized data. <= 0 indicates error
jksoft 24:6ba1245bf049 48 */
jksoft 24:6ba1245bf049 49 int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
jksoft 24:6ba1245bf049 50 int count, MQTTString topicFilters[])
jksoft 24:6ba1245bf049 51 {
jksoft 24:6ba1245bf049 52 unsigned char *ptr = buf;
jksoft 24:6ba1245bf049 53 MQTTHeader header = {0};
jksoft 24:6ba1245bf049 54 int rem_len = 0;
jksoft 24:6ba1245bf049 55 int rc = -1;
jksoft 24:6ba1245bf049 56 int i = 0;
jksoft 24:6ba1245bf049 57
jksoft 24:6ba1245bf049 58 FUNC_ENTRY;
jksoft 24:6ba1245bf049 59 if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
jksoft 24:6ba1245bf049 60 {
jksoft 24:6ba1245bf049 61 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 24:6ba1245bf049 62 goto exit;
jksoft 24:6ba1245bf049 63 }
jksoft 24:6ba1245bf049 64
jksoft 24:6ba1245bf049 65 header.byte = 0;
jksoft 24:6ba1245bf049 66 header.bits.type = UNSUBSCRIBE;
jksoft 24:6ba1245bf049 67 header.bits.dup = dup;
jksoft 24:6ba1245bf049 68 header.bits.qos = 1;
jksoft 24:6ba1245bf049 69 writeChar(&ptr, header.byte); /* write header */
jksoft 24:6ba1245bf049 70
jksoft 24:6ba1245bf049 71 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
jksoft 24:6ba1245bf049 72
jksoft 24:6ba1245bf049 73 writeInt(&ptr, packetid);
jksoft 24:6ba1245bf049 74
jksoft 24:6ba1245bf049 75 for (i = 0; i < count; ++i)
jksoft 24:6ba1245bf049 76 writeMQTTString(&ptr, topicFilters[i]);
jksoft 24:6ba1245bf049 77
jksoft 24:6ba1245bf049 78 rc = ptr - buf;
jksoft 24:6ba1245bf049 79 exit:
jksoft 24:6ba1245bf049 80 FUNC_EXIT_RC(rc);
jksoft 24:6ba1245bf049 81 return rc;
jksoft 24:6ba1245bf049 82 }
jksoft 24:6ba1245bf049 83
jksoft 24:6ba1245bf049 84
jksoft 24:6ba1245bf049 85 /**
jksoft 24:6ba1245bf049 86 * Deserializes the supplied (wire) buffer into unsuback data
jksoft 24:6ba1245bf049 87 * @param packetid returned integer - the MQTT packet identifier
jksoft 24:6ba1245bf049 88 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jksoft 24:6ba1245bf049 89 * @param buflen the length in bytes of the data in the supplied buffer
jksoft 24:6ba1245bf049 90 * @return error code. 1 is success, 0 is failure
jksoft 24:6ba1245bf049 91 */
jksoft 24:6ba1245bf049 92 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
jksoft 24:6ba1245bf049 93 {
jksoft 24:6ba1245bf049 94 unsigned char type = 0;
jksoft 24:6ba1245bf049 95 unsigned char dup = 0;
jksoft 24:6ba1245bf049 96 int rc = 0;
jksoft 24:6ba1245bf049 97
jksoft 24:6ba1245bf049 98 FUNC_ENTRY;
jksoft 24:6ba1245bf049 99 rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
jksoft 24:6ba1245bf049 100 if (type == UNSUBACK)
jksoft 24:6ba1245bf049 101 rc = 1;
jksoft 24:6ba1245bf049 102 FUNC_EXIT_RC(rc);
jksoft 24:6ba1245bf049 103 return rc;
jksoft 24:6ba1245bf049 104 }
jksoft 24:6ba1245bf049 105
jksoft 24:6ba1245bf049 106