res_parking.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend>bridge_holding</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. /*** DOCUMENTATION
  29. <configInfo name="res_parking" language="en_US">
  30. <configFile name="res_parking.conf">
  31. <configObject name="globals">
  32. <synopsis>Options that apply to every parking lot</synopsis>
  33. <configOption name="parkeddynamic">
  34. <synopsis>Enables dynamically created parkinglots.</synopsis>
  35. <description>
  36. <para>If the option is enabled then the following variables can
  37. be used to dynamically create new parking lots.
  38. </para>
  39. <para>The <variable>PARKINGDYNAMIC</variable> variable specifies the
  40. parking lot to use as a template to create a dynamic parking lot. It
  41. is an error to specify a non-existent parking lot for the template.
  42. If not set then the default parking lot is used as the template.
  43. </para>
  44. <para>The <variable>PARKINGDYNCONTEXT</variable> variable specifies the
  45. dialplan context to use for the newly created dynamic parking lot. If
  46. not set then the context from the parking lot template is used. The
  47. context is created if it does not already exist and the new parking lot
  48. needs to create extensions.
  49. </para>
  50. <para>The <variable>PARKINGDYNEXTEN</variable> variable specifies the
  51. <literal>parkext</literal> to use for the newly created dynamic
  52. parking lot. If not set then the <literal>parkext</literal> is used from
  53. the parking lot template. If the template does not specify a
  54. <literal>parkext</literal> then no extensions are created for the newly
  55. created parking lot. The dynamic parking lot cannot be created if it
  56. needs to create extensions that overlap existing parking lot extensions.
  57. The only exception to this is for the <literal>parkext</literal>
  58. extension and only if neither of the overlaping parking lot's
  59. <literal>parkext</literal> is exclusive.
  60. </para>
  61. <para>The <variable>PARKINGDYNPOS</variable> variable specifies the
  62. parking positions to use for the newly created dynamic parking lot. If
  63. not set then the <literal>parkpos</literal> from the parking lot template
  64. is used.
  65. </para>
  66. </description>
  67. </configOption>
  68. </configObject>
  69. <configObject name="parking_lot">
  70. <synopsis>Defined parking lots for res_parking to use to park calls on</synopsis>
  71. <configOption name="context" default="parkedcalls">
  72. <synopsis>The name of the context where calls are parked and picked up from.</synopsis>
  73. <description><para>This option is only used if parkext is set.</para></description>
  74. </configOption>
  75. <configOption name="parkext">
  76. <synopsis>Extension to park calls to this parking lot.</synopsis>
  77. <description>
  78. <para>If this option is used, this extension will automatically
  79. be created to place calls into parking lots. In addition, if
  80. <literal>parkext_exclusive</literal> is set for this parking
  81. lot, the name of the parking lot will be included in the
  82. application's arguments so that it only parks to this parking
  83. lot. The extension will be created in <literal>context</literal>.
  84. Using this option also creates extensions for retrieving
  85. parked calls from the parking spaces in the same context.
  86. </para>
  87. <note>
  88. <para>Generated parking extensions cannot overlap.
  89. The only exception is if neither overlapping
  90. <literal>parkext</literal> is exclusive.
  91. </para>
  92. </note>
  93. </description>
  94. </configOption>
  95. <configOption name="parkext_exclusive" default="no">
  96. <synopsis>If yes, the extension registered as parkext will park exclusively to this parking lot.</synopsis>
  97. </configOption>
  98. <configOption name="parkpos" default="701-750">
  99. <synopsis>Numerical range of parking spaces which can be used to retrieve parked calls.</synopsis>
  100. <description>
  101. <para>If <literal>parkext</literal> is set, these extensions
  102. will automatically be mapped in <literal>context</literal>
  103. in order to pick up calls parked to these parking spaces.
  104. </para>
  105. </description>
  106. </configOption>
  107. <configOption name="parkinghints" default="no">
  108. <synopsis>If yes, this parking lot will add hints automatically for parking spaces.</synopsis>
  109. </configOption>
  110. <configOption name="parkingtime" default="45">
  111. <synopsis>Amount of time a call will remain parked before giving up (in seconds).</synopsis>
  112. </configOption>
  113. <configOption name="parkedmusicclass">
  114. <synopsis>Which music class to use for parked calls. They will use the default if unspecified.</synopsis>
  115. </configOption>
  116. <configOption name="comebacktoorigin" default="yes">
  117. <synopsis>Determines what should be done with the parked channel if no one picks it up before it times out.</synopsis>
  118. <description><para>Valid Options:</para>
  119. <enumlist>
  120. <enum name="yes">
  121. <para>Automatically have the parked channel dial the device that parked the call with dial
  122. timeout set by the <literal>parkingtime</literal> option. When the call times out an extension
  123. to dial the PARKER will automatically be created in the <literal>park-dial</literal> context with
  124. an extension of the flattened parker device name. If the call is not answered, the parked channel
  125. that is timing out will continue in the dial plan at that point if there are more priorities in
  126. the extension (which won't be the case unless the dialplan deliberately includes such priorities
  127. in the <literal>park-dial</literal> context through pattern matching or deliberately written
  128. flattened peer extensions).</para>
  129. </enum>
  130. <enum name="no">
  131. <para>Place the call into the PBX at <literal>comebackcontext</literal> instead. The extension will
  132. still be set as the flattened peer name. If an extension the flattened peer name isn't available
  133. then it will fall back to the <literal>s</literal> extension. If that also is unavailable it will
  134. attempt to fall back to <literal>s@default</literal>. The normal dial extension will still be
  135. created in the <literal>park-dial</literal> context with the extension also being the flattened
  136. peer name.</para>
  137. </enum>
  138. </enumlist>
  139. <note><para>Flattened Peer Names - Extensions can not include slash characters since those are used for pattern
  140. matching. When a peer name is flattened, slashes become underscores. For example if the parker of a call
  141. is called <literal>SIP/0004F2040001</literal> then flattened peer name and therefor the extensions created
  142. and used on timeouts will be <literal>SIP_0004F204001</literal>.</para></note>
  143. <note><para>When parking times out and the channel returns to the dial plan, the following variables are set:
  144. </para></note>
  145. <variablelist>
  146. <variable name="PARKING_SPACE">
  147. <para>extension that the call was parked in prior to timing out.</para>
  148. </variable>
  149. <variable name="PARKINGSLOT">
  150. <para>Deprecated. Use <variable>PARKING_SPACE</variable> instead.</para>
  151. </variable>
  152. <variable name="PARKEDLOT">
  153. <para>name of the lot that the call was parked in prior to timing out.</para>
  154. </variable>
  155. <variable name="PARKER">
  156. <para>The device that parked the call</para>
  157. </variable>
  158. <variable name="PARKER_FLAT">
  159. <para>The flat version of <variable>PARKER</variable></para>
  160. </variable>
  161. </variablelist>
  162. </description>
  163. </configOption>
  164. <configOption name="comebackdialtime" default="30">
  165. <synopsis>Timeout for the Dial extension created to call back the parker when a parked call times out.</synopsis>
  166. </configOption>
  167. <configOption name="comebackcontext" default="parkedcallstimeout">
  168. <synopsis>Context where parked calls will enter the PBX on timeout when comebacktoorigin=no</synopsis>
  169. <description><para>The extension the call enters will prioritize the flattened peer name in this context.
  170. If the flattened peer name extension is unavailable, then the 's' extension in this context will be
  171. used. If that also is unavailable, the 's' extension in the 'default' context will be used.</para>
  172. </description>
  173. </configOption>
  174. <configOption name="courtesytone">
  175. <synopsis>If the name of a sound file is provided, use this as the courtesy tone</synopsis>
  176. <description><para>By default, this tone is only played to the caller of a parked call. Who receives the tone
  177. can be changed using the <literal>parkedplay</literal> option.</para>
  178. </description>
  179. </configOption>
  180. <configOption name="parkedplay" default="caller">
  181. <synopsis>Who we should play the courtesytone to on the pickup of a parked call from this lot</synopsis>
  182. <description>
  183. <enumlist>
  184. <enum name="no"><para>Apply to neither side.</para></enum>
  185. <enum name="caller"><para>Apply only to the call connecting with the call coming out of the parking lot.</para></enum>
  186. <enum name="callee"><para>Apply only to the call coming out of the parking lot.</para></enum>
  187. <enum name="both"><para>Apply to both sides.</para></enum>
  188. </enumlist>
  189. <note><para>If courtesy tone is not specified then this option will be ignored.</para></note>
  190. </description>
  191. </configOption>
  192. <configOption name="parkedcalltransfers" default="no">
  193. <synopsis>Who to apply the DTMF transfer features to when parked calls are picked up or timeout.</synopsis>
  194. <description>
  195. <xi:include xpointer="xpointer(/docs/configInfo[@name='res_parking']/configFile[@name='res_parking.conf']/configObject[@name='parking_lot']/configOption[@name='parkedplay']/description/enumlist)" />
  196. </description>
  197. </configOption>
  198. <configOption name="parkedcallreparking" default="no">
  199. <synopsis>Who to apply the DTMF parking feature to when parked calls are picked up or timeout.</synopsis>
  200. <description>
  201. <xi:include xpointer="xpointer(/docs/configInfo[@name='res_parking']/configFile[@name='res_parking.conf']/configObject[@name='parking_lot']/configOption[@name='parkedplay']/description/enumlist)" />
  202. </description>
  203. </configOption>
  204. <configOption name="parkedcallhangup" default="no">
  205. <synopsis>Who to apply the DTMF hangup feature to when parked calls are picked up or timeout.</synopsis>
  206. <description>
  207. <xi:include xpointer="xpointer(/docs/configInfo[@name='res_parking']/configFile[@name='res_parking.conf']/configObject[@name='parking_lot']/configOption[@name='parkedplay']/description/enumlist)" />
  208. </description>
  209. </configOption>
  210. <configOption name="parkedcallrecording" default="no">
  211. <synopsis>Who to apply the DTMF MixMonitor recording feature to when parked calls are picked up or timeout.</synopsis>
  212. <description>
  213. <xi:include xpointer="xpointer(/docs/configInfo[@name='res_parking']/configFile[@name='res_parking.conf']/configObject[@name='parking_lot']/configOption[@name='parkedplay']/description/enumlist)" />
  214. </description>
  215. </configOption>
  216. <configOption name="findslot" default="first">
  217. <synopsis>Rule to use when trying to figure out which parking space a call should be parked with.</synopsis>
  218. <description>
  219. <enumlist>
  220. <enum name="first"><para>Always try to place in the lowest available space in the parking lot</para></enum>
  221. <enum name="next"><para>Track the last parking space used and always attempt to use the one immediately after.
  222. </para></enum>
  223. </enumlist>
  224. </description>
  225. </configOption>
  226. </configObject>
  227. </configFile>
  228. </configInfo>
  229. ***/
  230. #include "asterisk.h"
  231. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  232. #include "parking/res_parking.h"
  233. #include "asterisk/config.h"
  234. #include "asterisk/config_options.h"
  235. #include "asterisk/utils.h"
  236. #include "asterisk/module.h"
  237. #include "asterisk/cli.h"
  238. #include "asterisk/astobj2.h"
  239. #include "asterisk/features.h"
  240. #include "asterisk/manager.h"
  241. #include "asterisk/pbx.h"
  242. static int parking_lot_sort_fn(const void *obj_left, const void *obj_right, int flags)
  243. {
  244. const struct parking_lot *left = obj_left;
  245. const struct parking_lot *right = obj_right;
  246. const char *right_key = obj_right;
  247. int cmp;
  248. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  249. default:
  250. case OBJ_POINTER:
  251. right_key = right->name;
  252. /* Fall through */
  253. case OBJ_KEY:
  254. cmp = strcmp(left->name, right_key);
  255. break;
  256. case OBJ_PARTIAL_KEY:
  257. cmp = strncmp(left->name, right_key, strlen(right_key));
  258. }
  259. return cmp;
  260. }
  261. /*! All parking lots that are currently alive in some fashion can be obtained from here */
  262. static struct ao2_container *parking_lot_container;
  263. static void *parking_config_alloc(void);
  264. static void *parking_lot_cfg_alloc(const char *cat);
  265. static void *named_item_find(struct ao2_container *container, const char *name); /* XXX This is really just a generic string find. Move to astobj2.c? */
  266. static int config_parking_preapply(void);
  267. static void link_configured_disable_marked_lots(void);
  268. struct parking_global_config {
  269. int parkeddynamic;
  270. };
  271. struct parking_config {
  272. struct parking_global_config *global;
  273. struct ao2_container *parking_lots;
  274. };
  275. static struct aco_type global_option = {
  276. .type = ACO_GLOBAL,
  277. .name = "globals",
  278. .item_offset = offsetof(struct parking_config, global),
  279. .category_match = ACO_WHITELIST_EXACT,
  280. .category = "general",
  281. };
  282. struct aco_type *global_options[] = ACO_TYPES(&global_option);
  283. static struct aco_type parking_lot_type = {
  284. .type = ACO_ITEM,
  285. .name = "parking_lot",
  286. .category_match = ACO_BLACKLIST_EXACT,
  287. .category = "general",
  288. .item_alloc = parking_lot_cfg_alloc,
  289. .item_find = named_item_find,
  290. .item_offset = offsetof(struct parking_config, parking_lots),
  291. };
  292. struct aco_type *parking_lot_types[] = ACO_TYPES(&parking_lot_type);
  293. struct aco_file parking_lot_conf = {
  294. .filename = "res_parking.conf",
  295. .types = ACO_TYPES(&global_option, &parking_lot_type),
  296. };
  297. static AO2_GLOBAL_OBJ_STATIC(globals);
  298. CONFIG_INFO_STANDARD(cfg_info, globals, parking_config_alloc,
  299. .files = ACO_FILES(&parking_lot_conf),
  300. .pre_apply_config = config_parking_preapply,
  301. .post_apply_config = link_configured_disable_marked_lots,
  302. );
  303. static int parking_lot_cfg_hash_fn(const void *obj, const int flags)
  304. {
  305. const struct parking_lot_cfg *entry;
  306. const char *key;
  307. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  308. case OBJ_KEY:
  309. key = obj;
  310. return ast_str_hash(key);
  311. case OBJ_PARTIAL_KEY:
  312. ast_assert(0);
  313. return 0;
  314. default:
  315. entry = obj;
  316. return ast_str_hash(entry->name);
  317. }
  318. }
  319. static int parking_lot_cfg_cmp_fn(void *obj, void *arg, const int flags)
  320. {
  321. struct parking_lot_cfg *entry1 = obj;
  322. char *key;
  323. size_t key_size;
  324. struct parking_lot_cfg *entry2;
  325. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  326. case OBJ_KEY:
  327. key = arg;
  328. return (!strcmp(entry1->name, key)) ? CMP_MATCH : 0;
  329. case OBJ_PARTIAL_KEY:
  330. key = arg;
  331. key_size = strlen(key);
  332. return (!strncmp(entry1->name, key, key_size)) ? CMP_MATCH : 0;
  333. case OBJ_POINTER:
  334. entry2 = arg;
  335. return (!strcmp(entry1->name, entry2->name)) ? CMP_MATCH : 0;
  336. default:
  337. return CMP_STOP;
  338. }
  339. }
  340. /*! \brief destructor for parking_config */
  341. static void parking_config_destructor(void *obj)
  342. {
  343. struct parking_config *cfg = obj;
  344. ao2_cleanup(cfg->parking_lots);
  345. ao2_cleanup(cfg->global);
  346. }
  347. /*! \brief destructor for parking_global_config */
  348. static void parking_global_config_destructor(void *obj)
  349. {
  350. /* For now, do nothing. */
  351. }
  352. /*! \brief allocator callback for parking_config. Notice it returns void * since it is only used by the backend config code */
  353. static void *parking_config_alloc(void)
  354. {
  355. RAII_VAR(struct parking_config *, cfg, NULL, ao2_cleanup);
  356. if (!(cfg = ao2_alloc(sizeof(*cfg), parking_config_destructor))) {
  357. return NULL;
  358. }
  359. cfg->parking_lots = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 37,
  360. parking_lot_cfg_hash_fn, NULL, parking_lot_cfg_cmp_fn);
  361. if (!cfg->parking_lots) {
  362. return NULL;
  363. }
  364. if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), parking_global_config_destructor))) {
  365. return NULL;
  366. }
  367. /* Bump the ref count since RAII_VAR is going to eat one */
  368. ao2_ref(cfg, +1);
  369. return cfg;
  370. }
  371. int parking_lot_remove_if_unused(struct parking_lot *lot)
  372. {
  373. if (lot->mode != PARKINGLOT_DISABLED) {
  374. return -1;
  375. }
  376. if (!ao2_container_count(lot->parked_users)) {
  377. ao2_unlink(parking_lot_container, lot);
  378. return 0;
  379. }
  380. return -1;
  381. }
  382. static void parking_lot_disable(struct parking_lot *lot)
  383. {
  384. /* If a dynamic lot wasn't removed, we need to restore it to full functionality afterwards. */
  385. int was_dynamic = (lot->mode == PARKINGLOT_DYNAMIC);
  386. lot->mode = PARKINGLOT_DISABLED;
  387. if (parking_lot_remove_if_unused(lot) && was_dynamic) {
  388. lot->mode = PARKINGLOT_DYNAMIC;
  389. lot->disable_mark = 0;
  390. }
  391. }
  392. /*! \brief Destroy a parking lot cfg object */
  393. static void parking_lot_cfg_destructor(void *obj)
  394. {
  395. struct parking_lot_cfg *lot_cfg = obj;
  396. parking_lot_cfg_remove_extensions(lot_cfg);
  397. ast_string_field_free_memory(lot_cfg);
  398. }
  399. /* The arg just needs to have the parking space with it */
  400. static int parked_user_cmp_fn(void *obj, void *arg, int flags)
  401. {
  402. int *search_space = arg;
  403. struct parked_user *user = obj;
  404. int object_space = user->parking_space;
  405. if (*search_space == object_space) {
  406. return CMP_MATCH;
  407. }
  408. return 0;
  409. }
  410. static int parked_user_sort_fn(const void *obj_left, const void *obj_right, int flags)
  411. {
  412. const struct parked_user *left = obj_left;
  413. const struct parked_user *right = obj_right;
  414. return left->parking_space - right->parking_space;
  415. }
  416. /*!
  417. * \brief create a parking lot structure
  418. * \param cat name given to the parking lot
  419. * \retval NULL failure
  420. * \retval non-NULL successfully allocated parking lot
  421. */
  422. static void *parking_lot_cfg_alloc(const char *cat)
  423. {
  424. struct parking_lot_cfg *lot_cfg;
  425. lot_cfg = ao2_alloc(sizeof(*lot_cfg), parking_lot_cfg_destructor);
  426. if (!lot_cfg) {
  427. return NULL;
  428. }
  429. if (ast_string_field_init(lot_cfg, 32)) {
  430. ao2_cleanup(lot_cfg);
  431. return NULL;
  432. }
  433. ast_string_field_set(lot_cfg, name, cat);
  434. return lot_cfg;
  435. }
  436. #if defined(TEST_FRAMEWORK)
  437. struct parking_lot_cfg *parking_lot_cfg_create(const char *cat)
  438. {
  439. return parking_lot_cfg_alloc(cat);
  440. }
  441. #endif
  442. /*!
  443. * XXX This is actually incredibly generic and might be better placed in something like astobj2 if there isn't already an equivalent
  444. * \brief find an item in a container by its name
  445. *
  446. * \param container ao2container where we want the item from
  447. * \param key name of the item wanted to be found
  448. *
  449. * \retval pointer to the parking lot if available. NULL if not found.
  450. */
  451. static void *named_item_find(struct ao2_container *container, const char *name)
  452. {
  453. return ao2_find(container, name, OBJ_KEY);
  454. }
  455. /*!
  456. * \brief Custom field handler for parking positions
  457. */
  458. static int option_handler_parkpos(const struct aco_option *opt, struct ast_variable *var, void *obj)
  459. {
  460. struct parking_lot_cfg *lot_cfg = obj;
  461. int low;
  462. int high;
  463. if (sscanf(var->value, "%30d-%30d", &low, &high) != 2) {
  464. ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers\n");
  465. } else if (high < low || low <= 0 || high <= 0) {
  466. ast_log(LOG_WARNING, "Format for parking positions is a-b, where a <= b\n");
  467. } else {
  468. lot_cfg->parking_start = low;
  469. lot_cfg->parking_stop = high;
  470. return 0;
  471. }
  472. return -1;
  473. }
  474. /*!
  475. * \brief Custom field handler for the findslot option
  476. */
  477. static int option_handler_findslot(const struct aco_option *opt, struct ast_variable *var, void *obj)
  478. {
  479. struct parking_lot_cfg *lot_cfg = obj;
  480. if (!strcmp(var->value, "first")) {
  481. lot_cfg->parkfindnext = 0;
  482. } else if (!strcmp(var->value, "next")) {
  483. lot_cfg->parkfindnext = 1;
  484. } else {
  485. ast_log(LOG_WARNING, "value '%s' is not valid for findslot option.\n", var->value);
  486. return -1;
  487. }
  488. return 0;
  489. }
  490. /*!
  491. * \brief Maps string values for option_handler_parkedfeature to their ENUM values
  492. */
  493. static int parking_feature_flag_cfg(int *param, const char *var)
  494. {
  495. if (ast_false(var)) {
  496. *param = 0;
  497. } else if (!strcasecmp(var, "both")) {
  498. *param = AST_FEATURE_FLAG_BYBOTH;
  499. } else if (!strcasecmp(var, "caller")) {
  500. *param = AST_FEATURE_FLAG_BYCALLER;
  501. } else if (!strcasecmp(var, "callee")) {
  502. *param = AST_FEATURE_FLAG_BYCALLEE;
  503. } else {
  504. return -1;
  505. }
  506. return 0;
  507. }
  508. /*!
  509. * \brief Custom field handler for feature mapping on parked call pickup options
  510. */
  511. static int option_handler_parkedfeature(const struct aco_option *opt, struct ast_variable *var, void *obj)
  512. {
  513. struct parking_lot_cfg *cfg = obj;
  514. enum parked_call_feature_options option = aco_option_get_flags(opt);
  515. int *parameter = NULL;
  516. switch (option) {
  517. case OPT_PARKEDPLAY:
  518. parameter = &cfg->parkedplay;
  519. break;
  520. case OPT_PARKEDTRANSFERS:
  521. parameter = &cfg->parkedcalltransfers;
  522. break;
  523. case OPT_PARKEDREPARKING:
  524. parameter = &cfg->parkedcallreparking;
  525. break;
  526. case OPT_PARKEDHANGUP:
  527. parameter = &cfg->parkedcallhangup;
  528. break;
  529. case OPT_PARKEDRECORDING:
  530. parameter = &cfg->parkedcallrecording;
  531. break;
  532. }
  533. ast_assert(parameter != NULL);
  534. if (!parameter || parking_feature_flag_cfg(parameter, var->value)) {
  535. return -1;
  536. }
  537. return 0;
  538. }
  539. struct ao2_container *get_parking_lot_container(void)
  540. {
  541. return parking_lot_container;
  542. }
  543. struct parking_lot *parking_lot_find_by_name(const char *lot_name)
  544. {
  545. struct parking_lot *lot = named_item_find(parking_lot_container, lot_name);
  546. return lot;
  547. }
  548. const char *find_channel_parking_lot_name(struct ast_channel *chan)
  549. {
  550. const char *name;
  551. /* The channel variable overrides everything */
  552. name = pbx_builtin_getvar_helper(chan, "PARKINGLOT");
  553. if (ast_strlen_zero(name)) {
  554. /* Try the channel's parking lot. */
  555. name = ast_channel_parkinglot(chan);
  556. if (ast_strlen_zero(name)) {
  557. /* Fall back to the default parking lot. */
  558. name = DEFAULT_PARKING_LOT;
  559. }
  560. }
  561. return name;
  562. }
  563. static void parking_lot_destructor(void *obj)
  564. {
  565. struct parking_lot *lot = obj;
  566. if (lot->parking_bridge) {
  567. ast_bridge_destroy(lot->parking_bridge, 0);
  568. }
  569. ao2_cleanup(lot->parked_users);
  570. ao2_cleanup(lot->cfg);
  571. ast_string_field_free_memory(lot);
  572. }
  573. static struct parking_lot *alloc_new_parking_lot(struct parking_lot_cfg *lot_cfg)
  574. {
  575. struct parking_lot *lot;
  576. if (!(lot = ao2_alloc(sizeof(*lot), parking_lot_destructor))) {
  577. return NULL;
  578. }
  579. if (ast_string_field_init(lot, 32)) {
  580. return NULL;
  581. }
  582. /* Create parked user ordered list */
  583. lot->parked_users = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK,
  584. AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
  585. parked_user_sort_fn,
  586. parked_user_cmp_fn);
  587. if (!lot->parked_users) {
  588. ao2_cleanup(lot);
  589. return NULL;
  590. }
  591. ast_string_field_set(lot, name, lot_cfg->name);
  592. return lot;
  593. }
  594. void parking_lot_cfg_remove_extensions(struct parking_lot_cfg *lot_cfg)
  595. {
  596. if (!ast_strlen_zero(lot_cfg->registrar)) {
  597. /* Although the function is called ast_context_destroy, the use of this funtion is
  598. * intended only to remove extensions, hints, etc registered by the parking lot's registrar.
  599. * It won't actually destroy the context unless that context is empty afterwards and it is
  600. * unreferenced.
  601. */
  602. ast_context_destroy(NULL, lot_cfg->registrar);
  603. }
  604. /* If we come back for a second pass, someone else has this registrar now. */
  605. ast_string_field_set(lot_cfg, registrar, "");
  606. }
  607. static void remove_all_configured_parking_lot_extensions(void)
  608. {
  609. RAII_VAR(struct parking_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  610. struct parking_lot_cfg *lot_cfg;
  611. struct ao2_iterator iter;
  612. if (!cfg) {
  613. return;
  614. }
  615. for (iter = ao2_iterator_init(cfg->parking_lots, 0); (lot_cfg = ao2_iterator_next(&iter)); ao2_ref(lot_cfg, -1)) {
  616. parking_lot_cfg_remove_extensions(lot_cfg);
  617. }
  618. ast_context_destroy(NULL, BASE_REGISTRAR);
  619. ao2_iterator_destroy(&iter);
  620. }
  621. /*!
  622. * \internal
  623. * \since 12
  624. * \brief Create an extension using ast_add_extension2_nolock. This function automatically allocates a duplicate
  625. * of the data string so that whatever calls it doesn't have to deal with freeing it if the ast_add_extension2_nolock
  626. * fails.
  627. *
  628. * \param context a write locked ast_context. Make certain it is write locked prior to calling this function
  629. * \param replace whether the extension should replace existing extensions
  630. * \param extension name of the extension desired
  631. * \param priority priority of the extension we are registering
  632. * \param application name of the application being used for the extension
  633. * \param data application arguments
  634. * \param registrar name of the registrar you should use for the extension.
  635. * Make sure this string doesn't go anywhere while there are still extensions using it.
  636. */
  637. static int parking_add_extension(struct ast_context *context, int replace, const char *extension,
  638. int priority, const char *application, const char *data, const char *registrar)
  639. {
  640. char *data_duplicate = ast_strdup(data);
  641. if (!data_duplicate) {
  642. return -1;
  643. }
  644. if (ast_add_extension2_nolock(context, replace, extension, priority, NULL, NULL,
  645. application, data_duplicate, ast_free_ptr, registrar)) {
  646. ast_free(data_duplicate);
  647. return -1;
  648. }
  649. return 0;
  650. }
  651. static int extension_is_compatible(struct parking_lot_cfg *lot_cfg, const char *app_type, struct ast_exten *extension)
  652. {
  653. const char *extension_registrar = ast_get_extension_registrar(extension);
  654. const char *extension_context = ast_get_context_name(ast_get_extension_context(extension));
  655. const char *extension_name = ast_get_extension_name(extension);
  656. const char *extension_application = ast_get_extension_app(extension);
  657. ast_assert(extension_registrar && extension_context && extension_name && extension_application);
  658. if (strcmp(extension_registrar, BASE_REGISTRAR)) {
  659. ast_log(LOG_ERROR, "Parking lot '%s' -- Needs an extension '%s@%s', but that extension is already owned by %s.\n",
  660. lot_cfg->name, extension_name, extension_context, extension_registrar);
  661. return 0;
  662. }
  663. if (strcmp(extension_application, app_type)) {
  664. ast_log(LOG_ERROR, "Parking lot '%s' -- Needs an extension '%s@%s' with a non-exclusive %s application, "
  665. "but a/an %s application is already registered to that extension by %s.\n",
  666. lot_cfg->name, extension_name, extension_context, app_type,
  667. extension_application, BASE_REGISTRAR);
  668. return 0;
  669. }
  670. ast_debug(3, "Parking lot '%s' -- extension '%s@%s' with application %s is compatible.\n",
  671. lot_cfg->name, extension_name, extension_context, app_type);
  672. return 1;
  673. }
  674. int parking_lot_cfg_create_extensions(struct parking_lot_cfg *lot_cfg)
  675. {
  676. int parkingspace;
  677. struct ast_exten *existing_exten;
  678. struct ast_context *lot_context;
  679. struct pbx_find_info find_info = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  680. const char *parkext_registrar_pointer; /* Used for park extension */
  681. const char *parkedcall_registrar_pointer; /* Used for parkedcall extensions/hints */
  682. if (ast_strlen_zero(lot_cfg->parkext)) {
  683. return 0;
  684. }
  685. ast_string_field_build(lot_cfg, registrar, "%s/%s", BASE_REGISTRAR, lot_cfg->name);
  686. parkedcall_registrar_pointer = lot_cfg->registrar;
  687. if (lot_cfg->parkext_exclusive) {
  688. parkext_registrar_pointer = lot_cfg->registrar;
  689. } else {
  690. parkext_registrar_pointer = BASE_REGISTRAR;
  691. }
  692. /* We need the contexts list locked to safely be able to both read and lock the specific context within */
  693. ast_wrlock_contexts();
  694. if (!(lot_context = ast_context_find_or_create(NULL, NULL, lot_cfg->parking_con, parkext_registrar_pointer))) {
  695. ast_log(LOG_ERROR, "Parking lot '%s' -- Needs a context '%s' which does not exist and Asterisk was unable to create\n",
  696. lot_cfg->name, lot_cfg->parking_con);
  697. ast_unlock_contexts();
  698. return -1;
  699. }
  700. /* Once we know what context we will be modifying, we need to write lock it because we will be reading extensions
  701. * and we don't want something else to destroy them while we are looking at them.
  702. */
  703. ast_wrlock_context(lot_context);
  704. ast_unlock_contexts();
  705. /* Handle generation/confirmation for the Park extension */
  706. if ((existing_exten = pbx_find_extension(NULL, NULL, &find_info, lot_cfg->parking_con, lot_cfg->parkext, 1, NULL, NULL, E_MATCH))) {
  707. if (lot_cfg->parkext_exclusive || !extension_is_compatible(lot_cfg, PARK_APPLICATION, existing_exten)) {
  708. ast_unlock_context(lot_context);
  709. return -1;
  710. }
  711. } else if (parking_add_extension(lot_context, 0, lot_cfg->parkext, 1, PARK_APPLICATION,
  712. lot_cfg->parkext_exclusive ? lot_cfg->name : "", parkext_registrar_pointer)) {
  713. ast_log(LOG_ERROR, "Parking lot '%s' -- Failed to add %s extension '%s@%s' to the PBX.\n",
  714. lot_cfg->name, PARK_APPLICATION, lot_cfg->parkext, lot_cfg->parking_con);
  715. ast_unlock_context(lot_context);
  716. return -1;
  717. }
  718. /* Handle generation/confirmation for the ParkedCall extensions and hints */
  719. for (parkingspace = lot_cfg->parking_start; parkingspace <= lot_cfg->parking_stop; parkingspace++) {
  720. char space[AST_MAX_EXTENSION];
  721. RAII_VAR(struct ast_str *, arguments_string, NULL, ast_free);
  722. find_info.stacklen = 0; /* reset for pbx_find_exten */
  723. snprintf(space, sizeof(space), "%d", parkingspace);
  724. /* Unlike the Park extensions, ParkedCall extensions and their hints may never be shared for any reason. */
  725. if ((existing_exten = pbx_find_extension(NULL, NULL, &find_info, lot_cfg->parking_con, space, 1, NULL, NULL, E_MATCH))) {
  726. ast_unlock_context(lot_context);
  727. return -1;
  728. }
  729. arguments_string = ast_str_create(32);
  730. if (!arguments_string) {
  731. ast_unlock_context(lot_context);
  732. return -1;
  733. }
  734. ast_str_set(&arguments_string, 0, "%s,%s", lot_cfg->name, space);
  735. if (parking_add_extension(lot_context, 0, space, 1, PARKED_CALL_APPLICATION,
  736. ast_str_buffer(arguments_string), parkedcall_registrar_pointer)) {
  737. ast_log(LOG_ERROR, "Parking lot '%s' -- Failed to add %s extension '%s@%s' to the PBX.\n",
  738. lot_cfg->name, PARKED_CALL_APPLICATION, space, lot_cfg->parking_con);
  739. ast_unlock_context(lot_context);
  740. return -1;
  741. }
  742. find_info.stacklen = 0; /* reset for pbx_find_exten */
  743. if (lot_cfg->parkaddhints) {
  744. char hint_device[AST_MAX_EXTENSION];
  745. snprintf(hint_device, sizeof(hint_device), "park:%s@%s", space, lot_cfg->parking_con);
  746. if ((existing_exten = pbx_find_extension(NULL, NULL, &find_info, lot_cfg->parking_con, space, PRIORITY_HINT, NULL, NULL, E_MATCH))) {
  747. ast_log(LOG_ERROR, "Parking lot '%s' -- Needs to add a hint '%s' at '%s@%s' but one already exists owned by %s\n",
  748. lot_cfg->name, hint_device, space, lot_cfg->parking_con, ast_get_extension_registrar(existing_exten));
  749. ast_unlock_context(lot_context);
  750. return -1;
  751. }
  752. if (parking_add_extension(lot_context, 0, space, PRIORITY_HINT, hint_device, "", parkedcall_registrar_pointer)) {
  753. ast_log(LOG_ERROR, "Parking lot '%s' -- Failed to add hint '%s@%s' to the PBX.\n",
  754. lot_cfg->name, space, lot_cfg->parking_con);
  755. ast_unlock_context(lot_context);
  756. return -1;
  757. }
  758. }
  759. }
  760. ast_unlock_context(lot_context);
  761. return 0;
  762. }
  763. struct parking_lot *parking_lot_build_or_update(struct parking_lot_cfg *lot_cfg, int dynamic)
  764. {
  765. struct parking_lot *lot;
  766. struct parking_lot_cfg *replaced_cfg = NULL;
  767. int found = 0;
  768. /* Start by trying to find it. If that works we can skip the rest. */
  769. lot = named_item_find(parking_lot_container, lot_cfg->name);
  770. if (!lot) {
  771. lot = alloc_new_parking_lot(lot_cfg);
  772. /* If we still don't have a lot, we failed to alloc one. */
  773. if (!lot) {
  774. return NULL;
  775. }
  776. } else {
  777. found = 1;
  778. if (dynamic) {
  779. ast_log(LOG_ERROR, "Tried to create dynamic parking lot with name '%s' but a lot with that name already exists.\n", lot_cfg->name);
  780. ao2_cleanup(lot);
  781. return NULL;
  782. }
  783. }
  784. /* Set the configuration reference. Unref the one currently in the lot if it's there. */
  785. if (lot->cfg) {
  786. replaced_cfg = lot->cfg;
  787. }
  788. ao2_ref(lot_cfg, +1);
  789. lot->cfg = lot_cfg;
  790. ao2_cleanup(replaced_cfg);
  791. /* Set the operating mode to normal since the parking lot has a configuration. */
  792. lot->disable_mark = 0;
  793. lot->mode = dynamic ? PARKINGLOT_DYNAMIC : PARKINGLOT_NORMAL;
  794. if (!found) {
  795. /* Link after configuration is set since a lot without configuration will cause all kinds of trouble. */
  796. ao2_link(parking_lot_container, lot);
  797. };
  798. return lot;
  799. }
  800. static void generate_or_link_lots_to_configs(void)
  801. {
  802. RAII_VAR(struct parking_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  803. struct parking_lot_cfg *lot_cfg;
  804. struct ao2_iterator iter;
  805. iter = ao2_iterator_init(cfg->parking_lots, 0);
  806. for (; (lot_cfg = ao2_iterator_next(&iter)); ao2_ref(lot_cfg, -1)) {
  807. ao2_cleanup(parking_lot_build_or_update(lot_cfg, 0));
  808. }
  809. ao2_iterator_destroy(&iter);
  810. }
  811. int parking_dynamic_lots_enabled(void)
  812. {
  813. RAII_VAR(struct parking_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  814. if (!cfg) {
  815. return 0;
  816. }
  817. return cfg->global->parkeddynamic;
  818. }
  819. static struct parking_lot_cfg *clone_parkinglot_cfg(struct parking_lot_cfg *source, const char *name)
  820. {
  821. struct parking_lot_cfg *cfg = parking_lot_cfg_alloc(name);
  822. if (!cfg) {
  823. return NULL;
  824. }
  825. ast_string_fields_copy(cfg, source);
  826. /* Needs to be reset after being copied */
  827. ast_string_field_set(cfg, name, name);
  828. /* Stuff that should be cloned that isn't hit by string field copy */
  829. cfg->parking_start = source->parking_start;
  830. cfg->parking_stop = source->parking_stop;
  831. cfg->parkingtime = source->parkingtime;
  832. cfg->comebackdialtime = source->comebackdialtime;
  833. cfg->parkfindnext = source->parkfindnext;
  834. cfg->parkext_exclusive = source->parkext_exclusive;
  835. cfg->parkaddhints = source->parkaddhints;
  836. cfg->comebacktoorigin = source->comebacktoorigin;
  837. cfg->parkedplay = source->parkedplay;
  838. cfg->parkedcalltransfers = source->parkedcalltransfers;
  839. cfg->parkedcallreparking = source->parkedcallreparking;
  840. cfg->parkedcallhangup = source->parkedcallhangup;
  841. cfg->parkedcallrecording = source->parkedcallrecording;
  842. return cfg;
  843. }
  844. static struct parking_lot *create_dynamic_lot_full(const char *name, struct ast_channel *chan, int forced)
  845. {
  846. RAII_VAR(struct parking_lot_cfg *, cfg, NULL, ao2_cleanup);
  847. RAII_VAR(struct parking_lot *, template_lot, NULL, ao2_cleanup);
  848. struct parking_lot *lot;
  849. const char *dyn_context;
  850. const char *dyn_exten;
  851. const char *dyn_range;
  852. const char *template_name;
  853. const char *chan_template_name;
  854. int dyn_start;
  855. int dyn_end;
  856. if (!forced && !parking_dynamic_lots_enabled()) {
  857. return NULL;
  858. }
  859. ast_channel_lock(chan);
  860. chan_template_name = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
  861. dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
  862. dyn_exten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNEXTEN"), ""));
  863. dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
  864. ast_channel_unlock(chan);
  865. template_name = S_OR(chan_template_name, DEFAULT_PARKING_LOT);
  866. template_lot = parking_lot_find_by_name(template_name);
  867. if (!template_lot) {
  868. ast_log(LOG_ERROR, "Lot %s does not exist. Can not use it as a dynamic parking lot template.\n",
  869. template_name);
  870. return NULL;
  871. }
  872. cfg = clone_parkinglot_cfg(template_lot->cfg, name);
  873. if (!cfg) {
  874. ast_log(LOG_ERROR, "Failed to allocate dynamic parking lot configuration.\n");
  875. return NULL;
  876. }
  877. if (!ast_strlen_zero(dyn_exten)) {
  878. ast_string_field_set(cfg, parkext, dyn_exten);
  879. }
  880. if (!ast_strlen_zero(dyn_context)) {
  881. ast_string_field_set(cfg, parking_con, dyn_context);
  882. }
  883. if (!ast_strlen_zero(dyn_range)) {
  884. if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
  885. ast_log(LOG_ERROR,
  886. "Invalid parking range %s specified in PARKINGDYNPOS: could not parse minimum/maximum parking space range\n", dyn_range);
  887. return NULL;
  888. }
  889. if (dyn_end < dyn_start || dyn_start < 0) {
  890. ast_log(LOG_ERROR,
  891. "Invalid parking range %s specified for PARKINGDYNPOS: end parking space must be greater than starting parking space.\n", dyn_range);
  892. return NULL;
  893. }
  894. cfg->parking_start = dyn_start;
  895. cfg->parking_stop = dyn_end;
  896. }
  897. if (parking_lot_cfg_create_extensions(cfg)) {
  898. ast_log(LOG_ERROR, "Extensions for dynamic parking lot '%s' could not be registered. Dynamic lot creation failed.\n", name);
  899. return NULL;
  900. }
  901. ao2_lock(parking_lot_container);
  902. if ((lot = parking_lot_find_by_name(name))) {
  903. ao2_unlock(parking_lot_container);
  904. ast_log(LOG_ERROR, "Started creating dynamic parking lot '%s', but a parking lot with that name already exists.\n", name);
  905. ao2_ref(lot, -1);
  906. return NULL;
  907. }
  908. lot = parking_lot_build_or_update(cfg, 1);
  909. ao2_unlock(parking_lot_container);
  910. if (!lot) {
  911. ast_log(LOG_NOTICE, "Failed to build dynamic parking lot '%s'\n", name);
  912. }
  913. return lot;
  914. }
  915. struct parking_lot *parking_create_dynamic_lot(const char *name, struct ast_channel *chan){
  916. return create_dynamic_lot_full(name, chan, 0);
  917. }
  918. #if defined(TEST_FRAMEWORK)
  919. struct parking_lot *parking_create_dynamic_lot_forced(const char *name, struct ast_channel *chan) {
  920. return create_dynamic_lot_full(name, chan, 1);
  921. }
  922. #endif
  923. /* Preapply */
  924. static int verify_default_parking_lot(void)
  925. {
  926. struct parking_config *cfg = aco_pending_config(&cfg_info);
  927. RAII_VAR(struct parking_lot_cfg *, lot_cfg, NULL, ao2_cleanup);
  928. if (!cfg) {
  929. return 0;
  930. }
  931. lot_cfg = ao2_find(cfg->parking_lots, DEFAULT_PARKING_LOT, OBJ_KEY);
  932. if (!lot_cfg) {
  933. lot_cfg = parking_lot_cfg_alloc(DEFAULT_PARKING_LOT);
  934. if (!lot_cfg) {
  935. return -1;
  936. }
  937. ast_log(AST_LOG_NOTICE, "Adding %s profile to res_parking\n", DEFAULT_PARKING_LOT);
  938. aco_set_defaults(&parking_lot_type, DEFAULT_PARKING_LOT, lot_cfg);
  939. ast_string_field_set(lot_cfg, parkext, DEFAULT_PARKING_EXTEN);
  940. ao2_link(cfg->parking_lots, lot_cfg);
  941. }
  942. return 0;
  943. }
  944. static void remove_pending_parking_lot_extensions(struct parking_config *cfg_pending)
  945. {
  946. struct parking_lot_cfg *lot_cfg;
  947. struct ao2_iterator iter;
  948. for (iter = ao2_iterator_init(cfg_pending->parking_lots, 0); (lot_cfg = ao2_iterator_next(&iter)); ao2_ref(lot_cfg, -1)) {
  949. parking_lot_cfg_remove_extensions(lot_cfg);
  950. }
  951. ao2_iterator_destroy(&iter);
  952. ast_context_destroy(NULL, BASE_REGISTRAR);
  953. }
  954. static int configure_parking_extensions(void)
  955. {
  956. struct parking_config *cfg = aco_pending_config(&cfg_info);
  957. struct ao2_iterator iter;
  958. RAII_VAR(struct parking_lot_cfg *, lot_cfg, NULL, ao2_cleanup);
  959. int res = 0;
  960. if (!cfg) {
  961. return 0;
  962. }
  963. /* Clear existing extensions */
  964. remove_all_configured_parking_lot_extensions();
  965. /* Attempt to build new extensions for each lot */
  966. for (iter = ao2_iterator_init(cfg->parking_lots, 0); (lot_cfg = ao2_iterator_next(&iter)); ao2_ref(lot_cfg, -1)) {
  967. if (parking_lot_cfg_create_extensions(lot_cfg)) {
  968. ao2_cleanup(lot_cfg);
  969. lot_cfg = NULL;
  970. res = -1;
  971. break;
  972. }
  973. }
  974. ao2_iterator_destroy(&iter);
  975. if (res) {
  976. remove_pending_parking_lot_extensions(cfg);
  977. ast_log(LOG_ERROR, "Extension registration failed. Previously configured lot extensions were removed and can not be safely restored.\n");
  978. }
  979. return res;
  980. }
  981. static void mark_lots_as_disabled(void)
  982. {
  983. struct ao2_iterator iter;
  984. struct parking_lot *lot;
  985. for (iter = ao2_iterator_init(parking_lot_container, 0); (lot = ao2_iterator_next(&iter)); ao2_ref(lot, -1)) {
  986. lot->disable_mark = 1;
  987. }
  988. ao2_iterator_destroy(&iter);
  989. }
  990. static int config_parking_preapply(void)
  991. {
  992. mark_lots_as_disabled();
  993. if (verify_default_parking_lot()) {
  994. return -1;
  995. }
  996. if (configure_parking_extensions()) {
  997. return -1;
  998. }
  999. return 0;
  1000. }
  1001. static void disable_marked_lots(void)
  1002. {
  1003. struct ao2_iterator iter;
  1004. struct parking_lot *lot;
  1005. for (iter = ao2_iterator_init(parking_lot_container, 0); (lot = ao2_iterator_next(&iter)); ao2_ref(lot, -1)) {
  1006. if (lot->disable_mark) {
  1007. parking_lot_disable(lot);
  1008. }
  1009. }
  1010. ao2_iterator_destroy(&iter);
  1011. }
  1012. static void link_configured_disable_marked_lots(void)
  1013. {
  1014. generate_or_link_lots_to_configs();
  1015. disable_marked_lots();
  1016. }
  1017. const struct ast_module_info *parking_get_module_info(void)
  1018. {
  1019. return ast_module_info;
  1020. }
  1021. static int unload_module(void)
  1022. {
  1023. unload_parking_bridge_features();
  1024. remove_all_configured_parking_lot_extensions();
  1025. unload_parking_applications();
  1026. unload_parking_manager();
  1027. unload_parking_ui();
  1028. unload_parking_devstate();
  1029. unload_parking_tests();
  1030. ao2_cleanup(parking_lot_container);
  1031. parking_lot_container = NULL;
  1032. aco_info_destroy(&cfg_info);
  1033. ao2_global_obj_release(globals);
  1034. return 0;
  1035. }
  1036. static int load_module(void)
  1037. {
  1038. parking_lot_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX,
  1039. AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
  1040. parking_lot_sort_fn,
  1041. NULL);
  1042. if (!parking_lot_container) {
  1043. goto error;
  1044. }
  1045. if (aco_info_init(&cfg_info)) {
  1046. goto error;
  1047. }
  1048. /* Global options */
  1049. aco_option_register(&cfg_info, "parkeddynamic", ACO_EXACT, global_options, "no", OPT_BOOL_T, 1, FLDSET(struct parking_global_config, parkeddynamic));
  1050. /* Register the per parking lot options. */
  1051. aco_option_register(&cfg_info, "parkext", ACO_EXACT, parking_lot_types, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct parking_lot_cfg, parkext));
  1052. aco_option_register(&cfg_info, "context", ACO_EXACT, parking_lot_types, "parkedcalls", OPT_STRINGFIELD_T, 0, STRFLDSET(struct parking_lot_cfg, parking_con));
  1053. aco_option_register(&cfg_info, "parkingtime", ACO_EXACT, parking_lot_types, "45", OPT_UINT_T, 0, FLDSET(struct parking_lot_cfg, parkingtime));
  1054. aco_option_register(&cfg_info, "comebacktoorigin", ACO_EXACT, parking_lot_types, "yes", OPT_BOOL_T, 1, FLDSET(struct parking_lot_cfg, comebacktoorigin));
  1055. aco_option_register(&cfg_info, "comebackcontext", ACO_EXACT, parking_lot_types, "parkedcallstimeout", OPT_STRINGFIELD_T, 0, STRFLDSET(struct parking_lot_cfg, comebackcontext));
  1056. aco_option_register(&cfg_info, "comebackdialtime", ACO_EXACT, parking_lot_types, "30", OPT_UINT_T, 0, FLDSET(struct parking_lot_cfg, comebackdialtime));
  1057. aco_option_register(&cfg_info, "parkedmusicclass", ACO_EXACT, parking_lot_types, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct parking_lot_cfg, mohclass));
  1058. aco_option_register(&cfg_info, "parkext_exclusive", ACO_EXACT, parking_lot_types, "no", OPT_BOOL_T, 1, FLDSET(struct parking_lot_cfg, parkext_exclusive));
  1059. aco_option_register(&cfg_info, "parkinghints", ACO_EXACT, parking_lot_types, "no", OPT_BOOL_T, 1, FLDSET(struct parking_lot_cfg, parkaddhints));
  1060. aco_option_register(&cfg_info, "courtesytone", ACO_EXACT, parking_lot_types, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct parking_lot_cfg, courtesytone));
  1061. /* More complicated parking lot options that require special handling */
  1062. aco_option_register_custom(&cfg_info, "parkpos", ACO_EXACT, parking_lot_types, "701-750", option_handler_parkpos, 0);
  1063. aco_option_register_custom(&cfg_info, "findslot", ACO_EXACT, parking_lot_types, "first", option_handler_findslot, 0);
  1064. aco_option_register_custom(&cfg_info, "parkedplay", ACO_EXACT, parking_lot_types, "caller", option_handler_parkedfeature, OPT_PARKEDPLAY);
  1065. aco_option_register_custom(&cfg_info, "parkedcalltransfers", ACO_EXACT, parking_lot_types, "no", option_handler_parkedfeature, OPT_PARKEDTRANSFERS);
  1066. aco_option_register_custom(&cfg_info, "parkedcallreparking", ACO_EXACT, parking_lot_types, "no", option_handler_parkedfeature, OPT_PARKEDREPARKING);
  1067. aco_option_register_custom(&cfg_info, "parkedcallhangup", ACO_EXACT, parking_lot_types, "no", option_handler_parkedfeature, OPT_PARKEDHANGUP);
  1068. aco_option_register_custom(&cfg_info, "parkedcallrecording", ACO_EXACT, parking_lot_types, "no", option_handler_parkedfeature, OPT_PARKEDRECORDING);
  1069. if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
  1070. goto error;
  1071. }
  1072. if (load_parking_applications()) {
  1073. goto error;
  1074. }
  1075. if (load_parking_ui()) {
  1076. goto error;
  1077. }
  1078. if (load_parking_manager()) {
  1079. goto error;
  1080. }
  1081. if (load_parking_bridge_features()) {
  1082. goto error;
  1083. }
  1084. if (load_parking_devstate()) {
  1085. goto error;
  1086. }
  1087. if (load_parking_tests()) {
  1088. goto error;
  1089. }
  1090. return AST_MODULE_LOAD_SUCCESS;
  1091. error:
  1092. unload_module();
  1093. return AST_MODULE_LOAD_DECLINE;
  1094. }
  1095. static int reload_module(void)
  1096. {
  1097. if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
  1098. return AST_MODULE_LOAD_DECLINE;
  1099. }
  1100. return 0;
  1101. }
  1102. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Call Parking Resource",
  1103. .support_level = AST_MODULE_SUPPORT_CORE,
  1104. .load = load_module,
  1105. .unload = unload_module,
  1106. .reload = reload_module,
  1107. .load_pri = 0,
  1108. );