summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/vs_mme.c
blob: 7a4a4886c088a1800117fd956079561116ff37fd (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
/*
 * 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 "bridge.h"
#include "vs_mme.h"

#define FIRMWARE_LABEL "irmware"

/**
 * 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_t *get_version;
    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_t) + sizeof(MME_t));

    /* prepare ethernet header */
    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 = MME_TYPE_VS_GET_VERSION | MME_TYPE_CNF;
    confirm->fmi = 0;

    /* set get_version inside confirm buffer */
    get_version = (vs_get_version_t *)((unsigned char*)confirm + sizeof(MME_t));
    /* fill get_version */
    memcpy (get_version->oui, OUI_SPIDCOM, 3);
    get_version->result = 0;
    get_version->device_id = SPC300_ID;
    // TODO: find the right index value
    get_version->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->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->av_stack_version, version, sizeof(get_version->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_t));
    return TO_DROP;
}