libubus-sub.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *s)
  30. {
  31. struct ubus_object *obj = &s->obj;
  32. obj->methods = &watch_method;
  33. obj->n_methods = 1;
  34. return ubus_add_object(ctx, obj);
  35. }
  36. static int
  37. __ubus_subscribe_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, int type)
  38. {
  39. struct ubus_request req;
  40. blob_buf_init(&b, 0);
  41. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
  42. blob_put_int32(&b, UBUS_ATTR_TARGET, id);
  43. if (ubus_start_request(ctx, &req, b.head, type, 0) < 0)
  44. return UBUS_STATUS_INVALID_ARGUMENT;
  45. return ubus_complete_request(ctx, &req, 0);
  46. }
  47. int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
  48. {
  49. return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_SUBSCRIBE);
  50. }
  51. int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
  52. {
  53. return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_UNSUBSCRIBE);
  54. }