devicestate.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Russell Bryant <russell@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Device state management
  22. *
  23. * \author Mark Spencer <markster@digium.com>
  24. * \author Russell Bryant <russell@digium.com>
  25. *
  26. * \arg \ref AstExtState
  27. */
  28. /*! \page AstExtState Extension and device states in Asterisk
  29. *
  30. * (Note that these descriptions of device states and extension
  31. * states have not been updated to the way things work
  32. * in Asterisk 1.6.)
  33. *
  34. * Asterisk has an internal system that reports states
  35. * for an extension. By using the dialplan priority -1,
  36. * also called a \b hint, a connection can be made from an
  37. * extension to one or many devices. The state of the extension
  38. * now depends on the combined state of the devices.
  39. *
  40. * The device state is basically based on the current calls.
  41. * If the devicestate engine can find a call from or to the
  42. * device, it's in use.
  43. *
  44. * Some channel drivers implement a callback function for
  45. * a better level of reporting device states. The SIP channel
  46. * has a complicated system for this, which is improved
  47. * by adding call limits to the configuration.
  48. *
  49. * Functions that want to check the status of an extension
  50. * register themself as a \b watcher.
  51. * Watchers in this system can subscribe either to all extensions
  52. * or just a specific extensions.
  53. *
  54. * For non-device related states, there's an API called
  55. * devicestate providers. This is an extendible system for
  56. * delivering state information from outside sources or
  57. * functions within Asterisk. Currently we have providers
  58. * for app_meetme.c - the conference bridge - and call
  59. * parking (metermaids).
  60. *
  61. * There are manly three subscribers to extension states
  62. * within Asterisk:
  63. * - AMI, the manager interface
  64. * - app_queue.c - the Queue dialplan application
  65. * - SIP subscriptions, a.k.a. "blinking lamps" or
  66. * "buddy lists"
  67. *
  68. * The CLI command "show hints" show last known state
  69. *
  70. * \note None of these handle user states, like an IM presence
  71. * system. res_xmpp.c can subscribe and watch such states
  72. * in jabber/xmpp based systems.
  73. *
  74. * \section AstDevStateArch Architecture for devicestates
  75. *
  76. * When a channel driver or asterisk app changes state for
  77. * a watched object, it alerts the core. The core queues
  78. * a change. When the change is processed, there's a query
  79. * sent to the channel driver/provider if there's a function
  80. * to handle that, otherwise a channel walk is issued to find
  81. * a channel that involves the object.
  82. *
  83. * The changes are queued and processed by a separate thread.
  84. * This thread calls the watchers subscribing to status
  85. * changes for the object. For manager, this results
  86. * in events. For SIP, NOTIFY requests.
  87. *
  88. * - Device states
  89. * \arg \ref devicestate.c
  90. * \arg \ref devicestate.h
  91. *
  92. * \section AstExtStateArch Architecture for extension states
  93. *
  94. * Hints are connected to extension. If an extension changes state
  95. * it checks the hint devices. If there is a hint, the callbacks into
  96. * device states are checked. The aggregated state is set for the hint
  97. * and reported back.
  98. *
  99. * - Extension states
  100. * \arg \ref AstENUM ast_extension_states
  101. * \arg \ref pbx.c
  102. * \arg \ref pbx.h
  103. * - Structures
  104. * - \ref ast_state_cb struct. Callbacks for watchers
  105. * - Callback ast_state_cb_type
  106. * - \ref ast_hint struct.
  107. * - Functions
  108. * - ast_extension_state_add()
  109. * - ast_extension_state_del()
  110. * - ast_get_hint()
  111. *
  112. */
  113. /*** MODULEINFO
  114. <support_level>core</support_level>
  115. ***/
  116. #include "asterisk.h"
  117. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  118. #include "asterisk/_private.h"
  119. #include "asterisk/channel.h"
  120. #include "asterisk/utils.h"
  121. #include "asterisk/lock.h"
  122. #include "asterisk/linkedlists.h"
  123. #include "asterisk/devicestate.h"
  124. #include "asterisk/pbx.h"
  125. #include "asterisk/app.h"
  126. #include "asterisk/event.h"
  127. /*! \brief Device state strings for printing */
  128. static const char * const devstatestring[][2] = {
  129. { /* 0 AST_DEVICE_UNKNOWN */ "Unknown", "UNKNOWN" }, /*!< Valid, but unknown state */
  130. { /* 1 AST_DEVICE_NOT_INUSE */ "Not in use", "NOT_INUSE" }, /*!< Not used */
  131. { /* 2 AST_DEVICE IN USE */ "In use", "INUSE" }, /*!< In use */
  132. { /* 3 AST_DEVICE_BUSY */ "Busy", "BUSY" }, /*!< Busy */
  133. { /* 4 AST_DEVICE_INVALID */ "Invalid", "INVALID" }, /*!< Invalid - not known to Asterisk */
  134. { /* 5 AST_DEVICE_UNAVAILABLE */ "Unavailable", "UNAVAILABLE" }, /*!< Unavailable (not registered) */
  135. { /* 6 AST_DEVICE_RINGING */ "Ringing", "RINGING" }, /*!< Ring, ring, ring */
  136. { /* 7 AST_DEVICE_RINGINUSE */ "Ring+Inuse", "RINGINUSE" }, /*!< Ring and in use */
  137. { /* 8 AST_DEVICE_ONHOLD */ "On Hold", "ONHOLD" }, /*!< On Hold */
  138. };
  139. /*!\brief Mapping for channel states to device states */
  140. static const struct chan2dev {
  141. enum ast_channel_state chan;
  142. enum ast_device_state dev;
  143. } chan2dev[] = {
  144. { AST_STATE_DOWN, AST_DEVICE_NOT_INUSE },
  145. { AST_STATE_RESERVED, AST_DEVICE_INUSE },
  146. { AST_STATE_OFFHOOK, AST_DEVICE_INUSE },
  147. { AST_STATE_DIALING, AST_DEVICE_INUSE },
  148. { AST_STATE_RING, AST_DEVICE_INUSE },
  149. { AST_STATE_RINGING, AST_DEVICE_RINGING },
  150. { AST_STATE_UP, AST_DEVICE_INUSE },
  151. { AST_STATE_BUSY, AST_DEVICE_BUSY },
  152. { AST_STATE_DIALING_OFFHOOK, AST_DEVICE_INUSE },
  153. { AST_STATE_PRERING, AST_DEVICE_RINGING },
  154. { -100, -100 },
  155. };
  156. /*! \brief A device state provider (not a channel) */
  157. struct devstate_prov {
  158. char label[40];
  159. ast_devstate_prov_cb_type callback;
  160. AST_RWLIST_ENTRY(devstate_prov) list;
  161. };
  162. /*! \brief A list of providers */
  163. static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);
  164. struct state_change {
  165. AST_LIST_ENTRY(state_change) list;
  166. enum ast_devstate_cache cachable;
  167. char device[1];
  168. };
  169. /*! \brief The state change queue. State changes are queued
  170. for processing by a separate thread */
  171. static AST_LIST_HEAD_STATIC(state_changes, state_change);
  172. /*! \brief The device state change notification thread */
  173. static pthread_t change_thread = AST_PTHREADT_NULL;
  174. /*! \brief Flag for the queue */
  175. static ast_cond_t change_pending;
  176. struct devstate_change {
  177. AST_LIST_ENTRY(devstate_change) entry;
  178. uint32_t state;
  179. struct ast_eid eid;
  180. enum ast_devstate_cache cachable;
  181. char device[1];
  182. };
  183. static struct {
  184. pthread_t thread;
  185. struct ast_event_sub *event_sub;
  186. ast_cond_t cond;
  187. ast_mutex_t lock;
  188. AST_LIST_HEAD_NOLOCK(, devstate_change) devstate_change_q;
  189. unsigned int enabled:1;
  190. } devstate_collector = {
  191. .thread = AST_PTHREADT_NULL,
  192. .enabled = 0,
  193. };
  194. /* Forward declarations */
  195. static int getproviderstate(const char *provider, const char *address);
  196. /*! \brief Find devicestate as text message for output */
  197. const char *ast_devstate2str(enum ast_device_state devstate)
  198. {
  199. return devstatestring[devstate][0];
  200. }
  201. /* Deprecated interface (not prefixed with ast_) */
  202. const char *devstate2str(enum ast_device_state devstate)
  203. {
  204. return devstatestring[devstate][0];
  205. }
  206. enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
  207. {
  208. int i;
  209. chanstate &= 0xFFFF;
  210. for (i = 0; chan2dev[i].chan != -100; i++) {
  211. if (chan2dev[i].chan == chanstate) {
  212. return chan2dev[i].dev;
  213. }
  214. }
  215. return AST_DEVICE_UNKNOWN;
  216. }
  217. /* Parseable */
  218. const char *ast_devstate_str(enum ast_device_state state)
  219. {
  220. return devstatestring[state][1];
  221. }
  222. enum ast_device_state ast_devstate_val(const char *val)
  223. {
  224. if (!strcasecmp(val, "NOT_INUSE"))
  225. return AST_DEVICE_NOT_INUSE;
  226. else if (!strcasecmp(val, "INUSE"))
  227. return AST_DEVICE_INUSE;
  228. else if (!strcasecmp(val, "BUSY"))
  229. return AST_DEVICE_BUSY;
  230. else if (!strcasecmp(val, "INVALID"))
  231. return AST_DEVICE_INVALID;
  232. else if (!strcasecmp(val, "UNAVAILABLE"))
  233. return AST_DEVICE_UNAVAILABLE;
  234. else if (!strcasecmp(val, "RINGING"))
  235. return AST_DEVICE_RINGING;
  236. else if (!strcasecmp(val, "RINGINUSE"))
  237. return AST_DEVICE_RINGINUSE;
  238. else if (!strcasecmp(val, "ONHOLD"))
  239. return AST_DEVICE_ONHOLD;
  240. return AST_DEVICE_UNKNOWN;
  241. }
  242. /*! \brief Find out if device is active in a call or not
  243. \note find channels with the device's name in it
  244. This function is only used for channels that does not implement
  245. devicestate natively
  246. */
  247. enum ast_device_state ast_parse_device_state(const char *device)
  248. {
  249. struct ast_channel *chan;
  250. char match[AST_CHANNEL_NAME];
  251. enum ast_device_state res;
  252. snprintf(match, sizeof(match), "%s-", device);
  253. if (!(chan = ast_channel_get_by_name_prefix(match, strlen(match)))) {
  254. return AST_DEVICE_UNKNOWN;
  255. }
  256. res = (ast_channel_state(chan) == AST_STATE_RINGING) ? AST_DEVICE_RINGING : AST_DEVICE_INUSE;
  257. chan = ast_channel_unref(chan);
  258. return res;
  259. }
  260. static enum ast_device_state devstate_cached(const char *device)
  261. {
  262. enum ast_device_state res = AST_DEVICE_UNKNOWN;
  263. struct ast_event *event;
  264. event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
  265. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
  266. AST_EVENT_IE_END);
  267. if (!event)
  268. return res;
  269. res = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
  270. ast_event_destroy(event);
  271. return res;
  272. }
  273. /*! \brief Check device state through channel specific function or generic function */
  274. static enum ast_device_state _ast_device_state(const char *device, int check_cache)
  275. {
  276. char *buf;
  277. char *number;
  278. const struct ast_channel_tech *chan_tech;
  279. enum ast_device_state res;
  280. /*! \brief Channel driver that provides device state */
  281. char *tech;
  282. /*! \brief Another provider of device state */
  283. char *provider = NULL;
  284. /* If the last known state is cached, just return that */
  285. if (check_cache) {
  286. res = devstate_cached(device);
  287. if (res != AST_DEVICE_UNKNOWN) {
  288. return res;
  289. }
  290. }
  291. buf = ast_strdupa(device);
  292. tech = strsep(&buf, "/");
  293. if (!(number = buf)) {
  294. provider = strsep(&tech, ":");
  295. if (!tech) {
  296. return AST_DEVICE_INVALID;
  297. }
  298. /* We have a provider */
  299. number = tech;
  300. tech = NULL;
  301. ast_debug(3, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number);
  302. return getproviderstate(provider, number);
  303. }
  304. ast_debug(4, "No provider found, checking channel drivers for %s - %s\n", tech, number);
  305. if (!(chan_tech = ast_get_channel_tech(tech)))
  306. return AST_DEVICE_INVALID;
  307. if (!(chan_tech->devicestate)) /* Does the channel driver support device state notification? */
  308. return ast_parse_device_state(device); /* No, try the generic function */
  309. res = chan_tech->devicestate(number);
  310. if (res != AST_DEVICE_UNKNOWN)
  311. return res;
  312. res = ast_parse_device_state(device);
  313. return res;
  314. }
  315. enum ast_device_state ast_device_state(const char *device)
  316. {
  317. /* This function is called from elsewhere in the code to find out the
  318. * current state of a device. Check the cache, first. */
  319. return _ast_device_state(device, 1);
  320. }
  321. /*! \brief Add device state provider */
  322. int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
  323. {
  324. struct devstate_prov *devprov;
  325. if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov))))
  326. return -1;
  327. devprov->callback = callback;
  328. ast_copy_string(devprov->label, label, sizeof(devprov->label));
  329. AST_RWLIST_WRLOCK(&devstate_provs);
  330. AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list);
  331. AST_RWLIST_UNLOCK(&devstate_provs);
  332. return 0;
  333. }
  334. /*! \brief Remove device state provider */
  335. int ast_devstate_prov_del(const char *label)
  336. {
  337. struct devstate_prov *devcb;
  338. int res = -1;
  339. AST_RWLIST_WRLOCK(&devstate_provs);
  340. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) {
  341. if (!strcasecmp(devcb->label, label)) {
  342. AST_RWLIST_REMOVE_CURRENT(list);
  343. ast_free(devcb);
  344. res = 0;
  345. break;
  346. }
  347. }
  348. AST_RWLIST_TRAVERSE_SAFE_END;
  349. AST_RWLIST_UNLOCK(&devstate_provs);
  350. return res;
  351. }
  352. /*! \brief Get provider device state */
  353. static int getproviderstate(const char *provider, const char *address)
  354. {
  355. struct devstate_prov *devprov;
  356. int res = AST_DEVICE_INVALID;
  357. AST_RWLIST_RDLOCK(&devstate_provs);
  358. AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
  359. ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider);
  360. if (!strcasecmp(devprov->label, provider)) {
  361. res = devprov->callback(address);
  362. break;
  363. }
  364. }
  365. AST_RWLIST_UNLOCK(&devstate_provs);
  366. return res;
  367. }
  368. static void devstate_event(const char *device, enum ast_device_state state, int cachable)
  369. {
  370. struct ast_event *event;
  371. enum ast_event_type event_type;
  372. if (devstate_collector.enabled) {
  373. /* Distributed device state is enabled, so this state change is a change
  374. * for a single server, not the real state. */
  375. event_type = AST_EVENT_DEVICE_STATE_CHANGE;
  376. } else {
  377. event_type = AST_EVENT_DEVICE_STATE;
  378. }
  379. ast_debug(3, "device '%s' state '%u'\n", device, state);
  380. if (!(event = ast_event_new(event_type,
  381. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
  382. AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
  383. AST_EVENT_IE_CACHABLE, AST_EVENT_IE_PLTYPE_UINT, cachable,
  384. AST_EVENT_IE_END))) {
  385. return;
  386. }
  387. if (cachable) {
  388. ast_event_queue_and_cache(event);
  389. } else {
  390. ast_event_queue(event);
  391. }
  392. }
  393. /*! Called by the state change thread to find out what the state is, and then
  394. * to queue up the state change event */
  395. static void do_state_change(const char *device, int cachable)
  396. {
  397. enum ast_device_state state;
  398. state = _ast_device_state(device, 0);
  399. ast_debug(3, "Changing state for %s - state %u (%s)\n", device, state, ast_devstate2str(state));
  400. devstate_event(device, state, cachable);
  401. }
  402. int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
  403. {
  404. struct state_change *change;
  405. /*
  406. * If we know the state change (how nice of the caller of this function!)
  407. * then we can just generate a device state event.
  408. *
  409. * Otherwise, we do the following:
  410. * - Queue an event up to another thread that the state has changed
  411. * - In the processing thread, it calls the callback provided by the
  412. * device state provider (which may or may not be a channel driver)
  413. * to determine the state.
  414. * - If the device state provider does not know the state, or this is
  415. * for a channel and the channel driver does not implement a device
  416. * state callback, then we will look through the channel list to
  417. * see if we can determine a state based on active calls.
  418. * - Once a state has been determined, a device state event is generated.
  419. */
  420. if (state != AST_DEVICE_UNKNOWN) {
  421. devstate_event(device, state, cachable);
  422. } else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
  423. /* we could not allocate a change struct, or */
  424. /* there is no background thread, so process the change now */
  425. do_state_change(device, cachable);
  426. } else {
  427. /* queue the change */
  428. strcpy(change->device, device);
  429. change->cachable = cachable;
  430. AST_LIST_LOCK(&state_changes);
  431. AST_LIST_INSERT_TAIL(&state_changes, change, list);
  432. ast_cond_signal(&change_pending);
  433. AST_LIST_UNLOCK(&state_changes);
  434. }
  435. return 0;
  436. }
  437. int ast_device_state_changed_literal(const char *dev)
  438. {
  439. return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, dev);
  440. }
  441. int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt, ...)
  442. {
  443. char buf[AST_MAX_EXTENSION];
  444. va_list ap;
  445. va_start(ap, fmt);
  446. vsnprintf(buf, sizeof(buf), fmt, ap);
  447. va_end(ap);
  448. return ast_devstate_changed_literal(state, cachable, buf);
  449. }
  450. int ast_device_state_changed(const char *fmt, ...)
  451. {
  452. char buf[AST_MAX_EXTENSION];
  453. va_list ap;
  454. va_start(ap, fmt);
  455. vsnprintf(buf, sizeof(buf), fmt, ap);
  456. va_end(ap);
  457. return ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, buf);
  458. }
  459. /*! \brief Go through the dev state change queue and update changes in the dev state thread */
  460. static void *do_devstate_changes(void *data)
  461. {
  462. struct state_change *next, *current;
  463. for (;;) {
  464. /* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
  465. AST_LIST_LOCK(&state_changes);
  466. if (AST_LIST_EMPTY(&state_changes))
  467. ast_cond_wait(&change_pending, &state_changes.lock);
  468. next = AST_LIST_FIRST(&state_changes);
  469. AST_LIST_HEAD_INIT_NOLOCK(&state_changes);
  470. AST_LIST_UNLOCK(&state_changes);
  471. /* Process each state change */
  472. while ((current = next)) {
  473. next = AST_LIST_NEXT(current, list);
  474. do_state_change(current->device, current->cachable);
  475. ast_free(current);
  476. }
  477. }
  478. return NULL;
  479. }
  480. static void destroy_devstate_change(struct devstate_change *sc)
  481. {
  482. ast_free(sc);
  483. }
  484. #define MAX_SERVERS 64
  485. struct change_collection {
  486. struct devstate_change states[MAX_SERVERS];
  487. size_t num_states;
  488. };
  489. static void devstate_cache_cb(const struct ast_event *event, void *data)
  490. {
  491. struct change_collection *collection = data;
  492. int i;
  493. const struct ast_eid *eid;
  494. if (collection->num_states == ARRAY_LEN(collection->states)) {
  495. ast_log(LOG_ERROR, "More per-server state values than we have room for (MAX_SERVERS is %d)\n",
  496. MAX_SERVERS);
  497. return;
  498. }
  499. if (!(eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID))) {
  500. ast_log(LOG_ERROR, "Device state change event with no EID\n");
  501. return;
  502. }
  503. i = collection->num_states;
  504. collection->states[i].state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
  505. collection->states[i].eid = *eid;
  506. collection->num_states++;
  507. }
  508. static void process_collection(const char *device, enum ast_devstate_cache cachable, struct change_collection *collection)
  509. {
  510. int i;
  511. struct ast_devstate_aggregate agg;
  512. enum ast_device_state state;
  513. struct ast_event *event;
  514. ast_devstate_aggregate_init(&agg);
  515. for (i = 0; i < collection->num_states; i++) {
  516. ast_debug(1, "Adding per-server state of '%s' for '%s'\n",
  517. ast_devstate2str(collection->states[i].state), device);
  518. ast_devstate_aggregate_add(&agg, collection->states[i].state);
  519. }
  520. state = ast_devstate_aggregate_result(&agg);
  521. ast_debug(1, "Aggregate devstate result is '%s' for '%s'\n",
  522. ast_devstate2str(state), device);
  523. event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
  524. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
  525. AST_EVENT_IE_END);
  526. if (event) {
  527. enum ast_device_state old_state;
  528. old_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
  529. ast_event_destroy(event);
  530. if (state == old_state) {
  531. /* No change since last reported device state */
  532. ast_debug(1, "Aggregate state for device '%s' has not changed from '%s'\n",
  533. device, ast_devstate2str(state));
  534. return;
  535. }
  536. }
  537. ast_debug(1, "Aggregate state for device '%s' has changed to '%s'\n",
  538. device, ast_devstate2str(state));
  539. event = ast_event_new(AST_EVENT_DEVICE_STATE,
  540. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
  541. AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
  542. AST_EVENT_IE_END);
  543. if (!event) {
  544. return;
  545. }
  546. if (cachable) {
  547. ast_event_queue_and_cache(event);
  548. } else {
  549. ast_event_queue(event);
  550. }
  551. }
  552. static void handle_devstate_change(struct devstate_change *sc)
  553. {
  554. struct ast_event_sub *tmp_sub;
  555. struct change_collection collection = {
  556. .num_states = 0,
  557. };
  558. ast_debug(1, "Processing device state change for '%s'\n", sc->device);
  559. if (!(tmp_sub = ast_event_subscribe_new(AST_EVENT_DEVICE_STATE_CHANGE, devstate_cache_cb, &collection))) {
  560. ast_log(LOG_ERROR, "Failed to create subscription\n");
  561. return;
  562. }
  563. if (ast_event_sub_append_ie_str(tmp_sub, AST_EVENT_IE_DEVICE, sc->device)) {
  564. ast_log(LOG_ERROR, "Failed to append device IE\n");
  565. ast_event_sub_destroy(tmp_sub);
  566. return;
  567. }
  568. /* Populate the collection of device states from the cache */
  569. ast_event_dump_cache(tmp_sub);
  570. process_collection(sc->device, sc->cachable, &collection);
  571. ast_event_sub_destroy(tmp_sub);
  572. }
  573. static void *run_devstate_collector(void *data)
  574. {
  575. for (;;) {
  576. struct devstate_change *sc;
  577. ast_mutex_lock(&devstate_collector.lock);
  578. while (!(sc = AST_LIST_REMOVE_HEAD(&devstate_collector.devstate_change_q, entry)))
  579. ast_cond_wait(&devstate_collector.cond, &devstate_collector.lock);
  580. ast_mutex_unlock(&devstate_collector.lock);
  581. handle_devstate_change(sc);
  582. destroy_devstate_change(sc);
  583. }
  584. return NULL;
  585. }
  586. static void devstate_change_collector_cb(const struct ast_event *event, void *data)
  587. {
  588. struct devstate_change *sc;
  589. const char *device, *cachable_str;
  590. const struct ast_eid *eid;
  591. uint32_t state;
  592. enum ast_devstate_cache cachable = AST_DEVSTATE_CACHABLE;
  593. device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
  594. eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID);
  595. state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
  596. if (ast_strlen_zero(device) || !eid) {
  597. ast_log(LOG_ERROR, "Invalid device state change event received\n");
  598. return;
  599. }
  600. if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(device))))
  601. return;
  602. strcpy(sc->device, device);
  603. sc->eid = *eid;
  604. sc->state = state;
  605. /* For 'cachable' we cannot use ast_event_get_ie_uint(), it overwrites the default of AST_DEVSTATE_CACHABLE we
  606. * have already setup for 'cachable', if for whatever reason the AST_EVENT_IE_CACHABLE wasn't
  607. * posted in the event ast_event_get_ie_uint() is going will return 0,
  608. * which equates to AST_DEVSTATE_NOT_CACHABLE the first enumeration in 'ast_devstate_cache'.
  609. */
  610. if ((cachable_str = ast_event_get_ie_str(event, AST_EVENT_IE_CACHABLE))) {
  611. sscanf(cachable_str, "%30u", &cachable);
  612. }
  613. sc->cachable = cachable;
  614. ast_mutex_lock(&devstate_collector.lock);
  615. AST_LIST_INSERT_TAIL(&devstate_collector.devstate_change_q, sc, entry);
  616. ast_cond_signal(&devstate_collector.cond);
  617. ast_mutex_unlock(&devstate_collector.lock);
  618. }
  619. /*! \brief Initialize the device state engine in separate thread */
  620. int ast_device_state_engine_init(void)
  621. {
  622. ast_cond_init(&change_pending, NULL);
  623. if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
  624. ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
  625. return -1;
  626. }
  627. return 0;
  628. }
  629. void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
  630. {
  631. memset(agg, 0, sizeof(*agg));
  632. agg->state = AST_DEVICE_INVALID;
  633. }
  634. void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
  635. {
  636. static enum ast_device_state state_order[] = {
  637. 1, /* AST_DEVICE_UNKNOWN */
  638. 3, /* AST_DEVICE_NOT_INUSE */
  639. 6, /* AST_DEVICE_INUSE */
  640. 7, /* AST_DEVICE_BUSY */
  641. 0, /* AST_DEVICE_INVALID */
  642. 2, /* AST_DEVICE_UNAVAILABLE */
  643. 5, /* AST_DEVICE_RINGING */
  644. 8, /* AST_DEVICE_RINGINUSE */
  645. 4, /* AST_DEVICE_ONHOLD */
  646. };
  647. if (state == AST_DEVICE_RINGING) {
  648. agg->ringing = 1;
  649. } else if (state == AST_DEVICE_INUSE || state == AST_DEVICE_ONHOLD || state == AST_DEVICE_BUSY) {
  650. agg->inuse = 1;
  651. }
  652. if (agg->ringing && agg->inuse) {
  653. agg->state = AST_DEVICE_RINGINUSE;
  654. } else if (state_order[state] > state_order[agg->state]) {
  655. agg->state = state;
  656. }
  657. }
  658. enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
  659. {
  660. return agg->state;
  661. }
  662. int ast_enable_distributed_devstate(void)
  663. {
  664. if (devstate_collector.enabled) {
  665. return 0;
  666. }
  667. devstate_collector.event_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
  668. devstate_change_collector_cb, "devicestate_engine_enable_distributed", NULL, AST_EVENT_IE_END);
  669. if (!devstate_collector.event_sub) {
  670. ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n");
  671. return -1;
  672. }
  673. ast_mutex_init(&devstate_collector.lock);
  674. ast_cond_init(&devstate_collector.cond, NULL);
  675. if (ast_pthread_create_background(&devstate_collector.thread, NULL, run_devstate_collector, NULL) < 0) {
  676. ast_log(LOG_ERROR, "Unable to start device state collector thread.\n");
  677. return -1;
  678. }
  679. devstate_collector.enabled = 1;
  680. return 0;
  681. }