Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /* * Copyright (c) 2018 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <logging/log.h> LOG_MODULE_REGISTER(net_mqtt_rx, CONFIG_MQTT_LOG_LEVEL); #include "mqtt_internal.h" #include "mqtt_transport.h" #include "mqtt_os.h" /** @file mqtt_rx.c * * @brief MQTT Received data handling. */ static int mqtt_handle_packet(struct mqtt_client *client, u8_t type_and_flags, u32_t var_length, struct buf_ctx *buf) { int err_code = 0; bool notify_event = true; struct mqtt_evt evt; /* Success by default, overwritten in special cases. */ evt.result = 0; switch (type_and_flags & 0xF0) { case MQTT_PKT_TYPE_CONNACK: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_CONNACK!", client); evt.type = MQTT_EVT_CONNACK; err_code = connect_ack_decode(client, buf, &evt.param.connack); if (err_code == 0) { MQTT_TRC("[CID %p]: return_code: %d", client, evt.param.connack.return_code); if (evt.param.connack.return_code == MQTT_CONNECTION_ACCEPTED) { /* Set state. */ MQTT_SET_STATE(client, MQTT_STATE_CONNECTED); } evt.result = evt.param.connack.return_code; } else { evt.result = err_code; } break; case MQTT_PKT_TYPE_PUBLISH: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_PUBLISH", client); evt.type = MQTT_EVT_PUBLISH; err_code = publish_decode(type_and_flags, var_length, buf, &evt.param.publish); evt.result = err_code; client->internal.remaining_payload = evt.param.publish.message.payload.len; MQTT_TRC("PUB QoS:%02x, message len %08x, topic len %08x", evt.param.publish.message.topic.qos, evt.param.publish.message.payload.len, evt.param.publish.message.topic.topic.size); break; case MQTT_PKT_TYPE_PUBACK: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_PUBACK!", client); evt.type = MQTT_EVT_PUBACK; err_code = publish_ack_decode(buf, &evt.param.puback); evt.result = err_code; break; case MQTT_PKT_TYPE_PUBREC: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_PUBREC!", client); evt.type = MQTT_EVT_PUBREC; err_code = publish_receive_decode(buf, &evt.param.pubrec); evt.result = err_code; break; case MQTT_PKT_TYPE_PUBREL: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_PUBREL!", client); evt.type = MQTT_EVT_PUBREL; err_code = publish_release_decode(buf, &evt.param.pubrel); evt.result = err_code; break; case MQTT_PKT_TYPE_PUBCOMP: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_PUBCOMP!", client); evt.type = MQTT_EVT_PUBCOMP; err_code = publish_complete_decode(buf, &evt.param.pubcomp); evt.result = err_code; break; case MQTT_PKT_TYPE_SUBACK: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_SUBACK!", client); evt.type = MQTT_EVT_SUBACK; err_code = subscribe_ack_decode(buf, &evt.param.suback); evt.result = err_code; break; case MQTT_PKT_TYPE_UNSUBACK: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_UNSUBACK!", client); evt.type = MQTT_EVT_UNSUBACK; err_code = unsubscribe_ack_decode(buf, &evt.param.unsuback); evt.result = err_code; break; case MQTT_PKT_TYPE_PINGRSP: MQTT_TRC("[CID %p]: Received MQTT_PKT_TYPE_PINGRSP!", client); /* No notification of Ping response to application. */ notify_event = false; break; default: /* Nothing to notify. */ notify_event = false; break; } if (notify_event == true) { event_notify(client, &evt); } return err_code; } static int mqtt_read_message_chunk(struct mqtt_client *client, struct buf_ctx *buf, u32_t length) { int remaining; int len; /* Calculate how much data we need to read from the transport, * given the already buffered data. */ remaining = length - (buf->end - buf->cur); if (remaining <= 0) { return 0; } /* Check if read does not exceed the buffer. */ if (buf->end + remaining > client->rx_buf + client->rx_buf_size) { MQTT_ERR("[CID %p]: Buffer too small to receive the message", client); return -ENOMEM; } len = mqtt_transport_read(client, buf->end, remaining, false); if (len < 0) { MQTT_TRC("[CID %p]: Transport read error: %d", client, len); return len; } if (len == 0) { MQTT_TRC("[CID %p]: Connection closed.", client); return -ENOTCONN; } client->internal.rx_buf_datalen += len; buf->end += len; if (len < remaining) { MQTT_TRC("[CID %p]: Message partially received.", client); return -EAGAIN; } return 0; } static int mqtt_read_publish_var_header(struct mqtt_client *client, u8_t type_and_flags, struct buf_ctx *buf) { u8_t qos = (type_and_flags & MQTT_HEADER_QOS_MASK) >> 1; int err_code; u32_t variable_header_length; /* Read topic length field. */ err_code = mqtt_read_message_chunk(client, buf, sizeof(u16_t)); if (err_code < 0) { return err_code; } variable_header_length = *buf->cur << 8; /* MSB */ variable_header_length |= *(buf->cur + 1); /* LSB */ /* Add two bytes for topic length field. */ variable_header_length += sizeof(u16_t); /* Add two bytes for message_id, if needed. */ if (qos > MQTT_QOS_0_AT_MOST_ONCE) { variable_header_length += sizeof(u16_t); } /* Now we can read the whole header. */ err_code = mqtt_read_message_chunk(client, buf, variable_header_length); if (err_code < 0) { return err_code; } return 0; } static int mqtt_read_and_parse_fixed_header(struct mqtt_client *client, u8_t *type_and_flags, u32_t *var_length, struct buf_ctx *buf) { /* Read the mandatory part of the fixed header in first iteration. */ u8_t chunk_size = MQTT_FIXED_HEADER_MIN_SIZE; int err_code; do { err_code = mqtt_read_message_chunk(client, buf, chunk_size); if (err_code < 0) { return err_code; } /* Reset to pointer to the beginning of the frame. */ buf->cur = client->rx_buf; chunk_size = 1U; err_code = fixed_header_decode(buf, type_and_flags, var_length); } while (err_code == -EAGAIN); return err_code; } int mqtt_handle_rx(struct mqtt_client *client) { int err_code; u8_t type_and_flags; u32_t var_length; struct buf_ctx buf; buf.cur = client->rx_buf; buf.end = client->rx_buf + client->internal.rx_buf_datalen; err_code = mqtt_read_and_parse_fixed_header(client, &type_and_flags, &var_length, &buf); if (err_code < 0) { return (err_code == -EAGAIN) ? 0 : err_code; } if ((type_and_flags & 0xF0) == MQTT_PKT_TYPE_PUBLISH) { err_code = mqtt_read_publish_var_header(client, type_and_flags, &buf); } else { err_code = mqtt_read_message_chunk(client, &buf, var_length); } if (err_code < 0) { return (err_code == -EAGAIN) ? 0 : err_code; } /* At this point, packet is ready to be passed to the application. */ err_code = mqtt_handle_packet(client, type_and_flags, var_length, &buf); if (err_code < 0) { return err_code; } client->internal.rx_buf_datalen = 0U; return 0; } |