summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/misc_eoc.c
blob: 5811c297fa875c06f89386ccdd9b3fdea4efdb51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* SPC300 bundle {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/misc_eoc.c
 * \brief   Miscellaneous EoC-specific functions
 * \ingroup libspid
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <syslog.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/netlink.h>

#include "libspid.h"
#include "nvram.h"
#include "libmme.h"

/**
 * Reboots the slave with MAC address given as parameter.
 * \param mac_address MAC address of the slave to reboot
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameter
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */
libspid_error_t
libspid_eoc_reboot (char *mac_address)
{
    mme_ctx_t request_ctx, confirm_ctx;
    unsigned char snd_buffer[ETH_DATA_LEN];
    int ret;
    unsigned int result_len = 0;
    char *iface = LIBSPID_EOC_BR_IFACE;
    unsigned char mac_address_bin[ETH_ALEN];
    unsigned char result;

    /* initialize indication MME for VS_RESET */
    ret = mme_init (&request_ctx, VS_RESET | MME_REQ,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return ret;
    ret = mme_init (&confirm_ctx, VS_RESET | MME_CNF,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return ret;

    ret = libspid_mac_str_to_bin (mac_address, mac_address_bin);
    if (ret != LIBSPID_SUCCESS)
        return ret;

    /* send indication MME */
    ret = mme_send (&request_ctx, MME_SEND_REQ_CNF, iface,
                    mac_address_bin, &confirm_ctx);
    if (MME_SUCCESS != ret)
        return ret;

    /* extract SPIDCOM OUI and check if it is correct */
    if (confirm_ctx.oui != MME_OUI_SPIDCOM)
        return LIBSPID_ERROR_MME;

    /* collect confirmation result */
    ret = mme_pull (&confirm_ctx, &result, 1, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    return (result == 0) ? LIBSPID_SUCCESS : LIBSPID_ERROR_SYSTEM;
}

/**
 * Restore to factory defaults the slave with MAC address given as parameter.
 * \param mac_address MAC address of the slave to factory
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameter
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */
libspid_error_t
libspid_eoc_factory (char *mac_address)
{
    mme_ctx_t request_ctx, confirm_ctx;
    unsigned char snd_buffer[ETH_DATA_LEN];
    int ret;
    unsigned int result_len = 0;
    char *iface = LIBSPID_EOC_BR_IFACE;
    unsigned char mac_address_bin[ETH_ALEN];
    unsigned char result;

    if (NULL == mac_address)
        return LIBSPID_ERROR_PARAM;

    /* initialize indication MME for VS_FACTORY */
    ret = mme_init (&request_ctx, VS_FACTORY_REQ,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return ret;
    ret = mme_init (&confirm_ctx, VS_FACTORY_CNF,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return ret;

    ret = libspid_mac_str_to_bin (mac_address, mac_address_bin);
    if (ret != LIBSPID_SUCCESS)
        return ret;

    /* send indication MME */
    ret = mme_send (&request_ctx, MME_SEND_REQ_CNF, iface,
                    mac_address_bin, &confirm_ctx);
    if (MME_SUCCESS != ret)
        return ret;

    /* extract SPIDCOM OUI and check if it is correct */
    if (confirm_ctx.oui != MME_OUI_SPIDCOM)
        return LIBSPID_ERROR_MME;

    /* collect confirmation result */
    ret = mme_pull (&confirm_ctx, &result, 1, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    return (result == 0) ? LIBSPID_SUCCESS : LIBSPID_ERROR_SYSTEM;
}

/**
 * Returns list of MAC addresses of online slaves and number of online slaves.
 * \param mac_address_list pointer to store the list of MAC addresses
 * \param mac_address_count pointer to store number of online slaves
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */
extern libspid_error_t
libspid_eoc_get_topo (char *mac_address_list, int *mac_address_count)
{
    const char delimiters[2] = LIBSPID_ONLINE_INFO_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN] = {0};
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB] = {0};
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN] = {0};
    libspid_error_t error = LIBSPID_SUCCESS;

    /* check input parameters */
    if ((mac_address_list == NULL) || (mac_address_count == NULL))
        return LIBSPID_ERROR_PARAM;

    /* skim through lines of online info file */
    for (*mac_address_count = 0;
         *mac_address_count < LIBSPID_ONLINE_INFO_LINE_MAX_NB;
         (*mac_address_count)++)
    {
        /* get current line key */
        error = libspid_config_read_line (LIBSPID_ONLINE_INFO_PATH,
                                          delimiters, key, &elt_number,
                                          elt, buffer,
                                          LIBSPID_CONFIG_LINE_MAX_LEN);
        if (error != LIBSPID_SUCCESS)
            return error;

        /* check that eof is not reached */
        if (!strcmp (key, ""))
            break;

        /* convert mac address from string to char array */
        error = libspid_mac_str_to_bin (key, (unsigned char *)
                                        mac_address_list + LIBSPID_MAC_BIN_LEN
                                        * (*mac_address_count));
        if (error != LIBSPID_SUCCESS)
            return error;
    }

    return LIBSPID_SUCCESS;
}

/**
 * Returns real-time statistics of a slave with given MAC address.
 * \param mac_address MAC address of the slave
 * \param eoc_rt_stat pointer to structure for storing real-time statistics
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_GEN: bad result or oui received
 * \return LIBSPID_ERROR_MME: mme function failed
 */
extern libspid_error_t
libspid_eoc_get_stat (char *mac_address, libspid_eoc_rt_stat_t *eoc_rt_stat)
{
    mme_ctx_t request_ctx, confirm_ctx;
    unsigned char snd_buffer[ETH_DATA_LEN];
    int ret = 0;
    unsigned int result_len = 0;
    unsigned char mac_address_bin[ETH_ALEN];
    unsigned char result = 0;
    unsigned char control_value = 0;

    if (mac_address == NULL || eoc_rt_stat == NULL)
    {
        return LIBSPID_ERROR_PARAM;
    }

    ret = libspid_mac_str_to_bin (mac_address, mac_address_bin);
    if (ret != LIBSPID_SUCCESS)
        return ret;

    /** initialize MME for VS_EOC_REAL_TIME_STATISTICS **/
    ret = mme_init (&request_ctx,
                    VS_EOC_GET_REAL_TIME_STATISTICS | MME_REQ,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return LIBSPID_ERROR_MME;

    ret = mme_init (&confirm_ctx,
                    VS_EOC_GET_REAL_TIME_STATISTICS | MME_CNF,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return LIBSPID_ERROR_MME;

    control_value = LIBSPID_EOC_REAL_TIME_STATISTICS_GET;

    ret = mme_put (&request_ctx, &control_value, 1, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    /** send VS_EOC_REAL_TIME_STATISTICS MME request **/
    ret = mme_send (&request_ctx, MME_SEND_REQ_CNF,
                    NULL, mac_address_bin, &confirm_ctx);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    /** extract SPIDCOM OUI and check if it is correct **/
    if (confirm_ctx.oui != MME_OUI_SPIDCOM)
        return LIBSPID_ERROR_GEN;

    /** collect confirmation result **/
    ret = mme_pull (&confirm_ctx, &result, 1, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    if (result != 0)
        return LIBSPID_ERROR_GEN;

    /** collect data **/
    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->rx_pkt, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->tx_pkt, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->rx_byte, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->tx_byte, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->rx_bcast, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->tx_bcast, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->rx_mcast, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->tx_mcast, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->rx_crc, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->rx_short, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->tx_short, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->rx_dropped, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->tx_dropped, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    ret = mme_pull (&confirm_ctx, &eoc_rt_stat->avg_prefec, 4, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    return LIBSPID_SUCCESS;
}

/**
 * Resets real-time statistics of a slave with given MAC address.
 * \param mac_address MAC address of the slave
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_GEN: bad result or oui received
 * \return LIBSPID_ERROR_MME: mme function failed
 */

extern libspid_error_t
libspid_eoc_reset_stat (char *mac_address)
{
    mme_ctx_t request_ctx, confirm_ctx;
    unsigned char snd_buffer[ETH_DATA_LEN];
    int ret = 0;
    unsigned int result_len = 0;
    unsigned char mac_address_bin[ETH_ALEN];
    unsigned char result = 0;
    unsigned char control_value = 0;

    if (mac_address == NULL)
    {
        return LIBSPID_ERROR_PARAM;
    }

    ret = libspid_mac_str_to_bin (mac_address, mac_address_bin);
    if (ret != LIBSPID_SUCCESS)
        return ret;

    /** initialize MME for VS_EOC_REAL_TIME_STATISTICS **/
    ret = mme_init (&request_ctx,
                    VS_EOC_GET_REAL_TIME_STATISTICS | MME_REQ,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return LIBSPID_ERROR_MME;

    ret = mme_init (&confirm_ctx,
                    VS_EOC_GET_REAL_TIME_STATISTICS | MME_CNF,
                    snd_buffer, (unsigned int) ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
        return LIBSPID_ERROR_MME;

    control_value = LIBSPID_EOC_REAL_TIME_STATISTICS_RESET;

    ret = mme_put (&request_ctx, &control_value, 1, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    /** send VS_EOC_REAL_TIME_STATISTICS MME request **/
    ret = mme_send (&request_ctx, MME_SEND_REQ_CNF, NULL,
                    mac_address_bin, &confirm_ctx);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    /** extract SPIDCOM OUI and check if it is correct **/
    if (confirm_ctx.oui != MME_OUI_SPIDCOM)
        return LIBSPID_ERROR_GEN;

    /** collect confirmation result **/
    ret = mme_pull (&confirm_ctx, &result, 1, &result_len);
    if (MME_SUCCESS != ret)
        return LIBSPID_ERROR_MME;

    if (result != 0)
        return LIBSPID_ERROR_GEN;

    return LIBSPID_SUCCESS;
}

/**
 * Returns device information for a slave with given MAC address.<BR>
 * Device information includes:
 * model number, port amount and authorization state.
 * \param mac_address MAC address of the slave
 * \param eoc_dev_info pointer to structure for storing device information
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

extern libspid_error_t
libspid_eoc_get_device_info (char *mac_address,
                             libspid_eoc_dev_info_t *eoc_dev_info)
{
    const char delimiters[2] = LIBSPID_ONLINE_INFO_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN];
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB];
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN];
    libspid_error_t error = LIBSPID_SUCCESS;

    /* check input parameters */
    if ((mac_address == NULL) || (eoc_dev_info == NULL))
        return LIBSPID_ERROR_PARAM;

    /* duplicate MAC address to avoid its corruption */
    strcpy (key, mac_address);
    error = libspid_config_read_line (LIBSPID_ONLINE_INFO_PATH,
                                      delimiters, key, &elt_number,
                                      elt, buffer,
                                      LIBSPID_CONFIG_LINE_MAX_LEN);
    /* get current line key */
    if (error != LIBSPID_SUCCESS)
        return error;

    /* fill-in the device info */
    strcpy (eoc_dev_info->model_no, elt[0]);
    eoc_dev_info->eth_port_nb = atoi(elt[1]);
    strcpy (eoc_dev_info->sw_version, elt[3]);
    eoc_dev_info->downstream_attenuation = atoi(elt[4]);

    return LIBSPID_SUCCESS;
}

/**
 * Returns link quality for a slave with given MAC address.<BR>
 * Link quality includes: downstream and upstream attenuation and downstream
 * and upstream quality.
 * \param mac_address MAC address of the slave
 * \param eoc_link_quality pointer to structure for storing link quality
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

extern libspid_error_t
libspid_eoc_get_link (char *mac_address,
                      libspid_eoc_link_quality_t *eoc_link_quality)
{
    const char delimiters[2] = LIBSPID_ONLINE_INFO_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN];
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB];
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN];
    libspid_error_t error = LIBSPID_SUCCESS;

    /* check input parameters */
    if ((mac_address == NULL) || (eoc_link_quality == NULL))
        return LIBSPID_ERROR_PARAM;

    /* duplicate MAC address to avoid its corruption */
    strcpy (key, mac_address);

    /* get current line key from info file */
    error = libspid_config_read_line (LIBSPID_ONLINE_INFO_PATH,
                                      delimiters, key, &elt_number,
                                      elt, buffer,
                                      LIBSPID_CONFIG_LINE_MAX_LEN);
    if (error != LIBSPID_SUCCESS)
        return error;

    /* fill-in the attenuation info */
    eoc_link_quality->down_att = atoi (elt[4]);
    eoc_link_quality->up_att = atoi (elt[5]);

    /* TODO: fill-in the quality info */
    eoc_link_quality->up_quality = 100;
    eoc_link_quality->down_quality = 100;

    return LIBSPID_SUCCESS;
}