callerid.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief CallerID Generation support
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include <time.h>
  27. #include <math.h>
  28. #include <ctype.h>
  29. #include "asterisk/ulaw.h"
  30. #include "asterisk/alaw.h"
  31. #include "asterisk/frame.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/callerid.h"
  34. #include "asterisk/fskmodem.h"
  35. #include "asterisk/utils.h"
  36. struct callerid_state {
  37. fsk_data fskd;
  38. char rawdata[256];
  39. short oldstuff[160];
  40. int oldlen;
  41. int pos;
  42. int type;
  43. int cksum;
  44. char name[64];
  45. char number[64];
  46. int flags;
  47. int sawflag;
  48. int len;
  49. int skipflag;
  50. unsigned short crc;
  51. };
  52. float cid_dr[4], cid_di[4];
  53. float clidsb = 8000.0 / 1200.0;
  54. float sasdr, sasdi;
  55. float casdr1, casdi1, casdr2, casdi2;
  56. #define CALLERID_SPACE 2200.0 /*!< 2200 hz for "0" */
  57. #define CALLERID_MARK 1200.0 /*!< 1200 hz for "1" */
  58. #define SAS_FREQ 440.0
  59. #define CAS_FREQ1 2130.0
  60. #define CAS_FREQ2 2750.0
  61. #define AST_CALLERID_UNKNOWN "<unknown>"
  62. 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)
  63. {
  64. int x;
  65. float t;
  66. for (x = 0; x < len; x++) {
  67. t = *cr1 * ddr1 - *ci1 * ddi1;
  68. *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
  69. *cr1 = t;
  70. t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
  71. *cr1 *= t;
  72. *ci1 *= t;
  73. t = *cr2 * ddr2 - *ci2 * ddi2;
  74. *ci2 = *cr2 * ddi2 + *ci2 * ddr2;
  75. *cr2 = t;
  76. t = 2.0 - (*cr2 * *cr2 + *ci2 * *ci2);
  77. *cr2 *= t;
  78. *ci2 *= t;
  79. buf[x] = AST_LIN2X((*cr1 + *cr2) * 2048.0);
  80. }
  81. }
  82. static inline void gen_tone(unsigned char *buf, int len, int codec, float ddr1, float ddi1, float *cr1, float *ci1)
  83. {
  84. int x;
  85. float t;
  86. for (x = 0; x < len; x++) {
  87. t = *cr1 * ddr1 - *ci1 * ddi1;
  88. *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
  89. *cr1 = t;
  90. t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
  91. *cr1 *= t;
  92. *ci1 *= t;
  93. buf[x] = AST_LIN2X(*cr1 * 8192.0);
  94. }
  95. }
  96. /*! \brief Initialize stuff for inverse FFT */
  97. void callerid_init(void)
  98. {
  99. cid_dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
  100. cid_di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
  101. cid_dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0);
  102. cid_di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0);
  103. sasdr = cos(SAS_FREQ * 2.0 * M_PI / 8000.0);
  104. sasdi = sin(SAS_FREQ * 2.0 * M_PI / 8000.0);
  105. casdr1 = cos(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
  106. casdi1 = sin(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
  107. casdr2 = cos(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
  108. casdi2 = sin(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
  109. }
  110. struct callerid_state *callerid_new(int cid_signalling)
  111. {
  112. struct callerid_state *cid;
  113. if ((cid = ast_calloc(1, sizeof(*cid)))) {
  114. #ifdef INTEGER_CALLERID
  115. cid->fskd.ispb = 7; /* 1200 baud */
  116. /* Set up for 1200 / 8000 freq *32 to allow ints */
  117. cid->fskd.pllispb = (int)(8000 * 32 / 1200);
  118. cid->fskd.pllids = cid->fskd.pllispb/32;
  119. cid->fskd.pllispb2 = cid->fskd.pllispb/2;
  120. cid->fskd.icont = 0; /* PLL REset */
  121. /* cid->fskd.hdlc = 0; */ /* Async */
  122. cid->fskd.nbit = 8; /* 8 bits */
  123. cid->fskd.instop = 1; /* 1 stop bit */
  124. /* cid->fskd.paridad = 0; */ /* No parity */
  125. cid->fskd.bw = 1; /* Filter 800 Hz */
  126. if (cid_signalling == 2) { /* v23 signalling */
  127. cid->fskd.f_mark_idx = 4; /* 1300 Hz */
  128. cid->fskd.f_space_idx = 5; /* 2100 Hz */
  129. } else { /* Bell 202 signalling as default */
  130. cid->fskd.f_mark_idx = 2; /* 1200 Hz */
  131. cid->fskd.f_space_idx = 3; /* 2200 Hz */
  132. }
  133. /* cid->fskd.pcola = 0; */ /* No clue */
  134. /* cid->fskd.cont = 0.0; */ /* Digital PLL reset */
  135. /* cid->fskd.x0 = 0.0; */
  136. /* cid->fskd.state = 0; */
  137. cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
  138. /* cid->pos = 0; */
  139. fskmodem_init(&cid->fskd);
  140. #else
  141. cid->fskd.spb = 7.0; /* 1200 baud */
  142. /* cid->fskd.hdlc = 0; */ /* Async */
  143. cid->fskd.nbit = 8; /* 8 bits */
  144. cid->fskd.nstop = 1.0; /* 1 stop bit */
  145. /* cid->fskd.paridad = 0; */ /* No parity */
  146. cid->fskd.bw = 1; /* Filter 800 Hz */
  147. if (cid_signalling == 2) { /* v23 signalling */
  148. cid->fskd.f_mark_idx = 4; /* 1300 Hz */
  149. cid->fskd.f_space_idx = 5; /* 2100 Hz */
  150. } else { /* Bell 202 signalling as default */
  151. cid->fskd.f_mark_idx = 2; /* 1200 Hz */
  152. cid->fskd.f_space_idx = 3; /* 2200 Hz */
  153. }
  154. /* cid->fskd.pcola = 0; */ /* No clue */
  155. /* cid->fskd.cont = 0.0; */ /* Digital PLL reset */
  156. /* cid->fskd.x0 = 0.0; */
  157. /* cid->fskd.state = 0; */
  158. cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
  159. /* cid->pos = 0; */
  160. #endif
  161. }
  162. return cid;
  163. }
  164. void callerid_get(struct callerid_state *cid, char **name, char **number, int *flags)
  165. {
  166. *flags = cid->flags;
  167. if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NAME))
  168. *name = NULL;
  169. else
  170. *name = cid->name;
  171. if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER))
  172. *number = NULL;
  173. else
  174. *number = cid->number;
  175. }
  176. void callerid_get_dtmf(char *cidstring, char *number, int *flags)
  177. {
  178. int i;
  179. int code;
  180. /* "Clear" the number-buffer. */
  181. number[0] = 0;
  182. if (strlen(cidstring) < 2) {
  183. ast_debug(1, "No cid detected\n");
  184. *flags = CID_UNKNOWN_NUMBER;
  185. return;
  186. }
  187. /* Detect protocol and special types */
  188. if (cidstring[0] == 'B') {
  189. /* Handle special codes */
  190. code = atoi(&cidstring[1]);
  191. if (code == 0)
  192. *flags = CID_UNKNOWN_NUMBER;
  193. else if (code == 10)
  194. *flags = CID_PRIVATE_NUMBER;
  195. else
  196. ast_debug(1, "Unknown DTMF code %d\n", code);
  197. } else if (cidstring[0] == 'D' && cidstring[2] == '#') {
  198. /* .DK special code */
  199. if (cidstring[1] == '1')
  200. *flags = CID_PRIVATE_NUMBER;
  201. if (cidstring[1] == '2' || cidstring[1] == '3')
  202. *flags = CID_UNKNOWN_NUMBER;
  203. } else if (cidstring[0] == 'D' || cidstring[0] == 'A') {
  204. /* "Standard" callerid */
  205. for (i = 1; i < strlen(cidstring); i++) {
  206. if (cidstring[i] == 'C' || cidstring[i] == '#')
  207. break;
  208. if (isdigit(cidstring[i]))
  209. number[i-1] = cidstring[i];
  210. else
  211. ast_debug(1, "Unknown CID digit '%c'\n",
  212. cidstring[i]);
  213. }
  214. number[i-1] = 0;
  215. } else if (isdigit(cidstring[0])) {
  216. /* It begins with a digit, so we parse it as a number and hope
  217. * for the best */
  218. ast_log(LOG_WARNING, "Couldn't detect start-character. CID "
  219. "parsing might be unreliable\n");
  220. for (i = 0; i < strlen(cidstring); i++) {
  221. if (isdigit(cidstring[i]))
  222. number[i] = cidstring[i];
  223. else
  224. break;
  225. }
  226. number[i] = 0;
  227. } else {
  228. ast_debug(1, "Unknown CID protocol, start digit '%c'\n", cidstring[0]);
  229. *flags = CID_UNKNOWN_NUMBER;
  230. }
  231. }
  232. int ast_gen_cas(unsigned char *outbuf, int sendsas, int len, int codec)
  233. {
  234. int pos = 0;
  235. int saslen = 2400;
  236. float cr1 = 1.0;
  237. float ci1 = 0.0;
  238. float cr2 = 1.0;
  239. float ci2 = 0.0;
  240. if (sendsas) {
  241. if (len < saslen)
  242. return -1;
  243. gen_tone(outbuf, saslen, codec, sasdr, sasdi, &cr1, &ci1);
  244. len -= saslen;
  245. pos += saslen;
  246. cr2 = cr1;
  247. ci2 = ci1;
  248. }
  249. gen_tones(outbuf + pos, len, codec, casdr1, casdi1, casdr2, casdi2, &cr1, &ci1, &cr2, &ci2);
  250. return 0;
  251. }
  252. static unsigned short calc_crc(unsigned short crc, unsigned char data)
  253. {
  254. unsigned int i, j, org, dst;
  255. org = data;
  256. dst = 0;
  257. for (i = 0; i < CHAR_BIT; i++) {
  258. org <<= 1;
  259. dst >>= 1;
  260. if (org & 0x100)
  261. dst |= 0x80;
  262. }
  263. data = (unsigned char) dst;
  264. crc ^= (unsigned int) data << (16 - CHAR_BIT);
  265. for (j = 0; j < CHAR_BIT; j++) {
  266. if (crc & 0x8000U)
  267. crc = (crc << 1) ^ 0x1021U ;
  268. else
  269. crc <<= 1 ;
  270. }
  271. return crc;
  272. }
  273. int callerid_feed_jp(struct callerid_state *cid, unsigned char *ubuf, int len, int codec)
  274. {
  275. int mylen = len;
  276. int olen;
  277. int b = 'X';
  278. int b2;
  279. int res;
  280. int x;
  281. short *buf;
  282. buf = alloca(2 * len + cid->oldlen);
  283. memcpy(buf, cid->oldstuff, cid->oldlen);
  284. mylen += cid->oldlen / 2;
  285. for (x = 0; x < len; x++)
  286. buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
  287. while (mylen >= 160) {
  288. b = b2 = 0;
  289. olen = mylen;
  290. res = fsk_serial(&cid->fskd, buf, &mylen, &b);
  291. if (mylen < 0) {
  292. ast_log(LOG_ERROR, "No start bit found in fsk data.\n");
  293. return -1;
  294. }
  295. buf += (olen - mylen);
  296. if (res < 0) {
  297. ast_log(LOG_NOTICE, "fsk_serial failed\n");
  298. return -1;
  299. }
  300. if (res == 1) {
  301. b2 = b;
  302. b &= 0x7f;
  303. /* crc checksum calculation */
  304. if (cid->sawflag > 1)
  305. cid->crc = calc_crc(cid->crc, (unsigned char) b2);
  306. /* Ignore invalid bytes */
  307. if (b > 0xff)
  308. continue;
  309. /* skip DLE if needed */
  310. if (cid->sawflag > 0) {
  311. if (cid->sawflag != 5 && cid->skipflag == 0 && b == 0x10) {
  312. cid->skipflag = 1 ;
  313. continue ;
  314. }
  315. }
  316. if (cid->skipflag == 1)
  317. cid->skipflag = 0 ;
  318. /* caller id retrieval */
  319. switch (cid->sawflag) {
  320. case 0: /* DLE */
  321. if (b == 0x10) {
  322. cid->sawflag = 1;
  323. cid->skipflag = 0;
  324. cid->crc = 0;
  325. }
  326. break;
  327. case 1: /* SOH */
  328. if (b == 0x01)
  329. cid->sawflag = 2;
  330. break ;
  331. case 2: /* HEADER */
  332. if (b == 0x07)
  333. cid->sawflag = 3;
  334. break;
  335. case 3: /* STX */
  336. if (b == 0x02)
  337. cid->sawflag = 4;
  338. break;
  339. case 4: /* SERVICE TYPE */
  340. if (b == 0x40)
  341. cid->sawflag = 5;
  342. break;
  343. case 5: /* Frame Length */
  344. cid->sawflag = 6;
  345. break;
  346. case 6: /* NUMBER TYPE */
  347. cid->sawflag = 7;
  348. cid->pos = 0;
  349. cid->rawdata[cid->pos++] = b;
  350. break;
  351. case 7: /* NUMBER LENGTH */
  352. cid->sawflag = 8;
  353. cid->len = b;
  354. if ((cid->len+2) >= sizeof(cid->rawdata)) {
  355. ast_log(LOG_WARNING, "too long caller id string\n") ;
  356. return -1;
  357. }
  358. cid->rawdata[cid->pos++] = b;
  359. break;
  360. case 8: /* Retrieve message */
  361. cid->rawdata[cid->pos++] = b;
  362. cid->len--;
  363. if (cid->len<=0) {
  364. cid->rawdata[cid->pos] = '\0';
  365. cid->sawflag = 9;
  366. }
  367. break;
  368. case 9: /* ETX */
  369. cid->sawflag = 10;
  370. break;
  371. case 10: /* CRC Checksum 1 */
  372. cid->sawflag = 11;
  373. break;
  374. case 11: /* CRC Checksum 2 */
  375. cid->sawflag = 12;
  376. if (cid->crc != 0) {
  377. ast_log(LOG_WARNING, "crc checksum error\n") ;
  378. return -1;
  379. }
  380. /* extract caller id data */
  381. for (x = 0; x < cid->pos;) {
  382. switch (cid->rawdata[x++]) {
  383. case 0x02: /* caller id number */
  384. cid->number[0] = '\0';
  385. cid->name[0] = '\0';
  386. cid->flags = 0;
  387. res = cid->rawdata[x++];
  388. ast_copy_string(cid->number, &cid->rawdata[x], res+1);
  389. x += res;
  390. break;
  391. case 0x21: /* additional information */
  392. /* length */
  393. x++;
  394. /* number type */
  395. switch (cid->rawdata[x]) {
  396. case 0x00: /* unknown */
  397. case 0x01: /* international number */
  398. case 0x02: /* domestic number */
  399. case 0x03: /* network */
  400. case 0x04: /* local call */
  401. case 0x06: /* short dial number */
  402. case 0x07: /* reserved */
  403. default: /* reserved */
  404. ast_debug(2, "cid info:#1=%X\n", cid->rawdata[x]);
  405. break ;
  406. }
  407. x++;
  408. /* numbering plan octed 4 */
  409. x++;
  410. /* numbering plan octed 5 */
  411. switch (cid->rawdata[x]) {
  412. case 0x00: /* unknown */
  413. case 0x01: /* recommendation E.164 ISDN */
  414. case 0x03: /* recommendation X.121 */
  415. case 0x04: /* telex dial plan */
  416. case 0x08: /* domestic dial plan */
  417. case 0x09: /* private dial plan */
  418. case 0x05: /* reserved */
  419. default: /* reserved */
  420. ast_debug(2, "cid info:#2=%X\n", cid->rawdata[x]);
  421. break ;
  422. }
  423. x++;
  424. break ;
  425. case 0x04: /* no callerid reason */
  426. /* length */
  427. x++;
  428. /* no callerid reason code */
  429. switch (cid->rawdata[x]) {
  430. case 'P': /* caller id denied by user */
  431. case 'O': /* service not available */
  432. case 'C': /* pay phone */
  433. case 'S': /* service congested */
  434. cid->flags |= CID_UNKNOWN_NUMBER;
  435. ast_debug(2, "no cid reason:%c\n", cid->rawdata[x]);
  436. break ;
  437. }
  438. x++;
  439. break ;
  440. case 0x09: /* dialed number */
  441. /* length */
  442. res = cid->rawdata[x++];
  443. /* dialed number */
  444. x += res;
  445. break ;
  446. case 0x22: /* dialed number additional information */
  447. /* length */
  448. x++;
  449. /* number type */
  450. switch (cid->rawdata[x]) {
  451. case 0x00: /* unknown */
  452. case 0x01: /* international number */
  453. case 0x02: /* domestic number */
  454. case 0x03: /* network */
  455. case 0x04: /* local call */
  456. case 0x06: /* short dial number */
  457. case 0x07: /* reserved */
  458. default: /* reserved */
  459. if (option_debug > 1)
  460. ast_log(LOG_NOTICE, "did info:#1=%X\n", cid->rawdata[x]);
  461. break ;
  462. }
  463. x++;
  464. /* numbering plan octed 4 */
  465. x++;
  466. /* numbering plan octed 5 */
  467. switch (cid->rawdata[x]) {
  468. case 0x00: /* unknown */
  469. case 0x01: /* recommendation E.164 ISDN */
  470. case 0x03: /* recommendation X.121 */
  471. case 0x04: /* telex dial plan */
  472. case 0x08: /* domestic dial plan */
  473. case 0x09: /* private dial plan */
  474. case 0x05: /* reserved */
  475. default: /* reserved */
  476. ast_debug(2, "did info:#2=%X\n", cid->rawdata[x]);
  477. break ;
  478. }
  479. x++;
  480. break ;
  481. }
  482. }
  483. return 1;
  484. break;
  485. default:
  486. ast_log(LOG_ERROR, "invalid value in sawflag %d\n", cid->sawflag);
  487. }
  488. }
  489. }
  490. if (mylen) {
  491. memcpy(cid->oldstuff, buf, mylen * 2);
  492. cid->oldlen = mylen * 2;
  493. } else
  494. cid->oldlen = 0;
  495. return 0;
  496. }
  497. int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len, int codec)
  498. {
  499. int mylen = len;
  500. int olen;
  501. int b = 'X';
  502. int res;
  503. int x;
  504. short *buf;
  505. buf = alloca(2 * len + cid->oldlen);
  506. memcpy(buf, cid->oldstuff, cid->oldlen);
  507. mylen += cid->oldlen/2;
  508. for (x = 0; x < len; x++)
  509. buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
  510. while (mylen >= 160) {
  511. olen = mylen;
  512. res = fsk_serial(&cid->fskd, buf, &mylen, &b);
  513. if (mylen < 0) {
  514. ast_log(LOG_ERROR, "No start bit found in fsk data.\n");
  515. return -1;
  516. }
  517. buf += (olen - mylen);
  518. if (res < 0) {
  519. ast_log(LOG_NOTICE, "fsk_serial failed\n");
  520. return -1;
  521. }
  522. if (res == 1) {
  523. /* Ignore invalid bytes */
  524. if (b > 0xff)
  525. continue;
  526. switch (cid->sawflag) {
  527. case 0: /* Look for flag */
  528. if (b == 'U')
  529. cid->sawflag = 2;
  530. break;
  531. case 2: /* Get lead-in */
  532. if ((b == 0x04) || (b == 0x80) || (b == 0x06) || (b == 0x82)) {
  533. cid->type = b;
  534. cid->sawflag = 3;
  535. cid->cksum = b;
  536. }
  537. break;
  538. case 3: /* Get length */
  539. /* Not a lead in. We're ready */
  540. cid->sawflag = 4;
  541. cid->len = b;
  542. cid->pos = 0;
  543. cid->cksum += b;
  544. break;
  545. case 4: /* Retrieve message */
  546. if (cid->pos >= 128) {
  547. ast_log(LOG_WARNING, "Caller ID too long???\n");
  548. return -1;
  549. }
  550. cid->rawdata[cid->pos++] = b;
  551. cid->len--;
  552. cid->cksum += b;
  553. if (!cid->len) {
  554. cid->rawdata[cid->pos] = '\0';
  555. cid->sawflag = 5;
  556. }
  557. break;
  558. case 5: /* Check checksum */
  559. if (b != (256 - (cid->cksum & 0xff))) {
  560. ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
  561. /* Try again */
  562. cid->sawflag = 0;
  563. break;
  564. }
  565. cid->number[0] = '\0';
  566. cid->name[0] = '\0';
  567. /* Update flags */
  568. cid->flags = 0;
  569. /* If we get this far we're fine. */
  570. if ((cid->type == 0x80) || (cid->type == 0x82)) {
  571. /* MDMF */
  572. /* Go through each element and process */
  573. for (x = 0; x < cid->pos;) {
  574. switch (cid->rawdata[x++]) {
  575. case 1:
  576. /* Date */
  577. break;
  578. case 2: /* Number */
  579. case 3: /* Number (for Zebble) */
  580. case 4: /* Number */
  581. res = cid->rawdata[x];
  582. if (res > 32) {
  583. ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
  584. res = 32;
  585. }
  586. if (ast_strlen_zero(cid->number)) {
  587. memcpy(cid->number, cid->rawdata + x + 1, res);
  588. /* Null terminate */
  589. cid->number[res] = '\0';
  590. }
  591. break;
  592. case 6: /* Stentor Call Qualifier (ie. Long Distance call) */
  593. break;
  594. case 7: /* Name */
  595. case 8: /* Name */
  596. res = cid->rawdata[x];
  597. if (res > 32) {
  598. ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
  599. res = 32;
  600. }
  601. memcpy(cid->name, cid->rawdata + x + 1, res);
  602. cid->name[res] = '\0';
  603. break;
  604. case 11: /* Message Waiting */
  605. res = cid->rawdata[x + 1];
  606. if (res)
  607. cid->flags |= CID_MSGWAITING;
  608. else
  609. cid->flags |= CID_NOMSGWAITING;
  610. break;
  611. case 17: /* UK: Call type, 1=Voice Call, 2=Ringback when free, 129=Message waiting */
  612. case 19: /* UK: Network message system status (Number of messages waiting) */
  613. case 22: /* Something French */
  614. break;
  615. default:
  616. ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x - 1]);
  617. }
  618. res = cid->rawdata[x];
  619. if (0 > res){ /* Negative offset in the CID Spill */
  620. ast_log(LOG_NOTICE, "IE %d has bad field length of %d at offset %d\n", cid->rawdata[x-1], cid->rawdata[x], x);
  621. /* Try again */
  622. cid->sawflag = 0;
  623. break; /* Exit the loop */
  624. }
  625. x += cid->rawdata[x];
  626. x++;
  627. }
  628. } else if (cid->type == 0x6) {
  629. /* VMWI SDMF */
  630. if (cid->rawdata[2] == 0x42) {
  631. cid->flags |= CID_MSGWAITING;
  632. } else if (cid->rawdata[2] == 0x6f) {
  633. cid->flags |= CID_NOMSGWAITING;
  634. }
  635. } else {
  636. /* SDMF */
  637. ast_copy_string(cid->number, cid->rawdata + 8, sizeof(cid->number));
  638. }
  639. if (!strcmp(cid->number, "P")) {
  640. strcpy(cid->number, "");
  641. cid->flags |= CID_PRIVATE_NUMBER;
  642. } else if (!strcmp(cid->number, "O") || ast_strlen_zero(cid->number)) {
  643. strcpy(cid->number, "");
  644. cid->flags |= CID_UNKNOWN_NUMBER;
  645. }
  646. if (!strcmp(cid->name, "P")) {
  647. strcpy(cid->name, "");
  648. cid->flags |= CID_PRIVATE_NAME;
  649. } else if (!strcmp(cid->name, "O") || ast_strlen_zero(cid->name)) {
  650. strcpy(cid->name, "");
  651. cid->flags |= CID_UNKNOWN_NAME;
  652. }
  653. return 1;
  654. break;
  655. default:
  656. ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
  657. }
  658. }
  659. }
  660. if (mylen) {
  661. memcpy(cid->oldstuff, buf, mylen * 2);
  662. cid->oldlen = mylen * 2;
  663. } else
  664. cid->oldlen = 0;
  665. return 0;
  666. }
  667. void callerid_free(struct callerid_state *cid)
  668. {
  669. ast_free(cid);
  670. }
  671. static int callerid_genmsg(char *msg, int size, const char *number, const char *name, int flags)
  672. {
  673. struct timeval now = ast_tvnow();
  674. struct ast_tm tm;
  675. char *ptr;
  676. int res;
  677. int i, x;
  678. /* Get the time */
  679. ast_localtime(&now, &tm, NULL);
  680. ptr = msg;
  681. /* Format time and message header */
  682. res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm.tm_mon + 1,
  683. tm.tm_mday, tm.tm_hour, tm.tm_min);
  684. size -= res;
  685. ptr += res;
  686. if (ast_strlen_zero(number) || (flags & CID_UNKNOWN_NUMBER)) {
  687. /* Indicate number not known */
  688. res = snprintf(ptr, size, "\004\001O");
  689. size -= res;
  690. ptr += res;
  691. } else if (flags & CID_PRIVATE_NUMBER) {
  692. /* Indicate number is private */
  693. res = snprintf(ptr, size, "\004\001P");
  694. size -= res;
  695. ptr += res;
  696. } else {
  697. /* Send up to 16 digits of number MAX */
  698. i = strlen(number);
  699. if (i > 16)
  700. i = 16;
  701. res = snprintf(ptr, size, "\002%c", i);
  702. size -= res;
  703. ptr += res;
  704. for (x = 0; x < i; x++)
  705. ptr[x] = number[x];
  706. ptr[i] = '\0';
  707. ptr += i;
  708. size -= i;
  709. }
  710. if (ast_strlen_zero(name) || (flags & CID_UNKNOWN_NAME)) {
  711. /* Indicate name not known */
  712. res = snprintf(ptr, size, "\010\001O");
  713. size -= res;
  714. ptr += res;
  715. } else if (flags & CID_PRIVATE_NAME) {
  716. /* Indicate name is private */
  717. res = snprintf(ptr, size, "\010\001P");
  718. size -= res;
  719. ptr += res;
  720. } else {
  721. /* Send up to 16 digits of name MAX */
  722. i = strlen(name);
  723. if (i > 16)
  724. i = 16;
  725. res = snprintf(ptr, size, "\007%c", i);
  726. size -= res;
  727. ptr += res;
  728. for (x = 0; x < i; x++)
  729. ptr[x] = name[x];
  730. ptr[i] = '\0';
  731. ptr += i;
  732. size -= i;
  733. }
  734. return (ptr - msg);
  735. }
  736. int vmwi_generate(unsigned char *buf, int active, int type, int codec,
  737. const char* name, const char* number, int flags)
  738. {
  739. char msg[256];
  740. int len = 0;
  741. int sum;
  742. int x;
  743. int bytes = 0;
  744. float cr = 1.0;
  745. float ci = 0.0;
  746. float scont = 0.0;
  747. if (type == CID_MWI_TYPE_MDMF_FULL) {
  748. /* MDMF Message waiting with date, number, name and MWI parameter */
  749. msg[0] = 0x82;
  750. /* put date, number info at the right place */
  751. len = callerid_genmsg(msg+2, sizeof(msg)-2, number, name, flags);
  752. /* length of MDMF CLI plus Message Waiting Structure */
  753. msg[1] = len+3;
  754. /* Go to the position to write to */
  755. len = len+2;
  756. /* "Message Waiting Parameter" */
  757. msg[len++] = 0x0b;
  758. /* Length of IE is one */
  759. msg[len++] = 1;
  760. /* Active or not */
  761. if (active)
  762. msg[len++] = 0xff;
  763. else
  764. msg[len++] = 0x00;
  765. } else if (type == CID_MWI_TYPE_MDMF) {
  766. /* MDMF Message waiting only */
  767. /* same as above except that the we only put MWI parameter */
  768. msg[len++] = 0x82;
  769. /* Length is 3 */
  770. msg[len++] = 3;
  771. /* IE is "Message Waiting Parameter" */
  772. msg[len++] = 0x0b;
  773. /* Length of IE is one */
  774. msg[len++] = 1;
  775. /* Active or not */
  776. if (active)
  777. msg[len++] = 0xff;
  778. else
  779. msg[len++] = 0x00;
  780. } else {
  781. /* SDMF Message waiting */
  782. msg[len++] = 0x6;
  783. /* Length is 3 */
  784. msg[len++] = 3;
  785. if (active) {
  786. msg[len++] = 0x42;
  787. msg[len++] = 0x42;
  788. msg[len++] = 0x42;
  789. } else {
  790. msg[len++] = 0x6f;
  791. msg[len++] = 0x6f;
  792. msg[len++] = 0x6f;
  793. }
  794. }
  795. sum = 0;
  796. for (x = 0; x < len; x++)
  797. sum += msg[x];
  798. sum = (256 - (sum & 255));
  799. msg[len++] = sum;
  800. /* Wait a half a second */
  801. for (x = 0; x < 4000; x++)
  802. PUT_BYTE(0x7f);
  803. /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
  804. for (x = 0; x < 30; x++)
  805. PUT_CLID(0x55);
  806. /* Send 170ms of callerid marks */
  807. for (x = 0; x < 170; x++)
  808. PUT_CLID_MARKMS;
  809. for (x = 0; x < len; x++) {
  810. PUT_CLID(msg[x]);
  811. }
  812. /* Send 50 more ms of marks */
  813. for (x = 0; x < 50; x++)
  814. PUT_CLID_MARKMS;
  815. return bytes;
  816. }
  817. int callerid_generate(unsigned char *buf, const char *number, const char *name, int flags, int callwaiting, int codec)
  818. {
  819. int bytes = 0;
  820. int x, sum;
  821. int len;
  822. /* Initial carriers (real/imaginary) */
  823. float cr = 1.0;
  824. float ci = 0.0;
  825. float scont = 0.0;
  826. char msg[256];
  827. len = callerid_genmsg(msg, sizeof(msg), number, name, flags);
  828. if (!callwaiting) {
  829. /* Wait a half a second */
  830. for (x = 0; x < 4000; x++)
  831. PUT_BYTE(0x7f);
  832. /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
  833. for (x = 0; x < 30; x++)
  834. PUT_CLID(0x55);
  835. }
  836. /* Send 150ms of callerid marks */
  837. for (x = 0; x < 150; x++)
  838. PUT_CLID_MARKMS;
  839. /* Send 0x80 indicating MDMF format */
  840. PUT_CLID(0x80);
  841. /* Put length of whole message */
  842. PUT_CLID(len);
  843. sum = 0x80 + strlen(msg);
  844. /* Put each character of message and update checksum */
  845. for (x = 0; x < len; x++) {
  846. PUT_CLID(msg[x]);
  847. sum += msg[x];
  848. }
  849. /* Send 2's compliment of sum */
  850. PUT_CLID(256 - (sum & 255));
  851. /* Send 50 more ms of marks */
  852. for (x = 0; x < 50; x++)
  853. PUT_CLID_MARKMS;
  854. return bytes;
  855. }
  856. /*! \brief Clean up phone string
  857. * remove '(', ' ', ')', non-trailing '.', and '-' not in square brackets.
  858. * Basically, remove anything that could be invalid in a pattern.
  859. */
  860. void ast_shrink_phone_number(char *n)
  861. {
  862. int x, y = 0;
  863. int bracketed = 0;
  864. for (x = 0; n[x]; x++) {
  865. switch (n[x]) {
  866. case '[':
  867. bracketed++;
  868. n[y++] = n[x];
  869. break;
  870. case ']':
  871. bracketed--;
  872. n[y++] = n[x];
  873. break;
  874. case '-':
  875. if (bracketed)
  876. n[y++] = n[x];
  877. break;
  878. case '.':
  879. if (!n[x+1])
  880. n[y++] = n[x];
  881. break;
  882. default:
  883. /* ignore parenthesis and whitespace */
  884. if (!strchr("( )", n[x]))
  885. n[y++] = n[x];
  886. }
  887. }
  888. n[y] = '\0';
  889. }
  890. /*! \brief Checks if phone number consists of valid characters
  891. \param exten String that needs to be checked
  892. \param valid Valid characters in string
  893. \return 1 if valid string, 0 if string contains invalid characters
  894. */
  895. static int ast_is_valid_string(const char *exten, const char *valid)
  896. {
  897. int x;
  898. if (ast_strlen_zero(exten))
  899. return 0;
  900. for (x = 0; exten[x]; x++)
  901. if (!strchr(valid, exten[x]))
  902. return 0;
  903. return 1;
  904. }
  905. /*! \brief checks if string consists only of digits and * \# and +
  906. \return 1 if string is valid AST phone number
  907. \return 0 if not
  908. */
  909. int ast_isphonenumber(const char *n)
  910. {
  911. return ast_is_valid_string(n, "0123456789*#+");
  912. }
  913. /*! \brief checks if string consists only of digits and ( ) - * \# and +
  914. Pre-qualifies the string for ast_shrink_phone_number()
  915. \return 1 if string is valid AST shrinkable phone number
  916. \return 0 if not
  917. */
  918. int ast_is_shrinkable_phonenumber(const char *exten)
  919. {
  920. return ast_is_valid_string(exten, "0123456789*#+()-.");
  921. }
  922. /*!
  923. * \brief Destructively parse instr for caller id information
  924. * \return always returns 0, as the code always returns something.
  925. * \note XXX 'name' is not parsed consistently e.g. we have
  926. * input location name
  927. * " foo bar " <123> 123 ' foo bar ' (with spaces around)
  928. * " foo bar " NULL 'foo bar' (without spaces around)
  929. * The parsing of leading and trailing space/quotes should be more consistent.
  930. */
  931. int ast_callerid_parse(char *instr, char **name, char **location)
  932. {
  933. char *ns, *ne, *ls, *le;
  934. /* Try "name" <location> format or name <location> format */
  935. if ((ls = strrchr(instr, '<')) && (le = strrchr(ls, '>'))) {
  936. *ls = *le = '\0'; /* location found, trim off the brackets */
  937. *location = ls + 1; /* and this is the result */
  938. if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
  939. *ns = *ne = '\0'; /* trim off the quotes */
  940. *name = ns + 1; /* and this is the name */
  941. } else if (ns) {
  942. /* An opening quote was found but no closing quote was. The closing
  943. * quote may actually be after the end of the bracketed number
  944. */
  945. if (strchr(le + 1, '\"')) {
  946. *ns = '\0';
  947. *name = ns + 1;
  948. ast_trim_blanks(*name);
  949. }
  950. } else { /* no quotes, trim off leading and trailing spaces */
  951. *name = ast_skip_blanks(instr);
  952. ast_trim_blanks(*name);
  953. }
  954. } else { /* no valid brackets */
  955. char tmp[256];
  956. ast_copy_string(tmp, instr, sizeof(tmp));
  957. ast_shrink_phone_number(tmp);
  958. if (ast_isphonenumber(tmp)) { /* Assume it's just a location */
  959. *name = NULL;
  960. strcpy(instr, tmp); /* safe, because tmp will always be the same size or smaller than instr */
  961. *location = instr;
  962. } else { /* Assume it's just a name. */
  963. *location = NULL;
  964. if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
  965. *ns = *ne = '\0'; /* trim off the quotes */
  966. *name = ns + 1; /* and this is the name */
  967. } else { /* no quotes, trim off leading and trailing spaces */
  968. *name = ast_skip_blanks(instr);
  969. ast_trim_blanks(*name);
  970. }
  971. }
  972. }
  973. return 0;
  974. }
  975. static int __ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int callwaiting, int codec)
  976. {
  977. if (ast_strlen_zero(name))
  978. name = NULL;
  979. if (ast_strlen_zero(number))
  980. number = NULL;
  981. return callerid_generate(buf, number, name, 0, callwaiting, codec);
  982. }
  983. int ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int codec)
  984. {
  985. return __ast_callerid_generate(buf, name, number, 0, codec);
  986. }
  987. int ast_callerid_callwaiting_generate(unsigned char *buf, const char *name, const char *number, int codec)
  988. {
  989. return __ast_callerid_generate(buf, name, number, 1, codec);
  990. }
  991. char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown)
  992. {
  993. if (!unknown)
  994. unknown = "<unknown>";
  995. if (name && num)
  996. snprintf(buf, bufsiz, "\"%s\" <%s>", name, num);
  997. else if (name)
  998. ast_copy_string(buf, name, bufsiz);
  999. else if (num)
  1000. ast_copy_string(buf, num, bufsiz);
  1001. else
  1002. ast_copy_string(buf, unknown, bufsiz);
  1003. return buf;
  1004. }
  1005. int ast_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen)
  1006. {
  1007. char *tmp;
  1008. char *l = NULL, *n = NULL;
  1009. tmp = ast_strdupa(buf);
  1010. ast_callerid_parse(tmp, &n, &l);
  1011. if (n)
  1012. ast_copy_string(name, n, namelen);
  1013. else
  1014. name[0] = '\0';
  1015. if (l) {
  1016. ast_shrink_phone_number(l);
  1017. ast_copy_string(num, l, numlen);
  1018. } else
  1019. num[0] = '\0';
  1020. return 0;
  1021. }
  1022. /*! \brief Translation table for Caller ID Presentation settings */
  1023. static struct {
  1024. int val;
  1025. const char *name;
  1026. const char *description;
  1027. } pres_types[] = {
  1028. { AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "allowed_not_screened", "Presentation Allowed, Not Screened"},
  1029. { AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", "Presentation Allowed, Passed Screen"},
  1030. { AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", "Presentation Allowed, Failed Screen"},
  1031. { AST_PRES_ALLOWED_NETWORK_NUMBER, "allowed", "Presentation Allowed, Network Number"},
  1032. { AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "prohib_not_screened", "Presentation Prohibited, Not Screened"},
  1033. { AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", "Presentation Prohibited, Passed Screen"},
  1034. { AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", "Presentation Prohibited, Failed Screen"},
  1035. { AST_PRES_PROHIB_NETWORK_NUMBER, "prohib", "Presentation Prohibited, Network Number"},
  1036. { AST_PRES_NUMBER_NOT_AVAILABLE, "unavailable", "Number Unavailable"},
  1037. };
  1038. /*! \brief Convert caller ID text code to value
  1039. used in config file parsing
  1040. \param data text string
  1041. \return value AST_PRES_ from callerid.h
  1042. */
  1043. int ast_parse_caller_presentation(const char *data)
  1044. {
  1045. int i;
  1046. if (!data) {
  1047. return -1;
  1048. }
  1049. for (i = 0; i < ARRAY_LEN(pres_types); i++) {
  1050. if (!strcasecmp(pres_types[i].name, data))
  1051. return pres_types[i].val;
  1052. }
  1053. return -1;
  1054. }
  1055. /*! \brief Convert caller ID pres value to explanatory string
  1056. \param data value (see callerid.h AST_PRES_ )
  1057. \return string for human presentation
  1058. */
  1059. const char *ast_describe_caller_presentation(int data)
  1060. {
  1061. int i;
  1062. for (i = 0; i < ARRAY_LEN(pres_types); i++) {
  1063. if (pres_types[i].val == data)
  1064. return pres_types[i].description;
  1065. }
  1066. return "unknown";
  1067. }
  1068. /*! \brief Convert caller ID pres value to text code
  1069. \param data text string
  1070. \return string for config file
  1071. */
  1072. const char *ast_named_caller_presentation(int data)
  1073. {
  1074. int i;
  1075. for (i = 0; i < ARRAY_LEN(pres_types); i++) {
  1076. if (pres_types[i].val == data)
  1077. return pres_types[i].name;
  1078. }
  1079. return "unknown";
  1080. }