res_parking.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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 Call Parking Resource Internal API
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. */
  24. #include "asterisk/pbx.h"
  25. #include "asterisk/bridge.h"
  26. #include "asterisk/parking.h"
  27. #include "asterisk/stasis_channels.h"
  28. #define DEFAULT_PARKING_LOT "default"
  29. #define DEFAULT_PARKING_EXTEN "700"
  30. #define BASE_REGISTRAR "res_parking"
  31. #define PARK_DIAL_CONTEXT "park-dial"
  32. #define PARKED_CALL_APPLICATION "ParkedCall"
  33. enum park_call_resolution {
  34. PARK_UNSET = 0, /*! Nothing set a resolution. This should never be observed in practice. */
  35. PARK_ABANDON, /*! The channel for the parked call hung up */
  36. PARK_TIMEOUT, /*! The parked call stayed parked until the parking lot timeout was reached and was removed */
  37. PARK_FORCED, /*! The parked call was forcibly terminated by an unusual means in Asterisk */
  38. PARK_ANSWERED, /*! The parked call was retrieved successfully */
  39. };
  40. enum parked_call_feature_options {
  41. OPT_PARKEDPLAY = 0,
  42. OPT_PARKEDTRANSFERS,
  43. OPT_PARKEDREPARKING,
  44. OPT_PARKEDHANGUP,
  45. OPT_PARKEDRECORDING,
  46. };
  47. enum parking_lot_modes {
  48. PARKINGLOT_NORMAL = 0, /*! The parking lot is configured normally and can accept new calls. Disable on reload if the config isn't replaced.
  49. * valid transitions: PARKINGLOT_DISABLED */
  50. PARKINGLOT_DYNAMIC, /*! The parking lot is a dynamically created parking lot. It can be parked to at any time. Disabled on last parked call leaving.
  51. * valid transitions: PARKINGLOT_DISABLED */
  52. PARKINGLOT_DISABLED, /*! The parking lot is no longer linked to a parking lot in configuration. It can no longer be parked to.
  53. * and it can not be parked to. This mode has no transitions. */
  54. };
  55. struct parking_lot_cfg {
  56. int parking_start; /*!< First space in the parking lot */
  57. int parking_stop; /*!< Last space in the parking lot */
  58. unsigned int parkingtime; /*!< Analogous to parkingtime config option */
  59. unsigned int comebackdialtime; /*!< Analogous to comebackdialtime config option */
  60. unsigned int parkfindnext; /*!< Analogous to parkfindnext config option */
  61. unsigned int parkext_exclusive; /*!< Analogous to parkext_exclusive config option */
  62. unsigned int parkaddhints; /*!< Analogous to parkaddhints config option */
  63. unsigned int comebacktoorigin; /*!< Analogous to comebacktoorigin config option */
  64. int parkedplay; /*!< Analogous to parkedplay config option */
  65. int parkedcalltransfers; /*!< Analogous to parkedcalltransfers config option */
  66. int parkedcallreparking; /*!< Analogous to parkedcallreparking config option */
  67. int parkedcallhangup; /*!< Analogous to parkedcallhangup config option */
  68. int parkedcallrecording; /*!< Analogous to parkedcallrecording config option */
  69. AST_DECLARE_STRING_FIELDS(
  70. AST_STRING_FIELD(name); /*!< Name of the parking lot configuration object */
  71. AST_STRING_FIELD(registrar); /*!< Which registrar the lot uses if it isn't the default registrar */
  72. AST_STRING_FIELD(mohclass); /*!< Analogous to mohclass config option */
  73. AST_STRING_FIELD(parkext); /*!< Analogous to parkext config option */
  74. AST_STRING_FIELD(parking_con); /*!< Analogous to context config option */
  75. AST_STRING_FIELD(comebackcontext); /*!< Analogous to comebackcontext config option */
  76. AST_STRING_FIELD(courtesytone); /*!< Analogous to courtesytone config option */
  77. );
  78. };
  79. struct parking_lot {
  80. int next_space; /*!< When using parkfindnext, which space we should start searching from next time we park */
  81. struct ast_bridge *parking_bridge; /*!< Bridged where parked calls will rest until they are answered or otherwise leave */
  82. struct ao2_container *parked_users; /*!< List of parked users rigidly ordered by their parking space */
  83. struct parking_lot_cfg *cfg; /*!< Reference to configuration object for the parking lot */
  84. enum parking_lot_modes mode; /*!< Whether a parking lot is operational, being reconfigured, primed for deletion, or dynamically created. */
  85. int disable_mark; /*!< On reload, disable this parking lot if it doesn't receive a new configuration. */
  86. AST_DECLARE_STRING_FIELDS(
  87. AST_STRING_FIELD(name); /*!< Name of the parking lot object */
  88. );
  89. };
  90. struct parked_user {
  91. struct ast_channel *chan; /*!< Parked channel */
  92. struct ast_channel_snapshot *retriever; /*!< Snapshot of the channel that retrieves a parked call */
  93. struct timeval start; /*!< When the call was parked */
  94. int parking_space; /*!< Which parking space is used */
  95. char comeback[AST_MAX_CONTEXT]; /*!< Where to go on parking timeout */
  96. char *parker_dial_string; /*!< dialstring to call back with comebacktoorigin. Used timeout extension generation and call control */
  97. unsigned int time_limit; /*!< How long this specific channel may remain in the parking lot before timing out */
  98. struct parking_lot *lot; /*!< Which parking lot the user is parked to */
  99. enum park_call_resolution resolution; /*!< How did the parking session end? If the call is in a bridge, lock parked_user before checking/setting */
  100. };
  101. #if defined(TEST_FRAMEWORK)
  102. /*!
  103. * \since 12.0.0
  104. * \brief Create an empty parking lot configuration structure
  105. * useful for unit tests.
  106. *
  107. * \param cat name given to the parking lot
  108. *
  109. * \retval NULL failure
  110. * \retval non-NULL successfully allocated parking lot
  111. */
  112. struct parking_lot_cfg *parking_lot_cfg_create(const char *cat);
  113. #endif
  114. /*!
  115. * \since 12.0.0
  116. * \brief If a parking lot exists in the parking lot list already, update its status to match the provided
  117. * configuration and return a reference return a reference to it. Otherwise, create a parking lot
  118. * struct based on a parking lot configuration and return a reference to the new one.
  119. *
  120. * \param cfg The configuration being used as a reference to build the parking lot from.
  121. * \param dynamic non-zero if creating a dynamic parking lot with this. Don't replace existing parking lots. Ever.
  122. *
  123. * \retval A reference to the new parking lot
  124. * \retval NULL if it was not found and could not be allocated
  125. *
  126. * \note The parking lot will need to be unreffed if it ever falls out of scope
  127. * \note The parking lot will automatically be added to the parking lot container if needed as part of this process
  128. */
  129. struct parking_lot *parking_lot_build_or_update(struct parking_lot_cfg *cfg, int dynamic);
  130. /*!
  131. * \since 12.0.0
  132. * \brief Remove a parking lot from the usable lists if it is no longer involved in any calls and no configuration currently claims it
  133. *
  134. * \param lot Which parking lot is being checked for elimination
  135. *
  136. * \retval 0 if the parking lot was removed
  137. * \retval -1 if the parking lot wasn't removed.
  138. *
  139. * \note This should generally be called when something is happening that could cause a parking lot to die such as a call being unparked or
  140. * a parking lot no longer existing in configurations.
  141. */
  142. int parking_lot_remove_if_unused(struct parking_lot *lot);
  143. /*!
  144. * \since 12.0.0
  145. * \brief Create a new parking bridge
  146. *
  147. * \param bridge_lot Parking lot which the new bridge should be based on
  148. *
  149. * \retval NULL if the bridge can not be created
  150. * \retval Newly created parking bridge
  151. */
  152. struct ast_bridge *bridge_parking_new(struct parking_lot *bridge_lot);
  153. /*!
  154. * \since 12.0.0
  155. * \brief Get a reference to a parking lot's bridge. If it doesn't exist, create it and get a reference.
  156. *
  157. * \param lot Which parking lot we need the bridge from. This parking lot must be locked before calling this function.
  158. *
  159. * \retval A reference to the ast_bridge associated with the parking lot
  160. * \retval NULL if it didn't already have a bridge and one couldn't be created
  161. *
  162. * \note This bridge will need to be unreffed if it ever falls out of scope.
  163. */
  164. struct ast_bridge *parking_lot_get_bridge(struct parking_lot *lot);
  165. /*!
  166. * \since 12.0.0
  167. * \brief Get an available parking space within a parking lot.
  168. *
  169. * \param lot Which parking lot we are getting a space from
  170. * \param target_override If there is a specific slot we want, provide it here and we'll start from that position
  171. *
  172. * \retval -1 if No slot can be found
  173. * \retval integer value of parking space selected
  174. *
  175. * \note lot should be locked before this is called and unlocked only after a parked_user with the space
  176. * returned has been added to the parking lot.
  177. */
  178. int parking_lot_get_space(struct parking_lot *lot, int target_override);
  179. /*!
  180. * \since 12.0.0
  181. * \brief Determine if there is a parked user in a parking space and pull it from the parking lot if there is.
  182. *
  183. * \param lot Parking lot being pulled from
  184. * \param target If < 0 search for the first occupied space in the parking lot
  185. * If >= 0 Only pull from the indicated target
  186. *
  187. * \retval NULL if no parked user could be pulled from the requested parking lot at the requested parking space
  188. * \retval reference to the requested parked user
  189. *
  190. * \note The parked user will be removed from parking lot as part of this process
  191. * \note Remove this reference with ao2_cleanup once it falls out of scope.
  192. */
  193. struct parked_user *parking_lot_retrieve_parked_user(struct parking_lot *lot, int target);
  194. /*!
  195. * \since 12.0.0
  196. * \brief Apply features based on the parking lot feature options
  197. *
  198. * \param chan Which channel's feature set is being modified
  199. * \param lot parking lot which establishes the features used
  200. * \param recipient_mode AST_FEATURE_FLAG_BYCALLER if the user is the retriever
  201. * AST_FEATURE_FLAG_BYCALLEE if the user is the parkee
  202. */
  203. void parked_call_retrieve_enable_features(struct ast_channel *chan, struct parking_lot *lot, int recipient_mode);
  204. /*!
  205. * \since 12.0.0
  206. * \brief Set necessary bridge roles on a channel that is about to enter a parking lot
  207. *
  208. * \param chan Entering channel
  209. * \param lot The parking lot the channel will be entering
  210. * \param force_ringing Use ringing instead of music on hold
  211. *
  212. * \retval 0 on success
  213. * \retval non-zero on failure
  214. */
  215. int parking_channel_set_roles(struct ast_channel *chan, struct parking_lot *lot, int force_ringing);
  216. /*!
  217. * \since 12.0.0
  218. * \brief custom callback function for ast_bridge_channel_queue_playfile which plays a parking space
  219. * and optionally hangs up the call afterwards based on the payload in playfile.
  220. */
  221. void say_parking_space(struct ast_bridge_channel *bridge_channel, const char *payload);
  222. /*!
  223. * \since 12.0.0
  224. * \brief Setup timeout interval feature on an ast_bridge_features for parking
  225. *
  226. * \param features The ast_bridge_features we are establishing the interval hook on
  227. * \param user The parked_user receiving the timeout duration limits
  228. */
  229. void parking_set_duration(struct ast_bridge_features *features, struct parked_user *user);
  230. /*!
  231. * \since 12.0.0
  232. * \brief Get a pointer to the parking lot container for purposes such as iteration
  233. *
  234. * \retval pointer to the parking lot container.
  235. */
  236. struct ao2_container *get_parking_lot_container(void);
  237. /*!
  238. * \since 12.0.0
  239. * \brief Find a parking lot based on its name
  240. *
  241. * \param lot_name Name of the parking lot sought
  242. *
  243. * \retval The parking lot if found
  244. * \retval NULL if no parking lot with the name specified exists
  245. *
  246. * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
  247. */
  248. struct parking_lot *parking_lot_find_by_name(const char *lot_name);
  249. /*!
  250. * \since 12.0.0
  251. * \brief Create a dynamic parking lot
  252. *
  253. * \param name Dynamic parking lot name to create
  254. * \param chan Channel parkee to get dynamic parking lot parameters from
  255. *
  256. * \retval dynamically created parking lot on success
  257. * \retval NULL on error
  258. *
  259. * \note This should be called only after verifying that the named parking lot doesn't already exist in a non-dynamic way.
  260. */
  261. struct parking_lot *parking_create_dynamic_lot(const char *name, struct ast_channel *chan);
  262. #if defined(TEST_FRAMEWORK)
  263. /*!
  264. * \since 12.0.0
  265. * \brief Create a dynamic parking lot without respect to whether they are enabled by configuration
  266. *
  267. * \param name Dynamic parking lot name to create
  268. * \param chan Channel parkee to get the dynamic parking lot parameters from
  269. *
  270. * \retval dynamically created parking lot on success
  271. * \retval NULL on error
  272. *
  273. * \note This should be called only after verifying that the named parking lot doesn't already exist in a non-dynamic way.
  274. */
  275. struct parking_lot *parking_create_dynamic_lot_forced(const char *name, struct ast_channel *chan);
  276. #endif
  277. /*!
  278. * \since 12.0.0
  279. * \brief Find parking lot name from channel
  280. *
  281. * \param chan The channel we want the parking lot name for
  282. *
  283. * \return name of the parking lot to use for the channel.
  284. *
  285. * \note Always returns a parking lot name.
  286. *
  287. * \note Channel needs to be locked while the returned string is in use.
  288. */
  289. const char *find_channel_parking_lot_name(struct ast_channel *chan);
  290. /*!
  291. * \since 12.0.0
  292. * \brief Flattens a dial string so that it can be written to/found from PBX extensions
  293. *
  294. * \param peername unflattened dial string. This will be flattened in place.
  295. */
  296. void flatten_dial_string(char *dialstring);
  297. /*!
  298. * \since 12.0.0
  299. * \brief Set a channel's position in the PBX after timeout using the parking lot settings
  300. *
  301. * \param pu Parked user who is entering/reentering the PBX
  302. * \param lot Parking lot the user was removed from.
  303. *
  304. * \retval 0 Position set successfully
  305. * \retval -1 Failed to set the position
  306. */
  307. int comeback_goto(struct parked_user *pu, struct parking_lot *lot);
  308. /*!
  309. * \since 12.0.0
  310. * \brief Add extensions for a parking lot configuration
  311. *
  312. * \param lot_cfg parking lot configuration to generate extensions for
  313. *
  314. * \retval 0 on success
  315. * \retval non-zero on failure
  316. */
  317. int parking_lot_cfg_create_extensions(struct parking_lot_cfg *lot_cfg);
  318. /*!
  319. * \since 12.0.0
  320. * \brief Remove extensions belonging to a parking lot configuration
  321. *
  322. * \param lot_cfg parking lot configuratin to remove extensions from
  323. *
  324. * \note This will not remove extensions registered non-exclusively even
  325. * if those extensions were registered by lot_cfg. Those are only
  326. * purged on a res_parking module reload.
  327. */
  328. void parking_lot_cfg_remove_extensions(struct parking_lot_cfg *lot_cfg);
  329. /*!
  330. * \since 12.0.0
  331. * \brief Pull a parked user out of its parking lot. Use this when you don't want to use the parked user afterwards.
  332. * \param user The parked user being pulled.
  333. *
  334. * \retval 0 on success
  335. * \retval -1 if the user didn't have its parking lot set
  336. */
  337. int unpark_parked_user(struct parked_user *user);
  338. /*!
  339. * \since 12.0.0
  340. * \brief Publish a stasis parked call message for the channel indicating failure to park.
  341. *
  342. * \param parkee channel belonging to the failed parkee
  343. */
  344. void publish_parked_call_failure(struct ast_channel *parkee);
  345. /*!
  346. * \since 12.0.0
  347. * \brief Publish a stasis parked call message for a given parked user
  348. *
  349. * \param pu pointer to a parked_user that we are generating the message for
  350. * \param event_type What parked call event type is provoking this message
  351. */
  352. void publish_parked_call(struct parked_user *pu, enum ast_parked_call_event_type event_type);
  353. /*!
  354. * \since 12.3.0
  355. * \brief Create a parking announcement subscription
  356. *
  357. * \param chan Channel that will receive the announcement
  358. * \param parkee_uuid Unique ID of the channel being parked
  359. * \param hangup_after if non-zero, have the channel hangup after hearing the announcement
  360. *
  361. * \retval 0 on success
  362. * \retval -1 on failure
  363. */
  364. int create_parked_subscription(struct ast_channel *chan, const char *parkee_uuid, int hangup_after);
  365. /*!
  366. * \since 12.0.0
  367. * \brief Setup a parked call on a parking bridge without needing to parse appdata
  368. *
  369. */
  370. struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker,
  371. const char *lot_name, const char *comeback_override,
  372. int use_ringing, int randomize, int time_limit, int silence_announcements);
  373. /*!
  374. * \since 12.0.0
  375. * \brief Function to prepare a channel for parking by determining which parking bridge should
  376. * be used, setting up a park common datastore so that the parking bridge will have access
  377. * to necessary parking information when joining, and applying various bridge roles to the
  378. * channel.
  379. *
  380. * \param parkee The channel being preparred for parking
  381. * \param parker The channel initiating the park; may be the parkee as well. May be NULL.
  382. * \param app_data arguments supplied to the Park application. May be NULL.
  383. * \param silence_announcements optional pointer to an integer where we want to store the silence option flag
  384. * this value should be initialized to 0 prior to calling park_common_setup.
  385. *
  386. * \retval reference to a parking bridge if successful
  387. * \retval NULL on failure
  388. *
  389. * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
  390. */
  391. struct ast_bridge *park_application_setup(struct ast_channel *parkee, struct ast_channel *parker,
  392. const char *app_data, int *silence_announcements);
  393. struct park_common_datastore {
  394. char *parker_uuid; /*!< Unique ID of the channel parking the call. */
  395. char *parker_dial_string; /*!< Dial string that we would attempt to call when timing out when comebacktoorigin=yes */
  396. char *comeback_override; /*!< Optional goto string for where to send the call after we are done */
  397. int randomize; /*!< Pick a parking space to enter on at random */
  398. int time_limit; /*!< time limit override. -1 values don't override, 0 for unlimited time, >0 for custom time limit in seconds */
  399. int silence_announce; /*!< Used when a call parks itself to keep it from hearing the parked call announcement */
  400. };
  401. /*!
  402. * \since 12.0.0
  403. * \brief Get a copy of the park_common_datastore from a channel that is being parked
  404. *
  405. * \param parkee The channel entering parking with the datastore we are checking
  406. *
  407. * \retval Pointer to a copy of the park common datastore for parkee if it could be cloned. This needs to be free'd with park_common_datastore free.
  408. * \retval NULL if the park_common_datastore could not be copied off of the channel.
  409. */
  410. struct park_common_datastore *get_park_common_datastore_copy(struct ast_channel *parkee);
  411. /*!
  412. * \since 12.0.0
  413. * \brief Free a park common datastore struct
  414. *
  415. * \param datastore The park_common_datastore being free'd. (NULL tolerant)
  416. */
  417. void park_common_datastore_free(struct park_common_datastore *datastore);
  418. /*!
  419. * \since 12.0.0
  420. * \brief Notify metermaids that we've changed an extension
  421. *
  422. * \param exten Extension of the call parked/unparked
  423. * \param context Context of the call parked/unparked
  424. * \param state new device state
  425. */
  426. void parking_notify_metermaids(int exten, const char *context, enum ast_device_state state);
  427. /*!
  428. * \since 12.0.0
  429. * \brief Check global configuration to see if dynamic parking is enabled
  430. *
  431. * \retval 1 if dynamic parking is enabled
  432. * \retval 0 if dynamic parking is disabled
  433. */
  434. int parking_dynamic_lots_enabled(void);
  435. /*!
  436. * \since 12.0.0
  437. * \brief Register parking applications
  438. *
  439. * \retval 0 if successful
  440. * \retval -1 on failure
  441. */
  442. int load_parking_applications(void);
  443. /*!
  444. * \since 12.0.0
  445. * \brief Unregister parking applications
  446. */
  447. void unload_parking_applications(void);
  448. /*!
  449. * \since 12.0.0
  450. * \brief Register CLI commands
  451. *
  452. * \retval 0 if successful
  453. * \retval -1 on failure
  454. */
  455. int load_parking_ui(void);
  456. /*!
  457. * \since 12.0.0
  458. * \brief Unregister CLI commands
  459. */
  460. void unload_parking_ui(void);
  461. /*!
  462. * \since 12.0.0
  463. * \brief Register manager actions and setup subscriptions for stasis events
  464. */
  465. int load_parking_manager(void);
  466. /*!
  467. * \since 12.0.0
  468. * \brief Unregister manager actions and remove subscriptions for stasis events
  469. */
  470. void unload_parking_manager(void);
  471. /*!
  472. * \since 12.0.0
  473. * \brief Register bridge features for parking
  474. *
  475. * \retval 0 on success
  476. * \retval -1 on failure
  477. */
  478. int load_parking_bridge_features(void);
  479. /*!
  480. * \since 12.0.0
  481. * \brief Unregister features registered by load_parking_bridge_features
  482. */
  483. void unload_parking_bridge_features(void);
  484. /*!
  485. * \since 12.0.0
  486. * \brief Register Parking devstate handler
  487. */
  488. int load_parking_devstate(void);
  489. /*!
  490. * \since 12.0.0
  491. * \brief Unregister Parking devstate handler
  492. */
  493. void unload_parking_devstate(void);
  494. /*!
  495. * \since 12.0.0
  496. * \brief Register parking unit tests
  497. *
  498. * \retval 0 on success
  499. * \retval nonzero on failure
  500. */
  501. int load_parking_tests(void);
  502. /*!
  503. * \since 12.0.0
  504. * \brief Unregister parking unit tests
  505. *
  506. * \return Nothing
  507. */
  508. void unload_parking_tests(void);
  509. struct ast_module_info;
  510. /*!
  511. * \since 12.0.0
  512. * \brief Get res_parking's module info
  513. *
  514. * \retval res_parking's ast_module
  515. */
  516. const struct ast_module_info *parking_get_module_info(void);