• 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_sms.h"
17 
18 #include "hril_notification.h"
19 #include "hril_request.h"
20 
21 namespace OHOS {
22 namespace Telephony {
23 namespace {
24 const size_t HEX_WIDTH = 2;
25 const size_t MAX_PDU_LEN = 255;
26 const size_t MAX_LEN = 100;
27 const size_t MAX_CHN_LEN = 50000;
28 const int32_t MSG_DEFAULT_INDEX = -1;
29 } // namespace
30 
HRilSms(int32_t slotId,IHRilReporter & hrilReporter)31 HRilSms::HRilSms(int32_t slotId, IHRilReporter &hrilReporter) : HRilBase(slotId, hrilReporter)
32 {
33     AddHandlerToMap();
34 }
35 
IsSmsRespOrNotify(uint32_t code)36 bool HRilSms::IsSmsRespOrNotify(uint32_t code)
37 {
38     return IsSmsResponse(code) || IsSmsNotification(code);
39 }
40 
RegisterSmsFuncs(const HRilSmsReq * smsFuncs)41 void HRilSms::RegisterSmsFuncs(const HRilSmsReq *smsFuncs)
42 {
43     smsFuncs_ = smsFuncs;
44 }
45 
AddHandlerToMap()46 void HRilSms::AddHandlerToMap()
47 {
48     // Response
49     respMemberFuncMap_[HREQ_SMS_SEND_GSM_SMS] = &HRilSms::SendGsmSmsResponse;
50     respMemberFuncMap_[HREQ_SMS_SEND_SMS_MORE_MODE] = &HRilSms::SendSmsMoreModeResponse;
51     respMemberFuncMap_[HREQ_SMS_SEND_SMS_ACK] = &HRilSms::SendSmsAckResponse;
52     respMemberFuncMap_[HREQ_SMS_ADD_SIM_MESSAGE] = &HRilSms::AddSimMessageResponse;
53     respMemberFuncMap_[HREQ_SMS_DEL_SIM_MESSAGE] = &HRilSms::DelSimMessageResponse;
54     respMemberFuncMap_[HREQ_SMS_UPDATE_SIM_MESSAGE] = &HRilSms::UpdateSimMessageResponse;
55     respMemberFuncMap_[HREQ_SMS_SET_SMSC_ADDR] = &HRilSms::SetSmscAddrResponse;
56     respMemberFuncMap_[HREQ_SMS_GET_SMSC_ADDR] = &HRilSms::GetSmscAddrResponse;
57     respMemberFuncMap_[HREQ_SMS_SET_CB_CONFIG] = &HRilSms::SetCBConfigResponse;
58     respMemberFuncMap_[HREQ_SMS_GET_CB_CONFIG] = &HRilSms::GetCBConfigResponse;
59     respMemberFuncMap_[HREQ_SMS_GET_CDMA_CB_CONFIG] = &HRilSms::GetCdmaCBConfigResponse;
60     respMemberFuncMap_[HREQ_SMS_SET_CDMA_CB_CONFIG] = &HRilSms::SetCdmaCBConfigResponse;
61     respMemberFuncMap_[HREQ_SMS_SEND_CDMA_SMS] = &HRilSms::SendCdmaSmsResponse;
62     respMemberFuncMap_[HREQ_SMS_ADD_CDMA_SIM_MESSAGE] = &HRilSms::AddCdmaSimMessageResponse;
63     respMemberFuncMap_[HREQ_SMS_DEL_CDMA_SIM_MESSAGE] = &HRilSms::DelCdmaSimMessageResponse;
64     respMemberFuncMap_[HREQ_SMS_UPDATE_CDMA_SIM_MESSAGE] = &HRilSms::UpdateCdmaSimMessageResponse;
65 
66     // Notification
67     notiMemberFuncMap_[HNOTI_SMS_NEW_SMS] = &HRilSms::NewSmsNotify;
68     notiMemberFuncMap_[HNOTI_SMS_NEW_CDMA_SMS] = &HRilSms::NewCdmaSmsNotify;
69     notiMemberFuncMap_[HNOTI_SMS_STATUS_REPORT] = &HRilSms::SmsStatusReportNotify;
70     notiMemberFuncMap_[HNOTI_SMS_NEW_SMS_STORED_ON_SIM] = &HRilSms::NewSmsStoredOnSimNotify;
71     notiMemberFuncMap_[HNOTI_CB_CONFIG_REPORT] = &HRilSms::CBConfigNotify;
72 }
73 
SendGsmSms(int32_t serialId,const OHOS::HDI::Ril::V1_1::GsmSmsMessageInfo & gsmSmsMessageInfo)74 int32_t HRilSms::SendGsmSms(int32_t serialId, const OHOS::HDI::Ril::V1_1::GsmSmsMessageInfo &gsmSmsMessageInfo)
75 {
76     const int32_t COUNT_STRINGS_VALUE = 2;
77     return RequestWithStrings(serialId, HREQ_SMS_SEND_GSM_SMS, COUNT_STRINGS_VALUE, gsmSmsMessageInfo.smscPdu.c_str(),
78         gsmSmsMessageInfo.pdu.c_str());
79 }
80 
SendCdmaSms(int32_t serialId,const OHOS::HDI::Ril::V1_1::SendCdmaSmsMessageInfo & cdmaSmsMessageInfo)81 int32_t HRilSms::SendCdmaSms(int32_t serialId, const OHOS::HDI::Ril::V1_1::SendCdmaSmsMessageInfo &cdmaSmsMessageInfo)
82 {
83     return RequestVendor(serialId, HREQ_SMS_SEND_CDMA_SMS, smsFuncs_, &HRilSmsReq::SendCdmaSms,
84         cdmaSmsMessageInfo.smscPdu.c_str(), cdmaSmsMessageInfo.smscPdu.size());
85 }
86 
AddSimMessage(int32_t serialId,const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo & message)87 int32_t HRilSms::AddSimMessage(int32_t serialId, const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo &message)
88 {
89     HRilSmsWriteSms msg = {};
90     msg.state = message.state;
91     msg.index = MSG_DEFAULT_INDEX;
92     size_t pduLen = message.pdu.length();
93     if (pduLen > MAX_PDU_LEN) {
94         return HRIL_ERR_INVALID_PARAMETER;
95     }
96     CopyToCharPoint(&msg.pdu, message.pdu);
97     size_t smscPduLen = message.smscPdu.length() + 1;
98     if (smscPduLen > MAX_PDU_LEN) {
99         SafeFrees(msg.pdu);
100         return HRIL_ERR_INVALID_PARAMETER;
101     }
102     CopyToCharPoint(&msg.smsc, message.smscPdu);
103     int32_t result = RequestVendor(
104         serialId, HREQ_SMS_ADD_SIM_MESSAGE, smsFuncs_, &HRilSmsReq::AddSimMessage, &msg, sizeof(HRilSmsWriteSms));
105     TELEPHONY_LOGI("AddSimMessage result is: %{public}d", result);
106     SafeFrees(msg.pdu, msg.smsc);
107     return result;
108 }
109 
DelSimMessage(int32_t serialId,int32_t index)110 int32_t HRilSms::DelSimMessage(int32_t serialId, int32_t index)
111 {
112     if ((smsFuncs_ == nullptr) || (smsFuncs_->DelSimMessage == nullptr)) {
113         TELEPHONY_LOGE("smsFuncs_ or smsFuncs_->DelSimMessage is null");
114         return HRIL_ERR_NULL_POINT;
115     }
116     ReqDataInfo *requestInfo = CreateHRilRequest(serialId, HREQ_SMS_DEL_SIM_MESSAGE);
117     if (requestInfo == nullptr) {
118         return HRIL_ERR_INVALID_PARAMETER;
119     }
120     int32_t *pBuff = nullptr;
121     RequestWithInts(&pBuff, requestInfo, 1, index);
122     smsFuncs_->DelSimMessage(requestInfo, pBuff, 1);
123     if (pBuff != nullptr) {
124         SafeFrees(pBuff);
125     }
126     return HRIL_ERR_SUCCESS;
127 }
128 
UpdateSimMessage(int32_t serialId,const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo & message)129 int32_t HRilSms::UpdateSimMessage(int32_t serialId, const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo &message)
130 {
131     HRilSmsWriteSms msg = {};
132     msg.state = message.state;
133     msg.index = message.index;
134     size_t len = message.pdu.size() + 1;
135     if (len > MAX_PDU_LEN) {
136         return HRIL_ERR_INVALID_PARAMETER;
137     }
138     CopyToCharPoint(&msg.pdu, message.pdu);
139     size_t smscPduLen = message.smscPdu.length() + 1;
140     if (smscPduLen > MAX_PDU_LEN) {
141         SafeFrees(msg.pdu);
142         return HRIL_ERR_INVALID_PARAMETER;
143     }
144     CopyToCharPoint(&msg.smsc, message.smscPdu);
145     int32_t result = RequestVendor(
146         serialId, HREQ_SMS_UPDATE_SIM_MESSAGE, smsFuncs_, &HRilSmsReq::UpdateSimMessage, &msg, sizeof(HRilSmsWriteSms));
147     TELEPHONY_LOGI("UpdateSimMessage result is: %{public}d", result);
148     SafeFrees(msg.pdu, msg.smsc);
149     return result;
150 }
151 
SetSmscAddr(int32_t serialId,const OHOS::HDI::Ril::V1_1::ServiceCenterAddress & serCenterAddress)152 int32_t HRilSms::SetSmscAddr(int32_t serialId, const OHOS::HDI::Ril::V1_1::ServiceCenterAddress &serCenterAddress)
153 {
154     HRilServiceCenterAddress address;
155     size_t len = 0;
156     len = strlen(serCenterAddress.address.c_str()) + 1;
157     address.tosca = serCenterAddress.tosca;
158     if (len > MAX_LEN) {
159         return HRIL_ERR_INVALID_PARAMETER;
160     }
161     CopyToCharPoint(&address.address, serCenterAddress.address);
162     int32_t result = RequestVendor(serialId, HREQ_SMS_SET_SMSC_ADDR, smsFuncs_, &HRilSmsReq::SetSmscAddr, &address,
163         sizeof(HRilServiceCenterAddress));
164     TELEPHONY_LOGI("SetSmscAddr result is: %{public}d", result);
165     SafeFrees(address.address);
166     return result;
167 }
168 
GetSmscAddr(int32_t serialId)169 int32_t HRilSms::GetSmscAddr(int32_t serialId)
170 {
171     return RequestVendor(serialId, HREQ_SMS_GET_SMSC_ADDR, smsFuncs_, &HRilSmsReq::GetSmscAddr);
172 }
173 
SetCBConfig(int32_t serialId,const OHOS::HDI::Ril::V1_1::CBConfigInfo & broadcastInfo)174 int32_t HRilSms::SetCBConfig(int32_t serialId, const OHOS::HDI::Ril::V1_1::CBConfigInfo &broadcastInfo)
175 {
176     HRilCBConfigInfo cellBroadcastInfo;
177     cellBroadcastInfo.mode = broadcastInfo.mode;
178     size_t midsLen = broadcastInfo.mids.size() + 1;
179     if (midsLen > MAX_CHN_LEN) {
180         return HRIL_ERR_INVALID_PARAMETER;
181     }
182     CopyToCharPoint(&cellBroadcastInfo.mids, broadcastInfo.mids);
183     size_t dcssLen = broadcastInfo.dcss.size() + 1;
184     if (dcssLen <= 0) {
185         SafeFrees(cellBroadcastInfo.mids);
186         return HRIL_ERR_INVALID_PARAMETER;
187     }
188     CopyToCharPoint(&cellBroadcastInfo.dcss, broadcastInfo.dcss);
189     int32_t result = RequestVendor(serialId, HREQ_SMS_SET_CB_CONFIG, smsFuncs_, &HRilSmsReq::SetCBConfig,
190         &cellBroadcastInfo, sizeof(HRilServiceCenterAddress));
191     TELEPHONY_LOGI("SetCBConfig result is: %{public}d", result);
192     SafeFrees(cellBroadcastInfo.mids, cellBroadcastInfo.dcss);
193     return result;
194 }
195 
GetCBConfig(int32_t serialId)196 int32_t HRilSms::GetCBConfig(int32_t serialId)
197 {
198     return RequestVendor(serialId, HREQ_SMS_GET_CB_CONFIG, smsFuncs_, &HRilSmsReq::GetCBConfig);
199 }
200 
SetCdmaCBConfig(int32_t serialId,const OHOS::HDI::Ril::V1_1::CdmaCBConfigInfoList & cellBroadcastInfoList)201 int32_t HRilSms::SetCdmaCBConfig(
202     int32_t serialId, const OHOS::HDI::Ril::V1_1::CdmaCBConfigInfoList &cellBroadcastInfoList)
203 {
204     if ((smsFuncs_ == nullptr) || (smsFuncs_->SetCdmaCBConfig == nullptr)) {
205         TELEPHONY_LOGE("(smsFuncs_ or msFuncs_->SetCdmaCBConfig is null");
206         return HRIL_ERR_NULL_POINT;
207     }
208     ReqDataInfo *requestInfo = CreateHRilRequest(serialId, HREQ_SMS_SET_CDMA_CB_CONFIG);
209     if (requestInfo == nullptr) {
210         return HRIL_ERR_INVALID_PARAMETER;
211     }
212     auto size = cellBroadcastInfoList.size;
213     std::unique_ptr<HRilCdmaCBConfigInfo[]> list = std::make_unique<HRilCdmaCBConfigInfo[]>(size);
214     CopyToHRilCdmaCBConfigInfo(list.get(), cellBroadcastInfoList);
215     smsFuncs_->SetCdmaCBConfig(requestInfo, list.get(), cellBroadcastInfoList.size * sizeof(HRilCdmaCBConfigInfo));
216     return HRIL_ERR_SUCCESS;
217 }
218 
GetCdmaCBConfig(int32_t serialId)219 int32_t HRilSms::GetCdmaCBConfig(int32_t serialId)
220 {
221     return RequestVendor(serialId, HREQ_SMS_GET_CDMA_CB_CONFIG, smsFuncs_, &HRilSmsReq::GetCdmaCBConfig);
222 }
223 
SendSmsMoreMode(int32_t serialId,const OHOS::HDI::Ril::V1_1::GsmSmsMessageInfo & gsmSmsMessageInfo)224 int32_t HRilSms::SendSmsMoreMode(int32_t serialId, const OHOS::HDI::Ril::V1_1::GsmSmsMessageInfo &gsmSmsMessageInfo)
225 {
226     const int32_t COUNT_STRINGS_VALUE = 2;
227     return RequestWithStrings(serialId, HREQ_SMS_SEND_SMS_MORE_MODE, COUNT_STRINGS_VALUE,
228         gsmSmsMessageInfo.smscPdu.c_str(), gsmSmsMessageInfo.pdu.c_str());
229 }
230 
SendSmsAck(int32_t serialId,const OHOS::HDI::Ril::V1_1::ModeData & modeData)231 int32_t HRilSms::SendSmsAck(int32_t serialId, const OHOS::HDI::Ril::V1_1::ModeData &modeData)
232 {
233     if ((smsFuncs_ == nullptr) || (smsFuncs_->SendSmsAck == nullptr)) {
234         TELEPHONY_LOGE("smsFuncs_ or smsFuncs_->SendSmsAck is null");
235         return HRIL_ERR_NULL_POINT;
236     }
237     ReqDataInfo *requestInfo = CreateHRilRequest(serialId, HREQ_SMS_SEND_SMS_ACK);
238     if (requestInfo == nullptr) {
239         return HRIL_ERR_INVALID_PARAMETER;
240     }
241     int32_t *pBuff = nullptr;
242     const int32_t COUNT_INTS_VALUE = 2;
243     RequestWithInts(&pBuff, requestInfo, COUNT_INTS_VALUE, static_cast<int32_t>(modeData.result), modeData.mode);
244     smsFuncs_->SendSmsAck(requestInfo, pBuff, sizeof(int32_t));
245     if (pBuff != nullptr) {
246         SafeFrees(pBuff);
247     }
248     return HRIL_ERR_SUCCESS;
249 }
250 
AddCdmaSimMessage(int32_t serialId,const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo & message)251 int32_t HRilSms::AddCdmaSimMessage(int32_t serialId, const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo &message)
252 {
253     HRilSmsWriteSms msg = {};
254     int32_t pduLen = 0;
255     const int32_t MSG_INDEX = -1;
256     msg.state = message.state;
257     msg.index = MSG_INDEX;
258     pduLen = message.pdu.length();
259     int32_t len = pduLen + 1;
260     if (len <= 0) {
261         return HRIL_ERR_INVALID_PARAMETER;
262     }
263     CopyToCharPoint(&msg.pdu, message.pdu);
264     int32_t result = RequestVendor(serialId, HREQ_SMS_ADD_CDMA_SIM_MESSAGE, smsFuncs_, &HRilSmsReq::AddCdmaSimMessage,
265         &msg, sizeof(HRilSmsWriteSms));
266     TELEPHONY_LOGI("AddCdmaSimMessage result is: %{public}d", result);
267     SafeFrees(msg.pdu);
268     return result;
269 }
270 
DelCdmaSimMessage(int32_t serialId,int32_t index)271 int32_t HRilSms::DelCdmaSimMessage(int32_t serialId, int32_t index)
272 {
273     if ((smsFuncs_ == nullptr) || (smsFuncs_->DelCdmaSimMessage == nullptr)) {
274         TELEPHONY_LOGE("smsFuncs_ or smsFuncs_->DelCdmaSimMessage is null");
275         return HRIL_ERR_NULL_POINT;
276     }
277     ReqDataInfo *requestInfo = CreateHRilRequest(serialId, HREQ_SMS_DEL_CDMA_SIM_MESSAGE);
278     if (requestInfo == nullptr) {
279         return HRIL_ERR_INVALID_PARAMETER;
280     }
281     int32_t *pBuff = nullptr;
282     RequestWithInts(&pBuff, requestInfo, 1, index);
283     smsFuncs_->DelCdmaSimMessage(requestInfo, pBuff, 1);
284     if (pBuff != nullptr) {
285         SafeFrees(pBuff);
286     }
287     return HRIL_ERR_SUCCESS;
288 }
289 
UpdateCdmaSimMessage(int32_t serialId,const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo & message)290 int32_t HRilSms::UpdateCdmaSimMessage(int32_t serialId, const OHOS::HDI::Ril::V1_1::SmsMessageIOInfo &message)
291 {
292     HRilSmsWriteSms msg = {};
293     size_t len = 0;
294     len = message.pdu.size() + 1;
295     msg.state = message.state;
296     if (len > MAX_CHN_LEN) {
297         return HRIL_ERR_INVALID_PARAMETER;
298     }
299     CopyToCharPoint(&msg.pdu, message.pdu);
300     msg.index = message.index;
301     RequestVendor(serialId, HREQ_SMS_UPDATE_CDMA_SIM_MESSAGE, smsFuncs_, &HRilSmsReq::UpdateCdmaSimMessage, &msg,
302         sizeof(HRilSmsWriteSms));
303     SafeFrees(msg.pdu);
304     return HRIL_ERR_SUCCESS;
305 }
306 
SendGsmSmsResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)307 int32_t HRilSms::SendGsmSmsResponse(
308     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
309 {
310     HDI::Ril::V1_1::SendSmsResultInfo result =
311         MakeSendSmsResult(responseInfo, responseInfo.serial, response, responseLen);
312     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::SendGsmSmsResponse, result);
313 }
314 
SendCdmaSmsResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)315 int32_t HRilSms::SendCdmaSmsResponse(
316     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
317 {
318     HDI::Ril::V1_1::SendSmsResultInfo result =
319         MakeSendSmsResult(responseInfo, responseInfo.serial, response, responseLen);
320     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::SendCdmaSmsResponse, result);
321 }
322 
AddSimMessageResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)323 int32_t HRilSms::AddSimMessageResponse(
324     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
325 {
326     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::AddSimMessageResponse);
327 }
328 
DelSimMessageResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)329 int32_t HRilSms::DelSimMessageResponse(
330     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
331 {
332     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::DelSimMessageResponse);
333 }
334 
UpdateSimMessageResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)335 int32_t HRilSms::UpdateSimMessageResponse(
336     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
337 {
338     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::UpdateSimMessageResponse);
339 }
340 
SetSmscAddrResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)341 int32_t HRilSms::SetSmscAddrResponse(
342     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
343 {
344     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::SetSmscAddrResponse);
345 }
346 
GetSmscAddrResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)347 int32_t HRilSms::GetSmscAddrResponse(
348     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
349 {
350     HDI::Ril::V1_1::ServiceCenterAddress result;
351     if (response == nullptr || responseLen != sizeof(HRilServiceCenterAddress)) {
352         TELEPHONY_LOGE("Invalid response: response is nullptr");
353         if (responseInfo.error == HRilErrType::NONE) {
354             responseInfo.error = HRilErrType::HRIL_ERR_INVALID_RESPONSE;
355         }
356         result.address = std::string("");
357     } else {
358         const HRilServiceCenterAddress *address = static_cast<const HRilServiceCenterAddress *>(response);
359         result.tosca = address->tosca;
360         if (address->address == nullptr) {
361             result.address = std::string("");
362             TELEPHONY_LOGE("address is nullptr");
363         } else {
364             result.address = std::string(address->address);
365         }
366         TELEPHONY_LOGI("result address:%{private}s, tosca:%{private}d", result.address.c_str(), result.tosca);
367     }
368     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::GetSmscAddrResponse, result);
369 }
370 
SetCBConfigResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)371 int32_t HRilSms::SetCBConfigResponse(
372     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
373 {
374     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::SetCBConfigResponse);
375 }
376 
GetCBConfigResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)377 int32_t HRilSms::GetCBConfigResponse(
378     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
379 {
380     HDI::Ril::V1_1::CBConfigInfo broadcastInfo;
381     if (response == nullptr || responseLen != sizeof(HRilCBConfigInfo)) {
382         TELEPHONY_LOGE("Invalid response: response is nullptr");
383         broadcastInfo.mode = -1;
384         broadcastInfo.mids = std::string("");
385         broadcastInfo.dcss = std::string("");
386     } else {
387         HRilCBConfigInfo *cellBroadcastInfo = (HRilCBConfigInfo *)response;
388         broadcastInfo.mode = cellBroadcastInfo->mode;
389         if (cellBroadcastInfo->mids == nullptr) {
390             broadcastInfo.mids = std::string("");
391             TELEPHONY_LOGE("result.mids is nullptr");
392         } else {
393             TELEPHONY_LOGI("result.mids :%{private}s", cellBroadcastInfo->mids);
394             broadcastInfo.mids = std::string(cellBroadcastInfo->mids);
395         }
396         if (cellBroadcastInfo->dcss == nullptr) {
397             broadcastInfo.dcss = std::string("");
398             TELEPHONY_LOGE("result.dcss is nullptr");
399         } else {
400             TELEPHONY_LOGI("result.dcss :%{private}s", cellBroadcastInfo->dcss);
401             broadcastInfo.dcss = std::string(cellBroadcastInfo->dcss);
402         }
403         TELEPHONY_LOGD("mode:%{private}d, mids:%{private}s, dcss:%{private}s", cellBroadcastInfo->mode,
404             cellBroadcastInfo->mids, cellBroadcastInfo->dcss);
405     }
406     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::GetCBConfigResponse, broadcastInfo);
407 }
408 
SetCdmaCBConfigResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)409 int32_t HRilSms::SetCdmaCBConfigResponse(
410     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
411 {
412     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::SetCdmaCBConfigResponse);
413 }
414 
GetCdmaCBConfigResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)415 int32_t HRilSms::GetCdmaCBConfigResponse(
416     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
417 {
418     HDI::Ril::V1_1::CdmaCBConfigInfo broadcastInfo;
419     if (response == nullptr || responseLen != sizeof(HRilCdmaCBConfigInfo)) {
420         TELEPHONY_LOGE("Invalid response: response is nullptr");
421         broadcastInfo.checked = -1;
422         broadcastInfo.language = -1;
423         broadcastInfo.service = -1;
424     } else {
425         const HRilCdmaCBConfigInfo *cellBroadcastInfo = static_cast<const HRilCdmaCBConfigInfo *>(response);
426         broadcastInfo.checked = cellBroadcastInfo->checked;
427         broadcastInfo.language = cellBroadcastInfo->language;
428         broadcastInfo.service = cellBroadcastInfo->service;
429         TELEPHONY_LOGE("checked:%{public}c, language:%{public}d, service:%{public}d", cellBroadcastInfo->checked,
430             cellBroadcastInfo->language, cellBroadcastInfo->service);
431     }
432     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::GetCdmaCBConfigResponse, broadcastInfo);
433 }
434 
SendSmsMoreModeResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)435 int32_t HRilSms::SendSmsMoreModeResponse(
436     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
437 {
438     HDI::Ril::V1_1::SendSmsResultInfo result =
439         MakeSendSmsResult(responseInfo, responseInfo.serial, response, responseLen);
440     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::SendSmsMoreModeResponse, result);
441 }
442 
SendSmsAckResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)443 int32_t HRilSms::SendSmsAckResponse(
444     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
445 {
446     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::SendSmsAckResponse);
447 }
448 
AddCdmaSimMessageResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)449 int32_t HRilSms::AddCdmaSimMessageResponse(
450     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
451 {
452     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::AddCdmaSimMessageResponse);
453 }
454 
DelCdmaSimMessageResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)455 int32_t HRilSms::DelCdmaSimMessageResponse(
456     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
457 {
458     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::DelCdmaSimMessageResponse);
459 }
460 
UpdateCdmaSimMessageResponse(int32_t requestNum,HRilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)461 int32_t HRilSms::UpdateCdmaSimMessageResponse(
462     int32_t requestNum, HRilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
463 {
464     return Response(responseInfo, &HDI::Ril::V1_1::IRilCallback::UpdateCdmaSimMessageResponse);
465 }
466 
SmsStatusReportNotify(int32_t indType,const HRilErrNumber error,const void * response,size_t responseLen)467 int32_t HRilSms::SmsStatusReportNotify(
468     int32_t indType, const HRilErrNumber error, const void *response, size_t responseLen)
469 {
470     if (response == nullptr || responseLen == 0) {
471         TELEPHONY_LOGE("invalid response");
472         return HRIL_ERR_GENERIC_FAILURE;
473     }
474     HDI::Ril::V1_1::SmsMessageInfo smsMessageInfo;
475     uint8_t *bytes = ConvertHexStringToBytes(response, responseLen);
476     if (bytes == nullptr) {
477         TELEPHONY_LOGE("ConvertHexStringToBytes in SmsStatusReportNotify is failed!");
478         return HRIL_ERR_GENERIC_FAILURE;
479     }
480     const size_t MESSAGE_SIZE = responseLen / HEX_WIDTH;
481     smsMessageInfo.size = MESSAGE_SIZE;
482     smsMessageInfo.indicationType = static_cast<int32_t>(ConvertIntToRadioNoticeType(indType));
483     uint8_t *temp = bytes;
484     for (int32_t i = 0; i < smsMessageInfo.size; i++) {
485         smsMessageInfo.pdu.push_back(*temp);
486         temp++;
487     }
488     SafeFrees(bytes);
489     return Notify(indType, error, &HDI::Ril::V1_1::IRilCallback::SmsStatusReportNotify, smsMessageInfo);
490 }
491 
NewSmsStoredOnSimNotify(int32_t indType,const HRilErrNumber error,const void * response,size_t responseLen)492 int32_t HRilSms::NewSmsStoredOnSimNotify(
493     int32_t indType, const HRilErrNumber error, const void *response, size_t responseLen)
494 {
495     if (response == nullptr || responseLen != sizeof(int32_t)) {
496         TELEPHONY_LOGE("invalid response");
497         return HRIL_ERR_SUCCESS;
498     }
499     int32_t recordNumber = *(static_cast<const int32_t *>(response));
500     indType = static_cast<int32_t>(ConvertIntToRadioNoticeType(indType));
501     return Notify(indType, error, &HDI::Ril::V1_1::IRilCallback::NewSmsStoredOnSimNotify, recordNumber, indType);
502 }
503 
NewSmsNotify(int32_t indType,const HRilErrNumber error,const void * response,size_t responseLen)504 int32_t HRilSms::NewSmsNotify(int32_t indType, const HRilErrNumber error, const void *response, size_t responseLen)
505 {
506     HRilSmsResponse *smsResponse = nullptr;
507     if (response == nullptr || responseLen == 0) {
508         TELEPHONY_LOGE("NewSmsNotify: invalid response");
509         return HDF_FAILURE;
510     } else {
511         smsResponse = (HRilSmsResponse *)response;
512     }
513     uint8_t *bytes = ConvertHexStringToBytes(smsResponse->pdu, responseLen);
514     if (bytes == nullptr) {
515         TELEPHONY_LOGE("NewSmsNotify: ConvertHexStringToBytes failed");
516         return HRIL_ERR_GENERIC_FAILURE;
517     }
518     HDI::Ril::V1_1::SmsMessageInfo smsMessageInfo;
519     const size_t NEW_SMS_SIZE = responseLen / HEX_WIDTH;
520     smsMessageInfo.size = NEW_SMS_SIZE;
521     smsMessageInfo.indicationType = indType;
522     uint8_t *temp = bytes;
523     for (int32_t i = 0; i < static_cast<int32_t>(smsMessageInfo.size); i++) {
524         smsMessageInfo.pdu.push_back(*temp);
525         temp++;
526     }
527     SafeFrees(bytes);
528     return Notify(indType, error, &HDI::Ril::V1_1::IRilCallback::NewSmsNotify, smsMessageInfo);
529 }
530 
NewCdmaSmsNotify(int32_t indType,const HRilErrNumber error,const void * response,size_t responseLen)531 int32_t HRilSms::NewCdmaSmsNotify(int32_t indType, const HRilErrNumber error, const void *response, size_t responseLen)
532 {
533     HRilSmsResponse *message = nullptr;
534     if (response == nullptr || responseLen == 0) {
535         TELEPHONY_LOGE("invalid response");
536         return HRIL_ERR_GENERIC_FAILURE;
537     } else {
538         message = (HRilSmsResponse *)response;
539     }
540     HDI::Ril::V1_1::SmsMessageInfo messageInfo;
541     messageInfo.indicationType = indType;
542     if (message->pdu != nullptr) {
543         messageInfo.size = strlen(message->pdu) / HEX_WIDTH;
544         uint8_t *bytes = ConvertHexStringToBytes(message->pdu, strlen(message->pdu));
545         if (bytes == nullptr) {
546             TELEPHONY_LOGE("bytes is nullptr");
547             return HRIL_ERR_GENERIC_FAILURE;
548         }
549         uint8_t *temp = bytes;
550         for (int32_t i = 0; i < messageInfo.size; i++) {
551             messageInfo.pdu.push_back(*temp);
552             temp++;
553         }
554         SafeFrees(bytes);
555     }
556     return Notify(indType, error, &HDI::Ril::V1_1::IRilCallback::NewCdmaSmsNotify, messageInfo);
557 }
558 
CBConfigNotify(int32_t indType,const HRilErrNumber error,const void * response,size_t responseLen)559 int32_t HRilSms::CBConfigNotify(int32_t indType, const HRilErrNumber error, const void *response, size_t responseLen)
560 {
561     HDI::Ril::V1_1::CBConfigReportInfo result = MakeCBConfigResult(response, responseLen);
562     result.indicationType = indType;
563     return Notify(indType, error, &HDI::Ril::V1_1::IRilCallback::CBConfigNotify, result);
564 }
565 
IsSmsResponse(uint32_t code)566 bool HRilSms::IsSmsResponse(uint32_t code)
567 {
568     return ((code >= HREQ_SMS_BASE) && (code < HREQ_SIM_BASE));
569 }
570 
IsSmsNotification(uint32_t code)571 bool HRilSms::IsSmsNotification(uint32_t code)
572 {
573     return ((code >= HNOTI_SMS_BASE) && (code < HNOTI_SIM_BASE));
574 }
575 
RequestWithInts(int32_t ** p,ReqDataInfo * requestInfo,int32_t argCount,...)576 int32_t HRilSms::RequestWithInts(int32_t **p, ReqDataInfo *requestInfo, int32_t argCount, ...)
577 {
578     size_t len = sizeof(int32_t);
579     if (len <= 0 || argCount <= 0) {
580         return false;
581     }
582     *p = static_cast<int32_t *>(malloc(argCount * len));
583     if (*p == nullptr) {
584         TELEPHONY_LOGE("req: [%{public}d,%{public}d,%{public}d], malloc fail", requestInfo->serial,
585             static_cast<int32_t>(requestInfo->slotId), requestInfo->request);
586         return false;
587     }
588     if (memset_s(*p, argCount * len, 0, argCount * len) != EOK) {
589         TELEPHONY_LOGE("RequestWithInts memset_s failed");
590         SafeFrees(*p);
591         return false;
592     }
593     va_list list;
594     va_start(list, argCount);
595     int32_t i = 0;
596     while (i < argCount) {
597         (*p)[i] = va_arg(list, int32_t);
598         i++;
599     }
600     va_end(list);
601     return true;
602 }
603 
RequestWithStrings(int32_t serial,int32_t request,int32_t count,...)604 int32_t HRilSms::RequestWithStrings(int32_t serial, int32_t request, int32_t count, ...)
605 {
606     if ((smsFuncs_ == nullptr) || (smsFuncs_->SendGsmSms == nullptr)) {
607         TELEPHONY_LOGE("smsFuncs_ or smsFuncs_->SendGsmSms is null");
608         return HRIL_ERR_NULL_POINT;
609     }
610     char **pBuff = nullptr;
611     if (count <= 0) {
612         return HRIL_ERR_NULL_POINT;
613     }
614     int32_t len = count * sizeof(char *);
615     pBuff = (char **)malloc(len);
616     if (pBuff == nullptr) {
617         TELEPHONY_LOGE("req: [%{public}d,%{public}d], malloc fail", serial, request);
618         return HRIL_ERR_NULL_POINT;
619     }
620     if (memset_s(pBuff, len, 0, len) != EOK) {
621         TELEPHONY_LOGE("RequestWithInts memset_s failed");
622         SafeFrees(pBuff);
623         return false;
624     }
625     va_list list;
626     va_start(list, count);
627     int32_t i = 0;
628     while (i < count) {
629         const char *str = va_arg(list, const char *);
630         if (!ConvertToString(&pBuff[i], str)) {
631             TELEPHONY_LOGE("ConvertToString in RequestWithStrings is failed!");
632             va_end(list);
633             for (int32_t j = 0; j < i; j++) {
634                 SafeFrees(pBuff[j]);
635             }
636             SafeFrees(pBuff);
637             return HRIL_ERR_NULL_POINT;
638         }
639         i++;
640     }
641     va_end(list);
642     int32_t result = RequestVendor(serial, request, smsFuncs_, &HRilSmsReq::SendGsmSms, pBuff, count);
643     if (pBuff != nullptr) {
644         i = 0;
645         while (i < count) {
646             SafeFrees(pBuff[i]);
647             i++;
648         }
649         SafeFrees(pBuff);
650     }
651     return result;
652 }
653 
MakeCBConfigResult(const void * response,const size_t responseLen)654 HDI::Ril::V1_1::CBConfigReportInfo HRilSms::MakeCBConfigResult(const void *response, const size_t responseLen)
655 {
656     HDI::Ril::V1_1::CBConfigReportInfo result;
657     if (response == nullptr || responseLen != sizeof(HRilCBConfigReportInfo)) {
658         TELEPHONY_LOGE("Invalid response: response is nullptr");
659         result.data = std::string("");
660         result.pdu = std::string("");
661         result.dcs = std::string("");
662     } else {
663         const HRilCBConfigReportInfo *cellBroadcastReportInfo = static_cast<const HRilCBConfigReportInfo *>(response);
664         result.mid = cellBroadcastReportInfo->mid;
665         result.length = cellBroadcastReportInfo->length;
666         result.page = cellBroadcastReportInfo->page;
667         result.pages = cellBroadcastReportInfo->pages;
668         result.sn = cellBroadcastReportInfo->sn;
669         if (cellBroadcastReportInfo->data == nullptr) {
670             result.data = std::string("");
671             TELEPHONY_LOGE("result.data is nullptr");
672         } else {
673             TELEPHONY_LOGI("result.data :%{private}s", cellBroadcastReportInfo->data);
674             result.data = std::string(cellBroadcastReportInfo->data);
675         }
676         if (cellBroadcastReportInfo->pdu == nullptr) {
677             result.pdu = std::string("");
678             TELEPHONY_LOGE("result.pdu is nullptr");
679         } else {
680             TELEPHONY_LOGI("result.pdu :%{private}s", cellBroadcastReportInfo->pdu);
681             result.pdu = std::string(cellBroadcastReportInfo->pdu);
682         }
683         if (cellBroadcastReportInfo->dcs == nullptr) {
684             result.dcs = std::string("");
685             TELEPHONY_LOGE("result.dcs is nullptr");
686         } else {
687             TELEPHONY_LOGI("result.dcs :%{private}s", cellBroadcastReportInfo->dcs);
688             result.dcs = std::string(cellBroadcastReportInfo->dcs);
689         }
690     }
691     return result;
692 }
693 
MakeSendSmsResult(HRilRadioResponseInfo & responseInfo,int32_t serial,const void * response,const size_t responseLen)694 HDI::Ril::V1_1::SendSmsResultInfo HRilSms::MakeSendSmsResult(
695     HRilRadioResponseInfo &responseInfo, int32_t serial, const void *response, const size_t responseLen)
696 {
697     HDI::Ril::V1_1::SendSmsResultInfo result;
698     if (response == nullptr || responseLen != sizeof(HRilSmsResponse)) {
699         TELEPHONY_LOGE("Invalid response: response is nullptr");
700         if (responseInfo.error == HRilErrType::NONE) {
701             responseInfo.error = HRilErrType::HRIL_ERR_INVALID_RESPONSE;
702         }
703         result.pdu = std::string("");
704     } else {
705         const HRilSmsResponse *smsResponse = static_cast<const HRilSmsResponse *>(response);
706         result.msgRef = smsResponse->msgRef;
707         if (smsResponse->pdu == nullptr) {
708             result.pdu = std::string("");
709             TELEPHONY_LOGE("result.pdu is nullptr");
710         } else {
711             result.pdu = std::string(smsResponse->pdu);
712         }
713         result.errCode = smsResponse->errCode;
714     }
715     return result;
716 }
717 
CopyToHRilCdmaCBConfigInfo(HRilCdmaCBConfigInfo * list,OHOS::HDI::Ril::V1_1::CdmaCBConfigInfoList cellBroadcastInfoList)718 void HRilSms::CopyToHRilCdmaCBConfigInfo(
719     HRilCdmaCBConfigInfo *list, OHOS::HDI::Ril::V1_1::CdmaCBConfigInfoList cellBroadcastInfoList)
720 {
721     for (int32_t i = 0; i < cellBroadcastInfoList.size; i++) {
722         list[i].service = cellBroadcastInfoList.list[i].service;
723         list[i].language = cellBroadcastInfoList.list[i].language;
724         list[i].checked = cellBroadcastInfoList.list[i].checked;
725     }
726 }
727 } // namespace Telephony
728 } // namespace OHOS
729