summaryrefslogtreecommitdiff
path: root/cesar/maximus/sci/src/FunctionSciMsg.cpp
blob: f78d9839b577696c985ba64d14b3f6634d5c0fe3 (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
/* Maximus project {{{
 *
 * Copyright (C) 2012 MStar Semiconductor
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    maximus/sci/inc/FunctionSciMsg.cpp
 * \ingroup maximus_sci
 *
 */
#include "maximus/sci/inc/FunctionSciMsg.h"
#include "maximus/utils/inc/Logger.h"
#include <assert.h>
#include <netinet/in.h>

static const size_t h_fc_size = sizeof (Function_Call_Header);

FunctionSciMsg::FunctionSciMsg (struct Sci_Msg_Header &header,
                                unsigned char *p_alloc):
    SciMsg (header, p_alloc)
{
    logFunction ();

    unsigned char *p_buffer = SciMsg::get_sci_data_addr ();
    unsigned int data_length = get_sci_datalength ();

    p_fc_header = (Function_Call_Header*) p_buffer;
    p_fc_data = p_buffer + h_fc_size;

    assert (data_length >= h_fc_size);

    Function_Call_Header &h = *p_fc_header;
    h_fc_version = static_cast <uint8_t> (h.version);
    h_fc_type = static_cast <Function_Call_Type> (h.type);
    h_fc_msgid = static_cast <Function_Call_Msg_Id> (htons (h.msg_id));
    h_fc_param_nb = static_cast <Function_Call_Parameters_Number> (
        h.param_nb);
    h_fc_flags = static_cast <uint8_t> (h.flags);
    h_fc_reserved = static_cast <uint16_t> (htons (h.reserved));
}

FunctionSciMsg::FunctionSciMsg (size_t size_of_data):
    SciMsg (size_of_data + h_fc_size,
            SCI_MSG_TYPE_FUNCTION_CALL),
    h_fc_version (FUNCTION_CALL_VERSION),
    h_fc_type (FUNCTION_CALL_TYPE_REQ),
    h_fc_msgid (0),
    h_fc_param_nb (0),
    h_fc_flags (FUNCTION_CALL_FLAG_NONE),
    h_fc_reserved (0)
{
    logFunction ();

    unsigned char *p_buffer = SciMsg::get_sci_data_addr ();

    p_fc_header = (Function_Call_Header*) p_buffer;
    p_fc_data = p_buffer + h_fc_size;
}

FunctionSciMsg::~FunctionSciMsg ()
{
    logFunction ();
}

void
FunctionSciMsg::patch_to_get_data (
    unsigned char **pp_data,
    unsigned int *p_data_length,
    Function_Call_Msg_Id *p_msg_id_rx,
    Function_Call_Parameters_Number *p_param_nb) const
{
    logFunction ();

    *pp_data = p_fc_data;
    *p_data_length = get_sci_datalength () - h_fc_size;
    *p_msg_id_rx = p_fc_header->msg_id;
    *p_param_nb = p_fc_header->param_nb;
}

void
FunctionSciMsg::setup_and_send (SciServer &SciServer)
{
    logFunction ();

    Function_Call_Header &h = *p_fc_header;
    h.version = static_cast <uint8_t> (h_fc_version);
    h.type = static_cast <uint8_t> (h_fc_type);
    h.msg_id = static_cast <uint16_t> (htons (h_fc_msgid));
    h.param_nb = static_cast <uint8_t> (h_fc_param_nb);
    h.flags = static_cast <uint8_t> (h_fc_flags);
    h.reserved = static_cast <uint16_t> (htons (h_fc_reserved));

    SciMsg::setup_and_send (SciServer);
}

void
FunctionSciMsg::display_specialized_header () const
{
    static const char *type2str[FUNCTION_CALL_TYPE_NB] = {
        "TYPE_NONE",
        "TYPE_REQ",
        "TYPE_RSP"
    };

    maximus_log (LOG_INFO, "FunctionSciMsg header =");
    maximus_log (LOG_INFO, "   version = 0x"
                 << std::hex << (uint) h_fc_version);
    val2str_generic ("   type", (size_t) h_fc_type,
                     type2str, FUNCTION_CALL_TYPE_NB);
    maximus_log (LOG_INFO, "   msg id = 0x"
                 << std::hex << (uint) h_fc_msgid);
    maximus_log (LOG_INFO, "   param nb = "
                 << (uint) h_fc_param_nb << " (0x"
                 << std::hex << (uint) h_fc_param_nb << ")");
    maximus_log (LOG_INFO, "   flags = 0x"
                 << std::hex << (uint) h_fc_flags);
    maximus_log (LOG_INFO, "   reserved = 0x"
                 << std::hex << (uint) h_fc_reserved);
}