static int send_mqtt_message(PROV_TRANSPORT_MQTT_INFO* mqtt_info, const char* msg_topic) { int result; MQTT_MESSAGE_HANDLE msg_handle = NULL; if ((msg_handle = mqttmessage_create(get_next_packet_id(mqtt_info), msg_topic, DELIVER_AT_MOST_ONCE, NULL, 0)) == NULL) { LogError("Failed creating mqtt message"); result = __FAILURE__; } else { if (mqtt_client_publish(mqtt_info->mqtt_client, msg_handle) != 0) { LogError("Failed publishing client message"); result = __FAILURE__; } else { result = 0; } mqttmessage_destroy(msg_handle); } return result; }
int mqtt_unsubscribe(mqtt_client_t* c, const char* topicFilter) { int rc = MQTT_FAILURE; mqtt_timer_t timer; mqtt_string_t topic = mqtt_string_initializer; topic.cstring = (char *)topicFilter; int len = 0; mqtt_timer_init(&timer); mqtt_timer_countdown_ms(&timer, c->command_timeout_ms); if (!c->isconnected) goto exit; if ((len = mqtt_serialize_unsubscribe(c->buf, c->buf_size, 0, get_next_packet_id(c), 1, &topic)) <= 0) goto exit; if ((rc = send_packet(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet goto exit; // there was a problem if (waitfor(c, MQTTPACKET_UNSUBACK, &timer) == MQTTPACKET_UNSUBACK) { unsigned short mypacketid; // should be the same as the packetid above if (mqtt_deserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1) rc = 0; } else rc = MQTT_FAILURE; exit: return rc; }
int mqtt_subscribe(mqtt_client_t* c, const char* topic, enum mqtt_qos qos, mqtt_message_handler_t handler) { int rc = MQTT_FAILURE; mqtt_timer_t timer; int len = 0; mqtt_string_t topicStr = mqtt_string_initializer; topicStr.cstring = (char *)topic; mqtt_timer_init(&timer); mqtt_timer_countdown_ms(&timer, c->command_timeout_ms); if (!c->isconnected) goto exit; len = mqtt_serialize_subscribe(c->buf, c->buf_size, 0, get_next_packet_id(c), 1, &topicStr, (int*)&qos); if (len <= 0) goto exit; if ((rc = send_packet(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet { goto exit; // there was a problem } if (waitfor(c, MQTTPACKET_SUBACK, &timer) == MQTTPACKET_SUBACK) // wait for suback { int count = 0, grantedQoS = -1; unsigned short mypacketid; if (mqtt_deserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size) == 1) rc = grantedQoS; // 0, 1, 2 or 0x80 if (rc != 0x80) { int i; rc = MQTT_FAILURE; for (i = 0; i < MQTT_MAX_MESSAGE_HANDLERS; ++i) { if (c->messageHandlers[i].topicFilter == 0) { c->messageHandlers[i].topicFilter = topic; c->messageHandlers[i].fp = handler; rc = 0; break; } } } } else rc = MQTT_FAILURE; exit: return rc; }
static int subscribe_to_topic(PROV_TRANSPORT_MQTT_INFO* mqtt_info) { int result; SUBSCRIBE_PAYLOAD subscribe[SUBSCRIBE_TOPIC_COUNT]; subscribe[0].subscribeTopic = MQTT_SUBSCRIBE_TOPIC; subscribe[0].qosReturn = DELIVER_AT_LEAST_ONCE; if (mqtt_client_subscribe(mqtt_info->mqtt_client, get_next_packet_id(mqtt_info), subscribe, SUBSCRIBE_TOPIC_COUNT) != 0) { LogError("Failed subscribing to topic."); result = __FAILURE__; } else { result = 0; } return result; }
int mqtt_publish(mqtt_client_t* c, const char* topic, mqtt_message_t* message) { int rc = MQTT_FAILURE; mqtt_timer_t timer; mqtt_string_t topicStr = mqtt_string_initializer; topicStr.cstring = (char *)topic; int len = 0; mqtt_timer_init(&timer); mqtt_timer_countdown_ms(&timer, c->command_timeout_ms); if (!c->isconnected) goto exit; if (message->qos == MQTT_QOS1 || message->qos == MQTT_QOS2) message->id = get_next_packet_id(c); len = mqtt_serialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id, topicStr, (unsigned char*)message->payload, message->payloadlen); if (len <= 0) goto exit; if ((rc = send_packet(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet { goto exit; // there was a problem } if (message->qos == MQTT_QOS1) { if (waitfor(c, MQTTPACKET_PUBACK, &timer) == MQTTPACKET_PUBACK) { // We still can receive from broker, treat as recoverable c->fail_count = 0; unsigned short mypacketid; unsigned char dup, type; if (mqtt_deserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1) rc = MQTT_FAILURE; else rc = MQTT_SUCCESS; } else { rc = MQTT_FAILURE; } } else if (message->qos == MQTT_QOS2) { if (waitfor(c, MQTTPACKET_PUBCOMP, &timer) == MQTTPACKET_PUBCOMP) { // We still can receive from broker, treat as recoverable c->fail_count = 0; unsigned short mypacketid; unsigned char dup, type; if (mqtt_deserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1) rc = MQTT_FAILURE; else rc = MQTT_SUCCESS; } else { rc = MQTT_FAILURE; } } exit: return rc; }