various devicestate fixes (issue #5081, take two)
[asterisk/asterisk.git] / devicestate.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Device state management
5  * 
6  * Copyright (C) 2005, Digium, Inc.
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "asterisk.h"
18
19 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
20
21 #include "asterisk/channel.h"
22 #include "asterisk/utils.h"
23 #include "asterisk/lock.h"
24 #include "asterisk/linkedlists.h"
25 #include "asterisk/logger.h"
26 #include "asterisk/devicestate.h"
27 #include "asterisk/pbx.h"
28 #include "asterisk/options.h"
29
30 static const char *devstatestring[] = {
31         /* 0 AST_DEVICE_UNKNOWN */      "Unknown",      /* Valid, but unknown state */
32         /* 1 AST_DEVICE_NOT_INUSE */    "Not in use",   /* Not used */
33         /* 2 AST_DEVICE IN USE */       "In use",       /* In use */
34         /* 3 AST_DEVICE_BUSY */         "Busy",         /* Busy */
35         /* 4 AST_DEVICE_INVALID */      "Invalid",      /* Invalid - not known to Asterisk */
36         /* 5 AST_DEVICE_UNAVAILABLE */  "Unavailable",  /* Unavailable (not registred) */
37         /* 6 AST_DEVICE_RINGING */      "Ringing"       /* Ring, ring, ring */
38 };
39
40 /* ast_devstate_cb: A device state watcher (callback) */
41 struct devstate_cb {
42         void *data;
43         ast_devstate_cb_type callback;
44         AST_LIST_ENTRY(devstate_cb) list;
45 };
46
47 static AST_LIST_HEAD_STATIC(devstate_cbs, devstate_cb);
48
49 struct state_change {
50         AST_LIST_ENTRY(state_change) list;
51         char device[1];
52 };
53
54 static AST_LIST_HEAD_STATIC(state_changes, state_change);
55
56 static pthread_t change_thread = AST_PTHREADT_NULL;
57 static pthread_cond_t change_pending;
58
59 /*--- devstate2str: Find devicestate as text message for output */
60 const char *devstate2str(int devstate) 
61 {
62         return devstatestring[devstate];
63 }
64
65 /*--- ast_parse_device_state: Find out if device is active in a call or not */
66 int ast_parse_device_state(const char *device)
67 {
68         struct ast_channel *chan;
69         char match[AST_CHANNEL_NAME];
70         int res;
71
72         ast_copy_string(match, device, sizeof(match)-1);
73         strcat(match, "-");
74         chan = ast_get_channel_by_name_prefix_locked(match, strlen(match));
75
76         if (!chan)
77                 return AST_DEVICE_UNKNOWN;
78
79         if (chan->_state == AST_STATE_RINGING)
80                 res = AST_DEVICE_RINGING;
81         else
82                 res = AST_DEVICE_INUSE;
83         
84         ast_mutex_unlock(&chan->lock);
85
86         return res;
87 }
88
89 /*--- ast_device_state: Check device state through channel specific function or generic function */
90 int ast_device_state(const char *device)
91 {
92         char *buf;
93         char *tech;
94         char *number;
95         const struct ast_channel_tech *chan_tech;
96         int res = 0;
97         
98         buf = ast_strdupa(device);
99         tech = strsep(&buf, "/");
100         number = buf;
101         if (!number)
102                 return AST_DEVICE_INVALID;
103                 
104         chan_tech = ast_get_channel_tech(tech);
105         if (!chan_tech)
106                 return AST_DEVICE_INVALID;
107
108         if (!chan_tech->devicestate)    /* Does the channel driver support device state notification? */
109                 return ast_parse_device_state(device);  /* No, try the generic function */
110         else {
111                 res = chan_tech->devicestate(number);   /* Ask the channel driver for device state */
112                 if (res == AST_DEVICE_UNKNOWN) {
113                         res = ast_parse_device_state(device);
114                         /* at this point we know the device exists, but the channel driver
115                            could not give us a state; if there is no channel state available,
116                            it must be 'not in use'
117                         */
118                         if (res == AST_DEVICE_UNKNOWN)
119                                 res = AST_DEVICE_NOT_INUSE;
120                         return res;
121                 } else
122                         return res;
123         }
124 }
125
126 /*--- ast_devstate_add: Add device state watcher */
127 int ast_devstate_add(ast_devstate_cb_type callback, void *data)
128 {
129         struct devstate_cb *devcb;
130
131         if (!callback)
132                 return -1;
133
134         devcb = calloc(1, sizeof(*devcb));
135         if (!devcb)
136                 return -1;
137
138         devcb->data = data;
139         devcb->callback = callback;
140
141         AST_LIST_LOCK(&devstate_cbs);
142         AST_LIST_INSERT_HEAD(&devstate_cbs, devcb, list);
143         AST_LIST_UNLOCK(&devstate_cbs);
144
145         return 0;
146 }
147
148 /*--- ast_devstate_del: Remove device state watcher */
149 void ast_devstate_del(ast_devstate_cb_type callback, void *data)
150 {
151         struct devstate_cb *devcb;
152
153         AST_LIST_LOCK(&devstate_cbs);
154         AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_cbs, devcb, list) {
155                 if ((devcb->callback == callback) && (devcb->data == data)) {
156                         AST_LIST_REMOVE_CURRENT(&devstate_cbs, list);
157                         free(devcb);
158                         break;
159                 }
160         }
161         AST_LIST_TRAVERSE_SAFE_END;
162         AST_LIST_UNLOCK(&devstate_cbs);
163 }
164
165 /*--- do_state_change: Notify callback watchers of change, and notify PBX core for hint updates */
166 static void do_state_change(const char *device)
167 {
168         int state;
169         struct devstate_cb *devcb;
170
171         state = ast_device_state(device);
172         if (option_debug > 2)
173                 ast_log(LOG_DEBUG, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
174
175         AST_LIST_LOCK(&devstate_cbs);
176         AST_LIST_TRAVERSE(&devstate_cbs, devcb, list)
177                 devcb->callback(device, state, devcb->data);
178         AST_LIST_UNLOCK(&devstate_cbs);
179
180         ast_hint_state_changed(device);
181 }
182
183 /*--- ast_device_state_changed: Accept change notification, add it to change queue */
184 int ast_device_state_changed(const char *fmt, ...) 
185 {
186         char buf[AST_MAX_EXTENSION];
187         char *device;
188         char *parse;
189         struct state_change *change = NULL;
190         va_list ap;
191
192         va_start(ap, fmt);
193         vsnprintf(buf, sizeof(buf), fmt, ap);
194         va_end(ap);
195
196         parse = buf;
197         device = strsep(&parse, "-");
198
199         if (change_thread != AST_PTHREADT_NULL)
200                 change = calloc(1, sizeof(*change) + strlen(device));
201
202         if (!change) {
203                 /* we could not allocate a change struct, or */
204                 /* there is no background thread, so process the change now */
205                 do_state_change(device);
206         } else {
207                 /* queue the change */
208                 strcpy(change->device, device);
209                 AST_LIST_LOCK(&state_changes);
210                 AST_LIST_INSERT_TAIL(&state_changes, change, list);
211                 if (AST_LIST_FIRST(&state_changes) == change)
212                         /* the list was empty, signal the thread */
213                         pthread_cond_signal(&change_pending);
214                 AST_LIST_UNLOCK(&state_changes);
215         }
216
217         return 1;
218 }
219
220 /*--- do_devstate_changes: Go through the dev state change queue and update changes in the dev state thread */
221 static void *do_devstate_changes(void *data)
222 {
223         struct state_change *cur;
224
225         AST_LIST_LOCK(&state_changes);
226         for(;;) {
227                 /* the list lock will _always_ be held at this point in the loop */
228                 cur = AST_LIST_REMOVE_HEAD(&state_changes, list);
229                 if (cur) {
230                         /* we got an entry, so unlock the list while we process it */
231                         AST_LIST_UNLOCK(&state_changes);
232                         do_state_change(cur->device);
233                         free(cur);
234                         AST_LIST_LOCK(&state_changes);
235                 } else {
236                         /* there was no entry, so atomically unlock the list and wait for
237                            the condition to be signalled (returns with the lock held) */
238                         ast_pthread_cond_wait(&change_pending, &state_changes.lock);
239                 }
240         }
241
242         return NULL;
243 }
244
245 /*--- ast_device_state_engine_init: Initialize the device state engine in separate thread */
246 int ast_device_state_engine_init(void)
247 {
248         pthread_attr_t attr;
249
250         pthread_cond_init(&change_pending, NULL);
251         pthread_attr_init(&attr);
252         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
253         if (ast_pthread_create(&change_thread, &attr, do_devstate_changes, NULL) < 0) {
254                 ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
255                 return -1;
256         }
257
258         return 0;
259 }