summaryrefslogtreecommitdiff
path: root/implementation/protocol/src/subscribe_ack_command_base.cpp
blob: 2a8897232ee31b00b029646700c1ff3fa25bf30d (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
// 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 <limits>

#include <vsomeip/constants.hpp>

#include "../include/subscribe_ack_command_base.hpp"

namespace vsomeip_v3 {
namespace protocol {

subscribe_ack_command_base::subscribe_ack_command_base(id_e _id)
    : command(_id),
      service_(ANY_SERVICE),
      instance_(ANY_INSTANCE),
      eventgroup_(0),
      subscriber_(0),
      event_(ANY_EVENT),
      pending_id_(0) {
}

service_t
subscribe_ack_command_base::get_service() const {

    return service_;
}

void
subscribe_ack_command_base::set_service(service_t _service) {

    service_ = _service;
}

instance_t
subscribe_ack_command_base::get_instance() const {

    return instance_;
}

void
subscribe_ack_command_base::set_instance(instance_t _instance) {

    instance_ = _instance;
}

eventgroup_t
subscribe_ack_command_base::get_eventgroup() const {

    return eventgroup_;
}

void
subscribe_ack_command_base::set_eventgroup(eventgroup_t _eventgroup) {

    eventgroup_ = _eventgroup;
}

client_t
subscribe_ack_command_base::get_subscriber() const {

    return subscriber_;
}

void
subscribe_ack_command_base::set_subscriber(client_t _subscriber) {

    subscriber_ = _subscriber;
}

event_t
subscribe_ack_command_base::get_event() const {

    return event_;
}

void
subscribe_ack_command_base::set_event(event_t _event) {

    event_ = _event;
}

pending_id_t
subscribe_ack_command_base::get_pending_id() const {

    return pending_id_;
}

void
subscribe_ack_command_base::set_pending_id(pending_id_t _pending_id) {

    pending_id_ = _pending_id;
}

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

    size_t its_size(COMMAND_HEADER_SIZE
            + sizeof(service_) + sizeof(instance_)
            + sizeof(eventgroup_) + sizeof(subscriber_)
            + sizeof(event_) + sizeof(pending_id_));

    if (its_size > std::numeric_limits<command_size_t>::max()) {

        _error = error_e::ERROR_MAX_COMMAND_SIZE_EXCEEDED;
        return;
    }

    // resize buffer
    _buffer.resize(its_size);

    // set size
    size_ = static_cast<command_size_t>(its_size - COMMAND_HEADER_SIZE);

    // serialize header
    command::serialize(_buffer, _error);
    if (_error != error_e::ERROR_OK)
        return;

    // serialize payload
    size_t its_offset(COMMAND_POSITION_PAYLOAD);
    std::memcpy(&_buffer[its_offset], &service_, sizeof(service_));
    its_offset += sizeof(service_);
    std::memcpy(&_buffer[its_offset], &instance_, sizeof(instance_));
    its_offset += sizeof(instance_);
    std::memcpy(&_buffer[its_offset], &eventgroup_, sizeof(eventgroup_));
    its_offset += sizeof(instance_);
    std::memcpy(&_buffer[its_offset], &subscriber_, sizeof(subscriber_));
    its_offset += sizeof(subscriber_);
    std::memcpy(&_buffer[its_offset], &event_, sizeof(event_));
    its_offset += sizeof(event_);
    std::memcpy(&_buffer[its_offset], &pending_id_, sizeof(pending_id_));
}

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

    size_t its_size(COMMAND_HEADER_SIZE
            + sizeof(service_) + sizeof(instance_)
            + sizeof(eventgroup_) + sizeof(subscriber_)
            + sizeof(event_) + sizeof(pending_id_));

    if (its_size > _buffer.size()) {

        _error = error_e::ERROR_NOT_ENOUGH_BYTES;
        return;
    }

    // deserialize header
    command::deserialize(_buffer, _error);
    if (_error != error_e::ERROR_OK)
        return;

    // deserialize payload
    size_t its_offset(COMMAND_POSITION_PAYLOAD);
    std::memcpy(&service_, &_buffer[its_offset], sizeof(service_));
    its_offset += sizeof(service_);
    std::memcpy(&instance_, &_buffer[its_offset], sizeof(instance_));
    its_offset += sizeof(instance_);
    std::memcpy(&eventgroup_, &_buffer[its_offset], sizeof(eventgroup_));
    its_offset += sizeof(eventgroup_);
    std::memcpy(&subscriber_, &_buffer[its_offset], sizeof(subscriber_));
    its_offset += sizeof(subscriber_);
    std::memcpy(&event_, &_buffer[its_offset], sizeof(event_));
    its_offset += sizeof(event_);
    std::memcpy(&pending_id_, &_buffer[its_offset], sizeof(pending_id_));
}

} // namespace protocol
} // namespace vsomeip