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_PASSIVE_SUPPORT
17 #include "passive_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 {
PassiveAbilityProxy(const sptr<IRemoteObject> & impl)28 PassiveAbilityProxy::PassiveAbilityProxy(const sptr<IRemoteObject> &impl)
29 : IRemoteProxy<IPassiveAbility>(impl)
30 {
31 }
32
SendLocationRequest(WorkRecord & workrecord)33 LocationErrCode PassiveAbilityProxy::SendLocationRequest(WorkRecord &workrecord)
34 {
35 MessageParcel data;
36 MessageParcel reply;
37 MessageOption option;
38 if (!data.WriteInterfaceToken(GetDescriptor())) {
39 LBSLOGE(PASSIVE, "write interfaceToken fail!");
40 return ERRCODE_SERVICE_UNAVAILABLE;
41 }
42 workrecord.Marshalling(data);
43 int error = Remote()->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::SEND_LOCATION_REQUEST),
44 data,
45 reply,
46 option);
47 LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
48 return LocationErrCode(reply.ReadInt32());
49 }
50
SetEnable(bool state)51 LocationErrCode PassiveAbilityProxy::SetEnable(bool state)
52 {
53 MessageParcel data;
54 if (!data.WriteInterfaceToken(GetDescriptor())) {
55 LBSLOGE(PASSIVE, "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>(PassiveInterfaceCode::SET_ENABLE), data, reply, option);
63 LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
64 return LocationErrCode(reply.ReadInt32());
65 }
66
EnableMock()67 LocationErrCode PassiveAbilityProxy::EnableMock()
68 {
69 MessageParcel data;
70 MessageParcel reply;
71 MessageOption option;
72 sptr<IRemoteObject> remote = Remote();
73 if (remote == nullptr) {
74 LBSLOGE(PASSIVE, "EnableLocationMock remote is null");
75 return ERRCODE_SERVICE_UNAVAILABLE;
76 }
77 if (!data.WriteInterfaceToken(GetDescriptor())) {
78 LBSLOGE(PASSIVE, "write interfaceToken fail!");
79 return ERRCODE_SERVICE_UNAVAILABLE;
80 }
81 int error =
82 remote->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::ENABLE_LOCATION_MOCK), data, reply, option);
83 LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
84 return LocationErrCode(reply.ReadInt32());
85 }
86
DisableMock()87 LocationErrCode PassiveAbilityProxy::DisableMock()
88 {
89 MessageParcel data;
90 MessageParcel reply;
91 MessageOption option;
92 sptr<IRemoteObject> remote = Remote();
93 if (remote == nullptr) {
94 LBSLOGE(PASSIVE, "DisableLocationMock remote is null");
95 return ERRCODE_SERVICE_UNAVAILABLE;
96 }
97 if (!data.WriteInterfaceToken(GetDescriptor())) {
98 LBSLOGE(PASSIVE, "write interfaceToken fail!");
99 return ERRCODE_SERVICE_UNAVAILABLE;
100 }
101 int error =
102 remote->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::DISABLE_LOCATION_MOCK), data, reply, option);
103 LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
104 return LocationErrCode(reply.ReadInt32());
105 }
106
SetMocked(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)107 LocationErrCode PassiveAbilityProxy::SetMocked(
108 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
109 {
110 MessageParcel data;
111 MessageParcel reply;
112 MessageOption option;
113 sptr<IRemoteObject> remote = Remote();
114 if (remote == nullptr) {
115 LBSLOGE(PASSIVE, "SetMockedLocations remote is null");
116 return ERRCODE_SERVICE_UNAVAILABLE;
117 }
118 if (!data.WriteInterfaceToken(GetDescriptor())) {
119 LBSLOGE(PASSIVE, "write interfaceToken fail!");
120 return ERRCODE_SERVICE_UNAVAILABLE;
121 }
122 data.WriteInt32(timeInterval);
123 int locationSize = static_cast<int>(location.size());
124 data.WriteInt32(locationSize);
125 for (int i = 0; i < locationSize; i++) {
126 location.at(i)->Marshalling(data);
127 }
128 int error =
129 remote->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS), data, reply, option);
130 LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
131 return LocationErrCode(reply.ReadInt32());
132 }
133 } // namespace Location
134 } // namespace OHOS
135 #endif
136