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
16 #include "net_detection_callback_proxy.h"
17 #include "net_manager_constants.h"
18 #include "net_mgr_log_wrapper.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
NetDetectionCallbackProxy(const sptr<IRemoteObject> & impl)22 NetDetectionCallbackProxy::NetDetectionCallbackProxy(const sptr<IRemoteObject> &impl)
23 : IRemoteProxy<INetDetectionCallback>(impl)
24 {}
25
~NetDetectionCallbackProxy()26 NetDetectionCallbackProxy::~NetDetectionCallbackProxy() {}
27
OnNetDetectionResultChanged(NetDetectionResultCode detectionResult,const std::string & urlRedirect)28 int32_t NetDetectionCallbackProxy::OnNetDetectionResultChanged(NetDetectionResultCode detectionResult,
29 const std::string &urlRedirect)
30 {
31 MessageParcel dataParcel;
32 if (!WriteInterfaceToken(dataParcel)) {
33 NETMGR_LOG_E("WriteInterfaceToken failed");
34 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
35 }
36
37 if (!dataParcel.WriteString(urlRedirect)) {
38 return NETMANAGER_ERR_WRITE_DATA_FAIL;
39 }
40 if (!dataParcel.WriteInt32(static_cast<int32_t>(detectionResult))) {
41 return NETMANAGER_ERR_WRITE_DATA_FAIL;
42 }
43
44 sptr<IRemoteObject> remote = Remote();
45 if (remote == nullptr) {
46 NETMGR_LOG_E("Remote is null");
47 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
48 }
49 MessageParcel replyParcel;
50 MessageOption option;
51 int32_t retCode = remote->SendRequest(static_cast<uint32_t>(DetectionCallback::NET_DETECTION_RESULT),
52 dataParcel, replyParcel, option);
53 if (retCode != ERR_NONE) {
54 NETMGR_LOG_E("proxy SendRequest failed, retCode: [%{public}d]", retCode);
55 return retCode;
56 }
57
58 return replyParcel.ReadInt32();
59 }
60
WriteInterfaceToken(MessageParcel & data)61 bool NetDetectionCallbackProxy::WriteInterfaceToken(MessageParcel &data)
62 {
63 if (!data.WriteInterfaceToken(NetDetectionCallbackProxy::GetDescriptor())) {
64 NETMGR_LOG_E("WriteInterfaceToken failed");
65 return false;
66 }
67 return true;
68 }
69 } // namespace NetManagerStandard
70 } // namespace OHOS
71