2 * librouteros - src/interface.c
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
40 #include "routeros_api.h"
41 #include "ros_parse.h"
46 struct rt_internal_data_s
48 ros_system_resource_handler_t handler;
51 typedef struct rt_internal_data_s rt_internal_data_t;
56 static int rt_reply_to_system_resource (const ros_reply_t *r, /* {{{ */
57 ros_system_resource_t *ret)
62 if (strcmp ("re", ros_reply_status (r)) != 0)
63 return (rt_reply_to_system_resource (ros_reply_next (r), ret));
65 ret->uptime = sstrtodate (ros_reply_param_val_by_key (r, "uptime"));
67 ret->version = ros_reply_param_val_by_key (r, "version");
68 ret->architecture_name = ros_reply_param_val_by_key (r, "architecture-name");
69 ret->board_name = ros_reply_param_val_by_key (r, "board-name");
71 ret->cpu_model = ros_reply_param_val_by_key (r, "cpu");
72 ret->cpu_count = sstrtoui (ros_reply_param_val_by_key (r, "cpu-count"));
73 ret->cpu_load = sstrtoui (ros_reply_param_val_by_key (r, "cpu-load"));
74 ret->cpu_frequency = sstrtoui64 (ros_reply_param_val_by_key (r, "cpu-frequency")) * 1000000;
76 /* One "kilobyte" is 1024 bytes, according to "janisk", see
77 * <http://forum.mikrotik.com/viewtopic.php?f=2&t=37943> */
78 ret->free_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "free-memory")) * 1024;
79 ret->total_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "total-memory")) * 1024;
81 ret->free_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "free-hdd-space")) * 1024;
82 ret->total_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "total-hdd-space")) * 1024;
84 ret->write_sect_since_reboot = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-since-reboot"));
85 ret->write_sect_total = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-total"));
86 ret->bad_blocks = sstrtoui64 (ros_reply_param_val_by_key (r, "bad-blocks"));
89 } /* }}} int rt_reply_to_system_resource */
91 static int sr_internal_handler (ros_connection_t *c, /* {{{ */
92 const ros_reply_t *r, void *user_data)
94 ros_system_resource_t sys_res;
95 rt_internal_data_t *internal_data;
98 memset (&sys_res, 0, sizeof (sys_res));
99 status = rt_reply_to_system_resource (r, &sys_res);
103 internal_data = user_data;
105 status = internal_data->handler (c, &sys_res, internal_data->user_data);
108 } /* }}} int sr_internal_handler */
113 int ros_system_resource (ros_connection_t *c, /* {{{ */
114 ros_system_resource_handler_t handler, void *user_data)
116 rt_internal_data_t data;
118 if ((c == NULL) || (handler == NULL))
121 data.handler = handler;
122 data.user_data = user_data;
124 return (ros_query (c, "/system/resource/print",
125 /* args_num = */ 0, /* args = */ NULL,
126 sr_internal_handler, &data));
127 } /* }}} int ros_system_resource */
129 /* vim: set ts=2 sw=2 noet fdm=marker : */