1 /*
2  * Copyright (c) 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 "net_diag_callback_stub.h"
17 #include "net_manager_constants.h"
18 #include "netnative_log_wrapper.h"
19 #include "netsys_ipc_interface_code.h"
20 
21 namespace OHOS {
22 namespace NetsysNative {
23 namespace {
24 using namespace OHOS::NetManagerStandard;
25 } // namespace
26 
NetDiagCallbackStub()27 NetDiagCallbackStub::NetDiagCallbackStub()
28 {
29     memberFuncMap_[static_cast<uint32_t>(NetDiagInterfaceCode::ON_NOTIFY_PING_RESULT)] =
30         &NetDiagCallbackStub::CmdNotifyPingResult;
31 }
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t NetDiagCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34                                              MessageOption &option)
35 {
36     NETNATIVE_LOGI("Stub call start, code:[%{public}d]", code);
37     std::u16string myDescriptor = NetDiagCallbackStub::GetDescriptor();
38     std::u16string remoteDescriptor = data.ReadInterfaceToken();
39     if (myDescriptor != remoteDescriptor) {
40         NETNATIVE_LOGE("Descriptor checked failed");
41         return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
42     }
43 
44     auto itFunc = memberFuncMap_.find(code);
45     if (itFunc != memberFuncMap_.end()) {
46         auto requestFunc = itFunc->second;
47         if (requestFunc != nullptr) {
48             return (this->*requestFunc)(data, reply);
49         }
50     }
51 
52     NETNATIVE_LOGE("Stub default case, need check");
53     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54 }
55 
CmdNotifyPingResult(MessageParcel & data,MessageParcel & reply)56 int32_t NetDiagCallbackStub::CmdNotifyPingResult(MessageParcel &data, MessageParcel &reply)
57 {
58     NetDiagPingResult pingResult;
59     if (!NetDiagPingResult::Unmarshalling(data, pingResult)) {
60         return NETMANAGER_ERR_READ_DATA_FAIL;
61     }
62 
63     int32_t result = OnNotifyPingResult(pingResult);
64     if (!reply.WriteInt32(result)) {
65         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
66     }
67     return NETMANAGER_SUCCESS;
68 }
69 
OnNotifyPingResult(const NetDiagPingResult & pingResult)70 int32_t NetDiagCallbackStub::OnNotifyPingResult(const NetDiagPingResult &pingResult)
71 {
72     NETNATIVE_LOGI("OnNotifyPingResult received.");
73     return NETMANAGER_SUCCESS;
74 }
75 } // namespace NetsysNative
76 } // namespace OHOS
77