parking.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 Parking Core
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "asterisk/_private.h"
  27. #include "asterisk/astobj2.h"
  28. #include "asterisk/pbx.h"
  29. #include "asterisk/bridge.h"
  30. #include "asterisk/parking.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/_private.h"
  33. #include "asterisk/module.h"
  34. /*! \brief Message type for parked calls */
  35. STASIS_MESSAGE_TYPE_DEFN(ast_parked_call_type);
  36. /*! \brief Topic for parking lots */
  37. static struct stasis_topic *parking_topic;
  38. /*! \brief The container for the parking provider */
  39. static AO2_GLOBAL_OBJ_STATIC(parking_provider);
  40. static void parking_stasis_cleanup(void)
  41. {
  42. STASIS_MESSAGE_TYPE_CLEANUP(ast_parked_call_type);
  43. ao2_cleanup(parking_topic);
  44. parking_topic = NULL;
  45. }
  46. int ast_parking_stasis_init(void)
  47. {
  48. if (STASIS_MESSAGE_TYPE_INIT(ast_parked_call_type)) {
  49. return -1;
  50. }
  51. parking_topic = stasis_topic_create("ast_parking");
  52. if (!parking_topic) {
  53. return -1;
  54. }
  55. ast_register_cleanup(parking_stasis_cleanup);
  56. return 0;
  57. }
  58. struct stasis_topic *ast_parking_topic(void)
  59. {
  60. return parking_topic;
  61. }
  62. /*! \brief Destructor for parked_call_payload objects */
  63. static void parked_call_payload_destructor(void *obj)
  64. {
  65. struct ast_parked_call_payload *park_obj = obj;
  66. ao2_cleanup(park_obj->parkee);
  67. ao2_cleanup(park_obj->retriever);
  68. ast_string_field_free_memory(park_obj);
  69. }
  70. struct ast_parked_call_payload *ast_parked_call_payload_create(enum ast_parked_call_event_type event_type,
  71. struct ast_channel_snapshot *parkee_snapshot, const char *parker_dial_string,
  72. struct ast_channel_snapshot *retriever_snapshot, const char *parkinglot,
  73. unsigned int parkingspace, unsigned long int timeout,
  74. unsigned long int duration)
  75. {
  76. RAII_VAR(struct ast_parked_call_payload *, payload, NULL, ao2_cleanup);
  77. payload = ao2_alloc(sizeof(*payload), parked_call_payload_destructor);
  78. if (!payload) {
  79. return NULL;
  80. }
  81. if (ast_string_field_init(payload, 32)) {
  82. return NULL;
  83. }
  84. payload->event_type = event_type;
  85. ao2_ref(parkee_snapshot, +1);
  86. payload->parkee = parkee_snapshot;
  87. if (retriever_snapshot) {
  88. ao2_ref(retriever_snapshot, +1);
  89. payload->retriever = retriever_snapshot;
  90. }
  91. if (parkinglot) {
  92. ast_string_field_set(payload, parkinglot, parkinglot);
  93. }
  94. if (parker_dial_string) {
  95. ast_string_field_set(payload, parker_dial_string, parker_dial_string);
  96. }
  97. payload->parkingspace = parkingspace;
  98. payload->timeout = timeout;
  99. payload->duration = duration;
  100. /* Bump the ref count by one since RAII_VAR is going to eat one when we leave. */
  101. ao2_ref(payload, +1);
  102. return payload;
  103. }
  104. int ast_parking_park_bridge_channel(struct ast_bridge_channel *parkee, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
  105. {
  106. RAII_VAR(struct ast_parking_bridge_feature_fn_table *, table,
  107. ao2_global_obj_ref(parking_provider), ao2_cleanup);
  108. if (!table || !table->parking_park_bridge_channel) {
  109. return -1;
  110. }
  111. if (table->module_info) {
  112. SCOPED_MODULE_USE(table->module_info->self);
  113. return table->parking_park_bridge_channel(parkee, parkee_uuid, parker_uuid, app_data);
  114. }
  115. return table->parking_park_bridge_channel(parkee, parkee_uuid, parker_uuid, app_data);
  116. }
  117. int ast_parking_blind_transfer_park(struct ast_bridge_channel *parker,
  118. const char *context, const char *exten, transfer_channel_cb parked_channel_cb,
  119. struct transfer_channel_data *parked_channel_data)
  120. {
  121. RAII_VAR(struct ast_parking_bridge_feature_fn_table *, table,
  122. ao2_global_obj_ref(parking_provider), ao2_cleanup);
  123. if (!table || !table->parking_blind_transfer_park) {
  124. return -1;
  125. }
  126. if (table->module_info) {
  127. SCOPED_MODULE_USE(table->module_info->self);
  128. return table->parking_blind_transfer_park(parker, context, exten, parked_channel_cb, parked_channel_data);
  129. }
  130. return table->parking_blind_transfer_park(parker, context, exten, parked_channel_cb, parked_channel_data);
  131. }
  132. int ast_parking_park_call(struct ast_bridge_channel *parker, char *exten, size_t length)
  133. {
  134. RAII_VAR(struct ast_parking_bridge_feature_fn_table *, table,
  135. ao2_global_obj_ref(parking_provider), ao2_cleanup);
  136. if (!table || !table->parking_park_call) {
  137. return -1;
  138. }
  139. if (table->module_info) {
  140. SCOPED_MODULE_USE(table->module_info->self);
  141. return table->parking_park_call(parker, exten, length);
  142. }
  143. return table->parking_park_call(parker, exten, length);
  144. }
  145. int ast_parking_is_exten_park(const char *context, const char *exten)
  146. {
  147. RAII_VAR(struct ast_parking_bridge_feature_fn_table *, table,
  148. ao2_global_obj_ref(parking_provider), ao2_cleanup);
  149. if (!table || !table->parking_is_exten_park) {
  150. return -1;
  151. }
  152. if (table->module_info) {
  153. SCOPED_MODULE_USE(table->module_info->self);
  154. return table->parking_is_exten_park(context, exten);
  155. }
  156. return table->parking_is_exten_park(context, exten);
  157. }
  158. int ast_parking_register_bridge_features(struct ast_parking_bridge_feature_fn_table *fn_table)
  159. {
  160. RAII_VAR(struct ast_parking_bridge_feature_fn_table *, wrapper,
  161. ao2_global_obj_ref(parking_provider), ao2_cleanup);
  162. if (fn_table->module_version != PARKING_MODULE_VERSION) {
  163. ast_log(AST_LOG_WARNING, "Parking module provided incorrect parking module "
  164. "version: %u (expected: %d)\n", fn_table->module_version, PARKING_MODULE_VERSION);
  165. return -1;
  166. }
  167. if (wrapper) {
  168. ast_log(AST_LOG_WARNING, "Parking provider already registered by %s!\n",
  169. wrapper->module_name);
  170. return -1;
  171. }
  172. wrapper = ao2_alloc(sizeof(*wrapper), NULL);
  173. if (!wrapper) {
  174. return -1;
  175. }
  176. *wrapper = *fn_table;
  177. ao2_global_obj_replace_unref(parking_provider, wrapper);
  178. return 0;
  179. }
  180. int ast_parking_unregister_bridge_features(const char *module_name)
  181. {
  182. RAII_VAR(struct ast_parking_bridge_feature_fn_table *, wrapper,
  183. ao2_global_obj_ref(parking_provider), ao2_cleanup);
  184. if (!wrapper) {
  185. return -1;
  186. }
  187. if (strcmp(wrapper->module_name, module_name)) {
  188. ast_log(AST_LOG_WARNING, "%s has not registered the parking provider\n", module_name);
  189. return -1;
  190. }
  191. ao2_global_obj_release(parking_provider);
  192. return 0;
  193. }
  194. int ast_parking_provider_registered(void)
  195. {
  196. RAII_VAR(struct ast_parking_bridge_feature_fn_table *, table,
  197. ao2_global_obj_ref(parking_provider), ao2_cleanup);
  198. return !!table;
  199. }