forked from wiedehopf/readsb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
201 lines (163 loc) · 5.86 KB
/
util.h
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
// Part of readsb, a Mode-S/ADSB/TIS message decoder.
//
// track.h: aircraft state tracking prototypes
//
// Copyright (c) 2019 Michael Wolf <[email protected]>
//
// This code is based on a detached fork of dump1090-fa.
//
// Copyright (c) 2015 Oliver Jowett <[email protected]>
//
// This file 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
// any later version.
//
// This file 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/>.
#ifndef DUMP1090_UTIL_H
#define DUMP1090_UTIL_H
#define CHECK_APPROXIMATIONS (0)
#define GZBUFFER_BIG (1 * 1024 * 1024)
#include <stdint.h>
#define sfree(x) do { free(x); x = NULL; } while (0)
#define HOURS (60*60*1000LL)
#define MINUTES (60*1000LL)
#define SECONDS (1000LL)
#define MS (1LL)
int tryJoinThread(pthread_t *thread, int64_t timeout);
typedef struct {
pthread_t pthread;
pthread_mutex_t mutex;
pthread_cond_t cond;
char *name;
int8_t joined;
int8_t joinFailed;
} threadT;
void threadDestroyAll();
void threadInit(threadT *thread, char *name);
void threadCreate(threadT *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
void threadTimedWait(threadT *thread, struct timespec *ts, int64_t increment);
void threadSignalJoin(threadT *thread);
struct task_info {
int64_t now;
int32_t from;
int32_t to;
};
struct char_buffer {
char *buffer;
size_t len;
size_t alloc;
};
struct char_buffer readWholeFile(int fd, char *errorContext);
struct char_buffer readWholeGz(gzFile gzfp, char *errorContext);
int writeGz(gzFile gzfp, void *source, int toWrite, char *errorContext);
static inline void msleep(int64_t ms) {
struct timespec slp = {ms / 1000, (ms % 1000) * 1000 * 1000};
nanosleep(&slp, NULL);
}
/* Returns system time in milliseconds */
int64_t mstime (void);
// microseconds
int64_t microtime(void);
void milli_micro_seconds(int64_t *milli, int64_t *micro);
int snprintHMS(char *buf, size_t bufsize, int64_t now);
int64_t msThreadTime(void);
/* Returns the time elapsed, in nanoseconds, from t1 to t2,
* where t1 and t2 are 12MHz counters.
*/
int64_t receiveclock_ns_elapsed (int64_t t1, int64_t t2);
/* Same, in milliseconds */
int64_t receiveclock_ms_elapsed (int64_t t1, int64_t t2);
/* Normalize the value in ts so that ts->nsec lies in
* [0,999999999]
*/
static inline void normalize_timespec(struct timespec *ts) {
if (ts->tv_nsec >= 1000000000) {
ts->tv_sec += ts->tv_nsec / 1000000000;
ts->tv_nsec = ts->tv_nsec % 1000000000;
} else if (ts->tv_nsec < 0) {
long adjust = ts->tv_nsec / 1000000000 + 1;
ts->tv_sec -= adjust;
ts->tv_nsec = (ts->tv_nsec + 1000000000 * adjust) % 1000000000;
}
}
// convert ms to timespec
static inline struct timespec msToTimespec(int64_t ms) {
struct timespec ts;
ts.tv_sec = (ms / 1000);
ts.tv_nsec = (ms % 1000) * 1000 * 1000;
return ts;
}
/* record current CPU time in start_time */
void start_cpu_timing (struct timespec *start_time);
/* add difference between start_time and the current CPU time to add_to */
void end_cpu_timing (const struct timespec *start_time, struct timespec *add_to);
// given a start and end time, add the difference to the third timespec
void timespec_add_elapsed(const struct timespec *start_time, const struct timespec *end_time, struct timespec *add_to);
void start_monotonic_timing(struct timespec *start_time);
void end_monotonic_timing (const struct timespec *start_time, struct timespec *add_to);
// start watch for stopWatch
void startWatch(struct timespec *start_time);
// return elapsed time and set start_time to current time
int64_t stopWatch(struct timespec *start_time);
int64_t lapWatch(struct timespec *start_time);
// get nanoseconds and some other stuff for use with srand
unsigned int get_seed();
void log_with_timestamp(const char *format, ...) __attribute__ ((format(printf, 1, 2)));
// based on a give epoch time in ms, calculate the nearest offset interval step
// offset must be smaller than interval, at offset seconds after the full minute
// is the first possible value, all additional return values differ by a multiple
// of interval
int64_t roundSeconds(int interval, int offset, int64_t epoch_ms);
ssize_t check_write(int fd, const void *buf, size_t count, const char *error_context);
int my_epoll_create();
void epollAllocEvents(struct epoll_event **events, int *maxEvents);
char *sprint_uuid(uint64_t id1, uint64_t id2, char *p);
char *sprint_uuid1_partial(uint64_t id1, char *p);
char *sprint_uuid1(uint64_t id1, char *p);
char *sprint_uuid2(uint64_t id2, char *p);
double greatcircle(double lat0, double lon0, double lat1, double lon1, int approx);
double bearing(double lat0, double lon0, double lat1, double lon1);
static inline int64_t imin(int64_t a, int64_t b) {
if (a < b)
return a;
else
return b;
}
static inline int64_t imax(int64_t a, int64_t b) {
if (a > b)
return a;
else
return b;
}
static inline double
norm_diff (double a, double pi)
{
if (a < -pi)
a += 2 * pi;
if (a > pi)
a -= 2 * pi;
return a;
}
static inline double
norm_angle (double a, double pi)
{
if (a < 0)
a += 2 * pi;
if (a >= 2 * pi)
a -= 2 * pi;
return a;
}
static inline void fprintTime(FILE *stream, int64_t now) {
fprintf(stream, "%02d:%02d:%04.1f",
(int) ((now / (3600 * SECONDS)) % 24),
(int) ((now / (60 * SECONDS)) % 60),
(now % (60 * SECONDS)) / 1000.0);
}
#endif