blob: ebfa7ddf1dea3331a00b2ebb3ed8f607ef779cd6 [file] [log] [blame]
Yu-Han Yangc06b5362019-10-25 14:14:35 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "GnssMeasurement"
18
19#include "GnssMeasurement.h"
20#include <log/log.h>
21#include "Utils.h"
22
23namespace android {
24namespace hardware {
25namespace gnss {
26
27using common::Utils;
28
29namespace V2_1 {
30namespace implementation {
31
32sp<V2_1::IGnssMeasurementCallback> GnssMeasurement::sCallback = nullptr;
33
34GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {}
35
36GnssMeasurement::~GnssMeasurement() {
37 stop();
38}
39
40// Methods from V1_0::IGnssMeasurement follow.
41Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
42 const sp<V1_0::IGnssMeasurementCallback>&) {
43 // TODO implement
44 return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
45}
46
47Return<void> GnssMeasurement::close() {
48 ALOGD("close");
49 std::unique_lock<std::mutex> lock(mMutex);
50 stop();
51 sCallback = nullptr;
52 return Void();
53}
54
55// Methods from V1_1::IGnssMeasurement follow.
56Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
57 const sp<V1_1::IGnssMeasurementCallback>&, bool) {
58 // TODO implement
59 return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
60}
61
62// Methods from V2_0::IGnssMeasurement follow.
63Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
64 const sp<V2_0::IGnssMeasurementCallback>&, bool) {
65 // TODO implement
66 return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
67}
68
69// Methods from V2_1::IGnssMeasurement follow.
70Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_1(
71 const sp<V2_1::IGnssMeasurementCallback>& callback, bool) {
72 ALOGD("setCallback_2_1");
73 std::unique_lock<std::mutex> lock(mMutex);
74 sCallback = callback;
75
76 if (mIsActive) {
77 ALOGW("GnssMeasurement callback already set. Resetting the callback...");
78 stop();
79 }
80 start();
81
82 return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
83}
84
85void GnssMeasurement::start() {
86 ALOGD("start");
87 mIsActive = true;
88 mThread = std::thread([this]() {
89 while (mIsActive == true) {
90 auto measurement = Utils::getMockMeasurementV2_1();
91 this->reportMeasurement(measurement);
92
93 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
94 }
95 });
96}
97
98void GnssMeasurement::stop() {
99 ALOGD("stop");
100 mIsActive = false;
101 if (mThread.joinable()) {
102 mThread.join();
103 }
104}
105
106void GnssMeasurement::reportMeasurement(const GnssDataV2_1& data) {
107 ALOGD("reportMeasurement()");
108 std::unique_lock<std::mutex> lock(mMutex);
109 if (sCallback == nullptr) {
110 ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
111 return;
112 }
113 auto ret = sCallback->gnssMeasurementCb_2_1(data);
114 if (!ret.isOk()) {
115 ALOGE("%s: Unable to invoke callback", __func__);
116 }
117}
118
119} // namespace implementation
120} // namespace V2_1
121} // namespace gnss
122} // namespace hardware
123} // namespace android