Home | History | Annotate | Download | only in android_internal
      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 #include "src/android_internal/power_stats_hal.h"
     18 
     19 #include <string.h>
     20 
     21 #include <algorithm>
     22 
     23 #include <android/hardware/power/stats/1.0/IPowerStats.h>
     24 
     25 namespace perfetto {
     26 namespace android_internal {
     27 
     28 using android::hardware::hidl_vec;
     29 using android::hardware::Return;
     30 using android::hardware::power::stats::V1_0::EnergyData;
     31 using android::hardware::power::stats::V1_0::IPowerStats;
     32 using android::hardware::power::stats::V1_0::RailInfo;
     33 using android::hardware::power::stats::V1_0::Status;
     34 
     35 namespace {
     36 
     37 android::sp<IPowerStats> g_svc;
     38 
     39 bool GetService() {
     40   if (!g_svc)
     41     g_svc = IPowerStats::getService();
     42 
     43   return g_svc != nullptr;
     44 }
     45 
     46 }  // namespace
     47 
     48 bool GetAvailableRails(RailDescriptor* rail_descriptors, size_t* size_of_arr) {
     49   const size_t in_array_size = *size_of_arr;
     50   *size_of_arr = 0;
     51   if (!GetService())
     52     return false;
     53 
     54   Status status;
     55   auto rails_cb = [rail_descriptors, size_of_arr, &in_array_size, &status](
     56                       hidl_vec<RailInfo> r, Status s) {
     57     status = s;
     58     if (status == Status::SUCCESS) {
     59       *size_of_arr = std::min(in_array_size, r.size());
     60       for (int i = 0; i < *size_of_arr; ++i) {
     61         const RailInfo& rail_info = r[i];
     62         RailDescriptor& descriptor = rail_descriptors[i];
     63 
     64         descriptor.index = rail_info.index;
     65         descriptor.sampling_rate = rail_info.samplingRate;
     66 
     67         strncpy(descriptor.rail_name, rail_info.railName.c_str(),
     68                 sizeof(descriptor.rail_name));
     69         strncpy(descriptor.subsys_name, rail_info.subsysName.c_str(),
     70                 sizeof(descriptor.subsys_name));
     71         descriptor.rail_name[sizeof(descriptor.rail_name) - 1] = '\0';
     72         descriptor.subsys_name[sizeof(descriptor.subsys_name) - 1] = '\0';
     73       }
     74     }
     75   };
     76 
     77   Return<void> ret = g_svc->getRailInfo(rails_cb);
     78   return status == Status::SUCCESS;
     79 }
     80 
     81 bool GetRailEnergyData(RailEnergyData* rail_energy_array, size_t* size_of_arr) {
     82   const size_t in_array_size = *size_of_arr;
     83   *size_of_arr = 0;
     84 
     85   if (!GetService())
     86     return false;
     87 
     88   Status status;
     89   auto energy_cb = [rail_energy_array, size_of_arr, &in_array_size, &status](
     90                        hidl_vec<EnergyData> m, Status s) {
     91     status = s;
     92     if (status == Status::SUCCESS) {
     93       *size_of_arr = std::min(in_array_size, m.size());
     94       for (int i = 0; i < *size_of_arr; ++i) {
     95         const EnergyData& measurement = m[i];
     96         RailEnergyData& element = rail_energy_array[i];
     97 
     98         element.index = measurement.index;
     99         element.timestamp = measurement.timestamp;
    100         element.energy = measurement.energy;
    101       }
    102     }
    103   };
    104 
    105   Return<void> ret = g_svc->getEnergyData(hidl_vec<uint32_t>(), energy_cb);
    106   return status == Status::SUCCESS;
    107 }
    108 
    109 }  // namespace android_internal
    110 }  // namespace perfetto
    111