-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysBatt.c
More file actions
157 lines (142 loc) · 5.06 KB
/
sysBatt.c
File metadata and controls
157 lines (142 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* Author: Jose M. Fernandez (GitHub user: jmfernandez)
*/
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "sysBatt.h"
#define READBUFSIZE 4096
const static char power_supply_basedir[] = "/sys/class/power_supply";
const static char battery_present_entry[] = "present";
const static char power_supply_type_entry[] = "type";
const static char capacity_entry[] = "capacity";
const static char battery_type[] = "Battery";
struct BattStats * getBattStats() {
unsigned int numBatts = 0;
struct BattStats * battStats = NULL;
/* First, we are going to learn the available resources */
DIR * psDir = NULL;
if((psDir = opendir(power_supply_basedir)) != NULL) {
struct dirent * entry = NULL;
while((entry = readdir(psDir)) != NULL) {
/* The directory is interesting. Now we are going to open it */
if(entry->d_name[0] != '.' && entry->d_type != DT_REG) {
/* Get the path to the battery */
char * battName = strdup(entry->d_name);
size_t battNameLen = strlen(battName);
size_t battDirPathSize = sizeof(power_supply_basedir)+1+battNameLen;
char * battDirPath = (char *)malloc(battDirPathSize);
memcpy(battDirPath,power_supply_basedir,sizeof(power_supply_basedir)-1);
battDirPath[sizeof(power_supply_basedir)-1] = '/';
strcpy(battDirPath+sizeof(power_supply_basedir),entry->d_name);
/* saving the position */
long nextPos = telldir(psDir);
closedir(psDir);
DIR * battDir = NULL;
if((battDir = opendir(battDirPath))!=NULL) {
struct dirent * battEntry = NULL;
int isPresent = 0;
int isBattery = 0;
int capacity = 0;
/* reading buffer */
char * content = (char *)malloc(READBUFSIZE);
/* sys path buffer */
size_t battEntryPathLen = battDirPathSize+NAME_MAX;
char * battEntryPath = (char *)malloc(battEntryPathLen);
/* The file being opened */
FILE * battEntryFile = NULL;
int op = 0;
while((battEntry = readdir(battDir)) != NULL) {
if(strcmp(battEntry->d_name,power_supply_type_entry)==0) {
/* Is this a battery? */
op = 1;
} else if(strcmp(battEntry->d_name,battery_present_entry)==0) {
/* Is the device present? */
op = 2;
} else if(strcmp(battEntry->d_name,capacity_entry)==0) {
/* The charging level */
op = 3;
} else {
op = 0;
}
if(op > 0) {
/* Get the path to the property */
snprintf(battEntryPath,battEntryPathLen,"%s/%s",battDirPath,battEntry->d_name);
if((battEntryFile = fopen(battEntryPath,"r")) != NULL) {
switch(op) {
case 1:
/* Is this a battery? */
if(fgets(content,READBUFSIZE,battEntryFile)!=NULL) {
size_t lcontent = strlen(content);
if(content[lcontent-1]=='\n') {
content[lcontent-1] = '\0';
}
isBattery = strcmp(battery_type,content) == 0;
}
break;
case 2:
/* Is the device present? */
/*
fprintf(stderr,"%s => (%d)\n",battEntryPath,isPresent,fscanf(battEntryFile,"%d",&isPresent));
*/
fscanf(battEntryFile,"%d",&isPresent);
break;
case 3:
/* The charging level */
/*
fprintf(stderr,"%s => (%d)\n",battEntryPath,capacity,fscanf(battEntryFile,"%d",&capacity));
*/
fscanf(battEntryFile,"%d",&capacity);
break;
}
fclose(battEntryFile);
}
}
}
closedir(battDir);
if(content!=NULL) {
free((void *)content);
}
if(battEntryPath!=NULL) {
free((void *)battEntryPath);
}
/* We found a present battery, so record it */
if(isBattery > 0 && isPresent > 0) {
battStats = (struct BattStats *)realloc((void *)battStats,sizeof(struct BattStats *)*(numBatts+2));
battStats[numBatts].capacity = capacity;
strcpy(battStats[numBatts].name,battName);
numBatts++;
battStats[numBatts].name[0] = '\0';
}
}
free((void *)battName);
/* Restoring the position where we left the directory reading */
if((psDir = opendir(power_supply_basedir)) != NULL) {
seekdir(psDir,nextPos);
} else {
break;
}
}
}
closedir(psDir);
}
return battStats;
}