diff --git a/src/agent.c b/src/agent.c index afa06ee07d71d2e3d70d387f3fbc05bbc9e1afcf..551a4c8b6f2fb6dffa333fd426a82791b73b660e 100644 --- a/src/agent.c +++ b/src/agent.c @@ -66,7 +66,7 @@ static struct agent *this_agent; static int signal_pending; -static void agent_subscribe_for_cmdus(struct agent *a); +static int agent_subscribe_for_cmdus(struct agent *a); static void bsta_steer_cb(struct uloop_timeout *t) { @@ -5485,90 +5485,40 @@ void clear_fhlist(struct agent *a) } } -static void mapclient_sub_remove_cb(struct ubus_context *ctx, - struct ubus_subscriber *sub, - uint32_t obj) +int agent_map_sub_cb(void *bus, void *priv, void *data) { - struct agent *a = container_of(sub, struct agent, sub); - - a->subscribed = false; - trace("Object 0x%x no longer present\n", obj); -} - -static int mapclient_sub_cb(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *type, - struct blob_attr *msg) -{ - struct ubus_subscriber *s = container_of(obj, struct ubus_subscriber, obj); - struct agent *a = container_of(s, struct agent, sub); + struct blob_attr *msg = (struct blob_attr *)data; char *str; - str = blobmsg_format_json(msg, true); - trace("Received notification '%s': %s\n", type, str); - - ieee1905_cmdu_event_handler(a, msg); + str = blobmsg_format_json(msg, true); + trace("Received notification '%s'\n", str); free(str); - return 0; -} - -static int mapclient_subscribe(struct agent *a, uint32_t oid) -{ - int ret; - - /* register mapclient as a subscriber with ubus */ - a->sub.cb = mapclient_sub_cb; - a->sub.remove_cb = mapclient_sub_remove_cb; - ret = ubus_register_subscriber(a->ubus_ctx, &a->sub); - if (ret) { - trace("Failed to register sub: %s\n", ubus_strerror(ret)); - return -1; - } - - - /* now subscribe to events from map plugin over passed oid */ - ret = ubus_subscribe(a->ubus_ctx, &a->sub, oid); - if (ret) { - trace("Failed to subscribe: %s\n", ubus_strerror(ret)); - return -1; - } + ieee1905_cmdu_event_handler(priv, msg); - a->subscribed = true; return 0; } -static void register_cb(struct ubus_request *req, int type, struct blob_attr *msg) +int agent_map_del_cb(void *bus, void *priv, void *data) { - struct agent *priv = (struct agent *)req->priv; - const struct blobmsg_policy pol[1] = { - [0] = { .name = "oid", .type = BLOBMSG_TYPE_INT32 }, - }; - struct blob_attr *tb[1]; + struct agent *a = (struct agent *)priv; + uint32_t *obj = (uint32_t *)data; + a->subscribed = false; + fprintf(stdout, "Object 0x%x no longer present\n", *obj); - blobmsg_parse(pol, 1, tb, blob_data(msg), blob_len(msg)); - - if (tb[0]) { - uint32_t oid = blobmsg_get_u32(tb[0]); - - trace("Response ID: %u\n", oid); - mapclient_subscribe(priv, oid); - } + return 0; } -static void agent_subscribe_for_cmdus(struct agent *a) +static int agent_subscribe_for_cmdus(struct agent *a) { - char data[2 * sizeof(struct map_module) + 1] = {0}; - int ret; + mapmodule_cmdu_mask_t cmdu_mask = {0}; uint32_t map_id; - struct blob_buf bb = {}; - struct map_module m = { - .process_cmdu_funcname = "mapclient_process_cmdu", - }; + int ret; + - m.id = rand(); - map_prepare_cmdu_mask(m.cmdu_mask, + map_prepare_cmdu_mask(cmdu_mask, CMDU_TYPE_TOPOLOGY_DISCOVERY, CMDU_TYPE_TOPOLOGY_NOTIFICATION, CMDU_TYPE_TOPOLOGY_QUERY, @@ -5600,11 +5550,9 @@ static void agent_subscribe_for_cmdus(struct agent *a) CMDU_CAC_REQUEST, CMDU_CAC_TERMINATION, -1); - memcpy(a->cmdu_mask, m.cmdu_mask, sizeof(a->cmdu_mask)); + memcpy(a->cmdu_mask, cmdu_mask, sizeof(a->cmdu_mask)); trace("<----------------------------------- %s\n", __func__); - - for (;;) { ret = ubus_lookup_id(a->ubus_ctx, map_plugin, &map_id); if (!ret) @@ -5616,22 +5564,19 @@ static void agent_subscribe_for_cmdus(struct agent *a) a->map_oid = map_id; - /* register as client to the map module */ - blob_buf_init(&bb, 0); - blobmsg_add_string(&bb, "module", "mapagent"); - btostr((unsigned char *)&m, sizeof(struct map_module), data); - blobmsg_add_string(&bb, "data", data); - /* TODO: how to handle failure to subscribe from here */ - ret = ubus_invoke(a->ubus_ctx, a->map_oid, "register", bb.head, - register_cb, a, 1000); + ret = map_subscribe(a->ubus_ctx, + &a->map_oid, + "mapagent", &cmdu_mask, a, + agent_map_sub_cb, + agent_map_del_cb, + &a->subscriber); if (ret) { - trace("Failed to 'register' with %s (err = %s)\n", - map_plugin, ubus_strerror(ret)); + trace("mapagent: Failed to 'register' with %s (err = %s)\n", + map_plugin, ubus_strerror(ret)); } - blob_buf_free(&bb); - - return; + a->subscribed = true; + return ret; } int agent_init_defaults(struct agent *a) @@ -5795,6 +5740,7 @@ int start_agent(void) /* out_and_exit: */ ubus_unregister_event_handler(ctx, &w->evh); + map_unsubscribe(w->ubus_ctx, w->subscriber); warn("Error!! exit ...\n"); agent_remove_object(w); ubus_free(ctx); @@ -5870,6 +5816,7 @@ void stop_agent(struct agent *w) //exit_dispatcher(w); ubus_unregister_event_handler(w->ubus_ctx, &w->evh); + map_unsubscribe(w->ubus_ctx, w->subscriber); plugins_unload(&w->pluginlist); agent_remove_object(w); ubus_free(w->ubus_ctx); diff --git a/src/agent.h b/src/agent.h index f1cc1e16374f884526e088ace3a41888ccbe917c..e9f37242182f50ca59c7bb199c838d2de542026a 100644 --- a/src/agent.h +++ b/src/agent.h @@ -631,7 +631,7 @@ struct agent { /* i1905 stack subscription */ uint32_t map_oid; mapmodule_cmdu_mask_t cmdu_mask; - struct ubus_subscriber sub; + void *subscriber; bool subscribed; /* ubus object and events */ diff --git a/src/dynbh/dynbh.c b/src/dynbh/dynbh.c index a1b7807c1fa7da1afd64e7d6f1b0c2c90b74d48e..834a4169dbdbbf62bebe92ef14b0221861ac634b 100644 --- a/src/dynbh/dynbh.c +++ b/src/dynbh/dynbh.c @@ -679,112 +679,58 @@ error: return ret; } - -static int mapclient_sub_cb(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *type, - struct blob_attr *msg) +int dynbh_map_sub_cb(void *bus, void *priv, void *data) { + struct blob_attr *msg = (struct blob_attr *)data; char *str; - struct ubus_subscriber *s = container_of(obj, struct ubus_subscriber, obj); - struct mapclient_private *priv = container_of(s, struct mapclient_private, sub); - - fprintf(stderr, "sub_cb: &mapclient_private = %p\n", priv); str = blobmsg_format_json(msg, true); - fprintf(stderr, "Received notification string: '%s': %s\n", type, str); - - mapclient_handle_cmdu_notification(msg, priv); + fprintf(stderr, "Received notification '%s'\n", str); free(str); - return 0; -} -static void mapclient_sub_remove_cb(struct ubus_context *ctx, - struct ubus_subscriber *sub, - uint32_t obj) -{ - fprintf(stderr, "Object 0x%x no longer present\n", obj); -} - -static int mapclient_subscribe(struct mapclient_private *priv, uint32_t oid) -{ - int ret; - - /* register mapclient as a subscriber with ubus */ - priv->sub.cb = mapclient_sub_cb; - priv->sub.remove_cb = mapclient_sub_remove_cb; - ret = ubus_register_subscriber(priv->ctx, &priv->sub); - if (ret) - fprintf(stderr, "Failed to register sub: %s\n", ubus_strerror(ret)); - - - /* now subscribe to events from map plugin over passed oid */ - ret = ubus_subscribe(priv->ctx, &priv->sub, oid); - if (ret) - fprintf(stderr, "Failed to subscribe: %s\n", ubus_strerror(ret)); + mapclient_handle_cmdu_notification(msg, priv); - fprintf(stderr, "&mapclient_private = %p\n", priv); return 0; } -static void register_cb(struct ubus_request *req, int type, struct blob_attr *msg) +int dynbh_map_del_cb(void *bus, void *priv, void *data) { - struct mapclient_private *priv = (struct mapclient_private *)req->priv; - const struct blobmsg_policy pol[1] = { - [0] = { .name = "oid", .type = BLOBMSG_TYPE_INT32 }, - }; - struct blob_attr *tb[1]; - - - blobmsg_parse(pol, 1, tb, blob_data(msg), blob_len(msg)); + uint32_t *obj = (uint32_t *)data; - if (tb[0]) { - uint32_t oid = blobmsg_get_u32(tb[0]); - - fprintf(stderr, "Response ID: %u\n", oid); - mapclient_subscribe(priv, oid); - } + fprintf(stderr, "Object 0x%x no longer present\n", *obj); + return 0; } -static int mapclient_subscribe_for_cmdus(struct mapclient_private *priv) +static int dynbh_subscribe_for_cmdus(struct mapclient_private *priv) { - char data[2 * sizeof(struct map_module) + 1] = {0}; + mapmodule_cmdu_mask_t cmdu_mask = {0}; int ret; uint32_t map_id; - struct blob_buf bb = {}; - struct map_module m = { - .id = 0x11111111, - .process_cmdu_funcname = "mapclient_process_cmdu", - }; - map_prepare_cmdu_mask(m.cmdu_mask, + map_prepare_cmdu_mask(cmdu_mask, CMDU_TYPE_AP_AUTOCONFIGURATION_RESPONSE, -1); ret = ubus_lookup_id(priv->ctx, map_plugin, &map_id); if (ret) { - fprintf(stderr, "%s: %s\n", - map_plugin, ubus_strerror(ret)); + fprintf(stderr, "%s: %s\n", map_plugin, ubus_strerror(ret)); return -1; } priv->map_oid = map_id; - - /* register as client to the map module */ - blob_buf_init(&bb, 0); - blobmsg_add_string(&bb, "module", "mapclient1"); - btostr((unsigned char *)&m, sizeof(struct map_module), data); - blobmsg_add_string(&bb, "data", data); - ret = ubus_invoke(priv->ctx, priv->map_oid, "register", bb.head, register_cb, - priv, 1000); + ret = map_subscribe(priv->ctx, + &priv->map_oid, + "dynbhd", &cmdu_mask, priv, + dynbh_map_sub_cb, + dynbh_map_del_cb, + &priv->subscriber); if (ret) { - fprintf(stderr, "Failed to 'register' with %s (err = %s)\n", + fprintf(stderr, "dynbh: Failed to 'register' with %s (err = %s)\n", map_plugin, ubus_strerror(ret)); } - blob_buf_free(&bb); - return ret; } @@ -966,7 +912,7 @@ int main(int argc, char **argv) chrCmd(priv->alidstr, 16, "uci get ieee1905.ieee1905.macaddress | tr -d :"); chrCmd(priv->al_bridge, 16, "uci get mapagent.agent.al_bridge"); - ret = mapclient_subscribe_for_cmdus(priv); + ret = dynbh_subscribe_for_cmdus(priv); if (!ret) { i1905_register_nlevents(priv); read_queries(priv); @@ -974,6 +920,7 @@ int main(int argc, char **argv) uloop_run(); } + map_unsubscribe(priv->ctx, priv->subscriber); ubus_free(priv->ctx); uloop_done(); free(priv); diff --git a/src/dynbh/dynbh.h b/src/dynbh/dynbh.h index 42850fe2aa30eee52be1990de72777227ed93326..bd46086e22c9017accf2cabeedb666c394e16de8 100644 --- a/src/dynbh/dynbh.h +++ b/src/dynbh/dynbh.h @@ -26,7 +26,7 @@ struct mapclient_private { struct ubus_context *ctx; uint32_t oid; uint32_t map_oid; - struct ubus_subscriber sub; + void *subscriber; struct ubus_event_handler evh; int queued_searches; //struct list_head searches; /* currently active autoconfig search queries */