add English device state output (bug #4733, with minor mod)
[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                         return ast_parse_device_state(device);
114                 else
115                         return res;
116         }
117 }
118
119 /*--- ast_devstate_add: Add device state watcher */
120 int ast_devstate_add(ast_devstate_cb_type callback, void *data)
121 {
122         struct devstate_cb *devcb;
123
124         if (!callback)
125                 return -1;
126
127         devcb = calloc(1, sizeof(*devcb));
128         if (!devcb)
129                 return -1;
130
131         devcb->data = data;
132         devcb->callback = callback;
133
134         AST_LIST_LOCK(&devstate_cbs);
135         AST_LIST_INSERT_HEAD(&devstate_cbs, devcb, list);
136         AST_LIST_UNLOCK(&devstate_cbs);
137
138         return 0;
139 }
140
141 /*--- ast_devstate_del: Remove device state watcher */
142 void ast_devstate_del(ast_devstate_cb_type callback, void *data)
143 {
144         struct devstate_cb *devcb;
145
146         AST_LIST_LOCK(&devstate_cbs);
147         AST_LIST_TRAVERSE_SAFE_BEGIN(&devstate_cbs, devcb, list) {
148                 if ((devcb->callback == callback) && (devcb->data == data)) {
149                         AST_LIST_REMOVE_CURRENT(&devstate_cbs, list);
150                         free(devcb);
151                         break;
152                 }
153         }
154         AST_LIST_TRAVERSE_SAFE_END;
155         AST_LIST_UNLOCK(&devstate_cbs);
156 }
157
158 /*--- do_state_change: Notify callback watchers of change, and notify PBX core for hint updates */
159 static void do_state_change(const char *device)
160 {
161         int state;
162         struct devstate_cb *devcb;
163
164         state = ast_device_state(device);
165         if (option_debug > 2)
166                 ast_log(LOG_DEBUG, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
167
168         AST_LIST_LOCK(&devstate_cbs);
169         AST_LIST_TRAVERSE(&devstate_cbs, devcb, list)
170                 devcb->callback(device, state, devcb->data);
171         AST_LIST_UNLOCK(&devstate_cbs);
172
173         ast_hint_state_changed(device);
174 }
175
176 /*--- ast_device_state_changed: Accept change notification, add it to change queue */
177 int ast_device_state_changed(const char *fmt, ...) 
178 {
179         char buf[AST_MAX_EXTENSION];
180         char *device;
181         char *parse;
182         struct state_change *change = NULL;
183         va_list ap;
184
185         va_start(ap, fmt);
186         vsnprintf(buf, sizeof(buf), fmt, ap);
187         va_end(ap);
188
189         parse = buf;
190         device = strsep(&parse, "-");
191
192         if (change_thread != AST_PTHREADT_NULL)
193                 change = calloc(1, sizeof(*change) + strlen(device));
194
195         if (!change) {
196                 /* we could not allocate a change struct, or */
197                 /* there is no background thread, so process the change now */
198                 do_state_change(device);
199         } else {
200                 /* queue the change */
201                 strcpy(change->device, device);
202                 AST_LIST_LOCK(&state_changes);
203                 AST_LIST_INSERT_TAIL(&state_changes, change, list);
204                 if (AST_LIST_FIRST(&state_changes) == change)
205                         /* the list was empty, signal the thread */
206                         pthread_cond_signal(&change_pending);
207                 AST_LIST_UNLOCK(&state_changes);
208         }
209
210         return 1;
211 }
212
213 /*--- do_devstate_changes: Go through the dev state change queue and update changes in the dev state thread */
214 static void *do_devstate_changes(void *data)
215 {
216         struct state_change *cur;
217
218         AST_LIST_LOCK(&state_changes);
219         for(;;) {
220                 /* the list lock will _always_ be held at this point in the loop */
221                 cur = AST_LIST_REMOVE_HEAD(&state_changes, list);
222                 if (cur) {
223                         /* we got an entry, so unlock the list while we process it */
224                         AST_LIST_UNLOCK(&state_changes);
225                         do_state_change(cur->device);
226                         free(cur);
227                         AST_LIST_LOCK(&state_changes);
228                 } else {
229                         /* there was no entry, so atomically unlock the list and wait for
230                            the condition to be signalled (returns with the lock held) */
231                         pthread_cond_wait(&change_pending, &state_changes.lock);
232                 }
233         }
234
235         return NULL;
236 }
237
238 /*--- ast_device_state_engine_init: Initialize the device state engine in separate thread */
239 int ast_device_state_engine_init(void)
240 {
241         pthread_attr_t attr;
242
243         pthread_cond_init(&change_pending, NULL);
244         pthread_attr_init(&attr);
245         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
246         if (ast_pthread_create(&change_thread, &attr, do_devstate_changes, NULL) < 0) {
247                 ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
248                 return -1;
249         }
250
251         return 0;
252 }