Callerid fix and warning cleanup (bug #367 & #356)
[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         if (cid) {
114                 memset(cid, 0, sizeof(struct callerid_state));
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                                                 case 22: /* Something French */
278                                                         break;
279                                                 default:
280                                                         ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]);
281                                                 }
282                                                 x += cid->rawdata[x];
283                                                 x++;
284                                         }
285                                 } else {
286                                         /* SDMF */
287                                         strncpy(cid->number, cid->rawdata + 8, sizeof(cid->number)-1);
288                                 }
289                                 /* Update flags */
290                                 cid->flags = 0;
291                                 if (!strcmp(cid->number, "P")) {
292                                         strcpy(cid->number, "");
293                                         cid->flags |= CID_PRIVATE_NUMBER;
294                                 } else if (!strcmp(cid->number, "O") || !strlen(cid->number)) {
295                                         strcpy(cid->number, "");
296                                         cid->flags |= CID_UNKNOWN_NUMBER;
297                                 }
298                                 if (!strcmp(cid->name, "P")) {
299                                         strcpy(cid->name, "");
300                                         cid->flags |= CID_PRIVATE_NAME;
301                                 } else if (!strcmp(cid->name, "O") || !strlen(cid->name)) {
302                                         strcpy(cid->name, "");
303                                         cid->flags |= CID_UNKNOWN_NAME;
304                                 }
305                                 return 1;
306                                 break;
307                         default:
308                                 ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
309                         }
310                 }
311         }
312         if (mylen) {
313                 memcpy(cid->oldstuff, buf, mylen * 2);
314                 cid->oldlen = mylen * 2;
315         } else
316                 cid->oldlen = 0;
317         free(obuf);
318         return 0;
319 }
320
321 void callerid_free(struct callerid_state *cid)
322 {
323         free(cid);
324 }
325
326 static int callerid_genmsg(char *msg, int size, char *number, char *name, int flags)
327 {
328         time_t t;
329         struct tm tm;
330         char *ptr;
331         int res;
332         int i,x;
333         /* Get the time */
334         time(&t);
335         localtime_r(&t,&tm);
336         
337         ptr = msg;
338         
339         /* Format time and message header */
340         res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm.tm_mon + 1,
341                                 tm.tm_mday, tm.tm_hour, tm.tm_min);
342         size -= res;
343         ptr += res;
344         if (!number || !strlen(number) || (flags & CID_UNKNOWN_NUMBER)) {
345                 /* Indicate number not known */
346                 res = snprintf(ptr, size, "\004\001O");
347                 size -= res;
348                 ptr += res;
349         } else if (flags & CID_PRIVATE_NUMBER) {
350                 /* Indicate number is private */
351                 res = snprintf(ptr, size, "\004\001P");
352                 size -= res;
353                 ptr += res;
354         } else {
355                 /* Send up to 16 digits of number MAX */
356                 i = strlen(number);
357                 if (i > 16) i = 16;
358                 res = snprintf(ptr, size, "\002%c", i);
359                 size -= res;
360                 ptr += res;
361                 for (x=0;x<i;x++)
362                         ptr[x] = number[x];
363                 ptr[i] = '\0';
364                 ptr += i;
365                 size -= i;
366         }
367
368         if (!name || !strlen(name) || (flags & CID_UNKNOWN_NAME)) {
369                 /* Indicate name not known */
370                 res = snprintf(ptr, size, "\010\001O");
371                 size -= res;
372                 ptr += res;
373         } else if (flags & CID_PRIVATE_NAME) {
374                 /* Indicate name is private */
375                 res = snprintf(ptr, size, "\010\001P");
376                 size -= res;
377                 ptr += res;
378         } else {
379                 /* Send up to 16 digits of name MAX */
380                 i = strlen(name);
381                 if (i > 16) i = 16;
382                 res = snprintf(ptr, size, "\007%c", i);
383                 size -= res;
384                 ptr += res;
385                 for (x=0;x<i;x++)
386                         ptr[x] = name[x];
387                 ptr[i] = '\0';
388                 ptr += i;
389                 size -= i;
390         }
391         return (ptr - msg);
392         
393 }
394
395 int vmwi_generate(unsigned char *buf, int active, int mdmf, int codec)
396 {
397         unsigned char msg[256];
398         int len=0;
399         int sum;
400         int x;
401         int bytes = 0;
402         float cr = 1.0;
403         float ci = 0.0;
404         float scont = 0.0;
405         if (mdmf) {
406                 /* MDMF Message waiting */
407                 msg[len++] = 0x82;
408                 /* Length is 3 */
409                 msg[len++] = 3;
410                 /* IE is "Message Waiting Parameter" */
411                 msg[len++] = 0xb;
412                 /* Length of IE is one */
413                 msg[len++] = 1;
414                 /* Active or not */
415                 if (active)
416                         msg[len++] = 0xff;
417                 else
418                         msg[len++] = 0x00;
419         } else {
420                 /* SDMF Message waiting */
421                 msg[len++] = 0x6;
422                 /* Length is 3 */
423                 msg[len++] = 3;
424                 if (active) {
425                         msg[len++] = 0x42;
426                         msg[len++] = 0x42;
427                         msg[len++] = 0x42;
428                 } else {
429                         msg[len++] = 0x6f;
430                         msg[len++] = 0x6f;
431                         msg[len++] = 0x6f;
432                 }
433         }
434         sum = 0;
435         for (x=0;x<len;x++)
436                 sum += msg[x];
437         sum = (256 - (sum & 255));
438         msg[len++] = sum;
439         /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
440         for (x=0;x<30;x++)
441                 PUT_CLID(0x55);
442         /* Send 170ms of callerid marks */
443         for (x=0;x<170;x++)
444                 PUT_CLID_MARKMS;
445         for (x=0;x<len;x++) {
446                 PUT_CLID(msg[x]);
447         }
448         /* Send 50 more ms of marks */
449         for (x=0;x<50;x++)
450                 PUT_CLID_MARKMS;
451         return bytes;
452 }
453
454 int callerid_generate(unsigned char *buf, char *number, char *name, int flags, int callwaiting, int codec)
455 {
456         int bytes=0;
457         int x, sum;
458         int len;
459         /* Initial carriers (real/imaginary) */
460         float cr = 1.0;
461         float ci = 0.0;
462         float scont = 0.0;
463         unsigned char msg[256];
464         len = callerid_genmsg(msg, sizeof(msg), number, name, flags);
465         if (!callwaiting) {
466                 /* Wait a half a second */
467                 for (x=0;x<4000;x++)
468                         PUT_BYTE(0x7f);
469                 /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
470                 for (x=0;x<30;x++)
471                         PUT_CLID(0x55);
472         }
473         /* Send 150ms of callerid marks */
474         for (x=0;x<150;x++)
475                 PUT_CLID_MARKMS;
476         /* Send 0x80 indicating MDMF format */
477         PUT_CLID(0x80);
478         /* Put length of whole message */
479         PUT_CLID(len);
480         sum = 0x80 + strlen(msg);
481         /* Put each character of message and update checksum */
482         for (x=0;x<len; x++) {
483                 PUT_CLID(msg[x]);
484                 sum += msg[x];
485         }
486         /* Send 2's compliment of sum */
487         PUT_CLID(256 - (sum & 255));
488
489         /* Send 50 more ms of marks */
490         for (x=0;x<50;x++)
491                 PUT_CLID_MARKMS;
492         
493         return bytes;
494 }
495
496 void ast_shrink_phone_number(char *n)
497 {
498         int x,y=0;
499         for (x=0;n[x];x++)
500                 if (!strchr("( )-.", n[x]))
501                         n[y++] = n[x];
502         n[y] = '\0';
503 }
504
505 int ast_isphonenumber(char *n)
506 {
507         int x;
508         if (!n || !strlen(n))
509                 return 0;
510         for (x=0;n[x];x++)
511                 if (!strchr("0123456789*#", n[x]))
512                         return 0;
513         return 1;
514 }
515
516 int ast_callerid_parse(char *instr, char **name, char **location)
517 {
518         char *ns, *ne;
519         char *ls, *le;
520         char tmp[256];
521         /* Try for "name" <location> format or 
522            name <location> format */
523         if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
524                 /* Found the location */
525                 *le = '\0';
526                 *ls = '\0';
527                 *location = ls + 1;
528                 if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) {
529                         /* Get name out of quotes */
530                         *ns = '\0';
531                         *ne = '\0';
532                         *name = ns + 1;
533                         return 0;
534                 } else {
535                         /* Just trim off any trailing spaces */
536                         *name = instr;
537                         while(strlen(instr) && (instr[strlen(instr) - 1] < 33))
538                                 instr[strlen(instr) - 1] = '\0';
539                         /* And leading spaces */
540                         while(**name && (**name < 33))
541                                 name++;
542                         return 0;
543                 }
544         } else {
545                 strncpy(tmp, instr, sizeof(tmp)-1);
546                 ast_shrink_phone_number(tmp);
547                 if (ast_isphonenumber(tmp)) {
548                         /* Assume it's just a location */
549                         *name = NULL;
550                         *location = instr;
551                 } else {
552                         /* Assume it's just a name.  Make sure it's not quoted though */
553                         *name = instr;
554                         while(*(*name) && ((*(*name) < 33) || (*(*name) == '\"'))) (*name)++;
555                         ne = *name + strlen(*name) - 1;
556                         while((ne > *name) && ((*ne < 33) || (*ne == '\"'))) { *ne = '\0'; ne--; }
557                         *location = NULL;
558                 }
559                 return 0;
560         }
561         return -1;
562 }
563
564 static int __ast_callerid_generate(unsigned char *buf, char *callerid, int callwaiting, int codec)
565 {
566         char tmp[256];
567         char *n, *l;
568         if (!callerid)
569                 return callerid_generate(buf, NULL, NULL, 0, callwaiting, codec);
570         strncpy(tmp, callerid, sizeof(tmp)-1);
571         if (ast_callerid_parse(tmp, &n, &l)) {
572                 ast_log(LOG_WARNING, "Unable to parse '%s' into CallerID name & number\n", callerid);
573                 return callerid_generate(buf, NULL, NULL, 0, callwaiting, codec);
574         }
575         if (l)
576                 ast_shrink_phone_number(l);
577         if (!ast_isphonenumber(l))
578                 return callerid_generate(buf, NULL, n, 0, callwaiting, codec);
579         return callerid_generate(buf, l, n, 0, callwaiting, codec);
580 }
581
582 int ast_callerid_generate(unsigned char *buf, char *callerid, int codec)
583 {
584         return __ast_callerid_generate(buf, callerid, 0, codec);
585 }
586
587 int ast_callerid_callwaiting_generate(unsigned char *buf, char *callerid, int codec)
588 {
589         return __ast_callerid_generate(buf, callerid, 1, codec);
590 }