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     if (error != ERR_OK) {
48         LBSLOGI(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
49     }
50     return LocationErrCode(reply.ReadInt32());
51 }
52 
SetEnable(bool state)53 LocationErrCode PassiveAbilityProxy::SetEnable(bool state)
54 {
55     MessageParcel data;
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         LBSLOGE(PASSIVE, "write interfaceToken fail!");
58         return ERRCODE_SERVICE_UNAVAILABLE;
59     }
60     data.WriteBool(state);
61 
62     MessageParcel reply;
63     MessageOption option;
64     int error = Remote()->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::SET_ENABLE), data, reply, option);
65     if (error != ERR_OK) {
66         LBSLOGI(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
67     }
68     return LocationErrCode(reply.ReadInt32());
69 }
70 
EnableMock()71 LocationErrCode PassiveAbilityProxy::EnableMock()
72 {
73     MessageParcel data;
74     MessageParcel reply;
75     MessageOption option;
76     sptr<IRemoteObject> remote = Remote();
77     if (remote == nullptr) {
78         LBSLOGE(PASSIVE, "EnableLocationMock remote is null");
79         return ERRCODE_SERVICE_UNAVAILABLE;
80     }
81     if (!data.WriteInterfaceToken(GetDescriptor())) {
82         LBSLOGE(PASSIVE, "write interfaceToken fail!");
83         return ERRCODE_SERVICE_UNAVAILABLE;
84     }
85     int error =
86         remote->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::ENABLE_LOCATION_MOCK), data, reply, option);
87     LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
88     return LocationErrCode(reply.ReadInt32());
89 }
90 
DisableMock()91 LocationErrCode PassiveAbilityProxy::DisableMock()
92 {
93     MessageParcel data;
94     MessageParcel reply;
95     MessageOption option;
96     sptr<IRemoteObject> remote = Remote();
97     if (remote == nullptr) {
98         LBSLOGE(PASSIVE, "DisableLocationMock remote is null");
99         return ERRCODE_SERVICE_UNAVAILABLE;
100     }
101     if (!data.WriteInterfaceToken(GetDescriptor())) {
102         LBSLOGE(PASSIVE, "write interfaceToken fail!");
103         return ERRCODE_SERVICE_UNAVAILABLE;
104     }
105     int error =
106         remote->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::DISABLE_LOCATION_MOCK), data, reply, option);
107     LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
108     return LocationErrCode(reply.ReadInt32());
109 }
110 
SetMocked(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)111 LocationErrCode PassiveAbilityProxy::SetMocked(
112     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
113 {
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option;
117     sptr<IRemoteObject> remote = Remote();
118     if (remote == nullptr) {
119         LBSLOGE(PASSIVE, "SetMockedLocations remote is null");
120         return ERRCODE_SERVICE_UNAVAILABLE;
121     }
122     if (!data.WriteInterfaceToken(GetDescriptor())) {
123         LBSLOGE(PASSIVE, "write interfaceToken fail!");
124         return ERRCODE_SERVICE_UNAVAILABLE;
125     }
126     data.WriteInt32(timeInterval);
127     int locationSize = static_cast<int>(location.size());
128     data.WriteInt32(locationSize);
129     for (int i = 0; i < locationSize; i++) {
130         location.at(i)->Marshalling(data);
131     }
132     int error =
133         remote->SendRequest(static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS), data, reply, option);
134     LBSLOGD(PASSIVE, "%{public}s Transact Error = %{public}d", __func__, error);
135     return LocationErrCode(reply.ReadInt32());
136 }
137 } // namespace Location
138 } // namespace OHOS
139 #endif
140