static int s_mqtt_report_publish_fn()

in source/device_defender.c [178:236]


static int s_mqtt_report_publish_fn(struct aws_byte_cursor report, void *userdata) {
    AWS_PRECONDITION(userdata != NULL);
    AWS_PRECONDITION(aws_byte_cursor_is_valid(&report));

    int result = AWS_OP_ERR;
    struct aws_iotdevice_defender_task *defender_task = userdata;
    struct defender_report_publish_context *report_context =
        aws_mem_calloc(defender_task->allocator, 1, sizeof(struct defender_report_publish_context));

    report_context->allocator = defender_task->allocator;
    report_context->defender_task = defender_task;

    /* Make sure defender task stays alive until publish completes one way or another */
    aws_ref_count_acquire(&defender_task->ref_count);

    /* must copy the report data and make into a byte_cursor to use it for MQTT publish */
    if (aws_byte_buf_init_copy_from_cursor(&report_context->json_report, defender_task->allocator, report)) {
        goto done;
    }

    report_context->json_report_cursor = aws_byte_cursor_from_buf(&report_context->json_report);

    struct aws_byte_cursor report_topic = aws_byte_cursor_from_string(defender_task->publish_report_topic_name);

    uint16_t report_packet_id = aws_mqtt_client_connection_publish(
        defender_task->connection,
        &report_topic,
        AWS_MQTT_QOS_AT_LEAST_ONCE,
        false,
        &report_context->json_report_cursor,
        s_on_report_puback,
        report_context);
    if (report_packet_id == 0) {
        AWS_LOGF_ERROR(
            AWS_LS_IOTDEVICE_DEFENDER_TASK,
            "id=%p: Report failed to publish on topic " PRInSTR,
            (void *)defender_task,
            AWS_BYTE_CURSOR_PRI(report_topic));
        goto done;
    }

    /* publish success means we do not clean up the report context until the publish callback completes */
    AWS_LOGF_DEBUG(
        AWS_LS_IOTDEVICE_DEFENDER_TASK,
        "id=%p: Report packet_id %d published on topic " PRInSTR,
        (void *)defender_task,
        report_packet_id,
        AWS_BYTE_CURSOR_PRI(report_topic));

    result = AWS_OP_SUCCESS;

done:

    if (result == AWS_OP_ERR) {
        s_report_publish_context_clean_up(report_context);
    }

    return result;
}