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 /**
jksoft 24:6ba1245bf049 24 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
jksoft 24:6ba1245bf049 25 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
jksoft 24:6ba1245bf049 26 * @param topicName the topic name to be used in the publish
jksoft 24:6ba1245bf049 27 * @param payloadlen the length of the payload to be sent
jksoft 24:6ba1245bf049 28 * @return the length of buffer needed to contain the serialized version of the packet
jksoft 24:6ba1245bf049 29 */
jksoft 24:6ba1245bf049 30 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
jksoft 24:6ba1245bf049 31 {
jksoft 24:6ba1245bf049 32 int len = 0;
jksoft 24:6ba1245bf049 33
jksoft 24:6ba1245bf049 34 len += 2 + MQTTstrlen(topicName) + payloadlen;
jksoft 24:6ba1245bf049 35 if (qos > 0)
jksoft 24:6ba1245bf049 36 len += 2; /* packetid */
jksoft 24:6ba1245bf049 37 return len;
jksoft 24:6ba1245bf049 38 }
jksoft 24:6ba1245bf049 39
jksoft 24:6ba1245bf049 40
jksoft 24:6ba1245bf049 41 /**
jksoft 24:6ba1245bf049 42 * Serializes the supplied publish data into the supplied buffer, ready for sending
jksoft 24:6ba1245bf049 43 * @param buf the buffer into which the packet will be serialized
jksoft 24:6ba1245bf049 44 * @param buflen the length in bytes of the supplied buffer
jksoft 24:6ba1245bf049 45 * @param dup integer - the MQTT dup flag
jksoft 24:6ba1245bf049 46 * @param qos integer - the MQTT QoS value
jksoft 24:6ba1245bf049 47 * @param retained integer - the MQTT retained flag
jksoft 24:6ba1245bf049 48 * @param packetid integer - the MQTT packet identifier
jksoft 24:6ba1245bf049 49 * @param topicName MQTTString - the MQTT topic in the publish
jksoft 24:6ba1245bf049 50 * @param payload byte buffer - the MQTT publish payload
jksoft 24:6ba1245bf049 51 * @param payloadlen integer - the length of the MQTT payload
jksoft 24:6ba1245bf049 52 * @return the length of the serialized data. <= 0 indicates error
jksoft 24:6ba1245bf049 53 */
jksoft 24:6ba1245bf049 54 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
jksoft 24:6ba1245bf049 55 MQTTString topicName, unsigned char* payload, int payloadlen)
jksoft 24:6ba1245bf049 56 {
jksoft 24:6ba1245bf049 57 unsigned char *ptr = buf;
jksoft 24:6ba1245bf049 58 MQTTHeader header = {0};
jksoft 24:6ba1245bf049 59 int rem_len = 0;
jksoft 24:6ba1245bf049 60 int rc = 0;
jksoft 24:6ba1245bf049 61
jksoft 24:6ba1245bf049 62 FUNC_ENTRY;
jksoft 24:6ba1245bf049 63 if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
jksoft 24:6ba1245bf049 64 {
jksoft 24:6ba1245bf049 65 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 24:6ba1245bf049 66 goto exit;
jksoft 24:6ba1245bf049 67 }
jksoft 24:6ba1245bf049 68
jksoft 24:6ba1245bf049 69 header.bits.type = PUBLISH;
jksoft 24:6ba1245bf049 70 header.bits.dup = dup;
jksoft 24:6ba1245bf049 71 header.bits.qos = qos;
jksoft 24:6ba1245bf049 72 header.bits.retain = retained;
jksoft 24:6ba1245bf049 73 writeChar(&ptr, header.byte); /* write header */
jksoft 24:6ba1245bf049 74
jksoft 24:6ba1245bf049 75 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
jksoft 24:6ba1245bf049 76
jksoft 24:6ba1245bf049 77 writeMQTTString(&ptr, topicName);
jksoft 24:6ba1245bf049 78
jksoft 24:6ba1245bf049 79 if (qos > 0)
jksoft 24:6ba1245bf049 80 writeInt(&ptr, packetid);
jksoft 24:6ba1245bf049 81
jksoft 24:6ba1245bf049 82 memcpy(ptr, payload, payloadlen);
jksoft 24:6ba1245bf049 83 ptr += payloadlen;
jksoft 24:6ba1245bf049 84
jksoft 24:6ba1245bf049 85 rc = ptr - buf;
jksoft 24:6ba1245bf049 86
jksoft 24:6ba1245bf049 87 exit:
jksoft 24:6ba1245bf049 88 FUNC_EXIT_RC(rc);
jksoft 24:6ba1245bf049 89 return rc;
jksoft 24:6ba1245bf049 90 }
jksoft 24:6ba1245bf049 91
jksoft 24:6ba1245bf049 92
jksoft 24:6ba1245bf049 93
jksoft 24:6ba1245bf049 94 /**
jksoft 24:6ba1245bf049 95 * Serializes the ack packet into the supplied buffer.
jksoft 24:6ba1245bf049 96 * @param buf the buffer into which the packet will be serialized
jksoft 24:6ba1245bf049 97 * @param buflen the length in bytes of the supplied buffer
jksoft 24:6ba1245bf049 98 * @param type the MQTT packet type
jksoft 24:6ba1245bf049 99 * @param dup the MQTT dup flag
jksoft 24:6ba1245bf049 100 * @param packetid the MQTT packet identifier
jksoft 24:6ba1245bf049 101 * @return serialized length, or error if 0
jksoft 24:6ba1245bf049 102 */
jksoft 24:6ba1245bf049 103 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
jksoft 24:6ba1245bf049 104 {
jksoft 24:6ba1245bf049 105 MQTTHeader header = {0};
jksoft 24:6ba1245bf049 106 int rc = 0;
jksoft 24:6ba1245bf049 107 unsigned char *ptr = buf;
jksoft 24:6ba1245bf049 108
jksoft 24:6ba1245bf049 109 FUNC_ENTRY;
jksoft 24:6ba1245bf049 110 if (buflen < 4)
jksoft 24:6ba1245bf049 111 {
jksoft 24:6ba1245bf049 112 rc = MQTTPACKET_BUFFER_TOO_SHORT;
jksoft 24:6ba1245bf049 113 goto exit;
jksoft 24:6ba1245bf049 114 }
jksoft 24:6ba1245bf049 115 header.bits.type = packettype;
jksoft 24:6ba1245bf049 116 header.bits.dup = dup;
jksoft 24:6ba1245bf049 117 header.bits.qos = 0;
jksoft 24:6ba1245bf049 118 writeChar(&ptr, header.byte); /* write header */
jksoft 24:6ba1245bf049 119
jksoft 24:6ba1245bf049 120 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
jksoft 24:6ba1245bf049 121 writeInt(&ptr, packetid);
jksoft 24:6ba1245bf049 122 rc = ptr - buf;
jksoft 24:6ba1245bf049 123 exit:
jksoft 24:6ba1245bf049 124 FUNC_EXIT_RC(rc);
jksoft 24:6ba1245bf049 125 return rc;
jksoft 24:6ba1245bf049 126 }
jksoft 24:6ba1245bf049 127
jksoft 24:6ba1245bf049 128
jksoft 24:6ba1245bf049 129 /**
jksoft 24:6ba1245bf049 130 * Serializes a puback packet into the supplied buffer.
jksoft 24:6ba1245bf049 131 * @param buf the buffer into which the packet will be serialized
jksoft 24:6ba1245bf049 132 * @param buflen the length in bytes of the supplied buffer
jksoft 24:6ba1245bf049 133 * @param packetid integer - the MQTT packet identifier
jksoft 24:6ba1245bf049 134 * @return serialized length, or error if 0
jksoft 24:6ba1245bf049 135 */
jksoft 24:6ba1245bf049 136 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
jksoft 24:6ba1245bf049 137 {
jksoft 24:6ba1245bf049 138 return MQTTSerialize_ack(buf, buflen, PUBACK, packetid, 0);
jksoft 24:6ba1245bf049 139 }
jksoft 24:6ba1245bf049 140
jksoft 24:6ba1245bf049 141
jksoft 24:6ba1245bf049 142 /**
jksoft 24:6ba1245bf049 143 * Serializes a pubrel packet into the supplied buffer.
jksoft 24:6ba1245bf049 144 * @param buf the buffer into which the packet will be serialized
jksoft 24:6ba1245bf049 145 * @param buflen the length in bytes of the supplied buffer
jksoft 24:6ba1245bf049 146 * @param dup integer - the MQTT dup flag
jksoft 24:6ba1245bf049 147 * @param packetid integer - the MQTT packet identifier
jksoft 24:6ba1245bf049 148 * @return serialized length, or error if 0
jksoft 24:6ba1245bf049 149 */
jksoft 24:6ba1245bf049 150 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
jksoft 24:6ba1245bf049 151 {
jksoft 24:6ba1245bf049 152 return MQTTSerialize_ack(buf, buflen, PUBREL, packetid, dup);
jksoft 24:6ba1245bf049 153 }
jksoft 24:6ba1245bf049 154
jksoft 24:6ba1245bf049 155
jksoft 24:6ba1245bf049 156 /**
jksoft 24:6ba1245bf049 157 * Serializes a pubrel packet into the supplied buffer.
jksoft 24:6ba1245bf049 158 * @param buf the buffer into which the packet will be serialized
jksoft 24:6ba1245bf049 159 * @param buflen the length in bytes of the supplied buffer
jksoft 24:6ba1245bf049 160 * @param packetid integer - the MQTT packet identifier
jksoft 24:6ba1245bf049 161 * @return serialized length, or error if 0
jksoft 24:6ba1245bf049 162 */
jksoft 24:6ba1245bf049 163 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
jksoft 24:6ba1245bf049 164 {
jksoft 24:6ba1245bf049 165 return MQTTSerialize_ack(buf, buflen, PUBCOMP, packetid, 0);
jksoft 24:6ba1245bf049 166 }
jksoft 24:6ba1245bf049 167
jksoft 24:6ba1245bf049 168