1 /*
2 * Copyright (c) 2021-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 <unistd.h>
17
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "distributed_notification_service_ipc_interface_code.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "parcel.h"
25 #include "ans_manager_proxy.h"
26
27 namespace OHOS {
28 namespace Notification {
InnerTransact(NotificationInterfaceCode code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)29 ErrCode AnsManagerProxy::InnerTransact(NotificationInterfaceCode code,
30 MessageOption &flags, MessageParcel &data, MessageParcel &reply)
31 {
32 auto remote = Remote();
33 if (remote == nullptr) {
34 ANS_LOGE("[InnerTransact] defeat: get Remote defeat code %{public}u", code);
35 return ERR_DEAD_OBJECT;
36 }
37 int32_t err = remote->SendRequest(static_cast<uint32_t>(code), data, reply, flags);
38 switch (err) {
39 case NO_ERROR: {
40 return ERR_OK;
41 }
42 case DEAD_OBJECT: {
43 ANS_LOGE("[InnerTransact] defeat: ipcErr=%{public}d code %{public}d", err, code);
44 return ERR_DEAD_OBJECT;
45 }
46 default: {
47 ANS_LOGE("[InnerTransact] defeat: ipcErr=%{public}d code %{public}d", err, code);
48 return ERR_ANS_TRANSACT_FAILED;
49 }
50 }
51 }
52
RegisterPushCallback(const sptr<IRemoteObject> & pushCallback,const sptr<NotificationCheckRequest> & notificationCheckRequest)53 ErrCode AnsManagerProxy::RegisterPushCallback(
54 const sptr<IRemoteObject> &pushCallback, const sptr<NotificationCheckRequest> ¬ificationCheckRequest)
55 {
56 MessageParcel data;
57 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
58 ANS_LOGE("write interface token failed.");
59 return ERR_ANS_PARCELABLE_FAILED;
60 }
61
62 if (!data.WriteRemoteObject(pushCallback)) {
63 ANS_LOGE("write pushCallback failed.");
64 return ERR_ANS_PARCELABLE_FAILED;
65 }
66
67 if (!data.WriteParcelable(notificationCheckRequest)) {
68 ANS_LOGE("write notificationCheckRequest failed.");
69 return ERR_ANS_PARCELABLE_FAILED;
70 }
71
72 MessageParcel reply;
73 MessageOption option = { MessageOption::TF_SYNC };
74 ErrCode result = InnerTransact(NotificationInterfaceCode::REGISTER_PUSH_CALLBACK, option, data, reply);
75 if (result != ERR_OK) {
76 ANS_LOGE("transact ErrCode=%{public}d", result);
77 return ERR_ANS_TRANSACT_FAILED;
78 }
79
80 if (!reply.ReadInt32(result)) {
81 ANS_LOGE("fail: read result failed.");
82 return ERR_ANS_PARCELABLE_FAILED;
83 }
84
85 return result;
86 }
87
UnregisterPushCallback()88 ErrCode AnsManagerProxy::UnregisterPushCallback()
89 {
90 MessageParcel data;
91 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
92 ANS_LOGE("write interface token failed.");
93 return ERR_ANS_PARCELABLE_FAILED;
94 }
95
96 MessageParcel reply;
97 MessageOption option = { MessageOption::TF_SYNC };
98 ErrCode result = InnerTransact(NotificationInterfaceCode::UNREGISTER_PUSH_CALLBACK, option, data, reply);
99 if (result != ERR_OK) {
100 ANS_LOGE("transact ErrCode=%{public}d", result);
101 return ERR_ANS_TRANSACT_FAILED;
102 }
103
104 if (!reply.ReadInt32(result)) {
105 ANS_LOGE("read result failed.");
106 return ERR_ANS_PARCELABLE_FAILED;
107 }
108
109 return result;
110 }
111
SetTargetDeviceStatus(const std::string & deviceType,const uint32_t status)112 ErrCode AnsManagerProxy::SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status)
113 {
114 MessageParcel data;
115 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
116 ANS_LOGE("Set package config fail: write interface token failed.");
117 return ERR_ANS_PARCELABLE_FAILED;
118 }
119
120 if (!data.WriteString(deviceType)) {
121 ANS_LOGE("Set package config fail:: write deviceType failed.");
122 return ERR_ANS_PARCELABLE_FAILED;
123 }
124
125 if (!data.WriteInt32(status)) {
126 ANS_LOGE("Set package config fail:: write status failed.");
127 return ERR_ANS_PARCELABLE_FAILED;
128 }
129
130 MessageParcel reply;
131 MessageOption option = {MessageOption::TF_SYNC};
132 ErrCode result = InnerTransact(NotificationInterfaceCode::SET_TARGET_DEVICE_STATUS, option, data, reply);
133 if (result != ERR_OK) {
134 ANS_LOGE("Transact fail: transact ErrCode=%{public}d", result);
135 return ERR_ANS_TRANSACT_FAILED;
136 }
137
138 if (!reply.ReadInt32(result)) {
139 ANS_LOGE("Set package config fail: read result failed.");
140 return ERR_ANS_PARCELABLE_FAILED;
141 }
142
143 return result;
144 }
145
SetTargetDeviceStatus(const std::string & deviceType,const uint32_t status,const uint32_t controlFlag)146 ErrCode AnsManagerProxy::SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status,
147 const uint32_t controlFlag)
148 {
149 MessageParcel data;
150 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
151 ANS_LOGE("Set package config fail: write interface token failed.");
152 return ERR_ANS_PARCELABLE_FAILED;
153 }
154
155 if (!data.WriteString(deviceType)) {
156 ANS_LOGE("Set package config fail:: write deviceType failed.");
157 return ERR_ANS_PARCELABLE_FAILED;
158 }
159
160 if (!data.WriteInt32(status)) {
161 ANS_LOGE("Set package config fail:: write status failed.");
162 return ERR_ANS_PARCELABLE_FAILED;
163 }
164
165 if (!data.WriteInt32(controlFlag)) {
166 ANS_LOGE("Set package config fail:: write controlFlag failed.");
167 return ERR_ANS_PARCELABLE_FAILED;
168 }
169
170 MessageParcel reply;
171 MessageOption option = {MessageOption::TF_SYNC};
172 ErrCode result = InnerTransact(NotificationInterfaceCode::SET_TARGET_DEVICE_STATUS_WITH_FLAG, option, data, reply);
173 if (result != ERR_OK) {
174 ANS_LOGE("Transact fail: transact ErrCode=%{public}d", result);
175 return ERR_ANS_TRANSACT_FAILED;
176 }
177
178 if (!reply.ReadInt32(result)) {
179 ANS_LOGE("Set package config fail: read result failed.");
180 return ERR_ANS_PARCELABLE_FAILED;
181 }
182
183 return result;
184 }
185
GetTargetDeviceStatus(const std::string & deviceType,int32_t & status)186 ErrCode AnsManagerProxy::GetTargetDeviceStatus(const std::string &deviceType, int32_t &status)
187 {
188 MessageParcel data;
189 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
190 ANS_LOGE("Set package config fail: write interface token failed.");
191 return ERR_ANS_PARCELABLE_FAILED;
192 }
193
194 if (!data.WriteString(deviceType)) {
195 ANS_LOGE("Set package config fail:: write deviceType failed.");
196 return ERR_ANS_PARCELABLE_FAILED;
197 }
198
199 MessageParcel reply;
200 MessageOption option = {MessageOption::TF_SYNC};
201 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_TARGET_DEVICE_STATUS, option, data, reply);
202 if (result != ERR_OK) {
203 ANS_LOGE("Transact fail: transact ErrCode=%{public}d", result);
204 return ERR_ANS_TRANSACT_FAILED;
205 }
206
207 if (!reply.ReadInt32(result)) {
208 ANS_LOGE("Set package config fail: read result failed.");
209 return ERR_ANS_PARCELABLE_FAILED;
210 }
211
212 if (result == ERR_OK) {
213 status = reply.ReadInt32();
214 }
215
216 return result;
217 }
218
SetAdditionConfig(const std::string & key,const std::string & value)219 ErrCode AnsManagerProxy::SetAdditionConfig(const std::string &key, const std::string &value)
220 {
221 MessageParcel data;
222 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
223 ANS_LOGE("Set package config fail: write interface token failed.");
224 return ERR_ANS_PARCELABLE_FAILED;
225 }
226
227 if (!data.WriteString(key)) {
228 ANS_LOGE("Set package config fail:: write key failed.");
229 return ERR_ANS_PARCELABLE_FAILED;
230 }
231
232 if (!data.WriteString(value)) {
233 ANS_LOGE("Set package config fail:: write value failed.");
234 return ERR_ANS_PARCELABLE_FAILED;
235 }
236
237 MessageParcel reply;
238 MessageOption option = {MessageOption::TF_SYNC};
239 ErrCode result = InnerTransact(NotificationInterfaceCode::SET_NOTIFICATION_AGENT_RELATIONSHIP, option, data, reply);
240 if (result != ERR_OK) {
241 ANS_LOGE("Transact fail: transact ErrCode=%{public}d", result);
242 return ERR_ANS_TRANSACT_FAILED;
243 }
244
245 if (!reply.ReadInt32(result)) {
246 ANS_LOGE("Set package config fail: read result failed.");
247 return ERR_ANS_PARCELABLE_FAILED;
248 }
249
250 return result;
251 }
252
253 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
RegisterSwingCallback(const sptr<IRemoteObject> & swingCallback)254 ErrCode AnsManagerProxy::RegisterSwingCallback(const sptr<IRemoteObject> &swingCallback)
255 {
256 MessageParcel data;
257 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
258 ANS_LOGE("write interface token failed.");
259 return ERR_ANS_PARCELABLE_FAILED;
260 }
261
262 if (!data.WriteRemoteObject(swingCallback)) {
263 ANS_LOGE("write swingCallback failed.");
264 return ERR_ANS_PARCELABLE_FAILED;
265 }
266
267 MessageParcel reply;
268 MessageOption option = { MessageOption::TF_SYNC };
269 ErrCode result = InnerTransact(NotificationInterfaceCode::REGISTER_SWING_CALLBACK, option, data, reply);
270 if (result != ERR_OK) {
271 ANS_LOGE("transact ErrCode=%{public}d", result);
272 return ERR_ANS_TRANSACT_FAILED;
273 }
274
275 if (!reply.ReadInt32(result)) {
276 ANS_LOGE("fail: read result failed.");
277 return ERR_ANS_PARCELABLE_FAILED;
278 }
279
280 return result;
281 }
282 #endif
283
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)284 ErrCode AnsManagerProxy::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
285 {
286 ANS_LOGD("enter");
287 MessageParcel data;
288 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
289 ANS_LOGE("[SetSmartReminderEnabled] fail: write interface token failed.");
290 return ERR_ANS_PARCELABLE_FAILED;
291 }
292
293 if (!data.WriteString(deviceType)) {
294 ANS_LOGE("[SetSmartReminderEnabled] fail: write deviceType failed");
295 return ERR_ANS_PARCELABLE_FAILED;
296 }
297
298 if (!data.WriteBool(enabled)) {
299 ANS_LOGE("[SetSmartReminderEnabled] fail: write enabled failed");
300 return ERR_ANS_PARCELABLE_FAILED;
301 }
302
303 MessageParcel reply;
304 MessageOption option = {MessageOption::TF_SYNC};
305 ErrCode result = InnerTransact(NotificationInterfaceCode::SET_SMART_REMINDER_ENABLED, option, data, reply);
306 if (result != ERR_OK) {
307 ANS_LOGE("[SetSmartReminderEnabled] fail: transact ErrCode=%{public}d", result);
308 return ERR_ANS_TRANSACT_FAILED;
309 }
310
311 if (!reply.ReadInt32(result)) {
312 ANS_LOGE("[SetSmartReminderEnabled] fail: read result failed.");
313 return ERR_ANS_PARCELABLE_FAILED;
314 }
315
316 return result;
317 }
318
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)319 ErrCode AnsManagerProxy::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
320 {
321 ANS_LOGD("enter");
322 MessageParcel data;
323 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
324 ANS_LOGE("[IsSmartReminderEnabled] fail: write interface token failed.");
325 return ERR_ANS_PARCELABLE_FAILED;
326 }
327
328 if (!data.WriteString(deviceType)) {
329 ANS_LOGE("[IsSmartReminderEnabled] fail: write deviceType failed");
330 return ERR_ANS_PARCELABLE_FAILED;
331 }
332
333 MessageParcel reply;
334 MessageOption option = {MessageOption::TF_SYNC};
335 ErrCode result = InnerTransact(NotificationInterfaceCode::GET_SMART_REMINDER_ENABLED, option, data, reply);
336 if (result != ERR_OK) {
337 ANS_LOGE("[IsSmartReminderEnabled] fail: transact ErrCode=%{public}d", result);
338 return ERR_ANS_TRANSACT_FAILED;
339 }
340
341 if (!reply.ReadInt32(result)) {
342 ANS_LOGE("[IsSmartReminderEnabled] fail: read result failed.");
343 return ERR_ANS_PARCELABLE_FAILED;
344 }
345
346 if (!reply.ReadBool(enabled)) {
347 ANS_LOGE("[IsSmartReminderEnabled] fail: read enabled failed.");
348 return ERR_ANS_PARCELABLE_FAILED;
349 }
350
351 return result;
352 }
353
AllowUseReminder(const std::string & bundleName,bool & isAllowUseReminder)354 ErrCode AnsManagerProxy::AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder)
355 {
356 ANS_LOGD("enter");
357 MessageParcel data;
358 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
359 ANS_LOGE("fail: write interface token failed.");
360 return ERR_ANS_PARCELABLE_FAILED;
361 }
362
363 if (!data.WriteString(bundleName)) {
364 ANS_LOGE("fail: write bundleName failed");
365 return ERR_ANS_PARCELABLE_FAILED;
366 }
367
368 MessageParcel reply;
369 MessageOption option = {MessageOption::TF_SYNC};
370 ErrCode result = InnerTransact(NotificationInterfaceCode::ALLOW_USE_REMINDER, option, data, reply);
371 if (result != ERR_OK) {
372 ANS_LOGE("fail: transact ErrCode=%{public}d", result);
373 return ERR_ANS_TRANSACT_FAILED;
374 }
375
376 if (!reply.ReadInt32(result)) {
377 ANS_LOGE("fail: read result failed.");
378 return ERR_ANS_PARCELABLE_FAILED;
379 }
380
381 if (!reply.ReadBool(isAllowUseReminder)) {
382 ANS_LOGE("fail: read enabled failed.");
383 return ERR_ANS_PARCELABLE_FAILED;
384 }
385
386 return result;
387 }
388
ShellDump(const std::string & cmd,const std::string & bundle,int32_t userId,int32_t recvUserId,std::vector<std::string> & dumpInfo)389 ErrCode AnsManagerProxy::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId,
390 int32_t recvUserId, std::vector<std::string> &dumpInfo)
391 {
392 MessageParcel data;
393 if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
394 ANS_LOGE("[ShellDump] fail: write interface token failed.");
395 return ERR_ANS_PARCELABLE_FAILED;
396 }
397 if (!data.WriteString(cmd)) {
398 ANS_LOGE("[ShellDump] fail: write dump cmd failed.");
399 return ERR_ANS_PARCELABLE_FAILED;
400 }
401 if (!data.WriteString(bundle)) {
402 ANS_LOGE("[ShellDump] fail: write dump bundle failed.");
403 return ERR_ANS_PARCELABLE_FAILED;
404 }
405 if (!data.WriteInt32(userId)) {
406 ANS_LOGE("[ShellDump] fail: write dump userId failed.");
407 return ERR_ANS_PARCELABLE_FAILED;
408 }
409 if (!data.WriteInt32(recvUserId)) {
410 ANS_LOGE("[ShellDump] fail: write dump recvUserId failed.");
411 return ERR_ANS_PARCELABLE_FAILED;
412 }
413 MessageParcel reply;
414 MessageOption option = {MessageOption::TF_SYNC};
415 ErrCode result = InnerTransact(NotificationInterfaceCode::SHELL_DUMP, option, data, reply);
416 if (result != ERR_OK) {
417 ANS_LOGE("[ShellDump] fail: transact ErrCode=%{public}d", result);
418 return ERR_ANS_TRANSACT_FAILED;
419 }
420 if (!reply.ReadInt32(result)) {
421 ANS_LOGE("[ShellDump] fail: read result failed.");
422 return ERR_ANS_PARCELABLE_FAILED;
423 }
424 if (!reply.ReadStringVector(&dumpInfo)) {
425 ANS_LOGE("[ShellDump] fail: read dumpInfo failed.");
426 return ERR_ANS_PARCELABLE_FAILED;
427 }
428 return result;
429 }
430 } // namespace Notification
431 } // namespace OHOS
432