-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
159 lines (128 loc) · 2.78 KB
/
list.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
#include "list.h"
int list_init(struct list_t **list_ptr, int size)
{
struct list_t *list;
list = malloc(sizeof(struct list_t));
if (list==NULL)
return -1;
*list_ptr = list;
list->it = calloc(size, sizeof(void*));
list->used = 0;
list->size = size;
list->index = 0;
return 0;
}
void list_add(struct list_t *list, void* item)
{
list->it[list->index] = item;
(list->used)++;
(list->index)++;
if (list->index == list->size) list->index = 0;
if (list->used > list->size) list->used = list->size;
}
void list_clear(struct list_t *list)
{
list->used = 0;
list->index = 0;
}
void list_fill(struct list_t *list)
{
int i, imax;
imax = list->size;
struct item_t *item_new;
int ret;
for (i=0; i<imax; i++) {
ret = item_init(&item_new);
list->it[i] = (void*)item_new;
}
}
int list_search(struct list_t *list, unsigned char *mac)
{
int i, imax;
imax = list->used;
struct item_t *item;
for (i=0; i<imax; i++) {
item = (struct item_t *)(list->it[i]);
if (mac_cmp(item->mac, mac)==0) return i;
}
return -1;
}
int list_add_mac(struct list_t *list, unsigned char *mac, int signal)
{
struct item_t *item;
int ret;
ret = list_search(list, mac);
if (ret>=0) {
item = (struct item_t *)(list->it[ret]);
if (signal > item->signal_max) {
item->signal_max = signal;
tiempo_get(item->tiempo_max);
}
item->signal_last = signal;
tiempo_get(item->tiempo_last);
if (item->sent==0) {
item->sent = 1;
item_send(item, 'f');
}
return 1;
} else {
item = (struct item_t*)(list->it[list->index]);
(list->used)++;
(list->index)++;
if (list->index == list->size) list->index = 0;
if (list->used > list->size) list->used = list->size;
memcpy(item->mac, mac, 6);
item->signal_first = signal;
item->signal_max = signal;
item->signal_last = signal;
tiempo_get(item->tiempo_first);
tiempo_get(item->tiempo_max);
tiempo_get(item->tiempo_last);
item->sent = 0;
return 0;
}
return -1;
}
void list_send_last(struct list_t *list)
{
int i, imax;
imax = list->used;
struct item_t *item;
for(i=0; i<imax; i++) {
item = (struct item_t *)(list->it[i]);
if (item->tiempo_first->sec != item->tiempo_max->sec) {
item_send(item, 'm');
}
if ((item->tiempo_max->sec != item->tiempo_last->sec) && (item->tiempo_first->sec != item->tiempo_last->sec )) {
item_send(item, 'l');
}
}
}
void list_def_send_last()
{
list_send_last(list_def);
}
int list_def_add_mac(unsigned char *mac, int signal)
{
return list_add_mac(list_def, mac, signal);
}
int list_def_search(unsigned char *mac)
{
return list_search(list_def, mac);
}
void list_def_fill()
{
list_fill(list_def);
}
void list_def_clear()
{
list_clear(list_def);
}
int list_def_init(int size)
{
return list_init(&list_def, size);
}
void list_def_add(void* item)
{
list_add(list_def, item);
}