presencestate.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011-2012, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@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 Presence state management
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/_private.h"
  28. #include "asterisk/utils.h"
  29. #include "asterisk/lock.h"
  30. #include "asterisk/linkedlists.h"
  31. #include "asterisk/presencestate.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/event.h"
  35. /*! \brief Device state strings for printing */
  36. static const struct {
  37. const char *string;
  38. enum ast_presence_state state;
  39. } state2string[] = {
  40. { "not_set", AST_PRESENCE_NOT_SET},
  41. { "unavailable", AST_PRESENCE_UNAVAILABLE },
  42. { "available", AST_PRESENCE_AVAILABLE},
  43. { "away", AST_PRESENCE_AWAY},
  44. { "xa", AST_PRESENCE_XA},
  45. { "chat", AST_PRESENCE_CHAT},
  46. { "dnd", AST_PRESENCE_DND},
  47. };
  48. /*! \brief Flag for the queue */
  49. static ast_cond_t change_pending;
  50. struct state_change {
  51. AST_LIST_ENTRY(state_change) list;
  52. char provider[1];
  53. };
  54. /*! \brief A presence state provider */
  55. struct presence_state_provider {
  56. char label[40];
  57. ast_presence_state_prov_cb_type callback;
  58. AST_RWLIST_ENTRY(presence_state_provider) list;
  59. };
  60. /*! \brief A list of providers */
  61. static AST_RWLIST_HEAD_STATIC(presence_state_providers, presence_state_provider);
  62. /*! \brief The state change queue. State changes are queued
  63. for processing by a separate thread */
  64. static AST_LIST_HEAD_STATIC(state_changes, state_change);
  65. /*! \brief The presence state change notification thread */
  66. static pthread_t change_thread = AST_PTHREADT_NULL;
  67. const char *ast_presence_state2str(enum ast_presence_state state)
  68. {
  69. int i;
  70. for (i = 0; i < ARRAY_LEN(state2string); i++) {
  71. if (state == state2string[i].state) {
  72. return state2string[i].string;
  73. }
  74. }
  75. return "";
  76. }
  77. enum ast_presence_state ast_presence_state_val(const char *val)
  78. {
  79. int i;
  80. for (i = 0; i < ARRAY_LEN(state2string); i++) {
  81. if (!strcasecmp(val, state2string[i].string)) {
  82. return state2string[i].state;
  83. }
  84. }
  85. return AST_PRESENCE_INVALID;
  86. }
  87. static enum ast_presence_state presence_state_cached(const char *presence_provider, char **subtype, char **message)
  88. {
  89. enum ast_presence_state res = AST_PRESENCE_INVALID;
  90. struct ast_event *event;
  91. const char *_subtype;
  92. const char *_message;
  93. event = ast_event_get_cached(AST_EVENT_PRESENCE_STATE,
  94. AST_EVENT_IE_PRESENCE_PROVIDER, AST_EVENT_IE_PLTYPE_STR, presence_provider,
  95. AST_EVENT_IE_END);
  96. if (!event) {
  97. return res;
  98. }
  99. res = ast_event_get_ie_uint(event, AST_EVENT_IE_PRESENCE_STATE);
  100. _subtype = ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_SUBTYPE);
  101. _message = ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_MESSAGE);
  102. *subtype = !ast_strlen_zero(_subtype) ? ast_strdup(_subtype) : NULL;
  103. *message = !ast_strlen_zero(_message) ? ast_strdup(_message) : NULL;
  104. ast_event_destroy(event);
  105. return res;
  106. }
  107. static enum ast_presence_state ast_presence_state_helper(const char *presence_provider, char **subtype, char **message, int check_cache)
  108. {
  109. struct presence_state_provider *provider;
  110. char *address;
  111. char *label = ast_strdupa(presence_provider);
  112. int res = AST_PRESENCE_INVALID;
  113. if (check_cache) {
  114. res = presence_state_cached(presence_provider, subtype, message);
  115. if (res != AST_PRESENCE_INVALID) {
  116. return res;
  117. }
  118. }
  119. if ((address = strchr(label, ':'))) {
  120. *address = '\0';
  121. address++;
  122. } else {
  123. ast_log(LOG_WARNING, "No label found for presence state provider: %s\n", presence_provider);
  124. return res;
  125. }
  126. AST_RWLIST_RDLOCK(&presence_state_providers);
  127. AST_RWLIST_TRAVERSE(&presence_state_providers, provider, list) {
  128. ast_debug(5, "Checking provider %s with %s\n", provider->label, label);
  129. if (!strcasecmp(provider->label, label)) {
  130. res = provider->callback(address, subtype, message);
  131. break;
  132. }
  133. }
  134. AST_RWLIST_UNLOCK(&presence_state_providers);
  135. if (!provider) {
  136. ast_log(LOG_WARNING, "No provider found for label %s\n", label);
  137. }
  138. return res;
  139. }
  140. enum ast_presence_state ast_presence_state(const char *presence_provider, char **subtype, char **message)
  141. {
  142. return ast_presence_state_helper(presence_provider, subtype, message, 1);
  143. }
  144. enum ast_presence_state ast_presence_state_nocache(const char *presence_provider, char **subtype, char **message)
  145. {
  146. return ast_presence_state_helper(presence_provider, subtype, message, 0);
  147. }
  148. int ast_presence_state_prov_add(const char *label, ast_presence_state_prov_cb_type callback)
  149. {
  150. struct presence_state_provider *provider;
  151. if (!callback || !(provider = ast_calloc(1, sizeof(*provider)))) {
  152. return -1;
  153. }
  154. provider->callback = callback;
  155. ast_copy_string(provider->label, label, sizeof(provider->label));
  156. AST_RWLIST_WRLOCK(&presence_state_providers);
  157. AST_RWLIST_INSERT_HEAD(&presence_state_providers, provider, list);
  158. AST_RWLIST_UNLOCK(&presence_state_providers);
  159. return 0;
  160. }
  161. int ast_presence_state_prov_del(const char *label)
  162. {
  163. struct presence_state_provider *provider;
  164. int res = -1;
  165. AST_RWLIST_WRLOCK(&presence_state_providers);
  166. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&presence_state_providers, provider, list) {
  167. if (!strcasecmp(provider->label, label)) {
  168. AST_RWLIST_REMOVE_CURRENT(list);
  169. ast_free(provider);
  170. res = 0;
  171. break;
  172. }
  173. }
  174. AST_RWLIST_TRAVERSE_SAFE_END;
  175. AST_RWLIST_UNLOCK(&presence_state_providers);
  176. return res;
  177. }
  178. static void presence_state_event(const char *provider,
  179. enum ast_presence_state state,
  180. const char *subtype,
  181. const char *message)
  182. {
  183. struct ast_event *event;
  184. if (!(event = ast_event_new(AST_EVENT_PRESENCE_STATE,
  185. AST_EVENT_IE_PRESENCE_PROVIDER, AST_EVENT_IE_PLTYPE_STR, provider,
  186. AST_EVENT_IE_PRESENCE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
  187. AST_EVENT_IE_PRESENCE_SUBTYPE, AST_EVENT_IE_PLTYPE_STR, S_OR(subtype, ""),
  188. AST_EVENT_IE_PRESENCE_MESSAGE, AST_EVENT_IE_PLTYPE_STR, S_OR(message, ""),
  189. AST_EVENT_IE_END))) {
  190. return;
  191. }
  192. ast_event_queue_and_cache(event);
  193. }
  194. static void do_presence_state_change(const char *provider)
  195. {
  196. char *subtype = NULL;
  197. char *message = NULL;
  198. enum ast_presence_state state;
  199. state = ast_presence_state_helper(provider, &subtype, &message, 0);
  200. if (state == AST_PRESENCE_INVALID) {
  201. return;
  202. }
  203. presence_state_event(provider, state, subtype, message);
  204. ast_free(subtype);
  205. ast_free(message);
  206. }
  207. int ast_presence_state_changed_literal(enum ast_presence_state state,
  208. const char *subtype,
  209. const char *message,
  210. const char *presence_provider)
  211. {
  212. struct state_change *change;
  213. if (state != AST_PRESENCE_NOT_SET) {
  214. presence_state_event(presence_provider, state, subtype, message);
  215. } else if ((change_thread == AST_PTHREADT_NULL) ||
  216. !(change = ast_calloc(1, sizeof(*change) + strlen(presence_provider)))) {
  217. do_presence_state_change(presence_provider);
  218. } else {
  219. strcpy(change->provider, presence_provider);
  220. AST_LIST_LOCK(&state_changes);
  221. AST_LIST_INSERT_TAIL(&state_changes, change, list);
  222. ast_cond_signal(&change_pending);
  223. AST_LIST_UNLOCK(&state_changes);
  224. }
  225. return 0;
  226. }
  227. int ast_presence_state_changed(enum ast_presence_state state,
  228. const char *subtype,
  229. const char *message,
  230. const char *fmt, ...)
  231. {
  232. char buf[AST_MAX_EXTENSION];
  233. va_list ap;
  234. va_start(ap, fmt);
  235. vsnprintf(buf, sizeof(buf), fmt, ap);
  236. va_end(ap);
  237. return ast_presence_state_changed_literal(state, subtype, message, buf);
  238. }
  239. /*! \brief Go through the presence state change queue and update changes in the presence state thread */
  240. static void *do_presence_changes(void *data)
  241. {
  242. struct state_change *next, *current;
  243. for (;;) {
  244. /* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
  245. AST_LIST_LOCK(&state_changes);
  246. if (AST_LIST_EMPTY(&state_changes))
  247. ast_cond_wait(&change_pending, &state_changes.lock);
  248. next = AST_LIST_FIRST(&state_changes);
  249. AST_LIST_HEAD_INIT_NOLOCK(&state_changes);
  250. AST_LIST_UNLOCK(&state_changes);
  251. /* Process each state change */
  252. while ((current = next)) {
  253. next = AST_LIST_NEXT(current, list);
  254. do_presence_state_change(current->provider);
  255. ast_free(current);
  256. }
  257. }
  258. return NULL;
  259. }
  260. int ast_presence_state_engine_init(void)
  261. {
  262. ast_cond_init(&change_pending, NULL);
  263. if (ast_pthread_create_background(&change_thread, NULL, do_presence_changes, NULL) < 0) {
  264. ast_log(LOG_ERROR, "Unable to start presence state change thread.\n");
  265. return -1;
  266. }
  267. return 0;
  268. }