Signed-off-by: jiangbinghan <jiangbinghan@huawei.com>
This commit is contained in:
jiangbinghan 2023-05-06 14:54:11 +08:00
parent 0d12f00931
commit f5e789565c
5 changed files with 0 additions and 1458 deletions

View File

@ -1,85 +0,0 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SMS_MMS_DATA_H
#define SMS_MMS_DATA_H
namespace OHOS {
namespace Telephony {
class SmsMmsData {
public:
static constexpr const char *ID = "id";
static constexpr const char *SLOT_ID = "slot_id";
static constexpr const char *SMS_SUBSECTION_ID = "sms_subsection_id";
static constexpr const char *RECEIVER_NUMBER = "receiver_number";
static constexpr const char *SENDER_NUMBER = "sender_number";
static constexpr const char *IS_SENDER = "is_sender";
static constexpr const char *START_TIME = "start_time";
static constexpr const char *END_TIME = "end_time";
static constexpr const char *RAW_PUD = "raw_pdu";
static constexpr const char *FORMAT = "format";
static constexpr const char *DEST_PORT = "dest_port";
static constexpr const char *SUBSECTION_INDEX = "subsection_index";
static constexpr const char *SIZE = "size";
};
class SmsMmsInfo {
public:
static constexpr const char *MSG_ID = "msg_id";
static constexpr const char *SLOT_ID = "slot_id";
static constexpr const char *RECEIVER_NUMBER = "receiver_number";
static constexpr const char *SENDER_NUMBER = "sender_number";
static constexpr const char *IS_SENDER = "is_sender";
static constexpr const char *START_TIME = "start_time";
static constexpr const char *END_TIME = "end_time";
static constexpr const char *MSG_TYPE = "msg_type";
static constexpr const char *SMS_TYPE = "sms_type";
static constexpr const char *MSG_TITLE = "msg_title";
static constexpr const char *MSG_CONTENT = "msg_content";
static constexpr const char *MSG_STATE = "msg_state";
static constexpr const char *MSG_CODE = "msg_code";
static constexpr const char *IS_LOCK = "is_lock";
static constexpr const char *IS_READ = "is_read";
static constexpr const char *IS_COLLECT = "is_collect";
static constexpr const char *SESSION_TYPE = "session_type";
static constexpr const char *RETRY_NUMBER = "retry_number";
static constexpr const char *OPERATOR_SERVICE_NUMBER = "operator_service_number";
static constexpr const char *SESSION_ID = "session_id";
static constexpr const char *GROUP_ID = "group_id";
static constexpr const char *DEVICE_ID = "device_id";
static constexpr const char *IS_SUBSECTION = "is_subsection";
static constexpr const char *IS_SEND_REPORT = "is_send_report";
};
class Session {
public:
static constexpr const char *ID = "id";
static constexpr const char *TIME = "time";
static constexpr const char *TELEPHONE = "telephone";
static constexpr const char *CONTENT = "content";
static constexpr const char *CONTACTS_NUM = "contacts_num";
static constexpr const char *SMS_TYPE = "sms_type";
static constexpr const char *UNREAD_COUNT = "unread_count";
static constexpr const char *SENDING_STATUS = "sending_status";
static constexpr const char *HAS_DRAFT = "has_draft";
static constexpr const char *HAS_LOCK = "has_lock";
static constexpr const char *MESSAGE_COUNT = "message_count";
static constexpr const char *HAS_MMS = "has_mms";
static constexpr const char *HAS_ATTACHMENT = "has_attachment";
};
} // namespace Telephony
} // namespace OHOS
#endif // SMS_MMS_DATA_H

View File

@ -1,311 +0,0 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sms_persist_helper.h"
#include "ability_manager_interface.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability.h"
#include "system_ability_definition.h"
#include "resource_manager.h"
#include "parameter.h"
#include "telephony_log_wrapper.h"
#include "string_utils.h"
namespace OHOS {
namespace Telephony {
class AbsSharedResultSet;
SmsPersistHelper::SmsPersistHelper() {}
SmsPersistHelper::~SmsPersistHelper() {}
std::shared_ptr<DataShare::DataShareHelper> SmsPersistHelper::CreateDataShareHelper(std::string uri)
{
auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (saManager == nullptr) {
TELEPHONY_LOGE(" Get system ability mgr failed.");
return nullptr;
}
auto remoteObj = saManager->GetSystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID);
if (remoteObj == nullptr) {
TELEPHONY_LOGE("GetSystemAbility Service Failed.");
return nullptr;
}
return DataShare::DataShareHelper::Creator(remoteObj, uri);
}
bool SmsPersistHelper::Insert(DataShare::DataShareValuesBucket &values)
{
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(SMS_SUBSECTION);
int ret = helper->Insert(uri, values);
helper->Release();
return ret >= 0 ? true : false;
}
bool SmsPersistHelper::Insert(std::string tableUri, DataShare::DataShareValuesBucket &values)
{
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(tableUri);
int ret = helper->Insert(uri, values);
helper->Release();
return ret >= 0 ? true : false;
}
bool SmsPersistHelper::QuerySession(DataShare::DataSharePredicates &predicates, uint16_t &sectionId,
uint16_t &messageCount)
{
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(SESSION);
std::vector<std::string> columns;
auto resultSet = helper->Query(uri, predicates, columns);
if (resultSet == nullptr) {
TELEPHONY_LOGE("Query Result Set nullptr Failed.");
return false;
}
resultSet->GoToFirstRow();
int32_t columnInt;
int columnIndex;
resultSet->GetColumnIndex("id", columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
sectionId = columnInt;
}
resultSet->GetColumnIndex("message_count", columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
messageCount = columnInt;
return true;
}
resultSet->Close();
helper->Release();
return false;
}
bool SmsPersistHelper::Update(DataShare::DataSharePredicates &predicates, DataShare::DataShareValuesBucket &values)
{
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(SESSION);
int ret = helper->Update(uri, predicates, values);
helper->Release();
return ret >= 0 ? true : false;
}
bool SmsPersistHelper::Query(DataShare::DataSharePredicates &predicates, std::vector<SmsReceiveIndexer> &indexers)
{
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(SMS_SUBSECTION);
std::vector<std::string> columns;
auto resultSet = helper->Query(uri, predicates, columns);
helper->Release();
if (resultSet == nullptr) {
TELEPHONY_LOGE("Query Result Set nullptr Failed.");
return false;
}
int resultSetNum = resultSet->GoToFirstRow();
while (resultSetNum == 0) {
SmsReceiveIndexer indexer;
ResultSetConvertToIndexer(indexer, resultSet);
indexers.push_back(indexer);
resultSetNum = resultSet->GoToNextRow();
}
resultSet->Close();
return true;
}
bool SmsPersistHelper::QueryMaxGroupId(DataShare::DataSharePredicates &predicates, uint16_t &maxGroupId)
{
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(SMS_MMS_INFO);
std::vector<std::string> columns;
auto resultSet = helper->Query(uri, predicates, columns);
if (resultSet == nullptr) {
TELEPHONY_LOGE("Query Result Set nullptr Failed.");
return false;
}
int32_t columnInt;
int columnIndex;
resultSet->GoToLastRow();
resultSet->GetColumnIndex(SmsMmsInfo::GROUP_ID, columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
maxGroupId = columnInt;
}
resultSet->Close();
helper->Release();
return true;
}
bool SmsPersistHelper::Delete(DataShare::DataSharePredicates &predicates)
{
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(SMS_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(SMS_SUBSECTION);
int ret = helper->Delete(uri, predicates);
helper->Release();
return ret >= 0 ? true : false;
}
bool SmsPersistHelper::QueryBlockPhoneNumber(const std::string &phoneNum)
{
bool result = false;
int count = 0;
const std::string phoneNumber = "phone_number";
if (phoneNum.empty()) {
return result;
}
std::shared_ptr<DataShare::DataShareHelper> helper = CreateDataShareHelper(CONTACT_URI);
if (helper == nullptr) {
TELEPHONY_LOGE("Create Data Ability Helper nullptr Failed.");
return false;
}
Uri uri(CONTACT_BLOCK);
std::vector<std::string> columns;
DataShare::DataSharePredicates predicates;
predicates.EqualTo(phoneNumber, phoneNum);
auto resultSet = helper->Query(uri, predicates, columns);
helper->Release();
if (resultSet == nullptr) {
TELEPHONY_LOGE("Query Result Set nullptr Failed.");
return result;
}
if (resultSet->GetRowCount(count) == 0 && count != 0) {
result = true;
}
resultSet->Close();
return result;
}
bool SmsPersistHelper::QueryParamBoolean(const std::string key, bool defValue)
{
const int PARAM_SIZE = 64;
char paramOutBuff[PARAM_SIZE] = {0};
std::string defStrValue = defValue ? "1" : "0";
std::string paramValue(defStrValue);
int retLen = GetParameter(key.c_str(), defStrValue.c_str(), paramOutBuff, PARAM_SIZE);
if (retLen > 0) {
paramOutBuff[retLen] = '\0';
paramValue.assign(paramOutBuff, retLen);
}
TELEPHONY_LOGI("QueryParamBoolean: %{public}zu : %{public}s", paramValue.size(), paramValue.c_str());
if ((paramValue == std::string("1")) || (paramValue == std::string("y")) || (paramValue == std::string("yes")) ||
(paramValue == std::string("on")) || (paramValue == std::string("true"))) {
return true;
} else if ((paramValue == std::string("0")) || (paramValue == std::string("n")) ||
(paramValue == std::string("no")) || (paramValue == std::string("off")) ||
(paramValue == std::string("false"))) {
return false;
}
return defValue;
}
void SmsPersistHelper::ConvertIntToIndexer(
SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
{
int32_t columnInt;
int columnIndex;
resultSet->GetColumnIndex(SmsMmsData::FORMAT, columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
info.SetIsCdma(columnInt != 0);
}
resultSet->GetColumnIndex(SmsMmsData::SMS_SUBSECTION_ID, columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
info.SetMsgRefId(columnInt);
}
resultSet->GetColumnIndex(SmsMmsData::SIZE, columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
info.SetMsgCount(columnInt);
}
resultSet->GetColumnIndex(SmsMmsData::SUBSECTION_INDEX, columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
info.SetMsgSeqId(columnInt);
}
resultSet->GetColumnIndex(SmsMmsData::DEST_PORT, columnIndex);
if (resultSet->GetInt(columnIndex, columnInt) == 0) {
info.SetDestPort(columnInt);
}
}
void SmsPersistHelper::ConvertStringToIndexer(
SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
{
int columnIndex;
std::string columnValue;
resultSet->GetColumnIndex(SmsMmsData::RECEIVER_NUMBER, columnIndex);
if (resultSet->GetString(columnIndex, columnValue) == 0) {
info.SetVisibleAddress(columnValue);
}
resultSet->GetColumnIndex(SmsMmsData::SENDER_NUMBER, columnIndex);
if (resultSet->GetString(columnIndex, columnValue) == 0) {
info.SetOriginatingAddress(columnValue);
}
resultSet->GetColumnIndex(SmsMmsData::START_TIME, columnIndex);
if (resultSet->GetString(columnIndex, columnValue) == 0) {
info.SetTimestamp(std::stol(columnValue));
}
resultSet->GetColumnIndex(SmsMmsData::END_TIME, columnIndex);
if (resultSet->GetString(columnIndex, columnValue) == 0) {
info.SetTimestamp(std::stol(columnValue));
}
resultSet->GetColumnIndex(SmsMmsData::RAW_PUD, columnIndex);
if (resultSet->GetString(columnIndex, columnValue) == 0) {
info.SetPdu(StringUtils::HexToByteVector(columnValue));
}
}
void SmsPersistHelper::ResultSetConvertToIndexer(
SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
{
ConvertIntToIndexer(info, resultSet);
ConvertStringToIndexer(info, resultSet);
}
} // namespace Telephony
} // namespace OHOS

View File

@ -1,66 +0,0 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SMS_PERSIST_HELPER_H
#define SMS_PERSIST_HELPER_H
#include "singleton.h"
#include "datashare_helper.h"
#include "datashare_predicates.h"
#include "sms_receive_indexer.h"
#include "sms_mms_data.h"
namespace OHOS {
namespace Telephony {
class SmsPersistHelper {
DECLARE_DELAYED_SINGLETON(SmsPersistHelper)
public:
bool Insert(DataShare::DataShareValuesBucket &values);
bool Insert(std::string tableUri, DataShare::DataShareValuesBucket &values);
bool Query(DataShare::DataSharePredicates &predicates, std::vector<SmsReceiveIndexer> &indexers);
bool QueryMaxGroupId(DataShare::DataSharePredicates &predicates, uint16_t &maxGroupId);
bool QuerySession(DataShare::DataSharePredicates &predicates, uint16_t &sectionId, uint16_t &messageCount);
bool Delete(DataShare::DataSharePredicates &predicates);
bool QueryBlockPhoneNumber(const std::string &phoneNum);
bool QueryParamBoolean(const std::string key, bool defValue);
bool Update(DataShare::DataSharePredicates &predicates, DataShare::DataShareValuesBucket &values);
inline static const std::string SMS_CAPABLE_KEY = "sms_config_capable";
inline static const std::string SMS_ENCODING_KEY = "sms_config_force_7bit_encoding";
inline static const std::string SMS_CAPABLE_PARAM_KEY = "const.telephony.sms.capable";
inline static const std::string SMS_ENCODING_PARAM_KEY = "persist.sys.sms.config.7bitforce";
private:
const std::string SMS_URI = "datashare:///com.ohos.smsmmsability";
const std::string SMS_SUBSECTION = "datashare:///com.ohos.smsmmsability/sms_mms/sms_subsection";
const std::string SMS_MMS_INFO = "datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info";
const std::string SESSION = "datashare:///com.ohos.smsmmsability/sms_mms/session";
const std::string CONTACT_URI = "datashare:///com.ohos.contactsdataability";
const std::string CONTACT_BLOCK =
"datashare:///com.ohos.contactsdataability/contacts/contact_blocklist";
std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(std::string uri);
void ResultSetConvertToIndexer(
SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet);
void ConvertIntToIndexer(
SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet);
void ConvertStringToIndexer(
SmsReceiveIndexer &info, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet);
};
} // namespace Telephony
} // namespace OHOS
#endif

View File

@ -1,680 +0,0 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sms_service.h"
#include <regex>
#include "cdma_sms_message.h"
#include "core_manager_inner.h"
#include "ims_sms_client.h"
#include "msg_text_convert.h"
#include "sms_dump_helper.h"
#include "sms_hisysevent.h"
#include "string_utils.h"
#include "telephony_log_wrapper.h"
#include "telephony_common_utils.h"
#include "telephony_permission.h"
namespace OHOS {
namespace Telephony {
using namespace std;
using namespace AppExecFwk;
using namespace HiviewDFX;
constexpr static uint32_t CONNECT_SERVICE_WAIT_TIME = 2000;
constexpr static size_t MIN_LEN = 1;
bool g_registerResult = SystemAbility::MakeAndRegisterAbility(DelayedSingleton<SmsService>::GetInstance().get());
SmsService::SmsService() : SystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID, true) {}
SmsService::~SmsService() {}
void SmsService::OnStart()
{
TELEPHONY_LOGI("SmsService::OnStart start service Enter.");
bindTime_ =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
.count();
if (state_ == ServiceRunningState::STATE_RUNNING) {
TELEPHONY_LOGE("msService has already started.");
return;
}
if (!Init()) {
TELEPHONY_LOGE("failed to init SmsService");
return;
}
state_ = ServiceRunningState::STATE_RUNNING;
endTime_ =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
.count();
TELEPHONY_LOGI("SmsService::OnStart start service Exit.");
}
bool SmsService::Init()
{
DelayedSingleton<ImsSmsClient>::GetInstance()->Init();
if (!registerToService_) {
bool ret = Publish(DelayedSingleton<SmsService>::GetInstance().get());
if (!ret) {
TELEPHONY_LOGE("SmsService::Init Publish failed!");
return false;
}
registerToService_ = true;
WaitCoreServiceToInit();
}
DelayedSingleton<SmsStateHandler>::GetInstance()->Init();
return true;
}
void SmsService::OnStop()
{
state_ = ServiceRunningState::STATE_NOT_START;
registerToService_ = false;
DelayedSingleton<ImsSmsClient>::GetInstance()->UnInit();
DelayedSingleton<SmsStateHandler>::GetInstance()->UnInit();
TELEPHONY_LOGI("SmsService::OnStop stop service.");
}
int32_t SmsService::Dump(std::int32_t fd, const std::vector<std::u16string> &args)
{
if (fd < 0) {
TELEPHONY_LOGE("dump fd invalid");
return TELEPHONY_ERR_FAIL;
}
std::vector<std::string> argsInStr;
for (const auto &arg : args) {
TELEPHONY_LOGI("Dump args: %{public}s", Str16ToStr8(arg).c_str());
argsInStr.emplace_back(Str16ToStr8(arg));
}
std::string result;
SmsDumpHelper dumpHelper;
if (dumpHelper.Dump(argsInStr, result)) {
TELEPHONY_LOGI("%{public}s", result.c_str());
std::int32_t ret = dprintf(fd, "%s", result.c_str());
if (ret < 0) {
TELEPHONY_LOGE("dprintf to dump fd failed");
return TELEPHONY_ERR_FAIL;
}
return TELEPHONY_SUCCESS;
}
TELEPHONY_LOGW("dumpHelper failed");
return TELEPHONY_ERR_FAIL;
}
void SmsService::WaitCoreServiceToInit()
{
std::thread connectTask([&]() {
while (true) {
if (CoreManagerInner::GetInstance().IsInitFinished()) {
InitModule();
TELEPHONY_LOGI("SmsService Connection successful");
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(CONNECT_SERVICE_WAIT_TIME));
}
});
connectTask.detach();
}
std::string SmsService::GetBindTime()
{
return std::to_string(bindTime_);
}
int32_t SmsService::SendMessage(int32_t slotId, const u16string desAddr, const u16string scAddr, const u16string text,
const sptr<ISendShortMessageCallback> &sendCallback, const sptr<IDeliveryShortMessageCallback> &deliveryCallback)
{
if (NoPermissionOrParametersCheckFail(slotId, desAddr, sendCallback)) {
SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
SmsMmsErrorCode::SMS_ERROR_PERMISSION_ERROR, Permission::SEND_MESSAGES);
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
SmsMmsErrorCode::SMS_ERROR_NULL_POINTER, "text sms interfaceManager is nullptr");
TELEPHONY_LOGE("SmsService::SendMessage interfaceManager nullptr error.");
return TELEPHONY_ERR_LOCAL_PTR_NULL;
}
if (!ValidDestinationAddress(StringUtils::ToUtf8(desAddr))) {
SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
TELEPHONY_LOGE("SmsService::SendMessage desAddr not conform to the regular specification");
return TELEPHONY_ERR_ARGUMENT_INVALID;
}
int32_t ret = interfaceManager->TextBasedSmsDelivery(StringUtils::ToUtf8(desAddr), StringUtils::ToUtf8(scAddr),
StringUtils::ToUtf8(text), sendCallback, deliveryCallback);
std::string bundleName = GetBundleName();
if (bundleName != COM_OHOS_MMS) {
InsertSessionAndDetail(slotId, StringUtils::ToUtf8(desAddr), StringUtils::ToUtf8(text));
}
return ret;
}
void SmsService::InsertSessionAndDetail(int32_t slotId, const std::string &telephone, const std::string &text)
{
uint16_t sectionId;
uint16_t messageCount;
bool ret = QuerySessionByTelephone(telephone, sectionId, messageCount);
if (ret) {
InsertSmsMmsInfo(slotId, sectionId, telephone, text);
messageCount++;
InsertSession(false, messageCount, telephone, text);
} else {
ret = InsertSession(true, 0, telephone, text);
if (ret) {
QuerySessionByTelephone(telephone, sectionId, messageCount);
InsertSmsMmsInfo(slotId, sectionId, telephone, text);
}
}
}
bool SmsService::QuerySessionByTelephone(const std::string &telephone, uint16_t &sectionId, uint16_t &messageCount)
{
DataShare::DataSharePredicates predicates;
predicates.EqualTo(Session::TELEPHONE, telephone);
return DelayedSingleton<SmsPersistHelper>::GetInstance()->QuerySession(predicates, sectionId, messageCount);
}
void SmsService::InsertSmsMmsInfo(int32_t slotId, uint16_t sessionId, const std::string &number, const std::string &text)
{
DataShare::DataSharePredicates predicates;
uint16_t maxGroupId = 0;
DelayedSingleton<SmsPersistHelper>::GetInstance()->QueryMaxGroupId(predicates, maxGroupId);
auto now = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
DataShare::DataShareValuesBucket smsMmsInfoBucket;
smsMmsInfoBucket.Put(SmsMmsInfo::SLOT_ID, std::to_string(slotId));
smsMmsInfoBucket.Put(SmsMmsInfo::RECEIVER_NUMBER, number);
smsMmsInfoBucket.Put(SmsMmsInfo::SENDER_NUMBER, "");
smsMmsInfoBucket.Put(SmsMmsInfo::IS_SENDER, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::START_TIME, std::to_string(duration.count()));
smsMmsInfoBucket.Put(SmsMmsInfo::END_TIME, std::to_string(duration.count()));
smsMmsInfoBucket.Put(SmsMmsInfo::MSG_TYPE, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::SMS_TYPE, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::MSG_TITLE, text);
smsMmsInfoBucket.Put(SmsMmsInfo::MSG_CONTENT, text);
smsMmsInfoBucket.Put(SmsMmsInfo::MSG_STATE, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::MSG_CODE, "");
smsMmsInfoBucket.Put(SmsMmsInfo::IS_LOCK, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::IS_READ, "1");
smsMmsInfoBucket.Put(SmsMmsInfo::IS_COLLECT, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::SESSION_TYPE, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::SESSION_ID, std::to_string(sessionId));
smsMmsInfoBucket.Put(SmsMmsInfo::GROUP_ID, std::to_string(maxGroupId + 1));
smsMmsInfoBucket.Put(SmsMmsInfo::IS_SUBSECTION, "0");
smsMmsInfoBucket.Put(SmsMmsInfo::IS_SEND_REPORT, "0");
DelayedSingleton<SmsPersistHelper>::GetInstance()->Insert(SMS_MMS_INFO, smsMmsInfoBucket);
}
bool SmsService::InsertSession(bool isNewSession, uint16_t messageCount, const std::string &number,
const std::string &text)
{
auto now = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
DataShare::DataShareValuesBucket sessionBucket;
sessionBucket.Put(Session::TIME, std::to_string(duration.count()));
sessionBucket.Put(Session::TELEPHONE, number);
sessionBucket.Put(Session::CONTENT, text);
sessionBucket.Put(Session::CONTACTS_NUM, "1");
sessionBucket.Put(Session::SMS_TYPE, "0");
sessionBucket.Put(Session::UNREAD_COUNT, "0");
sessionBucket.Put(Session::SENDING_STATUS, "0");
sessionBucket.Put(Session::HAS_DRAFT, "0");
sessionBucket.Put(Session::HAS_LOCK, "0");
sessionBucket.Put(Session::HAS_MMS, "0");
sessionBucket.Put(Session::HAS_ATTACHMENT, "0");
if (!isNewSession) {
sessionBucket.Put(Session::MESSAGE_COUNT, std::to_string(messageCount));
DataShare::DataSharePredicates predicates;
predicates.EqualTo(Session::TELEPHONE, number);
return DelayedSingleton<SmsPersistHelper>::GetInstance()->Update(predicates, sessionBucket);
}
sessionBucket.Put(Session::MESSAGE_COUNT, "1");
return DelayedSingleton<SmsPersistHelper>::GetInstance()->Insert(SESSION, sessionBucket);
}
int32_t SmsService::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)
{
if (NoPermissionOrParametersCheckFail(slotId, desAddr, sendCallback)) {
SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
SmsMmsErrorCode::SMS_ERROR_PERMISSION_ERROR, Permission::SEND_MESSAGES);
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
TELEPHONY_LOGE("SmsService::SendMessage interfaceManager nullptr error.");
SmsHiSysEvent::WriteSmsSendFaultEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE,
SmsMmsErrorCode::SMS_ERROR_NULL_POINTER, "data sms interfaceManager is nullptr");
return TELEPHONY_ERR_LOCAL_PTR_NULL;
}
if (!ValidDestinationAddress(StringUtils::ToUtf8(desAddr))) {
SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
TELEPHONY_LOGE("SmsService::SendMessage desAddr not conform to the regular specification");
return TELEPHONY_ERR_ARGUMENT_INVALID;
}
return interfaceManager->DataBasedSmsDelivery(
StringUtils::ToUtf8(desAddr), StringUtils::ToUtf8(scAddr), port, data, dataLen, sendCallback, deliveryCallback);
}
bool SmsService::NoPermissionOrParametersCheckFail(
int32_t slotId, const u16string desAddr, const sptr<ISendShortMessageCallback> &sendCallback)
{
if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
return true;
}
if (desAddr.empty()) {
SmsSender::SendResultCallBack(sendCallback, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
TELEPHONY_LOGE("SmsService::SendMessage desAddr invalid.");
return true;
}
return false;
}
int32_t SmsService::IsImsSmsSupported(int32_t slotId, bool &isSupported)
{
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("interfaceManager is nullptr.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
return interfaceManager->IsImsSmsSupported(slotId, isSupported);
}
int32_t SmsService::GetImsShortMessageFormat(std::u16string &format)
{
int32_t slotId = GetDefaultSmsSlotId();
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("interfaceManager is nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
return interfaceManager->GetImsShortMessageFormat(format);
}
bool SmsService::HasSmsCapability()
{
int32_t slotId = GetDefaultSmsSlotId();
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("Get SmsInterfaceManager is nullptr error.");
return true;
}
return interfaceManager->HasSmsCapability();
}
int32_t SmsService::SetSmscAddr(int32_t slotId, const std::u16string &scAddr)
{
if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Set State Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("interfaceManager is nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
string sca = StringUtils::ToUtf8(scAddr);
TrimSmscAddr(sca);
if (sca.empty() || sca.length() == 0) {
TELEPHONY_LOGE("sca is empty");
return TELEPHONY_ERR_ARGUMENT_NULL;
}
return interfaceManager->SetSmscAddr(sca);
}
void SmsService::TrimSmscAddr(std::string &sca)
{
if (sca.length() < MIN_LEN) {
return;
}
while (sca.length() >= MIN_LEN && sca[0] == ' ') {
sca.erase(0, 1);
}
while (sca.length() > MIN_LEN && sca[sca.length() - 1] == ' ') {
sca.erase(sca.length() - 1, 1);
}
}
int32_t SmsService::GetSmscAddr(int32_t slotId, std::u16string &smscAddress)
{
if (!TelephonyPermission::CheckPermission(Permission::GET_TELEPHONY_STATE)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Get State Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::GetSmscAddr interfaceManager nullptr error");
return TELEPHONY_ERR_SLOTID_INVALID;
}
return interfaceManager->GetSmscAddr(smscAddress);
}
int32_t SmsService::AddSimMessage(
int32_t slotId, const std::u16string &smsc, const std::u16string &pdu, SimMessageStatus status)
{
if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::AddSimMessage interfaceManager nullptr error");
return TELEPHONY_ERR_SLOTID_INVALID;
}
std::string smscData = StringUtils::ToUtf8(smsc);
std::string pduData = StringUtils::ToUtf8(pdu);
if (pdu.empty() || pduData.empty()) {
TELEPHONY_LOGE("SmsService::AddSimMessage pdu empty error");
return TELEPHONY_ERR_ARGUMENT_INVALID;
}
return interfaceManager->AddSimMessage(smscData, pduData, status);
}
int32_t SmsService::DelSimMessage(int32_t slotId, uint32_t msgIndex)
{
if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::DelSimMessage interfaceManager nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
if (!CheckSimMessageIndexValid(slotId, msgIndex)) {
TELEPHONY_LOGE("SmsService::DelSimMessage msgIndex inValid");
return TELEPHONY_ERR_ARGUMENT_INVALID;
}
return interfaceManager->DelSimMessage(msgIndex);
}
int32_t SmsService::UpdateSimMessage(int32_t slotId, uint32_t msgIndex, SimMessageStatus newStatus,
const std::u16string &pdu, const std::u16string &smsc)
{
if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Send Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::UpdateSimMessage interfaceManager nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
std::string pduData = StringUtils::ToUtf8(pdu);
std::string smscData = StringUtils::ToUtf8(smsc);
if (!CheckSimMessageIndexValid(slotId, msgIndex)) {
TELEPHONY_LOGE("SmsService::UpdateSimMessage msgIndex inValid");
return TELEPHONY_ERR_ARGUMENT_INVALID;
}
if (pdu.empty() || pduData.empty()) {
TELEPHONY_LOGE("SmsService::UpdateSimMessage pdu empty error");
return TELEPHONY_ERR_ARGUMENT_INVALID;
}
return interfaceManager->UpdateSimMessage(msgIndex, newStatus, pduData, smscData);
}
int32_t SmsService::GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message)
{
if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::GetAllSimMessages interfaceManager nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
return interfaceManager->GetAllSimMessages(message);
}
bool SmsService::CheckSimMessageIndexValid(int32_t slotId, uint32_t msgIndex)
{
std::vector<ShortMessage> totalMessages;
int32_t result = GetAllSimMessages(slotId, totalMessages);
if (result != TELEPHONY_ERR_SUCCESS) {
TELEPHONY_LOGE("SmsService::CheckSimMessageIndexValid get result fail");
return false;
}
if (msgIndex < 0 || totalMessages.size() == 0) {
TELEPHONY_LOGE("SmsService::CheckSimMessageIndexValid msgIndex error");
return false;
}
for (auto message : totalMessages) {
if (message.GetIndexOnSim() == static_cast<int32_t>(msgIndex)) {
return true;
}
}
TELEPHONY_LOGI("SmsService::CheckSimMessageIndexValid msgIndex not founded");
return false;
}
int32_t SmsService::SetCBConfig(int32_t slotId, bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType)
{
if (!TelephonyPermission::CheckPermission(Permission::RECEIVE_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Receive Messages Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::SetCBConfig interfaceManager nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
return interfaceManager->SetCBConfig(enable, fromMsgId, toMsgId, netType);
}
bool SmsService::SetImsSmsConfig(int32_t slotId, int32_t enable)
{
if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
TELEPHONY_LOGE("Check permission failed.");
return false;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::SetImsSmsConfig interfaceManager nullptr error.");
return false;
}
return interfaceManager->SetImsSmsConfig(slotId, enable);
}
int32_t SmsService::SetDefaultSmsSlotId(int32_t slotId)
{
if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Set State Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager();
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::SetDefaultSmsSlotId interfaceManager nullptr error.");
return TELEPHONY_ERR_LOCAL_PTR_NULL;
}
return interfaceManager->SetDefaultSmsSlotId(slotId);
}
int32_t SmsService::GetDefaultSmsSlotId()
{
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager();
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::GetDefaultSmsSlotId interfaceManager nullptr error.");
return TELEPHONY_ERROR;
}
return interfaceManager->GetDefaultSmsSlotId();
}
int32_t SmsService::SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage)
{
if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
TELEPHONY_LOGE("Check Permission Failed, No Has Telephony Get State Permisson.");
return TELEPHONY_ERR_PERMISSION_ERR;
}
if (message.empty()) {
return TELEPHONY_ERR_ARGUMENT_INVALID;
}
int32_t slotId = GetDefaultSmsSlotId();
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("SmsService::SplitMessage interfaceManager nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
std::string messageData = StringUtils::ToUtf8(message);
return interfaceManager->SplitMessage(messageData, splitMessage);
}
int32_t SmsService::GetSmsSegmentsInfo(
int32_t slotId, const std::u16string &message, bool force7BitCode, ISmsServiceInterface::SmsSegmentsInfo &info)
{
if (message.empty()) {
return TELEPHONY_ERR_ARGUMENT_NULL;
}
std::shared_ptr<SmsInterfaceManager> interfaceManager = GetSmsInterfaceManager(slotId);
if (interfaceManager == nullptr) {
TELEPHONY_LOGE("GetSmsSegmentsInfo interfaceManager is nullptr error.");
return TELEPHONY_ERR_SLOTID_INVALID;
}
std::string messageData = StringUtils::ToUtf8(message);
LengthInfo result;
int32_t ret = interfaceManager->GetSmsSegmentsInfo(messageData, force7BitCode, result);
if (ret != TELEPHONY_ERR_SUCCESS) {
return ret;
}
info.msgSegCount = static_cast<int32_t>(result.msgSegCount);
info.msgEncodingCount = static_cast<int32_t>(result.msgEncodeCount);
info.msgRemainCount = static_cast<int32_t>(result.msgRemainCount);
SmsSegmentsInfo::SmsSegmentCodeScheme dcs = static_cast<SmsSegmentsInfo::SmsSegmentCodeScheme>(result.dcs);
switch (dcs) {
case SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_7BIT:
case SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_8BIT:
case SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_16BIT:
info.msgCodeScheme = dcs;
break;
default:
info.msgCodeScheme = SmsSegmentsInfo::SmsSegmentCodeScheme::SMS_ENCODING_UNKNOWN;
break;
}
return TELEPHONY_ERR_SUCCESS;
}
int32_t SmsService::GetServiceRunningState()
{
return static_cast<int32_t>(state_);
}
int64_t SmsService::GetEndTime()
{
return endTime_;
}
bool SmsService::ValidDestinationAddress(std::string desAddr)
{
// Allow address start with '+' and number, Address length range 3 to 20
std::regex regexMode("^([0-9_+]{1})([0-9]{2,19})$");
return std::regex_match(desAddr, regexMode);
}
int64_t SmsService::GetSpendTime()
{
spendTime_ = endTime_ - bindTime_;
return spendTime_;
}
int32_t SmsService::CreateMessage(std::string pdu, std::string specification, ShortMessage &message)
{
std::shared_ptr<SmsBaseMessage> baseMessage;
if (specification == "3gpp") {
baseMessage = GsmSmsMessage::CreateMessage(pdu);
} else if (specification == "3gpp2") {
baseMessage = CdmaSmsMessage::CreateMessage(pdu);
}
if (baseMessage == nullptr) {
return TELEPHONY_ERR_LOCAL_PTR_NULL;
}
message.visibleMessageBody_ = StringUtils::ToUtf16(baseMessage->GetVisibleMessageBody());
message.visibleRawAddress_ = StringUtils::ToUtf16(baseMessage->GetVisibleOriginatingAddress());
message.messageClass_ = static_cast<ShortMessage::SmsMessageClass>(baseMessage->GetMessageClass());
message.scAddress_ = StringUtils::ToUtf16(baseMessage->GetSmscAddr());
message.scTimestamp_ = baseMessage->GetScTimestamp();
message.isReplaceMessage_ = baseMessage->IsReplaceMessage();
message.status_ = baseMessage->GetStatus();
message.isSmsStatusReportMessage_ = baseMessage->IsSmsStatusReportMessage();
message.hasReplyPath_ = baseMessage->HasReplyPath();
message.protocolId_ = baseMessage->GetProtocolId();
message.pdu_ = baseMessage->GetRawPdu();
return TELEPHONY_ERR_SUCCESS;
}
bool SmsService::GetBase64Encode(std::string src, std::string &dest)
{
MsgTextConvert *textCvt = MsgTextConvert::Instance();
if (textCvt == nullptr) {
return false;
}
textCvt->Base64Encode(src, dest);
return true;
}
bool SmsService::GetBase64Decode(std::string src, std::string &dest)
{
MsgTextConvert *textCvt = MsgTextConvert::Instance();
if (textCvt == nullptr) {
return false;
}
textCvt->Base64Decode(src, dest);
return true;
}
bool SmsService::GetEncodeStringFunc(
std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString)
{
MsgTextConvert *textCvt = MsgTextConvert::Instance();
if (textCvt == nullptr) {
return false;
}
textCvt->GetEncodeString(encodeString, charset, valLength, strEncodeString);
return true;
}
} // namespace Telephony
} // namespace OHOS

View File

@ -1,316 +0,0 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SMS_SERVICE_H
#define SMS_SERVICE_H
#include <memory>
#include "sms_interface_stub.h"
#include "sms_state_handler.h"
#include "system_ability.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace Telephony {
enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
class SmsService : public SystemAbility, public SmsInterfaceStub, public std::enable_shared_from_this<SmsService> {
DECLARE_DELAYED_SINGLETON(SmsService)
DECLARE_SYSTEM_ABILITY(SmsService) // necessary
public:
void OnStart() override;
void OnStop() override;
int32_t Dump(std::int32_t fd, const std::vector<std::u16string> &args) override;
std::string GetBindTime();
/**
* Sends a text Type SMS message.
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param desAddr Indicates the destination address
* @param scAddr Indicates the sms center address
* @param text Indicates sms content
* @param sendCallback Indicates callback for send out
* @param deliverCallback Indicates callback for delivery to destination user
* @return Returns {@code 0} if send message success.
*/
int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
const std::u16string text, const sptr<ISendShortMessageCallback> &sendCallback,
const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override;
/**
* Sends a data Type SMS message.
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param desAddr Indicates the destination address
* @param scAddr Indicates the sms center address
* @param port Indicates the port of data sms
* @param data Indicates the array of data sms
* @param dataLen Indicates the array length of data sms
* @param sendCallback Indicates callback for send out
* @param deliverCallback Indicates callback for delivery to destination user
* @return Returns {@code 0} if send message success.
*/
int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr, uint16_t port,
const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback,
const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override;
/**
* Sets the address for the Short Message Service Center (SMSC) based on a
* specified slot ID
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param scAddr Indicates the sms center address
* @return Returns {@code 0} if set smsc success
*/
int32_t SetSmscAddr(int32_t slotId, const std::u16string &scAddr) override;
/**
* @brief GetSmscAddr
* Obtains the SMSC address based on a specified slot ID.
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param smscAddress [out]
* @return Returns {@code 0} if get smsc success.
*/
int32_t GetSmscAddr(int32_t slotId, std::u16string &smscAddress) override;
/**
* @brief AddSimMessage
* Add a sms to sim card.
* @param slotId [in]
* @param smsc [in]
* @param pdu [in]
* @param status [in]
* @return Returns {@code true} if add sim success; returns {@code false}
*/
int32_t AddSimMessage(
int32_t slotId, const std::u16string &smsc, const std::u16string &pdu, SimMessageStatus status) override;
/**
* @brief DelSimMessage
* Delete a sms in the sim card.
* @param slotId [in]
* @param msgIndex [in]
* @return Returns {@code true} if delete sim success; returns {@code false}
*/
int32_t DelSimMessage(int32_t slotId, uint32_t msgIndex) override;
/**
* @brief UpdateSimMessage
* Update a sms in the sim card.
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param msgIndex Indicates the sim sms index in sim card
* @param newStatusIndicates the sms status, read or not
* @param pdu Indicates the sms pdu data
* @param smsc Indicates the sms center address
* @return Returns {@code 0} if update sim success
*/
int32_t UpdateSimMessage(int32_t slotId, uint32_t msgIndex, SimMessageStatus newStatus, const std::u16string &pdu,
const std::u16string &smsc) override;
/**
* @brief GetAllSimMessages
* Get sim card all the sms.
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param message Indicates all SMS messages of sim card
* @return Returns {@code 0} if get all sim messages success
*/
int32_t GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message) override;
/**
* @brief SetCBConfig
* Configure a cell broadcast in a certain band range.
* @param slotId [in]
* @param enable [in]
* @param fromMsgId [in]
* @param toMsgId [in]
* @param netType [in]
* @return Returns {@code 0} if set CB config success
*/
int32_t SetCBConfig(int32_t slotId, bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType) override;
/**
* @brief SetImsSmsConfig enable or disable IMS SMS.
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by the device.
* @param enable Indicates enable or disable Ims sms
* ranging {@code 0} disable Ims sms {@code 1} enable Ims sms
* @return Returns {@code true} if enable or disable Ims Sms success; returns {@code false} otherwise.
*/
bool SetImsSmsConfig(int32_t slotId, int32_t enable) override;
/**
* @brief SetDefaultSmsSlotId
* Set the Default Sms Slot Id To SmsService
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @return Returns {@code 0} if set default sms slot id success
*/
int32_t SetDefaultSmsSlotId(int32_t slotId) override;
/**
* @brief GetDefaultSmsSlotId
* Get the Default Sms Slot Id From SmsService
* @return int32_t
*/
int32_t GetDefaultSmsSlotId() override;
/**
* @brief SplitMessage
* calculate Sms Message Split Segment count
* @param Indicates input message
* @param splitMessage Indicates the split information
* @return Returns {@code 0} if split message success
*/
int32_t SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage) override;
/**
* @brief GetSmsSegmentsInfo
* calculate the Sms Message Segments Info
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param message Indicates input message
* @param force7BitCode Indicates sms encode type, 7bit or not
* @param info Indicates output sms segment
* @return Returns {@code 0} if get sms segments info
*/
int32_t GetSmsSegmentsInfo(int32_t slotId, const std::u16string &message, bool force7BitCode,
ISmsServiceInterface::SmsSegmentsInfo &info) override;
/**
* @brief IsImsSmsSupported
* Check Sms Is supported Ims newtwork
* Hide this for inner system use
* @param slotId Indicates the card slot index number,
* ranging from {@code 0} to the maximum card slot index number supported by
* the device
* @param isSupported Whether ims SMS is supported
* @return Returns {@code 0} if successful
*/
int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported) override;
/**
* @brief GetImsShortMessageFormat
* Get the Ims Short Message Format 3gpp/3gpp2
* Hide this for inner system use
* @return int32_t
*/
int32_t GetImsShortMessageFormat(std::u16string &format) override;
/**
* @brief HasSmsCapability
* Check whether it is supported Sms Capability
* @return true
* @return false
*/
bool HasSmsCapability() override;
/**
* @brief GetServiceRunningState
* Get service running state
* @return ServiceRunningState
*/
int32_t GetServiceRunningState();
/**
* @brief GetSpendTime
* Get service start spend time
* @return Spend time
*/
int64_t GetSpendTime();
/**
* @brief GetEndTime
* Get service start finish time
* @return Spend time
*/
int64_t GetEndTime();
/**
* create a short message
* @param pdu Indicates pdu code,
* @param specification Indicates 3gpp or 3gpp2
* @param ShortMessage Indicates a short message object
* @return Returns {@code 0} if CreateMessage success
*/
int32_t CreateMessage(std::string pdu, std::string specification, ShortMessage &message) override;
/**
* mms base64 encode
* @param src Indicates source string,
* @param dest Indicates destination string
* @return Returns {@code true} if encode success; returns {@code false} otherwise
*/
bool GetBase64Encode(std::string src, std::string &dest) override;
/**
* mms base64 decode
* @param src Indicates source string,
* @param dest Indicates destination string
* @return Returns {@code true} if decode success; returns {@code false} otherwise
*/
bool GetBase64Decode(std::string src, std::string &dest) override;
/**
* Get Encode String
* @param encodeString Indicates output string,
* @param charset Indicates character set,
* @param valLength Indicates input string length,
* @param strEncodeString Indicates input string
* @return Returns {@code true} if decode success; returns {@code false} otherwise
*/
bool GetEncodeStringFunc(
std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString) override;
private:
bool Init();
void WaitCoreServiceToInit();
bool NoPermissionOrParametersCheckFail(
int32_t slotId, const std::u16string desAddr, const sptr<ISendShortMessageCallback> &sendCallback);
bool ValidDestinationAddress(std::string desAddr);
void TrimSmscAddr(std::string &sca);
void InsertSessionAndDetail(int32_t slotId, const std::string &telephone, const std::string &text);
void InsertSmsMmsInfo(int32_t slotId, uint16_t sessionId, const std::string &number, const std::string &text);
bool InsertSession(bool isNewSession, uint16_t messageCount, const std::string &number, const std::string &text);
bool QuerySessionByTelephone(const std::string &telephone, uint16_t &sectionId, uint16_t &messageCount);
bool CheckSimMessageIndexValid(int32_t slotId, uint32_t msgIndex);
private:
int64_t bindTime_ = 0;
int64_t endTime_ = 0;
int64_t spendTime_ = 0;
bool registerToService_ = false;
ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
std::shared_ptr<SmsStateHandler> smsStateHandler_;
const std::string SMS_MMS_INFO = "datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info";
const std::string SESSION = "datashare:///com.ohos.smsmmsability/sms_mms/session";
const std::string COM_OHOS_MMS = "100050";
};
} // namespace Telephony
} // namespace OHOS
#endif