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 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 if (!(d.items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items)))) {
381 d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / sample_rate)) * max_sample_val;
382 d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
383 d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
385 d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / sample_rate)) * max_sample_val;
386 d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
387 d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
389 d.items[d.nitems].duration = tone_data.time;
390 d.items[d.nitems].modulate = tone_data.modulate;
396 ast_log(LOG_ERROR, "No valid tone parts\n");
400 if (ast_activate_generator(chan, &playtones, &d)) {
408 void ast_playtones_stop(struct ast_channel *chan)
410 ast_deactivate_generator(chan);
413 int ast_tone_zone_count(void)
415 return ao2_container_count(ast_tone_zones);
418 struct ao2_iterator ast_tone_zone_iterator_init(void)
420 return ao2_iterator_init(ast_tone_zones, 0);
423 /*! \brief Set global indication country
424 If no country is specified or we are unable to find the zone, then return not found */
425 static int ast_set_indication_country(const char *country)
427 struct ast_tone_zone *zone = NULL;
429 if (ast_strlen_zero(country) || !(zone = ast_get_indication_zone(country))) {
433 ast_verb(3, "Setting default indication country to '%s'\n", country);
435 ao2_lock(ast_tone_zones);
436 if (default_tone_zone) {
437 default_tone_zone = ast_tone_zone_unref(default_tone_zone);
439 default_tone_zone = ast_tone_zone_ref(zone);
440 ao2_unlock(ast_tone_zones);
442 zone = ast_tone_zone_unref(zone);
447 /*! \brief locate ast_tone_zone, given the country. if country == NULL, use the default country */
448 struct ast_tone_zone *ast_get_indication_zone(const char *country)
450 struct ast_tone_zone *tz = NULL;
451 struct ast_tone_zone zone_arg = {
455 if (ast_strlen_zero(country)) {
456 ao2_lock(ast_tone_zones);
457 if (default_tone_zone) {
458 tz = ast_tone_zone_ref(default_tone_zone);
460 ao2_unlock(ast_tone_zones);
465 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
467 return ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER);
470 struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *_zone, const char *indication)
472 struct ast_tone_zone_sound *ts = NULL;
473 /* _zone is const to the users of the API */
474 struct ast_tone_zone *zone = (struct ast_tone_zone *) _zone;
476 /* If no zone is specified, use the default */
478 ao2_lock(ast_tone_zones);
479 if (default_tone_zone) {
480 zone = ast_tone_zone_ref(default_tone_zone);
482 ao2_unlock(ast_tone_zones);
489 ast_tone_zone_lock(zone);
491 /* Look through list of tones in the zone searching for the right one */
492 AST_LIST_TRAVERSE(&zone->tones, ts, entry) {
493 if (!strcasecmp(ts->name, indication)) {
494 /* Increase ref count for the reference we will return */
495 ts = ast_tone_zone_sound_ref(ts);
500 ast_tone_zone_unlock(zone);
505 static void ast_tone_zone_sound_destructor(void *obj)
507 struct ast_tone_zone_sound *ts = obj;
509 /* Deconstify the 'const char *'s so the compiler doesn't complain. (but it's safe) */
511 ast_free((char *) ts->name);
516 ast_free((char *) ts->data);
521 /*! \brief deallocate the passed tone zone */
522 static void ast_tone_zone_destructor(void *obj)
524 struct ast_tone_zone *zone = obj;
525 struct ast_tone_zone_sound *current;
527 while ((current = AST_LIST_REMOVE_HEAD(&zone->tones, entry))) {
528 current = ast_tone_zone_sound_unref(current);
531 if (zone->ringcadence) {
532 ast_free(zone->ringcadence);
533 zone->ringcadence = NULL;
537 /*! \brief add a new country, if country exists, it will be replaced. */
538 static int ast_register_indication_country(struct ast_tone_zone *zone)
540 ao2_lock(ast_tone_zones);
541 if (!default_tone_zone) {
542 default_tone_zone = ast_tone_zone_ref(zone);
544 ao2_unlock(ast_tone_zones);
546 ao2_link(ast_tone_zones, zone);
548 ast_verb(3, "Registered indication country '%s'\n", zone->country);
553 /*! \brief remove an existing country and all its indications, country must exist. */
554 static int ast_unregister_indication_country(const char *country)
556 struct ast_tone_zone *tz = NULL;
557 struct ast_tone_zone zone_arg = {
561 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
563 ao2_lock(ast_tone_zones);
564 tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER | OBJ_UNLINK);
566 ao2_unlock(ast_tone_zones);
570 if (default_tone_zone == tz) {
571 ast_tone_zone_unref(default_tone_zone);
572 /* Get a new default, punt to the first one we find */
573 default_tone_zone = ao2_callback(ast_tone_zones, 0, NULL, NULL);
575 ao2_unlock(ast_tone_zones);
577 tz = ast_tone_zone_unref(tz);
583 * \note called with the tone zone locked
585 static int ast_register_indication(struct ast_tone_zone *zone, const char *indication,
586 const char *tonelist)
588 struct ast_tone_zone_sound *ts;
590 if (ast_strlen_zero(indication) || ast_strlen_zero(tonelist)) {
594 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
595 if (!strcasecmp(indication, ts->name)) {
596 AST_LIST_REMOVE_CURRENT(entry);
597 ts = ast_tone_zone_sound_unref(ts);
601 AST_LIST_TRAVERSE_SAFE_END;
603 if (!(ts = ao2_alloc(sizeof(*ts), ast_tone_zone_sound_destructor))) {
607 if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
608 ts = ast_tone_zone_sound_unref(ts);
612 AST_LIST_INSERT_TAIL(&zone->tones, ts, entry); /* Inherit reference */
617 /*! \brief remove an existing country's indication. Both country and indication must exist */
618 static int ast_unregister_indication(struct ast_tone_zone *zone, const char *indication)
620 struct ast_tone_zone_sound *ts;
623 ast_tone_zone_lock(zone);
625 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
626 if (!strcasecmp(indication, ts->name)) {
627 AST_LIST_REMOVE_CURRENT(entry);
628 ts = ast_tone_zone_sound_unref(ts);
633 AST_LIST_TRAVERSE_SAFE_END;
635 ast_tone_zone_unlock(zone);
640 static struct ast_tone_zone *ast_tone_zone_alloc(void)
642 return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
645 static char *complete_country(struct ast_cli_args *a)
648 struct ao2_iterator i;
651 struct ast_tone_zone *tz;
653 wordlen = strlen(a->word);
655 i = ao2_iterator_init(ast_tone_zones, 0);
656 while ((tz = ao2_iterator_next(&i))) {
657 if (!strncasecmp(a->word, tz->country, wordlen) && ++which > a->n) {
658 res = ast_strdup(tz->country);
660 tz = ast_tone_zone_unref(tz);
665 ao2_iterator_destroy(&i);
670 static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
672 struct ast_tone_zone *tz;
673 int created_country = 0;
674 char *res = CLI_SUCCESS;
678 e->command = "indication add";
680 "Usage: indication add <country> <indication> \"<tonelist>\"\n"
681 " Add the given indication to the country.\n";
685 return complete_country(a);
692 return CLI_SHOWUSAGE;
695 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
696 /* country does not exist, create it */
697 ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
699 if (!(tz = ast_tone_zone_alloc())) {
703 ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
705 if (ast_register_indication_country(tz)) {
706 ast_log(LOG_WARNING, "Unable to register new country\n");
707 tz = ast_tone_zone_unref(tz);
714 ast_tone_zone_lock(tz);
716 if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
717 ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
718 if (created_country) {
719 ast_unregister_indication_country(a->argv[2]);
724 ast_tone_zone_unlock(tz);
726 tz = ast_tone_zone_unref(tz);
731 static char *complete_indications(struct ast_cli_args *a)
736 struct ast_tone_zone_sound *ts;
737 struct ast_tone_zone *tz, tmp_tz = {
741 ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
743 if (!(tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER))) {
747 wordlen = strlen(a->word);
749 ast_tone_zone_lock(tz);
750 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
751 if (!strncasecmp(a->word, ts->name, wordlen) && ++which > a->n) {
752 res = ast_strdup(ts->name);
756 ast_tone_zone_unlock(tz);
758 tz = ast_tone_zone_unref(tz);
763 static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
765 struct ast_tone_zone *tz;
766 char *res = CLI_SUCCESS;
770 e->command = "indication remove";
772 "Usage: indication remove <country> [indication]\n"
773 " Remove the given indication from the country.\n";
777 return complete_country(a);
778 } else if (a->pos == 3) {
779 return complete_indications(a);
783 if (a->argc != 3 && a->argc != 4) {
784 return CLI_SHOWUSAGE;
788 /* remove entire country */
789 if (ast_unregister_indication_country(a->argv[2])) {
790 ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
797 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
798 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
802 if (ast_unregister_indication(tz, a->argv[3])) {
803 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
807 tz = ast_tone_zone_unref(tz);
812 static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
814 struct ast_tone_zone *tz = NULL;
816 int found_country = 0;
821 e->command = "indication show";
823 "Usage: indication show [<country> ...]\n"
824 " Display either a condensed summary of all countries and indications, or a\n"
825 " more verbose list of indications for the specified countries.\n";
828 return complete_country(a);
832 struct ao2_iterator iter;
833 /* no arguments, show a list of countries */
834 ast_cli(a->fd, "Country Description\n");
835 ast_cli(a->fd, "===========================\n");
836 iter = ast_tone_zone_iterator_init();
837 while ((tz = ao2_iterator_next(&iter))) {
838 ast_tone_zone_lock(tz);
839 ast_cli(a->fd, "%-7.7s %s\n", tz->country, tz->description);
840 ast_tone_zone_unlock(tz);
841 tz = ast_tone_zone_unref(tz);
843 ao2_iterator_destroy(&iter);
847 buf = ast_str_alloca(256);
849 for (i = 2; i < a->argc; i++) {
850 struct ast_tone_zone zone_arg = {
853 struct ast_tone_zone_sound *ts;
856 ast_copy_string(zone_arg.country, a->argv[i], sizeof(zone_arg.country));
858 if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
862 if (!found_country) {
864 ast_cli(a->fd, "Country Indication PlayList\n");
865 ast_cli(a->fd, "=====================================\n");
868 ast_tone_zone_lock(tz);
870 ast_str_set(&buf, 0, "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
871 for (j = 0; j < tz->nrringcadence; j++) {
872 ast_str_append(&buf, 0, "%d%s", tz->ringcadence[j],
873 (j == tz->nrringcadence - 1) ? "" : ",");
875 ast_str_append(&buf, 0, "\n");
876 ast_cli(a->fd, "%s", ast_str_buffer(buf));
878 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
879 ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
882 ast_tone_zone_unlock(tz);
883 tz = ast_tone_zone_unref(tz);
886 if (!found_country) {
887 ast_cli(a->fd, "No countries matched your criteria.\n");
893 static int is_valid_tone_zone(struct ast_tone_zone *zone)
897 ast_tone_zone_lock(zone);
898 res = (!ast_strlen_zero(zone->description) && !AST_LIST_EMPTY(&zone->tones));
899 ast_tone_zone_unlock(zone);
906 * \note This is called with the tone zone locked.
908 static void store_tone_zone_ring_cadence(struct ast_tone_zone *zone, const char *val)
911 char *ring, *c = buf;
913 ast_copy_string(buf, val, sizeof(buf));
915 while ((ring = strsep(&c, ","))) {
918 ring = ast_strip(ring);
920 if (!isdigit(ring[0]) || (val = atoi(ring)) == -1) {
921 ast_log(LOG_WARNING, "Invalid ringcadence given '%s'.\n", ring);
925 if (!(tmp = ast_realloc(zone->ringcadence, (zone->nrringcadence + 1) * sizeof(int)))) {
929 zone->ringcadence = tmp;
930 tmp[zone->nrringcadence] = val;
931 zone->nrringcadence++;
935 static void store_config_tone_zone(struct ast_tone_zone *zone, const char *var,
938 CV_START(var, value);
940 CV_STR("description", zone->description);
941 CV_F("ringcadence", store_tone_zone_ring_cadence(zone, value));
943 ast_register_indication(zone, var, value);
948 static void reset_tone_zone(struct ast_tone_zone *zone)
950 ast_tone_zone_lock(zone);
954 if (zone->nrringcadence) {
955 zone->nrringcadence = 0;
956 ast_free(zone->ringcadence);
957 zone->ringcadence = NULL;
960 ast_tone_zone_unlock(zone);
963 static int parse_tone_zone(struct ast_config *cfg, const char *country)
965 struct ast_variable *v;
966 struct ast_tone_zone *zone;
967 struct ast_tone_zone tmp_zone = {
972 ast_copy_string(tmp_zone.country, country, sizeof(tmp_zone.country));
974 if ((zone = ao2_find(ast_tone_zones, &tmp_zone, OBJ_POINTER))) {
975 reset_tone_zone(zone);
976 } else if ((zone = ast_tone_zone_alloc())) {
978 ast_copy_string(zone->country, country, sizeof(zone->country));
983 ast_tone_zone_lock(zone);
984 for (v = ast_variable_browse(cfg, country); v; v = v->next) {
985 store_config_tone_zone(zone, v->name, v->value);
987 ast_tone_zone_unlock(zone);
990 if (!is_valid_tone_zone(zone)) {
991 ast_log(LOG_WARNING, "Indication country '%s' is invalid\n", country);
992 } else if (ast_register_indication_country(zone)) {
993 ast_log(LOG_WARNING, "Unable to register indication country '%s'.\n",
998 zone = ast_tone_zone_unref(zone);
1004 * Mark the zone and its tones before parsing configuration. We will use this
1005 * to know what to remove after configuration is parsed.
1007 static int tone_zone_mark(void *obj, void *arg, int flags)
1009 struct ast_tone_zone *zone = obj;
1010 struct ast_tone_zone_sound *s;
1012 ast_tone_zone_lock(zone);
1016 AST_LIST_TRAVERSE(&zone->tones, s, entry) {
1020 ast_tone_zone_unlock(zone);
1026 * Prune tones no longer in the configuration, and have the tone zone unlinked
1027 * if it is no longer in the configuration at all.
1029 static int prune_tone_zone(void *obj, void *arg, int flags)
1031 struct ast_tone_zone *zone = obj;
1032 struct ast_tone_zone_sound *s;
1034 ast_tone_zone_lock(zone);
1036 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, s, entry) {
1038 AST_LIST_REMOVE_CURRENT(entry);
1039 s = ast_tone_zone_sound_unref(s);
1042 AST_LIST_TRAVERSE_SAFE_END;
1044 ast_tone_zone_unlock(zone);
1046 return zone->killme ? CMP_MATCH : 0;
1049 /*! \brief load indications module */
1050 static int load_indications(int reload)
1052 struct ast_config *cfg;
1053 const char *cxt = NULL;
1054 const char *country = NULL;
1055 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1058 cfg = ast_config_load2(config, "indications", config_flags);
1060 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
1061 ast_log(LOG_WARNING, "Can't find indications config file %s.\n", config);
1063 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
1067 /* Lock the container to prevent multiple simultaneous reloads */
1068 ao2_lock(ast_tone_zones);
1070 ao2_callback(ast_tone_zones, OBJ_NODATA, tone_zone_mark, NULL);
1072 /* Use existing config to populate the Indication table */
1073 while ((cxt = ast_category_browse(cfg, cxt))) {
1074 /* All categories but "general" are considered countries */
1075 if (!strcasecmp(cxt, "general")) {
1079 if (parse_tone_zone(cfg, cxt)) {
1080 goto return_cleanup;
1084 ao2_callback(ast_tone_zones, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK,
1085 prune_tone_zone, NULL);
1087 /* determine which country is the default */
1088 country = ast_variable_retrieve(cfg, "general", "country");
1089 if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
1090 ast_log(LOG_WARNING, "Unable to set the default country (for indication tones)\n");
1096 ao2_unlock(ast_tone_zones);
1097 ast_config_destroy(cfg);
1102 /*! \brief CLI entries for commands provided by this module */
1103 static struct ast_cli_entry cli_indications[] = {
1104 AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
1105 AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
1106 AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
1109 static int ast_tone_zone_hash(const void *obj, const int flags)
1111 const struct ast_tone_zone *zone = obj;
1113 return ast_str_case_hash(zone->country);
1116 static int ast_tone_zone_cmp(void *obj, void *arg, int flags)
1118 struct ast_tone_zone *zone = obj;
1119 struct ast_tone_zone *zone_arg = arg;
1121 return (!strcasecmp(zone->country, zone_arg->country)) ?
1122 CMP_MATCH | CMP_STOP : 0;
1125 int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone *zone)
1127 struct ast_data *data_zone_sound;
1128 struct ast_tone_zone_sound *s;
1130 ast_data_add_structure(ast_tone_zone, tree, zone);
1132 if (AST_LIST_EMPTY(&zone->tones)) {
1136 data_zone_sound = ast_data_add_node(tree, "tones");
1137 if (!data_zone_sound) {
1141 ast_tone_zone_lock(zone);
1143 AST_LIST_TRAVERSE(&zone->tones, s, entry) {
1144 ast_data_add_structure(ast_tone_zone_sound, data_zone_sound, s);
1147 ast_tone_zone_unlock(zone);
1152 /*! \internal \brief Clean up resources on Asterisk shutdown */
1153 static void indications_shutdown(void)
1155 ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
1156 if (default_tone_zone) {
1157 ast_tone_zone_unref(default_tone_zone);
1158 default_tone_zone = NULL;
1160 if (ast_tone_zones) {
1161 ao2_ref(ast_tone_zones, -1);
1162 ast_tone_zones = NULL;
1166 /*! \brief Load indications module */
1167 int ast_indications_init(void)
1169 if (!(ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
1170 ast_tone_zone_hash, ast_tone_zone_cmp))) {
1174 if (load_indications(0)) {
1175 indications_shutdown();
1179 ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
1181 ast_register_atexit(indications_shutdown);
1185 /*! \brief Reload indications module */
1186 int ast_indications_reload(void)
1188 return load_indications(1);