1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #if defined(__ANDROID__)
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include "os_env.h"
22 #include <sys/system_properties.h>
23
24 /*
25 * NOTE: __system_property_set only available after android-21
26 * So the library should compiled on latest ndk
27 */
28
os_get_env_u32(const char * name,RK_U32 * value,RK_U32 default_value)29 RK_S32 os_get_env_u32(const char *name, RK_U32 *value, RK_U32 default_value)
30 {
31 char prop[PROP_VALUE_MAX + 1];
32 int len = __system_property_get(name, prop);
33 if (len > 0) {
34 char *endptr;
35 int base = (prop[0] == '0' && prop[1] == 'x') ? (16) : (10);
36 errno = 0;
37 *value = strtoul(prop, &endptr, base);
38 if (errno || (prop == endptr)) {
39 errno = 0;
40 *value = default_value;
41 }
42 } else {
43 *value = default_value;
44 }
45 return 0;
46 }
47
os_get_env_str(const char * name,const char ** value,const char * default_value)48 RK_S32 os_get_env_str(const char *name, const char **value, const char *default_value)
49 {
50 // use unsigned char to avoid warnning
51 static unsigned char env_str[2][PROP_VALUE_MAX + 1];
52 static RK_U32 env_idx = 0;
53 char *prop = (char *)env_str[env_idx];
54 int len = __system_property_get(name, prop);
55 if (len > 0) {
56 *value = prop;
57 env_idx = !env_idx;
58 } else {
59 *value = default_value;
60 }
61 return 0;
62 }
63
os_set_env_u32(const char * name,RK_U32 value)64 RK_S32 os_set_env_u32(const char *name, RK_U32 value)
65 {
66 char buf[PROP_VALUE_MAX + 1 + 2];
67 snprintf(buf, sizeof(buf) - 1, "0x%x", value);
68 int len = __system_property_set(name, buf);
69 return (len) ? (0) : (-1);
70 }
71
os_set_env_str(const char * name,char * value)72 RK_S32 os_set_env_str(const char *name, char *value)
73 {
74 int len = __system_property_set(name, value);
75 return (len) ? (0) : (-1);
76 }
77
78 #endif
79