From 5da70923b5d7bb58a83168712819be44117469d5 Mon Sep 17 00:00:00 2001 From: Denis Arnst Date: Mon, 22 Jul 2024 15:32:32 +0200 Subject: [PATCH] Increase api version utility: Make get_byte_data behave the same way Add equalizer info also as JSON --- src/output.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++----- src/utility.c | 7 ++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/output.c b/src/output.c index 38e585c..a0419c1 100644 --- a/src/output.c +++ b/src/output.c @@ -9,7 +9,7 @@ #include #include -const char* APIVERSION = "1.1"; +const char* APIVERSION = "1.2"; const char* HEADSETCONTROL_NAME = "HeadsetControl"; // Function to convert enum to string @@ -485,12 +485,38 @@ const char* yaml_replace_spaces_with_dash(const char* str) return result; } -void yaml_print_listitem(const char* value, int indent) +void yaml_print_listitem(const char* value, int indent, bool newline) { - for (int i = 0; i < indent; i++) { + if (newline) { + for (int i = 0; i < indent; i++) { + putchar(' '); + } + } + + printf("- %s", value); + + if (newline) { + putchar('\n'); + } else { + putchar(' '); + } +} + +void yaml_print_listitemfloat(const float value, int indent, bool newline) +{ + if (newline) { + for (int i = 0; i < indent; i++) { + putchar(' '); + } + } + + printf("- %.1f", value); + + if (newline) { + putchar('\n'); + } else { putchar(' '); } - printf("- %s\n", value); } void output_yaml(HeadsetControlStatus* status, HeadsetInfo* infos) @@ -532,12 +558,12 @@ void output_yaml(HeadsetControlStatus* status, HeadsetInfo* infos) yaml_print("capabilities", "", 4); for (int j = 0; j < info->capabilities_amount; j++) { - yaml_print_listitem(info->capabilities[j], 6); + yaml_print_listitem(info->capabilities[j], 6, true); } yaml_print("capabilities_str", "", 4); for (int j = 0; j < info->capabilities_amount; j++) { - yaml_print_listitem(info->capabilities_str[j], 6); + yaml_print_listitem(info->capabilities_str[j], 6, true); } if (info->has_battery_info) { @@ -546,6 +572,34 @@ void output_yaml(HeadsetControlStatus* status, HeadsetInfo* infos) yaml_printint("level", info->battery_level, 6); } + if (info->has_equalizer_info) { + yaml_print("equalizer", "", 4); + yaml_printint("bands", info->equalizer->bands_count, 6); + yaml_printint("baseline", info->equalizer->bands_baseline, 6); + yaml_printint("step", info->equalizer->bands_step, 6); + yaml_printint("min", info->equalizer->bands_min, 6); + yaml_printint("max", info->equalizer->bands_max, 6); + + if (info->has_equalizer_presets_info) { + yaml_printint("equalizer_presets_count", info->equalizer_presets->count, 4); + yaml_print("equalizer_presets", "", 4); + for (int i = 0; i < info->equalizer_presets->count; i++) { + EqualizerPreset* presets = info->equalizer_presets->presets; + + yaml_print_listitem(presets[i].name, 6, true); + + // Spaces for the list + for (int i = 0; i < 8; i++) { + putchar(' '); + } + for (int j = 0; j < info->equalizer->bands_count; j++) { + yaml_print_listitemfloat(presets[i].values[j], 8, false); + } + putchar('\n'); + } + } + } + if (info->has_chatmix_info) { yaml_printint("chatmix", info->chatmix, 4); } diff --git a/src/utility.c b/src/utility.c index 441872b..b618563 100644 --- a/src/utility.c +++ b/src/utility.c @@ -85,6 +85,13 @@ int get_byte_data_from_parameter(char* input, unsigned char* dest, size_t len) { const char* delim = " ,{}\n\r"; + // Make a copy of the input string to avoid modifying the original + char* input_copy = strdup(input); + if (input_copy == NULL) { + // Memory allocation failed + return -1; + } + // For each token in the string, parse and store in buf[]. char* token = strtok(input, delim); int i = 0;