2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2002, Pauline Middelink
5 * Copyright (C) 2009, Digium, Inc.
7 * See http://www.asterisk.org for more information about
8 * the Asterisk project. Please do not directly contact
9 * any of the maintainers of this project for assistance;
10 * the project provides a web site, mailing lists and IRC
11 * channels for your use.
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License Version 2. See the LICENSE file
15 * at the top of the source tree.
20 * \brief Indication Tone Handling
22 * \author Pauline Middelink <middelink@polyware.nl>
23 * \author Russell Bryant <russell@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/lock.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/indications.h"
39 #include "asterisk/frame.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/cli.h"
43 #include "asterisk/module.h"
44 #include "asterisk/astobj2.h"
45 #include "asterisk/data.h"
47 #include "asterisk/_private.h" /* _init(), _reload() */
49 #define DATA_EXPORT_TONE_ZONE(MEMBER) \
50 MEMBER(ast_tone_zone, country, AST_DATA_STRING) \
51 MEMBER(ast_tone_zone, description, AST_DATA_STRING) \
52 MEMBER(ast_tone_zone, nrringcadence, AST_DATA_UNSIGNED_INTEGER)
54 AST_DATA_STRUCTURE(ast_tone_zone, DATA_EXPORT_TONE_ZONE);
56 #define DATA_EXPORT_TONE_ZONE_SOUND(MEMBER) \
57 MEMBER(ast_tone_zone_sound, name, AST_DATA_STRING) \
58 MEMBER(ast_tone_zone_sound, data, AST_DATA_STRING)
60 AST_DATA_STRUCTURE(ast_tone_zone_sound, DATA_EXPORT_TONE_ZONE_SOUND);
63 static const char config[] = "indications.conf";
65 static const int midi_tohz[128] = {
66 8, 8, 9, 9, 10, 10, 11, 12, 12, 13,
67 14, 15, 16, 17, 18, 19, 20, 21, 23, 24,
68 25, 27, 29, 30, 32, 34, 36, 38, 41, 43,
69 46, 48, 51, 55, 58, 61, 65, 69, 73, 77,
70 82, 87, 92, 97, 103, 110, 116, 123, 130, 138,
71 146, 155, 164, 174, 184, 195, 207, 220, 233, 246,
72 261, 277, 293, 311, 329, 349, 369, 391, 415, 440,
73 466, 493, 523, 554, 587, 622, 659, 698, 739, 783,
74 830, 880, 932, 987, 1046, 1108, 1174, 1244, 1318, 1396,
75 1479, 1567, 1661, 1760, 1864, 1975, 2093, 2217, 2349, 2489,
76 2637, 2793, 2959, 3135, 3322, 3520, 3729, 3951, 4186, 4434,
77 4698, 4978, 5274, 5587, 5919, 6271, 6644, 7040, 7458, 7902,
78 8372, 8869, 9397, 9956, 10548, 11175, 11839, 12543
81 static struct ao2_container *ast_tone_zones;
83 #define NUM_TONE_ZONE_BUCKETS 53
86 * \note Access to this is protected by locking the ast_tone_zones container
88 static struct ast_tone_zone *default_tone_zone;
90 struct playtones_item {
101 struct playtones_def {
106 struct playtones_item *items;
109 struct playtones_state {
119 struct playtones_item *items;
123 struct ast_format origwfmt;
125 unsigned char offset[AST_FRIENDLY_OFFSET];
129 static void playtones_release(struct ast_channel *chan, void *params)
131 struct playtones_state *ps = params;
134 ast_set_write_format(chan, &ps->origwfmt);
145 static void *playtones_alloc(struct ast_channel *chan, void *params)
147 struct playtones_def *pd = params;
148 struct playtones_state *ps = NULL;
150 if (!(ps = ast_calloc(1, sizeof(*ps)))) {
154 ast_format_copy(&ps->origwfmt, ast_channel_writeformat(chan));
156 if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR)) {
157 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
158 playtones_release(NULL, ps);
162 ps->reppos = pd->reppos;
163 ps->nitems = pd->nitems;
164 ps->items = pd->items;
168 /* Let interrupts interrupt :) */
169 if (pd->interruptible) {
170 ast_set_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
172 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
178 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
180 struct playtones_state *ps = data;
181 struct playtones_item *pi;
184 /* we need to prepare a frame with 16 * timelen samples as we're
185 * generating SLIN audio */
188 if (len > sizeof(ps->data) / 2 - 1) {
189 ast_log(LOG_WARNING, "Can't generate that much data!\n");
193 memset(&ps->f, 0, sizeof(ps->f));
195 pi = &ps->items[ps->npos];
197 if (ps->oldnpos != ps->npos) {
198 /* Load new parameters */
200 ps->v2_1 = pi->init_v2_1;
201 ps->v3_1 = pi->init_v3_1;
203 ps->v2_2 = pi->init_v2_2;
204 ps->v3_2 = pi->init_v3_2;
205 ps->oldnpos = ps->npos;
208 for (x = 0; x < samples; x++) {
211 ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
215 ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
218 p = ps->v3_2 - 32768;
222 p = ((p * 9) / 10) + 1;
223 ps->data[x] = (ps->v3_1 * p) >> 15;
225 ps->data[x] = ps->v3_1 + ps->v3_2;
229 ps->f.frametype = AST_FRAME_VOICE;
230 ast_format_set(&ps->f.subclass.format, AST_FORMAT_SLINEAR, 0);
232 ps->f.samples = samples;
233 ps->f.offset = AST_FRIENDLY_OFFSET;
234 ps->f.data.ptr = ps->data;
236 if (ast_write(chan, &ps->f)) {
242 if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
243 ps->pos = 0; /* start new item */
245 if (ps->npos >= ps->nitems) { /* last item? */
246 if (ps->reppos == -1) { /* repeat set? */
249 ps->npos = ps->reppos; /* redo from top */
256 static struct ast_generator playtones = {
257 .alloc = playtones_alloc,
258 .release = playtones_release,
259 .generate = playtones_generator,
262 int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data)
264 if (sscanf(s, "%30u+%30u/%30u", &tone_data->freq1, &tone_data->freq2,
265 &tone_data->time) == 3) {
266 /* f1+f2/time format */
267 } else if (sscanf(s, "%30u+%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
270 } else if (sscanf(s, "%30u*%30u/%30u", &tone_data->freq1, &tone_data->freq2,
271 &tone_data->time) == 3) {
272 /* f1*f2/time format */
273 tone_data->modulate = 1;
274 } else if (sscanf(s, "%30u*%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
277 tone_data->modulate = 1;
278 } else if (sscanf(s, "%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
280 tone_data->freq2 = 0;
281 } else if (sscanf(s, "%30u", &tone_data->freq1) == 1) {
283 tone_data->freq2 = 0;
285 } else if (sscanf(s, "M%30u+M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
286 &tone_data->time) == 3) {
287 /* Mf1+Mf2/time format */
288 tone_data->midinote = 1;
289 } else if (sscanf(s, "M%30u+M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
292 tone_data->midinote = 1;
293 } else if (sscanf(s, "M%30u*M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
294 &tone_data->time) == 3) {
295 /* Mf1*Mf2/time format */
296 tone_data->modulate = 1;
297 tone_data->midinote = 1;
298 } else if (sscanf(s, "M%30u*M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
301 tone_data->modulate = 1;
302 tone_data->midinote = 1;
303 } else if (sscanf(s, "M%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
304 /* Mf1/time format */
305 tone_data->freq2 = -1;
306 tone_data->midinote = 1;
307 } else if (sscanf(s, "M%30u", &tone_data->freq1) == 1) {
309 tone_data->freq2 = -1;
311 tone_data->midinote = 1;
319 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
321 char *s, *data = ast_strdupa(playlst);
322 struct playtones_def d = { vol, -1, 0, 1, NULL };
325 static const float sample_rate = 8000.0;
326 static const float max_sample_val = 32768.0;
329 d.vol = 7219; /* Default to -8db */
332 d.interruptible = interruptible;
336 /* check if the data is separated with '|' or with ',' by default */
337 if (strchr(stringp,'|')) {
343 while ((s = strsep(&stringp, separator)) && !ast_strlen_zero(s)) {
344 struct playtones_item *new_items;
345 struct ast_tone_zone_part tone_data = {
352 } else if (d.reppos == -1) {
356 if (ast_tone_zone_part_parse(s, &tone_data)) {
357 ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
361 if (tone_data.midinote) {
362 /* midi notes must be between 0 and 127 */
364 if (tone_data.freq1 >= 0 && tone_data.freq1 <= 127) {
365 tone_data.freq1 = midi_tohz[tone_data.freq1];
370 if (tone_data.freq2 >= 0 && tone_data.freq2 <= 127) {
371 tone_data.freq2 = midi_tohz[tone_data.freq2];
377 new_items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items));
384 d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / sample_rate)) * max_sample_val;
385 d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
386 d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
388 d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / sample_rate)) * max_sample_val;
389 d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
390 d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
392 d.items[d.nitems].duration = tone_data.time;
393 d.items[d.nitems].modulate = tone_data.modulate;
399 ast_log(LOG_ERROR, "No valid tone parts\n");
403 if (ast_activate_generator(chan, &playtones, &d)) {
411 void ast_playtones_stop(struct ast_channel *chan)
413 ast_deactivate_generator(chan);
416 int ast_tone_zone_count(void)
418 return ao2_container_count(ast_tone_zones);
421 struct ao2_iterator ast_tone_zone_iterator_init(void)
423 return ao2_iterator_init(ast_tone_zones, 0);
426 /*! \brief Set global indication country
427 If no country is specified or we are unable to find the zone, then return not found */
428 static int ast_set_indication_country(const char *country)
430 struct ast_tone_zone *zone = NULL;
432 if (ast_strlen_zero(country) || !(zone = ast_get_indication_zone(country))) {
436 ast_verb(3, "Setting default indication country to '%s'\n", country);
438 ao2_lock(ast_tone_zones);
439 if (default_tone_zone) {
440 default_tone_zone = ast_tone_zone_unref(default_tone_zone);
442 default_tone_zone = ast_tone_zone_ref(zone);
443 ao2_unlock(ast_tone_zones);
445 zone = ast_tone_zone_unref(zone);
450 /*! \brief locate ast_tone_zone, given the country. if country == NULL, use the default country */
451 struct ast_tone_zone *ast_get_indication_zone(const char *country)
453 struct ast_tone_zone *tz = NULL;
454 struct ast_tone_zone zone_arg = {
458 if (ast_strlen_zero(country)) {
459 ao2_lock(ast_tone_zones);
460 if (default_tone_zone) {
461 tz = ast_tone_zone_ref(default_tone_zone);
463 ao2_unlock(ast_tone_zones);
468 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
470 return ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER);
473 struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *_zone, const char *indication)
475 struct ast_tone_zone_sound *ts = NULL;
476 /* _zone is const to the users of the API */
477 struct ast_tone_zone *zone = (struct ast_tone_zone *) _zone;
479 /* If no zone is specified, use the default */
481 ao2_lock(ast_tone_zones);
482 if (default_tone_zone) {
483 zone = ast_tone_zone_ref(default_tone_zone);
485 ao2_unlock(ast_tone_zones);
492 ast_tone_zone_lock(zone);
494 /* Look through list of tones in the zone searching for the right one */
495 AST_LIST_TRAVERSE(&zone->tones, ts, entry) {
496 if (!strcasecmp(ts->name, indication)) {
497 /* Increase ref count for the reference we will return */
498 ts = ast_tone_zone_sound_ref(ts);
503 ast_tone_zone_unlock(zone);
506 zone = ast_tone_zone_unref(zone);
511 static void ast_tone_zone_sound_destructor(void *obj)
513 struct ast_tone_zone_sound *ts = obj;
515 /* Deconstify the 'const char *'s so the compiler doesn't complain. (but it's safe) */
517 ast_free((char *) ts->name);
522 ast_free((char *) ts->data);
527 /*! \brief deallocate the passed tone zone */
528 static void ast_tone_zone_destructor(void *obj)
530 struct ast_tone_zone *zone = obj;
531 struct ast_tone_zone_sound *current;
533 while ((current = AST_LIST_REMOVE_HEAD(&zone->tones, entry))) {
534 current = ast_tone_zone_sound_unref(current);
537 if (zone->ringcadence) {
538 ast_free(zone->ringcadence);
539 zone->ringcadence = NULL;
543 /*! \brief add a new country, if country exists, it will be replaced. */
544 static int ast_register_indication_country(struct ast_tone_zone *zone)
546 ao2_lock(ast_tone_zones);
547 if (!default_tone_zone) {
548 default_tone_zone = ast_tone_zone_ref(zone);
550 ao2_unlock(ast_tone_zones);
552 ao2_link(ast_tone_zones, zone);
554 ast_verb(3, "Registered indication country '%s'\n", zone->country);
559 /*! \brief remove an existing country and all its indications, country must exist. */
560 static int ast_unregister_indication_country(const char *country)
562 struct ast_tone_zone *tz = NULL;
563 struct ast_tone_zone zone_arg = {
567 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
569 ao2_lock(ast_tone_zones);
570 tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER | OBJ_UNLINK);
572 ao2_unlock(ast_tone_zones);
576 if (default_tone_zone == tz) {
577 ast_tone_zone_unref(default_tone_zone);
578 /* Get a new default, punt to the first one we find */
579 default_tone_zone = ao2_callback(ast_tone_zones, 0, NULL, NULL);
581 ao2_unlock(ast_tone_zones);
583 tz = ast_tone_zone_unref(tz);
589 * \note called with the tone zone locked
591 static int ast_register_indication(struct ast_tone_zone *zone, const char *indication,
592 const char *tonelist)
594 struct ast_tone_zone_sound *ts;
596 if (ast_strlen_zero(indication) || ast_strlen_zero(tonelist)) {
600 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
601 if (!strcasecmp(indication, ts->name)) {
602 AST_LIST_REMOVE_CURRENT(entry);
603 ts = ast_tone_zone_sound_unref(ts);
607 AST_LIST_TRAVERSE_SAFE_END;
609 if (!(ts = ao2_alloc(sizeof(*ts), ast_tone_zone_sound_destructor))) {
613 if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
614 ts = ast_tone_zone_sound_unref(ts);
618 AST_LIST_INSERT_TAIL(&zone->tones, ts, entry); /* Inherit reference */
623 /*! \brief remove an existing country's indication. Both country and indication must exist */
624 static int ast_unregister_indication(struct ast_tone_zone *zone, const char *indication)
626 struct ast_tone_zone_sound *ts;
629 ast_tone_zone_lock(zone);
631 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
632 if (!strcasecmp(indication, ts->name)) {
633 AST_LIST_REMOVE_CURRENT(entry);
634 ts = ast_tone_zone_sound_unref(ts);
639 AST_LIST_TRAVERSE_SAFE_END;
641 ast_tone_zone_unlock(zone);
646 static struct ast_tone_zone *ast_tone_zone_alloc(void)
648 return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
651 static char *complete_country(struct ast_cli_args *a)
654 struct ao2_iterator i;
657 struct ast_tone_zone *tz;
659 wordlen = strlen(a->word);
661 i = ao2_iterator_init(ast_tone_zones, 0);
662 while ((tz = ao2_iterator_next(&i))) {
663 if (!strncasecmp(a->word, tz->country, wordlen) && ++which > a->n) {
664 res = ast_strdup(tz->country);
666 tz = ast_tone_zone_unref(tz);
671 ao2_iterator_destroy(&i);
676 static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
678 struct ast_tone_zone *tz;
679 int created_country = 0;
680 char *res = CLI_SUCCESS;
684 e->command = "indication add";
686 "Usage: indication add <country> <indication> \"<tonelist>\"\n"
687 " Add the given indication to the country.\n";
691 return complete_country(a);
698 return CLI_SHOWUSAGE;
701 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
702 /* country does not exist, create it */
703 ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
705 if (!(tz = ast_tone_zone_alloc())) {
709 ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
711 if (ast_register_indication_country(tz)) {
712 ast_log(LOG_WARNING, "Unable to register new country\n");
713 tz = ast_tone_zone_unref(tz);
720 ast_tone_zone_lock(tz);
722 if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
723 ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
724 if (created_country) {
725 ast_unregister_indication_country(a->argv[2]);
730 ast_tone_zone_unlock(tz);
732 tz = ast_tone_zone_unref(tz);
737 static char *complete_indications(struct ast_cli_args *a)
742 struct ast_tone_zone_sound *ts;
743 struct ast_tone_zone *tz, tmp_tz = {
747 ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
749 if (!(tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER))) {
753 wordlen = strlen(a->word);
755 ast_tone_zone_lock(tz);
756 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
757 if (!strncasecmp(a->word, ts->name, wordlen) && ++which > a->n) {
758 res = ast_strdup(ts->name);
762 ast_tone_zone_unlock(tz);
764 tz = ast_tone_zone_unref(tz);
769 static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
771 struct ast_tone_zone *tz;
772 char *res = CLI_SUCCESS;
776 e->command = "indication remove";
778 "Usage: indication remove <country> [indication]\n"
779 " Remove the given indication from the country.\n";
783 return complete_country(a);
784 } else if (a->pos == 3) {
785 return complete_indications(a);
789 if (a->argc != 3 && a->argc != 4) {
790 return CLI_SHOWUSAGE;
794 /* remove entire country */
795 if (ast_unregister_indication_country(a->argv[2])) {
796 ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
803 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
804 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
808 if (ast_unregister_indication(tz, a->argv[3])) {
809 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
813 tz = ast_tone_zone_unref(tz);
818 static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
820 struct ast_tone_zone *tz = NULL;
822 int found_country = 0;
827 e->command = "indication show";
829 "Usage: indication show [<country> ...]\n"
830 " Display either a condensed summary of all countries and indications, or a\n"
831 " more verbose list of indications for the specified countries.\n";
834 return complete_country(a);
838 struct ao2_iterator iter;
839 /* no arguments, show a list of countries */
840 ast_cli(a->fd, "Country Description\n");
841 ast_cli(a->fd, "===========================\n");
842 iter = ast_tone_zone_iterator_init();
843 while ((tz = ao2_iterator_next(&iter))) {
844 ast_tone_zone_lock(tz);
845 ast_cli(a->fd, "%-7.7s %s\n", tz->country, tz->description);
846 ast_tone_zone_unlock(tz);
847 tz = ast_tone_zone_unref(tz);
849 ao2_iterator_destroy(&iter);
853 buf = ast_str_alloca(256);
855 for (i = 2; i < a->argc; i++) {
856 struct ast_tone_zone zone_arg = {
859 struct ast_tone_zone_sound *ts;
862 ast_copy_string(zone_arg.country, a->argv[i], sizeof(zone_arg.country));
864 if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
868 if (!found_country) {
870 ast_cli(a->fd, "Country Indication PlayList\n");
871 ast_cli(a->fd, "=====================================\n");
874 ast_tone_zone_lock(tz);
876 ast_str_set(&buf, 0, "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
877 for (j = 0; j < tz->nrringcadence; j++) {
878 ast_str_append(&buf, 0, "%d%s", tz->ringcadence[j],
879 (j == tz->nrringcadence - 1) ? "" : ",");
881 ast_str_append(&buf, 0, "\n");
882 ast_cli(a->fd, "%s", ast_str_buffer(buf));
884 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
885 ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
888 ast_tone_zone_unlock(tz);
889 tz = ast_tone_zone_unref(tz);
892 if (!found_country) {
893 ast_cli(a->fd, "No countries matched your criteria.\n");
899 static int is_valid_tone_zone(struct ast_tone_zone *zone)
903 ast_tone_zone_lock(zone);
904 res = (!ast_strlen_zero(zone->description) && !AST_LIST_EMPTY(&zone->tones));
905 ast_tone_zone_unlock(zone);
912 * \note This is called with the tone zone locked.
914 static void store_tone_zone_ring_cadence(struct ast_tone_zone *zone, const char *val)
917 char *ring, *c = buf;
919 ast_copy_string(buf, val, sizeof(buf));
921 while ((ring = strsep(&c, ","))) {
924 ring = ast_strip(ring);
926 if (!isdigit(ring[0]) || (val = atoi(ring)) == -1) {
927 ast_log(LOG_WARNING, "Invalid ringcadence given '%s'.\n", ring);
931 if (!(tmp = ast_realloc(zone->ringcadence, (zone->nrringcadence + 1) * sizeof(int)))) {
935 zone->ringcadence = tmp;
936 tmp[zone->nrringcadence] = val;
937 zone->nrringcadence++;
941 static void store_config_tone_zone(struct ast_tone_zone *zone, const char *var,
944 CV_START(var, value);
946 CV_STR("description", zone->description);
947 CV_F("ringcadence", store_tone_zone_ring_cadence(zone, value));
949 ast_register_indication(zone, var, value);
954 static void reset_tone_zone(struct ast_tone_zone *zone)
956 ast_tone_zone_lock(zone);
960 if (zone->nrringcadence) {
961 zone->nrringcadence = 0;
962 ast_free(zone->ringcadence);
963 zone->ringcadence = NULL;
966 ast_tone_zone_unlock(zone);
969 static int parse_tone_zone(struct ast_config *cfg, const char *country)
971 struct ast_variable *v;
972 struct ast_tone_zone *zone;
973 struct ast_tone_zone tmp_zone = {
978 ast_copy_string(tmp_zone.country, country, sizeof(tmp_zone.country));
980 if ((zone = ao2_find(ast_tone_zones, &tmp_zone, OBJ_POINTER))) {
981 reset_tone_zone(zone);
982 } else if ((zone = ast_tone_zone_alloc())) {
984 ast_copy_string(zone->country, country, sizeof(zone->country));
989 ast_tone_zone_lock(zone);
990 for (v = ast_variable_browse(cfg, country); v; v = v->next) {
991 store_config_tone_zone(zone, v->name, v->value);
993 ast_tone_zone_unlock(zone);
996 if (!is_valid_tone_zone(zone)) {
997 ast_log(LOG_WARNING, "Indication country '%s' is invalid\n", country);
998 } else if (ast_register_indication_country(zone)) {
999 ast_log(LOG_WARNING, "Unable to register indication country '%s'.\n",
1004 zone = ast_tone_zone_unref(zone);
1010 * Mark the zone and its tones before parsing configuration. We will use this
1011 * to know what to remove after configuration is parsed.
1013 static int tone_zone_mark(void *obj, void *arg, int flags)
1015 struct ast_tone_zone *zone = obj;
1016 struct ast_tone_zone_sound *s;
1018 ast_tone_zone_lock(zone);
1022 AST_LIST_TRAVERSE(&zone->tones, s, entry) {
1026 ast_tone_zone_unlock(zone);
1032 * Prune tones no longer in the configuration, and have the tone zone unlinked
1033 * if it is no longer in the configuration at all.
1035 static int prune_tone_zone(void *obj, void *arg, int flags)
1037 struct ast_tone_zone *zone = obj;
1038 struct ast_tone_zone_sound *s;
1040 ast_tone_zone_lock(zone);
1042 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, s, entry) {
1044 AST_LIST_REMOVE_CURRENT(entry);
1045 s = ast_tone_zone_sound_unref(s);
1048 AST_LIST_TRAVERSE_SAFE_END;
1050 ast_tone_zone_unlock(zone);
1052 return zone->killme ? CMP_MATCH : 0;
1055 /*! \brief load indications module */
1056 static int load_indications(int reload)
1058 struct ast_config *cfg;
1059 const char *cxt = NULL;
1060 const char *country = NULL;
1061 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1064 cfg = ast_config_load2(config, "indications", config_flags);
1066 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
1067 ast_log(LOG_WARNING, "Can't find indications config file %s.\n", config);
1069 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
1073 /* Lock the container to prevent multiple simultaneous reloads */
1074 ao2_lock(ast_tone_zones);
1076 ao2_callback(ast_tone_zones, OBJ_NODATA, tone_zone_mark, NULL);
1078 /* Use existing config to populate the Indication table */
1079 while ((cxt = ast_category_browse(cfg, cxt))) {
1080 /* All categories but "general" are considered countries */
1081 if (!strcasecmp(cxt, "general")) {
1085 if (parse_tone_zone(cfg, cxt)) {
1086 goto return_cleanup;
1090 ao2_callback(ast_tone_zones, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK,
1091 prune_tone_zone, NULL);
1093 /* determine which country is the default */
1094 country = ast_variable_retrieve(cfg, "general", "country");
1095 if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
1096 ast_log(LOG_WARNING, "Unable to set the default country (for indication tones)\n");
1102 ao2_unlock(ast_tone_zones);
1103 ast_config_destroy(cfg);
1108 /*! \brief CLI entries for commands provided by this module */
1109 static struct ast_cli_entry cli_indications[] = {
1110 AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
1111 AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
1112 AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
1115 static int ast_tone_zone_hash(const void *obj, const int flags)
1117 const struct ast_tone_zone *zone = obj;
1119 return ast_str_case_hash(zone->country);
1122 static int ast_tone_zone_cmp(void *obj, void *arg, int flags)
1124 struct ast_tone_zone *zone = obj;
1125 struct ast_tone_zone *zone_arg = arg;
1127 return (!strcasecmp(zone->country, zone_arg->country)) ?
1128 CMP_MATCH | CMP_STOP : 0;
1131 int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone *zone)
1133 struct ast_data *data_zone_sound;
1134 struct ast_tone_zone_sound *s;
1136 ast_data_add_structure(ast_tone_zone, tree, zone);
1138 if (AST_LIST_EMPTY(&zone->tones)) {
1142 data_zone_sound = ast_data_add_node(tree, "tones");
1143 if (!data_zone_sound) {
1147 ast_tone_zone_lock(zone);
1149 AST_LIST_TRAVERSE(&zone->tones, s, entry) {
1150 ast_data_add_structure(ast_tone_zone_sound, data_zone_sound, s);
1153 ast_tone_zone_unlock(zone);
1160 * \brief Clean up resources on Asterisk shutdown
1162 static void indications_shutdown(void)
1164 ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
1165 if (default_tone_zone) {
1166 ast_tone_zone_unref(default_tone_zone);
1167 default_tone_zone = NULL;
1169 if (ast_tone_zones) {
1170 ao2_ref(ast_tone_zones, -1);
1171 ast_tone_zones = NULL;
1175 /*! \brief Load indications module */
1176 int ast_indications_init(void)
1178 if (!(ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
1179 ast_tone_zone_hash, ast_tone_zone_cmp))) {
1183 if (load_indications(0)) {
1184 indications_shutdown();
1188 ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
1190 ast_register_atexit(indications_shutdown);
1194 /*! \brief Reload indications module */
1195 int ast_indications_reload(void)
1197 return load_indications(1);