b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #include <iostream> |
| 2 | |
| 3 | #include "ubus-cxx.hpp" |
| 4 | |
| 5 | inline void example_for_checking_if_there_is_a_key() |
| 6 | { |
| 7 | if (ubus::call("service", "list").filter("cron")) { |
| 8 | std::cout << "Cron is active (with or without instances) " << std::endl; |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | inline void example_for_getting_values() |
| 13 | { |
| 14 | auto lan_status = ubus::call("network.interface.lan", "status"); |
| 15 | for (const auto* t : lan_status.filter("ipv6-address", "", "address")) { |
| 16 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 17 | auto* x = const_cast<blob_attr*>(t); |
| 18 | std::cout << "[" << blobmsg_get_string(x) << "] "; |
| 19 | } |
| 20 | for (const auto* t : lan_status.filter("ipv4-address", "").filter("address")) { |
| 21 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 22 | auto* x = const_cast<blob_attr*>(t); |
| 23 | std::cout << blobmsg_get_string(x) << " "; |
| 24 | } |
| 25 | std::cout << std::endl; |
| 26 | } |
| 27 | |
| 28 | inline void example_for_sending_message() |
| 29 | { |
| 30 | auto set_arg = [](blob_buf* buf) -> int { return blobmsg_add_string(buf, "config", "nginx"); }; |
| 31 | for (const auto* t : ubus::call("uci", "get", set_arg).filter("values")) { |
| 32 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 33 | auto* x = const_cast<blob_attr*>(t); |
| 34 | std::cout << blobmsg_get_string(x) << "\n"; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | inline void example_for_exploring() |
| 39 | { |
| 40 | ubus::strings keys{"ipv4-address", "", ""}; |
| 41 | for (const auto* t : ubus::call("network.interface.lan", "status").filter(keys)) { |
| 42 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 43 | auto* x = const_cast<blob_attr*>(t); |
| 44 | std::cout << blobmsg_name(x) << ": "; |
| 45 | switch (blob_id(x)) { |
| 46 | case BLOBMSG_TYPE_UNSPEC: std::cout << "[unspecified]"; break; |
| 47 | case BLOBMSG_TYPE_ARRAY: std::cout << "[array]"; break; |
| 48 | case BLOBMSG_TYPE_TABLE: std::cout << "[table]"; break; |
| 49 | case BLOBMSG_TYPE_STRING: std::cout << blobmsg_get_string(x); break; |
| 50 | case BLOBMSG_TYPE_INT64: std::cout << blobmsg_get_u64(x); break; |
| 51 | case BLOBMSG_TYPE_INT32: std::cout << blobmsg_get_u32(x); break; |
| 52 | case BLOBMSG_TYPE_INT16: std::cout << blobmsg_get_u16(x); break; |
| 53 | case BLOBMSG_TYPE_BOOL: std::cout << blobmsg_get_bool(x); break; |
| 54 | case BLOBMSG_TYPE_DOUBLE: std::cout << blobmsg_get_double(x); break; |
| 55 | default: std::cout << "[unknown]"; |
| 56 | } |
| 57 | std::cout << std::endl; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | inline void example_for_recursive_exploring() |
| 62 | { // output like from the original ubus call: |
| 63 | const auto explore = [](auto message) -> void { |
| 64 | auto end = message.end(); |
| 65 | auto explore_internal = [&end](auto& explore_ref, auto it, size_t depth = 1) -> void { |
| 66 | std::cout << std::endl; |
| 67 | bool first = true; |
| 68 | for (; it != end; ++it) { |
| 69 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 70 | auto* attr = const_cast<blob_attr*>(*it); |
| 71 | if (first) { |
| 72 | first = false; |
| 73 | } |
| 74 | else { |
| 75 | std::cout << ",\n"; |
| 76 | } |
| 77 | std::cout << std::string(depth, '\t'); |
| 78 | std::string name = blobmsg_name(attr); |
| 79 | if (!name.empty()) { |
| 80 | std::cout << "\"" << name << "\": "; |
| 81 | } |
| 82 | switch (blob_id(attr)) { |
| 83 | case BLOBMSG_TYPE_UNSPEC: std::cout << "(unspecified)"; break; |
| 84 | case BLOBMSG_TYPE_ARRAY: |
| 85 | std::cout << "["; |
| 86 | explore_ref(explore_ref, ubus::iterator{attr}, depth + 1); |
| 87 | std::cout << "\n" << std::string(depth, '\t') << "]"; |
| 88 | break; |
| 89 | case BLOBMSG_TYPE_TABLE: |
| 90 | std::cout << "{"; |
| 91 | explore_ref(explore_ref, ubus::iterator{attr}, depth + 1); |
| 92 | std::cout << "\n" << std::string(depth, '\t') << "}"; |
| 93 | break; |
| 94 | case BLOBMSG_TYPE_STRING: |
| 95 | std::cout << "\"" << blobmsg_get_string(attr) << "\""; |
| 96 | break; |
| 97 | case BLOBMSG_TYPE_INT64: std::cout << blobmsg_get_u64(attr); break; |
| 98 | case BLOBMSG_TYPE_INT32: std::cout << blobmsg_get_u32(attr); break; |
| 99 | case BLOBMSG_TYPE_INT16: std::cout << blobmsg_get_u16(attr); break; |
| 100 | case BLOBMSG_TYPE_BOOL: |
| 101 | std::cout << (blobmsg_get_bool(attr) ? "true" : "false"); |
| 102 | break; |
| 103 | case BLOBMSG_TYPE_DOUBLE: std::cout << blobmsg_get_double(attr); break; |
| 104 | default: std::cout << "(unknown)"; break; |
| 105 | } |
| 106 | } |
| 107 | }; |
| 108 | std::cout << "{"; |
| 109 | explore_internal(explore_internal, message.begin()); |
| 110 | std::cout << "\n}" << std::endl; |
| 111 | }; |
| 112 | explore(ubus::call("network.interface.lan", "status")); |
| 113 | } |
| 114 | |
| 115 | auto main() -> int |
| 116 | { |
| 117 | try { |
| 118 | example_for_checking_if_there_is_a_key(); |
| 119 | |
| 120 | example_for_getting_values(); |
| 121 | |
| 122 | example_for_sending_message(); |
| 123 | |
| 124 | example_for_exploring(); |
| 125 | |
| 126 | example_for_recursive_exploring(); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | catch (const std::exception& e) { |
| 132 | std::cerr << e.what() << std::endl; |
| 133 | } |
| 134 | |
| 135 | catch (...) { |
| 136 | perror("main error"); |
| 137 | } |
| 138 | |
| 139 | return 1; |
| 140 | } |