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