From: Nathael Pajani Date: Fri, 8 Mar 2019 16:30:26 +0000 (+0100) Subject: Change how MQTT strings are stored X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=2c597772605dc1a03db0aa6c4b3b2f24f68dcb10;p=soft%2Flpc122x%2Fcore Change how MQTT strings are stored --- diff --git a/include/lib/protocols/mqtt.h b/include/lib/protocols/mqtt.h index 3ea54d2..763e23b 100644 --- a/include/lib/protocols/mqtt.h +++ b/include/lib/protocols/mqtt.h @@ -79,11 +79,6 @@ enum MQTT_message_types { #define MQTT_QoS_2 (2) -struct mqtt_str { - uint16_t len; /* size of string, stored in host endianness */ - char* str; /* UTF-8 encoded string */ -}; - /***************************************************************************** */ /* Connect and Connack packets */ @@ -107,14 +102,14 @@ struct mqtt_connect_pkt_fixed_payload { struct mqtt_connect_pkt { uint16_t keep_alive_seconds; uint8_t clean_session_flag; - struct mqtt_str client_id; - struct mqtt_str will_topic; + char* client_id; + char* will_topic; uint16_t will_msg_size; /* stored in host endianness */ uint8_t will_QoS; /* 0, 1 or 2 */ uint8_t will_retain; /* 0 or 1 */ uint8_t* will_msg; - struct mqtt_str username; - struct mqtt_str password; + char* username; + char* password; }; /* Create MQTT connect packet @@ -178,7 +173,7 @@ int mqtt_check_connack_response_pkt(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 { - struct mqtt_str topic; + char* topic; uint16_t packet_id; /* Packet identifier is required for publish if QoS > 0 */ uint8_t QoS; uint8_t dup_flags; @@ -206,7 +201,7 @@ struct mqtt_publish_response_pkt { * It could be implemented using the following structure and the inclusion of "lib/list.h" : * struct mqtt_sub_request { * struct list_head list; - * struct mqtt_str topic; + * char* topic; * uint8_t qos; * }; * and then replace topic and qos fields in struct mqtt_subscribe_pkt with a pointer to a struct list_head @@ -219,7 +214,7 @@ struct mqtt_publish_response_pkt { /* MQTT subscribe packet */ struct mqtt_subscribe_pkt { uint16_t packet_id; /* Packet identifier */ - struct mqtt_str topic; + char* topic; uint8_t QoS; /* unshifted QoS value */ }; @@ -246,7 +241,7 @@ struct mqtt_subscribe_response_pkt { /* MQTT unsubscribe packet */ struct mqtt_unsubscribe_pkt { uint16_t packet_id; /* Packet identifier */ - struct mqtt_str topic; + char* topic; }; /* MQTT unsubscribe response packet */ diff --git a/lib/protocols/mqtt.c b/lib/protocols/mqtt.c index 690ba9e..6adc690 100644 --- a/lib/protocols/mqtt.c +++ b/lib/protocols/mqtt.c @@ -88,11 +88,11 @@ int decode_remaining_length(uint8_t* buf, uint8_t* length) /* Pack a string to MQTT strings format. * The string must already be UTF8 encoded. */ -int mqtt_pack_str(uint8_t *buf, struct mqtt_str* str) +static int mqtt_pack_str(uint8_t *buf, char* str, uint16_t len) { - *(uint16_t*)buf = htons(str->len); - memcpy((buf + 2), str->str, str->len); - return str->len + 2; + *(uint16_t*)buf = htons(len); + memcpy((buf + 2), str, len); + return len + 2; } /* Pack fixed header */ @@ -114,6 +114,7 @@ int mqtt_pack_connect_packet(struct mqtt_connect_pkt* pkt, uint8_t* buf, uint32_ struct mqtt_connect_pkt_fixed_payload fxpl; uint32_t remaining_length = 0; uint32_t len = 0; + uint16_t client_id_len = 0, will_topic_len = 0, username_len = 0, password_len = 0; /* Fixed payload part */ memset(&fxpl, 0, sizeof(struct mqtt_connect_pkt_fixed_payload)); @@ -124,10 +125,10 @@ int mqtt_pack_connect_packet(struct mqtt_connect_pkt* pkt, uint8_t* buf, uint32_ remaining_length = sizeof(struct mqtt_connect_pkt_fixed_payload); /* Client id is (almost) mandatory */ - if (pkt->client_id.str != NULL) { - pkt->client_id.len = strlen(pkt->client_id.str); + if (pkt->client_id != NULL) { + client_id_len = strlen(pkt->client_id); /* Update packet payload size */ - remaining_length += pkt->client_id.len + 2; + remaining_length += client_id_len + 2; } else { return -EINVAL; } @@ -137,28 +138,28 @@ int mqtt_pack_connect_packet(struct mqtt_connect_pkt* pkt, uint8_t* buf, uint32_ fxpl.connect_flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION; } /* Optionnal will message */ - if ((pkt->will_msg_size != 0) && (pkt->will_topic.str != NULL) && (pkt->will_msg != NULL)) { + if ((pkt->will_msg_size != 0) && (pkt->will_topic != NULL) && (pkt->will_msg != NULL)) { fxpl.connect_flags |= MQTT_CONNECT_FLAG_WILL_FLAG; fxpl.connect_flags |= MQTT_CONNECT_FLAG_WILL_QoS(pkt->will_QoS); if (pkt->will_retain != 0) { fxpl.connect_flags |= MQTT_CONNECT_FLAG_WILL_RETAIN; } - pkt->will_topic.len = strlen(pkt->will_topic.str); + will_topic_len = strlen(pkt->will_topic); /* Update packet payload size */ - remaining_length += pkt->will_topic.len + 2; + remaining_length += will_topic_len + 2; remaining_length += pkt->will_msg_size + 2; } /* Optionnal username and password */ - if (pkt->username.str != NULL) { - pkt->username.len = strlen(pkt->username.str); + if (pkt->username != NULL) { + username_len = strlen(pkt->username); fxpl.connect_flags |= MQTT_CONNECT_FLAG_USER_NAME; /* Update packet payload size */ - remaining_length += pkt->username.len + 2; - if (pkt->password.str != NULL) { - pkt->password.len = strlen(pkt->password.str); + remaining_length += username_len + 2; + if (pkt->password != NULL) { + password_len = strlen(pkt->password); fxpl.connect_flags |= MQTT_CONNECT_FLAG_PASSWORD; /* Update packet payload size */ - remaining_length += pkt->password.len + 2; + remaining_length += password_len + 2; } } @@ -173,21 +174,21 @@ int mqtt_pack_connect_packet(struct mqtt_connect_pkt* pkt, uint8_t* buf, uint32_ len += sizeof(struct mqtt_connect_pkt_fixed_payload); /* Client ID is mandatory, even if length of ID string is 0 */ - len += mqtt_pack_str((buf + len), &pkt->client_id); + 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.str != NULL) && (pkt->will_msg != NULL)) { - len += mqtt_pack_str((buf + len), &pkt->will_topic); + if ((pkt->will_msg_size != 0) && (pkt->will_topic != NULL) && (pkt->will_msg != NULL)) { + len += mqtt_pack_str((buf + len), pkt->will_topic, will_topic_len); *(uint16_t*)(buf + len) = htons(pkt->will_msg_size); memcpy((buf + len + 2), pkt->will_msg, pkt->will_msg_size); len += pkt->will_msg_size + 2; } /* Add optionnal username and password */ - if (pkt->username.str != NULL) { - len += mqtt_pack_str((buf + len), &pkt->username); + if (pkt->username != NULL) { + len += mqtt_pack_str((buf + len), pkt->username, username_len); /* Add password too ? */ - if (pkt->password.str != NULL) { - len += mqtt_pack_str((buf + len), &pkt->password); + if (pkt->password != NULL) { + len += mqtt_pack_str((buf + len), pkt->password, password_len); } }