1 /*
2 * Copyright (c) 2022 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
16 #include "hdi_service_test.h"
17 #include <chrono>
18 #include <condition_variable>
19 #include <csignal>
20 #include <cstring>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <fstream>
24 #include <iostream>
25 #include <memory>
26 #include <mutex>
27 #include <streambuf>
28 #include <sys/stat.h>
29 #include <thread>
30 #include <vector>
31 #include "battery_service.h"
32 #include "battery_thread_test.h"
33 #include "battery_vibrate.h"
34 #include "hdf_base.h"
35 #include "power_supply_provider.h"
36 #include "utils/hdf_log.h"
37
38 #define HDF_LOG_TAG HdiServiceTest
39
40 using namespace testing::ext;
41 using namespace OHOS;
42 using namespace OHOS::HDI::Battery::V1_0;
43 using namespace OHOS::PowerMgr;
44 using namespace std;
45
46 namespace HdiServiceTest {
47 const std::string SYSTEM_BATTERY_PATH = "/sys/class/power_supply";
48 static std::vector<std::string> g_filenodeName;
49 static std::map<std::string, std::string> g_nodeInfo;
50 const int STR_TO_LONG_LEN = 10;
51 const int NUM_ZERO = 0;
52 const int32_t ERROR = -1;
53 const int MAX_BUFF_SIZE = 128;
54 std::unique_ptr<PowerSupplyProvider> giver_ = nullptr;
55
SetUpTestCase(void)56 void HdiServiceTest::SetUpTestCase(void)
57 {
58 giver_ = std::make_unique<PowerSupplyProvider>();
59 if (giver_ == nullptr) {
60 HDF_LOGI("%{public}s: Failed to get PowerSupplyProvider", __func__);
61 }
62 }
63
TearDownTestCase(void)64 void HdiServiceTest::TearDownTestCase(void)
65 {
66 }
67
SetUp(void)68 void HdiServiceTest::SetUp(void)
69 {
70 }
71
TearDown(void)72 void HdiServiceTest::TearDown(void)
73 {
74 }
75
76 struct StringEnumMap {
77 const char* str;
78 int32_t enumVal;
79 };
80
CreateFile(std::string path,std::string content)81 std::string CreateFile(std::string path, std::string content)
82 {
83 std::ofstream stream(path.c_str());
84 if (!stream.is_open()) {
85 HDF_LOGI("%{public}s: Cannot create file %{public}s", __func__, path.c_str());
86 return nullptr;
87 }
88 stream << content.c_str() << std::endl;
89 stream.close();
90 return path;
91 }
92
CheckSubfolderNode(const std::string & path)93 static void CheckSubfolderNode(const std::string& path)
94 {
95 DIR *dir = nullptr;
96 struct dirent* entry = nullptr;
97 std::string batteryPath = SYSTEM_BATTERY_PATH + "/" + path;
98 HDF_LOGI("%{public}s: subfolder path is:%{public}s", __func__, batteryPath.c_str());
99
100 dir = opendir(batteryPath.c_str());
101 if (dir == nullptr) {
102 HDF_LOGI("%{public}s: subfolder file is not exist.", __func__);
103 return;
104 }
105
106 while (true) {
107 entry = readdir(dir);
108 if (entry == nullptr) {
109 break;
110 }
111
112 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
113 continue;
114 }
115
116 if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
117 continue;
118 }
119
120 if ((strcmp(entry->d_name, "type") == 0) && (g_nodeInfo["type"] == "") &&
121 (strcasecmp(path.c_str(), "battery") != 0)) {
122 g_nodeInfo["type"] = path;
123 }
124
125 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
126 if ((strcmp(entry->d_name, iter->first.c_str()) == 0) && (g_nodeInfo[iter->first] == "")) {
127 g_nodeInfo[iter->first] = path;
128 }
129 }
130 }
131 closedir(dir);
132 }
133
TraversalBaseNode()134 static void TraversalBaseNode()
135 {
136 g_nodeInfo.insert(std::make_pair("type", ""));
137 g_nodeInfo.insert(std::make_pair("online", ""));
138 g_nodeInfo.insert(std::make_pair("current_max", ""));
139 g_nodeInfo.insert(std::make_pair("voltage_max", ""));
140 g_nodeInfo.insert(std::make_pair("capacity", ""));
141 g_nodeInfo.insert(std::make_pair("voltage_now", ""));
142 g_nodeInfo.insert(std::make_pair("temp", ""));
143 g_nodeInfo.insert(std::make_pair("health", ""));
144 g_nodeInfo.insert(std::make_pair("status", ""));
145 g_nodeInfo.insert(std::make_pair("present", ""));
146 g_nodeInfo.insert(std::make_pair("charge_counter", ""));
147 g_nodeInfo.insert(std::make_pair("technology", ""));
148 g_nodeInfo.insert(std::make_pair("charge_full", ""));
149 g_nodeInfo.insert(std::make_pair("current_avg", ""));
150 g_nodeInfo.insert(std::make_pair("current_now", ""));
151 g_nodeInfo.insert(std::make_pair("charge_now", ""));
152
153 auto iter = g_filenodeName.begin();
154 while (iter != g_filenodeName.end()) {
155 if (*iter == "battery") {
156 CheckSubfolderNode(*iter);
157 iter = g_filenodeName.erase(iter);
158 } else {
159 iter++;
160 }
161 }
162
163 iter = g_filenodeName.begin();
164 while (iter != g_filenodeName.end()) {
165 if (*iter == "Battery") {
166 CheckSubfolderNode(*iter);
167 iter = g_filenodeName.erase(iter);
168 } else {
169 iter++;
170 }
171 }
172
173 for (auto it = g_filenodeName.begin(); it != g_filenodeName.end(); ++it) {
174 CheckSubfolderNode(*it);
175 }
176 }
177
InitBaseSysfs(void)178 static int32_t InitBaseSysfs(void)
179 {
180 DIR* dir = nullptr;
181 struct dirent* entry = nullptr;
182 int32_t index = 0;
183
184 dir = opendir(SYSTEM_BATTERY_PATH.c_str());
185 if (dir == nullptr) {
186 HDF_LOGE("%{public}s: cannot open POWER_SUPPLY_BASE_PATH", __func__);
187 return HDF_ERR_IO;
188 }
189
190 while (true) {
191 entry = readdir(dir);
192 if (entry == nullptr) {
193 break;
194 }
195
196 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
197 continue;
198 }
199
200 if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
201 HDF_LOGI("%{public}s: init sysfs info of %{public}s", __func__, entry->d_name);
202 if (index >= MAX_SYSFS_SIZE) {
203 HDF_LOGE("%{public}s: too many plugged types", __func__);
204 break;
205 }
206 g_filenodeName.emplace_back(entry->d_name);
207 index++;
208 }
209 }
210
211 TraversalBaseNode();
212 HDF_LOGI("%{public}s: index is %{public}d", __func__, index);
213 closedir(dir);
214
215 return HDF_SUCCESS;
216 }
217
ReadTemperatureSysfs()218 static int32_t ReadTemperatureSysfs()
219 {
220 int strlen = 10;
221 char buf[128] = {0};
222 int32_t readSize;
223 InitBaseSysfs();
224 std::string tempNode = "battery";
225 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
226 if (iter->first == "temp") {
227 tempNode = iter->second;
228 break;
229 }
230 }
231 std::string sysBattTemPath = SYSTEM_BATTERY_PATH + "/" + tempNode + "/" + "temp";
232 HDF_LOGE("%{public}s: sysBattTemPath is %{public}s", __func__, sysBattTemPath.c_str());
233
234 int fd = open(sysBattTemPath.c_str(), O_RDONLY);
235 if (fd < NUM_ZERO) {
236 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysBattTemPath.c_str());
237 return HDF_FAILURE;
238 }
239
240 readSize = read(fd, buf, sizeof(buf) - 1);
241 if (readSize < NUM_ZERO) {
242 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysBattTemPath.c_str());
243 close(fd);
244 return HDF_FAILURE;
245 }
246
247 buf[readSize] = '\0';
248 int32_t battTemperature = strtol(buf, nullptr, strlen);
249 if (battTemperature < NUM_ZERO) {
250 HDF_LOGE("%{public}s: read system file temperature is %{public}d", __func__, battTemperature);
251 }
252 HDF_LOGE("%{public}s: read system file temperature is %{public}d", __func__, battTemperature);
253 close(fd);
254 return battTemperature;
255 }
256
ReadVoltageSysfs()257 static int32_t ReadVoltageSysfs()
258 {
259 int strlen = 10;
260 char buf[128] = {0};
261 int32_t readSize;
262 std::string voltageNode = "battery";
263 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
264 if (iter->first == "voltage_now") {
265 voltageNode = iter->second;
266 break;
267 }
268 }
269 std::string sysBattVolPath = SYSTEM_BATTERY_PATH + "/" + voltageNode + "/" + "voltage_now";
270 HDF_LOGE("%{public}s: sysBattVolPath is %{public}s", __func__, sysBattVolPath.c_str());
271
272 int fd = open(sysBattVolPath.c_str(), O_RDONLY);
273 if (fd < NUM_ZERO) {
274 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysBattVolPath.c_str());
275 return HDF_FAILURE;
276 }
277
278 readSize = read(fd, buf, sizeof(buf) - 1);
279 if (readSize < HDF_SUCCESS) {
280 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysBattVolPath.c_str());
281 close(fd);
282 return HDF_FAILURE;
283 }
284
285 buf[readSize] = '\0';
286 int32_t battVoltage = strtol(buf, nullptr, strlen);
287 if (battVoltage < NUM_ZERO) {
288 HDF_LOGE("%{public}s: read system file voltage is %{public}d", __func__, battVoltage);
289 }
290 HDF_LOGE("%{public}s: read system file voltage is %{public}d", __func__, battVoltage);
291 close(fd);
292 return battVoltage;
293 }
294
ReadCapacitySysfs()295 static int32_t ReadCapacitySysfs()
296 {
297 int strlen = 10;
298 char buf[128] = {0};
299 int32_t readSize;
300 std::string capacityNode = "battery";
301 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
302 if (iter->first == "capacity") {
303 capacityNode = iter->second;
304 break;
305 }
306 }
307 std::string sysBattCapPath = SYSTEM_BATTERY_PATH + "/" + capacityNode + "/" + "capacity";
308 HDF_LOGE("%{public}s: sysBattCapPath is %{public}s", __func__, sysBattCapPath.c_str());
309
310 int fd = open(sysBattCapPath.c_str(), O_RDONLY);
311 if (fd < NUM_ZERO) {
312 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysBattCapPath.c_str());
313 return HDF_FAILURE;
314 }
315
316 readSize = read(fd, buf, sizeof(buf) - 1);
317 if (readSize < NUM_ZERO) {
318 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysBattCapPath.c_str());
319 close(fd);
320 return HDF_FAILURE;
321 }
322
323 buf[readSize] = '\0';
324 int32_t battCapacity = strtol(buf, nullptr, strlen);
325 if (battCapacity < NUM_ZERO) {
326 HDF_LOGE("%{public}s: read system file capacity is %{public}d", __func__, battCapacity);
327 }
328 HDF_LOGE("%{public}s: read system file capacity is %{public}d", __func__, battCapacity);
329 close(fd);
330 return battCapacity;
331 }
332
ReadTotalEnergySysfs()333 static int32_t ReadTotalEnergySysfs()
334 {
335 int strlen = 10;
336 char buf[128] = {0};
337 int32_t readSize;
338 InitBaseSysfs();
339 std::string totalEnergyNode = "battery";
340 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
341 if (iter->first == "charge_full") {
342 totalEnergyNode = iter->second;
343 break;
344 }
345 }
346 std::string sysBattTotalEnergyPath = SYSTEM_BATTERY_PATH + "/" + totalEnergyNode + "/" + "charge_full";
347 HDF_LOGE("%{public}s: sysBattTotalEnergyPath is %{public}s", __func__, sysBattTotalEnergyPath.c_str());
348
349 int fd = open(sysBattTotalEnergyPath.c_str(), O_RDONLY);
350 if (fd < NUM_ZERO) {
351 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysBattTotalEnergyPath.c_str());
352 return HDF_FAILURE;
353 }
354
355 readSize = read(fd, buf, sizeof(buf) - 1);
356 if (readSize < NUM_ZERO) {
357 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysBattTotalEnergyPath.c_str());
358 close(fd);
359 return HDF_FAILURE;
360 }
361
362 buf[readSize] = '\0';
363 int32_t totalEnergy = strtol(buf, nullptr, strlen);
364 if (totalEnergy < NUM_ZERO) {
365 HDF_LOGE("%{public}s: read system file totalEnergy is %{public}d", __func__, totalEnergy);
366 }
367 HDF_LOGE("%{public}s: read system file totalEnergy is %{public}d", __func__, totalEnergy);
368 close(fd);
369 return totalEnergy;
370 }
371
ReadCurrentAverageSysfs()372 static int32_t ReadCurrentAverageSysfs()
373 {
374 int strlen = 10;
375 char buf[128] = {0};
376 int32_t readSize;
377 InitBaseSysfs();
378 std::string currentAvgNode = "battery";
379 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
380 if (iter->first == "current_avg") {
381 currentAvgNode = iter->second;
382 break;
383 }
384 }
385 std::string sysBattCurrentAvgPath = SYSTEM_BATTERY_PATH + "/" + currentAvgNode + "/" + "current_avg";
386 HDF_LOGE("%{public}s: sysBattCurrentAvgPath is %{public}s", __func__, sysBattCurrentAvgPath.c_str());
387
388 int fd = open(sysBattCurrentAvgPath.c_str(), O_RDONLY);
389 if (fd < NUM_ZERO) {
390 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysBattCurrentAvgPath.c_str());
391 return HDF_FAILURE;
392 }
393
394 readSize = read(fd, buf, sizeof(buf) - 1);
395 if (readSize < NUM_ZERO) {
396 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysBattCurrentAvgPath.c_str());
397 close(fd);
398 return HDF_FAILURE;
399 }
400
401 buf[readSize] = '\0';
402 int32_t currentAvg = strtol(buf, nullptr, strlen);
403 if (currentAvg < NUM_ZERO) {
404 HDF_LOGE("%{public}s: read system file currentAvg is %{public}d", __func__, currentAvg);
405 }
406 HDF_LOGE("%{public}s: read system file currentAvg is %{public}d", __func__, currentAvg);
407 close(fd);
408 return currentAvg;
409 }
410
ReadCurrentNowSysfs()411 static int32_t ReadCurrentNowSysfs()
412 {
413 int strlen = 10;
414 char buf[128] = {0};
415 int32_t readSize;
416 InitBaseSysfs();
417 std::string currentNowNode = "battery";
418 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
419 if (iter->first == "current_now") {
420 currentNowNode = iter->second;
421 break;
422 }
423 }
424 std::string sysBattCurrentNowPath = SYSTEM_BATTERY_PATH + "/" + currentNowNode + "/" + "current_now";
425 HDF_LOGE("%{public}s: sysBattCurrentNowPath is %{public}s", __func__, sysBattCurrentNowPath.c_str());
426
427 int fd = open(sysBattCurrentNowPath.c_str(), O_RDONLY);
428 if (fd < NUM_ZERO) {
429 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysBattCurrentNowPath.c_str());
430 return HDF_FAILURE;
431 }
432
433 readSize = read(fd, buf, sizeof(buf) - 1);
434 if (readSize < NUM_ZERO) {
435 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysBattCurrentNowPath.c_str());
436 close(fd);
437 return HDF_FAILURE;
438 }
439
440 buf[readSize] = '\0';
441 int32_t currentNow = strtol(buf, nullptr, strlen);
442 if (currentNow < NUM_ZERO) {
443 HDF_LOGE("%{public}s: read system file currentNow is %{public}d", __func__, currentNow);
444 }
445 HDF_LOGE("%{public}s: read system file currentNow is %{public}d", __func__, currentNow);
446 close(fd);
447 return currentNow;
448 }
449
ReadRemainEnergySysfs()450 static int32_t ReadRemainEnergySysfs()
451 {
452 int strlen = 10;
453 char buf[128] = {0};
454 int32_t readSize;
455 InitBaseSysfs();
456 std::string chargeNowNode = "battery";
457 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
458 if (iter->first == "charge_now") {
459 chargeNowNode = iter->second;
460 break;
461 }
462 }
463 std::string sysBattChargeNowPath = SYSTEM_BATTERY_PATH + "/" + chargeNowNode + "/" + "charge_now";
464 HDF_LOGE("%{public}s: sysBattChargeNowPath is %{public}s", __func__, sysBattChargeNowPath.c_str());
465
466 int fd = open(sysBattChargeNowPath.c_str(), O_RDONLY);
467 if (fd < NUM_ZERO) {
468 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysBattChargeNowPath.c_str());
469 return HDF_FAILURE;
470 }
471
472 readSize = read(fd, buf, sizeof(buf) - 1);
473 if (readSize < NUM_ZERO) {
474 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysBattChargeNowPath.c_str());
475 close(fd);
476 return HDF_FAILURE;
477 }
478
479 buf[readSize] = '\0';
480 int32_t chargeNow = strtol(buf, nullptr, strlen);
481 if (chargeNow < NUM_ZERO) {
482 HDF_LOGE("%{public}s: read system file chargeNow is %{public}d", __func__, chargeNow);
483 }
484 HDF_LOGE("%{public}s: read system file chargeNow is %{public}d", __func__, chargeNow);
485 close(fd);
486 return chargeNow;
487 }
488
Trim(char * str)489 static void Trim(char* str)
490 {
491 if (str == nullptr) {
492 return;
493 }
494 str[strcspn(str, "\n")] = 0;
495 }
496
HealthStateEnumConverter(const char * str)497 static int32_t HealthStateEnumConverter(const char* str)
498 {
499 struct StringEnumMap healthStateEnumMap[] = {
500 { "Good", PowerSupplyProvider::BATTERY_HEALTH_GOOD },
501 { "Cold", PowerSupplyProvider::BATTERY_HEALTH_COLD },
502 { "Warm", PowerSupplyProvider::BATTERY_HEALTH_GOOD }, // JEITA specification
503 { "Cool", PowerSupplyProvider::BATTERY_HEALTH_GOOD }, // JEITA specification
504 { "Hot", PowerSupplyProvider::BATTERY_HEALTH_OVERHEAT }, // JEITA specification
505 { "Overheat", PowerSupplyProvider::BATTERY_HEALTH_OVERHEAT },
506 { "Over voltage", PowerSupplyProvider::BATTERY_HEALTH_OVERVOLTAGE },
507 { "Dead", PowerSupplyProvider::BATTERY_HEALTH_DEAD },
508 { "Unknown", PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN },
509 { "Unspecified failure", PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN },
510 { NULL, PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN },
511 };
512
513 for (int i = 0; healthStateEnumMap[i].str; ++i) {
514 if (strcmp(str, healthStateEnumMap[i].str) == 0) {
515 return healthStateEnumMap[i].enumVal;
516 }
517 }
518
519 return PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN;
520 }
521
ReadHealthStateSysfs()522 static int32_t ReadHealthStateSysfs()
523 {
524 char buf[128] = {0};
525 int32_t readSize;
526 std::string healthNode = "battery";
527 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
528 if (iter->first == "health") {
529 healthNode = iter->second;
530 break;
531 }
532 }
533 std::string sysHealthStatePath = SYSTEM_BATTERY_PATH + "/" + healthNode + "/" + "health";
534 HDF_LOGE("%{public}s: sysHealthStatePath is %{public}s", __func__, sysHealthStatePath.c_str());
535
536 int fd = open(sysHealthStatePath.c_str(), O_RDONLY);
537 if (fd < NUM_ZERO) {
538 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysHealthStatePath.c_str());
539 return HDF_FAILURE;
540 }
541
542 readSize = read(fd, buf, sizeof(buf) - 1);
543 if (readSize < NUM_ZERO) {
544 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysHealthStatePath.c_str());
545 close(fd);
546 return HDF_FAILURE;
547 }
548
549 Trim(buf);
550
551 int32_t battHealthState = HealthStateEnumConverter(buf);
552 HDF_LOGE("%{public}s: read system file healthState is %{public}d", __func__, battHealthState);
553 close(fd);
554 return battHealthState;
555 }
556
PluggedTypeEnumConverter(const char * str)557 static int32_t PluggedTypeEnumConverter(const char* str)
558 {
559 struct StringEnumMap pluggedTypeEnumMap[] = {
560 { "USB", PowerSupplyProvider::PLUGGED_TYPE_USB },
561 { "USB_PD_DRP", PowerSupplyProvider::PLUGGED_TYPE_USB },
562 { "Wireless", PowerSupplyProvider::PLUGGED_TYPE_WIRELESS },
563 { "Mains", PowerSupplyProvider::PLUGGED_TYPE_AC },
564 { "UPS", PowerSupplyProvider::PLUGGED_TYPE_AC },
565 { "USB_ACA", PowerSupplyProvider::PLUGGED_TYPE_AC },
566 { "USB_C", PowerSupplyProvider::PLUGGED_TYPE_AC },
567 { "USB_CDP", PowerSupplyProvider::PLUGGED_TYPE_AC },
568 { "USB_DCP", PowerSupplyProvider::PLUGGED_TYPE_AC },
569 { "USB_HVDCP", PowerSupplyProvider::PLUGGED_TYPE_AC },
570 { "USB_PD", PowerSupplyProvider::PLUGGED_TYPE_AC },
571 { "Unknown", PowerSupplyProvider::PLUGGED_TYPE_BUTT },
572 { NULL, PowerSupplyProvider::PLUGGED_TYPE_BUTT },
573 };
574
575 for (int i = 0; pluggedTypeEnumMap[i].str; ++i) {
576 if (strcmp(str, pluggedTypeEnumMap[i].str) == 0) {
577 return pluggedTypeEnumMap[i].enumVal;
578 }
579 }
580
581 return PowerSupplyProvider::PLUGGED_TYPE_BUTT;
582 }
583
ReadSysfsFile(const char * path,char * buf,size_t size)584 int32_t ReadSysfsFile(const char* path, char* buf, size_t size)
585 {
586 int fd = open(path, O_RDONLY);
587 if (fd < NUM_ZERO) {
588 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, path);
589 return HDF_ERR_IO;
590 }
591
592 int32_t readSize = read(fd, buf, size - 1);
593 if (readSize < NUM_ZERO) {
594 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, path);
595 close(fd);
596 return HDF_ERR_IO;
597 }
598
599 buf[readSize] = '\0';
600 Trim(buf);
601 close(fd);
602
603 return HDF_SUCCESS;
604 }
605
ReadPluggedTypeSysfs()606 static int32_t ReadPluggedTypeSysfs()
607 {
608 std::string node = "USB";
609 int32_t online = ERROR;
610 char buf[MAX_BUFF_SIZE] = {0};
611
612 auto iter = g_filenodeName.begin();
613 while (iter != g_filenodeName.end()) {
614 if (strcasecmp(iter->c_str(), "battery") == 0) {
615 continue;
616 }
617 std::string onlinePath = SYSTEM_BATTERY_PATH + "/" + *iter + "/" + "online";
618 if (ReadSysfsFile(onlinePath.c_str(), buf, MAX_BUFF_SIZE) != HDF_SUCCESS) {
619 HDF_LOGW("%{public}s: read online path failed in loop", __func__);
620 }
621 online = strtol(buf, nullptr, STR_TO_LONG_LEN);
622 if (online) {
623 node = *iter;
624 break;
625 }
626 iter++;
627 }
628 if (online == ERROR) {
629 return ERROR;
630 }
631 std::string typePath = SYSTEM_BATTERY_PATH + "/" + node + "/" + "type";
632 HDF_LOGI("%{public}s: type path is: %{public}s", __func__, typePath.c_str());
633 if (ReadSysfsFile(typePath.c_str(), buf, MAX_BUFF_SIZE) != HDF_SUCCESS) {
634 HDF_LOGW("%{public}s: read type path failed", __func__);
635 return ERROR;
636 }
637 Trim(buf);
638 return PluggedTypeEnumConverter(buf);
639 }
640
ChargeStateEnumConverter(const char * str)641 int32_t ChargeStateEnumConverter(const char* str)
642 {
643 struct StringEnumMap chargeStateEnumMap[] = {
644 { "Discharging", PowerSupplyProvider::CHARGE_STATE_NONE },
645 { "Charging", PowerSupplyProvider::CHARGE_STATE_ENABLE },
646 { "Full", PowerSupplyProvider::CHARGE_STATE_FULL },
647 { "Not charging", PowerSupplyProvider::CHARGE_STATE_DISABLE },
648 { "Unknown", PowerSupplyProvider::CHARGE_STATE_RESERVED },
649 { NULL, PowerSupplyProvider::CHARGE_STATE_RESERVED },
650 };
651
652 for (int i = 0; chargeStateEnumMap[i].str; ++i) {
653 if (strcmp(str, chargeStateEnumMap[i].str) == 0) {
654 return chargeStateEnumMap[i].enumVal;
655 }
656 }
657 return PowerSupplyProvider::CHARGE_STATE_RESERVED;
658 }
659
ReadChargeStateSysfs()660 static int32_t ReadChargeStateSysfs()
661 {
662 char buf[128] = {0};
663 int32_t readSize;
664 std::string statusNode = "battery";
665 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
666 if (iter->first == "status") {
667 statusNode = iter->second;
668 break;
669 }
670 }
671 std::string sysChargeStatePath = SYSTEM_BATTERY_PATH + "/" + statusNode + "/" + "status";
672 HDF_LOGE("%{public}s: sysChargeStatePath is %{public}s", __func__, sysChargeStatePath.c_str());
673
674 int fd = open(sysChargeStatePath.c_str(), O_RDONLY);
675 if (fd < NUM_ZERO) {
676 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysChargeStatePath.c_str());
677 return HDF_FAILURE;
678 }
679
680 readSize = read(fd, buf, sizeof(buf) - 1);
681 if (readSize < NUM_ZERO) {
682 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysChargeStatePath.c_str());
683 close(fd);
684 return HDF_FAILURE;
685 }
686
687 Trim(buf);
688 int32_t battChargeState = ChargeStateEnumConverter(buf);
689 HDF_LOGE("%{public}s: read system file chargeState is %{public}d", __func__, battChargeState);
690 close(fd);
691
692 return battChargeState;
693 }
694
ReadChargeCounterSysfs()695 static int32_t ReadChargeCounterSysfs()
696 {
697 int strlen = 10;
698 char buf[128] = {0};
699 int32_t readSize;
700 std::string counterNode = "battery";
701 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
702 if (iter->first == "charge_counter") {
703 counterNode = iter->second;
704 break;
705 }
706 }
707 std::string sysChargeCounterPath = SYSTEM_BATTERY_PATH + "/" + counterNode + "/" + "charge_counter";
708 HDF_LOGE("%{public}s: sysChargeCounterPath is %{public}s", __func__, sysChargeCounterPath.c_str());
709
710 int fd = open(sysChargeCounterPath.c_str(), O_RDONLY);
711 if (fd < NUM_ZERO) {
712 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysChargeCounterPath.c_str());
713 return HDF_FAILURE;
714 }
715
716 readSize = read(fd, buf, sizeof(buf) - 1);
717 if (readSize < NUM_ZERO) {
718 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysChargeCounterPath.c_str());
719 close(fd);
720 return HDF_FAILURE;
721 }
722
723 buf[readSize] = '\0';
724 int32_t battChargeCounter = strtol(buf, nullptr, strlen);
725 if (battChargeCounter < 0) {
726 HDF_LOGE("%{public}s: read system file chargeState is %{public}d", __func__, battChargeCounter);
727 }
728 HDF_LOGE("%{public}s: read system file chargeState is %{public}d", __func__, battChargeCounter);
729 close(fd);
730
731 return battChargeCounter;
732 }
733
ReadPresentSysfs()734 static int32_t ReadPresentSysfs()
735 {
736 int strlen = 10;
737 char buf[128] = {0};
738 int32_t readSize;
739 std::string presentNode = "battery";
740 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
741 if (iter->first == "present") {
742 presentNode = iter->second;
743 break;
744 }
745 }
746 std::string sysPresentPath = SYSTEM_BATTERY_PATH + "/" + presentNode + "/" + "present";
747 HDF_LOGE("%{public}s: sysPresentPath is %{public}s", __func__, sysPresentPath.c_str());
748
749 int fd = open(sysPresentPath.c_str(), O_RDONLY);
750 if (fd < NUM_ZERO) {
751 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysPresentPath.c_str());
752 return HDF_FAILURE;
753 }
754
755 readSize = read(fd, buf, sizeof(buf) - 1);
756 if (readSize < NUM_ZERO) {
757 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysPresentPath.c_str());
758 close(fd);
759 return HDF_FAILURE;
760 }
761
762 buf[readSize] = '\0';
763 int32_t battPresent = strtol(buf, nullptr, strlen);
764 if (battPresent < 0) {
765 HDF_LOGE("%{public}s: read system file chargeState is %{public}d", __func__, battPresent);
766 }
767 HDF_LOGE("%{public}s: read system file chargeState is %{public}d", __func__, battPresent);
768 close(fd);
769 return battPresent;
770 }
771
ReadTechnologySysfs(std::string & battTechnology)772 static std::string ReadTechnologySysfs(std::string& battTechnology)
773 {
774 char buf[128] = {0};
775 int32_t readSize;
776 std::string technologyNode = "battery";
777 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
778 if (iter->first == "technology") {
779 technologyNode = iter->second;
780 break;
781 }
782 }
783 std::string sysTechnologyPath = SYSTEM_BATTERY_PATH + "/" + technologyNode + "/" + "technology";
784 HDF_LOGE("%{public}s: sysTechnologyPath is %{public}s", __func__, sysTechnologyPath.c_str());
785
786 int fd = open(sysTechnologyPath.c_str(), O_RDONLY);
787 if (fd < NUM_ZERO) {
788 HDF_LOGE("%{public}s: failed to open %{public}s", __func__, sysTechnologyPath.c_str());
789 return "";
790 }
791
792 readSize = read(fd, buf, sizeof(buf) - 1);
793 if (readSize < NUM_ZERO) {
794 HDF_LOGE("%{public}s: failed to read %{public}s", __func__, sysTechnologyPath.c_str());
795 close(fd);
796 return "";
797 }
798 buf[readSize] = '\0';
799 Trim(buf);
800
801 battTechnology.assign(buf, strlen(buf));
802 HDF_LOGE("%{public}s: read system file technology is %{public}s.", __func__, battTechnology.c_str());
803 close(fd);
804 return battTechnology;
805 }
806
IsNotMock()807 static bool IsNotMock()
808 {
809 bool rootExist = access(SYSTEM_BATTERY_PATH.c_str(), F_OK) == 0;
810 bool lowerExist = access((SYSTEM_BATTERY_PATH + "/battery").c_str(), F_OK) == 0;
811 bool upperExist = access((SYSTEM_BATTERY_PATH + "/Battery").c_str(), F_OK) == 0;
812 return rootExist && (lowerExist || upperExist);
813 }
814
815 /**
816 * @tc.name: ProviderIsNotNull
817 * @tc.desc: Test functions of PowerSupplyProvider
818 * @tc.type: FUNC
819 */
820 HWTEST_F (HdiServiceTest, ProviderIsNotNull, TestSize.Level1)
821 {
822 ASSERT_TRUE(giver_ != nullptr);
823 if (!IsNotMock()) {
824 std::string path = "/data/local/tmp";
825 giver_->SetSysFilePath(path);
826 HDF_LOGI("%{public}s: Is mock test", __func__);
827 }
828 giver_->InitPowerSupplySysfs();
829 }
830
831 /**
832 * @tc.name: HdiService001
833 * @tc.desc: Test functions of ParseTemperature
834 * @tc.type: FUNC
835 */
836 HWTEST_F (HdiServiceTest, HdiService001, TestSize.Level1)
837 {
838 int32_t temperature = 0;
839 if (IsNotMock()) {
840 giver_->ParseTemperature(&temperature);
841 int32_t sysfsTemp = ReadTemperatureSysfs();
842 HDF_LOGI("%{public}s: Not Mock HdiService001::temperature=%{public}d, t=%{public}d",
843 __func__, temperature, sysfsTemp);
844 ASSERT_TRUE(temperature == sysfsTemp);
845 } else {
846 CreateFile("/data/local/tmp/battery/temp", "567");
847 giver_->ParseTemperature(&temperature);
848 HDF_LOGI("%{public}s: HdiService001::temperature=%{public}d.", __func__, temperature);
849 ASSERT_TRUE(temperature == 567);
850 }
851 }
852
853 /**
854 * @tc.name: HdiService002
855 * @tc.desc: Test functions of ParseVoltage
856 * @tc.type: FUNC
857 */
858 HWTEST_F (HdiServiceTest, HdiService002, TestSize.Level1)
859 {
860 int32_t voltage = 0;
861 if (IsNotMock()) {
862 giver_->ParseVoltage(&voltage);
863 int32_t sysfsVoltage = ReadVoltageSysfs();
864 HDF_LOGI("%{public}s: Not Mock HdiService002::voltage=%{public}d, v=%{public}d",
865 __func__, voltage, sysfsVoltage);
866 ASSERT_TRUE(voltage == sysfsVoltage);
867 } else {
868 CreateFile("/data/local/tmp/battery/voltage_avg", "4123456");
869 CreateFile("/data/local/tmp/battery/voltage_now", "4123456");
870 giver_->ParseVoltage(&voltage);
871 HDF_LOGI("%{public}s: Not Mock HdiService002::voltage=%{public}d", __func__, voltage);
872 ASSERT_TRUE(voltage == 4123456);
873 }
874 }
875
876 /**
877 * @tc.name: HdiService003
878 * @tc.desc: Test functions of ParseCapacity
879 * @tc.type: FUNC
880 */
881 HWTEST_F (HdiServiceTest, HdiService003, TestSize.Level1)
882 {
883 int32_t capacity = -1;
884 if (IsNotMock()) {
885 giver_->ParseCapacity(&capacity);
886 int32_t sysfsCapacity = ReadCapacitySysfs();
887 HDF_LOGI("%{public}s: Not Mcok HdiService003::capacity=%{public}d, l=%{public}d",
888 __func__, capacity, sysfsCapacity);
889 ASSERT_TRUE(capacity == sysfsCapacity);
890 } else {
891 CreateFile("/data/local/tmp/battery/capacity", "11");
892 giver_->ParseCapacity(&capacity);
893 HDF_LOGI("%{public}s: HdiService003::capacity=%{public}d", __func__, capacity);
894 ASSERT_TRUE(capacity == 11);
895 }
896 }
897
898 /**
899 * @tc.name: HdiService004
900 * @tc.desc: Test functions of ParseHealthState
901 * @tc.type: FUNC
902 */
903 HWTEST_F (HdiServiceTest, HdiService004, TestSize.Level1)
904 {
905 int32_t healthState = -1;
906 if (IsNotMock()) {
907 giver_->ParseHealthState(&healthState);
908 int32_t sysfsHealthState = ReadHealthStateSysfs();
909 HDF_LOGI("%{public}s: Not Mock HdiService004::healthState=%{public}d, h=%{public}d",
910 __func__, healthState, sysfsHealthState);
911 ASSERT_TRUE(healthState == sysfsHealthState);
912 } else {
913 CreateFile("/data/local/tmp/battery/health", "Good");
914 giver_->ParseHealthState(&healthState);
915 HDF_LOGI("%{public}s: HdiService004::healthState=%{public}d.", __func__, healthState);
916 ASSERT_TRUE(PowerSupplyProvider::BatteryHealthState(healthState) ==
917 PowerSupplyProvider::BatteryHealthState::BATTERY_HEALTH_GOOD);
918 }
919 }
920
921 /**
922 * @tc.name: HdiService005
923 * @tc.desc: Test functions of ParsePluggedType
924 * @tc.type: FUNC
925 */
926 HWTEST_F (HdiServiceTest, HdiService005, TestSize.Level1)
927 {
928 int32_t pluggedType = PowerSupplyProvider::PLUGGED_TYPE_NONE;
929 if (IsNotMock()) {
930 giver_->ParsePluggedType(&pluggedType);
931 int32_t sysfsPluggedType = ReadPluggedTypeSysfs();
932 HDF_LOGI("%{public}s: Not Mock HdiService005::pluggedType=%{public}d, p=%{public}d",
933 __func__, pluggedType, sysfsPluggedType);
934 ASSERT_TRUE(pluggedType == sysfsPluggedType);
935 } else {
936 CreateFile("/data/local/tmp/ohos_charger/online", "1");
937 CreateFile("/data/local/tmp/ohos_charger/type", "Wireless");
938 giver_->ParsePluggedType(&pluggedType);
939 HDF_LOGI("%{public}s: HdiService005::pluggedType=%{public}d.", __func__, pluggedType);
940 ASSERT_TRUE(PowerSupplyProvider::BatteryPluggedType(pluggedType) ==
941 PowerSupplyProvider::BatteryPluggedType::PLUGGED_TYPE_WIRELESS);
942 }
943 }
944
945 /**
946 * @tc.name: HdiService006
947 * @tc.desc: Test functions of ParseChargeState
948 * @tc.type: FUNC
949 */
950 HWTEST_F (HdiServiceTest, HdiService006, TestSize.Level1)
951 {
952 int32_t chargeState = PowerSupplyProvider::CHARGE_STATE_RESERVED;
953 if (IsNotMock()) {
954 giver_->ParseChargeState(&chargeState);
955 int32_t sysfsChargeState = ReadChargeStateSysfs();
956 HDF_LOGI("%{public}s: Not Mock HdiService006::chargeState=%{public}d, cs=%{public}d",
957 __func__, chargeState, sysfsChargeState);
958 ASSERT_TRUE(chargeState == sysfsChargeState);
959 } else {
960 CreateFile("/data/local/tmp/battery/status", "Not charging");
961 giver_->ParseChargeState(&chargeState);
962 HDF_LOGI("%{public}s: HdiService006::chargeState=%{public}d.", __func__, chargeState);
963 ASSERT_TRUE(PowerSupplyProvider::BatteryChargeState(chargeState) ==
964 PowerSupplyProvider::BatteryChargeState::CHARGE_STATE_DISABLE);
965 }
966 }
967
968 /**
969 * @tc.name: HdiService007
970 * @tc.desc: Test functions of ParseChargeCounter
971 * @tc.type: FUNC
972 */
973 HWTEST_F (HdiServiceTest, HdiService007, TestSize.Level1)
974 {
975 int32_t chargeCounter = -1;
976 if (IsNotMock()) {
977 giver_->ParseChargeCounter(&chargeCounter);
978 int32_t sysfsChargeCounter = ReadChargeCounterSysfs();
979 HDF_LOGI("%{public}s: Not Mcok HdiService007::chargeCounter=%{public}d, cc=%{public}d",
980 __func__, chargeCounter, sysfsChargeCounter);
981 ASSERT_TRUE(chargeCounter == sysfsChargeCounter);
982 } else {
983 CreateFile("/data/local/tmp/battery/charge_counter", "12345");
984 giver_->ParseChargeCounter(&chargeCounter);
985 HDF_LOGI("%{public}s: HdiService007::chargeCounter=%{public}d.", __func__, chargeCounter);
986 ASSERT_TRUE(chargeCounter == 12345);
987 }
988 }
989
990 /**
991 * @tc.name: HdiService008
992 * @tc.desc: Test functions of ParsePresent
993 * @tc.type: FUNC
994 */
995 HWTEST_F (HdiServiceTest, HdiService008, TestSize.Level1)
996 {
997 int8_t present = -1;
998 if (IsNotMock()) {
999 giver_->ParsePresent(&present);
1000 int32_t sysfsPresent = ReadPresentSysfs();
1001 HDF_LOGI("%{public}s: Not Mock HdiService008::present=%{public}d, p=%{public}d",
1002 __func__, present, sysfsPresent);
1003 ASSERT_TRUE(present == sysfsPresent);
1004 } else {
1005 CreateFile("/data/local/tmp/battery/present", "1");
1006 giver_->ParsePresent(&present);
1007 HDF_LOGI("%{public}s: HdiService008::present=%{public}d.", __func__, present);
1008 ASSERT_TRUE(present == 1);
1009 }
1010 }
1011
1012 /**
1013 * @tc.name: HdiService009
1014 * @tc.desc: Test functions to get value of technology
1015 * @tc.type: FUNC
1016 */
1017 HWTEST_F (HdiServiceTest, HdiService009, TestSize.Level1)
1018 {
1019 std::string technology = "invalid";
1020 if (IsNotMock()) {
1021 giver_->ParseTechnology(technology);
1022 std::string sysfsTechnology = "";
1023 ReadTechnologySysfs(sysfsTechnology);
1024 HDF_LOGI("%{public}s: HdiService009::technology=%{public}s, ty=%{public}s",
1025 __func__, technology.c_str(), sysfsTechnology.c_str());
1026 ASSERT_TRUE(technology == sysfsTechnology);
1027 } else {
1028 CreateFile("/data/local/tmp/ohos-fgu/technology", "Li");
1029 giver_->ParseTechnology(technology);
1030 HDF_LOGI("%{public}s: HdiService009::technology=%{public}s.", __func__, technology.c_str());
1031 ASSERT_TRUE(technology == "Li");
1032 }
1033 }
1034
1035 /**
1036 * @tc.name: HdiService010
1037 * @tc.desc: Test functions to get fd of socket
1038 * @tc.type: FUNC
1039 */
1040 HWTEST_F (HdiServiceTest, HdiService010, TestSize.Level1)
1041 {
1042 using namespace OHOS::HDI::Battery::V1_0;
1043
1044 BatteryThread bt;
1045 auto fd = OpenUeventSocketTest(bt);
1046 HDF_LOGI("%{public}s: HdiService010::fd=%{public}d.", __func__, fd);
1047
1048 ASSERT_TRUE(fd > 0);
1049 close(fd);
1050 }
1051
1052 /**
1053 * @tc.name: HdiService011
1054 * @tc.desc: Test functions UpdateEpollInterval when charge-online
1055 * @tc.type: FUNC
1056 */
1057 HWTEST_F (HdiServiceTest, HdiService011, TestSize.Level1)
1058 {
1059 const int32_t CHARGE_STATE_ENABLE = 1;
1060 BatteryThread bt;
1061
1062 UpdateEpollIntervalTest(CHARGE_STATE_ENABLE, bt);
1063 auto epollInterval = GetEpollIntervalTest(bt);
1064 HDF_LOGI("%{public}s: HdiService011::epollInterval=%{public}d.", __func__, epollInterval);
1065
1066 ASSERT_TRUE(epollInterval == 2000);
1067 }
1068
1069 /**
1070 * @tc.name: HdiService012
1071 * @tc.desc: Test functions UpdateEpollInterval when charge-offline
1072 * @tc.type: FUNC
1073 */
1074 HWTEST_F (HdiServiceTest, HdiService012, TestSize.Level1)
1075 {
1076 const int32_t CHARGE_STATE_NONE = 0;
1077 BatteryThread bt;
1078
1079 UpdateEpollIntervalTest(CHARGE_STATE_NONE, bt);
1080 auto epollInterval = GetEpollIntervalTest(bt);
1081 HDF_LOGI("%{public}s: HdiService012::epollInterval=%{public}d.", __func__, epollInterval);
1082
1083 ASSERT_TRUE(epollInterval == -1);
1084 }
1085
1086 /**
1087 * @tc.name: HdiService013
1088 * @tc.desc: Test functions Init
1089 * @tc.type: FUNC
1090 */
1091 HWTEST_F (HdiServiceTest, HdiService013, TestSize.Level1)
1092 {
1093 void* service = nullptr;
1094 BatteryThread bt;
1095
1096 InitTest(service, bt);
1097 HDF_LOGI("%{public}s: HdiService013::InitTest success", __func__);
1098 auto epollFd = GetEpollFdTest(bt);
1099 HDF_LOGI("%{public}s: HdiService013::epollFd=%{public}d", __func__, epollFd);
1100
1101 ASSERT_TRUE(epollFd > 0);
1102 }
1103
1104 /**
1105 * @tc.name: HdiService014
1106 * @tc.desc: Test functions InitTimer
1107 * @tc.type: FUNC
1108 */
1109 HWTEST_F (HdiServiceTest, HdiService014, TestSize.Level1)
1110 {
1111 BatteryThread bt;
1112
1113 InitTimerTest(bt);
1114 auto timerFd = GetTimerFdTest(bt);
1115 HDF_LOGI("%{public}s: HdiService014::timerFd==%{public}d", __func__, timerFd);
1116
1117 ASSERT_TRUE(timerFd > 0);
1118 }
1119
1120 /**
1121 * @tc.name: HdiService015
1122 * @tc.desc: Test functions GetLedConf in BatteryConfig
1123 * @tc.type: FUNC
1124 */
1125 HWTEST_F (HdiServiceTest, HdiService015, TestSize.Level1)
1126 {
1127 std::unique_ptr<BatteryConfig> conf = std::make_unique<BatteryConfig>();
1128 conf->Init();
1129 std::vector<BatteryConfig::LedConf> ledConf = conf->GetLedConf();
1130
1131 if (!ledConf.empty()) {
1132 ASSERT_TRUE(ledConf[0].capacityBegin == 0);
1133 ASSERT_TRUE(ledConf[0].capacityEnd == 10);
1134 ASSERT_TRUE(ledConf[0].color == 4);
1135 ASSERT_TRUE(ledConf[0].brightness == 255);
1136 ASSERT_TRUE(ledConf[1].capacityBegin == 10);
1137 ASSERT_TRUE(ledConf[1].capacityEnd == 90);
1138 ASSERT_TRUE(ledConf[1].color == 6);
1139 ASSERT_TRUE(ledConf[1].brightness == 255);
1140 ASSERT_TRUE(ledConf[2].capacityBegin == 90);
1141 ASSERT_TRUE(ledConf[2].capacityEnd == 100);
1142 ASSERT_TRUE(ledConf[2].color == 2);
1143 ASSERT_TRUE(ledConf[2].brightness == 255);
1144 }
1145 }
1146
1147 /**
1148 * @tc.name: HdiService016
1149 * @tc.desc: Test functions GetTempConf in BatteryConfig
1150 * @tc.type: FUNC
1151 */
1152 HWTEST_F (HdiServiceTest, HdiService016, TestSize.Level1)
1153 {
1154 std::unique_ptr<BatteryConfig> conf = std::make_unique<BatteryConfig>();
1155 conf->Init();
1156 auto tempConf = conf->GetTempConf();
1157
1158 ASSERT_TRUE(tempConf.lower == -100);
1159 ASSERT_TRUE(tempConf.upper == 600);
1160 }
1161
1162 /**
1163 * @tc.name: HdiService017
1164 * @tc.desc: Test functions GetCapacityConf in BatteryConfig
1165 * @tc.type: FUNC
1166 */
1167 HWTEST_F (HdiServiceTest, HdiService017, TestSize.Level1)
1168 {
1169 std::unique_ptr<BatteryConfig> conf = std::make_unique<BatteryConfig>();
1170 conf->Init();
1171 auto capacityConf = conf->GetCapacityConf();
1172
1173 ASSERT_TRUE(capacityConf == 3);
1174 }
1175
1176 /**
1177 * @tc.name: HdiService018
1178 * @tc.desc: Test functions ParseLedInfo in BatteryConfig
1179 * @tc.type: FUNC
1180 */
1181 HWTEST_F (HdiServiceTest, HdiService018, TestSize.Level1)
1182 {
1183 std::string filename = "error_path/system/etc/ledconfig/led_config.json";
1184 BatteryConfig bc;
1185
1186 ParseConfigTest(filename, bc);
1187 std::unique_ptr<BatteryConfig> conf = std::make_unique<BatteryConfig>();
1188 auto ledConf = conf->GetLedConf();
1189 ASSERT_TRUE(ledConf.empty());
1190 }
1191
1192 /**
1193 * @tc.name: HdiService019
1194 * @tc.desc: Test functions ParseTemperatureInfo in BatteryConfig
1195 * @tc.type: FUNC
1196 */
1197 HWTEST_F (HdiServiceTest, HdiService019, TestSize.Level1)
1198 {
1199 std::string filename = "error_path/system/etc/ledconfig/led_config.json";
1200 BatteryConfig bc;
1201
1202 ParseConfigTest(filename, bc);
1203 std::unique_ptr<BatteryConfig> conf = std::make_unique<BatteryConfig>();
1204 auto tempConf = conf->GetTempConf();
1205
1206 ASSERT_TRUE(tempConf.lower != -100);
1207 ASSERT_TRUE(tempConf.upper != 600);
1208 }
1209
1210 /**
1211 * @tc.name: HdiService020
1212 * @tc.desc: Test functions ParseSocInfo in BatteryConfig
1213 * @tc.type: FUNC
1214 */
1215 HWTEST_F (HdiServiceTest, HdiService020, TestSize.Level1)
1216 {
1217 std::string filename = "error_path/system/etc/ledconfig/led_config.json";
1218 BatteryConfig bc;
1219
1220 ParseConfigTest(filename, bc);
1221 std::unique_ptr<BatteryConfig> conf = std::make_unique<BatteryConfig>();
1222 auto capacityConf = conf->GetCapacityConf();
1223
1224 ASSERT_TRUE(capacityConf != 3);
1225 }
1226
1227 /**
1228 * @tc.name: HdiService021
1229 * @tc.desc: Test functions VibrateInit in ChargerThread
1230 * @tc.type: FUNC
1231 */
1232 HWTEST_F (HdiServiceTest, HdiService021, TestSize.Level1)
1233 {
1234 std::unique_ptr<BatteryVibrate> vibrate = std::make_unique<BatteryVibrate>();
1235 const std::string VIBRATOR_PLAYMODE_PATH = "/sys/class/leds/vibrator/play_mode";
1236 const std::string VIBRATOR_DURATIONMODE_PATH = "/sys/class/leds/vibrator/duration";
1237 auto ret = vibrate->VibrateInit();
1238 if ((access(VIBRATOR_PLAYMODE_PATH.c_str(), F_OK) == 0) ||
1239 (access(VIBRATOR_DURATIONMODE_PATH.c_str(), F_OK) == 0)) {
1240 ASSERT_TRUE(ret == 0);
1241 } else {
1242 ASSERT_TRUE(ret == -1);
1243 }
1244 }
1245
1246 /**
1247 * @tc.name: HdiService022
1248 * @tc.desc: Test functions CycleMatters in ChargerThread
1249 * @tc.type: FUNC
1250 */
1251 HWTEST_F (HdiServiceTest, HdiService022, TestSize.Level1)
1252 {
1253 ChargerThread ct;
1254
1255 ChargerThreadInitTest(ct);
1256 CycleMattersTest(ct);
1257 auto getBatteryInfo = GetBatteryInfoTest(ct);
1258
1259 ASSERT_TRUE(getBatteryInfo);
1260 }
1261
1262 /**
1263 * @tc.name: HdiService024
1264 * @tc.desc: Test functions SetTimerInterval
1265 * @tc.type: FUNC
1266 */
1267 HWTEST_F (HdiServiceTest, HdiService024, TestSize.Level1)
1268 {
1269 BatteryThread bt;
1270
1271 SetTimerFdTest(2, bt);
1272 SetTimerIntervalTest(5, bt);
1273 int interval = GetTimerIntervalTest(bt);
1274
1275 ASSERT_TRUE(interval == 5);
1276 }
1277
1278 /**
1279 * @tc.name: HdiService025
1280 * @tc.desc: Test functions HandleBacklight
1281 * @tc.type: FUNC
1282 */
1283 HWTEST_F (HdiServiceTest, HdiService025, TestSize.Level1)
1284 {
1285 std::unique_ptr<BatteryBacklight> backlight = std::make_unique<BatteryBacklight>();
1286 backlight->InitBacklightSysfs();
1287 auto ret = backlight->HandleBacklight(0);
1288 HDF_LOGI("%{public}s: HdiService025::ret==%{public}d", __func__, ret);
1289 backlight->TurnOnScreen();
1290
1291 ASSERT_TRUE(ret != -1);
1292 }
1293
1294 /**
1295 * @tc.name: HdiService026
1296 * @tc.desc: Test functions of ParseTotalEnergy
1297 * @tc.type: FUNC
1298 */
1299 HWTEST_F (HdiServiceTest, HdiService026, TestSize.Level1)
1300 {
1301 int32_t totalEnergy = 0;
1302 if (IsNotMock()) {
1303 giver_->ParseTotalEnergy(&totalEnergy);
1304 int32_t sysfsTotalEnergy = ReadTotalEnergySysfs();
1305 HDF_LOGI("%{public}s: Not Mock HdiService026::totalEnergy=%{public}d, t=%{public}d",
1306 __func__, totalEnergy, sysfsTotalEnergy);
1307 ASSERT_TRUE(totalEnergy == sysfsTotalEnergy);
1308 } else {
1309 CreateFile("/data/local/tmp/battery/charge_full", "4000000");
1310 giver_->ParseTotalEnergy(&totalEnergy);
1311 HDF_LOGI("%{public}s: HdiService026::totalEnergy=%{public}d.", __func__, totalEnergy);
1312 ASSERT_TRUE(totalEnergy == 4000000);
1313 }
1314 }
1315
1316 /**
1317 * @tc.name: HdiService027
1318 * @tc.desc: Test functions of ParseCurrentAverage
1319 * @tc.type: FUNC
1320 */
1321 HWTEST_F (HdiServiceTest, HdiService027, TestSize.Level1)
1322 {
1323 int32_t currentAvg = HDF_FAILURE;
1324 if (IsNotMock()) {
1325 giver_->ParseCurrentAverage(¤tAvg);
1326 int32_t sysfsCurrentAvg = ReadCurrentAverageSysfs();
1327 HDF_LOGI("%{public}s: Not Mock HdiService027::currentAvg=%{public}d, t=%{public}d",
1328 __func__, currentAvg, sysfsCurrentAvg);
1329 ASSERT_TRUE(currentAvg == sysfsCurrentAvg);
1330 } else {
1331 CreateFile("/data/local/tmp/battery/current_avg", "1000");
1332 giver_->ParseCurrentAverage(¤tAvg);
1333 HDF_LOGI("%{public}s: HdiService027::currentAvg=%{public}d.", __func__, currentAvg);
1334 ASSERT_TRUE(currentAvg == 1000);
1335 }
1336 }
1337
1338 /**
1339 * @tc.name: HdiService028
1340 * @tc.desc: Test functions of ParseCurrentNow
1341 * @tc.type: FUNC
1342 */
1343 HWTEST_F (HdiServiceTest, HdiService028, TestSize.Level1)
1344 {
1345 int32_t currentNow = 0;
1346 if (IsNotMock()) {
1347 giver_->ParseCurrentNow(¤tNow);
1348 int32_t sysfsCurrentNow = ReadCurrentNowSysfs();
1349 HDF_LOGI("%{public}s: Not Mock HdiService028::currentNow=%{public}d, t=%{public}d",
1350 __func__, currentNow, sysfsCurrentNow);
1351 ASSERT_TRUE(currentNow == sysfsCurrentNow);
1352 } else {
1353 CreateFile("/data/local/tmp/battery/current_now", "1000");
1354 giver_->ParseCurrentNow(¤tNow);
1355 HDF_LOGI("%{public}s: HdiService028::currentNow=%{public}d.", __func__, currentNow);
1356 ASSERT_TRUE(currentNow == 1000);
1357 }
1358 }
1359
1360 /**
1361 * @tc.name: HdiService029
1362 * @tc.desc: Test functions of ParseChargeNow
1363 * @tc.type: FUNC
1364 */
1365 HWTEST_F (HdiServiceTest, HdiService029, TestSize.Level1)
1366 {
1367 int32_t chargeNow = 0;
1368 if (IsNotMock()) {
1369 giver_->ParseRemainEnergy(&chargeNow);
1370 int32_t sysfsChargeNow = ReadRemainEnergySysfs();
1371 HDF_LOGI("%{public}s: Not Mock HdiService029::chargeNow=%{public}d, t=%{public}d",
1372 __func__, chargeNow, sysfsChargeNow);
1373 ASSERT_TRUE(chargeNow == sysfsChargeNow);
1374 } else {
1375 CreateFile("/data/local/tmp/battery/charge_now", "1000");
1376 giver_->ParseRemainEnergy(&chargeNow);
1377 HDF_LOGI("%{public}s: HdiService029::chargeNow=%{public}d.", __func__, chargeNow);
1378 ASSERT_TRUE(chargeNow == 1000);
1379 }
1380 }
1381 }
1382