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_sms_proxy.h"
17 
18 #include "message_option.h"
19 #include "message_parcel.h"
20 #include "telephony_errors.h"
21 
22 namespace OHOS {
23 namespace Telephony {
ImsSendMessage(int32_t slotId,const ImsMessageInfo & imsMessageInfo)24 int32_t ImsSmsProxy::ImsSendMessage(int32_t slotId, const ImsMessageInfo &imsMessageInfo)
25 {
26     MessageParcel in;
27     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
28     if (ret != TELEPHONY_SUCCESS) {
29         return ret;
30     }
31     if (!in.WriteInt64(imsMessageInfo.refId)) {
32         TELEPHONY_LOGE("[slot%{public}d]Write imsMessageInfo fail!", slotId);
33         return TELEPHONY_ERR_WRITE_DATA_FAIL;
34     }
35     if (!in.WriteString(imsMessageInfo.smscPdu)) {
36         TELEPHONY_LOGE("[slot%{public}d]Write imsMessageInfo fail!", slotId);
37         return TELEPHONY_ERR_WRITE_DATA_FAIL;
38     }
39     if (!in.WriteString(imsMessageInfo.pdu)) {
40         TELEPHONY_LOGE("[slot%{public}d]Write imsMessageInfo fail!", slotId);
41         return TELEPHONY_ERR_WRITE_DATA_FAIL;
42     }
43     if (!in.WriteInt32(imsMessageInfo.tech)) {
44         TELEPHONY_LOGE("[slot%{public}d]Write imsMessageInfo fail!", slotId);
45         return TELEPHONY_ERR_WRITE_DATA_FAIL;
46     }
47     return SendRequest(in, slotId, static_cast<int32_t>(ImsSmsInterfaceCode::IMS_SEND_MESSAGE));
48 }
49 
ImsSetSmsConfig(int32_t slotId,int32_t imsSmsConfig)50 int32_t ImsSmsProxy::ImsSetSmsConfig(int32_t slotId, int32_t imsSmsConfig)
51 {
52     MessageParcel in;
53     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
54     if (ret != TELEPHONY_SUCCESS) {
55         return ret;
56     }
57     if (!in.WriteInt32(imsSmsConfig)) {
58         TELEPHONY_LOGE("[slot%{public}d]Write imsSmsConfig fail!", slotId);
59         return TELEPHONY_ERR_WRITE_DATA_FAIL;
60     }
61     return SendRequest(in, slotId, static_cast<int32_t>(ImsSmsInterfaceCode::IMS_SET_SMS_CONFIG));
62 }
63 
ImsGetSmsConfig(int32_t slotId)64 int32_t ImsSmsProxy::ImsGetSmsConfig(int32_t slotId)
65 {
66     MessageParcel in;
67     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
68     if (ret != TELEPHONY_SUCCESS) {
69         return ret;
70     }
71     return SendRequest(in, slotId, static_cast<int32_t>(ImsSmsInterfaceCode::IMS_GET_SMS_CONFIG));
72 }
73 
RegisterImsSmsCallback(const sptr<ImsSmsCallbackInterface> & callback)74 int32_t ImsSmsProxy::RegisterImsSmsCallback(const sptr<ImsSmsCallbackInterface> &callback)
75 {
76     if (callback == nullptr) {
77         TELEPHONY_LOGE("callback is nullptr!");
78         return TELEPHONY_ERR_ARGUMENT_INVALID;
79     }
80     MessageOption option;
81     MessageParcel in;
82     MessageParcel out;
83     if (!in.WriteInterfaceToken(ImsSmsProxy::GetDescriptor())) {
84         TELEPHONY_LOGE("Write descriptor token fail!");
85         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
86     }
87     if (!in.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
88         TELEPHONY_LOGE("Write ImsSmsCallbackInterface fail!");
89         return TELEPHONY_ERR_WRITE_DATA_FAIL;
90     }
91 
92     sptr<IRemoteObject> remote = Remote();
93     if (remote == nullptr) {
94         TELEPHONY_LOGE("Remote is null");
95         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
96     }
97 
98     int32_t error = remote->SendRequest(static_cast<int32_t>(ImsSmsInterfaceCode::IMS_SMS_REGISTER_CALLBACK), in,
99         out, option);
100     if (error == ERR_NONE) {
101         return out.ReadInt32();
102     }
103     TELEPHONY_LOGE("SendRequest fail, error:%{public}d", error);
104     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
105 }
106 
WriteCommonInfo(std::string funcName,MessageParcel & in,int32_t slotId)107 int32_t ImsSmsProxy::WriteCommonInfo(std::string funcName, MessageParcel &in, int32_t slotId)
108 {
109     if (!in.WriteInterfaceToken(ImsSmsProxy::GetDescriptor())) {
110         TELEPHONY_LOGE("[slot%{public}d] %{public}s Write descriptor token fail!", slotId, funcName.c_str());
111         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
112     }
113     if (!in.WriteInt32(slotId)) {
114         TELEPHONY_LOGE("[slot%{public}d] %{public}s Write slotId fail!", slotId, funcName.c_str());
115         return TELEPHONY_ERR_WRITE_DATA_FAIL;
116     }
117     return TELEPHONY_SUCCESS;
118 }
119 
SendRequest(MessageParcel & in,int32_t slotId,int32_t eventId)120 int32_t ImsSmsProxy::SendRequest(MessageParcel &in, int32_t slotId, int32_t eventId)
121 {
122     MessageParcel out;
123     MessageOption option;
124 
125     sptr<IRemoteObject> remote = Remote();
126     if (remote == nullptr) {
127         TELEPHONY_LOGE("[slot%{public}d]Remote is null, eventId:%{public}d", slotId, eventId);
128         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
129     }
130 
131     int32_t error = remote->SendRequest(eventId, in, out, option);
132     if (error == ERR_NONE) {
133         return out.ReadInt32();
134     }
135     TELEPHONY_LOGE("[slot%{public}d]SendRequest fail, eventId:%{public}d, error:%{public}d", slotId, eventId, error);
136     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
137 }
138 } // namespace Telephony
139 } // namespace OHOS
140