summaryrefslogtreecommitdiffstats
path: root/plugins/solidigm/solidigm-telemetry/config.c
blob: 781d78692daaff220bee84bf425c17dd82ab0a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: MIT
/*
 * Copyright (c) 2022 Solidigm.
 *
 * Author: leonardo.da.cunha@solidigm.com
 */
#include <stdbool.h>
#include "util/json.h"
#include <stdio.h>

// max 16 bit unsigned integer nummber 65535
#define MAX_16BIT_NUM_AS_STRING_SIZE  6

static bool config_get_by_version(const json_object *obj, int version_major,
				  int version_minor, json_object **value)
{
	char str_key[MAX_16BIT_NUM_AS_STRING_SIZE];
	char str_subkey[MAX_16BIT_NUM_AS_STRING_SIZE];

	snprintf(str_key, sizeof(str_key), "%d", version_major);
	snprintf(str_subkey, sizeof(str_subkey), "%d", version_minor);
	json_object *major_obj = NULL;

	if (!json_object_object_get_ex(obj, str_key, &major_obj))
		return false;
	if  (!json_object_object_get_ex(major_obj, str_subkey, value))
		return false;
	return value != NULL;
}

bool solidigm_config_get_by_token_version(const json_object *obj, int token_id,
					  int version_major, int version_minor,
					  json_object **value)
{
	json_object *token_obj = NULL;
	char str_key[MAX_16BIT_NUM_AS_STRING_SIZE];

	snprintf(str_key, sizeof(str_key), "%d", token_id);
	if (!json_object_object_get_ex(obj, str_key, &token_obj))
		return false;
	if  (!config_get_by_version(token_obj, version_major, version_minor, value))
		return false;
	return value != NULL;
}