Skip to content

Commit 1144c86

Browse files
committed
feat: uslp ccsds standard
1 parent 4678989 commit 1144c86

2 files changed

Lines changed: 95 additions & 10 deletions

File tree

src/Ccsds.zig

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const std = @import("std");
44

55
const Ccsds = @This();
66

7+
packetType: pktType,
78
header: HeaderMetadata,
89
primaryHeader: []const u8,
910
secondaryHeader: ?[]const u8,
@@ -13,6 +14,24 @@ allocator: std.mem.Allocator,
1314

1415
pub fn init(pl: []const u8, allocator: std.mem.Allocator, config: ?Config) !Ccsds {
1516
var rawPackets = try allocator.dupe(u8, pl); // dupe so it doesn't go out of scope too early
17+
const primaryHeader = rawPackets[0..6];
18+
const headers = fetchHeader(rawPackets, config);
19+
const end = 5 + headers.header.packetSize; // num of header bytes + packet_size
20+
const packets = rawPackets[headers.start..end];
21+
22+
_ = allocator.resize(rawPackets, end);
23+
24+
return .{
25+
.header = headers.header,
26+
.primaryHeader = primaryHeader,
27+
.secondaryHeader = headers.secondaryHeader,
28+
.packets = packets,
29+
.rawData = rawPackets,
30+
.allocator = allocator,
31+
};
32+
}
33+
34+
pub fn fetchHeader(rawPackets: []u8, config: ?Config) struct {header: HeaderMetadata, secondaryHeader: ?[]const u8, start: u8} {
1635
const primaryHeader = rawPackets[0..6];
1736
const version = @as(u3, @truncate((primaryHeader[0] >> 5) & 0x07));
1837
const packetType = @as(u1, @truncate((primaryHeader[0] >> 4) & 0x01));
@@ -46,18 +65,11 @@ pub fn init(pl: []const u8, allocator: std.mem.Allocator, config: ?Config) !Ccsd
4665
.packetSequenceCount = packetSequenceCount,
4766
.packetSize = packetSize + 1,
4867
};
49-
const end = 5 + header.packetSize; // num of header bytes + packet_size
50-
const packets = rawPackets[start..end];
51-
52-
_ = allocator.resize(rawPackets, end);
5368

5469
return .{
55-
.header = header,
56-
.primaryHeader = primaryHeader,
57-
.secondaryHeader = secondaryHeader,
58-
.packets = packets,
59-
.rawData = rawPackets,
60-
.allocator = allocator,
70+
.header = header,
71+
.secondaryHeader = secondaryHeader,
72+
.start = start,
6173
};
6274
}
6375

@@ -83,6 +95,11 @@ pub const HeaderMetadata = packed struct {
8395
packetSize: u16,
8496
};
8597

98+
pub const pktType = enum {
99+
spacePkt,
100+
uslp
101+
};
102+
86103
/// If you choose to use the CCSDS config you need to call this function first to get the Config struct
87104
pub fn parseConfig(configContent: []const u8, allocator: std.mem.Allocator) !Config {
88105
const configParsed = try std.json.parseFromSlice(Config, allocator, configContent, .{});

src/Uslp.zig

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const std = @import("std");
2+
3+
pub const Uslp = @This();
4+
5+
primaryHeader: USLPHeader,
6+
insertZone: []const u8,
7+
dataHeader: DataHeader,
8+
TFDZ: []const u8,
9+
OpControl: u32,
10+
FrameError: u16,
11+
rawData: []const u8,
12+
allocator: std.mem.Allocator,
13+
14+
pub fn init(pl: []const u8, allocator: std.mem.Allocator) !Uslp{
15+
var rawPackets = try allocator.dupe(u8, pl); // dupe so it doesn't go out of scope too early
16+
const primaryHeader = rawPackets[1..6];
17+
const headers = fetchHeader(rawPackets);
18+
const end = 5 + headers.header.packetSize; // num of header bytes + packet_size
19+
const packets = rawPackets[headers.start..end];
20+
21+
_ = allocator.resize(rawPackets, end);
22+
23+
return .{
24+
.header = headers.header,
25+
.allocator = allocator,
26+
.rawData = rawPackets,
27+
};
28+
}
29+
30+
pub fn deinit(self: *Uslp) !void {
31+
self.allocator.free(self.rawData);
32+
}
33+
34+
pub fn fetchHeader(rawPackets: u8) struct {header: USLPHeader } {
35+
const header = rawPackets[0..6];
36+
const tfv = @as(u4, @truncate((header[0] >> 5) & 0x07));
37+
std.debug.print("%s", .{tfv});
38+
// const tfvn = rawPackets[0..3];
39+
// const scid = rawPackets[3..19];
40+
}
41+
42+
pub const DataHeader = packed struct {};
43+
44+
pub const USLPHeader = packed struct {
45+
xferFrameVersion: u4,
46+
scID: u16,
47+
srcID: u1,
48+
virtChannel: u6,
49+
mapID: u4,
50+
eofFlag: u1,
51+
frameLength: u16,
52+
bypassCntrl: u1,
53+
protocolCmd: u1,
54+
spare: u2,
55+
ocf: u1,
56+
VCFrameCountLength: u3,
57+
VCFrameCount: u56,
58+
};
59+
60+
61+
test "CCSDS Structure Testing w/o config" {
62+
const raw_test_packet: [16]u8 = .{ 0x78, 0x97, 0xC0, 0x00, 0x00, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A };
63+
var converted_test_packet = try Uslp.init(&raw_test_packet, std.testing.allocator, null);
64+
defer converted_test_packet.deinit();
65+
66+
try std.testing.expectEqual(3, converted_test_packet.header.version);
67+
68+
}

0 commit comments

Comments
 (0)