1 /*
2 * Copyright (c) 2025 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_ffi.h"
17
18 #include "napi_util.h"
19 #include "short_message.h"
20 #include "sms_service_manager_client.h"
21
22 namespace OHOS {
23 namespace Telephony {
24 extern "C" {
SetCJShortMessage(CJShortMessage * res,ShortMessage * shortMessage)25 void SetCJShortMessage(CJShortMessage* res, ShortMessage* shortMessage)
26 {
27 if (res == nullptr || shortMessage == nullptr) {
28 return;
29 }
30 res->protocolId = shortMessage->GetProtocolId();
31 std::u16string smscAddress;
32 shortMessage->GetScAddress(smscAddress);
33 res->scAddress = MallocCString(NapiUtil::ToUtf8(smscAddress));
34 res->scTimestamp = shortMessage->GetScTimestamp();
35 res->status = shortMessage->GetStatus();
36 res->visibleMessageBody = MallocCString(NapiUtil::ToUtf8(shortMessage->GetVisibleMessageBody()));
37 res->visibleRawAddress = MallocCString(NapiUtil::ToUtf8(shortMessage->GetVisibleRawAddress()));
38 }
39
FfiSMSCreateMessage(CArrI32 arr,char * specification)40 CJShortMessage FfiSMSCreateMessage(CArrI32 arr, char* specification)
41 {
42 CJShortMessage res = CJShortMessage { 0 };
43 std::vector<unsigned char> pdu {};
44 for (uint32_t i = 0; i < static_cast<uint32_t>(arr.size); i++) {
45 pdu.push_back(static_cast<unsigned char>(arr.head[i]));
46 }
47 std::string specification8 = specification;
48 if (pdu.empty() || specification8.empty()) {
49 res.errCode = JS_ERROR_TELEPHONY_ARGUMENT_ERROR;
50 return res;
51 }
52 std::u16string specification16 = NapiUtil::ToUtf16(specification8);
53 auto shortMessage = new ShortMessage();
54 if (shortMessage == nullptr) {
55 res.errCode = JS_ERROR_TELEPHONY_SYSTEM_ERROR;
56 return res;
57 }
58 res.errCode = ShortMessage::CreateMessage(pdu, specification16, *shortMessage);
59 if (res.errCode == TELEPHONY_ERR_SUCCESS) {
60 res.hasReplyPath = shortMessage->HasReplyPath();
61 res.isReplaceMessage = shortMessage->IsReplaceMessage();
62 res.isSmsStatusReportMessage = shortMessage->IsSmsStatusReportMessage();
63 res.messageClass = shortMessage->GetMessageClass();
64 std::vector<unsigned char> pdu_ = shortMessage->GetPdu();
65 if (pdu_.size() == 0) {
66 delete shortMessage;
67 res.errCode = JS_ERROR_TELEPHONY_SYSTEM_ERROR;
68 return res;
69 }
70 res.pdu.head = static_cast<int32_t*>(malloc(sizeof(int32_t) * pdu_.size()));
71 if (res.pdu.head == nullptr) {
72 delete shortMessage;
73 res.errCode = JS_ERROR_TELEPHONY_SYSTEM_ERROR;
74 return res;
75 }
76 for (size_t i = 0; i < pdu_.size(); i++) {
77 res.pdu.head[i] = pdu_[i];
78 }
79 res.pdu.size = static_cast<int64_t>(pdu_.size());
80 SetCJShortMessage(&res, shortMessage);
81 }
82 delete shortMessage;
83 if (res.errCode != TELEPHONY_ERR_SUCCESS) {
84 res.errCode = ConverErrorCodeForCj(res.errCode);
85 }
86 return res;
87 }
88
FfiSMSGetDefaultSmsSlotId()89 int32_t FfiSMSGetDefaultSmsSlotId()
90 {
91 return Singleton<Telephony::SmsServiceManagerClient>::GetInstance().GetDefaultSmsSlotId();
92 }
93
FfiSMSGetDefaultSmsSimId()94 RetDataI32 FfiSMSGetDefaultSmsSimId()
95 {
96 RetDataI32 res = RetDataI32 { 0, 0 };
97 res.code =
98 ConverErrorCodeForCj(Singleton<Telephony::SmsServiceManagerClient>::GetInstance().GetDefaultSmsSimId(res.data));
99 return res;
100 }
101
FfiSMSHasSmsCapability()102 bool FfiSMSHasSmsCapability()
103 {
104 return Singleton<Telephony::SmsServiceManagerClient>::GetInstance().HasSmsCapability();
105 }
106 }
107 } // namespace Telephony
108 } // namespace OHOS