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