Skip to content

Commit

Permalink
interfaces-plugin: add read sys, proc
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej committed Oct 13, 2022
1 parent a8c2dc6 commit 9081501
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "plugin/common.h"
#include "srpc/common.h"

#include <errno.h>

int read_from_proc_file(const char *dir_path, char *interface, const char *fn, int *val)
{
int error = 0;
char tmp_buffer[PATH_MAX];
FILE *fptr = NULL;
char tmp_val[2] = {0};

error = snprintf(tmp_buffer, sizeof(tmp_buffer), "%s/%s/%s", dir_path, interface, fn);
if (error < 0) {
// snprintf error
SRPLG_LOG_ERR(PLUGIN_NAME, "snprintf failed");
goto out;
}

// snprintf returns return the number of bytes that are written
// reset error to 0
error = 0;

fptr = fopen((const char *) tmp_buffer, "r");

if (fptr != NULL) {
fgets(tmp_val, sizeof(tmp_val), fptr);

*val = atoi(tmp_val);

fclose(fptr);
} else {
SRPLG_LOG_ERR(PLUGIN_NAME, "failed to open %s: %s", tmp_buffer, strerror(errno));
error = -1;
goto out;
}

out:
return error;
}

int read_from_sys_file(const char* dir_path, char* interface, int* val)
{
int error = 0;
char tmp_buffer[PATH_MAX];
FILE* fptr = NULL;
char tmp_val[4] = { 0 };

error = snprintf(tmp_buffer, sizeof(tmp_buffer), "%s/%s/type", dir_path, interface);
if (error < 0) {
// snprintf error
SRPLG_LOG_ERR(PLUGIN_NAME, "%s: snprintf failed", __func__);
goto out;
}

/* snprintf returns return the number of bytes that are written - reset error to 0 */
error = 0;

fptr = fopen((const char*)tmp_buffer, "r");

if (fptr != NULL) {
fgets(tmp_val, sizeof(tmp_val), fptr);

*val = atoi(tmp_val);

fclose(fptr);
} else {
SRPLG_LOG_ERR(PLUGIN_NAME, "%s: failed to open %s: %s", __func__, tmp_buffer, strerror(errno));
error = -1;
goto out;
}

out:
return error;
}
8 changes: 8 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/read.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef READ_H
#define READ_H

int read_from_proc_file(const char *dir_path, char *interface, const char *fn, int *val);

int read_from_sys_file(const char* dir_path, char* interface, int* val);

#endif /* READ_H */

0 comments on commit 9081501

Please sign in to comment.