/* * Copyright (C) 2025 Amlogic Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "audio_hw_property" //#define LOG_NDEBUG 0 #include #include #include #ifdef ANDROID_BUILD #include #endif #include "aml_audio_property.h" #include "aml_audio_log.h" static pthread_mutex_t env_mutex = PTHREAD_MUTEX_INITIALIZER; static unsigned int aml_replacechar(char *dst,const char *src) { unsigned int len = 0; if (strlen(src) > PROPERTY_VALUE_MAX-1) return len; while (len < strlen(src)) { if (src[len] == '.') dst[len] = '_'; else dst[len] = src[len]; len++; } dst[len] = '\0'; return len; } char *aml_audio_property_get_str(const char *key, char *value, const char *default_value) { #ifdef ANDROID_BUILD int ret = property_get(key, value, default_value); if (ret > 0) { return value; } else { return NULL; } #else int len = 0; char str[PROPERTY_VALUE_MAX] = {0}; if (key == NULL || value == NULL) { return NULL; } aml_replacechar(str, key); pthread_mutex_lock(&env_mutex); char *result = getenv(str); if (result == NULL) { if (default_value) { len = strnlen(default_value, PROPERTY_VALUE_MAX - 1); memcpy(value, default_value, len); value[len] = '\0'; } else { pthread_mutex_unlock(&env_mutex); return NULL; } } else { len = strnlen(result, PROPERTY_VALUE_MAX - 1); memcpy(value, result, len); value[len] = '\0'; } pthread_mutex_unlock(&env_mutex); return value; #endif } bool aml_audio_property_get_bool(const char *key, const bool default_value) { #ifdef ANDROID_BUILD return property_get_bool(key, default_value); #else int len = 0; char str[PROPERTY_VALUE_MAX] = {0}; char buf[PROPERTY_VALUE_MAX] = {0}; bool ret = default_value; if (key == NULL) { return ret; } aml_replacechar(str,key); pthread_mutex_lock(&env_mutex); char *result = getenv(str); if (result) { len = strnlen(result, PROPERTY_VALUE_MAX - 1); memcpy(buf, result, len); buf[len] = '\0'; if (len == 1) { char ch = buf[0]; if (ch == '0' || ch == 'n') { ret = false; } else if (ch == '1' || ch == 'y') { ret = true; } } else if (len > 1) { if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) { ret = false; } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) { ret = true; } } } pthread_mutex_unlock(&env_mutex); return ret; #endif } int aml_audio_property_get_int(const char *key, const int default_value) { #ifdef ANDROID_BUILD return property_get_int32(key, default_value); #else int len = 0; char str[PROPERTY_VALUE_MAX] = {0}; char buf[PROPERTY_VALUE_MAX] = {0}; int ret = default_value; if (key == NULL) { return ret; } aml_replacechar(str,key); pthread_mutex_lock(&env_mutex); char *result = getenv(str); if (result) { len = strnlen(result, PROPERTY_VALUE_MAX - 1); memcpy(buf, result, len); buf[len] = '\0'; ret = atoi(buf); } pthread_mutex_unlock(&env_mutex); return ret; #endif } float aml_audio_property_get_float(const char *key, const float default_value) { int len = 0; char str[PROPERTY_VALUE_MAX] = {0}; char buf[PROPERTY_VALUE_MAX] = {0}; float ret = default_value; if (key == NULL) { return default_value; } aml_replacechar(str,key); pthread_mutex_lock(&env_mutex); char *result = getenv(str); if (result) { len = strnlen(result, PROPERTY_VALUE_MAX - 1); memcpy(buf, result, len); buf[len] = '\0'; ret = atof(buf); } pthread_mutex_unlock(&env_mutex); return ret; } void aml_audio_property_set(const char *key, const char *value) { if (key == NULL || value == NULL) { AM_LOGE("setenv key or value is NULL"); return; } pthread_mutex_lock(&env_mutex); if ((setenv(key, value, 1)) == 0) { AM_LOGI("setenv %s=%s success", key, value); } else { AM_LOGI("setenv %s=%s failed", key, value); } pthread_mutex_unlock(&env_mutex); } void aml_audio_property_unset(const char *key) { if (key == NULL) { AM_LOGE("unsetenv key is NULL"); return; } pthread_mutex_lock(&env_mutex); if ((unsetenv(key)) == 0) { AM_LOGI("unsetenv %s success", key); } else { AM_LOGI("unsetenv %s failed", key); } pthread_mutex_unlock(&env_mutex); }