callerid.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 tv = ast_tvnow();
  674. struct ast_tm tm;
  675. char *ptr;
  676. int res;
  677. int i, x;
  678. /* Get the time */
  679. ast_localtime(&tv, &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 mdmf, int codec)
  737. {
  738. unsigned char msg[256];
  739. int len = 0;
  740. int sum;
  741. int x;
  742. int bytes = 0;
  743. float cr = 1.0;
  744. float ci = 0.0;
  745. float scont = 0.0;
  746. if (mdmf) {
  747. /* MDMF Message waiting */
  748. msg[len++] = 0x82;
  749. /* Length is 3 */
  750. msg[len++] = 3;
  751. /* IE is "Message Waiting Parameter" */
  752. msg[len++] = 0xb;
  753. /* Length of IE is one */
  754. msg[len++] = 1;
  755. /* Active or not */
  756. if (active)
  757. msg[len++] = 0xff;
  758. else
  759. msg[len++] = 0x00;
  760. } else {
  761. /* SDMF Message waiting */
  762. msg[len++] = 0x6;
  763. /* Length is 3 */
  764. msg[len++] = 3;
  765. if (active) {
  766. msg[len++] = 0x42;
  767. msg[len++] = 0x42;
  768. msg[len++] = 0x42;
  769. } else {
  770. msg[len++] = 0x6f;
  771. msg[len++] = 0x6f;
  772. msg[len++] = 0x6f;
  773. }
  774. }
  775. sum = 0;
  776. for (x = 0; x < len; x++)
  777. sum += msg[x];
  778. sum = (256 - (sum & 255));
  779. msg[len++] = sum;
  780. /* Wait a half a second */
  781. for (x = 0; x < 4000; x++)
  782. PUT_BYTE(0x7f);
  783. /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
  784. for (x = 0; x < 30; x++)
  785. PUT_CLID(0x55);
  786. /* Send 170ms of callerid marks */
  787. for (x = 0; x < 170; x++)
  788. PUT_CLID_MARKMS;
  789. for (x = 0; x < len; x++) {
  790. PUT_CLID(msg[x]);
  791. }
  792. /* Send 50 more ms of marks */
  793. for (x = 0; x < 50; x++)
  794. PUT_CLID_MARKMS;
  795. return bytes;
  796. }
  797. int callerid_generate(unsigned char *buf, const char *number, const char *name, int flags, int callwaiting, int codec)
  798. {
  799. int bytes = 0;
  800. int x, sum;
  801. int len;
  802. /* Initial carriers (real/imaginary) */
  803. float cr = 1.0;
  804. float ci = 0.0;
  805. float scont = 0.0;
  806. char msg[256];
  807. len = callerid_genmsg(msg, sizeof(msg), number, name, flags);
  808. if (!callwaiting) {
  809. /* Wait a half a second */
  810. for (x = 0; x < 4000; x++)
  811. PUT_BYTE(0x7f);
  812. /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
  813. for (x = 0; x < 30; x++)
  814. PUT_CLID(0x55);
  815. }
  816. /* Send 150ms of callerid marks */
  817. for (x = 0; x < 150; x++)
  818. PUT_CLID_MARKMS;
  819. /* Send 0x80 indicating MDMF format */
  820. PUT_CLID(0x80);
  821. /* Put length of whole message */
  822. PUT_CLID(len);
  823. sum = 0x80 + strlen(msg);
  824. /* Put each character of message and update checksum */
  825. for (x = 0; x < len; x++) {
  826. PUT_CLID(msg[x]);
  827. sum += msg[x];
  828. }
  829. /* Send 2's compliment of sum */
  830. PUT_CLID(256 - (sum & 255));
  831. /* Send 50 more ms of marks */
  832. for (x = 0; x < 50; x++)
  833. PUT_CLID_MARKMS;
  834. return bytes;
  835. }
  836. /*! \brief Clean up phone string
  837. * remove '(', ' ', ')', non-trailing '.', and '-' not in square brackets.
  838. * Basically, remove anything that could be invalid in a pattern.
  839. */
  840. void ast_shrink_phone_number(char *n)
  841. {
  842. int x, y = 0;
  843. int bracketed = 0;
  844. for (x = 0; n[x]; x++) {
  845. switch (n[x]) {
  846. case '[':
  847. bracketed++;
  848. n[y++] = n[x];
  849. break;
  850. case ']':
  851. bracketed--;
  852. n[y++] = n[x];
  853. break;
  854. case '-':
  855. if (bracketed)
  856. n[y++] = n[x];
  857. break;
  858. case '.':
  859. if (!n[x+1])
  860. n[y++] = n[x];
  861. break;
  862. default:
  863. /* ignore parenthesis and whitespace */
  864. if (!strchr("( )", n[x]))
  865. n[y++] = n[x];
  866. }
  867. }
  868. n[y] = '\0';
  869. }
  870. /*! \brief Checks if phone number consists of valid characters
  871. \param exten String that needs to be checked
  872. \param valid Valid characters in string
  873. \return 1 if valid string, 0 if string contains invalid characters
  874. */
  875. static int ast_is_valid_string(const char *exten, const char *valid)
  876. {
  877. int x;
  878. if (ast_strlen_zero(exten))
  879. return 0;
  880. for (x = 0; exten[x]; x++)
  881. if (!strchr(valid, exten[x]))
  882. return 0;
  883. return 1;
  884. }
  885. /*! \brief checks if string consists only of digits and * \# and +
  886. \return 1 if string is valid AST phone number
  887. \return 0 if not
  888. */
  889. int ast_isphonenumber(const char *n)
  890. {
  891. return ast_is_valid_string(n, "0123456789*#+");
  892. }
  893. /*! \brief checks if string consists only of digits and ( ) - * \# and +
  894. Pre-qualifies the string for ast_shrink_phone_number()
  895. \return 1 if string is valid AST shrinkable phone number
  896. \return 0 if not
  897. */
  898. int ast_is_shrinkable_phonenumber(const char *exten)
  899. {
  900. return ast_is_valid_string(exten, "0123456789*#+()-.");
  901. }
  902. /*!
  903. * \brief Destructively parse instr for caller id information
  904. * \return always returns 0, as the code always returns something.
  905. * \note XXX 'name' is not parsed consistently e.g. we have
  906. * input location name
  907. * " foo bar " <123> 123 ' foo bar ' (with spaces around)
  908. * " foo bar " NULL 'foo bar' (without spaces around)
  909. * The parsing of leading and trailing space/quotes should be more consistent.
  910. */
  911. int ast_callerid_parse(char *instr, char **name, char **location)
  912. {
  913. char *ns, *ne, *ls, *le;
  914. /* Try "name" <location> format or name <location> format */
  915. if ((ls = strrchr(instr, '<')) && (le = strrchr(ls, '>'))) {
  916. *ls = *le = '\0'; /* location found, trim off the brackets */
  917. *location = ls + 1; /* and this is the result */
  918. if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
  919. *ns = *ne = '\0'; /* trim off the quotes */
  920. *name = ns + 1; /* and this is the name */
  921. } else if (ns) {
  922. /* An opening quote was found but no closing quote was. The closing
  923. * quote may actually be after the end of the bracketed number
  924. */
  925. if (strchr(le + 1, '\"')) {
  926. *ns = '\0';
  927. *name = ns + 1;
  928. ast_trim_blanks(*name);
  929. }
  930. } else { /* no quotes, trim off leading and trailing spaces */
  931. *name = ast_skip_blanks(instr);
  932. ast_trim_blanks(*name);
  933. }
  934. } else { /* no valid brackets */
  935. char tmp[256];
  936. ast_copy_string(tmp, instr, sizeof(tmp));
  937. ast_shrink_phone_number(tmp);
  938. if (ast_isphonenumber(tmp)) { /* Assume it's just a location */
  939. *name = NULL;
  940. strcpy(instr, tmp); /* safe, because tmp will always be the same size or smaller than instr */
  941. *location = instr;
  942. } else { /* Assume it's just a name. */
  943. *location = NULL;
  944. if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
  945. *ns = *ne = '\0'; /* trim off the quotes */
  946. *name = ns + 1; /* and this is the name */
  947. } else { /* no quotes, trim off leading and trailing spaces */
  948. *name = ast_skip_blanks(instr);
  949. ast_trim_blanks(*name);
  950. }
  951. }
  952. }
  953. return 0;
  954. }
  955. static int __ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int callwaiting, int codec)
  956. {
  957. if (ast_strlen_zero(name))
  958. name = NULL;
  959. if (ast_strlen_zero(number))
  960. number = NULL;
  961. return callerid_generate(buf, number, name, 0, callwaiting, codec);
  962. }
  963. int ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int codec)
  964. {
  965. return __ast_callerid_generate(buf, name, number, 0, codec);
  966. }
  967. int ast_callerid_callwaiting_generate(unsigned char *buf, const char *name, const char *number, int codec)
  968. {
  969. return __ast_callerid_generate(buf, name, number, 1, codec);
  970. }
  971. char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown)
  972. {
  973. if (!unknown)
  974. unknown = "<unknown>";
  975. if (name && num)
  976. snprintf(buf, bufsiz, "\"%s\" <%s>", name, num);
  977. else if (name)
  978. ast_copy_string(buf, name, bufsiz);
  979. else if (num)
  980. ast_copy_string(buf, num, bufsiz);
  981. else
  982. ast_copy_string(buf, unknown, bufsiz);
  983. return buf;
  984. }
  985. int ast_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen)
  986. {
  987. char *tmp;
  988. char *l = NULL, *n = NULL;
  989. tmp = ast_strdupa(buf);
  990. ast_callerid_parse(tmp, &n, &l);
  991. if (n)
  992. ast_copy_string(name, n, namelen);
  993. else
  994. name[0] = '\0';
  995. if (l) {
  996. ast_shrink_phone_number(l);
  997. ast_copy_string(num, l, numlen);
  998. } else
  999. num[0] = '\0';
  1000. return 0;
  1001. }
  1002. /*! \brief Translation table for Caller ID Presentation settings */
  1003. static struct {
  1004. int val;
  1005. const char *name;
  1006. const char *description;
  1007. } pres_types[] = {
  1008. { AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "allowed_not_screened", "Presentation Allowed, Not Screened"},
  1009. { AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", "Presentation Allowed, Passed Screen"},
  1010. { AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", "Presentation Allowed, Failed Screen"},
  1011. { AST_PRES_ALLOWED_NETWORK_NUMBER, "allowed", "Presentation Allowed, Network Number"},
  1012. { AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "prohib_not_screened", "Presentation Prohibited, Not Screened"},
  1013. { AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", "Presentation Prohibited, Passed Screen"},
  1014. { AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", "Presentation Prohibited, Failed Screen"},
  1015. { AST_PRES_PROHIB_NETWORK_NUMBER, "prohib", "Presentation Prohibited, Network Number"},
  1016. { AST_PRES_NUMBER_NOT_AVAILABLE, "unavailable", "Number Unavailable"},
  1017. };
  1018. /*! \brief Convert caller ID text code to value
  1019. used in config file parsing
  1020. \param data text string
  1021. \return value AST_PRES_ from callerid.h
  1022. */
  1023. int ast_parse_caller_presentation(const char *data)
  1024. {
  1025. int i;
  1026. if (!data) {
  1027. return -1;
  1028. }
  1029. for (i = 0; i < ARRAY_LEN(pres_types); i++) {
  1030. if (!strcasecmp(pres_types[i].name, data))
  1031. return pres_types[i].val;
  1032. }
  1033. return -1;
  1034. }
  1035. /*! \brief Convert caller ID pres value to explanatory string
  1036. \param data value (see callerid.h AST_PRES_ )
  1037. \return string for human presentation
  1038. */
  1039. const char *ast_describe_caller_presentation(int data)
  1040. {
  1041. int i;
  1042. for (i = 0; i < ARRAY_LEN(pres_types); i++) {
  1043. if (pres_types[i].val == data)
  1044. return pres_types[i].description;
  1045. }
  1046. return "unknown";
  1047. }
  1048. /*! \brief Convert caller ID pres value to text code
  1049. \param data text string
  1050. \return string for config file
  1051. */
  1052. const char *ast_named_caller_presentation(int data)
  1053. {
  1054. int i;
  1055. for (i = 0; i < ARRAY_LEN(pres_types); i++) {
  1056. if (pres_types[i].val == data)
  1057. return pres_types[i].name;
  1058. }
  1059. return "unknown";
  1060. }