Skip to content

Commit 1b430a8

Browse files
committed
Make bssid optional in payload for backwards compatibility, add tests,
1 parent e205509 commit 1b430a8

7 files changed

Lines changed: 171 additions & 31 deletions

File tree

components/CommandManager/CommandManager/CommandSchema.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
#include "CommandSchema.hpp"
22

3+
void to_json(nlohmann::json& j, const WifiPayload& payload)
4+
{
5+
j = nlohmann::json{
6+
{"name", payload.name}, {"ssid", payload.ssid}, {"bssid", payload.bssid},
7+
{"password", payload.password}, {"channel", payload.channel}, {"power", payload.power},
8+
};
9+
}
10+
11+
void from_json(const nlohmann::json& j, WifiPayload& payload)
12+
{
13+
payload.name = j.at("name").get<std::string>();
14+
payload.ssid = j.at("ssid").get<std::string>();
15+
payload.password = j.at("password").get<std::string>();
16+
payload.channel = j.at("channel").get<uint8_t>();
17+
payload.power = j.at("power").get<uint8_t>();
18+
19+
if (j.contains("bssid"))
20+
{
21+
payload.bssid = j.at("bssid").get<std::string>();
22+
}
23+
}
24+
325
void to_json(nlohmann::json& j, const UpdateWifiPayload& payload)
426
{
5-
j = nlohmann::json{{"name", payload.name}, {"ssid", payload.ssid}, {"password", payload.password}, {"channel", payload.channel}, {"power", payload.power}};
27+
j = nlohmann::json{
28+
{"name", payload.name}, {"ssid", payload.ssid}, {"bssid", payload.bssid},
29+
{"password", payload.password}, {"channel", payload.channel}, {"power", payload.power},
30+
};
631
}
732

833
void from_json(const nlohmann::json& j, UpdateWifiPayload& payload)
@@ -15,7 +40,7 @@ void from_json(const nlohmann::json& j, UpdateWifiPayload& payload)
1540

1641
if (j.contains("bssid"))
1742
{
18-
payload.ssid = j.at("bssid").get<std::string>();
43+
payload.bssid = j.at("bssid").get<std::string>();
1944
}
2045

2146
if (j.contains("password"))
@@ -59,7 +84,8 @@ void from_json(const nlohmann::json& j, UpdateAPWiFiPayload& payload)
5984
void to_json(nlohmann::json& j, const UpdateCameraConfigPayload& payload)
6085
{
6186
j = nlohmann::json{
62-
{"vflip", payload.vflip}, {"href", payload.href}, {"framesize", payload.framesize}, {"quality", payload.quality}, {"brightness", payload.brightness}};
87+
{"vflip", payload.vflip}, {"href", payload.href}, {"framesize", payload.framesize}, {"quality", payload.quality}, {"brightness", payload.brightness},
88+
};
6389
}
6490

6591
void from_json(const nlohmann::json& j, UpdateCameraConfigPayload& payload)

components/CommandManager/CommandManager/CommandSchema.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ struct WifiPayload : BasePayload
1010
{
1111
std::string name;
1212
std::string ssid;
13-
std::string bssid;
13+
std::optional<std::string> bssid;
1414
std::string password;
1515
uint8_t channel;
1616
uint8_t power;
1717
};
1818

19-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(WifiPayload, name, ssid, bssid, password, channel, power)
19+
void to_json(nlohmann::json& j, const WifiPayload& payload);
20+
void from_json(const nlohmann::json& j, WifiPayload& payload);
2021

2122
struct UpdateWifiPayload : BasePayload
2223
{

components/CommandManager/CommandManager/commands/wifi_commands.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ CommandResult setWiFiCommand(std::shared_ptr<DependencyRegistry> registry, const
1919
return CommandResult::getErrorResult("Invalid payload: missing SSID");
2020
}
2121

22-
auto bssid_len = payload.bssid.length();
23-
if (bssid_len > 0 && bssid_len != 11)
22+
// format is XX:XX:XX:XX:XX:XX
23+
const std::string bssid = payload.bssid.has_value() ? payload.bssid.value() : "";
24+
const auto bssid_len = bssid.length();
25+
if (bssid_len > 0 && bssid_len != 17)
2426
{
2527
return CommandResult::getErrorResult("BSSID is malformed");
2628
}
2729

2830
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
29-
projectConfig->setWifiConfig(payload.name, payload.ssid, payload.bssid, payload.password, payload.channel, payload.power);
31+
projectConfig->setWifiConfig(payload.name, payload.ssid, bssid, payload.password, payload.channel, payload.power);
3032

3133
return CommandResult::getSuccessResult("Config updated");
3234
}

tests/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
WIFI_SSID=
2+
WIFI_BSSID=
23
WIFI_PASS=
34
SWITCH_MODE_REBOOT_TIME=5
45
WIFI_CONNECTION_TIMEOUT=5

tests/conftest.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def pytest_addoption(parser):
3737

3838
def pytest_configure(config):
3939
config.addinivalue_line(
40-
"markers", "has_capability(caps): skip if the board does not have the capability"
40+
"markers",
41+
"has_capability(caps): skip if the board does not have the capability",
4142
)
4243
config.addinivalue_line(
4344
"markers", "lacks_capability(caps): skip if the board DOES have the capability"
@@ -60,7 +61,7 @@ def check_capability_marker(request, board_lacks_capability):
6061
"has_capability marker must be provided with a capability to check"
6162
)
6263

63-
for capability in marker.args:
64+
for capability in marker.args:
6465
if board_lacks_capability(capability):
6566
pytest.skip(f"Board does not have capability {capability}")
6667

@@ -72,7 +73,7 @@ def check_lacks_capability_marker(request, board_lacks_capability):
7273
raise ValueError(
7374
"lacks_capability marker must be provided with a capability to check"
7475
)
75-
76+
7677
for capability in lacks_capability_marker.args:
7778
if not board_lacks_capability(capability):
7879
pytest.skip(
@@ -119,6 +120,7 @@ def board_connection(request):
119120
@dataclass
120121
class TestConfig:
121122
WIFI_SSID: str
123+
WIFI_BSSID: str
122124
WIFI_PASS: str
123125
SWITCH_MODE_REBOOT_TIME: int
124126
WIFI_CONNECTION_TIMEOUT: int
@@ -127,12 +129,14 @@ class TestConfig:
127129
def __init__(
128130
self,
129131
WIFI_SSID: str,
132+
WIFI_BSSID: str,
130133
WIFI_PASS: str,
131134
SWITCH_MODE_REBOOT_TIME: int,
132135
WIFI_CONNECTION_TIMEOUT: int,
133136
INVALID_WIFI_CONNECTION_TIMEOUT: int,
134137
):
135138
self.WIFI_SSID = WIFI_SSID
139+
self.WIFI_BSSID = WIFI_BSSID
136140
self.WIFI_PASS = WIFI_PASS
137141
self.SWITCH_MODE_REBOOT_TIME = int(SWITCH_MODE_REBOOT_TIME)
138142
self.WIFI_CONNECTION_TIMEOUT = int(WIFI_CONNECTION_TIMEOUT)

tests/test_commands.py

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,39 @@ def test_reset_config(get_openiris_device, config):
255255

256256

257257
@pytest.mark.has_capability("wireless")
258-
def test_set_wifi(get_openiris_device, ensure_board_in_mode, config):
259-
# since we want to test actual connection to the network, let's reset the device and reboot it
258+
def test_set_wifi_no_bssid_in_payload(
259+
get_openiris_device, ensure_board_in_mode, config
260+
):
261+
device = get_openiris_device()
262+
reset_command = device.send_command("reset_config", {"section": "all"})
263+
assert not has_command_failed(reset_command)
264+
265+
with DetectPortChange():
266+
device.send_command("restart_device")
267+
time.sleep(config.SWITCH_MODE_REBOOT_TIME)
268+
269+
device = ensure_board_in_mode("wifi", device)
270+
params = {
271+
"name": "main",
272+
"ssid": config.WIFI_SSID,
273+
"password": config.WIFI_PASS,
274+
"channel": 0,
275+
"power": 0,
276+
}
277+
set_wifi_result = device.send_command("set_wifi", params)
278+
assert not has_command_failed(set_wifi_result)
279+
280+
connect_wifi_result = device.send_command("connect_wifi")
281+
assert not -has_command_failed(connect_wifi_result)
282+
time.sleep(config.WIFI_CONNECTION_TIMEOUT) # and let it try to for some time
283+
284+
wifi_status_command = device.send_command("get_wifi_status")
285+
assert not has_command_failed(wifi_status_command)
286+
assert wifi_status_command["results"][0]["result"]["data"]["status"] == "connected"
287+
288+
289+
@pytest.mark.has_capability("wireless")
290+
def test_set_wifi_no_bssid(get_openiris_device, ensure_board_in_mode, config):
260291
device = get_openiris_device()
261292
reset_command = device.send_command("reset_config", {"section": "all"})
262293
assert not has_command_failed(reset_command)
@@ -270,6 +301,7 @@ def test_set_wifi(get_openiris_device, ensure_board_in_mode, config):
270301
params = {
271302
"name": "main",
272303
"ssid": config.WIFI_SSID,
304+
"bssid": "",
273305
"password": config.WIFI_PASS,
274306
"channel": 0,
275307
"power": 0,
@@ -287,12 +319,76 @@ def test_set_wifi(get_openiris_device, ensure_board_in_mode, config):
287319
assert wifi_status_command["results"][0]["result"]["data"]["status"] == "connected"
288320

289321

322+
@pytest.mark.has_capability("wireless")
323+
def test_set_wifi_correct_bssid(get_openiris_device, ensure_board_in_mode, config):
324+
device = get_openiris_device()
325+
reset_command = device.send_command("reset_config", {"section": "all"})
326+
assert not has_command_failed(reset_command)
327+
328+
with DetectPortChange():
329+
device.send_command("restart_device")
330+
time.sleep(config.SWITCH_MODE_REBOOT_TIME)
331+
332+
device = ensure_board_in_mode("wifi", device)
333+
params = {
334+
"name": "main",
335+
"ssid": config.WIFI_SSID,
336+
"bssid": config.WIFI_BSSID,
337+
"password": config.WIFI_PASS,
338+
"channel": 0,
339+
"power": 0,
340+
}
341+
set_wifi_result = device.send_command("set_wifi", params)
342+
assert not has_command_failed(set_wifi_result)
343+
344+
connect_wifi_result = device.send_command("connect_wifi")
345+
assert not -has_command_failed(connect_wifi_result)
346+
time.sleep(config.WIFI_CONNECTION_TIMEOUT)
347+
348+
wifi_status_command = device.send_command("get_wifi_status")
349+
assert not has_command_failed(wifi_status_command)
350+
assert wifi_status_command["results"][0]["result"]["data"]["status"] == "connected"
351+
352+
353+
@pytest.mark.has_capability("wireless")
354+
def test_set_wifi_nonexitant_bssid(get_openiris_device, ensure_board_in_mode, config):
355+
device = get_openiris_device()
356+
reset_command = device.send_command("reset_config", {"section": "all"})
357+
assert not has_command_failed(reset_command)
358+
359+
with DetectPortChange():
360+
device.send_command("restart_device")
361+
time.sleep(config.SWITCH_MODE_REBOOT_TIME)
362+
363+
device = ensure_board_in_mode("wifi", device)
364+
params = {
365+
"name": "main",
366+
"ssid": config.WIFI_SSID,
367+
"bssid": "99:99:99:99:99:99", # a completely wrong BSSID, just to test that we fail to connect
368+
"password": config.WIFI_PASS,
369+
"channel": 0,
370+
"power": 0,
371+
}
372+
373+
set_wifi_result = device.send_command("set_wifi", params)
374+
assert not has_command_failed(set_wifi_result)
375+
376+
connect_wifi_result = device.send_command("connect_wifi")
377+
assert not -has_command_failed(connect_wifi_result)
378+
time.sleep(config.WIFI_CONNECTION_TIMEOUT)
379+
380+
wifi_status_command = device.send_command("get_wifi_status")
381+
assert not has_command_failed(wifi_status_command)
382+
assert wifi_status_command["results"][0]["result"]["data"]["status"] == "error"
383+
384+
290385
@pytest.mark.has_capability("wireless")
291386
def test_set_wifi_invalid_network(get_openiris_device, ensure_board_in_mode, config):
292387
device = ensure_board_in_mode("wifi", get_openiris_device())
293388
params = {
294389
"name": "main",
295390
"ssid": "PleaseDontBeARealNetwork",
391+
"bssid": "",
296392
"password": "AndThePasswordIsFake",
297393
"channel": 0,
298394
"power": 0,
@@ -351,6 +447,7 @@ def test_update_main_wifi_network(ensure_board_in_mode, get_openiris_device, con
351447
params1 = {
352448
"name": "main",
353449
"ssid": "Nada",
450+
"bssid": "",
354451
"password": "Nuuh",
355452
"channel": 0,
356453
"power": 0,
@@ -377,6 +474,7 @@ def test_set_wifi_add_another_network(ensure_board_in_mode, get_openiris_device)
377474
params = {
378475
"name": "anotherNetwork",
379476
"ssid": "PleaseDontBeARealNetwork",
477+
"bssid": "",
380478
"password": "AndThePassowrdIsFake",
381479
"channel": 0,
382480
"power": 0,
@@ -475,6 +573,7 @@ def test_update_wifi_command(ensure_board_in_mode, get_openiris_device, payload)
475573
params = {
476574
"name": "anotherNetwork",
477575
"ssid": "PleaseDontBeARealNetwork",
576+
"bssid": "",
478577
"password": "AndThePasswordIsFake",
479578
"channel": 0,
480579
"power": 0,

0 commit comments

Comments
 (0)