forked from atefsaeed2010/datacard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
at_queue.c
385 lines (328 loc) · 8.51 KB
/
at_queue.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
Copyright (C) 2010 - 2011
bg <[email protected]>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <asterisk.h>
#include <asterisk/utils.h> /* ast_free() */
#include "at_queue.h"
#include "chan_datacard.h" /* struct pvt */
/*!
* \brief Free an item data
* \param cmd - struct at_queue_cmd
*/
#/* */
static void at_queue_free_data(at_queue_cmd_t * cmd)
{
if(cmd->data)
{
if((cmd->flags & ATQ_CMD_FLAG_STATIC) == 0)
{
ast_free (cmd->data);
cmd->data = NULL;
}
/* right because work with copy of static data */
}
cmd->length = 0;
}
/*!
* \brief Free an item
* \param e -- struct at_queue_task structure
*/
#/* */
static void at_queue_free (at_queue_task_t * task)
{
unsigned no;
for(no = 0; no < task->cmdsno; no++)
{
at_queue_free_data(&task->cmds[no]);
}
ast_free (task);
}
/*!
* \brief Remove an job item from the front of the queue, and free it
* \param pvt -- pvt structure
*/
#/* */
static void at_queue_remove (struct pvt * pvt)
{
at_queue_task_t * task = AST_LIST_REMOVE_HEAD (&pvt->at_queue, entry);
if (task)
{
PVT_STATE(pvt, at_tasks)--;
PVT_STATE(pvt, at_cmds) -= task->cmdsno - task->cindex;
ast_debug (4, "[%s] remove task with %u command(s) begin with '%s' expected response '%s' from queue\n",
PVT_ID(pvt), task->cmdsno, at_cmd2str (task->cmds[0].cmd),
at_res2str (task->cmds[0].res));
at_queue_free(task);
}
}
#/* */
static at_queue_cmd_t* at_queue_head_cmd_nc (const struct pvt * pvt)
{
at_queue_task_t * e = AST_LIST_FIRST (&pvt->at_queue);
if(e)
return &e->cmds[e->cindex];
return NULL;
}
/*!
* \brief Add an list of commands (task) to the back of the queue
* \param cpvt -- cpvt structure
* \param cmds -- the commands that was sent to generate the response
* \param cmdsno -- number of commands
* \param prio -- priority 0 mean put at tail
* \return task on success, NULL on error
*/
#/* */
static at_queue_task_t * at_queue_add (struct cpvt * cpvt, const at_queue_cmd_t * cmds, unsigned cmdsno, int prio)
{
at_queue_task_t * e = NULL;
if(cmdsno > 0)
{
e = ast_malloc (sizeof(*e) + cmdsno * sizeof(*cmds));
if(e)
{
pvt_t * pvt = cpvt->pvt;
at_queue_task_t * first;
e->entry.next = 0;
e->cmdsno = cmdsno;
e->cindex = 0;
e->cpvt = cpvt;
memcpy(&e->cmds[0], cmds, cmdsno * sizeof(*cmds));
if(prio && (first = AST_LIST_FIRST (&pvt->at_queue)))
AST_LIST_INSERT_AFTER (&pvt->at_queue, first, e, entry);
else
AST_LIST_INSERT_TAIL (&pvt->at_queue, e, entry);
PVT_STATE(pvt, at_tasks) ++;
PVT_STATE(pvt, at_cmds) += cmdsno;
PVT_STAT(pvt, at_tasks) ++;
PVT_STAT(pvt, at_cmds) += cmdsno;
ast_debug (4, "[%s] insert task with %u commands begin with '%s' expected response '%s' %s of queue\n",
PVT_ID(pvt), e->cmdsno, at_cmd2str (e->cmds[0].cmd),
at_res2str (e->cmds[0].res), prio ? "after head" : "at tail");
}
}
return e;
}
/*!
* \brief Write to fd
* \param fd -- file descriptor
* \param buf -- buffer to write
* \param count -- number of bytes to write
*
* This function will write count characters from buf. It will always write
* count chars unless it encounters an error.
*
* \retval number of bytes wrote
*/
#/* */
EXPORT_DEF size_t write_all (int fd, const char* buf, size_t count)
{
ssize_t out_count;
size_t total = 0;
unsigned errs = 10;
while (count > 0)
{
out_count = write (fd, buf, count);
if (out_count <= 0)
{
if(errno == EINTR || errno == EAGAIN)
{
errs--;
if(errs != 0)
continue;
}
break;
}
errs = 10;
count -= out_count;
buf += out_count;
total += out_count;
}
return total;
}
/*!
* \brief Write to fd
* \param pvt -- pvt structure
* \param buf -- buffer to write
* \param count -- number of bytes to write
*
* This function will write count characters from buf. It will always write
* count chars unless it encounters an error.
*
* \retval !0 on error
* \retval 0 success
*/
#/* */
EXPORT_DEF int at_write (struct pvt* pvt, const char* buf, size_t count)
{
size_t wrote;
ast_debug (5, "[%s] [%.*s]\n", PVT_ID(pvt), (int) count, buf);
wrote = write_all(pvt->data_fd, buf, count);
PVT_STAT(pvt, d_write_bytes) += wrote;
if(wrote != count)
{
ast_debug (1, "[%s] write() error: %d\n", PVT_ID(pvt), errno);
}
return wrote != count;
}
/*!
* \brief Remove an cmd item from the front of the queue
* \param pvt -- pvt structure
*/
#/* */
EXPORT_DEF void at_queue_remove_cmd (struct pvt* pvt, at_res_t res)
{
at_queue_task_t * task = AST_LIST_FIRST (&pvt->at_queue);
if (task)
{
unsigned index = task->cindex;
task->cindex++;
PVT_STATE(pvt, at_cmds)--;
ast_debug (4, "[%s] remove command '%s' expected response '%s' real '%s' cmd %u/%u flags 0x%02x from queue\n",
PVT_ID(pvt), at_cmd2str (task->cmds[index].cmd),
at_res2str (task->cmds[index].res), at_res2str (res),
task->cindex, task->cmdsno, task->cmds[index].flags);
if((task->cindex >= task->cmdsno) || (task->cmds[index].res != res && (task->cmds[index].flags & ATQ_CMD_FLAG_IGNORE) == 0))
{
at_queue_remove(pvt);
}
}
}
/*!
* \brief Try real write first command on queue
* \param pvt -- pvt structure
* \return 0 on success, non-0 on error
*/
#/* */
EXPORT_DEF int at_queue_run (struct pvt * pvt)
{
int fail = 0;
at_queue_cmd_t * cmd = at_queue_head_cmd_nc(pvt);
if(cmd)
{
if(cmd->length > 0)
{
ast_debug (4, "[%s] write command '%s' expected response '%s' length %u\n",
PVT_ID(pvt), at_cmd2str (cmd->cmd), at_res2str (cmd->res), cmd->length);
fail = at_write(pvt, cmd->data, cmd->length);
if(fail)
{
ast_log (LOG_ERROR, "[%s] Error write command '%s' expected response '%s' length %u, cancel\n", PVT_ID(pvt), at_cmd2str (cmd->cmd), at_res2str (cmd->res), cmd->length);
at_queue_remove_cmd(pvt, cmd->res + 1);
}
else
{
/* set expire time */
cmd->timeout = ast_tvadd (ast_tvnow(), cmd->timeout);
/* free data and mark as written */
at_queue_free_data(cmd);
}
}
#if 0
else
{
/* check expiration */
if(ast_tvcmp (ast_tvnow(), cmd->timeout) > 0)
{
ast_log (LOG_ERROR, "[%s] Error command '%s' expected response '%s' expired, cancel\n", PVT_ID(pvt), at_cmd2str (cmd->cmd), at_res2str (cmd->res));
at_queue_remove_cmd(pvt, cmd->res + 1);
fail = -1;
}
}
#endif /* 0 */
}
/* else empty nothing todo */
return fail;
}
/*!
* \brief Write commands with queue
* \param pvt -- pvt structure
* \return 0 on success non-0 on error
*/
#/* */
EXPORT_DEF int at_queue_insert_const (struct cpvt * cpvt, const at_queue_cmd_t * cmds, unsigned cmdsno, int athead)
{
return at_queue_add(cpvt, cmds, cmdsno, athead) == NULL || at_queue_run(cpvt->pvt);
}
#/* */
EXPORT_DEF int at_queue_insert_task (struct cpvt * cpvt, at_queue_cmd_t * cmds, unsigned cmdsno, int athead, at_queue_task_t ** task)
{
unsigned idx;
task[0] = at_queue_add(cpvt, cmds, cmdsno, athead);
if(!task[0])
{
for(idx = 0; idx < cmdsno; idx++)
{
at_queue_free_data(&cmds[idx]);
}
}
if(at_queue_run(cpvt->pvt))
task[0] = NULL;
return task[0] == NULL;
}
#/* */
EXPORT_DEF int at_queue_insert(struct cpvt * cpvt, at_queue_cmd_t * cmds, unsigned cmdsno, int athead)
{
at_queue_task_t * task;
return at_queue_insert_task(cpvt, cmds, cmdsno, athead, &task);
}
#/* */
EXPORT_DEF void at_queue_handle_result (struct pvt* pvt, at_res_t res)
{
/* move queue */
at_queue_remove_cmd(pvt, res);
}
/*!
* \brief Remove all itmes from the queue and free them
* \param pvt -- pvt structure
*/
#/* */
EXPORT_DEF void at_queue_flush (struct pvt* pvt)
{
struct at_queue_task* task;
while ((task = AST_LIST_FIRST (&pvt->at_queue)))
{
at_queue_remove(pvt);
}
}
/*!
* \brief Get the first task on queue
* \param pvt -- pvt structure
* \return a pointer to the first command of the given queue
*/
#/* */
EXPORT_DEF const struct at_queue_task* at_queue_head_task (const struct pvt * pvt)
{
return AST_LIST_FIRST (&pvt->at_queue);
}
/*!
* \brief Get the first command of a queue
* \param pvt -- pvt structure
* \return a pointer to the first command of the given queue
*/
#/* */
EXPORT_DEF const at_queue_cmd_t * at_queue_head_cmd(const struct pvt * pvt)
{
return at_queue_task_cmd(at_queue_head_task(pvt));
}
#/* */
EXPORT_DEF int at_queue_timeout(const struct pvt * pvt)
{
int ms_timeout = -1;
const at_queue_cmd_t * cmd = at_queue_head_cmd(pvt);
if(cmd)
{
if(cmd->length == 0)
{
ms_timeout = ast_tvdiff_ms(cmd->timeout, ast_tvnow());
}
}
return ms_timeout;
}