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 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. * \retval name of the channel's assigned parking lot if it is defined by the channel in some way
  284. * \retval name of the default parking lot if it is not
  285. *
  286. * \note Channel needs to be locked while the returned string is in use.
  287. */
  288. const char *find_channel_parking_lot_name(struct ast_channel *chan);
  289. /*!
  290. * \since 12.0.0
  291. * \brief Flattens a dial string so that it can be written to/found from PBX extensions
  292. *
  293. * \param peername unflattened dial string. This will be flattened in place.
  294. */
  295. void flatten_dial_string(char *dialstring);
  296. /*!
  297. * \since 12.0.0
  298. * \brief Set a channel's position in the PBX after timeout using the parking lot settings
  299. *
  300. * \param pu Parked user who is entering/reentering the PBX
  301. * \param lot Parking lot the user was removed from.
  302. *
  303. * \retval 0 Position set successfully
  304. * \retval -1 Failed to set the position
  305. */
  306. int comeback_goto(struct parked_user *pu, struct parking_lot *lot);
  307. /*!
  308. * \since 12.0.0
  309. * \brief Add extensions for a parking lot configuration
  310. *
  311. * \param lot_cfg parking lot configuration to generate extensions for
  312. *
  313. * \retval 0 on success
  314. * \retval non-zero on failure
  315. */
  316. int parking_lot_cfg_create_extensions(struct parking_lot_cfg *lot_cfg);
  317. /*!
  318. * \since 12.0.0
  319. * \brief Remove extensions belonging to a parking lot configuration
  320. *
  321. * \param lot_cfg parking lot configuratin to remove extensions from
  322. *
  323. * \note This will not remove extensions registered non-exclusively even
  324. * if those extensions were registered by lot_cfg. Those are only
  325. * purged on a res_parking module reload.
  326. */
  327. void parking_lot_cfg_remove_extensions(struct parking_lot_cfg *lot_cfg);
  328. /*!
  329. * \since 12.0.0
  330. * \brief Pull a parked user out of its parking lot. Use this when you don't want to use the parked user afterwards.
  331. * \param user The parked user being pulled.
  332. *
  333. * \retval 0 on success
  334. * \retval -1 if the user didn't have its parking lot set
  335. */
  336. int unpark_parked_user(struct parked_user *user);
  337. /*!
  338. * \since 12.0.0
  339. * \brief Publish a stasis parked call message for the channel indicating failure to park.
  340. *
  341. * \param parkee channel belonging to the failed parkee
  342. */
  343. void publish_parked_call_failure(struct ast_channel *parkee);
  344. /*!
  345. * \since 12.0.0
  346. * \brief Publish a stasis parked call message for a given parked user
  347. *
  348. * \param pu pointer to a parked_user that we are generating the message for
  349. * \param event_type What parked call event type is provoking this message
  350. */
  351. void publish_parked_call(struct parked_user *pu, enum ast_parked_call_event_type event_type);
  352. /*!
  353. * \since 12.3.0
  354. * \brief Create a parking announcement subscription
  355. *
  356. * \param chan Channel that will receive the announcement
  357. * \param parkee_uuid Unique ID of the channel being parked
  358. * \param hangup_after if non-zero, have the channel hangup after hearing the announcement
  359. *
  360. * \retval 0 on success
  361. * \retval -1 on failure
  362. */
  363. int create_parked_subscription(struct ast_channel *chan, const char *parkee_uuid, int hangup_after);
  364. /*!
  365. * \since 12.0.0
  366. * \brief Setup a parked call on a parking bridge without needing to parse appdata
  367. *
  368. */
  369. struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker,
  370. const char *lot_name, const char *comeback_override,
  371. int use_ringing, int randomize, int time_limit, int silence_announcements);
  372. /*!
  373. * \since 12.0.0
  374. * \brief Function to prepare a channel for parking by determining which parking bridge should
  375. * be used, setting up a park common datastore so that the parking bridge will have access
  376. * to necessary parking information when joining, and applying various bridge roles to the
  377. * channel.
  378. *
  379. * \param parkee The channel being preparred for parking
  380. * \param parker The channel initiating the park; may be the parkee as well. May be NULL.
  381. * \param app_data arguments supplied to the Park application. May be NULL.
  382. * \param silence_announcements optional pointer to an integer where we want to store the silence option flag
  383. * this value should be initialized to 0 prior to calling park_common_setup.
  384. *
  385. * \retval reference to a parking bridge if successful
  386. * \retval NULL on failure
  387. *
  388. * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
  389. */
  390. struct ast_bridge *park_application_setup(struct ast_channel *parkee, struct ast_channel *parker,
  391. const char *app_data, int *silence_announcements);
  392. struct park_common_datastore {
  393. char *parker_uuid; /*!< Unique ID of the channel parking the call. */
  394. char *parker_dial_string; /*!< Dial string that we would attempt to call when timing out when comebacktoorigin=yes */
  395. char *comeback_override; /*!< Optional goto string for where to send the call after we are done */
  396. int randomize; /*!< Pick a parking space to enter on at random */
  397. int time_limit; /*!< time limit override. -1 values don't override, 0 for unlimited time, >0 for custom time limit in seconds */
  398. int silence_announce; /*!< Used when a call parks itself to keep it from hearing the parked call announcement */
  399. };
  400. /*!
  401. * \since 12.0.0
  402. * \brief Get a copy of the park_common_datastore from a channel that is being parked
  403. *
  404. * \param parkee The channel entering parking with the datastore we are checking
  405. *
  406. * \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.
  407. * \retval NULL if the park_common_datastore could not be copied off of the channel.
  408. */
  409. struct park_common_datastore *get_park_common_datastore_copy(struct ast_channel *parkee);
  410. /*!
  411. * \since 12.0.0
  412. * \brief Free a park common datastore struct
  413. *
  414. * \param datastore The park_common_datastore being free'd. (NULL tolerant)
  415. */
  416. void park_common_datastore_free(struct park_common_datastore *datastore);
  417. /*!
  418. * \since 12.0.0
  419. * \brief Notify metermaids that we've changed an extension
  420. *
  421. * \param exten Extension of the call parked/unparked
  422. * \param context Context of the call parked/unparked
  423. * \param state new device state
  424. */
  425. void parking_notify_metermaids(int exten, const char *context, enum ast_device_state state);
  426. /*!
  427. * \since 12.0.0
  428. * \brief Check global configuration to see if dynamic parking is enabled
  429. *
  430. * \retval 1 if dynamic parking is enabled
  431. * \retval 0 if dynamic parking is disabled
  432. */
  433. int parking_dynamic_lots_enabled(void);
  434. /*!
  435. * \since 12.0.0
  436. * \brief Register parking applications
  437. *
  438. * \retval 0 if successful
  439. * \retval -1 on failure
  440. */
  441. int load_parking_applications(void);
  442. /*!
  443. * \since 12.0.0
  444. * \brief Unregister parking applications
  445. */
  446. void unload_parking_applications(void);
  447. /*!
  448. * \since 12.0.0
  449. * \brief Register CLI commands
  450. *
  451. * \retval 0 if successful
  452. * \retval -1 on failure
  453. */
  454. int load_parking_ui(void);
  455. /*!
  456. * \since 12.0.0
  457. * \brief Unregister CLI commands
  458. */
  459. void unload_parking_ui(void);
  460. /*!
  461. * \since 12.0.0
  462. * \brief Register manager actions and setup subscriptions for stasis events
  463. */
  464. int load_parking_manager(void);
  465. /*!
  466. * \since 12.0.0
  467. * \brief Unregister manager actions and remove subscriptions for stasis events
  468. */
  469. void unload_parking_manager(void);
  470. /*!
  471. * \since 12.0.0
  472. * \brief Register bridge features for parking
  473. *
  474. * \retval 0 on success
  475. * \retval -1 on failure
  476. */
  477. int load_parking_bridge_features(void);
  478. /*!
  479. * \since 12.0.0
  480. * \brief Unregister features registered by load_parking_bridge_features
  481. */
  482. void unload_parking_bridge_features(void);
  483. /*!
  484. * \since 12.0.0
  485. * \brief Register Parking devstate handler
  486. */
  487. int load_parking_devstate(void);
  488. /*!
  489. * \since 12.0.0
  490. * \brief Unregister Parking devstate handler
  491. */
  492. void unload_parking_devstate(void);
  493. /*!
  494. * \since 12.0.0
  495. * \brief Register parking unit tests
  496. *
  497. * \retval 0 on success
  498. * \retval nonzero on failure
  499. */
  500. int load_parking_tests(void);
  501. /*!
  502. * \since 12.0.0
  503. * \brief Unregister parking unit tests
  504. *
  505. * \return Nothing
  506. */
  507. void unload_parking_tests(void);
  508. struct ast_module_info;
  509. /*!
  510. * \since 12.0.0
  511. * \brief Get res_parking's module info
  512. *
  513. * \retval res_parking's ast_module
  514. */
  515. const struct ast_module_info *parking_get_module_info(void);