timing: Improve performance for most timing implementations.
[asterisk/asterisk.git] / res / res_timing_dahdi.c
index 80b7b49..7af71f1 100644 (file)
@@ -25,6 +25,7 @@
 
 /*** MODULEINFO
        <depend>dahdi</depend>
+       <support_level>core</support_level>
  ***/
 
 #include "asterisk.h"
@@ -44,15 +45,19 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
 
 static void *timing_funcs_handle;
 
-static int dahdi_timer_open(void);
-static void dahdi_timer_close(int handle);
-static int dahdi_timer_set_rate(int handle, unsigned int rate);
-static void dahdi_timer_ack(int handle, unsigned int quantity);
-static int dahdi_timer_enable_continuous(int handle);
-static int dahdi_timer_disable_continuous(int handle);
-static enum ast_timing_event dahdi_timer_get_event(int handle);
-
-static struct ast_timing_functions dahdi_timing_functions = {
+static void *dahdi_timer_open(void);
+static void dahdi_timer_close(void *data);
+static int dahdi_timer_set_rate(void *data, unsigned int rate);
+static int dahdi_timer_ack(void *data, unsigned int quantity);
+static int dahdi_timer_enable_continuous(void *data);
+static int dahdi_timer_disable_continuous(void *data);
+static enum ast_timer_event dahdi_timer_get_event(void *data);
+static unsigned int dahdi_timer_get_max_rate(void *data);
+static int dahdi_timer_fd(void *data);
+
+static struct ast_timing_interface dahdi_timing = {
+       .name = "DAHDI",
+       .priority = 100,
        .timer_open = dahdi_timer_open,
        .timer_close = dahdi_timer_close,
        .timer_set_rate = dahdi_timer_set_rate,
@@ -60,27 +65,49 @@ static struct ast_timing_functions dahdi_timing_functions = {
        .timer_enable_continuous = dahdi_timer_enable_continuous,
        .timer_disable_continuous = dahdi_timer_disable_continuous,
        .timer_get_event = dahdi_timer_get_event,
+       .timer_get_max_rate = dahdi_timer_get_max_rate,
+       .timer_fd = dahdi_timer_fd,
+};
+
+struct dahdi_timer {
+       int fd;
 };
 
-static int dahdi_timer_open(void)
+static void *dahdi_timer_open(void)
 {
-       return open("/dev/dahdi/timer", O_RDWR);
+       struct dahdi_timer *timer;
+
+       if (!(timer = ast_calloc(1, sizeof(*timer)))) {
+               return NULL;
+       }
+
+       if ((timer->fd = open("/dev/dahdi/timer", O_RDWR)) < 0) {
+               ast_log(LOG_ERROR, "Failed to create dahdi timer: %s\n", strerror(errno));
+               ast_free(timer);
+               return NULL;
+       }
+
+       return timer;
 }
 
-static void dahdi_timer_close(int handle)
+static void dahdi_timer_close(void *data)
 {
-       close(handle);
+       struct dahdi_timer *timer = data;
+
+       close(timer->fd);
+       ast_free(timer);
 }
 
-static int dahdi_timer_set_rate(int handle, unsigned int rate)
+static int dahdi_timer_set_rate(void *data, unsigned int rate)
 {
+       struct dahdi_timer *timer = data;
        int samples;
 
        /* DAHDI timers are configured using a number of samples,
         * based on an 8 kHz sample rate. */
        samples = (unsigned int) roundf((8000.0 / ((float) rate)));
 
-       if (ioctl(handle, DAHDI_TIMERCONFIG, &samples)) {
+       if (ioctl(timer->fd, DAHDI_TIMERCONFIG, &samples)) {
                ast_log(LOG_ERROR, "Failed to configure DAHDI timing fd for %u sample timer ticks\n",
                        samples);
                return -1;
@@ -89,31 +116,36 @@ static int dahdi_timer_set_rate(int handle, unsigned int rate)
        return 0;
 }
 
-static void dahdi_timer_ack(int handle, unsigned int quantity)
+static int dahdi_timer_ack(void *data, unsigned int quantity)
 {
-       ioctl(handle, DAHDI_TIMERACK, &quantity);
+       struct dahdi_timer *timer = data;
+
+       return ioctl(timer->fd, DAHDI_TIMERACK, &quantity) ? -1 : 0;
 }
 
-static int dahdi_timer_enable_continuous(int handle)
+static int dahdi_timer_enable_continuous(void *data)
 {
+       struct dahdi_timer *timer = data;
        int flags = 1;
 
-       return ioctl(handle, DAHDI_TIMERPING, &flags) ? -1 : 0;
+       return ioctl(timer->fd, DAHDI_TIMERPING, &flags) ? -1 : 0;
 }
 
-static int dahdi_timer_disable_continuous(int handle)
+static int dahdi_timer_disable_continuous(void *data)
 {
+       struct dahdi_timer *timer = data;
        int flags = -1;
 
-       return ioctl(handle, DAHDI_TIMERPONG, &flags) ? -1 : 0;
+       return ioctl(timer->fd, DAHDI_TIMERPONG, &flags) ? -1 : 0;
 }
 
-static enum ast_timing_event dahdi_timer_get_event(int handle)
+static enum ast_timer_event dahdi_timer_get_event(void *data)
 {
+       struct dahdi_timer *timer = data;
        int res;
        int event;
 
-       res = ioctl(handle, DAHDI_GETEVENT, &event);
+       res = ioctl(timer->fd, DAHDI_GETEVENT, &event);
 
        if (res) {
                event = DAHDI_EVENT_TIMER_EXPIRED;
@@ -128,6 +160,20 @@ static enum ast_timing_event dahdi_timer_get_event(int handle)
        }
 }
 
+static unsigned int dahdi_timer_get_max_rate(void *data)
+{
+       return 1000;
+}
+
+static int dahdi_timer_fd(void *data)
+{
+       struct dahdi_timer *timer = data;
+
+       return timer->fd;
+}
+
+#define SEE_TIMING "For more information on Asterisk timing modules, including ways to potentially fix this problem, please see https://wiki.asterisk.org/wiki/display/AST/Timing+Interfaces\n"
+
 static int dahdi_test_timer(void)
 {
        int fd;
@@ -140,13 +186,13 @@ static int dahdi_test_timer(void)
        }
 
        if (ioctl(fd, DAHDI_TIMERCONFIG, &x)) {
-               ast_log(LOG_ERROR, "You have DAHDI built and drivers loaded, but the DAHDI timer test failed to set DAHDI_TIMERCONFIG to %d.\n", x);
+               ast_log(LOG_ERROR, "You have DAHDI built and drivers loaded, but the DAHDI timer test failed to set DAHDI_TIMERCONFIG to %d.\n" SEE_TIMING, x);
                close(fd);
                return -1;
        }
 
        if ((x = ast_wait_for_input(fd, 300)) < 0) {
-               ast_log(LOG_ERROR, "You have DAHDI built and drivers loaded, but the DAHDI timer could not be polled during the DAHDI timer test.\n");
+               ast_log(LOG_ERROR, "You have DAHDI built and drivers loaded, but the DAHDI timer could not be polled during the DAHDI timer test.\n" SEE_TIMING);
                close(fd);
                return -1;
        }
@@ -158,7 +204,7 @@ static int dahdi_test_timer(void)
                        "\n\t2. You only have to load DAHDI drivers if you want to take advantage of DAHDI services.  One option is to unload DAHDI modules if you don't need them."
                        "\n\t3. If you need DAHDI services, you must correctly configure DAHDI."
                };
-               ast_log(LOG_ERROR, "%s\n", dahdi_timer_error);
+               ast_log(LOG_ERROR, "%s\n" SEE_TIMING, dahdi_timer_error);
                usleep(100);
                close(fd);
                return -1;
@@ -175,17 +221,21 @@ static int load_module(void)
                return AST_MODULE_LOAD_DECLINE;
        }
 
-       return (timing_funcs_handle = ast_install_timing_functions(&dahdi_timing_functions)) ?
+       return (timing_funcs_handle = ast_register_timing_interface(&dahdi_timing)) ?
                AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
 }
 
 static int unload_module(void)
 {
-       /* ast_uninstall_timing_functions(timing_funcs_handle); */
-
-       /* This module can not currently be unloaded.  No use count handling is being done. */
+       if (timing_funcs_handle) {
+               return ast_unregister_timing_interface(timing_funcs_handle);
+       }
 
-       return -1;
+       return 0;
 }
 
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DAHDI Timing Interface");
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "DAHDI Timing Interface",
+               .load = load_module,
+               .unload = unload_module,
+               .load_pri = AST_MODPRI_TIMING,
+               );