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>
15 * Internal Telemetry Utility functions
17 * This file contains small inline functions to make it easier for applications
18 * to build up valid JSON responses to telemetry requests.
24 * Copies a value into a buffer if the buffer has enough available space.
25 * Nothing written to buffer if an overflow ocurs.
26 * This function is not for use for values larger than 1k.
28 __rte_format_printf(3, 4)
30 __json_snprintf(char *buf, const int len, const char *format, ...)
37 ret = vsnprintf(tmp, sizeof(tmp), format, ap);
39 if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) {
43 return 0; /* nothing written or modified */
46 /* Copies an empty array into the provided buffer. */
48 rte_tel_json_empty_array(char *buf, const int len, const int used)
50 return used + __json_snprintf(buf + used, len - used, "[]");
53 /* Copies an empty object into the provided buffer. */
55 rte_tel_json_empty_obj(char *buf, const int len, const int used)
57 return used + __json_snprintf(buf + used, len - used, "{}");
60 /* Copies a string into the provided buffer, in JSON format. */
62 rte_tel_json_str(char *buf, const int len, const int used, const char *str)
64 return used + __json_snprintf(buf + used, len - used, "\"%s\"", str);
67 /* Appends a string into the JSON array in the provided buffer. */
69 rte_tel_json_add_array_string(char *buf, const int len, const int used,
72 int ret, end = used - 1; /* strip off final delimiter */
73 if (used <= 2) /* assume empty, since minimum is '[]' */
74 return __json_snprintf(buf, len, "[\"%s\"]", str);
76 ret = __json_snprintf(buf + end, len - end, ",\"%s\"]", str);
77 return ret == 0 ? used : end + ret;
80 /* Appends an integer into the JSON array in the provided buffer. */
82 rte_tel_json_add_array_int(char *buf, const int len, const int used, int val)
84 int ret, end = used - 1; /* strip off final delimiter */
85 if (used <= 2) /* assume empty, since minimum is '[]' */
86 return __json_snprintf(buf, len, "[%d]", val);
88 ret = __json_snprintf(buf + end, len - end, ",%d]", val);
89 return ret == 0 ? used : end + ret;
92 /* Appends a uint64_t into the JSON array in the provided buffer. */
94 rte_tel_json_add_array_u64(char *buf, const int len, const int used,
97 int ret, end = used - 1; /* strip off final delimiter */
98 if (used <= 2) /* assume empty, since minimum is '[]' */
99 return __json_snprintf(buf, len, "[%"PRIu64"]", val);
101 ret = __json_snprintf(buf + end, len - end, ",%"PRIu64"]", val);
102 return ret == 0 ? used : end + ret;
106 * Add a new element with raw JSON value to the JSON array stored in the
110 rte_tel_json_add_array_json(char *buf, const int len, const int used,
113 int ret, end = used - 1; /* strip off final delimiter */
114 if (used <= 2) /* assume empty, since minimum is '[]' */
115 return __json_snprintf(buf, len, "[%s]", str);
117 ret = __json_snprintf(buf + end, len - end, ",%s]", str);
118 return ret == 0 ? used : end + ret;
122 * Add a new element with uint64_t value to the JSON object stored in the
126 rte_tel_json_add_obj_u64(char *buf, const int len, const int used,
127 const char *name, uint64_t val)
129 int ret, end = used - 1;
130 if (used <= 2) /* assume empty, since minimum is '{}' */
131 return __json_snprintf(buf, len, "{\"%s\":%"PRIu64"}", name,
134 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%"PRIu64"}",
136 return ret == 0 ? used : end + ret;
140 * Add a new element with int value to the JSON object stored in the
144 rte_tel_json_add_obj_int(char *buf, const int len, const int used,
145 const char *name, int val)
147 int ret, end = used - 1;
148 if (used <= 2) /* assume empty, since minimum is '{}' */
149 return __json_snprintf(buf, len, "{\"%s\":%d}", name,
152 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%d}",
154 return ret == 0 ? used : end + ret;
158 * Add a new element with string value to the JSON object stored in the
162 rte_tel_json_add_obj_str(char *buf, const int len, const int used,
163 const char *name, const char *val)
165 int ret, end = used - 1;
166 if (used <= 2) /* assume empty, since minimum is '{}' */
167 return __json_snprintf(buf, len, "{\"%s\":\"%s\"}", name, val);
169 ret = __json_snprintf(buf + end, len - end, ",\"%s\":\"%s\"}",
171 return ret == 0 ? used : end + ret;
175 * Add a new element with raw JSON value to the JSON object stored in the
179 rte_tel_json_add_obj_json(char *buf, const int len, const int used,
180 const char *name, const char *val)
182 int ret, end = used - 1;
183 if (used <= 2) /* assume empty, since minimum is '{}' */
184 return __json_snprintf(buf, len, "{\"%s\":%s}", name, val);
186 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%s}",
188 return ret == 0 ? used : end + ret;
191 #endif /*_RTE_TELEMETRY_JSON_H_*/