blob: 7fa2b085d0bb4e14d5e4268809d5e67d4d8d1e94 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/* Copyright Statement:
2
3 * This software/firmware and related documentation ("MediaTek Software") are
4 * protected under relevant copyright laws. The information contained herein is
5 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
6 * the prior written permission of MediaTek inc. and/or its licensors, any
7 * reproduction, modification, use or disclosure of MediaTek Software, and
8 * information contained herein, in whole or in part, shall be strictly
9 * prohibited.
10
11 * MediaTek Inc. (C) 2015. All rights reserved.
12
13 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
14 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
15 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
16 * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
17 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19 * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
20 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
21 * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
22 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
23 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
24 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
25 * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
26 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
27 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
28 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
29 * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
30 * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
31 * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include "cutils/properties.h"
38#include "request.h"
39#include "prop_debug.h"
40
41/*******************************************************************************/
42/* LIBSNCFG functions for Android Property Mechanism */
43/*******************************************************************************/
44int _property_check(const char *key, const char *value)
45{
46 if (key == 0) {
47 PROP_DEBUG_PRINT(PROP_DEBUG_ERROR, "[PropSet] key==0 return directly\n");
48 return -1;
49 }
50
51 if (strlen(key) >= PROPERTY_KEY_MAX) {
52 PROP_DEBUG_PRINT(PROP_DEBUG_ERROR, "[PropSet] key=%s size:%zd >= max:%d\n",
53 key, strlen(key), PROPERTY_KEY_MAX);
54 return -1;
55 }
56
57 if (strlen(value) >= PROPERTY_VALUE_MAX) {
58 PROP_DEBUG_PRINT(PROP_DEBUG_ERROR, "[PropSet] value=%s size:%zd >= max:%d\n",
59 value, strlen(value), PROPERTY_VALUE_MAX);
60 return -1;
61 }
62
63 return 0;
64}
65
66int property_get(const char *key, char *value, const char *default_value)
67{
68 struct sncfg_request request;
69 int ret_len = 1;
70
71 /*check validaty*/
72 if ((key == 0) || (value == 0)) {
73 PROP_DEBUG_PRINT(PROP_DEBUG_ERROR, "[PropSet] key==0 return directly\n");
74 return -1;
75 }
76
77 if (strlen(key) >= PROPERTY_KEY_MAX) {
78 PROP_DEBUG_PRINT(PROP_DEBUG_ERROR, "[PropSet] key=%s size:%zd >= max:%d\n",
79 key, strlen(key), PROPERTY_KEY_MAX);
80 return -1;
81 }
82
83 /*send socket to get the value*/
84 bzero(&request, sizeof(struct sncfg_request));
85 request.magic = SNCFG_REQUEST_MAGIC;
86 request.type = SNCFG_REQUEST_TYPE_PROP_GET;
87 request.key = (char *)key;
88 if (request_for_sncfg(&request) < 0) {
89 ret_len = 0;
90 } else if (NULL == request.value) {
91 ret_len = 0;
92 }
93
94 /*
95 * 1. normal case: use db's value.
96 * 2. other case: use default value.
97 */
98 if (ret_len>0) {
99 ret_len = strlen(request.value);
100 if (ret_len >= PROPERTY_VALUE_MAX) {
101 ret_len = PROPERTY_VALUE_MAX - 1;
102 }
103 strncpy(value, request.value, ret_len);
104 value[ret_len] = '\0';
105 } else {
106 if (default_value) {
107 ret_len = strlen(default_value);
108 if (ret_len >= PROPERTY_VALUE_MAX) {
109 ret_len = PROPERTY_VALUE_MAX - 1;
110 }
111 memcpy(value, default_value, ret_len);
112 value[ret_len] = '\0';
113 } else {
114 value[0] = '\0';
115 }
116 }
117
118 return ret_len;
119}
120
121int property_set(const char *key, const char *in_value)
122{
123 struct sncfg_request request;
124 char *value = "";
125
126 if (in_value != NULL)
127 value = in_value;
128
129 if (_property_check(key, value) < 0) {
130 return -1;
131 }
132
133 bzero(&request, sizeof(struct sncfg_request));
134 request.magic = SNCFG_REQUEST_MAGIC;
135 request.type = SNCFG_REQUEST_TYPE_PROP_SET;
136 request.key = (char *)key;
137 request.value = (char *)value;
138 if (request_for_sncfg(&request) < 0) {
139 return -1;
140 }
141
142 return 0;
143}
144
145int property_reload(void)
146{
147 struct sncfg_request request;
148
149 bzero(&request, sizeof(struct sncfg_request));
150
151 request.magic = SNCFG_REQUEST_MAGIC;
152 request.type = SNCFG_REQUEST_TYPE_PROP_RELOAD;
153 if (request_for_sncfg(&request) < 0) {
154 return -1;
155 }
156
157 return 0;
158}
159
160int property_debug(char *key, char *in_value)
161{
162 struct sncfg_request request;
163 char *value = "";
164
165 if (in_value != NULL)
166 value = in_value;
167
168 if (_property_check(key, value) < 0) {
169 return -1;
170 }
171
172 bzero(&request, sizeof(struct sncfg_request));
173 request.magic = SNCFG_REQUEST_MAGIC;
174 request.type = SNCFG_REQUEST_TYPE_PROP_DEBUG;
175 request.key = key;
176 request.value = value;
177 if (request_for_sncfg(&request) < 0) {
178 return -1;
179 }
180
181 return 0;
182}