Update revision to use TI's mqtt and Freertos.
Dependencies: mbed client server
Fork of cc3100_Test_mqtt_CM3 by
mqtt_V1/common/mqtt_common.h@3:a8c249046181, 2015-09-03 (annotated)
- Committer:
- dflet
- Date:
- Thu Sep 03 14:02:37 2015 +0000
- Revision:
- 3:a8c249046181
SPI Mode change 1 to 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dflet | 3:a8c249046181 | 1 | /****************************************************************************** |
dflet | 3:a8c249046181 | 2 | * |
dflet | 3:a8c249046181 | 3 | * Copyright (C) 2014 Texas Instruments Incorporated |
dflet | 3:a8c249046181 | 4 | * |
dflet | 3:a8c249046181 | 5 | * All rights reserved. Property of Texas Instruments Incorporated. |
dflet | 3:a8c249046181 | 6 | * Restricted rights to use, duplicate or disclose this code are |
dflet | 3:a8c249046181 | 7 | * granted through contract. |
dflet | 3:a8c249046181 | 8 | * |
dflet | 3:a8c249046181 | 9 | * The program may not be used without the written permission of |
dflet | 3:a8c249046181 | 10 | * Texas Instruments Incorporated or against the terms and conditions |
dflet | 3:a8c249046181 | 11 | * stipulated in the agreement under which this program has been supplied, |
dflet | 3:a8c249046181 | 12 | * and under no circumstances can it be used with non-TI connectivity device. |
dflet | 3:a8c249046181 | 13 | * |
dflet | 3:a8c249046181 | 14 | ******************************************************************************/ |
dflet | 3:a8c249046181 | 15 | |
dflet | 3:a8c249046181 | 16 | /* |
dflet | 3:a8c249046181 | 17 | mqtt_common.h |
dflet | 3:a8c249046181 | 18 | |
dflet | 3:a8c249046181 | 19 | This module outlines the interfaces that are common to both client amd |
dflet | 3:a8c249046181 | 20 | server components. The applications are not expected to utilize the |
dflet | 3:a8c249046181 | 21 | services outlined in this module. |
dflet | 3:a8c249046181 | 22 | */ |
dflet | 3:a8c249046181 | 23 | |
dflet | 3:a8c249046181 | 24 | #ifndef __MQTT_COMMON_H__ |
dflet | 3:a8c249046181 | 25 | #define __MQTT_COMMON_H__ |
dflet | 3:a8c249046181 | 26 | |
dflet | 3:a8c249046181 | 27 | /** @file mqtt_common.h |
dflet | 3:a8c249046181 | 28 | This file incorporates constructs that are common to both client and server |
dflet | 3:a8c249046181 | 29 | implementation. |
dflet | 3:a8c249046181 | 30 | |
dflet | 3:a8c249046181 | 31 | The applications are not expected to utlize the routines made available in |
dflet | 3:a8c249046181 | 32 | this module module. |
dflet | 3:a8c249046181 | 33 | |
dflet | 3:a8c249046181 | 34 | @note the routines in this module do not check for availability and |
dflet | 3:a8c249046181 | 35 | correctness of the input parameters |
dflet | 3:a8c249046181 | 36 | |
dflet | 3:a8c249046181 | 37 | @warning The module is expected to under-go changes whilst incorporating |
dflet | 3:a8c249046181 | 38 | support for the server. Therefore, it is suggested that applications do |
dflet | 3:a8c249046181 | 39 | not rely on the services provided in this module. |
dflet | 3:a8c249046181 | 40 | */ |
dflet | 3:a8c249046181 | 41 | |
dflet | 3:a8c249046181 | 42 | #include <stdio.h> |
dflet | 3:a8c249046181 | 43 | #include <stdlib.h> |
dflet | 3:a8c249046181 | 44 | #include <string.h> |
dflet | 3:a8c249046181 | 45 | #include <stdbool.h> |
dflet | 3:a8c249046181 | 46 | |
dflet | 3:a8c249046181 | 47 | #include "platform.h" |
dflet | 3:a8c249046181 | 48 | |
dflet | 3:a8c249046181 | 49 | #ifdef __cplusplus |
dflet | 3:a8c249046181 | 50 | extern "C" |
dflet | 3:a8c249046181 | 51 | { |
dflet | 3:a8c249046181 | 52 | #endif |
dflet | 3:a8c249046181 | 53 | |
dflet | 3:a8c249046181 | 54 | namespace mbed_mqtt { |
dflet | 3:a8c249046181 | 55 | |
dflet | 3:a8c249046181 | 56 | #define MQTT_COMMON_VERSTR "1.1.1" /**< Version of Common LIB */ |
dflet | 3:a8c249046181 | 57 | |
dflet | 3:a8c249046181 | 58 | #define MIN(a, b) ((a > b)? b : a) |
dflet | 3:a8c249046181 | 59 | |
dflet | 3:a8c249046181 | 60 | /** MQTT Message Types */ |
dflet | 3:a8c249046181 | 61 | #define MQTT_CONNECT 0x01 |
dflet | 3:a8c249046181 | 62 | #define MQTT_CONNACK 0x02 |
dflet | 3:a8c249046181 | 63 | #define MQTT_PUBLISH 0x03 |
dflet | 3:a8c249046181 | 64 | #define MQTT_PUBACK 0x04 |
dflet | 3:a8c249046181 | 65 | #define MQTT_PUBREC 0x05 |
dflet | 3:a8c249046181 | 66 | #define MQTT_PUBREL 0x06 |
dflet | 3:a8c249046181 | 67 | #define MQTT_PUBCOMP 0x07 |
dflet | 3:a8c249046181 | 68 | #define MQTT_SUBSCRIBE 0x08 |
dflet | 3:a8c249046181 | 69 | #define MQTT_SUBACK 0x09 |
dflet | 3:a8c249046181 | 70 | #define MQTT_UNSUBSCRIBE 0x0A |
dflet | 3:a8c249046181 | 71 | #define MQTT_UNSUBACK 0x0B |
dflet | 3:a8c249046181 | 72 | #define MQTT_PINGREQ 0x0C |
dflet | 3:a8c249046181 | 73 | #define MQTT_PINGRSP 0x0D |
dflet | 3:a8c249046181 | 74 | #define MQTT_DISCONNECT 0x0E |
dflet | 3:a8c249046181 | 75 | |
dflet | 3:a8c249046181 | 76 | #define MAX_FH_LEN 0x05 /**< MAX Length of Fixed Header */ |
dflet | 3:a8c249046181 | 77 | |
dflet | 3:a8c249046181 | 78 | /** Max number of bytes in remaining length field */ |
dflet | 3:a8c249046181 | 79 | #define MAX_REMLEN_BYTES (MAX_FH_LEN - 1) |
dflet | 3:a8c249046181 | 80 | |
dflet | 3:a8c249046181 | 81 | #define MAKE_FH_BYTE1(msg_type, flags) (uint8_t)((msg_type << 4) | flags) |
dflet | 3:a8c249046181 | 82 | |
dflet | 3:a8c249046181 | 83 | #define MAKE_FH_FLAGS(bool_dup, enum_qos, bool_retain) \ |
dflet | 3:a8c249046181 | 84 | (uint8_t)(((bool_dup << 3) | (enum_qos << 1) | bool_retain) & 0xF) |
dflet | 3:a8c249046181 | 85 | |
dflet | 3:a8c249046181 | 86 | #define QID_VMASK 0x3 |
dflet | 3:a8c249046181 | 87 | #define QOS_VALUE(enum_qos) (uint8_t)(enum_qos & QID_VMASK) |
dflet | 3:a8c249046181 | 88 | #define QFL_VALUE 0x80 /**< QOS Failure value (SUBACK) */ |
dflet | 3:a8c249046181 | 89 | |
dflet | 3:a8c249046181 | 90 | #define DUP_FLAG_VAL(bool_val) (uint8_t)(bool_val << 3) |
dflet | 3:a8c249046181 | 91 | |
dflet | 3:a8c249046181 | 92 | #define BOOL_RETAIN(fh_byte1) ((fh_byte1 & 0x1)? true : false) |
dflet | 3:a8c249046181 | 93 | #define BOOL_DUP(fh_byte1) ((fh_byte1 & 0x8)? true : false) |
dflet | 3:a8c249046181 | 94 | #define ENUM_QOS(fh_byte1) (enum mqtt_qos)((fh_byte1 & 0x6) >> 1) |
dflet | 3:a8c249046181 | 95 | |
dflet | 3:a8c249046181 | 96 | #define MSG_TYPE(fh_byte1) (uint8_t)((fh_byte1 & 0xf0) >> 4) |
dflet | 3:a8c249046181 | 97 | |
dflet | 3:a8c249046181 | 98 | static inline uint32_t buf_wr_nbytes(uint8_t *dst, const uint8_t *src, uint32_t n) |
dflet | 3:a8c249046181 | 99 | { |
dflet | 3:a8c249046181 | 100 | // printf("buf_wr_nbytes\r\n"); |
dflet | 3:a8c249046181 | 101 | uint32_t c = n; |
dflet | 3:a8c249046181 | 102 | while(c--) |
dflet | 3:a8c249046181 | 103 | *dst++ = *src++; |
dflet | 3:a8c249046181 | 104 | |
dflet | 3:a8c249046181 | 105 | return n; |
dflet | 3:a8c249046181 | 106 | } |
dflet | 3:a8c249046181 | 107 | |
dflet | 3:a8c249046181 | 108 | static inline uint32_t buf_set(uint8_t *dst, uint8_t val, uint32_t n) |
dflet | 3:a8c249046181 | 109 | { |
dflet | 3:a8c249046181 | 110 | uint32_t c = n; |
dflet | 3:a8c249046181 | 111 | while(c--) |
dflet | 3:a8c249046181 | 112 | *dst++ = val; |
dflet | 3:a8c249046181 | 113 | |
dflet | 3:a8c249046181 | 114 | return n; |
dflet | 3:a8c249046181 | 115 | } |
dflet | 3:a8c249046181 | 116 | |
dflet | 3:a8c249046181 | 117 | /** Writing 2 bytes entity in network byte order */ |
dflet | 3:a8c249046181 | 118 | static inline uint32_t buf_wr_nbo_2B(uint8_t *buf, uint16_t val) |
dflet | 3:a8c249046181 | 119 | { |
dflet | 3:a8c249046181 | 120 | buf[0] = (uint8_t)((val >> 8) & 0xFF); /* MSB */ |
dflet | 3:a8c249046181 | 121 | buf[1] = (uint8_t)((val) & 0xFF); /* LSB */ |
dflet | 3:a8c249046181 | 122 | return 2; |
dflet | 3:a8c249046181 | 123 | } |
dflet | 3:a8c249046181 | 124 | |
dflet | 3:a8c249046181 | 125 | /** Reading 2 bytes entity in network byte order */ |
dflet | 3:a8c249046181 | 126 | static inline uint32_t buf_rd_nbo_2B(const uint8_t *buf, uint16_t *val) |
dflet | 3:a8c249046181 | 127 | { |
dflet | 3:a8c249046181 | 128 | *val = (uint16_t)((buf[0] << 8) | (buf[1])); |
dflet | 3:a8c249046181 | 129 | return 2; |
dflet | 3:a8c249046181 | 130 | } |
dflet | 3:a8c249046181 | 131 | |
dflet | 3:a8c249046181 | 132 | /** @defgroup mqp_group MQTT Packet (MQP) Buffer structure |
dflet | 3:a8c249046181 | 133 | The core construct to encapsulate, construct and process a message |
dflet | 3:a8c249046181 | 134 | |
dflet | 3:a8c249046181 | 135 | @{ |
dflet | 3:a8c249046181 | 136 | */ |
dflet | 3:a8c249046181 | 137 | struct mqtt_packet { |
dflet | 3:a8c249046181 | 138 | |
dflet | 3:a8c249046181 | 139 | uint8_t msg_type; /**< MQTT Message Type */ |
dflet | 3:a8c249046181 | 140 | uint8_t fh_byte1; /**< Fixed Header: Byte1 */ |
dflet | 3:a8c249046181 | 141 | |
dflet | 3:a8c249046181 | 142 | uint16_t msg_id; /**< Msg transaction ID */ |
dflet | 3:a8c249046181 | 143 | |
dflet | 3:a8c249046181 | 144 | uint8_t n_refs; /**< # users of this msg */ |
dflet | 3:a8c249046181 | 145 | uint8_t pad[3]; |
dflet | 3:a8c249046181 | 146 | |
dflet | 3:a8c249046181 | 147 | uint8_t offset; /**< Start of data index */ |
dflet | 3:a8c249046181 | 148 | uint8_t fh_len; /**< Fix Header Length */ |
dflet | 3:a8c249046181 | 149 | uint16_t vh_len; /**< Var Header Length */ |
dflet | 3:a8c249046181 | 150 | uint32_t pl_len; /**< Pay Load Length */ |
dflet | 3:a8c249046181 | 151 | |
dflet | 3:a8c249046181 | 152 | uint32_t private_; |
dflet | 3:a8c249046181 | 153 | |
dflet | 3:a8c249046181 | 154 | uint32_t maxlen; /**< Maximum buffer size */ |
dflet | 3:a8c249046181 | 155 | uint8_t *buffer; /**< The attached buffer */ |
dflet | 3:a8c249046181 | 156 | |
dflet | 3:a8c249046181 | 157 | /** Method to free this packet to a particular pool */ |
dflet | 3:a8c249046181 | 158 | void (*free)(struct mqtt_packet *mqp); |
dflet | 3:a8c249046181 | 159 | |
dflet | 3:a8c249046181 | 160 | struct mqtt_packet *next; |
dflet | 3:a8c249046181 | 161 | }; |
dflet | 3:a8c249046181 | 162 | |
dflet | 3:a8c249046181 | 163 | /** @} */ |
dflet | 3:a8c249046181 | 164 | |
dflet | 3:a8c249046181 | 165 | #define MQP_FHEADER_BUF(mqp) (mqp->buffer + mqp->offset) |
dflet | 3:a8c249046181 | 166 | #define MQP_VHEADER_BUF(mqp) (MQP_FHEADER_BUF(mqp) + mqp->fh_len) |
dflet | 3:a8c249046181 | 167 | #define MQP_PAYLOAD_BUF(mqp) (MQP_VHEADER_BUF(mqp) + mqp->vh_len) |
dflet | 3:a8c249046181 | 168 | |
dflet | 3:a8c249046181 | 169 | #define MQP_CONTENT_LEN(mqp) (mqp->fh_len + mqp->vh_len + mqp->pl_len) |
dflet | 3:a8c249046181 | 170 | #define MQP_FREEBUF_LEN(mqp) (mqp->maxlen - mqp->offset - \ |
dflet | 3:a8c249046181 | 171 | MQP_CONTENT_LEN(mqp)) |
dflet | 3:a8c249046181 | 172 | |
dflet | 3:a8c249046181 | 173 | #define MQP_FHEADER_VAL(mqp) (mqp->fh_byte1) |
dflet | 3:a8c249046181 | 174 | #define MQP_FHEADER_MSG(mqp) (MSG_TYPE(MQP_FHEADER_VAL(mqp))) |
dflet | 3:a8c249046181 | 175 | #define MQP_FHEADER_FLG(mqp) (MSG_FLAGS(MQP_FHEADER_VAL(mqp))) |
dflet | 3:a8c249046181 | 176 | |
dflet | 3:a8c249046181 | 177 | #define DEFINE_MQP_VEC(num_mqp, mqp_vec) \ |
dflet | 3:a8c249046181 | 178 | static struct mqtt_packet mqp_vec[num_mqp]; |
dflet | 3:a8c249046181 | 179 | |
dflet | 3:a8c249046181 | 180 | #define DEFINE_MQP_BUF_VEC(num_mqp, mqp_vec, buf_len, buf_vec) \ |
dflet | 3:a8c249046181 | 181 | DEFINE_MQP_VEC(num_mqp, mqp_vec); \ |
dflet | 3:a8c249046181 | 182 | static uint8_t buf_vec[num_mqp][buf_len]; |
dflet | 3:a8c249046181 | 183 | |
dflet | 3:a8c249046181 | 184 | /*--------------------------------------------------------------------- |
dflet | 3:a8c249046181 | 185 | * Heleper MACROS for PUBLISH-RX Message Processing |
dflet | 3:a8c249046181 | 186 | *--------------------------------------------------------------------- |
dflet | 3:a8c249046181 | 187 | */ |
dflet | 3:a8c249046181 | 188 | |
dflet | 3:a8c249046181 | 189 | /** @defgroup rxpub_help_group Helper Macros for RX PUBLISH |
dflet | 3:a8c249046181 | 190 | @{ |
dflet | 3:a8c249046181 | 191 | */ |
dflet | 3:a8c249046181 | 192 | |
dflet | 3:a8c249046181 | 193 | /** Yields pointer to topic content */ |
dflet | 3:a8c249046181 | 194 | #define MQP_PUB_TOP_BUF(mqp) (MQP_VHEADER_BUF(mqp) + 2) |
dflet | 3:a8c249046181 | 195 | |
dflet | 3:a8c249046181 | 196 | /** Length or size of topic content */ |
dflet | 3:a8c249046181 | 197 | #define MQP_PUB_TOP_LEN(mqp) (mqp->vh_len - 2 - (mqp->msg_id? 2 : 0)) |
dflet | 3:a8c249046181 | 198 | |
dflet | 3:a8c249046181 | 199 | /** Yields pointer to payload data */ |
dflet | 3:a8c249046181 | 200 | #define MQP_PUB_PAY_BUF(mqp) (mqp->pl_len? MQP_PAYLOAD_BUF(mqp) : NULL) |
dflet | 3:a8c249046181 | 201 | |
dflet | 3:a8c249046181 | 202 | /** Length or size of payload data */ |
dflet | 3:a8c249046181 | 203 | #define MQP_PUB_PAY_LEN(mqp) (mqp->pl_len) |
dflet | 3:a8c249046181 | 204 | |
dflet | 3:a8c249046181 | 205 | /** @} */ |
dflet | 3:a8c249046181 | 206 | |
dflet | 3:a8c249046181 | 207 | /** @cond |
dflet | 3:a8c249046181 | 208 | CONNECT Message Flags, as outined in the MQTT Specification |
dflet | 3:a8c249046181 | 209 | */ |
dflet | 3:a8c249046181 | 210 | #define WILL_RETAIN_VAL 0x20 |
dflet | 3:a8c249046181 | 211 | #define WILL_CONFIG_VAL 0x04 |
dflet | 3:a8c249046181 | 212 | #define CLEAN_START_VAL 0x02 |
dflet | 3:a8c249046181 | 213 | #define USER_NAME_OPVAL 0x80 |
dflet | 3:a8c249046181 | 214 | #define PASS_WORD_OPVAL 0x40 |
dflet | 3:a8c249046181 | 215 | /** |
dflet | 3:a8c249046181 | 216 | @endcond |
dflet | 3:a8c249046181 | 217 | */ |
dflet | 3:a8c249046181 | 218 | |
dflet | 3:a8c249046181 | 219 | /** @cond |
dflet | 3:a8c249046181 | 220 | CONNACK 8bit Return Code, as outlined in the MQTT Specification |
dflet | 3:a8c249046181 | 221 | */ |
dflet | 3:a8c249046181 | 222 | #define CONNACK_RC_REQ_ACCEPT 0x00 |
dflet | 3:a8c249046181 | 223 | #define CONNACK_RC_BAD_PROTOV 0x01 |
dflet | 3:a8c249046181 | 224 | #define CONNACK_RC_CLI_REJECT 0x02 |
dflet | 3:a8c249046181 | 225 | #define CONNACK_RC_SVR_UNAVBL 0x03 |
dflet | 3:a8c249046181 | 226 | #define CONNACK_RC_BAD_USRPWD 0x04 |
dflet | 3:a8c249046181 | 227 | #define CONNACK_RC_NOT_AUTHED 0x05 |
dflet | 3:a8c249046181 | 228 | /** |
dflet | 3:a8c249046181 | 229 | @endcond |
dflet | 3:a8c249046181 | 230 | */ |
dflet | 3:a8c249046181 | 231 | |
dflet | 3:a8c249046181 | 232 | /** @defgroup lib_err_group LIBRARY Generated Error Codes |
dflet | 3:a8c249046181 | 233 | Library provides these codes as return values in several routines |
dflet | 3:a8c249046181 | 234 | |
dflet | 3:a8c249046181 | 235 | @{ |
dflet | 3:a8c249046181 | 236 | */ |
dflet | 3:a8c249046181 | 237 | #define MQP_ERR_NETWORK (-1) /**< Problem in network (sock err) */ |
dflet | 3:a8c249046181 | 238 | #define MQP_ERR_TIMEOUT (-2) /**< Net transaction has timed out */ |
dflet | 3:a8c249046181 | 239 | #define MQP_ERR_NET_OPS (-3) /**< Platform Net Ops un-available */ |
dflet | 3:a8c249046181 | 240 | #define MQP_ERR_FNPARAM (-4) /**< Invalid parameter(s) provided */ |
dflet | 3:a8c249046181 | 241 | #define MQP_ERR_PKT_AVL (-5) /**< No pkts are available in pool */ |
dflet | 3:a8c249046181 | 242 | #define MQP_ERR_PKT_LEN (-6) /**< Inadequate free buffer in pkt */ |
dflet | 3:a8c249046181 | 243 | #define MQP_ERR_NOTCONN (-7) /**< Lib isn't CONNECTED to server */ |
dflet | 3:a8c249046181 | 244 | #define MQP_ERR_BADCALL (-8) /**< Irrelevant call for LIB state */ |
dflet | 3:a8c249046181 | 245 | #define MQP_ERR_CONTENT (-9) /**< MSG / Data content has errors */ |
dflet | 3:a8c249046181 | 246 | #define MQP_ERR_LIBQUIT (-10) /**< Needs reboot library has quit */ |
dflet | 3:a8c249046181 | 247 | |
dflet | 3:a8c249046181 | 248 | |
dflet | 3:a8c249046181 | 249 | #define MQP_ERR_NOT_DEF (-32) /**< Value other than defined ones */ |
dflet | 3:a8c249046181 | 250 | |
dflet | 3:a8c249046181 | 251 | /** @} */ |
dflet | 3:a8c249046181 | 252 | |
dflet | 3:a8c249046181 | 253 | /*--------------------------------------------------------------------- |
dflet | 3:a8c249046181 | 254 | * Common Operations |
dflet | 3:a8c249046181 | 255 | *--------------------------------------------------------------------- |
dflet | 3:a8c249046181 | 256 | */ |
dflet | 3:a8c249046181 | 257 | |
dflet | 3:a8c249046181 | 258 | /** Free a MQTT Packet Buffer |
dflet | 3:a8c249046181 | 259 | Puts back the packet buffer in to the appropriate pool. |
dflet | 3:a8c249046181 | 260 | |
dflet | 3:a8c249046181 | 261 | @param[in] mqp packet buffer to be freed |
dflet | 3:a8c249046181 | 262 | @return none |
dflet | 3:a8c249046181 | 263 | */ |
dflet | 3:a8c249046181 | 264 | void mqp_free(struct mqtt_packet *mqp); |
dflet | 3:a8c249046181 | 265 | |
dflet | 3:a8c249046181 | 266 | /** Resets the attributes of MQTT Packet Holder to its init state |
dflet | 3:a8c249046181 | 267 | Not all fields are reset - entities such as offset, n_refs in addition |
dflet | 3:a8c249046181 | 268 | to buffer information are not updated. |
dflet | 3:a8c249046181 | 269 | |
dflet | 3:a8c249046181 | 270 | @param[in] mqp packet buffer to be reset |
dflet | 3:a8c249046181 | 271 | @return none |
dflet | 3:a8c249046181 | 272 | |
dflet | 3:a8c249046181 | 273 | @see mqp_init |
dflet | 3:a8c249046181 | 274 | */ |
dflet | 3:a8c249046181 | 275 | void mqp_reset(struct mqtt_packet *mqp); |
dflet | 3:a8c249046181 | 276 | |
dflet | 3:a8c249046181 | 277 | /** Initializes attributes of the MQTT Packet Holder. |
dflet | 3:a8c249046181 | 278 | This routine sets number of users of the MQTT Packet Holder to 1. However, |
dflet | 3:a8c249046181 | 279 | it leaves, if already provisioned, the reference to buffer and its size |
dflet | 3:a8c249046181 | 280 | un-altered. |
dflet | 3:a8c249046181 | 281 | |
dflet | 3:a8c249046181 | 282 | @param[in] mqp packet buffer to be initialized |
dflet | 3:a8c249046181 | 283 | @param[in] offset index in buffer to indicate start of the contents |
dflet | 3:a8c249046181 | 284 | @return none |
dflet | 3:a8c249046181 | 285 | */ |
dflet | 3:a8c249046181 | 286 | void mqp_init(struct mqtt_packet *mqp, uint8_t offset); |
dflet | 3:a8c249046181 | 287 | |
dflet | 3:a8c249046181 | 288 | /** Initialize MQTT Packet Holder and attach the buffer */ |
dflet | 3:a8c249046181 | 289 | static |
dflet | 3:a8c249046181 | 290 | inline void mqp_buffer_attach(struct mqtt_packet *mqp, uint8_t *buffer, uint32_t length, |
dflet | 3:a8c249046181 | 291 | uint8_t offset) |
dflet | 3:a8c249046181 | 292 | { |
dflet | 3:a8c249046181 | 293 | mqp_init(mqp, offset); |
dflet | 3:a8c249046181 | 294 | |
dflet | 3:a8c249046181 | 295 | mqp->buffer = buffer; |
dflet | 3:a8c249046181 | 296 | mqp->maxlen = length; |
dflet | 3:a8c249046181 | 297 | mqp->free = NULL; |
dflet | 3:a8c249046181 | 298 | |
dflet | 3:a8c249046181 | 299 | return; |
dflet | 3:a8c249046181 | 300 | } |
dflet | 3:a8c249046181 | 301 | |
dflet | 3:a8c249046181 | 302 | /** Description of UTF8 information as used by MQTT Library. */ |
dflet | 3:a8c249046181 | 303 | struct utf8_string { |
dflet | 3:a8c249046181 | 304 | |
dflet | 3:a8c249046181 | 305 | const char *buffer; /**< Refers to UTF8 content */ |
dflet | 3:a8c249046181 | 306 | uint16_t length; /**< Length of UTF8 content */ |
dflet | 3:a8c249046181 | 307 | }; |
dflet | 3:a8c249046181 | 308 | |
dflet | 3:a8c249046181 | 309 | /** Write UTF8 information into the buffer. |
dflet | 3:a8c249046181 | 310 | The UTF8 information includes content and its length. |
dflet | 3:a8c249046181 | 311 | |
dflet | 3:a8c249046181 | 312 | @warning The routine does not check for correctness of the paramters. |
dflet | 3:a8c249046181 | 313 | |
dflet | 3:a8c249046181 | 314 | @param[in] buf refers to memory to write UTF8 information into |
dflet | 3:a8c249046181 | 315 | @param[in] utf8 contains UTF8 information to be written |
dflet | 3:a8c249046181 | 316 | @return on success, number of bytes written, otherwise -1 on error. |
dflet | 3:a8c249046181 | 317 | */ |
dflet | 3:a8c249046181 | 318 | int32_t mqp_buf_wr_utf8(uint8_t *buf, const struct utf8_string *utf8); |
dflet | 3:a8c249046181 | 319 | |
dflet | 3:a8c249046181 | 320 | /** Write the MQTT construct 'Remaining Length' into trailing end of buffer. |
dflet | 3:a8c249046181 | 321 | The 'remaining length' is written in the format as outlined in the MQTT |
dflet | 3:a8c249046181 | 322 | specification. |
dflet | 3:a8c249046181 | 323 | |
dflet | 3:a8c249046181 | 324 | The implementation assumes availability of at-least 4 bytes in the buffer. |
dflet | 3:a8c249046181 | 325 | Depending on the value of 'Remaining Length' appropriate trailing bytes in |
dflet | 3:a8c249046181 | 326 | the buffer would be used. |
dflet | 3:a8c249046181 | 327 | |
dflet | 3:a8c249046181 | 328 | @param[in] buf refers to memory to tail-write 'Remaining Length' into |
dflet | 3:a8c249046181 | 329 | @param[in] remlen The 'Remaining Length' value |
dflet | 3:a8c249046181 | 330 | @return in success, number of trailing bytes used, otherwise -1 on error |
dflet | 3:a8c249046181 | 331 | */ |
dflet | 3:a8c249046181 | 332 | int32_t mqp_buf_tail_wr_remlen(uint8_t *buf, uint32_t remlen); |
dflet | 3:a8c249046181 | 333 | |
dflet | 3:a8c249046181 | 334 | /** Read MQTT construct 'Remaining Length' from leading bytes of the buffer. |
dflet | 3:a8c249046181 | 335 | The 'remaining length' is written in the format as outlined in the MQTT |
dflet | 3:a8c249046181 | 336 | specification. |
dflet | 3:a8c249046181 | 337 | |
dflet | 3:a8c249046181 | 338 | @param[in] buf refers to memory to head-read 'Remaining Length' from |
dflet | 3:a8c249046181 | 339 | @param[in] remlen place-holder for The 'Remaining Length' value |
dflet | 3:a8c249046181 | 340 | @return in success, number of header bytes read, otherwise -1 on error |
dflet | 3:a8c249046181 | 341 | */ |
dflet | 3:a8c249046181 | 342 | int32_t mqp_buf_rd_remlen(uint8_t *buf, uint32_t *remlen); |
dflet | 3:a8c249046181 | 343 | |
dflet | 3:a8c249046181 | 344 | /** Include variable header Topic as part of PUB Message construction. |
dflet | 3:a8c249046181 | 345 | Inclusion of a Topic also encompasses incorporation of the message ID. |
dflet | 3:a8c249046181 | 346 | |
dflet | 3:a8c249046181 | 347 | The topic refers to the subject for which data will be published by |
dflet | 3:a8c249046181 | 348 | the client or the server. The topic entity must be appended into the |
dflet | 3:a8c249046181 | 349 | packet buffer prior to the inclusion of the payload (data). |
dflet | 3:a8c249046181 | 350 | |
dflet | 3:a8c249046181 | 351 | @warning This routine does not check for correctness of the input |
dflet | 3:a8c249046181 | 352 | parameters. |
dflet | 3:a8c249046181 | 353 | |
dflet | 3:a8c249046181 | 354 | @param[in] mqp packet buffer in which topic must be included. |
dflet | 3:a8c249046181 | 355 | @param[in] topic UTF8 information |
dflet | 3:a8c249046181 | 356 | @param[in] msg_id Message or Packet transaction ID |
dflet | 3:a8c249046181 | 357 | @return on success, number of bytes appended, otherwise -1 on error. |
dflet | 3:a8c249046181 | 358 | |
dflet | 3:a8c249046181 | 359 | @note A 'topic' must be appended prior to inclusion of pulished data. |
dflet | 3:a8c249046181 | 360 | */ |
dflet | 3:a8c249046181 | 361 | int32_t |
dflet | 3:a8c249046181 | 362 | mqp_pub_append_topic(struct mqtt_packet *mqp, const struct utf8_string *topic, |
dflet | 3:a8c249046181 | 363 | uint16_t msg_id); |
dflet | 3:a8c249046181 | 364 | |
dflet | 3:a8c249046181 | 365 | /** Include payload data for publishing |
dflet | 3:a8c249046181 | 366 | The payload data is associated with a topic. |
dflet | 3:a8c249046181 | 367 | |
dflet | 3:a8c249046181 | 368 | @warning This routine does not check for correctness of the input |
dflet | 3:a8c249046181 | 369 | parameters. |
dflet | 3:a8c249046181 | 370 | |
dflet | 3:a8c249046181 | 371 | @param[in] mqp packet buffer in which payload data must be included. |
dflet | 3:a8c249046181 | 372 | @param[in] data_buf data to be included in the packet buffer |
dflet | 3:a8c249046181 | 373 | @param[in] data_len length of the data to be included in the packet. |
dflet | 3:a8c249046181 | 374 | @return on success, number of bytes appended, otherwise -1 on error. |
dflet | 3:a8c249046181 | 375 | |
dflet | 3:a8c249046181 | 376 | @note A 'topic' must be appended prior to inclusion of pulished data. |
dflet | 3:a8c249046181 | 377 | */ |
dflet | 3:a8c249046181 | 378 | int32_t mqp_pub_append_data(struct mqtt_packet *mqp, const uint8_t *data_buf, |
dflet | 3:a8c249046181 | 379 | uint32_t data_len); |
dflet | 3:a8c249046181 | 380 | |
dflet | 3:a8c249046181 | 381 | /** Construct a packet for Message ID enabled ACK received from network |
dflet | 3:a8c249046181 | 382 | Process the raw ACK message information to update the packet holder. |
dflet | 3:a8c249046181 | 383 | |
dflet | 3:a8c249046181 | 384 | @warning This routine does not check for correctness of the input |
dflet | 3:a8c249046181 | 385 | parameters. |
dflet | 3:a8c249046181 | 386 | |
dflet | 3:a8c249046181 | 387 | @param[in] mqp_raw holds a raw buffer from the network |
dflet | 3:a8c249046181 | 388 | @param[in] has_payload asserted, if ACK message should have a payload |
dflet | 3:a8c249046181 | 389 | @return on success, true, otherwise false |
dflet | 3:a8c249046181 | 390 | */ |
dflet | 3:a8c249046181 | 391 | bool mqp_proc_msg_id_ack_rx(struct mqtt_packet *mqp_raw, bool has_payload); |
dflet | 3:a8c249046181 | 392 | |
dflet | 3:a8c249046181 | 393 | /** Construct a packet for PUBLISH message received from the network |
dflet | 3:a8c249046181 | 394 | Process the raw PUB message information to update the packet holder. |
dflet | 3:a8c249046181 | 395 | |
dflet | 3:a8c249046181 | 396 | @warning This routine does not check for correctness of the input |
dflet | 3:a8c249046181 | 397 | parameters. |
dflet | 3:a8c249046181 | 398 | |
dflet | 3:a8c249046181 | 399 | @param[in] mqp_raw holds a raw buffer from the network |
dflet | 3:a8c249046181 | 400 | @return on success, true, other wise false |
dflet | 3:a8c249046181 | 401 | */ |
dflet | 3:a8c249046181 | 402 | bool mqp_proc_pub_rx(struct mqtt_packet *mqp_raw); |
dflet | 3:a8c249046181 | 403 | |
dflet | 3:a8c249046181 | 404 | /* |
dflet | 3:a8c249046181 | 405 | Wait-List of MQTT Messages for which acknoledge is pending from remote node. |
dflet | 3:a8c249046181 | 406 | */ |
dflet | 3:a8c249046181 | 407 | struct mqtt_ack_wlist { |
dflet | 3:a8c249046181 | 408 | |
dflet | 3:a8c249046181 | 409 | struct mqtt_packet *head; /* Points to head of single linked-list. */ |
dflet | 3:a8c249046181 | 410 | struct mqtt_packet *tail; /* Points to tail of single linked-list. */ |
dflet | 3:a8c249046181 | 411 | }; |
dflet | 3:a8c249046181 | 412 | |
dflet | 3:a8c249046181 | 413 | static inline bool mqp_ack_wlist_is_empty(struct mqtt_ack_wlist *list) |
dflet | 3:a8c249046181 | 414 | { |
dflet | 3:a8c249046181 | 415 | return (NULL == list->head) ? true : false; |
dflet | 3:a8c249046181 | 416 | } |
dflet | 3:a8c249046181 | 417 | |
dflet | 3:a8c249046181 | 418 | /* |
dflet | 3:a8c249046181 | 419 | Add specified element into trailing end of list. |
dflet | 3:a8c249046181 | 420 | |
dflet | 3:a8c249046181 | 421 | Returns, on success, true, otherwise false. |
dflet | 3:a8c249046181 | 422 | */ |
dflet | 3:a8c249046181 | 423 | bool mqp_ack_wlist_append(struct mqtt_ack_wlist *list, |
dflet | 3:a8c249046181 | 424 | struct mqtt_packet *elem); |
dflet | 3:a8c249046181 | 425 | |
dflet | 3:a8c249046181 | 426 | /* |
dflet | 3:a8c249046181 | 427 | Removes element that has specified msg_id from list. |
dflet | 3:a8c249046181 | 428 | |
dflet | 3:a8c249046181 | 429 | Returns, on success, pointer to removed element, otherwise NULL. |
dflet | 3:a8c249046181 | 430 | */ |
dflet | 3:a8c249046181 | 431 | struct mqtt_packet *mqp_ack_wlist_remove(struct mqtt_ack_wlist *list, |
dflet | 3:a8c249046181 | 432 | uint16_t msg_id); |
dflet | 3:a8c249046181 | 433 | /* |
dflet | 3:a8c249046181 | 434 | Removes and frees all elements in list. |
dflet | 3:a8c249046181 | 435 | */ |
dflet | 3:a8c249046181 | 436 | void mqp_ack_wlist_purge(struct mqtt_ack_wlist *list); |
dflet | 3:a8c249046181 | 437 | |
dflet | 3:a8c249046181 | 438 | static inline bool is_wlist_empty(const struct mqtt_ack_wlist *list) |
dflet | 3:a8c249046181 | 439 | { |
dflet | 3:a8c249046181 | 440 | return list->head? false : true; |
dflet | 3:a8c249046181 | 441 | } |
dflet | 3:a8c249046181 | 442 | |
dflet | 3:a8c249046181 | 443 | /** Prepare the Fixed-Header of the MQTT Packet (before being sent to network) |
dflet | 3:a8c249046181 | 444 | Based on the contents of the mqtt packet and the combination of DUP, QoS |
dflet | 3:a8c249046181 | 445 | and Retain flags as outlined the MQTT specification, the routine updates, |
dflet | 3:a8c249046181 | 446 | among others, significant internal fields such as 'remaining length' and |
dflet | 3:a8c249046181 | 447 | 'fixed header length' in the packet construct and embeds the fixed header, |
dflet | 3:a8c249046181 | 448 | so created, in the packet buffer. |
dflet | 3:a8c249046181 | 449 | |
dflet | 3:a8c249046181 | 450 | This service must be utilized on a packet that has been already populated |
dflet | 3:a8c249046181 | 451 | with all the payload data, topics and other contents. The fixed header |
dflet | 3:a8c249046181 | 452 | must be the final step in the compostion of MQTT packet prior to its |
dflet | 3:a8c249046181 | 453 | dispatch to the server. |
dflet | 3:a8c249046181 | 454 | |
dflet | 3:a8c249046181 | 455 | Returns size, in bytes, of the fixed-header, otherwise -1 on error. |
dflet | 3:a8c249046181 | 456 | */ |
dflet | 3:a8c249046181 | 457 | int32_t mqp_prep_fh(struct mqtt_packet *mqp, uint8_t flags); |
dflet | 3:a8c249046181 | 458 | |
dflet | 3:a8c249046181 | 459 | /** MQTT Quality of Service */ |
dflet | 3:a8c249046181 | 460 | enum mqtt_qos { |
dflet | 3:a8c249046181 | 461 | |
dflet | 3:a8c249046181 | 462 | MQTT_QOS0, /**< QoS Level 0 */ |
dflet | 3:a8c249046181 | 463 | MQTT_QOS1, /**< QoS Level 1 */ |
dflet | 3:a8c249046181 | 464 | MQTT_QOS2 /**< QoS Level 2 */ |
dflet | 3:a8c249046181 | 465 | }; |
dflet | 3:a8c249046181 | 466 | |
dflet | 3:a8c249046181 | 467 | /** Construct to create Topic to SUBSCRIBE */ |
dflet | 3:a8c249046181 | 468 | struct utf8_strqos { |
dflet | 3:a8c249046181 | 469 | |
dflet | 3:a8c249046181 | 470 | const char *buffer; /**< Refers to UTF8 content */ |
dflet | 3:a8c249046181 | 471 | uint16_t length; /**< Length of UTF8 content */ |
dflet | 3:a8c249046181 | 472 | enum mqtt_qos qosreq; /**< QoS Level for content */ |
dflet | 3:a8c249046181 | 473 | }; |
dflet | 3:a8c249046181 | 474 | |
dflet | 3:a8c249046181 | 475 | |
dflet | 3:a8c249046181 | 476 | /** @defgroup mqtt_netsec_grp Information to establish a secure connection. |
dflet | 3:a8c249046181 | 477 | This is implementation specific and is targeted for the network services. |
dflet | 3:a8c249046181 | 478 | |
dflet | 3:a8c249046181 | 479 | Specifically, the MQTT implementation makes no assumption or use of this |
dflet | 3:a8c249046181 | 480 | construct. The client library merely passes information from the app to |
dflet | 3:a8c249046181 | 481 | the network service layer. |
dflet | 3:a8c249046181 | 482 | Note: value of n_file can vary from 1 to 4, with corresponding pointers to |
dflet | 3:a8c249046181 | 483 | the files in files field. Value of 1(n_file) will assume the corresponding |
dflet | 3:a8c249046181 | 484 | pointer is for CA File Name. Any other value of n_file expects the files to |
dflet | 3:a8c249046181 | 485 | be in following order: |
dflet | 3:a8c249046181 | 486 | 1. Private Key File |
dflet | 3:a8c249046181 | 487 | 2. Certificate File Name |
dflet | 3:a8c249046181 | 488 | 3. CA File Name |
dflet | 3:a8c249046181 | 489 | 4. DH Key File Name |
dflet | 3:a8c249046181 | 490 | |
dflet | 3:a8c249046181 | 491 | example: |
dflet | 3:a8c249046181 | 492 | If you want to provide only CA File Name, following are the two way of doing it: |
dflet | 3:a8c249046181 | 493 | for n_file = 1 |
dflet | 3:a8c249046181 | 494 | char *security_file_list[] = {"/cert/testcacert.der"}; |
dflet | 3:a8c249046181 | 495 | for n_file = 4 |
dflet | 3:a8c249046181 | 496 | char *security_file_list[] = {NULL, NULL, "/cert/testcacert.der", NULL}; |
dflet | 3:a8c249046181 | 497 | |
dflet | 3:a8c249046181 | 498 | where files = security_file_list |
dflet | 3:a8c249046181 | 499 | @{ |
dflet | 3:a8c249046181 | 500 | */ |
dflet | 3:a8c249046181 | 501 | struct secure_conn { |
dflet | 3:a8c249046181 | 502 | |
dflet | 3:a8c249046181 | 503 | void *method; /**< Reference to information about protocol or methods */ |
dflet | 3:a8c249046181 | 504 | void *cipher; /**< Reference to information about cryptograph ciphers */ |
dflet | 3:a8c249046181 | 505 | uint32_t n_file; /**< Count of secure connection related files, certs... */ |
dflet | 3:a8c249046181 | 506 | char **files; /**< Reference to array of file-names used for security */ |
dflet | 3:a8c249046181 | 507 | }; |
dflet | 3:a8c249046181 | 508 | |
dflet | 3:a8c249046181 | 509 | /** @} */ |
dflet | 3:a8c249046181 | 510 | |
dflet | 3:a8c249046181 | 511 | /* Initialize the struct secure_conn data */ |
dflet | 3:a8c249046181 | 512 | void secure_conn_struct_init(struct secure_conn *nw_security); |
dflet | 3:a8c249046181 | 513 | |
dflet | 3:a8c249046181 | 514 | /** @defgroup net_ops_group Abstraction of Network Services on a platform |
dflet | 3:a8c249046181 | 515 | Services to enable the MQTT Client-Server communication over network |
dflet | 3:a8c249046181 | 516 | |
dflet | 3:a8c249046181 | 517 | These services are invoked by the MQTT Library. |
dflet | 3:a8c249046181 | 518 | |
dflet | 3:a8c249046181 | 519 | @{ |
dflet | 3:a8c249046181 | 520 | */ |
dflet | 3:a8c249046181 | 521 | struct device_net_services { |
dflet | 3:a8c249046181 | 522 | |
dflet | 3:a8c249046181 | 523 | /** @defgroup dev_netconn_opt_grp Options for platform to configure network |
dflet | 3:a8c249046181 | 524 | @{ |
dflet | 3:a8c249046181 | 525 | */ |
dflet | 3:a8c249046181 | 526 | #define DEV_NETCONN_OPT_TCP 0x01 /**< Assert to indicate TCP net connection */ |
dflet | 3:a8c249046181 | 527 | #define DEV_NETCONN_OPT_UDP 0x02 /**< Assert to create a local UDP port bind */ |
dflet | 3:a8c249046181 | 528 | #define DEV_NETCONN_OPT_IP6 0x04 /**< Assert for IPv6, otherwise it is IPv4 */ |
dflet | 3:a8c249046181 | 529 | #define DEV_NETCONN_OPT_URL 0x08 /**< Assert if the network address is a URL */ |
dflet | 3:a8c249046181 | 530 | #define DEV_NETCONN_OPT_SEC 0x10 /**< Assert to indicate a secure connection */ |
dflet | 3:a8c249046181 | 531 | /** @} */ |
dflet | 3:a8c249046181 | 532 | |
dflet | 3:a8c249046181 | 533 | /** Set up a communication channel with a server or set up a local port. |
dflet | 3:a8c249046181 | 534 | This routine opens up either a "connection oriented" communication |
dflet | 3:a8c249046181 | 535 | channel with the specified server or set up a local configuration for |
dflet | 3:a8c249046181 | 536 | "connectionless" transactions. |
dflet | 3:a8c249046181 | 537 | |
dflet | 3:a8c249046181 | 538 | @param[in] nwconn_opts Implementation specific construct to enumerate |
dflet | 3:a8c249046181 | 539 | server address and / or connection related details |
dflet | 3:a8c249046181 | 540 | @param[in] server_addr URL or IP address (string) or other server |
dflet | 3:a8c249046181 | 541 | reference. For setting up a local (UDP) port, set it to NULL. |
dflet | 3:a8c249046181 | 542 | @param[in] port_number Network port number, typically, 1883 or 8883 |
dflet | 3:a8c249046181 | 543 | for remote severs. For setting up a local (UDP) port, use an intended |
dflet | 3:a8c249046181 | 544 | port number. |
dflet | 3:a8c249046181 | 545 | @param[in] nw_security Information to establish a secure connection |
dflet | 3:a8c249046181 | 546 | with server. Set it to NULL, if not used. @ref mqtt_netsec_grp |
dflet | 3:a8c249046181 | 547 | @return a valid handle to connection, otherwise NULL |
dflet | 3:a8c249046181 | 548 | */ |
dflet | 3:a8c249046181 | 549 | int32_t (*open)(uint32_t nwconn_opts, const char *server_addr, uint16_t port_number, |
dflet | 3:a8c249046181 | 550 | const struct secure_conn *nw_security); |
dflet | 3:a8c249046181 | 551 | |
dflet | 3:a8c249046181 | 552 | /** Send data onto the "connection oriented" network. |
dflet | 3:a8c249046181 | 553 | The routine blocks till the time, the data has been copied into the |
dflet | 3:a8c249046181 | 554 | network stack for dispatch on to the "connection oriented" network. |
dflet | 3:a8c249046181 | 555 | |
dflet | 3:a8c249046181 | 556 | @param[in] comm handle to network connection as returned by open(). |
dflet | 3:a8c249046181 | 557 | @param[in] buf refers to the data that is intended to be sent |
dflet | 3:a8c249046181 | 558 | @param[in] len length of the data |
dflet | 3:a8c249046181 | 559 | @param[in] ctx reference to the MQTT connection context |
dflet | 3:a8c249046181 | 560 | @return on success, the number of bytes sent, 0 on connection reset, |
dflet | 3:a8c249046181 | 561 | otherwise -1 |
dflet | 3:a8c249046181 | 562 | */ |
dflet | 3:a8c249046181 | 563 | int32_t (*send)(int32_t comm, const uint8_t *buf, uint32_t len, void *ctx); |
dflet | 3:a8c249046181 | 564 | |
dflet | 3:a8c249046181 | 565 | /** Receive data from the "connection oriented" channel. |
dflet | 3:a8c249046181 | 566 | The routine blocks till the time, there is either a data that has |
dflet | 3:a8c249046181 | 567 | been received from the server or the time to await data from the |
dflet | 3:a8c249046181 | 568 | server has expired. |
dflet | 3:a8c249046181 | 569 | |
dflet | 3:a8c249046181 | 570 | @param[in] comm Handle to network connection as returned by |
dflet | 3:a8c249046181 | 571 | accept(). |
dflet | 3:a8c249046181 | 572 | @param[out] buf place-holder to which data from network should be |
dflet | 3:a8c249046181 | 573 | written into. |
dflet | 3:a8c249046181 | 574 | @param[in] len maximum length of 'buf' |
dflet | 3:a8c249046181 | 575 | @param[in] wait_secs maximum time to await data from network. If |
dflet | 3:a8c249046181 | 576 | exceeded, the routine returns error with the err_timeo flag set |
dflet | 3:a8c249046181 | 577 | as true. |
dflet | 3:a8c249046181 | 578 | @param[out] err_timeo if set, indicates that error is due to |
dflet | 3:a8c249046181 | 579 | timeout. |
dflet | 3:a8c249046181 | 580 | @param[in] ctx reference to the MQTT connection context |
dflet | 3:a8c249046181 | 581 | @return on success, number of bytes received, 0 on connection reset, |
dflet | 3:a8c249046181 | 582 | otherwise -1 on error. In case, error (-1) is due to the time-out, |
dflet | 3:a8c249046181 | 583 | then the implementation should set flag err_timeo as true. |
dflet | 3:a8c249046181 | 584 | */ |
dflet | 3:a8c249046181 | 585 | int32_t (*recv)(int32_t comm, uint8_t *buf, uint32_t len, uint32_t wait_secs, |
dflet | 3:a8c249046181 | 586 | bool *err_timeo, void *ctx); |
dflet | 3:a8c249046181 | 587 | |
dflet | 3:a8c249046181 | 588 | /** Send data to particular port on the specified network element. |
dflet | 3:a8c249046181 | 589 | The routine blocks till the time, the data has been copied into the |
dflet | 3:a8c249046181 | 590 | network stack for dispatch to the "specified" network entity. |
dflet | 3:a8c249046181 | 591 | |
dflet | 3:a8c249046181 | 592 | @param[in] comm handle to network connection as returned by open(). |
dflet | 3:a8c249046181 | 593 | @param[in] buf refers to data that is intended to be sent |
dflet | 3:a8c249046181 | 594 | @param[in] len length of the data |
dflet | 3:a8c249046181 | 595 | @param[in] dest_port network port to which data is to be sent. |
dflet | 3:a8c249046181 | 596 | @param[in] dest_ip IP address of the entity to which data is to be |
dflet | 3:a8c249046181 | 597 | sent. |
dflet | 3:a8c249046181 | 598 | @param[in] ip_len length of the destination IP address. |
dflet | 3:a8c249046181 | 599 | @return on success, the number of bytes sent, 0 on connection reset, |
dflet | 3:a8c249046181 | 600 | otherwise -1. |
dflet | 3:a8c249046181 | 601 | */ |
dflet | 3:a8c249046181 | 602 | int32_t (*send_dest)(int32_t comm, const uint8_t *buf, uint32_t len, uint16_t dest_port, |
dflet | 3:a8c249046181 | 603 | const uint8_t *dest_ip, uint32_t ip_len); |
dflet | 3:a8c249046181 | 604 | |
dflet | 3:a8c249046181 | 605 | /** Receive data on a local port sent by any network element. |
dflet | 3:a8c249046181 | 606 | The routine blocks till the time, data has been received on the local |
dflet | 3:a8c249046181 | 607 | port from any remote network element. |
dflet | 3:a8c249046181 | 608 | |
dflet | 3:a8c249046181 | 609 | @param[in] comm handle to network connection as return by open(). |
dflet | 3:a8c249046181 | 610 | @param[in] buf place-holder to which data from network should be |
dflet | 3:a8c249046181 | 611 | written into. |
dflet | 3:a8c249046181 | 612 | @param[in] len maximum lengh of 'buf' |
dflet | 3:a8c249046181 | 613 | @param[out] from_port place-holder for the port of the sender network |
dflet | 3:a8c249046181 | 614 | entity |
dflet | 3:a8c249046181 | 615 | @param[out] from_ip place-holder to retrieve the IP address of the |
dflet | 3:a8c249046181 | 616 | sender network entity. The memory space must be provisioned to store |
dflet | 3:a8c249046181 | 617 | atleast 16 bytes. |
dflet | 3:a8c249046181 | 618 | @param[in, out] ip_len length of IP address. It is provided by |
dflet | 3:a8c249046181 | 619 | the caller to declare the length of the place holder and updated by |
dflet | 3:a8c249046181 | 620 | routine to indicate the length of the remote network entity's IP |
dflet | 3:a8c249046181 | 621 | address. |
dflet | 3:a8c249046181 | 622 | @return on success, number of bytes received, 0 on connection reset, |
dflet | 3:a8c249046181 | 623 | otherwise -1 on errir. |
dflet | 3:a8c249046181 | 624 | */ |
dflet | 3:a8c249046181 | 625 | int32_t (*recv_from)(int32_t comm, uint8_t *buf, uint32_t len, uint16_t *from_port, |
dflet | 3:a8c249046181 | 626 | uint8_t *from_ip, uint32_t *ip_len); |
dflet | 3:a8c249046181 | 627 | |
dflet | 3:a8c249046181 | 628 | /** Close communication connection */ |
dflet | 3:a8c249046181 | 629 | int32_t (*close)(int32_t comm); |
dflet | 3:a8c249046181 | 630 | |
dflet | 3:a8c249046181 | 631 | /** Listen to incoming connection from clients. |
dflet | 3:a8c249046181 | 632 | This routine prepares the system to listen on the specified port |
dflet | 3:a8c249046181 | 633 | for the incoming network connections from the remote clients. |
dflet | 3:a8c249046181 | 634 | |
dflet | 3:a8c249046181 | 635 | @param[in] nwconn_opts Implementation specific construct to |
dflet | 3:a8c249046181 | 636 | enumerate server address and / or connection related details |
dflet | 3:a8c249046181 | 637 | @param[in] port_number Network port number, typically, 1883 or 8883 |
dflet | 3:a8c249046181 | 638 | @param[in] nw_security Information to establish a secure connection |
dflet | 3:a8c249046181 | 639 | with client. Set it to NULL, if not used. @ref mqtt_netsec_grp |
dflet | 3:a8c249046181 | 640 | @return a valid handle to listening contruct, otherwise NULL |
dflet | 3:a8c249046181 | 641 | */ |
dflet | 3:a8c249046181 | 642 | int32_t (*listen)(uint32_t nwconn_opts, uint16_t port_number, |
dflet | 3:a8c249046181 | 643 | const struct secure_conn *nw_security); |
dflet | 3:a8c249046181 | 644 | |
dflet | 3:a8c249046181 | 645 | /** Accept an incominng connection. |
dflet | 3:a8c249046181 | 646 | This routine creates a new communication channel for the (remote) |
dflet | 3:a8c249046181 | 647 | requesting client. |
dflet | 3:a8c249046181 | 648 | |
dflet | 3:a8c249046181 | 649 | @param[in] listen handle to listen for the incoming connection |
dflet | 3:a8c249046181 | 650 | requests from the remote clients |
dflet | 3:a8c249046181 | 651 | @param[out] client_ip IP address of the connected client. This value |
dflet | 3:a8c249046181 | 652 | is valid only on successful return of the routine. The place holder |
dflet | 3:a8c249046181 | 653 | must provide memory to store atleast 16 bytes. |
dflet | 3:a8c249046181 | 654 | @param[in, out] ip_length Length of IP address. It is provided by |
dflet | 3:a8c249046181 | 655 | the caller to declare the length of the place holder and updated by |
dflet | 3:a8c249046181 | 656 | routine to indicate the length of the connected client's IP address. |
dflet | 3:a8c249046181 | 657 | @return on success, a valid handle to the new connection, otherwise |
dflet | 3:a8c249046181 | 658 | NULL |
dflet | 3:a8c249046181 | 659 | */ |
dflet | 3:a8c249046181 | 660 | int32_t (*accept)(int32_t listen, uint8_t *client_ip, uint32_t *ip_length); |
dflet | 3:a8c249046181 | 661 | |
dflet | 3:a8c249046181 | 662 | /** Monitor activity on communication handles. |
dflet | 3:a8c249046181 | 663 | The routine blocks for the specified period of time to monitor |
dflet | 3:a8c249046181 | 664 | activity, if any, on each of the communication handle that has |
dflet | 3:a8c249046181 | 665 | been provided in one or more vector sets. At the expiry of the |
dflet | 3:a8c249046181 | 666 | wait time, this function must identify the handles, on which, |
dflet | 3:a8c249046181 | 667 | acitvities were observed. |
dflet | 3:a8c249046181 | 668 | |
dflet | 3:a8c249046181 | 669 | A particular collection of communication handles are packed as |
dflet | 3:a8c249046181 | 670 | an array or in a vector and is passed to the routine. A NULL |
dflet | 3:a8c249046181 | 671 | handle in the vector indicates the termination of the vector |
dflet | 3:a8c249046181 | 672 | and can effectively used to account for the size of the vector. |
dflet | 3:a8c249046181 | 673 | |
dflet | 3:a8c249046181 | 674 | Similarly, at end the end of the wait period, the routine must |
dflet | 3:a8c249046181 | 675 | provide a vector of the handles for which activity was observed. |
dflet | 3:a8c249046181 | 676 | |
dflet | 3:a8c249046181 | 677 | @param[in, out] recv_hvec a vector of handles which must be |
dflet | 3:a8c249046181 | 678 | monitored for receive activities. |
dflet | 3:a8c249046181 | 679 | @param[in, out] send_hvec a vector of handles which must be |
dflet | 3:a8c249046181 | 680 | monitored for send activities. |
dflet | 3:a8c249046181 | 681 | @param[in, out] rsvd_hvec reserved for future use. |
dflet | 3:a8c249046181 | 682 | @param[in] wait_secs time to wait and monitor activity on |
dflet | 3:a8c249046181 | 683 | communication handles provided in one or more sets. If set |
dflet | 3:a8c249046181 | 684 | to 0, the routine returns immediately. |
dflet | 3:a8c249046181 | 685 | @return on success, the total number of handles for which activity |
dflet | 3:a8c249046181 | 686 | was observed. This number can be 0, if no activity was observed on |
dflet | 3:a8c249046181 | 687 | any of the provided handle in the specified time. Otherwise, -1 on |
dflet | 3:a8c249046181 | 688 | error. |
dflet | 3:a8c249046181 | 689 | */ |
dflet | 3:a8c249046181 | 690 | int32_t (*io_mon)(int32_t *recv_cvec, int32_t *send_cvec, |
dflet | 3:a8c249046181 | 691 | int32_t *rsvd_cvec, uint32_t wait_secs); |
dflet | 3:a8c249046181 | 692 | |
dflet | 3:a8c249046181 | 693 | /** Get Time (in seconds). |
dflet | 3:a8c249046181 | 694 | Provides a monotonically incrementing value of a time service in |
dflet | 3:a8c249046181 | 695 | unit of seconds. The implementation should ensure that associated |
dflet | 3:a8c249046181 | 696 | timer hardware or the clock module remains active through the low |
dflet | 3:a8c249046181 | 697 | power states of the system. Such an arrangement ensures that MQTT |
dflet | 3:a8c249046181 | 698 | Library is able to track the Keep-Alive time across the cycles of |
dflet | 3:a8c249046181 | 699 | low power states. It would be typical of battery operated systems |
dflet | 3:a8c249046181 | 700 | to transition to low power states during the period of inactivity |
dflet | 3:a8c249046181 | 701 | or otherwise to conserve battery. |
dflet | 3:a8c249046181 | 702 | |
dflet | 3:a8c249046181 | 703 | In the absence of a sustained time reference across the low power |
dflet | 3:a8c249046181 | 704 | states, if the system transitions away from the active state, the |
dflet | 3:a8c249046181 | 705 | MQTT Library, then may not be able to effectively monitor the Keep |
dflet | 3:a8c249046181 | 706 | Alive duration. |
dflet | 3:a8c249046181 | 707 | |
dflet | 3:a8c249046181 | 708 | It is the responsbililty of the implementation to manage the roll- |
dflet | 3:a8c249046181 | 709 | over problem of the hardware and ensure the integrity of the time |
dflet | 3:a8c249046181 | 710 | value is maintained. |
dflet | 3:a8c249046181 | 711 | |
dflet | 3:a8c249046181 | 712 | @return time in seconds |
dflet | 3:a8c249046181 | 713 | */ |
dflet | 3:a8c249046181 | 714 | uint32_t (*time)(void); |
dflet | 3:a8c249046181 | 715 | }; |
dflet | 3:a8c249046181 | 716 | |
dflet | 3:a8c249046181 | 717 | /** @} */ /* device_net_services */ |
dflet | 3:a8c249046181 | 718 | |
dflet | 3:a8c249046181 | 719 | /* Receive data from the specified network and read into the 'mqp' */ |
dflet | 3:a8c249046181 | 720 | int32_t mqp_recv(int32_t net, const struct device_net_services *net_ops, |
dflet | 3:a8c249046181 | 721 | struct mqtt_packet *mqp, uint32_t wait_secs, bool *timed_out, |
dflet | 3:a8c249046181 | 722 | void *ctx); |
dflet | 3:a8c249046181 | 723 | |
dflet | 3:a8c249046181 | 724 | /*----------------------------------------------------------------------------- |
dflet | 3:a8c249046181 | 725 | * Data structure for managing the QoS2 PUB RX packets and follow-ups |
dflet | 3:a8c249046181 | 726 | *---------------------------------------------------------------------------*/ |
dflet | 3:a8c249046181 | 727 | |
dflet | 3:a8c249046181 | 728 | #define MAX_PUBREL_INFLT 8 /* Must be kept as a value of 2^n */ |
dflet | 3:a8c249046181 | 729 | |
dflet | 3:a8c249046181 | 730 | struct pub_qos2_cq { /* Circular Queue CQ to track QOS2 PUB RX messages */ |
dflet | 3:a8c249046181 | 731 | |
dflet | 3:a8c249046181 | 732 | uint16_t id_vec[MAX_PUBREL_INFLT]; /* Vector to store RX Message-IDs */ |
dflet | 3:a8c249046181 | 733 | uint8_t n_free; /* Num of free elements in vector */ |
dflet | 3:a8c249046181 | 734 | uint8_t rd_idx; /* Index to Read next Message-ID */ |
dflet | 3:a8c249046181 | 735 | uint8_t wr_idx; /* Index to Write next Message-ID */ |
dflet | 3:a8c249046181 | 736 | }; |
dflet | 3:a8c249046181 | 737 | |
dflet | 3:a8c249046181 | 738 | /* Reset the specified Circular Queue (CQ) */ |
dflet | 3:a8c249046181 | 739 | void qos2_pub_cq_reset(struct pub_qos2_cq *cq); |
dflet | 3:a8c249046181 | 740 | |
dflet | 3:a8c249046181 | 741 | /* Append the message-id into the CQ tail. Return true on success, else false */ |
dflet | 3:a8c249046181 | 742 | bool qos2_pub_cq_logup(struct pub_qos2_cq *cq, uint16_t msg_id); |
dflet | 3:a8c249046181 | 743 | |
dflet | 3:a8c249046181 | 744 | /* Remove the message-id from the CQ head. Return true on success, else false */ |
dflet | 3:a8c249046181 | 745 | bool qos2_pub_cq_unlog(struct pub_qos2_cq *cq, uint16_t msg_id); |
dflet | 3:a8c249046181 | 746 | |
dflet | 3:a8c249046181 | 747 | /* Is the message-id available in the CQ ? Return true on success, else false */ |
dflet | 3:a8c249046181 | 748 | bool qos2_pub_cq_check(struct pub_qos2_cq *cq, uint16_t msg_id); |
dflet | 3:a8c249046181 | 749 | |
dflet | 3:a8c249046181 | 750 | /* Get the count of message-ID(s) availalbe in the CQ */ |
dflet | 3:a8c249046181 | 751 | static inline int32_t qos2_pub_cq_count(struct pub_qos2_cq *cq) |
dflet | 3:a8c249046181 | 752 | { |
dflet | 3:a8c249046181 | 753 | return MAX_PUBREL_INFLT - cq->n_free; |
dflet | 3:a8c249046181 | 754 | } |
dflet | 3:a8c249046181 | 755 | |
dflet | 3:a8c249046181 | 756 | struct client_ctx { |
dflet | 3:a8c249046181 | 757 | |
dflet | 3:a8c249046181 | 758 | void *usr; /* Client Usr */ |
dflet | 3:a8c249046181 | 759 | int32_t net; /* Socket HND */ |
dflet | 3:a8c249046181 | 760 | |
dflet | 3:a8c249046181 | 761 | uint8_t remote_ip[16]; |
dflet | 3:a8c249046181 | 762 | uint32_t ip_length; |
dflet | 3:a8c249046181 | 763 | |
dflet | 3:a8c249046181 | 764 | uint32_t timeout; |
dflet | 3:a8c249046181 | 765 | uint16_t ka_secs; |
dflet | 3:a8c249046181 | 766 | |
dflet | 3:a8c249046181 | 767 | uint32_t flags; |
dflet | 3:a8c249046181 | 768 | |
dflet | 3:a8c249046181 | 769 | struct client_ctx *next; |
dflet | 3:a8c249046181 | 770 | }; |
dflet | 3:a8c249046181 | 771 | |
dflet | 3:a8c249046181 | 772 | void cl_ctx_reset(struct client_ctx *cl_ctx); |
dflet | 3:a8c249046181 | 773 | void cl_ctx_timeout_insert(struct client_ctx **head, |
dflet | 3:a8c249046181 | 774 | struct client_ctx *elem); |
dflet | 3:a8c249046181 | 775 | |
dflet | 3:a8c249046181 | 776 | void cl_ctx_remove(struct client_ctx **head, |
dflet | 3:a8c249046181 | 777 | struct client_ctx *elem); |
dflet | 3:a8c249046181 | 778 | |
dflet | 3:a8c249046181 | 779 | #define KA_TIMEOUT_NONE 0xffffffff /* Different than KA SECS = 0 */ |
dflet | 3:a8c249046181 | 780 | void cl_ctx_timeout_update(struct client_ctx *cl_ctx, uint32_t now_secs); |
dflet | 3:a8c249046181 | 781 | |
dflet | 3:a8c249046181 | 782 | }//namespace mbed_mqtt |
dflet | 3:a8c249046181 | 783 | |
dflet | 3:a8c249046181 | 784 | #ifdef __cplusplus |
dflet | 3:a8c249046181 | 785 | } |
dflet | 3:a8c249046181 | 786 | #endif |
dflet | 3:a8c249046181 | 787 | |
dflet | 3:a8c249046181 | 788 | #endif |
dflet | 3:a8c249046181 | 789 |