Version 0.3.0 from FTP
[asterisk/asterisk.git] / callerid.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * CallerID Generation support 
5  * 
6  * Copyright (C) 2001, Linux Support Services, Inc.
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License.
12  *
13  * Includes code and algorithms from the Zapata library.
14  *
15  */
16
17 #include <time.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <math.h>
23 #include <asterisk/ulaw.h>
24 #include <asterisk/alaw.h>
25 #include <asterisk/frame.h>
26 #include <asterisk/callerid.h>
27 #include <asterisk/logger.h>
28 #include <asterisk/fskmodem.h>
29
30 struct callerid_state {
31         fsk_data fskd;
32         char rawdata[256];
33         short oldstuff[160];
34         int oldlen;
35         int pos;
36         int type;
37         int cksum;
38         char name[64];
39         char number[64];
40         int flags;
41         int sawflag;
42         int len;
43 };
44
45
46 float cid_dr[4], cid_di[4];
47 float clidsb = 8000.0 / 1200.0;
48 float sasdr, sasdi;
49 float casdr1, casdi1, casdr2, casdi2;
50
51 #define CALLERID_SPACE  2200.0          /* 2200 hz for "0" */
52 #define CALLERID_MARK   1200.0          /* 1200 hz for "1" */
53 #define SAS_FREQ                 440.0
54 #define CAS_FREQ1               2130.0
55 #define CAS_FREQ2               2750.0
56
57 static inline void gen_tones(unsigned char *buf, int len, int codec, float ddr1, float ddi1, float ddr2, float ddi2, float *cr1, float *ci1, float *cr2, float *ci2)
58 {
59         int x;
60         float t;
61         for (x=0;x<len;x++) {
62                 t = *cr1 * ddr1 - *ci1 * ddi1;
63                 *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
64                 *cr1 = t;
65                 t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
66                 *cr1 *= t;
67                 *ci1 *= t;      
68
69                 t = *cr2 * ddr2 - *ci2 * ddi2;
70                 *ci2 = *cr2 * ddi2 + *ci2 * ddr2;
71                 *cr2 = t;
72                 t = 2.0 - (*cr2 * *cr2 + *ci2 * *ci2);
73                 *cr2 *= t;
74                 *ci2 *= t;      
75                 buf[x] = AST_LIN2X((*cr1 + *cr2) * 8192.0);
76         }
77 }
78
79 static inline void gen_tone(unsigned char *buf, int len, int codec, float ddr1, float ddi1, float *cr1, float *ci1)
80 {
81         int x;
82         float t;
83         for (x=0;x<len;x++) {
84                 t = *cr1 * ddr1 - *ci1 * ddi1;
85                 *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
86                 *cr1 = t;
87                 t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
88                 *cr1 *= t;
89                 *ci1 *= t;      
90                 buf[x] = AST_LIN2X(*cr1 * 8192.0);
91         }
92 }
93
94 void callerid_init(void)
95 {
96         /* Initialize stuff for inverse FFT */
97         cid_dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
98         cid_di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
99         cid_dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0);
100         cid_di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0);
101         sasdr = cos(SAS_FREQ * 2.0 * M_PI / 8000.0);
102         sasdi = sin(SAS_FREQ * 2.0 * M_PI / 8000.0);
103         casdr1 = cos(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
104         casdi1 = sin(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
105         casdr2 = cos(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
106         casdi2 = sin(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
107 }
108
109 struct callerid_state *callerid_new(void)
110 {
111         struct callerid_state *cid;
112         cid = malloc(sizeof(struct callerid_state));
113         memset(cid, 0, sizeof(struct callerid_state));
114         if (cid) {
115                 cid->fskd.spb = 7;              /* 1200 baud */
116                 cid->fskd.hdlc = 0;             /* Async */
117                 cid->fskd.nbit = 8;             /* 8 bits */
118                 cid->fskd.nstop = 1;    /* 1 stop bit */
119                 cid->fskd.paridad = 0;  /* No parity */
120                 cid->fskd.bw=1;                 /* Filter 800 Hz */
121                 cid->fskd.f_mark_idx =  2;      /* 1200 Hz */
122                 cid->fskd.f_space_idx = 3;      /* 2200 Hz */
123                 cid->fskd.pcola = 0;            /* No clue */
124                 cid->fskd.cont = 0;                     /* Digital PLL reset */
125                 cid->fskd.x0 = 0.0;
126                 cid->fskd.state = 0;
127                 memset(cid->name, 0, sizeof(cid->name));
128                 memset(cid->number, 0, sizeof(cid->number));
129                 cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
130                 cid->pos = 0;
131         } else
132                 ast_log(LOG_WARNING, "Out of memory\n");
133         return cid;
134 }
135
136 void callerid_get(struct callerid_state *cid, char **name, char **number, int *flags)
137 {
138         *flags = cid->flags;
139         if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NUMBER))
140                 *name = NULL;
141         else
142                 *name = cid->name;
143         if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER))
144                 *number = NULL;
145         else
146                 *number = cid->number;
147 }
148
149 int ast_gen_cas(unsigned char *outbuf, int sendsas, int len, int codec)
150 {
151         int pos = 0;
152         int saslen=2400;
153         float cr1 = 1.0;
154         float ci1 = 0.0;
155         float cr2 = 1.0;
156         float ci2 = 0.0;
157         if (sendsas) {
158                 if (len < saslen)
159                         return -1;
160                 gen_tone(outbuf, saslen, codec, sasdr, sasdi, &cr1, &ci1);
161                 len -= saslen;
162                 pos += saslen;
163                 cr2 = cr1;
164                 ci2 = ci1;
165         }
166         gen_tones(outbuf + pos, len, codec, casdr1, casdi1, casdr2, casdi2, &cr1, &ci1, &cr2, &ci2);
167         return 0;
168 }
169
170 int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len, int codec)
171 {
172         int mylen = len;
173         int olen;
174         int b = 'X';
175         int res;
176         int x;
177         short *buf = malloc(2 * len + cid->oldlen);
178         short *obuf = buf;
179         if (!buf) {
180                 ast_log(LOG_WARNING, "Out of memory\n");
181                 return -1;
182         }
183         memset(buf, 0, 2 * len + cid->oldlen);
184         memcpy(buf, cid->oldstuff, cid->oldlen);
185         mylen += cid->oldlen/2;
186         for (x=0;x<len;x++) 
187                 buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
188         while(mylen >= 80) {
189                 olen = mylen;
190                 res = fsk_serie(&cid->fskd, buf, &mylen, &b);
191                 if (mylen < 0) {
192                         ast_log(LOG_ERROR, "fsk_serie made mylen < 0 (%d)\n", mylen);
193                         return -1;
194                 }
195                 buf += (olen - mylen);
196                 if (res < 0) {
197                         ast_log(LOG_NOTICE, "fsk_serie failed\n");
198                         return -1;
199                 }
200                 if (res == 1) {
201                         /* Ignore invalid bytes */
202                         if (b > 0xff)
203                                 continue;
204                         switch(cid->sawflag) {
205                         case 0: /* Look for flag */
206                                 if (b == 'U')
207                                         cid->sawflag = 2;
208                                 break;
209                         case 2: /* Get lead-in */
210                                 if ((b == 0x04) || (b == 0x80)) {
211                                         cid->type = b;
212                                         cid->sawflag = 3;
213                                         cid->cksum = b;
214                                 }
215                                 break;
216                         case 3: /* Get length */
217                                 /* Not a lead in.  We're ready  */
218                                 cid->sawflag = 4;
219                                 cid->len = b;
220                                 cid->pos = 0;
221                                 cid->cksum += b;
222                                 break;
223                         case 4: /* Retrieve message */
224                                 if (cid->pos >= 128) {
225                                         ast_log(LOG_WARNING, "Caller ID too long???\n");
226                                         return -1;
227                                 }
228                                 cid->rawdata[cid->pos++] = b;
229                                 cid->len--;
230                                 cid->cksum += b;
231                                 if (!cid->len) {
232                                         cid->rawdata[cid->pos] = '\0';
233                                         cid->sawflag = 5;
234                                 }
235                                 break;
236                         case 5: /* Check checksum */
237                                 if (b != (256 - (cid->cksum & 0xff))) {
238                                         ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
239                                         /* Try again */
240                                         cid->sawflag = 0;
241                                         break;
242                                 }
243                 
244                                 strcpy(cid->number, "");
245                                 strcpy(cid->name, "");
246                                 /* If we get this far we're fine.  */
247                                 if (cid->type == 0x80) {
248                                         /* MDMF */
249                                         /* Go through each element and process */
250                                         for (x=0;x< cid->pos;) {
251                                                 switch(cid->rawdata[x++]) {
252                                                 case 1:
253                                                         /* Date */
254                                                         break;
255                                                 case 2: /* Number */
256                                                 case 3: /* Number (for Zebble) */
257                                                 case 4: /* Number */
258                                                         res = cid->rawdata[x];
259                                                         if (res > 32) {
260                                                                 ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
261                                                                 res = 32; 
262                                                         }
263                                                         memcpy(cid->number, cid->rawdata + x + 1, res);
264                                                         /* Null terminate */
265                                                         cid->number[res] = '\0';
266                                                         break;
267                                                 case 7: /* Name */
268                                                 case 8: /* Name */
269                                                         res = cid->rawdata[x];
270                                                         if (res > 32) {
271                                                                 ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
272                                                                 res = 32; 
273                                                         }
274                                                         memcpy(cid->name, cid->rawdata + x + 1, res);
275                                                         cid->name[res] = '\0';
276                                                         break;
277                                                 default:
278                                                         ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]);
279                                                 }
280                                                 x += cid->rawdata[x];
281                                                 x++;
282                                         }
283                                 } else {
284                                         /* SDMF */
285                                         strncpy(cid->number, cid->rawdata + 8, sizeof(cid->number)-1);
286                                 }
287                                 /* Update flags */
288                                 cid->flags = 0;
289                                 if (!strcmp(cid->number, "P")) {
290                                         strcpy(cid->number, "");
291                                         cid->flags |= CID_PRIVATE_NUMBER;
292                                 } else if (!strcmp(cid->number, "O") || !strlen(cid->number)) {
293                                         strcpy(cid->number, "");
294                                         cid->flags |= CID_UNKNOWN_NUMBER;
295                                 }
296                                 if (!strcmp(cid->name, "P")) {
297                                         strcpy(cid->name, "");
298                                         cid->flags |= CID_PRIVATE_NAME;
299                                 } else if (!strcmp(cid->name, "O") || !strlen(cid->name)) {
300                                         strcpy(cid->name, "");
301                                         cid->flags |= CID_UNKNOWN_NAME;
302                                 }
303                                 return 1;
304                                 break;
305                         default:
306                                 ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
307                         }
308                 }
309         }
310         if (mylen) {
311                 memcpy(cid->oldstuff, buf, mylen * 2);
312                 cid->oldlen = mylen * 2;
313         } else
314                 cid->oldlen = 0;
315         free(obuf);
316         return 0;
317 }
318
319 void callerid_free(struct callerid_state *cid)
320 {
321         free(cid);
322 }
323
324 static int callerid_genmsg(char *msg, int size, char *number, char *name, int flags)
325 {
326         time_t t;
327         struct tm *tm;
328         char *ptr;
329         int res;
330         int i,x;
331         /* Get the time */
332         time(&t);
333         tm = localtime(&t);
334         
335         ptr = msg;
336         
337         /* Format time and message header */
338         res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm->tm_mon + 1,
339                                 tm->tm_mday, tm->tm_hour, tm->tm_min);
340         size -= res;
341         ptr += res;
342         if (!number || !strlen(number) || (flags & CID_UNKNOWN_NUMBER)) {
343                 /* Indicate number not known */
344                 res = snprintf(ptr, size, "\004\001O");
345                 size -= res;
346                 ptr += res;
347         } else if (flags & CID_PRIVATE_NUMBER) {
348                 /* Indicate number is private */
349                 res = snprintf(ptr, size, "\004\001P");
350                 size -= res;
351                 ptr += res;
352         } else {
353                 /* Send up to 16 digits of number MAX */
354                 i = strlen(number);
355                 if (i > 16) i = 16;
356                 res = snprintf(ptr, size, "\002%c", i);
357                 size -= res;
358                 ptr += res;
359                 for (x=0;x<i;x++)
360                         ptr[x] = number[x];
361                 ptr[i] = '\0';
362                 ptr += i;
363                 size -= i;
364         }
365
366         if (!name || !strlen(name) || (flags & CID_UNKNOWN_NAME)) {
367                 /* Indicate name not known */
368                 res = snprintf(ptr, size, "\010\001O");
369                 size -= res;
370                 ptr += res;
371         } else if (flags & CID_PRIVATE_NAME) {
372                 /* Indicate name is private */
373                 res = snprintf(ptr, size, "\010\001P");
374                 size -= res;
375                 ptr += res;
376         } else {
377                 /* Send up to 16 digits of name MAX */
378                 i = strlen(name);
379                 if (i > 16) i = 16;
380                 res = snprintf(ptr, size, "\007%c", i);
381                 size -= res;
382                 ptr += res;
383                 for (x=0;x<i;x++)
384                         ptr[x] = name[x];
385                 ptr[i] = '\0';
386                 ptr += i;
387                 size -= i;
388         }
389         return (ptr - msg);
390         
391 }
392
393 int vmwi_generate(unsigned char *buf, int active, int mdmf, int codec)
394 {
395         unsigned char msg[256];
396         int len=0;
397         int sum;
398         int x;
399         int bytes = 0;
400         float cr = 1.0;
401         float ci = 0.0;
402         float scont = 0.0;
403         if (mdmf) {
404                 /* MDMF Message waiting */
405                 msg[len++] = 0x82;
406                 /* Length is 3 */
407                 msg[len++] = 3;
408                 /* IE is "Message Waiting Parameter" */
409                 msg[len++] = 0xb;
410                 /* Length of IE is one */
411                 msg[len++] = 1;
412                 /* Active or not */
413                 if (active)
414                         msg[len++] = 0xff;
415                 else
416                         msg[len++] = 0x00;
417         } else {
418                 /* SDMF Message waiting */
419                 msg[len++] = 0x6;
420                 /* Length is 3 */
421                 msg[len++] = 3;
422                 if (active) {
423                         msg[len++] = 0x42;
424                         msg[len++] = 0x42;
425                         msg[len++] = 0x42;
426                 } else {
427                         msg[len++] = 0x6f;
428                         msg[len++] = 0x6f;
429                         msg[len++] = 0x6f;
430                 }
431         }
432         sum = 0;
433         for (x=0;x<len;x++)
434                 sum += msg[x];
435         sum = (256 - (sum & 255));
436         msg[len++] = sum;
437         /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
438         for (x=0;x<30;x++)
439                 PUT_CLID(0x55);
440         /* Send 170ms of callerid marks */
441         for (x=0;x<170;x++)
442                 PUT_CLID_MARKMS;
443         for (x=0;x<len;x++) {
444                 PUT_CLID(msg[x]);
445         }
446         /* Send 50 more ms of marks */
447         for (x=0;x<50;x++)
448                 PUT_CLID_MARKMS;
449         return bytes;
450 }
451
452 int callerid_generate(unsigned char *buf, char *number, char *name, int flags, int callwaiting, int codec)
453 {
454         int bytes=0;
455         int x, sum;
456         int len;
457         /* Initial carriers (real/imaginary) */
458         float cr = 1.0;
459         float ci = 0.0;
460         float scont = 0.0;
461         unsigned char msg[256];
462         len = callerid_genmsg(msg, sizeof(msg), number, name, flags);
463         if (!callwaiting) {
464                 /* Wait a half a second */
465                 for (x=0;x<4000;x++)
466                         PUT_BYTE(0x7f);
467                 /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
468                 for (x=0;x<30;x++)
469                         PUT_CLID(0x55);
470         }
471         /* Send 150ms of callerid marks */
472         for (x=0;x<150;x++)
473                 PUT_CLID_MARKMS;
474         /* Send 0x80 indicating MDMF format */
475         PUT_CLID(0x80);
476         /* Put length of whole message */
477         PUT_CLID(len);
478         sum = 0x80 + strlen(msg);
479         /* Put each character of message and update checksum */
480         for (x=0;x<len; x++) {
481                 PUT_CLID(msg[x]);
482                 sum += msg[x];
483         }
484         /* Send 2's compliment of sum */
485         PUT_CLID(256 - (sum & 255));
486
487         /* Send 50 more ms of marks */
488         for (x=0;x<50;x++)
489                 PUT_CLID_MARKMS;
490         
491         return bytes;
492 }
493
494 void ast_shrink_phone_number(char *n)
495 {
496         int x,y=0;
497         for (x=0;n[x];x++)
498                 if (!strchr("( )-.", n[x]))
499                         n[y++] = n[x];
500         n[y] = '\0';
501 }
502
503 int ast_isphonenumber(char *n)
504 {
505         int x;
506         if (!n || !strlen(n))
507                 return 0;
508         for (x=0;n[x];x++)
509                 if (!strchr("0123456789", n[x]))
510                         return 0;
511         return 1;
512 }
513
514 int ast_callerid_parse(char *instr, char **name, char **location)
515 {
516         char *ns, *ne;
517         char *ls, *le;
518         char tmp[256];
519         /* Try for "name" <location> format or 
520            name <location> format */
521         if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
522                 /* Found the location */
523                 *le = '\0';
524                 *ls = '\0';
525                 *location = ls + 1;
526                 if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) {
527                         /* Get name out of quotes */
528                         *ns = '\0';
529                         *ne = '\0';
530                         *name = ns + 1;
531                         return 0;
532                 } else {
533                         /* Just trim off any trailing spaces */
534                         *name = instr;
535                         while(strlen(instr) && (instr[strlen(instr) - 1] < 33))
536                                 instr[strlen(instr) - 1] = '\0';
537                         /* And leading spaces */
538                         while(**name && (**name < 33))
539                                 name++;
540                         return 0;
541                 }
542         } else {
543                 strncpy(tmp, instr, sizeof(tmp)-1);
544                 ast_shrink_phone_number(tmp);
545                 if (ast_isphonenumber(tmp)) {
546                         /* Assume it's just a location */
547                         *name = NULL;
548                         *location = instr;
549                 } else {
550                         /* Assume it's just a name */
551                         *name = instr;
552                         *location = NULL;
553                 }
554                 return 0;
555         }
556         return -1;
557 }
558
559 static int __ast_callerid_generate(unsigned char *buf, char *callerid, int callwaiting, int codec)
560 {
561         char tmp[256];
562         char *n, *l;
563         if (!callerid)
564                 return callerid_generate(buf, NULL, NULL, 0, callwaiting, codec);
565         strncpy(tmp, callerid, sizeof(tmp)-1);
566         if (ast_callerid_parse(tmp, &n, &l)) {
567                 ast_log(LOG_WARNING, "Unable to parse '%s' into CallerID name & number\n", callerid);
568                 return callerid_generate(buf, NULL, NULL, 0, callwaiting, codec);
569         }
570         if (l)
571                 ast_shrink_phone_number(l);
572         if (!ast_isphonenumber(l))
573                 return callerid_generate(buf, NULL, n, 0, callwaiting, codec);
574         return callerid_generate(buf, l, n, 0, callwaiting, codec);
575 }
576
577 int ast_callerid_generate(unsigned char *buf, char *callerid, int codec)
578 {
579         return __ast_callerid_generate(buf, callerid, 0, codec);
580 }
581
582 int ast_callerid_callwaiting_generate(unsigned char *buf, char *callerid, int codec)
583 {
584         return __ast_callerid_generate(buf, callerid, 1, codec);
585 }