Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nvme-print-json: extern json object add functions #2487

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions nvme-print-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#define obj_add_array json_object_add_value_array
#define obj_add_int json_object_add_value_int
#define obj_add_obj json_object_add_value_object
#define obj_add_str json_object_add_value_string
#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
Expand Down Expand Up @@ -75,23 +74,23 @@ static void obj_add_uint_x(struct json_object *o, const char *k, __u32 v)
obj_add_str(o, k, str);
}

static void obj_add_uint_0x(struct json_object *o, const char *k, __u32 v)
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);
}

static void obj_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width)
void obj_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width)
{
char str[STR_LEN];

sprintf(str, "0x%02x", v);
sprintf(str, "0x%0*x", width, v);
obj_add_str(o, k, str);
}

static void obj_add_uint_02x(struct json_object *o, const char *k, __u32 v)
void obj_add_uint_02x(struct json_object *o, const char *k, __u32 v)
{
obj_add_uint_0nx(o, k, v, 2);
}
Expand All @@ -104,7 +103,7 @@ static void obj_add_uint_nx(struct json_object *o, const char *k, __u32 v)
obj_add_str(o, k, str);
}

static void obj_add_nprix64(struct json_object *o, const char *k, uint64_t v)
void obj_add_nprix64(struct json_object *o, const char *k, uint64_t v)
{
char str[STR_LEN];

Expand Down Expand Up @@ -166,7 +165,7 @@ static void obj_add_key(struct json_object *o, const char *k, const char *v, ...
va_end(ap);
}

static struct json_object *obj_create_array_obj(struct json_object *o, const char *k)
struct json_object *obj_create_array_obj(struct json_object *o, const char *k)
{
struct json_object *array = json_create_array();
struct json_object *obj = json_create_object();
Expand All @@ -191,7 +190,7 @@ static struct json_object *obj_create(const char *k)
return obj;
}

static void json_print(struct json_object *r)
void json_print(struct json_object *r)
{
json_print_object(r, NULL);
printf("\n");
Expand Down Expand Up @@ -4784,3 +4783,36 @@ 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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this being overwritten by the hex characters of the first byte?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Sorry the fix was not correct. Thanks for your suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the overwritten error.

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);
}
10 changes: 10 additions & 0 deletions nvme-print.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ typedef struct nvme_effects_log_node {

#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);

Expand Down Expand Up @@ -324,4 +326,12 @@ 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 */