diff --git a/include/events/event_types.h b/include/events/event_types.h index e2b0fed23a..c842887cd2 100644 --- a/include/events/event_types.h +++ b/include/events/event_types.h @@ -81,7 +81,7 @@ struct EventSubscription { std::chrono::milliseconds cycle_time{0}; // For periodic events std::chrono::steady_clock::time_point last_notification{std::chrono::steady_clock::now()}; - EventSubscription(uint16_t svc_id = 0, uint16_t inst_id = 0, uint16_t evt_id = 0, uint16_t eg_id = 0) + explicit EventSubscription(uint16_t svc_id = 0, uint16_t inst_id = 0, uint16_t evt_id = 0, uint16_t eg_id = 0) : service_id(svc_id), instance_id(inst_id), event_id(evt_id), eventgroup_id(eg_id) { last_notification = std::chrono::steady_clock::now(); } @@ -99,7 +99,7 @@ struct EventNotification { std::vector event_data; std::chrono::steady_clock::time_point timestamp{std::chrono::steady_clock::now()}; - EventNotification(uint16_t svc_id = 0, uint16_t inst_id = 0, uint16_t evt_id = 0) + explicit EventNotification(uint16_t svc_id = 0, uint16_t inst_id = 0, uint16_t evt_id = 0) : service_id(svc_id), instance_id(inst_id), event_id(evt_id) { timestamp = std::chrono::steady_clock::now(); } diff --git a/include/sd/sd_message.h b/include/sd/sd_message.h index bce4c85ccf..46171127d1 100644 --- a/include/sd/sd_message.h +++ b/include/sd/sd_message.h @@ -24,7 +24,7 @@ namespace someip::sd { /** @implements REQ_SD_200A, REQ_SD_200B, REQ_SD_200C */ class SdEntry { public: - SdEntry(EntryType type, uint32_t ttl = 0) + explicit SdEntry(EntryType type, uint32_t ttl = 0) : type_(type), ttl_(ttl) {} virtual ~SdEntry() = default; @@ -58,7 +58,7 @@ class SdEntry { /** @implements REQ_SD_200B */ class ServiceEntry : public SdEntry { public: - ServiceEntry(EntryType type = EntryType::FIND_SERVICE) + explicit ServiceEntry(EntryType type = EntryType::FIND_SERVICE) : SdEntry(type) {} uint16_t get_service_id() const { return service_id_; } @@ -88,7 +88,7 @@ class ServiceEntry : public SdEntry { */ class EventGroupEntry : public SdEntry { public: - EventGroupEntry(EntryType type = EntryType::SUBSCRIBE_EVENTGROUP) + explicit EventGroupEntry(EntryType type = EntryType::SUBSCRIBE_EVENTGROUP) : SdEntry(type) {} uint16_t get_service_id() const { return service_id_; } @@ -118,7 +118,7 @@ class EventGroupEntry : public SdEntry { */ class SdOption { public: - SdOption(OptionType type) : type_(type) {} + explicit SdOption(OptionType type) : type_(type) {} virtual ~SdOption() = default; OptionType get_type() const { return type_; } diff --git a/include/sd/sd_types.h b/include/sd/sd_types.h index fcbd1fd287..7e0c9c9265 100644 --- a/include/sd/sd_types.h +++ b/include/sd/sd_types.h @@ -68,7 +68,7 @@ struct ServiceInstance { uint8_t protocol{0x11}; // Default to UDP (0x11) uint32_t ttl_seconds{0}; // Time to live - ServiceInstance(uint16_t svc_id = 0, uint16_t inst_id = 0, + explicit ServiceInstance(uint16_t svc_id = 0, uint16_t inst_id = 0, uint8_t maj_ver = 0, uint8_t min_ver = 0) : service_id(svc_id), instance_id(inst_id), major_version(maj_ver), minor_version(min_ver) {} @@ -81,7 +81,7 @@ struct EventGroup { uint8_t minor_version{0}; std::vector event_ids; - EventGroup(uint16_t eg_id = 0, uint8_t maj_ver = 0, uint8_t min_ver = 0) + explicit EventGroup(uint16_t eg_id = 0, uint8_t maj_ver = 0, uint8_t min_ver = 0) : eventgroup_id(eg_id), major_version(maj_ver), minor_version(min_ver) {} }; @@ -125,7 +125,7 @@ struct EventGroupSubscription { SubscriptionState state{SubscriptionState::REQUESTED}; std::chrono::steady_clock::time_point timestamp{std::chrono::steady_clock::now()}; - EventGroupSubscription(uint16_t svc_id = 0, uint16_t inst_id = 0, uint16_t eg_id = 0) + explicit EventGroupSubscription(uint16_t svc_id = 0, uint16_t inst_id = 0, uint16_t eg_id = 0) : service_id(svc_id), instance_id(inst_id), eventgroup_id(eg_id) { timestamp = std::chrono::steady_clock::now(); } diff --git a/src/common/result.cpp b/src/common/result.cpp index 40e68c88dc..a0bf1ddc9f 100644 --- a/src/common/result.cpp +++ b/src/common/result.cpp @@ -10,7 +10,7 @@ namespace someip { */ /** @implements REQ_ARCH_004, REQ_MY_001 */ std::string to_string(Result result) { - static const std::unordered_map result_strings = { + static const std::unordered_map RESULT_STRINGS = { {Result::SUCCESS, "SUCCESS"}, {Result::NETWORK_ERROR, "NETWORK_ERROR"}, {Result::NOT_CONNECTED, "NOT_CONNECTED"}, @@ -46,8 +46,8 @@ std::string to_string(Result result) { {Result::UNKNOWN_ERROR, "UNKNOWN_ERROR"} }; - auto it = result_strings.find(result); - return (it != result_strings.end()) ? it->second : "UNKNOWN_RESULT"; + const auto it = RESULT_STRINGS.find(result); + return (it != RESULT_STRINGS.end()) ? it->second : "UNKNOWN_RESULT"; } } // namespace someip diff --git a/src/e2e/e2e_profiles/standard_profile.cpp b/src/e2e/e2e_profiles/standard_profile.cpp index 693c43bda4..a123d3824a 100644 --- a/src/e2e/e2e_profiles/standard_profile.cpp +++ b/src/e2e/e2e_profiles/standard_profile.cpp @@ -246,7 +246,7 @@ class BasicE2EProfile : public E2EProfile { auto now = std::chrono::steady_clock::now(); auto ms_now = std::chrono::duration_cast( now.time_since_epoch()).count(); - uint16_t current_freshness = static_cast(ms_now & 0xFFFF); + auto current_freshness = static_cast(ms_now & 0xFFFF); // Calculate freshness difference (handle wrap-around) // Since we're using 16-bit values, we need to handle wrap-around @@ -264,7 +264,7 @@ class BasicE2EProfile : public E2EProfile { // Convert timeout to 16-bit units (approximately) // Since we're storing lower 16 bits of milliseconds, // we compare against timeout_ms directly (assuming timeout < 65535 ms) - uint16_t const timeout_units = static_cast(config.freshness_timeout_ms); + auto const timeout_units = static_cast(config.freshness_timeout_ms); if (freshness_diff > timeout_units && freshness_diff < (0xFFFF - timeout_units)) { // If difference is large and not due to wrap-around, it's stale return Result::TIMEOUT; // Stale data diff --git a/src/platform/freertos/memory.cpp b/src/platform/freertos/memory.cpp index c69f5baa8c..3d798430b2 100644 --- a/src/platform/freertos/memory.cpp +++ b/src/platform/freertos/memory.cpp @@ -21,6 +21,8 @@ #include #include +#include +#include #include #include #include @@ -34,10 +36,10 @@ static constexpr size_t POOL_SIZE = SOMEIP_FREERTOS_MESSAGE_POOL_SIZE; namespace { -alignas(someip::Message) char - pool_buffer[POOL_SIZE * sizeof(someip::Message)]; +alignas(someip::Message) std::array + pool_buffer{}; -bool block_used[POOL_SIZE] = {}; +std::array block_used{}; SemaphoreHandle_t pool_mutex = nullptr; std::atomic pool_initialized{false}; @@ -52,7 +54,7 @@ void ensure_pool_init() { pool_mutex = xSemaphoreCreateMutex(); configASSERT(pool_mutex != nullptr); if (pool_mutex != nullptr) { - std::memset(block_used, 0, sizeof(block_used)); + std::fill(block_used.begin(), block_used.end(), false); pool_initialized.store(true, std::memory_order_release); } } @@ -66,7 +68,7 @@ void release_message_impl(someip::Message* msg) { } auto raw_addr = reinterpret_cast(msg); - auto pool_addr = reinterpret_cast(pool_buffer); + auto pool_addr = reinterpret_cast(pool_buffer.data()); if (raw_addr < pool_addr || raw_addr >= pool_addr + POOL_SIZE * sizeof(someip::Message)) { return; } @@ -98,7 +100,7 @@ MessagePtr allocate_message() { block_used[i] = true; xSemaphoreGive(pool_mutex); - void* block = pool_buffer + i * sizeof(Message); + void* block = pool_buffer.data() + i * sizeof(Message); auto* msg = new (block) Message(); return MessagePtr(msg, [](Message* p) { release_message(p); diff --git a/src/platform/threadx/memory.cpp b/src/platform/threadx/memory.cpp index 4dc1f832d2..93c1b2a117 100644 --- a/src/platform/threadx/memory.cpp +++ b/src/platform/threadx/memory.cpp @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -35,8 +36,8 @@ std::atomic pool_initialized{false}; namespace { -alignas(someip::Message) UCHAR - pool_buffer[POOL_SIZE * sizeof(someip::Message)]; +alignas(someip::Message) std::array + pool_buffer{}; TX_MUTEX pool_guard; @@ -56,7 +57,7 @@ void ensure_pool_init() { status = tx_block_pool_create(&message_pool, const_cast("someip_msg"), sizeof(someip::Message), - pool_buffer, + pool_buffer.data(), sizeof(pool_buffer)); if (status != TX_SUCCESS) { tx_mutex_delete(&pool_guard); diff --git a/src/rpc/rpc_server.cpp b/src/rpc/rpc_server.cpp index d99507213c..3740e7b6f4 100644 --- a/src/rpc/rpc_server.cpp +++ b/src/rpc/rpc_server.cpp @@ -126,7 +126,7 @@ class RpcServerImpl : public transport::ITransportListener { MethodHandler handler; { platform::ScopedLock const lock(methods_mutex_); - auto it = method_handlers_.find(message->get_method_id()); + const auto it = method_handlers_.find(message->get_method_id()); if (it == method_handlers_.end()) { // Method not found - send error response send_error_response(message, sender, ReturnCode::E_UNKNOWN_METHOD); @@ -163,12 +163,12 @@ class RpcServerImpl : public transport::ITransportListener { /** @implements REQ_MSG_115, REQ_MSG_117, REQ_MSG_117_E01 */ void send_success_response(MessagePtr const& request, const transport::Endpoint& sender, const std::vector& return_values) { - MessageId response_msg_id(request->get_service_id(), request->get_method_id()); + const MessageId response_msg_id(request->get_service_id(), request->get_method_id()); Message response(response_msg_id, request->get_request_id(), MessageType::RESPONSE, ReturnCode::E_OK); response.set_payload(return_values); - Result result = transport_->send_message(response, sender); + const Result result = transport_->send_message(response, sender); if (result != Result::SUCCESS) { // Log error or handle send failure } @@ -176,11 +176,11 @@ class RpcServerImpl : public transport::ITransportListener { /** @implements REQ_MSG_115, REQ_MSG_117, REQ_MSG_117_E01, REQ_MSG_129 */ void send_error_response(MessagePtr const& request, const transport::Endpoint& sender, ReturnCode error_code) { - MessageId response_msg_id(request->get_service_id(), request->get_method_id()); + const MessageId response_msg_id(request->get_service_id(), request->get_method_id()); Message response(response_msg_id, request->get_request_id(), MessageType::ERROR, error_code); - Result result = transport_->send_message(response, sender); + const Result result = transport_->send_message(response, sender); if (result != Result::SUCCESS) { // Log error or handle send failure } diff --git a/src/sd/sd_client.cpp b/src/sd/sd_client.cpp index fafe6ff6c6..b37871f279 100644 --- a/src/sd/sd_client.cpp +++ b/src/sd/sd_client.cpp @@ -126,7 +126,7 @@ class SdClientImpl : public transport::ITransportListener { someip_message.set_payload(sd_message.serialize()); // Send multicast find message - transport::Endpoint multicast_endpoint(config_.multicast_address, config_.multicast_port); + transport::Endpoint const multicast_endpoint(config_.multicast_address, config_.multicast_port); if (transport_->send_message(someip_message, multicast_endpoint) != Result::SUCCESS) { return false; } @@ -204,7 +204,7 @@ class SdClientImpl : public transport::ITransportListener { someip_message.set_payload(sd_message.serialize()); // Send multicast message - transport::Endpoint multicast_endpoint(config_.multicast_address, config_.multicast_port); + transport::Endpoint const multicast_endpoint(config_.multicast_address, config_.multicast_port); return transport_->send_message(someip_message, multicast_endpoint) == Result::SUCCESS; } @@ -233,7 +233,7 @@ class SdClientImpl : public transport::ITransportListener { someip_message.set_payload(sd_message.serialize()); // Send multicast message - transport::Endpoint multicast_endpoint(config_.multicast_address, config_.multicast_port); + transport::Endpoint const multicast_endpoint(config_.multicast_address, config_.multicast_port); return transport_->send_message(someip_message, multicast_endpoint) == Result::SUCCESS; } @@ -273,7 +273,7 @@ class SdClientImpl : public transport::ITransportListener { }; bool join_multicast_group() { - auto udp_transport = std::dynamic_pointer_cast(transport_); + const auto udp_transport = std::dynamic_pointer_cast(transport_); if (!udp_transport) { return false; } @@ -282,7 +282,7 @@ class SdClientImpl : public transport::ITransportListener { } void leave_multicast_group() { - auto udp_transport = std::dynamic_pointer_cast(transport_); + const auto udp_transport = std::dynamic_pointer_cast(transport_); if (udp_transport) { udp_transport->leave_multicast_group(config_.multicast_address); } @@ -363,7 +363,7 @@ class SdClientImpl : public transport::ITransportListener { { platform::ScopedLock const lock(available_services_mutex_); - auto it = std::find_if(available_services_.begin(), available_services_.end(), + const auto it = std::find_if(available_services_.begin(), available_services_.end(), [&](const ServiceInstance& svc) { return svc.service_id == instance.service_id && svc.instance_id == instance.instance_id; @@ -404,7 +404,7 @@ class SdClientImpl : public transport::ITransportListener { avail_cb(instance); } for (const auto& cb : find_cbs) { - std::vector found_services = {instance}; + const std::vector found_services = {instance}; cb(found_services); } } @@ -417,7 +417,7 @@ class SdClientImpl : public transport::ITransportListener { { platform::ScopedLock const lock(available_services_mutex_); - auto it = std::remove_if(available_services_.begin(), available_services_.end(), + const auto it = std::remove_if(available_services_.begin(), available_services_.end(), [&](const ServiceInstance& svc) { return svc.service_id == instance.service_id && svc.instance_id == instance.instance_id; diff --git a/src/sd/sd_message.cpp b/src/sd/sd_message.cpp index cac8bf569f..31c0a71fd3 100644 --- a/src/sd/sd_message.cpp +++ b/src/sd/sd_message.cpp @@ -16,7 +16,9 @@ #include "platform/byteorder.h" #include "platform/net.h" #include +#include #include +#include namespace someip::sd { @@ -78,7 +80,7 @@ bool SdEntry::deserialize(const std::vector& data, size_t& offset) { type_ = static_cast(data[offset++]); // byte 0 index1_ = data[offset++]; // byte 1 index2_ = data[offset++]; // byte 2 - uint8_t opts_byte = data[offset++]; // byte 3 + const uint8_t opts_byte = data[offset++]; // byte 3 num_opts1_ = (opts_byte >> 4) & 0x0F; num_opts2_ = opts_byte & 0x0F; @@ -120,12 +122,10 @@ bool ServiceEntry::deserialize(const std::vector& data, size_t& offset) // SdEntry::deserialize consumed bytes 0-4 (type, idx1, idx2, opts, skip). // We're now at byte 5 within the 16-byte entry. - service_id_ = (data[offset] << 8) | data[offset + 1]; - instance_id_ = (data[offset + 2] << 8) | data[offset + 3]; + service_id_ = static_cast((static_cast(data[offset]) << 8) | data[offset + 1]); + instance_id_ = static_cast((static_cast(data[offset + 2]) << 8) | data[offset + 3]); major_version_ = data[offset + 4]; - // TTL is 24-bit (bytes 9-11 of the entry) - ttl_ = (data[offset + 5] << 16) | (data[offset + 6] << 8) | data[offset + 7]; - // Minor version is 32-bit (bytes 12-15), but stored as uint8_t (low byte) + ttl_ = (static_cast(data[offset + 5]) << 16) | (static_cast(data[offset + 6]) << 8) | data[offset + 7]; minor_version_ = data[offset + 11]; offset += 12; @@ -166,14 +166,11 @@ bool EventGroupEntry::deserialize(const std::vector& data, size_t& offs return false; } - service_id_ = (data[offset] << 8) | data[offset + 1]; - instance_id_ = (data[offset + 2] << 8) | data[offset + 3]; + service_id_ = static_cast((static_cast(data[offset]) << 8) | data[offset + 1]); + instance_id_ = static_cast((static_cast(data[offset + 2]) << 8) | data[offset + 3]); major_version_ = data[offset + 4]; - // TTL is 24-bit (bytes 9-11 of the entry) - ttl_ = (data[offset + 5] << 16) | (data[offset + 6] << 8) | data[offset + 7]; - // Bytes 12-13: Reserved + Counter (skip) - // Bytes 14-15: EventGroup ID - eventgroup_id_ = (data[offset + 10] << 8) | data[offset + 11]; + ttl_ = (static_cast(data[offset + 5]) << 16) | (static_cast(data[offset + 6]) << 8) | data[offset + 7]; + eventgroup_id_ = static_cast((static_cast(data[offset + 10]) << 8) | data[offset + 11]); offset += 12; return true; @@ -201,7 +198,7 @@ bool SdOption::deserialize(const std::vector& data, size_t& offset) { return false; } - length_ = (data[offset] << 8) | data[offset + 1]; + length_ = static_cast((static_cast(data[offset]) << 8) | data[offset + 1]); offset += 2; type_ = static_cast(data[offset++]); @@ -252,8 +249,10 @@ bool IPv4EndpointOption::deserialize(const std::vector& data, size_t& o } // IPv4 Address (4 bytes, network byte order) - ipv4_address_ = (data[offset] << 24) | (data[offset + 1] << 16) | - (data[offset + 2] << 8) | data[offset + 3]; + ipv4_address_ = (static_cast(data[offset]) << 24) | + (static_cast(data[offset + 1]) << 16) | + (static_cast(data[offset + 2]) << 8) | + static_cast(data[offset + 3]); offset += 4; // Validate IP address (REQ_SD_064_E01) @@ -273,7 +272,7 @@ bool IPv4EndpointOption::deserialize(const std::vector& data, size_t& o protocol_ = data[offset++]; // Port (2 bytes, network byte order) - uint16_t network_port = (data[offset] << 8) | data[offset + 1]; + uint16_t network_port = static_cast((static_cast(data[offset]) << 8) | data[offset + 1]); port_ = someip_ntohs(network_port); offset += 2; @@ -290,11 +289,11 @@ void IPv4EndpointOption::set_ipv4_address_from_string(const std::string& ip_addr } std::string IPv4EndpointOption::get_ipv4_address_string() const { - char buffer[INET_ADDRSTRLEN]; + std::array buffer{}; struct in_addr addr{}; addr.s_addr = ipv4_address_; // Already in network byte order - someip_inet_ntop(AF_INET, &addr, buffer, sizeof(buffer)); - return buffer; + someip_inet_ntop(AF_INET, &addr, buffer.data(), buffer.size()); + return std::string(buffer.data()); } // IPv4MulticastOption implementation @@ -333,8 +332,10 @@ bool IPv4MulticastOption::deserialize(const std::vector& data, size_t& return false; } - ipv4_address_ = (data[offset] << 24) | (data[offset + 1] << 16) | - (data[offset + 2] << 8) | data[offset + 3]; + ipv4_address_ = (static_cast(data[offset]) << 24) | + (static_cast(data[offset + 1]) << 16) | + (static_cast(data[offset + 2]) << 8) | + static_cast(data[offset + 3]); offset += 5; // Skip address + reserved // Validate IP address (REQ_SD_064_E01) @@ -344,9 +345,8 @@ bool IPv4MulticastOption::deserialize(const std::vector& data, size_t& << ((ipv4_address_ >> 16) & 0xFF) << "." << ((ipv4_address_ >> 8) & 0xFF) << "." << (ipv4_address_ & 0xFF) << '\n'; - // Continue processing despite invalid address } - port_ = (data[offset] << 8) | data[offset + 1]; + port_ = static_cast((static_cast(data[offset]) << 8) | data[offset + 1]); offset += 2; return true; @@ -371,7 +371,7 @@ std::vector ConfigurationOption::serialize() const { data.insert(data.end(), config_string_.begin(), config_string_.end()); // Update length - uint16_t length = static_cast(config_string_.size()); + const auto length = static_cast(config_string_.size()); data[2] = (length >> 8) & 0xFF; data[3] = length & 0xFF; @@ -389,7 +389,7 @@ bool ConfigurationOption::deserialize(const std::vector& data, size_t& } // Extract configuration string - auto first = data.begin() + static_cast(offset); + const auto first = data.begin() + static_cast(offset); config_string_.assign(first, first + static_cast(length_)); offset += length_; @@ -410,7 +410,7 @@ std::vector SdMessage::serialize() const { std::vector data; // Flags (1 byte) - ensure reserved bits 5-0 are zero (REQ_SD_013) - uint8_t flags_to_send = flags_ & 0xC0; + const uint8_t flags_to_send = flags_ & 0xC0; data.push_back(flags_to_send); // Reserved (3 bytes) @@ -419,19 +419,19 @@ std::vector SdMessage::serialize() const { data.push_back(reserved_ & 0xFF); // Length of Entries Array (4 bytes) - placeholder - size_t entries_len_offset = data.size(); + const size_t entries_len_offset = data.size(); data.push_back(0); data.push_back(0); data.push_back(0); data.push_back(0); // Entries Array - size_t entries_start = data.size(); + const size_t entries_start = data.size(); for (const auto& entry : entries_) { auto entry_data = entry->serialize(); data.insert(data.end(), entry_data.begin(), entry_data.end()); } - uint32_t entries_length = data.size() - entries_start; + const uint32_t entries_length = static_cast(data.size() - entries_start); // Back-fill Length of Entries Array data[entries_len_offset] = (entries_length >> 24) & 0xFF; @@ -440,19 +440,19 @@ std::vector SdMessage::serialize() const { data[entries_len_offset + 3] = entries_length & 0xFF; // Length of Options Array (4 bytes) - placeholder - size_t options_len_offset = data.size(); + const size_t options_len_offset = data.size(); data.push_back(0); data.push_back(0); data.push_back(0); data.push_back(0); // Options Array - size_t options_start = data.size(); + const size_t options_start = data.size(); for (const auto& option : options_) { auto option_data = option->serialize(); data.insert(data.end(), option_data.begin(), option_data.end()); } - uint32_t options_length = data.size() - options_start; + const uint32_t options_length = static_cast(data.size() - options_start); // Back-fill Length of Options Array data[options_len_offset] = (options_length >> 24) & 0xFF; @@ -473,12 +473,14 @@ bool SdMessage::deserialize(const std::vector& data) { // Flags (1 byte) + Reserved (3 bytes) flags_ = data[offset++]; - reserved_ = (data[offset] << 16) | (data[offset + 1] << 8) | data[offset + 2]; + reserved_ = (static_cast(data[offset]) << 16) | (static_cast(data[offset + 1]) << 8) | data[offset + 2]; offset += 3; // Length of Entries Array (4 bytes) - uint32_t entries_length = (data[offset] << 24) | (data[offset + 1] << 16) | - (data[offset + 2] << 8) | data[offset + 3]; + const uint32_t entries_length = (static_cast(data[offset]) << 24) | + (static_cast(data[offset + 1]) << 16) | + (static_cast(data[offset + 2]) << 8) | + static_cast(data[offset + 3]); offset += 4; if (offset + entries_length > data.size()) { @@ -492,7 +494,7 @@ bool SdMessage::deserialize(const std::vector& data) { // Parse entries (each entry is exactly 16 bytes) size_t const entries_end = offset + entries_length; while (offset + 16 <= entries_end) { - uint8_t raw_entry_type = data[offset]; + const uint8_t raw_entry_type = data[offset]; std::unique_ptr entry; if (raw_entry_type == 0x00 || raw_entry_type == 0x01) { @@ -513,8 +515,10 @@ bool SdMessage::deserialize(const std::vector& data) { if (offset + 4 > data.size()) { return true; // no options section present } - uint32_t options_length = (data[offset] << 24) | (data[offset + 1] << 16) | - (data[offset + 2] << 8) | data[offset + 3]; + const uint32_t options_length = (static_cast(data[offset]) << 24) | + (static_cast(data[offset + 1]) << 16) | + (static_cast(data[offset + 2]) << 8) | + static_cast(data[offset + 3]); offset += 4; if (offset + options_length > data.size()) { @@ -530,7 +534,7 @@ bool SdMessage::deserialize(const std::vector& data) { // Options start with length(2) + type(1) + reserved(1). // Peek at the type byte (offset + 2) to determine the option kind. - uint8_t type_byte = data[offset + 2]; + const uint8_t type_byte = data[offset + 2]; OptionType const option_type = static_cast(type_byte); std::unique_ptr option; @@ -541,8 +545,11 @@ bool SdMessage::deserialize(const std::vector& data) { } else if (option_type == OptionType::IPV4_MULTICAST) { option = std::make_unique(); } else { - // Skip unknown option - uint16_t option_len = (data[offset] << 8) | data[offset + 1]; + const uint16_t option_len = static_cast( + (static_cast(data[offset]) << 8) | data[offset + 1]); + if (offset + 4 + option_len > options_end) { + return false; + } offset += 4 + option_len; continue; } diff --git a/src/sd/sd_server.cpp b/src/sd/sd_server.cpp index 67ed9bb483..63764f285e 100644 --- a/src/sd/sd_server.cpp +++ b/src/sd/sd_server.cpp @@ -116,7 +116,7 @@ class SdServerImpl : public transport::ITransportListener { platform::ScopedLock const lock(offered_services_mutex_); // Check if service already offered - auto it = std::find_if(offered_services_.begin(), offered_services_.end(), + const auto it = std::find_if(offered_services_.begin(), offered_services_.end(), [&](const OfferedService& svc) { return svc.instance.service_id == instance.service_id && svc.instance.instance_id == instance.instance_id; @@ -152,7 +152,7 @@ class SdServerImpl : public transport::ITransportListener { bool stop_offer_service(uint16_t service_id, uint16_t instance_id) { platform::ScopedLock const lock(offered_services_mutex_); - auto it = std::find_if(offered_services_.begin(), offered_services_.end(), + const auto it = std::find_if(offered_services_.begin(), offered_services_.end(), [&](const OfferedService& svc) { return svc.instance.service_id == service_id && svc.instance.instance_id == instance_id; @@ -173,7 +173,7 @@ class SdServerImpl : public transport::ITransportListener { bool update_service_ttl(uint16_t service_id, uint16_t instance_id, uint32_t ttl_seconds) { platform::ScopedLock const lock(offered_services_mutex_); - auto it = std::find_if(offered_services_.begin(), offered_services_.end(), + const auto it = std::find_if(offered_services_.begin(), offered_services_.end(), [&](const OfferedService& svc) { return svc.instance.service_id == service_id && svc.instance.instance_id == instance_id; @@ -207,7 +207,7 @@ class SdServerImpl : public transport::ITransportListener { // Add IPv4 multicast option (spec requires multicast option for ACK) auto multicast_option = std::make_unique(); // Convert multicast address to network byte order - in_addr_t multicast_addr = someip_inet_addr(config_.multicast_address.c_str()); + const in_addr_t multicast_addr = someip_inet_addr(config_.multicast_address.c_str()); multicast_option->set_ipv4_address(multicast_addr); multicast_option->set_port(someip_htons(config_.multicast_port)); response_message.add_option(std::move(multicast_option)); @@ -218,7 +218,7 @@ class SdServerImpl : public transport::ITransportListener { // Send unicast response to client // Parse client_address (format: "ip:port" or just "ip") - size_t colon_pos = client_address.find(':'); + const size_t colon_pos = client_address.find(':'); std::string client_ip = client_address; uint16_t client_port = config_.unicast_port; // Default to our unicast port @@ -236,7 +236,7 @@ class SdServerImpl : public transport::ITransportListener { someip_message.set_payload(response_message.serialize()); // Send the ACK message - Result result = transport_->send_message(someip_message, client_endpoint); + const Result result = transport_->send_message(someip_message, client_endpoint); return result == Result::SUCCESS; } @@ -269,7 +269,7 @@ class SdServerImpl : public transport::ITransportListener { }; bool join_multicast_group() { - auto udp_transport = std::dynamic_pointer_cast(transport_); + const auto udp_transport = std::dynamic_pointer_cast(transport_); if (!udp_transport) { return false; } @@ -278,7 +278,7 @@ class SdServerImpl : public transport::ITransportListener { } void leave_multicast_group() { - auto udp_transport = std::dynamic_pointer_cast(transport_); + const auto udp_transport = std::dynamic_pointer_cast(transport_); if (udp_transport) { udp_transport->leave_multicast_group(config_.multicast_address); } @@ -321,9 +321,9 @@ class SdServerImpl : public transport::ITransportListener { void send_periodic_offers() { platform::ScopedLock const lock(offered_services_mutex_); - auto now = std::chrono::steady_clock::now(); + const auto now = std::chrono::steady_clock::now(); for (auto& service : offered_services_) { - auto time_since_last_offer = std::chrono::duration_cast( + const auto time_since_last_offer = std::chrono::duration_cast( now - service.last_offer_time); if (time_since_last_offer >= config_.cyclic_offer) { @@ -357,10 +357,10 @@ class SdServerImpl : public transport::ITransportListener { auto endpoint_option = std::make_unique(); // Parse unicast endpoint (format: "ip:port") - size_t colon_pos = service.unicast_endpoint.find(':'); + const size_t colon_pos = service.unicast_endpoint.find(':'); if (colon_pos != std::string::npos) { - std::string ip_str = service.unicast_endpoint.substr(0, colon_pos); - std::string port_str = service.unicast_endpoint.substr(colon_pos + 1); + const std::string ip_str = service.unicast_endpoint.substr(0, colon_pos); + const std::string port_str = service.unicast_endpoint.substr(colon_pos + 1); endpoint_option->set_ipv4_address_from_string(ip_str); endpoint_option->set_port(static_cast(std::stoi(port_str))); @@ -381,8 +381,8 @@ class SdServerImpl : public transport::ITransportListener { someip_message.set_payload(sd_message.serialize()); // Send multicast offer - transport::Endpoint multicast_endpoint(config_.multicast_address, config_.multicast_port); - Result result = transport_->send_message(someip_message, multicast_endpoint); + transport::Endpoint const multicast_endpoint(config_.multicast_address, config_.multicast_port); + const Result result = transport_->send_message(someip_message, multicast_endpoint); if (result != Result::SUCCESS) { // Log error or handle failure } @@ -407,8 +407,8 @@ class SdServerImpl : public transport::ITransportListener { someip_message.set_payload(sd_message.serialize()); // Send multicast stop offer - transport::Endpoint multicast_endpoint(config_.multicast_address, config_.multicast_port); - Result result = transport_->send_message(someip_message, multicast_endpoint); + transport::Endpoint const multicast_endpoint(config_.multicast_address, config_.multicast_port); + const Result result = transport_->send_message(someip_message, multicast_endpoint); if (result != Result::SUCCESS) { // Log error or handle failure } @@ -488,7 +488,7 @@ class SdServerImpl : public transport::ITransportListener { [[maybe_unused]] uint8_t client_protocol = 0x11; // Default to UDP // Check if entry references an endpoint option - uint8_t index1 = subscription_entry.get_index1(); + const uint8_t index1 = subscription_entry.get_index1(); const auto& options = message.get_options(); if (index1 < options.size()) { @@ -530,10 +530,10 @@ class SdServerImpl : public transport::ITransportListener { auto endpoint_option = std::make_unique(); // Parse unicast endpoint (format: "ip:port") - size_t colon_pos = service.unicast_endpoint.find(':'); + const size_t colon_pos = service.unicast_endpoint.find(':'); if (colon_pos != std::string::npos) { - std::string ip_str = service.unicast_endpoint.substr(0, colon_pos); - std::string port_str = service.unicast_endpoint.substr(colon_pos + 1); + const std::string ip_str = service.unicast_endpoint.substr(0, colon_pos); + const std::string port_str = service.unicast_endpoint.substr(colon_pos + 1); endpoint_option->set_ipv4_address_from_string(ip_str); endpoint_option->set_port(static_cast(std::stoi(port_str))); @@ -554,7 +554,7 @@ class SdServerImpl : public transport::ITransportListener { someip_message.set_payload(sd_message.serialize()); // Send unicast offer to client - Result result = transport_->send_message(someip_message, client); + const Result result = transport_->send_message(someip_message, client); if (result != Result::SUCCESS) { // Log error or handle failure } diff --git a/src/serialization/serializer.cpp b/src/serialization/serializer.cpp index e339d9c45f..acad904af7 100644 --- a/src/serialization/serializer.cpp +++ b/src/serialization/serializer.cpp @@ -150,7 +150,7 @@ void Serializer::align_to(size_t alignment) { if (alignment == 0) { return; } - size_t current_size = buffer_.size(); + const size_t current_size = buffer_.size(); size_t const padding_needed = (alignment - (current_size % alignment)) % alignment; for (size_t i = 0; i < padding_needed; ++i) { @@ -169,20 +169,20 @@ void Serializer::add_padding(size_t bytes) { } void Serializer::append_be_uint16(uint16_t value) { - uint16_t be_value = someip_htons(value); - const uint8_t* bytes = reinterpret_cast(&be_value); + const uint16_t be_value = someip_htons(value); + auto bytes = reinterpret_cast(&be_value); buffer_.insert(buffer_.end(), bytes, bytes + sizeof(uint16_t)); } void Serializer::append_be_uint32(uint32_t value) { - uint32_t be_value = someip_htonl(value); - const uint8_t* bytes = reinterpret_cast(&be_value); + const uint32_t be_value = someip_htonl(value); + auto bytes = reinterpret_cast(&be_value); buffer_.insert(buffer_.end(), bytes, bytes + sizeof(uint32_t)); } void Serializer::append_be_uint64(uint64_t value) { // Manual big-endian conversion for macOS compatibility - uint64_t be_value = ((value & 0xFF00000000000000ULL) >> 56) | + const uint64_t be_value = ((value & 0xFF00000000000000ULL) >> 56) | ((value & 0x00FF000000000000ULL) >> 40) | ((value & 0x0000FF0000000000ULL) >> 24) | ((value & 0x000000FF00000000ULL) >> 8) | @@ -190,7 +190,7 @@ void Serializer::append_be_uint64(uint64_t value) { ((value & 0x0000000000FF0000ULL) << 24) | ((value & 0x000000000000FF00ULL) << 40) | ((value & 0x00000000000000FFULL) << 56); - const uint8_t* bytes = reinterpret_cast(&be_value); + auto bytes = reinterpret_cast(&be_value); buffer_.insert(buffer_.end(), bytes, bytes + sizeof(uint64_t)); } @@ -257,7 +257,7 @@ DeserializationResult Deserializer::deserialize_bool() { if (position_ + sizeof(uint8_t) > buffer_.size()) { return DeserializationResult::error(Result::MALFORMED_MESSAGE); } - bool value = buffer_[position_++] != 0x00; + const bool value = buffer_[position_++] != 0x00; return DeserializationResult::success(value); } @@ -270,7 +270,7 @@ DeserializationResult Deserializer::deserialize_uint8() { if (position_ + sizeof(uint8_t) > buffer_.size()) { return DeserializationResult::error(Result::MALFORMED_MESSAGE); } - uint8_t value = buffer_[position_++]; + const uint8_t value = buffer_[position_++]; return DeserializationResult::success(value); } @@ -394,13 +394,13 @@ DeserializationResult Deserializer::deserialize_string() { if (length_result.is_error()) { return DeserializationResult::error(length_result.get_error()); } - uint32_t length = length_result.get_value(); + const uint32_t length = length_result.get_value(); if (position_ + length > buffer_.size()) { return DeserializationResult::error(Result::MALFORMED_MESSAGE); } - auto first = buffer_.begin() + static_cast(position_); + const auto first = buffer_.begin() + static_cast(position_); std::string result(first, first + static_cast(length)); position_ += length; diff --git a/src/someip/types.cpp b/src/someip/types.cpp index 31fa0146df..13cfa188da 100644 --- a/src/someip/types.cpp +++ b/src/someip/types.cpp @@ -24,7 +24,7 @@ namespace someip { /** @implements REQ_ARCH_001 */ std::string to_string(MessageType type) { - static const std::unordered_map type_strings = { + static const std::unordered_map TYPE_STRINGS = { {MessageType::REQUEST, "REQUEST"}, {MessageType::REQUEST_NO_RETURN, "REQUEST_NO_RETURN"}, {MessageType::NOTIFICATION, "NOTIFICATION"}, @@ -38,12 +38,12 @@ std::string to_string(MessageType type) { {MessageType::TP_NOTIFICATION, "TP_NOTIFICATION"} }; - auto it = type_strings.find(type); - return (it != type_strings.end()) ? it->second : "UNKNOWN_MESSAGE_TYPE"; + const auto it = TYPE_STRINGS.find(type); + return (it != TYPE_STRINGS.end()) ? it->second : "UNKNOWN_MESSAGE_TYPE"; } std::string to_string(ReturnCode code) { - static const std::unordered_map code_strings = { + static const std::unordered_map CODE_STRINGS = { {ReturnCode::E_OK, "E_OK"}, {ReturnCode::E_NOT_OK, "E_NOT_OK"}, {ReturnCode::E_UNKNOWN_SERVICE, "E_UNKNOWN_SERVICE"}, @@ -62,8 +62,8 @@ std::string to_string(ReturnCode code) { {ReturnCode::E_E2E_NO_NEW_DATA, "E_E2E_NO_NEW_DATA"} }; - auto it = code_strings.find(code); - return (it != code_strings.end()) ? it->second : "UNKNOWN_RETURN_CODE"; + const auto it = CODE_STRINGS.find(code); + return (it != CODE_STRINGS.end()) ? it->second : "UNKNOWN_RETURN_CODE"; } bool is_request(MessageType type) { diff --git a/src/tp/tp_segmenter.cpp b/src/tp/tp_segmenter.cpp index 8e83afa72a..708d504fce 100644 --- a/src/tp/tp_segmenter.cpp +++ b/src/tp/tp_segmenter.cpp @@ -14,6 +14,7 @@ #include "tp/tp_segmenter.h" #include "someip/message.h" #include +#include #include namespace someip::tp { @@ -82,7 +83,7 @@ TpResult TpSegmenter::create_multi_segments(const Message& message, const std::vector& payload, std::vector& segments) { - uint32_t const total_length = static_cast(payload.size()); + auto const total_length = static_cast(payload.size()); uint16_t payload_offset = 0; // Offset into the payload data uint8_t const sequence_number = next_sequence_number_; @@ -145,7 +146,7 @@ TpResult TpSegmenter::create_multi_segments(const Message& message, uint32_t const segment_capacity = config_.max_segment_size > 4 ? config_.max_segment_size - 4 : 0; - uint16_t const payload_size = static_cast( + auto const payload_size = static_cast( std::min(segment_capacity, remaining_bytes)); // Create segment with TP header @@ -218,14 +219,14 @@ void TpSegmenter::serialize_tp_header(std::vector& payload, uint32_t const tp_header = (offset_units << 4) | (more_segments ? 0x01 : 0x00); // Serialize in big-endian - uint8_t header_bytes[4]; + std::array header_bytes{}; header_bytes[0] = (tp_header >> 24) & 0xFF; header_bytes[1] = (tp_header >> 16) & 0xFF; header_bytes[2] = (tp_header >> 8) & 0xFF; header_bytes[3] = tp_header & 0xFF; // Insert TP header after SOME/IP header (16 bytes) - payload.insert(payload.begin() + 16, header_bytes, header_bytes + 4); + payload.insert(payload.begin() + 16, header_bytes.begin(), header_bytes.end()); } /** diff --git a/src/transport/tcp_transport.cpp b/src/transport/tcp_transport.cpp index 46c6ad175e..f213dbf102 100644 --- a/src/transport/tcp_transport.cpp +++ b/src/transport/tcp_transport.cpp @@ -15,6 +15,7 @@ #include "platform/net.h" #include "platform/memory.h" #include "common/result.h" +#include #include #include #include @@ -69,10 +70,10 @@ Result TcpTransport::send_message(const Message& message, const Endpoint& /*endp } // Serialize message - std::vector data = message.serialize(); + const std::vector data = message.serialize(); // Send data - Result result = send_data(connection_.socket_fd, data); + const Result result = send_data(connection_.socket_fd, data); if (result == Result::SUCCESS) { connection_.update_activity(); } @@ -203,7 +204,7 @@ someip_socket_t TcpTransport::accept_connection() { FD_SET(listen_socket_fd_, &read_fds); struct timeval tv = {0, 100000}; // 100ms - int sel = someip_select(static_cast(listen_socket_fd_) + 1, &read_fds, nullptr, nullptr, &tv); + const int sel = someip_select(static_cast(listen_socket_fd_) + 1, &read_fds, nullptr, nullptr, &tv); if (sel <= 0) { return SOMEIP_INVALID_SOCKET; } @@ -329,7 +330,7 @@ Result TcpTransport::connect_internal(const Endpoint& endpoint) { if (connect_result > 0) { int error = 0; socklen_t len = sizeof(error); - int gso_ret = someip_getsockopt(connection_.socket_fd, SOL_SOCKET, SO_ERROR, &error, &len); + const int gso_ret = someip_getsockopt(connection_.socket_fd, SOL_SOCKET, SO_ERROR, &error, &len); if (gso_ret == 0 && error == 0) { connection_.state = TcpConnectionState::CONNECTED; @@ -442,8 +443,8 @@ void TcpTransport::receive_loop() { void TcpTransport::connection_monitor_loop() { while (running_) { if (is_connected()) { - auto now = std::chrono::steady_clock::now(); - auto time_since_activity = std::chrono::duration_cast( + const auto now = std::chrono::steady_clock::now(); + const auto time_since_activity = std::chrono::duration_cast( now - connection_.last_activity); if (time_since_activity > std::chrono::minutes(5)) { @@ -486,13 +487,13 @@ Result TcpTransport::send_data(someip_socket_t socket_fd, const std::vector& data) { // Respect maximum receive buffer size from config - size_t max_chunk_size = std::min(static_cast(4096), config_.max_receive_buffer - data.size()); + const size_t max_chunk_size = std::min(static_cast(4096), config_.max_receive_buffer - data.size()); if (max_chunk_size == 0) { return Result::BUFFER_OVERFLOW; // Already at buffer limit } - uint8_t buffer[4096]; - ssize_t received = someip_recv(socket_fd, buffer, max_chunk_size, 0); + std::array buffer{}; + const ssize_t received = someip_recv(socket_fd, buffer.data(), max_chunk_size, 0); if (received < 0) { int const err = someip_socket_errno(); @@ -504,7 +505,7 @@ Result TcpTransport::receive_data(someip_socket_t socket_fd, std::vector& buffer, Messa // Parse message length from header (bytes 4-7 in big-endian) // Length field contains length from client_id to end of message - uint32_t length_from_client_id = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; + const uint32_t length_from_client_id = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; if (length_from_client_id < 8 || length_from_client_id > MAX_MESSAGE_SIZE) { // Invalid message length - try to resync by skipping this potential header @@ -556,15 +557,15 @@ bool TcpTransport::parse_message_from_buffer(std::vector& buffer, Messa } // Total message size = message_id(4) + length(4) + length_from_client_id - size_t total_message_size = 8 + length_from_client_id; + const size_t total_message_size = 8 + length_from_client_id; if (buffer.size() < total_message_size) { return false; // Need more data } // Extract message data - auto msg_end = buffer.begin() + static_cast(total_message_size); - std::vector message_data(buffer.begin(), msg_end); + const auto msg_end = buffer.begin() + static_cast(total_message_size); + const std::vector message_data(buffer.begin(), msg_end); buffer.erase(buffer.begin(), msg_end); // Parse message diff --git a/src/transport/udp_transport.cpp b/src/transport/udp_transport.cpp index 990ee3023f..bef2279fef 100644 --- a/src/transport/udp_transport.cpp +++ b/src/transport/udp_transport.cpp @@ -15,6 +15,7 @@ #include "platform/net.h" #include "platform/memory.h" #include "common/result.h" +#include #include #include @@ -60,7 +61,7 @@ Result UdpTransport::send_message(const Message& message, const Endpoint& endpoi } // Serialize message - std::vector data = message.serialize(); + const std::vector data = message.serialize(); if (data.size() > MAX_UDP_PAYLOAD) { return Result::BUFFER_OVERFLOW; @@ -401,11 +402,11 @@ Result UdpTransport::send_data(const std::vector& data, const Endpoint& return Result::NOT_CONNECTED; } - sockaddr_in dest_addr = create_sockaddr(endpoint); + const sockaddr_in dest_addr = create_sockaddr(endpoint); ssize_t sent = 0; do { sent = someip_sendto(socket_fd_, data.data(), data.size(), 0, - reinterpret_cast(&dest_addr), + reinterpret_cast(&dest_addr), sizeof(dest_addr)); } while (sent < 0 && someip_socket_errno() == SOMEIP_EINTR); @@ -463,10 +464,10 @@ sockaddr_in UdpTransport::create_sockaddr(const Endpoint& endpoint) const { } Endpoint UdpTransport::sockaddr_to_endpoint(const sockaddr_in& addr) const { - char ip_str[INET_ADDRSTRLEN]; - someip_inet_ntop(AF_INET, &addr.sin_addr, ip_str, sizeof(ip_str)); + std::array ip_str{}; + someip_inet_ntop(AF_INET, &addr.sin_addr, ip_str.data(), ip_str.size()); - return Endpoint(ip_str, ntohs(addr.sin_port), TransportProtocol::UDP); + return Endpoint(ip_str.data(), ntohs(addr.sin_port), TransportProtocol::UDP); } bool UdpTransport::is_multicast_address(const std::string& address) const {