forked from psas/elderberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcfutils.c
293 lines (247 loc) · 7.28 KB
/
fcfutils.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
/*
PSAS Flight Control Framework
Copyright (C) 2013
Team Elderberry [Ron Astin, Clark Wachsmuth, Chris Glasser, Josef Mihalits, Jordan Hewitt, Michael Hooper]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "fcfutils.h"
#define FDS_INIT_SIZE 1
#define FDS_EXPANSION_FACTOR 2
/*
* This struct holds the callback functions and souce tokens of the devices.
*/
struct fcffd{
pollfd_callback callback;
char cb_cat;
};
static const char STANDARD = 0; //< Standard callback
static const char PPC = 1; //< Per poll cycle callback
static struct pollfd * fds = NULL; //< File descriptor array
static struct fcffd * fdx = NULL; //< File description array
static int nfds; //< Number of file descriptors in arrays
static int fd_array_size; //< Allocated size of file descriptor array, fds
static int run_fc; //< Main loop is running true/false
extern void fcf_initialize(void);
extern void fcf_finalize (void);
/*
* Initialization for fcf data structures
*/
static int init_fcf(){
fd_array_size = FDS_INIT_SIZE;
//initializing both file descriptor arrays
fds = (struct pollfd *) malloc(fd_array_size * sizeof(struct pollfd));
fdx = (struct fcffd *) malloc(fd_array_size * sizeof(struct fcffd));
if(!fds){
fprintf(stderr, "Could not allocate memory for file descriptor array.");
return -1;
}
else if(!fdx){
fprintf(stderr, "Could not allocate memory for callback array.");
return -1;
}
nfds = 0; //< Number of file descriptors in array
run_fc = 0; //< Main loop is running True/False
return 0;
}
/*
* Deallocate fcf data structures
*/
static void finalize_fcf(){
free(fds);
free(fdx);
nfds = -1;
}
/*
* Increases size of the file desciptor and file description arrays
*/
static int expand_arrays(){
struct pollfd * fds_temp;
struct fcffd * fdx_temp;
fd_array_size *= FDS_EXPANSION_FACTOR; // Expand array by pre-defined factor
// Increase size of fds array
fds_temp = realloc(fds, fd_array_size * sizeof(struct pollfd));
if(fds_temp == NULL){
return -1; //if failed
}
fds = fds_temp;
// Increase size of fdx array
fdx_temp = realloc(fdx, fd_array_size * sizeof(struct fcffd));
if(fdx_temp == NULL){
return -1; // if failed
}
fdx = fdx_temp;
return 0;
}
/*
* Add file descriptor into both fdx and fds arrays
*/
int fcf_add_fd(int fd, short events, pollfd_callback cb){
// Checks to see if fd arrays are full, if they are expand arrays.
if(fd_array_size == nfds){
expand_arrays();
}
// Filling file descriptor arrays with fd and callback data
fds[nfds].fd = fd;
fds[nfds].events = events;
fdx[nfds].callback = cb;
fdx[nfds].cb_cat = STANDARD;
nfds++;
return nfds - 1; // return value is the index of the newest file descriptor
}
/*
* Per poll loop file descriptor add
*/
int fcf_add_fd_ppc(int fd, short events, pollfd_callback cb){
int i = fcf_add_fd (fd, events, cb);
if(i >= 0){
fdx[i].cb_cat = PPC;
}
return i;
}
/*
* Removes the indicated file descriptor from the arrays.
*/
void fcf_remove_fd(int fd){
// If there are no fds, return
if(nfds <= 0)
return;
for(int i = 0; i < nfds; i++){
if(fds[i].fd == fd && i == (nfds - 1)){
nfds--;
}
else if(fds[i].fd == fd){
memmove(&fds[i], &fds[nfds - 1], sizeof(struct pollfd));
memmove(&fdx[i], &fdx[nfds - 1], sizeof(struct fcffd));
nfds--;
}
}
}
/*
* Function returns file descriptor information for specified array index
*/
struct pollfd *fcf_get_fd(int idx){
return &fds[idx];
}
/*
* Stops main poll loop from running
*/
void fcf_stop_main_loop(){
run_fc = 0;
}
/*
* Starts main poll loop running
*/
static void fcf_start_main_loop(){
run_fc = 1;
}
/*
* Detects change in file descriptors and polls fd arrays
*/
static int fcf_run_poll_loop(){
//currently returns -1 on error; 0 on success.
pollfd_callback ppc[nfds];
int nppc = 0, ret = 0;
//int count = 0;
fcf_start_main_loop();
while(run_fc){
fflush(stdout);
errno = 0;
int rc = poll(fds, nfds, -1);
switch (rc){
case -1: // error
if(errno != EINTR){
perror("run_main_loop: poll returned with error");
ret = -1;
fcf_stop_main_loop();
}
break;
case 0: // timeout
break;
default:
nppc = 0;
for(int i = 0; i < nfds && rc > 0; i++){
if(fds[i].revents != 0){
rc--;
if(fdx[i].cb_cat == STANDARD){
// callback for this active fd is a standard callback
fdx[i].callback(&fds[i]);
}
else{
// callback for this active fd is a "per poll cycle" callback
int j;
for(j = 0; j < nppc && ppc[j] != fdx[i].callback; j++)
{ /*empty*/ }
if(j == nppc){
// a new ppc callback
// add to ppc so that callback will be called at end of poll cycle
ppc[nppc++] = fdx[i].callback;
}
else{
//we have seen this callback before
//printf("\n multiple active ppc, ignoring callback for fd[%d]: fd=%d", i, fds[i].fd);
}
}
} // (revents set)
} // (for i)
// handle ppc callbacks
for(int j = 0; j < nppc; j++){
// if callback wants to access the fds, callback
// is expected to know the indices into the fds array
// i.e., module must store return values it gets from fcf_addfdPpc
//printf("\n calling ppc callback [%d]", j);
ppc[j](fds);
}
break;
}
}
//printf("\n exiting main loop");
return ret;
}
static void signalhandler(int signum){
if(signum == SIGINT){
fcf_stop_main_loop ();
}
}
int main(int argc, char *argv[]){
printf("\n FLIGHT CONTROL FRAMEWORK V1.0 -- Copyright (C) 2013\n"
" : Ron Astin, Clark Wachsmuth, Chris Glasser\n : Josef Mihalits, Jordan Hewitt, Michael Hooper\n\n"
"----------------------------------------------------------------\n"
" This program comes with ABSOLUTELY NO WARRANTY;\n for details please"
" visit http://www.gnu.org/licenses/gpl.html.\n\n"
" This is free software, and you are welcome to redistribute it\n"
" under certain conditions; For details, please visit\n"
" http://www.gnu.org/licenses/gpl.html.\n"
"----------------------------------------------------------------\n\n\n");
signal(SIGINT, signalhandler);
int rc = init_fcf(); //< FCF init that sets up fd structures
if(rc == 0){
fcf_initialize(); //< fcfmain init function for user modules
int rc = fcf_run_poll_loop();
fcf_finalize(); //< fcfmain finalize function for user modules
finalize_fcf(); //< FCF finalize that deallocates fd structures
if(rc == 0) {
return EXIT_SUCCESS;
}
}
return EXIT_FAILURE;
}