cli.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <unistd.h>
  2. #include <libubox/blobmsg_json.h>
  3. #include "libubus.h"
  4. static struct blob_buf b;
  5. static const char *attr_types[] = {
  6. [BLOBMSG_TYPE_INT32] = "\"Integer\"",
  7. [BLOBMSG_TYPE_STRING] = "\"String\"",
  8. };
  9. static const char *format_type(void *priv, struct blob_attr *attr)
  10. {
  11. const char *type = NULL;
  12. int typeid;
  13. if (blob_id(attr) != BLOBMSG_TYPE_INT32)
  14. return NULL;
  15. typeid = blobmsg_get_u32(attr);
  16. if (typeid < ARRAY_SIZE(attr_types))
  17. type = attr_types[typeid];
  18. if (!type)
  19. type = "\"(unknown)\"";
  20. return type;
  21. }
  22. static void receive_lookup(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
  23. {
  24. struct blob_attr *cur;
  25. char *s;
  26. int rem;
  27. fprintf(stderr, "'%s' @%08x\n", obj->path, obj->id);
  28. if (!obj->signature)
  29. return;
  30. blob_for_each_attr(cur, obj->signature, rem) {
  31. s = blobmsg_format_json_with_cb(cur, false, format_type, NULL);
  32. fprintf(stderr, "\t%s\n", s);
  33. free(s);
  34. }
  35. }
  36. static void receive_data(struct ubus_request *req, int type, struct blob_attr *msg)
  37. {
  38. char *str;
  39. if (!msg)
  40. return;
  41. str = blobmsg_format_json(msg, true);
  42. fprintf(stderr, "%s\n", str);
  43. free(str);
  44. }
  45. static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
  46. const char *type, struct blob_attr *msg)
  47. {
  48. char *str;
  49. if (msg)
  50. str = blobmsg_format_json(msg, true);
  51. else
  52. str = "";
  53. fprintf(stderr, "\"%s\":{ %s }\n", type, str);
  54. free(str);
  55. }
  56. static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
  57. {
  58. static struct ubus_event_handler listener;
  59. const char *event;
  60. int ret = 0;
  61. memset(&listener, 0, sizeof(listener));
  62. listener.cb = receive_event;
  63. if (!argc) {
  64. event = "*";
  65. ret = ubus_register_event_handler(ctx, &listener, NULL);
  66. }
  67. for (;argc;argv++, argc--) {
  68. event = argv[0];
  69. ret = ubus_register_event_handler(ctx, &listener, argv[0]);
  70. if (ret)
  71. break;
  72. }
  73. if (ret) {
  74. fprintf(stderr, "Error while registering for event '%s': %s\n",
  75. event, ubus_strerror(ret));
  76. }
  77. uloop_init();
  78. ubus_add_uloop(ctx);
  79. uloop_run();
  80. uloop_done();
  81. return 0;
  82. }
  83. static int usage(const char *prog)
  84. {
  85. fprintf(stderr,
  86. "Usage: %s [<options>] <command> [arguments...]\n"
  87. "Options:\n"
  88. " -s <socket>: Set the unix domain socket to connect to\n"
  89. "\n"
  90. "Commands:\n"
  91. " - list [<path>] List objects\n"
  92. " - call <path> <method> [<message>] Call an object method\n"
  93. " - listen [<path>...] Listen for events\n"
  94. "\n", prog);
  95. return 1;
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. const char *progname, *ubus_socket = NULL;
  100. static struct ubus_context *ctx;
  101. char *cmd;
  102. int ret = 0;
  103. int ch;
  104. progname = argv[0];
  105. while ((ch = getopt(argc, argv, "s:")) != -1) {
  106. switch (ch) {
  107. case 's':
  108. ubus_socket = optarg;
  109. break;
  110. default:
  111. return usage(progname);
  112. }
  113. }
  114. argc -= optind;
  115. argv += optind;
  116. ctx = ubus_connect(ubus_socket);
  117. if (!ctx) {
  118. fprintf(stderr, "Failed to connect to ubus\n");
  119. return -1;
  120. }
  121. cmd = argv[0];
  122. if (argc < 1)
  123. return usage(progname);
  124. argv++;
  125. argc--;
  126. if (!strcmp(cmd, "list")) {
  127. const char *path = NULL;
  128. if (argc == 1)
  129. path = argv[0];
  130. ret = ubus_lookup(ctx, path, receive_lookup, NULL);
  131. } else if (!strcmp(cmd, "call")) {
  132. uint32_t id;
  133. if (argc < 2 || argc > 3)
  134. return usage(progname);
  135. blob_buf_init(&b, 0);
  136. if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
  137. fprintf(stderr, "Failed to parse message data\n");
  138. goto out;
  139. }
  140. ret = ubus_lookup_id(ctx, argv[0], &id);
  141. if (!ret)
  142. ret = ubus_invoke(ctx, id, argv[1], b.head, receive_data, NULL);
  143. } else if (!strcmp(cmd, "listen")) {
  144. ret = ubus_cli_listen(ctx, argc, argv);
  145. } else {
  146. return usage(progname);
  147. }
  148. if (ret)
  149. fprintf(stderr, "Failed: %s\n", ubus_strerror(ret));
  150. out:
  151. ubus_free(ctx);
  152. return ret;
  153. }