#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 */
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
* 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;
* 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
/* 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 */
};
/* MQTT unsubscribe packet */
struct mqtt_unsubscribe_pkt {
uint16_t packet_id; /* Packet identifier */
- struct mqtt_str topic;
+ char* topic;
};
/* MQTT unsubscribe response packet */
/* 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 */
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));
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;
}
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;
}
}
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);
}
}