2 * Asterisk -- A telephony toolkit for Linux.
4 * CallerID Generation support
6 * Copyright (C) 2001, Linux Support Services, Inc.
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License.
13 * Includes code and algorithms from the Zapata library.
23 #include <asterisk/ulaw.h>
24 #include <asterisk/callerid.h>
25 #include <asterisk/logger.h>
26 #include <asterisk/fskmodem.h>
29 struct callerid_state {
44 static float dr[4], di[4];
45 static float clidsb = 8000.0 / 1200.0;
47 #define CALLERID_SPACE 2200.0 /* 2200 hz for "0" */
48 #define CALLERID_MARK 1200.0 /* 1200 hz for "1" */
50 void callerid_init(void)
52 /* Initialize stuff for inverse FFT */
53 dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
54 di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
55 dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0);
56 di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0);
59 struct callerid_state *callerid_new(void)
61 struct callerid_state *cid;
62 cid = malloc(sizeof(struct callerid_state));
63 memset(cid, 0, sizeof(*cid));
65 cid->fskd.spb = 7; /* 1200 baud */
66 cid->fskd.hdlc = 0; /* Async */
67 cid->fskd.nbit = 8; /* 8 bits */
68 cid->fskd.nstop = 1; /* 1 stop bit */
69 cid->fskd.paridad = 0; /* No parity */
70 cid->fskd.bw=1; /* Filter 800 Hz */
71 cid->fskd.f_mark_idx = 2; /* 1200 Hz */
72 cid->fskd.f_space_idx = 3; /* 2200 Hz */
73 cid->fskd.pcola = 0; /* No clue */
74 cid->fskd.cont = 0; /* Digital PLL reset */
77 memset(cid->name, 0, sizeof(cid->name));
78 memset(cid->number, 0, sizeof(cid->number));
79 cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
82 ast_log(LOG_WARNING, "Out of memory\n");
86 void callerid_get(struct callerid_state *cid, char **name, char **number, int *flags)
89 if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NUMBER))
93 if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER))
96 *number = cid->number;
99 int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len)
106 short *buf = malloc(2 * len + cid->oldlen);
109 ast_log(LOG_WARNING, "Out of memory\n");
112 memset(buf, 0, 2 * len + cid->oldlen);
113 memcpy(buf, cid->oldstuff, cid->oldlen);
114 mylen += cid->oldlen/2;
116 buf[x+cid->oldlen/2] = ast_mulaw[ubuf[x]];
119 res = fsk_serie(&cid->fskd, buf, &mylen, &b);
120 buf += (olen - mylen);
122 ast_log(LOG_NOTICE, "fsk_serie failed\n");
126 /* Ignore invalid bytes */
129 switch(cid->sawflag) {
130 case 0: /* Look for flag */
134 case 2: /* Get lead-in */
135 if ((b == 0x04) || (b == 0x80)) {
141 case 3: /* Get length */
142 /* Not a lead in. We're ready */
148 case 4: /* Retrieve message */
149 if (cid->pos >= 128) {
150 ast_log(LOG_WARNING, "Caller ID too long???\n");
153 cid->rawdata[cid->pos++] = b;
157 cid->rawdata[cid->pos] = '\0';
161 case 5: /* Check checksum */
162 if (b != (256 - (cid->cksum & 0xff))) {
163 ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
169 strcpy(cid->number, "");
170 strcpy(cid->name, "");
171 /* If we get this far we're fine. */
172 if (cid->type == 0x80) {
174 /* Go through each element and process */
175 for (x=0;x< cid->pos;) {
176 switch(cid->rawdata[x++]) {
182 res = cid->rawdata[x];
184 ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
187 memcpy(cid->number, cid->rawdata + x + 1, res);
189 cid->number[res] = '\0';
193 res = cid->rawdata[x];
195 ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
198 memcpy(cid->name, cid->rawdata + x + 1, res);
199 cid->name[res] = '\0';
202 ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]);
204 x += cid->rawdata[x];
209 strncpy(cid->number, cid->rawdata + 8, sizeof(cid->number));
213 if (!strcmp(cid->number, "P")) {
214 strcpy(cid->number, "");
215 cid->flags |= CID_PRIVATE_NUMBER;
216 } else if (!strcmp(cid->number, "O") || !strlen(cid->number)) {
217 strcpy(cid->number, "");
218 cid->flags |= CID_UNKNOWN_NUMBER;
220 if (!strcmp(cid->name, "P")) {
221 strcpy(cid->name, "");
222 cid->flags |= CID_PRIVATE_NAME;
223 } else if (!strcmp(cid->name, "O") || !strlen(cid->name)) {
224 strcpy(cid->name, "");
225 cid->flags |= CID_UNKNOWN_NAME;
230 ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
235 memcpy(cid->oldstuff, buf, mylen * 2);
236 cid->oldlen = mylen * 2;
242 void callerid_free(struct callerid_state *cid)
247 static void callerid_genmsg(char *msg, int size, char *number, char *name, int flags)
260 /* Format time and message header */
261 res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm->tm_mon + 1,
262 tm->tm_mday, tm->tm_hour, tm->tm_min);
265 if (!number || !strlen(number) || (flags & CID_UNKNOWN_NUMBER)) {
266 /* Indicate number not known */
267 res = snprintf(ptr, size, "\004\001O");
270 } else if (flags & CID_PRIVATE_NUMBER) {
271 /* Indicate number is private */
272 res = snprintf(ptr, size, "\004\001P");
276 /* Send up to 10 digits of number MAX */
279 res = snprintf(ptr, size, "\002%c", i);
289 if (!name || !strlen(name) || (flags & CID_UNKNOWN_NAME)) {
290 /* Indicate name not known */
291 res = snprintf(ptr, size, "\010\001O");
294 } else if (flags & CID_PRIVATE_NAME) {
295 /* Indicate name is private */
296 res = snprintf(ptr, size, "\010\001P");
300 /* Send up to 10 digits of number MAX */
303 res = snprintf(ptr, size, "\007%c", i);
315 static inline float callerid_getcarrier(float *cr, float *ci, int bit)
317 /* Move along. There's nothing to see here... */
319 t = *cr * dr[bit] - *ci * di[bit];
320 *ci = *cr * di[bit] + *ci * dr[bit];
323 t = 2.0 - (*cr * *cr + *ci * *ci);
329 #define PUT_BYTE(a) do { \
334 #define PUT_AUDIO_SAMPLE(y) do { \
335 int index = (short)(rint(8192.0 * (y))); \
336 *(buf++) = ast_lin2mu[index + 32768]; \
340 #define PUT_CLID_MARKMS do { \
343 PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, 1)); \
346 #define PUT_CLID_BAUD(bit) do { \
347 while(scont < clidsb) { \
348 PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, bit)); \
355 #define PUT_CLID(byte) do { \
357 unsigned char b = (byte); \
358 PUT_CLID_BAUD(0); /* Start bit */ \
359 for (z=0;z<8;z++) { \
360 PUT_CLID_BAUD(b & 1); \
363 PUT_CLID_BAUD(1); /* Stop bit */ \
366 int callerid_generate(unsigned char *buf, char *number, char *name, int flags)
370 /* Initial carriers (real/imaginary) */
375 callerid_genmsg(msg, sizeof(msg), number, name, flags);
378 /* Transmit 30 0x55's (looks like a square wave */
381 /* Send 150ms of callerid marks */
384 /* Send 0x80 indicating MDMF format */
386 /* Put length of whole message */
387 PUT_CLID(strlen(msg));
388 sum = 0x80 + strlen(msg);
389 /* Put each character of message and update checksum */
390 for (x=0;x<strlen(msg); x++) {
394 /* Send 2's compliment of sum */
395 PUT_CLID(256 - (sum & 255));
396 /* Send 50 more ms of marks */
403 void ast_shrink_phone_number(char *n)
407 if (!strchr("( )-.", n[x]))
412 int ast_isphonenumber(char *n)
416 if (!strchr("0123456789", n[x]))
421 int ast_callerid_parse(char *instr, char **name, char **location)
425 /* Try for "name" <location> format or
426 name <location> format */
427 if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
428 /* Found the location */
432 if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) {
433 /* Get name out of quotes */
439 /* Just trim off any trailing spaces */
441 while(strlen(instr) && (instr[strlen(instr) - 1] < 33))
442 instr[strlen(instr) - 1] = '\0';
443 /* And leading spaces */
444 while(**name && (**name < 33))
449 /* Assume it's just a location */
457 int ast_callerid_generate(unsigned char *buf, char *callerid)
462 return callerid_generate(buf, NULL, NULL, 0);
463 strncpy(tmp, callerid, sizeof(tmp));
464 if (ast_callerid_parse(tmp, &n, &l)) {
465 ast_log(LOG_WARNING, "Unable to parse '%s' into CallerID name & number\n", callerid);
466 return callerid_generate(buf, NULL, NULL, 0);
468 ast_shrink_phone_number(l);
469 if (!n && (!ast_isphonenumber(l)))
470 return callerid_generate(buf, NULL, NULL, 0);
471 if (!ast_isphonenumber(l))
472 return callerid_generate(buf, NULL, n, 0);
473 return callerid_generate(buf, l, n, 0);