work work

This commit is contained in:
Olaf Rempel 2011-05-22 16:06:50 +02:00
parent e1a32989ab
commit e3d2dae9ea
5 changed files with 89 additions and 61 deletions

View File

@ -85,13 +85,13 @@ int config_parse(const char *config)
{ {
FILE *fz = fopen(config, "r"); FILE *fz = fopen(config, "r");
if (fz == NULL) { if (fz == NULL) {
log_print(LOG_ERROR, "config_parse(): %s", config); log_print(LOG_ERROR, "%s(): failed to open config '%s'", __FUNCTION__, config);
return -1; return -1;
} }
char *line = malloc(BUFSIZE); char *line = malloc(BUFSIZE);
if (line == NULL) { if (line == NULL) {
log_print(LOG_ERROR, "config_parse(): out of memory"); log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
fclose(fz); fclose(fz);
return -1; return -1;
} }
@ -108,7 +108,7 @@ int config_parse(const char *config)
char *tok = strtok(line +1, " ]\n"); char *tok = strtok(line +1, " ]\n");
if (tok == NULL || (section = config_add_section(tok)) == NULL) { if (tok == NULL || (section = config_add_section(tok)) == NULL) {
log_print(LOG_WARN, "config_parse(): invalid section in row %d", linenum); log_print(LOG_WARN, "%s(): invalid section in row %d", __FUNCTION__, linenum);
free(line); free(line);
fclose(fz); fclose(fz);
return -1; return -1;
@ -116,7 +116,7 @@ int config_parse(const char *config)
continue; continue;
} else if (section == NULL) { } else if (section == NULL) {
log_print(LOG_WARN, "config_parse(): missing section in row %d", linenum); log_print(LOG_WARN, "%s(): missing section in row %d", __FUNCTION__, linenum);
free(line); free(line);
fclose(fz); fclose(fz);
return -1; return -1;
@ -127,7 +127,7 @@ int config_parse(const char *config)
char *tok2; char *tok2;
while ((tok2 = strtok_r(NULL, " \n", &tmp))) { while ((tok2 = strtok_r(NULL, " \n", &tmp))) {
if (config_add_tupel(section, tok, tok2) != 0) if (config_add_tupel(section, tok, tok2) != 0)
log_print(LOG_WARN, "config_parse(): invalid row %d", linenum); log_print(LOG_WARN, "%s(): invalid row %d", __FUNCTION__, linenum);
} }
} }
} }

18
event.c
View File

@ -60,13 +60,13 @@ struct event_fd * event_add_fd(
{ {
/* check valid filediskriptor */ /* check valid filediskriptor */
if (fd < 0 || fd > FD_SETSIZE) { if (fd < 0 || fd > FD_SETSIZE) {
log_print(LOG_ERROR, "event_add_fd(): invalid fd"); log_print(LOG_ERROR, "%s(): invalid fd", __FUNCTION__);
return NULL; return NULL;
} }
/* check valid type (read/write) */ /* check valid type (read/write) */
if (!(type & FD_TYPES)) { if (!(type & FD_TYPES)) {
log_print(LOG_ERROR, "event_add_fd(): invalid type"); log_print(LOG_ERROR, "%s(): invalid type", __FUNCTION__);
return NULL; return NULL;
} }
@ -74,7 +74,7 @@ struct event_fd * event_add_fd(
if (entry == NULL) { if (entry == NULL) {
entry = malloc(sizeof(struct event_fd)); entry = malloc(sizeof(struct event_fd));
if (entry == NULL) { if (entry == NULL) {
log_print(LOG_ERROR, "event_add_fd(): out of memory"); log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
return NULL; return NULL;
} }
@ -177,7 +177,7 @@ struct event_timeout * event_add_timeout(
struct event_timeout *entry; struct event_timeout *entry;
entry = malloc(sizeof(struct event_timeout)); entry = malloc(sizeof(struct event_timeout));
if (entry == NULL) { if (entry == NULL) {
log_print(LOG_ERROR, "event_add_timeout(): out of memory"); log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
return NULL; return NULL;
} }
@ -283,19 +283,19 @@ int event_loop(int (*pre_select_cb)(int *maxfd, void *readfds, void *writefds, s
else else
retval = select(maxfd, &readfds, &writefds, NULL, &timeout); retval = select(maxfd, &readfds, &writefds, NULL, &timeout);
/* exit loop if callback returns true */
if (post_select_cb != NULL && post_select_cb(retval, (void *)&readfds, (void *)&writefds, privdata) != 0)
break;
if (retval < 0 && errno == EINTR) { if (retval < 0 && errno == EINTR) {
errno = 0; errno = 0;
continue; continue;
} else if (retval < 0) { } else if (retval < 0) {
log_print(LOG_ERROR, "event_loop(): select():"); log_print(LOG_ERROR, "%s(): select():", __FUNCTION__);
continue; continue;
} }
/* exit loop if callback returns true */
if (post_select_cb != NULL && post_select_cb(retval, (void *)&readfds, (void *)&writefds, privdata) != 0)
break;
/* timeout */ /* timeout */
if (retval == 0) if (retval == 0)
continue; continue;

77
lcd.c
View File

@ -33,6 +33,7 @@
#include "logging.h" #include "logging.h"
#define _LCD_DEBUG 1 #define _LCD_DEBUG 1
#define _LCD_DUMMY 1
#define LCD_RESET_TIMEOUT_US 250000 /* 250ms */ #define LCD_RESET_TIMEOUT_US 250000 /* 250ms */
#define LCD_RESET_RETRY_TIMEOUT 10 /* 10s */ #define LCD_RESET_RETRY_TIMEOUT 10 /* 10s */
@ -59,7 +60,9 @@ enum lcdstate {
struct lcddev { struct lcddev {
int fd; int fd;
#if (_LCD_DUMMY)
struct event_fd *fakedevice_event; struct event_fd *fakedevice_event;
#endif /* (_LCD_DUMMY) */
struct termios oldtio; struct termios oldtio;
enum lcdstate state; enum lcdstate state;
@ -102,14 +105,17 @@ void lcd_close(struct lcddev *dev)
dev->read_event = NULL; dev->read_event = NULL;
} }
if (dev->fakedevice_event == NULL) { #if (_LCD_DUMMY)
tcsetattr(dev->fd, TCSANOW, &dev->oldtio); if (dev->fakedevice_event != NULL) {
} else {
int fd = event_get_fd(dev->fakedevice_event); int fd = event_get_fd(dev->fakedevice_event);
event_remove_fd(dev->fakedevice_event); event_remove_fd(dev->fakedevice_event);
dev->fakedevice_event = NULL; dev->fakedevice_event = NULL;
close(fd); close(fd);
} else
#endif /* (_LCD_DUMMY) */
{
tcsetattr(dev->fd, TCSANOW, &dev->oldtio);
} }
close(dev->fd); close(dev->fd);
@ -120,7 +126,7 @@ static int lcd_open(struct lcddev *dev, const char *device)
{ {
dev->fd = open(device, O_RDWR); dev->fd = open(device, O_RDWR);
if (dev->fd < 0) { if (dev->fd < 0) {
log_print(LOG_ERROR, "failed to open '%s'", device); log_print(LOG_ERROR, "%s(): failed to open '%s'", __FUNCTION__, device);
return -1; return -1;
} }
@ -138,7 +144,7 @@ static int lcd_open(struct lcddev *dev, const char *device)
int err = tcsetattr(dev->fd, TCSAFLUSH, &newtio); int err = tcsetattr(dev->fd, TCSAFLUSH, &newtio);
if (err < 0) { if (err < 0) {
log_print(LOG_ERROR, "failed to set termios"); log_print(LOG_ERROR, "%s(): failed to set termios", __FUNCTION__);
close(dev->fd); close(dev->fd);
return -1; return -1;
} }
@ -146,6 +152,7 @@ static int lcd_open(struct lcddev *dev, const char *device)
return 0; return 0;
} }
#if (_LCD_DUMMY)
static int lcd_fakedevice_reply(int fd, void *privdata) static int lcd_fakedevice_reply(int fd, void *privdata)
{ {
struct lcddev *dev = (struct lcddev *)privdata; struct lcddev *dev = (struct lcddev *)privdata;
@ -169,8 +176,22 @@ static int lcd_fakedevice_open(struct lcddev *dev)
{ {
int fd[2]; int fd[2];
if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0 , fd) < 0) { if (socketpair(AF_LOCAL, SOCK_STREAM, 0 , fd) < 0) {
log_print(LOG_ERROR, "socketpair() failed"); log_print(LOG_ERROR, "%s(): socketpair() failed", __FUNCTION__);
return -1;
}
if (fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) {
log_print(LOG_ERROR, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
close(fd[0]);
close(fd[1]);
return -1;
}
if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0) {
log_print(LOG_ERROR, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
close(fd[0]);
close(fd[1]);
return -1; return -1;
} }
@ -178,19 +199,22 @@ static int lcd_fakedevice_open(struct lcddev *dev)
dev->fakedevice_event = event_add_readfd(NULL, fd[1], lcd_fakedevice_reply, dev); dev->fakedevice_event = event_add_readfd(NULL, fd[1], lcd_fakedevice_reply, dev);
return 0; return 0;
} }
#endif /* (_LCD_DUMMY) */
#if (_LCD_DEBUG > 1) #if (_LCD_DEBUG > 1)
static void lcd_dump(const char *prefix, int len, int size, const char *buf) static void lcd_dump(const char *prefix, int len, int size, const char *buf)
{ {
int i; int i;
int pos = 0;
char buf[256];
fprintf(stderr, "%s:[%d/%d]: ", prefix, len, size); for (i = 0; i < len; i++) {
for (i = 0; i < len; i++) pos += snprintf(buf, sizeof(buf) - pos, "0x%X ", (unsigned char)buf[i]);
fprintf(stderr, "0x%X ", (unsigned char)buf[i]); }
fprintf(stderr, "\n"); log_print(LOG_DEBUG, "%s:[%d/%d]: %s", prefix, len, size, buf);
} }
#endif #endif /* (_LCD_DEBUG > 1) */
static int lcd_read(struct lcddev *dev, const char *buf, int len) static int lcd_read(struct lcddev *dev, const char *buf, int len)
{ {
@ -203,8 +227,8 @@ static int lcd_read(struct lcddev *dev, const char *buf, int len)
cnt += retval; cnt += retval;
} }
#if (_LCD_DEBUG > 1) #if (_LCD_DEBUG > 1)
lcd_dump("lcd_read", cnt, len, buf); lcd_dump(__FUNCTION__, cnt, len, buf);
#endif #endif /* (_LCD_DEBUG > 1) */
return (cnt != 0) ? cnt : retval; return (cnt != 0) ? cnt : retval;
} }
@ -213,8 +237,8 @@ static int lcd_write(struct lcddev *dev, char *buf, int len)
int retval = write(dev->fd, buf, len); int retval = write(dev->fd, buf, len);
#if (_LCD_DEBUG > 1) #if (_LCD_DEBUG > 1)
lcd_dump("lcd_write", retval, len, buf); lcd_dump(__FUNCTION__, retval, len, buf);
#endif #endif /* (_LCD_DEBUG > 1) */
return retval; return retval;
} }
@ -243,8 +267,8 @@ static int lcd_reset_timeout_cb(void *privdata)
void lcd_reset(struct lcddev *dev) void lcd_reset(struct lcddev *dev)
{ {
#if (_LCD_DEBUG > 0) #if (_LCD_DEBUG > 0)
fprintf(stderr, "lcd_reset()\n"); log_print(LOG_DEBUG, "%s()", __FUNCTION__);
#endif #endif /* (_LCD_DEBUG > 0) */
char cmd[] = A125_CMD_RESET; char cmd[] = A125_CMD_RESET;
lcd_write(dev, cmd, sizeof(cmd)); lcd_write(dev, cmd, sizeof(cmd));
@ -263,8 +287,8 @@ static int lcd_backlight(struct lcddev *dev, int mode)
return -1; return -1;
#if (_LCD_DEBUG > 0) #if (_LCD_DEBUG > 0)
fprintf(stderr, "lcd_backlight(%d)\n", mode); log_print(LOG_DEBUG, "%s(%d)", __FUNCTION__, mode);
#endif #endif /* (_LCD_DEBUG > 0) */
if (dev->backlight_state != mode) { if (dev->backlight_state != mode) {
char cmd[] = A125_CMD_BACKLIGHT; char cmd[] = A125_CMD_BACKLIGHT;
@ -358,8 +382,8 @@ static int lcd_setline(struct lcddev *dev, int line, int len, const char *buf)
cmd[20] = '\0'; cmd[20] = '\0';
#if (_LCD_DEBUG > 0) #if (_LCD_DEBUG > 0)
fprintf(stderr, "lcd_setline(%d, '%-16s')\n", line, cmd +4); log_print(LOG_DEBUG, "%s(%d, '%-16s')", __FUNCTION__, line, cmd +4);
#endif #endif /* (_LCD_DEBUG > 0) */
lcd_write(dev, cmd, 20); lcd_write(dev, cmd, 20);
return 0; return 0;
@ -436,16 +460,19 @@ struct lcddev * lcd_init(const char *devicename,
{ {
struct lcddev *dev = malloc(sizeof(struct lcddev)); struct lcddev *dev = malloc(sizeof(struct lcddev));
if (dev == NULL) { if (dev == NULL) {
log_print(LOG_ERROR, "lcd_init(): out of memory"); log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
return NULL; return NULL;
} }
memset(dev, 0, sizeof(struct lcddev)); memset(dev, 0, sizeof(struct lcddev));
int retval; int retval;
#if (_LCD_DUMMY)
if (strncmp(devicename, "dummy", 5) == 0) { if (strncmp(devicename, "dummy", 5) == 0) {
retval = lcd_fakedevice_open(dev); retval = lcd_fakedevice_open(dev);
} else { } else
#endif /* (_LCD_DUMMY) */
{
retval = lcd_open(dev, devicename); retval = lcd_open(dev, devicename);
} }

View File

@ -29,7 +29,7 @@
#define BUFSIZE 8192 #define BUFSIZE 8192
static FILE *log_fd = NULL; static FILE *log_file = NULL;
static int log_prio = LOG_EVERYTIME; static int log_prio = LOG_EVERYTIME;
static char *buffer = NULL; static char *buffer = NULL;
@ -44,12 +44,12 @@ int log_print(int prio, const char *fmt, ...)
if (buffer == NULL) { if (buffer == NULL) {
buffer = malloc(BUFSIZE); buffer = malloc(BUFSIZE);
if (buffer == NULL) { if (buffer == NULL) {
fprintf(stderr, "log_print(): out of memory\n"); fprintf(stderr, "%s(): out of memory\n", __FUNCTION__);
return -1; return -1;
} }
} }
if (log_fd != NULL) { if (log_file != NULL) {
time_t tzgr; time_t tzgr;
time(&tzgr); time(&tzgr);
@ -62,7 +62,7 @@ int log_print(int prio, const char *fmt, ...)
if (len < 0 || len >= BUFSIZE) { if (len < 0 || len >= BUFSIZE) {
errno = 0; errno = 0;
return log_print(LOG_ERROR, "log_print: arguments too long"); return log_print(LOG_ERROR, "%s: arguments too long", __FUNCTION__);
} }
if (errno) { if (errno) {
@ -70,8 +70,8 @@ int log_print(int prio, const char *fmt, ...)
errno = 0; errno = 0;
} }
retval = fprintf((log_fd ? log_fd : stderr), "%s\n", buffer); retval = fprintf((log_file ? log_file : stderr), "%s\n", buffer);
fflush(log_fd); fflush(log_file);
return retval; return retval;
} }
@ -82,25 +82,26 @@ void log_close(void)
buffer = NULL; buffer = NULL;
} }
if (log_fd) { if (log_file) {
fclose(log_fd); fclose(log_file);
log_fd = NULL; log_file = NULL;
} }
} }
int log_init(const char *logfile) int log_init(const char *logfile)
{ {
if (log_fd != NULL) if (log_file != NULL)
log_close(); log_close();
log_fd = fopen(logfile, "a"); log_file = fopen(logfile, "a");
if (log_fd == NULL) { if (log_file == NULL) {
fprintf(stderr, "log_init(): can not open logfile"); fprintf(stderr, "%s(): can not open logfile", __FUNCTION__);
return -1; return -1;
} }
if (fcntl(fileno(log_fd), F_SETFD, FD_CLOEXEC) < 0) { if (fcntl(fileno(log_file), F_SETFD, FD_CLOEXEC) < 0) {
fprintf(stderr, "log_init(): fcntl(FD_CLOEXEC)"); fprintf(stderr, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
fclose(log_file);
return -1; return -1;
} }

View File

@ -68,7 +68,7 @@ int signal_remove_callback(int signum, int type)
} }
if (sigaction(signum, &sig_action, NULL) < 0) { if (sigaction(signum, &sig_action, NULL) < 0) {
log_print(LOG_WARN, "signal_remove_callback(): sigaction()"); log_print(LOG_WARN, "%s(): sigaction(%d)", __FUNCTION__, signum);
return -1; return -1;
} }
@ -81,7 +81,7 @@ int signal_add_callback(int signum, void (*callback)(void *privdata), void *priv
{ {
struct signal_entry *entry = malloc(sizeof(struct signal_entry)); struct signal_entry *entry = malloc(sizeof(struct signal_entry));
if (entry == NULL) { if (entry == NULL) {
log_print(LOG_WARN, "signal_add_callback(): out of memory"); log_print(LOG_WARN, "%s(): out of memory", __FUNCTION__);
return -1; return -1;
} }
@ -96,7 +96,7 @@ int signal_add_callback(int signum, void (*callback)(void *privdata), void *priv
}; };
if (sigaction(signum, &sig_action, NULL) < 0) { if (sigaction(signum, &sig_action, NULL) < 0) {
log_print(LOG_WARN, "signal_add_callback(): sigaction()"); log_print(LOG_WARN, "%s(): sigaction(%d)", __FUNCTION__, signum);
list_del(&entry->list); list_del(&entry->list);
free(entry); free(entry);
return -1; return -1;
@ -110,7 +110,7 @@ static int sig_event(int fd, void *privdata)
unsigned char signum; unsigned char signum;
int len = read(fd, &signum, 1); int len = read(fd, &signum, 1);
if (len <= 0) { if (len <= 0) {
log_print(LOG_WARN, "sig_event(): read()"); log_print(LOG_WARN, "%s(): read()", __FUNCTION__);
return -1; return -1;
} }
@ -132,17 +132,17 @@ static int sig_event(int fd, void *privdata)
int signal_init(void) int signal_init(void)
{ {
if (pipe(sig_pipe) < 0) { if (pipe(sig_pipe) < 0) {
log_print(LOG_ERROR, "signal_init(): pipe()"); log_print(LOG_ERROR, "%s(): pipe()", __FUNCTION__);
return -1; return -1;
} }
if (fcntl(sig_pipe[0], F_SETFD, FD_CLOEXEC) < 0) { if (fcntl(sig_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)"); log_print(LOG_WARN, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
return -1; return -1;
} }
if (fcntl(sig_pipe[1], F_SETFD, FD_CLOEXEC) < 0) { if (fcntl(sig_pipe[1], F_SETFD, FD_CLOEXEC) < 0) {
log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)"); log_print(LOG_WARN, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
return -1; return -1;
} }