【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/
Dependents: mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn
MQTT/MQTTPacket/MQTTUnsubscribeClient.c@0:0a2f634d3324, 2017-02-09 (annotated)
- Committer:
- jksoft
- Date:
- Thu Feb 09 07:26:57 2017 +0000
- Revision:
- 0:0a2f634d3324
??
Who changed what in which revision?
User | Revision | Line number | New 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 unsubscribe 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_unsubscribeLength(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]); /* length + topic*/ |
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 unsubscribe data into the supplied buffer, ready for sending |
jksoft | 0:0a2f634d3324 | 41 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
jksoft | 0:0a2f634d3324 | 42 | * @param buflen the length in bytes of the data in the supplied buffer |
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 array |
jksoft | 0:0a2f634d3324 | 46 | * @param topicFilters - array of topic filter names |
jksoft | 0:0a2f634d3324 | 47 | * @return the length of the serialized data. <= 0 indicates error |
jksoft | 0:0a2f634d3324 | 48 | */ |
jksoft | 0:0a2f634d3324 | 49 | int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, |
jksoft | 0:0a2f634d3324 | 50 | int count, MQTTString topicFilters[]) |
jksoft | 0:0a2f634d3324 | 51 | { |
jksoft | 0:0a2f634d3324 | 52 | unsigned char *ptr = buf; |
jksoft | 0:0a2f634d3324 | 53 | MQTTHeader header = {0}; |
jksoft | 0:0a2f634d3324 | 54 | int rem_len = 0; |
jksoft | 0:0a2f634d3324 | 55 | int rc = -1; |
jksoft | 0:0a2f634d3324 | 56 | int i = 0; |
jksoft | 0:0a2f634d3324 | 57 | |
jksoft | 0:0a2f634d3324 | 58 | FUNC_ENTRY; |
jksoft | 0:0a2f634d3324 | 59 | if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) |
jksoft | 0:0a2f634d3324 | 60 | { |
jksoft | 0:0a2f634d3324 | 61 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
jksoft | 0:0a2f634d3324 | 62 | goto exit; |
jksoft | 0:0a2f634d3324 | 63 | } |
jksoft | 0:0a2f634d3324 | 64 | |
jksoft | 0:0a2f634d3324 | 65 | header.byte = 0; |
jksoft | 0:0a2f634d3324 | 66 | header.bits.type = UNSUBSCRIBE; |
jksoft | 0:0a2f634d3324 | 67 | header.bits.dup = dup; |
jksoft | 0:0a2f634d3324 | 68 | header.bits.qos = 1; |
jksoft | 0:0a2f634d3324 | 69 | writeChar(&ptr, header.byte); /* write header */ |
jksoft | 0:0a2f634d3324 | 70 | |
jksoft | 0:0a2f634d3324 | 71 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; |
jksoft | 0:0a2f634d3324 | 72 | |
jksoft | 0:0a2f634d3324 | 73 | writeInt(&ptr, packetid); |
jksoft | 0:0a2f634d3324 | 74 | |
jksoft | 0:0a2f634d3324 | 75 | for (i = 0; i < count; ++i) |
jksoft | 0:0a2f634d3324 | 76 | writeMQTTString(&ptr, topicFilters[i]); |
jksoft | 0:0a2f634d3324 | 77 | |
jksoft | 0:0a2f634d3324 | 78 | rc = ptr - buf; |
jksoft | 0:0a2f634d3324 | 79 | exit: |
jksoft | 0:0a2f634d3324 | 80 | FUNC_EXIT_RC(rc); |
jksoft | 0:0a2f634d3324 | 81 | return rc; |
jksoft | 0:0a2f634d3324 | 82 | } |
jksoft | 0:0a2f634d3324 | 83 | |
jksoft | 0:0a2f634d3324 | 84 | |
jksoft | 0:0a2f634d3324 | 85 | /** |
jksoft | 0:0a2f634d3324 | 86 | * Deserializes the supplied (wire) buffer into unsuback data |
jksoft | 0:0a2f634d3324 | 87 | * @param packetid returned integer - the MQTT packet identifier |
jksoft | 0:0a2f634d3324 | 88 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
jksoft | 0:0a2f634d3324 | 89 | * @param buflen the length in bytes of the data in the supplied buffer |
jksoft | 0:0a2f634d3324 | 90 | * @return error code. 1 is success, 0 is failure |
jksoft | 0:0a2f634d3324 | 91 | */ |
jksoft | 0:0a2f634d3324 | 92 | int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) |
jksoft | 0:0a2f634d3324 | 93 | { |
jksoft | 0:0a2f634d3324 | 94 | unsigned char type = 0; |
jksoft | 0:0a2f634d3324 | 95 | unsigned char dup = 0; |
jksoft | 0:0a2f634d3324 | 96 | int rc = 0; |
jksoft | 0:0a2f634d3324 | 97 | |
jksoft | 0:0a2f634d3324 | 98 | FUNC_ENTRY; |
jksoft | 0:0a2f634d3324 | 99 | rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen); |
jksoft | 0:0a2f634d3324 | 100 | if (type == UNSUBACK) |
jksoft | 0:0a2f634d3324 | 101 | rc = 1; |
jksoft | 0:0a2f634d3324 | 102 | FUNC_EXIT_RC(rc); |
jksoft | 0:0a2f634d3324 | 103 | return rc; |
jksoft | 0:0a2f634d3324 | 104 | } |
jksoft | 0:0a2f634d3324 | 105 | |
jksoft | 0:0a2f634d3324 | 106 |