summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/vs_mme.c
blob: 91baab946f5c19f450f68783a9e702ca142fbec9 (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
/*
 * cleopatre/application/managerd/src/vs_mme.c
 *
 * (C) Copyright 2009 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdlib.h>

#include "bridge.h"
#include "vs_mme.h"

#define FIRMWARE_LABEL "irmware"

static void _prepare_confirm (struct managerd_ctx *ctx, MME_t *request, MME_t *confirm, int len)
{
    memset (confirm, '\0', len);
    memcpy (confirm->mme_dest, request->mme_src, ETH_ALEN);
    memcpy (confirm->mme_src, ctx->br_mac_addr, ETH_ALEN);
    confirm->mtype = request->mtype;
    confirm->mmv = request->mmv;
    confirm->mmtype = (request->mmtype & ~MME_TYPE_MASK) | MME_TYPE_CNF;
    confirm->fmi = 0;
}

/**
 * Send a VS_GET_VERSION MME to BR interface.
 *
 * \param  ctx  managerd context.
 * \param  request MME containg VS_GET_VERSION request.
 * \param  confirm MME buffer to put VS_GET_VERSION confirm.
 * \param  len  confirm buffer length.
 * \return  error code.
 */
enum bridge_status vs_mme_get_version (struct managerd_ctx *ctx, MME_t *request, MME_t *confirm, int len)
{
    vs_get_version_cnf_t *get_version_cnf;
    spidcom_image_desc_t desc;
    char mtd_name[32];
    FILE *fp;
    char tmp_buffer[256];
    char label[64], version[64];

    assert (NULL != ctx);
    assert (NULL != request);
    assert (NULL != confirm);
    assert (len >= sizeof(vs_get_version_cnf_t) + sizeof(MME_t));

    /* check for request content */
    if(memcmp ((char *)request + sizeof(MME_t), OUI_SPIDCOM, 3))
        return TO_DROP;

    get_version_cnf = (vs_get_version_cnf_t *)((unsigned char*)confirm + sizeof(MME_t));

    /* prepare confirm packet */
    _prepare_confirm (ctx, request, confirm, len);

    /* set get_version inside confirm buffer */
    memcpy (get_version_cnf->oui, OUI_SPIDCOM, 3);
    get_version_cnf->result = 0;
    get_version_cnf->device_id = SPC300_ID;
    // TODO: find the right index value
    get_version_cnf->current_image_index = 0;

    /* get image description */
    memset (&desc, '\0', sizeof(desc));
    if(LIBSPID_SUCCESS == libspid_image_get_desc (LIBSPID_IMAGE_DESC_TYPE_CURRENT, &desc, mtd_name))
    {
        memcpy (get_version_cnf->applicative_version, desc.version, sizeof(desc.version));
    }

    if((fp = fopen (PLC_VERSION_PATH, "r")) != NULL)
    {
        while(fgets (tmp_buffer, 256, fp))
        {
            memset (version, '\0', sizeof(version));
            if(sscanf (tmp_buffer, "%[^:] %*s %s", label, version) == 2)
            {
                if(strstr (label, FIRMWARE_LABEL))
                {
                    strncpy ((char *)get_version_cnf->av_stack_version, version, sizeof(get_version_cnf->av_stack_version));
                    break;
                }
            }
        }
        fclose (fp);
    }

    // TODO: fill alternate image info with firmware !!!

    /* send confirm MME */
    bridge_send_to_br (ctx, (uint8_t *)confirm, sizeof(MME_t) + sizeof(vs_get_version_cnf_t));
    return TO_DROP;
}

/**
 * Send a VS_ETH_STATS MME to BR interface.
 *
 * \param  ctx  managerd context.
 * \param  request MME containg VS_ETH_STATS request.
 * \param  confirm MME buffer to put VS_ETH_STATS confirm.
 * \param  len  confirm buffer length.
 * \return  error code.
 */
enum bridge_status vs_mme_eth_stats (struct managerd_ctx *ctx, MME_t *request, MME_t *confirm, int len)
{
    vs_eth_stats_req_t *eth_stats_req;
    vs_eth_stats_cnf_t *eth_stats_cnf;

    FILE *fp;
    char tmp_buffer[256];
    char name[64], *ptr;
    unsigned long long rx_bytes, rx_packets, tx_bytes, tx_packets;

    assert (NULL != ctx);
    assert (NULL != request);
    assert (NULL != confirm);
    assert (len >= sizeof(vs_eth_stats_cnf_t) + sizeof(MME_t));

    eth_stats_req = (vs_eth_stats_req_t *)((unsigned char*)request + sizeof(MME_t));
    eth_stats_cnf = (vs_eth_stats_cnf_t *)((unsigned char*)confirm + sizeof(MME_t));

    if(memcmp (eth_stats_req->oui, OUI_SPIDCOM, 3))
        return TO_DROP;

    /* prepare confirm packet */
    _prepare_confirm (ctx, request, confirm, len);
    memcpy (eth_stats_cnf->oui, OUI_SPIDCOM, 3);
    eth_stats_cnf->result = MME_RESULT_FAILURE;

    /* check request type */
    if(ETH_STATS_COMMAND_GET == eth_stats_req->command)
    {
        if((fp = fopen ("/proc/net/dev", "r")) == NULL)
            goto failed;
        /* read the 2 comment lines */
        fgets (tmp_buffer, sizeof(tmp_buffer), fp);
        fgets (tmp_buffer, sizeof(tmp_buffer), fp);
        /* read all line until found eth0 */
        while(fgets (tmp_buffer, sizeof(tmp_buffer), fp))
        {
            if((ptr = strchr (tmp_buffer, ':')) == NULL)
                continue;
            *ptr = '\0';
            ptr++;
            if((sscanf (tmp_buffer, "%s", name) == 1)
               && !strncmp (name, ETH_IFNAME, strlen(ETH_IFNAME))
               && (sscanf (ptr, "%Lu %Lu %lu %lu %lu %lu %*s %*s %Lu %Lu %lu %lu %lu %lu %lu %*s",
                           &rx_bytes,
                           &rx_packets,
                           (unsigned long int *)&eth_stats_cnf->rx_errors,
                           (unsigned long int *)&eth_stats_cnf->rx_dropped,
                           (unsigned long int *)&eth_stats_cnf->rx_overruns,
                           (unsigned long int *)&eth_stats_cnf->rx_frames,
                           &tx_bytes,
                           &tx_packets,
                           (unsigned long int *)&eth_stats_cnf->tx_errors,
                           (unsigned long int *)&eth_stats_cnf->tx_dropped,
                           (unsigned long int *)&eth_stats_cnf->tx_fifo,
                           (unsigned long int *)&eth_stats_cnf->tx_collisions,
                           (unsigned long int *)&eth_stats_cnf->tx_carrier) == 13))
            {
                eth_stats_cnf->rx_bytes_low = rx_bytes & 0xffffffff;
                eth_stats_cnf->rx_bytes_high = (rx_bytes & 0xffffffff00000000LL) >> 32;
                eth_stats_cnf->rx_packets_low = rx_packets & 0xffffffff;
                eth_stats_cnf->rx_packets_high = (rx_packets & 0xffffffff00000000LL) >> 32;
                eth_stats_cnf->tx_bytes_low = tx_bytes & 0xffffffff;
                eth_stats_cnf->tx_bytes_high = (tx_bytes & 0xffffffff00000000LL) >> 32;
                eth_stats_cnf->tx_packets_low = tx_packets & 0xffffffff;
                eth_stats_cnf->tx_packets_high = (tx_packets & 0xffffffff00000000LL) >> 32;

                //printf ( "name='%s', rx_bytes=%llu rx_errors=%lu\n", name, eth_stats_cnf->rx_bytes_low, (unsigned long int)eth_stats_cnf->rx_errors);
                eth_stats_cnf->result = MME_RESULT_SUCCESS;
                break;
            }
        }
        fclose (fp);
    }

  failed:
    bridge_send_to_br (ctx, (uint8_t *)confirm, sizeof(MME_t) + sizeof(vs_eth_stats_cnf_t));
    return TO_DROP;
}


static void* _reboot_handler (void* arg)
{
    sleep(2);
    libspid_system_reboot();
    return NULL;
}

/**
 * Send a VS_RESET MME to BR interface
 *
 * \param  ctx  managerd context
 * \param  request MME containg VS_RESET request
 * \param  confirm MME buffer to put VS_RESET confirm
 * \param  len  confirm buffer length
 * \return  error code
 */
enum bridge_status vs_mme_reset (struct managerd_ctx *ctx, MME_t *request, MME_t *confirm, int len)
{
    vs_reset_cnf_t *reset_cnf;
    pthread_attr_t attr;
    pthread_t reset_job;

    assert (NULL != ctx);
    assert (NULL != request);
    assert (NULL != confirm);
    assert (len >= sizeof(vs_reset_cnf_t) + sizeof(MME_t));

    /* check for request content */
    if (memcmp((char*) request + sizeof(MME_t), OUI_SPIDCOM, 3)) return TO_DROP;

    reset_cnf = (vs_reset_cnf_t *) ((unsigned char*) confirm + sizeof(MME_t));

    /* prepare confirmation packet */
    _prepare_confirm(ctx, request, confirm, len);
    memcpy(reset_cnf->oui, OUI_SPIDCOM, 3);

    reset_cnf->result = 0;

    /* send confirmation MME */
    bridge_send_to_br(ctx, (uint8_t*) confirm, sizeof(MME_t) + sizeof(vs_reset_cnf_t));

    /* reset the modem in a dedicated thread */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_create(&reset_job, &attr, &_reboot_handler, NULL);
    pthread_attr_destroy(&attr);

    return TO_DROP;
}

void vs_mme_reset_ind (struct managerd_ctx *ctx)
{
    MME_t *mme;
    vs_reset_ind_t *vs_reset;

    assert (NULL != ctx);

    if((mme = (MME_t *)malloc (sizeof(MME_t) + sizeof(vs_reset_ind_t))) == NULL)
        return;
    vs_reset = (vs_reset_ind_t *)(mme + 1);

    memcpy (mme->mme_dest, BROADCAST_ADDR, ETH_ALEN);
    memcpy (mme->mme_src, ctx->br_mac_addr, ETH_ALEN);
    mme->mtype = htons (ETH_P_HPAV);
    mme->mmv = MME_VERSION;
    mme->mmtype = MME_TYPE_VS_RESET | MME_TYPE_IND;
    mme->fmi = 0;
    memcpy (vs_reset->oui, OUI_SPIDCOM, 3);

    bridge_send_to_br(ctx, (uint8_t*) mme, sizeof(MME_t) + sizeof(vs_reset_ind_t));
    return;
}