1 /* 2 * Copyright 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 "btaa/wakelock_processor.h" 18 19 #include "os/log.h" 20 21 namespace bluetooth { 22 namespace activity_attribution { 23 24 static const int kWakelockMaxDurationMs(10000); 25 WakelockProcessor()26WakelockProcessor::WakelockProcessor() { 27 wakelock_net_count_ = 0; 28 wakelock_acquired_time_ = {}; 29 } 30 OnWakelockReleased()31uint32_t WakelockProcessor::OnWakelockReleased() { 32 auto cur_time = std::chrono::system_clock::now(); 33 uint32_t wakelock_duration_ms = 0; 34 35 if (wakelock_net_count_ == 0) { 36 LOG_INFO("Release a never acquired wakelock, ignored."); 37 } else { 38 wakelock_net_count_--; 39 if (wakelock_net_count_ == 0) { 40 wakelock_duration_ms = static_cast<uint32_t>( 41 std::chrono::duration_cast<std::chrono::milliseconds>(cur_time - wakelock_acquired_time_).count()); 42 wakelock_acquired_time_ = {}; 43 } 44 } 45 46 return wakelock_duration_ms; 47 } 48 OnWakelockAcquired()49void WakelockProcessor::OnWakelockAcquired() { 50 auto cur_time = std::chrono::system_clock::now(); 51 52 if (wakelock_net_count_ == 0) { 53 if (wakelock_acquired_time_.time_since_epoch().count()) { 54 LOG_INFO("Previous wakelock acquired time is not consumed, dropped."); 55 } 56 wakelock_acquired_time_ = cur_time; 57 } else if (cur_time - wakelock_acquired_time_ > std::chrono::milliseconds(kWakelockMaxDurationMs)) { 58 LOG_INFO("Wakelock held for too long, likely we missed a release notification. Resetting wakelock stats."); 59 wakelock_net_count_ = 0; 60 wakelock_acquired_time_ = cur_time; 61 } 62 63 wakelock_net_count_++; 64 } 65 66 } // namespace activity_attribution 67 } // namespace bluetooth 68