callerid.c 37 KB

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