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 "LocSvc_GnssMeasurementInterface"
22
23#include <log_util.h>
24#include <GnssMeasurementAPIClient.h>
25#include "GnssMeasurement.h"
26
27namespace android {
28namespace hardware {
29namespace gnss {
30namespace V1_0 {
31namespace implementation {
32
33void 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
42GnssMeasurement::GnssMeasurement() {
43    mGnssMeasurementDeathRecipient = new GnssMeasurementDeathRecipient(this);
44    mApi = new GnssMeasurementAPIClient();
45}
46
47GnssMeasurement::~GnssMeasurement() {
48    if (mApi) {
49        delete mApi;
50        mApi = nullptr;
51    }
52}
53
54// Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
55Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
56        const sp<IGnssMeasurementCallback>& callback)  {
57
58    Return<IGnssMeasurement::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 /*cookie*/);
76
77    return mApi->gnssMeasurementSetCallback(callback);
78}
79
80Return<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    mApi->gnssMeasurementClose();
91
92    return Void();
93}
94
95}  // namespace implementation
96}  // namespace V1_0
97}  // namespace gnss
98}  // namespace hardware
99}  // namespace android
100