3232#include <sys/socket.h>
3333#include <netdb.h>
3434#include <sys/time.h>
35+ #include <unistd.h>
3536
3637#include "backend_event_loop.h"
3738#include "metadata_exporter.h"
@@ -48,10 +49,22 @@ static void md_input_gps_nsb_handle_event(void *ptr, int32_t fd, uint32_t events
4849 struct minmea_sentence_rmc rmc ;
4950 int32_t retval ;
5051
51- retval = recv (fd , rcv_buf , sizeof (rcv_buf ), 0 );
52+ if (!mign -> sockfd )
53+ return ;
54+
55+ retval = recv (mign -> sockfd , rcv_buf , sizeof (rcv_buf ), 0 );
56+
57+ if (retval <= 0 ) {
58+ if (mign -> sockfd ) {
59+ META_PRINT_SYSLOG (mign -> parent , LOG_ERR , "NSB GPS error\n" );
60+ close (mign -> sockfd );
61+ mign -> sockfd = 0 ;
62+ mde_start_timer (mign -> parent -> event_loop , mign -> sock_timeout_handle ,
63+ MD_GPS_NSB_SOCK_INTVL );
64+ }
5265
53- if (retval <= 0 )
5466 return ;
67+ }
5568
5669 sentence_id = minmea_sentence_id (rcv_buf , 0 );
5770
@@ -93,9 +106,7 @@ static void md_input_gps_nsb_handle_event(void *ptr, int32_t fd, uint32_t events
93106 mde_publish_event_obj (mign -> parent , (struct md_event * ) & gps_event );
94107}
95108
96- static uint8_t md_input_gps_nsb_config (struct md_input_gps_nsb * mign ,
97- const char * address ,
98- const char * port )
109+ static uint8_t md_input_gps_nsb_create_socket (struct md_input_gps_nsb * mign )
99110{
100111 int32_t sockfd = -1 ;
101112 struct addrinfo hints , * res ;
@@ -105,7 +116,7 @@ static uint8_t md_input_gps_nsb_config(struct md_input_gps_nsb *mign,
105116 hints .ai_socktype = SOCK_DGRAM ;
106117 hints .ai_protocol = IPPROTO_UDP ;
107118
108- if (getaddrinfo (address , port , & hints , & res )) {
119+ if (getaddrinfo (mign -> addr , mign -> port , & hints , & res )) {
109120 META_PRINT_SYSLOG (mign -> parent , LOG_ERR , "Could not get address info for NSB GPS\n" );
110121 return RETVAL_FAILURE ;
111122 }
@@ -122,17 +133,44 @@ static uint8_t md_input_gps_nsb_config(struct md_input_gps_nsb *mign,
122133 return RETVAL_FAILURE ;
123134 }
124135
125- if (!(mign -> event_handle = backend_create_epoll_handle (mign ,
126- sockfd , md_input_gps_nsb_handle_event )))
127- return RETVAL_FAILURE ;
136+ if (!(mign -> event_handle )) {
137+ if (!(mign -> event_handle = backend_create_epoll_handle (mign ,
138+ sockfd , md_input_gps_nsb_handle_event )))
139+ return RETVAL_FAILURE ;
140+ }
128141
129142 backend_event_loop_update (mign -> parent -> event_loop , EPOLLIN , EPOLL_CTL_ADD ,
130143 sockfd , mign -> event_handle );
131144
145+ mign -> sockfd = sockfd ;
132146 META_PRINT_SYSLOG (mign -> parent , LOG_INFO , "NSB GPS socket %d\n" , sockfd );
133147 return RETVAL_SUCCESS ;
134148}
135149
150+ static void md_input_gps_nsb_sock_timeout (void * ptr )
151+ {
152+ struct md_input_gps_nsb * mign = ptr ;
153+
154+ if (md_input_gps_nsb_create_socket (mign ) == RETVAL_FAILURE ) {
155+ mign -> sock_timeout_handle -> intvl = MD_GPS_NSB_SOCK_INTVL ;
156+ } else {
157+ mign -> sock_timeout_handle -> intvl = 0 ;
158+ }
159+ }
160+
161+ static uint8_t md_input_gps_nsb_config (struct md_input_gps_nsb * mign ,
162+ const char * address ,
163+ const char * port )
164+ {
165+ //call create_socket
166+ if (md_input_gps_nsb_create_socket (mign ) == RETVAL_FAILURE ) {
167+ mde_start_timer (mign -> parent -> event_loop , mign -> sock_timeout_handle ,
168+ MD_GPS_NSB_SOCK_INTVL );
169+ }
170+
171+ return RETVAL_SUCCESS ;
172+ }
173+
136174static uint8_t md_input_gps_nsb_init (void * ptr , json_object * config )
137175{
138176 struct md_input_gps_nsb * mign = ptr ;
@@ -153,6 +191,22 @@ static uint8_t md_input_gps_nsb_init(void *ptr, json_object* config)
153191 return RETVAL_FAILURE ;
154192 }
155193
194+ if (strlen (address ) > (sizeof (mign -> addr ) - 1 ) ||
195+ strlen (port ) > (sizeof (mign -> port ) - 1 )) {
196+ META_PRINT_SYSLOG (mign -> parent , LOG_ERR , "NSB GPS argument too long\n" );
197+ return RETVAL_FAILURE ;
198+ }
199+
200+ memcpy (mign -> addr , address , strlen (address ));
201+ memcpy (mign -> port , port , strlen (port ));
202+
203+ if (!(mign -> sock_timeout_handle = backend_event_loop_create_timeout (0 ,
204+ md_input_gps_nsb_sock_timeout , mign , 0 ))) {
205+ META_PRINT_SYSLOG (mign -> parent , LOG_ERR , "Failed to create NSB "
206+ "timer\n" );
207+ return RETVAL_FAILURE ;
208+ }
209+
156210 return md_input_gps_nsb_config (mign , address , port );
157211}
158212
0 commit comments