summaryrefslogtreecommitdiff
path: root/implementation/protocol/src/command.cpp
blob: fe338b06ab645bac2edc0d4b07236ae6f0563e48 (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
// Copyright (C) 2021 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 "../include/command.hpp"

namespace vsomeip_v3 {
namespace protocol {

command::command(id_e _id)
        : id_(_id),
          version_(MAX_SUPPORTED_VERSION),
          client_(0),
          size_(0) {
}

void
command::serialize(std::vector<byte_t> &_buffer,
        error_e &_error) const {

    // buffer space reservation is done within the code of
    // the derived classes that call this method

    _buffer[0] = static_cast<byte_t>(id_);
    std::memcpy(&_buffer[COMMAND_POSITION_VERSION], &version_,
            sizeof(version_));
    std::memcpy(&_buffer[COMMAND_POSITION_CLIENT], &client_,
            sizeof(client_));
    std::memcpy(&_buffer[COMMAND_POSITION_SIZE], &size_,
            sizeof(size_));

    _error = error_e::ERROR_OK;
}

void
command::deserialize(const std::vector<byte_t> &_buffer,
        error_e &_error) {

    // buffer size check (size >= header size) is
    // done within the code of the derived classes
    // that call this method

    // If the id_ is set to "UNKNOWN", read it.
    // Otherwise check it.
    if (id_ == id_e::UNKNOWN_ID) {

        id_ = static_cast<id_e>(_buffer[0]);
    } else if (_buffer[0] != static_cast<byte_t>(id_)) {

        _error = error_e::ERROR_MISMATCH;
        return;
    }

    std::memcpy(&version_, &_buffer[COMMAND_POSITION_VERSION],
            sizeof(version_));
    std::memcpy(&client_, &_buffer[COMMAND_POSITION_CLIENT],
            sizeof(client_));
    std::memcpy(&size_, &_buffer[COMMAND_POSITION_SIZE],
            sizeof(size_));

    _error = error_e::ERROR_OK;
}

} // namespace protocol
} // namespace vsomeip