mirror of https://github.com/asterisk/asterisk
This patch includes a number of changes to the indications API. The primary motivation for this work was to improve stability. The object management in this API was significantly flawed, and a number of trivial situations could cause crashes. The changes included are: 1) Remove the module res_indications. This included the critical functionality that actually loaded the indications configuration. I have seen many people have Asterisk problems because they accidentally did not have an indications.conf present and loaded. Now, this code is in the core, and Asterisk will fail to start without indications configuration. There was one part of res_indications, the dialplan applications, which did belong in a module, and have been moved to a new module, app_playtones. 2) Object management has been significantly changed. Tone zones are now managed using astobj2, and it is no longer possible to crash Asterisk by issuing a reload that destroys tone zones while they are in use. 3) The API documentation has been filled out. 4) The API has been updated to follow our naming conventions. 5) Various bits of code throughout the tree have been updated to account for the API update. 6) Configuration parsing has been mostly re-written. 7) "Code cleanup" The code is from svn/asterisk/team/russell/indications/. Review: http://reviewboard.digium.com/r/149/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@176627 65c4cc65-6c06-0410-ace0-fbb531ad65f31.6.2
parent
4ac9617be5
commit
4ec301360c
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2009, Digium, Inc.
|
||||
*
|
||||
* Russell Bryant <russell@digium.com>
|
||||
*
|
||||
* 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 Playtones application
|
||||
*
|
||||
* \author Russell Bryant <russell@digium.com>
|
||||
*
|
||||
* \ingroup applications
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/pbx.h"
|
||||
#include "asterisk/channel.h"
|
||||
#include "asterisk/indications.h"
|
||||
|
||||
static const char playtones_app[] = "PlayTones";
|
||||
static const char stopplaytones_app[] = "StopPlayTones";
|
||||
|
||||
/*** DOCUMENTATION
|
||||
<application name="PlayTones" language="en_US">
|
||||
<synopsis>
|
||||
Play a tone list.
|
||||
</synopsis>
|
||||
<syntax>
|
||||
<parameter name="arg" required="true">
|
||||
<para>Arg is either the tone name defined in the <filename>indications.conf</filename>
|
||||
configuration file, or a directly specified list of frequencies and durations.</para>
|
||||
</parameter>
|
||||
</syntax>
|
||||
<description>
|
||||
<para>Plays a tone list. Execution will continue with the next step in the dialplan
|
||||
immediately while the tones continue to play.</para>
|
||||
<para>See the sample <filename>indications.conf</filename> for a description of the
|
||||
specification of a tonelist.</para>
|
||||
</description>
|
||||
<see-also>
|
||||
<ref type="application">StopPlayTones</ref>
|
||||
</see-also>
|
||||
</application>
|
||||
<application name="StopPlayTones" language="en_US">
|
||||
<synopsis>
|
||||
Stop playing a tone list.
|
||||
</synopsis>
|
||||
<syntax />
|
||||
<description>
|
||||
<para>Stop playing a tone list, initiated by PlayTones().</para>
|
||||
</description>
|
||||
<see-also>
|
||||
<ref type="application">PlayTones</ref>
|
||||
</see-also>
|
||||
</application>
|
||||
***/
|
||||
|
||||
static int handle_playtones(struct ast_channel *chan, void *data)
|
||||
{
|
||||
struct ast_tone_zone_sound *ts;
|
||||
int res;
|
||||
const char *str = data;
|
||||
|
||||
if (ast_strlen_zero(str)) {
|
||||
ast_log(LOG_NOTICE,"Nothing to play\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ts = ast_get_indication_tone(chan->zone, str);
|
||||
|
||||
if (ts) {
|
||||
res = ast_playtones_start(chan, 0, ts->data, 0);
|
||||
ts = ast_tone_zone_sound_unref(ts);
|
||||
} else {
|
||||
res = ast_playtones_start(chan, 0, str, 0);
|
||||
}
|
||||
|
||||
if (res) {
|
||||
ast_log(LOG_NOTICE, "Unable to start playtones\n");
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int handle_stopplaytones(struct ast_channel *chan, void *data)
|
||||
{
|
||||
ast_playtones_stop(chan);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = ast_unregister_application(playtones_app);
|
||||
res |= ast_unregister_application(stopplaytones_app);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = ast_register_application_xml(playtones_app, handle_playtones);
|
||||
res |= ast_register_application_xml(stopplaytones_app, handle_stopplaytones);
|
||||
|
||||
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playtones Application");
|
@ -1,91 +1,251 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2002, Pauline Middelink
|
||||
* Copyright (C) 2009, Digium, Inc.
|
||||
*
|
||||
* 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 BSD Telephony Of Mexico "Tormenta" Tone Zone Support 2/22/01
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*
|
||||
* Primary Author: Pauline Middelink <middelink@polyware.nl>
|
||||
/*!
|
||||
* \file
|
||||
* \brief Tone Indication Support
|
||||
*
|
||||
* \author Pauline Middelink <middelink@polyware.nl>
|
||||
* \author Russell Bryant <russell@digium.com>
|
||||
*/
|
||||
|
||||
#ifndef _ASTERISK_INDICATIONS_H
|
||||
#define _ASTERISK_INDICATIONS_H
|
||||
|
||||
#include "asterisk/lock.h"
|
||||
|
||||
/*! \brief Description is a series of tones of the format:
|
||||
[!]freq1[+freq2][/duration] separated by commas. There
|
||||
are no spaces. The sequence is repeated back to the
|
||||
first tone description not preceeded by !. Duration is
|
||||
specified in milliseconds */
|
||||
struct tone_zone_sound {
|
||||
const char *name; /*!< Identifing name */
|
||||
const char *data; /*!< Actual zone description */
|
||||
AST_LIST_ENTRY(tone_zone_sound) list;
|
||||
#include "asterisk/astobj2.h"
|
||||
|
||||
/*!
|
||||
* \brief Description of a tone
|
||||
*/
|
||||
struct ast_tone_zone_sound {
|
||||
/*! \brief Name of the tone. For example, "busy". */
|
||||
const char *name;
|
||||
/*!
|
||||
* \brief Description of a tone
|
||||
*
|
||||
* The format is a comma separated list of tone parts in the following format:
|
||||
*
|
||||
* Format: [!][M]freq[<+|*>freq2][/duration]
|
||||
* - '!' - means that the element is NOT repeated
|
||||
* - 'M' - interpret the frequencies as midi notes instead of frequencies
|
||||
* - freq - The first frequency
|
||||
* - freq2 - The second frequency (optional)
|
||||
* - '*' - modulate freq by freq2 at a fixed depth of 90%
|
||||
* - '+' - combine the frequencies
|
||||
* - duration - the length of the tone part (optional, forever if not specified)
|
||||
*/
|
||||
const char *data;
|
||||
/*! \brief Linked list fields for including in the list on an ast_tone_zone */
|
||||
AST_LIST_ENTRY(ast_tone_zone_sound) entry;
|
||||
/*! \brief Flags only used internally */
|
||||
union {
|
||||
uint32_t __padding;
|
||||
struct {
|
||||
unsigned int killme:1;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief A set of tones for a given locale
|
||||
*
|
||||
* \note If a reference to this tone zone is held, then the country
|
||||
* is guaranteed not to change. It is safe to read it without
|
||||
* locking the tone zone. This is not the case for any other
|
||||
* field.
|
||||
*/
|
||||
struct ast_tone_zone {
|
||||
/*! \brief Country code that this set of tones is for */
|
||||
char country[5];
|
||||
/*!
|
||||
* \brief Text description of the given country.
|
||||
*
|
||||
* This is for nothing more than friendly display to a human.
|
||||
*/
|
||||
char description[40];
|
||||
/*! \brief Number of ring cadence elements in the ringcadence array */
|
||||
unsigned int nrringcadence;
|
||||
/*!
|
||||
* \brief Array of ring cadence parts
|
||||
*
|
||||
* Each element is an amount of time in milliseconds. The first element
|
||||
* is for time on, and from there it alternates between on and off.
|
||||
*/
|
||||
int *ringcadence;
|
||||
/*! \brief A list of tones for this locale */
|
||||
AST_LIST_HEAD_NOLOCK(, ast_tone_zone_sound) tones;
|
||||
/*! \brief Flags only used internally */
|
||||
union {
|
||||
uint32_t __padding;
|
||||
struct {
|
||||
unsigned int killme:1;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
struct tone_zone {
|
||||
AST_RWLIST_ENTRY(tone_zone) list;
|
||||
char country[5]; /*!< Country code */
|
||||
char alias[5]; /*!< is this an alias? */
|
||||
char description[40]; /*!< Description */
|
||||
int nrringcadence; /*!< # registered ringcadence elements */
|
||||
int *ringcadence; /*!< Ring cadence */
|
||||
AST_LIST_HEAD_NOLOCK(, tone_zone_sound) tones; /*!< The known tones for this zone */
|
||||
/*!
|
||||
* \brief A description of a part of a tone
|
||||
*
|
||||
* The elements in this structure map to the format described for the data
|
||||
* part of the ast_tone_zone_sound struct.
|
||||
*/
|
||||
struct ast_tone_zone_part {
|
||||
unsigned int freq1;
|
||||
unsigned int freq2;
|
||||
unsigned int time;
|
||||
unsigned int modulate:1;
|
||||
unsigned int midinote:1;
|
||||
};
|
||||
|
||||
/*! \brief set the default tone country */
|
||||
int ast_set_indication_country(const char *country);
|
||||
|
||||
/*! \brief locate tone_zone, given the country. if country == NULL, use the default country */
|
||||
struct tone_zone *ast_get_indication_zone(const char *country);
|
||||
/*! \brief locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
|
||||
struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication);
|
||||
/*! \brief deallocate the passed tone zone */
|
||||
void ast_destroy_indication_zone(struct tone_zone *zone);
|
||||
|
||||
/*! \brief add a new country, if country exists, it will be replaced. */
|
||||
int ast_register_indication_country(struct tone_zone *country);
|
||||
/*! \brief remove an existing country and all its indications, country must exist */
|
||||
int ast_unregister_indication_country(const char *country);
|
||||
/*! \brief add a new indication to a tone_zone. tone_zone must exist. if the indication already
|
||||
* exists, it will be replaced. */
|
||||
int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist);
|
||||
/*! \brief remove an existing tone_zone's indication. tone_zone must exist */
|
||||
int ast_unregister_indication(struct tone_zone *zone, const char *indication);
|
||||
|
||||
/*! \brief Start a tone-list going */
|
||||
int ast_playtones_start(struct ast_channel *chan, int vol, const char* tonelist, int interruptible);
|
||||
/*! \brief Stop the tones from playing */
|
||||
/*!
|
||||
* \brief Parse a tone part
|
||||
*
|
||||
* \param s The part of a tone to parse. This should be in the form described for
|
||||
* the data part of ast_tone_zone_sound. '!' should be removed if present.
|
||||
* \param tone_data An output parameter that contains the result of the parsing.
|
||||
*
|
||||
* \retval 0 success
|
||||
* \retval -1 failure, and the contents of tone_data are undefined
|
||||
*/
|
||||
int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data);
|
||||
|
||||
/*!
|
||||
* \brief locate ast_tone_zone
|
||||
*
|
||||
* \param country country to find. If NULL is provided, get the default.
|
||||
*
|
||||
* \return a reference to the specified country if found or NULL if not found
|
||||
*/
|
||||
struct ast_tone_zone *ast_get_indication_zone(const char *country);
|
||||
|
||||
/*!
|
||||
* \brief Locate a tone zone sound
|
||||
*
|
||||
* \param zone Zone to look in for a sound, if NULL, the default will be used
|
||||
* \param indication Sound to look for, such as "busy"
|
||||
*
|
||||
* \return a reference to the specified sound if it exists, NULL if not
|
||||
*/
|
||||
struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *zone, const char *indication);
|
||||
|
||||
/*!
|
||||
* \brief Start playing a list of tones on a channel
|
||||
*
|
||||
* \param chan the channel to play tones on
|
||||
* \param vol volume
|
||||
* \param tonelist the list of tones to play, comma separated
|
||||
* \param interruptible whether or not this tone can be interrupted
|
||||
*
|
||||
* \retval 0 success
|
||||
* \retval non-zero failure
|
||||
*/
|
||||
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible);
|
||||
|
||||
/*!
|
||||
* \brief Stop playing tones on a channel
|
||||
*
|
||||
* \param chan the channel to stop tones on
|
||||
*/
|
||||
void ast_playtones_stop(struct ast_channel *chan);
|
||||
|
||||
/*! \brief support for walking through a list of indications */
|
||||
struct tone_zone *ast_walk_indications(const struct tone_zone *cur);
|
||||
/*!
|
||||
* \brief Get the number of registered tone zones
|
||||
*
|
||||
* \return the total number of registered tone zones
|
||||
*/
|
||||
int ast_tone_zone_count(void);
|
||||
|
||||
/*!
|
||||
* \brief Get an iterator for the available tone zones
|
||||
*
|
||||
* Use ao2_iterator_next() to iterate the tone zones.
|
||||
*
|
||||
* \return an initialized iterator
|
||||
*/
|
||||
struct ao2_iterator ast_tone_zone_iterator_init(void);
|
||||
|
||||
extern struct ast_tone_zone __fake_tone_zone;
|
||||
extern struct ast_tone_zone_sound __fake_tone_zone_sound;
|
||||
|
||||
#define AST_CHECK_TONE_ZONE(tz) do { \
|
||||
(void) ((tz) == (&__fake_tone_zone)); \
|
||||
} while (0)
|
||||
|
||||
#define AST_CHECK_TONE_ZONE_SOUND(ts) do { \
|
||||
(void) ((ts) == (&__fake_tone_zone_sound)); \
|
||||
} while (0)
|
||||
|
||||
/*!
|
||||
* \brief Lock an ast_tone_zone
|
||||
*/
|
||||
#define ast_tone_zone_lock(tz) ao2_lock(tz)
|
||||
|
||||
/*!
|
||||
* \brief Unlock an ast_tone_zone
|
||||
*/
|
||||
#define ast_tone_zone_unlock(tz) ao2_unlock(tz)
|
||||
|
||||
#if 0
|
||||
extern struct tone_zone *tone_zones;
|
||||
extern ast_mutex_t tzlock;
|
||||
#endif
|
||||
/*!
|
||||
* \brief Trylock an ast_tone_zone
|
||||
*/
|
||||
#define ast_tone_zone_trylock(tz) ao2_trylock(tz)
|
||||
|
||||
/*!
|
||||
* \brief Release a reference to an ast_tone_zone
|
||||
*
|
||||
* \return NULL
|
||||
*/
|
||||
#define ast_tone_zone_unref(tz) ({ \
|
||||
AST_CHECK_TONE_ZONE(tz); \
|
||||
ao2_ref(tz, -1); \
|
||||
(NULL); \
|
||||
})
|
||||
|
||||
/*!
|
||||
* \brief Increase the reference count on an ast_tone_zone
|
||||
*
|
||||
* \return The tone zone provided as an argument
|
||||
*/
|
||||
#define ast_tone_zone_ref(tz) ({ \
|
||||
AST_CHECK_TONE_ZONE(tz); \
|
||||
ao2_ref(tz, +1); \
|
||||
(tz); \
|
||||
})
|
||||
|
||||
/*!
|
||||
* \brief Release a reference to an ast_tone_zone_sound
|
||||
*
|
||||
* \return NULL
|
||||
*/
|
||||
#define ast_tone_zone_sound_unref(ts) ({ \
|
||||
AST_CHECK_TONE_ZONE_SOUND(ts); \
|
||||
ao2_ref(ts, -1); \
|
||||
(NULL); \
|
||||
})
|
||||
|
||||
/*!
|
||||
* \brief Increase the reference count on an ast_tone_zone_sound
|
||||
*
|
||||
* \return The tone zone sound provided as an argument
|
||||
*/
|
||||
#define ast_tone_zone_sound_ref(ts) ({ \
|
||||
AST_CHECK_TONE_ZONE_SOUND(ts); \
|
||||
ao2_ref(ts, +1); \
|
||||
(ts); \
|
||||
})
|
||||
|
||||
#endif /* _ASTERISK_INDICATIONS_H */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,452 +0,0 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2002, Pauline Middelink
|
||||
*
|
||||
*
|
||||
* 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 res_indications.c
|
||||
*
|
||||
* \brief Load the indications
|
||||
*
|
||||
* \author Pauline Middelink <middelink@polyware.nl>
|
||||
*
|
||||
* Load the country specific dialtones into the asterisk PBX.
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include <ctype.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "asterisk/lock.h"
|
||||
#include "asterisk/file.h"
|
||||
#include "asterisk/cli.h"
|
||||
#include "asterisk/config.h"
|
||||
#include "asterisk/channel.h"
|
||||
#include "asterisk/pbx.h"
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/translate.h"
|
||||
#include "asterisk/indications.h"
|
||||
#include "asterisk/utils.h"
|
||||
|
||||
/*** DOCUMENTATION
|
||||
<application name="PlayTones" language="en_US">
|
||||
<synopsis>
|
||||
Play a tone list.
|
||||
</synopsis>
|
||||
<syntax>
|
||||
<parameter name="arg" required="true">
|
||||
<para>Arg is either the tone name defined in the <filename>indications.conf</filename>
|
||||
configuration file, or a directly specified list of frequencies and durations.</para>
|
||||
</parameter>
|
||||
</syntax>
|
||||
<description>
|
||||
<para>Plays a tone list. Execution will continue with the next step immediately,
|
||||
while the tones continue to play.</para>
|
||||
<para>See the sample <filename>indications.conf</filename> for a description of the
|
||||
specification of a tonelist.</para>
|
||||
</description>
|
||||
<see-also>
|
||||
<ref type="application">StopPlayTones</ref>
|
||||
</see-also>
|
||||
</application>
|
||||
<application name="StopPlayTones" language="en_US">
|
||||
<synopsis>
|
||||
Stop playing a tone list.
|
||||
</synopsis>
|
||||
<syntax />
|
||||
<description>
|
||||
<para>Stop playing a tone list, initiated by PlayTones().</para>
|
||||
</description>
|
||||
<see-also>
|
||||
<ref type="application">PlayTones</ref>
|
||||
</see-also>
|
||||
</application>
|
||||
***/
|
||||
|
||||
/* Globals */
|
||||
static const char config[] = "indications.conf";
|
||||
|
||||
/*
|
||||
* Implementation of functions provided by this module
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Add a country to indication
|
||||
* \param e the ast_cli_entry for this CLI command
|
||||
* \param cmd the reason we are being called
|
||||
* \param a the arguments being passed to us
|
||||
*/
|
||||
static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||
{
|
||||
struct tone_zone *tz;
|
||||
int created_country = 0;
|
||||
|
||||
switch (cmd) {
|
||||
case CLI_INIT:
|
||||
e->command = "indication add";
|
||||
e->usage =
|
||||
"Usage: indication add <country> <indication> \"<tonelist>\"\n"
|
||||
" Add the given indication to the country.\n";
|
||||
return NULL;
|
||||
case CLI_GENERATE:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (a->argc != 5)
|
||||
return CLI_SHOWUSAGE;
|
||||
|
||||
tz = ast_get_indication_zone(a->argv[2]);
|
||||
if (!tz) {
|
||||
/* country does not exist, create it */
|
||||
ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
|
||||
|
||||
if (!(tz = ast_calloc(1, sizeof(*tz)))) {
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
|
||||
if (ast_register_indication_country(tz)) {
|
||||
ast_log(LOG_WARNING, "Unable to register new country\n");
|
||||
ast_free(tz);
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
created_country = 1;
|
||||
}
|
||||
if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
|
||||
ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
|
||||
if (created_country)
|
||||
ast_unregister_indication_country(a->argv[2]);
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Remove a country from indication
|
||||
* \param e the ast_cli_entry for this CLI command
|
||||
* \param cmd the reason we are being called
|
||||
* \param a the arguments being passed to us
|
||||
*/
|
||||
static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||
{
|
||||
struct tone_zone *tz;
|
||||
|
||||
switch (cmd) {
|
||||
case CLI_INIT:
|
||||
e->command = "indication remove";
|
||||
e->usage =
|
||||
"Usage: indication remove <country> <indication>\n"
|
||||
" Remove the given indication from the country.\n";
|
||||
return NULL;
|
||||
case CLI_GENERATE:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (a->argc != 3 && a->argc != 4)
|
||||
return CLI_SHOWUSAGE;
|
||||
|
||||
if (a->argc == 3) {
|
||||
/* remove entiry country */
|
||||
if (ast_unregister_indication_country(a->argv[2])) {
|
||||
ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
tz = ast_get_indication_zone(a->argv[2]);
|
||||
if (!tz) {
|
||||
ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
if (ast_unregister_indication(tz, a->argv[3])) {
|
||||
ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Show the current indications
|
||||
* \param e the ast_cli_entry for this CLI command
|
||||
* \param cmd the reason we are being called
|
||||
* \param a the arguments being passed to us
|
||||
*/
|
||||
static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||
{
|
||||
struct tone_zone *tz = NULL;
|
||||
char buf[256];
|
||||
int found_country = 0;
|
||||
|
||||
switch (cmd) {
|
||||
case CLI_INIT:
|
||||
e->command = "indication show";
|
||||
e->usage =
|
||||
"Usage: indication show [<country> ...]\n"
|
||||
" Display either a condensed for of all country/indications, or the\n"
|
||||
" indications for the specified countries.\n";
|
||||
return NULL;
|
||||
case CLI_GENERATE:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (a->argc == 2) {
|
||||
/* no arguments, show a list of countries */
|
||||
ast_cli(a->fd, "Country Alias Description\n");
|
||||
ast_cli(a->fd, "===========================\n");
|
||||
while ((tz = ast_walk_indications(tz)))
|
||||
ast_cli(a->fd, "%-7.7s %-7.7s %s\n", tz->country, tz->alias, tz->description);
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
/* there was a request for specific country(ies), lets humor them */
|
||||
while ((tz = ast_walk_indications(tz))) {
|
||||
int i, j;
|
||||
for (i = 2; i < a->argc; i++) {
|
||||
if (strcasecmp(tz->country, a->argv[i]) == 0 && !tz->alias[0]) {
|
||||
struct tone_zone_sound *ts;
|
||||
if (!found_country) {
|
||||
found_country = 1;
|
||||
ast_cli(a->fd, "Country Indication PlayList\n");
|
||||
ast_cli(a->fd, "=====================================\n");
|
||||
}
|
||||
j = snprintf(buf, sizeof(buf), "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
|
||||
for (i = 0; i < tz->nrringcadence; i++) {
|
||||
j += snprintf(buf + j, sizeof(buf) - j, "%d,", tz->ringcadence[i]);
|
||||
}
|
||||
if (tz->nrringcadence)
|
||||
j--;
|
||||
ast_copy_string(buf + j, "\n", sizeof(buf) - j);
|
||||
ast_cli(a->fd, "%s", buf);
|
||||
AST_LIST_TRAVERSE(&tz->tones, ts, list) {
|
||||
ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found_country)
|
||||
ast_cli(a->fd, "No countries matched your criteria.\n");
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief play tone for indication country
|
||||
* \param chan ast_channel to play the sounds back to
|
||||
* \param data contains tone to play
|
||||
*/
|
||||
static int handle_playtones(struct ast_channel *chan, void *data)
|
||||
{
|
||||
struct tone_zone_sound *ts;
|
||||
int res;
|
||||
|
||||
if (!data || !((char*)data)[0]) {
|
||||
ast_log(LOG_NOTICE,"Nothing to play\n");
|
||||
return -1;
|
||||
}
|
||||
ts = ast_get_indication_tone(chan->zone, (const char*)data);
|
||||
if (ts && ts->data[0])
|
||||
res = ast_playtones_start(chan, 0, ts->data, 0);
|
||||
else
|
||||
res = ast_playtones_start(chan, 0, (const char*)data, 0);
|
||||
if (res)
|
||||
ast_log(LOG_NOTICE,"Unable to start playtones\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stop tones playing
|
||||
* \param chan
|
||||
* \param data
|
||||
*/
|
||||
static int handle_stopplaytones(struct ast_channel *chan, void *data)
|
||||
{
|
||||
ast_playtones_stop(chan);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief load indications module */
|
||||
static int ind_load_module(int reload)
|
||||
{
|
||||
struct ast_config *cfg;
|
||||
struct ast_variable *v;
|
||||
char *cxt;
|
||||
char *c;
|
||||
struct tone_zone *tones;
|
||||
const char *country = NULL;
|
||||
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
|
||||
|
||||
/* that the following cast is needed, is yuk! */
|
||||
/* yup, checked it out. It is NOT written to. */
|
||||
cfg = ast_config_load((char *)config, config_flags);
|
||||
if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
|
||||
return -1;
|
||||
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (reload)
|
||||
ast_unregister_indication_country(NULL);
|
||||
|
||||
/* Use existing config to populate the Indication table */
|
||||
cxt = ast_category_browse(cfg, NULL);
|
||||
while(cxt) {
|
||||
/* All categories but "general" are considered countries */
|
||||
if (!strcasecmp(cxt, "general")) {
|
||||
cxt = ast_category_browse(cfg, cxt);
|
||||
continue;
|
||||
}
|
||||
if (!(tones = ast_calloc(1, sizeof(*tones)))) {
|
||||
ast_config_destroy(cfg);
|
||||
return -1;
|
||||
}
|
||||
ast_copy_string(tones->country,cxt,sizeof(tones->country));
|
||||
|
||||
v = ast_variable_browse(cfg, cxt);
|
||||
while(v) {
|
||||
if (!strcasecmp(v->name, "description")) {
|
||||
ast_copy_string(tones->description, v->value, sizeof(tones->description));
|
||||
} else if ((!strcasecmp(v->name,"ringcadence"))||(!strcasecmp(v->name,"ringcadance"))) {
|
||||
char *ring,*rings = ast_strdupa(v->value);
|
||||
c = rings;
|
||||
ring = strsep(&c,",");
|
||||
while (ring) {
|
||||
int *tmp, val;
|
||||
if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
|
||||
ast_log(LOG_WARNING,"Invalid ringcadence given '%s' at line %d.\n",ring,v->lineno);
|
||||
ring = strsep(&c,",");
|
||||
continue;
|
||||
}
|
||||
if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) {
|
||||
ast_config_destroy(cfg);
|
||||
ast_destroy_indication_zone(tones);
|
||||
return -1;
|
||||
}
|
||||
tones->ringcadence = tmp;
|
||||
tmp[tones->nrringcadence] = val;
|
||||
tones->nrringcadence++;
|
||||
/* next item */
|
||||
ring = strsep(&c,",");
|
||||
}
|
||||
} else if (!strcasecmp(v->name,"alias")) {
|
||||
char *countries = ast_strdupa(v->value);
|
||||
c = countries;
|
||||
country = strsep(&c,",");
|
||||
while (country) {
|
||||
struct tone_zone* azone;
|
||||
if (!(azone = ast_calloc(1, sizeof(*azone)))) {
|
||||
ast_config_destroy(cfg);
|
||||
ast_destroy_indication_zone(tones);
|
||||
return -1;
|
||||
}
|
||||
ast_copy_string(azone->country, country, sizeof(azone->country));
|
||||
ast_copy_string(azone->alias, cxt, sizeof(azone->alias));
|
||||
if (ast_register_indication_country(azone)) {
|
||||
ast_log(LOG_WARNING, "Unable to register indication alias at line %d.\n",v->lineno);
|
||||
ast_destroy_indication_zone(tones);
|
||||
}
|
||||
/* next item */
|
||||
country = strsep(&c,",");
|
||||
}
|
||||
} else {
|
||||
struct tone_zone_sound *ts;
|
||||
|
||||
/* add tone to country */
|
||||
AST_LIST_TRAVERSE(&tones->tones, ts, list) {
|
||||
if (!strcasecmp(v->name, ts->name)) {
|
||||
/* already there */
|
||||
ast_log(LOG_NOTICE, "Duplicate entry '%s' skipped.\n", v->name);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
/* not there, add it to the back */
|
||||
if (!(ts = ast_calloc(1, sizeof(*ts)))) {
|
||||
ast_config_destroy(cfg);
|
||||
return -1;
|
||||
}
|
||||
ts->name = ast_strdup(v->name);
|
||||
ts->data = ast_strdup(v->value);
|
||||
|
||||
AST_LIST_INSERT_TAIL(&tones->tones, ts, list);
|
||||
}
|
||||
out: v = v->next;
|
||||
}
|
||||
if (tones->description[0] || tones->alias[0] || !AST_LIST_EMPTY(&tones->tones)) {
|
||||
if (ast_register_indication_country(tones)) {
|
||||
ast_log(LOG_WARNING, "Unable to register indication at line %d.\n",v->lineno);
|
||||
ast_destroy_indication_zone(tones);
|
||||
}
|
||||
} else {
|
||||
ast_destroy_indication_zone(tones);
|
||||
}
|
||||
|
||||
cxt = ast_category_browse(cfg, cxt);
|
||||
}
|
||||
|
||||
/* determine which country is the default */
|
||||
country = ast_variable_retrieve(cfg,"general","country");
|
||||
if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
|
||||
ast_log(LOG_WARNING,"Unable to set the default country (for indication tones)\n");
|
||||
}
|
||||
|
||||
ast_config_destroy(cfg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief CLI entries for commands provided by this module */
|
||||
static struct ast_cli_entry cli_indications[] = {
|
||||
AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
|
||||
AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
|
||||
AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
|
||||
};
|
||||
|
||||
/*! \brief Unload indicators module */
|
||||
static int unload_module(void)
|
||||
{
|
||||
/* remove the registed indications... */
|
||||
ast_unregister_indication_country(NULL);
|
||||
|
||||
/* and the functions */
|
||||
ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
|
||||
ast_unregister_application("PlayTones");
|
||||
ast_unregister_application("StopPlayTones");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*! \brief Load indications module */
|
||||
static int load_module(void)
|
||||
{
|
||||
if (ind_load_module(0))
|
||||
return AST_MODULE_LOAD_DECLINE;
|
||||
ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
|
||||
ast_register_application_xml("PlayTones", handle_playtones);
|
||||
ast_register_application_xml("StopPlayTones", handle_stopplaytones);
|
||||
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
/*! \brief Reload indications module */
|
||||
static int reload(void)
|
||||
{
|
||||
return ind_load_module(1);
|
||||
}
|
||||
|
||||
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Region-specific tones",
|
||||
.load = load_module,
|
||||
.unload = unload_module,
|
||||
.reload = reload,
|
||||
);
|
Loading…
Reference in new issue