/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 2007, Redfish Solutions * * Philip Prindeville * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. */ /*! \file * * \brief Sleep until the given epoch * * \ingroup applications */ #include #include #include #include #include #include #include "asterisk.h" ASTERISK_FILE_VERSION(__FILE__, "$Revision: 0 $") #include "asterisk/lock.h" #include "asterisk/file.h" #include "asterisk/logger.h" #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/app.h" #include "asterisk/options.h" static char *tdesc = "Generic WaitUntil() application"; static char *app = "WaitUntil"; static char *synopsis = "Wait (sleep) until the current time is the given epoch"; static char *descrip = " WaitUntil(epoch): Waits until the current time is that given. Returns\n" "immediately if the epoch is in the past.\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; static int waituntil_exec(struct ast_channel *chan, void *data) { int res = 0; struct localuser *u; time_t future; struct timeval tv; ulong msec; if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n"); return -1; } LOCAL_USER_ADD(u); if (sscanf(data, "%lu", &future) != 1) { ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n"); LOCAL_USER_REMOVE(u); return -1; } /* * Get the current time, and calculate the number of milliseconds * until then (rounding up from microseconds). */ gettimeofday(&tv, NULL); if (future <= tv.tv_sec) { ast_log(LOG_NOTICE, "WaitUntil called in the past (now %lu, arg %lu)\n", tv.tv_sec, future); LOCAL_USER_REMOVE(u); return 0; } msec = (future - tv.tv_sec) * 1000 - ((tv.tv_usec + 500) / 1000); res = ast_safe_sleep(chan, msec); LOCAL_USER_REMOVE(u); return res; } int unload_module(void) { int res; res = ast_unregister_application(app); STANDARD_HANGUP_LOCALUSERS; return res; } int load_module(void) { int res; res = ast_register_application(app, waituntil_exec, synopsis, descrip); return res; } char *description(void) { return tdesc; } int usecount(void) { int res; STANDARD_USECOUNT(res); return res; } char *key() { return ASTERISK_GPL_KEY; }