[asterisk-commits] russell: branch russell/events r121506 - /team/russell/events/main/devicestate.c
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Jun 10 09:13:52 CDT 2008
Author: russell
Date: Tue Jun 10 09:13:51 2008
New Revision: 121506
URL: http://svn.digium.com/view/asterisk?view=rev&rev=121506
Log:
remove duplicates
Modified:
team/russell/events/main/devicestate.c
Modified: team/russell/events/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/devicestate.c?view=diff&rev=121506&r1=121505&r2=121506
==============================================================================
--- team/russell/events/main/devicestate.c (original)
+++ team/russell/events/main/devicestate.c Tue Jun 10 09:13:51 2008
@@ -601,6 +601,162 @@
collection->num_states++;
}
+static void process_collection(const char *device, struct change_collection *collection)
+{
+ int i;
+ struct ast_devstate_aggregate agg;
+ enum ast_device_state state;
+ struct ast_event *event;
+
+ ast_devstate_aggregate_init(&agg);
+
+ for (i = 0; i < collection->num_states; i++) {
+ ast_debug(1, "Adding per-server state of '%s' for '%s'\n",
+ devstate2str(collection->states[i].state), device);
+ ast_devstate_aggregate_add(&agg, collection->states[i].state);
+ }
+
+ state = ast_devstate_aggregate_result(&agg);
+
+ ast_debug(1, "Aggregate devstate result is %d\n", state);
+
+ event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
+ AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
+ AST_EVENT_IE_END);
+
+ if (event) {
+ enum ast_device_state old_state;
+
+ old_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
+
+ ast_event_destroy(event);
+
+ if (state == old_state) {
+ /* No change since last reported device state */
+ ast_debug(1, "Aggregate state for device '%s' has not changed from '%s'\n",
+ device, devstate2str(state));
+ return;
+ }
+ }
+
+ ast_debug(1, "Aggregate state for device '%s' has changed to '%s'\n",
+ device, devstate2str(state));
+
+ event = ast_event_new(AST_EVENT_DEVICE_STATE,
+ AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
+ AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
+ AST_EVENT_IE_END);
+
+ if (!event)
+ return;
+
+ ast_event_queue_and_cache(event,
+ AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
+ AST_EVENT_IE_END);
+}
+
+static void handle_devstate_change(struct devstate_change *sc)
+{
+ struct ast_event_sub *tmp_sub;
+ struct change_collection collection = {
+ .num_states = 0,
+ };
+
+ ast_debug(1, "Processing device state change for '%s'\n", sc->device);
+
+ if (!(tmp_sub = ast_event_subscribe_new(AST_EVENT_DEVICE_STATE_CHANGE, devstate_cache_cb, &collection))) {
+ ast_log(LOG_ERROR, "Failed to create subscription\n");
+ return;
+ }
+
+ if (ast_event_sub_append_ie_str(tmp_sub, AST_EVENT_IE_DEVICE, sc->device)) {
+ ast_log(LOG_ERROR, "Failed to append device IE\n");
+ ast_event_sub_destroy(tmp_sub);
+ return;
+ }
+
+ /* Populate the collection of device states from the cache */
+ ast_event_dump_cache(tmp_sub);
+
+ process_collection(sc->device, &collection);
+
+ ast_event_sub_destroy(tmp_sub);
+}
+
+static void *run_devstate_collector(void *data)
+{
+ for (;;) {
+ struct devstate_change *sc;
+
+ ast_mutex_lock(&devstate_collector.lock);
+ while (!(sc = AST_LIST_REMOVE_HEAD(&devstate_collector.devstate_change_q, entry)))
+ ast_cond_wait(&devstate_collector.cond, &devstate_collector.lock);
+ ast_mutex_unlock(&devstate_collector.lock);
+
+ handle_devstate_change(sc);
+
+ destroy_devstate_change(sc);
+ }
+
+ return NULL;
+}
+
+static void devstate_change_collector_cb(const struct ast_event *event, void *data)
+{
+ struct devstate_change *sc;
+ const char *device;
+ const struct ast_eid *eid;
+ uint32_t state;
+
+ device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
+ eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID);
+ state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
+
+ if (ast_strlen_zero(device) || !eid) {
+ ast_log(LOG_ERROR, "Invalid device state change event received\n");
+ return;
+ }
+
+ if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(device))))
+ return;
+
+ strcpy(sc->device, device);
+ sc->eid = *eid;
+ sc->state = state;
+
+ ast_mutex_lock(&devstate_collector.lock);
+ AST_LIST_INSERT_TAIL(&devstate_collector.devstate_change_q, sc, entry);
+ ast_cond_signal(&devstate_collector.cond);
+ ast_mutex_unlock(&devstate_collector.lock);
+}
+
+/*! \brief Initialize the device state engine in separate thread */
+int ast_device_state_engine_init(void)
+{
+ devstate_collector.event_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
+ devstate_change_collector_cb, NULL, AST_EVENT_IE_END);
+
+ if (!devstate_collector.event_sub) {
+ ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n");
+ return -1;
+ }
+
+ ast_mutex_init(&devstate_collector.lock);
+ ast_cond_init(&devstate_collector.cond, NULL);
+ if (ast_pthread_create_background(&devstate_collector.thread, NULL, run_devstate_collector, NULL) < 0) {
+ ast_log(LOG_ERROR, "Unable to start device state collector thread.\n");
+ return -1;
+ }
+
+ ast_cond_init(&change_pending, NULL);
+ if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
+ ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
+ return -1;
+ }
+
+ return 0;
+}
+
void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
{
memset(agg, 0, sizeof(*agg));
@@ -688,246 +844,3 @@
return AST_DEVICE_NOT_INUSE;
}
-static void process_collection(const char *device, struct change_collection *collection)
-{
- int i;
- struct ast_devstate_aggregate agg;
- enum ast_device_state state;
- struct ast_event *event;
-
- ast_devstate_aggregate_init(&agg);
-
- for (i = 0; i < collection->num_states; i++) {
- ast_debug(1, "Adding per-server state of '%s' for '%s'\n",
- devstate2str(collection->states[i].state), device);
- ast_devstate_aggregate_add(&agg, collection->states[i].state);
- }
-
- state = ast_devstate_aggregate_result(&agg);
-
- ast_debug(1, "Aggregate devstate result is %d\n", state);
-
- event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
- AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
- AST_EVENT_IE_END);
-
- if (event) {
- enum ast_device_state old_state;
-
- old_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
-
- ast_event_destroy(event);
-
- if (state == old_state) {
- /* No change since last reported device state */
- ast_debug(1, "Aggregate state for device '%s' has not changed from '%s'\n",
- device, devstate2str(state));
- return;
- }
- }
-
- ast_debug(1, "Aggregate state for device '%s' has changed to '%s'\n",
- device, devstate2str(state));
-
- event = ast_event_new(AST_EVENT_DEVICE_STATE,
- AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
- AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
- AST_EVENT_IE_END);
-
- if (!event)
- return;
-
- ast_event_queue_and_cache(event,
- AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
- AST_EVENT_IE_END);
-}
-
-static void handle_devstate_change(struct devstate_change *sc)
-{
- struct ast_event_sub *tmp_sub;
- struct change_collection collection = {
- .num_states = 0,
- };
-
- ast_debug(1, "Processing device state change for '%s'\n", sc->device);
-
- if (!(tmp_sub = ast_event_subscribe_new(AST_EVENT_DEVICE_STATE_CHANGE, devstate_cache_cb, &collection))) {
- ast_log(LOG_ERROR, "Failed to create subscription\n");
- return;
- }
-
- if (ast_event_sub_append_ie_str(tmp_sub, AST_EVENT_IE_DEVICE, sc->device)) {
- ast_log(LOG_ERROR, "Failed to append device IE\n");
- ast_event_sub_destroy(tmp_sub);
- return;
- }
-
- /* Populate the collection of device states from the cache */
- ast_event_dump_cache(tmp_sub);
-
- process_collection(sc->device, &collection);
-
- ast_event_sub_destroy(tmp_sub);
-}
-
-static void *run_devstate_collector(void *data)
-{
- for (;;) {
- struct devstate_change *sc;
-
- ast_mutex_lock(&devstate_collector.lock);
- while (!(sc = AST_LIST_REMOVE_HEAD(&devstate_collector.devstate_change_q, entry)))
- ast_cond_wait(&devstate_collector.cond, &devstate_collector.lock);
- ast_mutex_unlock(&devstate_collector.lock);
-
- handle_devstate_change(sc);
-
- destroy_devstate_change(sc);
- }
-
- return NULL;
-}
-
-static void devstate_change_collector_cb(const struct ast_event *event, void *data)
-{
- struct devstate_change *sc;
- const char *device;
- const struct ast_eid *eid;
- uint32_t state;
-
- device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
- eid = ast_event_get_ie_raw(event, AST_EVENT_IE_EID);
- state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
-
- if (ast_strlen_zero(device) || !eid) {
- ast_log(LOG_ERROR, "Invalid device state change event received\n");
- return;
- }
-
- if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(device))))
- return;
-
- strcpy(sc->device, device);
- sc->eid = *eid;
- sc->state = state;
-
- ast_mutex_lock(&devstate_collector.lock);
- AST_LIST_INSERT_TAIL(&devstate_collector.devstate_change_q, sc, entry);
- ast_cond_signal(&devstate_collector.cond);
- ast_mutex_unlock(&devstate_collector.lock);
-}
-
-/*! \brief Initialize the device state engine in separate thread */
-int ast_device_state_engine_init(void)
-{
- devstate_collector.event_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
- devstate_change_collector_cb, NULL, AST_EVENT_IE_END);
-
- if (!devstate_collector.event_sub) {
- ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n");
- return -1;
- }
-
- ast_mutex_init(&devstate_collector.lock);
- ast_cond_init(&devstate_collector.cond, NULL);
- if (ast_pthread_create_background(&devstate_collector.thread, NULL, run_devstate_collector, NULL) < 0) {
- ast_log(LOG_ERROR, "Unable to start device state collector thread.\n");
- return -1;
- }
-
- ast_cond_init(&change_pending, NULL);
- if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
- ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
- return -1;
- }
-
- return 0;
-}
-
-void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
-{
- memset(agg, 0, sizeof(*agg));
-
- agg->all_unavail = 1;
- agg->all_busy = 1;
- agg->all_free = 1;
- agg->all_on_hold = 1;
-}
-
-void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
-{
- switch (state) {
- case AST_DEVICE_NOT_INUSE:
- agg->all_unavail = 0;
- agg->all_busy = 0;
- agg->all_on_hold = 0;
- break;
- case AST_DEVICE_INUSE:
- agg->in_use = 1;
- agg->all_busy = 0;
- agg->all_unavail = 0;
- agg->all_free = 0;
- agg->all_on_hold = 0;
- break;
- case AST_DEVICE_RINGING:
- agg->ring = 1;
- agg->all_busy = 0;
- agg->all_unavail = 0;
- agg->all_free = 0;
- agg->all_on_hold = 0;
- break;
- case AST_DEVICE_RINGINUSE:
- agg->in_use = 1;
- agg->ring = 1;
- agg->all_busy = 0;
- agg->all_unavail = 0;
- agg->all_free = 0;
- agg->all_on_hold = 0;
- break;
- case AST_DEVICE_ONHOLD:
- agg->all_unavail = 0;
- agg->all_free = 0;
- break;
- case AST_DEVICE_BUSY:
- agg->all_unavail = 0;
- agg->all_free = 0;
- agg->all_on_hold = 0;
- agg->busy = 1;
- break;
- case AST_DEVICE_UNAVAILABLE:
- case AST_DEVICE_INVALID:
- agg->all_busy = 0;
- agg->all_free = 0;
- agg->all_on_hold = 0;
- break;
- case AST_DEVICE_UNKNOWN:
- break;
- }
-}
-
-enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
-{
- if (agg->all_free)
- return AST_DEVICE_NOT_INUSE;
-
- if (agg->all_on_hold)
- return AST_DEVICE_ONHOLD;
-
- if (agg->all_busy)
- return AST_DEVICE_BUSY;
-
- if (agg->all_unavail)
- return AST_DEVICE_UNAVAILABLE;
-
- if (agg->ring)
- return agg->in_use ? AST_DEVICE_RINGINUSE : AST_DEVICE_RINGING;
-
- if (agg->in_use)
- return AST_DEVICE_INUSE;
-
- if (agg->busy)
- return AST_DEVICE_BUSY;
-
- return AST_DEVICE_NOT_INUSE;
-}
-
More information about the asterisk-commits
mailing list