812f54b0b6ae19591cb4c2a9ab2e51369b06dcc5
[routeros-api.git] / src / registration_table.c
1 /**
2  * librouteros - src/registration_table.c
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 _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #include "config.h"
31
32 #include <stdlib.h>
33 #include <math.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <assert.h>
39
40 #include "routeros_api.h"
41 #include "ros_parse.h"
42
43 /*
44 Status: re
45   Param 0: .id = *C
46   Param 1: interface = wlan2
47   Param 2: radio-name = 000C423AECCF
48   Param 3: mac-address = 00:0C:42:3A:EC:CF
49   Param 4: ap = true
50   Param 5: wds = false
51   Param 6: rx-rate = 58.5Mbps-HT
52   Param 7: tx-rate = 52.0Mbps-HT
53   Param 8: packets = 6962070,10208268
54   Param 9: bytes = 1090924066,2872515632
55   Param 10: frames = 6662813,9698341
56   Param 11: frame-bytes = 1111022872,2846535122
57   Param 12: hw-frames = 8338229,9718084
58   Param 13: hw-frame-bytes = 1941643579,3255546365
59   Param 14: tx-frames-timed-out = 0
60   Param 15: uptime = 2w6d13:21:53
61   Param 16: last-activity = 00:00:00.060
62   Param 17: signal-strength = -74dBm@6Mbps
63   Param 18: signal-to-noise = 42
64   Param 19: strength-at-rates = -74dBm@6Mbps 10ms,-78dBm@9Mbps 2w2d16h56m20s890ms,-78dBm@12Mbps 2w2d16h55m51s670ms,-77dBm@18Mbps 2w2d16h55m35s880ms,-78dBm@24Mbps 2w2d16h55m5s590ms,-75dBm@36Mbps 1d10h58m48s840ms,-75dBm@48Mbps 1d10h48m34s130ms,-77dBm@54Mbps 1d10h47m34s680ms,-73dBm@HT20-4 38m45s600ms,-73dBm@HT20-5 4m700ms,-74dBm@HT20-6 60ms,-78dBm@HT20-7 1d10h30m34s410ms
65   Param 20: tx-signal-strength = -76
66   Param 21: tx-ccq = 51
67   Param 22: rx-ccq = 77
68   Param 23: p-throughput = 36877
69   Param 24: ack-timeout = 30
70   Param 25: nstreme = false
71   Param 26: framing-mode = none
72   Param 27: routeros-version = 4.2
73   Param 28: last-ip = 62.128.1.53
74   Param 29: 802.1x-port-enabled = true
75   Param 30: authentication-type = wpa2-psk
76   Param 31: encryption = aes-ccm
77   Param 32: group-encryption = aes-ccm
78   Param 33: compression = false
79   Param 34: wmm-enabled = true
80 ===
81 Status: done
82 ===
83  */
84
85 /*
86  * Private data types
87  */
88 struct rt_internal_data_s
89 {
90         ros_registration_table_handler_t handler;
91         void *user_data;
92 };
93 typedef struct rt_internal_data_s rt_internal_data_t;
94
95 /*
96  * Private functions
97  */
98 static ros_registration_table_t *rt_reply_to_regtable (const ros_reply_t *r) /* {{{ */
99 {
100         ros_registration_table_t *ret;
101
102         if (r == NULL)
103                 return (NULL);
104
105         if (strcmp ("re", ros_reply_status (r)) != 0)
106                 return (rt_reply_to_regtable (ros_reply_next (r)));
107
108         ret = malloc (sizeof (*ret));
109         if (ret == NULL)
110                 return (NULL);
111         memset (ret, 0, sizeof (*ret));
112
113         ret->interface = ros_reply_param_val_by_key (r, "interface");
114         ret->radio_name = ros_reply_param_val_by_key (r, "radio-name");
115
116         ret->ap = sstrtob (ros_reply_param_val_by_key (r, "ap"));
117         ret->wds = sstrtob (ros_reply_param_val_by_key (r, "wds"));
118
119         ret->rx_rate = sstrtod (ros_reply_param_val_by_key (r, "rx-rate"));
120         ret->tx_rate = sstrtod (ros_reply_param_val_by_key (r, "tx-rate"));
121
122         sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "packets"),
123                         &ret->rx_packets, &ret->tx_packets);
124         sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "bytes"),
125                         &ret->rx_bytes, &ret->tx_bytes);
126         sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "frames"),
127                         &ret->rx_frames, &ret->tx_frames);
128         sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "frame-bytes"),
129                         &ret->rx_frame_bytes, &ret->tx_frame_bytes);
130         sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "hw-frames"),
131                         &ret->rx_hw_frames, &ret->tx_hw_frames);
132         sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "hw-frame-bytes"),
133                         &ret->rx_hw_frame_bytes, &ret->tx_hw_frame_bytes);
134
135         ret->rx_signal_strength = sstrtod (ros_reply_param_val_by_key (r, "signal-strength"));
136         ret->tx_signal_strength = sstrtod (ros_reply_param_val_by_key (r, "tx-signal-strength"));
137         ret->signal_to_noise = sstrtod (ros_reply_param_val_by_key (r, "signal-to-noise"));
138
139         ret->rx_ccq = sstrtod (ros_reply_param_val_by_key (r, "rx-ccq"));
140         ret->tx_ccq = sstrtod (ros_reply_param_val_by_key (r, "tx-ccq"));
141
142         ret->next = rt_reply_to_regtable (ros_reply_next (r));
143
144         return (ret);
145 } /* }}} ros_registration_table_t *rt_reply_to_regtable */
146
147 static void rt_regtable_free (const ros_registration_table_t *r) /* {{{ */
148 {
149         const ros_registration_table_t *next;
150
151         while (r != NULL)
152         {
153                 next = r->next;
154                 free ((void *) r);
155                 r = next;
156         }
157 } /* }}} void rt_regtable_free */
158
159 static int rt_internal_handler (ros_connection_t *c, /* {{{ */
160                 const ros_reply_t *r, void *user_data)
161 {
162         ros_registration_table_t *rt_data;
163         rt_internal_data_t *internal_data;
164         int status;
165
166         rt_data = rt_reply_to_regtable (r);
167         if (rt_data == NULL)
168                 return (errno);
169
170         internal_data = user_data;
171
172         status = internal_data->handler (c, rt_data, internal_data->user_data);
173
174         rt_regtable_free (rt_data);
175
176         return (status);
177 } /* }}} int rt_internal_handler */
178
179 /*
180  * Public functions
181  */
182 int ros_registration_table (ros_connection_t *c, /* {{{ */
183                 ros_registration_table_handler_t handler, void *user_data)
184 {
185         rt_internal_data_t data;
186
187         if ((c == NULL) || (handler == NULL))
188                 return (EINVAL);
189
190         data.handler = handler;
191         data.user_data = user_data;
192
193         return (ros_query (c, "/interface/wireless/registration-table/print",
194                                 /* args_num = */ 0, /* args = */ NULL,
195                                 rt_internal_handler, &data));
196 } /* }}} int ros_registration_table */
197
198 /* vim: set ts=2 sw=2 noet fdm=marker : */