Skip to content

Commit

Permalink
Increase api version
Browse files Browse the repository at this point in the history
utility: Make get_byte_data behave the same way

Add equalizer info also as JSON
  • Loading branch information
Sapd committed Jul 22, 2024
1 parent 38cd8b9 commit 5da7092
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
66 changes: 60 additions & 6 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdlib.h>
#include <string.h>

const char* APIVERSION = "1.1";
const char* APIVERSION = "1.2";
const char* HEADSETCONTROL_NAME = "HeadsetControl";

// Function to convert enum to string
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5da7092

Please sign in to comment.