rssi_list.c
6.01 KB
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
#include "rssi_list.h"
u_char *string_to_mac(char * buf, u_char * byte_mac) {
sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &byte_mac[0], &byte_mac[1],&byte_mac[2],&byte_mac[3],&byte_mac[4],&byte_mac[5]);
return byte_mac;
}
char * mac_to_string(u_char * byte_mac, char * buf) {
//opposite of sscanf is sprintf
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", byte_mac[0], byte_mac[1], byte_mac[2], byte_mac[3], byte_mac[4], byte_mac[5]);
return buf;
}
int is_equal(u_char * array1, u_char * array2) {
if (sizeof(array1) != sizeof(array2))
return 0;
int i = 0;
while (i < 6) {
if(array1[i] != array2[i])
return 0;
++i;
}
return 1;
}
/* -------------------------------------------------------------------------------------------- */
/* ---------------------------------gestionaire de liste chainée ------------------------------ */
/* -------------------------------------------------------------------------------------------- */
void clear_outdated_values(Deque * list) {
if(list != NULL) {
struct timeval timer;
unsigned long long ull_timer = 0;
gettimeofday(&timer, NULL);
ull_timer = timer.tv_sec;
if (list->head != NULL) {
Rssi_sample * cursor = list->head;
Rssi_sample * lastElem = NULL; //last element checked
while (cursor != NULL) {
//check if cursor element has to be cleared
if (cursor->deadline + DEFAULT_KEEP_DELAY < ull_timer ) {
//only my cursor element
if (cursor == list->head && cursor->next == NULL) {
free(cursor);
list->head = NULL;
list->tail = NULL;
cursor = NULL;
}
//first element but not alone
else if (cursor == list->head) {
list->head = cursor->next;
free(cursor);
cursor = list->head;
}
//last element : if deque is properly sorted it should never appear
else if (cursor == list->tail){
list->tail = lastElem;
lastElem->next = NULL;
free(cursor);
cursor = NULL;
}
else {
lastElem->next = cursor->next;
free(cursor);
cursor = lastElem->next;
}
}
//no need to modify. get next elem
else {
//update last elem with cursor, go to next elem
lastElem = cursor;
cursor = cursor->next;
}
}
}
}
}
void clear_values(Deque * list) {
if (list != NULL && list->head != NULL) {
Rssi_sample * next;
while (list->head != NULL) {
next = list->head->next;
free(list->head);
list->head = next;
}
}
list->tail = NULL;
}
void add_value(Deque * list, int value) {
if (list != NULL)
{
//initialize the new rssi_sample
//Rssi_sample * new_rssi= NULL;
/***********************************************************
*Note : *
*using malloc causes memory corruption *
*It seems to be because of a static declaration of new_el *
* ! need an investigation ! *
************************************************************/
//new_rssi = (Rssi_sample*) calloc(sizeof(Rssi_sample), 1);
//initialize the new rssi_sample
Rssi_sample * new_rssi;
new_rssi = malloc(sizeof(Rssi_sample));
new_rssi->rssi_mW = value;
struct timeval timer;
gettimeofday(&timer, NULL);
new_rssi->deadline = timer.tv_sec;
new_rssi->next = NULL;
if (list->head == NULL) {
list->head = new_rssi;
}
else {
list->tail->next = new_rssi;
}
list->tail = new_rssi;
}
}
void clear_list(Element ** list) {
if (*list != NULL) {
Element * next;
while (*list != NULL) {
//save next device(element)
next = (*list)->next;
//clear deque of the device
clear_values(&(*list)->measurements);
free(*list);
*list = next;
}
}
}
Element * find_mac(Element * list, u_char * mac_value) {
if (list != NULL) {
Element * cursor = list;
while (cursor != NULL) {
if (is_equal(cursor->mac_addr, mac_value))
return cursor;
cursor = cursor->next;
}
}
return NULL;
}
Element * add_element(Element ** list, u_char * mac_value) {
int i;
//initialize new element
Element * elem;
//el = (Element*)calloc(sizeof(Element), 1);
elem = malloc(sizeof(Element));
for (i =0; i < 6; i++)
elem->mac_addr[i] = mac_value[i];
elem->measurements.head = NULL;
elem->measurements.tail = NULL;
elem->next = NULL;
if (*list != NULL) {
Element * cursor = *list;
while (cursor!= NULL)
cursor = cursor->next;
cursor = elem;
}
else
*list = elem;
return elem;
}
void delete_element(Element ** list, Element * e) {
if (*list != NULL && e != NULL)
{
Element * cursor = *list;
Element * previous = NULL;
// find the matched device (element)
while (cursor->next != NULL && cursor->mac_addr != e->mac_addr)
{
previous = cursor;
cursor = cursor->next;
}
if(previous==NULL) // first elem will be delete
*list = cursor->next;
else
previous->next=cursor->next; //relink the list
//clear the rssi list
//clear rssi deque of the device
clear_values(&(cursor->measurements));
/*Rssi_sample * rssi = cursor->measurements.head;
while (rssi != NULL) {
cursor->measurements.head = rssi->next;
free(rssi);
rssi = cursor->next->measurements.head;
}
cursor->measurements.tail = NULL;*/
free(cursor);
}
}
void clear_empty_macs(Element ** list) {
Element * cursor = *list;
Element * tmp;
while (cursor != NULL) {
//if the current element has an empty rssi sample list
if (cursor->measurements.head == NULL) {
//get next element
tmp = cursor->next;
//deleete element from the list
delete_element(list, cursor);
//restore the element
cursor = tmp;
}
else
cursor = cursor->next; //get next element
}
}
/* -------------------------------------------------------------------------------------------- */
/* --------------------------------- element building ------------------------------ */
/* -------------------------------------------------------------------------------------------- */
/*
char * build_element(Element * e, char * buf) {
}
char * build_buffer(Element * list, char * buffer, char * my_name, u_char * macs_requested, unsigned short nb_macs) {
}*/