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 #ifndef VSOMEIP_V3_UTILITY_HPP 7 #define VSOMEIP_V3_UTILITY_HPP 8 9 #include <map> 10 #include <set> 11 #include <memory> 12 #include <vector> 13 #include <atomic> 14 15 #ifdef _WIN32 16 #define WIN32_LEAN_AND_MEAN 17 #include <windows.h> 18 #endif 19 20 #include <vsomeip/enumeration_types.hpp> 21 #include <vsomeip/message.hpp> 22 #include "criticalsection.hpp" 23 24 namespace vsomeip_v3 { 25 26 class configuration; 27 28 class utility { 29 public: is_request(std::shared_ptr<message> _message)30 static inline bool is_request(std::shared_ptr<message> _message) { 31 return (_message ? is_request(_message->get_message_type()) : false); 32 } 33 is_request(byte_t _type)34 static inline bool is_request(byte_t _type) { 35 return (is_request(static_cast<message_type_e>(_type))); 36 } 37 is_request(message_type_e _type)38 static inline bool is_request(message_type_e _type) { 39 return (_type < message_type_e::MT_NOTIFICATION); 40 } 41 is_request_no_return(std::shared_ptr<message> _message)42 static inline bool is_request_no_return(std::shared_ptr<message> _message) { 43 return (_message && is_request_no_return(_message->get_message_type())); 44 } 45 is_request_no_return(byte_t _type)46 static inline bool is_request_no_return(byte_t _type) { 47 return (is_request_no_return(static_cast<message_type_e>(_type))); 48 } 49 is_request_no_return(message_type_e _type)50 static inline bool is_request_no_return(message_type_e _type) { 51 return (_type == message_type_e::MT_REQUEST_NO_RETURN 52 || _type == message_type_e::MT_REQUEST_NO_RETURN_ACK); 53 } 54 is_response(byte_t _type)55 static inline bool is_response(byte_t _type) { 56 return is_response(static_cast<message_type_e>(_type)); 57 } 58 is_response(message_type_e _type)59 static inline bool is_response(message_type_e _type) { 60 return _type == message_type_e::MT_RESPONSE; 61 } 62 is_error(byte_t _type)63 static inline bool is_error(byte_t _type) { 64 return is_error(static_cast<message_type_e>(_type)); 65 } 66 is_error(message_type_e _type)67 static inline bool is_error(message_type_e _type) { 68 return _type == message_type_e::MT_ERROR; 69 } 70 is_event(byte_t _data)71 static inline bool is_event(byte_t _data) { 72 return (0x80 & _data) > 0; 73 } 74 is_notification(byte_t _type)75 static inline bool is_notification(byte_t _type) { 76 return (is_notification(static_cast<message_type_e>(_type))); 77 } 78 is_notification(message_type_e _type)79 static inline bool is_notification(message_type_e _type) { 80 return (_type == message_type_e::MT_NOTIFICATION); 81 } 82 83 static uint64_t get_message_size(const byte_t *_data, size_t _size); get_message_size(std::vector<byte_t> & _data)84 static inline uint64_t get_message_size(std::vector<byte_t> &_data) { 85 if (_data.size() > 0) { 86 return (get_message_size(&_data[0], _data.size())); 87 } 88 return 0; 89 } 90 91 static uint32_t get_payload_size(const byte_t *_data, uint32_t _size); 92 93 static bool is_routing_manager(const std::shared_ptr<configuration> &_config); 94 static void remove_lockfile(const std::shared_ptr<configuration> &_config); 95 static bool exists(const std::string &_path); 96 static bool VSOMEIP_IMPORT_EXPORT is_file(const std::string &_path); 97 static bool VSOMEIP_IMPORT_EXPORT is_folder(const std::string &_path); 98 99 static const std::string get_base_path(const std::shared_ptr<configuration> &_config); 100 101 static client_t request_client_id(const std::shared_ptr<configuration> &_config, 102 const std::string &_name, client_t _client); 103 static void release_client_id(client_t _client); 104 static std::set<client_t> get_used_client_ids(); 105 static void reset_client_ids(); 106 is_valid_message_type(message_type_e _type)107 static inline bool is_valid_message_type(message_type_e _type) { 108 return (_type == message_type_e::MT_REQUEST 109 || _type == message_type_e::MT_REQUEST_NO_RETURN 110 || _type == message_type_e::MT_NOTIFICATION 111 || _type == message_type_e::MT_REQUEST_ACK 112 || _type == message_type_e::MT_REQUEST_NO_RETURN_ACK 113 || _type == message_type_e::MT_NOTIFICATION_ACK 114 || _type == message_type_e::MT_RESPONSE 115 || _type == message_type_e::MT_ERROR 116 || _type == message_type_e::MT_RESPONSE_ACK 117 || _type == message_type_e::MT_ERROR_ACK 118 || _type == message_type_e::MT_UNKNOWN); 119 } 120 is_valid_return_code(return_code_e _code)121 static inline bool is_valid_return_code(return_code_e _code) { 122 return (_code == return_code_e::E_OK 123 || _code == return_code_e::E_NOT_OK 124 || _code == return_code_e::E_UNKNOWN_SERVICE 125 || _code == return_code_e::E_UNKNOWN_METHOD 126 || _code == return_code_e::E_NOT_READY 127 || _code == return_code_e::E_NOT_REACHABLE 128 || _code == return_code_e::E_TIMEOUT 129 || _code == return_code_e::E_WRONG_PROTOCOL_VERSION 130 || _code == return_code_e::E_WRONG_INTERFACE_VERSION 131 || _code == return_code_e::E_MALFORMED_MESSAGE 132 || _code == return_code_e::E_WRONG_MESSAGE_TYPE 133 || (static_cast<std::uint8_t>(_code) >= 0x20 134 && static_cast<std::uint8_t>(_code) <= 0x5E)); 135 } 136 137 private: 138 static std::uint16_t get_max_client_number(const std::shared_ptr<configuration> &_config); 139 140 static std::mutex mutex__; 141 static client_t next_client__; 142 static std::map<client_t, std::string> used_clients__; 143 #ifdef _WIN32 144 static HANDLE lock_handle__; 145 #else 146 static int lock_fd__; 147 #endif 148 #ifndef VSOMEIP_ENABLE_CONFIGURATION_OVERLAYS 149 static bool is_checked__; 150 #else 151 static std::set<std::string> is_checked__; 152 #endif 153 }; 154 155 } // namespace vsomeip_v3 156 157 #endif // VSOMEIP_V3_UTILITY_HPP 158