• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 "sms_send_manager.h"
17 
18 #include <functional>
19 #include <memory.h>
20 
21 #include "cdma_sms_message.h"
22 #include "gsm_sms_message.h"
23 #include "gsm_sms_tpdu_codec.h"
24 #include "i_sms_service_interface.h"
25 #include "runner_pool.h"
26 #include "sms_hisysevent.h"
27 #include "sms_receive_manager.h"
28 #include "telephony_errors.h"
29 #include "telephony_log_wrapper.h"
30 
31 namespace OHOS {
32 namespace Telephony {
33 static constexpr const char *NET_TYPE_GSM_STRING = "3gpp";
34 static constexpr const char *NET_TYPE_CDMA_STRING = "3gpp2";
35 static constexpr const char *NET_TYPE_UNKNOWN_STRING = "unknown";
36 
37 using namespace std;
SmsSendManager(int32_t slotId)38 SmsSendManager::SmsSendManager(int32_t slotId) : slotId_(slotId) {}
39 
~SmsSendManager()40 SmsSendManager::~SmsSendManager()
41 {
42     if (gsmSmsSender_ != nullptr && networkManager_ != nullptr) {
43         if (auto id = gsmSmsSender_->GetNetworkId(); id.has_value()) {
44             networkManager_->NetworkUnregister(id.value());
45         }
46     }
47     if (cdmaSmsSender_ != nullptr && networkManager_ != nullptr) {
48         if (auto id = cdmaSmsSender_->GetNetworkId(); id.has_value()) {
49             networkManager_->NetworkUnregister(id.value());
50         }
51     }
52     if (networkManager_ != nullptr) {
53         networkManager_->UnRegisterHandler();
54     }
55 }
56 
Init()57 void SmsSendManager::Init()
58 {
59     gsmSmsSendRunner_ = RunnerPool::GetInstance().GetSmsSendReceiveRunnerBySlotId(slotId_);
60     if (gsmSmsSendRunner_ == nullptr) {
61         TELEPHONY_LOGE("failed to create GsmSenderEventRunner");
62         return;
63     }
64     gsmSmsSender_ = std::make_shared<GsmSmsSender>(
65         gsmSmsSendRunner_, slotId_, bind(&SmsSendManager::RetriedSmsDelivery, this, placeholders::_1));
66     if (gsmSmsSender_ == nullptr) {
67         TELEPHONY_LOGE("failed to create GsmSmsSender");
68         return;
69     }
70     gsmSmsSender_->Init();
71 
72     cdmaSmsSendRunner_ = RunnerPool::GetInstance().GetSmsSendReceiveRunnerBySlotId(slotId_);
73     if (cdmaSmsSendRunner_ == nullptr) {
74         TELEPHONY_LOGE("failed to create CdmaSenderEventRunner");
75         return;
76     }
77     cdmaSmsSender_ = std::make_shared<CdmaSmsSender>(
78         cdmaSmsSendRunner_, slotId_, bind(&SmsSendManager::RetriedSmsDelivery, this, placeholders::_1));
79     if (cdmaSmsSender_ == nullptr) {
80         TELEPHONY_LOGE("failed to create CdmaSmsSender");
81         return;
82     }
83     cdmaSmsSender_->Init();
84     InitNetworkHandle();
85 }
86 
InitNetworkHandle()87 void SmsSendManager::InitNetworkHandle()
88 {
89     networkRunner_ = RunnerPool::GetInstance().GetSmsCommonRunner();
90     if (networkRunner_ == nullptr) {
91         TELEPHONY_LOGE("failed to create networkRunner");
92         return;
93     }
94     networkManager_ = std::make_shared<SmsNetworkPolicyManager>(networkRunner_, slotId_);
95     if (networkManager_ == nullptr) {
96         TELEPHONY_LOGE("failed to create networkManager");
97         return;
98     }
99     networkManager_->Init();
100     TELEPHONY_LOGI("Init SmsSendManager successfully.");
101     if (auto ret = networkManager_->NetworkRegister(
102         std::bind(&SmsSender::SetNetworkState, gsmSmsSender_, std::placeholders::_1, std::placeholders::_2));
103         ret.has_value()) {
104         gsmSmsSender_->SetNetworkId(ret);
105     } else {
106         TELEPHONY_LOGE("gsm failed to register networkManager");
107     }
108     if (auto ret = networkManager_->NetworkRegister(
109         std::bind(&SmsSender::SetNetworkState, cdmaSmsSender_, std::placeholders::_1, std::placeholders::_2));
110         ret.has_value()) {
111         cdmaSmsSender_->SetNetworkId(ret);
112     } else {
113         TELEPHONY_LOGE("cdma failed to register networkManager");
114     }
115 }
116 
TextBasedSmsDelivery(const string & desAddr,const string & scAddr,const string & text,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)117 void SmsSendManager::TextBasedSmsDelivery(const string &desAddr, const string &scAddr, const string &text,
118     const sptr<ISendShortMessageCallback> &sendCallback,
119     const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
120 {
121     if (desAddr.empty() || text.empty()) {
122         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
123         TELEPHONY_LOGE("TextBasedSmsDelivery::param Set Error.");
124         return;
125     }
126     if (networkManager_ == nullptr) {
127         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
128         TELEPHONY_LOGE("TextBasedSmsDelivery::networkManager nullptr error.");
129         return;
130     }
131     if (gsmSmsSender_ == nullptr || cdmaSmsSender_ == nullptr) {
132         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
133         TELEPHONY_LOGE("gsmSmsSender or cdmaSmsSender nullptr error.");
134         return;
135     }
136 
137     NetWorkType netWorkType = networkManager_->GetNetWorkType();
138     TELEPHONY_LOGI("netWorkType = %{public}d.", netWorkType);
139     if (netWorkType == NetWorkType::NET_TYPE_GSM) {
140         gsmSmsSender_->RegisterImsHandler();
141         gsmSmsSender_->TextBasedSmsDelivery(desAddr, scAddr, text, sendCallback, deliveryCallback);
142     } else if (netWorkType == NetWorkType::NET_TYPE_CDMA) {
143         cdmaSmsSender_->RegisterImsHandler();
144         cdmaSmsSender_->TextBasedSmsDelivery(desAddr, scAddr, text, sendCallback, deliveryCallback);
145     } else {
146         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_SERVICE_UNAVAILABLE);
147         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
148             SmsMmsErrorCode::SMS_ERROR_UNKNOWN_NETWORK_TYPE, "text sms network unknown send error");
149         TELEPHONY_LOGI("network unknown send error.");
150     }
151 }
152 
DataBasedSmsDelivery(const string & desAddr,const string & scAddr,uint16_t port,const uint8_t * data,uint16_t dataLen,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)153 void SmsSendManager::DataBasedSmsDelivery(const string &desAddr, const string &scAddr, uint16_t port,
154     const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback,
155     const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
156 {
157     if (desAddr.empty() || data == nullptr) {
158         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
159         TELEPHONY_LOGE("DataBasedSmsDelivery::param Set Error.");
160         return;
161     }
162     if (networkManager_ == nullptr) {
163         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
164         TELEPHONY_LOGE("DataBasedSmsDelivery::networkManager nullptr error.");
165         return;
166     }
167     if (gsmSmsSender_ == nullptr || cdmaSmsSender_ == nullptr) {
168         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
169         TELEPHONY_LOGE("gsmSmsSender or cdmaSmsSender nullptr error.");
170         return;
171     }
172 
173     NetWorkType netWorkType = networkManager_->GetNetWorkType();
174     if (netWorkType == NetWorkType::NET_TYPE_GSM) {
175         gsmSmsSender_->RegisterImsHandler();
176         gsmSmsSender_->DataBasedSmsDelivery(desAddr, scAddr, port, data, dataLen, sendCallback, deliveryCallback);
177     } else if (netWorkType == NetWorkType::NET_TYPE_CDMA) {
178         cdmaSmsSender_->RegisterImsHandler();
179         cdmaSmsSender_->DataBasedSmsDelivery(desAddr, scAddr, port, data, dataLen, sendCallback, deliveryCallback);
180     } else {
181         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_SERVICE_UNAVAILABLE);
182         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
183             SmsMmsErrorCode::SMS_ERROR_UNKNOWN_NETWORK_TYPE, "data sms network unknown send error");
184         TELEPHONY_LOGI("network unknown send error.");
185     }
186 }
187 
RetriedSmsDelivery(const shared_ptr<SmsSendIndexer> smsIndexer)188 void SmsSendManager::RetriedSmsDelivery(const shared_ptr<SmsSendIndexer> smsIndexer)
189 {
190     if (smsIndexer == nullptr) {
191         TELEPHONY_LOGI("RetriedSmsDelivery::smsIndexer is nullptr error.");
192         return;
193     }
194     if (gsmSmsSender_ == nullptr || cdmaSmsSender_ == nullptr || networkManager_ == nullptr) {
195         TELEPHONY_LOGE("Sender or network nullptr error.");
196         return;
197     }
198 
199     NetWorkType oldNetWorkType = smsIndexer->GetNetWorkType();
200     NetWorkType newNetWorkType = networkManager_->GetNetWorkType();
201     if (oldNetWorkType != newNetWorkType) {
202         smsIndexer->SetNetWorkType(newNetWorkType);
203         shared_ptr<SmsSendIndexer> indexer = smsIndexer;
204         switch (newNetWorkType) {
205             case NetWorkType::NET_TYPE_CDMA:
206                 if (smsIndexer->GetIsText()) {
207                     cdmaSmsSender_->ResendTextDelivery(indexer);
208                 } else {
209                     cdmaSmsSender_->ResendDataDelivery(indexer);
210                 }
211                 break;
212             case NetWorkType::NET_TYPE_GSM:
213                 if (smsIndexer->GetIsText()) {
214                     gsmSmsSender_->ResendTextDelivery(indexer);
215                 } else {
216                     gsmSmsSender_->ResendDataDelivery(indexer);
217                 }
218                 break;
219             default:
220                 SmsSender::SendResultCallBack(
221                     smsIndexer, ISendShortMessageCallback::SEND_SMS_FAILURE_SERVICE_UNAVAILABLE);
222                 TELEPHONY_LOGI("network unknown send error.");
223                 break;
224         }
225         return;
226     }
227 
228     if (newNetWorkType == NetWorkType::NET_TYPE_GSM) {
229         gsmSmsSender_->SendSmsToRil(smsIndexer);
230     } else if (newNetWorkType == NetWorkType::NET_TYPE_CDMA) {
231         cdmaSmsSender_->SendSmsToRil(smsIndexer);
232     } else {
233         SmsSender::SendResultCallBack(smsIndexer, ISendShortMessageCallback::SEND_SMS_FAILURE_SERVICE_UNAVAILABLE);
234         TELEPHONY_LOGI("Network Unknown.");
235     }
236 }
237 
SplitMessage(const std::string & message,std::vector<std::u16string> & splitMessage)238 int32_t SmsSendManager::SplitMessage(const std::string &message, std::vector<std::u16string> &splitMessage)
239 {
240     if (networkManager_ == nullptr) {
241         TELEPHONY_LOGE("SmsSendManager::SplitMessage networkManager nullptr Error.");
242         return TELEPHONY_ERR_LOCAL_PTR_NULL;
243     }
244 
245     DataCodingScheme codingType;
246     std::vector<struct SplitInfo> cellsInfos;
247     NetWorkType netWorkType = networkManager_->GetNetWorkType();
248     switch (netWorkType) {
249         case NetWorkType::NET_TYPE_CDMA: {
250             GsmSmsMessage gsmSmsMessage;
251             gsmSmsMessage.SplitMessage(cellsInfos, message, false, codingType, false);
252             break;
253         }
254         case NetWorkType::NET_TYPE_GSM: {
255             CdmaSmsMessage cdmaSmsMessage;
256             cdmaSmsMessage.SplitMessage(cellsInfos, message, false, codingType, false);
257             break;
258         }
259         default:
260             TELEPHONY_LOGE("netWorkType is NET_TYPE_UNKNOWN");
261             break;
262     }
263 
264     for (auto &item : cellsInfos) {
265         splitMessage.emplace_back(StringUtils::ToUtf16(item.text));
266     }
267     return TELEPHONY_ERR_SUCCESS;
268 }
269 
GetSmsSegmentsInfo(const std::string & message,bool force7BitCode,LengthInfo & lenInfo)270 int32_t SmsSendManager::GetSmsSegmentsInfo(const std::string &message, bool force7BitCode, LengthInfo &lenInfo)
271 {
272     if (networkManager_ == nullptr) {
273         TELEPHONY_LOGE("GetSmsSegmentsInfo networkManager_ Nullptr Error.");
274         return TELEPHONY_ERR_LOCAL_PTR_NULL;
275     }
276     NetWorkType netWorkType = networkManager_->GetNetWorkType();
277     switch (netWorkType) {
278         case NetWorkType::NET_TYPE_CDMA: {
279             CdmaSmsMessage cdmaSmsMessage;
280             return cdmaSmsMessage.GetSmsSegmentsInfo(message, force7BitCode, lenInfo);
281         }
282         case NetWorkType::NET_TYPE_GSM: {
283             GsmSmsMessage gsmSmsMessage;
284             return gsmSmsMessage.GetSmsSegmentsInfo(message, force7BitCode, lenInfo);
285         }
286         default:
287             TELEPHONY_LOGE("netWorkType is NET_TYPE_UNKNOWN");
288             break;
289     }
290     return TELEPHONY_ERR_UNKNOWN_NETWORK_TYPE;
291 }
292 
GetCdmaSmsSender() const293 std::shared_ptr<SmsSender> SmsSendManager::GetCdmaSmsSender() const
294 {
295     return cdmaSmsSender_;
296 }
297 
IsImsSmsSupported(int32_t slotId,bool & isSupported)298 int32_t SmsSendManager::IsImsSmsSupported(int32_t slotId, bool &isSupported)
299 {
300     if (networkManager_ == nullptr) {
301         TELEPHONY_LOGE("networkManager is nullptr error.");
302         return TELEPHONY_ERR_LOCAL_PTR_NULL;
303     }
304 
305     if (gsmSmsSender_ == nullptr || cdmaSmsSender_ == nullptr || networkManager_ == nullptr) {
306         TELEPHONY_LOGE("Sender or network nullptr error.");
307         return TELEPHONY_ERR_LOCAL_PTR_NULL;
308     }
309 
310     NetWorkType newNetWorkType = networkManager_->GetNetWorkType();
311     switch (newNetWorkType) {
312         case NetWorkType::NET_TYPE_GSM:
313             return gsmSmsSender_->IsImsSmsSupported(slotId, isSupported);
314         case NetWorkType::NET_TYPE_CDMA:
315             return cdmaSmsSender_->IsImsSmsSupported(slotId, isSupported);
316         default:
317             TELEPHONY_LOGE("network unknown send error.");
318             return TELEPHONY_ERR_UNKNOWN_NETWORK_TYPE;
319     }
320 }
321 
SetImsSmsConfig(int32_t slotId,int32_t enable)322 bool SmsSendManager::SetImsSmsConfig(int32_t slotId, int32_t enable)
323 {
324     bool result = false;
325     if (gsmSmsSender_ == nullptr || cdmaSmsSender_ == nullptr || networkManager_ == nullptr) {
326         TELEPHONY_LOGE("Sender or network nullptr error.");
327         return result;
328     }
329     NetWorkType newNetWorkType = networkManager_->GetNetWorkType();
330 
331     switch (newNetWorkType) {
332         case NetWorkType::NET_TYPE_GSM:
333             result = gsmSmsSender_->SetImsSmsConfig(slotId, enable);
334             break;
335         case NetWorkType::NET_TYPE_CDMA:
336             result = cdmaSmsSender_->SetImsSmsConfig(slotId, enable);
337             break;
338         default:
339             TELEPHONY_LOGE("network unknown send error.");
340             break;
341     }
342     return result;
343 }
344 
GetImsShortMessageFormat(std::u16string & format)345 int32_t SmsSendManager::GetImsShortMessageFormat(std::u16string &format)
346 {
347     format = StringUtils::ToUtf16(NET_TYPE_UNKNOWN_STRING);
348     if (networkManager_ == nullptr) {
349         TELEPHONY_LOGE("networkManager is nullptr error.");
350         return TELEPHONY_ERR_LOCAL_PTR_NULL;
351     }
352     switch (networkManager_->GetNetWorkType()) {
353         case NetWorkType::NET_TYPE_GSM:
354             format = StringUtils::ToUtf16(NET_TYPE_GSM_STRING);
355             break;
356         case NetWorkType::NET_TYPE_CDMA:
357             format = StringUtils::ToUtf16(NET_TYPE_CDMA_STRING);
358             break;
359         default:
360             break;
361     }
362     return TELEPHONY_ERR_SUCCESS;
363 }
364 } // namespace Telephony
365 } // namespace OHOS
366