-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_manager.cpp
172 lines (135 loc) · 4.14 KB
/
wifi_manager.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (c) 2024 David Leeds <davidesleeds@gmail.com>
*
* jstore is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <cstdio>
#include <optional>
#include <fmt/base.h>
#include <sdbus-c++/sdbus-c++.h>
#include <visit_struct/visit_struct.hpp>
#include <jstore.hpp>
using json = nlohmann::json;
/*
* ADL serializer for for std::optional<T>
*/
namespace nlohmann {
template <typename T>
void to_json(json &j, const std::optional<T> &v)
{
j = v ? json(*v) : json();
}
template <typename T>
void from_json(const json &j, std::optional<T> &v)
{
v = j.is_null() ? std::nullopt : std::optional<T>{j.get<T>()};
}
} /* namespace nlohmann */
/*
* Demo Wi-Fi manager configuration.
*/
namespace wifi {
enum class security {
WEP,
WPA,
WPA2,
WPA3
};
struct profile {
std::string name;
std::vector<uint8_t> ssid;
std::vector<uint8_t> psk;
security mode = security::WPA3;
bool operator==(const profile &) const = default;
};
struct config {
std::string country = "US";
std::optional<uint32_t> selected_profile;
std::map<uint32_t, profile> profiles;
bool operator==(const config &) const = default;
};
} /* namespace wifi */
/* visit_struct metadata defined outside of wifi namespace */
VISITABLE_STRUCT(wifi::profile, name, ssid, psk, mode);
VISITABLE_STRUCT(wifi::config, country, selected_profile, profiles);
/*
* Demo Wi-Fi manager application.
*/
namespace wifi {
class manager {
public:
static inline const sdbus::ServiceName DBUS_SERVICE { "com.example.WifiManager" };
static inline const sdbus::ObjectPath DBUS_OBJECT { "/com/example/WifiManager" };
manager(std::filesystem::path config_file) :
config_(std::move(config_file), config_error_handler),
dbus_conn_(sdbus::createBusConnection(DBUS_SERVICE)),
dbus_object_(sdbus::createObject(*dbus_conn_, DBUS_OBJECT))
{
fmt::println(stderr, "starting Wi-Fi manager");
/* Register the io.davidleeds.JStore interface with the D-Bus object */
config_.register_dbus(*dbus_object_);
/* Save and apply updated config when set via D-Bus */
config_.dbus().on_set([this](const std::string &path) {
config_.save();
apply();
});
}
const auto &config() const
{
return config_.root();
}
void run()
{
/*
* Consume calling thread to service the D-Bus connection. This is
* for demonstration purposes only, and a real application would likely
* integrate sdbus-c++ with its event loop to allow it to coexist with
* other events on the same thread.
*/
dbus_conn_->enterEventLoop();
}
void set_profile(uint32_t id, profile p)
{
config_->profiles[id] = std::move(p);
}
void select_profile(uint32_t id)
{
config_->selected_profile = id;
}
void apply()
{
fmt::println(stderr, "applying config: profile[{}] selected", config_->selected_profile.value_or(0));
/* TODO apply current configuration */
}
private:
/*
* Error callback for config serialization and deserialzation errors.
*/
static void config_error_handler(std::string &&msg)
{
fmt::println(stderr, "config error: {}", msg);
}
jstore::tree<struct config> config_;
std::unique_ptr<sdbus::IConnection> dbus_conn_{sdbus::createBusConnection(DBUS_SERVICE)};
std::unique_ptr<sdbus::IObject> dbus_object_{sdbus::createObject(*dbus_conn_, DBUS_OBJECT)};
};
} /* namespace wifi */
int main(int argc, char **argv)
{
/* Create hypothetical Wi-Fi manager application */
wifi::manager wm{"/tmp/test/jstore/wifi.conf"};
/* Add a default profile if none are present */
if (wm.config().profiles.empty()) {
wm.set_profile(42, {
.name = "Work",
.ssid = { 'I', 'n', 'i', 't', 'e', 'c', 'h' },
.psk = { 'S', 't', 'a', 'p', 'l', 'e', 'r' }
});
wm.select_profile(42);
wm.apply();
}
/* Enter the event loop to process incoming messages */
wm.run();
return 0;
}