You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.2 KiB

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