• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #ifdef FEATURE_NETWORK_SUPPORT
17 #include "network_ability_proxy.h"
18 
19 #include "message_parcel.h"
20 #include "message_option.h"
21 
22 #include "location_log.h"
23 #include "subability_common.h"
24 #include "locationhub_ipc_interface_code.h"
25 
26 namespace OHOS {
27 namespace Location {
NetworkAbilityProxy(const sptr<IRemoteObject> & impl)28 NetworkAbilityProxy::NetworkAbilityProxy(const sptr<IRemoteObject> &impl)
29     : IRemoteProxy<INetworkAbility>(impl)
30 {
31 }
32 
SendLocationRequest(WorkRecord & workrecord)33 LocationErrCode NetworkAbilityProxy::SendLocationRequest(WorkRecord &workrecord)
34 {
35     MessageParcel data;
36     MessageParcel reply;
37     MessageOption option;
38     if (!data.WriteInterfaceToken(GetDescriptor())) {
39         LBSLOGE(NETWORK, "write interfaceToken fail!");
40         return ERRCODE_SERVICE_UNAVAILABLE;
41     }
42     workrecord.Marshalling(data);
43     int error = Remote()->SendRequest(static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST),
44                                       data,
45                                       reply,
46                                       option);
47     LBSLOGD(NETWORK, "%{public}s Transact Error = %{public}d", __func__, error);
48     return LocationErrCode(reply.ReadInt32());
49 }
50 
SetEnable(bool state)51 LocationErrCode NetworkAbilityProxy::SetEnable(bool state)
52 {
53     MessageParcel data;
54     if (!data.WriteInterfaceToken(GetDescriptor())) {
55         LBSLOGE(NETWORK, "write interfaceToken fail!");
56         return ERRCODE_SERVICE_UNAVAILABLE;
57     }
58     data.WriteBool(state);
59 
60     MessageParcel reply;
61     MessageOption option;
62     int error = Remote()->SendRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_ENABLE), data, reply, option);
63     LBSLOGD(NETWORK, "%{public}s Transact Error = %{public}d", __func__, error);
64     return LocationErrCode(reply.ReadInt32());
65 }
66 
SelfRequest(bool state)67 LocationErrCode NetworkAbilityProxy::SelfRequest(bool state)
68 {
69     MessageParcel data;
70     MessageParcel reply;
71     MessageOption option;
72     if (!data.WriteInterfaceToken(GetDescriptor())) {
73         LBSLOGE(NETWORK, "write interfaceToken fail!");
74         return ERRCODE_SERVICE_UNAVAILABLE;
75     }
76     data.WriteBool(state);
77     int error =
78         Remote()->SendRequest(static_cast<uint32_t>(NetworkInterfaceCode::SELF_REQUEST), data, reply, option);
79     LBSLOGD(NETWORK, "%{public}s Transact Error = %{public}d", __func__, error);
80     return LocationErrCode(reply.ReadInt32());
81 }
82 
EnableMock()83 LocationErrCode NetworkAbilityProxy::EnableMock()
84 {
85     MessageParcel data;
86     MessageParcel reply;
87     MessageOption option;
88     sptr<IRemoteObject> remote = Remote();
89     if (remote == nullptr) {
90         LBSLOGE(NETWORK, "EnableLocationMock remote is null");
91         return ERRCODE_SERVICE_UNAVAILABLE;
92     }
93     if (!data.WriteInterfaceToken(GetDescriptor())) {
94         LBSLOGE(NETWORK, "write interfaceToken fail!");
95         return ERRCODE_SERVICE_UNAVAILABLE;
96     }
97     int error =
98         remote->SendRequest(static_cast<uint32_t>(NetworkInterfaceCode::ENABLE_LOCATION_MOCK), data, reply, option);
99     LBSLOGD(NETWORK, "%{public}s Transact Error = %{public}d", __func__, error);
100     return LocationErrCode(reply.ReadInt32());
101 }
102 
DisableMock()103 LocationErrCode NetworkAbilityProxy::DisableMock()
104 {
105     MessageParcel data;
106     MessageParcel reply;
107     MessageOption option;
108     sptr<IRemoteObject> remote = Remote();
109     if (remote == nullptr) {
110         LBSLOGE(NETWORK, "DisableLocationMock remote is null");
111         return ERRCODE_SERVICE_UNAVAILABLE;
112     }
113     if (!data.WriteInterfaceToken(GetDescriptor())) {
114         LBSLOGE(NETWORK, "write interfaceToken fail!");
115         return ERRCODE_SERVICE_UNAVAILABLE;
116     }
117     int error =
118         remote->SendRequest(static_cast<uint32_t>(NetworkInterfaceCode::DISABLE_LOCATION_MOCK), data, reply, option);
119     LBSLOGD(NETWORK, "%{public}s Transact Error = %{public}d", __func__, error);
120     return LocationErrCode(reply.ReadInt32());
121 }
122 
SetMocked(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)123 LocationErrCode NetworkAbilityProxy::SetMocked(
124     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
125 {
126     MessageParcel data;
127     MessageParcel reply;
128     MessageOption option;
129     sptr<IRemoteObject> remote = Remote();
130     if (remote == nullptr) {
131         LBSLOGE(NETWORK, "SetMockedLocations remote is null");
132         return ERRCODE_SERVICE_UNAVAILABLE;
133     }
134     if (!data.WriteInterfaceToken(GetDescriptor())) {
135         LBSLOGE(NETWORK, "write interfaceToken fail!");
136         return ERRCODE_SERVICE_UNAVAILABLE;
137     }
138     data.WriteInt32(timeInterval);
139     int locationSize = static_cast<int>(location.size());
140     data.WriteInt32(locationSize);
141     for (int i = 0; i < locationSize; i++) {
142         location.at(i)->Marshalling(data);
143     }
144     int error =
145         remote->SendRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS), data, reply, option);
146     LBSLOGD(NETWORK, "%{public}s Transact Error = %{public}d", __func__, error);
147     return LocationErrCode(reply.ReadInt32());
148 }
149 } // namespace Location
150 } // namespace OHOS
151 #endif
152