Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / plugins / sink.ctf.fs / succeed / gen-trace-float.c
CommitLineData
1fba7c7b 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
1fba7c7b 3 *
0235b0db 4 * Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com>
1fba7c7b
PP
5 */
6
7#include <stdint.h>
217cf9d3
PP
8#include <babeltrace2-ctf-writer/writer.h>
9#include <babeltrace2-ctf-writer/clock.h>
10#include <babeltrace2-ctf-writer/clock-class.h>
11#include <babeltrace2-ctf-writer/stream.h>
12#include <babeltrace2-ctf-writer/event.h>
13#include <babeltrace2-ctf-writer/event-types.h>
14#include <babeltrace2-ctf-writer/event-fields.h>
15#include <babeltrace2-ctf-writer/stream-class.h>
16#include <babeltrace2-ctf-writer/trace.h>
1fba7c7b
PP
17
18#include "common/assert.h"
19
20struct config {
21 struct bt_ctf_writer *writer;
22 struct bt_ctf_trace *trace;
23 struct bt_ctf_clock *clock;
24 struct bt_ctf_stream_class *sc;
25 struct bt_ctf_stream *stream;
26 struct bt_ctf_event_class *ec;
27};
28
29static
30void fini_config(struct config *cfg)
31{
32 bt_ctf_object_put_ref(cfg->stream);
33 bt_ctf_object_put_ref(cfg->sc);
34 bt_ctf_object_put_ref(cfg->ec);
35 bt_ctf_object_put_ref(cfg->clock);
36 bt_ctf_object_put_ref(cfg->trace);
37 bt_ctf_object_put_ref(cfg->writer);
38}
39
40static
41void configure_writer(struct config *cfg, const char *path)
42{
43 struct bt_ctf_field_type *ft;
44 int ret;
45
46 cfg->writer = bt_ctf_writer_create(path);
47 BT_ASSERT(cfg->writer);
48 cfg->trace = bt_ctf_writer_get_trace(cfg->writer);
49 BT_ASSERT(cfg->trace);
50 cfg->clock = bt_ctf_clock_create("default");
51 BT_ASSERT(cfg->clock);
52 ret = bt_ctf_writer_add_clock(cfg->writer, cfg->clock);
53 BT_ASSERT(ret == 0);
54 ret = bt_ctf_writer_set_byte_order(cfg->writer,
55 BT_CTF_BYTE_ORDER_BIG_ENDIAN);
56 BT_ASSERT(ret == 0);
57 cfg->sc = bt_ctf_stream_class_create("hello");
58 BT_ASSERT(cfg->sc);
59 ret = bt_ctf_stream_class_set_clock(cfg->sc, cfg->clock);
60 BT_ASSERT(ret == 0);
61 cfg->ec = bt_ctf_event_class_create("ev");
62 BT_ASSERT(cfg->ec);
63 ft = bt_ctf_field_type_floating_point_create();
64 BT_ASSERT(ft);
65 ret = bt_ctf_field_type_floating_point_set_exponent_digits(ft, 8);
66 BT_ASSERT(ret == 0);
67 ret = bt_ctf_field_type_floating_point_set_mantissa_digits(ft, 24);
68 BT_ASSERT(ret == 0);
69 ret = bt_ctf_event_class_add_field(cfg->ec, ft, "flt");
70 BT_ASSERT(ret == 0);
71 bt_ctf_object_put_ref(ft);
72 ret = bt_ctf_stream_class_add_event_class(cfg->sc, cfg->ec);
73 BT_ASSERT(ret == 0);
74 cfg->stream = bt_ctf_writer_create_stream(cfg->writer, cfg->sc);
75 BT_ASSERT(cfg->stream);
76}
77
78static
79void write_stream(struct config *cfg)
80{
81 struct bt_ctf_event *ev;
82 struct bt_ctf_field *field;
83 int ret;
84
85 /* Create event and fill fields */
86 ev = bt_ctf_event_create(cfg->ec);
87 BT_ASSERT(ev);
88 field = bt_ctf_event_get_payload(ev, "flt");
89 BT_ASSERT(field);
90 ret = bt_ctf_field_floating_point_set_value(field, -28.4751);
91 BT_ASSERT(ret == 0);
92 bt_ctf_object_put_ref(field);
93 ret = bt_ctf_clock_set_time(cfg->clock, 0);
94 BT_ASSERT(ret == 0);
95
96 /* Append event */
97 ret = bt_ctf_stream_append_event(cfg->stream, ev);
98 BT_ASSERT(ret == 0);
99 bt_ctf_object_put_ref(ev);
100
101 /* Create packet */
102 ret = bt_ctf_stream_flush(cfg->stream);
103 BT_ASSERT(ret == 0);
104}
105
106int main(int argc, char **argv)
107{
108 struct config cfg = {0};
109
110 BT_ASSERT(argc >= 2);
111 configure_writer(&cfg, argv[1]);
112 write_stream(&cfg);
113 fini_config(&cfg);
114 return 0;
115}
This page took 0.043871 seconds and 4 git commands to generate.