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 #include "common_utils.h"
16 #include "location_sa_load_manager.h"
17
18 #include "iremote_object.h"
19 #include "location_log.h"
20 #include "ipc_skeleton.h"
21
22 #include "fence_impl.h"
23 namespace OHOS {
24 namespace Location {
25 const int FENCE_SA_ID = 4353;
26 const std::u16string FENCE_DESCRIPTION = u"location.IHifenceAbility";
27 const int REGISTER_GEOFENCE_CALLBACK = 21;
28 const int UNREGISTER_GEOFENCE_CALLBACK = 22;
29
InitLocationExt()30 bool FenceImpl::InitLocationExt()
31 {
32 if (CommonUtils::CheckIfSystemAbilityAvailable(FENCE_SA_ID)) {
33 LBSLOGD(LOCATOR_STANDARD, "locator sa has been loaded");
34 return true;
35 }
36 auto instance = DelayedSingleton<LocationSaLoadManager>::GetInstance();
37 if (instance == nullptr || instance->LoadLocationSa(FENCE_SA_ID) != ERRCODE_SUCCESS) {
38 LBSLOGE(LOCATOR_STANDARD, "locator sa load failed.");
39 return false;
40 }
41 LBSLOGI(LOCATOR_STANDARD, "init successfully");
42 return true;
43 }
44
AddFenceExt(std::unique_ptr<GeofenceRequest> & request,const AbilityRuntime::WantAgent::WantAgent wantAgent)45 LocationErrCode FenceImpl::AddFenceExt(
46 std::unique_ptr<GeofenceRequest> &request, const AbilityRuntime::WantAgent::WantAgent wantAgent)
47 {
48 if (!InitLocationExt()) {
49 return ERRCODE_NOT_SUPPORTED;
50 }
51 LBSLOGD(LOCATOR_STANDARD, "FenceImpl::AddFenceV9()");
52 MessageParcel data;
53 MessageParcel reply;
54 MessageOption option;
55 if (!data.WriteInterfaceToken(FENCE_DESCRIPTION)) {
56 LBSLOGE(LOCATOR_STANDARD, "%{public}s WriteInterfaceToken failed", __func__);
57 return ERRCODE_SERVICE_UNAVAILABLE;
58 }
59 data.WriteInt32(request->scenario);
60 data.WriteDouble(request->geofence.latitude);
61 data.WriteDouble(request->geofence.longitude);
62 data.WriteDouble(request->geofence.radius);
63 data.WriteDouble(request->geofence.expiration);
64 data.WriteParcelable(&wantAgent);
65 sptr<IRemoteObject> proxyExt = CommonUtils::GetRemoteObject(FENCE_SA_ID, CommonUtils::InitDeviceId());
66 if (proxyExt == nullptr) {
67 return ERRCODE_SERVICE_UNAVAILABLE;
68 }
69 proxyExt->SendRequest(REGISTER_GEOFENCE_CALLBACK, data, reply, option);
70 return LocationErrCode(reply.ReadInt32());
71 }
72
RemoveFenceExt(std::unique_ptr<GeofenceRequest> & request,const AbilityRuntime::WantAgent::WantAgent wantAgent)73 LocationErrCode FenceImpl::RemoveFenceExt(
74 std::unique_ptr<GeofenceRequest> &request, const AbilityRuntime::WantAgent::WantAgent wantAgent)
75 {
76 if (!InitLocationExt()) {
77 return ERRCODE_NOT_SUPPORTED;
78 }
79 LBSLOGD(LOCATOR_STANDARD, "FenceImpl::RemoveFenceV9()");
80 MessageParcel data;
81 MessageParcel reply;
82 MessageOption option;
83 if (!data.WriteInterfaceToken(FENCE_DESCRIPTION)) {
84 LBSLOGE(LOCATOR_STANDARD, "%{public}s WriteInterfaceToken failed", __func__);
85 return ERRCODE_SERVICE_UNAVAILABLE;
86 }
87 data.WriteInt32(request->scenario);
88 data.WriteDouble(request->geofence.latitude);
89 data.WriteDouble(request->geofence.longitude);
90 data.WriteDouble(request->geofence.radius);
91 data.WriteDouble(request->geofence.expiration);
92 data.WriteParcelable(&wantAgent);
93 sptr<IRemoteObject> proxyExt = CommonUtils::GetRemoteObject(FENCE_SA_ID, CommonUtils::InitDeviceId());
94 if (proxyExt == nullptr) {
95 return ERRCODE_SERVICE_UNAVAILABLE;
96 }
97 proxyExt->SendRequest(UNREGISTER_GEOFENCE_CALLBACK, data, reply, option);
98 return LocationErrCode(reply.ReadInt32());
99 }
100 } // namespace Location
101 } // namespace OHOS