1
2
3
4
5#ifndef _RTE_TELEMETRY_JSON_H_
6#define _RTE_TELEMETRY_JSON_H_
7
8#include <inttypes.h>
9#include <stdarg.h>
10#include <stdio.h>
11#include <rte_common.h>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28__rte_format_printf(3, 4)
29static inline int
30__json_snprintf(char *buf, const int len, const char *format, ...)
31{
32 char tmp[1024];
33 va_list ap;
34 int ret;
35
36 va_start(ap, format);
37 ret = vsnprintf(tmp, sizeof(tmp), format, ap);
38 va_end(ap);
39 if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) {
40 strcpy(buf, tmp);
41 return ret;
42 }
43 return 0;
44}
45
46
47static inline int
48rte_tel_json_empty_array(char *buf, const int len, const int used)
49{
50 return used + __json_snprintf(buf + used, len - used, "[]");
51}
52
53
54static inline int
55rte_tel_json_empty_obj(char *buf, const int len, const int used)
56{
57 return used + __json_snprintf(buf + used, len - used, "{}");
58}
59
60
61static inline int
62rte_tel_json_str(char *buf, const int len, const int used, const char *str)
63{
64 return used + __json_snprintf(buf + used, len - used, "\"%s\"", str);
65}
66
67
68static inline int
69rte_tel_json_add_array_string(char *buf, const int len, const int used,
70 const char *str)
71{
72 int ret, end = used - 1;
73 if (used <= 2)
74 return __json_snprintf(buf, len, "[\"%s\"]", str);
75
76 ret = __json_snprintf(buf + end, len - end, ",\"%s\"]", str);
77 return ret == 0 ? used : end + ret;
78}
79
80
81static inline int
82rte_tel_json_add_array_int(char *buf, const int len, const int used, int val)
83{
84 int ret, end = used - 1;
85 if (used <= 2)
86 return __json_snprintf(buf, len, "[%d]", val);
87
88 ret = __json_snprintf(buf + end, len - end, ",%d]", val);
89 return ret == 0 ? used : end + ret;
90}
91
92
93static inline int
94rte_tel_json_add_array_u64(char *buf, const int len, const int used,
95 uint64_t val)
96{
97 int ret, end = used - 1;
98 if (used <= 2)
99 return __json_snprintf(buf, len, "[%"PRIu64"]", val);
100
101 ret = __json_snprintf(buf + end, len - end, ",%"PRIu64"]", val);
102 return ret == 0 ? used : end + ret;
103}
104
105
106
107
108
109static inline int
110rte_tel_json_add_array_json(char *buf, const int len, const int used,
111 const char *str)
112{
113 int ret, end = used - 1;
114 if (used <= 2)
115 return __json_snprintf(buf, len, "[%s]", str);
116
117 ret = __json_snprintf(buf + end, len - end, ",%s]", str);
118 return ret == 0 ? used : end + ret;
119}
120
121
122
123
124
125static inline int
126rte_tel_json_add_obj_u64(char *buf, const int len, const int used,
127 const char *name, uint64_t val)
128{
129 int ret, end = used - 1;
130 if (used <= 2)
131 return __json_snprintf(buf, len, "{\"%s\":%"PRIu64"}", name,
132 val);
133
134 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%"PRIu64"}",
135 name, val);
136 return ret == 0 ? used : end + ret;
137}
138
139
140
141
142
143static inline int
144rte_tel_json_add_obj_int(char *buf, const int len, const int used,
145 const char *name, int val)
146{
147 int ret, end = used - 1;
148 if (used <= 2)
149 return __json_snprintf(buf, len, "{\"%s\":%d}", name,
150 val);
151
152 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%d}",
153 name, val);
154 return ret == 0 ? used : end + ret;
155}
156
157
158
159
160
161static inline int
162rte_tel_json_add_obj_str(char *buf, const int len, const int used,
163 const char *name, const char *val)
164{
165 int ret, end = used - 1;
166 if (used <= 2)
167 return __json_snprintf(buf, len, "{\"%s\":\"%s\"}", name, val);
168
169 ret = __json_snprintf(buf + end, len - end, ",\"%s\":\"%s\"}",
170 name, val);
171 return ret == 0 ? used : end + ret;
172}
173
174
175
176
177
178static inline int
179rte_tel_json_add_obj_json(char *buf, const int len, const int used,
180 const char *name, const char *val)
181{
182 int ret, end = used - 1;
183 if (used <= 2)
184 return __json_snprintf(buf, len, "{\"%s\":%s}", name, val);
185
186 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%s}",
187 name, val);
188 return ret == 0 ? used : end + ret;
189}
190
191#endif
192