summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/misc.c
blob: 1c10bd0ef0f3f68d0c27203c0cb322ba24eb1dcc (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
/* SPC300 bundle {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/misc.c
 * \brief   misc functions
 * \ingroup libspid
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include "libspid.h"

/**
 * Convert a MAC address in string format aa:bb:cc:dd:ee:ff into a
 * MAC address in binary format (6 bytes).<br>
 * The array to store the binary MAC address needs to be provided.<br>
 * Example : "00:23:54:ae:f5:24" ---> { 0x00, 0x23, 0x54, 0xae, 0xf5, 0x24 }
 *
 * \param str MAC address string
 * \param bin MAC address returned in binary format
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 */

libspid_error_t libspid_mac_str_to_bin(const char *str, unsigned char *bin)
{
    int i;
    char *s, *e;

    if( (bin == NULL) || (str == NULL) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    s = (char *)str;
	for (i = 0; i < 6; ++i)
    {
		bin[i] = s ? strtoul (s, &e, 16) : 0;
		if (s)
			s = (*e) ? e + 1 : e;
	}

    return LIBSPID_SUCCESS;
}

/**
 * Convert a MAC address in binary format (6 bytes) into a MAC address
 * in string format aa:bb:cc:dd:ee:ff.<br>
 * The array to store the string MAC address needs to be provided with minimal
 * size of 18 bytes (LIBSPID_MAC_STR_LEN).<br>
 * Example : { 0x00, 0x23, 0x54, 0xae, 0xf5, 0x24 } ---> "00:23:54:ae:f5:24"
 *
 * \param bin MAC address returned in binary format
 * \param str MAC address string
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 */

libspid_error_t libspid_mac_bin_to_str(const unsigned char *bin, char *str)
{
    if( (bin == NULL) || (str == NULL) )
    {
        return LIBSPID_ERROR_PARAM;
    }

    sprintf ( str, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
                bin[0], bin[1], bin[2], bin[3], bin[4], bin[5] );

    return LIBSPID_SUCCESS;
}