From ba1e6b6fa2b34633f022b1900c0e1cf94d305943 Mon Sep 17 00:00:00 2001
From: vdutta <vivek.dutta@iopsys.eu>
Date: Mon, 4 Oct 2021 14:53:19 +0530
Subject: [PATCH] Added customized timeout option for ubus call

---
 main.c      |  7 ++++++-
 ubus_call.c | 11 ++++++++---
 ubus_call.h |  3 ++-
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/main.c b/main.c
index e1968d4..6be1683 100644
--- a/main.c
+++ b/main.c
@@ -41,13 +41,14 @@ struct global_args_t {
   const char *dir_path;
 } global_args;
 
-static const char *opt_string = "f:d:h";
+static const char *opt_string = "f:d:t:h";
 
 static void show_usage(char *application_name) {
 
   fprintf(stdout,
           "\nUsage: %s: <options> \n"
           "Options: \n"
+          "  -t Timeout for ubus call in seconds\n"
           "  -f json file with ubus objects\n"
           "  -d directory with json files \n"
           "  -h help\n\n",
@@ -68,6 +69,10 @@ static void parse_command_line_args(int argc, char *argv[]) {
       global_args.dir_path = optarg;
       break;
 
+    case 't':
+      set_ubus_timeout(atoi(optarg));
+      break;
+
     case 'h':
       show_usage(argv[0]);
       exit(EXIT_SUCCESS);
diff --git a/ubus_call.c b/ubus_call.c
index 354bc8a..67410ee 100644
--- a/ubus_call.c
+++ b/ubus_call.c
@@ -22,10 +22,15 @@
 #include "ubus_call.h"
 #include <time.h>
 
-#define UBUS_DEFAULT_TIMEOUT 1500
 #define UBUS_DEFAULT_POLL_10_SEC 10000
+#define UBUS_DEFAULT_TIMEOUT 1500
 
 struct ubus_context *ctx;
+int g_ubus_timeout = UBUS_DEFAULT_TIMEOUT;
+
+void set_ubus_timeout(int timeout) {
+  g_ubus_timeout = timeout * 1000;
+}
 
 int ubus_init(void) {
 
@@ -99,7 +104,7 @@ struct ubus_result *ubus_call(const char *object, const char *method,
 
   rv = ubus_invoke(ctx, obj_id, method, buff.head,
                    (ubus_data_handler_t)parse_ubus_cb, &json_ubus_result,
-                   UBUS_DEFAULT_TIMEOUT);
+                   g_ubus_timeout);
 
   res = calloc(1, sizeof(struct ubus_result));
   if (!res)
@@ -112,4 +117,4 @@ out:
   blob_buf_free(&buff);
 
   return res;
-}
\ No newline at end of file
+}
diff --git a/ubus_call.h b/ubus_call.h
index cefdc57..14e13d7 100644
--- a/ubus_call.h
+++ b/ubus_call.h
@@ -13,8 +13,9 @@ struct ubus_result
 };
 
 int ubus_init(void);
+void set_ubus_timeout(int timeout);
 
 struct ubus_result *ubus_call(const char *object, const char *method, const char *args);
 
 
-#endif /*  UBUS_CALL_H */
\ No newline at end of file
+#endif /*  UBUS_CALL_H */
-- 
GitLab