#include <vib_acq_client.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>

int main(void) {
    vib_acq_t *dev = NULL;
    vib_acq_info_t info = {0};
    vib_acq_capabilities_t caps = {0};
    vib_acq_device_state_t ds = {0};
    vib_acq_status_t before = {0}, after = {0};
    int32_t samples[VIB_ACQ_BLOCK_FRAMES * VIB_ACQ_CHANNEL_COUNT];
    int32_t lo[4] = {INT32_MAX,INT32_MAX,INT32_MAX,INT32_MAX};
    int32_t hi[4] = {INT32_MIN,INT32_MIN,INT32_MIN,INT32_MIN};
    uint64_t frames = 0, next = 0;
    uint32_t flags = 0;
    int rc = vib_acq_open(&dev), started = 0;
    if (rc) goto done;
    if ((rc = vib_acq_get_info(dev, &info))) goto done;
    if ((rc = vib_acq_get_capabilities(dev, &caps))) goto done;
    if ((rc = vib_acq_get_device_config(dev, &ds))) goto done;
    if ((rc = vib_acq_get_status(dev, &before))) goto done;
    if (info.abi_version != 5 || info.fpga_version != 0x10005 ||
        info.channel_count != 4 || !ds.verified ||
        (caps.flags & VIB_ACQ_CAP_SIMULATED) || before.state != VIB_ACQ_STATE_IDLE) {
        rc = VIB_ACQ_ERROR_INVALID_STATE; goto done;
    }
    vib_acq_config_t config = {15, info.sample_rate_hz, info.vref_volts};
    if ((rc = vib_acq_configure(dev, &config))) goto done;
    if ((rc = vib_acq_start(dev))) goto done;
    started = 1;
    for (unsigned b = 0; b < 16; ++b) {
        vib_acq_block_t block = {.samples=samples, .sample_capacity=sizeof(samples)/sizeof(samples[0])};
        if ((rc = vib_acq_read_block(dev, &block, 2000))) goto done;
        if (block.channel_count != 4 || block.frame_count != 4096 || block.sample_rate_hz != info.sample_rate_hz ||
            (b && block.first_sample_index != next)) { rc = VIB_ACQ_ERROR_DATA_LOSS; goto done; }
        next = block.first_sample_index + block.frame_count;
        flags |= block.flags;
        frames += block.frame_count;
        for (unsigned f = 0; f < block.frame_count; ++f) for (unsigned ch = 0; ch < 4; ++ch) {
            const int32_t v = samples[f*4+ch];
            if (v < lo[ch]) lo[ch] = v;
            if (v > hi[ch]) hi[ch] = v;
        }
    }
done:
    if (dev && started) {
        int stop = vib_acq_stop(dev);
        if (!rc) rc = stop;
        int status = vib_acq_get_status(dev, &after);
        if (!rc) rc = status;
    }
    if (dev) vib_acq_close(dev);
    if (!rc && ((flags & (VIB_ACQ_BLOCK_ADC_QUALITY_MASK | VIB_ACQ_BLOCK_FLAG_DISCONTINUITY_BEFORE)) ||
        after.bad_packets || after.sequence_gaps || after.bad_tags || after.fpga_dropped_frames ||
        after.fpga_frame_errors || after.fpga_ingress_drops || after.host_overruns)) rc = VIB_ACQ_ERROR_DATA_LOSS;
    printf("{\"diagnostic_only\":true,\"rc\":%d,\"frames\":%" PRIu64 ",\"channels\":%u,\"rate_hz\":%u,\"flags\":%u,\"state_after\":%u,\"bad_packets\":%" PRIu64 ",\"sequence_gaps\":%" PRIu64 ",\"dropped_frames\":%" PRIu64 ",\"frame_errors\":%" PRIu64 ",\"ingress_drops\":%" PRIu64 ",\"host_overruns\":%" PRIu64 ",\"adc_code_ranges\":[",
        rc, frames, info.channel_count, info.sample_rate_hz, flags, after.state, after.bad_packets,
        after.sequence_gaps, after.fpga_dropped_frames, after.fpga_frame_errors, after.fpga_ingress_drops, after.host_overruns);
    for (unsigned ch = 0; ch < 4; ++ch) printf("%s[%d,%d]", ch ? "," : "", lo[ch], hi[ch]);
    puts("]}");
    if (rc) fprintf(stderr, "C API capture: %s\n", vib_acq_strerror(rc));
    return rc ? 1 : 0;
}
