forked from t4rd15/asterisk-chan-quectel
-
Notifications
You must be signed in to change notification settings - Fork 34
/
helpers.c
204 lines (175 loc) · 4.21 KB
/
helpers.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
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
https://www.makhutov.org
Dmitry Vagin <[email protected]>
bg <[email protected]>
*/
#include "ast_config.h"
#include <signal.h> /* SIGURG */
#include <asterisk/callerid.h> /* AST_PRES_* */
#include "helpers.h"
#include "chan_quectel.h" /* devices */
#include "at_command.h"
#include "error.h"
// #include "pdu.h" /* pdu_digit2code() */
static int is_valid_ussd_string(const char* number)
{
for (; *number; number++) {
if ((*number >= '0' && *number <= '9') || *number == '*' || *number == '#') {
continue;
}
return 0;
}
return 1;
}
#/* */
EXPORT_DEF int is_valid_phone_number(const char *number)
{
if (number[0] == '+') {
number++;
}
for (; *number; number++) {
if (*number >= '0' && *number <= '9') {
continue;
}
return 0;
}
return 1;
}
#/* */
EXPORT_DEF int get_at_clir_value (struct pvt* pvt, int clir)
{
int res = 0;
switch (clir)
{
case AST_PRES_ALLOWED_NETWORK_NUMBER:
case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
case AST_PRES_NUMBER_NOT_AVAILABLE:
ast_debug (2, "[%s] callingpres: %s\n", PVT_ID(pvt), ast_describe_caller_presentation (clir));
res = 2;
break;
case AST_PRES_PROHIB_NETWORK_NUMBER:
case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
ast_debug (2, "[%s] callingpres: %s\n", PVT_ID(pvt), ast_describe_caller_presentation (clir));
res = 1;
break;
default:
ast_log (LOG_WARNING, "[%s] Unsupported callingpres: %d\n", PVT_ID(pvt), clir);
if ((clir & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED)
{
res = 0;
}
else
{
res = 2;
}
break;
}
return res;
}
typedef int (*at_cmd_f)(struct cpvt*, const char*, const char*, unsigned, int, const char*, size_t);
void free_pvt(struct pvt *pvt)
{
ast_mutex_unlock(&pvt->lock);
}
struct pvt *get_pvt(const char *dev_name, int online)
{
struct pvt *pvt;
pvt = find_device_ext(dev_name);
if (pvt) {
if (pvt->connected && (!online || (pvt->initialized && pvt->gsm_registered))) {
return pvt;
}
free_pvt(pvt);
}
chan_quectel_err = E_DEVICE_DISCONNECTED;
return NULL;
}
#/* */
EXPORT_DEF int send_ussd(const char *dev_name, const char *ussd)
{
if (!is_valid_ussd_string(ussd)) {
chan_quectel_err = E_INVALID_USSD;
return -1;
}
struct pvt *pvt = get_pvt(dev_name, 1);
if (!pvt) {
return -1;
}
int res = at_enqueue_ussd(&pvt->sys_chan, ussd);
free_pvt(pvt);
return res;
}
#/* */
EXPORT_DEF int send_sms(const char *dev_name, const char *number, const char *message, const char *validity, const char *report, const char *payload, size_t payload_len)
{
if (!is_valid_phone_number(number)) {
chan_quectel_err = E_INVALID_PHONE_NUMBER;
return -1;
}
int val = 0;
if (validity) {
val = strtol(validity, NULL, 10);
val = val <= 0 ? 0 : val;
}
int srr = !report ? 0 : ast_true(report);
struct pvt *pvt = get_pvt(dev_name, 1);
if (!pvt) {
return -1;
}
int res = at_enqueue_sms(&pvt->sys_chan, number, message, val, srr, payload, payload_len);
free_pvt(pvt);
return res;
}
#/* */
EXPORT_DEF int send_reset(const char *dev_name)
{
struct pvt *pvt = get_pvt(dev_name, 0);
if (!pvt) {
return -1;
}
int res = at_enqueue_reset(&pvt->sys_chan);
free_pvt(pvt);
return res;
}
#/* */
EXPORT_DEF int send_ccwa_set(const char *dev_name, call_waiting_t enable)
{
struct pvt *pvt = get_pvt(dev_name, 1);
if (!pvt) {
return -1;
}
int res = at_enqueue_set_ccwa(&pvt->sys_chan, enable);
free_pvt(pvt);
return res;
}
#/* */
EXPORT_DEF int send_at_command(const char *dev_name, const char *command)
{
struct pvt *pvt = get_pvt(dev_name, 0);
if (!pvt) {
return -1;
}
int res = at_enqueue_user_cmd(&pvt->sys_chan, command);
free_pvt(pvt);
return res;
}
EXPORT_DEF int schedule_restart_event(dev_state_t event, restate_time_t when, const char *dev_name)
{
struct pvt *pvt = find_device(dev_name);
if (pvt) {
pvt->desired_state = event;
pvt->restart_time = when;
pvt_try_restate(pvt);
ast_mutex_unlock(&pvt->lock);
} else {
chan_quectel_err = E_DEVICE_NOT_FOUND;
return -1;
}
return 0;
}