Skip to content

Commit 98d8b59

Browse files
committed
battery: Code cleanup
This patch removes unused code and replaces banned functions with safe functions in battery host utility. Tracked-On: OAM-92844 Signed-off-by: Saranya Gopal <saranya.gopal@intel.com>
1 parent 8f5c61c commit 98d8b59

File tree

1 file changed

+8
-87
lines changed

1 file changed

+8
-87
lines changed

vm_battery_utility/battery_sysfsread.c

Lines changed: 8 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
#include "battery_notifypkt.h"
3232

3333
#define TEST_PORT 14196
34-
#define debug_local_flag 0
3534

3635
struct initial_pkt initpkt;
3736
struct monitor_pkt monitorpkt;
3837
struct monitor_pkt monitortemp;
3938

40-
int start_connection(struct sockaddr_vm sa_listen, int listen_fd, socklen_t socklen_client, int *m_acpidsock);
39+
int start_connection(struct sockaddr_vm sa_listen, int listen_fd, socklen_t socklen_client);
4140

4241
int client_fd = 0;
4342
char base_path[100] = "/sys/class/power_supply/";
@@ -119,9 +118,6 @@ void read_store_values()
119118
read_sysfs_values(CHARGE_TYPE, monitorpkt.charge_type, sizeof(monitorpkt.charge_type), 0);
120119
read_sysfs_values(CAPACITY_LEVEL, monitorpkt.capacity_level, sizeof(monitorpkt.capacity_level), 0);
121120
read_sysfs_values(STATUS, monitorpkt.status, sizeof(monitorpkt.status), 0);
122-
#if debug_local_flag
123-
printf("In file %s: Read and store complete\n", __FILE__);
124-
#endif
125121
}
126122

127123
/* read_monitor_pkt: Reads the latest struct monitor_pkt variable values
@@ -130,15 +126,7 @@ void read_store_values()
130126

131127
bool read_monitor_pkt(struct monitor_pkt *monitortemp)
132128
{
133-
#if debug_local_flag
134-
static int count = 0;
135-
count++;
136-
#endif
137129
read_sysfs_values(CAPACITY, &monitortemp->capacity, sizeof(monitortemp->capacity), 1);
138-
#if debug_local_flag
139-
if(count%10==0)
140-
monitortemp->capacity = 51;
141-
#endif
142130
read_sysfs_values(CHARGE_FULL_DESIGN, &monitortemp->charge_full_design, sizeof(monitortemp->charge_full_design), 1);
143131
read_sysfs_values(HEALTH, monitortemp->health, sizeof(monitortemp->health), 0);
144132
read_sysfs_values(TEMP, &monitortemp->temp, sizeof(monitortemp->temp), 1);
@@ -175,7 +163,7 @@ bool read_monitor_pkt(struct monitor_pkt *monitortemp)
175163
*/
176164

177165
void fill_header (struct header *head, uint16_t id) {
178-
strcpy((char *)head->intelipc, INTELIPCID);
166+
strncpy((char *)head->intelipc, INTELIPCID, sizeof(head->intelipc));
179167
head->notify_id = id;
180168
if (id == 1)
181169
head->length = sizeof(initpkt) + sizeof(monitorpkt);
@@ -184,48 +172,23 @@ void fill_header (struct header *head, uint16_t id) {
184172
}
185173

186174

187-
#if debug_local_flag
188-
int main()
189-
#else
190175
int send_pkt()
191-
#endif
192176
{
193177
char msgbuf[1024] = {0};
194178
struct header head;
195179
bool flag = 0;
196180
int return_value = 0;
197181

198-
#if debug_local_flag
199-
char battery_module_name[50];
200-
201-
get_battery_module_name(battery_module_name);
202-
printf("Battery module name: %s\n",battery_module_name);
203-
204-
/* Updating the base_path to point to the battery module */
205-
strcat(base_path, battery_module_name);
206-
207-
printf("Starting the program\n");
208-
#endif
209-
210182
/* Read and store the battery sysfs values for the 1st time */
211183
read_store_values();
212184
fill_header(&head, 1);
213185
memcpy(msgbuf, (const unsigned char*)&head, sizeof(head));
214186
memcpy(msgbuf + sizeof(head), (const unsigned char*)&initpkt, sizeof(initpkt));
215187
memcpy(msgbuf + sizeof(head) + sizeof(initpkt), (const unsigned char*)&monitorpkt, sizeof(monitorpkt));
216-
#if debug_local_flag
217-
printf("Sending initial values\n");
218-
#else
219188
return_value = send(client_fd, msgbuf, sizeof(msgbuf), MSG_DONTWAIT);
220189
if (return_value == -1)
221190
goto out;
222191
memset(msgbuf, 0, sizeof(msgbuf));
223-
#endif
224-
#if debug_local_flag
225-
printf("Initial values sent\n");
226-
for(int i = 0; i < (sizeof(head) + sizeof(initpkt) + sizeof(monitorpkt)); i++)
227-
printf("%c", msgbuf[i]);
228-
#endif
229192
while(1)
230193
{
231194
sleep(1);
@@ -236,81 +199,45 @@ int send_pkt()
236199
fill_header(&head, 2);
237200
memcpy(msgbuf, (const unsigned char*)&head, sizeof(head));
238201
memcpy(msgbuf + sizeof(head), (const unsigned char *)&monitortemp, sizeof(monitortemp));
239-
#if debug_local_flag
240-
printf("Sending the changed values\n");
241-
#else
242202
return_value = send(client_fd, msgbuf, sizeof(msgbuf), MSG_DONTWAIT);
243203
if (return_value == -1)
244204
goto out;
245-
#endif
246-
#if debug_local_flag
247-
for(int j = 0; j < sizeof(msgbuf); j++)
248-
printf("%c", msgbuf[j]);
249-
#endif
250205
}
251-
#if debug_local_flag
252-
else
253-
printf("nothing changed\n");
254-
#endif
255206
}
256207
return 0;
257208
out:
258209
return -1;
259210
}
260211

261-
int start_connection(struct sockaddr_vm sa_listen, int listen_fd, socklen_t socklen_client, int *m_acpidsock) {
212+
int start_connection(struct sockaddr_vm sa_listen, int listen_fd, socklen_t socklen_client) {
262213
int ret = 0;
263214
struct sockaddr_vm sa_client;
264-
struct sockaddr_un m_acpidsockaddr;
265215
fprintf(stderr, "Battery utility listening on cid(%d), port(%d)\n", sa_listen.svm_cid, sa_listen.svm_port);
266216
if (listen(listen_fd, 32) != 0) {
267217
fprintf(stderr, "listen failed\n");
268-
ret = -1;
269-
goto out;
218+
return -1;
270219
}
271220

272221
client_fd = accept(listen_fd, (struct sockaddr*)&sa_client, &socklen_client);
273222
if(client_fd < 0) {
274223
fprintf(stderr, "accept failed\n");
275-
ret = -1;
276-
goto out;
224+
return -1;
277225
}
278226
fprintf(stderr, "Battery utility connected from guest(%d)\n", sa_client.svm_cid);
279-
280-
/* Connect to acpid socket */
281-
*m_acpidsock = socket(AF_UNIX, SOCK_STREAM, 0);
282-
if (*m_acpidsock < 0) {
283-
perror("new acpidsocket failed");
284-
ret = -2;
285-
goto out;
286-
}
287-
288-
m_acpidsockaddr.sun_family = AF_UNIX;
289-
strcpy(m_acpidsockaddr.sun_path,"/var/run/acpid.socket");
290-
if(connect(*m_acpidsock, (struct sockaddr *)&m_acpidsockaddr, 108)<0)
291-
{
292-
/* can't connect */
293-
perror("connect acpidsocket failed");
294-
ret = -2;
295-
goto out;
296-
}
297-
out:
298-
return ret;
227+
return 0;
299228
}
300229

301-
#if !debug_local_flag
302230
int main()
303231
{
304232
int listen_fd = 0;
305233
int ret = 0;
306234
int return_value = 0;
307-
int m_acpidsock = 0;
308235
char battery_module_name[50];
309236

310237
get_battery_module_name(battery_module_name);
311238

312239
/* Updating the base_path to point to the battery module */
313-
strcat(base_path, battery_module_name);
240+
strncat(base_path, battery_module_name, sizeof(base_path));
314241

315242
struct sockaddr_vm sa_listen = {
316243
.svm_family = AF_VSOCK,
@@ -332,7 +259,7 @@ int main()
332259
goto out;
333260
}
334261
start:
335-
ret = start_connection(sa_listen, listen_fd, socklen_client, &m_acpidsock);
262+
ret = start_connection(sa_listen, listen_fd, socklen_client);
336263
if (ret < 0)
337264
goto out;
338265
return_value = send_pkt();
@@ -345,11 +272,5 @@ int main()
345272
close(listen_fd);
346273
}
347274

348-
if(m_acpidsock >= 0)
349-
{
350-
printf("Closing acpisocket\n");
351-
close(m_acpidsock);
352-
}
353275
return ret;
354276
}
355-
#endif

0 commit comments

Comments
 (0)