b556bcb10f1675c8618ff262192c50851fd76ed6
[routeros-api.git] / src / routeros_api.h
1 /**
2  * librouteros - src/routeros_api.h
3  * Copyright (C) 2009  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #ifndef ROUTEROS_API_H
23 #define ROUTEROS_API_H 1
24
25 #include <stdint.h>
26 #include <inttypes.h>
27
28 #include <routeros_version.h>
29
30 #define ROUTEROS_API_PORT "8728"
31
32 /*
33  * C++ doesn't have _Bool. We can't simply "#define _Bool bool", because we
34  * don't know if "bool" and "_Bool" are of the same size. If they are not, this
35  * would result in ABI incompatible code.
36  *
37  * So we're doing a best-effort solution here: If we're compiled with the GNU
38  * C++ compiler, g++, we include <stdbool.h>. The GCC will, as a GNU extension,
39  * define _Bool for C++. Since it's the compiler doing the definition, it's
40  * kind of save to assume that it will be done in an ABI compatible manner.
41  *
42  * If this results in any problems for you, define "ROS_HAVE_CPP_BOOL" to true
43  * to have this magic disabled. You will then have to define _Bool yourself.
44  *
45  * TODO: Write a test program for the configure sript to figure out the size of
46  *   _Bool. Make this size available via <routeros_versioin.h> and define _Bool
47  *   to short, long, ... here.
48  */
49 #ifdef __cplusplus
50 # if !defined (ROS_HAVE_CPP_BOOL) || !ROS_HAVE_CPP_BOOL
51 #  ifdef __GNUC__
52 #   include <stdbool.h>
53 #  endif /* __GNUC__ */
54 # endif /* !defined (ROS_HAVE_CPP_BOOL) || !ROS_HAVE_CPP_BOOL */
55
56 extern "C" {
57 #endif
58
59 struct ros_connection_s;
60 typedef struct ros_connection_s ros_connection_t;
61
62 struct ros_reply_s;
63 typedef struct ros_reply_s ros_reply_t;
64
65 typedef int (*ros_reply_handler_t) (ros_connection_t *c, const ros_reply_t *r,
66                 void *user_data);
67
68 /*
69  * Connection handling
70  */
71 ros_connection_t *ros_connect (const char *node, const char *service,
72                 const char *username, const char *password);
73 int ros_disconnect (ros_connection_t *con);
74
75 /* 
76  * Command execution
77  */
78 int ros_query (ros_connection_t *c,
79                 const char *command,
80                 size_t args_num, const char * const *args,
81                 ros_reply_handler_t handler, void *user_data);
82
83 /* 
84  * Reply handling
85  */
86 const ros_reply_t *ros_reply_next (const ros_reply_t *r);
87 int ros_reply_num (const ros_reply_t *r);
88
89 const char *ros_reply_status (const ros_reply_t *r);
90
91 /* Receiving reply parameters */
92 const char *ros_reply_param_key_by_index (const ros_reply_t *r,
93                 unsigned int index);
94 const char *ros_reply_param_val_by_index (const ros_reply_t *r,
95                 unsigned int index);
96 const char *ros_reply_param_val_by_key (const ros_reply_t *r, const char *key);
97
98 /* High-level function for accessing /interface {{{ */
99 struct ros_interface_s;
100 typedef struct ros_interface_s ros_interface_t;
101 struct ros_interface_s
102 {
103         /* Name of the interface */
104         const char *name;
105         const char *type;
106         const char *comment;
107
108         /* Packet, octet and error counters. */
109         uint64_t rx_packets;
110         uint64_t tx_packets;
111         uint64_t rx_bytes;
112         uint64_t tx_bytes;
113         uint64_t rx_errors;
114         uint64_t tx_errors;
115         uint64_t rx_drops;
116         uint64_t tx_drops;
117
118         /* Maximum transfer unit */
119         unsigned int mtu;
120         unsigned int l2mtu;
121
122         /* Interface flags */
123         _Bool dynamic;
124         _Bool running;
125         _Bool enabled;
126
127         /* Next interface */
128         const ros_interface_t *next;
129 };
130
131 /* Callback function */
132 typedef int (*ros_interface_handler_t) (ros_connection_t *c,
133                 const ros_interface_t *i, void *user_data);
134
135 int ros_interface (ros_connection_t *c,
136                 ros_interface_handler_t handler, void *user_data);
137 /* }}} /interface */
138
139 /* High-level function for accessing /interface/wireless/registration-table {{{ */
140 struct ros_registration_table_s;
141 typedef struct ros_registration_table_s ros_registration_table_t;
142 struct ros_registration_table_s
143 {
144         /* Name of the interface */
145         const char *interface;
146         /* Name of the remote radio */
147         const char *radio_name;
148
149         /* ap is set to true, if the REMOTE radio is an access point. */
150         _Bool ap;
151         _Bool wds;
152
153         /* Receive and transmit rate in MBit/s */
154         double rx_rate;
155         double tx_rate;
156
157         /* Packet, octet and frame counters. */
158         uint64_t rx_packets;
159         uint64_t tx_packets;
160         uint64_t rx_bytes;
161         uint64_t tx_bytes;
162         uint64_t rx_frames;
163         uint64_t tx_frames;
164         uint64_t rx_frame_bytes;
165         uint64_t tx_frame_bytes;
166         uint64_t rx_hw_frames;
167         uint64_t tx_hw_frames;
168         uint64_t rx_hw_frame_bytes;
169         uint64_t tx_hw_frame_bytes;
170
171         /* Signal quality information (in dBm) */
172         double rx_signal_strength;
173         double tx_signal_strength;
174         double signal_to_noise;
175
176         /* Overall connection quality (in percent) */
177         double rx_ccq;
178         double tx_ccq;
179
180         /* Next interface */
181         const ros_registration_table_t *next;
182 };
183
184 /* Callback function */
185 typedef int (*ros_registration_table_handler_t) (ros_connection_t *c,
186                 const ros_registration_table_t *r, void *user_data);
187
188 int ros_registration_table (ros_connection_t *c,
189                 ros_registration_table_handler_t handler, void *user_data);
190 /* }}} /interface/wireless/registration-table */
191
192 /* High-level function for accessing /system/resource {{{ */
193 struct ros_system_resource_s;
194 typedef struct ros_system_resource_s ros_system_resource_t;
195 struct ros_system_resource_s
196 {
197         uint64_t uptime;
198
199         const char *version;
200         const char *architecture_name;
201         const char *board_name;
202
203         const char *cpu_model;
204         unsigned int cpu_count;
205         unsigned int cpu_load;
206         uint64_t cpu_frequency;
207
208         uint64_t free_memory;
209         uint64_t total_memory;
210
211         uint64_t free_hdd_space;
212         uint64_t total_hdd_space;
213
214         uint64_t write_sect_since_reboot;
215         uint64_t write_sect_total;
216         uint64_t bad_blocks;
217 };
218
219 /* Callback function */
220 typedef int (*ros_system_resource_handler_t) (ros_connection_t *c,
221                 const ros_system_resource_t *r, void *user_data);
222
223 int ros_system_resource (ros_connection_t *c,
224                 ros_system_resource_handler_t handler, void *user_data);
225 /* }}} /system/resource */
226
227 #ifdef __cplusplus
228 }
229 #endif
230
231 #endif /* ROUTEROS_API_H */
232
233 /* vim: set ts=2 sw=2 noet fdm=marker : */