• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "v2_1/GnssMeasurement.h"
20 #include <log/log.h>
21 #include "Utils.h"
22 
23 namespace android::hardware::gnss::V2_1::implementation {
24 
25 using common::Utils;
26 
27 sp<V2_1::IGnssMeasurementCallback> GnssMeasurement::sCallback_2_1 = nullptr;
28 sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback_2_0 = nullptr;
29 
GnssMeasurement()30 GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {}
31 
~GnssMeasurement()32 GnssMeasurement::~GnssMeasurement() {
33     stop();
34 }
35 
36 // Methods from V1_0::IGnssMeasurement follow.
setCallback(const sp<V1_0::IGnssMeasurementCallback> &)37 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
38         const sp<V1_0::IGnssMeasurementCallback>&) {
39     // TODO implement
40     return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
41 }
42 
close()43 Return<void> GnssMeasurement::close() {
44     ALOGD("close");
45     stop();
46     std::unique_lock<std::mutex> lock(mMutex);
47     sCallback_2_1 = nullptr;
48     sCallback_2_0 = nullptr;
49     return Void();
50 }
51 
52 // Methods from V1_1::IGnssMeasurement follow.
setCallback_1_1(const sp<V1_1::IGnssMeasurementCallback> &,bool)53 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
54         const sp<V1_1::IGnssMeasurementCallback>&, bool) {
55     // TODO implement
56     return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
57 }
58 
59 // Methods from V2_0::IGnssMeasurement follow.
setCallback_2_0(const sp<V2_0::IGnssMeasurementCallback> & callback,bool)60 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
61         const sp<V2_0::IGnssMeasurementCallback>& callback, bool) {
62     ALOGD("setCallback_2_0");
63     std::unique_lock<std::mutex> lock(mMutex);
64     sCallback_2_0 = callback;
65 
66     if (mIsActive) {
67         ALOGW("GnssMeasurement callback already set. Resetting the callback...");
68         stop();
69     }
70     start();
71 
72     return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
73 }
74 
75 // Methods from V2_1::IGnssMeasurement follow.
setCallback_2_1(const sp<V2_1::IGnssMeasurementCallback> & callback,bool)76 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_1(
77         const sp<V2_1::IGnssMeasurementCallback>& callback, bool) {
78     ALOGD("setCallback_2_1");
79     std::unique_lock<std::mutex> lock(mMutex);
80     sCallback_2_1 = callback;
81 
82     if (mIsActive) {
83         ALOGW("GnssMeasurement callback already set. Resetting the callback...");
84         stop();
85     }
86     start();
87 
88     return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
89 }
90 
start()91 void GnssMeasurement::start() {
92     ALOGD("start");
93     mIsActive = true;
94     mThread = std::thread([this]() {
95         while (mIsActive == true) {
96             if (sCallback_2_1 != nullptr) {
97                 auto measurement = Utils::getMockMeasurementV2_1();
98                 this->reportMeasurement(measurement);
99             } else {
100                 auto measurement = Utils::getMockMeasurementV2_0();
101                 this->reportMeasurement(measurement);
102             }
103 
104             std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
105         }
106     });
107 }
108 
stop()109 void GnssMeasurement::stop() {
110     ALOGD("stop");
111     mIsActive = false;
112     if (mThread.joinable()) {
113         mThread.join();
114     }
115 }
116 
reportMeasurement(const V2_0::IGnssMeasurementCallback::GnssData & data)117 void GnssMeasurement::reportMeasurement(const V2_0::IGnssMeasurementCallback::GnssData& data) {
118     ALOGD("reportMeasurement()");
119     std::unique_lock<std::mutex> lock(mMutex);
120     if (sCallback_2_0 == nullptr) {
121         ALOGE("%s: GnssMeasurement::sCallback_2_0 is null.", __func__);
122         return;
123     }
124     auto ret = sCallback_2_0->gnssMeasurementCb_2_0(data);
125     if (!ret.isOk()) {
126         ALOGE("%s: Unable to invoke callback", __func__);
127     }
128 }
129 
reportMeasurement(const V2_1::IGnssMeasurementCallback::GnssData & data)130 void GnssMeasurement::reportMeasurement(const V2_1::IGnssMeasurementCallback::GnssData& data) {
131     ALOGD("reportMeasurement()");
132     std::unique_lock<std::mutex> lock(mMutex);
133     if (sCallback_2_1 == nullptr) {
134         ALOGE("%s: GnssMeasurement::sCallback_2_1 is null.", __func__);
135         return;
136     }
137     auto ret = sCallback_2_1->gnssMeasurementCb_2_1(data);
138     if (!ret.isOk()) {
139         ALOGE("%s: Unable to invoke callback", __func__);
140     }
141 }
142 
143 }  // namespace android::hardware::gnss::V2_1::implementation
144