callerid.c 14 KB

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