[spp] [PATCH 03/13] shared/sec: move principle JSON formatter funcs

yasufum.o at gmail.com yasufum.o at gmail.com
Mon Jun 24 06:36:03 CEST 2019


From: Yasufumi Ogawa <yasufum.o at gmail.com>

This update is to move JSON formatter functions start with `append_json`
which have principle features, such as appending int, uint or so, to
separate other formatters have prefix `append_` but not principle
features. Added files are `json_helper.c` and json_helper.h` for the
moved functions.

Signed-off-by: Yasufumi Ogawa <yasufum.o at gmail.com>
---
 src/mirror/Makefile                           |   1 +
 .../secondary/spp_worker_th/cmd_runner.c      | 127 +-----------------
 .../secondary/spp_worker_th/json_helper.c     | 125 +++++++++++++++++
 .../secondary/spp_worker_th/json_helper.h     |  38 ++++++
 .../secondary/spp_worker_th/string_buffer.h   |   2 +
 src/vf/Makefile                               |   1 +
 6 files changed, 168 insertions(+), 126 deletions(-)
 create mode 100644 src/shared/secondary/spp_worker_th/json_helper.c
 create mode 100644 src/shared/secondary/spp_worker_th/json_helper.h

diff --git a/src/mirror/Makefile b/src/mirror/Makefile
index 3e31cfa..6b6b9b9 100644
--- a/src/mirror/Makefile
+++ b/src/mirror/Makefile
@@ -22,6 +22,7 @@ SRCS-y += $(SPP_WKT_DIR)/spp_port.c
 SRCS-y += $(SPP_WKT_DIR)/conn_spp_ctl.c
 SRCS-y += $(SPP_WKT_DIR)/cmd_parser.c
 SRCS-y += $(SPP_WKT_DIR)/cmd_runner.c
+SRCS-y += $(SPP_WKT_DIR)/json_helper.c
 SRCS-y += $(SPP_WKT_DIR)/string_buffer.c
 SRCS-y += $(SPP_WKT_DIR)/ringlatencystats.c
 
diff --git a/src/shared/secondary/spp_worker_th/cmd_runner.c b/src/shared/secondary/spp_worker_th/cmd_runner.c
index 4c4abd8..423774b 100644
--- a/src/shared/secondary/spp_worker_th/cmd_runner.c
+++ b/src/shared/secondary/spp_worker_th/cmd_runner.c
@@ -13,6 +13,7 @@
 #include "spp_port.h"
 #include "string_buffer.h"
 
+#include "json_helper.h"
 #include "conn_spp_ctl.h"
 #include "cmd_parser.h"
 #include "cmd_runner.h"
@@ -21,17 +22,9 @@
 
 /* request message initial size */
 #define CMD_ERR_MSG_LEN 128
-#define CMD_TAG_APPEND_SIZE 16
 #define CMD_REQ_BUF_INIT_SIZE 2048
 #define CMD_RES_BUF_INIT_SIZE 2048
 
-/* TODO(yasufum) confirm why using "" for the alternative of comma? */
-#define JSON_APPEND_COMMA(flg)    ((flg)?", ":"")
-#define JSON_APPEND_VALUE(format) "%s\"%s\": "format
-#define JSON_APPEND_ARRAY         "%s\"%s\": [ %s ]"
-#define JSON_APPEND_BLOCK         "%s\"%s\": { %s }"
-#define JSON_APPEND_BLOCK_NONAME  "%s%s{ %s }"
-
 enum cmd_res_code {
 	CMD_SUCCESS = 0,
 	CMD_FAILED,
@@ -597,124 +590,6 @@ sppwk_get_ethdev_port_id(enum port_type iface_type, int iface_no)
 	}
 }
 
-/* append a comma for JSON format */
-static int
-append_json_comma(char **output)
-{
-	*output = spp_strbuf_append(*output, ", ", strlen(", "));
-	if (unlikely(*output == NULL)) {
-		RTE_LOG(ERR, WK_CMD_RUNNER,
-				"JSON's comma failed to add.\n");
-		return SPP_RET_NG;
-	}
-
-	return SPP_RET_OK;
-}
-
-/* append data of unsigned integral type for JSON format */
-static int
-append_json_uint_value(const char *name, char **output, unsigned int value)
-{
-	int len = strlen(*output);
-	/* extend the buffer */
-	*output = spp_strbuf_append(*output, "",
-			strlen(name) + CMD_TAG_APPEND_SIZE*2);
-	if (unlikely(*output == NULL)) {
-		RTE_LOG(ERR, WK_CMD_RUNNER,
-				"JSON's numeric format failed to add. "
-				"(name = %s, uint = %u)\n", name, value);
-		return SPP_RET_NG;
-	}
-
-	sprintf(&(*output)[len], JSON_APPEND_VALUE("%u"),
-			JSON_APPEND_COMMA(len), name, value);
-	return SPP_RET_OK;
-}
-
-/* append data of integral type for JSON format */
-static int
-append_json_int_value(const char *name, char **output, int value)
-{
-	int len = strlen(*output);
-	/* extend the buffer */
-	*output = spp_strbuf_append(*output, "",
-			strlen(name) + CMD_TAG_APPEND_SIZE*2);
-	if (unlikely(*output == NULL)) {
-		RTE_LOG(ERR, WK_CMD_RUNNER,
-				"JSON's numeric format failed to add. "
-				"(name = %s, int = %d)\n", name, value);
-		return SPP_RET_NG;
-	}
-
-	sprintf(&(*output)[len], JSON_APPEND_VALUE("%d"),
-			JSON_APPEND_COMMA(len), name, value);
-	return SPP_RET_OK;
-}
-
-/* append data of string type for JSON format */
-static int
-append_json_str_value(const char *name, char **output, const char *str)
-{
-	int len = strlen(*output);
-	/* extend the buffer */
-	*output = spp_strbuf_append(*output, "",
-			strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
-	if (unlikely(*output == NULL)) {
-		RTE_LOG(ERR, WK_CMD_RUNNER,
-				"JSON's string format failed to add. "
-				"(name = %s, str = %s)\n", name, str);
-		return SPP_RET_NG;
-	}
-
-	sprintf(&(*output)[len], JSON_APPEND_VALUE("\"%s\""),
-			JSON_APPEND_COMMA(len), name, str);
-	return SPP_RET_OK;
-}
-
-/* append brackets of the array for JSON format */
-static int
-append_json_array_brackets(const char *name, char **output, const char *str)
-{
-	int len = strlen(*output);
-	/* extend the buffer */
-	*output = spp_strbuf_append(*output, "",
-			strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
-	if (unlikely(*output == NULL)) {
-		RTE_LOG(ERR, WK_CMD_RUNNER,
-				"JSON's square bracket failed to add. "
-				"(name = %s, str = %s)\n", name, str);
-		return SPP_RET_NG;
-	}
-
-	sprintf(&(*output)[len], JSON_APPEND_ARRAY,
-			JSON_APPEND_COMMA(len), name, str);
-	return SPP_RET_OK;
-}
-
-/* append brackets of the blocks for JSON format */
-static int
-append_json_block_brackets(const char *name, char **output, const char *str)
-{
-	int len = strlen(*output);
-	/* extend the buffer */
-	*output = spp_strbuf_append(*output, "",
-			strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
-	if (unlikely(*output == NULL)) {
-		RTE_LOG(ERR, WK_CMD_RUNNER,
-				"JSON's curly bracket failed to add. "
-				"(name = %s, str = %s)\n", name, str);
-		return SPP_RET_NG;
-	}
-
-	if (name[0] == '\0')
-		sprintf(&(*output)[len], JSON_APPEND_BLOCK_NONAME,
-				JSON_APPEND_COMMA(len), name, str);
-	else
-		sprintf(&(*output)[len], JSON_APPEND_BLOCK,
-				JSON_APPEND_COMMA(len), name, str);
-	return SPP_RET_OK;
-}
-
 /* Execute one command. */
 static int
 exec_one_cmd(const struct sppwk_cmd_attrs *cmd)
diff --git a/src/shared/secondary/spp_worker_th/json_helper.c b/src/shared/secondary/spp_worker_th/json_helper.c
new file mode 100644
index 0000000..4c1baa3
--- /dev/null
+++ b/src/shared/secondary/spp_worker_th/json_helper.c
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2019 Nippon Telegraph and Telephone Corporation
+ */
+
+#include "string_buffer.h"
+#include "json_helper.h"
+
+#define RTE_LOGTYPE_WK_JSON_HELPER RTE_LOGTYPE_USER1
+
+int
+append_json_comma(char **output)
+{
+	*output = spp_strbuf_append(*output, ", ", strlen(", "));
+	if (unlikely(*output == NULL)) {
+		RTE_LOG(ERR, WK_JSON_HELPER,
+				"JSON's comma failed to add.\n");
+		return SPP_RET_NG;
+	}
+
+	return SPP_RET_OK;
+}
+
+/* append data of unsigned integral type for JSON format */
+int
+append_json_uint_value(const char *name, char **output, unsigned int value)
+{
+	int len = strlen(*output);
+	/* extend the buffer */
+	*output = spp_strbuf_append(*output, "",
+			strlen(name) + CMD_TAG_APPEND_SIZE*2);
+	if (unlikely(*output == NULL)) {
+		RTE_LOG(ERR, WK_JSON_HELPER,
+				"JSON's numeric format failed to add. "
+				"(name = %s, uint = %u)\n", name, value);
+		return SPP_RET_NG;
+	}
+
+	sprintf(&(*output)[len], JSON_APPEND_VALUE("%u"),
+			JSON_APPEND_COMMA(len), name, value);
+	return SPP_RET_OK;
+}
+
+/* append data of integral type for JSON format */
+int
+append_json_int_value(const char *name, char **output, int value)
+{
+	int len = strlen(*output);
+	/* extend the buffer */
+	*output = spp_strbuf_append(*output, "",
+			strlen(name) + CMD_TAG_APPEND_SIZE*2);
+	if (unlikely(*output == NULL)) {
+		RTE_LOG(ERR, WK_JSON_HELPER,
+				"JSON's numeric format failed to add. "
+				"(name = %s, int = %d)\n", name, value);
+		return SPP_RET_NG;
+	}
+
+	sprintf(&(*output)[len], JSON_APPEND_VALUE("%d"),
+			JSON_APPEND_COMMA(len), name, value);
+	return SPP_RET_OK;
+}
+
+/* append data of string type for JSON format */
+int
+append_json_str_value(const char *name, char **output, const char *str)
+{
+	int len = strlen(*output);
+	/* extend the buffer */
+	*output = spp_strbuf_append(*output, "",
+			strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
+	if (unlikely(*output == NULL)) {
+		RTE_LOG(ERR, WK_JSON_HELPER,
+				"JSON's string format failed to add. "
+				"(name = %s, str = %s)\n", name, str);
+		return SPP_RET_NG;
+	}
+
+	sprintf(&(*output)[len], JSON_APPEND_VALUE("\"%s\""),
+			JSON_APPEND_COMMA(len), name, str);
+	return SPP_RET_OK;
+}
+
+/* append brackets of the array for JSON format */
+int
+append_json_array_brackets(const char *name, char **output, const char *str)
+{
+	int len = strlen(*output);
+	/* extend the buffer */
+	*output = spp_strbuf_append(*output, "",
+			strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
+	if (unlikely(*output == NULL)) {
+		RTE_LOG(ERR, WK_JSON_HELPER,
+				"JSON's square bracket failed to add. "
+				"(name = %s, str = %s)\n", name, str);
+		return SPP_RET_NG;
+	}
+
+	sprintf(&(*output)[len], JSON_APPEND_ARRAY,
+			JSON_APPEND_COMMA(len), name, str);
+	return SPP_RET_OK;
+}
+
+/* append brackets of the blocks for JSON format */
+int
+append_json_block_brackets(const char *name, char **output, const char *str)
+{
+	int len = strlen(*output);
+	/* extend the buffer */
+	*output = spp_strbuf_append(*output, "",
+			strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
+	if (unlikely(*output == NULL)) {
+		RTE_LOG(ERR, WK_JSON_HELPER,
+				"JSON's curly bracket failed to add. "
+				"(name = %s, str = %s)\n", name, str);
+		return SPP_RET_NG;
+	}
+
+	if (name[0] == '\0')
+		sprintf(&(*output)[len], JSON_APPEND_BLOCK_NONAME,
+				JSON_APPEND_COMMA(len), name, str);
+	else
+		sprintf(&(*output)[len], JSON_APPEND_BLOCK,
+				JSON_APPEND_COMMA(len), name, str);
+	return SPP_RET_OK;
+}
diff --git a/src/shared/secondary/spp_worker_th/json_helper.h b/src/shared/secondary/spp_worker_th/json_helper.h
new file mode 100644
index 0000000..f286404
--- /dev/null
+++ b/src/shared/secondary/spp_worker_th/json_helper.h
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2019 Nippon Telegraph and Telephone Corporation
+ */
+
+#ifndef _SPPWK_JSON_HELPER_H_
+#define _SPPWK_JSON_HELPER_H_
+
+#include "cmd_utils.h"
+
+#define CMD_TAG_APPEND_SIZE 16
+
+#define JSON_APPEND_COMMA(flg)    ((flg)?", ":"")
+
+#define JSON_APPEND_VALUE(format) "%s\"%s\": "format
+
+#define JSON_APPEND_ARRAY         "%s\"%s\": [ %s ]"
+
+#define JSON_APPEND_BLOCK_NONAME  "%s%s{ %s }"
+#define JSON_APPEND_BLOCK         "%s\"%s\": { %s }"
+
+int append_json_comma(char **output);
+
+int append_json_uint_value(const char *name, char **output,
+		unsigned int value);
+
+int append_json_int_value(const char *name, char **output,
+		int value);
+
+int append_json_str_value(const char *name, char **output,
+		const char *str);
+
+int append_json_array_brackets(const char *name, char **output,
+		const char *str);
+
+int append_json_block_brackets(const char *name, char **output,
+		const char *str);
+
+#endif
diff --git a/src/shared/secondary/spp_worker_th/string_buffer.h b/src/shared/secondary/spp_worker_th/string_buffer.h
index 34ee6cb..951f0ae 100644
--- a/src/shared/secondary/spp_worker_th/string_buffer.h
+++ b/src/shared/secondary/spp_worker_th/string_buffer.h
@@ -5,6 +5,8 @@
 #ifndef _STRING_BUFFER_H_
 #define _STRING_BUFFER_H_
 
+#include <stdlib.h>
+
 /**
  * @file
  * SPP String buffer management
diff --git a/src/vf/Makefile b/src/vf/Makefile
index faf72ee..ca8d2b3 100644
--- a/src/vf/Makefile
+++ b/src/vf/Makefile
@@ -22,6 +22,7 @@ SRCS-y += $(SPP_WKT_DIR)/conn_spp_ctl.c
 SRCS-y += $(SPP_WKT_DIR)/cmd_parser.c
 SRCS-y += $(SPP_WKT_DIR)/cmd_runner.c
 SRCS-y += $(SPP_WKT_DIR)/cmd_utils.c
+SRCS-y += $(SPP_WKT_DIR)/json_helper.c
 SRCS-y += ../shared/common.c
 SRCS-y += ../shared/secondary/utils.c ../shared/secondary/add_port.c
 
-- 
2.17.1



More information about the spp mailing list