1 /*
2  * Copyright (C) 2020 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 #include <utils/SystemClock.h>
18 #include <aidl/android/hardware/gnss/IGnss.h>
19 
20 #include "GnssPowerIndication.h"
21 #include "debug.h"
22 
23 namespace aidl {
24 namespace android {
25 namespace hardware {
26 namespace gnss {
27 namespace implementation {
28 
GnssPowerIndication(std::function<double ()> getRunningTime)29 GnssPowerIndication::GnssPowerIndication(std::function<double()> getRunningTime)
30         : mGetRunningTime(std::move(getRunningTime)) {}
31 
setCallback(const std::shared_ptr<IGnssPowerIndicationCallback> & callback)32 ndk::ScopedAStatus GnssPowerIndication::setCallback(
33         const std::shared_ptr<IGnssPowerIndicationCallback>& callback) {
34     mCb = callback;
35 
36     callback->setCapabilitiesCb(IGnssPowerIndicationCallback::CAPABILITY_TOTAL |
37                                 IGnssPowerIndicationCallback::CAPABILITY_SINGLEBAND_TRACKING |
38                                 IGnssPowerIndicationCallback::CAPABILITY_MULTIBAND_TRACKING |
39                                 IGnssPowerIndicationCallback::CAPABILITY_SINGLEBAND_ACQUISITION |
40                                 IGnssPowerIndicationCallback::CAPABILITY_MULTIBAND_ACQUISITION |
41                                 IGnssPowerIndicationCallback::CAPABILITY_OTHER_MODES);
42 
43     return ndk::ScopedAStatus::ok();
44 }
45 
requestGnssPowerStats()46 ndk::ScopedAStatus GnssPowerIndication::requestGnssPowerStats() {
47     if (mCb) {
48         return doRequestGnssPowerStats(*mCb);
49     } else {
50         return ndk::ScopedAStatus::fromExceptionCode(FAILURE(IGnss::ERROR_INVALID_ARGUMENT));
51     }
52 }
53 
doRequestGnssPowerStats(IGnssPowerIndicationCallback & cb)54 ndk::ScopedAStatus GnssPowerIndication::doRequestGnssPowerStats(
55         IGnssPowerIndicationCallback& cb) {
56     GnssPowerStats gnssPowerStats;
57 
58     const double d = mGetRunningTime();
59 
60     if (d > 0.0) {
61         ElapsedRealtime elapsedRealtime = {
62                 .flags = ElapsedRealtime::HAS_TIMESTAMP_NS |
63                          ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS,
64                 .timestampNs = ::android::elapsedRealtimeNano(),
65                 .timeUncertaintyNs = 1000,
66         };
67 
68         gnssPowerStats.elapsedRealtime = elapsedRealtime;
69         gnssPowerStats.totalEnergyMilliJoule = 1.500e+3 + d * 22.0;
70         gnssPowerStats.singlebandTrackingModeEnergyMilliJoule = 0.0;
71         gnssPowerStats.multibandTrackingModeEnergyMilliJoule = 1.28e+2 + d * 4.0;
72         gnssPowerStats.singlebandAcquisitionModeEnergyMilliJoule = 0.0;
73         gnssPowerStats.multibandAcquisitionModeEnergyMilliJoule = 3.65e+2 + d * 15.0;
74         gnssPowerStats.otherModesEnergyMilliJoule = {1.232e+2, 3.234e+3};
75     }
76 
77     cb.gnssPowerStatsCb(gnssPowerStats);
78 
79     return ndk::ScopedAStatus::ok();
80 }
81 
82 }  // namespace implementation
83 }  // namespace gnss
84 }  // namespace hardware
85 }  // namespace android
86 }  // namespace aidl
87