callerid.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. * \brief CallerID (and other GR30) management and generation
  20. * Includes code and algorithms from the Zapata library.
  21. *
  22. * \ref CID
  23. *
  24. */
  25. /*!
  26. * \page CID Caller ID names and numbers
  27. *
  28. * Caller ID names are currently 8 bit characters, propably
  29. * ISO8859-1, depending on what your channel drivers handle.
  30. *
  31. * IAX2 and SIP caller ID names are UTF8
  32. * On ISDN Caller ID names are 7 bit, Almost ASCII
  33. * (See http://www.zytrax.com/tech/ia5.html )
  34. *
  35. * \note Asterisk does not currently support SIP utf8 caller ID names or caller ID's.
  36. *
  37. * \par See also
  38. * \arg \ref callerid.c
  39. * \arg \ref callerid.h
  40. * \arg \ref Def_CallerPres
  41. */
  42. #ifndef _ASTERISK_CALLERID_H
  43. #define _ASTERISK_CALLERID_H
  44. #include "asterisk/format.h"
  45. #define MAX_CALLERID_SIZE 32000
  46. #define CID_PRIVATE_NAME (1 << 0)
  47. #define CID_PRIVATE_NUMBER (1 << 1)
  48. #define CID_UNKNOWN_NAME (1 << 2)
  49. #define CID_UNKNOWN_NUMBER (1 << 3)
  50. #define CID_MSGWAITING (1 << 4)
  51. #define CID_NOMSGWAITING (1 << 5)
  52. #define CID_SIG_BELL 1
  53. #define CID_SIG_V23 2
  54. #define CID_SIG_DTMF 3
  55. #define CID_SIG_V23_JP 4
  56. #define CID_SIG_SMDI 5
  57. #define CID_START_RING 1
  58. #define CID_START_POLARITY 2
  59. #define CID_START_POLARITY_IN 3
  60. #define CID_START_DTMF_NOALERT 4
  61. /* defines dealing with message waiting indication generation */
  62. /*! MWI SDMF format */
  63. #define CID_MWI_TYPE_SDMF 0x00
  64. /*! MWI MDMF format -- generate only MWI field */
  65. #define CID_MWI_TYPE_MDMF 0x01
  66. /*! MWI MDMF format -- generate name, callerid, date and MWI fields */
  67. #define CID_MWI_TYPE_MDMF_FULL 0x02
  68. #define AST_LIN2X(a) ((ast_format_cmp(codec, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ? (AST_LIN2A(a)) : (AST_LIN2MU(a)))
  69. #define AST_XLAW(a) ((ast_format_cmp(codec, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ? (AST_ALAW(a)) : (AST_MULAW(a)))
  70. struct callerid_state;
  71. typedef struct callerid_state CIDSTATE;
  72. /*! \brief CallerID Initialization
  73. * \par
  74. * Initializes the callerid system. Mostly stuff for inverse FFT
  75. */
  76. void callerid_init(void);
  77. /*! \brief Generates a CallerID FSK stream in ulaw format suitable for transmission.
  78. * \param buf Buffer to use. If "buf" is supplied, it will use that buffer instead of allocating its own.
  79. * "buf" must be at least 32000 bytes in size of you want to be sure you don't have an overrun.
  80. * \param number Use NULL for no number or "P" for "private"
  81. * \param name name to be used
  82. * \param flags passed flags
  83. * \param callwaiting callwaiting flag
  84. * \param codec -- either AST_FORMAT_ULAW or AST_FORMAT_ALAW
  85. * \details
  86. * This function creates a stream of callerid (a callerid spill) data in ulaw format.
  87. * \return It returns the size
  88. * (in bytes) of the data (if it returns a size of 0, there is probably an error)
  89. */
  90. int callerid_generate(unsigned char *buf, const char *number, const char *name, int flags, int callwaiting, struct ast_format *codec);
  91. /*! \brief Create a callerID state machine
  92. * \param cid_signalling Type of signalling in use
  93. *
  94. * \details
  95. * This function returns a malloc'd instance of the callerid_state data structure.
  96. * \return Returns a pointer to a malloc'd callerid_state structure, or NULL on error.
  97. */
  98. struct callerid_state *callerid_new(int cid_signalling);
  99. /*! \brief Read samples into the state machine.
  100. * \param cid Which state machine to act upon
  101. * \param ubuf containing your samples
  102. * \param samples number of samples contained within the buffer.
  103. * \param codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
  104. *
  105. * \details
  106. * Send received audio to the Caller*ID demodulator.
  107. * \retval -1 on error
  108. * \retval 0 for "needs more samples"
  109. * \retval 1 if the CallerID spill reception is complete.
  110. */
  111. int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int samples, struct ast_format *codec);
  112. /*! \brief Read samples into the state machine.
  113. * \param cid Which state machine to act upon
  114. * \param ubuf containing your samples
  115. * \param samples number of samples contained within the buffer.
  116. * \param codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
  117. *
  118. * \details
  119. * Send received audio to the Caller*ID demodulator (for japanese style lines).
  120. * \retval -1 on error
  121. * \retval 0 for "needs more samples"
  122. * \retval 1 if the CallerID spill reception is complete.
  123. */
  124. int callerid_feed_jp(struct callerid_state *cid, unsigned char *ubuf, int samples, struct ast_format *codec);
  125. /*! \brief Extract info out of callerID state machine. Flags are listed above
  126. * \param cid Callerid state machine to act upon
  127. * \param number Pass the address of a pointer-to-char (will contain the phone number)
  128. * \param name Pass the address of a pointer-to-char (will contain the name)
  129. * \param flags Pass the address of an int variable(will contain the various callerid flags)
  130. *
  131. * \details
  132. * This function extracts a callerid string out of a callerid_state state machine.
  133. * If no number is found, *number will be set to NULL. Likewise for the name.
  134. * Flags can contain any of the following:
  135. *
  136. * \return Returns nothing.
  137. */
  138. void callerid_get(struct callerid_state *cid, char **number, char **name, int *flags);
  139. /*!
  140. * \brief Get and parse DTMF-based callerid
  141. * \param cidstring The actual transmitted string.
  142. * \param number The cid number is returned here.
  143. * \param flags The cid flags are returned here.
  144. */
  145. void callerid_get_dtmf(char *cidstring, char *number, int *flags);
  146. /*! \brief This function frees callerid_state cid.
  147. * \param cid This is the callerid_state state machine to free
  148. */
  149. void callerid_free(struct callerid_state *cid);
  150. /*! \brief Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format)
  151. * \param buf buffer for output samples. See callerid_generate() for details regarding buffer.
  152. * \param name Caller-ID Name
  153. * \param number Caller-ID Number
  154. * \param codec Asterisk codec (either AST_FORMAT_ALAW or AST_FORMAT_ULAW)
  155. *
  156. * \details
  157. * Acts like callerid_generate except uses an asterisk format callerid string.
  158. */
  159. int ast_callerid_generate(unsigned char *buf, const char *name, const char *number, struct ast_format *codec);
  160. /*!
  161. * \brief Generate message waiting indicator
  162. * \param buf
  163. * \param active The message indicator state
  164. * -- either 0 no messages in mailbox or 1 messages in mailbox
  165. * \param type Format of message (any of CID_MWI_TYPE_*)
  166. * \param codec
  167. * \param name
  168. * \param number
  169. * \param flags
  170. * \see callerid_generate() for more info as it uses the same encoding
  171. * \version 1.6.1 changed mdmf parameter to type, added name, number and flags for caller id message generation
  172. */
  173. int ast_callerid_vmwi_generate(unsigned char *buf, int active, int type, struct ast_format *codec, const char *name,
  174. const char *number, int flags);
  175. /*! \brief Generate Caller-ID spill but in a format suitable for Call Waiting(tm)'s Caller*ID(tm)
  176. * \see ast_callerid_generate() for other details
  177. */
  178. int ast_callerid_callwaiting_generate(unsigned char *buf, const char *name, const char *number, struct ast_format *codec);
  179. /*! \brief Destructively parse inbuf into name and location (or number)
  180. * \details
  181. * Parses callerid stream from inbuf and changes into useable form, outputed in name and location.
  182. * \param instr buffer of callerid stream (in audio form) to be parsed. Warning, data in buffer is changed.
  183. * \param name address of a pointer-to-char for the name value of the stream.
  184. * \param location address of a pointer-to-char for the phone number value of the stream.
  185. * \note XXX 'name' is not parsed consistently e.g. we have
  186. * input location name
  187. * " foo bar " <123> 123 ' foo bar ' (with spaces around)
  188. * " foo bar " NULL 'foo bar' (without spaces around)
  189. * The parsing of leading and trailing space/quotes should be more consistent.
  190. * \return Returns 0 on success, -1 on failure.
  191. */
  192. int ast_callerid_parse(char *instr, char **name, char **location);
  193. /*!
  194. * \brief Generate a CAS (CPE Alert Signal) tone for 'n' samples
  195. * \param outbuf Allocated buffer for data. Must be at least 2400 bytes unless no SAS is desired
  196. * \param sas Non-zero if CAS should be preceeded by SAS
  197. * \param len How many samples to generate.
  198. * \param codec Which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
  199. * \return Returns -1 on error (if len is less than 2400), 0 on success.
  200. */
  201. int ast_gen_cas(unsigned char *outbuf, int sas, int len, struct ast_format *codec);
  202. /*!
  203. * \brief Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s...
  204. * \param n The number to be stripped/shrunk
  205. * \return Returns nothing important
  206. */
  207. void ast_shrink_phone_number(char *n);
  208. /*!
  209. * \brief Check if a string consists only of digits and + \#
  210. * \param n number to be checked.
  211. * \return Returns 0 if n is a number, 1 if it's not.
  212. */
  213. int ast_isphonenumber(const char *n);
  214. /*!
  215. * \brief Check if a string consists only of digits and + \# ( ) - .
  216. * (meaning it can be cleaned with ast_shrink_phone_number)
  217. * \param exten The extension (or URI) to be checked.
  218. * \retval 1 if string is valid AST shrinkable phone number
  219. * \retval 0 if not
  220. */
  221. int ast_is_shrinkable_phonenumber(const char *exten);
  222. int ast_callerid_split(const char *src, char *name, int namelen, char *num, int numlen);
  223. char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown);
  224. /*
  225. * Caller*ID and other GR-30 compatible generation
  226. * routines (used by ADSI for example)
  227. */
  228. extern float cid_dr[4];
  229. extern float cid_di[4];
  230. extern float clidsb;
  231. static inline float callerid_getcarrier(float *cr, float *ci, int bit)
  232. {
  233. /* Move along. There's nothing to see here... */
  234. float t;
  235. t = *cr * cid_dr[bit] - *ci * cid_di[bit];
  236. *ci = *cr * cid_di[bit] + *ci * cid_dr[bit];
  237. *cr = t;
  238. t = 2.0 - (*cr * *cr + *ci * *ci);
  239. *cr *= t;
  240. *ci *= t;
  241. return *cr;
  242. }
  243. #define PUT_BYTE(a) do { \
  244. *(buf++) = (a); \
  245. bytes++; \
  246. } while(0)
  247. #define PUT_AUDIO_SAMPLE(y) do { \
  248. int __sample_idx = (short)(rint(8192.0 * (y))); \
  249. *(buf++) = AST_LIN2X(__sample_idx); \
  250. bytes++; \
  251. } while(0)
  252. #define PUT_CLID_MARKMS do { \
  253. int __clid_x; \
  254. for (__clid_x=0;__clid_x<8;__clid_x++) \
  255. PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, 1)); \
  256. } while(0)
  257. #define PUT_CLID_BAUD(bit) do { \
  258. while(scont < clidsb) { \
  259. PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, bit)); \
  260. scont += 1.0; \
  261. } \
  262. scont -= clidsb; \
  263. } while(0)
  264. #define PUT_CLID(byte) do { \
  265. int z; \
  266. unsigned char b = (byte); \
  267. PUT_CLID_BAUD(0); /* Start bit */ \
  268. for (z=0;z<8;z++) { \
  269. PUT_CLID_BAUD(b & 1); \
  270. b >>= 1; \
  271. } \
  272. PUT_CLID_BAUD(1); /* Stop bit */ \
  273. } while(0)
  274. /* Various defines and bits for handling PRI- and SS7-type restriction */
  275. #define AST_PRES_NUMBER_TYPE 0x03
  276. #define AST_PRES_USER_NUMBER_UNSCREENED 0x00
  277. #define AST_PRES_USER_NUMBER_PASSED_SCREEN 0x01
  278. #define AST_PRES_USER_NUMBER_FAILED_SCREEN 0x02
  279. #define AST_PRES_NETWORK_NUMBER 0x03
  280. #define AST_PRES_RESTRICTION 0x60
  281. #define AST_PRES_ALLOWED 0x00
  282. #define AST_PRES_RESTRICTED 0x20
  283. #define AST_PRES_UNAVAILABLE 0x40
  284. #define AST_PRES_RESERVED 0x60
  285. #define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED \
  286. (AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED)
  287. #define AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN \
  288. (AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN)
  289. #define AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN \
  290. (AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN)
  291. #define AST_PRES_ALLOWED_NETWORK_NUMBER \
  292. (AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER)
  293. #define AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED \
  294. (AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED)
  295. #define AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN \
  296. (AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN)
  297. #define AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN \
  298. (AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN)
  299. #define AST_PRES_PROHIB_NETWORK_NUMBER \
  300. (AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER)
  301. #define AST_PRES_NUMBER_NOT_AVAILABLE \
  302. (AST_PRES_UNAVAILABLE | AST_PRES_NETWORK_NUMBER)
  303. int ast_parse_caller_presentation(const char *data);
  304. const char *ast_describe_caller_presentation(int data);
  305. const char *ast_named_caller_presentation(int data);
  306. /*!
  307. * \page Def_CallerPres Caller ID Presentation
  308. *
  309. * Caller ID presentation values are used to set properties to a
  310. * caller ID in PSTN networks, and as RPID value in SIP transactions.
  311. *
  312. * The following values are available to use:
  313. * \arg \b Defined value, text string in config file, explanation
  314. *
  315. * \arg \b AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "allowed_not_screened", Presentation Allowed, Not Screened,
  316. * \arg \b AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", Presentation Allowed, Passed Screen,
  317. * \arg \b AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", Presentation Allowed, Failed Screen,
  318. * \arg \b AST_PRES_ALLOWED_NETWORK_NUMBER, "allowed", Presentation Allowed, Network Number,
  319. * \arg \b AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "prohib_not_screened", Presentation Prohibited, Not Screened,
  320. * \arg \b AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", Presentation Prohibited, Passed Screen,
  321. * \arg \b AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", Presentation Prohibited, Failed Screen,
  322. * \arg \b AST_PRES_PROHIB_NETWORK_NUMBER, "prohib", Presentation Prohibited, Network Number,
  323. *
  324. * \par References
  325. * \arg \ref callerid.h Definitions
  326. * \arg \ref callerid.c Functions
  327. * \arg \ref CID Caller ID names and numbers
  328. */
  329. /*!
  330. * \brief redirecting reason codes.
  331. *
  332. * This list attempts to encompass redirecting reasons
  333. * as defined by several channel technologies.
  334. */
  335. enum AST_REDIRECTING_REASON {
  336. AST_REDIRECTING_REASON_UNKNOWN,
  337. AST_REDIRECTING_REASON_USER_BUSY,
  338. AST_REDIRECTING_REASON_NO_ANSWER,
  339. AST_REDIRECTING_REASON_UNAVAILABLE,
  340. AST_REDIRECTING_REASON_UNCONDITIONAL,
  341. AST_REDIRECTING_REASON_TIME_OF_DAY,
  342. AST_REDIRECTING_REASON_DO_NOT_DISTURB,
  343. AST_REDIRECTING_REASON_DEFLECTION,
  344. AST_REDIRECTING_REASON_FOLLOW_ME,
  345. AST_REDIRECTING_REASON_OUT_OF_ORDER,
  346. AST_REDIRECTING_REASON_AWAY,
  347. AST_REDIRECTING_REASON_CALL_FWD_DTE, /* This is something defined in Q.931, and no I don't know what it means */
  348. AST_REDIRECTING_REASON_SEND_TO_VM,
  349. };
  350. /*!
  351. * \since 1.8
  352. * \brief Convert redirecting reason text code to value (used in config file parsing)
  353. *
  354. * \param data text string from config file
  355. *
  356. * \retval Q931_REDIRECTING_REASON from callerid.h
  357. * \retval -1 if not in table
  358. */
  359. int ast_redirecting_reason_parse(const char *data);
  360. /*!
  361. * \since 1.8
  362. * \brief Convert redirecting reason value to explanatory string
  363. *
  364. * \param data Q931_REDIRECTING_REASON from callerid.h
  365. *
  366. * \return string for human presentation
  367. */
  368. const char *ast_redirecting_reason_describe(int data);
  369. struct ast_party_redirecting_reason;
  370. /*!
  371. * \since 1.8
  372. * \brief Convert redirecting reason value to text code
  373. *
  374. * \param data ast_party_redirecting_reason structure from channel.h
  375. *
  376. * \return string for config file
  377. */
  378. const char *ast_redirecting_reason_name(const struct ast_party_redirecting_reason *data);
  379. /*!
  380. * \brief Connected line update source code
  381. */
  382. enum AST_CONNECTED_LINE_UPDATE_SOURCE {
  383. /*! Update for unknown reason (May be interpreted to mean from answer) */
  384. AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN,
  385. /*! Update from normal call answering */
  386. AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER,
  387. /*! Update from call diversion (Deprecated, use REDIRECTING updates instead.) */
  388. AST_CONNECTED_LINE_UPDATE_SOURCE_DIVERSION,
  389. /*! Update from call transfer(active) (Party has already answered) */
  390. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,
  391. /*! Update from call transfer(alerting) (Party has not answered yet) */
  392. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING
  393. };
  394. /*!
  395. * \since 1.8
  396. * \brief Convert connected line update source text code to value (used in config file parsing)
  397. *
  398. * \param data text string from config file
  399. *
  400. * \retval AST_CONNECTED_LINE_UPDATE_SOURCE from callerid.h
  401. * \retval -1 if not in table
  402. */
  403. int ast_connected_line_source_parse(const char *data);
  404. /*!
  405. * \since 1.8
  406. * \brief Convert connected line update source value to explanatory string
  407. *
  408. * \param data AST_CONNECTED_LINE_UPDATE_SOURCE from callerid.h
  409. *
  410. * \return string for human presentation
  411. */
  412. const char *ast_connected_line_source_describe(int data);
  413. /*!
  414. * \since 1.8
  415. * \brief Convert connected line update source value to text code
  416. *
  417. * \param data AST_CONNECTED_LINE_UPDATE_SOURCE from callerid.h
  418. *
  419. * \return string for config file
  420. */
  421. const char *ast_connected_line_source_name(int data);
  422. /*!
  423. * \since 1.8
  424. * \brief Convert ast_party_name.char_set text code to value (used in config file parsing)
  425. *
  426. * \param data text string from config file
  427. *
  428. * \retval AST_PARTY_CHAR_SET from channel.h
  429. * \retval -1 if not in table
  430. */
  431. int ast_party_name_charset_parse(const char *data);
  432. /*!
  433. * \since 1.8
  434. * \brief Convert ast_party_name.char_set value to explanatory string
  435. *
  436. * \param data AST_PARTY_CHAR_SET from channel.h
  437. *
  438. * \return string for human presentation
  439. */
  440. const char *ast_party_name_charset_describe(int data);
  441. /*!
  442. * \since 1.8
  443. * \brief Convert ast_party_name.char_set value to text code
  444. *
  445. * \param data AST_PARTY_CHAR_SET from channel.h
  446. *
  447. * \return string for config file
  448. */
  449. const char *ast_party_name_charset_str(int data);
  450. #endif /* _ASTERISK_CALLERID_H */