Last active
May 8, 2025 04:22
-
-
Save Vienchau/3fa41abc902710d5ed36e47f8a9309fa 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> | |
/** | |
* Constants for ubus configuration | |
*/ | |
#define TOPIC_NAME "topology.connection" | |
#define UBUS_SOCKET "/var/run/ubus.sock" | |
/** | |
* Example JSON payload to publish | |
*/ | |
#define EXAMPLE_PAYLOAD "{\"mac_client\":\"000000000000\",\"timestamp\":1741318099,\"status\":1,\"hostname\":\"test\",\"ip\":\"192.168.1.100\"}" | |
/** | |
* Push JSON data to ubus service | |
* | |
* @param ctx Ubus context | |
* @return 0 on success, -1 on failure | |
*/ | |
static int push_json_to_ubus(struct ubus_context *ctx) { | |
struct blob_buf b = {}; | |
int ret = 0; | |
uint32_t id; | |
if (!ctx) { | |
fprintf(stderr, "Invalid ubus context\n"); | |
return -1; | |
} | |
blob_buf_init(&b, 0); | |
// Parse and add JSON payload to blob buffer | |
if (!blobmsg_add_json_from_string(&b, EXAMPLE_PAYLOAD)) { | |
fprintf(stderr, "Failed to parse JSON payload\n"); | |
ret = -1; | |
goto cleanup; | |
} | |
// Look up the service object by name | |
if (ubus_lookup_id(ctx, TOPIC_NAME, &id) != 0) { | |
fprintf(stderr, "[CLIENT] Failed to find %s object on UBUS.\n", TOPIC_NAME); | |
ret = -1; | |
goto cleanup; | |
} | |
// Invoke the push_event method on the service | |
if (ubus_invoke(ctx, id, "push_event", b.head, NULL, NULL, 1000) != 0) { | |
fprintf(stderr, "[CLIENT] Failed to push JSON to server.\n"); | |
ret = -1; | |
goto cleanup; | |
} | |
printf("[CLIENT] JSON pushed to server successfully.\n"); | |
cleanup: | |
blob_buf_free(&b); | |
return ret; | |
} | |
/** | |
* Initialize ubus connection | |
* | |
* @param ubus_socket Path to ubus socket | |
* @return Ubus context on success, NULL on failure | |
*/ | |
static struct ubus_context *init_ubus(const char *ubus_socket) { | |
struct ubus_context *ctx; | |
ctx = ubus_connect(ubus_socket); | |
if (!ctx) { | |
fprintf(stderr, "Failed to connect to UBUS at %s\n", ubus_socket); | |
return NULL; | |
} | |
printf("Connected to UBUS at %s\n", ubus_socket); | |
return ctx; | |
} | |
/** | |
* Main function | |
*/ | |
int main(int argc, char **argv) { | |
struct ubus_context *ctx; | |
int ret = EXIT_SUCCESS; | |
// Initialize ubus connection | |
ctx = init_ubus(UBUS_SOCKET); | |
if (!ctx) { | |
return EXIT_FAILURE; | |
} | |
// Publish JSON data | |
if (push_json_to_ubus(ctx) < 0) { | |
ret = EXIT_FAILURE; | |
} | |
// Cleanup | |
ubus_free(ctx); | |
printf("Disconnected from UBUS\n"); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment