• 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 {
ReadFromHdfBuf(struct HdfSBuf * data,int32_t & val)20 bool HRilBase::ReadFromHdfBuf(struct HdfSBuf *data, int32_t &val)
21 {
22     return HdfSbufReadInt32(data, &val);
23 }
24 
ReadFromHdfBuf(struct HdfSBuf * data,const char * & val)25 bool HRilBase::ReadFromHdfBuf(struct HdfSBuf *data, const char *&val)
26 {
27     val = HdfSbufReadString(data);
28     if (val == nullptr) {
29         TELEPHONY_LOGE("read failed from hdf buf!!!");
30         return false;
31     }
32     return true;
33 }
34 
ResponseHeader(const HRilRadioResponseInfo & responseInfo,std::shared_ptr<struct HdfSBuf> & dataSbuf,MessageParcel & parcel)35 int32_t HRilBase::ResponseHeader(
36     const HRilRadioResponseInfo &responseInfo, std::shared_ptr<struct HdfSBuf> &dataSbuf, MessageParcel &parcel)
37 {
38     int32_t ret = ReportHeader(dataSbuf, parcel);
39     if (ret != HRIL_ERR_SUCCESS) {
40         TELEPHONY_LOGE("write fail for report header!!!");
41         return ret;
42     }
43     if (!HdfSbufWriteUnpadBuffer(dataSbuf.get(), (const uint8_t *)&responseInfo, sizeof(responseInfo))) {
44         TELEPHONY_LOGE("write failed for buffer");
45         return HRIL_ERR_GENERIC_FAILURE;
46     }
47     return HRIL_ERR_SUCCESS;
48 }
49 
ReportHeader(std::shared_ptr<struct HdfSBuf> & dataSbuf,MessageParcel & parcel)50 int32_t HRilBase::ReportHeader(std::shared_ptr<struct HdfSBuf> &dataSbuf, MessageParcel &parcel)
51 {
52     if (!parcel.WriteInterfaceToken(HRIL_INTERFACE_TOKEN)) {
53         TELEPHONY_LOGE("write interface token failed.");
54         return HDF_FAILURE;
55     }
56     dataSbuf.reset(ParcelToSbuf(&parcel), [](struct HdfSBuf *d) { HdfSbufRecycle(d); });
57     if (dataSbuf == nullptr) {
58         TELEPHONY_LOGE("dataSbuf is null!!!");
59         return HRIL_ERR_NULL_POINT;
60     }
61     if (!HdfSbufWriteInt32(dataSbuf.get(), GetSlotId())) {
62         TELEPHONY_LOGE("write failed for slot id");
63         return HRIL_ERR_GENERIC_FAILURE;
64     }
65     return HRIL_ERR_SUCCESS;
66 }
67 
ResponseBuffer(int32_t requestNum,const void * responseInfo,uint32_t reqLen,const void * event,uint32_t eventLen)68 int32_t HRilBase::ResponseBuffer(
69     int32_t requestNum, const void *responseInfo, uint32_t reqLen, const void *event, uint32_t eventLen)
70 {
71     std::unique_ptr<MessageParcel> parcel = std::make_unique<MessageParcel>();
72     if (parcel == nullptr) {
73         TELEPHONY_LOGE("parcel is nullptr");
74         return HRIL_ERR_NULL_POINT;
75     }
76     if (!parcel->WriteInterfaceToken(HRIL_INTERFACE_TOKEN)) {
77         TELEPHONY_LOGE("write interface token failed.");
78         return HRIL_ERR_GENERIC_FAILURE;
79     }
80     struct HdfSBuf *dataSbuf = ParcelToSbuf(parcel.get());
81     if (dataSbuf == nullptr) {
82         return HRIL_ERR_NULL_POINT;
83     }
84     if (!HdfSbufWriteInt32(dataSbuf, slotId_)) {
85         HdfSbufRecycle(dataSbuf);
86         return HRIL_ERR_GENERIC_FAILURE;
87     }
88     if (!HdfSbufWriteUnpadBuffer(dataSbuf, (const uint8_t *)responseInfo, reqLen)) {
89         HdfSbufRecycle(dataSbuf);
90         return HRIL_ERR_GENERIC_FAILURE;
91     }
92     if (!HdfSbufWriteUnpadBuffer(dataSbuf, (const uint8_t *)event, eventLen)) {
93         HdfSbufRecycle(dataSbuf);
94         return HRIL_ERR_GENERIC_FAILURE;
95     }
96     int32_t ret = ServiceDispatcher(requestNum, dataSbuf);
97     if (ret != HRIL_ERR_SUCCESS) {
98         HdfSbufRecycle(dataSbuf);
99         return HRIL_ERR_GENERIC_FAILURE;
100     }
101     if (dataSbuf != nullptr) {
102         HdfSbufRecycle(dataSbuf);
103     }
104     return HRIL_ERR_SUCCESS;
105 }
106 
ResponseInt32(int32_t requestNum,const void * responseInfo,uint32_t reqLen,uint32_t value)107 int32_t HRilBase::ResponseInt32(int32_t requestNum, const void *responseInfo, uint32_t reqLen, uint32_t value)
108 {
109     std::unique_ptr<MessageParcel> parcel = std::make_unique<MessageParcel>();
110     if (parcel == nullptr) {
111         TELEPHONY_LOGE("parcel is nullptr");
112         return HRIL_ERR_NULL_POINT;
113     }
114     if (!parcel->WriteInterfaceToken(HRIL_INTERFACE_TOKEN)) {
115         TELEPHONY_LOGE("write interface token failed.");
116         return HRIL_ERR_GENERIC_FAILURE;
117     }
118     struct HdfSBuf *dataSbuf = ParcelToSbuf(parcel.get());
119     if (dataSbuf == nullptr) {
120         return HRIL_ERR_NULL_POINT;
121     }
122     if (!HdfSbufWriteInt32(dataSbuf, slotId_)) {
123         HdfSbufRecycle(dataSbuf);
124         return HRIL_ERR_GENERIC_FAILURE;
125     }
126     if (!HdfSbufWriteUnpadBuffer(dataSbuf, (const uint8_t *)responseInfo, reqLen)) {
127         HdfSbufRecycle(dataSbuf);
128         return HRIL_ERR_GENERIC_FAILURE;
129     }
130     if (!HdfSbufWriteInt32(dataSbuf, value)) {
131         HdfSbufRecycle(dataSbuf);
132         return HRIL_ERR_GENERIC_FAILURE;
133     }
134     int32_t ret = ServiceDispatcher(requestNum, dataSbuf);
135     if (ret != HRIL_ERR_SUCCESS) {
136         HdfSbufRecycle(dataSbuf);
137         return HRIL_ERR_GENERIC_FAILURE;
138     }
139     if (dataSbuf != nullptr) {
140         HdfSbufRecycle(dataSbuf);
141     }
142     return HRIL_ERR_SUCCESS;
143 }
144 
ResponseRequestInfo(int32_t requestNum,const void * responseInfo,uint32_t reqLen)145 int32_t HRilBase::ResponseRequestInfo(int32_t requestNum, const void *responseInfo, uint32_t reqLen)
146 {
147     std::unique_ptr<MessageParcel> parcel = std::make_unique<MessageParcel>();
148     if (parcel == nullptr) {
149         TELEPHONY_LOGE("parcel is nullptr");
150         return HRIL_ERR_NULL_POINT;
151     }
152     if (!parcel->WriteInterfaceToken(HRIL_INTERFACE_TOKEN)) {
153         TELEPHONY_LOGE("write interface token failed.");
154         return HRIL_ERR_GENERIC_FAILURE;
155     }
156     struct HdfSBuf *dataSbuf = ParcelToSbuf(parcel.get());
157     if (dataSbuf == nullptr) {
158         return HRIL_ERR_NULL_POINT;
159     }
160     if (!HdfSbufWriteInt32(dataSbuf, slotId_)) {
161         HdfSbufRecycle(dataSbuf);
162         return HRIL_ERR_GENERIC_FAILURE;
163     }
164     if (!HdfSbufWriteUnpadBuffer(dataSbuf, (const uint8_t *)responseInfo, reqLen)) {
165         HdfSbufRecycle(dataSbuf);
166         return HRIL_ERR_GENERIC_FAILURE;
167     }
168     int32_t ret = ServiceDispatcher(requestNum, dataSbuf);
169     if (ret != HRIL_ERR_SUCCESS) {
170         HdfSbufRecycle(dataSbuf);
171         return HRIL_ERR_GENERIC_FAILURE;
172     }
173     if (dataSbuf != nullptr) {
174         HdfSbufRecycle(dataSbuf);
175     }
176     return HRIL_ERR_SUCCESS;
177 }
178 
ConvertHexStringToInt(char ** response,int32_t index,int32_t length)179 int32_t HRilBase::ConvertHexStringToInt(char **response, int32_t index, int32_t length)
180 {
181     const int32_t hexBase = HRIL_INVALID_HEX_CHAR;
182     if ((response == nullptr) || (length <= index) || (response[index] == nullptr)) {
183         return HRIL_ERR_GENERIC_FAILURE;
184     }
185     return strtol(response[index], nullptr, hexBase);
186 }
187 
ConvertIntToRadioNoticeType(int32_t indicationType)188 HRilNotiType HRilBase::ConvertIntToRadioNoticeType(int32_t indicationType)
189 {
190     return (indicationType == (int32_t)ReportType::HRIL_NOTIFICATION) ? (HRilNotiType::HRIL_NOTIFICATION) :
191                                                                         (HRilNotiType::HRIL_NO_DEFINE);
192 }
193 
ConvertHexCharToInt(uint8_t ch)194 uint8_t HRilBase::ConvertHexCharToInt(uint8_t ch)
195 {
196     if ((ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
197         return ((ch - 'A') % HRIL_UPPER_CASE_LETTERS_OFFSET + HRIL_DEC);
198     } else if (ch >= '0' && ch <= '9') {
199         return (ch - '0');
200     } else {
201         return HRIL_INVALID_HEX_CHAR;
202     }
203 }
204 
ConvertHexStringToBytes(const void * response,size_t length)205 uint8_t *HRilBase::ConvertHexStringToBytes(const void *response, size_t length)
206 {
207     const int32_t SIZE_VALUE = 2;
208     const int32_t BIT_NUM = 4;
209 
210     if (response == nullptr) {
211         TELEPHONY_LOGE("response is null!!!");
212         return nullptr;
213     }
214 
215     size_t bytesLen = length / SIZE_VALUE;
216     if (length % SIZE_VALUE != 0 || bytesLen <= 0) {
217         TELEPHONY_LOGE("invalid length: %{public}zu", length);
218         return nullptr;
219     }
220     uint8_t *bytes = (uint8_t *)calloc(bytesLen, sizeof(uint8_t));
221     if (bytes == nullptr) {
222         TELEPHONY_LOGE("ConvertHexStringToBytes: cannot allocate memory for bytes string");
223         return nullptr;
224     }
225 
226     uint8_t *hexStr = (uint8_t *)response;
227     size_t i = 0;
228     while (i < length) {
229         uint8_t hexCh1 = ConvertHexCharToInt(hexStr[i]);
230         uint8_t hexCh2 = ConvertHexCharToInt(hexStr[i + 1]);
231         if (hexCh1 == HRIL_INVALID_HEX_CHAR || hexCh2 == HRIL_INVALID_HEX_CHAR) {
232             free(bytes);
233             return nullptr;
234         }
235         bytes[i / SIZE_VALUE] = ((hexCh1 << BIT_NUM) | hexCh2);
236         i += SIZE_VALUE;
237     }
238     return bytes;
239 }
240 
ConvertToString(char ** dest,const std::string & srcStr)241 bool HRilBase::ConvertToString(char **dest, const std::string &srcStr)
242 {
243     size_t size = srcStr.size();
244     if (size == 0) {
245         *dest = nullptr;
246         return true;
247     }
248     size_t len = size + 1;
249     if (len <= 0) {
250         return false;
251     }
252 
253     *dest = (char *)calloc(len, sizeof(char));
254     if (*dest == nullptr) {
255         TELEPHONY_LOGE("ConvertToString malloc fail");
256         return false;
257     }
258     (void)strncpy_s(*dest, len, reinterpret_cast<const char *>(srcStr.c_str()), size);
259     return true;
260 }
261 
ServiceDispatcher(int32_t requestNum,const HdfSBuf * dataSbuf)262 int32_t HRilBase::ServiceDispatcher(int32_t requestNum, const HdfSBuf *dataSbuf)
263 {
264     return hrilReporter_.ReportToParent(requestNum, dataSbuf);
265 }
266 
ServiceNotifyDispatcher(int32_t requestNum,const HdfSBuf * dataSbuf)267 int32_t HRilBase::ServiceNotifyDispatcher(int32_t requestNum, const HdfSBuf *dataSbuf)
268 {
269     return hrilReporter_.NotifyToParent(requestNum, dataSbuf);
270 }
271 
CreateHRilRequest(int32_t serial,int32_t request)272 ReqDataInfo *HRilBase::CreateHRilRequest(int32_t serial, int32_t request)
273 {
274     return hrilReporter_.CreateHRilRequest(serial, slotId_, request);
275 }
276 } // namespace Telephony
277 } // namespace OHOS