summaryrefslogtreecommitdiff
path: root/implementation/utility/src/utility.cpp
blob: a3ed5d105f7ead0c794a72b49a1798685838475c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
299
300
301
302
// 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 <iomanip>

#ifdef _WIN32
    #include <iostream>
    #include <tchar.h>
    #include <intrin.h>
#else
    #include <dlfcn.h>
    #include <signal.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/mman.h>
    #include <thread>
    #include <sstream>
#endif

#include <sys/stat.h>

#include <vsomeip/constants.hpp>
#include <vsomeip/defines.hpp>
#include <vsomeip/internal/logger.hpp>

#include "../include/byteorder.hpp"
#include "../include/utility.hpp"
#include "../../configuration/include/configuration.hpp"

namespace vsomeip_v3 {

std::mutex utility::mutex__;
std::map<std::string, utility::data_t> utility::data__;

utility::data_t::data_t()
    : next_client_(VSOMEIP_CLIENT_UNSET),
#ifdef _WIN32
        lock_handle_(INVALID_HANDLE_VALUE)
#else
        lock_fd_(-1)
#endif
{}

uint64_t utility::get_message_size(const byte_t *_data, size_t _size) {
    uint64_t its_size(0);
    if (VSOMEIP_SOMEIP_HEADER_SIZE <= _size) {
        its_size = VSOMEIP_SOMEIP_HEADER_SIZE
                + VSOMEIP_BYTES_TO_LONG(_data[4], _data[5], _data[6], _data[7]);
    }
    return (its_size);
}

uint32_t utility::get_payload_size(const byte_t *_data, uint32_t _size) {
    uint32_t its_size(0);
    if (VSOMEIP_SOMEIP_HEADER_SIZE <= _size) {
        its_size = VSOMEIP_BYTES_TO_LONG(_data[4], _data[5], _data[6], _data[7])
                - VSOMEIP_SOMEIP_HEADER_SIZE;
    }
    return (its_size);
}

bool utility::is_routing_manager(const std::string &_network) {
    // Only the first caller can become routing manager.
    // Therefore, subsequent calls can be immediately answered...
    std::lock_guard<std::mutex> its_lock(mutex__);
    if (data__.find(_network) != data__.end())
        return (false);

    auto r = data__.insert(std::make_pair(_network, data_t()));
    if (!r.second)
        return (false);

#ifdef _WIN32
    wchar_t its_tmp_folder[MAX_PATH];
    if (GetTempPathW(MAX_PATH, its_tmp_folder)) {
        std::wstring its_lockfile(its_tmp_folder);
        std::string its_network(_network + ".lck");
        its_lockfile.append(its_network.begin(), its_network.end());
        r.first->second.lock_handle_ = CreateFileW(its_lockfile.c_str(), GENERIC_READ, 0, NULL, CREATE_NEW, 0, NULL);
        if (r.first->second.lock_handle_ == INVALID_HANDLE_VALUE) {
            VSOMEIP_ERROR << __func__ << ": CreateFileW failed: " << std::hex << GetLastError();
        }
    } else {
        VSOMEIP_ERROR << __func__ << ": Could not get temp folder: "
                << std::hex << GetLastError();
        r.first->second.lock_handle_ = INVALID_HANDLE_VALUE;
    }

    return (r.first->second.lock_handle_ != INVALID_HANDLE_VALUE);
#else
    std::string its_base_path(VSOMEIP_BASE_PATH + _network);
    std::string its_lockfile(its_base_path + ".lck");
    int its_lock_ctrl(-1);

    struct flock its_lock_data = { F_WRLCK, SEEK_SET, 0, 0, 0 };

    r.first->second.lock_fd_ = open(its_lockfile.c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IWGRP);
    if (-1 != r.first->second.lock_fd_) {
        its_lock_data.l_pid = getpid();
        its_lock_ctrl = fcntl(r.first->second.lock_fd_, F_SETLK, &its_lock_data);
    } else {
        VSOMEIP_ERROR << __func__
                << ": Could not open " << its_lockfile << ": " << std::strerror(errno);
    }

    return (its_lock_ctrl != -1);
#endif
}

void utility::remove_lockfile(const std::string &_network) {
    std::lock_guard<std::mutex> its_lock(mutex__);

    auto r = data__.find(_network);
    if (r == data__.end()) // No need to do anything as automatic
        return;

#ifdef _WIN32
    if (r->second.lock_handle_ != INVALID_HANDLE_VALUE) {
        if (CloseHandle(r->second.lock_handle_) == 0) {
            VSOMEIP_ERROR << __func__ << ": CloseHandle failed."
                    << std::hex << GetLastError();
        }
        wchar_t its_tmp_folder[MAX_PATH];
        if (GetTempPathW(MAX_PATH, its_tmp_folder)) {
            std::wstring its_lockfile(its_tmp_folder);
            std::string its_network(_network + ".lck");
            its_lockfile.append(its_network.begin(), its_network.end());
            if (DeleteFileW(its_lockfile.c_str()) == 0) {
                VSOMEIP_ERROR << __func__ << ": DeleteFileW failed: "
                        << std::hex << GetLastError();

            }
        } else {
            VSOMEIP_ERROR << __func__ << ": Could not get temp folder."
                    << std::hex << GetLastError();
        }
    }
#else
    std::string its_base_path(VSOMEIP_BASE_PATH + _network);
    std::string its_lockfile(its_base_path + ".lck");

    if (r->second.lock_fd_ != -1) {
       if (close(r->second.lock_fd_) == -1) {
           VSOMEIP_ERROR << __func__ << ": Could not close lock_fd__"
                   << std::strerror(errno);
       }
    }
    if (remove(its_lockfile.c_str()) == -1) {
        VSOMEIP_ERROR << __func__ << ": Could not remove " << its_lockfile
                << ": " << std::strerror(errno);
    }
#endif
    data__.erase(_network);
}

bool utility::exists(const std::string &_path) {
    struct stat its_stat;
    return (stat(_path.c_str(), &its_stat) == 0);
}

bool utility::is_file(const std::string &_path) {
    struct stat its_stat;
    if (stat(_path.c_str(), &its_stat) == 0) {
        if (its_stat.st_mode & S_IFREG)
            return true;
    }
    return false;
}

bool utility::is_folder(const std::string &_path) {
    struct stat its_stat;
    if (stat(_path.c_str(), &its_stat) == 0) {
        if (its_stat.st_mode & S_IFDIR)
            return true;
    }
    return false;
}

std::string utility::get_base_path(const std::string &_network) {
    return std::string(VSOMEIP_BASE_PATH + _network + "-");
}

client_t
utility::request_client_id(
        const std::shared_ptr<configuration> &_config,
        const std::string &_name, client_t _client) {
    std::lock_guard<std::mutex> its_lock(mutex__);
    static const std::uint16_t its_max_num_clients = get_max_client_number(_config);

    static const std::uint16_t its_diagnosis_mask = _config->get_diagnosis_mask();
    static const std::uint16_t its_client_mask = static_cast<std::uint16_t>(~its_diagnosis_mask);
    static const client_t its_masked_diagnosis_address = static_cast<client_t>(
            (_config->get_diagnosis_address() << 8) & its_diagnosis_mask);
    static const client_t its_biggest_client = its_masked_diagnosis_address | its_client_mask;
    static const client_t its_smallest_client = its_masked_diagnosis_address;

    auto r = data__.find(_config->get_network());
    if (r == data__.end())
        return (VSOMEIP_CLIENT_UNSET);

    if (r->second.next_client_ == VSOMEIP_CLIENT_UNSET) {
        r->second.next_client_ = its_smallest_client;
    }

    if (_client != VSOMEIP_CLIENT_UNSET) { // predefined client identifier
        const auto its_iterator = r->second.used_clients_.find(_client);
        if (its_iterator == r->second.used_clients_.end()) { // unused identifier
            r->second.used_clients_[_client] = _name;
            return (_client);
        } else { // already in use

            // The name matches the assigned name --> return client
            // NOTE: THIS REQUIRES A CONSISTENT CONFIGURATION!!!
            if (its_iterator->second == _name) {
                return (_client);
            }

            VSOMEIP_WARNING << "Requested client identifier "
                    << std::setw(4) << std::setfill('0')
                    << std::hex << _client
                    << " is already used by application \""
                    << its_iterator->second
                    << "\".";
            // intentionally fall through
        }
    }

    if (r->second.next_client_ == its_biggest_client) {
        // start at beginning of client range again when the biggest client was reached
        r->second.next_client_ = its_smallest_client;
    }
    std::uint16_t increase_count = 0;
    do {
        r->second.next_client_ = (r->second.next_client_
                & static_cast<std::uint16_t>(~its_client_mask)) // save diagnosis address bits
                | (static_cast<std::uint16_t>((r->second.next_client_ // set all diagnosis address bits to one
                        | static_cast<std::uint16_t>(~its_client_mask)) + 1u) //  and add one to the result
                                & its_client_mask); // set the diagnosis address bits to zero again
        if (increase_count++ == its_max_num_clients) {
            VSOMEIP_ERROR << __func__ << " no free client IDs left! "
                    "Max amount of possible concurrent active vsomeip "
                    "applications reached ("  << std::dec << r->second.used_clients_.size()
                    << ").";
            return (VSOMEIP_CLIENT_UNSET);
        }
    } while (r->second.used_clients_.find(r->second.next_client_) != r->second.used_clients_.end()
            || _config->is_configured_client_id(r->second.next_client_));

    r->second.used_clients_[r->second.next_client_] = _name;
    return (r->second.next_client_);
}

void
utility::release_client_id(const std::string &_network, client_t _client) {
    std::lock_guard<std::mutex> its_lock(mutex__);
    auto r = data__.find(_network);
    if (r != data__.end())
        r->second.used_clients_.erase(_client);
}

std::set<client_t>
utility::get_used_client_ids(const std::string &_network) {
    std::lock_guard<std::mutex> its_lock(mutex__);
    std::set<client_t> its_used_clients;
    auto r = data__.find(_network);
    if (r != data__.end()) {
        for (const auto& c : r->second.used_clients_)
            its_used_clients.insert(c.first);
    }
    return its_used_clients;
}

void utility::reset_client_ids(const std::string &_network) {
    std::lock_guard<std::mutex> its_lock(mutex__);
    auto r = data__.find(_network);
    if (r != data__.end()) {
        r->second.used_clients_.clear();
        r->second.next_client_ = VSOMEIP_CLIENT_UNSET;
    }
}



std::uint16_t utility::get_max_client_number(
        const std::shared_ptr<configuration> &_config) {
    std::uint16_t its_max_clients(0);
    const int bits_for_clients =
#ifdef _WIN32
            __popcnt(
#else
            __builtin_popcount(
#endif
            static_cast<std::uint16_t>(~_config->get_diagnosis_mask()));
    for (int var = 0; var < bits_for_clients; ++var) {
        its_max_clients = static_cast<std::uint16_t>(its_max_clients | (1 << var));
    }
    return its_max_clients;
}

} // namespace vsomeip_v3