Start untangling header inclusion in a way that does not affect
[asterisk/asterisk.git] / main / indications.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2002, Pauline Middelink
5  *
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 /*! \file
19  *
20  * \brief Tone Management
21  * 
22  * \author Pauline Middelink <middelink@polyware.nl>
23  *
24  * This set of function allow us to play a list of tones on a channel.
25  * Each element has two frequencies, which are mixed together and a
26  * duration. For silence both frequencies can be set to 0.
27  * The playtones can be given as a comma separated string.
28  *
29  */
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <math.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/linkedlists.h"
39 #include "asterisk/indications.h"
40 #include "asterisk/frame.h"
41 #include "asterisk/options.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/utils.h"
45
46 static int midi_tohz[128] = {
47                         8,8,9,9,10,10,11,12,12,13,14,
48                         15,16,17,18,19,20,21,23,24,25,
49                         27,29,30,32,34,36,38,41,43,46,
50                         48,51,55,58,61,65,69,73,77,82,
51                         87,92,97,103,110,116,123,130,138,146,
52                         155,164,174,184,195,207,220,233,246,261,
53                         277,293,311,329,349,369,391,415,440,466,
54                         493,523,554,587,622,659,698,739,783,830,
55                         880,932,987,1046,1108,1174,1244,1318,1396,1479,
56                         1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,
57                         2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,
58                         4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,
59                         8869,9397,9956,10548,11175,11839,12543
60                         };
61
62 struct playtones_item {
63         int fac1;
64         int init_v2_1;
65         int init_v3_1;
66         int fac2;
67         int init_v2_2;
68         int init_v3_2;
69         int modulate;
70         int duration;
71 };
72
73 struct playtones_def {
74         int vol;
75         int reppos;
76         int nitems;
77         int interruptible;
78         struct playtones_item *items;
79 };
80
81 struct playtones_state {
82         int vol;
83         int v1_1;
84         int v2_1;
85         int v3_1;
86         int v1_2;
87         int v2_2;
88         int v3_2;
89         int reppos;
90         int nitems;
91         struct playtones_item *items;
92         int npos;
93         int oldnpos;
94         int pos;
95         int origwfmt;
96         struct ast_frame f;
97         unsigned char offset[AST_FRIENDLY_OFFSET];
98         short data[4000];
99 };
100
101 static void playtones_release(struct ast_channel *chan, void *params)
102 {
103         struct playtones_state *ps = params;
104
105         if (chan)
106                 ast_set_write_format(chan, ps->origwfmt);
107         if (ps->items)
108                 ast_free(ps->items);
109
110         ast_free(ps);
111 }
112
113 static void * playtones_alloc(struct ast_channel *chan, void *params)
114 {
115         struct playtones_def *pd = params;
116         struct playtones_state *ps = NULL;
117
118         if (!(ps = ast_calloc(1, sizeof(*ps))))
119                 return NULL;
120
121         ps->origwfmt = chan->writeformat;
122
123         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
124                 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
125                 playtones_release(NULL, ps);
126                 ps = NULL;
127         } else {
128                 ps->vol = pd->vol;
129                 ps->reppos = pd->reppos;
130                 ps->nitems = pd->nitems;
131                 ps->items = pd->items;
132                 ps->oldnpos = -1;
133         }
134
135         /* Let interrupts interrupt :) */
136         if (pd->interruptible)
137                 ast_set_flag(chan, AST_FLAG_WRITE_INT);
138         else
139                 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
140
141         return ps;
142 }
143
144 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
145 {
146         struct playtones_state *ps = data;
147         struct playtones_item *pi;
148         int x;
149         /* we need to prepare a frame with 16 * timelen samples as we're 
150          * generating SLIN audio
151          */
152         len = samples * 2;
153         if (len > sizeof(ps->data) / 2 - 1) {
154                 ast_log(LOG_WARNING, "Can't generate that much data!\n");
155                 return -1;
156         }
157         memset(&ps->f, 0, sizeof(ps->f));
158
159         pi = &ps->items[ps->npos];
160         if (ps->oldnpos != ps->npos) {
161                 /* Load new parameters */
162                 ps->v1_1 = 0;
163                 ps->v2_1 = pi->init_v2_1;
164                 ps->v3_1 = pi->init_v3_1;
165                 ps->v1_2 = 0;
166                 ps->v2_2 = pi->init_v2_2;
167                 ps->v3_2 = pi->init_v3_2;
168                 ps->oldnpos = ps->npos;
169         }
170         for (x=0;x<len/2;x++) {
171                 ps->v1_1 = ps->v2_1;
172                 ps->v2_1 = ps->v3_1;
173                 ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
174                 
175                 ps->v1_2 = ps->v2_2;
176                 ps->v2_2 = ps->v3_2;
177                 ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
178                 if (pi->modulate) {
179                         int p;
180                         p = ps->v3_2 - 32768;
181                         if (p < 0) p = -p;
182                         p = ((p * 9) / 10) + 1;
183                         ps->data[x] = (ps->v3_1 * p) >> 15;
184                 } else
185                         ps->data[x] = ps->v3_1 + ps->v3_2; 
186         }
187         
188         ps->f.frametype = AST_FRAME_VOICE;
189         ps->f.subclass = AST_FORMAT_SLINEAR;
190         ps->f.datalen = len;
191         ps->f.samples = samples;
192         ps->f.offset = AST_FRIENDLY_OFFSET;
193         ps->f.data = ps->data;
194         ps->f.delivery.tv_sec = 0;
195         ps->f.delivery.tv_usec = 0;
196         ast_write(chan, &ps->f);
197
198         ps->pos += x;
199         if (pi->duration && ps->pos >= pi->duration * 8) {      /* item finished? */
200                 ps->pos = 0;                                    /* start new item */
201                 ps->npos++;
202                 if (ps->npos >= ps->nitems) {                   /* last item? */
203                         if (ps->reppos == -1)                   /* repeat set? */
204                                 return -1;
205                         ps->npos = ps->reppos;                  /* redo from top */
206                 }
207         }
208         return 0;
209 }
210
211 static struct ast_generator playtones = {
212         alloc: playtones_alloc,
213         release: playtones_release,
214         generate: playtones_generator,
215 };
216
217 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
218 {
219         char *s, *data = ast_strdupa(playlst); /* cute */
220         struct playtones_def d = { vol, -1, 0, 1, NULL};
221         char *stringp;
222         char *separator;
223         
224         if (vol < 1)
225                 d.vol = 7219; /* Default to -8db */
226
227         d.interruptible = interruptible;
228         
229         stringp=data;
230         /* the stringp/data is not null here */
231         /* check if the data is separated with '|' or with ',' by default */
232         if (strchr(stringp,'|'))
233                 separator = "|";
234         else
235                 separator = ",";
236         s = strsep(&stringp,separator);
237         while (s && *s) {
238                 int freq1, freq2, time, modulate=0, midinote=0;
239
240                 if (s[0]=='!')
241                         s++;
242                 else if (d.reppos == -1)
243                         d.reppos = d.nitems;
244                 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
245                         /* f1+f2/time format */
246                 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
247                         /* f1+f2 format */
248                         time = 0;
249                 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
250                         /* f1*f2/time format */
251                         modulate = 1;
252                 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
253                         /* f1*f2 format */
254                         time = 0;
255                         modulate = 1;
256                 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
257                         /* f1/time format */
258                         freq2 = 0;
259                 } else if (sscanf(s, "%d", &freq1) == 1) {
260                         /* f1 format */
261                         freq2 = 0;
262                         time = 0;
263                 } else if (sscanf(s, "M%d+M%d/%d", &freq1, &freq2, &time) == 3) {
264                         /* Mf1+Mf2/time format */
265                         midinote = 1;
266                 } else if (sscanf(s, "M%d+M%d", &freq1, &freq2) == 2) {
267                         /* Mf1+Mf2 format */
268                         time = 0;
269                         midinote = 1;
270                 } else if (sscanf(s, "M%d*M%d/%d", &freq1, &freq2, &time) == 3) {
271                         /* Mf1*Mf2/time format */
272                         modulate = 1;
273                         midinote = 1;
274                 } else if (sscanf(s, "M%d*M%d", &freq1, &freq2) == 2) {
275                         /* Mf1*Mf2 format */
276                         time = 0;
277                         modulate = 1;
278                         midinote = 1;
279                 } else if (sscanf(s, "M%d/%d", &freq1, &time) == 2) {
280                         /* Mf1/time format */
281                         freq2 = -1;
282                         midinote = 1;
283                 } else if (sscanf(s, "M%d", &freq1) == 1) {
284                         /* Mf1 format */
285                         freq2 = -1;
286                         time = 0;
287                         midinote = 1;
288                 } else {
289                         ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
290                         return -1;
291                 }
292
293                 if (midinote) {
294                         /* midi notes must be between 0 and 127 */
295                         if ((freq1 >= 0) && (freq1 <= 127))
296                                 freq1 = midi_tohz[freq1];
297                         else
298                                 freq1 = 0;
299
300                         if ((freq2 >= 0) && (freq2 <= 127))
301                                 freq2 = midi_tohz[freq2];
302                         else
303                                 freq2 = 0;
304                 }
305
306                 if (!(d.items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items)))) {
307                         return -1;
308                 }
309                 d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (freq1 / 8000.0)) * 32768.0;
310                 d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (freq1 / 8000.0)) * d.vol;
311                 d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (freq1 / 8000.0)) * d.vol;
312
313                 d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (freq2 / 8000.0)) * 32768.0;
314                 d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (freq2 / 8000.0)) * d.vol;
315                 d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (freq2 / 8000.0)) * d.vol;
316                 d.items[d.nitems].duration = time;
317                 d.items[d.nitems].modulate = modulate;
318                 d.nitems++;
319
320                 s = strsep(&stringp,separator);
321         }
322
323         if (ast_activate_generator(chan, &playtones, &d)) {
324                 ast_free(d.items);
325                 return -1;
326         }
327         return 0;
328 }
329
330 void ast_playtones_stop(struct ast_channel *chan)
331 {
332         ast_deactivate_generator(chan);
333 }
334
335 /*--------------------------------------------*/
336
337 static AST_RWLIST_HEAD_STATIC(tone_zones, ind_tone_zone);
338 static struct ind_tone_zone *current_tonezone;
339
340 struct ind_tone_zone *ast_walk_indications(const struct ind_tone_zone *cur)
341 {
342         struct ind_tone_zone *tz = NULL;
343
344         AST_RWLIST_RDLOCK(&tone_zones);
345         /* If cur is not NULL, then we have to iterate through - otherwise just return the first entry */
346         if (cur) {
347                 AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
348                         if (tz == cur)
349                                 break;
350                 }
351                 tz = AST_RWLIST_NEXT(tz, list);
352         } else {
353                 tz = AST_RWLIST_FIRST(&tone_zones);
354         }
355         AST_RWLIST_UNLOCK(&tone_zones);
356
357         return tz;
358 }
359
360 /* Set global indication country */
361 int ast_set_indication_country(const char *country)
362 {
363         struct ind_tone_zone *zone = NULL;
364
365         /* If no country is specified or we are unable to find the zone, then return not found */
366         if (!country || !(zone = ast_get_indication_zone(country)))
367                 return 1;
368         
369         ast_verb(3, "Setting default indication country to '%s'\n", country);
370
371         /* Protect the current tonezone using the tone_zones lock as well */
372         AST_RWLIST_WRLOCK(&tone_zones);
373         current_tonezone = zone;
374         AST_RWLIST_UNLOCK(&tone_zones);
375
376         /* Zone was found */
377         return 0;
378 }
379
380 /* locate tone_zone, given the country. if country == NULL, use the default country */
381 struct ind_tone_zone *ast_get_indication_zone(const char *country)
382 {
383         struct ind_tone_zone *tz = NULL;
384         int alias_loop = 0;
385
386         AST_RWLIST_RDLOCK(&tone_zones);
387
388         if (!country) {
389                 if (current_tonezone)
390                         tz = current_tonezone;
391                 else
392                         tz = AST_LIST_FIRST(&tone_zones);
393         } else {
394                 do {
395                         AST_RWLIST_TRAVERSE(&tone_zones, tz, list) {
396                                 if (!strcasecmp(tz->country, country))
397                                         break;
398                         }
399                         if (!tz)
400                                 break;
401                         /* If this is an alias then we have to search yet again otherwise we have found the zonezone */
402                         if (tz->alias && tz->alias[0])
403                                 country = tz->alias;
404                         else
405                                 break;
406                 } while ((++alias_loop < 20) && tz);
407         }
408
409         AST_RWLIST_UNLOCK(&tone_zones);
410
411         /* If we reached the maximum loops to find the proper country via alias, print out a notice */
412         if (alias_loop == 20)
413                 ast_log(LOG_NOTICE, "Alias loop for '%s' is bonkers\n", country);
414
415         return tz;
416 }
417
418 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
419 struct ind_tone_zone_sound *ast_get_indication_tone(const struct ind_tone_zone *zone, const char *indication)
420 {
421         struct ind_tone_zone_sound *ts = NULL;
422
423         AST_RWLIST_RDLOCK(&tone_zones);
424
425         /* If no zone is already specified we need to try to pick one */
426         if (!zone) {
427                 if (current_tonezone) {
428                         zone = current_tonezone;
429                 } else if (!(zone = AST_LIST_FIRST(&tone_zones))) {
430                         /* No zone has been found ;( */
431                         AST_RWLIST_UNLOCK(&tone_zones);
432                         return NULL;
433                 }
434         }
435
436         /* Look through list of tones in the zone searching for the right one */
437         for (ts = zone->tones; ts; ts = ts->next) {
438                 if (!strcasecmp(ts->name, indication))
439                         break;
440         }
441
442         AST_RWLIST_UNLOCK(&tone_zones);
443
444         return ts;
445 }
446
447 /* helper function to delete a tone_zone in its entirety */
448 static inline void free_zone(struct ind_tone_zone* zone)
449 {
450         while (zone->tones) {
451                 struct ind_tone_zone_sound *tmp = zone->tones->next;
452                 ast_free((void *)zone->tones->name);
453                 ast_free((void *)zone->tones->data);
454                 ast_free(zone->tones);
455                 zone->tones = tmp;
456         }
457
458         if (zone->ringcadence)
459                 ast_free(zone->ringcadence);
460
461         ast_free(zone);
462 }
463
464 /*--------------------------------------------*/
465
466 /* add a new country, if country exists, it will be replaced. */
467 int ast_register_indication_country(struct ind_tone_zone *zone)
468 {
469         struct ind_tone_zone *tz = NULL;
470
471         AST_RWLIST_WRLOCK(&tone_zones);
472         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
473                 /* If this is not the same zone, then just continue to the next entry */
474                 if (strcasecmp(zone->country, tz->country))
475                         continue;
476                 /* If this zone we are going to remove is the current default then make the new zone the default */
477                 if (tz == current_tonezone)
478                         current_tonezone = zone;
479                 /* Remove from the linked list */
480                 AST_RWLIST_REMOVE_CURRENT(list);
481                 /* Finally free the zone itself */
482                 free_zone(tz);
483                 break;
484         }
485         AST_RWLIST_TRAVERSE_SAFE_END;
486
487         /* Add zone to the list */
488         AST_RWLIST_INSERT_TAIL(&tone_zones, zone, list);
489
490         /* It's all over. */
491         AST_RWLIST_UNLOCK(&tone_zones);
492
493         ast_verb(3, "Registered indication country '%s'\n", zone->country);
494
495         return 0;
496 }
497
498 /* remove an existing country and all its indications, country must exist.
499  * Also, all countries which are an alias for the specified country are removed. */
500 int ast_unregister_indication_country(const char *country)
501 {
502         struct ind_tone_zone *tz = NULL;
503         int res = -1;
504
505         AST_RWLIST_WRLOCK(&tone_zones);
506         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&tone_zones, tz, list) {
507                 if (country && (strcasecmp(country, tz->country) && strcasecmp(country, tz->alias)))
508                         continue;
509                 /* If this tonezone is the current default then unset it */
510                 if (tz == current_tonezone) {
511                         ast_log(LOG_NOTICE,"Removed default indication country '%s'\n", tz->country);
512                         current_tonezone = NULL;
513                 }
514                 /* Remove from the list */
515                 AST_RWLIST_REMOVE_CURRENT(list);
516                 ast_verb(3, "Unregistered indication country '%s'\n", tz->country);
517                 free_zone(tz);
518                 res = 0;
519         }
520         AST_RWLIST_TRAVERSE_SAFE_END;
521         AST_RWLIST_UNLOCK(&tone_zones);
522
523         return res;
524 }
525
526 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
527  * exists, it will be replaced. */
528 int ast_register_indication(struct ind_tone_zone *zone, const char *indication, const char *tonelist)
529 {
530         struct ind_tone_zone_sound *ts, *ps;
531
532         /* is it an alias? stop */
533         if (zone->alias[0])
534                 return -1;
535
536         AST_RWLIST_WRLOCK(&tone_zones);
537         for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
538                 if (strcasecmp(indication,ts->name)==0) {
539                         /* indication already there, replace */
540                         ast_free((void*)ts->name);
541                         ast_free((void*)ts->data);
542                         break;
543                 }
544         }
545         if (!ts) {
546                 /* not there, we have to add */
547                 if (!(ts = ast_malloc(sizeof(*ts)))) {
548                         AST_RWLIST_UNLOCK(&tone_zones);
549                         return -2;
550                 }
551                 ts->next = NULL;
552         }
553         if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
554                 AST_RWLIST_UNLOCK(&tone_zones);
555                 return -2;
556         }
557         if (ps)
558                 ps->next = ts;
559         else
560                 zone->tones = ts;
561         AST_RWLIST_UNLOCK(&tone_zones);
562         return 0;
563 }
564
565 /* remove an existing country's indication. Both country and indication must exist */
566 int ast_unregister_indication(struct ind_tone_zone *zone, const char *indication)
567 {
568         struct ind_tone_zone_sound *ts,*ps = NULL, *tmp;
569         int res = -1;
570
571         /* is it an alias? stop */
572         if (zone->alias[0])
573                 return -1;
574
575         AST_RWLIST_WRLOCK(&tone_zones);
576         ts = zone->tones;
577         while (ts) {
578                 if (strcasecmp(indication,ts->name)==0) {
579                         /* indication found */
580                         tmp = ts->next;
581                         if (ps)
582                                 ps->next = tmp;
583                         else
584                                 zone->tones = tmp;
585                         ast_free((void*)ts->name);
586                         ast_free((void*)ts->data);
587                         ast_free(ts);
588                         ts = tmp;
589                         res = 0;
590                 }
591                 else {
592                         /* next zone please */
593                         ps = ts;
594                         ts = ts->next;
595                 }
596         }
597         /* indication not found, goodbye */
598         AST_RWLIST_UNLOCK(&tone_zones);
599         return res;
600 }