From b42b30c251662129e04e29ab6e0559a7239e235a Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 8 Dec 2024 13:58:12 -0700 Subject: [PATCH] Initial commit --- .gitignore | 4 ++ README.md | 20 +++++++ gleam.toml | 19 ++++++ manifest.toml | 11 ++++ src/ccsds_space_packet.gleam | 95 ++++++++++++++++++++++++++++++ test/ccsds_space_packet_test.gleam | 12 ++++ 6 files changed, 161 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 gleam.toml create mode 100644 manifest.toml create mode 100644 src/ccsds_space_packet.gleam create mode 100644 test/ccsds_space_packet_test.gleam diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..599be4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +/build +erl_crash.dump diff --git a/README.md b/README.md new file mode 100644 index 0000000..38b7499 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# ccsds_space_packet +Basic CCSDS Space Packet Parsing in Gleam! + +```sh +gleam add ccsds_space_packet@1 +``` +```gleam +import ccsds_space_packet + +pub fn main() { + // TODO: An example of the project in use +} +``` + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +``` diff --git a/gleam.toml b/gleam.toml new file mode 100644 index 0000000..c09a807 --- /dev/null +++ b/gleam.toml @@ -0,0 +1,19 @@ +name = "ccsds_space_packet" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "", repo = "" } +# links = [{ title = "Website", href = "" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = ">= 0.34.0 and < 2.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..4771009 --- /dev/null +++ b/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.46.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "53940A91251A6BE9AEBB959D46E1CB45B510551D81342A52213850947732D4AB" }, + { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, +] + +[requirements] +gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/src/ccsds_space_packet.gleam b/src/ccsds_space_packet.gleam new file mode 100644 index 0000000..d07cae2 --- /dev/null +++ b/src/ccsds_space_packet.gleam @@ -0,0 +1,95 @@ +import gleam/bool +import gleam/int +import gleam/io +import gleam/string_tree + +pub type SpacePacketVersion { + VersionZero +} + +pub type SpacePacketType { + Telemetry + Command +} + +pub type SpacePacketSequenceFlags { + Continuation + FirstSegment + LastSegment + Unsegmented +} + +pub type SpacePacketHeader { + SpacePacketHeader( + version: SpacePacketVersion, + sp_type: SpacePacketType, + sec_hdr_flag: Bool, + apid: Int, + seq_flag: SpacePacketSequenceFlags, + seq_count: Int, + packet_len: Int, + ) +} + +pub fn version_to_string(sp_version: SpacePacketVersion) -> String { + case sp_version { + VersionZero -> "v0" + } +} + +pub fn packet_type_to_string(sp_type: SpacePacketType) -> String { + case sp_type { + Telemetry -> "Telemetry" + Command -> "Command" + } +} + +pub fn seq_flag_to_string(sp_seq_flags: SpacePacketSequenceFlags) -> String { + case sp_seq_flags { + Continuation -> "Continuation" + FirstSegment -> "First Segment" + LastSegment -> "Last Segment" + Unsegmented -> "Unsegmented" + } +} + +pub fn space_packet_header_to_string(sp: SpacePacketHeader) -> String { + string_tree.new() + |> string_tree.append("Version: ") + |> string_tree.append(version_to_string(sp.version)) + |> string_tree.append("\n") + |> string_tree.append("Type: ") + |> string_tree.append(packet_type_to_string(sp.sp_type)) + |> string_tree.append("\n") + |> string_tree.append("Secondary Header Flag: ") + |> string_tree.append(bool.to_string(sp.sec_hdr_flag)) + |> string_tree.append("\n") + |> string_tree.append("APID: 0x") + |> string_tree.append(int.to_base16(sp.apid)) + |> string_tree.append("\n") + |> string_tree.append("Sequence Flags: ") + |> string_tree.append(seq_flag_to_string(sp.seq_flag)) + |> string_tree.append("\n") + |> string_tree.append("Sequence Count: ") + |> string_tree.append(int.to_string(sp.seq_count)) + |> string_tree.append("\n") + |> string_tree.append("Packet Length: ") + |> string_tree.append(int.to_string(sp.packet_len)) + |> string_tree.append("\n") + |> string_tree.to_string() +} + +pub fn main() { + let space_packet = + SpacePacketHeader( + VersionZero, + Telemetry, + False, + 0x55, + Unsegmented, + 0x00, + 0x00, + ) + + io.println(space_packet_header_to_string(space_packet)) +} diff --git a/test/ccsds_space_packet_test.gleam b/test/ccsds_space_packet_test.gleam new file mode 100644 index 0000000..3831e7a --- /dev/null +++ b/test/ccsds_space_packet_test.gleam @@ -0,0 +1,12 @@ +import gleeunit +import gleeunit/should + +pub fn main() { + gleeunit.main() +} + +// gleeunit test functions end in `_test` +pub fn hello_world_test() { + 1 + |> should.equal(1) +}