X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fx_plane_data.erl;fp=src%2Fx_plane_data.erl;h=207673db855b1be9f38fb83f483d9742d1ddeb8e;hb=cfe4f77b7b6746c9cae311b37e92f8f0ce378680;hp=0000000000000000000000000000000000000000;hpb=3549a33ac1d2d49c8cef0b6cf679e90f9cc3c89a;p=erlang-x-plane-data.git diff --git a/src/x_plane_data.erl b/src/x_plane_data.erl new file mode 100644 index 0000000..207673d --- /dev/null +++ b/src/x_plane_data.erl @@ -0,0 +1,49 @@ +-module(x_plane_data). + +-include("x_plane_datum_defaults.hrl"). + +-export_type( + [ t/0 + ]). + +-export( + [ of_bin/1 + ]). + +-type parsing_error() :: + packet_unrecognized + | packet_length_invalid + | x_plane_datum:parsing_error() + . + +-type t() :: + [x_plane_datum:t()]. + +-define(BYTE_SIZE_OF_EACH_BLOCK, 36). + +-spec of_bin(binary()) -> + hope_result:t(t(), parsing_error()). +of_bin(<>) -> + of_bin(Packet, ?DEFAULT_MAX_INDEX). + +-spec of_bin(binary(), non_neg_integer()) -> + hope_result:t(t(), parsing_error()). +of_bin(<<"DATA", _PacketIndexByte:1/bytes, ContiguousBlocks/binary>>, MaxIndex) -> + % Packet index byte seems to be changing from X-Plane version to verion. + % What is it's meaning? + if byte_size(ContiguousBlocks) rem ?BYTE_SIZE_OF_EACH_BLOCK =:= 0 -> + Blocks = blocks_split(ContiguousBlocks), + ParseBlock = fun (B) -> x_plane_datum:of_bin(B, MaxIndex) end, + hope_list:map_result(Blocks, ParseBlock) + ; true -> + {error, packet_length_invalid} + end; +of_bin(<<_/binary>>, _) -> + {error, packet_unrecognized}. + +-spec blocks_split(binary()) -> + [binary()]. +blocks_split(<<>>) -> + []; +blocks_split(<>) -> + [Block | blocks_split(Blocks)].