callerid.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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", (unsigned)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", (unsigned)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", (unsigned)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", (unsigned)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 *input_str, char **name, char **location)
  934. {
  935. char *ls;
  936. char *le;
  937. char *name_start;
  938. char *instr;
  939. int quotes_stripped = 0;
  940. /* Handle surrounding quotes */
  941. input_str = ast_strip(input_str);
  942. instr = ast_strip_quoted(input_str, "\"", "\"");
  943. if (instr != input_str) {
  944. quotes_stripped = 1;
  945. }
  946. /* Try "name" <location> format or name <location> format or with a missing > */
  947. if ((ls = strrchr(instr, '<'))) {
  948. if ((le = strrchr(ls, '>'))) {
  949. *le = '\0'; /* location found, trim off the brackets */
  950. }
  951. *ls = '\0';
  952. *location = ls + 1; /* and this is the result */
  953. name_start = ast_strip_quoted(instr, "\"", "\"");
  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 (!quotes_stripped && ast_isphonenumber(tmp)) { /* Assume it's just a location */
  959. name_start = 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. name_start = ast_strip_quoted(instr, "\"", "\"");
  965. }
  966. }
  967. if (name_start) {
  968. ast_unescape_quoted(name_start);
  969. }
  970. *name = name_start;
  971. return 0;
  972. }
  973. static int __ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int callwaiting, struct ast_format *codec)
  974. {
  975. if (ast_strlen_zero(name))
  976. name = NULL;
  977. if (ast_strlen_zero(number))
  978. number = NULL;
  979. return callerid_generate(buf, number, name, 0, callwaiting, codec);
  980. }
  981. int ast_callerid_generate(unsigned char *buf, const char *name, const char *number, struct ast_format *codec)
  982. {
  983. return __ast_callerid_generate(buf, name, number, 0, codec);
  984. }
  985. int ast_callerid_callwaiting_generate(unsigned char *buf, const char *name, const char *number, struct ast_format *codec)
  986. {
  987. return __ast_callerid_generate(buf, name, number, 1, codec);
  988. }
  989. char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown)
  990. {
  991. if (!unknown)
  992. unknown = "<unknown>";
  993. if (name && num) {
  994. char name_buf[128];
  995. ast_escape_quoted(name, name_buf, sizeof(name_buf));
  996. snprintf(buf, bufsiz, "\"%s\" <%s>", name_buf, 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. }
  1004. return buf;
  1005. }
  1006. int ast_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen)
  1007. {
  1008. char *tmp;
  1009. char *l = NULL, *n = NULL;
  1010. tmp = ast_strdupa(buf);
  1011. ast_callerid_parse(tmp, &n, &l);
  1012. if (n)
  1013. ast_copy_string(name, n, namelen);
  1014. else
  1015. name[0] = '\0';
  1016. if (l) {
  1017. ast_shrink_phone_number(l);
  1018. ast_copy_string(num, l, numlen);
  1019. } else
  1020. num[0] = '\0';
  1021. return 0;
  1022. }
  1023. struct ast_value_translation {
  1024. int value;
  1025. const char *name;
  1026. const char *description;
  1027. };
  1028. /*! \brief Translation table for Caller ID Presentation settings */
  1029. static const struct ast_value_translation pres_types[] = {
  1030. /* *INDENT-OFF* */
  1031. { AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED, "allowed_not_screened", "Presentation Allowed, Not Screened" },
  1032. { AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", "Presentation Allowed, Passed Screen" },
  1033. { AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", "Presentation Allowed, Failed Screen" },
  1034. { AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER, "allowed", "Presentation Allowed, Network Number" },
  1035. { AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED, "prohib_not_screened", "Presentation Prohibited, Not Screened" },
  1036. { AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", "Presentation Prohibited, Passed Screen" },
  1037. { AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", "Presentation Prohibited, Failed Screen" },
  1038. { AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER, "prohib", "Presentation Prohibited, Network Number" },
  1039. { AST_PRES_UNAVAILABLE | AST_PRES_NETWORK_NUMBER, "unavailable", "Number Unavailable" }, /* Default name to value conversion. */
  1040. { AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_UNSCREENED, "unavailable", "Number Unavailable" },
  1041. { AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_FAILED_SCREEN, "unavailable", "Number Unavailable" },
  1042. { AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_PASSED_SCREEN, "unavailable", "Number Unavailable" },
  1043. /* *INDENT-ON* */
  1044. };
  1045. /*!
  1046. * \brief Convert caller ID text code to value (used in config file parsing)
  1047. * \param data text string from config file
  1048. * \retval value AST_PRES_ from callerid.h
  1049. * \retval -1 if not in table
  1050. */
  1051. int ast_parse_caller_presentation(const char *data)
  1052. {
  1053. int index;
  1054. if (!data) {
  1055. return -1;
  1056. }
  1057. for (index = 0; index < ARRAY_LEN(pres_types); ++index) {
  1058. if (!strcasecmp(pres_types[index].name, data)) {
  1059. return pres_types[index].value;
  1060. }
  1061. }
  1062. return -1;
  1063. }
  1064. /*!
  1065. * \brief Convert caller ID pres value to explanatory string
  1066. * \param data AST_PRES_ value from callerid.h
  1067. * \return string for human presentation
  1068. */
  1069. const char *ast_describe_caller_presentation(int data)
  1070. {
  1071. int index;
  1072. for (index = 0; index < ARRAY_LEN(pres_types); ++index) {
  1073. if (pres_types[index].value == data) {
  1074. return pres_types[index].description;
  1075. }
  1076. }
  1077. return "unknown";
  1078. }
  1079. /*!
  1080. * \brief Convert caller ID pres value to text code
  1081. * \param data AST_PRES_ value from callerid.h
  1082. * \return string for config file
  1083. */
  1084. const char *ast_named_caller_presentation(int data)
  1085. {
  1086. int index;
  1087. for (index = 0; index < ARRAY_LEN(pres_types); ++index) {
  1088. if (pres_types[index].value == data) {
  1089. return pres_types[index].name;
  1090. }
  1091. }
  1092. return "unknown";
  1093. }
  1094. /*! \brief Translation table for redirecting reason settings */
  1095. static const struct ast_value_translation redirecting_reason_types[] = {
  1096. /* *INDENT-OFF* */
  1097. { AST_REDIRECTING_REASON_UNKNOWN, "unknown", "Unknown" },
  1098. { AST_REDIRECTING_REASON_USER_BUSY, "cfb", "Call Forwarding Busy" },
  1099. { AST_REDIRECTING_REASON_NO_ANSWER, "cfnr", "Call Forwarding No Reply" },
  1100. { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable", "Callee is Unavailable" },
  1101. { AST_REDIRECTING_REASON_UNCONDITIONAL, "cfu", "Call Forwarding Unconditional" },
  1102. { AST_REDIRECTING_REASON_TIME_OF_DAY, "time_of_day", "Time of Day" },
  1103. { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "dnd", "Do Not Disturb" },
  1104. { AST_REDIRECTING_REASON_DEFLECTION, "deflection", "Call Deflection" },
  1105. { AST_REDIRECTING_REASON_FOLLOW_ME, "follow_me", "Follow Me" },
  1106. { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out_of_order", "Called DTE Out-Of-Order" },
  1107. { AST_REDIRECTING_REASON_AWAY, "away", "Callee is Away" },
  1108. { AST_REDIRECTING_REASON_CALL_FWD_DTE, "cf_dte", "Call Forwarding By The Called DTE" },
  1109. { AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm", "Call is being redirected to user's voicemail"},
  1110. /* *INDENT-ON* */
  1111. };
  1112. int ast_redirecting_reason_parse(const char *data)
  1113. {
  1114. int index;
  1115. for (index = 0; index < ARRAY_LEN(redirecting_reason_types); ++index) {
  1116. if (!strcasecmp(redirecting_reason_types[index].name, data)) {
  1117. return redirecting_reason_types[index].value;
  1118. }
  1119. }
  1120. return -1;
  1121. }
  1122. const char *ast_redirecting_reason_describe(int data)
  1123. {
  1124. int index;
  1125. for (index = 0; index < ARRAY_LEN(redirecting_reason_types); ++index) {
  1126. if (redirecting_reason_types[index].value == data) {
  1127. return redirecting_reason_types[index].description;
  1128. }
  1129. }
  1130. return "not-known";
  1131. }
  1132. const char *ast_redirecting_reason_name(int data)
  1133. {
  1134. int index;
  1135. for (index = 0; index < ARRAY_LEN(redirecting_reason_types); ++index) {
  1136. if (redirecting_reason_types[index].value == data) {
  1137. return redirecting_reason_types[index].name;
  1138. }
  1139. }
  1140. return "not-known";
  1141. }
  1142. /*! \brief Translation table for connected line update source settings */
  1143. static const struct ast_value_translation connected_line_source_types[] = {
  1144. /* *INDENT-OFF* */
  1145. { AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN, "unknown", "Unknown" },
  1146. { AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER, "answer", "Normal Call Answering" },
  1147. { AST_CONNECTED_LINE_UPDATE_SOURCE_DIVERSION, "diversion", "Call Diversion (Deprecated, use REDIRECTING)" },
  1148. { AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, "transfer_active", "Call Transfer(Active)" },
  1149. { AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, "transfer", "Call Transfer(Active)" },/* Old name must come after new name. */
  1150. { AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING, "transfer_alerting", "Call Transfer(Alerting)" }
  1151. /* *INDENT-ON* */
  1152. };
  1153. int ast_connected_line_source_parse(const char *data)
  1154. {
  1155. int index;
  1156. for (index = 0; index < ARRAY_LEN(connected_line_source_types); ++index) {
  1157. if (!strcasecmp(connected_line_source_types[index].name, data)) {
  1158. return connected_line_source_types[index].value;
  1159. }
  1160. }
  1161. return -1;
  1162. }
  1163. const char *ast_connected_line_source_describe(int data)
  1164. {
  1165. int index;
  1166. for (index = 0; index < ARRAY_LEN(connected_line_source_types); ++index) {
  1167. if (connected_line_source_types[index].value == data) {
  1168. return connected_line_source_types[index].description;
  1169. }
  1170. }
  1171. return "not-known";
  1172. }
  1173. const char *ast_connected_line_source_name(int data)
  1174. {
  1175. int index;
  1176. for (index = 0; index < ARRAY_LEN(connected_line_source_types); ++index) {
  1177. if (connected_line_source_types[index].value == data) {
  1178. return connected_line_source_types[index].name;
  1179. }
  1180. }
  1181. return "not-known";
  1182. }
  1183. /*! \brief Translation table for ast_party_name char-set settings */
  1184. static const struct ast_value_translation party_name_charset_tbl[] = {
  1185. /* *INDENT-OFF* */
  1186. { AST_PARTY_CHAR_SET_UNKNOWN, "unknown", "Unknown" },
  1187. { AST_PARTY_CHAR_SET_ISO8859_1, "iso8859-1", "ISO8859-1" },
  1188. { AST_PARTY_CHAR_SET_WITHDRAWN, "withdrawn", "Withdrawn" },
  1189. { AST_PARTY_CHAR_SET_ISO8859_2, "iso8859-2", "ISO8859-2" },
  1190. { AST_PARTY_CHAR_SET_ISO8859_3, "iso8859-3", "ISO8859-3" },
  1191. { AST_PARTY_CHAR_SET_ISO8859_4, "iso8859-4", "ISO8859-4" },
  1192. { AST_PARTY_CHAR_SET_ISO8859_5, "iso8859-5", "ISO8859-5" },
  1193. { AST_PARTY_CHAR_SET_ISO8859_7, "iso8859-7", "ISO8859-7" },
  1194. { AST_PARTY_CHAR_SET_ISO10646_BMPSTRING, "bmp", "ISO10646 Bmp String" },
  1195. { AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING, "utf8", "ISO10646 UTF-8 String" },
  1196. /* *INDENT-ON* */
  1197. };
  1198. int ast_party_name_charset_parse(const char *data)
  1199. {
  1200. int index;
  1201. for (index = 0; index < ARRAY_LEN(party_name_charset_tbl); ++index) {
  1202. if (!strcasecmp(party_name_charset_tbl[index].name, data)) {
  1203. return party_name_charset_tbl[index].value;
  1204. }
  1205. }
  1206. return -1;
  1207. }
  1208. const char *ast_party_name_charset_describe(int data)
  1209. {
  1210. int index;
  1211. for (index = 0; index < ARRAY_LEN(party_name_charset_tbl); ++index) {
  1212. if (party_name_charset_tbl[index].value == data) {
  1213. return party_name_charset_tbl[index].description;
  1214. }
  1215. }
  1216. return "not-known";
  1217. }
  1218. const char *ast_party_name_charset_str(int data)
  1219. {
  1220. int index;
  1221. for (index = 0; index < ARRAY_LEN(party_name_charset_tbl); ++index) {
  1222. if (party_name_charset_tbl[index].value == data) {
  1223. return party_name_charset_tbl[index].name;
  1224. }
  1225. }
  1226. return "not-known";
  1227. }