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 #define LOG_TAG "GnssPowerIndicationAidl"
18 
19 #include "GnssPowerIndication.h"
20 #include <aidl/android/hardware/gnss/BnGnss.h>
21 #include <log/log.h>
22 #include <utils/SystemClock.h>
23 
24 namespace aidl::android::hardware::gnss {
25 
26 std::shared_ptr<IGnssPowerIndicationCallback> GnssPowerIndication::sCallback = nullptr;
27 
setCallback(const std::shared_ptr<IGnssPowerIndicationCallback> & callback)28 ndk::ScopedAStatus GnssPowerIndication::setCallback(
29         const std::shared_ptr<IGnssPowerIndicationCallback>& callback) {
30     ALOGD("setCallback");
31     std::unique_lock<std::mutex> lock(mMutex);
32     sCallback = callback;
33     sCallback->setCapabilitiesCb(IGnssPowerIndicationCallback::CAPABILITY_TOTAL |
34                                  IGnssPowerIndicationCallback::CAPABILITY_SINGLEBAND_TRACKING |
35                                  IGnssPowerIndicationCallback::CAPABILITY_MULTIBAND_TRACKING |
36                                  IGnssPowerIndicationCallback::CAPABILITY_SINGLEBAND_ACQUISITION |
37                                  IGnssPowerIndicationCallback::CAPABILITY_MULTIBAND_ACQUISITION |
38                                  IGnssPowerIndicationCallback::CAPABILITY_OTHER_MODES);
39     return ndk::ScopedAStatus::ok();
40 }
41 
requestGnssPowerStats()42 ndk::ScopedAStatus GnssPowerIndication::requestGnssPowerStats() {
43     ALOGD("requestGnssPowerStats");
44     std::unique_lock<std::mutex> lock(mMutex);
45 
46     ElapsedRealtime elapsedRealtime = {
47             .flags = ElapsedRealtime::HAS_TIMESTAMP_NS | ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS,
48             .timestampNs = ::android::elapsedRealtimeNano(),
49             .timeUncertaintyNs = 1000,
50     };
51     GnssPowerStats gnssPowerStats = {
52             .elapsedRealtime = elapsedRealtime,
53             .totalEnergyMilliJoule = 1.500e+3 + numLocationReported * 22.0,
54             .singlebandTrackingModeEnergyMilliJoule = 0.0,
55             .multibandTrackingModeEnergyMilliJoule = 1.28e+2 + numLocationReported * 4.0,
56             .singlebandAcquisitionModeEnergyMilliJoule = 0.0,
57             .multibandAcquisitionModeEnergyMilliJoule = 3.65e+2 + numLocationReported * 15.0,
58             .otherModesEnergyMilliJoule = {1.232e+2, 3.234e+3},
59     };
60     sCallback->gnssPowerStatsCb(gnssPowerStats);
61     return ndk::ScopedAStatus::ok();
62 }
63 
notePowerConsumption()64 void GnssPowerIndication::notePowerConsumption() {
65     numLocationReported++;
66 }
67 
68 }  // namespace aidl::android::hardware::gnss
69