misc_utils: Added functions to print enum and flags value

Added name_val structure representing name/value tuple. Added function
print_enum which displays the name of the enum corresponding to a given
value. Added print_flags function which display names of flags constituting
a given value.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
This commit is contained in:
Adrian Warecki 2023-03-07 15:28:12 +01:00 committed by Liam Girdwood
parent c21018335a
commit d20916066e
2 changed files with 49 additions and 0 deletions

View File

@ -3,6 +3,9 @@
* Copyright(c) 2023 Intel Corporation. All rights reserved.
*/
#ifndef __MISC_UTILS_H__
#define __MISC_UTILS_H__
#include <stdint.h>
/**
@ -11,3 +14,16 @@
* @param size of the array
*/
void bytes_swap(uint8_t *ptr, uint32_t size);
struct name_val {
const char *name;
unsigned long value;
};
#define NAME_VAL_ENTRY(x) { .name = #x, .value = x }
#define NAME_VAL_END { .name = 0, .value = 0 }
void print_enum(unsigned long value, const struct name_val *values);
void print_flags(unsigned long value, const struct name_val *flags);
#endif /* __MISC_UTILS_H__ */

View File

@ -1,8 +1,11 @@
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright(c) 2018-2023 Intel Corporation. All rights reserved.
*
* Author: Adrian Warecki <adrian.warecki@intel.com>
*/
#include <stdio.h>
#include <rimage/misc_utils.h>
void bytes_swap(uint8_t *ptr, uint32_t size)
@ -16,3 +19,33 @@ void bytes_swap(uint8_t *ptr, uint32_t size)
ptr[size - 1 - index] = tmp;
}
}
void print_enum(unsigned long value, const struct name_val *values)
{
while (values->name) {
if (values->value == value) {
fprintf(stdout, "%s\n", values->name);
return;
}
values++;
}
printf("Unknown: 0x%lx\n", value);
}
void print_flags(unsigned long value, const struct name_val *flags)
{
while (flags->name) {
if (value & flags->value) {
fprintf(stdout, "%s ", flags->name);
value &= ~flags->value;
}
flags++;
}
if (value)
fprintf(stdout, "+ 0x%lx", value);
printf("\n");
}