forked from wdoekes/asterisk-chan-dongle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
226 lines (185 loc) · 4.95 KB
/
app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Required outside the BUILD_APPLICATIONS #ifdef! */
#include "ast_config.h"
#ifdef BUILD_APPLICATIONS
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
bg <[email protected]>
*/
#include <asterisk/app.h> /* AST_DECLARE_APP_ARGS() ... */
#include <asterisk/pbx.h> /* pbx_builtin_setvar_helper() */
#include <asterisk/module.h> /* ast_register_application2() ast_unregister_application() */
#include "ast_compat.h" /* asterisk compatibility fixes */
#include "app.h" /* app_register() app_unregister() */
#include "chan_dongle.h" /* struct pvt */
#include "helpers.h" /* send_sms() ITEMS_OF() */
#include "error.h"
struct ast_channel;
static int app_status_exec (struct ast_channel* channel, const char* data)
{
struct pvt * pvt;
char * parse;
int stat;
char status[2];
int exists = 0;
AST_DECLARE_APP_ARGS (args,
AST_APP_ARG (resource);
AST_APP_ARG (variable);
);
if (ast_strlen_zero (data))
{
return -1;
}
parse = ast_strdupa (data);
AST_STANDARD_APP_ARGS (args, parse);
if (ast_strlen_zero (args.resource) || ast_strlen_zero (args.variable))
{
return -1;
}
/* TODO: including options number */
pvt = find_device_by_resource(args.resource, 0, NULL, &exists);
if(pvt)
{
/* ready for outgoing call */
ast_mutex_unlock (&pvt->lock);
stat = 2;
}
else
{
stat = exists ? 3 : 1;
}
snprintf (status, sizeof (status), "%d", stat);
pbx_builtin_setvar_helper (channel, args.variable, status);
return 0;
}
static int app_send_sms_exec (attribute_unused struct ast_channel* channel, const char* data)
{
char* parse;
AST_DECLARE_APP_ARGS (args,
AST_APP_ARG (device);
AST_APP_ARG (number);
AST_APP_ARG (message);
AST_APP_ARG (validity);
AST_APP_ARG (report);
AST_APP_ARG (payload);
);
if (ast_strlen_zero (data))
{
return -1;
}
parse = ast_strdupa (data);
AST_STANDARD_APP_ARGS (args, parse);
if (ast_strlen_zero (args.device))
{
ast_log (LOG_ERROR, "NULL device for message -- SMS will not be sent\n");
return -1;
}
if (ast_strlen_zero (args.number))
{
ast_log (LOG_ERROR, "NULL destination for message -- SMS will not be sent\n");
return -1;
}
if (ast_strlen_zero(args.payload))
{
ast_log(LOG_ERROR, "NULL payload for message -- SMS will not be sent\n");
return -1;
}
if (send_sms(args.device, args.number, args.message, args.validity, args.report, args.payload, strlen(args.payload) + 1) < 0) {
ast_log(LOG_ERROR, "[%s] %s\n", args.device, error2str(chan_dongle_err));
return -1;
}
return 0;
}
static int app_send_ussd_exec(attribute_unused struct ast_channel* channel, const char* data)
{
char* parse;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(device);
AST_APP_ARG(ussd);
);
if (ast_strlen_zero(data))
{
return -1;
}
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (ast_strlen_zero(args.device))
{
ast_log(LOG_ERROR, "NULL device for ussd -- USSD will not be sent\n");
return -1;
}
if (ast_strlen_zero(args.ussd))
{
ast_log(LOG_ERROR, "NULL ussd command -- USSD will not be sent\n");
return -1;
}
if (send_ussd(args.device, args.ussd) < 0) {
ast_log(LOG_ERROR, "[%s] %s\n", args.device, error2str(chan_dongle_err));
return -1;
}
return 0;
}
static const struct dongle_application
{
const char* name;
int (*func)(struct ast_channel* channel, const char* data);
const char* synopsis;
const char* desc;
} dca[] =
{
{
"DongleStatus",
app_status_exec,
"DongleStatus(Resource,Variable)",
"DongleStatus(Resource,Variable)\n"
" Resource - Resource string as for Dial()\n"
" Variable - Variable to store status in will be 1-3.\n"
" In order, Disconnected, Connected & Free, Connected & Busy.\n"
},
{
"DongleSendSMS",
app_send_sms_exec,
"DongleSendSMS(Device,Dest,Message,Validity,Report,Payload)",
"DongleSendSMS(Device,Dest,Message,Validity,Report,Payload)\n"
" Device - Id of device from dongle.conf\n"
" Dest - destination\n"
" Message - text of the message\n"
" Validity - Validity period in minutes\n"
" Report - Boolean flag for report request\n"
" Payload - Unstructured data that will be included in delivery report\n"
},
{
"DongleSendUSSD",
app_send_ussd_exec,
"DongleSendUSSD(Device,USSD)",
"DongleSendUSSD(Device,USSD)\n"
" Device - Id of device from dongle.conf\n"
" USSD - ussd command\n"
}
};
#if ASTERISK_VERSION_NUM >= 10800 /* 1.8+ */
typedef int (*app_func_t)(struct ast_channel *channel, const char *data);
#else /* 1.8- */
typedef int (*app_func_t)(struct ast_channel *channel, void *data);
#endif /* ^1.8- */
#/* */
EXPORT_DEF void app_register()
{
unsigned i;
for(i = 0; i < ITEMS_OF(dca); i++)
{
ast_register_application2 (dca[i].name, (app_func_t)(dca[i].func), dca[i].synopsis, dca[i].desc, self_module());
}
}
#/* */
EXPORT_DEF void app_unregister()
{
int i;
for(i = ITEMS_OF(dca)-1; i >= 0; i--)
{
ast_unregister_application(dca[i].name);
}
}
#endif /* BUILD_APPLICATIONS */