1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "telephony_config.h"
17
18 #include <securec.h>
19
20 #include "parameters.h"
21 #include "telephony_log_wrapper.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 static const uint32_t BCD_MAX_VALUE = 15;
26 static const uint32_t ASCII_BASE_OFFSET_VALUE = 10;
27 static const uint32_t MODEM_CAP_MIN_VALUE = 0;
28 static const uint32_t MODEM_CAP_MAX_VALUE = 32;
29 static const uint32_t BCD_LEN = 4;
30 static const uint32_t BASE_VALUE = 1;
31 static const int32_t INVALID_VALUE = -1;
32 static const int32_t VALID_VALUE = 0;
33 static const uint32_t VALID_VALUE_LENGTH = 100;
34 static constexpr const char *NULL_STRING = "";
35 static constexpr const char *MODEM_CAP = "persist.radio.modem.cap";
36
IsCapabilitySupport(uint32_t capablity)37 bool TelephonyConfig::IsCapabilitySupport(uint32_t capablity)
38 {
39 if (capablity < MODEM_CAP_MIN_VALUE || capablity >= MODEM_CAP_MAX_VALUE) {
40 TELEPHONY_LOGE("IsCapabilitySupport capablity is out of range.");
41 return false;
42 }
43 uint32_t bcdIndex = capablity / BCD_LEN;
44 uint32_t bcdOffset = capablity % BCD_LEN;
45 std::string maxCap = system::GetParameter(MODEM_CAP, NULL_STRING);
46 uint32_t bcdValue = BCD_MAX_VALUE + 1;
47 if (ConvertCharToInt(bcdValue, maxCap, bcdIndex) != VALID_VALUE) {
48 return false;
49 }
50 if (bcdValue > BCD_MAX_VALUE) {
51 return false;
52 }
53 return (bcdValue & (BASE_VALUE << (BCD_LEN - BASE_VALUE - bcdOffset))) > 0;
54 }
55
ConvertCharToInt(uint32_t & retValue,const std::string & maxCap,uint32_t index)56 int32_t TelephonyConfig::ConvertCharToInt(uint32_t &retValue, const std::string &maxCap, uint32_t index)
57 {
58 if (index > VALID_VALUE_LENGTH) {
59 return INVALID_VALUE;
60 }
61 if (maxCap.length() > static_cast<size_t>(VALID_VALUE_LENGTH)) {
62 return INVALID_VALUE;
63 }
64 char content[VALID_VALUE_LENGTH + 1] = { 0 };
65 size_t cpyLen = maxCap.length() + 1;
66 if (strcpy_s(content, cpyLen, maxCap.c_str()) != EOK) {
67 TELEPHONY_LOGE("ConvertCharToInt strcpy_s fail.");
68 return INVALID_VALUE;
69 }
70
71 char originChar = content[index];
72 if (originChar >= '0' && originChar <= '9') {
73 retValue = static_cast<uint32_t>(originChar - '0');
74 } else if (originChar >= 'a' && originChar <= 'f') {
75 retValue = static_cast<uint32_t>(originChar - 'a') + ASCII_BASE_OFFSET_VALUE;
76 } else if (originChar >= 'A' && originChar <= 'F') {
77 retValue = static_cast<uint32_t>(originChar - 'A') + ASCII_BASE_OFFSET_VALUE;
78 } else {
79 return INVALID_VALUE;
80 }
81 return VALID_VALUE;
82 }
83 } // namespace Telephony
84 } // namespace OHOS
85