• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "telephony_permission.h"
22 
23 namespace OHOS {
24 namespace Telephony {
ImsSendMessage(int32_t slotId,const ImsMessageInfo & imsMessageInfo)25 int32_t ImsSmsProxy::ImsSendMessage(int32_t slotId, const ImsMessageInfo &imsMessageInfo)
26 {
27     if (!TelephonyPermission::CheckPermission(Permission::SEND_MESSAGES)) {
28         TELEPHONY_LOGE("[slot%{public}d]Permission denied!", slotId);
29         return TELEPHONY_ERR_PERMISSION_ERR;
30     }
31     MessageParcel in;
32     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
33     if (ret != TELEPHONY_SUCCESS) {
34         return ret;
35     }
36     if (!in.WriteRawData((const void *)&imsMessageInfo, sizeof(imsMessageInfo))) {
37         TELEPHONY_LOGE("[slot%{public}d]Write imsMessageInfo fail!", slotId);
38         return TELEPHONY_ERR_WRITE_DATA_FAIL;
39     }
40     return SendRequest(in, slotId, IMS_GET_SMS_CONFIG);
41 }
42 
ImsSetSmsConfig(int32_t slotId,int32_t imsSmsConfig)43 int32_t ImsSmsProxy::ImsSetSmsConfig(int32_t slotId, int32_t imsSmsConfig)
44 {
45     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
46         TELEPHONY_LOGE("[slot%{public}d]Permission denied!", slotId);
47         return TELEPHONY_ERR_PERMISSION_ERR;
48     }
49     MessageParcel in;
50     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
51     if (ret != TELEPHONY_SUCCESS) {
52         return ret;
53     }
54     if (!in.WriteInt32(imsSmsConfig)) {
55         TELEPHONY_LOGE("[slot%{public}d]Write imsSmsConfig fail!", slotId);
56         return TELEPHONY_ERR_WRITE_DATA_FAIL;
57     }
58     return SendRequest(in, slotId, IMS_GET_SMS_CONFIG);
59 }
60 
ImsGetSmsConfig(int32_t slotId)61 int32_t ImsSmsProxy::ImsGetSmsConfig(int32_t slotId)
62 {
63     MessageParcel in;
64     int32_t ret = WriteCommonInfo(__FUNCTION__, in, slotId);
65     if (ret != TELEPHONY_SUCCESS) {
66         return ret;
67     }
68     return SendRequest(in, slotId, IMS_GET_SMS_CONFIG);
69 }
70 
RegisterImsSmsCallback(const sptr<ImsSmsCallbackInterface> & callback)71 int32_t ImsSmsProxy::RegisterImsSmsCallback(const sptr<ImsSmsCallbackInterface> &callback)
72 {
73     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
74         TELEPHONY_LOGE("Permission denied!");
75         return TELEPHONY_ERR_PERMISSION_ERR;
76     }
77     if (callback == nullptr) {
78         TELEPHONY_LOGE("callback is nullptr!");
79         return TELEPHONY_ERR_ARGUMENT_INVALID;
80     }
81     MessageOption option;
82     MessageParcel in;
83     MessageParcel out;
84     if (!in.WriteInterfaceToken(ImsSmsProxy::GetDescriptor())) {
85         TELEPHONY_LOGE("Write descriptor token fail!");
86         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
87     }
88     if (!in.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
89         TELEPHONY_LOGE("Write ImsSmsCallbackInterface fail!");
90         return TELEPHONY_ERR_WRITE_DATA_FAIL;
91     }
92 
93     sptr<IRemoteObject> remote = Remote();
94     if (remote == nullptr) {
95         TELEPHONY_LOGE("Remote is null");
96         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
97     }
98 
99     int32_t error = remote->SendRequest(IMS_SMS_REGISTER_CALLBACK, in, 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