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_interface_manager.h"
17
18 #include "runner_pool.h"
19 #include "sms_hisysevent.h"
20 #include "sms_misc_manager.h"
21 #include "string_utils.h"
22 #include "telephony_errors.h"
23 #include "telephony_log_wrapper.h"
24
25 namespace OHOS {
26 namespace Telephony {
27 using namespace std;
SmsInterfaceManager(int32_t slotId)28 SmsInterfaceManager::SmsInterfaceManager(int32_t slotId) : slotId_(slotId) {}
29
~SmsInterfaceManager()30 SmsInterfaceManager::~SmsInterfaceManager() {}
31
InitInterfaceManager()32 void SmsInterfaceManager::InitInterfaceManager()
33 {
34 smsSendManager_ = make_unique<SmsSendManager>(slotId_);
35 if (smsSendManager_ == nullptr) {
36 TELEPHONY_LOGE("failed to create SmsSendManager");
37 return;
38 }
39 smsSendManager_->Init();
40
41 smsReceiveManager_ = make_unique<SmsReceiveManager>(slotId_);
42 if (smsReceiveManager_ == nullptr) {
43 TELEPHONY_LOGE("failed to create SmsReceiveManager");
44 return;
45 }
46 smsReceiveManager_->Init();
47 auto smsMiscRunner = RunnerPool::GetInstance().GetSmsCommonRunner();
48 if (smsMiscRunner == nullptr) {
49 TELEPHONY_LOGE("failed to create SmsMiscRunner");
50 return;
51 }
52 smsReceiveManager_->SetCdmaSender(smsSendManager_->GetCdmaSmsSender());
53 smsMiscManager_ = make_shared<SmsMiscManager>(smsMiscRunner, slotId_);
54
55 mmsSendManager_ = make_unique<MmsSendManager>(smsMiscRunner, slotId_);
56 if (mmsSendManager_ == nullptr) {
57 TELEPHONY_LOGE("failed to create MmsSendManager");
58 return;
59 }
60 mmsSendManager_->Init();
61
62 mmsReceiverManager_ = make_unique<MmsReceiveManager>(smsMiscRunner, slotId_);
63 if (mmsReceiverManager_ == nullptr) {
64 TELEPHONY_LOGE("failed to create MmsReceiveManager");
65 return;
66 }
67 mmsReceiverManager_->Init();
68
69 TELEPHONY_LOGI("SmsInterfaceManager::InitInterfaceManager success, %{public}d", slotId_);
70 }
71
TextBasedSmsDelivery(const string & desAddr,const string & scAddr,const string & text,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)72 int32_t SmsInterfaceManager::TextBasedSmsDelivery(const string &desAddr, const string &scAddr, const string &text,
73 const sptr<ISendShortMessageCallback> &sendCallback, const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
74 {
75 if (desAddr.empty() || text.empty()) {
76 SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
77 SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
78 SmsMmsErrorCode::SMS_ERROR_EMPTY_INPUT_PARAMETER, "text sms arges is empty");
79 TELEPHONY_LOGE("TextBasedSmsDelivery failed to send.");
80 return TELEPHONY_ERR_ARGUMENT_INVALID;
81 }
82 if (smsSendManager_ == nullptr) {
83 TELEPHONY_LOGE("TextBasedSmsDelivery failed to send.");
84 SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
85 SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
86 SmsMmsErrorCode::SMS_ERROR_NULL_POINTER, "text sms smsSendManager_ is nullptr");
87 return TELEPHONY_ERR_LOCAL_PTR_NULL;
88 }
89 smsSendManager_->TextBasedSmsDelivery(desAddr, scAddr, text, sendCallback, deliveryCallback);
90 return TELEPHONY_ERR_SUCCESS;
91 }
92
DataBasedSmsDelivery(const string & desAddr,const string & scAddr,const uint16_t port,const uint8_t * data,uint16_t dataLen,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)93 int32_t SmsInterfaceManager::DataBasedSmsDelivery(const string &desAddr, const string &scAddr, const uint16_t port,
94 const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback,
95 const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
96 {
97 if (desAddr.empty() || (data == nullptr)) {
98 SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
99 SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
100 SmsMmsErrorCode::SMS_ERROR_EMPTY_INPUT_PARAMETER, "data sms arges is empty");
101 TELEPHONY_LOGE("DataBasedSmsDelivery failed to send.");
102 return TELEPHONY_ERR_ARGUMENT_INVALID;
103 }
104 if (smsSendManager_ == nullptr) {
105 TELEPHONY_LOGE("TextBasedSmsDelivery failed to send.");
106 SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
107 SmsHiSysEvent::WriteSmsSendFaultEvent(slotId_, SmsMmsMessageType::SMS_SHORT_MESSAGE,
108 SmsMmsErrorCode::SMS_ERROR_NULL_POINTER, "text sms smsSendManager_ is nullptr");
109 return TELEPHONY_ERR_LOCAL_PTR_NULL;
110 }
111 smsSendManager_->DataBasedSmsDelivery(desAddr, scAddr, port, data, dataLen, sendCallback, deliveryCallback);
112 return TELEPHONY_ERR_SUCCESS;
113 }
114
AddSimMessage(const std::string & smsc,const std::string & pdu,ISmsServiceInterface::SimMessageStatus status)115 int32_t SmsInterfaceManager::AddSimMessage(
116 const std::string &smsc, const std::string &pdu, ISmsServiceInterface::SimMessageStatus status)
117 {
118 if (smsMiscManager_ == nullptr) {
119 TELEPHONY_LOGE("smsMiscManager nullptr error.");
120 return TELEPHONY_ERR_LOCAL_PTR_NULL;
121 }
122 return smsMiscManager_->AddSimMessage(smsc, pdu, status);
123 }
124
DelSimMessage(uint32_t msgIndex)125 int32_t SmsInterfaceManager::DelSimMessage(uint32_t msgIndex)
126 {
127 if (smsMiscManager_ == nullptr) {
128 TELEPHONY_LOGE("smsMiscManager nullptr error.");
129 return TELEPHONY_ERR_LOCAL_PTR_NULL;
130 }
131 return smsMiscManager_->DelSimMessage(msgIndex);
132 }
133
UpdateSimMessage(uint32_t msgIndex,ISmsServiceInterface::SimMessageStatus newStatus,const std::string & pdu,const std::string & smsc)134 int32_t SmsInterfaceManager::UpdateSimMessage(uint32_t msgIndex, ISmsServiceInterface::SimMessageStatus newStatus,
135 const std::string &pdu, const std::string &smsc)
136 {
137 if (smsMiscManager_ == nullptr) {
138 TELEPHONY_LOGE("smsMiscManager nullptr error.");
139 return TELEPHONY_ERR_LOCAL_PTR_NULL;
140 }
141 return smsMiscManager_->UpdateSimMessage(msgIndex, newStatus, pdu, smsc);
142 }
143
GetAllSimMessages(std::vector<ShortMessage> & message)144 int32_t SmsInterfaceManager::GetAllSimMessages(std::vector<ShortMessage> &message)
145 {
146 if (smsMiscManager_ == nullptr) {
147 TELEPHONY_LOGE("smsMiscManager nullptr error.");
148 return TELEPHONY_ERR_LOCAL_PTR_NULL;
149 }
150 return smsMiscManager_->GetAllSimMessages(message);
151 }
152
SetSmscAddr(const std::string & scAddr)153 int32_t SmsInterfaceManager::SetSmscAddr(const std::string &scAddr)
154 {
155 if (smsMiscManager_ == nullptr) {
156 TELEPHONY_LOGE("smsMiscManager nullptr error.");
157 return TELEPHONY_ERR_LOCAL_PTR_NULL;
158 }
159 return smsMiscManager_->SetSmscAddr(scAddr);
160 }
161
GetSmscAddr(std::u16string & smscAddress)162 int32_t SmsInterfaceManager::GetSmscAddr(std::u16string &smscAddress)
163 {
164 if (smsMiscManager_ == nullptr) {
165 TELEPHONY_LOGE("smsMiscManager nullptr error.");
166 return TELEPHONY_ERR_LOCAL_PTR_NULL;
167 }
168 return smsMiscManager_->GetSmscAddr(smscAddress);
169 }
170
SetCBConfig(bool enable,uint32_t fromMsgId,uint32_t toMsgId,uint8_t netType)171 int32_t SmsInterfaceManager::SetCBConfig(bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType)
172 {
173 if (smsMiscManager_ == nullptr) {
174 TELEPHONY_LOGE("smsMiscManager nullptr error.");
175 return TELEPHONY_ERR_LOCAL_PTR_NULL;
176 }
177 return smsMiscManager_->SetCBConfig(enable, fromMsgId, toMsgId, netType);
178 }
179
SetImsSmsConfig(int32_t slotId,int32_t enable)180 bool SmsInterfaceManager::SetImsSmsConfig(int32_t slotId, int32_t enable)
181 {
182 if (smsMiscManager_ == nullptr) {
183 TELEPHONY_LOGE("smsMiscManager nullptr error.");
184 return false;
185 }
186 return smsSendManager_->SetImsSmsConfig(slotId, enable);
187 }
188
SetDefaultSmsSlotId(int32_t slotId)189 int32_t SmsInterfaceManager::SetDefaultSmsSlotId(int32_t slotId)
190 {
191 if (smsMiscManager_ == nullptr) {
192 TELEPHONY_LOGE("smsMiscManager nullptr error.");
193 return TELEPHONY_ERR_LOCAL_PTR_NULL;
194 }
195 return smsMiscManager_->SetDefaultSmsSlotId(slotId);
196 }
197
GetDefaultSmsSlotId()198 int32_t SmsInterfaceManager::GetDefaultSmsSlotId()
199 {
200 if (smsMiscManager_ == nullptr) {
201 TELEPHONY_LOGE("smsMiscManager nullptr error.");
202 return TELEPHONY_ERROR;
203 }
204 return smsMiscManager_->GetDefaultSmsSlotId();
205 }
206
GetDefaultSmsSimId(int32_t & simId)207 int32_t SmsInterfaceManager::GetDefaultSmsSimId(int32_t &simId)
208 {
209 if (smsMiscManager_ == nullptr) {
210 TELEPHONY_LOGE("smsMiscManager nullptr error.");
211 return TELEPHONY_ERR_LOCAL_PTR_NULL;
212 }
213 return smsMiscManager_->GetDefaultSmsSimId(simId);
214 }
215
SplitMessage(const std::string & message,std::vector<std::u16string> & splitMessage)216 int32_t SmsInterfaceManager::SplitMessage(const std::string &message, std::vector<std::u16string> &splitMessage)
217 {
218 if (smsSendManager_ == nullptr) {
219 TELEPHONY_LOGE("smsSendManager nullptr error.");
220 return TELEPHONY_ERR_LOCAL_PTR_NULL;
221 }
222 return smsSendManager_->SplitMessage(message, splitMessage);
223 }
224
GetSmsSegmentsInfo(const std::string & message,bool force7BitCode,LengthInfo & outInfo)225 int32_t SmsInterfaceManager::GetSmsSegmentsInfo(const std::string &message, bool force7BitCode, LengthInfo &outInfo)
226 {
227 if (smsSendManager_ == nullptr) {
228 TELEPHONY_LOGE("smsSendManager nullptr error.");
229 return TELEPHONY_ERR_LOCAL_PTR_NULL;
230 }
231 return smsSendManager_->GetSmsSegmentsInfo(message, force7BitCode, outInfo);
232 }
233
IsImsSmsSupported(int32_t slotId,bool & isSupported)234 int32_t SmsInterfaceManager::IsImsSmsSupported(int32_t slotId, bool &isSupported)
235 {
236 if (smsSendManager_ == nullptr) {
237 TELEPHONY_LOGE("smsSendManager is nullptr error.");
238 return TELEPHONY_ERR_LOCAL_PTR_NULL;
239 }
240 return smsSendManager_->IsImsSmsSupported(slotId, isSupported);
241 }
242
GetImsShortMessageFormat(std::u16string & format)243 int32_t SmsInterfaceManager::GetImsShortMessageFormat(std::u16string &format)
244 {
245 if (smsSendManager_ == nullptr) {
246 TELEPHONY_LOGE("smsSendManager is nullptr error.");
247 return TELEPHONY_ERR_LOCAL_PTR_NULL;
248 }
249 return smsSendManager_->GetImsShortMessageFormat(format);
250 }
251
HasSmsCapability()252 bool SmsInterfaceManager::HasSmsCapability()
253 {
254 auto helperPtr = DelayedSingleton<SmsPersistHelper>::GetInstance();
255 if (helperPtr == nullptr) {
256 TELEPHONY_LOGE("Get SmsPersistHelper Singleton nullptr error.");
257 return true;
258 }
259 return helperPtr->QueryParamBoolean(SmsPersistHelper::SMS_CAPABLE_PARAM_KEY, true);
260 }
261
SendMms(const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)262 int32_t SmsInterfaceManager::SendMms(
263 const std::u16string &mmsc, const std::u16string &data, const std::u16string &ua, const std::u16string &uaprof)
264 {
265 if (mmsSendManager_ == nullptr) {
266 TELEPHONY_LOGE("mmsSendManager_ nullptr error");
267 return TELEPHONY_ERR_LOCAL_PTR_NULL;
268 }
269 return mmsSendManager_->SendMms(mmsc, data, ua, uaprof);
270 }
271
DownloadMms(const std::u16string & mmsc,const std::u16string & data,const std::u16string & ua,const std::u16string & uaprof)272 int32_t SmsInterfaceManager::DownloadMms(
273 const std::u16string &mmsc, const std::u16string &data, const std::u16string &ua, const std::u16string &uaprof)
274 {
275 if (mmsReceiverManager_ == nullptr) {
276 TELEPHONY_LOGE("mmsReceiverManager_ nullptr error");
277 return TELEPHONY_ERR_LOCAL_PTR_NULL;
278 }
279 return mmsReceiverManager_->DownloadMms(mmsc, data, ua, uaprof);
280 }
281 } // namespace Telephony
282 } // namespace OHOS
283