#define MQTT_QoS_2 (2)
+struct mqtt_topic {
+ char* name;
+ uint8_t QoS; /* 0, 1 or 2 */
+};
+
/***************************************************************************** */
/* Connect and Connack packets */
uint16_t keep_alive_seconds;
uint8_t clean_session_flag; /* Either MQTT_SESSION_RESUME or MQTT_SESSION_NEW */
char* client_id;
- char* will_topic;
- uint16_t will_msg_size; /* stored in host endianness */
- uint8_t will_QoS; /* 0, 1 or 2 */
+ struct mqtt_topic will_topic;
uint8_t will_retain; /* 0 or 1 */
+ uint16_t will_msg_size; /* stored in host endianness */
uint8_t* will_msg;
char* username;
char* password;
* A publish control packet is sent from a Client to a Server or from Server to a Client to transport
* an Application Message. */
struct mqtt_publish_pkt {
- char* topic;
+ struct mqtt_topic topic;
uint16_t packet_id; /* Packet identifier is required for publish if QoS > 0 */
- uint8_t QoS; /* 0, 1 or 2 */
uint8_t dup_flag; /* Accept 1 or MQTT_PUBLISH_DUP as flag set */
uint8_t retain_flag;
uint32_t message_size;
/* MQTT subscribe packet */
struct mqtt_subscribe_pkt {
uint16_t packet_id; /* Packet identifier */
- char* topic;
- uint8_t QoS; /* unshifted QoS value */
+ uint8_t nb_topics; /* Number of topics in the topics table. Limited to 125 */
+ struct mqtt_topic* topics; /* Table of topics */
};
/* MQTT subscribe response packet */
fxpl.connect_flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
}
/* Optionnal will message */
- if ((pkt->will_msg_size != 0) && (pkt->will_topic != NULL) && (pkt->will_msg != NULL)) {
+ if ((pkt->will_msg_size != 0) && (pkt->will_topic.name != NULL) && (pkt->will_msg != NULL)) {
fxpl.connect_flags |= MQTT_CONNECT_FLAG_WILL_FLAG;
- fxpl.connect_flags |= MQTT_CONNECT_FLAG_WILL_QoS(pkt->will_QoS);
+ fxpl.connect_flags |= MQTT_CONNECT_FLAG_WILL_QoS(pkt->will_topic.QoS);
if (pkt->will_retain != 0) {
fxpl.connect_flags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
}
- will_topic_len = strlen(pkt->will_topic);
+ will_topic_len = strlen(pkt->will_topic.name);
/* Update packet payload size */
remaining_length += will_topic_len + 2;
remaining_length += pkt->will_msg_size + 2;
len += mqtt_pack_str((buf + len), pkt->client_id, client_id_len);
/* Add optionnal Will message */
- if ((pkt->will_msg_size != 0) && (pkt->will_topic != NULL) && (pkt->will_msg != NULL)) {
+ if ((pkt->will_msg_size != 0) && (pkt->will_topic.name != NULL) && (pkt->will_msg != NULL)) {
uint16_t tmp = htons(pkt->will_msg_size);
- len += mqtt_pack_str((buf + len), pkt->will_topic, will_topic_len);
+ len += mqtt_pack_str((buf + len), pkt->will_topic.name, will_topic_len);
memcpy((buf + len), &tmp, 2);
memcpy((buf + len + 2), pkt->will_msg, pkt->will_msg_size);
len += pkt->will_msg_size + 2;
* the packet "remaining length" field. */
remaining_length = 2 + pkt->message_size;
/* Topic is mandatory */
- if (pkt->topic == NULL) {
+ if (pkt->topic.name == NULL) {
return -EINVAL;
}
- topic_len = strlen(pkt->topic);
+ topic_len = strlen(pkt->topic.name);
/* Update packet payload size */
remaining_length += topic_len + 2;
/* Set publish flags */
- publish_flags = MQTT_PUBLISH_QoS(pkt->QoS);
+ publish_flags = MQTT_PUBLISH_QoS(pkt->topic.QoS);
if (pkt->dup_flag) {
publish_flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
}
return -E2BIG;
}
/* Topic is mandatory */
- len += mqtt_pack_str((buf + len), pkt->topic, topic_len);
+ len += mqtt_pack_str((buf + len), pkt->topic.name, topic_len);
/* Packet ID */
tmp_packet_id = htons(pkt->packet_id);
memcpy((buf + len), &tmp_packet_id, 2);
}
idx = 1 + ret;
/* Decode flags */
- pkt->QoS = (buf[0] >> 1) & 0x03;
+ pkt->topic.QoS = (buf[0] >> 1) & 0x03;
pkt->dup_flag = (buf[0] & MQTT_PUBLISH_DUP);
pkt->retain_flag = (buf[0] & MQTT_PUBLISH_RETAIN);
- if (pkt->QoS > 2) {
+ if (pkt->topic.QoS > 2) {
return -EPROTO;
}
/* Decode topic string */
tmp = *((uint16_t*)(buf + idx));
topic_len = ntohs(tmp);
- pkt->topic = (char*)(buf + idx + 2);
+ pkt->topic.name = (char*)(buf + idx + 2);
idx += topic_len + 2;
if ((idx + 2) > size) {
return -EPROTO;