When preparing the json response to a telemetry socket query, the code for prefixing the command name, and appending the file "}" on the end of the response was duplicated for multiple reply types. Taking this code out of the switch statement reduces the duplication and makes the code more maintainable.
For completeness of testing, add in a test case to validate the "null" response type - the only leg of the switch statment not already covered by an existing test case in the telemetry_data tests. Signed-off-by: Bruce Richardson <bruce.richard...@intel.com> --- app/test/test_telemetry_data.c | 7 +++++++ lib/telemetry/telemetry.c | 35 ++++++++++++---------------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/app/test/test_telemetry_data.c b/app/test/test_telemetry_data.c index cfa4d636f0..d0fc78474e 100644 --- a/app/test/test_telemetry_data.c +++ b/app/test/test_telemetry_data.c @@ -93,6 +93,12 @@ check_output(const char *func_name, const char *expected) return strncmp(expected, buf, sizeof(buf)); } +static int +test_null_return(void) +{ + return CHECK_OUTPUT("null"); +} + static int test_simple_string(void) { @@ -419,6 +425,7 @@ telemetry_data_autotest(void) return -1; test_case test_cases[] = { + test_null_return, test_simple_string, test_case_array_string, test_case_array_int, test_case_array_u64, diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c index 03651e947d..cf60d27bd4 100644 --- a/lib/telemetry/telemetry.c +++ b/lib/telemetry/telemetry.c @@ -233,27 +233,22 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s) RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN + RTE_TEL_MAX_SINGLE_STRING_LEN + 10); + + prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", + MAX_CMD_LEN, cmd); + cb_data_buf = &out_buf[prefix_used]; + buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ + switch (d->type) { case RTE_TEL_NULL: - used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}", - MAX_CMD_LEN, cmd ? cmd : "none"); + used = strlcpy(cb_data_buf, "null", buf_len); break; - case RTE_TEL_STRING: - prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", - MAX_CMD_LEN, cmd); - cb_data_buf = &out_buf[prefix_used]; - buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ + case RTE_TEL_STRING: used = rte_tel_json_str(cb_data_buf, buf_len, 0, d->data.str); - used += prefix_used; - used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); break; - case RTE_TEL_DICT: - prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", - MAX_CMD_LEN, cmd); - cb_data_buf = &out_buf[prefix_used]; - buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ + case RTE_TEL_DICT: used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0); for (i = 0; i < d->data_len; i++) { const struct tel_dict_entry *v = &d->data.dict[i]; @@ -289,18 +284,12 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s) } } } - used += prefix_used; - used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); break; + case RTE_TEL_ARRAY_STRING: case RTE_TEL_ARRAY_INT: case RTE_TEL_ARRAY_U64: case RTE_TEL_ARRAY_CONTAINER: - prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", - MAX_CMD_LEN, cmd); - cb_data_buf = &out_buf[prefix_used]; - buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ - used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0); for (i = 0; i < d->data_len; i++) if (d->type == RTE_TEL_ARRAY_STRING) @@ -328,10 +317,10 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s) if (!rec_data->keep) rte_tel_data_free(rec_data->data); } - used += prefix_used; - used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); break; } + used += prefix_used; + used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); if (write(s, out_buf, used) < 0) perror("Error writing to socket"); } -- 2.34.1