blob: a430d10db1ba65549ba000231a7d8c68d0efd5eb [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001 /*
2 * Copyright (C) 2006 The Android Open Source Project
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#include <cstdarg>
18#include <cstdio>
19#include <string>
20#include <algorithm>
21#include <cutils/properties.h>
22#include <errno.h>
23#include <inttypes.h>
24
25#include "utils.h"
26#include "log_extra.h"
27
28#undef LOG_TAG
29#define LOG_TAG "DEMO_UTILS"
30
31constexpr int utils::MODEM_UNKNOWN;
32constexpr int utils::MODEM_GSM;
33constexpr int utils::MODEM_TDSCDMA;
34constexpr int utils::MODEM_WCDMA;
35constexpr int utils::MODEM_LTE_TDD;
36constexpr int utils::MODEM_LTE_FDD;
37constexpr int utils::MODEM_CDMA_EVDO;
38constexpr int utils::MODEM_CDMA_1X;
39
40utils::utils() {
41 // TODO Auto-generated constructor stub
42
43}
44
45utils::~utils() {
46 // TODO Auto-generated destructor stub
47}
48
49bool utils::is93Modem() {
50#ifdef MD_93_SUPPORT
51 return true;
52#else
53 return false;
54#endif
55}
56
57bool utils::is90Modem() {
58#ifdef MD_90_SUPPORT
59 return true;
60#else
61 return false;
62#endif
63}
64
65bool utils::isC2KSupport() {
66#ifdef C2K_SUPPORT
67 return true;
68#else
69 return false;
70#endif
71}
72
73
74bool utils::isMt2635() {
75#ifdef TARGET_PLATFORM_MT2635
76 return true;
77#else
78 return false;
79#endif
80}
81
82bool utils::isMt2731(){
83#ifdef TARGET_PLATFORM_MT2731
84 return true;
85#else
86 return false;
87#endif
88}
89
90bool utils::is_support_dsds(){
91#ifdef MODE_DSDS
92 return true;
93#else
94 return false;
95#endif
96}
97
98bool utils::is_suppport_dsss(){
99#ifdef MODE_DSSS
100 return true;
101#else
102 return false;
103#endif
104}
105
106/*
107 * Get property
108 */
109int utils::mtk_property_get(const char *key, char *value, const char *default_value)
110{
111 int ali_pro_res = property_get(key, value, default_value);
112 LOG_D(LOG_TAG, "get key is %s, value is %s, result: %d", key, value, ali_pro_res);
113 return ali_pro_res;
114}
115
116/*
117 * Set property
118 */
119int utils::mtk_property_set(const char *key, const char *value)
120{
121 int ret_val = property_set(key, value);
122 LOG_D(LOG_TAG, "set key is %s, value is %s,result: %d", key, value, ret_val);
123 return ret_val;
124}
125
126bool utils::mtk_property_get_bool(const char *key, bool default_value) {
127 if (!key) {
128 return default_value;
129 }
130
131 bool result = default_value;
132 char buf[PROPERTY_VALUE_MAX] = {'\0',};
133
134 int len = property_get(key, buf, "");
135 if (len == 1) {
136 char ch = buf[0];
137 if (ch == '0' || ch == 'n') {
138 result = false;
139 } else if (ch == '1' || ch == 'y') {
140 result = true;
141 }
142 } else if (len > 1) {
143 if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
144 result = false;
145 } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
146 result = true;
147 }
148 }
149
150 return result;
151}
152
153intmax_t utils::property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
154 intmax_t default_value) {
155 if (!key) {
156 return default_value;
157 }
158
159 intmax_t result = default_value;
160 char buf[PROPERTY_VALUE_MAX] = {'\0',};
161 char *end = NULL;
162
163 int len = property_get(key, buf, "");
164 if (len > 0) {
165 int tmp = errno;
166 errno = 0;
167
168 // Infer base automatically
169 result = strtoimax(buf, &end, /*base*/0);
170 if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
171 // Over or underflow
172 result = default_value;
173 ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
174 } else if (result < lower_bound || result > upper_bound) {
175 // Out of range of requested bounds
176 result = default_value;
177 ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
178 } else if (end == buf) {
179 // Numeric conversion failed
180 result = default_value;
181 ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed",
182 __FUNCTION__, key, default_value);
183 }
184
185 errno = tmp;
186 }
187
188 return result;
189}
190
191int64_t utils::mtk_property_get_int64(const char *key, int64_t default_value){
192 return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value);
193}
194
195int32_t utils::mtk_property_get_int32(const char *key, int32_t default_value) {
196 return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
197}
198
199int utils::find_index(std::vector<std::string> v, std::string &str) {
200 auto is_find = std::find(v.begin(), v.end(), str);
201 int index = -1;
202 if(is_find != v.end()) {
203 index = std::distance(v.begin(), is_find);
204 LOG_D(LOG_TAG,"find_index: %d, band: %s", index, str.c_str());
205 }
206 return index;
207}
208
209std::string utils::format(const std::string& format, ...) {
210 va_list args;
211 va_start (args, format);
212 size_t len = std::vsnprintf(NULL, 0, format.c_str(), args);
213 va_end(args);
214 std::vector<char> vec(len + 1);
215 va_start(args, format);
216 std::vsnprintf(&vec[0], len + 1, format.c_str(), args);
217 va_end(args);
218 return &vec[0];
219}
220
221void utils::tokenize(std::string const &str, const char delim, std::vector<std::string> &out){
222 std::stringstream ss(str);
223 std::string s;
224 while(std::getline(ss, s ,delim)) {
225 out.push_back(s);
226 }
227}
228
229void utils::tokenize(std::string const &str, const char* delim, std::vector<std::string> &out){
230 char* token = strtok(const_cast<char*>(str.c_str()), delim);
231 while (token != nullptr) {
232 out.push_back(std::string(token));
233 token = strtok(nullptr, delim);
234 }
235}
236
237void utils::setMSimProperty(int phoneId, char *pPropertyName, char *pUpdateValue) {
238 #define MAX_PHONE_NUM 10
239 #define MIN(a,b) ((a)<(b) ? (a) : (b))
240
241 char oldItemValue[PROPERTY_VALUE_MAX] = {0};
242 char newPropertyValue[PROPERTY_VALUE_MAX] = {0};
243 int i = 0;
244 int strLen = 0;
245
246 for (i = 0; i < MAX_PHONE_NUM; i++) {
247 if (i == phoneId) {
248 // use new value
249 strncat(newPropertyValue, pUpdateValue, PROPERTY_VALUE_MAX - strlen(newPropertyValue));
250 } else {
251 getMSimProperty(i, pPropertyName, oldItemValue);
252 strncat(newPropertyValue, oldItemValue, PROPERTY_VALUE_MAX - strlen(newPropertyValue));
253 }
254 if (i != MAX_PHONE_NUM-1) {
255 strncat(newPropertyValue, ",", 1);
256 }
257 memset(oldItemValue, 0, PROPERTY_VALUE_MAX);
258 }
259 LOG_D(LOG_TAG,"setMSimProperty phoneId=%d, newPropertyValue=%s", phoneId, newPropertyValue);
260 // remove no use ','
261 strLen = strlen(newPropertyValue);
262 for (i = (strLen-1); i >= 0; i--) {
263 if (newPropertyValue[i] == ',') {
264 // remove
265 newPropertyValue[i] = '\0';
266 } else {
267 break;
268 }
269 }
270 LOG_D(LOG_TAG,"newPropertyValue %s\n", newPropertyValue);
271 mtk_property_set(pPropertyName, newPropertyValue);
272}
273
274void utils::getMSimProperty(int phoneId, char *pPropertyName,char *pPropertyValue) {
275 char prop[PROPERTY_VALUE_MAX] = {0};
276 char value[PROPERTY_VALUE_MAX] = {0};
277 int count= 0;
278 int propLen = 0;
279 int i = 0;
280 int j = 0;
281
282 mtk_property_get(pPropertyName, prop, "");
283 LOG_D(LOG_TAG,"getMSimProperty pPropertyName=%s, prop=%s", pPropertyName, prop);
284 propLen = strlen(prop);
285 for (i = 0; i < propLen; i++) {
286 if(prop[i] == ',') {
287 count++;
288 if((count-1) == phoneId) {
289 // return current buffer
290 LOG_D(LOG_TAG,"getMSimProperty found! phoneId=%d, value =%s", phoneId, value);
291 strncpy(pPropertyValue, value, strlen(value));
292 return;
293 } else {
294 // clear current buffer
295 j = 0;
296 memset(value, 0, sizeof(char) * PROPERTY_VALUE_MAX);
297 }
298 } else {
299 value[j] = prop[i];
300 j++;
301 }
302 }
303 if (count == phoneId) {
304 strncpy(pPropertyValue, value, strlen(value));
305 LOG_D(LOG_TAG,"getMSimProperty found at end! phoneId=%d, value =%s", phoneId, value);
306 }
307}
308
309bool utils::is_number(const std::string& s) {
310 std::string::const_iterator it = s.begin();
311 while(it != s.end() && std::isdigit(*it))
312 {
313 ++it;
314 }
315 return !s.empty() && it == s.end();
316}