libubus-sub.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "libubus.h"
  14. #include "libubus-internal.h"
  15. static int ubus_subscriber_cb(struct ubus_context *ctx, struct ubus_object *obj,
  16. struct ubus_request_data *req,
  17. const char *method, struct blob_attr *msg)
  18. {
  19. struct ubus_subscriber *s;
  20. s = container_of(obj, struct ubus_subscriber, obj);
  21. if (s->cb)
  22. return s->cb(ctx, obj, req, method, msg);
  23. return 0;
  24. }
  25. const struct ubus_method watch_method __hidden = {
  26. .name = NULL,
  27. .handler = ubus_subscriber_cb,
  28. };
  29. static void
  30. ubus_auto_sub_event_handler_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
  31. const char *type, struct blob_attr *msg)
  32. {
  33. enum {
  34. EVENT_ID,
  35. EVENT_PATH,
  36. __EVENT_MAX
  37. };
  38. static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
  39. [EVENT_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
  40. [EVENT_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
  41. };
  42. struct blob_attr *tb[__EVENT_MAX];
  43. struct ubus_subscriber *s;
  44. const char *path;
  45. int id;
  46. blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
  47. if (!tb[EVENT_ID] || !tb[EVENT_PATH])
  48. return;
  49. path = blobmsg_get_string(tb[EVENT_PATH]);
  50. id = blobmsg_get_u32(tb[EVENT_ID]);
  51. list_for_each_entry(s, &ctx->auto_subscribers, list)
  52. if (s->new_obj_cb(ctx, s, path))
  53. ubus_subscribe(ctx, s, id);
  54. }
  55. static void
  56. ubus_auto_sub_lookup(struct ubus_context *ctx, struct ubus_object_data *obj,
  57. void *priv)
  58. {
  59. struct ubus_subscriber *s = priv;
  60. if (s->new_obj_cb(ctx, s, obj->path))
  61. ubus_subscribe(ctx, s, obj->id);
  62. }
  63. int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *s)
  64. {
  65. struct ubus_object *obj = &s->obj;
  66. int ret;
  67. INIT_LIST_HEAD(&s->list);
  68. obj->methods = &watch_method;
  69. obj->n_methods = 1;
  70. ret = ubus_add_object(ctx, obj);
  71. if (ret)
  72. return ret;
  73. if (s->new_obj_cb) {
  74. struct ubus_event_handler *ev = &ctx->auto_subscribe_event_handler;
  75. list_add(&s->list, &ctx->auto_subscribers);
  76. ev->cb = ubus_auto_sub_event_handler_cb;
  77. if (!ev->obj.id)
  78. ubus_register_event_handler(ctx, ev, "ubus.object.add");
  79. ubus_lookup(ctx, NULL, ubus_auto_sub_lookup, s);
  80. }
  81. return 0;
  82. }
  83. static int
  84. __ubus_subscribe_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, int type)
  85. {
  86. struct ubus_request req;
  87. blob_buf_init(&b, 0);
  88. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
  89. blob_put_int32(&b, UBUS_ATTR_TARGET, id);
  90. if (ubus_start_request(ctx, &req, b.head, type, 0) < 0)
  91. return UBUS_STATUS_INVALID_ARGUMENT;
  92. return ubus_complete_request(ctx, &req, 0);
  93. }
  94. int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
  95. {
  96. return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_SUBSCRIBE);
  97. }
  98. int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
  99. {
  100. return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_UNSUBSCRIBE);
  101. }