Debian Codegraph @

main/indications.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2002, Pauline Middelink
5 * Copyright (C) 2009, Digium, Inc.
6 *
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.
12 *
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.
16 */
17
18 /*!
19 * \file
20 * \brief Indication Tone Handling
21 *
22 * \author Pauline Middelink <[email protected]>
23 * \author Russell Bryant <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include <math.h>
33
34 #include "asterisk/lock.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/indications.h"
37 #include "asterisk/frame.h"
38 #include "asterisk/format_cache.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/cli.h"
42 #include "asterisk/module.h"
43 #include "asterisk/astobj2.h"
44
45 #include "asterisk/_private.h" /* _init(), _reload() */
46
47 /* Globals */
48 static const char config[] = "indications.conf";
49
50 static const int midi_tohz[128] = {
51 8, 8, 9, 9, 10, 10, 11, 12, 12, 13,
52 14, 15, 16, 17, 18, 19, 20, 21, 23, 24,
53 25, 27, 29, 30, 32, 34, 36, 38, 41, 43,
54 46, 48, 51, 55, 58, 61, 65, 69, 73, 77,
55 82, 87, 92, 97, 103, 110, 116, 123, 130, 138,
56 146, 155, 164, 174, 184, 195, 207, 220, 233, 246,
57 261, 277, 293, 311, 329, 349, 369, 391, 415, 440,
58 466, 493, 523, 554, 587, 622, 659, 698, 739, 783,
59 830, 880, 932, 987, 1046, 1108, 1174, 1244, 1318, 1396,
60 1479, 1567, 1661, 1760, 1864, 1975, 2093, 2217, 2349, 2489,
61 2637, 2793, 2959, 3135, 3322, 3520, 3729, 3951, 4186, 4434,
62 4698, 4978, 5274, 5587, 5919, 6271, 6644, 7040, 7458, 7902,
63 8372, 8869, 9397, 9956, 10548, 11175, 11839, 12543
64 };
65
66 static struct ao2_container *ast_tone_zones;
67
68 #define NUM_TONE_ZONE_BUCKETS 53
69
70 /*!
71 * \note Access to this is protected by locking the ast_tone_zones container
72 */
73 static struct ast_tone_zone *default_tone_zone;
74
75 struct playtones_item {
76 int fac1;
77 int init_v2_1;
78 int init_v3_1;
79 int fac2;
80 int init_v2_2;
81 int init_v3_2;
82 int modulate;
83 int duration;
84 };
85
86 struct playtones_def {
87 int vol;
88 int reppos;
89 int nitems;
90 int interruptible;
91 struct playtones_item *items;
92 };
93
94 struct playtones_state {
95 int vol;
96 int v1_1;
97 int v2_1;
98 int v3_1;
99 int v1_2;
100 int v2_2;
101 int v3_2;
102 int reppos;
103 int nitems;
104 struct playtones_item *items;
105 int npos;
106 int oldnpos;
107 int pos;
108 struct ast_format *origwfmt;
109 struct ast_frame f;
110 unsigned char offset[AST_FRIENDLY_OFFSET];
111 short data[4000];
112 };
113
114 static void playtones_release(struct ast_channel *chan, void *params)
115 {
116 struct playtones_state *ps = params;
117
118 if (chan) {
119 ast_set_write_format(chan, ps->origwfmt);
120 }
121
122 ao2_cleanup(ps->origwfmt);
123 ast_free(ps->items);
124
125 ast_free(ps);
126 }
127
128 static void *playtones_alloc(struct ast_channel *chan, void *params)
129 {
130 struct playtones_def *pd = params;
131 struct playtones_state *ps = NULL;
132
133 if (!(ps = ast_calloc(1, sizeof(*ps)))) {
134 return NULL;
135 }
136
137 ps->origwfmt = ao2_bump(ast_channel_writeformat(chan));
138
139 if (ast_set_write_format(chan, ast_format_slin)) {
140 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
141 playtones_release(NULL, ps);
142 ps = NULL;
143 } else {
144 ps->vol = pd->vol;
145 ps->reppos = pd->reppos;
146 ps->nitems = pd->nitems;
147 ps->items = pd->items;
148 ps->oldnpos = -1;
149 }
150
151 /* Let interrupts interrupt :) */
152 if (pd->interruptible) {
153 ast_set_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
154 } else {
155 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
156 }
157
158 return ps;
159 }
160
161 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
162 {
163 struct playtones_state *ps = data;
164 struct playtones_item *pi;
165 int x;
166
167 /* we need to prepare a frame with 16 * timelen samples as we're
168 * generating SLIN audio */
169
170 len = samples * 2;
171 if (len > sizeof(ps->data) / 2 - 1) {
172 ast_log(LOG_WARNING, "Can't generate that much data!\n");
173 return -1;
174 }
175
176 memset(&ps->f, 0, sizeof(ps->f));
177
178 pi = &ps->items[ps->npos];
179
180 if (ps->oldnpos != ps->npos) {
181 /* Load new parameters */
182 ps->v1_1 = 0;
183 ps->v2_1 = pi->init_v2_1;
184 ps->v3_1 = pi->init_v3_1;
185 ps->v1_2 = 0;
186 ps->v2_2 = pi->init_v2_2;
187 ps->v3_2 = pi->init_v3_2;
188 ps->oldnpos = ps->npos;
189 }
190
191 for (x = 0; x < samples; x++) {
192 ps->v1_1 = ps->v2_1;
193 ps->v2_1 = ps->v3_1;
194 ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
195
196 ps->v1_2 = ps->v2_2;
197 ps->v2_2 = ps->v3_2;
198 ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
199 if (pi->modulate) {
200 int p;
201 p = ps->v3_2 - 32768;
202 if (p < 0) {
203 p = -p;
204 }
205 p = ((p * 9) / 10) + 1;
206 ps->data[x] = (ps->v3_1 * p) >> 15;
207 } else {
208 ps->data[x] = ps->v3_1 + ps->v3_2;
209 }
210 }
211
212 ps->f.frametype = AST_FRAME_VOICE;
213 ps->f.subclass.format = ast_format_slin;
214 ps->f.datalen = len;
215 ps->f.samples = samples;
216 ps->f.offset = AST_FRIENDLY_OFFSET;
217 ps->f.data.ptr = ps->data;
218
219 if (ast_write(chan, &ps->f)) {
220 return -1;
221 }
222
223 ps->pos += x;
224
225 if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
226 ps->pos = 0; /* start new item */
227 ps->npos++;
228 if (ps->npos >= ps->nitems) { /* last item? */
229 if (ps->reppos == -1) { /* repeat set? */
230 return -1;
231 }
232 ps->npos = ps->reppos; /* redo from top */
233 }
234 }
235
236 return 0;
237 }
238
239 static struct ast_generator playtones = {
240 .alloc = playtones_alloc,
241 .release = playtones_release,
242 .generate = playtones_generator,
243 };
244
245 int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data)
246 {
247 if (sscanf(s, "%30u+%30u/%30u", &tone_data->freq1, &tone_data->freq2,
248 &tone_data->time) == 3) {
249 /* f1+f2/time format */
250 } else if (sscanf(s, "%30u+%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
251 /* f1+f2 format */
252 tone_data->time = 0;
253 } else if (sscanf(s, "%30u*%30u/%30u", &tone_data->freq1, &tone_data->freq2,
254 &tone_data->time) == 3) {
255 /* f1*f2/time format */
256 tone_data->modulate = 1;
257 } else if (sscanf(s, "%30u*%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
258 /* f1*f2 format */
259 tone_data->time = 0;
260 tone_data->modulate = 1;
261 } else if (sscanf(s, "%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
262 /* f1/time format */
263 tone_data->freq2 = 0;
264 } else if (sscanf(s, "%30u", &tone_data->freq1) == 1) {
265 /* f1 format */
266 tone_data->freq2 = 0;
267 tone_data->time = 0;
268 } else if (sscanf(s, "M%30u+M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
269 &tone_data->time) == 3) {
270 /* Mf1+Mf2/time format */
271 tone_data->midinote = 1;
272 } else if (sscanf(s, "M%30u+M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
273 /* Mf1+Mf2 format */
274 tone_data->time = 0;
275 tone_data->midinote = 1;
276 } else if (sscanf(s, "M%30u*M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
277 &tone_data->time) == 3) {
278 /* Mf1*Mf2/time format */
279 tone_data->modulate = 1;
280 tone_data->midinote = 1;
281 } else if (sscanf(s, "M%30u*M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
282 /* Mf1*Mf2 format */
283 tone_data->time = 0;
284 tone_data->modulate = 1;
285 tone_data->midinote = 1;
286 } else if (sscanf(s, "M%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
287 /* Mf1/time format */
288 tone_data->freq2 = -1;
289 tone_data->midinote = 1;
290 } else if (sscanf(s, "M%30u", &tone_data->freq1) == 1) {
291 /* Mf1 format */
292 tone_data->freq2 = -1;
293 tone_data->time = 0;
294 tone_data->midinote = 1;
295 } else {
296 return -1;
297 }
298
299 return 0;
300 }
301
302 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
303 {
304 char *s, *data = ast_strdupa(playlst);
305 struct playtones_def d = { vol, -1, 0, 1, NULL };
306 char *stringp;
307 char *separator;
308 static const float sample_rate = 8000.0;
309 static const float max_sample_val = 32768.0;
310
311 if (vol < 1) {
312 d.vol = 7219; /* Default to -8db */
313 }
314
315 d.interruptible = interruptible;
316
317 stringp = data;
318
319 /* check if the data is separated with '|' or with ',' by default */
320 if (strchr(stringp,'|')) {
321 separator = "|";
322 } else {
323 separator = ",";
324 }
325
326 while ((s = strsep(&stringp, separator)) && !ast_strlen_zero(s)) {
327 struct playtones_item *new_items;
328 struct ast_tone_zone_part tone_data = {
329 .time = 0,
330 };
331
332 s = ast_strip(s);
333 if (s[0]=='!') {
334 s++;
335 } else if (d.reppos == -1) {
336 d.reppos = d.nitems;
337 }
338
339 if (ast_tone_zone_part_parse(s, &tone_data)) {
340 ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
341 continue;
342 }
343
344 if (tone_data.midinote) {
345 /* midi notes must be between 0 and 127 */
346 if (tone_data.freq1 <= 127) {
347 tone_data.freq1 = midi_tohz[tone_data.freq1];
348 } else {
349 tone_data.freq1 = 0;
350 }
351
352 if (tone_data.freq2 <= 127) {
353 tone_data.freq2 = midi_tohz[tone_data.freq2];
354 } else {
355 tone_data.freq2 = 0;
356 }
357 }
358
359 new_items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items));
360 if (!new_items) {
361 ast_free(d.items);
362 return -1;
363 }
364 d.items = new_items;
365
366 d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / sample_rate)) * max_sample_val;
367 d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
368 d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
369
370 d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / sample_rate)) * max_sample_val;
371 d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
372 d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
373
374 d.items[d.nitems].duration = tone_data.time;
375 d.items[d.nitems].modulate = tone_data.modulate;
376
377 d.nitems++;
378 }
379
380 if (!d.nitems) {
381 ast_log(LOG_ERROR, "No valid tone parts\n");
382 return -1;
383 }
384
385 if (ast_activate_generator(chan, &playtones, &d)) {
386 ast_free(d.items);
387 return -1;
388 }
389
390 return 0;
391 }
392
393 void ast_playtones_stop(struct ast_channel *chan)
394 {
395 ast_deactivate_generator(chan);
396 }
397
398 int ast_tone_zone_count(void)
399 {
400 return ao2_container_count(ast_tone_zones);
401 }
402
403 struct ao2_iterator ast_tone_zone_iterator_init(void)
404 {
405 return ao2_iterator_init(ast_tone_zones, 0);
406 }
407
408 /*! \brief Set global indication country
409 If no country is specified or we are unable to find the zone, then return not found */
410 static int ast_set_indication_country(const char *country)
411 {
412 struct ast_tone_zone *zone = NULL;
413
414 if (ast_strlen_zero(country)) {
415 ast_log(LOG_WARNING, "Failed to get indication zone, country not set\n");
416 return -1;
417 }
418
419 if (!(zone = ast_get_indication_zone(country))) {
420 ast_log(LOG_WARNING, "Failed to get indication zone: %s\n", country);
421 return -1;
422 }
423
424 ast_log(LOG_NOTICE, "Setting default indication country to '%s'\n", country);
425
426 ao2_lock(ast_tone_zones);
427 if (default_tone_zone) {
428 default_tone_zone = ast_tone_zone_unref(default_tone_zone);
429 }
430 default_tone_zone = ast_tone_zone_ref(zone);
431 ao2_unlock(ast_tone_zones);
432
433 zone = ast_tone_zone_unref(zone);
434
435 return 0;
436 }
437
438 /*! \brief locate ast_tone_zone, given the country. if country == NULL, use the default country */
439 struct ast_tone_zone *ast_get_indication_zone(const char *country)
440 {
441 struct ast_tone_zone *tz = NULL;
442 struct ast_tone_zone zone_arg = {
443 .nrringcadence = 0,
444 };
445
446 if (ast_strlen_zero(country)) {
447 ao2_lock(ast_tone_zones);
448 if (default_tone_zone) {
449 tz = ast_tone_zone_ref(default_tone_zone);
450 }
451 ao2_unlock(ast_tone_zones);
452
453 return tz;
454 }
455
456 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
457
458 return ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER);
459 }
460
461 struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *_zone, const char *indication)
462 {
463 struct ast_tone_zone_sound *ts = NULL;
464 /* _zone is const to the users of the API */
465 struct ast_tone_zone *zone = (struct ast_tone_zone *) _zone;
466
467 /* If no zone is specified, use the default */
468 if (!zone) {
469 ao2_lock(ast_tone_zones);
470 if (default_tone_zone) {
471 zone = ast_tone_zone_ref(default_tone_zone);
472 }
473 ao2_unlock(ast_tone_zones);
474
475 if (!zone) {
476 return NULL;
477 }
478 }
479
480 ast_tone_zone_lock(zone);
481
482 /* Look through list of tones in the zone searching for the right one */
483 AST_LIST_TRAVERSE(&zone->tones, ts, entry) {
484 if (!strcasecmp(ts->name, indication)) {
485 /* Increase ref count for the reference we will return */
486 ts = ast_tone_zone_sound_ref(ts);
487 break;
488 }
489 }
490
491 ast_tone_zone_unlock(zone);
492
493 if (!_zone)
494 zone = ast_tone_zone_unref(zone);
495
496 return ts;
497 }
498
499 static void ast_tone_zone_sound_destructor(void *obj)
500 {
501 struct ast_tone_zone_sound *ts = obj;
502
503 /* Deconstify the 'const char *'s so the compiler doesn't complain. (but it's safe) */
504 if (ts->name) {
505 ast_free((char *) ts->name);
506 ts->name = NULL;
507 }
508
509 if (ts->data) {
510 ast_free((char *) ts->data);
511 ts->data = NULL;
512 }
513 }
514
515 /*! \brief deallocate the passed tone zone */
516 static void ast_tone_zone_destructor(void *obj)
517 {
518 struct ast_tone_zone *zone = obj;
519 struct ast_tone_zone_sound *current;
520
521 while ((current = AST_LIST_REMOVE_HEAD(&zone->tones, entry))) {
522 current = ast_tone_zone_sound_unref(current);
523 }
524
525 if (zone->ringcadence) {
526 ast_free(zone->ringcadence);
527 zone->ringcadence = NULL;
528 }
529 }
530
531 /*! \brief add a new country, if country exists, it will be replaced. */
532 static int ast_register_indication_country(struct ast_tone_zone *zone)
533 {
534 ao2_lock(ast_tone_zones);
535 if (!default_tone_zone) {
536 default_tone_zone = ast_tone_zone_ref(zone);
537 }
538 ao2_unlock(ast_tone_zones);
539
540 ao2_link(ast_tone_zones, zone);
541
542 ast_verb(5, "Registered indication country '%s'\n", zone->country);
543
544 return 0;
545 }
546
547 /*! \brief remove an existing country and all its indications, country must exist. */
548 static int ast_unregister_indication_country(const char *country)
549 {
550 struct ast_tone_zone *tz = NULL;
551 struct ast_tone_zone zone_arg = {
552 .nrringcadence = 0,
553 };
554
555 ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
556
557 ao2_lock(ast_tone_zones);
558 tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER | OBJ_UNLINK);
559 if (!tz) {
560 ao2_unlock(ast_tone_zones);
561 return -1;
562 }
563
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);
568 }
569 ao2_unlock(ast_tone_zones);
570
571 tz = ast_tone_zone_unref(tz);
572
573 return 0;
574 }
575
576 /*!
577 * \note called with the tone zone locked
578 */
579 static int ast_register_indication(struct ast_tone_zone *zone, const char *indication,
580 const char *tonelist)
581 {
582 struct ast_tone_zone_sound *ts;
583
584 if (ast_strlen_zero(indication) || ast_strlen_zero(tonelist)) {
585 return -1;
586 }
587
588 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
589 if (!strcasecmp(indication, ts->name)) {
590 AST_LIST_REMOVE_CURRENT(entry);
591 ts = ast_tone_zone_sound_unref(ts);
592 break;
593 }
594 }
595 AST_LIST_TRAVERSE_SAFE_END;
596
597 ts = ao2_alloc_options(sizeof(*ts), ast_tone_zone_sound_destructor,
598 AO2_ALLOC_OPT_LOCK_NOLOCK);
599 if (!ts) {
600 return -1;
601 }
602
603 if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
604 ts = ast_tone_zone_sound_unref(ts);
605 return -1;
606 }
607
608 AST_LIST_INSERT_TAIL(&zone->tones, ts, entry); /* Inherit reference */
609
610 return 0;
611 }
612
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)
615 {
616 struct ast_tone_zone_sound *ts;
617 int res = -1;
618
619 ast_tone_zone_lock(zone);
620
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);
625 res = 0;
626 break;
627 }
628 }
629 AST_LIST_TRAVERSE_SAFE_END;
630
631 ast_tone_zone_unlock(zone);
632
633 return res;
634 }
635
636 static struct ast_tone_zone *ast_tone_zone_alloc(void)
637 {
638 return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
639 }
640
641 static char *complete_country(struct ast_cli_args *a)
642 {
643 struct ao2_iterator i;
644 size_t wordlen;
645 struct ast_tone_zone *tz;
646
647 wordlen = strlen(a->word);
648
649 i = ao2_iterator_init(ast_tone_zones, 0);
650 while ((tz = ao2_iterator_next(&i))) {
651 if (!strncasecmp(a->word, tz->country, wordlen)) {
652 if (ast_cli_completion_add(ast_strdup(tz->country))) {
653 ast_tone_zone_unref(tz);
654 break;
655 }
656 }
657 ast_tone_zone_unref(tz);
658 }
659 ao2_iterator_destroy(&i);
660
661 return NULL;
662 }
663
664 static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
665 {
666 struct ast_tone_zone *tz;
667 int created_country = 0;
668 char *res = CLI_SUCCESS;
669
670 switch (cmd) {
671 case CLI_INIT:
672 e->command = "indication add";
673 e->usage =
674 "Usage: indication add <country> <indication> \"<tonelist>\"\n"
675 " Add the given indication to the country.\n";
676 return NULL;
677 case CLI_GENERATE:
678 if (a->pos == 2) {
679 return complete_country(a);
680 } else {
681 return NULL;
682 }
683 }
684
685 if (a->argc != 5) {
686 return CLI_SHOWUSAGE;
687 }
688
689 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
690 /* country does not exist, create it */
691 ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
692
693 if (!(tz = ast_tone_zone_alloc())) {
694 return CLI_FAILURE;
695 }
696
697 ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
698
699 if (ast_register_indication_country(tz)) {
700 ast_log(LOG_WARNING, "Unable to register new country\n");
701 tz = ast_tone_zone_unref(tz);
702 return CLI_FAILURE;
703 }
704
705 created_country = 1;
706 }
707
708 ast_tone_zone_lock(tz);
709
710 if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
711 if (ast_strlen_zero(a->argv[3])) {
712 ast_log(LOG_WARNING, "Unable to register indication %s\n", a->argv[2]);
713 } else {
714 ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
715 }
716 if (created_country) {
717 ast_unregister_indication_country(a->argv[2]);
718 }
719 res = CLI_FAILURE;
720 }
721
722 ast_tone_zone_unlock(tz);
723
724 tz = ast_tone_zone_unref(tz);
725
726 return res;
727 }
728
729 static char *complete_indications(struct ast_cli_args *a)
730 {
731 size_t wordlen;
732 struct ast_tone_zone_sound *ts;
733 struct ast_tone_zone *tz;
734 struct ast_tone_zone tmp_tz = {
735 .nrringcadence = 0,
736 };
737
738 ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
739
740 tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER);
741 if (!tz) {
742 return NULL;
743 }
744
745 wordlen = strlen(a->word);
746
747 ast_tone_zone_lock(tz);
748 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
749 if (!strncasecmp(a->word, ts->name, wordlen)) {
750 if (ast_cli_completion_add(ast_strdup(ts->name))) {
751 break;
752 }
753 }
754 }
755 ast_tone_zone_unlock(tz);
756
757 tz = ast_tone_zone_unref(tz);
758
759 return NULL;
760 }
761
762 static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
763 {
764 struct ast_tone_zone *tz;
765 char *res = CLI_SUCCESS;
766
767 switch (cmd) {
768 case CLI_INIT:
769 e->command = "indication remove";
770 e->usage =
771 "Usage: indication remove <country> [indication]\n"
772 " Remove the given indication from the country.\n";
773 return NULL;
774 case CLI_GENERATE:
775 if (a->pos == 2) {
776 return complete_country(a);
777 }
778 if (a->pos == 3) {
779 return complete_indications(a);
780 }
781 return NULL;
782 }
783
784 if (a->argc != 3 && a->argc != 4) {
785 return CLI_SHOWUSAGE;
786 }
787
788 if (a->argc == 3) {
789 /* remove entire country */
790 if (ast_unregister_indication_country(a->argv[2])) {
791 ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
792 return CLI_FAILURE;
793 }
794
795 return CLI_SUCCESS;
796 }
797
798 if (!(tz = ast_get_indication_zone(a->argv[2]))) {
799 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
800 return CLI_FAILURE;
801 }
802
803 if (ast_unregister_indication(tz, a->argv[3])) {
804 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
805 res = CLI_FAILURE;
806 }
807
808 tz = ast_tone_zone_unref(tz);
809
810 return res;
811 }
812
813 static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
814 {
815 struct ast_tone_zone *tz = NULL;
816 struct ast_str *buf;
817 int found_country = 0;
818 int i;
819
820 switch (cmd) {
821 case CLI_INIT:
822 e->command = "indication show";
823 e->usage =
824 "Usage: indication show [<country> ...]\n"
825 " Display either a condensed summary of all countries and indications, or a\n"
826 " more verbose list of indications for the specified countries.\n";
827 return NULL;
828 case CLI_GENERATE:
829 return complete_country(a);
830 }
831
832 if (a->argc == 2) {
833 struct ao2_iterator iter;
834 /* no arguments, show a list of countries */
835 ast_cli(a->fd, "Country Description\n");
836 ast_cli(a->fd, "===========================\n");
837 iter = ast_tone_zone_iterator_init();
838 while ((tz = ao2_iterator_next(&iter))) {
839 ast_tone_zone_lock(tz);
840 ast_cli(a->fd, "%-7.7s %s\n", tz->country, tz->description);
841 ast_tone_zone_unlock(tz);
842 tz = ast_tone_zone_unref(tz);
843 }
844 ao2_iterator_destroy(&iter);
845
846 ao2_lock(ast_tone_zones);
847 if (default_tone_zone) {
848 ast_cli(a->fd, "===========================\n");
849 tz = ast_tone_zone_ref(default_tone_zone);
850 ast_cli(a->fd, "Default tone zone: %s\n", tz->country);
851 ast_tone_zone_unref(tz);
852 }
853 ao2_unlock(ast_tone_zones);
854 return CLI_SUCCESS;
855 }
856
857 buf = ast_str_alloca(256);
858
859 for (i = 2; i < a->argc; i++) {
860 struct ast_tone_zone zone_arg = {
861 .nrringcadence = 0,
862 };
863 struct ast_tone_zone_sound *ts;
864 int j;
865
866 ast_copy_string(zone_arg.country, a->argv[i], sizeof(zone_arg.country));
867
868 if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
869 continue;
870 }
871
872 if (!found_country) {
873 found_country = 1;
874 ast_cli(a->fd, "Country Indication PlayList\n");
875 ast_cli(a->fd, "=====================================\n");
876 }
877
878 ast_tone_zone_lock(tz);
879
880 ast_str_set(&buf, 0, "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
881 for (j = 0; j < tz->nrringcadence; j++) {
882 ast_str_append(&buf, 0, "%d%s", tz->ringcadence[j],
883 (j == tz->nrringcadence - 1) ? "" : ",");
884 }
885 ast_str_append(&buf, 0, "\n");
886 ast_cli(a->fd, "%s", ast_str_buffer(buf));
887
888 AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
889 ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
890 }
891
892 ast_tone_zone_unlock(tz);
893 tz = ast_tone_zone_unref(tz);
894 }
895
896 if (!found_country) {
897 ast_cli(a->fd, "No countries matched your criteria.\n");
898 }
899
900 return CLI_SUCCESS;
901 }
902
903 static int is_valid_tone_zone(struct ast_tone_zone *zone)
904 {
905 int res;
906
907 ast_tone_zone_lock(zone);
908 res = (!ast_strlen_zero(zone->description) && !AST_LIST_EMPTY(&zone->tones));
909 ast_tone_zone_unlock(zone);
910
911 return res;
912 }
913
914 /*!\brief
915 *
916 * \note This is called with the tone zone locked.
917 */
918 static void store_tone_zone_ring_cadence(struct ast_tone_zone *zone, const char *val)
919 {
920 char buf[1024];
921 char *ring, *c = buf;
922
923 ast_copy_string(buf, val, sizeof(buf));
924
925 while ((ring = strsep(&c, ","))) {
926 int *tmp, value;
927
928 ring = ast_strip(ring);
929
930 if (!isdigit(ring[0]) || (value = atoi(ring)) == -1) {
931 ast_log(LOG_WARNING, "Invalid ringcadence given '%s'.\n", ring);
932 continue;
933 }
934
935 if (!(tmp = ast_realloc(zone->ringcadence, (zone->nrringcadence + 1) * sizeof(int)))) {
936 return;
937 }
938
939 zone->ringcadence = tmp;
940 tmp[zone->nrringcadence] = value;
941 zone->nrringcadence++;
942 }
943 }
944
945 static void store_config_tone_zone(struct ast_tone_zone *zone, const char *var,
946 const char *value)
947 {
948 CV_START(var, value);
949
950 CV_STR("description", zone->description);
951 CV_F("ringcadence", store_tone_zone_ring_cadence(zone, value));
952
953 ast_register_indication(zone, var, value);
954
955 CV_END;
956 }
957
958 static void reset_tone_zone(struct ast_tone_zone *zone)
959 {
960 ast_tone_zone_lock(zone);
961
962 zone->killme = 0;
963
964 if (zone->nrringcadence) {
965 zone->nrringcadence = 0;
966 ast_free(zone->ringcadence);
967 zone->ringcadence = NULL;
968 }
969
970 ast_tone_zone_unlock(zone);
971 }
972
973 static int parse_tone_zone(struct ast_config *cfg, const char *country)
974 {
975 struct ast_variable *v;
976 struct ast_tone_zone *zone;
977 struct ast_tone_zone tmp_zone = {
978 .nrringcadence = 0,
979 };
980 int allocd = 0;
981
982 ast_copy_string(tmp_zone.country, country, sizeof(tmp_zone.country));
983
984 if ((zone = ao2_find(ast_tone_zones, &tmp_zone, OBJ_POINTER))) {
985 reset_tone_zone(zone);
986 } else if ((zone = ast_tone_zone_alloc())) {
987 allocd = 1;
988 ast_copy_string(zone->country, country, sizeof(zone->country));
989 } else {
990 return -1;
991 }
992
993 ast_tone_zone_lock(zone);
994 for (v = ast_variable_browse(cfg, country); v; v = v->next) {
995 store_config_tone_zone(zone, v->name, v->value);
996 }
997 ast_tone_zone_unlock(zone);
998
999 if (allocd) {
1000 if (!is_valid_tone_zone(zone)) {
1001 ast_log(LOG_WARNING, "Indication country '%s' is invalid\n", country);
1002 } else if (ast_register_indication_country(zone)) {
1003 ast_log(LOG_WARNING, "Unable to register indication country '%s'.\n",
1004 country);
1005 }
1006 }
1007
1008 zone = ast_tone_zone_unref(zone);
1009
1010 return 0;
1011 }
1012
1013 /*! \brief
1014 * Mark the zone and its tones before parsing configuration. We will use this
1015 * to know what to remove after configuration is parsed.
1016 */
1017 static int tone_zone_mark(void *obj, void *arg, int flags)
1018 {
1019 struct ast_tone_zone *zone = obj;
1020 struct ast_tone_zone_sound *s;
1021
1022 ast_tone_zone_lock(zone);
1023
1024 zone->killme = 1;
1025
1026 AST_LIST_TRAVERSE(&zone->tones, s, entry) {
1027 s->killme = 1;
1028 }
1029
1030 ast_tone_zone_unlock(zone);
1031
1032 return 0;
1033 }
1034
1035 /*! \brief
1036 * Prune tones no longer in the configuration, and have the tone zone unlinked
1037 * if it is no longer in the configuration at all.
1038 */
1039 static int prune_tone_zone(void *obj, void *arg, int flags)
1040 {
1041 struct ast_tone_zone *zone = obj;
1042 struct ast_tone_zone_sound *s;
1043
1044 ast_tone_zone_lock(zone);
1045
1046 AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, s, entry) {
1047 if (s->killme) {
1048 AST_LIST_REMOVE_CURRENT(entry);
1049 s = ast_tone_zone_sound_unref(s);
1050 }
1051 }
1052 AST_LIST_TRAVERSE_SAFE_END;
1053
1054 ast_tone_zone_unlock(zone);
1055
1056 return zone->killme ? CMP_MATCH : 0;
1057 }
1058
1059 /*! \brief load indications module */
1060 static int load_indications(int reload)
1061 {
1062 struct ast_config *cfg;
1063 const char *cxt = NULL;
1064 const char *country = NULL;
1065 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1066 int res = -1;
1067
1068 cfg = ast_config_load2(config, "indications", config_flags);
1069
1070 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
1071 ast_log(LOG_WARNING, "Can't find indications config file %s.\n", config);
1072 return 0;
1073 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
1074 return 0;
1075 }
1076
1077 /* Lock the container to prevent multiple simultaneous reloads */
1078 ao2_lock(ast_tone_zones);
1079
1080 ao2_callback(ast_tone_zones, OBJ_NODATA, tone_zone_mark, NULL);
1081
1082 /* Use existing config to populate the Indication table */
1083 while ((cxt = ast_category_browse(cfg, cxt))) {
1084 /* All categories but "general" are considered countries */
1085 if (!strcasecmp(cxt, "general")) {
1086 continue;
1087 }
1088
1089 if (parse_tone_zone(cfg, cxt)) {
1090 goto return_cleanup;
1091 }
1092 }
1093
1094 ao2_callback(ast_tone_zones, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK,
1095 prune_tone_zone, NULL);
1096
1097 /* determine which country is the default */
1098 country = ast_variable_retrieve(cfg, "general", "country");
1099 if (!ast_strlen_zero(country)) {
1100 ast_log(LOG_NOTICE, "Default country for indication tones: %s\n", country);
1101 if (ast_set_indication_country(country)) {
1102 ast_log(LOG_WARNING, "Unable to set the default country (for indication tones)\n");
1103 }
1104 } else {
1105 ast_log(LOG_WARNING, "Missing default country (for indication tones)\n");
1106 }
1107
1108 res = 0;
1109
1110 return_cleanup:
1111 ao2_unlock(ast_tone_zones);
1112 ast_config_destroy(cfg);
1113
1114 return res;
1115 }
1116
1117 /*! \brief CLI entries for commands provided by this module */
1118 static struct ast_cli_entry cli_indications[] = {
1119 AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
1120 AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
1121 AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
1122 };
1123
1124 static int ast_tone_zone_hash(const void *obj, const int flags)
1125 {
1126 const struct ast_tone_zone *zone = obj;
1127
1128 return ast_str_case_hash(zone->country);
1129 }
1130
1131 static int ast_tone_zone_cmp(void *obj, void *arg, int flags)
1132 {
1133 struct ast_tone_zone *zone = obj;
1134 struct ast_tone_zone *zone_arg = arg;
1135
1136 return (!strcasecmp(zone->country, zone_arg->country)) ?
1137 CMP_MATCH | CMP_STOP : 0;
1138 }
1139
1140 /*!
1141 * \internal
1142 * \brief Clean up resources on Asterisk shutdown
1143 */
1144 static int unload_module(void)
1145 {
1146 ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
1147 if (default_tone_zone) {
1148 ast_tone_zone_unref(default_tone_zone);
1149 default_tone_zone = NULL;
1150 }
1151 if (ast_tone_zones) {
1152 ao2_ref(ast_tone_zones, -1);
1153 ast_tone_zones = NULL;
1154 }
1155
1156 return 0;
1157 }
1158
1159 /*! \brief Load indications module */
1160 static int load_module(void)
1161 {
1162 ast_tone_zones = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1163 NUM_TONE_ZONE_BUCKETS, ast_tone_zone_hash, NULL, ast_tone_zone_cmp);
1164 if (!ast_tone_zones) {
1165 return AST_MODULE_LOAD_FAILURE;
1166 }
1167
1168 if (load_indications(0)) {
1169 return AST_MODULE_LOAD_FAILURE;
1170 }
1171
1172 ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
1173
1174 return AST_MODULE_LOAD_SUCCESS;
1175 }
1176
1177 /*! \brief Reload indications module */
1178 static int reload_module(void)
1179 {
1180 return load_indications(1);
1181 }
1182
1183 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Indication Tone Handling",
1184 .support_level = AST_MODULE_SUPPORT_CORE,
1185 .load = load_module,
1186 .unload = unload_module,
1187 .reload = reload_module,
1188 .load_pri = AST_MODPRI_CORE,
1189 .requires = "extconfig",
1190 );