1 // Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6 #include "../../../../../e2e_protection/include/e2e/profile/profile01/protector.hpp"
7 #include <vsomeip/internal/logger.hpp>
8
9 #include <iostream>
10 #include <sstream>
11 #include <string>
12 #include <iomanip>
13
14 namespace vsomeip_v3 {
15 namespace e2e {
16 namespace profile01 {
17
18 /** @req [SWS_E2E_00195] */
protect(e2e_buffer & _buffer,instance_t _instance)19 void protector::protect(e2e_buffer &_buffer, instance_t _instance) {
20
21 (void)_instance;
22
23 std::lock_guard<std::mutex> lock(protect_mutex_);
24
25 if (profile_01::is_buffer_length_valid(config_, _buffer)) {
26 // write the current Counter value in Data
27 write_counter(_buffer);
28
29 // write DataID nibble in Data (E2E_P01_DATAID_NIBBLE) in Data
30 write_data_id(_buffer);
31
32 // compute the CRC over DataID and Data
33 uint8_t computed_crc = profile_01::compute_crc(config_, _buffer);
34 // write CRC in Data
35 write_crc(_buffer, computed_crc);
36
37 // increment the Counter (new value will be used in the next invocation of E2E_P01Protect()),
38 increment_counter();
39 }
40 }
41
42 /** @req [SRS_E2E_08528] */
write_counter(e2e_buffer & _buffer)43 void protector::write_counter(e2e_buffer &_buffer) {
44 if (config_.counter_offset_ % 8 == 0) {
45 // write write counter value into low nibble
46 _buffer[config_.counter_offset_ / 8] =
47 static_cast<uint8_t>((_buffer[config_.counter_offset_ / 8] & 0xF0) | (counter_ & 0x0F));
48 } else {
49 // write counter into high nibble
50 _buffer[config_.counter_offset_ / 8] =
51 static_cast<uint8_t>((_buffer[config_.counter_offset_ / 8] & 0x0F) | ((counter_ << 4) & 0xF0));
52 }
53 }
54
55 /** @req [SRS_E2E_08528] */
write_data_id(e2e_buffer & _buffer)56 void protector::write_data_id(e2e_buffer &_buffer) {
57 if (config_.data_id_mode_ == p01_data_id_mode::E2E_P01_DATAID_NIBBLE) {
58 if (config_.data_id_nibble_offset_ % 8 == 0) {
59 // write low nibble of high byte of Data ID
60 _buffer[config_.data_id_nibble_offset_ / 8] =
61 static_cast<uint8_t>((_buffer[config_.data_id_nibble_offset_ / 8] & 0xF0) | ((config_.data_id_ >> 8) & 0x0F));
62 } else {
63 // write low nibble of high byte of Data ID
64 _buffer[config_.data_id_nibble_offset_ / 8] =
65 static_cast<uint8_t>((_buffer[config_.data_id_nibble_offset_ / 8] & 0x0F) | ((config_.data_id_ >> 4) & 0xF0));
66 }
67 }
68 }
69
70 /** @req [SRS_E2E_08528] */
write_crc(e2e_buffer & _buffer,uint8_t _computed_crc)71 void protector::write_crc(e2e_buffer &_buffer, uint8_t _computed_crc) {
72 _buffer[config_.crc_offset_] = _computed_crc;
73 }
74
75 /** @req [SWS_E2E_00075] */
increment_counter(void)76 void protector::increment_counter(void) {
77 counter_ = static_cast<uint8_t>((counter_ + 1U) % 15);
78 }
79
80 } // namespace profile01
81 } // namespace e2e
82 } // namespace vsomeip_v3
83