Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTSNUnsubscribeClient.c@0:c524a894b5e8, 2015-02-26 (annotated)
- Committer:
- icraggs
- Date:
- Thu Feb 26 15:59:36 2015 +0000
- Revision:
- 0:c524a894b5e8
- Child:
- 1:7fa362fa563f
First version that works nicely :-)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| icraggs | 0:c524a894b5e8 | 1 | /******************************************************************************* |
| icraggs | 0:c524a894b5e8 | 2 | * Copyright (c) 2014 IBM Corp. |
| icraggs | 0:c524a894b5e8 | 3 | * |
| icraggs | 0:c524a894b5e8 | 4 | * All rights reserved. This program and the accompanying materials |
| icraggs | 0:c524a894b5e8 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| icraggs | 0:c524a894b5e8 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
| icraggs | 0:c524a894b5e8 | 7 | * |
| icraggs | 0:c524a894b5e8 | 8 | * The Eclipse Public License is available at |
| icraggs | 0:c524a894b5e8 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
| icraggs | 0:c524a894b5e8 | 10 | * and the Eclipse Distribution License is available at |
| icraggs | 0:c524a894b5e8 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
| icraggs | 0:c524a894b5e8 | 12 | * |
| icraggs | 0:c524a894b5e8 | 13 | * Contributors: |
| icraggs | 0:c524a894b5e8 | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
| icraggs | 0:c524a894b5e8 | 15 | *******************************************************************************/ |
| icraggs | 0:c524a894b5e8 | 16 | |
| icraggs | 0:c524a894b5e8 | 17 | #include "MQTTSNPacket.h" |
| icraggs | 0:c524a894b5e8 | 18 | #include "StackTrace.h" |
| icraggs | 0:c524a894b5e8 | 19 | |
| icraggs | 0:c524a894b5e8 | 20 | #include <string.h> |
| icraggs | 0:c524a894b5e8 | 21 | |
| icraggs | 0:c524a894b5e8 | 22 | /** |
| icraggs | 0:c524a894b5e8 | 23 | * Determines the length of the MQTTSN subscribe packet that would be produced using the supplied parameters, |
| icraggs | 0:c524a894b5e8 | 24 | * excluding length |
| icraggs | 0:c524a894b5e8 | 25 | * @param topicName the topic name to be used in the publish |
| icraggs | 0:c524a894b5e8 | 26 | * @return the length of buffer needed to contain the serialized version of the packet |
| icraggs | 0:c524a894b5e8 | 27 | */ |
| icraggs | 0:c524a894b5e8 | 28 | int MQTTSNSerialize_unsubscribeLength(MQTTSN_topicid* topicFilter) |
| icraggs | 0:c524a894b5e8 | 29 | { |
| icraggs | 0:c524a894b5e8 | 30 | int len = 4; |
| icraggs | 0:c524a894b5e8 | 31 | |
| icraggs | 0:c524a894b5e8 | 32 | if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL) |
| icraggs | 0:c524a894b5e8 | 33 | len += topicFilter->data.long_.len; |
| icraggs | 0:c524a894b5e8 | 34 | else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT || topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED) |
| icraggs | 0:c524a894b5e8 | 35 | len += 2; |
| icraggs | 0:c524a894b5e8 | 36 | |
| icraggs | 0:c524a894b5e8 | 37 | return len; |
| icraggs | 0:c524a894b5e8 | 38 | } |
| icraggs | 0:c524a894b5e8 | 39 | |
| icraggs | 0:c524a894b5e8 | 40 | |
| icraggs | 0:c524a894b5e8 | 41 | int MQTTSNSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned short packetid, MQTTSN_topicid* topicFilter) |
| icraggs | 0:c524a894b5e8 | 42 | { |
| icraggs | 0:c524a894b5e8 | 43 | unsigned char *ptr = buf; |
| icraggs | 0:c524a894b5e8 | 44 | MQTTSNFlags flags; |
| icraggs | 0:c524a894b5e8 | 45 | int len = 0; |
| icraggs | 0:c524a894b5e8 | 46 | int rc = 0; |
| icraggs | 0:c524a894b5e8 | 47 | |
| icraggs | 0:c524a894b5e8 | 48 | FUNC_ENTRY; |
| icraggs | 0:c524a894b5e8 | 49 | if ((len = MQTTSNPacket_len(MQTTSNSerialize_unsubscribeLength(topicFilter))) > buflen) |
| icraggs | 0:c524a894b5e8 | 50 | { |
| icraggs | 0:c524a894b5e8 | 51 | rc = MQTTSNPACKET_BUFFER_TOO_SHORT; |
| icraggs | 0:c524a894b5e8 | 52 | goto exit; |
| icraggs | 0:c524a894b5e8 | 53 | } |
| icraggs | 0:c524a894b5e8 | 54 | ptr += MQTTSNPacket_encode(ptr, len); /* write length */ |
| icraggs | 0:c524a894b5e8 | 55 | writeChar(&ptr, MQTTSN_UNSUBSCRIBE); /* write message type */ |
| icraggs | 0:c524a894b5e8 | 56 | |
| icraggs | 0:c524a894b5e8 | 57 | flags.all = 0; |
| icraggs | 0:c524a894b5e8 | 58 | flags.bits.topicIdType = topicFilter->type; |
| icraggs | 0:c524a894b5e8 | 59 | writeChar(&ptr, flags.all); |
| icraggs | 0:c524a894b5e8 | 60 | |
| icraggs | 0:c524a894b5e8 | 61 | writeInt(&ptr, packetid); |
| icraggs | 0:c524a894b5e8 | 62 | |
| icraggs | 0:c524a894b5e8 | 63 | /* now the topic id or name */ |
| icraggs | 0:c524a894b5e8 | 64 | if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL) /* means long topic name */ |
| icraggs | 0:c524a894b5e8 | 65 | { |
| icraggs | 0:c524a894b5e8 | 66 | memcpy(ptr, topicFilter->data.long_.name, topicFilter->data.long_.len); |
| icraggs | 0:c524a894b5e8 | 67 | ptr += topicFilter->data.long_.len; |
| icraggs | 0:c524a894b5e8 | 68 | } |
| icraggs | 0:c524a894b5e8 | 69 | else if (topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED) |
| icraggs | 0:c524a894b5e8 | 70 | writeInt(&ptr, topicFilter->data.id); |
| icraggs | 0:c524a894b5e8 | 71 | else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT) |
| icraggs | 0:c524a894b5e8 | 72 | { |
| icraggs | 0:c524a894b5e8 | 73 | writeChar(&ptr, topicFilter->data.short_name[0]); |
| icraggs | 0:c524a894b5e8 | 74 | writeChar(&ptr, topicFilter->data.short_name[1]); |
| icraggs | 0:c524a894b5e8 | 75 | } |
| icraggs | 0:c524a894b5e8 | 76 | |
| icraggs | 0:c524a894b5e8 | 77 | rc = ptr - buf; |
| icraggs | 0:c524a894b5e8 | 78 | exit: |
| icraggs | 0:c524a894b5e8 | 79 | FUNC_EXIT_RC(rc); |
| icraggs | 0:c524a894b5e8 | 80 | return rc; |
| icraggs | 0:c524a894b5e8 | 81 | |
| icraggs | 0:c524a894b5e8 | 82 | } |
| icraggs | 0:c524a894b5e8 | 83 | |
| icraggs | 0:c524a894b5e8 | 84 | |
| icraggs | 0:c524a894b5e8 | 85 | /** |
| icraggs | 0:c524a894b5e8 | 86 | * Deserializes the supplied (wire) buffer into unsuback data |
| icraggs | 0:c524a894b5e8 | 87 | * @param packetid returned - the same value as the one contained in the corresponding SUBSCRIBE |
| icraggs | 0:c524a894b5e8 | 88 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
| icraggs | 0:c524a894b5e8 | 89 | * @param buflen the length in bytes of the data in the supplied buffer |
| icraggs | 0:c524a894b5e8 | 90 | * @return error code. 1 is success |
| icraggs | 0:c524a894b5e8 | 91 | */ |
| icraggs | 0:c524a894b5e8 | 92 | int MQTTSNDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) |
| icraggs | 0:c524a894b5e8 | 93 | { |
| icraggs | 0:c524a894b5e8 | 94 | unsigned char* curdata = buf; |
| icraggs | 0:c524a894b5e8 | 95 | unsigned char* enddata = NULL; |
| icraggs | 0:c524a894b5e8 | 96 | int rc = 0; |
| icraggs | 0:c524a894b5e8 | 97 | int mylen = 0; |
| icraggs | 0:c524a894b5e8 | 98 | |
| icraggs | 0:c524a894b5e8 | 99 | FUNC_ENTRY; |
| icraggs | 0:c524a894b5e8 | 100 | curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ |
| icraggs | 0:c524a894b5e8 | 101 | enddata = buf + mylen; |
| icraggs | 0:c524a894b5e8 | 102 | if (enddata - curdata > buflen) |
| icraggs | 0:c524a894b5e8 | 103 | goto exit; |
| icraggs | 0:c524a894b5e8 | 104 | |
| icraggs | 0:c524a894b5e8 | 105 | if (readChar(&curdata) != MQTTSN_UNSUBACK) |
| icraggs | 0:c524a894b5e8 | 106 | goto exit; |
| icraggs | 0:c524a894b5e8 | 107 | |
| icraggs | 0:c524a894b5e8 | 108 | *packetid = readInt(&curdata); |
| icraggs | 0:c524a894b5e8 | 109 | |
| icraggs | 0:c524a894b5e8 | 110 | rc = 1; |
| icraggs | 0:c524a894b5e8 | 111 | exit: |
| icraggs | 0:c524a894b5e8 | 112 | FUNC_EXIT_RC(rc); |
| icraggs | 0:c524a894b5e8 | 113 | return rc; |
| icraggs | 0:c524a894b5e8 | 114 | } |
| icraggs | 0:c524a894b5e8 | 115 | |
| icraggs | 0:c524a894b5e8 | 116 | |
| icraggs | 0:c524a894b5e8 | 117 | |
| icraggs | 0:c524a894b5e8 | 118 |