b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #ifndef _UBUS_CXX_HPP |
| 2 | #define _UBUS_CXX_HPP |
| 3 | |
| 4 | #include <libubus.h> |
| 5 | #include <cassert> |
| 6 | #include <memory> |
| 7 | #include <mutex> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #ifndef NDEBUG |
| 13 | #include <iostream> |
| 14 | #endif |
| 15 | |
| 16 | namespace ubus { |
| 17 | |
| 18 | static constexpr int call_timeout = 500; |
| 19 | |
| 20 | using msg_ptr = std::shared_ptr<const blob_attr>; |
| 21 | |
| 22 | using strings = std::vector<std::string>; |
| 23 | |
| 24 | inline auto concat(strings dest) |
| 25 | { |
| 26 | return dest; |
| 27 | } |
| 28 | |
| 29 | template <class... Strings> |
| 30 | inline auto concat(strings dest, strings src, Strings... more) |
| 31 | { |
| 32 | dest.reserve(dest.size() + src.size()); |
| 33 | dest.insert(std::end(dest), std::make_move_iterator(std::begin(src)), |
| 34 | std::make_move_iterator(std::end(src))); |
| 35 | return concat(std::move(dest), std::move(more)...); |
| 36 | } |
| 37 | |
| 38 | template <class S, class... Strings> |
| 39 | inline auto concat(strings dest, S src, Strings... more) |
| 40 | { |
| 41 | dest.emplace_back(std::move(src)); |
| 42 | return concat(std::move(dest), std::move(more)...); |
| 43 | } |
| 44 | |
| 45 | class iterator { |
| 46 | private: |
| 47 | const strings& keys; |
| 48 | |
| 49 | const size_t n = 0; |
| 50 | |
| 51 | size_t i = 0; |
| 52 | |
| 53 | const blob_attr* pos = nullptr; |
| 54 | |
| 55 | std::unique_ptr<iterator> cur{}; |
| 56 | |
| 57 | iterator* parent = nullptr; |
| 58 | |
| 59 | size_t rem = 0; |
| 60 | |
| 61 | [[nodiscard]] inline auto matches() const -> bool |
| 62 | { |
| 63 | return (keys[i].empty() || blobmsg_name(cur->pos) == keys[i]); |
| 64 | } |
| 65 | |
| 66 | explicit iterator(iterator* par) |
| 67 | : keys{par->keys}, n{par->n}, pos{par->pos}, cur{this}, parent{par} |
| 68 | { |
| 69 | if (pos != nullptr) { |
| 70 | rem = blobmsg_data_len(pos); |
| 71 | pos = static_cast<blob_attr*>(blobmsg_data(pos)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public: |
| 76 | explicit iterator(const blob_attr* msg, const strings& key_filter = {""}) |
| 77 | : keys{key_filter}, n{keys.size() - 1}, pos{msg}, cur{this} |
| 78 | { |
| 79 | if (pos != nullptr) { |
| 80 | rem = blobmsg_data_len(pos); |
| 81 | pos = static_cast<blob_attr*>(blobmsg_data(pos)); |
| 82 | |
| 83 | if (rem == 0) { |
| 84 | pos = nullptr; |
| 85 | } |
| 86 | else if (i != n || !matches()) { |
| 87 | ++*this; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | inline iterator(iterator&&) noexcept = default; |
| 93 | |
| 94 | inline iterator(const iterator&) = delete; |
| 95 | |
| 96 | inline auto operator=(const iterator&) -> iterator& = delete; |
| 97 | |
| 98 | inline auto operator=(iterator &&) -> iterator& = delete; |
| 99 | |
| 100 | inline auto operator*() |
| 101 | { |
| 102 | return cur->pos; |
| 103 | } |
| 104 | |
| 105 | inline auto operator!=(const iterator& rhs) |
| 106 | { |
| 107 | return (cur->rem != rhs.cur->rem || cur->pos != rhs.cur->pos); |
| 108 | } |
| 109 | |
| 110 | auto operator++() -> iterator&; |
| 111 | |
| 112 | inline ~iterator() |
| 113 | { |
| 114 | if (cur.get() == this) { |
| 115 | static_cast<void>(cur.release()); |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | class message { |
| 121 | private: |
| 122 | const msg_ptr msg{}; // initialized by callback. |
| 123 | |
| 124 | const strings keys{}; |
| 125 | |
| 126 | public: |
| 127 | inline explicit message(msg_ptr message_ptr, strings key_filter = {""}) |
| 128 | : msg{std::move(message_ptr)}, keys{std::move(key_filter)} |
| 129 | {} |
| 130 | |
| 131 | inline message(message&&) = default; |
| 132 | |
| 133 | inline message(const message&) = delete; |
| 134 | |
| 135 | inline auto operator=(message &&) -> message& = delete; |
| 136 | |
| 137 | inline auto operator=(const message&) -> message& = delete; |
| 138 | |
| 139 | [[nodiscard]] inline auto begin() const -> iterator |
| 140 | { |
| 141 | return iterator{msg.get(), keys}; |
| 142 | } |
| 143 | |
| 144 | [[nodiscard]] inline auto end() const -> iterator |
| 145 | { |
| 146 | return iterator{nullptr, keys}; |
| 147 | } |
| 148 | |
| 149 | inline explicit operator bool() const |
| 150 | { |
| 151 | return begin() != end(); |
| 152 | } |
| 153 | |
| 154 | template <class... Strings> |
| 155 | auto filter(Strings... key_filter) |
| 156 | { |
| 157 | strings both{}; |
| 158 | if (keys.size() != 1 || !keys[0].empty()) { |
| 159 | both = keys; |
| 160 | } |
| 161 | both = concat(std::move(both), std::move(key_filter)...); |
| 162 | return std::move(message{msg, std::move(both)}); |
| 163 | } |
| 164 | |
| 165 | inline ~message() = default; |
| 166 | }; |
| 167 | |
| 168 | class lock_shared_resources { |
| 169 | private: |
| 170 | static std::mutex inuse; |
| 171 | |
| 172 | public: |
| 173 | inline lock_shared_resources() |
| 174 | { |
| 175 | inuse.lock(); |
| 176 | } |
| 177 | |
| 178 | inline lock_shared_resources(lock_shared_resources&&) noexcept = default; |
| 179 | |
| 180 | inline lock_shared_resources(const lock_shared_resources&) = delete; |
| 181 | |
| 182 | inline auto operator=(const lock_shared_resources&) -> auto& = delete; |
| 183 | |
| 184 | inline auto operator=(lock_shared_resources &&) -> auto&& = delete; |
| 185 | |
| 186 | // NOLINTNEXTLINE(readability-convert-member-functions-to-static) |
| 187 | inline auto get_context() -> ubus_context* // is member to enforce inuse. |
| 188 | { |
| 189 | static auto ubus_freeing = [](ubus_context* ctx) { ubus_free(ctx); }; |
| 190 | static std::unique_ptr<ubus_context, decltype(ubus_freeing)> lazy_ctx{ubus_connect(nullptr), |
| 191 | ubus_freeing}; |
| 192 | |
| 193 | if (!lazy_ctx) { // it could be available on a later call: |
| 194 | |
| 195 | lazy_ctx.reset(ubus_connect(nullptr)); |
| 196 | |
| 197 | if (!lazy_ctx) { |
| 198 | throw std::runtime_error("ubus error: cannot connect context"); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return lazy_ctx.get(); |
| 203 | } |
| 204 | |
| 205 | // NOLINTNEXTLINE(readability-convert-member-functions-to-static) |
| 206 | inline auto get_blob_buf() -> blob_buf* // is member to enforce inuse. |
| 207 | { |
| 208 | static blob_buf buf; |
| 209 | |
| 210 | static auto blob_buf_freeing = [](blob_buf* b) { blob_buf_free(b); }; |
| 211 | static std::unique_ptr<blob_buf, decltype(blob_buf_freeing)> |
| 212 | created_to_free_on_the_end_of_life{&buf, blob_buf_freeing}; |
| 213 | |
| 214 | blob_buf_init(&buf, 0); |
| 215 | |
| 216 | return &buf; |
| 217 | } |
| 218 | |
| 219 | inline ~lock_shared_resources() |
| 220 | { |
| 221 | inuse.unlock(); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | template <class F> |
| 226 | auto call(const char* path, const char* method, F set_arguments, int timeout = call_timeout) |
| 227 | -> message; |
| 228 | |
| 229 | inline auto call(const char* path, const char* method, int timeout = call_timeout) -> message |
| 230 | { |
| 231 | return call( |
| 232 | path, method, [](blob_buf* /*buf*/) { return 0; }, timeout); |
| 233 | } |
| 234 | |
| 235 | inline auto call(const char* path, int timeout = call_timeout) -> message |
| 236 | { |
| 237 | return call(path, "", timeout); |
| 238 | } |
| 239 | |
| 240 | // ------------------------- implementation: ---------------------------------- |
| 241 | |
| 242 | std::mutex lock_shared_resources::inuse; |
| 243 | |
| 244 | inline auto iterator::operator++() -> iterator& |
| 245 | { |
| 246 | for (;;) { |
| 247 | #ifndef NDEBUG |
| 248 | std::cout << std::string(i, '>') << " look for " << keys[i] << " at "; |
| 249 | std::cout << blobmsg_name(cur->pos) << std::endl; |
| 250 | #endif |
| 251 | |
| 252 | auto id = blob_id(cur->pos); |
| 253 | if ((id == BLOBMSG_TYPE_TABLE || id == BLOBMSG_TYPE_ARRAY) && i < n && matches() && |
| 254 | blobmsg_data_len(cur->pos) > 0) |
| 255 | { // immmerge: |
| 256 | ++i; |
| 257 | |
| 258 | auto* tmp = cur.release(); |
| 259 | |
| 260 | struct new_iterator : public iterator // use private constructor: |
| 261 | { |
| 262 | explicit new_iterator(iterator* par) : iterator{par} {} |
| 263 | }; |
| 264 | cur = std::make_unique<new_iterator>(tmp); |
| 265 | } |
| 266 | else { |
| 267 | while (true) { |
| 268 | cur->rem -= blob_pad_len(cur->pos); |
| 269 | cur->pos = blob_next(cur->pos); |
| 270 | auto len = blob_pad_len(cur->pos); |
| 271 | |
| 272 | if (cur->rem > 0 && len <= cur->rem && len >= sizeof(blob_attr)) { |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | // emerge: |
| 277 | auto* tmp = cur->parent; |
| 278 | |
| 279 | if (tmp == nullptr) { |
| 280 | cur->pos = nullptr; |
| 281 | return *cur; |
| 282 | } |
| 283 | |
| 284 | cur.reset(tmp); |
| 285 | |
| 286 | --i; |
| 287 | } |
| 288 | } |
| 289 | if (i == n && matches()) { |
| 290 | return *cur; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | template <class F> |
| 296 | inline auto call(const char* path, const char* method, F set_arguments, int timeout) -> message |
| 297 | { |
| 298 | auto shared = lock_shared_resources{}; |
| 299 | |
| 300 | auto* ctx = shared.get_context(); |
| 301 | |
| 302 | uint32_t id = 0; |
| 303 | int err = ubus_lookup_id(ctx, path, &id); |
| 304 | |
| 305 | if (err == 0) { // call |
| 306 | ubus_request request{}; |
| 307 | |
| 308 | auto* buf = shared.get_blob_buf(); |
| 309 | err = set_arguments(buf); |
| 310 | if (err == 0) { |
| 311 | err = ubus_invoke_async(ctx, id, method, buf->head, &request); |
| 312 | } |
| 313 | |
| 314 | if (err == 0) { |
| 315 | msg_ptr message_ptr; |
| 316 | |
| 317 | /* Cannot capture message_ptr, the lambda would be another type. |
| 318 | * Pass a location where to save the message as priv pointer when |
| 319 | * invoking and get it back here: |
| 320 | */ |
| 321 | request.priv = &message_ptr; |
| 322 | |
| 323 | request.data_cb = [](ubus_request* req, int /*type*/, blob_attr* msg) { |
| 324 | if (req == nullptr || msg == nullptr) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | auto* saved = static_cast<msg_ptr*>(req->priv); |
| 329 | if (saved == nullptr || *saved) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | saved->reset(blob_memdup(msg), free); |
| 334 | if (!*saved) { |
| 335 | throw std::bad_alloc(); |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | err = ubus_complete_request(ctx, &request, timeout); |
| 340 | |
| 341 | if (err == 0) { |
| 342 | return message{message_ptr}; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | std::string errmsg = "ubus::call error: cannot invoke"; |
| 348 | errmsg += " (" + std::to_string(err) + ") " + path + " " + method; |
| 349 | throw std::runtime_error(errmsg); |
| 350 | } |
| 351 | |
| 352 | } // namespace ubus |
| 353 | |
| 354 | #endif |