Created
May 8, 2025 04:17
-
-
Save Vienchau/490f47eb3cbf42bdf555fd6d83fb8ca3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libubox/blobmsg_json.h> | |
#include <libubus.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define TOPIC_NAME "topology.connection" | |
#define UBUS_SOCKET "/var/run/ubus.sock" | |
/** | |
* Global ubus context | |
*/ | |
static struct ubus_context *ctx; | |
/** | |
* Handle event messages from ubus | |
* | |
* @param ctx Ubus context | |
* @param obj Ubus object | |
* @param req Request data | |
* @param method Method name | |
* @param msg Message blob | |
* @return 0 on success | |
*/ | |
static int handle_receive_event(struct ubus_context *ctx, struct ubus_object *obj, | |
struct ubus_request_data *req, const char *method, | |
struct blob_attr *msg) { | |
char *json_str = blobmsg_format_json(msg, true); | |
if (json_str) { | |
printf("Received event: %s\n", json_str); | |
free(json_str); | |
} else { | |
printf("Failed to parse received event.\n"); | |
} | |
return 0; | |
} | |
/** | |
* Ubus method definition | |
*/ | |
static const struct ubus_method receive_methods[] = { | |
UBUS_METHOD_NOARG("push_event", handle_receive_event), | |
}; | |
/** | |
* Ubus object type definition | |
*/ | |
static struct ubus_object_type receive_object_type = | |
UBUS_OBJECT_TYPE("push_event", receive_methods); | |
/** | |
* Ubus object definition | |
*/ | |
static struct ubus_object receive_object = { | |
.name = TOPIC_NAME, | |
.type = &receive_object_type, | |
.methods = receive_methods, | |
.n_methods = ARRAY_SIZE(receive_methods), | |
}; | |
/** | |
* Initialize ubus connection | |
* | |
* @param ubus_socket Path to ubus socket | |
* @return 0 on success, -1 on failure | |
*/ | |
static int init_ubus(const char *ubus_socket) { | |
ctx = ubus_connect(ubus_socket); | |
if (!ctx) { | |
fprintf(stderr, "Failed to connect to UBUS at %s\n", ubus_socket); | |
return -1; | |
} | |
printf("Connected to UBUS at %s\n", ubus_socket); | |
if (ubus_add_object(ctx, &receive_object)) { | |
fprintf(stderr, "Failed to add UBUS object.\n"); | |
ubus_free(ctx); | |
return -1; | |
} | |
printf("UBUS object registered successfully.\n"); | |
return 0; | |
} | |
/** | |
* Cleanup ubus resources | |
*/ | |
static void cleanup_ubus(void) { | |
if (ctx) { | |
ubus_free(ctx); | |
printf("Disconnected from UBUS\n"); | |
} | |
} | |
int main(int argc, char **argv) { | |
int ret = EXIT_SUCCESS; | |
/* Initialize ubus connection */ | |
if (init_ubus(UBUS_SOCKET) < 0) { | |
return EXIT_FAILURE; | |
} | |
/* Start event loop */ | |
uloop_init(); | |
ubus_add_uloop(ctx); | |
uloop_run(); | |
/* Cleanup */ | |
cleanup_ubus(); | |
uloop_done(); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment