1 /*
2 * Copyright (c) 2017-2020, 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_GnssMeasurementInterface"
22
23 #include <log_util.h>
24 #include "GnssMeasurement.h"
25 #include "MeasurementAPIClient.h"
26
27 namespace android {
28 namespace hardware {
29 namespace gnss {
30 namespace V2_1 {
31 namespace implementation {
32
serviceDied(uint64_t cookie,const wp<IBase> & who)33 void GnssMeasurement::GnssMeasurementDeathRecipient::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 (mGnssMeasurement != nullptr) {
38 mGnssMeasurement->close();
39 }
40 }
41
GnssMeasurement()42 GnssMeasurement::GnssMeasurement() {
43 mGnssMeasurementDeathRecipient = new GnssMeasurementDeathRecipient(this);
44 mApi = new MeasurementAPIClient();
45 }
46
~GnssMeasurement()47 GnssMeasurement::~GnssMeasurement() {
48 if (mApi) {
49 delete mApi;
50 mApi = nullptr;
51 }
52 }
53
54 // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
setCallback(const sp<V1_0::IGnssMeasurementCallback> & callback)55 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
56 const sp<V1_0::IGnssMeasurementCallback>& callback) {
57
58 Return<GnssMeasurement::GnssMeasurementStatus> ret =
59 IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
60 if (mGnssMeasurementCbIface != nullptr) {
61 LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
62 return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
63 }
64
65 if (callback == nullptr) {
66 LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
67 return ret;
68 }
69 if (mApi == nullptr) {
70 LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
71 return ret;
72 }
73
74 mGnssMeasurementCbIface = callback;
75 mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
76
77 return mApi->measurementSetCallback(callback);
78 }
79
close()80 Return<void> GnssMeasurement::close() {
81 if (mApi == nullptr) {
82 LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
83 return Void();
84 }
85
86 if (mGnssMeasurementCbIface != nullptr) {
87 mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
88 mGnssMeasurementCbIface = nullptr;
89 }
90 if (mGnssMeasurementCbIface_1_1 != nullptr) {
91 mGnssMeasurementCbIface_1_1->unlinkToDeath(mGnssMeasurementDeathRecipient);
92 mGnssMeasurementCbIface_1_1 = nullptr;
93 }
94 if (mGnssMeasurementCbIface_2_0 != nullptr) {
95 mGnssMeasurementCbIface_2_0->unlinkToDeath(mGnssMeasurementDeathRecipient);
96 mGnssMeasurementCbIface_2_0 = nullptr;
97 }
98 if (mGnssMeasurementCbIface_2_1 != nullptr) {
99 mGnssMeasurementCbIface_2_1->unlinkToDeath(mGnssMeasurementDeathRecipient);
100 mGnssMeasurementCbIface_2_1 = nullptr;
101 }
102 mApi->measurementClose();
103
104 return Void();
105 }
106
107 // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
setCallback_1_1(const sp<V1_1::IGnssMeasurementCallback> & callback,bool enableFullTracking)108 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
109 const sp<V1_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) {
110
111 Return<GnssMeasurement::GnssMeasurementStatus> ret =
112 IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
113 if (mGnssMeasurementCbIface_1_1 != nullptr) {
114 LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
115 return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
116 }
117
118 if (callback == nullptr) {
119 LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
120 return ret;
121 }
122 if (nullptr == mApi) {
123 LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
124 return ret;
125 }
126
127 mGnssMeasurementCbIface_1_1 = callback;
128 mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
129
130 GnssPowerMode powerMode = enableFullTracking?
131 GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
132
133 return mApi->measurementSetCallback_1_1(callback, powerMode);
134 }
135 // Methods from ::android::hardware::gnss::V2_0::IGnssMeasurement follow.
setCallback_2_0(const sp<V2_0::IGnssMeasurementCallback> & callback,bool enableFullTracking)136 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
137 const sp<V2_0::IGnssMeasurementCallback>& callback,
138 bool enableFullTracking) {
139
140 Return<GnssMeasurement::GnssMeasurementStatus> ret =
141 IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
142 if (mGnssMeasurementCbIface_2_0 != nullptr) {
143 LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
144 return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
145 }
146
147 if (callback == nullptr) {
148 LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
149 return ret;
150 }
151 if (nullptr == mApi) {
152 LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
153 return ret;
154 }
155
156 mGnssMeasurementCbIface_2_0 = callback;
157 mGnssMeasurementCbIface_2_0->linkToDeath(mGnssMeasurementDeathRecipient, 0);
158
159 GnssPowerMode powerMode = enableFullTracking ?
160 GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
161
162 return mApi->measurementSetCallback_2_0(callback, powerMode);
163 }
164
165 // Methods from ::android::hardware::gnss::V2_1::IGnssMeasurement follow.
setCallback_2_1(const sp<::android::hardware::gnss::V2_1::IGnssMeasurementCallback> & callback,bool enableFullTracking)166 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_1(
167 const sp<::android::hardware::gnss::V2_1::IGnssMeasurementCallback>& callback,
168 bool enableFullTracking) {
169 Return<GnssMeasurement::GnssMeasurementStatus> ret =
170 IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
171 if (mGnssMeasurementCbIface_2_1 != nullptr) {
172 LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
173 return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
174 }
175
176 if (callback == nullptr) {
177 LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
178 return ret;
179 }
180 if (nullptr == mApi) {
181 LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
182 return ret;
183 }
184
185 mGnssMeasurementCbIface_2_1 = callback;
186 mGnssMeasurementCbIface_2_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
187
188 GnssPowerMode powerMode = enableFullTracking ?
189 GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
190
191 return mApi->measurementSetCallback_2_1(callback, powerMode);
192
193 }
194
195 } // namespace implementation
196 } // namespace V2_1
197 } // namespace gnss
198 } // namespace hardware
199 } // namespace android
200