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