-
Notifications
You must be signed in to change notification settings - Fork 0
/
__at_fifo_queue.c
129 lines (101 loc) · 2.75 KB
/
__at_fifo_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
/*
Copyright (C) 2009 - 2010
Artem Makhutov <artem@makhutov.org>
http://www.makhutov.org
Dmitry Vagin <dmitry2004@yandex.ru>
*/
/*!
* \brief Add an item to the back of the queue
* \param pvt -- pvt structure
* \param cmd -- the command that was sent to generate the response
* \param res -- the expected response
*/
static inline int at_fifo_queue_add (pvt_t* pvt, at_cmd_t cmd, at_res_t res)
{
return at_fifo_queue_add_ptr (pvt, cmd, res, NULL);
}
/*!
* \brief Add an item to the back of the queue with pointer data
* \param pvt -- pvt structure
* \param cmd -- the command that was sent to generate the response
* \param res -- the expected response
* \param data -- pointer data associated with this entry, it will be freed when the message is freed
*/
static int at_fifo_queue_add_ptr (pvt_t* pvt, at_cmd_t cmd, at_res_t res, void* data)
{
at_queue_t* e;
if (!(e = ast_calloc (1, sizeof(*e))))
{
return -1;
}
e->cmd = cmd;
e->res = res;
e->ptype = 0;
e->param.data = data;
AST_LIST_INSERT_TAIL (&pvt->at_queue, e, entry);
ast_debug (4, "[%s] add command '%s' expected response '%s'\n", pvt->id,
at_cmd2str (e->cmd), at_res2str (e->res));
return 0;
}
/*!
* \brief Add an item to the back of the queue with pointer data
* \param pvt -- pvt structure
* \param cmd -- the command that was sent to generate the response
* \param res -- the expected response
* \param num -- numeric data
*/
static int at_fifo_queue_add_num (pvt_t* pvt, at_cmd_t cmd, at_res_t res, int num)
{
at_queue_t* e;
if (!(e = ast_calloc (1, sizeof(*e))))
{
return -1;
}
e->cmd = cmd;
e->res = res;
e->ptype = 1;
e->param.num = num;
AST_LIST_INSERT_TAIL (&pvt->at_queue, e, entry);
ast_debug (4, "[%s] add command '%s' expected response '%s'\n", pvt->id,
at_cmd2str (e->cmd), at_res2str (e->res));
return 0;
}
/*!
* \brief Remove an item from the front of the queue, and free it
* \param pvt -- pvt structure
*/
static inline void at_fifo_queue_rem (pvt_t* pvt)
{
at_queue_t* e = AST_LIST_REMOVE_HEAD (&pvt->at_queue, entry);
if (e)
{
ast_debug (4, "[%s] remove command '%s' expected response '%s'\n", pvt->id,
at_cmd2str (e->cmd), at_res2str (e->res));
if (e->ptype == 0 && e->param.data)
{
ast_free (e->param.data);
}
ast_free (e);
}
}
/*!
* \brief Remove all itmes from the queue and free them
* \param pvt -- pvt structure
*/
static inline void at_fifo_queue_flush (pvt_t* pvt)
{
at_queue_t* e;
while ((e = at_fifo_queue_head (pvt)))
{
at_fifo_queue_rem (pvt);
}
}
/*!
* \brief Get the head of a queue
* \param pvt -- pvt structure
* \return a pointer to the head of the given queue
*/
static inline at_queue_t* at_fifo_queue_head (pvt_t* pvt)
{
return AST_LIST_FIRST (&pvt->at_queue);
}