1 /*
2  * Copyright (c) 2017, The Linux Foundation. All rights reserved.
3  * Not a Contribution
4  */
5 /*
6  * Copyright (C) 2016 The Android Open Source Project
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #define LOG_TAG "GnssHal_GnssGeofencing"
22 
23 #include <log_util.h>
24 #include <GeofenceAPIClient.h>
25 #include "GnssGeofencing.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace gnss {
30 namespace V1_0 {
31 namespace implementation {
32 
serviceDied(uint64_t cookie,const wp<IBase> & who)33 void GnssGeofencing::GnssGeofencingDeathRecipient::serviceDied(
34         uint64_t cookie, const wp<IBase>& who) {
35     LOC_LOGE("%s] service died. cookie: %llu, who: %p",
36             __FUNCTION__, static_cast<unsigned long long>(cookie), &who);
37     if (mGnssGeofencing != nullptr) {
38         mGnssGeofencing->removeAllGeofences();
39     }
40 }
41 
GnssGeofencing()42 GnssGeofencing::GnssGeofencing() : mApi(nullptr) {
43     mGnssGeofencingDeathRecipient = new GnssGeofencingDeathRecipient(this);
44 }
45 
~GnssGeofencing()46 GnssGeofencing::~GnssGeofencing() {
47     if (mApi != nullptr) {
48         delete mApi;
49         mApi = nullptr;
50     }
51 }
52 
53 // Methods from ::android::hardware::gnss::V1_0::IGnssGeofencing follow.
setCallback(const sp<IGnssGeofenceCallback> & callback)54 Return<void> GnssGeofencing::setCallback(const sp<IGnssGeofenceCallback>& callback)  {
55     if (mApi != nullptr) {
56         LOC_LOGE("%s]: mApi is NOT nullptr", __FUNCTION__);
57         return Void();
58     }
59 
60     mApi = new GeofenceAPIClient(callback);
61     if (mApi == nullptr) {
62         LOC_LOGE("%s]: failed to create mApi", __FUNCTION__);
63     }
64 
65     if (mGnssGeofencingCbIface != nullptr) {
66         mGnssGeofencingCbIface->unlinkToDeath(mGnssGeofencingDeathRecipient);
67     }
68     mGnssGeofencingCbIface = callback;
69     if (mGnssGeofencingCbIface != nullptr) {
70         mGnssGeofencingCbIface->linkToDeath(mGnssGeofencingDeathRecipient, 0 /*cookie*/);
71     }
72 
73     return Void();
74 }
75 
addGeofence(int32_t geofenceId,double latitudeDegrees,double longitudeDegrees,double radiusMeters,IGnssGeofenceCallback::GeofenceTransition lastTransition,int32_t monitorTransitions,uint32_t notificationResponsivenessMs,uint32_t unknownTimerMs)76 Return<void> GnssGeofencing::addGeofence(
77         int32_t geofenceId,
78         double latitudeDegrees,
79         double longitudeDegrees,
80         double radiusMeters,
81         IGnssGeofenceCallback::GeofenceTransition lastTransition,
82         int32_t monitorTransitions,
83         uint32_t notificationResponsivenessMs,
84         uint32_t unknownTimerMs)  {
85     if (mApi == nullptr) {
86         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
87     } else {
88         mApi->geofenceAdd(
89                 geofenceId,
90                 latitudeDegrees,
91                 longitudeDegrees,
92                 radiusMeters,
93                 static_cast<int32_t>(lastTransition),
94                 monitorTransitions,
95                 notificationResponsivenessMs,
96                 unknownTimerMs);
97     }
98     return Void();
99 }
100 
pauseGeofence(int32_t geofenceId)101 Return<void> GnssGeofencing::pauseGeofence(int32_t geofenceId)  {
102     if (mApi == nullptr) {
103         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
104     } else {
105         mApi->geofencePause(geofenceId);
106     }
107     return Void();
108 }
109 
resumeGeofence(int32_t geofenceId,int32_t monitorTransitions)110 Return<void> GnssGeofencing::resumeGeofence(int32_t geofenceId, int32_t monitorTransitions)  {
111     if (mApi == nullptr) {
112         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
113     } else {
114         mApi->geofenceResume(geofenceId, monitorTransitions);
115     }
116     return Void();
117 }
118 
removeGeofence(int32_t geofenceId)119 Return<void> GnssGeofencing::removeGeofence(int32_t geofenceId)  {
120     if (mApi == nullptr) {
121         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
122     } else {
123         mApi->geofenceRemove(geofenceId);
124     }
125     return Void();
126 }
127 
removeAllGeofences()128 Return<void> GnssGeofencing::removeAllGeofences()  {
129     if (mApi == nullptr) {
130         LOC_LOGD("%s]: mApi is nullptr, do nothing", __FUNCTION__);
131     } else {
132         mApi->geofenceRemoveAll();
133     }
134     return Void();
135 }
136 
137 }  // namespace implementation
138 }  // namespace V1_0
139 }  // namespace gnss
140 }  // namespace hardware
141 }  // namespace android
142