diff options
Diffstat (limited to 'implementation/protocol/src')
41 files changed, 3286 insertions, 0 deletions
diff --git a/implementation/protocol/src/assign_client_ack_command.cpp b/implementation/protocol/src/assign_client_ack_command.cpp new file mode 100644 index 0000000..b5d19ea --- /dev/null +++ b/implementation/protocol/src/assign_client_ack_command.cpp @@ -0,0 +1,79 @@ +// 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 "../include/assign_client_ack_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +assign_client_ack_command::assign_client_ack_command() + : command(id_e::ASSIGN_CLIENT_ACK_ID) { + +} + +void +assign_client_ack_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(assigned_)); + + 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>(sizeof(assigned_)); + + // serialize header + command::serialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // serialize payload + std::memcpy(&_buffer[COMMAND_POSITION_PAYLOAD], &assigned_, + sizeof(assigned_)); +} + +void +assign_client_ack_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(assigned_) > _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 + std::memcpy(&assigned_, &_buffer[COMMAND_POSITION_PAYLOAD], + sizeof(assigned_)); +} + +client_t +assign_client_ack_command::get_assigned() const { + + return (assigned_); +} + +void +assign_client_ack_command::set_assigned(client_t _assigned) { + + assigned_ = _assigned; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/assign_client_command.cpp b/implementation/protocol/src/assign_client_command.cpp new file mode 100644 index 0000000..245ea92 --- /dev/null +++ b/implementation/protocol/src/assign_client_command.cpp @@ -0,0 +1,83 @@ +// 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/internal/logger.hpp> +#include "../include/assign_client_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +assign_client_command::assign_client_command() + : command(id_e::ASSIGN_CLIENT_ID) { + +} + +void +assign_client_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + name_.length()); + + 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>(name_.length()); + + // serialize header + command::serialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // serialize payload + std::memcpy(&_buffer[COMMAND_POSITION_PAYLOAD], name_.data(), name_.length()); +} + +void +assign_client_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (_buffer.size() < COMMAND_HEADER_SIZE) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + command::deserialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // payload? + if (size_ == 0) + return; + + // name + name_.assign(&_buffer[COMMAND_POSITION_PAYLOAD], + &_buffer[_buffer.size()-1]); +} + +std::string +assign_client_command::get_name() const { + + return (name_); +} + +void +assign_client_command::set_name(const std::string &_name) { + + name_ = _name; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/command.cpp b/implementation/protocol/src/command.cpp new file mode 100644 index 0000000..fe338b0 --- /dev/null +++ b/implementation/protocol/src/command.cpp @@ -0,0 +1,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 diff --git a/implementation/protocol/src/deregister_application_command.cpp b/implementation/protocol/src/deregister_application_command.cpp new file mode 100644 index 0000000..5cd6f8d --- /dev/null +++ b/implementation/protocol/src/deregister_application_command.cpp @@ -0,0 +1,19 @@ +// 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 "../include/deregister_application_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +deregister_application_command::deregister_application_command() + : simple_command(id_e::DEREGISTER_APPLICATION_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/distribute_security_policies_command.cpp b/implementation/protocol/src/distribute_security_policies_command.cpp new file mode 100644 index 0000000..c93b530 --- /dev/null +++ b/implementation/protocol/src/distribute_security_policies_command.cpp @@ -0,0 +1,130 @@ +// 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/payload.hpp> + +#include "../include/distribute_security_policies_command.hpp" +#include "../../security/include/policy.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +distribute_security_policies_command::distribute_security_policies_command() + : command(id_e::DISTRIBUTE_SECURITY_POLICIES_ID) { +} + +void +distribute_security_policies_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + std::min(payload_.size(), size_t(std::numeric_limits<uint32_t>::max()))); + + 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 (add) payload + size_t its_offset(COMMAND_HEADER_SIZE); + if (payload_.empty()) { // No policy data (--> set_payloads was not called) + std::memset(&_buffer[its_offset], 0, sizeof(uint32_t)); + } else { + std::memcpy(&_buffer[its_offset], payload_.data(), payload_.size()); + } +} + +void +distribute_security_policies_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(uint32_t) > _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_HEADER_SIZE); + uint32_t its_policies_count; + std::memcpy(&its_policies_count, &_buffer[its_offset], + sizeof(its_policies_count)); + its_offset += sizeof(its_policies_count); + + for (uint32_t i = 0; i < its_policies_count; i++) { + + uint32_t its_policy_size; + std::memcpy(&its_policy_size, &_buffer[its_offset], + sizeof(its_policy_size)); + its_offset += sizeof(its_policy_size); + + const byte_t *its_policy_data = &_buffer[its_offset]; + + // set offset to the next policy + its_offset += its_policy_size; + + auto its_policy = std::make_shared<policy>(); + if (its_policy_size == 0 + || !its_policy->deserialize(its_policy_data, its_policy_size)) { + + _error = error_e::ERROR_UNKNOWN; + policies_.clear(); + return; + } + + policies_.insert(its_policy); + } +} + +std::set<std::shared_ptr<policy> > +distribute_security_policies_command::get_policies() const { + + return (policies_); +} + +void +distribute_security_policies_command::set_payloads( + const std::map<uint32_t, std::shared_ptr<payload> > &_payloads) { + + uint32_t its_count(uint32_t(_payloads.size())); + for (uint32_t i = 0; i < sizeof(its_count); ++i) { + payload_.push_back( + reinterpret_cast<const byte_t*>(&its_count)[i]); + } + + for (const auto &its_uid_gid : _payloads) { + // policy payload length including gid and uid + std::uint32_t its_length(uint32_t(its_uid_gid.second->get_length())); + for (uint32_t i = 0; i < sizeof(its_length); ++i) { + payload_.push_back( + reinterpret_cast<const byte_t*>(&its_length)[i]); + } + // payload + payload_.insert(payload_.end(), its_uid_gid.second->get_data(), + its_uid_gid.second->get_data() + its_uid_gid.second->get_length()); + } +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/dummy_command.cpp b/implementation/protocol/src/dummy_command.cpp new file mode 100644 index 0000000..0ecc18f --- /dev/null +++ b/implementation/protocol/src/dummy_command.cpp @@ -0,0 +1,40 @@ +// 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 "../include/dummy_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +dummy_command::dummy_command() + : command(id_e::UNKNOWN_ID) { + +} + +void +dummy_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + (void)_buffer; + _error = error_e::ERROR_NOT_ALLOWED; +} + +void +dummy_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (_buffer.size() < COMMAND_HEADER_SIZE) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + command::deserialize(_buffer, _error); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/expire_command.cpp b/implementation/protocol/src/expire_command.cpp new file mode 100644 index 0000000..0611901 --- /dev/null +++ b/implementation/protocol/src/expire_command.cpp @@ -0,0 +1,64 @@ +// 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/expire_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +expire_command::expire_command() + : subscribe_command_base(id_e::EXPIRE_ID) { +} + +void +expire_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + sizeof(major_) + + 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); + + // payload + subscribe_command_base::serialize(_buffer, _error); +} + +void +expire_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + sizeof(major_) + + sizeof(event_) + sizeof(pending_id_)); + + if (its_size > _buffer.size()) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + // deserialize header + subscribe_command_base::deserialize(_buffer, _error); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/multiple_services_command_base.cpp b/implementation/protocol/src/multiple_services_command_base.cpp new file mode 100644 index 0000000..47c8ab2 --- /dev/null +++ b/implementation/protocol/src/multiple_services_command_base.cpp @@ -0,0 +1,119 @@ +// 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 "../include/multiple_services_command_base.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +multiple_services_command_base::multiple_services_command_base(id_e _id) + : command(_id) { + +} + +void +multiple_services_command_base::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + (services_.size() + * (sizeof(service::service_) + sizeof(service::instance_) + + sizeof(service::major_) + sizeof(service::minor_)))); + + 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); + for (const auto &s : services_) { + std::memcpy(&_buffer[its_offset], &s.service_, sizeof(s.service_)); + its_offset += sizeof(s.service_); + std::memcpy(&_buffer[its_offset], &s.instance_, sizeof(s.instance_)); + its_offset += sizeof(s.instance_); + _buffer[its_offset] = s.major_; + its_offset += sizeof(s.major_); + std::memcpy(&_buffer[its_offset], &s.minor_, sizeof(s.minor_)); + its_offset += sizeof(s.minor_); + } +} + +void +multiple_services_command_base::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_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); + size_t its_count = (_buffer.size() - its_offset) / + (sizeof(service::service_) + sizeof(service::instance_) + + sizeof(service::major_) + sizeof(service::minor_)); + + for (size_t i = 0; i < its_count; i++) { + service its_service; + + std::memcpy(&its_service.service_, &_buffer[its_offset], + sizeof(its_service.service_)); + its_offset += sizeof(its_service.service_); + std::memcpy(&its_service.instance_, &_buffer[its_offset], + sizeof(its_service.instance_)); + its_offset += sizeof(its_service.instance_); + std::memcpy(&its_service.major_, &_buffer[its_offset], + sizeof(its_service.major_)); + its_offset += sizeof(its_service.major_); + std::memcpy(&its_service.minor_, &_buffer[its_offset], + sizeof(its_service.minor_)); + its_offset += sizeof(its_service.minor_); + + services_.insert(its_service); + } +} + +std::set<service> +multiple_services_command_base::get_services() const { + + return (services_); +} + +void +multiple_services_command_base::set_services(const std::set<service> &_services) { + + services_ = _services; +} + +void +multiple_services_command_base::add_service(const service &_service) { + + services_.insert(_service); +} + + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/offer_service_command.cpp b/implementation/protocol/src/offer_service_command.cpp new file mode 100644 index 0000000..286a6f7 --- /dev/null +++ b/implementation/protocol/src/offer_service_command.cpp @@ -0,0 +1,17 @@ +// 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/offer_service_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +offer_service_command::offer_service_command() + : service_command_base(id_e::OFFER_SERVICE_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/offered_services_request_command.cpp b/implementation/protocol/src/offered_services_request_command.cpp new file mode 100644 index 0000000..3a0aa95 --- /dev/null +++ b/implementation/protocol/src/offered_services_request_command.cpp @@ -0,0 +1,78 @@ +// 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 "../include/offered_services_request_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +offered_services_request_command::offered_services_request_command() + : command(id_e::OFFERED_SERVICES_REQUEST_ID) { +} + +void +offered_services_request_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(offer_type_)); + + 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>(sizeof(offer_type_)); + + // serialize header + command::serialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // serialize payload + std::memcpy(&_buffer[COMMAND_POSITION_PAYLOAD], &offer_type_, + sizeof(offer_type_)); +} + +void +offered_services_request_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(offer_type_) > _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 + std::memcpy(&offer_type_, &_buffer[COMMAND_POSITION_PAYLOAD], + sizeof(offer_type_)); +} + +offer_type_e +offered_services_request_command::get_offer_type() const { + + return (offer_type_); +} + +void +offered_services_request_command::set_offer_type(offer_type_e _offer_type) { + + offer_type_ = _offer_type; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/offered_services_response_command.cpp b/implementation/protocol/src/offered_services_response_command.cpp new file mode 100644 index 0000000..bc8df95 --- /dev/null +++ b/implementation/protocol/src/offered_services_response_command.cpp @@ -0,0 +1,17 @@ +// 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/offered_services_response_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +offered_services_response_command::offered_services_response_command() + : multiple_services_command_base(id_e::OFFERED_SERVICES_RESPONSE_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/ping_command.cpp b/implementation/protocol/src/ping_command.cpp new file mode 100644 index 0000000..fab227b --- /dev/null +++ b/implementation/protocol/src/ping_command.cpp @@ -0,0 +1,17 @@ +// 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/ping_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +ping_command::ping_command() + : simple_command(id_e::PING_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/pong_command.cpp b/implementation/protocol/src/pong_command.cpp new file mode 100644 index 0000000..8be1e90 --- /dev/null +++ b/implementation/protocol/src/pong_command.cpp @@ -0,0 +1,17 @@ +// 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/pong_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +pong_command::pong_command() + : simple_command(id_e::PONG_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/register_application_command.cpp b/implementation/protocol/src/register_application_command.cpp new file mode 100644 index 0000000..d8babc7 --- /dev/null +++ b/implementation/protocol/src/register_application_command.cpp @@ -0,0 +1,81 @@ +// 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 "../include/register_application_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +register_application_command::register_application_command() + : command(id_e::REGISTER_APPLICATION_ID), + port_(ILLEGAL_PORT) { + +} + +void +register_application_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(port_)); + + 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>(sizeof(port_)); + + // serialize header + command::serialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // serialize payload + std::memcpy(&_buffer[COMMAND_POSITION_PAYLOAD], &port_, + sizeof(port_)); + +} + +void +register_application_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(port_) > _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 + std::memcpy(&port_, &_buffer[COMMAND_POSITION_PAYLOAD], + sizeof(port_)); +} + +port_t +register_application_command::get_port() const { + + return (port_); +} + +void +register_application_command::set_port(port_t _port) { + + port_ = _port; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/register_event.cpp b/implementation/protocol/src/register_event.cpp new file mode 100644 index 0000000..5be91b6 --- /dev/null +++ b/implementation/protocol/src/register_event.cpp @@ -0,0 +1,82 @@ +#include "../include/register_event.hpp" +#include <vsomeip/internal/logger.hpp> + +namespace vsomeip_v3 { +namespace protocol { + +register_event::register_event(service_t service, instance_t instance, + event_t event, event_type_e event_type, + bool is_provided, reliability_type_e reliability, + bool is_cyclic, uint16_t num_eventg, + const std::set<eventgroup_t> &eventgroups): + service_(service), instance_(instance), event_(event), + event_type_(event_type), is_provided_(is_provided), + reliability_(reliability), is_cyclic_(is_cyclic), + num_eventg_(num_eventg), eventgroups_(eventgroups) { +} + +void +register_event::serialize(std::vector<byte_t> &_buffer, size_t &_offset) const { + + std::memcpy(&_buffer[_offset], &service_, sizeof(service_)); + _offset += sizeof(service_); + std::memcpy(&_buffer[_offset], &instance_, sizeof(instance_)); + _offset += sizeof(instance_); + std::memcpy(&_buffer[_offset], &event_, sizeof(event_)); + _offset += sizeof(event_); + _buffer[_offset] = static_cast<byte_t>(event_type_); + _offset += sizeof(event_type_); + _buffer[_offset] = static_cast<byte_t>(is_provided_); + _offset += sizeof(is_provided_); + _buffer[_offset] = static_cast<byte_t>(reliability_); + _offset += sizeof(reliability_); + _buffer[_offset] = static_cast<byte_t>(is_cyclic_); + _offset += sizeof(is_cyclic_); + std::memcpy(&_buffer[_offset], &num_eventg_, sizeof(num_eventg_)); + _offset += sizeof(num_eventg_); + + for (const auto g : eventgroups_) { + std::memcpy(&_buffer[_offset], &g, sizeof(g)); + _offset += sizeof(g); + } +} + +void +register_event::deserialize(const std::vector<byte_t> &_buffer, size_t &_offset) { + + std::memcpy(&service_, &_buffer[_offset], sizeof(service_)); + _offset += sizeof(service_); + std::memcpy(&instance_, &_buffer[_offset], sizeof(instance_)); + _offset += sizeof(instance_); + std::memcpy(&event_, &_buffer[_offset], sizeof(event_)); + _offset += sizeof(event_); + event_type_ = static_cast<event_type_e>(_buffer[_offset]); + _offset += sizeof(event_type_); + is_provided_ = static_cast<bool>(_buffer[_offset]); + _offset += sizeof(is_provided_); + reliability_ = static_cast<reliability_type_e>(_buffer[_offset]); + _offset += sizeof(reliability_); + is_cyclic_ = static_cast<bool>(_buffer[_offset]); + _offset += sizeof(is_cyclic_); + std::memcpy(&num_eventg_, &_buffer[_offset], sizeof(num_eventg_)); + _offset += sizeof(num_eventg_); + + eventgroups_.clear(); + for (size_t i = 0; i < num_eventg_; i++) { + eventgroup_t its_g; + std::memcpy(&its_g, &_buffer[_offset], sizeof(its_g)); + _offset += sizeof(its_g); + + eventgroups_.insert(its_g); + } +} + +void +register_event::set_eventgroups(const std::set<eventgroup_t> &_eventgroups) { + + eventgroups_ = _eventgroups; + num_eventg_ = (uint16_t)eventgroups_.size(); +} + +} // namespace protocol +} // namespace vsomeip_v3 diff --git a/implementation/protocol/src/register_events_command.cpp b/implementation/protocol/src/register_events_command.cpp new file mode 100644 index 0000000..830e223 --- /dev/null +++ b/implementation/protocol/src/register_events_command.cpp @@ -0,0 +1,110 @@ +// 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 "../include/register_events_command.hpp" +#include <vsomeip/internal/logger.hpp> + +namespace vsomeip_v3 { +namespace protocol { + +register_events_command::register_events_command() + : command(id_e::REGISTER_EVENT_ID) { +} + +bool +register_events_command::add_registration(const register_event &_register_event) { + + size_t its_size(size_ + COMMAND_HEADER_SIZE + + sizeof(_register_event.get_service()) + sizeof(_register_event.get_instance()) + + sizeof(_register_event.get_event()) + sizeof(_register_event.get_event_type()) + + sizeof(_register_event.is_provided()) + sizeof(_register_event.get_reliability()) + + sizeof(_register_event.is_cyclic()) + sizeof(_register_event.get_num_eventgroups()) + + (_register_event.get_num_eventgroups() * sizeof(eventgroup_t) )); + if (its_size > std::numeric_limits<command_size_t>::max()) { + return false; + } else { + // set size + size_ = static_cast<command_size_t>(its_size - COMMAND_HEADER_SIZE); + registrations_.push_back(_register_event); + } + return true; +} + +void +register_events_command::serialize(std::vector<byte_t> &_buffer, error_e &_error) const { + + if (size_ + COMMAND_HEADER_SIZE> std::numeric_limits<command_size_t>::max()) { + + _error = error_e::ERROR_MAX_COMMAND_SIZE_EXCEEDED; + return; + } + + // resize buffer + _buffer.resize(size_+COMMAND_HEADER_SIZE); + + // serialize header + command::serialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // serialize payload + size_t its_offset(COMMAND_HEADER_SIZE); + for(auto ® : registrations_) { + reg.serialize(_buffer, its_offset); + } +} + +void +register_events_command::deserialize(const std::vector<byte_t> &_buffer, error_e &_error) { + registrations_.clear(); + + if(_buffer.size() < COMMAND_HEADER_SIZE) { + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + // deserialize header + command::deserialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + size_t its_offset(COMMAND_HEADER_SIZE); + + while(its_offset < _buffer.size()) { + size_t its_size(its_offset+ sizeof(service_t) + sizeof(instance_t) + + sizeof(event_t) + sizeof(event_type_e) + + sizeof(bool) + sizeof(bool) + sizeof(bool) + sizeof(uint16_t) + + sizeof(eventgroup_t)); // at least one is needed + if (its_size > _buffer.size()) { + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + register_event event_command; + event_command.deserialize(_buffer, its_offset); + registrations_.push_back(event_command); + } +} + +std::size_t +register_events_command::get_num_registrations() const { + + return registrations_.size(); +} + +bool +register_events_command::get_registration_at(std::size_t _position, register_event & _reg) const { + + if(_position < registrations_.size()) { + _reg = registrations_[_position]; + return true; + } + return false; +} + +} // namespace protocol +} // namespace vsomeip_v3 diff --git a/implementation/protocol/src/registered_ack_command.cpp b/implementation/protocol/src/registered_ack_command.cpp new file mode 100644 index 0000000..a9df5d3 --- /dev/null +++ b/implementation/protocol/src/registered_ack_command.cpp @@ -0,0 +1,17 @@ +// 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/registered_ack_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +registered_ack_command::registered_ack_command() + : simple_command(id_e::REGISTERED_ACK_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/release_service_command.cpp b/implementation/protocol/src/release_service_command.cpp new file mode 100644 index 0000000..41475ec --- /dev/null +++ b/implementation/protocol/src/release_service_command.cpp @@ -0,0 +1,101 @@ +// 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 "../include/release_service_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +release_service_command::release_service_command() + : command(id_e::RELEASE_SERVICE_ID) { + +} + +void +release_service_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service::service_) + sizeof(service::instance_)); + + 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_HEADER_SIZE); + std::memcpy(&_buffer[its_offset], &service_.service_, sizeof(service_.service_)); + its_offset += sizeof(service_.service_); + std::memcpy(&_buffer[its_offset], &service_.instance_, sizeof(service_.instance_)); +} + +void +release_service_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service::service_) + sizeof(service::instance_)); + + 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_.service_, &_buffer[its_offset], + sizeof(service_.service_)); + its_offset += sizeof(service_.service_); + std::memcpy(&service_.instance_, &_buffer[its_offset], + sizeof(service_.instance_)); +} + +service_t +release_service_command::get_service() const { + + return (service_.service_); +} + +void +release_service_command::set_service(service_t _service) { + + service_.service_ = _service; +} + +instance_t +release_service_command::get_instance() const { + + return (service_.instance_); +} + +void +release_service_command::set_instance(instance_t _instance) { + + service_.instance_ = _instance; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/remove_security_policy_command.cpp b/implementation/protocol/src/remove_security_policy_command.cpp new file mode 100644 index 0000000..acf8aed --- /dev/null +++ b/implementation/protocol/src/remove_security_policy_command.cpp @@ -0,0 +1,114 @@ +// 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 "../include/remove_security_policy_command.hpp" +#include "../../security/include/policy.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +remove_security_policy_command::remove_security_policy_command() + : command(id_e::REMOVE_SECURITY_POLICY_ID) { +} + +void +remove_security_policy_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(update_id_) + + sizeof(uid_) + sizeof(gid_)); + + 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_HEADER_SIZE); + std::memcpy(&_buffer[its_offset], &update_id_, sizeof(update_id_)); + its_offset += sizeof(update_id_); + std::memcpy(&_buffer[its_offset], &uid_, sizeof(uid_)); + its_offset += sizeof(uid_); + std::memcpy(&_buffer[its_offset], &gid_, sizeof(gid_)); +} + +void +remove_security_policy_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(update_id_) + + sizeof(uid_) + sizeof(gid_t) > _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_HEADER_SIZE); + std::memcpy(&update_id_, &_buffer[its_offset], sizeof(update_id_)); + its_offset += sizeof(update_id_); + std::memcpy(&uid_, &_buffer[its_offset], sizeof(uid_)); + its_offset += sizeof(uid_); + std::memcpy(&gid_, &_buffer[its_offset], sizeof(gid_)); +} + +uint32_t +remove_security_policy_command::get_update_id() const { + + return (update_id_); +} + +void +remove_security_policy_command::set_update_id(uint32_t _update_id) { + + update_id_ = _update_id; +} + + +uid_t +remove_security_policy_command::get_uid() const { + + return (uid_); +} + +void +remove_security_policy_command::set_uid(uid_t _uid) { + + uid_ = _uid; +} + +gid_t +remove_security_policy_command::get_gid() const { + + return (gid_); +} + +void +remove_security_policy_command::set_gid(gid_t _gid) { + + gid_ = _gid; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/remove_security_policy_response_command.cpp b/implementation/protocol/src/remove_security_policy_response_command.cpp new file mode 100644 index 0000000..ce8ab0b --- /dev/null +++ b/implementation/protocol/src/remove_security_policy_response_command.cpp @@ -0,0 +1,18 @@ +// 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/remove_security_policy_response_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +remove_security_policy_response_command::remove_security_policy_response_command() + : security_policy_response_command_base( + id_e::REMOVE_SECURITY_POLICY_RESPONSE_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/request_service_command.cpp b/implementation/protocol/src/request_service_command.cpp new file mode 100644 index 0000000..c98f8eb --- /dev/null +++ b/implementation/protocol/src/request_service_command.cpp @@ -0,0 +1,17 @@ +// 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/request_service_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +request_service_command::request_service_command() + : multiple_services_command_base(id_e::REQUEST_SERVICE_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/resend_provided_events_command.cpp b/implementation/protocol/src/resend_provided_events_command.cpp new file mode 100644 index 0000000..929a434 --- /dev/null +++ b/implementation/protocol/src/resend_provided_events_command.cpp @@ -0,0 +1,80 @@ +// 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 "../include/resend_provided_events_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +resend_provided_events_command::resend_provided_events_command() + : command(id_e::RESEND_PROVIDED_EVENTS_ID) { + +} + +void +resend_provided_events_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(remote_offer_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>(sizeof(remote_offer_id_)); + + // serialize header + command::serialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // serialize payload + std::memcpy(&_buffer[COMMAND_POSITION_PAYLOAD], &remote_offer_id_, + sizeof(remote_offer_id_)); +} + +void +resend_provided_events_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(remote_offer_id_) > _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 + std::memcpy(&remote_offer_id_, &_buffer[COMMAND_POSITION_PAYLOAD], + sizeof(remote_offer_id_)); +} + +pending_remote_offer_id_t +resend_provided_events_command::get_remote_offer_id() const { + + return (remote_offer_id_); +} + +void +resend_provided_events_command::set_remote_offer_id( + pending_remote_offer_id_t _remote_offer_id) { + + remote_offer_id_ = _remote_offer_id; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/routing_info_command.cpp b/implementation/protocol/src/routing_info_command.cpp new file mode 100644 index 0000000..ae2747f --- /dev/null +++ b/implementation/protocol/src/routing_info_command.cpp @@ -0,0 +1,98 @@ +// 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 "../include/routing_info_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +routing_info_command::routing_info_command() + : command(id_e::ROUTING_INFO_ID) { + +} + +void +routing_info_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE); + for (const auto &e : entries_) + its_size += e.get_size(); + + // 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 _index(COMMAND_HEADER_SIZE); + for (const auto &e : entries_) { + e.serialize(_buffer, _index, _error); + if (_error != error_e::ERROR_OK) { + _buffer.clear(); + return; + } + } +} + +void +routing_info_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_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_index(COMMAND_HEADER_SIZE); + while (its_index < _buffer.size()) { + + routing_info_entry its_entry; + its_entry.deserialize(_buffer, its_index, _error); + + if (_error == error_e::ERROR_OK) + entries_.emplace_back(its_entry); + else + break; + } +} + +// specific +const std::vector<routing_info_entry> & +routing_info_command::get_entries() const { + + return (entries_); +} + +void +routing_info_command::set_entries(std::vector<routing_info_entry> &&_entries) { + + entries_ = std::move(_entries); +} + +void +routing_info_command::add_entry(const routing_info_entry &_entry) { + + entries_.push_back(_entry); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/routing_info_entry.cpp b/implementation/protocol/src/routing_info_entry.cpp new file mode 100644 index 0000000..1edb23b --- /dev/null +++ b/implementation/protocol/src/routing_info_entry.cpp @@ -0,0 +1,304 @@ +// 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 <cstring> +#include <limits> + +#include <boost/asio/ip/address_v4.hpp> +#include <boost/asio/ip/address_v6.hpp> + +#include "../include/routing_info_entry.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +routing_info_entry::routing_info_entry() + : type_(routing_info_entry_type_e::RIE_UNKNOWN), + port_(0) { + +} + +routing_info_entry::routing_info_entry(const routing_info_entry &_source) + : type_(_source.type_), + client_(_source.client_), + address_(_source.address_), + port_(_source.port_), + services_(_source.services_) { + +} + +void +routing_info_entry::serialize(std::vector<byte_t> &_buffer, + size_t &_index, error_e &_error) const { + + _buffer[_index] = static_cast<byte_t>(type_); + _index += sizeof(type_); + + // Size is overall size - size field - command type + size_t its_size = get_size() - sizeof(uint32_t) - 1; + if (its_size > std::numeric_limits<uint32_t>::max()) { + + _error = error_e::ERROR_MALFORMED; + return; + } + + uint32_t its_size32(static_cast<uint32_t>(its_size)); + std::memcpy(&_buffer[_index], &its_size32, sizeof(its_size32)); + _index += sizeof(its_size32); + + uint32_t its_client_size(sizeof(client_)); + if (!address_.is_unspecified()) { + if (address_.is_v4()) { + its_client_size += uint32_t(sizeof(boost::asio::ip::address_v4::bytes_type) + + sizeof(port_)); + } else { + its_client_size += uint32_t(sizeof(boost::asio::ip::address_v6::bytes_type) + + sizeof(port_)); + } + } + + if (type_ > routing_info_entry_type_e::RIE_DELETE_CLIENT) { + + std::memcpy(&_buffer[_index], &its_client_size, sizeof(its_client_size)); + _index += sizeof(its_client_size); + } + + std::memcpy(&_buffer[_index], &client_, sizeof(client_)); + _index += sizeof(client_); + + if (!address_.is_unspecified()) { + + if (address_.is_v4()) { + std::memcpy(&_buffer[_index], address_.to_v4().to_bytes().data(), + sizeof(boost::asio::ip::address_v4::bytes_type)); + _index += sizeof(boost::asio::ip::address_v4::bytes_type); + } else { + std::memcpy(&_buffer[_index], address_.to_v6().to_bytes().data(), + sizeof(boost::asio::ip::address_v6::bytes_type)); + _index += sizeof(boost::asio::ip::address_v6::bytes_type); + } + std::memcpy(&_buffer[_index], &port_, sizeof(port_)); + _index += sizeof(port_); + } + + if (type_ > routing_info_entry_type_e::RIE_DELETE_CLIENT) { + + its_size = (services_.size() * + (sizeof(service_t) + sizeof(instance_t) + + sizeof(major_version_t) + sizeof(minor_version_t))); + + if (its_size > std::numeric_limits<uint32_t>::max()) { + + _error = error_e::ERROR_MALFORMED; + return; + } + + its_size32 = static_cast<uint32_t>(its_size); + std::memcpy(&_buffer[_index], &its_size32, sizeof(its_size32)); + _index += sizeof(its_size32); + + for (const auto &s : services_) { + + std::memcpy(&_buffer[_index], &s.service_, sizeof(s.service_)); + _index += sizeof(s.service_); + std::memcpy(&_buffer[_index], &s.instance_, sizeof(s.instance_)); + _index += sizeof(s.instance_); + std::memcpy(&_buffer[_index], &s.major_, sizeof(s.major_)); + _index += sizeof(s.major_); + std::memcpy(&_buffer[_index], &s.minor_, sizeof(s.minor_)); + _index += sizeof(s.minor_); + } + } +} + +void +routing_info_entry::deserialize(const std::vector<byte_t> &_buffer, + size_t &_index, error_e &_error) { + + uint32_t its_size; + uint32_t its_client_size; + + if (_buffer.size() < _index + sizeof(type_) + sizeof(its_size) + + sizeof(client_)) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + type_ = static_cast<routing_info_entry_type_e>(_buffer[_index++]); + if (type_ == routing_info_entry_type_e::RIE_UNKNOWN) { + + _error = error_e::ERROR_MALFORMED; + return; + } + + std::memcpy(&its_size, &_buffer[_index], sizeof(its_size)); + _index += sizeof(its_size); + + if (type_ > routing_info_entry_type_e::RIE_DELETE_CLIENT) { + std::memcpy(&its_client_size, &_buffer[_index], sizeof(its_client_size)); + _index += sizeof(its_client_size); + } else { + its_client_size = its_size; + } + + std::memcpy(&client_, &_buffer[_index], sizeof(client_)); + _index += sizeof(client_); + + if (its_client_size > sizeof(client_)) { + + uint32_t its_address_size = its_client_size + - uint32_t(sizeof(client_t) + sizeof(port_)); + + if (its_address_size == sizeof(boost::asio::ip::address_v4::bytes_type)) { + + boost::asio::ip::address_v4::bytes_type its_array; + std::memcpy(&its_array, &_buffer[_index], its_array.size()); + address_ = boost::asio::ip::address_v4(its_array); + _index += its_array.size(); + + } else if (its_address_size == sizeof(boost::asio::ip::address_v6::bytes_type)) { + + boost::asio::ip::address_v6::bytes_type its_array; + std::memcpy(&its_array, &_buffer[_index], its_array.size()); + address_ = boost::asio::ip::address_v6(its_array); + _index += its_array.size(); + + } else { + + _error = error_e::ERROR_MALFORMED; + return; + } + + std::memcpy(&port_, &_buffer[_index], sizeof(port_)); + _index += sizeof(port_); + } + + if (type_ > routing_info_entry_type_e::RIE_DELETE_CLIENT) { + + if (_buffer.size() < _index + sizeof(its_size)) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + std::memcpy(&its_size, &_buffer[_index], sizeof(its_size)); + _index += sizeof(its_size); + + if (_buffer.size() < _index + its_size) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + size_t its_n = (its_size / + (sizeof(service_t) + sizeof(instance_t) + + sizeof(major_version_t) + sizeof(minor_version_t))); + + for (size_t i = 0; i < its_n; i++) { + + service its_service; + std::memcpy(&its_service.service_, &_buffer[_index], sizeof(its_service.service_)); + _index += sizeof(its_service.service_); + std::memcpy(&its_service.instance_, &_buffer[_index], sizeof(its_service.instance_)); + _index += sizeof(its_service.instance_); + its_service.major_ = static_cast<major_version_t>(_buffer[_index]); + _index += sizeof(its_service.major_); + std::memcpy(&its_service.minor_, &_buffer[_index], sizeof(its_service.minor_)); + _index += sizeof(its_service.minor_); + + services_.emplace_back(its_service); + } + } +} + +routing_info_entry_type_e +routing_info_entry::get_type() const { + + return (type_); +} + +void +routing_info_entry::set_type(routing_info_entry_type_e _type) { + + type_ = _type; +} + +size_t +routing_info_entry::get_size() const { + + size_t its_size(ROUTING_INFO_ENTRY_HEADER_SIZE); + + if (!address_.is_unspecified()) { + if (address_.is_v4()) { + its_size += (sizeof(boost::asio::ip::address_v4::bytes_type) + + sizeof(port_)); + } else { + its_size += (sizeof(boost::asio::ip::address_v6::bytes_type) + + sizeof(port_)); + } + } + + if (type_ > routing_info_entry_type_e::RIE_DELETE_CLIENT) { + its_size += sizeof(uint32_t); // size of the client info + its_size += sizeof(uint32_t); // size of the services array + its_size += (services_.size() * + (sizeof(service_t) + sizeof(instance_t) + + sizeof(major_version_t) + sizeof(minor_version_t))); + } + + return (its_size); +} + +client_t +routing_info_entry::get_client() const { + + return (client_); +} + +void +routing_info_entry::set_client(client_t _client) { + + client_ = _client; +} + +boost::asio::ip::address +routing_info_entry::get_address() const { + + return (address_); +} + +void +routing_info_entry::set_address(const boost::asio::ip::address &_address) { + + address_ = _address; +} + +port_t +routing_info_entry::get_port() const { + + return (port_); +} + +void +routing_info_entry::set_port(port_t _port) { + + port_ = _port; +} + +const std::vector<service> & +routing_info_entry::get_services() const { + + return (services_); +} + +void +routing_info_entry::add_service(const service &_service) { + + services_.push_back(_service); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/security_policy_response_command_base.cpp b/implementation/protocol/src/security_policy_response_command_base.cpp new file mode 100644 index 0000000..2a4837a --- /dev/null +++ b/implementation/protocol/src/security_policy_response_command_base.cpp @@ -0,0 +1,80 @@ +// 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 "../include/security_policy_response_command_base.hpp" +#include "../../security/include/policy.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +security_policy_response_command_base::security_policy_response_command_base( + id_e _id) + : command(_id) { +} + +void +security_policy_response_command_base::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(update_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_HEADER_SIZE); + std::memcpy(&_buffer[its_offset], &update_id_, sizeof(update_id_)); +} + +void +security_policy_response_command_base::deserialize( + const std::vector<byte_t> &_buffer, error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(update_id_) > _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 + std::memcpy(&update_id_, &_buffer[COMMAND_POSITION_PAYLOAD], + sizeof(update_id_)); +} + +uint32_t +security_policy_response_command_base::get_update_id() const { + + return (update_id_); +} + +void +security_policy_response_command_base::set_update_id(uint32_t _update_id) { + + update_id_ = _update_id; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/send_command.cpp b/implementation/protocol/src/send_command.cpp new file mode 100644 index 0000000..7959744 --- /dev/null +++ b/implementation/protocol/src/send_command.cpp @@ -0,0 +1,149 @@ +// 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 "../include/send_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +send_command::send_command(id_e _id) + : command(_id) { +} + +instance_t +send_command::get_instance() const { + + return instance_; +} + +void +send_command::set_instance(instance_t _instance) { + + instance_ = _instance; +} + +bool +send_command::is_reliable() const { + + return is_reliable_; +} + +void +send_command::set_reliable(bool _is_reliable) { + + is_reliable_ = _is_reliable; +} + +uint8_t +send_command::get_status() const { + + return status_; +} + +void +send_command::set_status(uint8_t _status) { + + status_ = _status; +} + +client_t +send_command::get_target() const { + + return target_; +} + +void +send_command::set_target(client_t _target) { + + target_ = _target; +} + +std::vector<byte_t> +send_command::get_message() const { + + return message_; +} + +void +send_command::set_message(const std::vector<byte_t> &_message) { + + message_ = std::move(_message); +} + +void +send_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(instance_) + + sizeof(is_reliable_) + sizeof(status_) + + sizeof(target_) + message_.size()); + + 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], &instance_, sizeof(instance_)); + its_offset += sizeof(instance_); + _buffer[its_offset] = static_cast<byte_t>(is_reliable_); + its_offset += sizeof(is_reliable_); + _buffer[its_offset] = static_cast<byte_t>(status_); + its_offset += sizeof(status_); + std::memcpy(&_buffer[its_offset], &target_, sizeof(target_)); + its_offset += sizeof(target_); + std::memcpy(&_buffer[its_offset], &message_[0], message_.size()); +} + +void +send_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(instance_) + + sizeof(is_reliable_) + sizeof(status_) + + sizeof(target_)); + + 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(&instance_, &_buffer[its_offset], sizeof(instance_)); + its_offset += sizeof(instance_); + is_reliable_ = static_cast<bool>(_buffer[its_offset]); + its_offset += sizeof(is_reliable_); + status_ = static_cast<uint8_t>(_buffer[its_offset]); + its_offset += sizeof(status_); + std::memcpy(&target_, &_buffer[its_offset], sizeof(target_)); + its_offset += sizeof(target_); + message_.resize(_buffer.size() - its_offset); + std::memcpy(&message_[0], &_buffer[its_offset], message_.size()); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/service_command_base.cpp b/implementation/protocol/src/service_command_base.cpp new file mode 100644 index 0000000..ae270f2 --- /dev/null +++ b/implementation/protocol/src/service_command_base.cpp @@ -0,0 +1,130 @@ +// 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 "../include/service_command_base.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +service_command_base::service_command_base(id_e _id) + : command(_id) { +} + +service_t +service_command_base::get_service() const { + + return service_.service_; +} + +void +service_command_base::set_service(service_t _service) { + + service_.service_ = _service; +} + +instance_t +service_command_base::get_instance() const { + + return service_.instance_; +} + +void +service_command_base::set_instance(instance_t _instance) { + + service_.instance_ = _instance; +} + +major_version_t +service_command_base::get_major() const { + + return service_.major_; +} + +void +service_command_base::set_major(major_version_t _major) { + + service_.major_ = _major; +} + +minor_version_t +service_command_base::get_minor() const { + + return service_.minor_; +} + +void +service_command_base::set_minor(minor_version_t _minor) { + + service_.minor_ = _minor; +} + +void +service_command_base::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_.service_) + sizeof(service_.instance_) + + sizeof(service_.major_) + sizeof(service_.minor_)); + + 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_.service_, sizeof(service_.service_)); + its_offset += sizeof(service_.service_); + std::memcpy(&_buffer[its_offset], &service_.instance_, sizeof(service_.instance_)); + its_offset += sizeof(service_.instance_); + _buffer[its_offset] = service_.major_; + its_offset += sizeof(service_.major_); + std::memcpy(&_buffer[its_offset], &service_.minor_, sizeof(service_.minor_)); +} + +void +service_command_base::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_.service_) + sizeof(service_.instance_) + + sizeof(service_.major_) + sizeof(service_.minor_)); + + if (its_size > _buffer.size()) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + // deserialize header + command::deserialize(_buffer, _error); + + // deserialize payload + size_t its_offset(COMMAND_POSITION_PAYLOAD); + std::memcpy(&service_.service_, &_buffer[its_offset], sizeof(service_.service_)); + its_offset += sizeof(service_.service_); + std::memcpy(&service_.instance_, &_buffer[its_offset], sizeof(service_.instance_)); + its_offset += sizeof(service_.instance_); + service_.major_ = _buffer[its_offset]; + its_offset += sizeof(service_.major_); + std::memcpy(&service_.minor_, &_buffer[its_offset], sizeof(service_.minor_)); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/simple_command.cpp b/implementation/protocol/src/simple_command.cpp new file mode 100644 index 0000000..9076012 --- /dev/null +++ b/implementation/protocol/src/simple_command.cpp @@ -0,0 +1,48 @@ +// 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 "../include/simple_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +simple_command::simple_command(id_e _id) + : command(_id) { + +} + +void +simple_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + // no size check as we know this is small enough + + // resize buffer + _buffer.resize(COMMAND_HEADER_SIZE); + + // set size + size_ = 0; + + // serialize header + command::serialize(_buffer, _error); +} + +void +simple_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (_buffer.size() < COMMAND_HEADER_SIZE) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + command::deserialize(_buffer, _error); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/stop_offer_service_command.cpp b/implementation/protocol/src/stop_offer_service_command.cpp new file mode 100644 index 0000000..c12a20d --- /dev/null +++ b/implementation/protocol/src/stop_offer_service_command.cpp @@ -0,0 +1,17 @@ +// 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/stop_offer_service_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +stop_offer_service_command::stop_offer_service_command() + : service_command_base(id_e::STOP_OFFER_SERVICE_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/subscribe_ack_command.cpp b/implementation/protocol/src/subscribe_ack_command.cpp new file mode 100644 index 0000000..12fe1cd --- /dev/null +++ b/implementation/protocol/src/subscribe_ack_command.cpp @@ -0,0 +1,17 @@ +// 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/subscribe_ack_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +subscribe_ack_command::subscribe_ack_command() + : subscribe_ack_command_base(id_e::SUBSCRIBE_ACK_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/subscribe_ack_command_base.cpp b/implementation/protocol/src/subscribe_ack_command_base.cpp new file mode 100644 index 0000000..2a88972 --- /dev/null +++ b/implementation/protocol/src/subscribe_ack_command_base.cpp @@ -0,0 +1,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 diff --git a/implementation/protocol/src/subscribe_command.cpp b/implementation/protocol/src/subscribe_command.cpp new file mode 100644 index 0000000..89f19af --- /dev/null +++ b/implementation/protocol/src/subscribe_command.cpp @@ -0,0 +1,144 @@ +// 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_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +subscribe_command::subscribe_command() + : subscribe_command_base(id_e::SUBSCRIBE_ID) { +} + +std::shared_ptr<debounce_filter_t> +subscribe_command::get_filter() const { + + return filter_; +} + +void +subscribe_command::set_filter( + const std::shared_ptr<debounce_filter_t> &_filter) { + + filter_ = _filter; +} + +void +subscribe_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + sizeof(major_) + + sizeof(event_) + sizeof(pending_id_)); + size_t its_offset(its_size); + + if (filter_) { + its_size += sizeof(filter_->on_change_) + + sizeof(filter_->on_change_resets_interval_) + + sizeof(filter_->interval_) + + (filter_->ignore_.size() * (sizeof(size_t) + sizeof(byte_t))); + } + + 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 + subscribe_command_base::serialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + if (filter_) { + + _buffer[its_offset] = static_cast<byte_t>(filter_->on_change_); + its_offset += sizeof(filter_->on_change_); + _buffer[its_offset] = static_cast<byte_t>(filter_->on_change_resets_interval_); + its_offset += sizeof(filter_->on_change_resets_interval_); + std::memcpy(&_buffer[its_offset], &filter_->interval_, sizeof(filter_->interval_)); + its_offset += sizeof(filter_->interval_); + for (const auto &its_ignore : filter_->ignore_) { + std::memcpy(&_buffer[its_offset], &its_ignore.first, sizeof(size_t)); + its_offset += sizeof(size_t); + _buffer[its_offset] = its_ignore.second; + its_offset += sizeof(byte_t); + } + } +} + +void +subscribe_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + sizeof(major_) + + 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 subscription + subscribe_command_base::deserialize(_buffer, _error); + if (_error != error_e::ERROR_OK) + return; + + // deserialize filter + size_t its_offset(its_size); + if (_buffer.size() - its_offset + >= sizeof(bool) + sizeof(bool) + sizeof(int64_t)) { + + filter_ = std::make_shared<debounce_filter_t>(); + std::memcpy(&filter_->on_change_, &_buffer[its_offset], sizeof(filter_->on_change_)); + its_offset += sizeof(filter_->on_change_); + std::memcpy(&filter_->on_change_resets_interval_, &_buffer[its_offset], sizeof(filter_->on_change_resets_interval_)); + its_offset += sizeof(filter_->on_change_resets_interval_); + std::memcpy(&filter_->interval_, &_buffer[its_offset], sizeof(filter_->interval_)); + its_offset += sizeof(filter_->interval_); + + while (_buffer.size() - its_offset + >= sizeof(size_t) + sizeof(byte_t)) { + + size_t its_key; + byte_t its_value; + + std::memcpy(&its_key, &_buffer[its_offset], sizeof(its_key)); + if (filter_->ignore_.find(its_key) != filter_->ignore_.end()) { + + _error = error_e::ERROR_MALFORMED; + return; + } + + its_offset += sizeof(its_key); + its_value = _buffer[its_offset]; + its_offset += sizeof(its_value); + + filter_->ignore_.emplace(std::make_pair(its_key, its_value)); + } + } +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/subscribe_command_base.cpp b/implementation/protocol/src/subscribe_command_base.cpp new file mode 100644 index 0000000..cd5644a --- /dev/null +++ b/implementation/protocol/src/subscribe_command_base.cpp @@ -0,0 +1,146 @@ +// 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_command_base.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +subscribe_command_base::subscribe_command_base(id_e _id) + : command(_id), + service_(ANY_SERVICE), + instance_(ANY_INSTANCE), + eventgroup_(0), + major_(ANY_MAJOR), + event_(ANY_EVENT), + pending_id_(0) { +} + +service_t +subscribe_command_base::get_service() const { + + return service_; +} + +void +subscribe_command_base::set_service(service_t _service) { + + service_ = _service; +} + +instance_t +subscribe_command_base::get_instance() const { + + return instance_; +} + +void +subscribe_command_base::set_instance(instance_t _instance) { + + instance_ = _instance; +} + +eventgroup_t +subscribe_command_base::get_eventgroup() const { + + return eventgroup_; +} + +void +subscribe_command_base::set_eventgroup(eventgroup_t _eventgroup) { + + eventgroup_ = _eventgroup; +} + +major_version_t +subscribe_command_base::get_major() const { + + return major_; +} + +void +subscribe_command_base::set_major(major_version_t _major) { + + major_ = _major; +} + +event_t +subscribe_command_base::get_event() const { + + return event_; +} + +void +subscribe_command_base::set_event(event_t _event) { + + event_ = _event; +} + +pending_id_t +subscribe_command_base::get_pending_id() const { + + return pending_id_; +} + +void +subscribe_command_base::set_pending_id(pending_id_t _pending_id) { + + pending_id_ = _pending_id; +} + +void +subscribe_command_base::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + // 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_); + _buffer[its_offset] = major_; + its_offset += sizeof(major_); + std::memcpy(&_buffer[its_offset], &event_, sizeof(event_)); + its_offset += sizeof(event_); + std::memcpy(&_buffer[its_offset], &pending_id_, sizeof(pending_id_)); +} + +void +subscribe_command_base::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + // 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_); + major_ = _buffer[its_offset]; + its_offset += sizeof(major_); + 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 diff --git a/implementation/protocol/src/subscribe_nack_command.cpp b/implementation/protocol/src/subscribe_nack_command.cpp new file mode 100644 index 0000000..b009dad --- /dev/null +++ b/implementation/protocol/src/subscribe_nack_command.cpp @@ -0,0 +1,17 @@ +// 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/subscribe_nack_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +subscribe_nack_command::subscribe_nack_command() + : subscribe_ack_command_base(id_e::SUBSCRIBE_NACK_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/suspend_command.cpp b/implementation/protocol/src/suspend_command.cpp new file mode 100644 index 0000000..4f780f0 --- /dev/null +++ b/implementation/protocol/src/suspend_command.cpp @@ -0,0 +1,17 @@ +// 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/suspend_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +suspend_command::suspend_command() + : simple_command(id_e::SUSPEND_ID) { + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/unregister_event_command.cpp b/implementation/protocol/src/unregister_event_command.cpp new file mode 100644 index 0000000..ff9813c --- /dev/null +++ b/implementation/protocol/src/unregister_event_command.cpp @@ -0,0 +1,135 @@ +// 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 "../include/unregister_event_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +unregister_event_command::unregister_event_command() + : command(id_e::UNREGISTER_EVENT_ID), + service_(ANY_SERVICE), + instance_(ANY_INSTANCE), + event_(ANY_EVENT), + is_provided_(false) { +} + +service_t +unregister_event_command::get_service() const { + + return service_; +} + +void +unregister_event_command::set_service(service_t _service) { + + service_ = _service; +} + +instance_t +unregister_event_command::get_instance() const { + + return instance_; +} + +void +unregister_event_command::set_instance(instance_t _instance) { + + instance_ = _instance; +} + +event_t +unregister_event_command::get_event() const { + + return event_; +} + +void +unregister_event_command::set_event(event_t _event) { + + event_ = _event; +} + + +bool +unregister_event_command::is_provided() const { + + return is_provided_; +} + +void +unregister_event_command::set_provided(bool _is_provided) { + + is_provided_ = _is_provided; +} + +void +unregister_event_command::serialize(std::vector<byte_t> &_buffer, error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(event_) + sizeof(is_provided_)); + + 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_HEADER_SIZE); + 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], &event_, sizeof(event_)); + its_offset += sizeof(event_); + _buffer[its_offset] = static_cast<byte_t>(is_provided_); +} + +void +unregister_event_command::deserialize(const std::vector<byte_t> &_buffer, error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(event_) + sizeof(is_provided_)); + + 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; + + // payload + size_t its_offset(COMMAND_HEADER_SIZE); + 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(&event_, &_buffer[its_offset], sizeof(event_)); + its_offset += sizeof(event_); + is_provided_ = static_cast<bool>(_buffer[its_offset]); +} + +} // namespace protocol +} // namespace vsomeip_v3 diff --git a/implementation/protocol/src/unsubscribe_ack_command.cpp b/implementation/protocol/src/unsubscribe_ack_command.cpp new file mode 100644 index 0000000..ab7138d --- /dev/null +++ b/implementation/protocol/src/unsubscribe_ack_command.cpp @@ -0,0 +1,139 @@ +// 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/unsubscribe_ack_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +unsubscribe_ack_command::unsubscribe_ack_command() + : command(id_e::UNSUBSCRIBE_ACK_ID), + service_(ANY_SERVICE), + instance_(ANY_INSTANCE), + eventgroup_(0), + pending_id_(0) { +} + +service_t +unsubscribe_ack_command::get_service() const { + + return service_; +} + +void +unsubscribe_ack_command::set_service(service_t _service) { + + service_ = _service; +} + +instance_t +unsubscribe_ack_command::get_instance() const { + + return instance_; +} + +void +unsubscribe_ack_command::set_instance(instance_t _instance) { + + instance_ = _instance; +} + +eventgroup_t +unsubscribe_ack_command::get_eventgroup() const { + + return eventgroup_; +} + +void +unsubscribe_ack_command::set_eventgroup(eventgroup_t _eventgroup) { + + eventgroup_ = _eventgroup; +} + +pending_id_t +unsubscribe_ack_command::get_pending_id() const { + + return pending_id_; +} + +void +unsubscribe_ack_command::set_pending_id(pending_id_t _pending_id) { + + pending_id_ = _pending_id; +} + +void +unsubscribe_ack_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + 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; + + // payload + size_t its_offset(COMMAND_HEADER_SIZE); + 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(eventgroup_); + std::memcpy(&_buffer[its_offset], &pending_id_, sizeof(pending_id_)); +} + +void +unsubscribe_ack_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + 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; + + // payload + size_t its_offset(COMMAND_HEADER_SIZE); + 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(&pending_id_, &_buffer[its_offset], sizeof(pending_id_)); + +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/unsubscribe_command.cpp b/implementation/protocol/src/unsubscribe_command.cpp new file mode 100644 index 0000000..247b301 --- /dev/null +++ b/implementation/protocol/src/unsubscribe_command.cpp @@ -0,0 +1,64 @@ +// 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/unsubscribe_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +unsubscribe_command::unsubscribe_command() + : subscribe_command_base(id_e::UNSUBSCRIBE_ID) { +} + +void +unsubscribe_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + sizeof(major_) + + 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); + + // payload + subscribe_command_base::serialize(_buffer, _error); +} + +void +unsubscribe_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + size_t its_size(COMMAND_HEADER_SIZE + + sizeof(service_) + sizeof(instance_) + + sizeof(eventgroup_) + sizeof(major_) + + sizeof(event_) + sizeof(pending_id_)); + + if (its_size > _buffer.size()) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + // deserialize header + subscribe_command_base::deserialize(_buffer, _error); +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/update_security_credentials_command.cpp b/implementation/protocol/src/update_security_credentials_command.cpp new file mode 100644 index 0000000..3e69653 --- /dev/null +++ b/implementation/protocol/src/update_security_credentials_command.cpp @@ -0,0 +1,103 @@ +// 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 "../include/update_security_credentials_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +update_security_credentials_command::update_security_credentials_command() + : command(id_e::UPDATE_SECURITY_CREDENTIALS_ID) { +} + +void +update_security_credentials_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + size_t its_size(COMMAND_HEADER_SIZE + + (credentials_.size() * (sizeof(uid_t) + sizeof(gid_t)))); + + 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_HEADER_SIZE); + for (const auto &c : credentials_) { + std::memcpy(&_buffer[its_offset], &c.first, sizeof(c.first)); + its_offset += sizeof(c.first); + std::memcpy(&_buffer[its_offset], &c.second, sizeof(c.second)); + its_offset += sizeof(c.second); + } +} + +void +update_security_credentials_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_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 + if (COMMAND_HEADER_SIZE + size_ > _buffer.size()) { + + _error = error_e::ERROR_NOT_ENOUGH_BYTES; + return; + } + + size_t its_count(size_ / (sizeof(uid_t) + sizeof(gid_t))); + size_t its_offset(COMMAND_HEADER_SIZE); + + uid_t its_uid; + gid_t its_gid; + for (size_t i = 0; i < its_count; i++) { + std::memcpy(&its_uid, &_buffer[its_offset], sizeof(its_uid)); + its_offset += sizeof(its_uid); + std::memcpy(&its_gid, &_buffer[its_offset], sizeof(its_gid)); + its_offset += sizeof(its_gid); + + credentials_.emplace(std::make_pair(its_uid, its_gid)); + } +} + + +std::set<std::pair<uid_t, gid_t> > +update_security_credentials_command::get_credentials() const { + + return (credentials_); +} + +void +update_security_credentials_command::set_credentials( + const std::set<std::pair<uid_t, gid_t> > &_credentials) { + + credentials_ = _credentials; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/update_security_policy_command.cpp b/implementation/protocol/src/update_security_policy_command.cpp new file mode 100644 index 0000000..8ff79e2 --- /dev/null +++ b/implementation/protocol/src/update_security_policy_command.cpp @@ -0,0 +1,120 @@ +// 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 "../include/update_security_policy_command.hpp" +#include "../../security/include/policy.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +update_security_policy_command::update_security_policy_command( + bool _is_internal) + : command(_is_internal ? + id_e::UPDATE_SECURITY_POLICY_INT_ID : + id_e::UPDATE_SECURITY_POLICY_ID) { +} + +void +update_security_policy_command::serialize(std::vector<byte_t> &_buffer, + error_e &_error) const { + + std::vector<byte_t> its_policy_data; + if (policy_) { + if (policy_->serialize(its_policy_data)) { + _error = error_e::ERROR_UNKNOWN; + return; + } + } + + size_t its_size(COMMAND_HEADER_SIZE + sizeof(update_id_) + + its_policy_data.size()); + + 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_HEADER_SIZE); + std::memcpy(&_buffer[its_offset], &update_id_, sizeof(update_id_)); + its_offset += sizeof(update_id_); + std::memcpy(&_buffer[its_offset], + &its_policy_data[0], its_policy_data.size()); +} + +void +update_security_policy_command::deserialize(const std::vector<byte_t> &_buffer, + error_e &_error) { + + if (COMMAND_HEADER_SIZE + sizeof(update_id_) > _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 + std::memcpy(&update_id_, &_buffer[COMMAND_POSITION_PAYLOAD], + sizeof(update_id_)); + policy_ = std::make_shared<policy>(); + const byte_t *its_policy_data + = &_buffer[COMMAND_HEADER_SIZE + sizeof(update_id_)]; + uint32_t its_policy_size + = uint32_t(_buffer.size() - COMMAND_HEADER_SIZE - sizeof(update_id_)); + + if (its_policy_size == 0 + || !policy_->deserialize(its_policy_data, its_policy_size)) { + + _error = error_e::ERROR_UNKNOWN; + policy_.reset(); + return; + } +} + +uint32_t +update_security_policy_command::get_update_id() const { + + return (update_id_); +} + +void +update_security_policy_command::set_update_id(uint32_t _update_id) { + + update_id_ = _update_id; +} + +std::shared_ptr<policy> +update_security_policy_command::get_policy() const { + + return (policy_); +} + +void +update_security_policy_command::set_policy( + const std::shared_ptr<policy> &_policy) { + + policy_ = _policy; +} + +} // namespace protocol +} // namespace vsomeip diff --git a/implementation/protocol/src/update_security_policy_response_command.cpp b/implementation/protocol/src/update_security_policy_response_command.cpp new file mode 100644 index 0000000..91573f0 --- /dev/null +++ b/implementation/protocol/src/update_security_policy_response_command.cpp @@ -0,0 +1,18 @@ +// 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/update_security_policy_response_command.hpp" + +namespace vsomeip_v3 { +namespace protocol { + +update_security_policy_response_command::update_security_policy_response_command() + : security_policy_response_command_base( + id_e::UPDATE_SECURITY_POLICY_RESPONSE_ID) { + +} + +} // namespace protocol +} // namespace vsomeip |