-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_qlinkdatanode.c
291 lines (245 loc) · 6.62 KB
/
queue_qlinkdatanode.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
#include <string.h> // memset, memcpy
#include <stdlib.h> // malloc, free
#include <errno.h>
#include "queue_qlinkdatanode.h"
#include "feature_macro_qlinkdatanode.h"
#include "common_qlinkdatanode.h"
#ifndef FEATURE_ENABLE_QUEUE_qlinkdatanode_C_LOG
#define LOG print_to_null
#endif
/******************************External Variables*******************************/
extern rsmp_control_block_type flow_info;
extern bool isQueued;
extern bool isUimPwrDwn;
extern unsigned int StatInfMsgSn;
extern int StatInfReqNum;
/******************************Global Variables*******************************/
Queue_Node head_of_queue = {
.next_node = NULL,
.prev_node = NULL,
.type = false,
.request = {-1, NULL, false},
.last_flow_state = FLOW_STATE_IDLE,
.last_req_type = REQ_TYPE_IDLE
};
Queue_Node *p_tail_of_queue;
static void init_queue(void)
{
Queue_Node *next = head_of_queue.next_node;
for(; next != NULL; ){
Queue_Node *cur = next;
next = next->next_node;
free(cur->request.data);
cur->request.data = NULL;
free(cur);
}
p_tail_of_queue = &head_of_queue;
isQueued = false;
return;
}
//Description:
// 1. The arg "req" can't be pointer to pointer.
// 2. It is FIFO queue.
// 3. Add node right after p_tail_of_queue. It includes creating node.
//Return Value:
// Added node pointer.
Queue_Node *add_node(rsmp_transmit_buffer_s_type *req, rsmp_state_s_type state)
{
Queue_Node *new_node = NULL;
LOG6("~~~~ add_node start ~~~~\n");
isQueued = true;
new_node = (Queue_Node *)malloc(sizeof(Queue_Node));
if(NULL == new_node){
LOG("ERROR: malloc() failed. errno=%d.\n", errno);
return NULL;
}
memset((void *)new_node, 0, sizeof(Queue_Node));
new_node->last_flow_state = state;
switch(state){
case FLOW_STATE_APDU_COMM:
new_node->last_req_type = REQ_TYPE_APDU;break;
case FLOW_STATE_ID_AUTH:
new_node->last_req_type = REQ_TYPE_ID;break;
case FLOW_STATE_PWR_DOWN_UPLOAD:
new_node->last_req_type = REQ_TYPE_PWR_DOWN;break;
case FLOW_STATE_REMOTE_UIM_DATA_REQ:
new_node->last_req_type = REQ_TYPE_REMOTE_UIM_DATA;break;
case FLOW_STATE_STAT_UPLOAD:
new_node->last_req_type = REQ_TYPE_STATUS;break;
case FLOW_STATE_IMSI_ACQ:
case FLOW_STATE_SET_OPER_MODE:
case FLOW_STATE_RESET_UIM:
default:{
LOG("ERROR: Wrong param input. state=%d.\n", state);
return NULL;
}
}
// StatInfReqNum is for real-time examination of status info msg. So if any status info msg is added
//into queue, StatInfReqNum would be decreased by 1.
if(REQ_TYPE_STATUS == new_node->last_req_type){
StatInfReqNum --;
}
new_node->next_node = NULL;
new_node->prev_node = p_tail_of_queue;
p_tail_of_queue->next_node = new_node;
new_node->request.size = req->size;
new_node->request.data = malloc(req->size);
memset(new_node->request.data, 0, req->size);
memcpy(new_node->request.data, req->data, req->size);
if(REQ_TYPE_STATUS == new_node->last_req_type){
LOG("StatInfMsgSn = %u.\n", new_node->request.StatInfMsgSn);
new_node->request.StatInfMsgSn = req->StatInfMsgSn;
}
p_tail_of_queue = new_node;
LOG6("~~~~ add_node end ~~~~\n");
return p_tail_of_queue;
}
//Function:
// Remove "first" node of queue except for header.
// "Remove" from queue structure, not clear its memory.
//Return Value:
// Node pointer of removed node.
Queue_Node *remove_first_node_from_queue(void)
{
Queue_Node *rm_node = head_of_queue.next_node;
if(isQueued == false){
LOG("ERROR: Queue is empty.\n");
return NULL;
}
if(rm_node == NULL){
isQueued = false;
LOG("ERROR: isQueued=1, but queue is empty.\n");
return NULL;
}
head_of_queue.next_node = rm_node->next_node;
if(rm_node->next_node == NULL){
isQueued = false;
p_tail_of_queue = &head_of_queue;
}else{
rm_node->next_node->prev_node = &head_of_queue;
}
return rm_node;
}
static Queue_Node *get_first_node_from_queue(void)
{
Queue_Node *p_node = head_of_queue.next_node;
if(isQueued == false){
LOG("ERROR: Queue is empty.\n");
return NULL;
}
if(p_node == NULL){
isQueued = false;
LOG("ERROR: isQueued=1, but queue is empty.\n");
return NULL;
}
return p_node;
}
static Queue_Node *remove_from_queue(Queue_Node *rm_node)
{
Queue_Node *nxt_node = NULL, *cur_node = head_of_queue.next_node;
if(isQueued == false){
LOG("ERROR: Queue is empty.\n");
return NULL;
}
if(cur_node == NULL){
isQueued = false;
LOG("ERROR: isQueued=1, but no node to remove. cur_node=null.\n");
return NULL;
}
if(rm_node == NULL){
LOG("ERROR: Wrong param input. rm_node=null.\n");
return NULL;
}
for(; cur_node!=NULL; cur_node=cur_node->next_node){
if(cur_node == rm_node){
rm_node->prev_node->next_node = rm_node->next_node;
if(rm_node->next_node != NULL){
rm_node->next_node->prev_node = rm_node->prev_node;
}else{
p_tail_of_queue = rm_node->prev_node;
if(&head_of_queue == rm_node->prev_node)
isQueued = false;
}
break;
}
}
return nxt_node;
}
//Function:
// Clear memory of node.
//NOTE:
// Not allow to call remove_node() before calling remove_first_node_from_queue() or remove_from_queue!!!
void remove_node(Queue_Node *node)
{
if (node == NULL){
// LOG("Wrong param input. node=null.\n");
return;
}
free(node->request.data);
free(node);
return;
}
static rsmp_protocol_type queue_check_node_req_type(void)
{
Queue_Node *p_first_node = get_first_node_from_queue();
return p_first_node->last_req_type;
}
//Description:
// Check if queued node is valid(Not out-of-date).
//Return Values:
// 1: Valid.
// 0: Invalid.
int queue_check_if_node_valid(void)
{
rsmp_protocol_type node_req_type = queue_check_node_req_type();
int ret = 1;
if(REQ_TYPE_APDU == node_req_type
&& (true == isUimPwrDwn || FLOW_STATE_RESET_UIM == flow_info.flow_state)){
LOG("The cached data is out-of-date APDU!\n");
ret = 0;
}
return ret;
}
//Description:
//Clr specific-type nodes in queue.
//Return Values:
//The num of clred nodes.
static int queue_clr_node(rsmp_protocol_type req_t)
{
Queue_Node *cur_node = head_of_queue.next_node;
int count = 0;
if(NULL == cur_node){
return 0;
}
for(; cur_node != NULL;){
if(req_t == cur_node->last_req_type){
Queue_Node *rm_node = cur_node;
cur_node = cur_node->next_node;
remove_from_queue(rm_node);
remove_node(rm_node);
count ++;
continue;
}
cur_node = cur_node->next_node;
}
return count;
}
void queue_clr_apdu_node(void)
{
int clr_num = queue_clr_node(REQ_TYPE_APDU);
LOG("Clear %d APDU node(s) in total.\n", clr_num);
return;
}
void queue_clr_stat_inf_node(void)
{
int clr_num = queue_clr_node(REQ_TYPE_STATUS);
LOG("Clear %d status info node(s) in total.\n", clr_num);
return;
}
int startQueue(void)
{
#ifdef FEATURE_ENABLE_QUEUE_SYSTEM
init_queue();
#endif
return 1;
}