Implemented "ros_system_resource", a high-level interface for /system/resource/print.
authorFlorian Forster <octo@verplant.org>
Wed, 23 Dec 2009 15:08:21 +0000 (16:08 +0100)
committerFlorian Forster <octo@verplant.org>
Wed, 23 Dec 2009 15:08:21 +0000 (16:08 +0100)
src/Makefile.am
src/ros_parse.c
src/ros_parse.h
src/routeros_api.h
src/system_resource.c [new file with mode: 0644]

index 34f3efb..149682c 100644 (file)
@@ -19,7 +19,8 @@ endif
 librouteros_la_SOURCES = main.c routeros_api.h routeros_version.h \
                         ros_parse.c ros_parse.h \
                         registration_table.c \
-                        interface.c
+                        interface.c \
+                        system_resource.c
 
 bin_PROGRAMS = ros
 
index d236739..b9d7e99 100644 (file)
@@ -68,6 +68,23 @@ unsigned int sstrtoui (const char *str) /* {{{ */
        return (ret);
 } /* }}} unsigned int sstrtoui */
 
+uint64_t sstrtoui64 (const char *str) /* {{{ */
+{
+       uint64_t ret;
+       char *endptr;
+
+       if (str == NULL)
+               return (0);
+
+       errno = 0;
+       endptr = NULL;
+       ret = (uint64_t) strtoull (str, &endptr, /* base = */ 10);
+       if ((endptr == str) || (errno != 0))
+               return (0);
+
+       return (ret);
+} /* }}} uint64_t sstrtoui64 */
+
 double sstrtod (const char *str) /* {{{ */
 {
        double ret;
index c68f0f2..a39ac89 100644 (file)
@@ -25,6 +25,7 @@
 _Bool sstrtob (const char *str);
 
 unsigned int sstrtoui (const char *str);
+uint64_t sstrtoui64 (const char *str);
 
 double sstrtod (const char *str);
 
index 4527876..0811b7b 100644 (file)
@@ -189,6 +189,41 @@ int ros_registration_table (ros_connection_t *c,
                ros_registration_table_handler_t handler, void *user_data);
 /* }}} /interface/wireless/registration-table */
 
+/* High-level function for accessing /system/resource {{{ */
+struct ros_system_resource_s;
+typedef struct ros_system_resource_s ros_system_resource_t;
+struct ros_system_resource_s
+{
+       uint64_t uptime;
+
+       const char *version;
+  const char *architecture_name;
+  const char *board_name;
+
+  const char *cpu_model;
+       unsigned int cpu_count;
+       unsigned int cpu_load;
+  uint64_t cpu_frequency;
+
+       uint64_t free_memory;
+       uint64_t total_memory;
+
+       uint64_t free_hdd_space;
+  uint64_t total_hdd_space;
+
+  uint64_t write_sect_since_reboot;
+  uint64_t write_sect_total;
+  uint64_t bad_blocks;
+};
+
+/* Callback function */
+typedef int (*ros_system_resource_handler_t) (ros_connection_t *c,
+               const ros_system_resource_t *r, void *user_data);
+
+int ros_system_resource (ros_connection_t *c,
+               ros_system_resource_handler_t handler, void *user_data);
+/* }}} /system/resource */
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/system_resource.c b/src/system_resource.c
new file mode 100644 (file)
index 0000000..2e1ef85
--- /dev/null
@@ -0,0 +1,127 @@
+/**
+ * librouteros - src/interface.c
+ * Copyright (C) 2009  Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ **/
+
+#ifndef _ISOC99_SOURCE
+# define _ISOC99_SOURCE
+#endif
+
+#ifndef _POSIX_C_SOURCE
+# define _POSIX_C_SOURCE 200112L
+#endif
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <math.h>
+#include <errno.h>
+#include <string.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <assert.h>
+
+#include "routeros_api.h"
+#include "ros_parse.h"
+
+/*
+ * Private data types
+ */
+struct rt_internal_data_s
+{
+       ros_system_resource_handler_t handler;
+       void *user_data;
+};
+typedef struct rt_internal_data_s rt_internal_data_t;
+
+/*
+ * Private functions
+ */
+static int rt_reply_to_system_resource (const ros_reply_t *r, /* {{{ */
+               ros_system_resource_t *ret)
+{
+       if (r == NULL)
+               return (EINVAL);
+
+       if (strcmp ("re", ros_reply_status (r)) != 0)
+               return (rt_reply_to_system_resource (ros_reply_next (r), ret));
+
+       /* TODO: Uptime */
+
+       ret->version = ros_reply_param_val_by_key (r, "version");
+       ret->architecture_name = ros_reply_param_val_by_key (r, "architecture-name");
+       ret->board_name = ros_reply_param_val_by_key (r, "board-name");
+
+       ret->cpu_model = ros_reply_param_val_by_key (r, "cpu");
+       ret->cpu_count = sstrtoui (ros_reply_param_val_by_key (r, "cpu-count"));
+       ret->cpu_load = sstrtoui (ros_reply_param_val_by_key (r, "cpu-load"));
+       ret->cpu_frequency = sstrtoui64 (ros_reply_param_val_by_key (r, "cpu-frequency"));
+
+       ret->free_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "free-memory"));
+       ret->total_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "total-memory"));
+
+       ret->free_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "free-hdd-space"));
+       ret->total_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "total-hdd-space"));
+
+       ret->write_sect_since_reboot = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-since-reboot"));
+       ret->write_sect_total = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-total"));
+       ret->bad_blocks = sstrtoui64 (ros_reply_param_val_by_key (r, "bad-blocks"));
+
+       return (0);
+} /* }}} int rt_reply_to_system_resource */
+
+static int sr_internal_handler (ros_connection_t *c, /* {{{ */
+               const ros_reply_t *r, void *user_data)
+{
+       ros_system_resource_t sys_res;
+       rt_internal_data_t *internal_data;
+       int status;
+
+       memset (&sys_res, 0, sizeof (sys_res));
+       status = rt_reply_to_system_resource (r, &sys_res);
+       if (status != 0)
+               return (status);
+
+       internal_data = user_data;
+
+       status = internal_data->handler (c, &sys_res, internal_data->user_data);
+
+       return (status);
+} /* }}} int sr_internal_handler */
+
+/*
+ * Public functions
+ */
+int ros_system_resource (ros_connection_t *c, /* {{{ */
+               ros_system_resource_handler_t handler, void *user_data)
+{
+       rt_internal_data_t data;
+
+       if ((c == NULL) || (handler == NULL))
+               return (EINVAL);
+
+       data.handler = handler;
+       data.user_data = user_data;
+
+       return (ros_query (c, "/system/resource/print",
+                               /* args_num = */ 0, /* args = */ NULL,
+                               sr_internal_handler, &data));
+} /* }}} int ros_system_resource */
+
+/* vim: set ts=2 sw=2 noet fdm=marker : */