• 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 "sms_sender.h"
17 
18 #include "core_manager_inner.h"
19 #include "ims_sms_client.h"
20 #include "radio_event.h"
21 #include "sms_hisysevent.h"
22 #include "string_utils.h"
23 #include "telephony_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 using namespace std;
28 using namespace std::chrono;
SmsSender(const shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId,function<void (shared_ptr<SmsSendIndexer>)> & sendRetryFun)29 SmsSender::SmsSender(const shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId,
30     function<void(shared_ptr<SmsSendIndexer>)> &sendRetryFun)
31     : AppExecFwk::EventHandler(runner), slotId_(slotId), sendRetryFun_(sendRetryFun)
32 {}
33 
~SmsSender()34 SmsSender::~SmsSender() {}
35 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)36 void SmsSender::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
37 {
38     if (event == nullptr) {
39         TELEPHONY_LOGE("event is nullptr");
40         return;
41     }
42     shared_ptr<SmsSendIndexer> smsIndexer = nullptr;
43     uint32_t eventId = event->GetInnerEventId();
44     TELEPHONY_LOGI("SmsSender::ProcessEvent eventId %{public}d", eventId);
45     switch (eventId) {
46         case RadioEvent::RADIO_SEND_SMS:
47         case RadioEvent::RADIO_SEND_CDMA_SMS:
48         case RadioEvent::RADIO_SEND_IMS_GSM_SMS:
49         case RadioEvent::RADIO_SEND_SMS_EXPECT_MORE: {
50             smsIndexer = FindCacheMapAndTransform(event);
51             HandleMessageResponse(smsIndexer);
52             break;
53         }
54         case MSG_SMS_RETRY_DELIVERY: {
55             smsIndexer = event->GetSharedObject<SmsSendIndexer>();
56             if (sendRetryFun_ != nullptr) {
57                 sendRetryFun_(smsIndexer);
58             }
59             break;
60         }
61         case RadioEvent::RADIO_SMS_STATUS: {
62             StatusReportAnalysis(event);
63             break;
64         }
65         case RadioEvent::RADIO_SET_IMS_SMS: {
66             StatusReportSetImsSms(event);
67             SyncSwitchISmsResponse();
68             break;
69         }
70         case RadioEvent::RADIO_GET_IMS_SMS: {
71             StatusReportGetImsSms(event);
72             SyncSwitchISmsResponse();
73             break;
74         }
75         default:
76             TELEPHONY_LOGE("SmsSender::ProcessEvent Unknown %{public}d", eventId);
77             break;
78     }
79 }
80 
SyncSwitchISmsResponse()81 void SmsSender::SyncSwitchISmsResponse()
82 {
83     std::unique_lock<std::mutex> lck(ctx_);
84     resIsSmsReady_ = true;
85     TELEPHONY_LOGI("resIsSmsReady_ = %{public}d", resIsSmsReady_);
86     cv_.notify_one();
87 }
88 
HandleMessageResponse(const shared_ptr<SmsSendIndexer> & smsIndexer)89 void SmsSender::HandleMessageResponse(const shared_ptr<SmsSendIndexer> &smsIndexer)
90 {
91     if (smsIndexer == nullptr) {
92         TELEPHONY_LOGE("smsIndexer is nullptr");
93         return;
94     }
95     if (!SendCacheMapEraseItem(smsIndexer->GetMsgRefId64Bit())) {
96         TELEPHONY_LOGE("SendCacheMapEraseItem fail !!!!!");
97     }
98     SendCacheMapTimeoutCheck();
99     if (!smsIndexer->GetIsFailure()) {
100         if (smsIndexer->GetDeliveryCallback() != nullptr) {
101             // Expecting a status report.  Add it to the list.
102             if (reportList_.size() > MAX_REPORT_LIST_LIMIT) {
103                 reportList_.pop_front();
104             }
105             reportList_.push_back(smsIndexer);
106         }
107         SendMessageSucceed(smsIndexer);
108     } else {
109         HandleResend(smsIndexer);
110     }
111 }
112 
SendMessageSucceed(const shared_ptr<SmsSendIndexer> & smsIndexer)113 void SmsSender::SendMessageSucceed(const shared_ptr<SmsSendIndexer> &smsIndexer)
114 {
115     if (smsIndexer == nullptr) {
116         TELEPHONY_LOGE("SendMessageSucceed but smsIndexer drop!");
117         return;
118     }
119     bool isLastPart = false;
120     std::shared_ptr<uint8_t> unSentCellCount = smsIndexer->GetUnSentCellCount();
121     if (unSentCellCount != nullptr) {
122         (*unSentCellCount) = (*unSentCellCount) - 1;
123         if ((*unSentCellCount) == 0) {
124             isLastPart = true;
125         }
126     }
127     if (isLastPart) {
128         smsIndexer->SetPsResendCount(INITIAL_COUNT);
129         smsIndexer->SetCsResendCount(INITIAL_COUNT);
130         ISendShortMessageCallback::SmsSendResult messageType = ISendShortMessageCallback::SEND_SMS_SUCCESS;
131         if (smsIndexer->GetHasCellFailed() != nullptr) {
132             if (*smsIndexer->GetHasCellFailed()) {
133                 messageType = ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN;
134             }
135         }
136         SendResultCallBack(smsIndexer->GetSendCallback(), messageType);
137         if (messageType == ISendShortMessageCallback::SEND_SMS_SUCCESS) {
138             SmsHiSysEvent::WriteSmsSendBehaviorEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE);
139         }
140     }
141 }
142 
SendMessageFailed(const shared_ptr<SmsSendIndexer> & smsIndexer)143 void SmsSender::SendMessageFailed(const shared_ptr<SmsSendIndexer> &smsIndexer)
144 {
145     if (smsIndexer == nullptr) {
146         TELEPHONY_LOGE("smsIndexer is nullptr");
147         return;
148     }
149     shared_ptr<bool> hasCellFailed = smsIndexer->GetHasCellFailed();
150     if (hasCellFailed != nullptr) {
151         *hasCellFailed = true;
152     }
153     bool isLastPart = false;
154     std::shared_ptr<uint8_t> unSentCellCount = smsIndexer->GetUnSentCellCount();
155     if (unSentCellCount == nullptr) {
156         TELEPHONY_LOGE("unSentCellCount is nullptr");
157         return;
158     }
159     (*unSentCellCount) = (*unSentCellCount) - 1;
160     if ((*unSentCellCount) == 0) {
161         isLastPart = true;
162     }
163     if (isLastPart) {
164         smsIndexer->SetPsResendCount(INITIAL_COUNT);
165         smsIndexer->SetCsResendCount(INITIAL_COUNT);
166         // save to db and update state
167         sptr<ISendShortMessageCallback> sendCallback = smsIndexer->GetSendCallback();
168         SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
169     }
170 }
171 
SendResultCallBack(const std::shared_ptr<SmsSendIndexer> & indexer,ISendShortMessageCallback::SmsSendResult result)172 void SmsSender::SendResultCallBack(
173     const std::shared_ptr<SmsSendIndexer> &indexer, ISendShortMessageCallback::SmsSendResult result)
174 {
175     if (indexer != nullptr && indexer->GetSendCallback() != nullptr) {
176         indexer->GetSendCallback()->OnSmsSendResult(result);
177     }
178 }
179 
SendResultCallBack(const sptr<ISendShortMessageCallback> & sendCallback,ISendShortMessageCallback::SmsSendResult result)180 void SmsSender::SendResultCallBack(
181     const sptr<ISendShortMessageCallback> &sendCallback, ISendShortMessageCallback::SmsSendResult result)
182 {
183     if (sendCallback != nullptr) {
184         sendCallback->OnSmsSendResult(result);
185     }
186 }
187 
SendCacheMapTimeoutCheck()188 void SmsSender::SendCacheMapTimeoutCheck()
189 {
190     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
191     system_clock::duration timePoint = system_clock::now().time_since_epoch();
192     seconds sec = duration_cast<seconds>(timePoint);
193     long timeStamp = sec.count();
194     auto item = sendCacheMap_.begin();
195     while (item != sendCacheMap_.end()) {
196         auto iter = item++;
197         shared_ptr<SmsSendIndexer> &indexer = iter->second;
198         if (indexer == nullptr || (timeStamp - indexer->GetTimeStamp()) > EXPIRED_TIME) {
199             sendCacheMap_.erase(iter);
200         }
201     }
202 }
203 
SendCacheMapLimitCheck(const sptr<ISendShortMessageCallback> & sendCallback)204 bool SmsSender::SendCacheMapLimitCheck(const sptr<ISendShortMessageCallback> &sendCallback)
205 {
206     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
207     if (sendCacheMap_.size() > MSG_QUEUE_LIMIT) {
208         SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
209         return true;
210     }
211     return false;
212 }
213 
SendCacheMapAddItem(int64_t id,const std::shared_ptr<SmsSendIndexer> & smsIndexer)214 bool SmsSender::SendCacheMapAddItem(int64_t id, const std::shared_ptr<SmsSendIndexer> &smsIndexer)
215 {
216     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
217     if (smsIndexer != nullptr) {
218         auto result = sendCacheMap_.emplace(id, smsIndexer);
219         return result.second;
220     }
221     return false;
222 }
223 
SendCacheMapEraseItem(int64_t id)224 bool SmsSender::SendCacheMapEraseItem(int64_t id)
225 {
226     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
227     return (sendCacheMap_.erase(id) != 0);
228 }
229 
FindCacheMapAndTransform(const AppExecFwk::InnerEvent::Pointer & event)230 std::shared_ptr<SmsSendIndexer> SmsSender::FindCacheMapAndTransform(const AppExecFwk::InnerEvent::Pointer &event)
231 {
232     if (event == nullptr) {
233         TELEPHONY_LOGE("event is nullptr");
234         return nullptr;
235     }
236     std::shared_ptr<SmsSendIndexer> smsIndexer = nullptr;
237     std::lock_guard<std::mutex> guard(sendCacheMapMutex_);
238     std::shared_ptr<HRilRadioResponseInfo> res = event->GetSharedObject<HRilRadioResponseInfo>();
239     if (res != nullptr) {
240         auto iter = sendCacheMap_.find(res->flag);
241         if (iter != sendCacheMap_.end()) {
242             smsIndexer = iter->second;
243             if (smsIndexer == nullptr) {
244                 TELEPHONY_LOGE("smsIndexer is nullptr");
245                 return nullptr;
246             }
247             smsIndexer->SetErrorCode(ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
248             smsIndexer->SetMsgRefId64Bit(res->flag);
249             smsIndexer->SetIsFailure(true);
250         }
251         return smsIndexer;
252     }
253 
254     std::shared_ptr<SendSmsResultInfo> info = event->GetSharedObject<SendSmsResultInfo>();
255     if (info != nullptr) {
256         auto iter = sendCacheMap_.find(info->flag);
257         if (iter != sendCacheMap_.end()) {
258             TELEPHONY_LOGI("msgRef = %{public}d", info->msgRef);
259             smsIndexer = iter->second;
260             if (smsIndexer == nullptr) {
261                 TELEPHONY_LOGE("smsIndexer is nullptr");
262                 return nullptr;
263             }
264             smsIndexer->SetMsgRefId((uint8_t)info->msgRef);
265             smsIndexer->SetAckPdu(std::move(StringUtils::HexToByteVector(info->pdu)));
266             if (info->errCode != 0) {
267                 smsIndexer->SetIsFailure(true);
268             }
269             smsIndexer->SetErrorCode(info->errCode);
270             smsIndexer->SetMsgRefId64Bit(info->flag);
271         }
272     }
273     return smsIndexer;
274 }
275 
SetImsSmsConfig(int32_t slotId,int32_t enable)276 bool SmsSender::SetImsSmsConfig(int32_t slotId, int32_t enable)
277 {
278     auto smsClient = DelayedSingleton<ImsSmsClient>::GetInstance();
279     if (smsClient == nullptr) {
280         TELEPHONY_LOGE("SetImsSmsConfig return, ImsSmsClient is nullptr.");
281         return false;
282     }
283     imsSmsCfg_ = enable;
284     std::unique_lock<std::mutex> lck(ctx_);
285     resIsSmsReady_ = false;
286 
287     int32_t reply = smsClient->ImsSetSmsConfig(slotId, enable);
288     TELEPHONY_LOGI("SetImsSmsConfig reply = %{public}d", reply);
289     while (resIsSmsReady_) {
290         TELEPHONY_LOGI("SetImsSmsConfig::wait(), resIsSmsReady_ = false");
291         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
292             break;
293         }
294     }
295     TELEPHONY_LOGI("SmsSender::SetImsSmsConfig(), %{public}d:", imsSmsCfg_);
296     return true;
297 }
298 
HandleResend(const std::shared_ptr<SmsSendIndexer> & smsIndexer)299 void SmsSender::HandleResend(const std::shared_ptr<SmsSendIndexer> &smsIndexer)
300 {
301     if (smsIndexer == nullptr) {
302         TELEPHONY_LOGE("smsIndexer is nullptr");
303         return;
304     }
305     // resending mechanism
306     bool errorCode = false;
307     if ((smsIndexer->GetErrorCode() == HRIL_ERR_GENERIC_FAILURE) ||
308         (smsIndexer->GetErrorCode() == HRIL_ERR_CMD_SEND_FAILURE)) {
309         errorCode = true;
310     }
311     bool csResend = false;
312     if (!lastSmsDomain_ && smsIndexer->GetCsResendCount() < MAX_SEND_RETRIES) {
313         csResend = true;
314     }
315     bool psResend = false;
316     if (lastSmsDomain_ && smsIndexer->GetPsResendCount() <= MAX_SEND_RETRIES) {
317         psResend = true;
318     }
319     if (errorCode && (csResend || psResend)) {
320         if (lastSmsDomain_ && psResend) {
321             smsIndexer->SetPsResendCount(smsIndexer->GetPsResendCount() + 1);
322             SendEvent(MSG_SMS_RETRY_DELIVERY, smsIndexer, DELAY_MAX_TIME_MSCE);
323         } else if (csResend) {
324             smsIndexer->SetCsResendCount(smsIndexer->GetCsResendCount() + 1);
325             SendEvent(MSG_SMS_RETRY_DELIVERY, smsIndexer, DELAY_MAX_TIME_MSCE);
326         }
327     } else {
328         SendMessageFailed(smsIndexer);
329     }
330 }
331 
GetMsgRef8Bit()332 uint8_t SmsSender::GetMsgRef8Bit()
333 {
334     return ++msgRef8bit_;
335 }
336 
GetMsgRef64Bit()337 int64_t SmsSender::GetMsgRef64Bit()
338 {
339     return ++msgRef64bit_;
340 }
341 
SetNetworkState(bool isImsNetDomain,int32_t voiceServiceState)342 void SmsSender::SetNetworkState(bool isImsNetDomain, int32_t voiceServiceState)
343 {
344     isImsNetDomain_ = isImsNetDomain;
345     voiceServiceState_ = voiceServiceState;
346     if (enableImsSmsOnceWhenImsReg_ && isImsNetDomain_) {
347         SetImsSmsConfig(slotId_, IMS_SMS_ENABLE);
348         enableImsSmsOnceWhenImsReg_ = false;
349     }
350     TELEPHONY_LOGI("isImsNetDomain = %{public}s voiceServiceState = %{public}d",
351         isImsNetDomain_ ? "true" : "false", voiceServiceState_);
352 }
353 
CheckForce7BitEncodeType()354 bool SmsSender::CheckForce7BitEncodeType()
355 {
356     auto helperPtr = DelayedSingleton<SmsPersistHelper>::GetInstance();
357     if (helperPtr == nullptr) {
358         TELEPHONY_LOGE("Check User Force 7Bit Encode Type helperPtr nullptr error.");
359         return false;
360     }
361     return helperPtr->QueryParamBoolean(SmsPersistHelper::SMS_ENCODING_PARAM_KEY, false);
362 }
363 
GetNetworkId()364 std::optional<int32_t> SmsSender::GetNetworkId()
365 {
366     return networkId_;
367 }
368 
SetNetworkId(std::optional<int32_t> & id)369 void SmsSender::SetNetworkId(std::optional<int32_t> &id)
370 {
371     networkId_ = id;
372 }
373 } // namespace Telephony
374 } // namespace OHOS