Compare commits
3 commits
dad6a6819c
...
ee3cba3cab
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee3cba3cab | ||
|
|
43b3ca386d | ||
|
|
ad6eb69ec7 |
4 changed files with 91 additions and 51 deletions
56
build.zig
56
build.zig
|
|
@ -1,36 +1,48 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn build(b: *std.Build) void {
|
pub fn build(b: *std.Build) void {
|
||||||
|
|
||||||
|
// Compilation options
|
||||||
|
const opt_shared = b.option(
|
||||||
|
bool,
|
||||||
|
"shared",
|
||||||
|
"Build as a dynamic (shared) library",
|
||||||
|
) orelse false;
|
||||||
|
|
||||||
|
const linkage: std.builtin.LinkMode = if (opt_shared)
|
||||||
|
.static
|
||||||
|
else
|
||||||
|
.dynamic;
|
||||||
|
|
||||||
|
// Configuration
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
// Create library
|
// Create root module
|
||||||
const reticulumzero = b.addLibrary(.{
|
const root_module = b.createModule(.{
|
||||||
.name = "reticulum-zero",
|
|
||||||
.linkage = .static,
|
|
||||||
.root_module = b.createModule(.{
|
|
||||||
.root_source_file = b.path("src/root.zig"),
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Output library object
|
|
||||||
b.installArtifact(reticulumzero);
|
|
||||||
|
|
||||||
// Expose library as a module
|
|
||||||
try b.modules.put(b.dupe("reticulum-zero"), reticulumzero.root_module);
|
|
||||||
|
|
||||||
// Set up unit testing
|
|
||||||
const unit_tests = b.addTest(.{
|
|
||||||
.root_source_file = b.path("src/root.zig"),
|
.root_source_file = b.path("src/root.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
const run_unit_tests = b.addRunStep(unit_tests);
|
// Create library
|
||||||
|
const lib = b.addLibrary(.{
|
||||||
|
.name = "reticulum-zero",
|
||||||
|
.linkage = linkage,
|
||||||
|
.root_module = root_module,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Output library object
|
||||||
|
b.installArtifact(lib);
|
||||||
|
|
||||||
|
// Set up unit testing
|
||||||
|
// const unit_tests = b.addTest(.{
|
||||||
|
// .root_module = root_module,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const run_unit_tests = b.addRunStep(unit_tests);
|
||||||
|
|
||||||
// `zig build test` command
|
// `zig build test` command
|
||||||
const test_step = b.step("test", "Run library unit tests");
|
// const test_step = b.step("test", "Run library unit tests");
|
||||||
test_step.dependency(&run_unit_tests.step);
|
// test_step.dependency(&run_unit_tests.step);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
.{
|
.{
|
||||||
.name = "reticulum-zero",
|
.name = .reticulumzero,
|
||||||
.version = "0.0.0",
|
.version = "0.0.0",
|
||||||
.minimum_zig_version = "0.16.0",
|
.minimum_zig_version = "0.16.0",
|
||||||
|
.fingerprint = 0x7bbb9f9a7c7a4705,
|
||||||
.dependencies = .{},
|
.dependencies = .{},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
"build.zig",
|
"build.zig",
|
||||||
|
|
|
||||||
|
|
@ -30,14 +30,14 @@ const ContextFlag = enum(u1) { // Meaning depends on packet context
|
||||||
};
|
};
|
||||||
const PropagationType = enum(u1) { broadcast = 0, transport = 1 };
|
const PropagationType = enum(u1) { broadcast = 0, transport = 1 };
|
||||||
|
|
||||||
const DestinationType = enum(u2) { //
|
const DestinationType = enum(u2) {
|
||||||
single = 0b0,
|
single = 0b0,
|
||||||
group = 0b01,
|
group = 0b01,
|
||||||
plain = 0b10,
|
plain = 0b10,
|
||||||
link = 0b11,
|
link = 0b11,
|
||||||
};
|
};
|
||||||
|
|
||||||
const PacketType = enum(u2) { //
|
const PacketType = enum(u2) {
|
||||||
data = 0b0,
|
data = 0b0,
|
||||||
announce = 0b01,
|
announce = 0b01,
|
||||||
link_request = 0b10,
|
link_request = 0b10,
|
||||||
|
|
@ -45,27 +45,27 @@ const PacketType = enum(u2) { //
|
||||||
};
|
};
|
||||||
|
|
||||||
const PacketContext = enum(u8) {
|
const PacketContext = enum(u8) {
|
||||||
none = 0x00, // Generic data packet
|
none = 0x00, // Generic data packet
|
||||||
resource = 0x01, // Packet is part of a resource
|
resource = 0x01, // Packet is part of a resource
|
||||||
resource_adv = 0x02, // Packet is a resource advertisement
|
resource_adv = 0x02, // Packet is a resource advertisement
|
||||||
resource_req = 0x03, // Packet is a resource part request
|
resource_req = 0x03, // Packet is a resource part request
|
||||||
resource_hmu = 0x04, // Packet is a resource hashmap update
|
resource_hmu = 0x04, // Packet is a resource hashmap update
|
||||||
resource_prf = 0x05, // Packet is a resource proof
|
resource_prf = 0x05, // Packet is a resource proof
|
||||||
resource_icl = 0x06, // Packet is a resource initiator cancel message
|
resource_icl = 0x06, // Packet is a resource initiator cancel message
|
||||||
resource_rcl = 0x07, // Packet is a resource receiver cancel message
|
resource_rcl = 0x07, // Packet is a resource receiver cancel message
|
||||||
cache_request = 0x08, // Packet is a cache request
|
cache_request = 0x08, // Packet is a cache request
|
||||||
request = 0x09, // Packet is a request
|
request = 0x09, // Packet is a request
|
||||||
response = 0x0A, // Packet is a response to a request
|
response = 0x0A, // Packet is a response to a request
|
||||||
path_response = 0x0B, // Packet is a response to a path request
|
path_response = 0x0B, // Packet is a response to a path request
|
||||||
command = 0x0C, // Packet is a command
|
command = 0x0C, // Packet is a command
|
||||||
command_status = 0x0D, // Packet is a status of an executed command
|
command_status = 0x0D, // Packet is a status of an executed command
|
||||||
channel = 0x0E, // Packet contains link channel data
|
channel = 0x0E, // Packet contains link channel data
|
||||||
keepalive = 0xFA, // Packet is a keepalive packet
|
keepalive = 0xFA, // Packet is a keepalive packet
|
||||||
linkidentify = 0xFB, // Packet is a link peer identification proof
|
linkidentify = 0xFB, // Packet is a link peer identification proof
|
||||||
linkclose = 0xFC, // Packet is a link close message
|
linkclose = 0xFC, // Packet is a link close message
|
||||||
linkproof = 0xFD, // Packet is a link packet proof
|
linkproof = 0xFD, // Packet is a link packet proof
|
||||||
lrrtt = 0xFE, // Packet is a link request round-trip time measurement
|
lrrtt = 0xFE, // Packet is a link request round-trip time measurement
|
||||||
lrproof = 0xFF, // Packet is a link request proof
|
lrproof = 0xFF, // Packet is a link request proof
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////// Structs
|
///////////////// Structs
|
||||||
|
|
@ -78,7 +78,7 @@ const PacketHeader = packed struct {
|
||||||
propagation: PropagationType,
|
propagation: PropagationType,
|
||||||
destination: DestinationType,
|
destination: DestinationType,
|
||||||
packet: PacketType,
|
packet: PacketType,
|
||||||
hops: u8, //
|
hops: u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Packet = struct {
|
const Packet = struct {
|
||||||
|
|
@ -86,7 +86,7 @@ const Packet = struct {
|
||||||
address1: [ADDRESS_SIZE]u8,
|
address1: [ADDRESS_SIZE]u8,
|
||||||
address2: [ADDRESS_SIZE]u8,
|
address2: [ADDRESS_SIZE]u8,
|
||||||
context: PacketContext,
|
context: PacketContext,
|
||||||
data: []const u8, //
|
data: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////// Functions
|
///////////////// Functions
|
||||||
|
|
@ -133,10 +133,12 @@ pub fn serializePacket(packet: Packet, output_buffer: []u8) !usize {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copyToPacket(dst: anytype, src: []const u8, offset: usize, count: usize) !usize {
|
fn copyToPacket(dst: anytype, src: []const u8, offset: usize, count: usize) !usize {
|
||||||
if (src.len > offset + count) {
|
if (src.len < offset + count) {
|
||||||
return error.BufferTooShort;
|
return error.BufferTooShort;
|
||||||
}
|
}
|
||||||
@memcpy(@as(*u8, &dst), src[offset .. offset + count]);
|
|
||||||
|
const dst_ptr: []u8 = @ptrCast(dst);
|
||||||
|
@memcpy(dst_ptr, src[offset .. offset + count]);
|
||||||
|
|
||||||
return offset + count;
|
return offset + count;
|
||||||
}
|
}
|
||||||
|
|
@ -160,6 +162,9 @@ pub fn deserializePacket(message_buffer: []u8, dest_packet: *Packet) !void {
|
||||||
///////////////// Tests
|
///////////////// Tests
|
||||||
//
|
//
|
||||||
|
|
||||||
|
//////// Helper functions
|
||||||
|
//
|
||||||
|
|
||||||
// Serializes the given packet and makes sure the output is correct
|
// Serializes the given packet and makes sure the output is correct
|
||||||
fn testPacketSerialization(packet: Packet) !void {
|
fn testPacketSerialization(packet: Packet) !void {
|
||||||
const buf_size = comptime @sizeOf(Packet) + MAX_DATA_SIZE;
|
const buf_size = comptime @sizeOf(Packet) + MAX_DATA_SIZE;
|
||||||
|
|
@ -188,6 +193,28 @@ fn testPacketSerialization(packet: Packet) !void {
|
||||||
offset += packet.data.len;
|
offset += packet.data.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn headersEql(h1: *const PacketHeader, h2: *const PacketHeader) bool {
|
||||||
|
return h1.ifac == h2.ifac
|
||||||
|
and h1.header == h2.header
|
||||||
|
and h1.context == h2.context
|
||||||
|
and h1.propagation == h2.propagation
|
||||||
|
and h1.destination == h2.destination
|
||||||
|
and h1.packet == h2.packet
|
||||||
|
and h1.hops == h2.hops;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn packetsEql(p1: *const Packet, p2: *const Packet) bool {
|
||||||
|
return headersEql(&p1.header, &p2.header)
|
||||||
|
and memeql(u8, &p1.address1, &p2.address1)
|
||||||
|
and memeql(u8, &p1.address2, &p2.address2)
|
||||||
|
and p1.context == p2.context
|
||||||
|
and p1.data.len == p2.data.len
|
||||||
|
and memeql(u8, p1.data, p2.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
//////// Tests
|
||||||
|
//
|
||||||
|
|
||||||
test "Structs size" {
|
test "Structs size" {
|
||||||
try expect(@sizeOf(PacketHeader) == 2);
|
try expect(@sizeOf(PacketHeader) == 2);
|
||||||
}
|
}
|
||||||
|
|
@ -320,5 +347,5 @@ test "Serialize / Deserialize Packet: Header type2, Medium data size" {
|
||||||
_ = try serializePacket(packet, &buf);
|
_ = try serializePacket(packet, &buf);
|
||||||
try deserializePacket(&buf, &res_packet);
|
try deserializePacket(&buf, &res_packet);
|
||||||
|
|
||||||
try expect(res_packet == packet);
|
try expect(packetsEql(&res_packet, &packet));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
const packet = @import("packet.zig");
|
pub const packet = @import("packet.zig");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue