mqtt_message_t* mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id) { init_message(connection); if (append_message_id(connection, message_id) == 0) return fail_message(connection); return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0); }
mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id) { init_message(connection); if(topic == NULL || topic[0] == '\0') return fail_message(connection); if(append_string(connection, topic, strlen(topic)) < 0) return fail_message(connection); if(qos > 0) { if((*message_id = append_message_id(connection, 0)) == 0) return fail_message(connection); } else *message_id = 0; if(connection->message.length + data_length > connection->buffer_length) return fail_message(connection); memcpy(connection->buffer + connection->message.length, data, data_length); connection->message.length += data_length; return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain); }
mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id) { init_message(connection); if (topic == NULL || topic[0] == '\0') return fail_message(connection); if (append_string(connection, topic, strlen(topic)) < 0) return fail_message(connection); if (qos > 0) { if ((*message_id = append_message_id(connection, 0)) == 0) return fail_message(connection); } else *message_id = 0; if (connection->message.length + data_length > connection->buffer_length) { // Not enough size in buffer -> fragment this message connection->message.fragmented_msg_data_offset = connection->message.length; memcpy(connection->buffer + connection->message.length, data, connection->buffer_length - connection->message.length); connection->message.length = connection->buffer_length; connection->message.fragmented_msg_total_length = data_length + connection->message.fragmented_msg_data_offset; } else { memcpy(connection->buffer + connection->message.length, data, data_length); connection->message.length += data_length; connection->message.fragmented_msg_total_length = 0; } return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain); }
mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id) { init_message(connection); if(append_message_id(connection, message_id) == 0) return fail_message(connection); return fini_message(connection, MQTT_MSG_TYPE_PUBREL, 0, 1, 0); }
mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id) { if (init_message(connection) == 0) { return fail_message(connection); } if (append_message_id(connection, message_id) == 0) return fail_message(connection); return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0); }
mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id) { init_message(connection); if(topic == NULL || topic[0] == '\0') return fail_message(connection); if((*message_id = append_message_id(connection, 0)) == 0) return fail_message(connection); if(append_string(connection, topic, strlen(topic)) < 0) return fail_message(connection); return fini_message(connection, MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0); }
mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id) { init_message(connection); if(topic == NULL || topic[0] == '\0') return fail_message(connection); if((*message_id = append_message_id(connection, 0)) == 0) return fail_message(connection); if(append_string(connection, topic, strlen(topic)) < 0) return fail_message(connection); if(connection->message.length + 1 > connection->buffer_length) return fail_message(connection); connection->buffer[connection->message.length++] = qos; return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0); }
mqtt_message_t* mqtt_msg_disconnect(mqtt_connection_t* connection) { init_message(connection); return fini_message(connection, MQTT_MSG_TYPE_DISCONNECT, 0, 0, 0); }
mqtt_message_t* mqtt_msg_pingresp(mqtt_connection_t* connection) { init_message(connection); return fini_message(connection, MQTT_MSG_TYPE_PINGRESP, 0, 0, 0); }
mqtt_message_t* mqtt_msg_unsubscribe_fini(mqtt_connection_t* connection) { return fini_message(connection, MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0); }
mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info) { struct mqtt_connect_variable_header* variable_header; init_message(connection); if(connection->message.length + sizeof(*variable_header) > connection->buffer_length) return fail_message(connection); variable_header = (void*)(connection->buffer + connection->message.length); connection->message.length += sizeof(*variable_header); variable_header->lengthMsb = 0; variable_header->lengthLsb = 4; c_memcpy(variable_header->magic, "MQTT", 4); variable_header->version = 4; variable_header->flags = 0; variable_header->keepaliveMsb = info->keepalive >> 8; variable_header->keepaliveLsb = info->keepalive & 0xff; if(info->clean_session) variable_header->flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION; if(info->client_id != NULL && info->client_id[0] != '\0') { if(append_string(connection, info->client_id, c_strlen(info->client_id)) < 0) return fail_message(connection); } else return fail_message(connection); if(info->will_topic != NULL && info->will_topic[0] != '\0') { if(append_string(connection, info->will_topic, c_strlen(info->will_topic)) < 0) return fail_message(connection); if(append_string(connection, info->will_message, c_strlen(info->will_message)) < 0) return fail_message(connection); variable_header->flags |= MQTT_CONNECT_FLAG_WILL; if(info->will_retain) variable_header->flags |= MQTT_CONNECT_FLAG_WILL_RETAIN; variable_header->flags |= (info->will_qos & 3) << 3; } if(info->username != NULL && info->username[0] != '\0') { if(append_string(connection, info->username, c_strlen(info->username)) < 0) return fail_message(connection); variable_header->flags |= MQTT_CONNECT_FLAG_USERNAME; } if(info->password != NULL && info->password[0] != '\0') { if(append_string(connection, info->password, c_strlen(info->password)) < 0) return fail_message(connection); variable_header->flags |= MQTT_CONNECT_FLAG_PASSWORD; } return fini_message(connection, MQTT_MSG_TYPE_CONNECT, 0, 0, 0); }
mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pingreq(mqtt_connection_t* connection) { init_message(connection); return fini_message(connection, MQTT_MSG_TYPE_PINGREQ, 0, 0, 0); }
mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_disconnect(mqtt_connection_t* connection) { if (init_message(connection) == 0) { return fail_message(connection); } return fini_message(connection, MQTT_MSG_TYPE_DISCONNECT, 0, 0, 0); }
mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pingresp(mqtt_connection_t* connection) { if (init_message(connection) == 0) { return fail_message(connection); } return fini_message(connection, MQTT_MSG_TYPE_PINGRESP, 0, 0, 0); }