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 
16 #include "ims_core_service_proxy.h"
17 
18 #include "message_option.h"
19 #include "message_parcel.h"
20 #include "telephony_errors.h"
21 #include "telephony_permission.h"
22 
23 namespace OHOS {
24 namespace Telephony {
GetImsRegistrationStatus(int32_t slotId)25 int32_t ImsCoreServiceProxy::GetImsRegistrationStatus(int32_t slotId)
26 {
27     MessageParcel in;
28     if (!in.WriteInterfaceToken(ImsCoreServiceProxy::GetDescriptor())) {
29         TELEPHONY_LOGE("[slot%{public}d]Write descriptor token fail!", slotId);
30         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
31     }
32     if (!in.WriteInt32(slotId)) {
33         TELEPHONY_LOGE("[slot%{public}d]Write slotId fail!", slotId);
34         return TELEPHONY_ERR_WRITE_DATA_FAIL;
35     }
36     auto remote = Remote();
37     if (remote == nullptr) {
38         TELEPHONY_LOGE("[slot%{public}d]Remote is null", slotId);
39         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
40     }
41     MessageParcel out;
42     MessageOption option;
43     int32_t error = remote->SendRequest(static_cast<int32_t>(ImsCoreServiceInterfaceCode::IMS_GET_REGISTRATION_STATUS),
44         in, out, option);
45     if (error == ERR_NONE) {
46         return out.ReadInt32();
47     }
48     TELEPHONY_LOGE("[slot%{public}d]SendRequest fail, error:%{public}d", slotId, error);
49     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
50 }
51 
GetPhoneNumberFromIMPU(int32_t slotId,std::string & phoneNumber)52 int32_t ImsCoreServiceProxy::GetPhoneNumberFromIMPU(int32_t slotId, std::string &phoneNumber)
53 {
54     MessageParcel in;
55     if (!in.WriteInterfaceToken(ImsCoreServiceProxy::GetDescriptor())) {
56         TELEPHONY_LOGE("[slot%{public}d]Write descriptor token fail!", slotId);
57         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
58     }
59     if (!in.WriteInt32(slotId)) {
60         TELEPHONY_LOGE("[slot%{public}d]Write slotId fail!", slotId);
61         return TELEPHONY_ERR_WRITE_DATA_FAIL;
62     }
63     auto remote = Remote();
64     if (remote == nullptr) {
65         TELEPHONY_LOGE("[slot%{public}d]Remote is null", slotId);
66         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
67     }
68     MessageParcel out;
69     MessageOption option;
70     int32_t error = remote->SendRequest(
71         static_cast<int32_t>(ImsCoreServiceInterfaceCode::IMS_GET_PHONE_NUMBER_FROM_IMPU), in, out, option);
72     if (error != ERR_NONE) {
73         TELEPHONY_LOGE("[slot%{public}d]SendRequest fail, error:%{public}d", slotId, error);
74     }
75     int32_t result = out.ReadInt32();
76     phoneNumber = out.ReadString();
77     return result;
78 }
79 
RegisterImsCoreServiceCallback(const sptr<ImsCoreServiceCallbackInterface> & callback)80 int32_t ImsCoreServiceProxy::RegisterImsCoreServiceCallback(const sptr<ImsCoreServiceCallbackInterface> &callback)
81 {
82     if (callback == nullptr) {
83         TELEPHONY_LOGE("callback is nullptr!");
84         return TELEPHONY_ERR_ARGUMENT_INVALID;
85     }
86     MessageParcel in;
87     if (!in.WriteInterfaceToken(ImsCoreServiceProxy::GetDescriptor())) {
88         TELEPHONY_LOGE("Write descriptor token fail!");
89         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
90     }
91     if (!in.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
92         TELEPHONY_LOGE("Write ImsCoreServiceCallbackInterface fail!");
93         return TELEPHONY_ERR_WRITE_DATA_FAIL;
94     }
95     auto remote = Remote();
96     if (remote == nullptr) {
97         TELEPHONY_LOGE("Remote is null");
98         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
99     }
100     MessageParcel out;
101     MessageOption option;
102     int32_t error = remote->SendRequest(static_cast<int32_t>(ImsCoreServiceInterfaceCode::IMS_REGISTER_CALLBACK), in,
103         out, option);
104     if (error == ERR_NONE) {
105         return out.ReadInt32();
106     }
107     TELEPHONY_LOGE("SendRequest fail, error:%{public}d", error);
108     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
109 }
110 
GetProxyObjectPtr(ImsServiceProxyType proxyType)111 sptr<IRemoteObject> ImsCoreServiceProxy::GetProxyObjectPtr(ImsServiceProxyType proxyType)
112 {
113     MessageParcel dataParcel;
114     if (!dataParcel.WriteInterfaceToken(ImsCoreServiceProxy::GetDescriptor())) {
115         TELEPHONY_LOGE("write descriptor fail");
116         return nullptr;
117     }
118     dataParcel.WriteInt32(static_cast<int32_t>(proxyType));
119     auto remote = Remote();
120     if (remote == nullptr) {
121         TELEPHONY_LOGE("function Remote() return nullptr!");
122         return nullptr;
123     }
124     MessageParcel replyParcel;
125     MessageOption option;
126     int32_t error = remote->SendRequest(static_cast<int32_t>(ImsCoreServiceInterfaceCode::IMS_GET_PROXY_OBJECT_PTR),
127         dataParcel, replyParcel, option);
128     if (error != TELEPHONY_SUCCESS) {
129         TELEPHONY_LOGE("function GetProxyObjectPtr failed! errCode:%{public}d", error);
130         return nullptr;
131     }
132     return replyParcel.ReadRemoteObject();
133 }
134 } // namespace Telephony
135 } // namespace OHOS
136