lib/reticulum/src/wire/context.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 
 3 /// The header's last byte names what the payload is for: nothing in particular,
 4 /// one of the seven resource-transfer kinds, a cache request, a request or its
 5 /// response, a path response, a command or its status, a channel message, a
 6 /// keepalive, or one of the five link control values, following Reticulum@1.5.0
 7 /// RNS/Packet.py:72-92. A caller reads this byte to learn what an arriving
 8 /// payload is for, and sets it on a payload it sends. A byte this list leaves
 9 /// unnamed decodes to a value that encodes back to the same byte, so a payload
10 /// from a peer running a newer build survives the trip through this code.
11 pub const Context = enum(u8) {
12     none = 0x00,
13     resource = 0x01,
14     resource_adv = 0x02,
15     resource_req = 0x03,
16     resource_hmu = 0x04,
17     resource_prf = 0x05,
18     resource_icl = 0x06,
19     resource_rcl = 0x07,
20     cache_request = 0x08,
21     request = 0x09,
22     response = 0x0a,
23     path_response = 0x0b,
24     command = 0x0c,
25     command_status = 0x0d,
26     channel = 0x0e,
27     keepalive = 0xfa,
28     linkidentify = 0xfb,
29     linkclose = 0xfc,
30     linkproof = 0xfd,
31     lrrtt = 0xfe,
32     lrproof = 0xff,
33     _,
34 
35     pub fn decode(byte: u8) Context {
36         return @fromBackingInt(@intCast(byte));
37     }
38 
39     pub fn encode(self: Context) u8 {
40         return @backingInt(self);
41     }
42 };
43 
44 test "Reticulum@1.5.0 RNS/Packet.py:72-92 preserves unknown contexts" {
45     const unknown = Context.decode(0x80);
46     try std.testing.expectEqual(@as(u8, 0x80), unknown.encode());
47 }