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>
31
struct callerid_state {
47
float cid_dr[4], cid_di[4];
48
float clidsb = 8000.0 / 1200.0;
50
#define CALLERID_SPACE 2200.0 /* 2200 hz for "0" */
51
#define CALLERID_MARK 1200.0 /* 1200 hz for "1" */
53
void callerid_init(void)
55
/* Initialize stuff for inverse FFT */
56
cid_dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
57
cid_di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
58
cid_dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0);
59
cid_di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0);
62
struct callerid_state *callerid_new(void)
64
struct callerid_state *cid;
65
cid = malloc(sizeof(struct callerid_state));
66
memset(cid, 0, sizeof(struct callerid_state));
68
cid->fskd.spb = 7; /* 1200 baud */
69
cid->fskd.hdlc = 0; /* Async */
70
cid->fskd.nbit = 8; /* 8 bits */
71
cid->fskd.nstop = 1; /* 1 stop bit */
72
cid->fskd.paridad = 0; /* No parity */
73
cid->fskd.bw=1; /* Filter 800 Hz */
74
cid->fskd.f_mark_idx = 2; /* 1200 Hz */
75
cid->fskd.f_space_idx = 3; /* 2200 Hz */
76
cid->fskd.pcola = 0; /* No clue */
77
cid->fskd.cont = 0; /* Digital PLL reset */
80
memset(cid->name, 0, sizeof(cid->name));
81
memset(cid->number, 0, sizeof(cid->number));
82
cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
85
ast_log(LOG_WARNING, "Out of memory\n");
89
void callerid_get(struct callerid_state *cid, char **name, char **number, int *flags)
92
if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NUMBER))
96
if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER))
99
*number = cid->number;
102
int ast_gen_cas(unsigned char *outbuf, int sendsas, int len)
112
if (cnt > sizeof(sas))
114
memcpy(outbuf + pos, sas, cnt);
122
if (cnt > sizeof(cas))
124
memcpy(outbuf + pos, cas, cnt);
131
int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len)
138
short *buf = malloc(2 * len + cid->oldlen);
141
ast_log(LOG_WARNING, "Out of memory\n");
144
memset(buf, 0, 2 * len + cid->oldlen);
145
memcpy(buf, cid->oldstuff, cid->oldlen);
146
mylen += cid->oldlen/2;
148
buf[x+cid->oldlen/2] = AST_MULAW(ubuf[x]);
151
res = fsk_serie(&cid->fskd, buf, &mylen, &b);
153
ast_log(LOG_ERROR, "fsk_serie made mylen < 0 (%d)\n", mylen);
156
buf += (olen - mylen);
158
ast_log(LOG_NOTICE, "fsk_serie failed\n");
162
/* Ignore invalid bytes */
165
switch(cid->sawflag) {
166
case 0: /* Look for flag */
170
case 2: /* Get lead-in */
171
if ((b == 0x04) || (b == 0x80)) {
177
case 3: /* Get length */
178
/* Not a lead in. We're ready */
184
case 4: /* Retrieve message */
185
if (cid->pos >= 128) {
186
ast_log(LOG_WARNING, "Caller ID too long???\n");
189
cid->rawdata[cid->pos++] = b;
193
cid->rawdata[cid->pos] = '\0';
197
case 5: /* Check checksum */
198
if (b != (256 - (cid->cksum & 0xff))) {
199
ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
205
strcpy(cid->number, "");
206
strcpy(cid->name, "");
207
/* If we get this far we're fine. */
208
if (cid->type == 0x80) {
210
/* Go through each element and process */
211
for (x=0;x< cid->pos;) {
212
switch(cid->rawdata[x++]) {
218
res = cid->rawdata[x];
220
ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
223
memcpy(cid->number, cid->rawdata + x + 1, res);
225
cid->number[res] = '\0';
229
res = cid->rawdata[x];
231
ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
234
memcpy(cid->name, cid->rawdata + x + 1, res);
235
cid->name[res] = '\0';
238
ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]);
240
x += cid->rawdata[x];
245
strncpy(cid->number, cid->rawdata + 8, sizeof(cid->number)-1);
249
if (!strcmp(cid->number, "P")) {
250
strcpy(cid->number, "");
251
cid->flags |= CID_PRIVATE_NUMBER;
252
} else if (!strcmp(cid->number, "O") || !strlen(cid->number)) {
253
strcpy(cid->number, "");
254
cid->flags |= CID_UNKNOWN_NUMBER;
256
if (!strcmp(cid->name, "P")) {
257
strcpy(cid->name, "");
258
cid->flags |= CID_PRIVATE_NAME;
259
} else if (!strcmp(cid->name, "O") || !strlen(cid->name)) {
260
strcpy(cid->name, "");
261
cid->flags |= CID_UNKNOWN_NAME;
266
ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
271
memcpy(cid->oldstuff, buf, mylen * 2);
272
cid->oldlen = mylen * 2;
279
void callerid_free(struct callerid_state *cid)
284
static void callerid_genmsg(char *msg, int size, char *number, char *name, int flags)
297
/* Format time and message header */
298
res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm->tm_mon + 1,
299
tm->tm_mday, tm->tm_hour, tm->tm_min);
302
if (!number || !strlen(number) || (flags & CID_UNKNOWN_NUMBER)) {
303
/* Indicate number not known */
304
res = snprintf(ptr, size, "\004\001O");
307
} else if (flags & CID_PRIVATE_NUMBER) {
308
/* Indicate number is private */
309
res = snprintf(ptr, size, "\004\001P");
313
/* Send up to 10 digits of number MAX */
316
res = snprintf(ptr, size, "\002%c", i);
326
if (!name || !strlen(name) || (flags & CID_UNKNOWN_NAME)) {
327
/* Indicate name not known */
328
res = snprintf(ptr, size, "\010\001O");
331
} else if (flags & CID_PRIVATE_NAME) {
332
/* Indicate name is private */
333
res = snprintf(ptr, size, "\010\001P");
337
/* Send up to 10 digits of number MAX */
340
res = snprintf(ptr, size, "\007%c", i);
352
int callerid_generate(unsigned char *buf, char *number, char *name, int flags, int callwaiting)
356
/* Initial carriers (real/imaginary) */
361
callerid_genmsg(msg, sizeof(msg), number, name, flags);
363
/* Wait a half a second */
366
/* Transmit 30 0x55's (looks like a square wave) for channel seizure */
370
/* Send 150ms of callerid marks */
373
/* Send 0x80 indicating MDMF format */
375
/* Put length of whole message */
376
PUT_CLID(strlen(msg));
377
sum = 0x80 + strlen(msg);
378
/* Put each character of message and update checksum */
379
for (x=0;x<strlen(msg); x++) {
383
/* Send 2's compliment of sum */
384
PUT_CLID(256 - (sum & 255));
385
/* Send 50 more ms of marks */
392
void ast_shrink_phone_number(char *n)
396
if (!strchr("( )-.", n[x]))
401
int ast_isphonenumber(char *n)
404
if (!n || !strlen(n))
407
if (!strchr("0123456789", n[x]))
412
int ast_callerid_parse(char *instr, char **name, char **location)
417
/* Try for "name" <location> format or
418
name <location> format */
419
if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
420
/* Found the location */
424
if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) {
425
/* Get name out of quotes */
431
/* Just trim off any trailing spaces */
433
while(strlen(instr) && (instr[strlen(instr) - 1] < 33))
434
instr[strlen(instr) - 1] = '\0';
435
/* And leading spaces */
436
while(**name && (**name < 33))
441
strncpy(tmp, instr, sizeof(tmp)-1);
442
ast_shrink_phone_number(tmp);
443
if (ast_isphonenumber(tmp)) {
444
/* Assume it's just a location */
448
/* Assume it's just a name */
457
static int __ast_callerid_generate(unsigned char *buf, char *callerid, int callwaiting)
462
return callerid_generate(buf, NULL, NULL, 0, callwaiting);
463
strncpy(tmp, callerid, sizeof(tmp)-1);
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, callwaiting);
469
ast_shrink_phone_number(l);
470
if (!ast_isphonenumber(l))
471
return callerid_generate(buf, NULL, n, 0, callwaiting);
472
return callerid_generate(buf, l, n, 0, callwaiting);
475
int ast_callerid_generate(unsigned char *buf, char *callerid)
477
return __ast_callerid_generate(buf, callerid, 0);
480
int ast_callerid_callwaiting_generate(unsigned char *buf, char *callerid)
482
return __ast_callerid_generate(buf, callerid, 1);