From a98eeec91e021a08f81dc36d1784b00d0bc4297f Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 23 Feb 2016 15:19:56 -0800 Subject: [PATCH] Proto protocol structure. --- adsbus/Makefile | 12 +- adsbus/adsb.pb-c.c | 382 +++++++++++++++++++++++++++++++++++++++++++++ adsbus/adsb.pb-c.h | 151 ++++++++++++++++++ adsbus/adsb.proto | 23 +++ adsbus/buf.h | 1 + adsbus/proto.c | 11 ++ adsbus/proto.h | 9 ++ 7 files changed, 585 insertions(+), 4 deletions(-) create mode 100644 adsbus/adsb.pb-c.c create mode 100644 adsbus/adsb.pb-c.h create mode 100644 adsbus/adsb.proto create mode 100644 adsbus/proto.c create mode 100644 adsbus/proto.h diff --git a/adsbus/Makefile b/adsbus/Makefile index 9092ded..d8c3840 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -1,11 +1,12 @@ CC ?= clang CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack-protector-strong -pthread LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now -LIBS ?= -ljansson +LIBS ?= -ljansson -lprotobuf-c OBJ_NETWORK = incoming.o outgoing.o receive.o send.o -OBJ_PROTOCOL = airspy_adsb.o beast.o json.o raw.o stats.o +OBJ_PROTOCOL = airspy_adsb.o beast.o json.o proto.o raw.o stats.o OBJ_UTIL = buf.o hex.o opts.o packet.o peer.o rand.o resolve.o server.o uuid.o wakeup.o +OBJ_PROTO = adsb.pb-c.o all: adsbus @@ -15,5 +16,8 @@ clean: %.o: %.c *.h $(CC) -c $(CFLAGS) $< -o $@ -adsbus: adsbus.o $(OBJ_NETWORK) $(OBJ_PROTOCOL) $(OBJ_UTIL) - $(CC) $(LDFLAGS) -o adsbus adsbus.o $(OBJ_NETWORK) $(OBJ_PROTOCOL) $(OBJ_UTIL) $(LIBS) +adsb.pb-c.c: adsb.proto + protoc-c --c_out=$(dir $<) $< + +adsbus: adsbus.o $(OBJ_NETWORK) $(OBJ_PROTOCOL) $(OBJ_UTIL) $(OBJ_PROTO) + $(CC) $(LDFLAGS) -o adsbus adsbus.o $(OBJ_NETWORK) $(OBJ_PROTOCOL) $(OBJ_UTIL) $(OBJ_PROTO) $(LIBS) diff --git a/adsbus/adsb.pb-c.c b/adsbus/adsb.pb-c.c new file mode 100644 index 0000000..1fbef9a --- /dev/null +++ b/adsbus/adsb.pb-c.c @@ -0,0 +1,382 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: adsb.proto */ + +/* Do not generate deprecated warnings for self */ +#ifndef PROTOBUF_C__NO_DEPRECATED +#define PROTOBUF_C__NO_DEPRECATED +#endif + +#include "adsb.pb-c.h" +void adsb_header__init + (AdsbHeader *message) +{ + static AdsbHeader init_value = ADSB_HEADER__INIT; + *message = init_value; +} +size_t adsb_header__get_packed_size + (const AdsbHeader *message) +{ + assert(message->base.descriptor == &adsb_header__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t adsb_header__pack + (const AdsbHeader *message, + uint8_t *out) +{ + assert(message->base.descriptor == &adsb_header__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t adsb_header__pack_to_buffer + (const AdsbHeader *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &adsb_header__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +AdsbHeader * + adsb_header__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (AdsbHeader *) + protobuf_c_message_unpack (&adsb_header__descriptor, + allocator, len, data); +} +void adsb_header__free_unpacked + (AdsbHeader *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &adsb_header__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void adsb_packet__init + (AdsbPacket *message) +{ + static AdsbPacket init_value = ADSB_PACKET__INIT; + *message = init_value; +} +size_t adsb_packet__get_packed_size + (const AdsbPacket *message) +{ + assert(message->base.descriptor == &adsb_packet__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t adsb_packet__pack + (const AdsbPacket *message, + uint8_t *out) +{ + assert(message->base.descriptor == &adsb_packet__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t adsb_packet__pack_to_buffer + (const AdsbPacket *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &adsb_packet__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +AdsbPacket * + adsb_packet__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (AdsbPacket *) + protobuf_c_message_unpack (&adsb_packet__descriptor, + allocator, len, data); +} +void adsb_packet__free_unpacked + (AdsbPacket *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &adsb_packet__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void adsb__init + (Adsb *message) +{ + static Adsb init_value = ADSB__INIT; + *message = init_value; +} +size_t adsb__get_packed_size + (const Adsb *message) +{ + assert(message->base.descriptor == &adsb__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t adsb__pack + (const Adsb *message, + uint8_t *out) +{ + assert(message->base.descriptor == &adsb__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t adsb__pack_to_buffer + (const Adsb *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &adsb__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +Adsb * + adsb__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (Adsb *) + protobuf_c_message_unpack (&adsb__descriptor, + allocator, len, data); +} +void adsb__free_unpacked + (Adsb *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &adsb__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +static const ProtobufCFieldDescriptor adsb_header__field_descriptors[6] = +{ + { + "magic", + 1, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_STRING, + 0, /* quantifier_offset */ + offsetof(AdsbHeader, magic), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "server_version", + 2, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_STRING, + 0, /* quantifier_offset */ + offsetof(AdsbHeader, server_version), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "server_id", + 3, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_STRING, + 0, /* quantifier_offset */ + offsetof(AdsbHeader, server_id), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "mlat_timestamp_mhz", + 4, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_FIXED32, + 0, /* quantifier_offset */ + offsetof(AdsbHeader, mlat_timestamp_mhz), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "mlat_timestamp_max", + 5, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_FIXED64, + 0, /* quantifier_offset */ + offsetof(AdsbHeader, mlat_timestamp_max), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "rssi_max", + 6, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_FIXED32, + 0, /* quantifier_offset */ + offsetof(AdsbHeader, rssi_max), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned adsb_header__field_indices_by_name[] = { + 0, /* field[0] = magic */ + 4, /* field[4] = mlat_timestamp_max */ + 3, /* field[3] = mlat_timestamp_mhz */ + 5, /* field[5] = rssi_max */ + 2, /* field[2] = server_id */ + 1, /* field[1] = server_version */ +}; +static const ProtobufCIntRange adsb_header__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 6 } +}; +const ProtobufCMessageDescriptor adsb_header__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "AdsbHeader", + "AdsbHeader", + "AdsbHeader", + "", + sizeof(AdsbHeader), + 6, + adsb_header__field_descriptors, + adsb_header__field_indices_by_name, + 1, adsb_header__number_ranges, + (ProtobufCMessageInit) adsb_header__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor adsb_packet__field_descriptors[4] = +{ + { + "source_id", + 1, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_STRING, + 0, /* quantifier_offset */ + offsetof(AdsbPacket, source_id), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "mlat_timestamp", + 2, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_FIXED64, + offsetof(AdsbPacket, has_mlat_timestamp), + offsetof(AdsbPacket, mlat_timestamp), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "rssi", + 3, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_FIXED32, + offsetof(AdsbPacket, has_rssi), + offsetof(AdsbPacket, rssi), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "payload", + 4, + PROTOBUF_C_LABEL_REQUIRED, + PROTOBUF_C_TYPE_BYTES, + 0, /* quantifier_offset */ + offsetof(AdsbPacket, payload), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned adsb_packet__field_indices_by_name[] = { + 1, /* field[1] = mlat_timestamp */ + 3, /* field[3] = payload */ + 2, /* field[2] = rssi */ + 0, /* field[0] = source_id */ +}; +static const ProtobufCIntRange adsb_packet__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 4 } +}; +const ProtobufCMessageDescriptor adsb_packet__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "AdsbPacket", + "AdsbPacket", + "AdsbPacket", + "", + sizeof(AdsbPacket), + 4, + adsb_packet__field_descriptors, + adsb_packet__field_indices_by_name, + 1, adsb_packet__number_ranges, + (ProtobufCMessageInit) adsb_packet__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor adsb__field_descriptors[3] = +{ + { + "header", + 1, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_MESSAGE, + 0, /* quantifier_offset */ + offsetof(Adsb, header), + &adsb_header__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "mode_s_short", + 2, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_MESSAGE, + 0, /* quantifier_offset */ + offsetof(Adsb, mode_s_short), + &adsb_packet__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "mode_s_long", + 3, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_MESSAGE, + 0, /* quantifier_offset */ + offsetof(Adsb, mode_s_long), + &adsb_packet__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned adsb__field_indices_by_name[] = { + 0, /* field[0] = header */ + 2, /* field[2] = mode_s_long */ + 1, /* field[1] = mode_s_short */ +}; +static const ProtobufCIntRange adsb__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 3 } +}; +const ProtobufCMessageDescriptor adsb__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "Adsb", + "Adsb", + "Adsb", + "", + sizeof(Adsb), + 3, + adsb__field_descriptors, + adsb__field_indices_by_name, + 1, adsb__number_ranges, + (ProtobufCMessageInit) adsb__init, + NULL,NULL,NULL /* reserved[123] */ +}; diff --git a/adsbus/adsb.pb-c.h b/adsbus/adsb.pb-c.h new file mode 100644 index 0000000..9b14fca --- /dev/null +++ b/adsbus/adsb.pb-c.h @@ -0,0 +1,151 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: adsb.proto */ + +#ifndef PROTOBUF_C_adsb_2eproto__INCLUDED +#define PROTOBUF_C_adsb_2eproto__INCLUDED + +#include + +PROTOBUF_C__BEGIN_DECLS + +#if PROTOBUF_C_VERSION_NUMBER < 1000000 +# error This file was generated by a newer version of protoc-c which is incompatible with your libprotobuf-c headers. Please update your headers. +#elif 1000002 < PROTOBUF_C_MIN_COMPILER_VERSION +# error This file was generated by an older version of protoc-c which is incompatible with your libprotobuf-c headers. Please regenerate this file with a newer version of protoc-c. +#endif + + +typedef struct _AdsbHeader AdsbHeader; +typedef struct _AdsbPacket AdsbPacket; +typedef struct _Adsb Adsb; + + +/* --- enums --- */ + + +/* --- messages --- */ + +struct _AdsbHeader +{ + ProtobufCMessage base; + char *magic; + char *server_version; + char *server_id; + uint32_t mlat_timestamp_mhz; + uint64_t mlat_timestamp_max; + uint32_t rssi_max; +}; +#define ADSB_HEADER__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&adsb_header__descriptor) \ + , NULL, NULL, NULL, 0, 0, 0 } + + +struct _AdsbPacket +{ + ProtobufCMessage base; + char *source_id; + protobuf_c_boolean has_mlat_timestamp; + uint64_t mlat_timestamp; + protobuf_c_boolean has_rssi; + uint32_t rssi; + ProtobufCBinaryData payload; +}; +#define ADSB_PACKET__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&adsb_packet__descriptor) \ + , NULL, 0,0, 0,0, {0,NULL} } + + +struct _Adsb +{ + ProtobufCMessage base; + AdsbHeader *header; + AdsbPacket *mode_s_short; + AdsbPacket *mode_s_long; +}; +#define ADSB__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&adsb__descriptor) \ + , NULL, NULL, NULL } + + +/* AdsbHeader methods */ +void adsb_header__init + (AdsbHeader *message); +size_t adsb_header__get_packed_size + (const AdsbHeader *message); +size_t adsb_header__pack + (const AdsbHeader *message, + uint8_t *out); +size_t adsb_header__pack_to_buffer + (const AdsbHeader *message, + ProtobufCBuffer *buffer); +AdsbHeader * + adsb_header__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void adsb_header__free_unpacked + (AdsbHeader *message, + ProtobufCAllocator *allocator); +/* AdsbPacket methods */ +void adsb_packet__init + (AdsbPacket *message); +size_t adsb_packet__get_packed_size + (const AdsbPacket *message); +size_t adsb_packet__pack + (const AdsbPacket *message, + uint8_t *out); +size_t adsb_packet__pack_to_buffer + (const AdsbPacket *message, + ProtobufCBuffer *buffer); +AdsbPacket * + adsb_packet__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void adsb_packet__free_unpacked + (AdsbPacket *message, + ProtobufCAllocator *allocator); +/* Adsb methods */ +void adsb__init + (Adsb *message); +size_t adsb__get_packed_size + (const Adsb *message); +size_t adsb__pack + (const Adsb *message, + uint8_t *out); +size_t adsb__pack_to_buffer + (const Adsb *message, + ProtobufCBuffer *buffer); +Adsb * + adsb__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void adsb__free_unpacked + (Adsb *message, + ProtobufCAllocator *allocator); +/* --- per-message closures --- */ + +typedef void (*AdsbHeader_Closure) + (const AdsbHeader *message, + void *closure_data); +typedef void (*AdsbPacket_Closure) + (const AdsbPacket *message, + void *closure_data); +typedef void (*Adsb_Closure) + (const Adsb *message, + void *closure_data); + +/* --- services --- */ + + +/* --- descriptors --- */ + +extern const ProtobufCMessageDescriptor adsb_header__descriptor; +extern const ProtobufCMessageDescriptor adsb_packet__descriptor; +extern const ProtobufCMessageDescriptor adsb__descriptor; + +PROTOBUF_C__END_DECLS + + +#endif /* PROTOBUF_C_adsb_2eproto__INCLUDED */ diff --git a/adsbus/adsb.proto b/adsbus/adsb.proto new file mode 100644 index 0000000..c7b05ec --- /dev/null +++ b/adsbus/adsb.proto @@ -0,0 +1,23 @@ +message AdsbHeader { + required string magic = 1; + required string server_version = 2; + required string server_id = 3; + required fixed32 mlat_timestamp_mhz = 4; + required fixed64 mlat_timestamp_max = 5; + required fixed32 rssi_max = 6; +} + +message AdsbPacket { + required string source_id = 1; + optional fixed64 mlat_timestamp = 2; + optional fixed32 rssi = 3; + required bytes payload = 4; +} + +message Adsb { + oneof record { + AdsbHeader header = 1; + AdsbPacket mode_s_short = 2; + AdsbPacket mode_s_long = 3; + } +} diff --git a/adsbus/buf.h b/adsbus/buf.h index bb967c7..bb18803 100644 --- a/adsbus/buf.h +++ b/adsbus/buf.h @@ -1,6 +1,7 @@ #pragma once #include +#include #define BUF_LEN_MAX 256 struct buf { diff --git a/adsbus/proto.c b/adsbus/proto.c new file mode 100644 index 0000000..73e9281 --- /dev/null +++ b/adsbus/proto.c @@ -0,0 +1,11 @@ +#include "buf.h" +#include "packet.h" + +#include "proto.h" + +bool proto_parse(struct buf *buf, struct packet *packet, void *state_in) { + return false; +} + +void proto_serialize(struct packet *packet, struct buf *buf) { +} diff --git a/adsbus/proto.h b/adsbus/proto.h new file mode 100644 index 0000000..73020a0 --- /dev/null +++ b/adsbus/proto.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +struct buf; +struct packet; + +bool proto_parse(struct buf *, struct packet *, void *); +void proto_serialize(struct packet *, struct buf *);