1 /*
2  * Copyright (c) 2017-2019, 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 "LocSvc_GnssBatchingInterface"
22 
23 #include <log_util.h>
24 #include <BatchingAPIClient.h>
25 #include "GnssBatching.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace gnss {
30 namespace V2_0 {
31 namespace implementation {
32 
serviceDied(uint64_t cookie,const wp<IBase> & who)33 void GnssBatching::GnssBatchingDeathRecipient::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 (mGnssBatching != nullptr) {
38         mGnssBatching->stop();
39         mGnssBatching->cleanup();
40     }
41 }
42 
GnssBatching()43 GnssBatching::GnssBatching() : mApi(nullptr) {
44     mGnssBatchingDeathRecipient = new GnssBatchingDeathRecipient(this);
45 }
46 
~GnssBatching()47 GnssBatching::~GnssBatching() {
48     if (mApi != nullptr) {
49         delete mApi;
50         mApi = nullptr;
51     }
52 }
53 
54 
55 // Methods from ::android::hardware::gnss::V1_0::IGnssBatching follow.
init(const sp<V1_0::IGnssBatchingCallback> & callback)56 Return<bool> GnssBatching::init(const sp<V1_0::IGnssBatchingCallback>& callback) {
57     if (mApi != nullptr) {
58         LOC_LOGD("%s]: mApi is NOT nullptr, delete it first", __FUNCTION__);
59         delete mApi;
60         mApi = nullptr;
61     }
62 
63     mApi = new BatchingAPIClient(callback);
64     if (mApi == nullptr) {
65         LOC_LOGE("%s]: failed to create mApi", __FUNCTION__);
66         return false;
67     }
68 
69     if (mGnssBatchingCbIface != nullptr) {
70         mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient);
71     }
72     mGnssBatchingCbIface = callback;
73     if (mGnssBatchingCbIface != nullptr) {
74         mGnssBatchingCbIface->linkToDeath(mGnssBatchingDeathRecipient, 0 /*cookie*/);
75     }
76 
77     return true;
78 }
79 
getBatchSize()80 Return<uint16_t> GnssBatching::getBatchSize() {
81     uint16_t ret = 0;
82     if (mApi == nullptr) {
83         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
84     } else {
85         ret = mApi->getBatchSize();
86     }
87     return ret;
88 }
89 
start(const IGnssBatching::Options & options)90 Return<bool> GnssBatching::start(const IGnssBatching::Options& options) {
91     bool ret = false;
92     if (mApi == nullptr) {
93         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
94     } else {
95         ret = mApi->startSession(options);
96     }
97     return ret;
98 }
99 
flush()100 Return<void> GnssBatching::flush() {
101     if (mApi == nullptr) {
102         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
103     } else {
104         mApi->flushBatchedLocations();
105     }
106     return Void();
107 }
108 
stop()109 Return<bool> GnssBatching::stop() {
110     bool ret = false;
111     if (mApi == nullptr) {
112         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
113     } else {
114         ret = mApi->stopSession();
115     }
116     return ret;
117 }
118 
cleanup()119 Return<void> GnssBatching::cleanup() {
120     if (mApi != nullptr) {
121         mApi->stopSession();
122     }
123     if (mGnssBatchingCbIface != nullptr) {
124         mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient);
125         mGnssBatchingCbIface = nullptr;
126     }
127     if (mGnssBatchingCbIface_2_0 != nullptr) {
128         mGnssBatchingCbIface_2_0->unlinkToDeath(mGnssBatchingDeathRecipient);
129         mGnssBatchingCbIface_2_0 = nullptr;
130     }
131     return Void();
132 }
133 
134 // Methods from ::android::hardware::gnss::V2_0::IGnssBatching follow.
init_2_0(const sp<V2_0::IGnssBatchingCallback> & callback)135 Return<bool> GnssBatching::init_2_0(const sp<V2_0::IGnssBatchingCallback>& callback) {
136     if (mApi != nullptr) {
137         LOC_LOGD("%s]: mApi is NOT nullptr, delete it first", __FUNCTION__);
138         delete mApi;
139         mApi = nullptr;
140     }
141 
142     mApi = new BatchingAPIClient(callback);
143     if (mApi == nullptr) {
144         LOC_LOGE("%s]: failed to create mApi", __FUNCTION__);
145         return false;
146     }
147 
148     if (mGnssBatchingCbIface_2_0 != nullptr) {
149         mGnssBatchingCbIface_2_0->unlinkToDeath(mGnssBatchingDeathRecipient);
150     }
151     mGnssBatchingCbIface_2_0 = callback;
152     if (mGnssBatchingCbIface_2_0 != nullptr) {
153         mGnssBatchingCbIface_2_0->linkToDeath(mGnssBatchingDeathRecipient, 0 /*cookie*/);
154     }
155 
156     return true;
157 }
158 
159 }  // namespace implementation
160 }  // namespace V2_0
161 }  // namespace gnss
162 }  // namespace hardware
163 }  // namespace android
164