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 #include "faultlogger_service_proxy.h"
16 
17 #include <unistd.h>
18 #include "ipc_types.h"
19 #include "message_parcel.h"
20 #include "hiview_logger.h"
21 #include "faultlog_info_ohos.h"
22 #include "faultlog_query_result_proxy.h"
23 #include "hiview_logger.h"
24 #include "hiviewfaultlogger_ipc_interface_code.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_LABEL(0xD002D11, "FaultLoggerProxy");
AddFaultLog(const FaultLogInfoOhos & info)29 void FaultLoggerServiceProxy::AddFaultLog(const FaultLogInfoOhos& info)
30 {
31     sptr<IRemoteObject> remote = Remote();
32     if (remote == nullptr) {
33         return;
34     }
35 
36     MessageParcel data;
37     MessageParcel reply;
38     MessageOption option;
39     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
40         return;
41     }
42 
43     if (!info.Marshalling(data)) {
44         return;
45     }
46 
47     if (info.pipeFd != -1) {
48         if (!data.WriteFileDescriptor(info.pipeFd)) {
49             HIVIEW_LOGE("failed to write file descriptor.");
50         }
51         close(info.pipeFd);
52     }
53     auto flags = option.GetFlags();
54     option.SetFlags(flags | 0x01); // 0X01 return immediately
55     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::ADD_FAULTLOG),
56         data, reply, option) != ERR_OK) {
57         return;
58     }
59 }
60 
QuerySelfFaultLog(int32_t faultType,int32_t maxNum)61 sptr<IRemoteObject> FaultLoggerServiceProxy::QuerySelfFaultLog(int32_t faultType, int32_t maxNum)
62 {
63     sptr<IRemoteObject> remote = Remote();
64     if (remote == nullptr) {
65         return nullptr;
66     }
67 
68     MessageParcel data;
69     MessageParcel reply;
70     MessageOption option;
71     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
72         return nullptr;
73     }
74 
75     if (!data.WriteInt32(faultType)) {
76         return nullptr;
77     }
78 
79     if (!data.WriteInt32(maxNum)) {
80         return nullptr;
81     }
82 
83     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::QUERY_SELF_FAULTLOG),
84         data, reply, option) != ERR_OK) {
85         return nullptr;
86     }
87 
88     sptr<IRemoteObject> remoteObject = reply.ReadRemoteObject();
89     if (remoteObject == nullptr) {
90         HIVIEW_LOGE("Failed to transfer Result.");
91     }
92     return remoteObject;
93 }
94 
Destroy()95 void FaultLoggerServiceProxy::Destroy()
96 {
97     sptr<IRemoteObject> remote = Remote();
98     if (remote == nullptr) {
99         return;
100     }
101 
102     MessageParcel data;
103     MessageParcel reply;
104     MessageOption option;
105     if (!data.WriteInterfaceToken(FaultLoggerServiceProxy::GetDescriptor())) {
106         return;
107     }
108 
109     if (remote->SendRequest(static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::DESTROY),
110         data, reply, option) != ERR_OK) {
111         return;
112     }
113 }
114 } // namespace HiviewDFX
115 } // namespace OHOS
116