ubus-example.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <unistd.h>
  2. #include "libubus.h"
  3. static struct ubus_context *ctx;
  4. struct blob_buf b;
  5. static const struct ubus_signature test_object_sig[] = {
  6. UBUS_METHOD_START("hello"),
  7. UBUS_ARRAY("test"),
  8. UBUS_TABLE_START(NULL),
  9. UBUS_FIELD(INT32, "id"),
  10. UBUS_FIELD(STRING, "msg"),
  11. UBUS_TABLE_END(),
  12. UBUS_METHOD_END(),
  13. };
  14. static struct ubus_object_type test_object_type =
  15. UBUS_OBJECT_TYPE("test", test_object_sig);
  16. enum {
  17. HELLO_ID,
  18. HELLO_MSG,
  19. HELLO_LAST
  20. };
  21. static const struct blobmsg_policy hello_policy[] = {
  22. [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
  23. [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
  24. };
  25. static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
  26. struct ubus_request_data *req, const char *method,
  27. struct blob_attr *msg)
  28. {
  29. struct blob_attr *tb[HELLO_LAST];
  30. char *msgstr = "(unknown)";
  31. char *strbuf;
  32. blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
  33. if (tb[HELLO_MSG])
  34. msgstr = blobmsg_data(tb[HELLO_MSG]);
  35. blob_buf_init(&b, 0);
  36. strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
  37. sprintf(strbuf, "%s: Hello, world: %s", obj->name, msgstr);
  38. blobmsg_add_string_buffer(&b);
  39. ubus_send_reply(ctx, req, b.head);
  40. return 0;
  41. }
  42. static const struct ubus_method test_methods[] = {
  43. { .name = "hello", .handler = test_hello },
  44. };
  45. static struct ubus_object test_object = {
  46. .name = "test",
  47. .type = &test_object_type,
  48. .methods = test_methods,
  49. .n_methods = ARRAY_SIZE(test_methods),
  50. };
  51. static struct ubus_object test_object2 = {
  52. .name = "test2",
  53. .type = &test_object_type,
  54. .methods = test_methods,
  55. .n_methods = ARRAY_SIZE(test_methods),
  56. };
  57. int main(int argc, char **argv)
  58. {
  59. const char *progname, *ubus_socket = NULL;
  60. int ret = 0;
  61. int ch;
  62. progname = argv[0];
  63. while ((ch = getopt(argc, argv, "s:")) != -1) {
  64. switch (ch) {
  65. case 's':
  66. ubus_socket = optarg;
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. argc -= optind;
  73. argv += optind;
  74. ctx = ubus_connect(ubus_socket);
  75. if (!ctx) {
  76. fprintf(stderr, "Failed to connect to ubus\n");
  77. return -1;
  78. }
  79. fprintf(stderr, "Connected as ID 0x%08x\n", ctx->local_id);
  80. fprintf(stderr, "Publishing object\n");
  81. ret = ubus_add_object(ctx, &test_object);
  82. if (ret)
  83. fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
  84. else {
  85. fprintf(stderr, "Object ID: %08x\n", test_object.id);
  86. fprintf(stderr, "Object Type ID: %08x\n", test_object.type->id);
  87. }
  88. fprintf(stderr, "Publishing object\n");
  89. ret = ubus_add_object(ctx, &test_object2);
  90. if (ret)
  91. fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
  92. else {
  93. fprintf(stderr, "Object ID: %08x\n", test_object2.id);
  94. fprintf(stderr, "Object Type ID: %08x\n", test_object2.type->id);
  95. }
  96. uloop_init();
  97. ubus_add_uloop(ctx);
  98. uloop_run();
  99. uloop_done();
  100. ubus_free(ctx);
  101. return 0;
  102. }