json: Add conditionals to avoid locking if Jansson is thread safe.
[asterisk/asterisk.git] / main / callerid.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief CallerID Generation support
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include <time.h>
33 #include <math.h>
34 #include <ctype.h>
35
36 #include "asterisk/ulaw.h"
37 #include "asterisk/alaw.h"
38 #include "asterisk/frame.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/callerid.h"
41 #include "asterisk/fskmodem.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/format_cache.h"
44
45 struct callerid_state {
46         fsk_data fskd;
47         char rawdata[256];
48         short oldstuff[160];
49         int oldlen;
50         int pos;
51         int type;
52         int cksum;
53         char name[64];
54         char number[64];
55         int flags;
56         int sawflag;
57         int len;
58
59         int skipflag;
60         unsigned short crc;
61 };
62
63
64 float cid_dr[4], cid_di[4];
65 float clidsb = 8000.0 / 1200.0;
66 float sasdr, sasdi;
67 float casdr1, casdi1, casdr2, casdi2;
68
69 #define CALLERID_SPACE  2200.0          /*!< 2200 hz for "0" */
70 #define CALLERID_MARK   1200.0          /*!< 1200 hz for "1" */
71 #define SAS_FREQ                 440.0
72 #define CAS_FREQ1               2130.0
73 #define CAS_FREQ2               2750.0
74
75 #define AST_CALLERID_UNKNOWN    "<unknown>"
76
77 static inline void gen_tones(unsigned char *buf, int len, struct ast_format *codec, float ddr1, float ddi1, float ddr2, float ddi2, float *cr1, float *ci1, float *cr2, float *ci2)
78 {
79         int x;
80         float t;
81         for (x = 0; x < len; x++) {
82                 t = *cr1 * ddr1 - *ci1 * ddi1;
83                 *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
84                 *cr1 = t;
85                 t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
86                 *cr1 *= t;
87                 *ci1 *= t;
88
89                 t = *cr2 * ddr2 - *ci2 * ddi2;
90                 *ci2 = *cr2 * ddi2 + *ci2 * ddr2;
91                 *cr2 = t;
92                 t = 2.0 - (*cr2 * *cr2 + *ci2 * *ci2);
93                 *cr2 *= t;
94                 *ci2 *= t;
95                 buf[x] = AST_LIN2X((*cr1 + *cr2) * 2048.0);
96         }
97 }
98
99 static inline void gen_tone(unsigned char *buf, int len, struct ast_format *codec, float ddr1, float ddi1, float *cr1, float *ci1)
100 {
101         int x;
102         float t;
103         for (x = 0; x < len; x++) {
104                 t = *cr1 * ddr1 - *ci1 * ddi1;
105                 *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
106                 *cr1 = t;
107                 t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
108                 *cr1 *= t;
109                 *ci1 *= t;
110                 buf[x] = AST_LIN2X(*cr1 * 8192.0);
111         }
112 }
113
114 /*! \brief Initialize stuff for inverse FFT */
115 void callerid_init(void)
116 {
117         cid_dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
118         cid_di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
119         cid_dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0);
120         cid_di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0);
121         sasdr = cos(SAS_FREQ * 2.0 * M_PI / 8000.0);
122         sasdi = sin(SAS_FREQ * 2.0 * M_PI / 8000.0);
123         casdr1 = cos(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
124         casdi1 = sin(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
125         casdr2 = cos(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
126         casdi2 = sin(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
127 }
128
129 struct callerid_state *callerid_new(int cid_signalling)
130 {
131         struct callerid_state *cid;
132
133         if ((cid = ast_calloc(1, sizeof(*cid)))) {
134 #ifdef INTEGER_CALLERID
135                 cid->fskd.ispb = 7;             /* 1200 baud */
136                 /* Set up for 1200 / 8000 freq *32 to allow ints */
137                 cid->fskd.pllispb  = (int)(8000 * 32  / 1200);
138                 cid->fskd.pllids   = cid->fskd.pllispb/32;
139                 cid->fskd.pllispb2 = cid->fskd.pllispb/2;
140
141                 cid->fskd.icont = 0;           /* PLL REset */
142                 /* cid->fskd.hdlc = 0; */       /* Async */
143                 cid->fskd.nbit = 8;             /* 8 bits */
144                 cid->fskd.instop = 1;           /* 1 stop bit */
145                 /* cid->fskd.paridad = 0; */    /* No parity */
146                 cid->fskd.bw = 1;               /* Filter 800 Hz */
147                 if (cid_signalling == 2) {      /* v23 signalling */
148                         cid->fskd.f_mark_idx  = 4;      /* 1300 Hz */
149                         cid->fskd.f_space_idx = 5;      /* 2100 Hz */
150                 } else {                        /* Bell 202 signalling as default */
151                         cid->fskd.f_mark_idx  = 2;      /* 1200 Hz */
152                         cid->fskd.f_space_idx = 3;      /* 2200 Hz */
153                 }
154                 /* cid->fskd.pcola = 0; */      /* No clue */
155                 /* cid->fskd.cont = 0.0; */     /* Digital PLL reset */
156                 /* cid->fskd.x0 = 0.0; */
157                 /* cid->fskd.state = 0; */
158                 cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
159                 /* cid->pos = 0; */
160
161                 fskmodem_init(&cid->fskd);
162 #else
163                 cid->fskd.spb = 7.0;            /* 1200 baud */
164                 /* cid->fskd.hdlc = 0; */       /* Async */
165                 cid->fskd.nbit = 8;             /* 8 bits */
166                 cid->fskd.nstop = 1.0;          /* 1 stop bit */
167                 /* cid->fskd.paridad = 0; */    /* No parity */
168                 cid->fskd.bw = 1;               /* Filter 800 Hz */
169                 if (cid_signalling == 2) {      /* v23 signalling */
170                         cid->fskd.f_mark_idx =  4;      /* 1300 Hz */
171                         cid->fskd.f_space_idx = 5;      /* 2100 Hz */
172                 } else {                        /* Bell 202 signalling as default */
173                         cid->fskd.f_mark_idx =  2;      /* 1200 Hz */
174                         cid->fskd.f_space_idx = 3;      /* 2200 Hz */
175                 }
176                 /* cid->fskd.pcola = 0; */      /* No clue */
177                 /* cid->fskd.cont = 0.0; */     /* Digital PLL reset */
178                 /* cid->fskd.x0 = 0.0; */
179                 /* cid->fskd.state = 0; */
180                 cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
181                 /* cid->pos = 0; */
182 #endif
183         }
184
185         return cid;
186 }
187
188 void callerid_get(struct callerid_state *cid, char **name, char **number, int *flags)
189 {
190         *flags = cid->flags;
191         if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NAME))
192                 *name = NULL;
193         else
194                 *name = cid->name;
195         if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER))
196                 *number = NULL;
197         else
198                 *number = cid->number;
199 }
200
201 void callerid_get_dtmf(char *cidstring, char *number, int *flags)
202 {
203         int i;
204         int code;
205
206         /* "Clear" the number-buffer. */
207         number[0] = 0;
208
209         if (strlen(cidstring) < 2) {
210                 ast_debug(1, "No cid detected\n");
211                 *flags = CID_UNKNOWN_NUMBER;
212                 return;
213         }
214
215         /* Detect protocol and special types */
216         if (cidstring[0] == 'B') {
217                 /* Handle special codes */
218                 code = atoi(&cidstring[1]);
219                 if (code == 0)
220                         *flags = CID_UNKNOWN_NUMBER;
221                 else if (code == 10)
222                         *flags = CID_PRIVATE_NUMBER;
223                 else
224                         ast_debug(1, "Unknown DTMF code %d\n", code);
225         } else if (cidstring[0] == 'D' && cidstring[2] == '#') {
226                 /* .DK special code */
227                 if (cidstring[1] == '1')
228                         *flags = CID_PRIVATE_NUMBER;
229                 if (cidstring[1] == '2' || cidstring[1] == '3')
230                         *flags = CID_UNKNOWN_NUMBER;
231         } else if (cidstring[0] == 'D' || cidstring[0] == 'A') {
232                 /* "Standard" callerid */
233                 for (i = 1; i < strlen(cidstring); i++) {
234                         if (cidstring[i] == 'C' || cidstring[i] == '#')
235                                 break;
236                         if (isdigit(cidstring[i]))
237                                 number[i-1] = cidstring[i];
238                         else
239                                 ast_debug(1, "Unknown CID digit '%c'\n",
240                                         cidstring[i]);
241                 }
242                 number[i-1] = 0;
243         } else if (isdigit(cidstring[0])) {
244                 /* It begins with a digit, so we parse it as a number and hope
245                  * for the best */
246                 ast_log(LOG_WARNING, "Couldn't detect start-character. CID "
247                         "parsing might be unreliable\n");
248                 for (i = 0; i < strlen(cidstring); i++) {
249                         if (isdigit(cidstring[i]))
250                                 number[i] = cidstring[i];
251                         else
252                                 break;
253                 }
254                 number[i] = 0;
255         } else {
256                 ast_debug(1, "Unknown CID protocol, start digit '%c'\n", cidstring[0]);
257                 *flags = CID_UNKNOWN_NUMBER;
258         }
259 }
260
261 int ast_gen_cas(unsigned char *outbuf, int sendsas, int len, struct ast_format *codec)
262 {
263         int pos = 0;
264         int saslen = 2400;
265         float cr1 = 1.0;
266         float ci1 = 0.0;
267         float cr2 = 1.0;
268         float ci2 = 0.0;
269
270         if (sendsas) {
271                 if (len < saslen)
272                         return -1;
273                 gen_tone(outbuf, saslen, codec, sasdr, sasdi, &cr1, &ci1);
274                 len -= saslen;
275                 pos += saslen;
276                 cr2 = cr1;
277                 ci2 = ci1;
278         }
279         gen_tones(outbuf + pos, len, codec, casdr1, casdi1, casdr2, casdi2, &cr1, &ci1, &cr2, &ci2);
280         return 0;
281 }
282
283 static unsigned short calc_crc(unsigned short crc, unsigned char data)
284 {
285         unsigned int i, j, org, dst;
286         org = data;
287         dst = 0;
288
289         for (i = 0; i < CHAR_BIT; i++) {
290                 org <<= 1;
291                 dst >>= 1;
292                 if (org & 0x100)
293                         dst |= 0x80;
294         }
295         data = (unsigned char) dst;
296         crc ^= (unsigned int) data << (16 - CHAR_BIT);
297         for (j = 0; j < CHAR_BIT; j++) {
298                 if (crc & 0x8000U)
299                         crc = (crc << 1) ^ 0x1021U ;
300                 else
301                         crc <<= 1 ;
302         }
303         return crc;
304 }
305
306 int callerid_feed_jp(struct callerid_state *cid, unsigned char *ubuf, int len, struct ast_format *codec)
307 {
308         int mylen = len;
309         int olen;
310         int b = 'X';
311         int b2;
312         int res;
313         int x;
314         short *buf;
315
316         buf = ast_alloca(2 * len + cid->oldlen);
317
318         memcpy(buf, cid->oldstuff, cid->oldlen);
319         mylen += cid->oldlen / 2;
320
321         for (x = 0; x < len; x++)
322                 buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
323
324         while (mylen >= 160) {
325                 b = b2 = 0;
326                 olen = mylen;
327                 res = fsk_serial(&cid->fskd, buf, &mylen, &b);
328
329                 if (mylen < 0) {
330                         ast_log(LOG_ERROR, "No start bit found in fsk data.\n");
331                         return -1;
332                 }
333
334                 buf += (olen - mylen);
335
336                 if (res < 0) {
337                         ast_log(LOG_NOTICE, "fsk_serial failed\n");
338                         return -1;
339                 }
340
341                 if (res == 1) {
342                         b2 = b;
343                         b  &= 0x7f;
344
345                         /* crc checksum calculation */
346                         if (cid->sawflag > 1)
347                                 cid->crc = calc_crc(cid->crc, (unsigned char) b2);
348
349                         /* Ignore invalid bytes */
350                         if (b > 0xff)
351                                 continue;
352
353                         /* skip DLE if needed */
354                         if (cid->sawflag > 0) {
355                                 if (cid->sawflag != 5 && cid->skipflag == 0 && b == 0x10) {
356                                         cid->skipflag = 1 ;
357                                         continue ;
358                                 }
359                         }
360                         if (cid->skipflag == 1)
361                                 cid->skipflag = 0 ;
362
363                         /* caller id retrieval */
364                         switch (cid->sawflag) {
365                         case 0: /* DLE */
366                                 if (b == 0x10) {
367                                         cid->sawflag = 1;
368                                         cid->skipflag = 0;
369                                         cid->crc = 0;
370                                 }
371                                 break;
372                         case 1: /* SOH */
373                                 if (b == 0x01)
374                                         cid->sawflag = 2;
375                                 break ;
376                         case 2: /* HEADER */
377                                 if (b == 0x07)
378                                         cid->sawflag = 3;
379                                 break;
380                         case 3: /* STX */
381                                 if (b == 0x02)
382                                         cid->sawflag = 4;
383                                 break;
384                         case 4: /* SERVICE TYPE */
385                                 if (b == 0x40)
386                                         cid->sawflag = 5;
387                                 break;
388                         case 5: /* Frame Length */
389                                 cid->sawflag = 6;
390                                 break;
391                         case 6: /* NUMBER TYPE */
392                                 cid->sawflag = 7;
393                                 cid->pos = 0;
394                                 cid->rawdata[cid->pos++] = b;
395                                 break;
396                         case 7: /* NUMBER LENGTH */
397                                 cid->sawflag = 8;
398                                 cid->len = b;
399                                 if ((cid->len+2) >= sizeof(cid->rawdata)) {
400                                         ast_log(LOG_WARNING, "too long caller id string\n") ;
401                                         return -1;
402                                 }
403                                 cid->rawdata[cid->pos++] = b;
404                                 break;
405                         case 8: /* Retrieve message */
406                                 cid->rawdata[cid->pos++] = b;
407                                 cid->len--;
408                                 if (cid->len<=0) {
409                                         cid->rawdata[cid->pos] = '\0';
410                                         cid->sawflag = 9;
411                                 }
412                                 break;
413                         case 9: /* ETX */
414                                 cid->sawflag = 10;
415                                 break;
416                         case 10: /* CRC Checksum 1 */
417                                 cid->sawflag = 11;
418                                 break;
419                         case 11: /* CRC Checksum 2 */
420                                 cid->sawflag = 12;
421                                 if (cid->crc != 0) {
422                                         ast_log(LOG_WARNING, "crc checksum error\n") ;
423                                         return -1;
424                                 }
425                                 /* extract caller id data */
426                                 for (x = 0; x < cid->pos;) {
427                                         switch (cid->rawdata[x++]) {
428                                         case 0x02: /* caller id  number */
429                                                 cid->number[0] = '\0';
430                                                 cid->name[0] = '\0';
431                                                 cid->flags = 0;
432                                                 res = cid->rawdata[x++];
433                                                 ast_copy_string(cid->number, &cid->rawdata[x], res+1);
434                                                 x += res;
435                                                 break;
436                                         case 0x21: /* additional information */
437                                                 /* length */
438                                                 x++;
439                                                 /* number type */
440                                                 switch (cid->rawdata[x]) {
441                                                 case 0x00: /* unknown */
442                                                 case 0x01: /* international number */
443                                                 case 0x02: /* domestic number */
444                                                 case 0x03: /* network */
445                                                 case 0x04: /* local call */
446                                                 case 0x06: /* short dial number */
447                                                 case 0x07: /* reserved */
448                                                 default:   /* reserved */
449                                                         ast_debug(2, "cid info:#1=%X\n", (unsigned)cid->rawdata[x]);
450                                                         break ;
451                                                 }
452                                                 x++;
453                                                 /* numbering plan octed 4 */
454                                                 x++;
455                                                 /* numbering plan octed 5 */
456                                                 switch (cid->rawdata[x]) {
457                                                 case 0x00: /* unknown */
458                                                 case 0x01: /* recommendation E.164 ISDN */
459                                                 case 0x03: /* recommendation X.121 */
460                                                 case 0x04: /* telex dial plan */
461                                                 case 0x08: /* domestic dial plan */
462                                                 case 0x09: /* private dial plan */
463                                                 case 0x05: /* reserved */
464                                                 default:   /* reserved */
465                                                         ast_debug(2, "cid info:#2=%X\n", (unsigned)cid->rawdata[x]);
466                                                         break ;
467                                                 }
468                                                 x++;
469                                                 break ;
470                                         case 0x04: /* no callerid reason */
471                                                 /* length */
472                                                 x++;
473                                                 /* no callerid reason code */
474                                                 switch (cid->rawdata[x]) {
475                                                 case 'P': /* caller id denied by user */
476                                                 case 'O': /* service not available */
477                                                 case 'C': /* pay phone */
478                                                 case 'S': /* service congested */
479                                                         cid->flags |= CID_UNKNOWN_NUMBER;
480                                                         ast_debug(2, "no cid reason:%c\n", cid->rawdata[x]);
481                                                         break ;
482                                                 }
483                                                 x++;
484                                                 break ;
485                                         case 0x09: /* dialed number */
486                                                 /* length */
487                                                 res = cid->rawdata[x++];
488                                                 /* dialed number */
489                                                 x += res;
490                                                 break ;
491                                         case 0x22: /* dialed number additional information */
492                                                 /* length */
493                                                 x++;
494                                                 /* number type */
495                                                 switch (cid->rawdata[x]) {
496                                                 case 0x00: /* unknown */
497                                                 case 0x01: /* international number */
498                                                 case 0x02: /* domestic number */
499                                                 case 0x03: /* network */
500                                                 case 0x04: /* local call */
501                                                 case 0x06: /* short dial number */
502                                                 case 0x07: /* reserved */
503                                                 default:   /* reserved */
504                                                         if (option_debug > 1)
505                                                                 ast_log(LOG_NOTICE, "did info:#1=%X\n", (unsigned)cid->rawdata[x]);
506                                                         break ;
507                                                 }
508                                                 x++;
509                                                 /* numbering plan octed 4 */
510                                                 x++;
511                                                 /* numbering plan octed 5 */
512                                                 switch (cid->rawdata[x]) {
513                                                 case 0x00: /* unknown */
514                                                 case 0x01: /* recommendation E.164 ISDN */
515                                                 case 0x03: /* recommendation X.121 */
516                                                 case 0x04: /* telex dial plan */
517                                                 case 0x08: /* domestic dial plan */
518                                                 case 0x09: /* private dial plan */
519                                                 case 0x05: /* reserved */
520                                                 default:   /* reserved */
521                                                         ast_debug(2, "did info:#2=%X\n", (unsigned)cid->rawdata[x]);
522                                                         break ;
523                                                 }
524                                                 x++;
525                                                 break ;
526                                         }
527                                 }
528                                 return 1;
529                                 break;
530                         default:
531                                 ast_log(LOG_ERROR, "invalid value in sawflag %d\n", cid->sawflag);
532                         }
533                 }
534         }
535         if (mylen) {
536                 memcpy(cid->oldstuff, buf, mylen * 2);
537                 cid->oldlen = mylen * 2;
538         } else
539                 cid->oldlen = 0;
540
541         return 0;
542 }
543
544
545 int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len, struct ast_format *codec)
546 {
547         int mylen = len;
548         int olen;
549         int b = 'X';
550         int res;
551         int x;
552         short *buf;
553
554         buf = ast_alloca(2 * len + cid->oldlen);
555
556         memcpy(buf, cid->oldstuff, cid->oldlen);
557         mylen += cid->oldlen/2;
558
559         for (x = 0; x < len; x++)
560                 buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
561         while (mylen >= 160) {
562                 olen = mylen;
563                 res = fsk_serial(&cid->fskd, buf, &mylen, &b);
564                 if (mylen < 0) {
565                         ast_log(LOG_ERROR, "No start bit found in fsk data.\n");
566                         return -1;
567                 }
568                 buf += (olen - mylen);
569                 if (res < 0) {
570                         ast_log(LOG_NOTICE, "fsk_serial failed\n");
571                         return -1;
572                 }
573                 if (res == 1) {
574                         if (b > 0xff) {
575                                 if (cid->sawflag != 5) {
576                                         /* Ignore invalid bytes */
577                                         continue;
578                                 }
579                                 /*
580                                  * We can tollerate an error on the checksum character since the
581                                  * checksum character is the last character in the message and
582                                  * it validates the message.
583                                  *
584                                  * Remove character error flags.
585                                  * Bit 8 : Parity error
586                                  * Bit 9 : Framing error
587                                  */
588                                 b &= 0xff;
589                         }
590                         switch (cid->sawflag) {
591                         case 0: /* Look for flag */
592                                 if (b == 'U')
593                                         cid->sawflag = 2;
594                                 break;
595                         case 2: /* Get lead-in */
596                                 if ((b == 0x04) || (b == 0x80) || (b == 0x06) || (b == 0x82)) {
597                                         cid->type = b;
598                                         cid->sawflag = 3;
599                                         cid->cksum = b;
600                                 }
601                                 break;
602                         case 3: /* Get length */
603                                 /* Not a lead in.  We're ready  */
604                                 cid->sawflag = 4;
605                                 cid->len = b;
606                                 cid->pos = 0;
607                                 cid->cksum += b;
608                                 break;
609                         case 4: /* Retrieve message */
610                                 if (cid->pos >= 128) {
611                                         ast_log(LOG_WARNING, "Caller ID too long???\n");
612                                         return -1;
613                                 }
614                                 cid->rawdata[cid->pos++] = b;
615                                 cid->len--;
616                                 cid->cksum += b;
617                                 if (!cid->len) {
618                                         cid->rawdata[cid->pos] = '\0';
619                                         cid->sawflag = 5;
620                                 }
621                                 break;
622                         case 5: /* Check checksum */
623                                 if ((b + cid->cksum) & 0xff) {
624                                         ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
625                                         /* Try again */
626                                         cid->sawflag = 0;
627                                         break;
628                                 }
629
630                                 cid->number[0] = '\0';
631                                 cid->name[0] = '\0';
632                                 /* Update flags */
633                                 cid->flags = 0;
634                                 /* If we get this far we're fine.  */
635                                 if ((cid->type == 0x80) || (cid->type == 0x82)) {
636                                         /* MDMF */
637                                         /* Go through each element and process */
638                                         for (x = 0; x < cid->pos;) {
639                                                 switch (cid->rawdata[x++]) {
640                                                 case 1:
641                                                         /* Date */
642                                                         break;
643                                                 case 2: /* Number */
644                                                 case 3: /* Number (for Zebble) */
645                                                 case 4: /* Number */
646                                                         res = cid->rawdata[x];
647                                                         if (res > 32) {
648                                                                 ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
649                                                                 res = 32;
650                                                         }
651                                                         if (ast_strlen_zero(cid->number)) {
652                                                                 memcpy(cid->number, cid->rawdata + x + 1, res);
653                                                                 /* Null terminate */
654                                                                 cid->number[res] = '\0';
655                                                         }
656                                                         break;
657                                                 case 6: /* Stentor Call Qualifier (ie. Long Distance call) */
658                                                         break;
659                                                 case 7: /* Name */
660                                                 case 8: /* Name */
661                                                         res = cid->rawdata[x];
662                                                         if (res > 32) {
663                                                                 ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
664                                                                 res = 32;
665                                                         }
666                                                         memcpy(cid->name, cid->rawdata + x + 1, res);
667                                                         cid->name[res] = '\0';
668                                                         break;
669                                                 case 11: /* Message Waiting */
670                                                         res = cid->rawdata[x + 1];
671                                                         if (res)
672                                                                 cid->flags |= CID_MSGWAITING;
673                                                         else
674                                                                 cid->flags |= CID_NOMSGWAITING;
675                                                         break;
676                                                 case 17: /* UK: Call type, 1=Voice Call, 2=Ringback when free, 129=Message waiting  */
677                                                 case 19: /* UK: Network message system status (Number of messages waiting) */
678                                                 case 22: /* Something French */
679                                                         break;
680                                                 default:
681                                                         ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x - 1]);
682                                                 }
683                                                 res = cid->rawdata[x];
684                                                 if (0 > res){   /* Negative offset in the CID Spill */
685                                                         ast_log(LOG_NOTICE, "IE %d has bad field length of %d at offset %d\n", cid->rawdata[x-1], cid->rawdata[x], x);
686                                                         /* Try again */
687                                                         cid->sawflag = 0;
688                                                         break;  /* Exit the loop */
689                                                 }
690                                                 x += cid->rawdata[x];
691                                                 x++;
692                                         }
693                                 } else if (cid->type == 0x6) {
694                                         /* VMWI SDMF */
695                                         if (cid->rawdata[2] == 0x42) {
696                                                 cid->flags |= CID_MSGWAITING;
697                                         } else if (cid->rawdata[2] == 0x6f) {
698                                                 cid->flags |= CID_NOMSGWAITING;
699                                         }
700                                 } else {
701                                         /* SDMF */
702                                         ast_copy_string(cid->number, cid->rawdata + 8, sizeof(cid->number));
703                                 }
704                                 if (!strcmp(cid->number, "P")) {
705                                         strcpy(cid->number, "");
706                                         cid->flags |= CID_PRIVATE_NUMBER;
707                                 } else if (!strcmp(cid->number, "O") || ast_strlen_zero(cid->number)) {
708                                         strcpy(cid->number, "");
709                                         cid->flags |= CID_UNKNOWN_NUMBER;
710                                 }
711                                 if (!strcmp(cid->name, "P")) {
712                                         strcpy(cid->name, "");
713                                         cid->flags |= CID_PRIVATE_NAME;
714                                 } else if (!strcmp(cid->name, "O") || ast_strlen_zero(cid->name)) {
715                                         strcpy(cid->name, "");
716                                         cid->flags |= CID_UNKNOWN_NAME;
717                                 }
718                                 return 1;
719                                 break;
720                         default:
721                                 ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
722                         }
723                 }
724         }
725         if (mylen) {
726                 memcpy(cid->oldstuff, buf, mylen * 2);
727                 cid->oldlen = mylen * 2;
728         } else
729                 cid->oldlen = 0;
730
731         return 0;
732 }
733
734 void callerid_free(struct callerid_state *cid)
735 {
736         ast_free(cid);
737 }
738
739 static int callerid_genmsg(char *msg, int size, const char *number, const char *name, int flags)
740 {
741         struct timeval now = ast_tvnow();
742         struct ast_tm tm;
743         char *ptr;
744         int res;
745         int i, x;
746
747         /* Get the time */
748         ast_localtime(&now, &tm, NULL);
749
750         ptr = msg;
751
752         /* Format time and message header */
753         res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm.tm_mon + 1,
754                                 tm.tm_mday, tm.tm_hour, tm.tm_min);
755         size -= res;
756         ptr += res;
757         if (ast_strlen_zero(number) || (flags & CID_UNKNOWN_NUMBER)) {
758                 /* Indicate number not known */
759                 res = snprintf(ptr, size, "\004\001O");
760                 size -= res;
761                 ptr += res;
762         } else if (flags & CID_PRIVATE_NUMBER) {
763                 /* Indicate number is private */
764                 res = snprintf(ptr, size, "\004\001P");
765                 size -= res;
766                 ptr += res;
767         } else {
768                 /* Send up to 16 digits of number MAX */
769                 i = strlen(number);
770                 if (i > 16)
771                         i = 16;
772                 res = snprintf(ptr, size, "\002%c", i);
773                 size -= res;
774                 ptr += res;
775                 for (x = 0; x < i; x++)
776                         ptr[x] = number[x];
777                 ptr[i] = '\0';
778                 ptr += i;
779                 size -= i;
780         }
781
782         if (ast_strlen_zero(name) || (flags & CID_UNKNOWN_NAME)) {
783                 /* Indicate name not known */
784                 res = snprintf(ptr, size, "\010\001O");
785                 size -= res;
786                 ptr += res;
787         } else if (flags & CID_PRIVATE_NAME) {
788                 /* Indicate name is private */
789                 res = snprintf(ptr, size, "\010\001P");
790                 size -= res;
791                 ptr += res;
792         } else {
793                 /* Send up to 16 digits of name MAX */
794                 i = strlen(name);
795                 if (i > 16)
796                         i = 16;
797                 res = snprintf(ptr, size, "\007%c", i);
798                 size -= res;
799                 ptr += res;
800                 for (x = 0; x < i; x++)
801                         ptr[x] = name[x];
802                 ptr[i] = '\0';
803                 ptr += i;
804                 size -= i;
805         }
806         return (ptr - msg);
807
808 }
809
810 int ast_callerid_vmwi_generate(unsigned char *buf, int active, int type, struct ast_format *codec,
811                                const char* name, const char* number, int flags)
812 {
813         char msg[256];
814         int len = 0;
815         int sum;
816         int x;
817         int bytes = 0;
818         float cr = 1.0;
819         float ci = 0.0;
820         float scont = 0.0;
821
822         if (type == CID_MWI_TYPE_MDMF_FULL) {
823                 /* MDMF Message waiting with date, number, name and MWI parameter */
824                 msg[0] = 0x82;
825
826                 /* put date, number info at the right place */
827                 len = callerid_genmsg(msg+2, sizeof(msg)-2, number, name, flags);
828
829                 /* length of MDMF CLI plus Message Waiting Structure */
830                 msg[1] = len+3;
831
832                 /* Go to the position to write to */
833                 len = len+2;
834
835                 /* "Message Waiting Parameter" */
836                 msg[len++] = 0x0b;
837                 /* Length of IE is one */
838                 msg[len++] = 1;
839                 /* Active or not */
840                 if (active)
841                         msg[len++] = 0xff;
842                 else
843                         msg[len++] = 0x00;
844
845         } else if (type == CID_MWI_TYPE_MDMF) {
846                 /* MDMF Message waiting only */
847                 /* same as above except that the we only put MWI parameter */
848                 msg[len++] = 0x82;
849                 /* Length is 3 */
850                 msg[len++] = 3;
851                 /* IE is "Message Waiting Parameter" */
852                 msg[len++] = 0x0b;
853                 /* Length of IE is one */
854                 msg[len++] = 1;
855                 /* Active or not */
856                 if (active)
857                         msg[len++] = 0xff;
858                 else
859                         msg[len++] = 0x00;
860         } else {
861                 /* SDMF Message waiting */
862                 msg[len++] = 0x6;
863                 /* Length is 3 */
864                 msg[len++] = 3;
865                 if (active) {
866                         msg[len++] = 0x42;
867                         msg[len++] = 0x42;
868                         msg[len++] = 0x42;
869                 } else {
870                         msg[len++] = 0x6f;
871                         msg[len++] = 0x6f;
872                         msg[len++] = 0x6f;
873                 }
874         }
875         sum = 0;
876         for (x = 0; x < len; x++)
877                 sum += msg[x];
878         sum = (256 - (sum & 255));
879         msg[len++] = sum;
880         /* Wait a half a second */
881         for (x = 0; x < 4000; x++)
882                 PUT_BYTE(0x7f);
883         /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
884         for (x = 0; x < 30; x++)
885                 PUT_CLID(0x55);
886         /* Send 170ms of callerid marks */
887         for (x = 0; x < 170; x++)
888                 PUT_CLID_MARKMS;
889         for (x = 0; x < len; x++) {
890                 PUT_CLID(msg[x]);
891         }
892         /* Send 50 more ms of marks */
893         for (x = 0; x < 50; x++)
894                 PUT_CLID_MARKMS;
895         return bytes;
896 }
897
898 int callerid_generate(unsigned char *buf, const char *number, const char *name, int flags, int callwaiting, struct ast_format *codec)
899 {
900         int bytes = 0;
901         int x, sum;
902         int len;
903
904         /* Initial carriers (real/imaginary) */
905         float cr = 1.0;
906         float ci = 0.0;
907         float scont = 0.0;
908         char msg[256];
909         len = callerid_genmsg(msg, sizeof(msg), number, name, flags);
910         if (!callwaiting) {
911                 /* Wait a half a second */
912                 for (x = 0; x < 4000; x++)
913                         PUT_BYTE(0x7f);
914                 /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
915                 for (x = 0; x < 30; x++)
916                         PUT_CLID(0x55);
917         }
918         /* Send 150ms of callerid marks */
919         for (x = 0; x < 150; x++)
920                 PUT_CLID_MARKMS;
921         /* Send 0x80 indicating MDMF format */
922         PUT_CLID(0x80);
923         /* Put length of whole message */
924         PUT_CLID(len);
925         sum = 0x80 + strlen(msg);
926         /* Put each character of message and update checksum */
927         for (x = 0; x < len; x++) {
928                 PUT_CLID(msg[x]);
929                 sum += msg[x];
930         }
931         /* Send 2's compliment of sum */
932         PUT_CLID(256 - (sum & 255));
933
934         /* Send 50 more ms of marks */
935         for (x = 0; x < 50; x++)
936                 PUT_CLID_MARKMS;
937
938         return bytes;
939 }
940
941 /*!
942  * \brief Clean up phone string
943  * \details
944  * Remove '(', ' ', ')', non-trailing '.', and '-' not in square brackets.
945  * Basically, remove anything that could be invalid in a pattern.
946  */
947 void ast_shrink_phone_number(char *n)
948 {
949         int x, y = 0;
950         int bracketed = 0;
951
952         for (x = 0; n[x]; x++) {
953                 switch (n[x]) {
954                 case '[':
955                         bracketed++;
956                         n[y++] = n[x];
957                         break;
958                 case ']':
959                         bracketed--;
960                         n[y++] = n[x];
961                         break;
962                 case '-':
963                         if (bracketed)
964                                 n[y++] = n[x];
965                         break;
966                 case '.':
967                         if (!n[x+1])
968                                 n[y++] = n[x];
969                         break;
970                 default:
971                         /* ignore parenthesis and whitespace */
972                         if (!strchr("( )", n[x]))
973                                 n[y++] = n[x];
974                 }
975         }
976         n[y] = '\0';
977 }
978
979 /*!
980  * \brief Checks if phone number consists of valid characters
981  * \param exten String that needs to be checked
982  * \param valid Valid characters in string
983  * \retval 1 if valid string
984  * \retval 0 if string contains invalid characters
985  */
986 static int ast_is_valid_string(const char *exten, const char *valid)
987 {
988         int x;
989
990         if (ast_strlen_zero(exten))
991                 return 0;
992         for (x = 0; exten[x]; x++)
993                 if (!strchr(valid, exten[x]))
994                         return 0;
995         return 1;
996 }
997
998 int ast_isphonenumber(const char *n)
999 {
1000         return ast_is_valid_string(n, "0123456789*#+");
1001 }
1002
1003 int ast_is_shrinkable_phonenumber(const char *exten)
1004 {
1005         return ast_is_valid_string(exten, "0123456789*#+()-.");
1006 }
1007
1008 int ast_callerid_parse(char *input_str, char **name, char **location)
1009 {
1010         char *ls;
1011         char *le;
1012         char *name_start;
1013         char *instr;
1014         int quotes_stripped = 0;
1015
1016         /* Handle surrounding quotes */
1017         input_str = ast_strip(input_str);
1018         instr = ast_strip_quoted(input_str, "\"", "\"");
1019         if (instr != input_str) {
1020                 quotes_stripped = 1;
1021         }
1022
1023         /* Try "name" <location> format or name <location> format or with a missing > */
1024         if ((ls = strrchr(instr, '<'))) {
1025                 if ((le = strrchr(ls, '>'))) {
1026                         *le = '\0';     /* location found, trim off the brackets */
1027                 }
1028                 *ls = '\0';
1029                 *location = ls + 1;     /* and this is the result */
1030
1031                 name_start = ast_strip_quoted(instr, "\"", "\"");
1032         } else {        /* no valid brackets */
1033                 char tmp[256];
1034
1035                 ast_copy_string(tmp, instr, sizeof(tmp));
1036                 ast_shrink_phone_number(tmp);
1037                 if (!quotes_stripped && ast_isphonenumber(tmp)) {       /* Assume it's just a location */
1038                         name_start = NULL;
1039                         strcpy(instr, tmp); /* safe, because tmp will always be the same size or smaller than instr */
1040                         *location = instr;
1041                 } else { /* Assume it's just a name. */
1042                         *location = NULL;
1043                         name_start = ast_strip_quoted(instr, "\"", "\"");
1044                 }
1045         }
1046
1047         if (name_start) {
1048                 ast_unescape_quoted(name_start);
1049         }
1050         *name = name_start;
1051         return 0;
1052 }
1053
1054 static int __ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int callwaiting, struct ast_format *codec)
1055 {
1056         if (ast_strlen_zero(name))
1057                 name = NULL;
1058         if (ast_strlen_zero(number))
1059                 number = NULL;
1060         return callerid_generate(buf, number, name, 0, callwaiting, codec);
1061 }
1062
1063 int ast_callerid_generate(unsigned char *buf, const char *name, const char *number, struct ast_format *codec)
1064 {
1065         return __ast_callerid_generate(buf, name, number, 0, codec);
1066 }
1067
1068 int ast_callerid_callwaiting_generate(unsigned char *buf, const char *name, const char *number, struct ast_format *codec)
1069 {
1070         return __ast_callerid_generate(buf, name, number, 1, codec);
1071 }
1072
1073 char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown)
1074 {
1075         if (!unknown)
1076                 unknown = "<unknown>";
1077         if (name && num) {
1078                 char name_buf[128];
1079
1080                 ast_escape_quoted(name, name_buf, sizeof(name_buf));
1081                 snprintf(buf, bufsiz, "\"%s\" <%s>", name_buf, num);
1082         } else if (name) {
1083                 ast_copy_string(buf, name, bufsiz);
1084         } else if (num) {
1085                 ast_copy_string(buf, num, bufsiz);
1086         } else {
1087                 ast_copy_string(buf, unknown, bufsiz);
1088         }
1089         return buf;
1090 }
1091
1092 int ast_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen)
1093 {
1094         char *tmp;
1095         char *l = NULL, *n = NULL;
1096
1097         tmp = ast_strdupa(buf);
1098         ast_callerid_parse(tmp, &n, &l);
1099         if (n)
1100                 ast_copy_string(name, n, namelen);
1101         else
1102                 name[0] = '\0';
1103         if (l) {
1104                 ast_shrink_phone_number(l);
1105                 ast_copy_string(num, l, numlen);
1106         } else
1107                 num[0] = '\0';
1108         return 0;
1109 }
1110
1111 struct ast_value_translation {
1112         int value;
1113         const char *name;
1114         const char *description;
1115 };
1116
1117 /*! \brief Translation table for Caller ID Presentation settings */
1118 static const struct ast_value_translation pres_types[] = {
1119 /* *INDENT-OFF* */
1120         { AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED,        "allowed_not_screened",  "Presentation Allowed, Not Screened" },
1121         { AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN,     "allowed_passed_screen", "Presentation Allowed, Passed Screen" },
1122         { AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN,     "allowed_failed_screen", "Presentation Allowed, Failed Screen" },
1123         { AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER,                "allowed",               "Presentation Allowed, Network Number" },
1124
1125         { AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED,     "prohib_not_screened",   "Presentation Prohibited, Not Screened" },
1126         { AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN,  "prohib_passed_screen",  "Presentation Prohibited, Passed Screen" },
1127         { AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN,  "prohib_failed_screen",  "Presentation Prohibited, Failed Screen" },
1128         { AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER,             "prohib",                "Presentation Prohibited, Network Number" },
1129
1130         { AST_PRES_UNAVAILABLE | AST_PRES_NETWORK_NUMBER,            "unavailable",           "Number Unavailable" }, /* Default name to value conversion. */
1131         { AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_UNSCREENED,    "unavailable",           "Number Unavailable" },
1132         { AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_FAILED_SCREEN, "unavailable",           "Number Unavailable" },
1133         { AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_PASSED_SCREEN, "unavailable",           "Number Unavailable" },
1134 /* *INDENT-ON* */
1135 };
1136
1137 /*!
1138  * \brief Convert caller ID text code to value (used in config file parsing)
1139  * \param data text string from config file
1140  * \retval value AST_PRES_ from callerid.h
1141  * \retval -1 if not in table
1142  */
1143 int ast_parse_caller_presentation(const char *data)
1144 {
1145         int index;
1146         if (!data) {
1147                 return -1;
1148         }
1149
1150         for (index = 0; index < ARRAY_LEN(pres_types); ++index) {
1151                 if (!strcasecmp(pres_types[index].name, data)) {
1152                         return pres_types[index].value;
1153                 }
1154         }
1155
1156         return -1;
1157 }
1158
1159 /*!
1160  * \brief Convert caller ID pres value to explanatory string
1161  * \param data AST_PRES_ value from callerid.h
1162  * \return string for human presentation
1163  */
1164 const char *ast_describe_caller_presentation(int data)
1165 {
1166         int index;
1167
1168         for (index = 0; index < ARRAY_LEN(pres_types); ++index) {
1169                 if (pres_types[index].value == data) {
1170                         return pres_types[index].description;
1171                 }
1172         }
1173
1174         return "unknown";
1175 }
1176
1177 /*!
1178  * \brief Convert caller ID pres value to text code
1179  * \param data AST_PRES_ value from callerid.h
1180  * \return string for config file
1181  */
1182 const char *ast_named_caller_presentation(int data)
1183 {
1184         int index;
1185
1186         for (index = 0; index < ARRAY_LEN(pres_types); ++index) {
1187                 if (pres_types[index].value == data) {
1188                         return pres_types[index].name;
1189                 }
1190         }
1191
1192         return "unknown";
1193 }
1194
1195 /*! \brief Translation table for redirecting reason settings */
1196 static const struct ast_value_translation redirecting_reason_types[] = {
1197 /* *INDENT-OFF* */
1198         { AST_REDIRECTING_REASON_UNKNOWN,        "unknown",      "Unknown" },
1199         { AST_REDIRECTING_REASON_USER_BUSY,      "cfb",          "Call Forwarding Busy" },
1200         { AST_REDIRECTING_REASON_NO_ANSWER,      "cfnr",         "Call Forwarding No Reply" },
1201         { AST_REDIRECTING_REASON_UNAVAILABLE,    "unavailable",  "Callee is Unavailable" },
1202         { AST_REDIRECTING_REASON_UNCONDITIONAL,  "cfu",          "Call Forwarding Unconditional" },
1203         { AST_REDIRECTING_REASON_TIME_OF_DAY,    "time_of_day",  "Time of Day" },
1204         { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "dnd",          "Do Not Disturb" },
1205         { AST_REDIRECTING_REASON_DEFLECTION,     "deflection",   "Call Deflection" },
1206         { AST_REDIRECTING_REASON_FOLLOW_ME,      "follow_me",    "Follow Me" },
1207         { AST_REDIRECTING_REASON_OUT_OF_ORDER,   "out_of_order", "Called DTE Out-Of-Order" },
1208         { AST_REDIRECTING_REASON_AWAY,           "away",         "Callee is Away" },
1209         { AST_REDIRECTING_REASON_CALL_FWD_DTE,   "cf_dte",       "Call Forwarding By The Called DTE" },
1210         { AST_REDIRECTING_REASON_SEND_TO_VM,     "send_to_vm",   "Call is being redirected to user's voicemail" },
1211
1212         /* Convenience SIP aliases.  Alias descriptions are not used. */
1213         { AST_REDIRECTING_REASON_USER_BUSY,      "user-busy" },
1214         { AST_REDIRECTING_REASON_NO_ANSWER,      "no-answer" },
1215         { AST_REDIRECTING_REASON_UNCONDITIONAL,  "unconditional" },
1216         { AST_REDIRECTING_REASON_TIME_OF_DAY,    "time-of-day" },
1217         { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb" },
1218         { AST_REDIRECTING_REASON_FOLLOW_ME,      "follow-me" },
1219         { AST_REDIRECTING_REASON_OUT_OF_ORDER,   "out-of-service" },
1220 /* *INDENT-ON* */
1221 };
1222
1223 int ast_redirecting_reason_parse(const char *data)
1224 {
1225         int index;
1226
1227         for (index = 0; index < ARRAY_LEN(redirecting_reason_types); ++index) {
1228                 if (!strcasecmp(redirecting_reason_types[index].name, data)) {
1229                         return redirecting_reason_types[index].value;
1230                 }
1231         }
1232
1233         return -1;
1234 }
1235
1236 const char *ast_redirecting_reason_describe(int data)
1237 {
1238         int index;
1239
1240         for (index = 0; index < ARRAY_LEN(redirecting_reason_types); ++index) {
1241                 if (redirecting_reason_types[index].value == data) {
1242                         return redirecting_reason_types[index].description ?: "Redirecting reason alias-bug";
1243                 }
1244         }
1245
1246         return "not-known";
1247 }
1248
1249 const char *ast_redirecting_reason_name(const struct ast_party_redirecting_reason *data)
1250 {
1251         int index;
1252
1253         if (!ast_strlen_zero(data->str)) {
1254                 /* Use this string if it has been set. Otherwise, use the table. */
1255                 return data->str;
1256         }
1257
1258         for (index = 0; index < ARRAY_LEN(redirecting_reason_types); ++index) {
1259                 if (redirecting_reason_types[index].value == data->code) {
1260                         return redirecting_reason_types[index].name;
1261                 }
1262         }
1263
1264         return "not-known";
1265 }
1266
1267 /*! \brief Translation table for connected line update source settings */
1268 static const struct ast_value_translation connected_line_source_types[] = {
1269 /* *INDENT-OFF* */
1270         { AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN,           "unknown",           "Unknown" },
1271         { AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER,            "answer",            "Normal Call Answering" },
1272         { AST_CONNECTED_LINE_UPDATE_SOURCE_DIVERSION,         "diversion",         "Call Diversion (Deprecated, use REDIRECTING)" },
1273         { AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,          "transfer_active",   "Call Transfer(Active)" },
1274         { AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,          "transfer",          "Call Transfer(Active)" },/* Old name must come after new name. */
1275         { AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING, "transfer_alerting", "Call Transfer(Alerting)" }
1276 /* *INDENT-ON* */
1277 };
1278
1279 int ast_connected_line_source_parse(const char *data)
1280 {
1281         int index;
1282
1283         for (index = 0; index < ARRAY_LEN(connected_line_source_types); ++index) {
1284                 if (!strcasecmp(connected_line_source_types[index].name, data)) {
1285                         return connected_line_source_types[index].value;
1286                 }
1287         }
1288
1289         return -1;
1290 }
1291
1292 const char *ast_connected_line_source_describe(int data)
1293 {
1294         int index;
1295
1296         for (index = 0; index < ARRAY_LEN(connected_line_source_types); ++index) {
1297                 if (connected_line_source_types[index].value == data) {
1298                         return connected_line_source_types[index].description;
1299                 }
1300         }
1301
1302         return "not-known";
1303 }
1304
1305 const char *ast_connected_line_source_name(int data)
1306 {
1307         int index;
1308
1309         for (index = 0; index < ARRAY_LEN(connected_line_source_types); ++index) {
1310                 if (connected_line_source_types[index].value == data) {
1311                         return connected_line_source_types[index].name;
1312                 }
1313         }
1314
1315         return "not-known";
1316 }
1317
1318 /*! \brief Translation table for ast_party_name char-set settings */
1319 static const struct ast_value_translation party_name_charset_tbl[] = {
1320 /* *INDENT-OFF* */
1321         { AST_PARTY_CHAR_SET_UNKNOWN,               "unknown",      "Unknown" },
1322         { AST_PARTY_CHAR_SET_ISO8859_1,             "iso8859-1",    "ISO8859-1" },
1323         { AST_PARTY_CHAR_SET_WITHDRAWN,             "withdrawn",    "Withdrawn" },
1324         { AST_PARTY_CHAR_SET_ISO8859_2,             "iso8859-2",    "ISO8859-2" },
1325         { AST_PARTY_CHAR_SET_ISO8859_3,             "iso8859-3",    "ISO8859-3" },
1326         { AST_PARTY_CHAR_SET_ISO8859_4,             "iso8859-4",    "ISO8859-4" },
1327         { AST_PARTY_CHAR_SET_ISO8859_5,             "iso8859-5",    "ISO8859-5" },
1328         { AST_PARTY_CHAR_SET_ISO8859_7,             "iso8859-7",    "ISO8859-7" },
1329         { AST_PARTY_CHAR_SET_ISO10646_BMPSTRING,    "bmp",          "ISO10646 Bmp String" },
1330         { AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING,  "utf8",         "ISO10646 UTF-8 String" },
1331 /* *INDENT-ON* */
1332 };
1333
1334 int ast_party_name_charset_parse(const char *data)
1335 {
1336         int index;
1337
1338         for (index = 0; index < ARRAY_LEN(party_name_charset_tbl); ++index) {
1339                 if (!strcasecmp(party_name_charset_tbl[index].name, data)) {
1340                         return party_name_charset_tbl[index].value;
1341                 }
1342         }
1343
1344         return -1;
1345 }
1346
1347 const char *ast_party_name_charset_describe(int data)
1348 {
1349         int index;
1350
1351         for (index = 0; index < ARRAY_LEN(party_name_charset_tbl); ++index) {
1352                 if (party_name_charset_tbl[index].value == data) {
1353                         return party_name_charset_tbl[index].description;
1354                 }
1355         }
1356
1357         return "not-known";
1358 }
1359
1360 const char *ast_party_name_charset_str(int data)
1361 {
1362         int index;
1363
1364         for (index = 0; index < ARRAY_LEN(party_name_charset_tbl); ++index) {
1365                 if (party_name_charset_tbl[index].value == data) {
1366                         return party_name_charset_tbl[index].name;
1367                 }
1368         }
1369
1370         return "not-known";
1371 }