2015-06-26 18:50:51 +02:00
|
|
|
// Part of dump1090, a Mode S message decoder for RTLSDR devices.
|
|
|
|
//
|
|
|
|
// net_io.h: network handling.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2014,2015 Oliver Jowett <oliver@mutability.co.uk>
|
|
|
|
//
|
2017-06-15 19:16:51 +02:00
|
|
|
// This file is free software: you may copy, redistribute and/or modify it
|
2015-06-26 18:50:51 +02:00
|
|
|
// under the terms of the GNU General Public License as published by the
|
2017-06-15 19:16:51 +02:00
|
|
|
// Free Software Foundation, either version 2 of the License, or (at your
|
|
|
|
// option) any later version.
|
2015-06-26 18:50:51 +02:00
|
|
|
//
|
2017-06-15 19:16:51 +02:00
|
|
|
// 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
|
2015-06-26 18:50:51 +02:00
|
|
|
// General Public License for more details.
|
|
|
|
//
|
2017-06-15 19:16:51 +02:00
|
|
|
// You should have received a copy of the GNU General Public License
|
2015-06-26 18:50:51 +02:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#ifndef DUMP1090_NETIO_H
|
|
|
|
#define DUMP1090_NETIO_H
|
|
|
|
|
|
|
|
// Describes a networking service (group of connections)
|
|
|
|
|
2016-01-01 16:15:28 +01:00
|
|
|
struct aircraft;
|
2015-06-26 18:50:51 +02:00
|
|
|
struct modesMessage;
|
|
|
|
struct client;
|
2015-06-29 11:06:13 +02:00
|
|
|
struct net_service;
|
|
|
|
typedef int (*read_fn)(struct client *, char *);
|
|
|
|
typedef void (*heartbeat_fn)(struct net_service *);
|
2015-06-26 18:50:51 +02:00
|
|
|
|
2016-12-29 18:54:53 +01:00
|
|
|
typedef enum {
|
|
|
|
READ_MODE_IGNORE,
|
|
|
|
READ_MODE_BEAST,
|
2016-12-29 18:55:56 +01:00
|
|
|
READ_MODE_BEAST_COMMAND,
|
2016-12-29 18:54:53 +01:00
|
|
|
READ_MODE_ASCII
|
|
|
|
} read_mode_t;
|
|
|
|
|
2015-06-26 18:50:51 +02:00
|
|
|
// Describes one network service (a group of clients with common behaviour)
|
|
|
|
struct net_service {
|
|
|
|
struct net_service* next;
|
|
|
|
const char *descr;
|
2016-01-24 19:45:35 +01:00
|
|
|
int listener_count; // number of listeners
|
|
|
|
int *listener_fds; // listening FDs
|
2015-06-26 18:50:51 +02:00
|
|
|
|
|
|
|
int connections; // number of active clients
|
|
|
|
|
|
|
|
struct net_writer *writer; // shared writer state
|
|
|
|
|
|
|
|
const char *read_sep; // hander details for input data
|
2016-12-29 18:54:53 +01:00
|
|
|
read_mode_t read_mode;
|
2015-06-29 11:06:13 +02:00
|
|
|
read_fn read_handler;
|
2015-06-26 18:50:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Structure used to describe a networking client
|
|
|
|
struct client {
|
|
|
|
struct client* next; // Pointer to next client
|
|
|
|
int fd; // File descriptor
|
|
|
|
struct net_service *service; // Service this client is part of
|
|
|
|
int buflen; // Amount of data on buffer
|
|
|
|
char buf[MODES_CLIENT_BUF_SIZE+1]; // Read buffer
|
2016-12-29 18:55:56 +01:00
|
|
|
int modeac_requested; // 1 if this Beast output connection has asked for A/C
|
2015-06-26 18:50:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Common writer state for all output sockets of one type
|
|
|
|
struct net_writer {
|
|
|
|
struct net_service *service; // owning service
|
|
|
|
void *data; // shared write buffer, sized MODES_OUT_BUF_SIZE
|
|
|
|
int dataUsed; // number of bytes of write buffer currently used
|
|
|
|
uint64_t lastWrite; // time of last write to clients
|
2015-06-29 11:06:13 +02:00
|
|
|
heartbeat_fn send_heartbeat; // function that queues a heartbeat if needed
|
2015-06-26 18:50:51 +02:00
|
|
|
};
|
|
|
|
|
2016-12-29 18:54:53 +01:00
|
|
|
struct net_service *serviceInit(const char *descr, struct net_writer *writer, heartbeat_fn hb_handler, read_mode_t mode, const char *sep, read_fn read_handler);
|
2015-06-26 18:50:51 +02:00
|
|
|
struct client *serviceConnect(struct net_service *service, char *addr, int port);
|
2016-01-24 19:45:35 +01:00
|
|
|
void serviceListen(struct net_service *service, char *bind_addr, char *bind_ports);
|
2015-06-28 20:59:49 +02:00
|
|
|
struct client *createSocketClient(struct net_service *service, int fd);
|
|
|
|
struct client *createGenericClient(struct net_service *service, int fd);
|
2015-06-26 18:50:51 +02:00
|
|
|
|
2015-06-28 20:59:49 +02:00
|
|
|
// view1090 / faup1090 want to create these themselves:
|
2015-06-26 18:50:51 +02:00
|
|
|
struct net_service *makeBeastInputService(void);
|
2015-06-28 20:59:49 +02:00
|
|
|
struct net_service *makeFatsvOutputService(void);
|
2015-06-26 18:50:51 +02:00
|
|
|
|
2016-12-29 18:53:04 +01:00
|
|
|
void sendBeastSettings(struct client *c, const char *settings);
|
|
|
|
|
2015-06-26 18:50:51 +02:00
|
|
|
void modesInitNet(void);
|
2016-01-01 16:15:28 +01:00
|
|
|
void modesQueueOutput(struct modesMessage *mm, struct aircraft *a);
|
2015-06-26 18:50:51 +02:00
|
|
|
void modesNetPeriodicWork(void);
|
|
|
|
|
2018-01-09 18:07:47 +01:00
|
|
|
void writeFATSVHeader();
|
|
|
|
|
2015-06-26 18:50:51 +02:00
|
|
|
// TODO: move these somewhere else
|
|
|
|
char *generateAircraftJson(const char *url_path, int *len);
|
|
|
|
char *generateStatsJson(const char *url_path, int *len);
|
|
|
|
char *generateReceiverJson(const char *url_path, int *len);
|
|
|
|
char *generateHistoryJson(const char *url_path, int *len);
|
|
|
|
void writeJsonToFile(const char *file, char * (*generator) (const char *,int*));
|
|
|
|
|
|
|
|
#endif
|