summaryrefslogtreecommitdiff
path: root/cesar/maximus/sci/src/ClockSciMsg.cpp
blob: 1ecf98d37c2f9c1952d50d9468bdff0b07015666 (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/src/ClockSciMsg.cpp
 * \ingroup maximus_sci
 *
 */
#include "maximus/sci/inc/ClockSciMsg.h"
#include "maximus/sci/inc/SciServer.h"
#include "maximus/utils/inc/Logger.h"
#include <assert.h>
#include <netinet/in.h>

static const size_t h_clock_size = sizeof (Network_Clock_Header);

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

    unsigned char *p_buffer = get_sci_data_addr ();
    size_t data_length = get_sci_datalength ();

    p_clock_header = (Network_Clock_Header *) p_buffer;
    p_clock_data = p_buffer + h_clock_size;

    assert (data_length >= h_clock_size);

    Network_Clock_Header &h = *p_clock_header;
    h_clock_version = static_cast <uint8_t> (h.version);
    h_clock_type = static_cast <Network_Clock_Type> (h.type);
    h_clock_id = static_cast <Network_Clock_Id> (ntohs (h.id));
    h_clock_flags = static_cast <uint16_t> (ntohs (h.flags));
    h_clock_reserved = static_cast <uint16_t> (ntohs (h.reserved));

    uint32_t tick_high = ntohl (p_clock_header->tick_high);
    uint32_t tick_low = ntohl (p_clock_header->tick_low);

    /* Recompose tick value. */
    h_clock_tick = ((static_cast <uint64_t> (tick_high) << 32)
                    + static_cast <uint64_t> (tick_low));
}

ClockSciMsg::ClockSciMsg (size_t size_of_data):
    SciMsg (size_of_data + h_clock_size,
            SCI_MSG_TYPE_NETWORK_CLOCK),
    h_clock_version (NETWORK_CLOCK_VERSION),
    h_clock_type (NETWORK_CLOCK_TYPE_NONE),
    h_clock_id (0),
    h_clock_flags (0),
    h_clock_reserved (0),
    h_clock_tick (0)

{
    logFunction ();

    unsigned char *p_buffer = get_sci_data_addr ();
    p_clock_header = (Network_Clock_Header*) p_buffer;
    p_clock_data = p_buffer + h_clock_size;
}

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

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

    h_clock_tick = SciServer.get_current_tick ();

    uint32_t tick_low = h_clock_tick;
    uint32_t tick_high = h_clock_tick >> 32;

    Network_Clock_Header &h =  *p_clock_header;
    h.version = static_cast <uint8_t> (h_clock_version);
    h.type = static_cast <uint8_t> (h_clock_type);
    h.id = static_cast <uint16_t> (htons (h_clock_id));
    h.flags = static_cast <uint16_t> (htons (h_clock_flags));
    h.reserved = static_cast <uint16_t> (htons (h_clock_reserved));
    h.tick_high = static_cast <uint32_t> (htonl (tick_high));
    h.tick_low = static_cast <uint32_t> (htonl (tick_low));

    SciMsg::setup_and_send (SciServer);
}

void
ClockSciMsg::display_specialized_header () const
{
    static const char *type2str[NETWORK_CLOCK_TYPE_NB] = {
        "TYPE_NONE",
        "TYPE_REMOVE",
        "TYPE_STATION",
        "TYPE_FUNCTION_CALL",
        "TYPE_PHY",
        "TYPE_SYSTEM",
        "TYPE_ETHERNET"
    };

    maximus_log (LOG_INFO, "ClockSciMsg header =");
    maximus_log (LOG_INFO, "   version = 0x"
                 << std::hex << (uint) h_clock_version);
    val2str_generic ("   type", (size_t) h_clock_type,
                     type2str, NETWORK_CLOCK_TYPE_NB);
    maximus_log (LOG_INFO, "   id = 0x"
                 << std::hex << h_clock_id);
    maximus_log (LOG_INFO, "   flags = 0x"
                 << std::hex << h_clock_flags);
    maximus_log (LOG_INFO, "   reserved = 0x"
                 << std::hex << h_clock_reserved);
    maximus_log (LOG_INFO, "   tick = 0x"
                 << std::hex << h_clock_tick);
}