-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxqx_gps.c
194 lines (145 loc) · 3.47 KB
/
xqx_gps.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2021 Cyril Hrubis <metan@ucw.cz>
*/
#include <gps.h>
#include <widgets/gp_widgets.h>
#include "xqx_gps.h"
static struct gps_data_t gpsdata;
static LIST_HEAD(root);
static struct list_head *notify_root = &root;
static const char *gps_addr = "127.0.0.1";
static const char *gps_port = "2947";
static unsigned int gps_reconnect_delay_ms = 10000;
static void gps_notify(struct xqx_gps_notify *nt, enum xqx_gps_msg_type type, void *data)
{
nt->gps_msg_cb(nt, type, data);
}
static void gps_notify_broadcast(enum xqx_gps_msg_type type, void *data)
{
struct xqx_gps_notify *nt;
LIST_FOREACH(notify_root, i) {
nt = LIST_ENTRY(i, struct xqx_gps_notify, list);
gps_notify(nt, type, data);
}
}
static uint32_t gps_notify_no_data(gp_timer *self)
{
(void)self;
gps_notify_broadcast(XQX_GPS_MSG_NO_DATA, NULL);
return GP_TIMER_STOP;
}
static uint32_t gpsd_reconnect(gp_timer *self)
{
(void)self;
if (xqx_gps_connect())
return GP_TIMER_STOP;
else
return gps_reconnect_delay_ms;
}
static struct gp_timer gps_read_timeout = {
.callback = gps_notify_no_data,
.id = "gpsd read timeout"
};
static struct gp_timer gps_reconnect = {
.callback = gpsd_reconnect,
.id = "gpsd reconnect",
};
static void gps_schedulle_reconnect(void)
{
if (!gps_reconnect_delay_ms)
return;
gps_reconnect.expires = gps_reconnect_delay_ms;
gp_widgets_timer_ins(&gps_reconnect);
}
static void gps_cancel_reconnect(void)
{
if (!gps_reconnect_delay_ms)
return;
gp_widgets_timer_rem(&gps_reconnect);
}
static enum gp_poll_event_ret read_gps(struct gp_fd *self)
{
int ret;
(void) self;
gp_widgets_timer_rem(&gps_read_timeout);
#if GPSD_API_MAJOR_VERSION >= 10
ret = gps_read(&gpsdata, NULL, 0);
#else
ret = gps_read(&gpsdata);
#endif
if (ret < 0) {
gps_close(&gpsdata);
gpsdata.gps_fd = 0;
gps_notify_broadcast(XQX_GPS_MSG_DISCONNECTED, NULL);
gps_schedulle_reconnect();
return 1;
}
gps_notify_broadcast(XQX_GPS_MSG_FIX, &gpsdata.fix);
gps_read_timeout.expires = 5000;
gp_widgets_timer_ins(&gps_read_timeout);
return 0;
}
static int gps_is_connected(void)
{
return gpsdata.gps_fd != 0;
}
static gp_fd gps_fd = {
.events = GP_POLLIN,
.event = read_gps,
};
static void gps_connect(void)
{
if (gps_open(gps_addr, gps_port, &gpsdata)) {
gpsdata.gps_fd = 0;
return;
}
gps_stream(&gpsdata, WATCH_ENABLE, NULL);
gps_fd.fd = gpsdata.gps_fd;
gp_widget_poll_add(&gps_fd);
gp_widgets_timer_ins(&gps_read_timeout);
}
void gps_disconnect(void)
{
gp_widget_poll_rem(&gps_fd);
gps_close(&gpsdata);
gpsdata.gps_fd = 0;
}
void xqx_gps_init(const char *addr, const char *port, unsigned int reconnect_delay)
{
if (gps_is_connected())
return;
gps_addr = addr;
gps_port = port;
gps_reconnect_delay_ms = reconnect_delay * 1000;
}
void xqx_gps_unregister_notify(struct xqx_gps_notify *notify)
{
list_remove(¬ify->list);
}
void xqx_gps_register_notify(struct xqx_gps_notify *notify)
{
list_append(notify_root, ¬ify->list);
if (gps_is_connected())
gps_notify(notify, XQX_GPS_MSG_CONNECTED, NULL);
else
gps_notify(notify, XQX_GPS_MSG_DISCONNECTED, NULL);
}
int xqx_gps_connect(void)
{
if (gps_is_connected())
return 1;
gps_connect();
if (!gps_is_connected()) {
gps_schedulle_reconnect();
return 0;
}
gps_notify_broadcast(XQX_GPS_MSG_CONNECTED, NULL);
return 1;
}
void xqx_gps_disconnect(void)
{
gps_disconnect();
gps_cancel_reconnect();
gps_notify_broadcast(XQX_GPS_MSG_DISCONNECTED, NULL);
}