From: Nathael Pajani Date: Sat, 16 Mar 2019 16:20:52 +0000 (+0100) Subject: Use struct mqtt_topic for topic names X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=034b01dc9b690861048e2c0dfcf02f58cb58bb2c;p=soft%2Flpc122x%2Fcore Use struct mqtt_topic for topic names --- diff --git a/include/lib/protocols/mqtt.h b/include/lib/protocols/mqtt.h index d17b20c..b75a33b 100644 --- a/include/lib/protocols/mqtt.h +++ b/include/lib/protocols/mqtt.h @@ -74,6 +74,11 @@ enum MQTT_message_types { #define MQTT_QoS_2 (2) +struct mqtt_topic { + char* name; + uint8_t QoS; /* 0, 1 or 2 */ +}; + /***************************************************************************** */ /* Connect and Connack packets */ @@ -100,10 +105,9 @@ struct mqtt_connect_pkt { 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; @@ -170,9 +174,8 @@ int mqtt_check_connack_response_pkt(const struct mqtt_connack_response_pkt* pkt) * 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; @@ -239,8 +242,8 @@ int mqtt_check_publish_response_pkt(struct mqtt_publish_response_pkt* pkt, uint8 /* 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 */ diff --git a/lib/protocols/mqtt.c b/lib/protocols/mqtt.c index 36cd5fe..005029d 100644 --- a/lib/protocols/mqtt.c +++ b/lib/protocols/mqtt.c @@ -142,13 +142,13 @@ int mqtt_pack_connect_packet(const struct mqtt_connect_pkt* pkt, uint8_t* buf, u 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; @@ -181,9 +181,9 @@ int mqtt_pack_connect_packet(const struct mqtt_connect_pkt* pkt, uint8_t* buf, u 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; @@ -258,15 +258,15 @@ int mqtt_pack_publish_packet(const struct mqtt_publish_pkt* pkt, uint8_t* buf, u * 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; } @@ -281,7 +281,7 @@ int mqtt_pack_publish_packet(const struct mqtt_publish_pkt* pkt, uint8_t* buf, u 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); @@ -323,16 +323,16 @@ int mqtt_unpack_publish_packet(struct mqtt_publish_pkt* pkt, uint8_t* buf, uint3 } 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;