res/stasis/command.c: Fix recent commit using spaces instead of tabs.
[asterisk/asterisk.git] / res / stasis / command.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@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
19 /*! \file
20  *
21  * \brief Stasis application command support.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "command.h"
31
32 #include "asterisk/lock.h"
33 #include "asterisk/stasis_app_impl.h"
34
35 struct stasis_app_command {
36         ast_mutex_t lock;
37         ast_cond_t condition;
38         stasis_app_command_cb callback;
39         void *data;
40         int retval;
41         int is_done:1;
42 };
43
44 static void command_dtor(void *obj)
45 {
46         struct stasis_app_command *command = obj;
47         ast_mutex_destroy(&command->lock);
48         ast_cond_destroy(&command->condition);
49 }
50
51 struct stasis_app_command *command_create(
52         stasis_app_command_cb callback, void *data)
53 {
54         RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
55
56         command = ao2_alloc(sizeof(*command), command_dtor);
57         if (!command) {
58                 return NULL;
59         }
60
61         ast_mutex_init(&command->lock);
62         ast_cond_init(&command->condition, 0);
63         command->callback = callback;
64         command->data = data;
65
66         ao2_ref(command, +1);
67         return command;
68 }
69
70 void command_complete(struct stasis_app_command *command, int retval)
71 {
72         SCOPED_MUTEX(lock, &command->lock);
73
74         command->is_done = 1;
75         command->retval = retval;
76         ast_cond_signal(&command->condition);
77 }
78
79 int command_join(struct stasis_app_command *command)
80 {
81         SCOPED_MUTEX(lock, &command->lock);
82         while (!command->is_done) {
83                 ast_cond_wait(&command->condition, &command->lock);
84         }
85
86         return command->retval;
87 }
88
89 void command_invoke(struct stasis_app_command *command,
90         struct stasis_app_control *control, struct ast_channel *chan)
91 {
92         int retval = command->callback(control, chan, command->data);
93         command_complete(command, retval);
94 }
95
96 static void command_queue_prestart_destroy(void *obj)
97 {
98         /* Clean up the container */
99         ao2_cleanup(obj);
100 }
101
102 static const struct ast_datastore_info command_queue_prestart = {
103         .type = "stasis-command-prestart-queue",
104         .destroy = command_queue_prestart_destroy,
105 };
106
107 int command_prestart_queue_command(struct ast_channel *chan,
108         stasis_app_command_cb command_fn, void *data)
109 {
110         struct ast_datastore *datastore;
111         struct ao2_container *command_queue;
112         RAII_VAR(struct stasis_app_command *, command,
113                 command_create(command_fn, data), ao2_cleanup);
114
115         if (!command) {
116                 return -1;
117         }
118
119         datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL);
120         if (datastore) {
121                 command_queue = datastore->data;
122                 ao2_link(command_queue, command);
123                 return 0;
124         }
125
126         command_queue = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
127         if (!command_queue) {
128                 return -1;
129         }
130
131         datastore = ast_datastore_alloc(&command_queue_prestart, NULL);
132         if (!datastore) {
133                 ao2_cleanup(command_queue);
134                 return -1;
135         }
136         ast_channel_datastore_add(chan, datastore);
137
138         datastore->data = command_queue;
139         ao2_link(command_queue, command);
140
141         return 0;
142 }
143
144 struct ao2_container *command_prestart_get_container(struct ast_channel *chan)
145 {
146         struct ast_datastore *datastore = ast_channel_datastore_find(chan, &command_queue_prestart, NULL);
147
148         if (!datastore) {
149                 return NULL;
150         }
151
152         return ao2_bump(datastore->data);
153 }