summaryrefslogtreecommitdiff
path: root/cleopatre/application/eoc/onlined/src/onlined_mme.c
blob: fbef767cee4150803608b8ccf3978027f47db4fd (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
/* SPC300 project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/eoc/onlined/src/onlined_mme.c
 * \brief   communication with HP AV stack
 * \ingroup onlined
 *
 * All communication functions with the HP AV stack
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <syslog.h>
#include <linux/if_ether.h>
#include <errno.h>
#include <assert.h>
#include <arpa/inet.h>          /* for htons() */
#include "onlined_mme.h"
#include "onlined.h"
#include "../../../../application/spidlib/inc/path.h"

#ifdef __UTESTS__
    #include "../../../../devkit/tests/onlined/utests/override/inc/spidlib_stub.h"
    #include "../../../../devkit/tests/onlined/utests/override/inc/libmme_stub.h"
#else
    #include "spidlib.h"
    #include "libmme.h"
#endif

int snd_rcv_mmes (onlined_ctx_t *ctx)
{
    const char delimiter = ';';
    unsigned char snd_buffer[ETH_DATA_LEN];
    unsigned char rcv_buffer[ETH_DATA_LEN];
    mme_ctx_t mme_request_ctx, mme_confirm_ctx;
    char result, number_of_mac_addr;
    unsigned int result_len, number_of_mac_addr_len;
    unsigned char mac_addr_bin[ETH_ALEN];
    char mac_addr_str[MAC_STRING_MAX_LEN];
    unsigned int mac_addr_bin_len;
    char mac_plc0_str[MAC_STRING_MAX_LEN];
    unsigned char mac_plc0_bin[ETH_ALEN];
    int ret, j;
    char i;
    char *line[10];
    char model_number[64] = "SPiDCOMmodem1";
    char port_amount = 0x04;
    char port_amount_str[4];
    char authorisation;
    char authorisation_str[2];
    unsigned int authorisation_len;
    char software_version[64] = "eoc-alpha0-1";
    char downstream_attenuation = 0x0A;
    char downstream_attenuation_str[4];
    char upstream_attenuation;
    char upstream_attenuation_str[4];
    unsigned int upstream_attenuation_len;
    char downstream_quality = 0x1E;
    char downstream_quality_str[4];
    char upstream_quality = 0x28;
    char upstream_quality_str[4];
    char phy_downstream_quality = 0x32;
    char phy_downstream_quality_str[4];
    char phy_upstream_quality = 0x3C;
    char phy_upstream_quality_str[4];
    char *iface = IFACE;

    assert (NULL != ctx);

    /* init mme */
    ret = mme_init (&mme_request_ctx, MME_TYPE_VS_EOC_GET_TOPO | MME_TYPE_REQ, snd_buffer, (unsigned int)ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
    {
        return ret;
    }

    ret = mme_init (&mme_confirm_ctx, MME_TYPE_VS_EOC_GET_TOPO | MME_TYPE_CNF, rcv_buffer, (unsigned int)ETH_DATA_LEN);
    if (ret != MME_SUCCESS)
    {
        return ret;
    }

    /* get the PLC MAC address */
    //ret = spidlib_network_get_mac (iface, (unsigned char *)mac_plc0_str);
    sprintf(mac_plc0_str, "%s", "aa:bb:cc:dd:ee:ff");
    if (ret != SPIDLIB_SUCCESS)
    {
        return ret;
    }

    /* ...and convert it to binary format */
    ret = spidlib_mac_str_to_bin(mac_plc0_str, mac_plc0_bin);
    if (ret != SPIDLIB_SUCCESS)
    {
        return ONLINED_SPIDLIB_BIN_STR_CONVERSION;
    }

    //mme_request_ctx.tail = 1;

    ret = mme_send (&mme_request_ctx, MME_SEND_REQ_CNF, (unsigned char *)iface, mac_plc0_bin, &mme_confirm_ctx);
    if (MME_SUCCESS != ret)
    {
        return ret;
    }

    ret = mme_pull (&mme_confirm_ctx, (void *)&result, 1, &result_len);
    if (MME_SUCCESS != ret)
    {
        return ret;
    }

    if (result != 0 )
    {
        return ONLINED_MME_WRONG_RESULT;
    }

    /* get the number of received MAC addresses */
    ret = mme_pull (&mme_confirm_ctx, (void *)&number_of_mac_addr, 1, &number_of_mac_addr_len);
    if (MME_SUCCESS != ret)
    {
        return ret;
    }

    for (i = 0; i < number_of_mac_addr; i++)
    {
        // get MAC address from MME
        ret = mme_pull (&mme_confirm_ctx, (void *)mac_addr_bin, ETH_ALEN, &mac_addr_bin_len);
        if (MME_SUCCESS != ret)
        {
            return ret;
        }

        ret = spidlib_mac_bin_to_str(mac_addr_bin, mac_addr_str);
        if (SPIDLIB_SUCCESS != ret)
        {
            return ONLINED_SPIDLIB_BIN_STR_CONVERSION;
        }

        if (strcmp(mac_addr_str, "00:00:00:00:00:00") == 0)
        {
            return ONLINED_MME_MAC_ADDR_NOT_AVAILABLE;
        }

        // get STA allowed / not allowed from MME
        ret = mme_pull (&mme_confirm_ctx, (void *)&authorisation, 1, &authorisation_len);
        if (MME_SUCCESS != ret)
        {
            return ret;
        }

        if ((authorisation != 0) && (authorisation != 1))
        {
            return ONLINED_MME_AUTHORISATION_WRONG_VALUE;
        }

        sprintf(authorisation_str, "%d", authorisation);

        // get attenuation from MME
        ret = mme_pull (&mme_confirm_ctx, (void *)&upstream_attenuation, 1, &upstream_attenuation_len);
        if (MME_SUCCESS != ret)
        {
            return ret;
        }

        sprintf(upstream_attenuation_str, "%d", upstream_attenuation);

        line[0] = model_number;

        sprintf(port_amount_str, "%d", port_amount);
        line[1] = port_amount_str;

        line[2] = authorisation_str;

        line[3] = software_version;

        sprintf(downstream_attenuation_str, "%d", downstream_attenuation);
        line[4] = downstream_attenuation_str;

        line[5] = upstream_attenuation_str;

        sprintf(downstream_quality_str, "%d", downstream_quality);
        line[6] = downstream_quality_str;

        sprintf(upstream_quality_str, "%d", upstream_quality);
        line[7] = upstream_quality_str;

        sprintf(phy_downstream_quality_str, "%d", phy_downstream_quality);
        line[8] = phy_downstream_quality_str;

        sprintf(phy_upstream_quality_str, "%d", phy_upstream_quality);
        line[9] = phy_upstream_quality_str;

        //write line to file
        ret = spidlib_config_write_line(SPIDLIB_EOC_ONLINE_INFO_PATH, delimiter, mac_addr_str, 10, line);
        if (SPIDLIB_SUCCESS != ret)
        {
            return ret;
        }
    }

    // there should be no more data left in MME. If there are, it is an error.
    ret = mme_pull (&mme_confirm_ctx, (void *)mac_addr_bin, ETH_ALEN, &mac_addr_bin_len);
    if (MME_SUCCESS != ret)
    {
        return ret;
    }

    for (j = 0; j < ETH_ALEN; j++)
    {
        if ( mac_addr_bin[j] != '\0' )
        {
            return ONLINED_MME_NOT_EMPTY;
        }
    }

    return ONLINED_SUCCESS;
}