forked from sam-itt/sofis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-source.c
199 lines (165 loc) · 5.49 KB
/
data-source.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
/*
* SPDX-FileCopyrightText: 2021 Samuel Cuella <samuel.cuella@gmail.com>
*
* This file is part of SoFIS - an open source EFIS
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <stdio.h>
#include <stdarg.h>
#include "data-source.h"
#include "misc.h"
static DataSource *_datasource = NULL;
/*forward declarations of private functions*/
static bool get_listener_range(DataType type, uintf8_t *start, uintf8_t *limit);
DataSource *data_source_get_instance(void)
{
return _datasource;
}
void data_source_set(DataSource *source)
{
_datasource = source;
}
/**
* @brief Allow a single object to register for several events at once.
*
* @param target The object that will receive the event
* @param nevents Number of DataType/ValueListenerFunc couples that follows
* @param ... couples of DataType/ValueListenerFunc
*
* @return The number of successfuly added listeners. A value different from
* @param nevents indicates a failure after the return value nth listener
*/
size_t data_source_add_events_listener(DataSource *self, void *target,
size_t nevents, ...)
{
va_list args;
int i;
va_start(args, nevents);
for(i = 0; i < nevents; i++){
DataType type = va_arg(args, DataType);
ValueListenerFunc func = va_arg(args, ValueListenerFunc);
bool rv = data_source_add_listener(self, type,&(ValueListener){
.callback = func,
.target = target
});
if(!rv) break;
}
va_end(args);
return i;
}
bool data_source_add_listener(DataSource *self, DataType type, ValueListener *listener)
{
uintf8_t idx, limit;
self = self ? self : data_source_get_instance();
get_listener_range(type, &idx, &limit);
if(self->nlisteners[type] == limit){
printf(
"Tried to add another location listener while %d limit has been reached."
"Please increment the corresponding listeners limit\n", limit
);
return false;
}
self->listeners[idx+self->nlisteners[type]] = *listener;
self->nlisteners[type]++;
return true;
}
void data_source_print_listener_stats(DataSource *self)
{
printf(
"Current number of listeners:\n"
"\tlocation: %zu\n"
"\tattitude: %zu\n"
"\tdynamics: %zu\n"
"\tengine data: %zu\n"
"\troute: %zu\n",
self->nlisteners[LOCATION_DATA],
self->nlisteners[ATTITUDE_DATA],
self->nlisteners[DYNAMICS_DATA],
self->nlisteners[ENGINE_DATA],
self->nlisteners[ROUTE_DATA]
);
}
static void data_source_fire_listeners(DataSource *self, DataType type, void *param)
{
uintf8_t idx, limit;
self = self ? self : data_source_get_instance();
get_listener_range(type, &idx, &limit);
for(int i = idx; i < idx + self->nlisteners[type]; i++){
self->listeners[i].callback(
self->listeners[i].target,
param
);
}
}
void data_source_set_location(DataSource *self, LocationData *location)
{
self = self ? self : data_source_get_instance();
if(location_equals(location, &self->location))
return;
data_source_fire_listeners(self, LOCATION_DATA, location);
self->location = *location;
}
void data_source_set_attitude(DataSource *self, AttitudeData *attitude)
{
self = self ? self : data_source_get_instance();
if(attitude_equals(attitude, &self->attitude))
return;
data_source_fire_listeners(self, ATTITUDE_DATA, attitude);
self->attitude = *attitude;
}
void data_source_set_dynamics(DataSource *self, DynamicsData *dynamics)
{
self = self ? self : data_source_get_instance();
if(dynamics_equals(dynamics, &self->dynamics))
return;
data_source_fire_listeners(self, DYNAMICS_DATA, dynamics);
self->dynamics = *dynamics;
}
void data_source_set_engine_data(DataSource *self, EngineData *engine_data)
{
self = self ? self : data_source_get_instance();
if(engine_data_equals(engine_data, &self->engine_data))
return;
data_source_fire_listeners(self, ENGINE_DATA, engine_data);
self->engine_data = *engine_data;
}
void data_source_set_route_data(DataSource *self, RouteData *route_data)
{
self = self ? self : data_source_get_instance();
if(route_data_equals(route_data, &self->route))
return;
data_source_fire_listeners(self, ROUTE_DATA, route_data);
self->route = *route_data;
}
static bool get_listener_range(DataType type, uintf8_t *start, uintf8_t *limit)
{
switch(type){
case LOCATION_DATA:
*start = 0;
*limit = *start + MAX_LOCATION_LISTENERS;
return true;
case ATTITUDE_DATA:
*start = MAX_LOCATION_LISTENERS;
*limit = *start + MAX_ATTITUDE_LISTENERS;
return true;
case DYNAMICS_DATA:
*start = MAX_LOCATION_LISTENERS+MAX_ATTITUDE_LISTENERS;
*limit = *start + MAX_DYNAMICS_LISTENERS;
return true;
case ENGINE_DATA:
*start = MAX_LOCATION_LISTENERS+MAX_ATTITUDE_LISTENERS+MAX_DYNAMICS_LISTENERS;
*limit = *start + MAX_ENGINE_DATA_LISTENERS;
return true;
case ROUTE_DATA:
*start = MAX_LOCATION_LISTENERS+MAX_ATTITUDE_LISTENERS
+ MAX_DYNAMICS_LISTENERS + MAX_ENGINE_DATA_LISTENERS;
*limit = *start + MAX_ROUTE_DATA_LISTENERS;
return true;
break;
default:
printf("CRIT: %s: bad type, problems ahead!\n",__FUNCTION__);
return 0;
break;
};
}