1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <cmath> 16 #include <iostream> 17 #include "include/gp_utils.h" 18 #include "include/Power.h" 19 20 namespace OHOS { 21 namespace SmartPerf { 22 pthread_mutex_t Power::mutex; 23 Power *Power::instance = nullptr; 24 getInstance()25 Power *Power::getInstance() 26 { 27 if (instance == nullptr) { 28 pthread_mutex_lock(&mutex); 29 if (instance == nullptr) { 30 instance = new Power(); 31 } 32 pthread_mutex_unlock(&mutex); 33 } 34 return instance; 35 } 36 Power()37 Power::Power() 38 { 39 pthread_mutex_init(&mutex, nullptr); 40 41 for (int i = 0; i < sizeof(power_path) / sizeof(const char *); ++i) { 42 if (GPUtils::canOpen(std::string(power_path[i]))) { 43 power_base_path = std::string(power_path[i]); 44 } 45 } 46 47 char powerNode[256]; 48 for (int j = 0; j < sizeof(default_collect_power_info) / sizeof(const char *); ++j) { 49 if (snprintf(powerNode, sizeof(powerNode), "%s/%s", 50 power_base_path.c_str(), default_collect_power_info[j]) < 0) { 51 std::cout << "snprintf fail"; 52 } 53 // file exists 54 std::string type = std::string(default_collect_power_info[j]); 55 if (power_node_path_map.count(type) > 0) { 56 continue; 57 } 58 power_node_path_map[type] = std::string(powerNode); 59 } 60 } 61 getPowerMap()62 std::map<std::string, std::string> Power::getPowerMap() 63 { 64 std::map<std::string, std::string> power_map; 65 FILE *fp = nullptr; 66 char buffer[256]; 67 std::map<std::string, std::string>::iterator iter; 68 int charging = 1; 69 for (iter = power_node_path_map.begin(); iter != power_node_path_map.end(); ++iter) { 70 std::string type = iter->first; 71 std::string powerNode = power_node_path_map[type]; 72 fp = fopen(powerNode.c_str(), "r"); 73 if (fp == nullptr) { 74 power_map[type] = "-1.0"; 75 continue; 76 } 77 buffer[0] = '\0'; 78 while (fgets(buffer, sizeof(buffer), fp) == nullptr) { 79 std::cout << "fgets fail"; 80 } 81 if (fclose(fp) == EOF) { 82 std::cout << "fclose fail"; 83 } 84 std::string power_value = std::string(buffer); 85 if (iter->first == "status") { 86 if (power_value.find("Charging") == std::string::npos && 87 power_value.find("Full") == std::string::npos) { 88 charging = 0; 89 } 90 if (power_value.find("Discharging") != std::string::npos) { 91 charging = 0; 92 } 93 } else if (iter->first == "enable_hiz") { 94 if (strcmp(buffer, "1") == 0) { 95 charging = 0; 96 } 97 if (std::stoi(buffer) == 1) { 98 charging = 0; 99 } 100 } else if (iter->first == "current_now") { 101 // 若current now 大于 100000 单位归一化为 1000,大于10000 单位归一化为100,大于3000 单位归一化为10 102 double tmp = fabs(std::stof(buffer)); 103 power_value = std::to_string(tmp); 104 } else if (iter->first == "voltage_now") { 105 // 若voltage now 大于100 单位归一化为100 106 double tmp = std::stof(buffer); 107 power_value = std::to_string(tmp); 108 } 109 power_map[type] = power_value; 110 } 111 if (power_map.count("voltage_now") == 0 && power_map.count("bat_id_0") > 0 && 112 power_map.count("bat_id_1") > 0) { 113 power_map["voltage_now"] = 114 std::to_string(std::stof(power_map["bat_id_0"]) + std::stof(power_map["bat_id_1"])); 115 } 116 if (power_map.count("current_now") > 0 && charging) { 117 power_map["current_now"] = "-" + power_map["current_now"]; 118 } 119 if (power_map.count("status") > 0 && charging == 0) { 120 power_map["status"] = "Discharging"; 121 } 122 return power_map; 123 } 124 } 125 }