-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquectel.zig
More file actions
86 lines (75 loc) · 2.89 KB
/
quectel.zig
File metadata and controls
86 lines (75 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const drivers = @import("drivers");
const DriverModem = drivers.Modem;
const core_mod = @import("src/core.zig");
const modem_mod = @import("src/Modem.zig");
const mux_mod = @import("src/mux.zig");
const ppp_mod = @import("src/ppp.zig");
const power_mod = @import("src/power.zig");
pub const Transport = @import("src/Transport.zig");
pub const Modem = modem_mod;
pub const core = core_mod;
pub const mux = mux_mod;
pub const ppp = ppp_mod;
pub const power = power_mod;
pub const CommandSet = enum {
core,
mux,
ppp,
power,
};
pub fn make(comptime command_sets: anytype) type {
const info = @typeInfo(@TypeOf(command_sets));
if (info != .@"struct" or !info.@"struct".is_tuple)
@compileError("quectel.make expects a tuple of command sets like .{ .mux }");
const has_core = hasCommandSet(command_sets, .core);
const has_mux = hasCommandSet(command_sets, .mux);
const has_ppp = hasCommandSet(command_sets, .ppp);
const has_power = hasCommandSet(command_sets, .power);
return struct {
transport: Transport,
core: if (has_core) core_mod.CommandSet else void = if (has_core) undefined else {},
mux: if (has_mux) mux_mod.CommandSet else void = if (has_mux) undefined else {},
ppp: if (has_ppp) ppp_mod.CommandSet else void = if (has_ppp) undefined else {},
power: if (has_power) power_mod.CommandSet else void = if (has_power) undefined else {},
const Self = @This();
pub fn init(link: Transport) Self {
var self: Self = .{
.transport = link,
};
inline for (command_sets) |command_set| {
@field(self, @tagName(command_set)) = commandSetInit(command_set, link);
}
return self;
}
pub fn modem(self: *Self, comptime lib: type, allocator: lib.mem.Allocator) !DriverModem {
const ModemType = modem_mod.make(lib, Self);
return ModemType.init(allocator, self);
}
};
}
fn hasCommandSet(comptime command_sets: anytype, comptime want: CommandSet) bool {
inline for (command_sets) |command_set| {
if (command_set == want) return true;
}
return false;
}
fn commandSetType(comptime command_set: CommandSet) type {
return switch (command_set) {
.core => core_mod.CommandSet,
.mux => mux_mod.CommandSet,
.ppp => ppp_mod.CommandSet,
.power => power_mod.CommandSet,
};
}
fn commandSetInit(comptime command_set: CommandSet, link: Transport) commandSetType(command_set) {
return switch (command_set) {
.core => core_mod.CommandSet.init(link),
.mux => mux_mod.CommandSet.init(link),
.ppp => ppp_mod.CommandSet.init(link),
.power => power_mod.CommandSet.init(link),
};
}
pub const test_runner = struct {
pub const unit = @import("test_runner/unit.zig");
pub const integration = @import("test_runner/integration.zig");
};