-
Notifications
You must be signed in to change notification settings - Fork 6
/
metadata_input_gps_nsb.c
225 lines (189 loc) · 7.32 KB
/
metadata_input_gps_nsb.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
/* Copyright (c) 2015, Celerway, Kristian Evensen <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdint.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/time.h>
#include <unistd.h>
#include "backend_event_loop.h"
#include "metadata_exporter.h"
#include "metadata_input_gps_nsb.h"
#include "lib/minmea.h"
#include "metadata_exporter_log.h"
static void md_input_gps_nsb_handle_event(void *ptr, int32_t fd, uint32_t events)
{
struct md_input_gps_nsb *mign = ptr;
char rcv_buf[4096] = {0};
struct md_gps_event gps_event;
int8_t sentence_id = 0;
struct minmea_sentence_rmc rmc;
int32_t retval;
if (!mign->sockfd)
return;
retval = recv(mign->sockfd, rcv_buf, sizeof(rcv_buf), 0);
if (retval <= 0) {
if (mign->sockfd) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "NSB GPS error\n");
close(mign->sockfd);
mign->sockfd = 0;
mde_start_timer(mign->parent->event_loop, mign->sock_timeout_handle,
MD_GPS_NSB_SOCK_INTVL);
}
return;
}
sentence_id = minmea_sentence_id(rcv_buf, 0);
if (sentence_id <= 0) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "NSB sentence err. Sentence %s\n", rcv_buf);
return;
}
memset(&gps_event, 0, sizeof(struct md_gps_event));
gettimeofday(&gps_event.tstamp_tv, NULL);
gps_event.md_type = META_TYPE_POS;
//TODO: HACK until we implement better GPS handling in sqlite writer, NSB
//only export RMC as far as I can tell
gps_event.minmea_id = MINMEA_UNKNOWN;
//We can ignore NMEA checksum
switch (sentence_id) {
case MINMEA_SENTENCE_RMC:
retval = minmea_parse_rmc(&rmc, rcv_buf);
if (retval && !rmc.valid) {
retval = 0;
} else {
gps_event.time = rmc.time;
gps_event.latitude = minmea_tocoord(&(rmc.latitude));
gps_event.longitude = minmea_tocoord(&(rmc.longitude));
gps_event.speed = minmea_tofloat(&(rmc.speed));
}
break;
default:
retval = 0;
return;
}
if (!retval)
return;
gps_event.sequence = mde_inc_seq(mign->parent);
mde_publish_event_obj(mign->parent, (struct md_event *) &gps_event);
}
static uint8_t md_input_gps_nsb_create_socket(struct md_input_gps_nsb *mign)
{
int32_t sockfd = -1;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
if (getaddrinfo(mign->addr, mign->port, &hints, &res)) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "Could not get address info for NSB GPS\n");
return RETVAL_FAILURE;
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "Failed to create socket for NSB GPS\n");
return RETVAL_FAILURE;
}
if (bind(sockfd, res->ai_addr, res->ai_addrlen)) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "Failed to bind NSB GPS socket\n");
return RETVAL_FAILURE;
}
if (!(mign->event_handle)) {
if(!(mign->event_handle = backend_create_epoll_handle(mign,
sockfd, md_input_gps_nsb_handle_event)))
return RETVAL_FAILURE;
}
backend_event_loop_update(mign->parent->event_loop, EPOLLIN, EPOLL_CTL_ADD,
sockfd, mign->event_handle);
mign->sockfd = sockfd;
META_PRINT_SYSLOG(mign->parent, LOG_INFO, "NSB GPS socket %d\n", sockfd);
return RETVAL_SUCCESS;
}
static void md_input_gps_nsb_sock_timeout(void *ptr)
{
struct md_input_gps_nsb *mign = ptr;
if (md_input_gps_nsb_create_socket(mign) == RETVAL_FAILURE) {
mign->sock_timeout_handle->intvl = MD_GPS_NSB_SOCK_INTVL;
} else {
mign->sock_timeout_handle->intvl = 0;
}
}
static uint8_t md_input_gps_nsb_config(struct md_input_gps_nsb *mign,
const char *address,
const char *port)
{
//call create_socket
if (md_input_gps_nsb_create_socket(mign) == RETVAL_FAILURE) {
mde_start_timer(mign->parent->event_loop, mign->sock_timeout_handle,
MD_GPS_NSB_SOCK_INTVL);
}
return RETVAL_SUCCESS;
}
static uint8_t md_input_gps_nsb_init(void *ptr, json_object* config)
{
struct md_input_gps_nsb *mign = ptr;
const char *address = NULL, *port = NULL;
json_object* subconfig;
if (json_object_object_get_ex(config, "gps_nsb", &subconfig)) {
json_object_object_foreach(subconfig, key, val) {
if (!strcmp(key, "address"))
address = json_object_get_string(val);
else if (!strcmp(key, "port"))
port = json_object_get_string(val);
}
}
if (address == NULL || port == NULL) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "Missing required NSB GPS argument\n");
return RETVAL_FAILURE;
}
if (strlen(address) > (sizeof(mign->addr) - 1) ||
strlen(port) > (sizeof(mign->port) - 1)) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "NSB GPS argument too long\n");
return RETVAL_FAILURE;
}
memcpy(mign->addr, address, strlen(address));
memcpy(mign->port, port, strlen(port));
if(!(mign->sock_timeout_handle = backend_event_loop_create_timeout(0,
md_input_gps_nsb_sock_timeout, mign, 0))) {
META_PRINT_SYSLOG(mign->parent, LOG_ERR, "Failed to create NSB "
"timer\n");
return RETVAL_FAILURE;
}
return md_input_gps_nsb_config(mign, address, port);
}
void md_gps_nsb_usage()
{
fprintf(stderr, "\"gps_nsb\": {\t\tNSB GPS input\n");
fprintf(stderr, " \"address\":\t\tIP NSB broadcasts GPS to\n");
fprintf(stderr, " \"port\":\t\tPort NSB broadcasts GPS to\n");
fprintf(stderr, "},\n");
}
void md_gps_nsb_setup(struct md_exporter *mde, struct md_input_gps_nsb *mign)
{
mign->parent = mde;
mign->init = md_input_gps_nsb_init;
}