Relicense to ISC License.
[routeros-api.git] / src / system_resource.c
1 /**
2  * librouteros - src/system_resource.c
3  * Copyright (C) 2009  Florian octo Forster
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  *
17  * Authors:
18  *   Florian octo Forster <octo at verplant.org>
19  **/
20
21 #ifndef _ISOC99_SOURCE
22 # define _ISOC99_SOURCE
23 #endif
24
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
27 #endif
28
29 #include "config.h"
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <inttypes.h>
37 #include <assert.h>
38
39 #include "routeros_api.h"
40 #include "ros_parse.h"
41
42 /*
43  * Private data types
44  */
45 struct rt_internal_data_s
46 {
47         ros_system_resource_handler_t handler;
48         void *user_data;
49 };
50 typedef struct rt_internal_data_s rt_internal_data_t;
51
52 /*
53  * Private functions
54  */
55 static int rt_reply_to_system_resource (const ros_reply_t *r, /* {{{ */
56                 ros_system_resource_t *ret)
57 {
58         if (r == NULL)
59                 return (EINVAL);
60
61         if (strcmp ("re", ros_reply_status (r)) != 0)
62                 return (rt_reply_to_system_resource (ros_reply_next (r), ret));
63
64         ret->uptime = sstrtodate (ros_reply_param_val_by_key (r, "uptime"));
65
66         ret->version = ros_reply_param_val_by_key (r, "version");
67         ret->architecture_name = ros_reply_param_val_by_key (r, "architecture-name");
68         ret->board_name = ros_reply_param_val_by_key (r, "board-name");
69
70         ret->cpu_model = ros_reply_param_val_by_key (r, "cpu");
71         ret->cpu_count = sstrtoui (ros_reply_param_val_by_key (r, "cpu-count"));
72         ret->cpu_load = sstrtoui (ros_reply_param_val_by_key (r, "cpu-load"));
73         ret->cpu_frequency = sstrtoui64 (ros_reply_param_val_by_key (r, "cpu-frequency")) * 1000000;
74
75         /* One "kilobyte" is 1024 bytes, according to "janisk", see
76          * <http://forum.mikrotik.com/viewtopic.php?f=2&t=37943> */
77         ret->free_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "free-memory")) * 1024;
78         ret->total_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "total-memory")) * 1024;
79
80         ret->free_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "free-hdd-space")) * 1024;
81         ret->total_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "total-hdd-space")) * 1024;
82
83         ret->write_sect_since_reboot = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-since-reboot"));
84         ret->write_sect_total = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-total"));
85         ret->bad_blocks = sstrtoui64 (ros_reply_param_val_by_key (r, "bad-blocks"));
86
87         return (0);
88 } /* }}} int rt_reply_to_system_resource */
89
90 static int sr_internal_handler (ros_connection_t *c, /* {{{ */
91                 const ros_reply_t *r, void *user_data)
92 {
93         ros_system_resource_t sys_res;
94         rt_internal_data_t *internal_data;
95         int status;
96
97         memset (&sys_res, 0, sizeof (sys_res));
98         status = rt_reply_to_system_resource (r, &sys_res);
99         if (status != 0)
100                 return (status);
101
102         internal_data = user_data;
103
104         status = internal_data->handler (c, &sys_res, internal_data->user_data);
105
106         return (status);
107 } /* }}} int sr_internal_handler */
108
109 /*
110  * Public functions
111  */
112 int ros_system_resource (ros_connection_t *c, /* {{{ */
113                 ros_system_resource_handler_t handler, void *user_data)
114 {
115         rt_internal_data_t data;
116
117         if ((c == NULL) || (handler == NULL))
118                 return (EINVAL);
119
120         data.handler = handler;
121         data.user_data = user_data;
122
123         return (ros_query (c, "/system/resource/print",
124                                 /* args_num = */ 0, /* args = */ NULL,
125                                 sr_internal_handler, &data));
126 } /* }}} int ros_system_resource */
127
128 /* vim: set ts=2 sw=2 noet fdm=marker : */