callerid.c 36 KB

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