1 /*
2  * Copyright (c) 2024 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 "file_dfs_listener_stub.h"
16 #include "file_dfs_listener_interface_code.h"
17 #include "filemgmt_libhilog.h"
18 
19 namespace OHOS {
20 namespace FileManagement {
21 namespace ModuleFileIO {
22 using namespace FileManagement;
23 namespace {
24     constexpr int NO_ERROR = 0;
25     constexpr int E_INVAL_ARG = 1;
26 }
27 
FileDfsListenerStub()28 FileDfsListenerStub::FileDfsListenerStub()
29 {
30     opToInterfaceMap_[static_cast<uint32_t>
31         (Storage::DistributedFile::FileDfsListenerInterfaceCode::FILE_DFS_LISTENER_ON_STATUS)] =
32         &FileDfsListenerStub::HandleOnStatus;
33 }
34 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t FileDfsListenerStub::OnRemoteRequest(uint32_t code,
36                                              MessageParcel &data,
37                                              MessageParcel &reply,
38                                              MessageOption &option)
39 {
40     if (data.ReadInterfaceToken() != GetDescriptor()) {
41         return FILE_DFS_LISTENER_DESCRIPTOR_IS_EMPTY;
42     }
43     switch (code) {
44         case static_cast<uint32_t>(Storage::DistributedFile::FileDfsListenerInterfaceCode::FILE_DFS_LISTENER_ON_STATUS):
45             return HandleOnStatus(data, reply);
46         default:
47             HILOGE("Cannot response request %{public}d: unknown tranction", code);
48             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49     }
50 }
51 
HandleOnStatus(MessageParcel & data,MessageParcel & reply)52 int32_t FileDfsListenerStub::HandleOnStatus(MessageParcel &data, MessageParcel &reply)
53 {
54     std::string networkId;
55     if (!data.ReadString(networkId)) {
56         HILOGE("read networkId failed");
57         return E_INVAL_ARG;
58     }
59     int32_t status;
60     if (!data.ReadInt32(status)) {
61         HILOGE("read status failed");
62         return E_INVAL_ARG;
63     }
64     if (networkId.empty() || status < 0) {
65         HILOGE("Invalid arguments");
66         return E_INVAL_ARG;
67     }
68     OnStatus(networkId, status);
69     return NO_ERROR;
70 }
71 
72 } // namespace ModuleFileIO
73 } // namespace FileManagement
74 } // namespace OHOS