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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/lock.h"
33 #include "asterisk/linkedlists.h"
34 #include "asterisk/indications.h"
35 #include "asterisk/frame.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/module.h"
40 #include "asterisk/astobj2.h"
41 #include "asterisk/data.h"
43 #include "asterisk/_private.h" /* _init(), _reload() */
45 #define DATA_EXPORT_TONE_ZONE(MEMBER) \
46 MEMBER(ast_tone_zone, country, AST_DATA_STRING) \
47 MEMBER(ast_tone_zone, description, AST_DATA_STRING) \
48 MEMBER(ast_tone_zone, nrringcadence, AST_DATA_UNSIGNED_INTEGER)
50 AST_DATA_STRUCTURE(ast_tone_zone, DATA_EXPORT_TONE_ZONE);
52 #define DATA_EXPORT_TONE_ZONE_SOUND(MEMBER) \
53 MEMBER(ast_tone_zone_sound, name, AST_DATA_STRING) \
54 MEMBER(ast_tone_zone_sound, data, AST_DATA_STRING)
56 AST_DATA_STRUCTURE(ast_tone_zone_sound, DATA_EXPORT_TONE_ZONE_SOUND);
59 static const char config[] = "indications.conf";
61 static const int midi_tohz[128] = {
62 8, 8, 9, 9, 10, 10, 11, 12, 12, 13,
63 14, 15, 16, 17, 18, 19, 20, 21, 23, 24,
64 25, 27, 29, 30, 32, 34, 36, 38, 41, 43,
65 46, 48, 51, 55, 58, 61, 65, 69, 73, 77,
66 82, 87, 92, 97, 103, 110, 116, 123, 130, 138,
67 146, 155, 164, 174, 184, 195, 207, 220, 233, 246,
68 261, 277, 293, 311, 329, 349, 369, 391, 415, 440,
69 466, 493, 523, 554, 587, 622, 659, 698, 739, 783,
70 830, 880, 932, 987, 1046, 1108, 1174, 1244, 1318, 1396,
71 1479, 1567, 1661, 1760, 1864, 1975, 2093, 2217, 2349, 2489,
72 2637, 2793, 2959, 3135, 3322, 3520, 3729, 3951, 4186, 4434,
73 4698, 4978, 5274, 5587, 5919, 6271, 6644, 7040, 7458, 7902,
74 8372, 8869, 9397, 9956, 10548, 11175, 11839, 12543
77 static struct ao2_container *ast_tone_zones;
79 #define NUM_TONE_ZONE_BUCKETS 53
82 * \note Access to this is protected by locking the ast_tone_zones container
84 static struct ast_tone_zone *default_tone_zone;
86 struct playtones_item {
97 struct playtones_def {
102 struct playtones_item *items;
105 struct playtones_state {
115 struct playtones_item *items;
121 unsigned char offset[AST_FRIENDLY_OFFSET];
125 static void playtones_release(struct ast_channel *chan, void *params)
127 struct playtones_state *ps = params;
130 ast_set_write_format(chan, ps->origwfmt);
141 static void *playtones_alloc(struct ast_channel *chan, void *params)
143 struct playtones_def *pd = params;
144 struct playtones_state *ps = NULL;
146 if (!(ps = ast_calloc(1, sizeof(*ps)))) {
150 ps->origwfmt = chan->writeformat;
152 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
153 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
154 playtones_release(NULL, ps);
158 ps->reppos = pd->reppos;
159 ps->nitems = pd->nitems;
160 ps->items = pd->items;
164 /* Let interrupts interrupt :) */
165 if (pd->interruptible) {
166 ast_set_flag(chan, AST_FLAG_WRITE_INT);
168 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
174 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
176 struct playtones_state *ps = data;
177 struct playtones_item *pi;
180 /* we need to prepare a frame with 16 * timelen samples as we're
181 * generating SLIN audio */
184 if (len > sizeof(ps->data) / 2 - 1) {
185 ast_log(LOG_WARNING, "Can't generate that much data!\n");
189 memset(&ps->f, 0, sizeof(ps->f));
191 pi = &ps->items[ps->npos];
193 if (ps->oldnpos != ps->npos) {
194 /* Load new parameters */
196 ps->v2_1 = pi->init_v2_1;
197 ps->v3_1 = pi->init_v3_1;
199 ps->v2_2 = pi->init_v2_2;
200 ps->v3_2 = pi->init_v3_2;
201 ps->oldnpos = ps->npos;
204 for (x = 0; x < samples; x++) {
207 ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
211 ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
214 p = ps->v3_2 - 32768;
218 p = ((p * 9) / 10) + 1;
219 ps->data[x] = (ps->v3_1 * p) >> 15;
221 ps->data[x] = ps->v3_1 + ps->v3_2;
225 ps->f.frametype = AST_FRAME_VOICE;
226 ps->f.subclass.codec = AST_FORMAT_SLINEAR;
228 ps->f.samples = samples;
229 ps->f.offset = AST_FRIENDLY_OFFSET;
230 ps->f.data.ptr = ps->data;
232 if (ast_write(chan, &ps->f)) {
238 if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
239 ps->pos = 0; /* start new item */
241 if (ps->npos >= ps->nitems) { /* last item? */
242 if (ps->reppos == -1) { /* repeat set? */
245 ps->npos = ps->reppos; /* redo from top */
252 static struct ast_generator playtones = {
253 .alloc = playtones_alloc,
254 .release = playtones_release,
255 .generate = playtones_generator,
258 int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data)
260 if (sscanf(s, "%30u+%30u/%30u", &tone_data->freq1, &tone_data->freq2,
261 &tone_data->time) == 3) {
262 /* f1+f2/time format */
263 } else if (sscanf(s, "%30u+%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
266 } else if (sscanf(s, "%30u*%30u/%30u", &tone_data->freq1, &tone_data->freq2,
267 &tone_data->time) == 3) {
268 /* f1*f2/time format */
269 tone_data->modulate = 1;
270 } else if (sscanf(s, "%30u*%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
273 tone_data->modulate = 1;
274 } else if (sscanf(s, "%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
276 tone_data->freq2 = 0;
277 } else if (sscanf(s, "%30u", &tone_data->freq1) == 1) {
279 tone_data->freq2 = 0;
281 } else if (sscanf(s, "M%30u+M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
282 &tone_data->time) == 3) {
283 /* Mf1+Mf2/time format */
284 tone_data->midinote = 1;
285 } else if (sscanf(s, "M%30u+M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
288 tone_data->midinote = 1;
289 } else if (sscanf(s, "M%30u*M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
290 &tone_data->time) == 3) {
291 /* Mf1*Mf2/time format */
292 tone_data->modulate = 1;
293 tone_data->midinote = 1;
294 } else if (sscanf(s, "M%30u*M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
297 tone_data->modulate = 1;
298 tone_data->midinote = 1;
299 } else if (sscanf(s, "M%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
300 /* Mf1/time format */
301 tone_data->freq2 = -1;
302 tone_data->midinote = 1;
303 } else if (sscanf(s, "M%30u", &tone_data->freq1) == 1) {
305 tone_data->freq2 = -1;
307 tone_data->midinote = 1;
315 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
317 char *s, *data = ast_strdupa(playlst);
318 struct playtones_def d = { vol, -1, 0, 1, NULL };
321 static const float sample_rate = 8000.0;
322 static const float max_sample_val = 32768.0;
325 d.vol = 7219; /* Default to -8db */
328 d.interruptible = interruptible;
332 /* check if the data is separated with '|' or with ',' by default */
333 if (strchr(stringp,'|')) {
339 while ((s = strsep(&stringp, separator)) && !ast_strlen_zero(s)) {
340 struct ast_tone_zone_part tone_data = {
348 } else if (d.reppos == -1) {
352 if (ast_tone_zone_part_parse(s, &tone_data)) {
353 ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
357 if (tone_data.midinote) {
358 /* midi notes must be between 0 and 127 */
360 if (tone_data.freq1 >= 0 && tone_data.freq1 <= 127) {
361 tone_data.freq1 = midi_tohz[tone_data.freq1];
366 if (tone_data.freq2 >= 0 && tone_data.freq2 <= 127) {
367 tone_data.freq2 = midi_tohz[tone_data.freq2];
373 if (!(d.items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items)))) {
377 d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / sample_rate)) * max_sample_val;
378 d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
379 d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
381 d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / sample_rate)) * max_sample_val;
382 d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
383 d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
385 d.items[d.nitems].duration = tone_data.time;
386 d.items[d.nitems].modulate = tone_data.modulate;
392 ast_log(LOG_ERROR, "No valid tone parts\n");
396 if (ast_activate_generator(chan, &playtones, &d)) {
404 void ast_playtones_stop(struct ast_channel *chan)
406 ast_deactivate_generator(chan);
409 int ast_tone_zone_count(void)
411 return ao2_container_count(ast_tone_zones);
414 struct ao2_iterator ast_tone_zone_iterator_init(void)
416 return ao2_iterator_init(ast_tone_zones, 0);
419 /*! \brief Set global indication country
420 If no country is specified or we are unable to find the zone, then return not found */
421 static int ast_set_indication_country(const char *country)
423 struct ast_tone_zone *zone = NULL;
425 if (ast_strlen_zero(country) || !(zone = ast_get_indication_zone(country))) {
429 ast_verb(3, "Setting default indication country to '%s'\n", country);
431 ao2_lock(ast_tone_zones);
432 if (default_tone_zone) {
433 default_tone_zone = ast_tone_zone_unref(default_tone_zone);
435 default_tone_zone = ast_tone_zone_ref(zone);
436 ao2_unlock(ast_tone_zones);
438 zone = ast_tone_zone_unref(zone);
443 /*! \brief locate ast_tone_zone, given the country. if country == NULL, use the default country */
444 struct ast_tone_zone *ast_get_indication_zone(const char *country)
446 struct ast_tone_zone *tz = NULL;
447 struct ast_tone_zone zone_arg = {
451 if (ast_strlen_zero(country)) {
452 ao2_lock(ast_tone_zones);
453 if (default_tone_zone) {
454 tz = ast_tone_zone_ref(default_tone_zone);
456 ao2_unlock(ast_tone_zones);
461 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
463 return ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER);
466 struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *_zone, const char *indication)
468 struct ast_tone_zone_sound *ts = NULL;
469 /* _zone is const to the users of the API */
470 struct ast_tone_zone *zone = (struct ast_tone_zone *) _zone;
472 /* If no zone is specified, use the default */
474 ao2_lock(ast_tone_zones);
475 if (default_tone_zone) {
476 zone = ast_tone_zone_ref(default_tone_zone);
478 ao2_unlock(ast_tone_zones);
485 ast_tone_zone_lock(zone);
487 /* Look through list of tones in the zone searching for the right one */
488 AST_LIST_TRAVERSE(&zone->tones, ts, entry) {
489 if (!strcasecmp(ts->name, indication)) {
490 /* Increase ref count for the reference we will return */
491 ts = ast_tone_zone_sound_ref(ts);
496 ast_tone_zone_unlock(zone);
501 static void ast_tone_zone_sound_destructor(void *obj)
503 struct ast_tone_zone_sound *ts = obj;
505 /* Deconstify the 'const char *'s so the compiler doesn't complain. (but it's safe) */
507 ast_free((char *) ts->name);
512 ast_free((char *) ts->data);
517 /*! \brief deallocate the passed tone zone */
518 static void ast_tone_zone_destructor(void *obj)
520 struct ast_tone_zone *zone = obj;
521 struct ast_tone_zone_sound *current;
523 while ((current = AST_LIST_REMOVE_HEAD(&zone->tones, entry))) {
524 current = ast_tone_zone_sound_unref(current);
527 if (zone->ringcadence) {
528 ast_free(zone->ringcadence);
529 zone->ringcadence = NULL;
533 /*! \brief add a new country, if country exists, it will be replaced. */
534 static int ast_register_indication_country(struct ast_tone_zone *zone)
536 ao2_lock(ast_tone_zones);
537 if (!default_tone_zone) {
538 default_tone_zone = ast_tone_zone_ref(zone);
540 ao2_unlock(ast_tone_zones);
542 ao2_link(ast_tone_zones, zone);
544 ast_verb(3, "Registered indication country '%s'\n", zone->country);
549 /*! \brief remove an existing country and all its indications, country must exist. */
550 static int ast_unregister_indication_country(const char *country)
552 struct ast_tone_zone *tz = NULL;
553 struct ast_tone_zone zone_arg = {
557 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
559 if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
563 ao2_lock(ast_tone_zones);
564 if (default_tone_zone == tz) {
565 ast_tone_zone_unref(default_tone_zone);
566 /* Get a new default, punt to the first one we find */
567 default_tone_zone = ao2_callback(ast_tone_zones, 0, NULL, NULL);
569 ao2_unlock(ast_tone_zones);
571 ao2_unlink(ast_tone_zones, tz);
573 tz = ast_tone_zone_unref(tz);
579 * \note called with the tone zone locked
581 static int ast_register_indication(struct ast_tone_zone *zone, const char *indication,
582 const char *tonelist)
584 struct ast_tone_zone_sound *ts;
586 if (ast_strlen_zero(indication) || ast_strlen_zero(tonelist)) {
590 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
591 if (!strcasecmp(indication, ts->name)) {
592 AST_LIST_REMOVE_CURRENT(entry);
593 ts = ast_tone_zone_sound_unref(ts);
597 AST_LIST_TRAVERSE_SAFE_END;
599 if (!(ts = ao2_alloc(sizeof(*ts), ast_tone_zone_sound_destructor))) {
603 if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
604 ts = ast_tone_zone_sound_unref(ts);
608 AST_LIST_INSERT_TAIL(&zone->tones, ts, entry); /* Inherit reference */
613 /*! \brief remove an existing country's indication. Both country and indication must exist */
614 static int ast_unregister_indication(struct ast_tone_zone *zone, const char *indication)
616 struct ast_tone_zone_sound *ts;
619 ast_tone_zone_lock(zone);
621 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
622 if (!strcasecmp(indication, ts->name)) {
623 AST_LIST_REMOVE_CURRENT(entry);
624 ts = ast_tone_zone_sound_unref(ts);
629 AST_LIST_TRAVERSE_SAFE_END;
631 ast_tone_zone_unlock(zone);
636 static struct ast_tone_zone *ast_tone_zone_alloc(void)
638 return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
641 static char *complete_country(struct ast_cli_args *a)
644 struct ao2_iterator i;
647 struct ast_tone_zone *tz;
649 wordlen = strlen(a->word);
651 i = ao2_iterator_init(ast_tone_zones, 0);
652 while ((tz = ao2_iterator_next(&i))) {
653 if (!strncasecmp(a->word, tz->country, wordlen) && ++which > a->n) {
654 res = ast_strdup(tz->country);
656 tz = ast_tone_zone_unref(tz);
665 static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
667 struct ast_tone_zone *tz;
668 int created_country = 0;
669 char *res = CLI_SUCCESS;
673 e->command = "indication add";
675 "Usage: indication add <country> <indication> \"<tonelist>\"\n"
676 " Add the given indication to the country.\n";
680 return complete_country(a);
687 return CLI_SHOWUSAGE;
690 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
691 /* country does not exist, create it */
692 ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
694 if (!(tz = ast_tone_zone_alloc())) {
698 ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
700 if (ast_register_indication_country(tz)) {
701 ast_log(LOG_WARNING, "Unable to register new country\n");
702 tz = ast_tone_zone_unref(tz);
709 ast_tone_zone_lock(tz);
711 if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
712 ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
713 if (created_country) {
714 ast_unregister_indication_country(a->argv[2]);
719 ast_tone_zone_unlock(tz);
721 tz = ast_tone_zone_unref(tz);
726 static char *complete_indications(struct ast_cli_args *a)
731 struct ast_tone_zone_sound *ts;
732 struct ast_tone_zone *tz, tmp_tz = {
736 ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
738 if (!(tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER))) {
742 wordlen = strlen(a->word);
744 ast_tone_zone_lock(tz);
745 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
746 if (!strncasecmp(a->word, ts->name, wordlen) && ++which > a->n) {
747 res = ast_strdup(ts->name);
751 ast_tone_zone_unlock(tz);
753 tz = ast_tone_zone_unref(tz);
758 static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
760 struct ast_tone_zone *tz;
761 char *res = CLI_SUCCESS;
765 e->command = "indication remove";
767 "Usage: indication remove <country> [indication]\n"
768 " Remove the given indication from the country.\n";
772 return complete_country(a);
773 } else if (a->pos == 3) {
774 return complete_indications(a);
778 if (a->argc != 3 && a->argc != 4) {
779 return CLI_SHOWUSAGE;
783 /* remove entire country */
784 if (ast_unregister_indication_country(a->argv[2])) {
785 ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
792 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
793 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
797 if (ast_unregister_indication(tz, a->argv[3])) {
798 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
802 tz = ast_tone_zone_unref(tz);
807 static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
809 struct ast_tone_zone *tz = NULL;
811 int found_country = 0;
816 e->command = "indication show";
818 "Usage: indication show [<country> ...]\n"
819 " Display either a condensed summary of all countries and indications, or a\n"
820 " more verbose list of indications for the specified countries.\n";
823 return complete_country(a);
827 struct ao2_iterator iter;
828 /* no arguments, show a list of countries */
829 ast_cli(a->fd, "Country Description\n");
830 ast_cli(a->fd, "===========================\n");
831 iter = ast_tone_zone_iterator_init();
832 while ((tz = ao2_iterator_next(&iter))) {
833 ast_tone_zone_lock(tz);
834 ast_cli(a->fd, "%-7.7s %s\n", tz->country, tz->description);
835 ast_tone_zone_unlock(tz);
836 tz = ast_tone_zone_unref(tz);
841 buf = ast_str_alloca(256);
843 for (i = 2; i < a->argc; i++) {
844 struct ast_tone_zone zone_arg = {
847 struct ast_tone_zone_sound *ts;
850 ast_copy_string(zone_arg.country, a->argv[i], sizeof(zone_arg.country));
852 if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
856 if (!found_country) {
858 ast_cli(a->fd, "Country Indication PlayList\n");
859 ast_cli(a->fd, "=====================================\n");
862 ast_tone_zone_lock(tz);
864 ast_str_set(&buf, 0, "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
865 for (j = 0; j < tz->nrringcadence; j++) {
866 ast_str_append(&buf, 0, "%d%s", tz->ringcadence[j],
867 (j == tz->nrringcadence - 1) ? "" : ",");
869 ast_str_append(&buf, 0, "\n");
870 ast_cli(a->fd, "%s", buf->str);
872 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
873 ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
876 ast_tone_zone_unlock(tz);
877 tz = ast_tone_zone_unref(tz);
880 if (!found_country) {
881 ast_cli(a->fd, "No countries matched your criteria.\n");
887 static int is_valid_tone_zone(struct ast_tone_zone *zone)
891 ast_tone_zone_lock(zone);
892 res = (!ast_strlen_zero(zone->description) && !AST_LIST_EMPTY(&zone->tones));
893 ast_tone_zone_unlock(zone);
900 * \note This is called with the tone zone locked.
902 static void store_tone_zone_ring_cadence(struct ast_tone_zone *zone, const char *val)
905 char *ring, *c = buf;
907 ast_copy_string(buf, val, sizeof(buf));
909 while ((ring = strsep(&c, ","))) {
912 ring = ast_strip(ring);
914 if (!isdigit(ring[0]) || (val = atoi(ring)) == -1) {
915 ast_log(LOG_WARNING, "Invalid ringcadence given '%s'.\n", ring);
919 if (!(tmp = ast_realloc(zone->ringcadence, (zone->nrringcadence + 1) * sizeof(int)))) {
923 zone->ringcadence = tmp;
924 tmp[zone->nrringcadence] = val;
925 zone->nrringcadence++;
929 static void store_config_tone_zone(struct ast_tone_zone *zone, const char *var,
932 CV_START(var, value);
934 CV_STR("description", zone->description);
935 CV_F("ringcadence", store_tone_zone_ring_cadence(zone, value));
936 CV_F("ringcadance", store_tone_zone_ring_cadence(zone, value));
938 ast_register_indication(zone, var, value);
943 static void reset_tone_zone(struct ast_tone_zone *zone)
945 ast_tone_zone_lock(zone);
949 if (zone->nrringcadence) {
950 zone->nrringcadence = 0;
951 ast_free(zone->ringcadence);
952 zone->ringcadence = NULL;
955 ast_tone_zone_unlock(zone);
958 static int parse_tone_zone(struct ast_config *cfg, const char *country)
960 struct ast_variable *v;
961 struct ast_tone_zone *zone;
962 struct ast_tone_zone tmp_zone = {
967 ast_copy_string(tmp_zone.country, country, sizeof(tmp_zone.country));
969 if ((zone = ao2_find(ast_tone_zones, &tmp_zone, OBJ_POINTER))) {
970 reset_tone_zone(zone);
971 } else if ((zone = ast_tone_zone_alloc())) {
973 ast_copy_string(zone->country, country, sizeof(zone->country));
978 ast_tone_zone_lock(zone);
979 for (v = ast_variable_browse(cfg, country); v; v = v->next) {
980 store_config_tone_zone(zone, v->name, v->value);
982 ast_tone_zone_unlock(zone);
985 if (!is_valid_tone_zone(zone)) {
986 ast_log(LOG_WARNING, "Indication country '%s' is invalid\n", country);
987 } else if (ast_register_indication_country(zone)) {
988 ast_log(LOG_WARNING, "Unable to register indication country '%s'.\n",
993 zone = ast_tone_zone_unref(zone);
999 * Mark the zone and its tones before parsing configuration. We will use this
1000 * to know what to remove after configuration is parsed.
1002 static int tone_zone_mark(void *obj, void *arg, int flags)
1004 struct ast_tone_zone *zone = obj;
1005 struct ast_tone_zone_sound *s;
1007 ast_tone_zone_lock(zone);
1011 AST_LIST_TRAVERSE(&zone->tones, s, entry) {
1015 ast_tone_zone_unlock(zone);
1021 * Prune tones no longer in the configuration, and have the tone zone unlinked
1022 * if it is no longer in the configuration at all.
1024 static int prune_tone_zone(void *obj, void *arg, int flags)
1026 struct ast_tone_zone *zone = obj;
1027 struct ast_tone_zone_sound *s;
1029 ast_tone_zone_lock(zone);
1031 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, s, entry) {
1033 AST_LIST_REMOVE_CURRENT(entry);
1034 s = ast_tone_zone_sound_unref(s);
1037 AST_LIST_TRAVERSE_SAFE_END;
1039 ast_tone_zone_unlock(zone);
1041 return zone->killme ? CMP_MATCH : 0;
1044 /*! \brief load indications module */
1045 static int load_indications(int reload)
1047 struct ast_config *cfg;
1048 const char *cxt = NULL;
1049 const char *country = NULL;
1050 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1053 cfg = ast_config_load2(config, "indications", config_flags);
1055 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
1056 ast_log(LOG_WARNING, "Can't find indications config file %s.\n", config);
1058 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
1062 /* Lock the container to prevent multiple simultaneous reloads */
1063 ao2_lock(ast_tone_zones);
1065 ao2_callback(ast_tone_zones, OBJ_NODATA, tone_zone_mark, NULL);
1067 /* Use existing config to populate the Indication table */
1068 while ((cxt = ast_category_browse(cfg, cxt))) {
1069 /* All categories but "general" are considered countries */
1070 if (!strcasecmp(cxt, "general")) {
1074 if (parse_tone_zone(cfg, cxt)) {
1075 goto return_cleanup;
1079 ao2_callback(ast_tone_zones, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK,
1080 prune_tone_zone, NULL);
1082 /* determine which country is the default */
1083 country = ast_variable_retrieve(cfg, "general", "country");
1084 if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
1085 ast_log(LOG_WARNING, "Unable to set the default country (for indication tones)\n");
1091 ao2_unlock(ast_tone_zones);
1092 ast_config_destroy(cfg);
1097 /*! \brief CLI entries for commands provided by this module */
1098 static struct ast_cli_entry cli_indications[] = {
1099 AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
1100 AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
1101 AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
1104 static int ast_tone_zone_hash(const void *obj, const int flags)
1106 const struct ast_tone_zone *zone = obj;
1108 return ast_str_case_hash(zone->country);
1111 static int ast_tone_zone_cmp(void *obj, void *arg, int flags)
1113 struct ast_tone_zone *zone = obj;
1114 struct ast_tone_zone *zone_arg = arg;
1116 return (!strcasecmp(zone->country, zone_arg->country)) ?
1117 CMP_MATCH | CMP_STOP : 0;
1120 int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone *zone)
1122 struct ast_data *data_zone_sound;
1123 struct ast_tone_zone_sound *s;
1125 ast_data_add_structure(ast_tone_zone, tree, zone);
1127 if (AST_LIST_EMPTY(&zone->tones)) {
1131 data_zone_sound = ast_data_add_node(tree, "tones");
1132 if (!data_zone_sound) {
1136 ast_tone_zone_lock(zone);
1138 AST_LIST_TRAVERSE(&zone->tones, s, entry) {
1139 ast_data_add_structure(ast_tone_zone_sound, data_zone_sound, s);
1142 ast_tone_zone_unlock(zone);
1147 /*! \brief Load indications module */
1148 int ast_indications_init(void)
1150 if (!(ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
1151 ast_tone_zone_hash, ast_tone_zone_cmp))) {
1155 if (load_indications(0)) {
1159 ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
1164 /*! \brief Reload indications module */
1165 int ast_indications_reload(void)
1167 return load_indications(1);