mwi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2019, Sangoma Technologies Corporation
  5. *
  6. * Kevin Harwell <kharwell@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. /*** MODULEINFO
  19. <support_level>core</support_level>
  20. ***/
  21. #include "asterisk.h"
  22. #include "asterisk/app.h"
  23. #include "asterisk/mwi.h"
  24. #include "asterisk/stasis_channels.h"
  25. /*!
  26. * \brief Define \ref stasis topic objects
  27. * @{
  28. */
  29. static struct stasis_state_manager *mwi_state_manager;
  30. static struct stasis_cache *mwi_state_cache;
  31. static struct stasis_caching_topic *mwi_topic_cached;
  32. /*! @} */
  33. /*! \brief Convert a MWI \ref stasis_message to a \ref ast_event */
  34. static struct ast_event *mwi_to_event(struct stasis_message *message)
  35. {
  36. struct ast_event *event;
  37. struct ast_mwi_state *mwi_state;
  38. char *mailbox;
  39. char *context;
  40. if (!message) {
  41. return NULL;
  42. }
  43. mwi_state = stasis_message_data(message);
  44. /* Strip off @context */
  45. context = mailbox = ast_strdupa(mwi_state->uniqueid);
  46. strsep(&context, "@");
  47. if (ast_strlen_zero(context)) {
  48. context = "default";
  49. }
  50. event = ast_event_new(AST_EVENT_MWI,
  51. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
  52. AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
  53. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, mwi_state->new_msgs,
  54. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, mwi_state->old_msgs,
  55. AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, &mwi_state->eid, sizeof(mwi_state->eid),
  56. AST_EVENT_IE_END);
  57. return event;
  58. }
  59. /*!
  60. * \brief Define \ref stasis message types for MWI
  61. * @{
  62. */
  63. STASIS_MESSAGE_TYPE_DEFN(ast_mwi_state_type,
  64. .to_event = mwi_to_event, );
  65. STASIS_MESSAGE_TYPE_DEFN(ast_mwi_vm_app_type);
  66. /*! @} */
  67. static void mwi_state_dtor(void *obj)
  68. {
  69. struct ast_mwi_state *mwi_state = obj;
  70. ast_string_field_free_memory(mwi_state);
  71. ao2_cleanup(mwi_state->snapshot);
  72. mwi_state->snapshot = NULL;
  73. }
  74. struct stasis_topic *ast_mwi_topic_all(void)
  75. {
  76. return stasis_state_all_topic(mwi_state_manager);
  77. }
  78. struct stasis_cache *ast_mwi_state_cache(void)
  79. {
  80. return mwi_state_cache;
  81. }
  82. struct stasis_topic *ast_mwi_topic_cached(void)
  83. {
  84. return stasis_caching_get_topic(mwi_topic_cached);
  85. }
  86. struct stasis_topic *ast_mwi_topic(const char *uniqueid)
  87. {
  88. return stasis_state_topic(mwi_state_manager, uniqueid);
  89. }
  90. static struct ast_mwi_state *mwi_create_state(const char *mailbox, const char *context,
  91. int urgent_msgs, int new_msgs, int old_msgs)
  92. {
  93. struct ast_mwi_state *mwi_state;
  94. ast_assert(!ast_strlen_zero(mailbox));
  95. mwi_state = ao2_alloc(sizeof(*mwi_state), mwi_state_dtor);
  96. if (!mwi_state) {
  97. ast_log(LOG_ERROR, "Unable to create MWI state for mailbox '%s@%s'\n",
  98. mailbox, ast_strlen_zero(context) ? "" : context);
  99. return NULL;
  100. }
  101. if (ast_string_field_init(mwi_state, 256)) {
  102. ast_log(LOG_ERROR, "Unable to initialize MWI state for mailbox '%s@%s'\n",
  103. mailbox, ast_strlen_zero(context) ? "" : context);
  104. ao2_ref(mwi_state, -1);
  105. return NULL;
  106. }
  107. if (!ast_strlen_zero(context)) {
  108. ast_string_field_build(mwi_state, uniqueid, "%s@%s", mailbox, context);
  109. } else {
  110. ast_string_field_set(mwi_state, uniqueid, mailbox);
  111. }
  112. mwi_state->urgent_msgs = urgent_msgs;
  113. mwi_state->new_msgs = new_msgs;
  114. mwi_state->old_msgs = old_msgs;
  115. return mwi_state;
  116. }
  117. static struct ast_mwi_state *mwi_retrieve_then_create_state(const char *mailbox)
  118. {
  119. int urgent_msgs;
  120. int new_msgs;
  121. int old_msgs;
  122. ast_app_inboxcount2(mailbox, &urgent_msgs, &new_msgs, &old_msgs);
  123. return mwi_create_state(mailbox, NULL, urgent_msgs, new_msgs, old_msgs);
  124. }
  125. struct ast_mwi_state *ast_mwi_create(const char *mailbox, const char *context)
  126. {
  127. return mwi_create_state(mailbox, context, 0, 0, 0);
  128. }
  129. /*!
  130. * \internal
  131. * \brief Create a MWI state snapshot message.
  132. * \since 12.2.0
  133. *
  134. * \param[in] mailbox The mailbox identifier string.
  135. * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
  136. * \param urgent_msgs
  137. * \param[in] new_msgs The number of new messages in this mailbox
  138. * \param[in] old_msgs The number of old messages in this mailbox
  139. * \param[in] channel_id A unique identifier for a channel associated with this
  140. * change in mailbox state
  141. * \param[in] eid The EID of the server that originally published the message
  142. *
  143. * \return message on success. Use ao2_cleanup() when done with it.
  144. * \retval NULL on error.
  145. */
  146. static struct stasis_message *mwi_state_create_message(
  147. const char *mailbox,
  148. const char *context,
  149. int urgent_msgs,
  150. int new_msgs,
  151. int old_msgs,
  152. const char *channel_id,
  153. struct ast_eid *eid)
  154. {
  155. struct ast_mwi_state *mwi_state;
  156. struct stasis_message *message;
  157. if (!ast_mwi_state_type()) {
  158. return NULL;
  159. }
  160. mwi_state = mwi_create_state(mailbox, context, urgent_msgs, new_msgs, old_msgs);
  161. if (!mwi_state) {
  162. return NULL;
  163. }
  164. if (!ast_strlen_zero(channel_id)) {
  165. mwi_state->snapshot = ast_channel_snapshot_get_latest(channel_id);
  166. }
  167. if (eid) {
  168. mwi_state->eid = *eid;
  169. } else {
  170. mwi_state->eid = ast_eid_default;
  171. }
  172. /*
  173. * XXX As far as stasis is concerned, all MWI events are local.
  174. *
  175. * We may in the future want to make MWI aggregate local/remote
  176. * message counts similar to how device state aggregates state.
  177. */
  178. message = stasis_message_create_full(ast_mwi_state_type(), mwi_state, &ast_eid_default);
  179. ao2_cleanup(mwi_state);
  180. return message;
  181. }
  182. /*!
  183. * \internal
  184. *
  185. * This object currently acts as a typedef, but can also be thought of as a "child" object
  186. * of the stasis_state_subscriber type. As such the "base" pointer should always be the
  187. * first object attribute. Doing so allows this object to be easily type cast and used by
  188. * the stasis_state code.
  189. */
  190. struct ast_mwi_subscriber {
  191. /*! The "base" state subscriber. (Must be first object attribute) */
  192. struct stasis_state_subscriber *base;
  193. };
  194. struct ast_mwi_subscriber *ast_mwi_add_subscriber(const char *mailbox)
  195. {
  196. return (struct ast_mwi_subscriber *)stasis_state_add_subscriber(
  197. mwi_state_manager, mailbox);
  198. }
  199. struct ast_mwi_subscriber *ast_mwi_subscribe_pool(const char *mailbox,
  200. stasis_subscription_cb callback, void *data)
  201. {
  202. struct stasis_subscription *stasis_sub;
  203. struct ast_mwi_subscriber *sub = (struct ast_mwi_subscriber *)stasis_state_subscribe_pool(
  204. mwi_state_manager, mailbox, callback, data);
  205. if (!sub) {
  206. return NULL;
  207. }
  208. stasis_sub = ast_mwi_subscriber_subscription(sub);
  209. stasis_subscription_accept_message_type(stasis_sub, ast_mwi_state_type());
  210. stasis_subscription_set_filter(stasis_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  211. return sub;
  212. }
  213. void *ast_mwi_unsubscribe(struct ast_mwi_subscriber *sub)
  214. {
  215. return stasis_state_unsubscribe((struct stasis_state_subscriber *)sub);
  216. }
  217. void *ast_mwi_unsubscribe_and_join(struct ast_mwi_subscriber *sub)
  218. {
  219. return stasis_state_unsubscribe_and_join((struct stasis_state_subscriber *)sub);
  220. }
  221. struct stasis_topic *ast_mwi_subscriber_topic(struct ast_mwi_subscriber *sub)
  222. {
  223. return stasis_state_subscriber_topic((struct stasis_state_subscriber *)sub);
  224. }
  225. struct ast_mwi_state *ast_mwi_subscriber_data(struct ast_mwi_subscriber *sub)
  226. {
  227. struct stasis_state_subscriber *s = (struct stasis_state_subscriber *)sub;
  228. struct ast_mwi_state *mwi_state = stasis_state_subscriber_data(s);
  229. return mwi_state ?: mwi_retrieve_then_create_state(stasis_state_subscriber_id(s));
  230. }
  231. struct stasis_subscription *ast_mwi_subscriber_subscription(struct ast_mwi_subscriber *sub)
  232. {
  233. return stasis_state_subscriber_subscription((struct stasis_state_subscriber *)sub);
  234. }
  235. /*!
  236. * \internal
  237. *
  238. * This object currently acts as a typedef, but can also be thought of as a "child" object
  239. * of the stasis_state_publisher type. As such the "base" pointer should always be the
  240. * first object attribute. Doing so allows this object to be easily type cast and used by
  241. * the stasis_state code.
  242. */
  243. struct ast_mwi_publisher {
  244. /*! The "base" state publisher. (Must be first object attribute) */
  245. struct stasis_state_publisher *base;
  246. };
  247. struct ast_mwi_publisher *ast_mwi_add_publisher(const char *mailbox)
  248. {
  249. return (struct ast_mwi_publisher *)stasis_state_add_publisher(
  250. mwi_state_manager, mailbox);
  251. }
  252. int ast_mwi_add_observer(struct ast_mwi_observer *observer)
  253. {
  254. return stasis_state_add_observer(mwi_state_manager,
  255. (struct stasis_state_observer *)observer);
  256. }
  257. void ast_mwi_remove_observer(struct ast_mwi_observer *observer)
  258. {
  259. stasis_state_remove_observer(mwi_state_manager,
  260. (struct stasis_state_observer *)observer);
  261. }
  262. struct mwi_handler_data {
  263. on_mwi_state handler;
  264. void *data;
  265. };
  266. static int handle_mwi_state(const char *id, struct stasis_message *msg, void *user_data)
  267. {
  268. struct mwi_handler_data *d = user_data;
  269. struct ast_mwi_state *mwi_state = stasis_message_data(msg);
  270. int res;
  271. if (mwi_state) {
  272. return d->handler(mwi_state, d->data);
  273. }
  274. mwi_state = mwi_create_state(id, NULL, 0, 0, 0);
  275. if (!mwi_state) {
  276. return 0;
  277. }
  278. res = d->handler(mwi_state, d->data);
  279. ao2_ref(mwi_state, -1);
  280. return res;
  281. }
  282. void ast_mwi_state_callback_all(on_mwi_state handler, void *data)
  283. {
  284. struct mwi_handler_data d = {
  285. .handler = handler,
  286. .data = data
  287. };
  288. stasis_state_callback_all(mwi_state_manager, handle_mwi_state, &d);
  289. }
  290. void ast_mwi_state_callback_subscribed(on_mwi_state handler, void *data)
  291. {
  292. struct mwi_handler_data d = {
  293. .handler = handler,
  294. .data = data
  295. };
  296. stasis_state_callback_subscribed(mwi_state_manager, handle_mwi_state, &d);
  297. }
  298. int ast_mwi_publish(struct ast_mwi_publisher *pub, int urgent_msgs,
  299. int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
  300. {
  301. struct stasis_state_publisher *p = (struct stasis_state_publisher *)pub;
  302. struct stasis_message *msg = mwi_state_create_message(stasis_state_publisher_id(p),
  303. NULL, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
  304. if (!msg) {
  305. return -1;
  306. }
  307. stasis_state_publish(p, msg);
  308. ao2_ref(msg, -1);
  309. return 0;
  310. }
  311. int ast_mwi_publish_by_mailbox(const char *mailbox, const char *context, int urgent_msgs,
  312. int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
  313. {
  314. struct ast_mwi_state *mwi_state;
  315. struct stasis_message *msg = mwi_state_create_message(
  316. mailbox, context, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
  317. if (!msg) {
  318. return -1;
  319. }
  320. mwi_state = stasis_message_data(msg);
  321. stasis_state_publish_by_id(mwi_state_manager, mwi_state->uniqueid, NULL, msg);
  322. ao2_ref(msg, -1);
  323. return 0;
  324. }
  325. int ast_publish_mwi_state_full(
  326. const char *mailbox,
  327. const char *context,
  328. int new_msgs,
  329. int old_msgs,
  330. const char *channel_id,
  331. struct ast_eid *eid)
  332. {
  333. return ast_mwi_publish_by_mailbox(mailbox, context, 0, new_msgs, old_msgs, channel_id, eid);
  334. }
  335. int ast_delete_mwi_state_full(const char *mailbox, const char *context, struct ast_eid *eid)
  336. {
  337. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  338. struct stasis_message *cached_msg;
  339. struct stasis_message *clear_msg;
  340. struct ast_mwi_state *mwi_state;
  341. msg = mwi_state_create_message(mailbox, context, 0, 0, 0, NULL, eid);
  342. if (!msg) {
  343. return -1;
  344. }
  345. mwi_state = stasis_message_data(msg);
  346. /*
  347. * XXX As far as stasis is concerned, all MWI events are local.
  348. *
  349. * For now, it is assumed that there is only one entity
  350. * maintaining the state of a particular mailbox.
  351. *
  352. * If we ever have multiple MWI event entities maintaining
  353. * the same mailbox that wish to delete their cached entry
  354. * we will need to do something about the race condition
  355. * potential between checking the cache and removing the
  356. * cache entry.
  357. */
  358. cached_msg = stasis_cache_get_by_eid(ast_mwi_state_cache(),
  359. ast_mwi_state_type(), mwi_state->uniqueid, &ast_eid_default);
  360. if (!cached_msg) {
  361. /* Nothing to clear from the cache, but still need to remove state */
  362. stasis_state_remove_publish_by_id(mwi_state_manager, mwi_state->uniqueid, eid, NULL);
  363. return -1;
  364. }
  365. ao2_cleanup(cached_msg);
  366. clear_msg = stasis_cache_clear_create(msg);
  367. stasis_state_remove_publish_by_id(mwi_state_manager, mwi_state->uniqueid, eid, clear_msg);
  368. ao2_cleanup(clear_msg);
  369. return 0;
  370. }
  371. static const char *mwi_state_get_id(struct stasis_message *message)
  372. {
  373. if (ast_mwi_state_type() == stasis_message_type(message)) {
  374. struct ast_mwi_state *mwi_state = stasis_message_data(message);
  375. return mwi_state->uniqueid;
  376. } else if (stasis_subscription_change_type() == stasis_message_type(message)) {
  377. struct stasis_subscription_change *change = stasis_message_data(message);
  378. return change->uniqueid;
  379. }
  380. return NULL;
  381. }
  382. static void mwi_blob_dtor(void *obj)
  383. {
  384. struct ast_mwi_blob *mwi_blob = obj;
  385. ao2_cleanup(mwi_blob->mwi_state);
  386. ast_json_unref(mwi_blob->blob);
  387. }
  388. struct stasis_message *ast_mwi_blob_create(struct ast_mwi_state *mwi_state,
  389. struct stasis_message_type *message_type,
  390. struct ast_json *blob)
  391. {
  392. struct ast_mwi_blob *obj;
  393. struct stasis_message *msg;
  394. ast_assert(blob != NULL);
  395. if (!message_type) {
  396. return NULL;
  397. }
  398. obj = ao2_alloc(sizeof(*obj), mwi_blob_dtor);
  399. if (!obj) {
  400. return NULL;
  401. }
  402. obj->mwi_state = mwi_state;
  403. ao2_ref(obj->mwi_state, +1);
  404. obj->blob = ast_json_ref(blob);
  405. /* This is not a normal MWI event. Only used by the MinivmNotify app. */
  406. msg = stasis_message_create(message_type, obj);
  407. ao2_ref(obj, -1);
  408. return msg;
  409. }
  410. static void mwi_cleanup(void)
  411. {
  412. ao2_cleanup(mwi_state_cache);
  413. mwi_state_cache = NULL;
  414. mwi_topic_cached = stasis_caching_unsubscribe_and_join(mwi_topic_cached);
  415. ao2_cleanup(mwi_state_manager);
  416. mwi_state_manager = NULL;
  417. STASIS_MESSAGE_TYPE_CLEANUP(ast_mwi_state_type);
  418. STASIS_MESSAGE_TYPE_CLEANUP(ast_mwi_vm_app_type);
  419. }
  420. int mwi_init(void)
  421. {
  422. ast_register_cleanup(mwi_cleanup);
  423. if (STASIS_MESSAGE_TYPE_INIT(ast_mwi_state_type) != 0) {
  424. return -1;
  425. }
  426. if (STASIS_MESSAGE_TYPE_INIT(ast_mwi_vm_app_type) != 0) {
  427. return -1;
  428. }
  429. mwi_state_manager = stasis_state_manager_create("mwi:all");
  430. if (!mwi_state_manager) {
  431. return -1;
  432. }
  433. mwi_state_cache = stasis_cache_create(mwi_state_get_id);
  434. if (!mwi_state_cache) {
  435. return -1;
  436. }
  437. mwi_topic_cached = stasis_caching_topic_create(ast_mwi_topic_all(), mwi_state_cache);
  438. if (!mwi_topic_cached) {
  439. return -1;
  440. }
  441. return 0;
  442. }