コード例 #1
0
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;
}
コード例 #2
0
static int publishMqttMessage(PMQTTTRANSPORT_HANDLE_DATA transportState, MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry, const unsigned char* payload, size_t len)
{
    int result;
    MQTT_MESSAGE_HANDLE mqttMsg = mqttmessage_create(transportState->packetId++, STRING_c_str(transportState->mqttEventTopic), DELIVER_AT_LEAST_ONCE, payload, len);
    if (mqttMsg == NULL)
    {
        result = __LINE__;
    }
    else
    {
        if (mqtt_client_publish(transportState->mqttClient, mqttMsg) != 0)
        {
            result = __LINE__;
        }
        else
        {
            mqttMsgEntry->retryCount++;
            (void)tickcounter_get_current_ms(g_msgTickCounter, &mqttMsgEntry->msgPublishTime);
            result = 0;
        }
        mqttmessage_destroy(mqttMsg);
    }
    return result;
}
コード例 #3
0
static void OnOperationComplete(MQTT_CLIENT_HANDLE handle, MQTT_CLIENT_EVENT_RESULT actionResult, const void* msgInfo, void* callbackCtx)
{
    (void)callbackCtx;
    switch (actionResult)
    {
        case MQTT_CLIENT_ON_CONNACK:
        {
            PrintLogFunction(LOG_LINE, "ConnAck function called");
            const CONNECT_ACK* connack = (CONNECT_ACK*)msgInfo;
            SUBSCRIBE_PAYLOAD subscribe[] = {
                { TOPIC_NAME_A, DELIVER_AT_MOST_ONCE },
                { TOPIC_NAME_B, DELIVER_EXACTLY_ONCE },
            };
            if (mqtt_client_subscribe(handle, PACKET_ID_VALUE++, subscribe, sizeof(subscribe) / sizeof(subscribe[0])) != 0)
            {
                PrintLogFunction(LOG_LINE, "%d: mqtt_client_subscribe failed", __LINE__);
                g_continue = false;
            }
            break;
        }
        case MQTT_CLIENT_ON_SUBSCRIBE_ACK:
        {
            const SUBSCRIBE_ACK* suback = (SUBSCRIBE_ACK*)msgInfo;
            MQTT_MESSAGE_HANDLE msg = mqttmessage_create(PACKET_ID_VALUE++, TOPIC_NAME_A, DELIVER_EXACTLY_ONCE, APP_NAME_A, strlen(APP_NAME_A));
            if (msg == NULL)
            {
                PrintLogFunction(LOG_LINE, "%d: mqttmessage_create failed", __LINE__);
                g_continue = false;
            }
            else
            {
                if (mqtt_client_publish(handle, msg))
                {
                    PrintLogFunction(LOG_LINE, "%d: mqtt_client_publish failed", __LINE__);
                    g_continue = false;
                }
                mqttmessage_destroy(msg);
            }
            // Now send a message that will get 
            break;
        }
        case MQTT_CLIENT_ON_PUBLISH_ACK:
        {
            const PUBLISH_ACK* puback = (PUBLISH_ACK*)msgInfo;
            break;
        }
        case MQTT_CLIENT_ON_PUBLISH_RECV:
        {
            const PUBLISH_ACK* puback = (PUBLISH_ACK*)msgInfo;
            break;
        }
        case MQTT_CLIENT_ON_PUBLISH_REL:
        {
            const PUBLISH_ACK* puback = (PUBLISH_ACK*)msgInfo;
            break;
        }
        case MQTT_CLIENT_ON_PUBLISH_COMP:
        {
            const PUBLISH_ACK* puback = (PUBLISH_ACK*)msgInfo;
            // Done so send disconnect
            mqtt_client_disconnect(handle);
            break;
        }
        case MQTT_CLIENT_ON_ERROR:
        case MQTT_CLIENT_ON_DISCONNECT:
            g_continue = false;
            break;
    }
}