1 /*
2 * Copyright (c) 2019 Intel corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <string.h>
8 #include <sys/cbprintf.h>
9 #include <tracing_buffer.h>
10 #include <tracing_format_common.h>
11
str_put(int c,void * ctx)12 static int str_put(int c, void *ctx)
13 {
14 tracing_ctx_t *str_ctx = (tracing_ctx_t *)ctx;
15
16 if (str_ctx->status == 0) {
17 uint8_t *buf;
18 uint32_t claimed_size;
19
20 claimed_size = tracing_buffer_put_claim(&buf, 1);
21 if (claimed_size) {
22 *buf = (uint8_t)c;
23 str_ctx->length++;
24 } else {
25 str_ctx->status = -1;
26 }
27 }
28
29 return 0;
30 }
31
tracing_format_string_put(const char * str,va_list args)32 bool tracing_format_string_put(const char *str, va_list args)
33 {
34 tracing_ctx_t str_ctx = {0};
35
36 (void)cbvprintf(str_put, (void *)&str_ctx, str, args);
37
38 if (str_ctx.status == 0) {
39 tracing_buffer_put_finish(str_ctx.length);
40 return true;
41 }
42
43 tracing_buffer_put_finish(0);
44 return false;
45 }
46
tracing_format_raw_data_put(uint8_t * data,uint32_t size)47 bool tracing_format_raw_data_put(uint8_t *data, uint32_t size)
48 {
49 uint32_t space = tracing_buffer_space_get();
50
51 if (space >= size) {
52 tracing_buffer_put(data, size);
53 return true;
54 }
55
56 return false;
57 }
58
tracing_format_data_put(tracing_data_t * tracing_data_array,uint32_t count)59 bool tracing_format_data_put(tracing_data_t *tracing_data_array, uint32_t count)
60 {
61 uint32_t total_size = 0U;
62
63 for (uint32_t i = 0; i < count; i++) {
64 tracing_data_t *tracing_data =
65 tracing_data_array + i;
66 uint8_t *data = tracing_data->data, *buf;
67 uint32_t length = tracing_data->length, claimed_size;
68
69 do {
70 claimed_size = tracing_buffer_put_claim(&buf, length);
71 memcpy(buf, data, claimed_size);
72 total_size += claimed_size;
73 length -= claimed_size;
74 data += claimed_size;
75 } while (length && claimed_size);
76
77 if (length && claimed_size == 0) {
78 tracing_buffer_put_finish(0);
79 return false;
80 }
81 }
82
83 tracing_buffer_put_finish(total_size);
84 return true;
85 }
86