#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>

#include "barectf.h"

static uint64_t get_clock(void* data)
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);

	return ts.tv_sec * 1000000000UL + ts.tv_nsec;
}

enum state_t {
	NEW,
	TERMINATED,
	READY,
	RUNNING,
	WAITING,
};

static void simple(uint8_t* buf, size_t sz)
{
	/* initialize barectf context */
	struct barectf_ctx ctx;
	struct barectf_ctx* pctx = &ctx;

	barectf_init(pctx, buf, sz, get_clock, NULL);

	/* open packet */
	printf("barectf_open_packet(): %d\n", barectf_open_packet(pctx));

	/* record events */
	printf("barectf_trace_simple_uint32(): %d\n", barectf_trace_simple_uint32(pctx, 20150101));
	printf("barectf_trace_simple_int16(): %d\n", barectf_trace_simple_int16(pctx, -2999));
	printf("barectf_trace_simple_float(): %d\n", barectf_trace_simple_float(pctx, 23.57));
	printf("barectf_trace_simple_string(): %d\n", barectf_trace_simple_string(pctx, "Hello, World!"));
	printf("barectf_trace_simple_enum(): %d\n", barectf_trace_simple_enum(pctx, RUNNING));
	printf("barectf_trace_a_few_fields(): %d\n", barectf_trace_a_few_fields(pctx, -1, 301, -3.14159, "Hello again!", NEW));
	printf("barectf_trace_bit_packed_integers(): %d\n", barectf_trace_bit_packed_integers(pctx, 1, -1, 3, -2, 2, 7, 23, -55, 232));

	/* close packet with 3 discarded events */
	barectf_close_packet(pctx, 3);
}

static void write_packet(const char* filename, const uint8_t* buf, size_t sz)
{
	FILE* fh = fopen(filename, "wb");

	if (!fh) {
		return;
	}

	fwrite(buf, 1, sz, fh);
	fclose(fh);
}

int main(void)
{
	puts("simple barectf example!");

	const size_t buf_sz = 128;

	uint8_t* buf = malloc(buf_sz);

	if (!buf) {
		return 1;
	}

	simple(buf, buf_sz);
	write_packet("ctf/stream_0", buf, buf_sz);
	free(buf);

	return 0;
}