summaryrefslogtreecommitdiff
path: root/implementation/e2e_protection/src/e2e/profile/e2e_provider_impl.cpp
blob: 111e126e6f7c35136b2722b552098439522083ac (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
// Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <iomanip>
#include <sstream>

#include <vsomeip/internal/logger.hpp>

#include "../../../../e2e_protection/include/e2e/profile/e2e_provider_impl.hpp"

#include "../../../../e2e_protection/include/e2e/profile/profile01/checker.hpp"
#include "../../../../e2e_protection/include/e2e/profile/profile01/profile_01.hpp"
#include "../../../../e2e_protection/include/e2e/profile/profile01/protector.hpp"

#include "../../../../e2e_protection/include/e2e/profile/profile04/checker.hpp"
#include "../../../../e2e_protection/include/e2e/profile/profile04/profile_04.hpp"
#include "../../../../e2e_protection/include/e2e/profile/profile04/protector.hpp"

#include "../../../../e2e_protection/include/e2e/profile/profile_custom/checker.hpp"
#include "../../../../e2e_protection/include/e2e/profile/profile_custom/profile_custom.hpp"
#include "../../../../e2e_protection/include/e2e/profile/profile_custom/protector.hpp"

namespace {

template<typename value_t>
value_t read_value_from_config(const std::shared_ptr<vsomeip_v3::cfg::e2e> &_config,
                               const std::string &_name,
                               value_t _default_value = value_t()) {

    if (_config && _config->custom_parameters.count(_name)) {

        std::stringstream its_converter;
        std::string its_value(_config->custom_parameters[_name]);

        if (its_value.size() > 1 && its_value[0] == '0' && its_value[1] == 'x') {
            its_converter << std::hex << its_value;
        } else {
            its_converter << std::dec << its_value;
        }

        value_t its_converted_value;
        its_converter >> its_converted_value;
        return its_converted_value;
    }

    return _default_value;
}

} // namespace


VSOMEIP_PLUGIN(vsomeip_v3::e2e::e2e_provider_impl)

namespace vsomeip_v3 {
namespace e2e {

e2e_provider_impl::e2e_provider_impl()
    : plugin_impl("vsomeip e2e plugin", 1, plugin_type_e::APPLICATION_PLUGIN)
{
}

e2e_provider_impl::~e2e_provider_impl()
{
}

bool e2e_provider_impl::add_configuration(std::shared_ptr<cfg::e2e> config)
{
    if (config->profile == "CRC8" || config->profile == "P01") {
        process_e2e_profile<profile01::profile_config, profile01::profile_01_checker, profile01::protector>(config);
        return true;
    }

    if (config->profile == "CRC32" || config->profile == "CSTM") {
        process_e2e_profile<profile_custom::profile_config, profile_custom::profile_custom_checker, profile_custom::protector>(config);
        return true;
    }

    if (config->profile == "P04") {
        process_e2e_profile<profile04::profile_config, profile04::profile_04_checker, profile04::protector>(config);
        return true;
    }

    return false;
}

bool e2e_provider_impl::is_protected(e2exf::data_identifier_t id) const
{
    return custom_protectors_.count(id) > 0;
}

bool e2e_provider_impl::is_checked(e2exf::data_identifier_t id) const
{
    return custom_checkers_.count(id) > 0;
}

std::size_t e2e_provider_impl::get_protection_base(e2exf::data_identifier_t id) const
{
    const auto found_base = custom_bases_.find(id);
    if (found_base != custom_bases_.end())
        return (found_base->second);

    return (0);
}

void e2e_provider_impl::protect(e2exf::data_identifier_t id, e2e_buffer &_buffer,
        instance_t _instance)
{
    auto protector = custom_protectors_.find(id);
    if(protector != custom_protectors_.end()) {
        protector->second->protect(_buffer, _instance);
    }
}

void e2e_provider_impl::check(e2exf::data_identifier_t id,
        const e2e_buffer &_buffer, instance_t _instance,
        profile_interface::check_status_t &_generic_check_status)
{
    auto checker = custom_checkers_.find(id);
    if(checker != custom_checkers_.end()) {
        checker->second->check(_buffer, _instance, _generic_check_status);
    }
}

template<>
vsomeip_v3::e2e::profile01::profile_config
e2e_provider_impl::make_e2e_profile_config(const std::shared_ptr<cfg::e2e> &_config) {
    uint16_t data_id = read_value_from_config<uint16_t>(_config, "data_id");
    uint16_t crc_offset = read_value_from_config<uint16_t>(_config, "crc_offset");
    uint16_t data_length = read_value_from_config<uint16_t>(_config, "data_length");

    // counter field behind CRC8
    uint16_t counter_offset = read_value_from_config<uint16_t>(_config, "counter_offset", 8);

    // data id nibble behind 4 bit counter value
    uint16_t data_id_nibble_offset = read_value_from_config<uint16_t>(_config, "data_id_nibble_offset", 12);

    e2e::profile01::p01_data_id_mode data_id_mode =
        static_cast<e2e::profile01::p01_data_id_mode>(
            read_value_from_config<uint16_t>(_config, "data_id_mode"));

    return e2e::profile01::profile_config(crc_offset, data_id, data_id_mode,
        data_length, counter_offset, data_id_nibble_offset);
}

template<>
vsomeip_v3::e2e::profile04::profile_config
e2e_provider_impl::make_e2e_profile_config(const std::shared_ptr<cfg::e2e> &_config) {

    uint32_t data_id = read_value_from_config<uint32_t>(_config, "data_id");

    size_t offset = read_value_from_config<size_t>(_config, "crc_offset");
    if (offset % 8)
        VSOMEIP_ERROR << "Offset in E2E P04 configuration must be multiple of 8"
            " (" << offset << ")";
    offset /= 8;

    size_t min_data_length = read_value_from_config<size_t>(_config,
            "min_data_length", 0);

    size_t max_data_length = read_value_from_config<size_t>(_config,
            "max_data_length", size_t(0xffff));

    uint16_t max_delta_counter = read_value_from_config<uint16_t>(_config,
            "max_delta_counter", uint16_t(0xffff));

    return e2e::profile04::profile_config(data_id, offset,
            min_data_length, max_data_length, max_delta_counter);
}

template<>
e2e::profile_custom::profile_config
e2e_provider_impl::make_e2e_profile_config(const std::shared_ptr<cfg::e2e>& config) {
    uint16_t crc_offset = read_value_from_config<uint16_t>(config, "crc_offset");
    return e2e::profile_custom::profile_config(crc_offset);
}

} // namespace e2e
} // namespace vsomeip_v3