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 #pragma once
18 
19 #include <android/hardware/gnss/2.1/IGnssMeasurement.h>
20 #include <hidl/MQDescriptor.h>
21 #include <hidl/Status.h>
22 #include <atomic>
23 #include <mutex>
24 #include <thread>
25 
26 namespace android::hardware::gnss::V2_1::implementation {
27 
28 struct GnssMeasurement : public IGnssMeasurement {
29     GnssMeasurement();
30     ~GnssMeasurement();
31     // Methods from V1_0::IGnssMeasurement follow.
32     Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> setCallback(
33             const sp<V1_0::IGnssMeasurementCallback>& callback) override;
34     Return<void> close() override;
35 
36     // Methods from V1_1::IGnssMeasurement follow.
37     Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> setCallback_1_1(
38             const sp<V1_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) override;
39 
40     // Methods from V2_0::IGnssMeasurement follow.
41     Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> setCallback_2_0(
42             const sp<V2_0::IGnssMeasurementCallback>& callback, bool enableFullTracking) override;
43 
44     // Methods from V2_1::IGnssMeasurement follow.
45     Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> setCallback_2_1(
46             const sp<V2_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) override;
47 
48   private:
49     void start();
50     void stop();
51     void reportMeasurement(const V2_0::IGnssMeasurementCallback::GnssData&);
52     void reportMeasurement(const V2_1::IGnssMeasurementCallback::GnssData&);
53 
54     // Guarded by mMutex
55     static sp<V2_1::IGnssMeasurementCallback> sCallback_2_1;
56 
57     // Guarded by mMutex
58     static sp<V2_0::IGnssMeasurementCallback> sCallback_2_0;
59 
60     std::atomic<long> mMinIntervalMillis;
61     std::atomic<bool> mIsActive;
62     std::thread mThread;
63 
64     // Synchronization lock for sCallback_2_1 and sCallback_2_0
65     mutable std::mutex mMutex;
66 };
67 
68 }  // namespace android::hardware::gnss::V2_1::implementation
69