]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print: move obj_add_* definitions into util/json.h
authorTokunori Ikegami <ikegami.t@gmail.com>
Fri, 3 Jan 2025 12:04:43 +0000 (21:04 +0900)
committerDaniel Wagner <wagi@monom.org>
Fri, 3 Jan 2025 14:29:22 +0000 (15:29 +0100)
Also use the same prefix json_object_add_*.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-json.c
nvme-print.h
plugins/ocp/ocp-print-json.c
util/json.c
util/json.h

index 8cc3f424f11c96ae7271e6e756758b866d783776..1f57d6ad3494a024d06799f3dc7e8b72196875af 100644 (file)
 #define obj_add_uint json_object_add_value_uint
 #define obj_add_uint128 json_object_add_value_uint128
 #define obj_add_uint64 json_object_add_value_uint64
+#define obj_add_str json_object_add_value_string
+#define obj_add_uint_02x json_object_add_uint_02x
+#define obj_add_uint_0x json_object_add_uint_0x
+#define obj_add_byte_array json_object_add_byte_array
+#define obj_add_nprix64 json_object_add_nprix64
+#define obj_add_uint_0nx json_object_add_uint_0nx
+#define obj_add_0nprix64 json_object_add_0nprix64
 
 static const uint8_t zero_uuid[16] = { 0 };
 static struct print_ops json_print_ops;
@@ -74,27 +81,6 @@ static void obj_add_uint_x(struct json_object *o, const char *k, __u32 v)
        obj_add_str(o, k, str);
 }
 
-void obj_add_uint_0x(struct json_object *o, const char *k, __u32 v)
-{
-       char str[STR_LEN];
-
-       sprintf(str, "0x%x", v);
-       obj_add_str(o, k, str);
-}
-
-void obj_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width)
-{
-       char str[STR_LEN];
-
-       sprintf(str, "0x%0*x", width, v);
-       obj_add_str(o, k, str);
-}
-
-void obj_add_uint_02x(struct json_object *o, const char *k, __u32 v)
-{
-       obj_add_uint_0nx(o, k, v, 2);
-}
-
 static void obj_add_uint_nx(struct json_object *o, const char *k, __u32 v)
 {
        char str[STR_LEN];
@@ -103,14 +89,6 @@ static void obj_add_uint_nx(struct json_object *o, const char *k, __u32 v)
        obj_add_str(o, k, str);
 }
 
-void obj_add_nprix64(struct json_object *o, const char *k, uint64_t v)
-{
-       char str[STR_LEN];
-
-       sprintf(str, "%#"PRIx64"", v);
-       obj_add_str(o, k, str);
-}
-
 static void obj_add_prix64(struct json_object *o, const char *k, uint64_t v)
 {
        char str[STR_LEN];
@@ -4695,36 +4673,3 @@ struct print_ops *nvme_get_json_print_ops(nvme_print_flags_t flags)
        json_print_ops.flags = flags;
        return &json_print_ops;
 }
-
-void obj_add_byte_array(struct json_object *o, const char *k, unsigned char *buf, int len)
-{
-       int i;
-
-       _cleanup_free_ char *value = NULL;
-
-       if (!buf || !len) {
-               obj_add_str(o, k, "No information provided");
-               return;
-       }
-
-       value = calloc(1, (len + 1) * 2 + 1);
-
-       if (!value) {
-               obj_add_str(o, k, "Could not allocate string");
-               return;
-       }
-
-       sprintf(value, "0x");
-       for (i = 1; i <= len; i++)
-               sprintf(&value[i * 2], "%02x", buf[len - i]);
-
-       obj_add_str(o, k, value);
-}
-
-void obj_add_0nprix64(struct json_object *o, const char *k, uint64_t v, int width)
-{
-       char str[STR_LEN];
-
-       sprintf(str, "0x%0*"PRIx64"", width, v);
-       obj_add_str(o, k, str);
-}
index 304d712a5aaa59a9d6f6932fcd455974ce688fd8..00f19947c87884e06f7f9ea619db288df496b844 100644 (file)
@@ -18,10 +18,6 @@ typedef struct nvme_effects_log_node {
 
 #define POWER_OF_TWO(exponent) (1 << (exponent))
 
-#define STR_LEN 100
-
-#define obj_add_str json_object_add_value_string
-
 void d(unsigned char *buf, int len, int width, int group);
 void d_raw(unsigned char *buf, unsigned len);
 
@@ -329,12 +325,6 @@ bool nvme_registers_cmbloc_support(__u32 cmbsz);
 bool nvme_registers_pmrctl_ready(__u32 pmrctl);
 const char *nvme_degrees_string(long t);
 void print_array(char *name, __u8 *data, int size);
-void obj_add_uint_02x(struct json_object *o, const char *k, __u32 v);
 void json_print(struct json_object *r);
-void obj_add_uint_0x(struct json_object *o, const char *k, __u32 v);
-void obj_add_byte_array(struct json_object *o, const char *k, unsigned char *buf, int len);
-void obj_add_nprix64(struct json_object *o, const char *k, uint64_t v);
 struct json_object *obj_create_array_obj(struct json_object *o, const char *k);
-void obj_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width);
-void obj_add_0nprix64(struct json_object *o, const char *k, uint64_t v, int width);
 #endif /* NVME_PRINT_H */
index 17789cbd448e36f42621fe6a85cae8fb96c37ae9..3ed74ffeca0288bcbcc88a94a039af42a19e8adc 100644 (file)
 
 static void print_hwcomp_desc_json(struct hwcomp_desc_entry *e, struct json_object *r)
 {
-       obj_add_str(r, "Description", hwcomp_id_to_string(le32_to_cpu(e->desc->id)));
-       obj_add_nprix64(r, "Date/Lot Size", e->date_lot_size);
-       obj_add_nprix64(r, "Additional Information Size", e->add_info_size);
-       obj_add_uint_0nx(r, "Identifier", le32_to_cpu(e->desc->id), 8);
-       obj_add_0nprix64(r, "Manufacture", le64_to_cpu(e->desc->mfg), 16);
-       obj_add_0nprix64(r, "Revision", le64_to_cpu(e->desc->rev), 16);
-       obj_add_0nprix64(r, "Manufacture Code", le64_to_cpu(e->desc->mfg_code), 16);
-       obj_add_byte_array(r, "Date/Lot Code", e->date_lot_code, e->date_lot_size);
-       obj_add_byte_array(r, "Additional Information", e->add_info, e->add_info_size);
+       json_object_add_value_string(r, "Description",
+                                    hwcomp_id_to_string(le32_to_cpu(e->desc->id)));
+       json_object_add_nprix64(r, "Date/Lot Size", e->date_lot_size);
+       json_object_add_nprix64(r, "Additional Information Size", e->add_info_size);
+       json_object_add_uint_0nx(r, "Identifier", le32_to_cpu(e->desc->id), 8);
+       json_object_add_0nprix64(r, "Manufacture", le64_to_cpu(e->desc->mfg), 16);
+       json_object_add_0nprix64(r, "Revision", le64_to_cpu(e->desc->rev), 16);
+       json_object_add_0nprix64(r, "Manufacture Code", le64_to_cpu(e->desc->mfg_code), 16);
+       json_object_add_byte_array(r, "Date/Lot Code", e->date_lot_code, e->date_lot_size);
+       json_object_add_byte_array(r, "Additional Information", e->add_info, e->add_info_size);
 }
 
 static void print_hwcomp_desc_list_json(struct json_object *r, struct hwcomp_desc_entry *e,
@@ -31,7 +32,8 @@ static void print_hwcomp_desc_list_json(struct json_object *r, struct hwcomp_des
                return;
 
        if (list) {
-               obj_add_str(r, k, hwcomp_id_to_string(le32_to_cpu(e->desc->id)));
+               json_object_add_value_string(r, k,
+                                            hwcomp_id_to_string(le32_to_cpu(e->desc->id)));
                return;
        }
 
@@ -69,12 +71,12 @@ static void json_hwcomp_log(struct hwcomp_log *log, __u32 id, bool list)
        if (log->ver == 1)
                log_size *= sizeof(__le32);
 
-       obj_add_uint_02x(r, "Log Identifier", LID_HWCOMP);
-       obj_add_uint_0x(r, "Log Page Version", le16_to_cpu(log->ver));
-       obj_add_byte_array(r, "Reserved2", log->rsvd2, ARRAY_SIZE(log->rsvd2));
-       obj_add_byte_array(r, "Log page GUID", log->guid, ARRAY_SIZE(log->guid));
-       obj_add_nprix64(r, "Hardware Component Log Size", (unsigned long long)log_size);
-       obj_add_byte_array(r, "Reserved48", log->rsvd48, ARRAY_SIZE(log->rsvd48));
+       json_object_add_uint_02x(r, "Log Identifier", LID_HWCOMP);
+       json_object_add_uint_0x(r, "Log Page Version", le16_to_cpu(log->ver));
+       json_object_add_byte_array(r, "Reserved2", log->rsvd2, ARRAY_SIZE(log->rsvd2));
+       json_object_add_byte_array(r, "Log page GUID", log->guid, ARRAY_SIZE(log->guid));
+       json_object_add_nprix64(r, "Hardware Component Log Size", (unsigned long long)log_size);
+       json_object_add_byte_array(r, "Reserved48", log->rsvd48, ARRAY_SIZE(log->rsvd48));
        print_hwcomp_descs_json(log->desc, log_size - offsetof(struct hwcomp_log, desc), id, list,
                                obj_create_array_obj(r, "Component Descriptions"));
 
index 2de5848288c646e80612d518fcc503c5675c1a91..6c6413c348c0178afd17755b5d5c430e023bc119 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "json.h"
 #include "types.h"
+#include "cleanup.h"
 
 struct json_object *util_json_object_new_double(long double d)
 {
@@ -72,3 +73,65 @@ uint64_t util_json_object_get_uint64(struct json_object *obj)
 
        return val;
 }
+
+void json_object_add_uint_02x(struct json_object *o, const char *k, __u32 v)
+{
+       json_object_add_uint_0nx(o, k, v, 2);
+}
+
+void json_object_add_uint_0x(struct json_object *o, const char *k, __u32 v)
+{
+       char str[STR_LEN];
+
+       sprintf(str, "0x%x", v);
+       json_object_add_value_string(o, k, str);
+}
+
+void json_object_add_byte_array(struct json_object *o, const char *k, unsigned char *buf, int len)
+{
+       int i;
+
+       _cleanup_free_ char *value = NULL;
+
+       if (!buf || !len) {
+               json_object_add_value_string(o, k, "No information provided");
+               return;
+       }
+
+       value = calloc(1, (len + 1) * 2 + 1);
+
+       if (!value) {
+               json_object_add_value_string(o, k, "Could not allocate string");
+               return;
+       }
+
+       sprintf(value, "0x");
+       for (i = 1; i <= len; i++)
+               sprintf(&value[i * 2], "%02x", buf[len - i]);
+
+       json_object_add_value_string(o, k, value);
+}
+
+void json_object_add_nprix64(struct json_object *o, const char *k, uint64_t v)
+{
+       char str[STR_LEN];
+
+       sprintf(str, "%#"PRIx64"", v);
+       json_object_add_value_string(o, k, str);
+}
+
+void json_object_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width)
+{
+       char str[STR_LEN];
+
+       sprintf(str, "0x%0*x", width, v);
+       json_object_add_value_string(o, k, str);
+}
+
+void json_object_add_0nprix64(struct json_object *o, const char *k, uint64_t v, int width)
+{
+       char str[STR_LEN];
+
+       sprintf(str, "0x%0*"PRIx64"", width, v);
+       json_object_add_value_string(o, k, str);
+}
index b98d8a9b61582ade3fb77d01f8b68e327b6dfa24..da4d7a4586c93e9a8a4ccebcd3df7a505ef54206 100644 (file)
@@ -72,4 +72,13 @@ struct json_object;
 #define json_object_add_value_array(o, k, v) json_object_object_add(o, k, v)
 #define json_object_add_value_object(o, k, v) json_object_object_add(o, k, v)
 
+#define STR_LEN 100
+
+void json_object_add_uint_02x(struct json_object *o, const char *k, __u32 v);
+void json_object_add_uint_0x(struct json_object *o, const char *k, __u32 v);
+void json_object_add_byte_array(struct json_object *o, const char *k, unsigned char *buf, int len);
+void json_object_add_nprix64(struct json_object *o, const char *k, uint64_t v);
+void json_object_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width);
+void json_object_add_0nprix64(struct json_object *o, const char *k, uint64_t v, int width);
+
 #endif /* __JSON__H */