#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#include <ctype.h>
#include "tizen-audio-internal.h"
+#define MAX_LINE_LENGTH 256
+#define MAX_KEY_LENGTH 64
+#define MAX_VALUE_LENGTH 128
+
+typedef struct {
+ char key[MAX_KEY_LENGTH];
+ char value[MAX_VALUE_LENGTH];
+} key_value_pair_t;
+
+struct _dictionary {
+ key_value_pair_t *pairs;
+ int count;
+};
+
/* ------ dump helper -------- */
#define MAX(a, b) ((a) > (b) ? (a) : (b))
+typedef struct _dictionary dictionary_t;
+
#ifdef __USE_TINYALSA__
static const uint32_t g_format_convert_hal_table[] = {
[AUDIO_SAMPLE_U8] = PCM_FORMAT_S8,
}
}
/* ------ dump helper -------- */
+
+/* ------ volume table parse -------- */
+char *rtrim(char *s)
+{
+ char* back = s + strlen(s);
+ while(isspace(*--back));
+ *(back+1) = '\0';
+ return s;
+}
+
+dictionary_t *parser_load(const char *filename) {
+ FILE *file = NULL;
+ dictionary_t *dict = NULL;
+ char line[MAX_LINE_LENGTH];
+
+ file = fopen(filename, "r");
+ if (!file) {
+ AUDIO_LOG_ERROR("Failed to open file: %s", filename);
+ return NULL;
+ }
+
+ dict = (dictionary_t *)malloc(sizeof(dictionary_t));
+ if (!dict) {
+ AUDIO_LOG_ERROR("failed to alloc memory");
+ goto fail;
+ }
+ dict->pairs = NULL;
+ dict->count = 0;
+
+ while (fgets(line, MAX_LINE_LENGTH, file)) {
+ char* saveptr;
+ char *key = strtok_r(line, "=", &saveptr);
+ char *value = strtok_r(NULL, "\n", &saveptr);
+
+ if (key && value) {
+ key_value_pair_t pair;
+ strncpy(pair.key, key, MAX_KEY_LENGTH - 1);
+ rtrim(pair.key);
+ pair.key[MAX_KEY_LENGTH - 1] = '\0';
+ strncpy(pair.value, value, MAX_VALUE_LENGTH - 1);
+ pair.value[MAX_VALUE_LENGTH - 1] = '\0';
+
+ dict->pairs = (key_value_pair_t *)realloc(dict->pairs, sizeof(key_value_pair_t) * (dict->count + 1));
+ if (!dict->pairs) {
+ AUDIO_LOG_ERROR("failed to alloc memory");
+ goto fail;
+ }
+ dict->pairs[dict->count++] = pair;
+ }
+ }
+
+ fclose(file);
+
+ return dict;
+
+fail:
+ if (file)
+ fclose(file);
+ if (dict)
+ parser_freedict(dict);
+
+ return NULL;
+}
+
+const char *parser_getstring(const dictionary_t *dict, const char *key, const char *default_value) {
+ if (!dict || !key) {
+ return default_value;
+ }
+
+ for (int i = 0; i < dict->count; i++) {
+ if (strcmp(dict->pairs[i].key, key) == 0) {
+ return dict->pairs[i].value;
+ }
+ }
+
+ return default_value;
+}
+
+void parser_freedict(dictionary_t *dict) {
+ if (dict) {
+ if (dict->pairs) {
+ free(dict->pairs);
+ }
+ free(dict);
+ }
+}
+
+void dump_dictionary(const dictionary_t *dict) {
+ if (!dict) {
+ AUDIO_LOG_ERROR("dictionary is NULL");
+ return;
+ }
+
+ AUDIO_LOG_INFO("dictionary contents:");
+ for (int i = 0; i < dict->count; i++)
+ AUDIO_LOG_INFO("Key: %-16s Value: %s", dict->pairs[i].key, dict->pairs[i].value);
+}
+
#include <stdlib.h>
#include <string.h>
#include <math.h>
-#include <iniparser.h>
#include "tizen-audio-internal.h"
#include "tizen-audio-impl.h"
}
-static audio_return_e __load_volume_value_table_from_ini(audio_hal_s *ah)
+static audio_return_e __load_volume_value_table(audio_hal_s *ah)
{
- dictionary * dict = NULL;
+ dictionary_t * dict = NULL;
uint32_t vol_type_idx, vol_level_idx, gain_type_idx;
audio_volume_value_table_s *volume_value_table = ah->volume.volume_value_table;
int size = 0;
const char delimiter[] = ", ";
- const char *table_str = "volumes";
const char *tmp_str = NULL;
const char *gain_str = NULL;
char *list_str = NULL, *ptr = NULL;
char *key, *token;
if (access(VOLUME_INI_TEMP_PATH, F_OK) == 0)
- dict = iniparser_load(VOLUME_INI_TEMP_PATH);
+ dict = parser_load(VOLUME_INI_TEMP_PATH);
if (!dict) {
AUDIO_LOG_DEBUG("Use default volume&gain ini file");
- dict = iniparser_load(VOLUME_INI_DEFAULT_PATH);
+ dict = parser_load(VOLUME_INI_DEFAULT_PATH);
if (!dict) {
AUDIO_LOG_WARN("Loading volume&gain table from ini file failed");
return AUDIO_ERR_UNDEFINED;
}
}
+ dump_dictionary(dict);
+
/* Load volume table */
for (vol_type_idx = 0; vol_type_idx < AUDIO_VOLUME_TYPE_MAX; vol_type_idx++) {
const char *vol_type_str = __get_volume_type_string_by_idx(vol_type_idx);
ah->volume.volume_level_max[vol_type_idx] = 0;
- size = strlen(table_str) + strlen(vol_type_str) + 2;
- key = malloc(size);
- if (key) {
- snprintf(key, size, "%s:%s", table_str, vol_type_str);
- if ((tmp_str = iniparser_getstring(dict, key, NULL)))
- list_str = strdup(tmp_str);
-
- if (list_str) {
- token = strtok_r(list_str, delimiter, &ptr);
- while (token) {
- /* convert dB volume to linear volume */
- double vol_value = 0.0f;
- if (strncmp(token, "0", strlen(token)))
- vol_value = pow(10.0, (atof(token) - 100) / 20.0);
- volume_value_table->volume[vol_type_idx][ah->volume.volume_level_max[vol_type_idx]++] = vol_value;
- token = strtok_r(NULL, delimiter, &ptr);
- }
- free(list_str);
- list_str = NULL;
- } else {
- ah->volume.volume_level_max[vol_type_idx] = 1;
- for (vol_level_idx = 0; vol_level_idx < AUDIO_VOLUME_LEVEL_MAX; vol_level_idx++) {
- volume_value_table->volume[vol_type_idx][vol_level_idx] = VOLUME_VALUE_MAX;
- }
+
+ if ((tmp_str = parser_getstring(dict, vol_type_str, NULL)))
+ list_str = strdup(tmp_str);
+
+ if (list_str) {
+ token = strtok_r(list_str, delimiter, &ptr);
+ while (token) {
+ /* convert dB volume to linear volume */
+ double vol_value = 0.0f;
+ if (strncmp(token, "0", strlen(token)))
+ vol_value = pow(10.0, (atof(token) - 100) / 20.0);
+ volume_value_table->volume[vol_type_idx][ah->volume.volume_level_max[vol_type_idx]++] = vol_value;
+ token = strtok_r(NULL, delimiter, &ptr);
+ }
+ free(list_str);
+ list_str = NULL;
+ } else {
+ ah->volume.volume_level_max[vol_type_idx] = 1;
+ for (vol_level_idx = 0; vol_level_idx < AUDIO_VOLUME_LEVEL_MAX; vol_level_idx++) {
+ volume_value_table->volume[vol_type_idx][vol_level_idx] = VOLUME_VALUE_MAX;
}
- free(key);
}
}
for (gain_type_idx = AUDIO_GAIN_TYPE_DEFAULT + 1; gain_type_idx < AUDIO_GAIN_TYPE_MAX; gain_type_idx++) {
const char *gain_type_str = __get_gain_type_string_by_idx(gain_type_idx);
- size = strlen(table_str) + strlen("gain") + strlen(gain_type_str) + 3;
+ size = strlen("gain") + strlen(gain_type_str) + 3;
key = malloc(size);
if (key) {
- snprintf(key, size, "%s:gain_%s", table_str, gain_type_str);
- gain_str = iniparser_getstring(dict, key, NULL);
+ snprintf(key, size, "gain_%s", gain_type_str);
+ gain_str = parser_getstring(dict, key, NULL);
if (gain_str) {
volume_value_table->gain[gain_type_idx] = atof(gain_str);
} else {
}
}
- iniparser_freedict(dict);
+ parser_freedict(dict);
__dump_tb(ah);
return AUDIO_ERR_RESOURCE;
}
- audio_ret = __load_volume_value_table_from_ini(ah);
+ audio_ret = __load_volume_value_table(ah);
if (audio_ret != AUDIO_RET_OK) {
AUDIO_LOG_ERROR("gain table load error");
return AUDIO_ERR_UNDEFINED;