1 /*
2  * Copyright (C) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "ims_sms_stub.h"
16 
17 #include "telephony_errors.h"
18 #include "telephony_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Telephony {
ImsSmsStub()22 ImsSmsStub::ImsSmsStub()
23 {
24     InitFuncMap();
25 }
26 
~ImsSmsStub()27 ImsSmsStub::~ImsSmsStub() {}
28 
InitFuncMap()29 void ImsSmsStub::InitFuncMap()
30 {
31     memberFuncMap_[static_cast<uint32_t>(ImsSmsInterfaceCode::IMS_SEND_MESSAGE)] =
32         [this](MessageParcel &data, MessageParcel &reply) { return OnImsSendMessage(data, reply); };
33     memberFuncMap_[static_cast<uint32_t>(ImsSmsInterfaceCode::IMS_SET_SMS_CONFIG)] =
34         [this](MessageParcel &data, MessageParcel &reply) { return OnImsSetSmsConfig(data, reply); };
35     memberFuncMap_[static_cast<uint32_t>(ImsSmsInterfaceCode::IMS_GET_SMS_CONFIG)] =
36         [this](MessageParcel &data, MessageParcel &reply) { return OnImsGetSmsConfig(data, reply); };
37     memberFuncMap_[static_cast<uint32_t>(ImsSmsInterfaceCode::IMS_SMS_REGISTER_CALLBACK)] =
38         [this](MessageParcel &data, MessageParcel &reply) { return OnRegisterSmsCallCallback(data, reply); };
39 }
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t ImsSmsStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43     std::u16string descriptor = ImsSmsStub::GetDescriptor();
44     std::u16string remoteDescriptor = data.ReadInterfaceToken();
45     if (descriptor != remoteDescriptor) {
46         TELEPHONY_LOGE("descriptor checking fail!");
47         return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
48     }
49     TELEPHONY_LOGI("OnReceived, cmd = %{public}u", code);
50     auto itFunction = memberFuncMap_.find(code);
51     if (itFunction != memberFuncMap_.end()) {
52         auto memberFunc = itFunction->second;
53         if (memberFunc != nullptr) {
54             return memberFunc(data, reply);
55         }
56     }
57     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58 }
59 
OnImsSendMessage(MessageParcel & data,MessageParcel & reply)60 int32_t ImsSmsStub::OnImsSendMessage(MessageParcel &data, MessageParcel &reply)
61 {
62     int32_t slotId = data.ReadInt32();
63     ImsMessageInfo sendMessageInfo;
64     sendMessageInfo.refId = data.ReadInt64();
65     sendMessageInfo.smscPdu = data.ReadString();
66     sendMessageInfo.pdu = data.ReadString();
67     sendMessageInfo.tech = static_cast<const SmsRadioTechnologyFamily>(data.ReadInt32());
68     reply.WriteInt32(ImsSendMessage(slotId, sendMessageInfo));
69 
70     return TELEPHONY_SUCCESS;
71 }
72 
OnImsSetSmsConfig(MessageParcel & data,MessageParcel & reply)73 int32_t ImsSmsStub::OnImsSetSmsConfig(MessageParcel &data, MessageParcel &reply)
74 {
75     int32_t slotId = data.ReadInt32();
76     int32_t imsSmsConfig = data.ReadInt32();
77     reply.WriteInt32(ImsSetSmsConfig(slotId, imsSmsConfig));
78 
79     return TELEPHONY_SUCCESS;
80 }
81 
OnImsGetSmsConfig(MessageParcel & data,MessageParcel & reply)82 int32_t ImsSmsStub::OnImsGetSmsConfig(MessageParcel &data, MessageParcel &reply)
83 {
84     int32_t slotId = data.ReadInt32();
85     reply.WriteInt32(ImsGetSmsConfig(slotId));
86 
87     return TELEPHONY_SUCCESS;
88 }
89 
OnRegisterSmsCallCallback(MessageParcel & data,MessageParcel & reply)90 int32_t ImsSmsStub::OnRegisterSmsCallCallback(MessageParcel &data, MessageParcel &reply)
91 {
92     auto remote = data.ReadRemoteObject();
93     int32_t result = TELEPHONY_ERR_LOCAL_PTR_NULL;
94     if (remote == nullptr) {
95         TELEPHONY_LOGE("ImsSmsCallback is nullptr");
96         reply.WriteInt32(result);
97         return result;
98     }
99     result = RegisterImsSmsCallback(iface_cast<ImsSmsCallbackInterface>(remote));
100     reply.WriteInt32(result);
101     return TELEPHONY_SUCCESS;
102 }
103 } // namespace Telephony
104 } // namespace OHOS
105