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_stub.h"
16 
17 #include "ipc_types.h"
18 #include "message_parcel.h"
19 
20 #include "faultlog_info.h"
21 #include "faultlog_info_ohos.h"
22 #include "hiviewfaultlogger_ipc_interface_code.h"
23 
24 #include "hiview_logger.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_LABEL(0xD002D11, "FaultLoggerServiceStub");
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int FaultLoggerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
30     MessageParcel &reply, MessageOption &option)
31 {
32     std::u16string descripter = FaultLoggerServiceStub::GetDescriptor();
33     std::u16string remoteDescripter = data.ReadInterfaceToken();
34     if (descripter != remoteDescripter) {
35         HIVIEW_LOGE("read descriptor failed.");
36         return -1;
37     }
38 
39     switch (code) {
40         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::ADD_FAULTLOG): {
41             sptr<FaultLogInfoOhos> ohosInfo = FaultLogInfoOhos::Unmarshalling(data);
42             if (ohosInfo == nullptr) {
43                 HIVIEW_LOGE("failed to Unmarshalling info.");
44                 return ERR_FLATTEN_OBJECT;
45             }
46             if (data.ContainFileDescriptors()) {
47                 ohosInfo->pipeFd = data.ReadFileDescriptor();
48             }
49             FaultLogInfoOhos info(*ohosInfo);
50             AddFaultLog(info);
51             return ERR_OK;
52         }
53         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::QUERY_SELF_FAULTLOG): {
54             int32_t type = data.ReadInt32();
55             int32_t maxNum = data.ReadInt32();
56             auto result = QuerySelfFaultLog(type, maxNum);
57             if (result == nullptr) {
58                 HIVIEW_LOGE("failed to query self log.");
59                 return -1;
60             }
61 
62             if (!reply.WriteRemoteObject(result)) {
63                 HIVIEW_LOGE("failed to write query result.");
64                 return ERR_FLATTEN_OBJECT;
65             }
66             return ERR_OK;
67         }
68         case static_cast<uint32_t>(FaultLoggerServiceInterfaceCode::DESTROY): {
69             Destroy();
70             return ERR_OK;
71         }
72 
73         default:
74             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
75     }
76 }
77 } // namespace HiviewDFX
78 } // namespace OHOS
79