2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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 "properties"
|
2014-04-09 02:22:42 +08:00
|
|
|
// #define LOG_NDEBUG 0
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2016-09-29 04:33:27 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdbool.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cutils/properties.h>
|
2016-09-29 04:33:27 +08:00
|
|
|
#include <cutils/sockets.h>
|
2017-01-11 05:19:54 +08:00
|
|
|
#include <log/log.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-04-09 02:22:42 +08:00
|
|
|
int8_t property_get_bool(const char *key, int8_t default_value) {
|
|
|
|
if (!key) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t result = default_value;
|
2016-12-20 22:47:36 +08:00
|
|
|
char buf[PROPERTY_VALUE_MAX] = {'\0'};
|
2014-04-09 02:22:42 +08:00
|
|
|
|
|
|
|
int len = property_get(key, buf, "");
|
|
|
|
if (len == 1) {
|
|
|
|
char ch = buf[0];
|
|
|
|
if (ch == '0' || ch == 'n') {
|
|
|
|
result = false;
|
|
|
|
} else if (ch == '1' || ch == 'y') {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
} else if (len > 1) {
|
2016-12-20 22:47:36 +08:00
|
|
|
if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
|
2014-04-09 02:22:42 +08:00
|
|
|
result = false;
|
|
|
|
} else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert string property to int (default if fails); return default value if out of bounds
|
|
|
|
static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
|
2016-12-20 22:47:36 +08:00
|
|
|
intmax_t default_value) {
|
2014-04-09 02:22:42 +08:00
|
|
|
if (!key) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
intmax_t result = default_value;
|
2016-12-20 22:47:36 +08:00
|
|
|
char buf[PROPERTY_VALUE_MAX] = {'\0'};
|
2014-04-09 02:22:42 +08:00
|
|
|
char *end = NULL;
|
|
|
|
|
|
|
|
int len = property_get(key, buf, "");
|
|
|
|
if (len > 0) {
|
|
|
|
int tmp = errno;
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
// Infer base automatically
|
2016-12-20 22:47:36 +08:00
|
|
|
result = strtoimax(buf, &end, /*base*/ 0);
|
2014-04-09 02:22:42 +08:00
|
|
|
if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
|
|
|
|
// Over or underflow
|
|
|
|
result = default_value;
|
2014-04-16 09:06:39 +08:00
|
|
|
ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
|
2014-04-09 02:22:42 +08:00
|
|
|
} else if (result < lower_bound || result > upper_bound) {
|
|
|
|
// Out of range of requested bounds
|
|
|
|
result = default_value;
|
2014-04-16 09:06:39 +08:00
|
|
|
ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
|
2014-04-09 02:22:42 +08:00
|
|
|
} else if (end == buf) {
|
|
|
|
// Numeric conversion failed
|
|
|
|
result = default_value;
|
2016-12-20 22:47:36 +08:00
|
|
|
ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed", __FUNCTION__, key,
|
|
|
|
default_value);
|
2014-04-09 02:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t property_get_int64(const char *key, int64_t default_value) {
|
|
|
|
return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t property_get_int32(const char *key, int32_t default_value) {
|
|
|
|
return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
2016-12-20 22:47:36 +08:00
|
|
|
int property_set(const char *key, const char *value) {
|
2011-03-08 05:52:21 +08:00
|
|
|
return __system_property_set(key, value);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2016-12-20 22:47:36 +08:00
|
|
|
int property_get(const char *key, char *value, const char *default_value) {
|
2017-01-27 09:31:40 +08:00
|
|
|
int len = __system_property_get(key, value);
|
2016-12-20 22:47:36 +08:00
|
|
|
if (len > 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
return len;
|
|
|
|
}
|
2016-12-20 22:47:36 +08:00
|
|
|
if (default_value) {
|
2016-12-23 01:05:18 +08:00
|
|
|
len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
|
2014-04-09 02:22:42 +08:00
|
|
|
memcpy(value, default_value, len);
|
|
|
|
value[len] = '\0';
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-01-27 09:31:40 +08:00
|
|
|
struct callback_data {
|
|
|
|
void (*callback)(const char* name, const char* value, void* cookie);
|
|
|
|
void* cookie;
|
2013-02-13 06:41:59 +08:00
|
|
|
};
|
|
|
|
|
2017-02-11 11:02:51 +08:00
|
|
|
static void trampoline(void* raw_data, const char* name, const char* value, unsigned /*serial*/) {
|
2017-01-27 09:31:40 +08:00
|
|
|
callback_data* data = reinterpret_cast<callback_data*>(raw_data);
|
|
|
|
data->callback(name, value, data->cookie);
|
|
|
|
}
|
2013-02-13 06:41:59 +08:00
|
|
|
|
2017-01-27 09:31:40 +08:00
|
|
|
static void property_list_callback(const prop_info* pi, void* data) {
|
|
|
|
__system_property_read_callback(pi, trampoline, data);
|
2013-02-13 06:41:59 +08:00
|
|
|
}
|
|
|
|
|
2017-01-27 09:31:40 +08:00
|
|
|
int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) {
|
|
|
|
callback_data data = { fn, cookie };
|
2013-02-13 06:41:59 +08:00
|
|
|
return __system_property_foreach(property_list_callback, &data);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|