1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
5 #ifndef _RTE_TELEMETRY_JSON_H_
6 #define _RTE_TELEMETRY_JSON_H_
11 #include <rte_common.h>
12 #include <rte_telemetry.h>
16 * Internal Telemetry Utility functions
18 * This file contains small inline functions to make it easier for applications
19 * to build up valid JSON responses to telemetry requests.
25 * Copies a value into a buffer if the buffer has enough available space.
26 * Nothing written to buffer if an overflow occurs.
27 * This function is not for use for values larger than given buffer length.
29 __rte_format_printf(3, 4)
31 __json_snprintf(char *buf, const int len, const char *format, ...)
38 ret = vsnprintf(tmp, sizeof(tmp), format, ap);
40 if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) {
44 return 0; /* nothing written or modified */
47 /* Copies an empty array into the provided buffer. */
49 rte_tel_json_empty_array(char *buf, const int len, const int used)
51 return used + __json_snprintf(buf + used, len - used, "[]");
54 /* Copies an empty object into the provided buffer. */
56 rte_tel_json_empty_obj(char *buf, const int len, const int used)
58 return used + __json_snprintf(buf + used, len - used, "{}");
61 /* Copies a string into the provided buffer, in JSON format. */
63 rte_tel_json_str(char *buf, const int len, const int used, const char *str)
65 return used + __json_snprintf(buf + used, len - used, "\"%s\"", str);
68 /* Appends a string into the JSON array in the provided buffer. */
70 rte_tel_json_add_array_string(char *buf, const int len, const int used,
73 int ret, end = used - 1; /* strip off final delimiter */
74 if (used <= 2) /* assume empty, since minimum is '[]' */
75 return __json_snprintf(buf, len, "[\"%s\"]", str);
77 ret = __json_snprintf(buf + end, len - end, ",\"%s\"]", str);
78 return ret == 0 ? used : end + ret;
81 /* Appends an integer into the JSON array in the provided buffer. */
83 rte_tel_json_add_array_int(char *buf, const int len, const int used, int val)
85 int ret, end = used - 1; /* strip off final delimiter */
86 if (used <= 2) /* assume empty, since minimum is '[]' */
87 return __json_snprintf(buf, len, "[%d]", val);
89 ret = __json_snprintf(buf + end, len - end, ",%d]", val);
90 return ret == 0 ? used : end + ret;
93 /* Appends a uint64_t into the JSON array in the provided buffer. */
95 rte_tel_json_add_array_u64(char *buf, const int len, const int used,
98 int ret, end = used - 1; /* strip off final delimiter */
99 if (used <= 2) /* assume empty, since minimum is '[]' */
100 return __json_snprintf(buf, len, "[%"PRIu64"]", val);
102 ret = __json_snprintf(buf + end, len - end, ",%"PRIu64"]", val);
103 return ret == 0 ? used : end + ret;
107 * Add a new element with raw JSON value to the JSON array stored in the
111 rte_tel_json_add_array_json(char *buf, const int len, const int used,
114 int ret, end = used - 1; /* strip off final delimiter */
115 if (used <= 2) /* assume empty, since minimum is '[]' */
116 return __json_snprintf(buf, len, "[%s]", str);
118 ret = __json_snprintf(buf + end, len - end, ",%s]", str);
119 return ret == 0 ? used : end + ret;
123 * Add a new element with uint64_t value to the JSON object stored in the
127 rte_tel_json_add_obj_u64(char *buf, const int len, const int used,
128 const char *name, uint64_t val)
130 int ret, end = used - 1;
131 if (used <= 2) /* assume empty, since minimum is '{}' */
132 return __json_snprintf(buf, len, "{\"%s\":%"PRIu64"}", name,
135 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%"PRIu64"}",
137 return ret == 0 ? used : end + ret;
141 * Add a new element with int value to the JSON object stored in the
145 rte_tel_json_add_obj_int(char *buf, const int len, const int used,
146 const char *name, int val)
148 int ret, end = used - 1;
149 if (used <= 2) /* assume empty, since minimum is '{}' */
150 return __json_snprintf(buf, len, "{\"%s\":%d}", name,
153 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%d}",
155 return ret == 0 ? used : end + ret;
159 * Add a new element with string value to the JSON object stored in the
163 rte_tel_json_add_obj_str(char *buf, const int len, const int used,
164 const char *name, const char *val)
166 int ret, end = used - 1;
167 if (used <= 2) /* assume empty, since minimum is '{}' */
168 return __json_snprintf(buf, len, "{\"%s\":\"%s\"}", name, val);
170 ret = __json_snprintf(buf + end, len - end, ",\"%s\":\"%s\"}",
172 return ret == 0 ? used : end + ret;
176 * Add a new element with raw JSON value to the JSON object stored in the
180 rte_tel_json_add_obj_json(char *buf, const int len, const int used,
181 const char *name, const char *val)
183 int ret, end = used - 1;
184 if (used <= 2) /* assume empty, since minimum is '{}' */
185 return __json_snprintf(buf, len, "{\"%s\":%s}", name, val);
187 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%s}",
189 return ret == 0 ? used : end + ret;
192 #endif /*_RTE_TELEMETRY_JSON_H_*/