1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
/**
* @file oper_data_pull_example.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief example of an application providing some operational data by a callback
*
* @copyright
* Copyright (c) 2019 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _QNX_SOURCE /* sleep() */
#define _GNU_SOURCE
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libyang/libyang.h>
#include "sysrepo.h"
volatile int exit_application = 0;
static int
dp_get_items_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name, const char *xpath,
const char *request_xpath, uint32_t request_id, struct lyd_node **parent, void *private_data)
{
const struct ly_ctx *ly_ctx;
(void)session;
(void)sub_id;
(void)request_xpath;
(void)request_id;
(void)private_data;
printf("\n\n ========== DATA FOR \"%s\" \"%s\" REQUESTED =======================\n\n", module_name, xpath);
if (!strcmp(module_name, "examples") && !strcmp(xpath, "/examples:stats")) {
ly_ctx = sr_acquire_context(sr_session_get_connection(session));
lyd_new_path(NULL, ly_ctx, "/examples:stats/counter", "852", 0, parent);
lyd_new_path(*parent, NULL, "/examples:stats/counter2", "1052", 0, NULL);
sr_release_context(sr_session_get_connection(session));
}
return SR_ERR_OK;
}
static void
sigint_handler(int signum)
{
(void)signum;
exit_application = 1;
}
int
main(int argc, char **argv)
{
sr_conn_ctx_t *connection = NULL;
sr_session_ctx_t *session = NULL;
sr_subscription_ctx_t *subscription = NULL;
int rc = SR_ERR_OK;
const char *mod_name, *path;
if (argc != 3) {
printf("%s <module-to-provide-data-from> <path-to-provide>\n", argv[0]);
return EXIT_FAILURE;
}
mod_name = argv[1];
path = argv[2];
printf("Application will provide data \"%s\" of \"%s\".\n\n", path, mod_name);
/* turn logging on */
sr_log_stderr(SR_LL_WRN);
/* connect to sysrepo */
rc = sr_connect(0, &connection);
if (rc != SR_ERR_OK) {
goto cleanup;
}
/* start session */
rc = sr_session_start(connection, SR_DS_RUNNING, &session);
if (rc != SR_ERR_OK) {
goto cleanup;
}
/* subscribe for providing the operational data */
rc = sr_oper_get_subscribe(session, mod_name, path, dp_get_items_cb, NULL, 0, &subscription);
if (rc != SR_ERR_OK) {
goto cleanup;
}
printf("\n\n ========== LISTENING FOR REQUESTS ==========\n\n");
/* loop until ctrl-c is pressed / SIGINT is received */
signal(SIGINT, sigint_handler);
signal(SIGPIPE, SIG_IGN);
while (!exit_application) {
sleep(1000);
}
printf("Application exit requested, exiting.\n");
cleanup:
sr_disconnect(connection);
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
|