• 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_service.h"
17 
18 #include <regex>
19 
20 #include "cdma_sms_message.h"
21 #include "core_manager_inner.h"
22 #include "ims_sms_client.h"
23 #include "msg_text_convert.h"
24 #include "sms_dump_helper.h"
25 #include "sms_hisysevent.h"
26 #include "string_utils.h"
27 #include "telephony_log_wrapper.h"
28 #include "telephony_permission.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 using namespace std;
33 using namespace AppExecFwk;
34 using namespace HiviewDFX;
35 constexpr static uint32_t CONNECT_SERVICE_WAIT_TIME = 2000;
36 constexpr static size_t MIN_LEN = 1;
37 bool g_registerResult = SystemAbility::MakeAndRegisterAbility(DelayedSingleton<SmsService>::GetInstance().get());
38 
SmsService()39 SmsService::SmsService() : SystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID, true) {}
40 
~SmsService()41 SmsService::~SmsService() {}
42 
OnStart()43 void SmsService::OnStart()
44 {
45     TELEPHONY_LOGI("SmsService::OnStart start service Enter.");
46     bindTime_ =
47         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
48             .count();
49     if (state_ == ServiceRunningState::STATE_RUNNING) {
50         TELEPHONY_LOGE("msService has already started.");
51         return;
52     }
53     if (!Init()) {
54         TELEPHONY_LOGE("failed to init SmsService");
55         return;
56     }
57     state_ = ServiceRunningState::STATE_RUNNING;
58     endTime_ =
59         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
60             .count();
61     TELEPHONY_LOGI("SmsService::OnStart start service Exit.");
62 }
63 
Init()64 bool SmsService::Init()
65 {
66     DelayedSingleton<ImsSmsClient>::GetInstance()->Init();
67     if (!registerToService_) {
68         bool ret = Publish(DelayedSingleton<SmsService>::GetInstance().get());
69         if (!ret) {
70             TELEPHONY_LOGE("SmsService::Init Publish failed!");
71             return false;
72         }
73         registerToService_ = true;
74         WaitCoreServiceToInit();
75     }
76     DelayedSingleton<SmsStateHandler>::GetInstance()->Init();
77     return true;
78 }
79 
OnStop()80 void SmsService::OnStop()
81 {
82     state_ = ServiceRunningState::STATE_NOT_START;
83     registerToService_ = false;
84     DelayedSingleton<ImsSmsClient>::GetInstance()->UnInit();
85     DelayedSingleton<SmsStateHandler>::GetInstance()->UnInit();
86     TELEPHONY_LOGI("SmsService::OnStop stop service.");
87 }
88 
Dump(std::int32_t fd,const std::vector<std::u16string> & args)89 int32_t SmsService::Dump(std::int32_t fd, const std::vector<std::u16string> &args)
90 {
91     if (fd < 0) {
92         TELEPHONY_LOGE("dump fd invalid");
93         return TELEPHONY_ERR_FAIL;
94     }
95     std::vector<std::string> argsInStr;
96     for (const auto &arg : args) {
97         TELEPHONY_LOGI("Dump args: %{public}s", Str16ToStr8(arg).c_str());
98         argsInStr.emplace_back(Str16ToStr8(arg));
99     }
100     std::string result;
101     SmsDumpHelper dumpHelper;
102     if (dumpHelper.Dump(argsInStr, result)) {
103         TELEPHONY_LOGI("%{public}s", result.c_str());
104         std::int32_t ret = dprintf(fd, "%s", result.c_str());
105         if (ret < 0) {
106             TELEPHONY_LOGE("dprintf to dump fd failed");
107             return TELEPHONY_ERR_FAIL;
108         }
109         return TELEPHONY_SUCCESS;
110     }
111     TELEPHONY_LOGW("dumpHelper failed");
112     return TELEPHONY_ERR_FAIL;
113 }
114 
WaitCoreServiceToInit()115 void SmsService::WaitCoreServiceToInit()
116 {
117     std::thread connectTask([&]() {
118         while (true) {
119             if (CoreManagerInner::GetInstance().IsInitFinished()) {
120                 InitModule();
121                 TELEPHONY_LOGI("SmsService Connection successful");
122                 break;
123             }
124             std::this_thread::sleep_for(std::chrono::milliseconds(CONNECT_SERVICE_WAIT_TIME));
125         }
126     });
127     connectTask.detach();
128 }
129 
GetBindTime()130 std::string SmsService::GetBindTime()
131 {
132     return std::to_string(bindTime_);
133 }
134 
SendMessage(int32_t slotId,const u16string desAddr,const u16string scAddr,const u16string text,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)135 int32_t SmsService::SendMessage(int32_t slotId, const u16string desAddr, const u16string scAddr, const u16string text,
136     const sptr<ISendShortMessageCallback> &sendCallback, const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
137 {
138     if (NoPermissionOrParametersCheckFail(slotId, desAddr, sendCallback)) {
139         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
140             SmsMmsErrorCode::SMS_ERROR_PERMISSION_ERROR, Permission::SEND_MESSAGES);
141         return TELEPHONY_ERR_PERMISSION_ERR;
142     }
143     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
144     if (interfaceManager == nullptr) {
145         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
146         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
147             SmsMmsErrorCode::SMS_ERROR_NULL_POINTER, "text sms interfaceManager is nullptr");
148         TELEPHONY_LOGE("SmsService::SendMessage interfaceManager nullptr error.");
149         return TELEPHONY_ERR_LOCAL_PTR_NULL;
150     }
151     if (!ValidDestinationAddress(StringUtils::ToUtf8(desAddr))) {
152         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
153         TELEPHONY_LOGE("SmsService::SendMessage desAddr not conform to the regular specification");
154         return TELEPHONY_ERR_ARGUMENT_INVALID;
155     }
156     return interfaceManager->TextBasedSmsDelivery(StringUtils::ToUtf8(desAddr), StringUtils::ToUtf8(scAddr),
157         StringUtils::ToUtf8(text), sendCallback, deliveryCallback);
158 }
159 
SendMessage(int32_t slotId,const u16string desAddr,const u16string scAddr,uint16_t port,const uint8_t * data,uint16_t dataLen,const sptr<ISendShortMessageCallback> & sendCallback,const sptr<IDeliveryShortMessageCallback> & deliveryCallback)160 int32_t SmsService::SendMessage(int32_t slotId, const u16string desAddr, const u16string scAddr, uint16_t port,
161     const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback,
162     const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
163 {
164     if (NoPermissionOrParametersCheckFail(slotId, desAddr, sendCallback)) {
165         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
166             SmsMmsErrorCode::SMS_ERROR_PERMISSION_ERROR, Permission::SEND_MESSAGES);
167         return TELEPHONY_ERR_PERMISSION_ERR;
168     }
169     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
170     if (interfaceManager == nullptr) {
171         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
172         TELEPHONY_LOGE("SmsService::SendMessage interfaceManager nullptr error.");
173         SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
174             SmsMmsErrorCode::SMS_ERROR_NULL_POINTER, "data sms interfaceManager is nullptr");
175         return TELEPHONY_ERR_LOCAL_PTR_NULL;
176     }
177     if (!ValidDestinationAddress(StringUtils::ToUtf8(desAddr))) {
178         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
179         TELEPHONY_LOGE("SmsService::SendMessage desAddr not conform to the regular specification");
180         return TELEPHONY_ERR_ARGUMENT_INVALID;
181     }
182     return interfaceManager->DataBasedSmsDelivery(
183         StringUtils::ToUtf8(desAddr), StringUtils::ToUtf8(scAddr), port, data, dataLen, sendCallback, deliveryCallback);
184 }
185 
NoPermissionOrParametersCheckFail(int32_t slotId,const u16string desAddr,const sptr<ISendShortMessageCallback> & sendCallback)186 bool SmsService::NoPermissionOrParametersCheckFail(
187     int32_t slotId, const u16string desAddr, const sptr<ISendShortMessageCallback> &sendCallback)
188 {
189     if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
190         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
191         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
192         return true;
193     }
194 
195     if (desAddr.empty()) {
196         SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
197         TELEPHONY_LOGE("SmsService::SendMessage desAddr invalid.");
198         return true;
199     }
200 
201     return false;
202 }
203 
IsImsSmsSupported(int32_t slotId,bool & isSupported)204 int32_t SmsService::IsImsSmsSupported(int32_t slotId, bool &isSupported)
205 {
206     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
207     if (interfaceManager == nullptr) {
208         TELEPHONY_LOGE("interfaceManager is nullptr.");
209         return TELEPHONY_ERR_SLOTID_INVALID;
210     }
211     return interfaceManager->IsImsSmsSupported(slotId, isSupported);
212 }
213 
GetImsShortMessageFormat(std::u16string & format)214 int32_t SmsService::GetImsShortMessageFormat(std::u16string &format)
215 {
216     int32_t slotId = GetDefaultSmsSlotId();
217     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
218     if (interfaceManager == nullptr) {
219         TELEPHONY_LOGE("interfaceManager is nullptr error.");
220         return TELEPHONY_ERR_SLOTID_INVALID;
221     }
222     return interfaceManager->GetImsShortMessageFormat(format);
223 }
224 
HasSmsCapability()225 bool SmsService::HasSmsCapability()
226 {
227     int32_t slotId = GetDefaultSmsSlotId();
228     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
229     if (interfaceManager == nullptr) {
230         TELEPHONY_LOGE("Get SmsInterfaceManager is nullptr error.");
231         return true;
232     }
233     return interfaceManager->HasSmsCapability();
234 }
235 
SetSmscAddr(int32_t slotId,const std::u16string & scAddr)236 int32_t SmsService::SetSmscAddr(int32_t slotId, const std::u16string &scAddr)
237 {
238     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
239         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Set State Permisson.");
240         return TELEPHONY_ERR_PERMISSION_ERR;
241     }
242 
243     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
244     if (interfaceManager == nullptr) {
245         TELEPHONY_LOGE("interfaceManager is nullptr error.");
246         return TELEPHONY_ERR_SLOTID_INVALID;
247     }
248     string sca = StringUtils::ToUtf8(scAddr);
249     TrimSmscAddr(sca);
250     if (sca.empty() || sca.length() == 0) {
251         TELEPHONY_LOGE("sca is empty");
252         return TELEPHONY_ERR_ARGUMENT_NULL;
253     }
254     return interfaceManager->SetSmscAddr(sca);
255 }
256 
TrimSmscAddr(std::string & sca)257 void SmsService::TrimSmscAddr(std::string &sca)
258 {
259     if (sca.length() < MIN_LEN) {
260         return;
261     }
262     while (sca.length() >= MIN_LEN && sca[0] == ' ') {
263         sca.erase(0, 1);
264     }
265     while (sca.length() > MIN_LEN && sca[sca.length() - 1] == ' ') {
266         sca.erase(sca.length() - 1, 1);
267     }
268 }
269 
GetSmscAddr(int32_t slotId,std::u16string & smscAddress)270 int32_t SmsService::GetSmscAddr(int32_t slotId, std::u16string &smscAddress)
271 {
272     if (!TelephonyPermission::CheckPermission(Permission::GET_TELEPHONY_STATE)) {
273         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Get State Permisson.");
274         return TELEPHONY_ERR_PERMISSION_ERR;
275     }
276 
277     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
278     if (interfaceManager == nullptr) {
279         TELEPHONY_LOGE("SmsService::GetSmscAddr interfaceManager nullptr error");
280         return TELEPHONY_ERR_SLOTID_INVALID;
281     }
282     return interfaceManager->GetSmscAddr(smscAddress);
283 }
284 
AddSimMessage(int32_t slotId,const std::u16string & smsc,const std::u16string & pdu,SimMessageStatus status)285 int32_t SmsService::AddSimMessage(
286     int32_t slotId, const std::u16string &smsc, const std::u16string &pdu, SimMessageStatus status)
287 {
288     if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
289         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
290         return TELEPHONY_ERR_PERMISSION_ERR;
291     }
292     if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
293         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
294         return TELEPHONY_ERR_PERMISSION_ERR;
295     }
296 
297     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
298     if (interfaceManager == nullptr) {
299         TELEPHONY_LOGE("SmsService::AddSimMessage interfaceManager nullptr error");
300         return TELEPHONY_ERR_SLOTID_INVALID;
301     }
302     std::string smscData = StringUtils::ToUtf8(smsc);
303     std::string pduData = StringUtils::ToUtf8(pdu);
304     return interfaceManager->AddSimMessage(smscData, pduData, status);
305 }
306 
DelSimMessage(int32_t slotId,uint32_t msgIndex)307 int32_t SmsService::DelSimMessage(int32_t slotId, uint32_t msgIndex)
308 {
309     if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
310         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
311         return TELEPHONY_ERR_PERMISSION_ERR;
312     }
313     if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
314         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
315         return TELEPHONY_ERR_PERMISSION_ERR;
316     }
317 
318     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
319     if (interfaceManager == nullptr) {
320         TELEPHONY_LOGE("SmsService::DelSimMessage interfaceManager nullptr error.");
321         return TELEPHONY_ERR_SLOTID_INVALID;
322     }
323     return interfaceManager->DelSimMessage(msgIndex);
324 }
325 
UpdateSimMessage(int32_t slotId,uint32_t msgIndex,SimMessageStatus newStatus,const std::u16string & pdu,const std::u16string & smsc)326 int32_t SmsService::UpdateSimMessage(int32_t slotId, uint32_t msgIndex, SimMessageStatus newStatus,
327     const std::u16string &pdu, const std::u16string &smsc)
328 {
329     if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
330         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
331         return TELEPHONY_ERR_PERMISSION_ERR;
332     }
333     if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
334         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
335         return TELEPHONY_ERR_PERMISSION_ERR;
336     }
337 
338     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
339     if (interfaceManager == nullptr) {
340         TELEPHONY_LOGE("SmsService::UpdateSimMessage interfaceManager nullptr error.");
341         return TELEPHONY_ERR_SLOTID_INVALID;
342     }
343     std::string pduData = StringUtils::ToUtf8(pdu);
344     std::string smscData = StringUtils::ToUtf8(smsc);
345     return interfaceManager->UpdateSimMessage(msgIndex, newStatus, pduData, smscData);
346 }
347 
GetAllSimMessages(int32_t slotId,std::vector<ShortMessage> & message)348 int32_t SmsService::GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message)
349 {
350     if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
351         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
352         return TELEPHONY_ERR_PERMISSION_ERR;
353     }
354 
355     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
356     if (interfaceManager == nullptr) {
357         TELEPHONY_LOGE("SmsService::GetAllSimMessages interfaceManager nullptr error.");
358         return TELEPHONY_ERR_SLOTID_INVALID;
359     }
360     return interfaceManager->GetAllSimMessages(message);
361 }
362 
SetCBConfig(int32_t slotId,bool enable,uint32_t fromMsgId,uint32_t toMsgId,uint8_t netType)363 int32_t SmsService::SetCBConfig(int32_t slotId, bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType)
364 {
365     if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
366         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
367         return TELEPHONY_ERR_PERMISSION_ERR;
368     }
369 
370     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
371     if (interfaceManager == nullptr) {
372         TELEPHONY_LOGE("SmsService::SetCBConfig interfaceManager nullptr error.");
373         return TELEPHONY_ERR_SLOTID_INVALID;
374     }
375     return interfaceManager->SetCBConfig(enable, fromMsgId, toMsgId, netType);
376 }
377 
SetImsSmsConfig(int32_t slotId,int32_t enable)378 bool SmsService::SetImsSmsConfig(int32_t slotId, int32_t enable)
379 {
380     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
381         TELEPHONY_LOGE("Check permission failed.");
382         return false;
383     }
384     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
385     if (interfaceManager == nullptr) {
386         TELEPHONY_LOGE("SmsService::SetImsSmsConfig interfaceManager nullptr error.");
387         return false;
388     }
389     return interfaceManager->SetImsSmsConfig(slotId, enable);
390 }
391 
SetDefaultSmsSlotId(int32_t slotId)392 int32_t SmsService::SetDefaultSmsSlotId(int32_t slotId)
393 {
394     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
395         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Set State Permisson.");
396         return TELEPHONY_ERR_PERMISSION_ERR;
397     }
398 
399     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager();
400     if (interfaceManager == nullptr) {
401         TELEPHONY_LOGE("SmsService::SetDefaultSmsSlotId interfaceManager nullptr error.");
402         return TELEPHONY_ERR_LOCAL_PTR_NULL;
403     }
404     return interfaceManager->SetDefaultSmsSlotId(slotId);
405 }
406 
GetDefaultSmsSlotId()407 int32_t SmsService::GetDefaultSmsSlotId()
408 {
409     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager();
410     if (interfaceManager == nullptr) {
411         TELEPHONY_LOGE("SmsService::GetDefaultSmsSlotId interfaceManager nullptr error.");
412         return TELEPHONY_ERROR;
413     }
414     return interfaceManager->GetDefaultSmsSlotId();
415 }
416 
SplitMessage(const std::u16string & message,std::vector<std::u16string> & splitMessage)417 int32_t SmsService::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
418 {
419     if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
420         TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Get State Permisson.");
421         return TELEPHONY_ERR_PERMISSION_ERR;
422     }
423 
424     if (message.empty()) {
425         return TELEPHONY_ERR_ARGUMENT_INVALID;
426     }
427     int32_t slotId = GetDefaultSmsSlotId();
428     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
429     if (interfaceManager == nullptr) {
430         TELEPHONY_LOGE("SmsService::SplitMessage interfaceManager nullptr error.");
431         return TELEPHONY_ERR_SLOTID_INVALID;
432     }
433     std::string messageData = StringUtils::ToUtf8(message);
434     return interfaceManager->SplitMessage(messageData, splitMessage);
435 }
436 
GetSmsSegmentsInfo(int32_t slotId,const std::u16string & message,bool force7BitCode,ISmsServiceInterface::SmsSegmentsInfo & info)437 int32_t SmsService::GetSmsSegmentsInfo(
438     int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &info)
439 {
440     if (message.empty()) {
441         return TELEPHONY_ERR_ARGUMENT_NULL;
442     }
443     std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
444     if (interfaceManager == nullptr) {
445         TELEPHONY_LOGE("GetSmsSegmentsInfo interfaceManager is nullptr error.");
446         return TELEPHONY_ERR_SLOTID_INVALID;
447     }
448     std::string messageData = StringUtils::ToUtf8(message);
449     LengthInfo result;
450     int32_t ret = interfaceManager->GetSmsSegmentsInfo(messageData, force7BitCode, result);
451     if (ret != TELEPHONY_ERR_SUCCESS) {
452         return ret;
453     }
454     info.msgSegCount = static_cast<int32_t>(result.msgSegCount);
455     info.msgEncodingCount = static_cast<int32_t>(result.msgEncodeCount);
456     info.msgRemainCount = static_cast<int32_t>(result.msgRemainCount);
457     SmsSegmentsInfo::SmsSegmentCodeScheme dcs = static_cast<SmsSegmentsInfo::SmsSegmentCodeScheme>(result.dcs);
458     switch (dcs) {
459         case SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_7BIT:
460         case SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_8BIT:
461         case SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_16BIT:
462             info.msgCodeScheme = dcs;
463             break;
464         default:
465             info.msgCodeScheme = SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_UNKNOWN;
466             break;
467     }
468     return TELEPHONY_ERR_SUCCESS;
469 }
470 
GetServiceRunningState()471 int32_t SmsService::GetServiceRunningState()
472 {
473     return static_cast<int32_t>(state_);
474 }
475 
GetEndTime()476 int64_t SmsService::GetEndTime()
477 {
478     return endTime_;
479 }
480 
ValidDestinationAddress(std::string desAddr)481 bool SmsService::ValidDestinationAddress(std::string desAddr)
482 {
483     // Allow address start with '+' and number, Address length range 3 to 20
484     std::regex regexMode("^([0-9_+]{1})([0-9]{2,19})$");
485     return std::regex_match(desAddr, regexMode);
486 }
487 
GetSpendTime()488 int64_t SmsService::GetSpendTime()
489 {
490     spendTime_ = endTime_ - bindTime_;
491     return spendTime_;
492 }
493 
CreateMessage(std::string pdu,std::string specification,ShortMessage & message)494 int32_t SmsService::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
495 {
496     std::shared_ptr<SmsBaseMessage> baseMessage;
497     if (specification == "3gpp") {
498         baseMessage = GsmSmsMessage::CreateMessage(pdu);
499     } else if (specification == "3gpp2") {
500         baseMessage = CdmaSmsMessage::CreateMessage(pdu);
501     }
502 
503     if (baseMessage == nullptr) {
504         return TELEPHONY_ERR_LOCAL_PTR_NULL;
505     }
506     message.visibleMessageBody_ = StringUtils::ToUtf16(baseMessage->GetVisibleMessageBody());
507     message.visibleRawAddress_ = StringUtils::ToUtf16(baseMessage->GetVisibleOriginatingAddress());
508     message.messageClass_ = static_cast<ShortMessage::SmsMessageClass>(baseMessage->GetMessageClass());
509     message.scAddress_ = StringUtils::ToUtf16(baseMessage->GetSmscAddr());
510     message.scTimestamp_ = baseMessage->GetScTimestamp();
511     message.isReplaceMessage_ = baseMessage->IsReplaceMessage();
512     message.status_ = baseMessage->GetStatus();
513     message.isSmsStatusReportMessage_ = baseMessage->IsSmsStatusReportMessage();
514     message.hasReplyPath_ = baseMessage->HasReplyPath();
515     message.protocolId_ = baseMessage->GetProtocolId();
516     message.pdu_ = baseMessage->GetRawPdu();
517     return TELEPHONY_ERR_SUCCESS;
518 }
519 
GetBase64Encode(std::string src,std::string & dest)520 bool SmsService::GetBase64Encode(std::string src, std::string &dest)
521 {
522     MsgTextConvert *textCvt = MsgTextConvert::Instance();
523     if (textCvt == nullptr) {
524         return false;
525     }
526 
527     textCvt->Base64Encode(src, dest);
528     return true;
529 }
530 
GetBase64Decode(std::string src,std::string & dest)531 bool SmsService::GetBase64Decode(std::string src, std::string &dest)
532 {
533     MsgTextConvert *textCvt = MsgTextConvert::Instance();
534     if (textCvt == nullptr) {
535         return false;
536     }
537 
538     textCvt->Base64Decode(src, dest);
539     return true;
540 }
541 
GetEncodeStringFunc(std::string & encodeString,uint32_t charset,uint32_t valLength,std::string strEncodeString)542 bool SmsService::GetEncodeStringFunc(
543     std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
544 {
545     MsgTextConvert *textCvt = MsgTextConvert::Instance();
546     if (textCvt == nullptr) {
547         return false;
548     }
549 
550     textCvt->GetEncodeString(encodeString, charset, valLength, strEncodeString);
551     return true;
552 }
553 } // namespace Telephony
554 } // namespace OHOS