mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-11-18 11:05:18 +00:00
cleanup apcupsd a bit, fix some whitespace errors
This commit is contained in:
parent
9b66a4715b
commit
0522832350
@ -33,12 +33,37 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
enum _apcupsd_items {
|
||||||
|
APCUPSD_NAME,
|
||||||
|
APCUPSD_MODEL,
|
||||||
|
APCUPSD_UPSMODE,
|
||||||
|
APCUPSD_CABLE,
|
||||||
|
APCUPSD_STATUS,
|
||||||
|
APCUPSD_LINEV,
|
||||||
|
APCUPSD_LOAD,
|
||||||
|
APCUPSD_CHARGE,
|
||||||
|
APCUPSD_TIMELEFT,
|
||||||
|
APCUPSD_TEMP,
|
||||||
|
APCUPSD_LASTXFER,
|
||||||
|
_APCUPSD_COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
/* type for data exchange with main thread */
|
||||||
|
#define APCUPSD_MAXSTR 32
|
||||||
|
typedef struct apcupsd_s {
|
||||||
|
char items[_APCUPSD_COUNT][APCUPSD_MAXSTR+1]; /* e.g. items[APCUPSD_STATUS] */
|
||||||
|
char host[64];
|
||||||
|
int port;
|
||||||
|
} APCUPSD_S, *PAPCUPSD_S;
|
||||||
|
|
||||||
|
static APCUPSD_S apcupsd;
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// encapsulated recv()
|
// encapsulated recv()
|
||||||
//
|
//
|
||||||
static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv)
|
static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv)
|
||||||
{
|
{
|
||||||
|
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
@ -74,8 +99,8 @@ static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv)
|
|||||||
//
|
//
|
||||||
// read whole buffer or fail
|
// read whole buffer or fail
|
||||||
//
|
//
|
||||||
static int net_recv(int sock, void* buf, int size) {
|
static int net_recv(int sock, void* buf, int size)
|
||||||
|
{
|
||||||
int todo = size;
|
int todo = size;
|
||||||
int off = 0;
|
int off = 0;
|
||||||
int len;
|
int len;
|
||||||
@ -93,8 +118,8 @@ static int net_recv(int sock, void* buf, int size) {
|
|||||||
//
|
//
|
||||||
// get one response line
|
// get one response line
|
||||||
//
|
//
|
||||||
static int get_line(int sock, char line[], short linesize) {
|
static int get_line(int sock, char line[], short linesize)
|
||||||
|
{
|
||||||
// get the line length
|
// get the line length
|
||||||
short sz;
|
short sz;
|
||||||
if (!net_recv(sock, &sz, sizeof(sz))) return -1;
|
if (!net_recv(sock, &sz, sizeof(sz))) return -1;
|
||||||
@ -130,8 +155,8 @@ static int get_line(int sock, char line[], short linesize) {
|
|||||||
//
|
//
|
||||||
// fills in the data received from a socket
|
// fills in the data received from a socket
|
||||||
//
|
//
|
||||||
static int fill_items(int sock, PAPCUPSD_S apc) {
|
static int fill_items(int sock, PAPCUPSD_S apc)
|
||||||
|
{
|
||||||
char line[512];
|
char line[512];
|
||||||
int len;
|
int len;
|
||||||
while ((len = get_line(sock, line, sizeof(line)))) {
|
while ((len = get_line(sock, line, sizeof(line)))) {
|
||||||
@ -148,15 +173,15 @@ static int fill_items(int sock, PAPCUPSD_S apc) {
|
|||||||
FILL("ITEMP", APCUPSD_TEMP, TRUE);
|
FILL("ITEMP", APCUPSD_TEMP, TRUE);
|
||||||
FILL("LASTXFER", APCUPSD_LASTXFER, FALSE);
|
FILL("LASTXFER", APCUPSD_LASTXFER, FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return len == 0;
|
return len == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Conky update function for apcupsd data
|
// Conky update function for apcupsd data
|
||||||
//
|
//
|
||||||
void update_apcupsd(void) {
|
void update_apcupsd(void)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
APCUPSD_S apc;
|
APCUPSD_S apc;
|
||||||
int sock;
|
int sock;
|
||||||
@ -182,27 +207,27 @@ void update_apcupsd(void) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#ifdef HAVE_GETHOSTBYNAME_R
|
#ifdef HAVE_GETHOSTBYNAME_R
|
||||||
if (gethostbyname_r(info.apcupsd.host, &he_mem, hostbuff, sizeof(hostbuff), &he, &he_errno) || !he ) {
|
if (gethostbyname_r(apcupsd.host, &he_mem, hostbuff, sizeof(hostbuff), &he, &he_errno) || !he ) {
|
||||||
NORM_ERR("APCUPSD gethostbyname_r: %s", hstrerror(h_errno));
|
NORM_ERR("APCUPSD gethostbyname_r: %s", hstrerror(h_errno));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#else /* HAVE_GETHOSTBYNAME_R */
|
#else /* HAVE_GETHOSTBYNAME_R */
|
||||||
he = gethostbyname(info.apcupsd.host);
|
he = gethostbyname(apcupsd.host);
|
||||||
if (!he) {
|
if (!he) {
|
||||||
herror("gethostbyname");
|
herror("gethostbyname");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif /* HAVE_GETHOSTBYNAME_R */
|
#endif /* HAVE_GETHOSTBYNAME_R */
|
||||||
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = info.apcupsd.port;
|
addr.sin_port = apcupsd.port;
|
||||||
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
|
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
|
||||||
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) < 0) {
|
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) < 0) {
|
||||||
// no error reporting, the daemon is probably not running
|
// no error reporting, the daemon is probably not running
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// send status request - "status" - 6B
|
// send status request - "status" - 6B
|
||||||
//
|
//
|
||||||
@ -212,7 +237,7 @@ void update_apcupsd(void) {
|
|||||||
perror("send");
|
perror("send");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// read the lines of output and put them into the info structure
|
// read the lines of output and put them into the info structure
|
||||||
//
|
//
|
||||||
@ -225,22 +250,34 @@ void update_apcupsd(void) {
|
|||||||
//
|
//
|
||||||
// "atomically" copy the data into working set
|
// "atomically" copy the data into working set
|
||||||
//
|
//
|
||||||
memcpy(info.apcupsd.items, apc.items, sizeof(info.apcupsd.items));
|
memcpy(apcupsd.items, apc.items, sizeof(apcupsd.items));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int apcupsd_scan_arg(const char *arg)
|
||||||
|
{
|
||||||
|
char host[64];
|
||||||
|
int port;
|
||||||
|
if (sscanf(arg, "%63s %d", host, &port) != 2)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
apcupsd.port = htons(port);
|
||||||
|
strncpy(apcupsd.host, host, sizeof(apcupsd.host));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
double apcupsd_loadbarval(struct text_object *obj)
|
double apcupsd_loadbarval(struct text_object *obj)
|
||||||
{
|
{
|
||||||
(void)obj;
|
(void)obj;
|
||||||
|
|
||||||
return atof(info.apcupsd.items[APCUPSD_LOAD]);
|
return atof(apcupsd.items[APCUPSD_LOAD]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define APCUPSD_PRINT_GENERATOR(name, idx) \
|
#define APCUPSD_PRINT_GENERATOR(name, idx) \
|
||||||
void print_apcupsd_##name(struct text_object *obj, char *p, int p_max_size) \
|
void print_apcupsd_##name(struct text_object *obj, char *p, int p_max_size) \
|
||||||
{ \
|
{ \
|
||||||
(void)obj; \
|
(void)obj; \
|
||||||
snprintf(p, p_max_size, "%s", info.apcupsd.items[APCUPSD_##idx]); \
|
snprintf(p, p_max_size, "%s", apcupsd.items[APCUPSD_##idx]); \
|
||||||
}
|
}
|
||||||
|
|
||||||
APCUPSD_PRINT_GENERATOR(name, NAME)
|
APCUPSD_PRINT_GENERATOR(name, NAME)
|
||||||
|
@ -29,28 +29,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum _apcupsd_items {
|
int apcupsd_scan_arg(const char *);
|
||||||
APCUPSD_NAME,
|
|
||||||
APCUPSD_MODEL,
|
|
||||||
APCUPSD_UPSMODE,
|
|
||||||
APCUPSD_CABLE,
|
|
||||||
APCUPSD_STATUS,
|
|
||||||
APCUPSD_LINEV,
|
|
||||||
APCUPSD_LOAD,
|
|
||||||
APCUPSD_CHARGE,
|
|
||||||
APCUPSD_TIMELEFT,
|
|
||||||
APCUPSD_TEMP,
|
|
||||||
APCUPSD_LASTXFER,
|
|
||||||
_APCUPSD_COUNT
|
|
||||||
};
|
|
||||||
|
|
||||||
/* type for data exchange with main thread */
|
|
||||||
#define APCUPSD_MAXSTR 32
|
|
||||||
typedef struct apcupsd_s {
|
|
||||||
char items[_APCUPSD_COUNT][APCUPSD_MAXSTR+1]; /* e.g. items[APCUPSD_STATUS] */
|
|
||||||
char host[64];
|
|
||||||
int port;
|
|
||||||
} APCUPSD_S, *PAPCUPSD_S;
|
|
||||||
|
|
||||||
/* Service routine for the conky main thread */
|
/* Service routine for the conky main thread */
|
||||||
void update_apcupsd(void);
|
void update_apcupsd(void);
|
||||||
|
@ -242,10 +242,6 @@ struct information {
|
|||||||
struct x11_info x11;
|
struct x11_info x11;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef APCUPSD
|
|
||||||
APCUPSD_S apcupsd;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
short kflags; /* kernel settings, see enum KFLAG */
|
short kflags; /* kernel settings, see enum KFLAG */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1546,13 +1546,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
|
|||||||
#endif /* NVIDIA */
|
#endif /* NVIDIA */
|
||||||
#ifdef APCUPSD
|
#ifdef APCUPSD
|
||||||
END OBJ_ARG(apcupsd, &update_apcupsd, "apcupsd needs arguments: <host> <port>")
|
END OBJ_ARG(apcupsd, &update_apcupsd, "apcupsd needs arguments: <host> <port>")
|
||||||
char host[64];
|
if (apcupsd_scan_arg(arg)) {
|
||||||
int port;
|
|
||||||
if (sscanf(arg, "%63s %d", host, &port) != 2) {
|
|
||||||
CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
|
CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
|
||||||
} else {
|
|
||||||
info.apcupsd.port = htons(port);
|
|
||||||
strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
|
|
||||||
}
|
}
|
||||||
obj->callbacks.print = &gen_print_nothing;
|
obj->callbacks.print = &gen_print_nothing;
|
||||||
END OBJ(apcupsd_name, &update_apcupsd)
|
END OBJ(apcupsd_name, &update_apcupsd)
|
||||||
|
Loading…
Reference in New Issue
Block a user