Skip to content

Commit 5455b67

Browse files
committed
Rework mDNS configuration
1 parent 803fc13 commit 5455b67

File tree

5 files changed

+46
-58
lines changed

5 files changed

+46
-58
lines changed

components/ocs_pipeline/config/mdns_config.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ status::StatusCode MdnsConfig::configure(const char* hostname) {
6767
return status::StatusCode::NotModified;
6868
}
6969

70+
status::StatusCode MdnsConfig::reset() {
71+
auto code = storage_->erase(hostname_key_);
72+
if (code == status::StatusCode::NoData) {
73+
code = status::StatusCode::NotModified;
74+
}
75+
76+
return code;
77+
}
78+
7079
} // namespace config
7180
} // namespace pipeline
7281
} // namespace ocs

components/ocs_pipeline/config/mdns_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class MdnsConfig : public core::NonCopyable<> {
3232
//! Update mDNS configuration.
3333
status::StatusCode configure(const char* hostname);
3434

35+
//! Reset mDNS configuration.
36+
status::StatusCode reset();
37+
3538
private:
3639
static constexpr const char* hostname_key_ = "host";
3740

components/ocs_pipeline/httpserver/mdns_handler.cpp

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
#include <cstring>
1010

11-
#include "ocs_fmt/json/cjson_builder.h"
12-
#include "ocs_fmt/json/cjson_object_formatter.h"
13-
#include "ocs_fmt/json/dynamic_formatter.h"
1411
#include "ocs_pipeline/httpserver/mdns_handler.h"
1512

1613
namespace ocs {
@@ -22,62 +19,44 @@ MdnsHandler::MdnsHandler(http::Server& server,
2219
scheduler::ITask& reboot_task)
2320
: config_(config)
2421
, reboot_task_(reboot_task) {
25-
server.add_GET("/api/v1/system/mdns", [this](httpd_req_t* req) {
22+
server.add_GET("/api/v1/config/mdns", [this](httpd_req_t* req) {
2623
const auto values = algo::UriOps::parse_query(req->uri);
2724
if (!values.size()) {
28-
return handle_mdns_get_(req);
25+
return status::StatusCode::InvalidArg;
2926
}
3027

31-
return handle_mdns_set_(req, values);
28+
return handle_update_(req, values);
3229
});
3330
}
3431

35-
status::StatusCode MdnsHandler::handle_mdns_get_(httpd_req_t* req) {
36-
fmt::json::CjsonUniqueBuilder builder;
32+
status::StatusCode MdnsHandler::handle_update_(httpd_req_t* req,
33+
const algo::UriOps::Values& values) {
34+
status::StatusCode code = status::StatusCode::OK;
3735

38-
auto json = builder.make_object();
39-
if (!json) {
40-
return status::StatusCode::NoMem;
41-
}
42-
43-
fmt::json::CjsonObjectFormatter formatter(json.get());
44-
if (!formatter.add_string_ref_cs("hostname", config_.get_hostname())) {
45-
return status::StatusCode::NoMem;
46-
}
47-
48-
fmt::json::DynamicFormatter json_formatter(64);
49-
const auto code = json_formatter.format(json.get());
50-
if (code != status::StatusCode::OK) {
51-
return code;
52-
}
53-
54-
auto err = httpd_resp_set_type(req, HTTPD_TYPE_JSON);
55-
if (err != ESP_OK) {
56-
return status::StatusCode::Error;
57-
}
58-
59-
err = httpd_resp_send(req, json_formatter.c_str(), HTTPD_RESP_USE_STRLEN);
60-
if (err != ESP_OK) {
61-
return status::StatusCode::Error;
62-
}
63-
64-
return status::StatusCode::OK;
65-
}
36+
const auto reset = values.find("reset");
37+
if (reset != values.end()) {
38+
if (reset->second != "1") {
39+
return status::StatusCode::InvalidArg;
40+
}
6641

67-
status::StatusCode MdnsHandler::handle_mdns_set_(httpd_req_t* req,
68-
const algo::UriOps::Values& values) {
69-
const auto hostname = values.find("hostname");
70-
if (hostname == values.end()) {
71-
return status::StatusCode::InvalidArg;
72-
}
42+
code = config_.reset();
43+
if (code != status::StatusCode::OK && code != status::StatusCode::NotModified) {
44+
return code;
45+
}
46+
} else {
47+
const auto hostname = values.find("hostname");
48+
if (hostname == values.end()) {
49+
return status::StatusCode::InvalidArg;
50+
}
7351

74-
char hostname_buf[hostname->second.size() + 1];
75-
memset(hostname_buf, 0, sizeof(hostname_buf));
76-
memcpy(hostname_buf, hostname->second.data(), hostname->second.size());
52+
char hostname_buf[hostname->second.size() + 1];
53+
memset(hostname_buf, 0, sizeof(hostname_buf));
54+
memcpy(hostname_buf, hostname->second.data(), hostname->second.size());
7755

78-
const auto code = config_.configure(hostname_buf);
79-
if (code != status::StatusCode::OK && code != status::StatusCode::NotModified) {
80-
return code;
56+
code = config_.configure(hostname_buf);
57+
if (code != status::StatusCode::OK && code != status::StatusCode::NotModified) {
58+
return code;
59+
}
8160
}
8261

8362
auto err = httpd_resp_set_type(req, HTTPD_TYPE_TEXT);

components/ocs_pipeline/httpserver/mdns_handler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class MdnsHandler : public core::NonCopyable<> {
3131
scheduler::ITask& reboot_task);
3232

3333
private:
34-
status::StatusCode handle_mdns_get_(httpd_req_t*);
35-
status::StatusCode handle_mdns_set_(httpd_req_t*, const algo::UriOps::Values&);
34+
status::StatusCode handle_update_(httpd_req_t*, const algo::UriOps::Values&);
3635

3736
config::MdnsConfig& config_;
3837
scheduler::ITask& reboot_task_;

docs/httpserver.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,17 @@ http "bonsai-firmware.local/api/v1/system/time?value=1733233869"
169169
OK
170170
```
171171

172-
**Get mDNS configuration**
172+
**Set mDNS configuration**
173173

174-
http "bonsai-firmware.local/api/v1/system/mdns"
174+
http "bonsai-firmware.local/api/v1/config/mdns?hostname=test-lab"
175175

176-
```json
177-
{
178-
"hostname": "bonsai-firmware"
179-
}
176+
```txt
177+
OK
180178
```
181179

182-
**Set mDNS configuration**
180+
**Reset mDNS configuration**
183181

184-
http "bonsai-firmware.local/api/v1/system/mdns?hostname=test-lab"
182+
http "bonsai-firmware.local/api/v1/config/mdns?reset=1
185183

186184
```txt
187185
OK

0 commit comments

Comments
 (0)