cdr.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. /*!
  19. * \file
  20. * \brief Call Detail Record API
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. #ifndef _ASTERISK_CDR_H
  25. #define _ASTERISK_CDR_H
  26. #include "asterisk/channel.h"
  27. /*! \file
  28. *
  29. * \since 12
  30. *
  31. * \brief Call Detail Record Engine.
  32. *
  33. * \page CDR Call Detail Record Engine
  34. *
  35. * \par Intro
  36. *
  37. * The Call Detail Record (CDR) engine uses the \ref stasis Stasis Message Bus
  38. * to build records for the channels in Asterisk. As the state of a channel and
  39. * the bridges it participates in changes, notifications are sent over the
  40. * Stasis Message Bus. The CDR engine consumes these notifications and builds
  41. * records that reflect that state. Over the lifetime of a channel, many CDRs
  42. * may be generated for that channel or that involve that channel.
  43. *
  44. * CDRs have a lifecycle that is a subset of the channel that they reflect. A
  45. * single CDR for a channel represents a path of communication between the
  46. * endpoint behind a channel and Asterisk, or between two endpoints. When a
  47. * channel establishes a new path of communication, a new CDR is created for the
  48. * channel. Likewise, when a path of communication is terminated, a CDR is
  49. * finalized. Finally, when a channel is no longer present in Asterisk, all CDRs
  50. * for that channel are dispatched for recording.
  51. *
  52. * Dispatching of CDRs occurs to registered CDR backends. CDR backends register
  53. * through \ref ast_cdr_register and are responsible for taking the produced
  54. * CDRs and storing them in permanent storage.
  55. *
  56. * \par CDR attributes
  57. *
  58. * While a CDR can have many attributes, all CDRs have two parties: a Party A
  59. * and a Party B. The Party A is \em always the channel that owns the CDR. A CDR
  60. * may or may not have a Party B, depending on its state.
  61. *
  62. * For the most part, attributes on a CDR are reflective of those same
  63. * attributes on the channel at the time when the CDR was finalized. Specific
  64. * CDR attributes include:
  65. * \li \c start The time when the CDR was created
  66. * \li \c answer The time when the Party A was answered, or when the path of
  67. * communication between Party A and Party B was established
  68. * \li \c end The time when the CDR was finalized
  69. * \li \c duration \c end - \c start. If \c end is not known, the current time
  70. * is used
  71. * \li \c billsec \c end - \c answer. If \c end is not known, the current time
  72. * is used
  73. * \li \c userfield User set data on some party in the CDR
  74. *
  75. * Note that \c accountcode and \c amaflags are actually properties of a
  76. * channel, not the CDR.
  77. *
  78. * \par CDR States
  79. *
  80. * CDRs go through various states during their lifetime. State transitions occur
  81. * due to messages received over the \ref stasis Stasis Message Bus. The
  82. * following describes the possible states a CDR can be in, and how it
  83. * transitions through the states.
  84. *
  85. * \par Single
  86. *
  87. * When a CDR is created, it is put into the Single state. The Single state
  88. * represents a CDR for a channel that has no Party B. CDRs can be unanswered
  89. * or answered while in the Single state.
  90. *
  91. * The following transitions can occur while in the Single state:
  92. * \li If a \ref ast_channel_dial_type indicating a Dial Begin is received, the
  93. * state transitions to Dial
  94. * \li If a \ref ast_channel_snapshot is received indicating that the channel
  95. * has hung up, the state transitions to Finalized
  96. * \li If a \ref ast_bridge_blob_type is received indicating a Bridge Enter, the
  97. * state transitions to Bridge
  98. * \li If a \ref ast_bridge_blob_type message indicating an entrance to a
  99. * holding bridge with a subclass type of "parking" is received, the CDR is
  100. * transitioned to the Parked state.
  101. *
  102. * \par Dial
  103. *
  104. * This state represents a dial that is occurring within Asterisk. The Party A
  105. * can either be the caller for a two party dial, or it can be the dialed party
  106. * if the calling party is Asterisk (that is, an Originated channel). In the
  107. * first case, the Party B is \em always the dialed channel; in the second case,
  108. * the channel is not considered to be a "dialed" channel as it is alone in the
  109. * dialed operation.
  110. *
  111. * While in the Dial state, multiple CDRs can be created for the Party A if a
  112. * parallel dial occurs. Each dialed party receives its own CDR with Party A.
  113. *
  114. * The following transitions can occur while in the Dial state:
  115. * \li If a \ref ast_channel_dial_type indicating a Dial End is received where
  116. * the \ref dial_status is not ANSWER, the state transitions to Finalized
  117. * \li If a \ref ast_channel_snapshot is received indicating that the channel
  118. * has hung up, the state transitions to Finalized
  119. * \li If a \ref ast_channel_dial_type indicating a Dial End is received where
  120. * the \ref dial_status is ANSWER, the state transitions to DialedPending
  121. * \li If a \ref ast_bridge_blob_type is received indicating a Bridge Enter, the
  122. * state transitions to Bridge
  123. *
  124. * \par DialedPending
  125. *
  126. * Technically, after being dialed, a CDR does not have to transition to the
  127. * Bridge state. If the channel being dialed was originated, the channel may
  128. * being executing dialplan. Strangely enough, it is also valid to have both
  129. * Party A and Party B - after a dial - to not be bridged and instead execute
  130. * dialplan. DialedPending handles the state where we figure out if the CDR
  131. * showing the dial needs to move to the Bridge state; if the CDR should show
  132. * that we started executing dialplan; of if we need a new CDR.
  133. *
  134. * The following transition can occur while in the DialedPending state:
  135. * \li If a \ref ast_channel_snapshot is received that indicates that the
  136. * channel has begun executing dialplan, we transition to the Finalized state
  137. * if we have a Party B. Otherwise, we transition to the Single state.
  138. * \li If a \ref ast_bridge_blob_type is received indicating a Bridge Enter, the
  139. * state transitions to Bridge (through the Dial state)
  140. * \li If a \ref ast_bridge_blob_type message indicating an entrance to a
  141. * holding bridge with a subclass type of "parking" is received, the CDR is
  142. * transitioned to the Parked state.
  143. *
  144. * \par Bridge
  145. *
  146. * The Bridge state represents a path of communication between Party A and one
  147. * or more other parties. When a CDR enters into the Bridge state, the following
  148. * occurs:
  149. * \li The CDR attempts to find a Party B. If the CDR has a Party B, it looks
  150. * for that channel in the bridge and updates itself accordingly. If the CDR
  151. * does not yet have a Party B, it attempts to find a channel that can be its
  152. * Party B. If it finds one, it updates itself; otherwise, the CDR is
  153. * temporarily finalized.
  154. * \li Once the CDR has a Party B or it is determined that it cannot have a
  155. * Party B, new CDRs are created for each pairing of channels with the CDR's
  156. * Party A.
  157. *
  158. * As an example, consider the following:
  159. * \li A Dials B - both answer
  160. * \li B joins a bridge. Since no one is in the bridge and it was a dialed
  161. * channel, it cannot have a Party B.
  162. * \li A joins the bridge. Since A's Party B is B, A updates itself with B.
  163. * \li Now say an Originated channel, C, joins the bridge. The bridge becomes
  164. * a multi-party bridge.
  165. * \li C attempts to get a Party B. A cannot be C's Party B, as it was created
  166. * before it. B is a dialed channel and can thus be C's Party B, so C's CDR
  167. * updates its Party B to B.
  168. * \li New CDRs are now generated. A gets a new CDR for A -> C. B is dialed, and
  169. * hence cannot get any CDR.
  170. * \li Now say another Originated channel, D, joins the bridge. Say D has the
  171. * \ref party_a flag set on it, such that it is always the preferred Party A.
  172. * As such, it takes A as its Party B.
  173. * \li New CDRs are generated. D gets new CDRs for D -> B and D -> C.
  174. *
  175. * The following transitions can occur while in the Bridge state:
  176. * \li If a \ref ast_bridge_blob_type message indicating a leave is received,
  177. * the state transitions to the Finalized state.
  178. *
  179. * \par Parked
  180. *
  181. * Parking is technically just another bridge in the Asterisk bridging
  182. * framework. Unlike other bridges, however there are several key distinctions:
  183. * \li With normal bridges, you want to show paths of communication between
  184. * the participants. In parking, however, each participant is independent.
  185. * From the perspective of a CDR, a call in parking should look like a dialplan
  186. * application just executed.
  187. * \li Holding bridges are typically items using in more complex applications,
  188. * and so we usually don't want to show them. However, with Park, there is no
  189. * application execution - often, a channel will be put directly into the
  190. * holding bridge, bypassing the dialplan. This occurs when a call is blind
  191. * transferred to a parking extension.
  192. *
  193. * As such, if a channel enters a bridge and that happens to be a holding bridge
  194. * with a subclass type of "parking", we transition the CDR into the Parked
  195. * state. The parking Stasis message updates the application name and data to
  196. * reflect that the channel is in parking. When this occurs, a special flag is
  197. * set on the CDR that prevents the application name from being updates by
  198. * subsequent channel snapshot updates.
  199. *
  200. * The following transitions can occur while in the Parked state:
  201. * \li If a \ref ast_bridge_blob_type message indicating a leave is received,
  202. * the state transitions to the Finalized state
  203. *
  204. * \par Finalized
  205. *
  206. * Once a CDR enters the finalized state, it is finished. No further updates
  207. * can be made to the party information, and the CDR cannot be changed.
  208. *
  209. * One exception to this occurs during linkedid propagation, in which the CDRs
  210. * linkedids are updated based on who the channel is bridged with. In general,
  211. * however, a finalized CDR is waiting for dispatch to the CDR backends.
  212. */
  213. /*! \brief CDR engine settings */
  214. enum ast_cdr_settings {
  215. CDR_ENABLED = 1 << 0, /*!< Enable CDRs */
  216. CDR_BATCHMODE = 1 << 1, /*!< Whether or not we should dispatch CDRs in batches */
  217. CDR_UNANSWERED = 1 << 2, /*!< Log unanswered CDRs */
  218. CDR_CONGESTION = 1 << 3, /*!< Treat congestion as if it were a failed call */
  219. CDR_END_BEFORE_H_EXTEN = 1 << 4, /*!< End the CDR before the 'h' extension runs */
  220. CDR_INITIATED_SECONDS = 1 << 5, /*!< Include microseconds into the billing time */
  221. CDR_DEBUG = 1 << 6, /*!< Enables extra debug statements */
  222. };
  223. /*! \brief CDR Batch Mode settings */
  224. enum ast_cdr_batch_mode_settings {
  225. BATCH_MODE_SCHEDULER_ONLY = 1 << 0, /*!< Don't spawn a thread to handle the batches - do it on the scheduler */
  226. BATCH_MODE_SAFE_SHUTDOWN = 1 << 1, /*!< During safe shutdown, submit the batched CDRs */
  227. };
  228. /*!
  229. * \brief CDR manipulation options. Certain function calls will manipulate the
  230. * state of a CDR object based on these flags.
  231. */
  232. enum ast_cdr_options {
  233. AST_CDR_FLAG_KEEP_VARS = (1 << 0), /*!< Copy variables during the operation */
  234. AST_CDR_FLAG_DISABLE = (1 << 1), /*!< Disable the current CDR */
  235. AST_CDR_FLAG_DISABLE_ALL = (3 << 1), /*!< Disable the CDR and all future CDRs */
  236. AST_CDR_FLAG_PARTY_A = (1 << 3), /*!< Set the channel as party A */
  237. AST_CDR_FLAG_FINALIZE = (1 << 4), /*!< Finalize the current CDRs */
  238. AST_CDR_FLAG_SET_ANSWER = (1 << 5), /*!< If the channel is answered, set the answer time to now */
  239. AST_CDR_FLAG_RESET = (1 << 6), /*!< If set, set the start and answer time to now */
  240. AST_CDR_LOCK_APP = (1 << 7), /*!< Prevent any further changes to the application field/data field for this CDR */
  241. };
  242. /*!
  243. * \brief CDR Flags - Disposition
  244. */
  245. enum ast_cdr_disposition {
  246. AST_CDR_NOANSWER = 0,
  247. AST_CDR_NULL = (1 << 0),
  248. AST_CDR_FAILED = (1 << 1),
  249. AST_CDR_BUSY = (1 << 2),
  250. AST_CDR_ANSWERED = (1 << 3),
  251. AST_CDR_CONGESTION = (1 << 4),
  252. };
  253. /*! \brief The global options available for CDRs */
  254. struct ast_cdr_config {
  255. struct ast_flags settings; /*!< CDR settings */
  256. struct batch_settings {
  257. unsigned int time; /*!< Time between batches */
  258. unsigned int size; /*!< Size to trigger a batch */
  259. struct ast_flags settings; /*!< Settings for batches */
  260. } batch_settings;
  261. };
  262. /*!
  263. * \brief Responsible for call detail data
  264. */
  265. struct ast_cdr {
  266. /*! Caller*ID with text */
  267. char clid[AST_MAX_EXTENSION];
  268. /*! Caller*ID number */
  269. char src[AST_MAX_EXTENSION];
  270. /*! Destination extension */
  271. char dst[AST_MAX_EXTENSION];
  272. /*! Destination context */
  273. char dcontext[AST_MAX_EXTENSION];
  274. char channel[AST_MAX_EXTENSION];
  275. /*! Destination channel if appropriate */
  276. char dstchannel[AST_MAX_EXTENSION];
  277. /*! Last application if appropriate */
  278. char lastapp[AST_MAX_EXTENSION];
  279. /*! Last application data */
  280. char lastdata[AST_MAX_EXTENSION];
  281. struct timeval start;
  282. struct timeval answer;
  283. struct timeval end;
  284. /*! Total time in system, in seconds */
  285. long int duration;
  286. /*! Total time call is up, in seconds */
  287. long int billsec;
  288. /*! What happened to the call */
  289. long int disposition;
  290. /*! What flags to use */
  291. long int amaflags;
  292. /*! What account number to use */
  293. char accountcode[AST_MAX_ACCOUNT_CODE];
  294. /*! Account number of the last person we talked to */
  295. char peeraccount[AST_MAX_ACCOUNT_CODE];
  296. /*! flags */
  297. unsigned int flags;
  298. /*! Unique Channel Identifier */
  299. char uniqueid[AST_MAX_UNIQUEID];
  300. /*! Linked group Identifier */
  301. char linkedid[AST_MAX_UNIQUEID];
  302. /*! User field */
  303. char userfield[AST_MAX_USER_FIELD];
  304. /*! Sequence field */
  305. int sequence;
  306. /*! A linked list for variables */
  307. struct varshead varshead;
  308. struct ast_cdr *next;
  309. };
  310. /*!
  311. * \since 12
  312. * \brief Obtain the current CDR configuration
  313. *
  314. * The configuration is a ref counted object. The caller of this function must
  315. * decrement the ref count when finished with the configuration.
  316. *
  317. * \retval NULL on error
  318. * \retval The current CDR configuration
  319. */
  320. struct ast_cdr_config *ast_cdr_get_config(void);
  321. /*!
  322. * \since 12
  323. * \brief Set the current CDR configuration
  324. *
  325. * \param config The new CDR configuration
  326. */
  327. void ast_cdr_set_config(struct ast_cdr_config *config);
  328. /*!
  329. * \since 12
  330. * \brief Format a CDR variable from an already posted CDR
  331. *
  332. * \param cdr The dispatched CDR to process
  333. * \param name The name of the variable
  334. * \param ret Pointer to the formatted buffer
  335. * \param workspace A pointer to the buffer to use to format the variable
  336. * \param workspacelen The size of \ref workspace
  337. * \param raw If non-zero and a date/time is extraced, provide epoch seconds. Otherwise format as a date/time stamp
  338. */
  339. void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw);
  340. /*!
  341. * \since 12
  342. * \brief Retrieve a CDR variable from a channel's current CDR
  343. *
  344. * \param channel_name The name of the party A channel that the CDR is associated with
  345. * \param name The name of the variable to retrieve
  346. * \param value Buffer to hold the value
  347. * \param length The size of the buffer
  348. *
  349. * \retval 0 on success
  350. * \retval non-zero on failure
  351. */
  352. int ast_cdr_getvar(const char *channel_name, const char *name, char *value, size_t length);
  353. /*!
  354. * \since 12
  355. * \brief Set a variable on a CDR
  356. *
  357. * \param channel_name The channel to set the variable on
  358. * \param name The name of the variable to set
  359. * \param value The value of the variable to set
  360. *
  361. * \retval 0 on success
  362. * \retval non-zero on failure
  363. */
  364. int ast_cdr_setvar(const char *channel_name, const char *name, const char *value);
  365. /*!
  366. * \since 12
  367. * \brief Fork a CDR
  368. *
  369. * \param channel_name The name of the channel whose CDR should be forked
  370. * \param options Options to control how the fork occurs.
  371. *
  372. * \retval 0 on success
  373. * \retval -1 on failure
  374. */
  375. int ast_cdr_fork(const char *channel_name, struct ast_flags *options);
  376. /*!
  377. * \since 12
  378. * \brief Set a property on a CDR for a channel
  379. *
  380. * This function sets specific administrative properties on a CDR for a channel.
  381. * This includes properties like preventing a CDR from being dispatched, to
  382. * setting the channel as the preferred Party A in future CDRs. See
  383. * \ref enum ast_cdr_options for more information.
  384. *
  385. * \param channel_name The CDR's channel
  386. * \param option Option to apply to the CDR
  387. *
  388. * \retval 0 on success
  389. * \retval 1 on error
  390. */
  391. int ast_cdr_set_property(const char *channel_name, enum ast_cdr_options option);
  392. /*!
  393. * \since 12
  394. * \brief Clear a property on a CDR for a channel
  395. *
  396. * Clears a flag previously set by \ref ast_cdr_set_property
  397. *
  398. * \param channel_name The CDR's channel
  399. * \param option Option to clear from the CDR
  400. *
  401. * \retval 0 on success
  402. * \retval 1 on error
  403. */
  404. int ast_cdr_clear_property(const char *channel_name, enum ast_cdr_options option);
  405. /*!
  406. * \brief Reset the detail record
  407. * \param channel_name The channel that the CDR is associated with
  408. * \param keep_variables Keep the variables during the reset. If zero,
  409. * variables are discarded during the reset.
  410. *
  411. * \retval 0 on success
  412. * \retval -1 on failure
  413. */
  414. int ast_cdr_reset(const char *channel_name, int keep_variables);
  415. /*!
  416. * \brief Serializes all the data and variables for a current CDR record
  417. * \param channel_name The channel to get the CDR for
  418. * \param buf A buffer to use for formatting the data
  419. * \param delim A delimeter to use to separate variable keys/values
  420. * \param sep A separator to use between nestings
  421. * \retval the total number of serialized variables
  422. */
  423. int ast_cdr_serialize_variables(const char *channel_name, struct ast_str **buf, char delim, char sep);
  424. /*!
  425. * \brief CDR backend callback
  426. * \warning CDR backends should NOT attempt to access the channel associated
  427. * with a CDR record. This channel is not guaranteed to exist when the CDR
  428. * backend is invoked.
  429. */
  430. typedef int (*ast_cdrbe)(struct ast_cdr *cdr);
  431. /*! \brief Return TRUE if CDR subsystem is enabled */
  432. int ast_cdr_is_enabled(void);
  433. /*!
  434. * \brief Allocate a CDR record
  435. * \retval a malloc'd ast_cdr structure
  436. * \retval NULL on error (malloc failure)
  437. */
  438. struct ast_cdr *ast_cdr_alloc(void);
  439. struct stasis_message_router;
  440. /*!
  441. * \brief Return the message router for the CDR engine
  442. *
  443. * This returns the \ref stasis_message_router that the CDR engine
  444. * uses for dispatching \ref stasis messages. The reference on the
  445. * message router is bumped and must be released by the caller of
  446. * this function.
  447. *
  448. * \retval NULL if the CDR engine is disabled or unavailable
  449. * \retval the \ref stasis_message_router otherwise
  450. */
  451. struct stasis_message_router *ast_cdr_message_router(void);
  452. /*!
  453. * \brief Duplicate a public CDR
  454. * \param cdr the record to duplicate
  455. *
  456. * \retval a malloc'd ast_cdr structure,
  457. * \retval NULL on error (malloc failure)
  458. */
  459. struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr);
  460. /*!
  461. * \brief Free a CDR record
  462. * \param cdr ast_cdr structure to free
  463. * Returns nothing
  464. */
  465. void ast_cdr_free(struct ast_cdr *cdr);
  466. /*!
  467. * \brief Register a CDR handling engine
  468. * \param name name associated with the particular CDR handler
  469. * \param desc description of the CDR handler
  470. * \param be function pointer to a CDR handler
  471. * Used to register a Call Detail Record handler.
  472. * \retval 0 on success.
  473. * \retval -1 on error
  474. */
  475. int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be);
  476. /*!
  477. * \brief Unregister a CDR handling engine
  478. * \param name name of CDR handler to unregister
  479. * Unregisters a CDR by it's name
  480. *
  481. * \retval 0 The backend unregistered successfully
  482. * \retval -1 The backend could not be unregistered at this time
  483. */
  484. int ast_cdr_unregister(const char *name);
  485. /*!
  486. * \brief Suspend a CDR backend temporarily
  487. *
  488. * \retval 0 The backend is suspdended
  489. * \retval -1 The backend could not be suspended
  490. */
  491. int ast_cdr_backend_suspend(const char *name);
  492. /*!
  493. * \brief Unsuspend a CDR backend
  494. *
  495. * \retval 0 The backend was unsuspended
  496. * \retval -1 The back could not be unsuspended
  497. */
  498. int ast_cdr_backend_unsuspend(const char *name);
  499. /*!
  500. * \brief Disposition to a string
  501. * \param disposition input binary form
  502. * Converts the binary form of a disposition to string form.
  503. * \return a pointer to the string form
  504. */
  505. const char *ast_cdr_disp2str(int disposition);
  506. /*!
  507. * \brief Set CDR user field for channel (stored in CDR)
  508. *
  509. * \param channel_name The name of the channel that owns the CDR
  510. * \param userfield The user field to set
  511. */
  512. void ast_cdr_setuserfield(const char *channel_name, const char *userfield);
  513. /*! \brief Reload the configuration file cdr.conf and start/stop CDR scheduling thread */
  514. int ast_cdr_engine_reload(void);
  515. /*! \brief Load the configuration file cdr.conf and possibly start the CDR scheduling thread */
  516. int ast_cdr_engine_init(void);
  517. /*! Submit any remaining CDRs and prepare for shutdown */
  518. void ast_cdr_engine_term(void);
  519. #endif /* _ASTERISK_CDR_H */