1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// Copyright (C) 2014-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 <vsomeip/constants.hpp>
#include <vsomeip/internal/logger.hpp>
#include <vsomeip/runtime.hpp>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include "../include/channel_impl.hpp"
#include "../include/connector_impl.hpp"
#include "../include/defines.hpp"
#include "../../configuration/include/trace.hpp"
#include "../../utility/include/byteorder.hpp"
#ifdef ANDROID
#include <utils/Log.h>
#ifdef ALOGI
#undef ALOGI
#endif
#define ALOGI(LOG_TAG, ...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
#ifndef LOGE
#define LOGI ALOGI
#endif
#endif
namespace vsomeip_v3 {
namespace trace {
const char *VSOMEIP_TC_DEFAULT_CHANNEL_ID = "TC";
std::shared_ptr<connector_impl> connector_impl::get() {
static std::shared_ptr<connector_impl> instance = std::make_shared<connector_impl>();
return instance;
}
connector_impl::connector_impl() :
is_enabled_(false),
is_sd_enabled_(false) {
channels_[VSOMEIP_TC_DEFAULT_CHANNEL_ID]
= std::make_shared<channel_impl>(VSOMEIP_TC_DEFAULT_CHANNEL_ID,
VSOMEIP_TC_DEFAULT_CHANNEL_NAME);
#ifdef USE_DLT
#ifndef ANDROID
std::shared_ptr<DltContext> its_default_context
= std::make_shared<DltContext>();
contexts_[VSOMEIP_TC_DEFAULT_CHANNEL_ID] = its_default_context;
DLT_REGISTER_CONTEXT_LL_TS(*(its_default_context.get()),
VSOMEIP_TC_DEFAULT_CHANNEL_ID, VSOMEIP_TC_DEFAULT_CHANNEL_NAME,
DLT_LOG_INFO, DLT_TRACE_STATUS_ON);
#endif
#endif
}
connector_impl::~connector_impl() {
reset();
}
void connector_impl::configure(const std::shared_ptr<cfg::trace> &_configuration) {
if (_configuration) {
is_enabled_ = _configuration->is_enabled_;
is_sd_enabled_ = _configuration->is_sd_enabled_;
}
if (is_enabled_) { // No need to create filters if tracing is disabled!
for (auto &its_channel : _configuration->channels_) {
if (!add_channel(its_channel->id_, its_channel->name_)) {
VSOMEIP_ERROR << "Channel " << its_channel->id_
<< " has multiple definitions.";
}
}
for (auto &its_filter : _configuration->filters_) {
for (auto &its_channel : its_filter->channels_) {
auto its_channel_ptr = get_channel_impl(its_channel);
if (its_channel_ptr) {
if (its_filter->is_range_) {
its_channel_ptr->add_filter(its_filter->matches_[0],
its_filter->matches_[1], its_filter->ftype_);
} else {
its_channel_ptr->add_filter(its_filter->matches_,
its_filter->ftype_);
}
}
}
}
}
VSOMEIP_INFO << "vsomeip tracing "
<< (is_enabled_ ? "enabled " : "not enabled. ")
<< ". vsomeip service discovery tracing "
<< (is_sd_enabled_ ? "enabled " : "not enabled. ");
}
void connector_impl::reset() {
#ifdef USE_DLT
#ifndef ANDROID
std::lock_guard<std::mutex> its_contexts_lock(contexts_mutex_);
contexts_.clear();
#endif
#endif
// reset to default
std::lock_guard<std::mutex> its_lock_channels(channels_mutex_);
channels_.clear();
}
void connector_impl::set_enabled(const bool _enabled) {
is_enabled_ = _enabled;
}
bool connector_impl::is_enabled() const {
return is_enabled_;
}
void connector_impl::set_sd_enabled(const bool _sd_enabled) {
is_sd_enabled_ = _sd_enabled;
}
bool connector_impl::is_sd_enabled() const {
return is_sd_enabled_;
}
bool connector_impl::is_sd_message(const byte_t *_data, uint16_t _data_size) const {
if (VSOMEIP_METHOD_POS_MAX < _data_size) {
return (_data[VSOMEIP_SERVICE_POS_MIN] == 0xFF && _data[VSOMEIP_SERVICE_POS_MAX] == 0xFF &&
_data[VSOMEIP_METHOD_POS_MIN] == 0x81 && _data[VSOMEIP_METHOD_POS_MAX] == 0x00);
}
return false;
}
std::shared_ptr<channel> connector_impl::add_channel(
const trace_channel_t &_id, const std::string &_name) {
std::lock_guard<std::mutex> its_channels_lock(channels_mutex_);
// check whether we already know the requested channel
if (channels_.find(_id) != channels_.end())
return nullptr;
// create new channel
std::shared_ptr<channel_impl> its_channel
= std::make_shared<channel_impl>(_id, _name);
// add channel
channels_[_id] = its_channel;
// register context
#ifdef USE_DLT
#ifndef ANDROID
std::lock_guard<std::mutex> its_contexts_lock(contexts_mutex_);
std::shared_ptr<DltContext> its_context = std::make_shared<DltContext>();
contexts_[_id] = its_context;
DLT_REGISTER_CONTEXT_LL_TS(*(its_context.get()), _id.c_str(), _name.c_str(),
DLT_LOG_INFO, DLT_TRACE_STATUS_ON);
#endif
#endif
return its_channel;
}
bool connector_impl::remove_channel(const trace_channel_t &_id) {
if (_id == VSOMEIP_TC_DEFAULT_CHANNEL_ID) {
// the default channel can not be removed
return false;
}
std::lock_guard<std::mutex> its_channels_lock(channels_mutex_);
bool has_removed = (channels_.erase(_id) == 1);
if (has_removed) {
// unregister context
#ifdef USE_DLT
#ifndef ANDROID
std::lock_guard<std::mutex> its_contexts_lock(contexts_mutex_);
auto its_context = contexts_.find(_id);
if (its_context != contexts_.end()) {
DLT_UNREGISTER_CONTEXT(*(its_context->second.get()));
}
#endif
#endif
}
return true;
}
std::shared_ptr<channel> connector_impl::get_channel(const std::string &_id) const {
std::lock_guard<std::mutex> its_channels_lock(channels_mutex_);
auto its_channel = channels_.find(_id);
return (its_channel != channels_.end() ? its_channel->second : nullptr);
}
std::shared_ptr<channel_impl> connector_impl::get_channel_impl(const std::string &_id) const {
std::lock_guard<std::mutex> its_channels_lock(channels_mutex_);
auto its_channel = channels_.find(_id);
return (its_channel != channels_.end() ? its_channel->second : nullptr);
}
void connector_impl::trace(const byte_t *_header, uint16_t _header_size,
const byte_t *_data, uint32_t _data_size) {
#if USE_DLT
if (!is_enabled_)
return;
if (_data_size == 0)
return; // no data
// Clip
const uint16_t its_data_size
= uint16_t(_data_size > USHRT_MAX ? USHRT_MAX : _data_size);
if (is_sd_message(_data, its_data_size) && !is_sd_enabled_)
return; // tracing of service discovery messages is disabled!
service_t its_service = VSOMEIP_BYTES_TO_WORD(
_data[VSOMEIP_SERVICE_POS_MIN],
_data[VSOMEIP_SERVICE_POS_MAX]);
// Instance is not part of the SOME/IP header, read it from the trace
// header
instance_t its_instance = VSOMEIP_BYTES_TO_WORD(
_header[VSOMEIP_TC_INSTANCE_POS_MIN],
_header[VSOMEIP_TC_INSTANCE_POS_MAX]);
method_t its_method = VSOMEIP_BYTES_TO_WORD(
_data[VSOMEIP_METHOD_POS_MIN],
_data[VSOMEIP_METHOD_POS_MAX]);
// Forward to channel if the filter set of the channel allows
std::lock_guard<std::mutex> its_channels_lock(channels_mutex_);
#ifndef ANDROID
std::lock_guard<std::mutex> its_contexts_lock(contexts_mutex_);
#endif
for (auto its_channel : channels_) {
auto ftype = its_channel.second->matches(its_service, its_instance, its_method);
if (ftype.first) {
#ifndef ANDROID
auto its_context = contexts_.find(its_channel.second->get_id());
if (its_context != contexts_.end()) {
try {
if (ftype.second) {
//Positive Filter
DLT_TRACE_NETWORK_SEGMENTED(*(its_context->second.get()),
DLT_NW_TRACE_IPC,
_header_size, static_cast<void *>(const_cast<byte_t *>(_header)),
its_data_size, static_cast<void *>(const_cast<byte_t *>(_data)));
} else {
//Header-Only Filter
DLT_TRACE_NETWORK_TRUNCATED(*(its_context->second.get()),
DLT_NW_TRACE_IPC,
_header_size, static_cast<void *>(const_cast<byte_t *>(_header)),
VSOMEIP_FULL_HEADER_SIZE,
static_cast<void *>(const_cast<byte_t *>(_data)));
}
} catch (const std::exception& e) {
VSOMEIP_INFO << "connector_impl::trace: "
<< "Exception caught when trying to log a trace with DLT. "
<< e.what();
}
} else {
// This should never happen!
VSOMEIP_ERROR << "tracing: found channel without DLT context!";
}
#else
std::stringstream ss;
ss << "TC:";
for(int i = 0; i < _header_size; i++) {
ss << ' ' << std::setfill('0') << std::setw(2) << std::hex << int(_header[i]);
}
if (ftype.second)
_data_size = VSOMEIP_FULL_HEADER_SIZE;
for(int i = 0; i < its_data_size; i++) {
ss << ' ' << std::setfill('0') << std::setw(2) << std::hex << int(_data[i]);
}
std::string app = runtime::get_property("LogApplication");
ALOGI(app.c_str(), ss.str().c_str());
#endif
}
}
#else
(void)_header;
(void)_header_size;
(void)_data;
(void)_data_size;
#endif
}
} // namespace trace
} // namespace vsomeip_v3
|