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 #ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_COMMON_INCLUDE_IPC_UTIL_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_COMMON_INCLUDE_IPC_UTIL_H
18 
19 #include <iremote_object.h>
20 #include <iservice_registry.h>
21 #include <string_ex.h>
22 
23 #include "transient_task_log.h"
24 
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 #define READ_PARCEL(in, type, data)                         \
28     do {                                                    \
29         if (!(in).Read##type(data)) {                       \
30             BGTASK_LOGE("Failed to read "#data);            \
31             return;                                         \
32         }                                                   \
33     } while (0)
34 
35 #define READ_PARCEL_WITH_RET(in, type, data, ret)           \
36     do {                                                    \
37         if (!(in).Read##type(data)) {                       \
38             BGTASK_LOGE("Failed to read "#data);            \
39             return (ret);                                   \
40         }                                                   \
41     } while (0)
42 
43 #define WRITE_PARCEL(out, type, data)                       \
44     do {                                                    \
45         if (!(out).Write##type(data)) {                     \
46             BGTASK_LOGE("Failed to write "#data);           \
47             return;                                         \
48         }                                                   \
49     } while (0)
50 
51 #define WRITE_PARCEL_WITH_RET(out, type, data, ret)         \
52     do {                                                    \
53         if (!(out).Write##type(data)) {                     \
54             BGTASK_LOGE("Failed to write "#data);           \
55             return (ret);                                   \
56         }                                                   \
57     } while (0)
58 
GetSystemService(const std::string & name)59 static inline sptr<IRemoteObject> GetSystemService(const std::string &name)
60 {
61     auto registry = ServiceRegistry::GetInstance();
62     return (registry != nullptr) ? registry->GetService(Str8ToStr16(name)) : nullptr;
63 }
64 };  // namespace BackgroundTaskMgr
65 };  // namespace OHOS
66 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_FRAMEWORKS_COMMON_INCLUDE_IPC_UTIL_H