2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include "test-platform.h"
32 struct test_platform_ctx
{
33 struct barectf_default_ctx ctx
;
38 static uint64_t get_clock_val(void * const data
)
40 struct test_platform_ctx
* const platform_ctx
= (void *) data
;
41 const uint64_t ret
= platform_ctx
->clock_val
;
43 ++platform_ctx
->clock_val
;
47 static void write_packet(struct test_platform_ctx
* const platform_ctx
)
49 const size_t nmemb
= fwrite(barectf_packet_buf(&platform_ctx
->ctx
),
50 barectf_packet_buf_size(&platform_ctx
->ctx
), 1,
56 static int is_backend_full(void * const data
)
61 static void open_packet(void * const data
)
63 struct test_platform_ctx
* const platform_ctx
= (void *) data
;
65 memset(barectf_packet_buf(&platform_ctx
->ctx
), 0,
66 barectf_packet_buf_size(&platform_ctx
->ctx
));
67 barectf_default_open_packet(&platform_ctx
->ctx
);
70 static void close_packet(void * const data
)
72 struct test_platform_ctx
* const platform_ctx
= (void *) data
;
74 barectf_default_close_packet(&platform_ctx
->ctx
);
75 write_packet(platform_ctx
);
78 struct test_platform_ctx
*test_platform_init(const size_t buf_size
)
81 struct test_platform_ctx
*platform_ctx
;
82 struct barectf_platform_callbacks cbs
;
84 cbs
.default_clock_get_value
= get_clock_val
;
85 cbs
.is_backend_full
= is_backend_full
;
86 cbs
.open_packet
= open_packet
;
87 cbs
.close_packet
= close_packet
;
88 platform_ctx
= malloc(sizeof(*platform_ctx
));
90 platform_ctx
->clock_val
= 0;
91 buf
= malloc(buf_size
);
93 platform_ctx
->fh
= fopen("stream", "wb");
94 assert(platform_ctx
->fh
);
95 barectf_init(&platform_ctx
->ctx
, buf
, buf_size
, cbs
, platform_ctx
);
96 open_packet(platform_ctx
);
100 void test_platform_fini(struct test_platform_ctx
* const platform_ctx
)
102 if (barectf_packet_is_open(&platform_ctx
->ctx
)) {
103 close_packet(platform_ctx
);
106 fclose(platform_ctx
->fh
);
107 free(barectf_packet_buf(&platform_ctx
->ctx
));
111 struct barectf_default_ctx
*test_platform_barectf_ctx(
112 struct test_platform_ctx
* const platform_ctx
)
114 return &platform_ctx
->ctx
;
117 void test_platform_new_packet(struct test_platform_ctx
* const platform_ctx
)
119 if (barectf_packet_is_open(&platform_ctx
->ctx
)) {
120 close_packet(platform_ctx
);
123 open_packet(platform_ctx
);