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
1613namespace 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);
0 commit comments