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_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_0 {
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         mApi->destroy();
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     clearInterfaces();
75 
76     mGnssMeasurementCbIface = callback;
77     mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
78 
79     return mApi->measurementSetCallback(callback);
80 }
81 
clearInterfaces()82 void GnssMeasurement::clearInterfaces() {
83     if (mGnssMeasurementCbIface != nullptr) {
84         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
85         mGnssMeasurementCbIface = nullptr;
86     }
87     if (mGnssMeasurementCbIface_1_1 != nullptr) {
88         mGnssMeasurementCbIface_1_1->unlinkToDeath(mGnssMeasurementDeathRecipient);
89         mGnssMeasurementCbIface_1_1 = nullptr;
90     }
91     if (mGnssMeasurementCbIface_2_0 != nullptr) {
92         mGnssMeasurementCbIface_2_0->unlinkToDeath(mGnssMeasurementDeathRecipient);
93         mGnssMeasurementCbIface_2_0 = nullptr;
94     }
95 }
96 
close()97 Return<void> GnssMeasurement::close()  {
98     if (mApi == nullptr) {
99         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
100         return Void();
101     }
102 
103     clearInterfaces();
104     mApi->measurementClose();
105 
106     return Void();
107 }
108 
109 // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
setCallback_1_1(const sp<V1_1::IGnssMeasurementCallback> & callback,bool enableFullTracking)110 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
111         const sp<V1_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) {
112 
113     Return<GnssMeasurement::GnssMeasurementStatus> ret =
114         IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
115     if (mGnssMeasurementCbIface_1_1 != nullptr) {
116         LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
117         return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
118     }
119 
120     if (callback == nullptr) {
121         LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
122         return ret;
123     }
124     if (nullptr == mApi) {
125         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
126         return ret;
127     }
128 
129     clearInterfaces();
130 
131     mGnssMeasurementCbIface_1_1 = callback;
132     mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
133 
134     GnssPowerMode powerMode = enableFullTracking?
135             GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
136 
137     return mApi->measurementSetCallback_1_1(callback, powerMode);
138 }
139 // Methods from ::android::hardware::gnss::V2_0::IGnssMeasurement follow.
setCallback_2_0(const sp<V2_0::IGnssMeasurementCallback> & callback,bool enableFullTracking)140 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
141         const sp<V2_0::IGnssMeasurementCallback>& callback,
142         bool enableFullTracking) {
143 
144     Return<GnssMeasurement::GnssMeasurementStatus> ret =
145         IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
146     if (mGnssMeasurementCbIface_2_0 != nullptr) {
147         LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
148         return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
149     }
150 
151     if (callback == nullptr) {
152         LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
153         return ret;
154     }
155     if (nullptr == mApi) {
156         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
157         return ret;
158     }
159 
160     clearInterfaces();
161 
162     mGnssMeasurementCbIface_2_0 = callback;
163     mGnssMeasurementCbIface_2_0->linkToDeath(mGnssMeasurementDeathRecipient, 0);
164 
165     GnssPowerMode powerMode = enableFullTracking ?
166         GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
167 
168     return mApi->measurementSetCallback_2_0(callback, powerMode);
169 }
170 
171 }  // namespace implementation
172 }  // namespace V2_0
173 }  // namespace gnss
174 }  // namespace hardware
175 }  // namespace android
176