2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
19 * The Initial Developer of the Original Code is
20 * Anthony Minessale II <anthm@freeswitch.org>
21 * Portions created by the Initial Developer are Copyright (C)
22 * the Initial Developer. All Rights Reserved.
26 * Anthony Minessale II <anthm@freeswitch.org>
27 * Michael Jerris <mike@jerris.com>
28 * Paul D. Tinsley <pdt at jackhammer.org>
31 * switch_core_timer.c -- Main Core Library (timer interface)
36 #include "private/switch_core_pvt.h"
38 SWITCH_DECLARE(switch_status_t
) switch_core_timer_init(switch_timer_t
*timer
, const char *timer_name
, int interval
, int samples
,
39 switch_memory_pool_t
*pool
)
41 switch_timer_interface_t
*timer_interface
;
42 switch_status_t status
;
43 memset(timer
, 0, sizeof(*timer
));
44 if ((timer_interface
= switch_loadable_module_get_timer_interface(timer_name
)) == 0 || !timer_interface
->timer_init
) {
45 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "invalid timer %s!\n", timer_name
);
46 return SWITCH_STATUS_GENERR
;
49 timer
->interval
= interval
;
50 timer
->samples
= samples
;
51 timer
->samplecount
= samples
;
52 timer
->timer_interface
= timer_interface
;
55 timer
->memory_pool
= pool
;
57 if ((status
= switch_core_new_memory_pool(&timer
->memory_pool
)) != SWITCH_STATUS_SUCCESS
) {
58 UNPROTECT_INTERFACE(timer
->timer_interface
);
61 switch_set_flag(timer
, SWITCH_TIMER_FLAG_FREE_POOL
);
64 return timer
->timer_interface
->timer_init(timer
);
67 SWITCH_DECLARE(switch_status_t
) switch_core_timer_next(switch_timer_t
*timer
)
69 if (!timer
->timer_interface
|| !timer
->timer_interface
->timer_next
) {
70 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Timer is not properly configured.\n");
71 return SWITCH_STATUS_GENERR
;
74 if (timer
->timer_interface
->timer_next(timer
) == SWITCH_STATUS_SUCCESS
) {
75 return SWITCH_STATUS_SUCCESS
;
77 return SWITCH_STATUS_GENERR
;
82 SWITCH_DECLARE(switch_status_t
) switch_core_timer_step(switch_timer_t
*timer
)
84 if (!timer
->timer_interface
|| !timer
->timer_interface
->timer_step
) {
85 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Timer is not properly configured.\n");
86 return SWITCH_STATUS_GENERR
;
89 return timer
->timer_interface
->timer_step(timer
);
93 SWITCH_DECLARE(switch_status_t
) switch_core_timer_sync(switch_timer_t
*timer
)
95 if (!timer
->timer_interface
|| !timer
->timer_interface
->timer_sync
) {
96 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Timer is not properly configured.\n");
97 return SWITCH_STATUS_GENERR
;
100 return timer
->timer_interface
->timer_sync(timer
);
103 SWITCH_DECLARE(switch_status_t
) switch_core_timer_check(switch_timer_t
*timer
, switch_bool_t step
)
105 if (!timer
->timer_interface
|| !timer
->timer_interface
->timer_check
) {
106 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Timer is not properly configured.\n");
107 return SWITCH_STATUS_GENERR
;
110 return timer
->timer_interface
->timer_check(timer
, step
);
114 SWITCH_DECLARE(switch_status_t
) switch_core_timer_destroy(switch_timer_t
*timer
)
116 if (!timer
->timer_interface
|| !timer
->timer_interface
->timer_destroy
) {
117 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Timer is not properly configured.\n");
118 return SWITCH_STATUS_GENERR
;
121 timer
->timer_interface
->timer_destroy(timer
);
122 UNPROTECT_INTERFACE(timer
->timer_interface
);
124 if (switch_test_flag(timer
, SWITCH_TIMER_FLAG_FREE_POOL
)) {
125 switch_core_destroy_memory_pool(&timer
->memory_pool
);
128 memset(timer
, 0, sizeof(*timer
));
130 return SWITCH_STATUS_SUCCESS
;
141 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: