update timer interface

This commit is contained in:
Olaf Rempel 2012-11-25 13:47:29 +01:00
parent 1189b08b54
commit 86e94cce91
6 changed files with 51 additions and 39 deletions

View File

@ -26,7 +26,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h> #include <ctype.h>
#include <time.h>
#include "configfile.h" #include "configfile.h"
#include "event.h" #include "event.h"
@ -55,7 +54,7 @@ struct disk_entry {
static LIST_HEAD(disk_list); static LIST_HEAD(disk_list);
static struct event_timeout *check_timeout; static struct event_timeout *check_timeout;
static int diskto_timeout_cb(void *privdata) static int diskto_timeout_cb(int timerid, void *privdata)
{ {
struct disk_entry *entry = (struct disk_entry *) privdata; struct disk_entry *entry = (struct disk_entry *) privdata;
entry->timeout_event = NULL; entry->timeout_event = NULL;
@ -113,15 +112,14 @@ static int diskto_add_disk(struct strtoken *tokens, void *privdata)
entry->sectors_rd = 0; entry->sectors_rd = 0;
entry->sectors_wr = 0; entry->sectors_wr = 0;
struct timeval tv = { .tv_sec = entry->timeout }; entry->timeout_event = event_add_timeout_ms(entry->timeout * 1000, diskto_timeout_cb, 0, (void *)entry);
entry->timeout_event = event_add_timeout(&tv, diskto_timeout_cb, (void *)entry);
log_print(LOG_INFO, "disktimeout: watching '%s' (standby timeout: %ds)", entry->device, entry->timeout); log_print(LOG_INFO, "disktimeout: watching '%s' (standby timeout: %ds)", entry->device, entry->timeout);
list_add_tail(&entry->list, &disk_list); list_add_tail(&entry->list, &disk_list);
return 0; return 0;
} }
static int diskto_check_cb(void *privdata) static int diskto_check_cb(int timerid, void *privdata)
{ {
struct disk_entry *entry; struct disk_entry *entry;
list_for_each_entry(entry, &disk_list, list) { list_for_each_entry(entry, &disk_list, list) {
@ -190,8 +188,7 @@ static int diskto_check_cb(void *privdata)
event_remove_timeout(entry->timeout_event); event_remove_timeout(entry->timeout_event);
/* set new one */ /* set new one */
struct timeval tv = { .tv_sec = entry->timeout }; entry->timeout_event = event_add_timeout_ms(entry->timeout * 1000, diskto_timeout_cb, 0, (void *)entry);
entry->timeout_event = event_add_timeout(&tv, diskto_timeout_cb, (void *)entry);
} }
} }
@ -232,8 +229,7 @@ static int diskto_check_cb(void *privdata)
/* device is not in standby mode, start timer */ /* device is not in standby mode, start timer */
if (entry->timeout_event == NULL) { if (entry->timeout_event == NULL) {
struct timeval tv = { .tv_sec = entry->timeout }; entry->timeout_event = event_add_timeout_ms(entry->timeout * 1000, diskto_timeout_cb, 0, (void *)entry);
entry->timeout_event = event_add_timeout(&tv, diskto_timeout_cb, (void *)entry);
} }
entry->flags &= ~(F_STANDBY); entry->flags &= ~(F_STANDBY);
} }
@ -284,8 +280,7 @@ int disktimeout_init(void)
int check_interval = 0; int check_interval = 0;
config_get_int("disktimeout", "check_interval", &check_interval, 60); config_get_int("disktimeout", "check_interval", &check_interval, 60);
struct timeval tv = { .tv_sec = check_interval }; check_timeout = event_add_timeout_ms(check_interval * 1000, diskto_check_cb, 0, NULL);
check_timeout = event_add_timeout(&tv, diskto_check_cb, NULL);
return 0; return 0;
} }

22
event.c
View File

@ -47,7 +47,8 @@ struct event_timeout {
unsigned int flags; unsigned int flags;
struct timeval intervall; struct timeval intervall;
struct timeval nextrun; struct timeval nextrun;
int (*callback)(void *privdata); int (*callback)(int timerid, void *privdata);
int timerid;
void *privdata; void *privdata;
}; };
@ -171,7 +172,8 @@ static void schedule_nextrun(struct event_timeout *entry, struct timeval *now)
struct event_timeout * event_add_timeout( struct event_timeout * event_add_timeout(
struct timeval *timeout, struct timeval *timeout,
int (*callback)(void *privdata), int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata) void *privdata)
{ {
struct event_timeout *entry; struct event_timeout *entry;
@ -184,6 +186,7 @@ struct event_timeout * event_add_timeout(
entry->flags = 0; entry->flags = 0;
memcpy(&entry->intervall, timeout, sizeof(entry->intervall)); memcpy(&entry->intervall, timeout, sizeof(entry->intervall));
entry->callback = callback; entry->callback = callback;
entry->timerid = timerid;
entry->privdata = privdata; entry->privdata = privdata;
struct timeval now; struct timeval now;
@ -193,6 +196,19 @@ struct event_timeout * event_add_timeout(
return entry; return entry;
} }
struct event_timeout * event_add_timeout_ms(
int timeout_ms,
int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata)
{
struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
return event_add_timeout(&tv, callback, timerid, privdata);
}
void event_remove_timeout(struct event_timeout *entry) void event_remove_timeout(struct event_timeout *entry)
{ {
/* mark the event as deleted -> remove in select() loop */ /* mark the event as deleted -> remove in select() loop */
@ -230,7 +246,7 @@ int event_loop(int (*pre_select_cb)(int *maxfd, void *readfds, void *writefds, s
list_del(&entry->list); list_del(&entry->list);
/* execute callback, when callback returns 0 -> schedule event again */ /* execute callback, when callback returns 0 -> schedule event again */
if (entry->callback(entry->privdata)) { if (entry->callback(entry->timerid, entry->privdata)) {
free(entry); free(entry);
} else { } else {

View File

@ -32,7 +32,14 @@ void event_remove_fd(struct event_fd *entry);
struct event_timeout * event_add_timeout( struct event_timeout * event_add_timeout(
struct timeval *timeout, struct timeval *timeout,
int (*callback)(void *privdata), int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata);
struct event_timeout * event_add_timeout_ms(
int timeout_ms,
int (*callback)(int timerid, void *privdata),
int timerid,
void *privdata); void *privdata);
void event_remove_timeout(struct event_timeout *entry); void event_remove_timeout(struct event_timeout *entry);

38
lcd.c
View File

@ -35,8 +35,8 @@
#define _LCD_DEBUG 1 #define _LCD_DEBUG 1
#define _LCD_DUMMY 1 #define _LCD_DUMMY 1
#define LCD_RESET_TIMEOUT_US 250000 /* 250ms */ #define LCD_RESET_TIMEOUT 250 /* 250ms */
#define LCD_RESET_RETRY_TIMEOUT 10 /* 10s */ #define LCD_RESET_RETRY_TIMEOUT 10000 /* 10s */
#define A125_CMD_GETBUTTON { 0x4D, 0x06 } // not tried #define A125_CMD_GETBUTTON { 0x4D, 0x06 } // not tried
#define A125_CMD_SETLINE { 0x4D, 0x0C, 0x00, 0x10 } // [2] is line, append 16 chars #define A125_CMD_SETLINE { 0x4D, 0x0C, 0x00, 0x10 } // [2] is line, append 16 chars
@ -128,7 +128,7 @@ void lcd_close(struct lcddev *dev)
free(dev); free(dev);
} }
static int lcd_open(struct lcddev *dev, const char *device) static int lcd_realdevice_open(struct lcddev *dev, const char *device)
{ {
dev->fd = open(device, O_RDWR | O_NOCTTY | O_CLOEXEC); dev->fd = open(device, O_RDWR | O_NOCTTY | O_CLOEXEC);
if (dev->fd < 0) { if (dev->fd < 0) {
@ -248,7 +248,7 @@ static int lcd_write(struct lcddev *dev, char *buf, int len)
return retval; return retval;
} }
static int lcd_reset_retry_timeout_cb(void *privdata) static int lcd_reset_retry_timeout_cb(int timerid, void *privdata)
{ {
struct lcddev *dev = (struct lcddev *)privdata; struct lcddev *dev = (struct lcddev *)privdata;
dev->reset_timeout = NULL; dev->reset_timeout = NULL;
@ -257,15 +257,14 @@ static int lcd_reset_retry_timeout_cb(void *privdata)
return -1; /* singleshot */ return -1; /* singleshot */
} }
static int lcd_reset_timeout_cb(void *privdata) static int lcd_reset_timeout_cb(int timerid, void *privdata)
{ {
struct lcddev *dev = (struct lcddev *)privdata; struct lcddev *dev = (struct lcddev *)privdata;
log_print(LOG_ERROR, "failed to initalize LCD"); log_print(LOG_ERROR, "failed to initalize LCD");
dev->state = LCD_STATE_INITIALIZING_FAILED; dev->state = LCD_STATE_INITIALIZING_FAILED;
struct timeval tv = { .tv_sec = LCD_RESET_RETRY_TIMEOUT }; dev->reset_timeout = event_add_timeout_ms(LCD_RESET_RETRY_TIMEOUT, lcd_reset_retry_timeout_cb, 0, dev);
dev->reset_timeout = event_add_timeout(&tv, lcd_reset_retry_timeout_cb, dev);
return -1; /* singleshot */ return -1; /* singleshot */
} }
@ -283,8 +282,7 @@ void lcd_reset(struct lcddev *dev)
dev->state = LCD_STATE_INITIALIZING; dev->state = LCD_STATE_INITIALIZING;
struct timeval tv = { .tv_usec = LCD_RESET_TIMEOUT_US }; dev->reset_timeout = event_add_timeout_ms(LCD_RESET_TIMEOUT, lcd_reset_timeout_cb, 0, dev);
dev->reset_timeout = event_add_timeout(&tv, lcd_reset_timeout_cb, dev);
} }
static int lcd_backlight(struct lcddev *dev, int mode) static int lcd_backlight(struct lcddev *dev, int mode)
@ -306,7 +304,7 @@ static int lcd_backlight(struct lcddev *dev, int mode)
return 0; return 0;
} }
static int lcd_backlight_timeout_cb(void *privdata) static int lcd_backlight_timeout_cb(int timerid, void *privdata)
{ {
struct lcddev *dev = (struct lcddev *)privdata; struct lcddev *dev = (struct lcddev *)privdata;
dev->backlight_timeout = NULL; dev->backlight_timeout = NULL;
@ -326,14 +324,13 @@ int lcd_trigger_backlight(struct lcddev *dev, int timeout)
int retval = lcd_backlight(dev, (timeout == 0) ? 0 : 1); int retval = lcd_backlight(dev, (timeout == 0) ? 0 : 1);
if (timeout != -1) { if (timeout != -1) {
struct timeval tv = { .tv_sec = timeout }; dev->backlight_timeout = event_add_timeout_ms(timeout * 1000, lcd_backlight_timeout_cb, 0, dev);
dev->backlight_timeout = event_add_timeout(&tv, lcd_backlight_timeout_cb, dev);
} }
return retval; return retval;
} }
static int lcd_update_timeout_cb(void *privdata) static int lcd_update_timeout_cb(int timerid, void *privdata)
{ {
struct lcddev *dev = (struct lcddev *)privdata; struct lcddev *dev = (struct lcddev *)privdata;
dev->update_timeout = NULL; dev->update_timeout = NULL;
@ -342,8 +339,7 @@ static int lcd_update_timeout_cb(void *privdata)
/* update only if backlight is on (display visible) */ /* update only if backlight is on (display visible) */
if ((update != 0) && (dev->backlight_state != 0)) { if ((update != 0) && (dev->backlight_state != 0)) {
struct timeval tv = { .tv_sec = update / 1000, .tv_usec = (update % 1000) * 1000 }; dev->update_timeout = event_add_timeout_ms(update, lcd_update_timeout_cb, 0, dev);
dev->update_timeout = event_add_timeout(&tv, lcd_update_timeout_cb, dev);
} }
return -1; /* singleshot */ return -1; /* singleshot */
@ -396,8 +392,7 @@ static int lcd_read_cb(int fd, void *privdata)
/* update only if backlight is on (display visible) */ /* update only if backlight is on (display visible) */
if ((update != 0) && (dev->backlight_state != 0)) { if ((update != 0) && (dev->backlight_state != 0)) {
struct timeval tv = { .tv_sec = update / 1000, .tv_usec = (update % 1000) * 1000 }; dev->update_timeout = event_add_timeout_ms(update, lcd_update_timeout_cb, 0, dev);
dev->update_timeout = event_add_timeout(&tv, lcd_update_timeout_cb, dev);
} }
return 0; return 0;
@ -423,7 +418,7 @@ static int lcd_setline(struct lcddev *dev, int line, int len, const char *buf)
return 0; return 0;
} }
static int lcd_scroll_timeout_cb(void *privdata) static int lcd_scroll_timeout_cb(int timerid, void *privdata)
{ {
struct lcddev *dev = (struct lcddev *)privdata; struct lcddev *dev = (struct lcddev *)privdata;
int i, reset_pos = 0; int i, reset_pos = 0;
@ -502,14 +497,13 @@ int lcd_setlines(struct lcddev *dev, int scroll_speed, const char *line1, const
dev->scroll_pos = 0; dev->scroll_pos = 0;
if ((dev->scroll_timeout == NULL) && scroll_enable && (scroll_speed > 0)) { if ((dev->scroll_timeout == NULL) && scroll_enable && (scroll_speed > 0)) {
struct timeval tv = { .tv_sec = dev->scroll_speed / 1000, .tv_usec = (dev->scroll_speed % 1000) * 1000 }; dev->scroll_timeout = event_add_timeout_ms(dev->scroll_speed, lcd_scroll_timeout_cb, 0, dev);
dev->scroll_timeout = event_add_timeout(&tv, lcd_scroll_timeout_cb, dev);
} }
return 0; return 0;
} }
struct lcddev * lcd_init(const char *devicename, struct lcddev * lcd_open(const char *devicename,
int (*event_callback)(struct lcddev *dev, int button, void *privdata), int (*event_callback)(struct lcddev *dev, int button, void *privdata),
void *event_privdata) void *event_privdata)
{ {
@ -528,7 +522,7 @@ struct lcddev * lcd_init(const char *devicename,
} else } else
#endif /* (_LCD_DUMMY) */ #endif /* (_LCD_DUMMY) */
{ {
retval = lcd_open(dev, devicename); retval = lcd_realdevice_open(dev, devicename);
} }
if (retval < 0) { if (retval < 0) {

2
lcd.h
View File

@ -9,7 +9,7 @@
struct lcddev; /* private data */ struct lcddev; /* private data */
struct lcddev * lcd_init(const char *device, struct lcddev * lcd_open(const char *device,
int (*event_callback)(struct lcddev *dev, int event, void *privdata), int (*event_callback)(struct lcddev *dev, int event, void *privdata),
void *event_privdata); void *event_privdata);

View File

@ -241,7 +241,7 @@ int main(int argc, char *argv[])
const char *lcddevice = config_get_string("global", "lcddevice", NULL); const char *lcddevice = config_get_string("global", "lcddevice", NULL);
struct lcddev *dev = NULL; struct lcddev *dev = NULL;
if (lcddevice != NULL) { if (lcddevice != NULL) {
dev = lcd_init(lcddevice, &button_callback, NULL); dev = lcd_open(lcddevice, &button_callback, NULL);
if (dev == NULL) if (dev == NULL)
break; break;
} }