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_core_service_stub.h"
16 
17 #include "telephony_errors.h"
18 #include "telephony_permission.h"
19 
20 namespace OHOS {
21 namespace Telephony {
ImsCoreServiceStub()22 ImsCoreServiceStub::ImsCoreServiceStub()
23 {
24     InitMemberFuncMap();
25 }
26 
~ImsCoreServiceStub()27 ImsCoreServiceStub::~ImsCoreServiceStub() {}
28 
InitMemberFuncMap()29 void ImsCoreServiceStub::InitMemberFuncMap()
30 {
31     memberFuncMap_[ImsCoreServiceInterfaceCode::IMS_GET_REGISTRATION_STATUS] =
32         [this](MessageParcel &data, MessageParcel &reply) { return OnGetImsRegistrationStatus(data, reply); };
33 
34     /* ------------ callback ------------- */
35     memberFuncMap_[ImsCoreServiceInterfaceCode::IMS_REGISTER_CALLBACK] =
36         [this](MessageParcel &data, MessageParcel &reply) { return OnRegisterImsCoreServiceCallback(data, reply); };
37     memberFuncMap_[ImsCoreServiceInterfaceCode::IMS_GET_PROXY_OBJECT_PTR] =
38         [this](MessageParcel &data, MessageParcel &reply) { return OnGetProxyObjectPtr(data, reply); };
39 }
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t ImsCoreServiceStub::OnRemoteRequest(
42     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
43 {
44     if (!TelephonyPermission::CheckPermission(Permission::CONNECT_IMS_SERVICE)) {
45         TELEPHONY_LOGE("Permission denied!");
46         return TELEPHONY_ERR_PERMISSION_ERR;
47     }
48     std::u16string serviceDescriptor = ImsCoreServiceStub::GetDescriptor();
49     std::u16string remoteDescriptor = data.ReadInterfaceToken();
50     if (serviceDescriptor != remoteDescriptor) {
51         TELEPHONY_LOGE("descriptor checking fail!");
52         return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
53     }
54     TELEPHONY_LOGI("OnReceived, cmd = %{public}u", code);
55     auto itFunction = memberFuncMap_.find(code);
56     if (itFunction != memberFuncMap_.end()) {
57         auto memberFunc = itFunction->second;
58         if (memberFunc != nullptr) {
59             return memberFunc(data, reply);
60         }
61     }
62     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
63 }
64 
OnGetImsRegistrationStatus(MessageParcel & data,MessageParcel & reply)65 int32_t ImsCoreServiceStub::OnGetImsRegistrationStatus(MessageParcel &data, MessageParcel &reply)
66 {
67     TELEPHONY_LOGI("ImsCoreServiceStub::OnGetImsRegistrationStatus enter");
68     int32_t slotId = data.ReadInt32();
69     reply.WriteInt32(GetImsRegistrationStatus(slotId));
70     return TELEPHONY_SUCCESS;
71 }
72 
OnRegisterImsCoreServiceCallback(MessageParcel & data,MessageParcel & reply)73 int32_t ImsCoreServiceStub::OnRegisterImsCoreServiceCallback(MessageParcel &data, MessageParcel &reply)
74 {
75     TELEPHONY_LOGI("ImsCoreServiceStub::OnRegisterImsCoreServiceCallback enter");
76 
77     int32_t result = TELEPHONY_ERR_LOCAL_PTR_NULL;
78     auto remote = data.ReadRemoteObject();
79     if (remote == nullptr) {
80         TELEPHONY_LOGE("ImsCoreServiceStub::OnRegisterImsCoreServiceCallback return remote is nullptr");
81         reply.WriteInt32(result);
82         return result;
83     }
84     result = RegisterImsCoreServiceCallback(iface_cast<ImsCoreServiceCallbackInterface>(remote));
85     reply.WriteInt32(result);
86     return TELEPHONY_SUCCESS;
87 }
88 
OnGetProxyObjectPtr(MessageParcel & data,MessageParcel & reply)89 int32_t ImsCoreServiceStub::OnGetProxyObjectPtr(MessageParcel &data, MessageParcel &reply)
90 {
91     ImsServiceProxyType proxyType = static_cast<ImsServiceProxyType>(data.ReadInt32());
92     sptr<IRemoteObject> object = GetProxyObjectPtr(proxyType);
93     if (!reply.WriteRemoteObject(object)) {
94         TELEPHONY_LOGE("OnGetProxyObjectPtr fail to write parcel");
95         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
96     }
97     return TELEPHONY_SUCCESS;
98 }
99 } // namespace Telephony
100 } // namespace OHOS
101