• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "hril_base.h"
17 
18 namespace OHOS {
19 namespace Telephony {
ConvertHexStringToInt(char ** response,int32_t index,int32_t length)20 int32_t HRilBase::ConvertHexStringToInt(char **response, int32_t index, int32_t length)
21 {
22     const int32_t hexBase = HRIL_INVALID_HEX_CHAR;
23     if ((response == nullptr) || (length <= index) || (response[index] == nullptr)) {
24         return HRIL_ERR_GENERIC_FAILURE;
25     }
26     return strtol(response[index], nullptr, hexBase);
27 }
28 
ConvertIntToRadioNoticeType(int32_t indicationType)29 HRilNotiType HRilBase::ConvertIntToRadioNoticeType(int32_t indicationType)
30 {
31     return (indicationType == (int32_t)ReportType::HRIL_NOTIFICATION) ? (HRilNotiType::HRIL_NOTIFICATION) :
32                                                                         (HRilNotiType::HRIL_NO_DEFINE);
33 }
34 
ConvertHexCharToInt(uint8_t ch)35 uint8_t HRilBase::ConvertHexCharToInt(uint8_t ch)
36 {
37     if ((ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
38         return ((ch - 'A') % HRIL_UPPER_CASE_LETTERS_OFFSET + HRIL_DEC);
39     } else if (ch >= '0' && ch <= '9') {
40         return (ch - '0');
41     } else {
42         return HRIL_INVALID_HEX_CHAR;
43     }
44 }
45 
ConvertHexStringToBytes(const void * response,size_t length)46 uint8_t *HRilBase::ConvertHexStringToBytes(const void *response, size_t length)
47 {
48     const int32_t SIZE_VALUE = 2;
49     const int32_t BIT_NUM = 4;
50 
51     if (response == nullptr) {
52         TELEPHONY_LOGE("response is null!!!");
53         return nullptr;
54     }
55 
56     size_t bytesLen = length / SIZE_VALUE;
57     if (length % SIZE_VALUE != 0 || bytesLen <= 0) {
58         TELEPHONY_LOGE("invalid length: %{public}zu", length);
59         return nullptr;
60     }
61     uint8_t *bytes = (uint8_t *)calloc(bytesLen, sizeof(uint8_t));
62     if (bytes == nullptr) {
63         TELEPHONY_LOGE("ConvertHexStringToBytes: cannot allocate memory for bytes string");
64         return nullptr;
65     }
66 
67     uint8_t *hexStr = (uint8_t *)response;
68     size_t i = 0;
69     while (i < length) {
70         uint8_t hexCh1 = ConvertHexCharToInt(hexStr[i]);
71         uint8_t hexCh2 = ConvertHexCharToInt(hexStr[i + 1]);
72         if (hexCh1 == HRIL_INVALID_HEX_CHAR || hexCh2 == HRIL_INVALID_HEX_CHAR) {
73             free(bytes);
74             return nullptr;
75         }
76         bytes[i / SIZE_VALUE] = ((hexCh1 << BIT_NUM) | hexCh2);
77         i += SIZE_VALUE;
78     }
79     return bytes;
80 }
81 
ConvertToString(char ** dest,const std::string & srcStr)82 bool HRilBase::ConvertToString(char **dest, const std::string &srcStr)
83 {
84     if (dest == nullptr) {
85         TELEPHONY_LOGE("ConvertToString dest is null");
86         return false;
87     }
88     size_t size = srcStr.size();
89     if (size == 0) {
90         *dest = nullptr;
91         return true;
92     }
93     size_t len = size + 1;
94     if (len <= 0) {
95         return false;
96     }
97 
98     *dest = (char *)calloc(len, sizeof(char));
99     if (*dest == nullptr) {
100         TELEPHONY_LOGE("ConvertToString malloc fail");
101         return false;
102     }
103     if (strncpy_s(*dest, len, reinterpret_cast<const char *>(srcStr.c_str()), size) != EOK) {
104         return false;
105     }
106     return true;
107 }
108 
CopyToCharPoint(char ** dest,const std::string & src)109 void HRilBase::CopyToCharPoint(char **dest, const std::string &src)
110 {
111     size_t size = src.size();
112     if (size <= 0) {
113         TELEPHONY_LOGE("CopyToCharPoint src is null");
114         return;
115     }
116     *dest = (char *)malloc((size + 1) * sizeof(char));
117     if (*dest == nullptr) {
118         TELEPHONY_LOGE("CopyToCharPoint malloc content fail!");
119         return;
120     }
121     if (memset_s(*dest, size + 1, 0, size + 1) != EOK) {
122         TELEPHONY_LOGE("CopyToCharPoint memset_s failed");
123         SafeFrees(*dest);
124         return;
125     }
126     if (strcpy_s(*dest, size + 1, src.c_str()) != EOK) {
127         TELEPHONY_LOGE("CopyToCharPoint strcpy_s error");
128         SafeFrees(*dest);
129     }
130 }
131 
CreateHRilRequest(int32_t serial,int32_t request)132 ReqDataInfo *HRilBase::CreateHRilRequest(int32_t serial, int32_t request)
133 {
134     return hrilReporter_.CreateHRilRequest(serial, slotId_, request);
135 }
136 
BuildIHRilRadioResponseInfo(const HRilRadioResponseInfo & responseInfo)137 HDI::Ril::V1_0::RilRadioResponseInfo HRilBase::BuildIHRilRadioResponseInfo(const HRilRadioResponseInfo &responseInfo)
138 {
139     HDI::Ril::V1_0::RilRadioResponseInfo iResponseInfo = { 0 };
140     iResponseInfo.slotId = GetSlotId();
141     iResponseInfo.flag = responseInfo.flag;
142     iResponseInfo.serial = responseInfo.serial;
143     iResponseInfo.error = (HDI::Ril::V1_0::RilErrType)responseInfo.error;
144     iResponseInfo.type = (HDI::Ril::V1_0::RilResponseTypes)responseInfo.type;
145     return iResponseInfo;
146 }
147 
SetRilCallback(const sptr<HDI::Ril::V1_0::IRilCallback> & callback)148 void HRilBase::SetRilCallback(const sptr<HDI::Ril::V1_0::IRilCallback> &callback)
149 {
150     callback_ = callback;
151 }
152 } // namespace Telephony
153 } // namespace OHOS