Revert Jim's earlier "fix" :)
[asterisk/asterisk.git] / indications.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Tone Management
5  * 
6  * Copyright (C) 2002, Pauline Middelink
7  *
8  * Pauline Middelink <middelink@polyware.nl>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  *
13  * This set of function allow us to play a list of tones on a channel.
14  * Each element has two frequencies, which are mixed together and a
15  * duration. For silence both frequencies can be set to 0.
16  * The playtones can be given as a comma seperated string.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>                       /* For PI */
23 #include <asterisk/indications.h>
24 #include <asterisk/frame.h>
25 #include <asterisk/options.h>
26 #include <asterisk/channel.h>
27 #include <asterisk/logger.h>
28 #include <asterisk/lock.h>
29 #include <asterisk/utils.h>
30
31 struct playtones_item {
32         int freq1;
33         int freq2;
34         int duration;
35         int modulate;
36 };
37
38 struct playtones_def {
39         int vol;
40         int reppos;
41         int nitems;
42         int interruptible;
43         struct playtones_item *items;
44 };
45
46 struct playtones_state {
47         int vol;
48         int reppos;
49         int nitems;
50         struct playtones_item *items;
51         int npos;
52         int pos;
53         int origwfmt;
54         struct ast_frame f;
55         unsigned char offset[AST_FRIENDLY_OFFSET];
56         short data[4000];
57 };
58
59 static void playtones_release(struct ast_channel *chan, void *params)
60 {
61         struct playtones_state *ps = params;
62         if (chan) {
63                 ast_set_write_format(chan, ps->origwfmt);
64         }
65         if (ps->items) free(ps->items);
66         free(ps);
67 }
68
69 static void * playtones_alloc(struct ast_channel *chan, void *params)
70 {
71         struct playtones_def *pd = params;
72         struct playtones_state *ps = malloc(sizeof(struct playtones_state));
73         if (!ps)
74                 return NULL;
75         memset(ps, 0, sizeof(struct playtones_state));
76         ps->origwfmt = chan->writeformat;
77         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
78                 ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
79                 playtones_release(NULL, ps);
80                 ps = NULL;
81         } else {
82                 ps->vol = pd->vol;
83                 ps->reppos = pd->reppos;
84                 ps->nitems = pd->nitems;
85                 ps->items = pd->items;
86         }
87         /* Let interrupts interrupt :) */
88         if (pd->interruptible)
89                 ast_set_flag(chan, AST_FLAG_WRITE_INT);
90         else
91                 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
92         return ps;
93 }
94
95 static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
96 {
97         struct playtones_state *ps = data;
98         struct playtones_item *pi;
99         int x;
100         /* we need to prepare a frame with 16 * timelen samples as we're 
101          * generating SLIN audio
102          */
103         len = samples * 2;
104         if (len > sizeof(ps->data) / 2 - 1) {
105                 ast_log(LOG_WARNING, "Can't generate that much data!\n");
106                 return -1;
107         }
108         memset(&ps->f, 0, sizeof(ps->f));
109
110         pi = &ps->items[ps->npos];
111         for (x=0;x<len/2;x++) {
112                 if (pi->modulate)
113                 /* Modulate 1st tone with 2nd, to 90% modulation depth */
114                 ps->data[x] = ps->vol * 2 * (
115                         sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) *
116                         (0.9 * fabs(sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))) + 0.1)
117                         );
118                 else
119                         /* Add 2 tones together */
120                         ps->data[x] = ps->vol * (
121                                 sin((pi->freq1 * 2.0 * M_PI / 8000.0) * (ps->pos + x)) +
122                                 sin((pi->freq2 * 2.0 * M_PI / 8000.0) * (ps->pos + x))
123                         );
124         }
125         ps->f.frametype = AST_FRAME_VOICE;
126         ps->f.subclass = AST_FORMAT_SLINEAR;
127         ps->f.datalen = len;
128         ps->f.samples = samples;
129         ps->f.offset = AST_FRIENDLY_OFFSET;
130         ps->f.data = ps->data;
131         ps->f.delivery.tv_sec = 0;
132         ps->f.delivery.tv_usec = 0;
133         ast_write(chan, &ps->f);
134
135         ps->pos += x;
136         if (pi->duration && ps->pos >= pi->duration * 8) {      /* item finished? */
137                 ps->pos = 0;                                    /* start new item */
138                 ps->npos++;
139                 if (ps->npos >= ps->nitems) {                   /* last item? */
140                         if (ps->reppos == -1)                   /* repeat set? */
141                                 return -1;
142                         ps->npos = ps->reppos;                  /* redo from top */
143                 }
144         }
145         return 0;
146 }
147
148 static struct ast_generator playtones = {
149         alloc: playtones_alloc,
150         release: playtones_release,
151         generate: playtones_generator,
152 };
153
154 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
155 {
156         char *s, *data = ast_strdupa(playlst); /* cute */
157         struct playtones_def d = { vol, -1, 0, 1, NULL};
158         char *stringp=NULL;
159         char *separator;
160         if (!data)
161                 return -1;
162         if (vol < 1)
163                 d.vol = 8192;
164
165         d.interruptible = interruptible;
166         
167         stringp=data;
168         /* the stringp/data is not null here */
169         /* check if the data is separated with '|' or with ',' by default */
170         if (strchr(stringp,'|'))
171                 separator = "|";
172         else
173                 separator = ",";
174         s = strsep(&stringp,separator);
175         while (s && *s) {
176                 int freq1, freq2, time, modulate=0;
177
178                 if (s[0]=='!')
179                         s++;
180                 else if (d.reppos == -1)
181                         d.reppos = d.nitems;
182                 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
183                         /* f1+f2/time format */
184                 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
185                         /* f1+f2 format */
186                         time = 0;
187                 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
188                         /* f1*f2/time format */
189                         modulate = 1;
190                 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
191                         /* f1*f2 format */
192                         time = 0;
193                         modulate = 1;
194                 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
195                         /* f1/time format */
196                         freq2 = 0;
197                 } else if (sscanf(s, "%d", &freq1) == 1) {
198                         /* f1 format */
199                         freq2 = 0;
200                         time = 0;
201                 } else {
202                         ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
203                         return -1;
204                 }
205
206                 d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
207                 if (d.items == NULL)
208                         return -1;
209                 d.items[d.nitems].freq1    = freq1;
210                 d.items[d.nitems].freq2    = freq2;
211                 d.items[d.nitems].duration = time;
212                 d.items[d.nitems].modulate = modulate;
213                 d.nitems++;
214
215                 s = strsep(&stringp,separator);
216         }
217
218         if (ast_activate_generator(chan, &playtones, &d)) {
219                 free(d.items);
220                 return -1;
221         }
222         return 0;
223 }
224
225 void ast_playtones_stop(struct ast_channel *chan)
226 {
227         ast_deactivate_generator(chan);
228 }
229
230 /*--------------------------------------------*/
231
232 struct tone_zone *tone_zones;
233 static struct tone_zone *current_tonezone;
234
235 /* Protect the tone_zones list (highly unlikely that two things would change
236  * it at the same time, but still! */
237 AST_MUTEX_DEFINE_EXPORTED(tzlock);
238
239 /* Set global indication country */
240 int ast_set_indication_country(const char *country)
241 {
242         if (country) {
243                 struct tone_zone *z = ast_get_indication_zone(country);
244                 if (z) {
245                         if (option_verbose > 2)
246                                 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
247                         current_tonezone = z;
248                         return 0;
249                 }
250         }
251         return 1; /* not found */
252 }
253
254 /* locate tone_zone, given the country. if country == NULL, use the default country */
255 struct tone_zone *ast_get_indication_zone(const char *country)
256 {
257         struct tone_zone *tz;
258         int alias_loop = 0;
259
260         /* we need some tonezone, pick the first */
261         if (country == NULL && current_tonezone)
262                 return current_tonezone;        /* default country? */
263         if (country == NULL && tone_zones)
264                 return tone_zones;              /* any country? */
265         if (country == NULL)
266                 return 0;       /* not a single country insight */
267
268         if (ast_mutex_lock(&tzlock)) {
269                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
270                 return 0;
271         }
272         do {
273                 for (tz=tone_zones; tz; tz=tz->next) {
274                         if (strcasecmp(country,tz->country)==0) {
275                                 /* tone_zone found */
276                                 if (tz->alias && tz->alias[0]) {
277                                         country = tz->alias;
278                                         break;
279                                 }
280                                 ast_mutex_unlock(&tzlock);
281                                 return tz;
282                         }
283                 }
284         } while (++alias_loop<20 && tz);
285         ast_mutex_unlock(&tzlock);
286         if (alias_loop==20)
287                 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
288         /* nothing found, sorry */
289         return 0;
290 }
291
292 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
293 struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
294 {
295         struct tone_zone_sound *ts;
296
297         /* we need some tonezone, pick the first */
298         if (zone == NULL && current_tonezone)
299                 zone = current_tonezone;        /* default country? */
300         if (zone == NULL && tone_zones)
301                 zone = tone_zones;              /* any country? */
302         if (zone == NULL)
303                 return 0;       /* not a single country insight */
304
305         if (ast_mutex_lock(&tzlock)) {
306                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
307                 return 0;
308         }
309         for (ts=zone->tones; ts; ts=ts->next) {
310                 if (strcasecmp(indication,ts->name)==0) {
311                         /* found indication! */
312                         ast_mutex_unlock(&tzlock);
313                         return ts;
314                 }
315         }
316         /* nothing found, sorry */
317         ast_mutex_unlock(&tzlock);
318         return 0;
319 }
320
321 /* helper function to delete a tone_zone in its entirety */
322 static inline void free_zone(struct tone_zone* zone)
323 {
324         while (zone->tones) {
325                 struct tone_zone_sound *tmp = zone->tones->next;
326                 free((void*)zone->tones->name);
327                 free((void*)zone->tones->data);
328                 free(zone->tones);
329                 zone->tones = tmp;
330         }
331         if (zone->ringcadance)
332                 free((void*)zone->ringcadance);
333         free(zone);
334 }
335
336 /*--------------------------------------------*/
337
338 /* add a new country, if country exists, it will be replaced. */
339 int ast_register_indication_country(struct tone_zone *zone)
340 {
341         struct tone_zone *tz,*pz;
342
343         if (ast_mutex_lock(&tzlock)) {
344                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
345                 return -1;
346         }
347         for (pz=NULL,tz=tone_zones; tz; pz=tz,tz=tz->next) {
348                 if (strcasecmp(zone->country,tz->country)==0) {
349                         /* tone_zone already there, replace */
350                         zone->next = tz->next;
351                         if (pz)
352                                 pz->next = zone;
353                         else
354                                 tone_zones = zone;
355                         /* if we are replacing the default zone, re-point it */
356                         if (tz == current_tonezone)
357                                 current_tonezone = zone;
358                         /* now free the previous zone */
359                         free_zone(tz);
360                         ast_mutex_unlock(&tzlock);
361                         return 0;
362                 }
363         }
364         /* country not there, add */
365         zone->next = NULL;
366         if (pz)
367                 pz->next = zone;
368         else
369                 tone_zones = zone;
370         ast_mutex_unlock(&tzlock);
371
372         if (option_verbose > 2)
373                 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
374         return 0;
375 }
376
377 /* remove an existing country and all its indications, country must exist.
378  * Also, all countries which are an alias for the specified country are removed. */
379 int ast_unregister_indication_country(const char *country)
380 {
381         struct tone_zone *tz, *pz = NULL, *tmp;
382         int res = -1;
383
384         if (ast_mutex_lock(&tzlock)) {
385                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
386                 return -1;
387         }
388         tz = tone_zones;
389         while (tz) {
390                 if (country==NULL ||
391                     (strcasecmp(country, tz->country)==0 ||
392                      strcasecmp(country, tz->alias)==0)) {
393                         /* tone_zone found, remove */
394                         tmp = tz->next;
395                         if (pz)
396                                 pz->next = tmp;
397                         else
398                                 tone_zones = tmp;
399                         /* if we are unregistering the default country, w'll notice */
400                         if (tz == current_tonezone) {
401                                 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n",tz->country);
402                                 current_tonezone = NULL;
403                         }
404                         if (option_verbose > 2)
405                                 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
406                         free_zone(tz);
407                         if (tone_zones == tz)
408                                 tone_zones = tmp;
409                         tz = tmp;
410                         res = 0;
411                 }
412                 else {
413                         /* next zone please */
414                         pz = tz;
415                         tz = tz->next;
416                 }
417         }
418         ast_mutex_unlock(&tzlock);
419         return res;
420 }
421
422 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
423  * exists, it will be replaced. */
424 int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
425 {
426         struct tone_zone_sound *ts,*ps;
427
428         /* is it an alias? stop */
429         if (zone->alias[0])
430                 return -1;
431
432         if (ast_mutex_lock(&tzlock)) {
433                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
434                 return -2;
435         }
436         for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
437                 if (strcasecmp(indication,ts->name)==0) {
438                         /* indication already there, replace */
439                         free((void*)ts->name);
440                         free((void*)ts->data);
441                         break;
442                 }
443         }
444         if (!ts) {
445                 /* not there, we have to add */
446                 ts = malloc(sizeof(struct tone_zone_sound));
447                 if (!ts) {
448                         ast_log(LOG_WARNING, "Out of memory\n");
449                         ast_mutex_unlock(&tzlock);
450                         return -2;
451                 }
452                 ts->next = NULL;
453         }
454         ts->name = strdup(indication);
455         ts->data = strdup(tonelist);
456         if (ts->name==NULL || ts->data==NULL) {
457                 ast_log(LOG_WARNING, "Out of memory\n");
458                 ast_mutex_unlock(&tzlock);
459                 return -2;
460         }
461         if (ps)
462                 ps->next = ts;
463         else
464                 zone->tones = ts;
465         ast_mutex_unlock(&tzlock);
466         return 0;
467 }
468
469 /* remove an existing country's indication. Both country and indication must exist */
470 int ast_unregister_indication(struct tone_zone *zone, const char *indication)
471 {
472         struct tone_zone_sound *ts,*ps = NULL, *tmp;
473         int res = -1;
474
475         /* is it an alias? stop */
476         if (zone->alias[0])
477                 return -1;
478
479         if (ast_mutex_lock(&tzlock)) {
480                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
481                 return -1;
482         }
483         ts = zone->tones;
484         while (ts) {
485                 if (strcasecmp(indication,ts->name)==0) {
486                         /* indication found */
487                         tmp = ts->next;
488                         if (ps)
489                                 ps->next = tmp;
490                         else
491                                 zone->tones = tmp;
492                         free((void*)ts->name);
493                         free((void*)ts->data);
494                         free(ts);
495                         ts = tmp;
496                         res = 0;
497                 }
498                 else {
499                         /* next zone please */
500                         ps = ts;
501                         ts = ts->next;
502                 }
503         }
504         /* indication not found, goodbye */
505         ast_mutex_unlock(&tzlock);
506         return res;
507 }