From 7bded08db4af1626c27356913a4ca4ec6dcfd5f6 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Fri, 3 Nov 2023 03:20:26 +0900 Subject: [PATCH] nvme-print-json: Change d() output to use d_json() Signed-off-by: Tokunori Ikegami --- nvme-print-json.c | 29 ++++++++++++++++++++--------- nvme-print-stdout.c | 36 ++++++++++++++++++++++++++++++++++++ nvme-print.c | 27 +-------------------------- nvme-print.h | 1 + 4 files changed, 58 insertions(+), 35 deletions(-) diff --git a/nvme-print-json.c b/nvme-print-json.c index dbe06f9c..c0fbbbd9 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -1930,23 +1930,22 @@ static void json_ctrl_registers(void *bar, bool fabrics) json_print(root); } -static void d_json(unsigned char *buf, int len, int width, int group, - struct json_object *array) +static void d_json(unsigned char *buf, int len, int width, int group, struct json_object *array) { - int i, line_done = 0; - char ascii[32 + 1]; + int i; + char ascii[32 + 1] = { 0 }; + assert(width < sizeof(ascii)); for (i = 0; i < len; i++) { - line_done = 0; ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.'; - if (((i + 1) % width) == 0) { - ascii[i % width + 1] = '\0'; + if (!((i + 1) % width)) { json_array_add_value_string(array, ascii); - line_done = 1; + memset(ascii, 0, sizeof(ascii)); } } - if (!line_done) { + + if (strlen(ascii)) { ascii[i % width + 1] = '\0'; json_array_add_value_string(array, ascii); } @@ -2958,6 +2957,17 @@ static void json_feature_show_fields(enum nvme_features_id fid, unsigned int res } } +void json_d(unsigned char *buf, int len, int width, int group) +{ + struct json_object *root = json_create_object(); + struct json_object *data = json_create_array(); + + d_json(buf, len, width, group, data); + json_object_add_value_array(root, "data", data); + + json_print(root); +} + static void json_nvme_list_ctrl(struct nvme_ctrl_list *ctrl_list) { __u16 num = le16_to_cpu(ctrl_list->num); @@ -3825,6 +3835,7 @@ static struct print_ops json_print_ops = { .id_ctrl_rpmbs = NULL, .lba_range = NULL, .lba_status_info = NULL, + .d = json_d, /* libnvme tree print functions */ .list_item = NULL, diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c index a0f5dd1c..2f0c8a4b 100644 --- a/nvme-print-stdout.c +++ b/nvme-print-stdout.c @@ -4323,6 +4323,41 @@ static void stdout_lba_status_info(__u32 result) printf("\tLBA Status Information Report Interval (LSIRI): %u\n", result & 0xffff); } +void stdout_d(unsigned char *buf, int len, int width, int group) +{ + int i, offset = 0; + char ascii[32 + 1] = { 0 }; + + assert(width < sizeof(ascii)); + + printf(" "); + + for (i = 0; i <= 15; i++) + printf("%3x", i); + + for (i = 0; i < len; i++) { + if (!(i % width)) + printf( "\n%04x:", offset); + if (i % group) + printf( "%02x", buf[i]); + else + printf( " %02x", buf[i]); + ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.'; + if (!((i + 1) % width)) { + printf( " \"%.*s\"", width, ascii); + offset += width; + memset(ascii, 0, sizeof(ascii)); + } + } + + if (strlen(ascii)) { + unsigned b = width - (i % width); + printf( " %*s \"%.*s\"", 2 * b + b / group + (b % group ? 1 : 0), "", width, ascii); + } + + printf( "\n"); +} + static void stdout_plm_config(struct nvme_plm_config *plmcfg) { printf("\tEnable Event :%04x\n", le16_to_cpu(plmcfg->ee)); @@ -5139,6 +5174,7 @@ static struct print_ops stdout_print_ops = { .id_ctrl_rpmbs = stdout_id_ctrl_rpmbs, .lba_range = stdout_lba_range, .lba_status_info = stdout_lba_status_info, + .d = stdout_d, /* libnvme tree print functions */ .list_item = stdout_list_item, diff --git a/nvme-print.c b/nvme-print.c index f4c3cc81..362ad6ff 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -382,32 +382,7 @@ void nvme_show_relatives(const char *name) void d(unsigned char *buf, int len, int width, int group) { - int i, offset = 0; - char ascii[32 + 1] = { 0 }; - - assert(width < sizeof(ascii)); - printf(" "); - for (i = 0; i <= 15; i++) - printf("%3x", i); - for (i = 0; i < len; i++) { - if (i % width == 0) - printf( "\n%04x:", offset); - if (i % group == 0) - printf( " %02x", buf[i]); - else - printf( "%02x", buf[i]); - ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.'; - if (((i + 1) % width) == 0) { - printf( " \"%.*s\"", width, ascii); - offset += width; - memset(ascii, 0, sizeof(ascii)); - } - } - if (strlen(ascii)) { - unsigned b = width - (i % width); - printf( " %*s \"%.*s\"", 2 * b + b / group + (b % group ? 1 : 0), "", width, ascii); - } - printf( "\n"); + nvme_print(d, 0, buf, len, width, group); } void d_raw(unsigned char *buf, unsigned len) diff --git a/nvme-print.h b/nvme-print.h index 88ff9ed0..c05576b7 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -82,6 +82,7 @@ struct print_ops { void (*id_ctrl_rpmbs)(__le32 ctrl_rpmbs); void (*lba_range)(struct nvme_lba_range_type *lbrt, int nr_ranges); void (*lba_status_info)(__u32 result); + void (*d)(unsigned char *buf, int len, int width, int group); /* libnvme tree print functions */ void (*list_item)(nvme_ns_t n); -- 2.50.1