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