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_callback_proxy.h"
17 
18 #include "message_option.h"
19 #include "message_parcel.h"
20 
21 namespace OHOS {
22 namespace Telephony {
ImsSmsCallbackProxy(const sptr<IRemoteObject> & impl)23 ImsSmsCallbackProxy::ImsSmsCallbackProxy(const sptr<IRemoteObject> &impl)
24     : IRemoteProxy<ImsSmsCallbackInterface>(impl) {}
25 
ImsSendMessageResponse(int32_t slotId,const SendSmsResultInfo & result)26 int32_t ImsSmsCallbackProxy::ImsSendMessageResponse(int32_t slotId, const SendSmsResultInfo &result)
27 {
28     MessageParcel in;
29     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
30     if (ret != TELEPHONY_SUCCESS) {
31         return ret;
32     }
33     if (!in.WriteRawData((const void *)&result, sizeof(SendSmsResultInfo))) {
34         TELEPHONY_LOGE("[slot%{public}d]Write SendSmsResultInfo fail!", slotId);
35         return TELEPHONY_ERR_WRITE_DATA_FAIL;
36     }
37     return SendRequest(in, slotId, static_cast<int32_t>(ImsSmsCallbackInterfaceCode::IMS_SEND_MESSAGE));
38 }
39 
ImsSendMessageResponse(int32_t slotId,const RadioResponseInfo & info)40 int32_t ImsSmsCallbackProxy::ImsSendMessageResponse(int32_t slotId, const RadioResponseInfo &info)
41 {
42     return SendHRilRadioResponseInfo(__FUNCTION__, slotId,
43         static_cast<int32_t>(ImsSmsCallbackInterfaceCode::IMS_SEND_MESSAGE), info);
44 }
45 
ImsSetSmsConfigResponse(int32_t slotId,const RadioResponseInfo & info)46 int32_t ImsSmsCallbackProxy::ImsSetSmsConfigResponse(int32_t slotId, const RadioResponseInfo &info)
47 {
48     return SendHRilRadioResponseInfo(__FUNCTION__, slotId,
49         static_cast<int32_t>(ImsSmsCallbackInterfaceCode::IMS_SET_SMS_CONFIG), info);
50 }
51 
ImsGetSmsConfigResponse(int32_t slotId,int32_t imsSmsConfig)52 int32_t ImsSmsCallbackProxy::ImsGetSmsConfigResponse(int32_t slotId, int32_t imsSmsConfig)
53 {
54     MessageParcel in;
55     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
56     if (ret != TELEPHONY_SUCCESS) {
57         return ret;
58     }
59     if (!in.WriteInt32(imsSmsConfig)) {
60         TELEPHONY_LOGE("[slot%{public}d]Write imsSmsConfig fail!", slotId);
61         return TELEPHONY_ERR_WRITE_DATA_FAIL;
62     }
63     return SendRequest(in, slotId, static_cast<int32_t>(ImsSmsCallbackInterfaceCode::IMS_GET_SMS_CONFIG));
64 }
65 
ImsGetSmsConfigResponse(int32_t slotId,const RadioResponseInfo & info)66 int32_t ImsSmsCallbackProxy::ImsGetSmsConfigResponse(int32_t slotId, const RadioResponseInfo &info)
67 {
68     return SendHRilRadioResponseInfo(__FUNCTION__, slotId,
69         static_cast<int32_t>(ImsSmsCallbackInterfaceCode::IMS_GET_SMS_CONFIG), info);
70 }
71 
WriteCommonInfo(std::string funcName,MessageParcel & in,int32_t slotId)72 int32_t ImsSmsCallbackProxy::WriteCommonInfo(std::string funcName, MessageParcel &in, int32_t slotId)
73 {
74     if (!in.WriteInterfaceToken(ImsSmsCallbackProxy::GetDescriptor())) {
75         TELEPHONY_LOGE("[slot%{public}d] %{public}s Write descriptor token fail!", slotId, funcName.c_str());
76         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
77     }
78     if (!in.WriteInt32(slotId)) {
79         TELEPHONY_LOGE("[slot%{public}d] %{public}s Write slotId fail!", slotId, funcName.c_str());
80         return TELEPHONY_ERR_WRITE_DATA_FAIL;
81     }
82     return TELEPHONY_SUCCESS;
83 }
84 
SendHRilRadioResponseInfo(std::string funcName,int32_t slotId,int32_t eventId,const RadioResponseInfo & info)85 int32_t ImsSmsCallbackProxy::SendHRilRadioResponseInfo(
86     std::string funcName, int32_t slotId, int32_t eventId, const RadioResponseInfo &info)
87 {
88     TELEPHONY_LOGI("[slot%{public}d]Send RadioResponseInfo for eventId:%{public}d", slotId, eventId);
89     MessageParcel in;
90     int32_t ret = WriteCommonInfo(funcName, in, slotId);
91     if (ret != TELEPHONY_SUCCESS) {
92         return ret;
93     }
94     if (!in.WriteRawData((const void *)&info, sizeof(RadioResponseInfo))) {
95         TELEPHONY_LOGE("[slot%{public}d]Write RadioResponseInfo fail! eventId:%{public}d", slotId, eventId);
96         return TELEPHONY_ERR_WRITE_DATA_FAIL;
97     }
98     return SendRequest(in, slotId, eventId);
99 }
100 
SendRequest(MessageParcel & in,int32_t slotId,int32_t eventId)101 int32_t ImsSmsCallbackProxy::SendRequest(MessageParcel &in, int32_t slotId, int32_t eventId)
102 {
103     MessageParcel out;
104     MessageOption option;
105 
106     sptr<IRemoteObject> remote = Remote();
107     if (remote == nullptr) {
108         TELEPHONY_LOGE("[slot%{public}d]Remote is null, eventId:%{public}d", slotId, eventId);
109         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
110     }
111 
112     int32_t error = remote->SendRequest(eventId, in, out, option);
113     if (error == ERR_NONE) {
114         return out.ReadInt32();
115     }
116     TELEPHONY_LOGE("[slot%{public}d]SendRequest fail, eventId:%{public}d, error:%{public}d", slotId, eventId, error);
117     return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
118 }
119 } // namespace Telephony
120 } // namespace OHOS
121