More fixes for remote_party_id
[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         ps->f.delivery.tv_sec = 0;
127         ps->f.delivery.tv_usec = 0;
128         ast_write(chan, &ps->f);
129
130         ps->pos += x;
131         if (pi->duration && ps->pos >= pi->duration * 8) {      /* item finished? */
132                 ps->pos = 0;                                    /* start new item */
133                 ps->npos++;
134                 if (ps->npos >= ps->nitems) {                   /* last item? */
135                         if (ps->reppos == -1)                   /* repeat set? */
136                                 return -1;
137                         ps->npos = ps->reppos;                  /* redo from top */
138                 }
139         }
140         return 0;
141 }
142
143 static struct ast_generator playtones = {
144         alloc: playtones_alloc,
145         release: playtones_release,
146         generate: playtones_generator,
147 };
148
149 int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
150 {
151         char *s, *data = ast_strdupa(playlst); /* cute */
152         struct playtones_def d = { vol, -1, 0, 1, NULL};
153         char *stringp=NULL;
154         char *separator;
155         if (!data)
156                 return -1;
157         if (vol < 1)
158                 d.vol = 8192;
159
160         d.interruptible = interruptible;
161         
162         stringp=data;
163         /* the stringp/data is not null here */
164         /* check if the data is separated with '|' or with ',' by default */
165         if (strchr(stringp,'|'))
166                 separator = "|";
167         else
168                 separator = ",";
169         s = strsep(&stringp,separator);
170         while(s && *s) {
171                 int freq1, freq2, time, modulate=0;
172
173                 if (s[0]=='!')
174                         s++;
175                 else if (d.reppos == -1)
176                         d.reppos = d.nitems;
177                 if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &time) == 3) {
178                         /* f1+f2/time format */
179                 } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
180                         /* f1+f2 format */
181                         time = 0;
182                 } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &time) == 3) {
183                         /* f1*f2/time format */
184                         modulate = 1;
185                 } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
186                         /* f1*f2 format */
187                         time = 0;
188                         modulate = 1;
189                 } else if (sscanf(s, "%d/%d", &freq1, &time) == 2) {
190                         /* f1/time format */
191                         freq2 = 0;
192                 } else if (sscanf(s, "%d", &freq1) == 1) {
193                         /* f1 format */
194                         freq2 = 0;
195                         time = 0;
196                 } else {
197                         ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n",chan->name,s,playlst);
198                         return -1;
199                 }
200
201                 d.items = realloc(d.items,(d.nitems+1)*sizeof(struct playtones_item));
202                 if (d.items == NULL)
203                         return -1;
204                 d.items[d.nitems].freq1    = freq1;
205                 d.items[d.nitems].freq2    = freq2;
206                 d.items[d.nitems].duration = time;
207                 d.items[d.nitems].modulate = modulate;
208                 d.nitems++;
209
210                 s = strsep(&stringp,separator);
211         }
212
213         if (ast_activate_generator(chan, &playtones, &d)) {
214                 free(d.items);
215                 return -1;
216         }
217         return 0;
218 }
219
220 void ast_playtones_stop(struct ast_channel *chan)
221 {
222         ast_deactivate_generator(chan);
223 }
224
225 /*--------------------------------------------*/
226
227 struct tone_zone *tone_zones;
228 static struct tone_zone *current_tonezone;
229
230 /* Protect the tone_zones list (highly unlikely that two things would change
231  * it at the same time, but still! */
232 AST_MUTEX_DEFINE_EXPORTED(tzlock);
233
234 /* Set global indication country */
235 int ast_set_indication_country(const char *country)
236 {
237         if (country) {
238                 struct tone_zone *z = ast_get_indication_zone(country);
239                 if (z) {
240                         if (option_verbose > 2)
241                                 ast_verbose(VERBOSE_PREFIX_3 "Setting default indication country to '%s'\n",country);
242                         current_tonezone = z;
243                         return 0;
244                 }
245         }
246         return 1; /* not found */
247 }
248
249 /* locate tone_zone, given the country. if country == NULL, use the default country */
250 struct tone_zone *ast_get_indication_zone(const char *country)
251 {
252         struct tone_zone *tz;
253         int alias_loop = 0;
254
255         /* we need some tonezone, pick the first */
256         if (country == NULL && current_tonezone)
257                 return current_tonezone;        /* default country? */
258         if (country == NULL && tone_zones)
259                 return tone_zones;              /* any country? */
260         if (country == NULL)
261                 return 0;       /* not a single country insight */
262
263         if (ast_mutex_lock(&tzlock)) {
264                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
265                 return 0;
266         }
267         do {
268                 for (tz=tone_zones; tz; tz=tz->next) {
269                         if (strcasecmp(country,tz->country)==0) {
270                                 /* tone_zone found */
271                                 if (tz->alias && tz->alias[0]) {
272                                         country = tz->alias;
273                                         break;
274                                 }
275                                 ast_mutex_unlock(&tzlock);
276                                 return tz;
277                         }
278                 }
279         } while (++alias_loop<20 && tz);
280         ast_mutex_unlock(&tzlock);
281         if (alias_loop==20)
282                 ast_log(LOG_NOTICE,"Alias loop for '%s' forcefull broken\n",country);
283         /* nothing found, sorry */
284         return 0;
285 }
286
287 /* locate a tone_zone_sound, given the tone_zone. if tone_zone == NULL, use the default tone_zone */
288 struct tone_zone_sound *ast_get_indication_tone(const struct tone_zone *zone, const char *indication)
289 {
290         struct tone_zone_sound *ts;
291
292         /* we need some tonezone, pick the first */
293         if (zone == NULL && current_tonezone)
294                 zone = current_tonezone;        /* default country? */
295         if (zone == NULL && tone_zones)
296                 zone = tone_zones;              /* any country? */
297         if (zone == NULL)
298                 return 0;       /* not a single country insight */
299
300         if (ast_mutex_lock(&tzlock)) {
301                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
302                 return 0;
303         }
304         for (ts=zone->tones; ts; ts=ts->next) {
305                 if (strcasecmp(indication,ts->name)==0) {
306                         /* found indication! */
307                         ast_mutex_unlock(&tzlock);
308                         return ts;
309                 }
310         }
311         /* nothing found, sorry */
312         ast_mutex_unlock(&tzlock);
313         return 0;
314 }
315
316 /* helper function to delete a tone_zone in its entirety */
317 static inline void free_zone(struct tone_zone* zone)
318 {
319         while (zone->tones) {
320                 struct tone_zone_sound *tmp = zone->tones->next;
321                 free((void*)zone->tones->name);
322                 free((void*)zone->tones->data);
323                 free(zone->tones);
324                 zone->tones = tmp;
325         }
326         if (zone->ringcadance)
327                 free((void*)zone->ringcadance);
328         free(zone);
329 }
330
331 /*--------------------------------------------*/
332
333 /* add a new country, if country exists, it will be replaced. */
334 int ast_register_indication_country(struct tone_zone *zone)
335 {
336         struct tone_zone *tz,*pz;
337
338         if (ast_mutex_lock(&tzlock)) {
339                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
340                 return -1;
341         }
342         for (pz=NULL,tz=tone_zones; tz; pz=tz,tz=tz->next) {
343                 if (strcasecmp(zone->country,tz->country)==0) {
344                         /* tone_zone already there, replace */
345                         zone->next = tz->next;
346                         if (pz)
347                                 pz->next = zone;
348                         else
349                                 tone_zones = zone;
350                         /* if we are replacing the default zone, re-point it */
351                         if (tz == current_tonezone)
352                                 current_tonezone = zone;
353                         /* now free the previous zone */
354                         free_zone(tz);
355                         ast_mutex_unlock(&tzlock);
356                         return 0;
357                 }
358         }
359         /* country not there, add */
360         zone->next = NULL;
361         if (pz)
362                 pz->next = zone;
363         else
364                 tone_zones = zone;
365         ast_mutex_unlock(&tzlock);
366
367         if (option_verbose > 2)
368                 ast_verbose(VERBOSE_PREFIX_3 "Registered indication country '%s'\n",zone->country);
369         return 0;
370 }
371
372 /* remove an existing country and all its indications, country must exist.
373  * Also, all countries which are an alias for the specified country are removed. */
374 int ast_unregister_indication_country(const char *country)
375 {
376         struct tone_zone *tz, *pz = NULL, *tmp;
377         int res = -1;
378
379         if (ast_mutex_lock(&tzlock)) {
380                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
381                 return -1;
382         }
383         tz = tone_zones;
384         while (tz) {
385                 if (country==NULL ||
386                     (strcasecmp(country, tz->country)==0 ||
387                      strcasecmp(country, tz->alias)==0)) {
388                         /* tone_zone found, remove */
389                         tmp = tz->next;
390                         if (pz)
391                                 pz->next = tmp;
392                         else
393                                 tone_zones = tmp;
394                         /* if we are unregistering the default country, w'll notice */
395                         if (tz == current_tonezone) {
396                                 ast_log(LOG_NOTICE,"Removed default indication country '%s'\n",tz->country);
397                                 current_tonezone = NULL;
398                         }
399                         if (option_verbose > 2)
400                                 ast_verbose(VERBOSE_PREFIX_3 "Unregistered indication country '%s'\n",tz->country);
401                         free_zone(tz);
402                         if (tone_zones == tz)
403                                 tone_zones = tmp;
404                         tz = tmp;
405                         res = 0;
406                 }
407                 else {
408                         /* next zone please */
409                         pz = tz;
410                         tz = tz->next;
411                 }
412         }
413         ast_mutex_unlock(&tzlock);
414         return res;
415 }
416
417 /* add a new indication to a tone_zone. tone_zone must exist. if the indication already
418  * exists, it will be replaced. */
419 int ast_register_indication(struct tone_zone *zone, const char *indication, const char *tonelist)
420 {
421         struct tone_zone_sound *ts,*ps;
422
423         /* is it an alias? stop */
424         if (zone->alias[0])
425                 return -1;
426
427         if (ast_mutex_lock(&tzlock)) {
428                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
429                 return -2;
430         }
431         for (ps=NULL,ts=zone->tones; ts; ps=ts,ts=ts->next) {
432                 if (strcasecmp(indication,ts->name)==0) {
433                         /* indication already there, replace */
434                         free((void*)ts->name);
435                         free((void*)ts->data);
436                         break;
437                 }
438         }
439         if (!ts) {
440                 /* not there, we have to add */
441                 ts = malloc(sizeof(struct tone_zone_sound));
442                 if (!ts) {
443                         ast_log(LOG_WARNING, "Out of memory\n");
444                         ast_mutex_unlock(&tzlock);
445                         return -2;
446                 }
447                 ts->next = NULL;
448         }
449         ts->name = strdup(indication);
450         ts->data = strdup(tonelist);
451         if (ts->name==NULL || ts->data==NULL) {
452                 ast_log(LOG_WARNING, "Out of memory\n");
453                 ast_mutex_unlock(&tzlock);
454                 return -2;
455         }
456         if (ps)
457                 ps->next = ts;
458         else
459                 zone->tones = ts;
460         ast_mutex_unlock(&tzlock);
461         return 0;
462 }
463
464 /* remove an existing country's indication. Both country and indication must exist */
465 int ast_unregister_indication(struct tone_zone *zone, const char *indication)
466 {
467         struct tone_zone_sound *ts,*ps = NULL, *tmp;
468         int res = -1;
469
470         /* is it an alias? stop */
471         if (zone->alias[0])
472                 return -1;
473
474         if (ast_mutex_lock(&tzlock)) {
475                 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
476                 return -1;
477         }
478         ts = zone->tones;
479         while (ts) {
480                 if (strcasecmp(indication,ts->name)==0) {
481                         /* indication found */
482                         tmp = ts->next;
483                         if (ps)
484                                 ps->next = tmp;
485                         else
486                                 zone->tones = tmp;
487                         free((void*)ts->name);
488                         free((void*)ts->data);
489                         free(ts);
490                         ts = tmp;
491                         res = 0;
492                 }
493                 else {
494                         /* next zone please */
495                         ps = ts;
496                         ts = ts->next;
497                 }
498         }
499         /* indication not found, goodbye */
500         ast_mutex_unlock(&tzlock);
501         return res;
502 }