1 /*
2  * Copyright (c) 2017-2018, 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 <MeasurementAPIClient.h>
25 #include "GnssMeasurement.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace gnss {
30 namespace V1_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         mApi->destroy();
50         mApi = nullptr;
51     }
52 }
53 
54 // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
55 
setCallback(const sp<V1_0::IGnssMeasurementCallback> & callback)56 Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
57         const sp<V1_0::IGnssMeasurementCallback>& callback)  {
58 
59     Return<IGnssMeasurement::GnssMeasurementStatus> ret =
60         IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
61     if (mGnssMeasurementCbIface != nullptr) {
62         LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
63         return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
64     }
65 
66     if (callback == nullptr) {
67         LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
68         return ret;
69     }
70     if (mApi == nullptr) {
71         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
72         return ret;
73     }
74 
75     mGnssMeasurementCbIface = callback;
76     mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
77 
78     return mApi->measurementSetCallback(callback);
79 
80 }
81 
close()82 Return<void> GnssMeasurement::close()  {
83     if (mApi == nullptr) {
84         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
85         return Void();
86     }
87 
88     if (mGnssMeasurementCbIface != nullptr) {
89         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
90         mGnssMeasurementCbIface = nullptr;
91     }
92     if (mGnssMeasurementCbIface_1_1 != nullptr) {
93         mGnssMeasurementCbIface_1_1->unlinkToDeath(mGnssMeasurementDeathRecipient);
94         mGnssMeasurementCbIface_1_1 = nullptr;
95     }
96     mApi->measurementClose();
97 
98     return Void();
99 }
100 
101 // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
setCallback_1_1(const sp<IGnssMeasurementCallback> & callback,bool enableFullTracking)102 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
103         const sp<IGnssMeasurementCallback>& callback, bool enableFullTracking) {
104 
105     Return<IGnssMeasurement::GnssMeasurementStatus> ret =
106         IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
107     if (mGnssMeasurementCbIface_1_1 != nullptr) {
108         LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
109         return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
110     }
111 
112     if (callback == nullptr) {
113         LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
114         return ret;
115     }
116     if (nullptr == mApi) {
117         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
118         return ret;
119     }
120 
121     mGnssMeasurementCbIface_1_1 = callback;
122     mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
123 
124     GnssPowerMode powerMode = enableFullTracking?
125             GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
126 
127     return mApi->measurementSetCallback_1_1(callback, powerMode);
128 }
129 
130 }  // namespace implementation
131 }  // namespace V1_1
132 }  // namespace gnss
133 }  // namespace hardware
134 }  // namespace android
135