├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── common ├── CMakeLists.txt ├── config.hpp └── utils │ ├── c-args.hpp │ ├── callback-guard.cpp │ ├── callback-guard.hpp │ ├── callback-wrapper.hpp │ ├── ccolor.cpp │ ├── ccolor.hpp │ ├── channel.cpp │ ├── channel.hpp │ ├── counting-map.hpp │ ├── credentials.cpp │ ├── credentials.hpp │ ├── daemon.cpp │ ├── daemon.hpp │ ├── environment.cpp │ ├── environment.hpp │ ├── eventfd.cpp │ ├── eventfd.hpp │ ├── exception.cpp │ ├── exception.hpp │ ├── execute.cpp │ ├── execute.hpp │ ├── fd-utils.cpp │ ├── fd-utils.hpp │ ├── file-wait.cpp │ ├── file-wait.hpp │ ├── fs.cpp │ ├── fs.hpp │ ├── glib-loop.cpp │ ├── glib-loop.hpp │ ├── glib-utils.cpp │ ├── glib-utils.hpp │ ├── img.cpp │ ├── img.hpp │ ├── initctl.cpp │ ├── initctl.hpp │ ├── inotify.cpp │ ├── inotify.hpp │ ├── latch.cpp │ ├── latch.hpp │ ├── make-clean.hpp │ ├── paths.hpp │ ├── same-thread-guard.cpp │ ├── same-thread-guard.hpp │ ├── scoped-daemon.cpp │ ├── scoped-daemon.hpp │ ├── scoped-dir.cpp │ ├── scoped-dir.hpp │ ├── scoped-gerror.cpp │ ├── scoped-gerror.hpp │ ├── signal.cpp │ ├── signal.hpp │ ├── signalfd.cpp │ ├── signalfd.hpp │ ├── smack.cpp │ ├── smack.hpp │ ├── spin-wait-for.hpp │ ├── text.cpp │ ├── text.hpp │ ├── typeinfo.cpp │ ├── typeinfo.hpp │ ├── value-latch.hpp │ ├── vt.cpp │ ├── vt.hpp │ ├── worker.cpp │ └── worker.hpp ├── doc ├── cargo_usage.md ├── custom.css ├── doxygen.cfg ├── footer.html ├── generate_documentation.sh └── header.html ├── libs ├── cargo-fd │ ├── CMakeLists.txt │ ├── cargo-fd.hpp │ ├── internals │ │ ├── fdstore.cpp │ │ ├── fdstore.hpp │ │ ├── from-fdstore-internet-visitor.hpp │ │ ├── from-fdstore-visitor-base.hpp │ │ ├── from-fdstore-visitor.hpp │ │ ├── to-fdstore-internet-visitor.hpp │ │ ├── to-fdstore-visitor-base.hpp │ │ └── to-fdstore-visitor.hpp │ └── libcargo-fd.pc.in ├── cargo-gvariant │ ├── CMakeLists.txt │ ├── cargo-gvariant.hpp │ ├── internals │ │ ├── from-gvariant-visitor.hpp │ │ └── to-gvariant-visitor.hpp │ └── libcargo-gvariant.pc.in ├── cargo-ipc │ ├── CMakeLists.txt │ ├── client.cpp │ ├── client.hpp │ ├── epoll │ │ ├── event-poll.cpp │ │ ├── event-poll.hpp │ │ ├── events.cpp │ │ ├── events.hpp │ │ ├── glib-dispatcher.cpp │ │ ├── glib-dispatcher.hpp │ │ ├── thread-dispatcher.cpp │ │ └── thread-dispatcher.hpp │ ├── exception.hpp │ ├── internals │ │ ├── acceptor.cpp │ │ ├── acceptor.hpp │ │ ├── add-peer-request.hpp │ │ ├── event-queue.hpp │ │ ├── finish-request.hpp │ │ ├── method-request.hpp │ │ ├── processor.cpp │ │ ├── processor.hpp │ │ ├── remove-method-request.hpp │ │ ├── remove-peer-request.hpp │ │ ├── request-queue.hpp │ │ ├── result-builder.hpp │ │ ├── send-result-request.hpp │ │ ├── signal-request.hpp │ │ ├── socket.cpp │ │ └── socket.hpp │ ├── libcargo-ipc.pc.in │ ├── method-result.cpp │ ├── method-result.hpp │ ├── result.hpp │ ├── service.cpp │ ├── service.hpp │ ├── types.cpp │ ├── types.hpp │ ├── unique-id.cpp │ └── unique-id.hpp ├── cargo-json │ ├── CMakeLists.txt │ ├── cargo-json.hpp │ ├── internals │ │ ├── from-json-visitor.hpp │ │ └── to-json-visitor.hpp │ └── libcargo-json.pc.in ├── cargo-sqlite-json │ ├── CMakeLists.txt │ ├── cargo-sqlite-json.hpp │ └── libcargo-sqlite-json.pc.in ├── cargo-sqlite │ ├── CMakeLists.txt │ ├── cargo-sqlite.hpp │ ├── internals │ │ ├── from-kvstore-ignoring-visitor.hpp │ │ ├── from-kvstore-visitor-base.hpp │ │ ├── from-kvstore-visitor.hpp │ │ ├── kvstore-visitor-utils.hpp │ │ ├── kvstore.cpp │ │ ├── kvstore.hpp │ │ └── to-kvstore-visitor.hpp │ ├── libcargo-sqlite.pc.in │ └── sqlite3 │ │ ├── connection.cpp │ │ ├── connection.hpp │ │ ├── statement.cpp │ │ └── statement.hpp ├── cargo-validator │ ├── CMakeLists.txt │ ├── exception.hpp │ ├── internals │ │ ├── is-validable.hpp │ │ └── validator-visitor.hpp │ ├── libcargo-validator.pc.in │ ├── validator.cpp │ └── validator.hpp ├── cargo │ ├── CMakeLists.txt │ ├── exception.hpp │ ├── fields-union.hpp │ ├── fields.hpp │ ├── internals │ │ ├── is-like-tuple.hpp │ │ ├── is-streamable.hpp │ │ ├── is-union.hpp │ │ ├── is-visitable.hpp │ │ └── visit-fields.hpp │ ├── libcargo.pc.in │ └── types.hpp └── logger │ ├── CMakeLists.txt │ ├── backend-file.cpp │ ├── backend-file.hpp │ ├── backend-journal.cpp │ ├── backend-journal.hpp │ ├── backend-null.hpp │ ├── backend-persistent-file.cpp │ ├── backend-persistent-file.hpp │ ├── backend-stderr.cpp │ ├── backend-stderr.hpp │ ├── backend-syslog.cpp │ ├── backend-syslog.hpp │ ├── backend.hpp │ ├── formatter.cpp │ ├── formatter.hpp │ ├── level.cpp │ ├── level.hpp │ ├── libLogger.pc.in │ ├── logger-scope.cpp │ ├── logger-scope.hpp │ ├── logger.cpp │ └── logger.hpp ├── packaging └── cargo.spec └── tests ├── CMakeLists.txt ├── cppcheck ├── cppcheck.sh └── cppcheck.suppress ├── scripts ├── CMakeLists.txt ├── cargo_all_tests.py ├── cargo_launch_test.py └── cargo_test_parser.py └── unit_tests ├── CMakeLists.txt ├── cargo-ipc ├── ut-ipc.cpp ├── ut-socket.cpp └── ut-unique-id.cpp ├── cargo ├── testconfig-example.hpp ├── ut-cargo.cpp ├── ut-dynvisit.cpp ├── ut-kvstore.cpp └── ut-validator.cpp ├── configs ├── CMakeLists.txt ├── systemd │ ├── cargo-socket-test.service.in │ └── cargo-socket-test.socket └── utils │ └── file.txt ├── epoll └── ut-event-poll.cpp ├── log └── ut-logger.cpp ├── socket_test_service ├── socket-test.cpp └── socket-test.hpp ├── ut.cpp ├── ut.hpp └── utils ├── ut-callback-guard.cpp ├── ut-cargs.cpp ├── ut-channel.cpp ├── ut-counting-map.cpp ├── ut-fd-utils.cpp ├── ut-fs.cpp ├── ut-glib-loop.cpp ├── ut-inotify.cpp ├── ut-paths.cpp ├── ut-same-thread-guard.cpp ├── ut-signalfd.cpp ├── ut-text.cpp ├── ut-value-latch.cpp └── ut-worker.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | 9 | # Compiled Static libraries 10 | *.lai 11 | *.la 12 | *.a 13 | 14 | # CMake/Makefile files 15 | *.cmake 16 | CMakeCache.txt 17 | CMakeFiles 18 | Makefile 19 | 20 | # Others 21 | doc/html 22 | -------------------------------------------------------------------------------- /common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Pawel Kubik (p.kubik@samsung.com) 18 | # 19 | 20 | PROJECT(cargo-utils) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the libcargo-utils...") 24 | FILE(GLOB_RECURSE SRCS *.cpp *.hpp) 25 | 26 | ## Setup target ################################################################ 27 | ADD_LIBRARY(${PROJECT_NAME} STATIC ${SRCS}) 28 | 29 | FIND_PACKAGE (Boost REQUIRED COMPONENTS system filesystem) 30 | PKG_CHECK_MODULES(CARGO_UTILS_DEPS REQUIRED glib-2.0) 31 | 32 | INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${LIBS_FOLDER}) 33 | INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIRS} ${CARGO_UTILS_DEPS_INCLUDE_DIRS}) 34 | 35 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES} ${CARGO_UTILS_DEPS_LIBRARIES}) 36 | 37 | ## Install ##################################################################### 38 | INSTALL(TARGETS ${PROJECT_NAME} 39 | DESTINATION ${LIB_INSTALL_DIR} 40 | COMPONENT DevelopmentLibraries) 41 | 42 | INSTALL(DIRECTORY . DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME} 43 | FILES_MATCHING PATTERN "*.hpp" 44 | PATTERN "CMakeFiles" EXCLUDE) 45 | -------------------------------------------------------------------------------- /common/utils/callback-guard.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Callback guard 23 | */ 24 | 25 | #ifndef COMMON_UTILS_CALLBACK_GUARD_HPP 26 | #define COMMON_UTILS_CALLBACK_GUARD_HPP 27 | 28 | #include 29 | 30 | 31 | namespace utils { 32 | 33 | /** 34 | * Callback guard. 35 | * An utility class to control and/or monitor callback lifecycle. 36 | */ 37 | class CallbackGuard { 38 | public: 39 | typedef std::shared_ptr Tracker; 40 | 41 | /** 42 | * Creates a guard. 43 | */ 44 | CallbackGuard(); 45 | 46 | /** 47 | * Waits for all trackers. 48 | */ 49 | ~CallbackGuard(); 50 | 51 | /** 52 | * Creates a tracker. 53 | */ 54 | Tracker spawn() const; 55 | 56 | /** 57 | * Gets trackers count 58 | */ 59 | long getTrackersCount() const; 60 | 61 | /** 62 | * Wait for all trackers. 63 | */ 64 | bool waitForTrackers(const unsigned int timeoutMs); 65 | private: 66 | class SharedState; 67 | std::shared_ptr mSharedState; 68 | 69 | CallbackGuard(const CallbackGuard&) = delete; 70 | CallbackGuard& operator=(const CallbackGuard&) = delete; 71 | }; 72 | 73 | } // namespace utils 74 | 75 | 76 | #endif // COMMON_UTILS_CALLBACK_GUARD_HPP 77 | -------------------------------------------------------------------------------- /common/utils/ccolor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Dariusz Michaluk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Dariusz Michaluk (d.michaluk@samsung.com) 22 | * @brief Console colors utility 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/ccolor.hpp" 28 | 29 | #include 30 | 31 | namespace utils { 32 | 33 | std::string getConsoleEscapeSequence(Attributes attr, Color color) 34 | { 35 | char command[10]; 36 | 37 | // Command is the control command to the terminal 38 | snprintf(command, sizeof(command), "%c[%u;%um", 0x1B, (unsigned int)attr, (unsigned int)color); 39 | return std::string(command); 40 | } 41 | 42 | } // namespace utils 43 | -------------------------------------------------------------------------------- /common/utils/ccolor.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Dariusz Michaluk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Dariusz Michaluk (d.michaluk@samsung.com) 22 | * @brief Console colors utility 23 | */ 24 | 25 | #ifndef COMMON_UTILS_CCOLOR_HPP 26 | #define COMMON_UTILS_CCOLOR_HPP 27 | 28 | #include 29 | 30 | namespace utils { 31 | 32 | enum class Color : unsigned int { 33 | DEFAULT = 0, 34 | BLACK = 30, 35 | RED = 31, 36 | GREEN = 32, 37 | YELLOW = 33, 38 | BLUE = 34, 39 | MAGENTA = 35, 40 | CYAN = 36, 41 | WHITE = 37 42 | }; 43 | 44 | enum class Attributes : unsigned int { 45 | DEFAULT = 0, 46 | BOLD = 1 47 | }; 48 | 49 | std::string getConsoleEscapeSequence(Attributes attr, Color color); 50 | 51 | } // namespace utils 52 | 53 | #endif // COMMON_UTILS_CCOLOR_HPP 54 | -------------------------------------------------------------------------------- /common/utils/counting-map.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Counting map 23 | */ 24 | 25 | #ifndef COMMON_UTILS_COUNTING_MAP_HPP 26 | #define COMMON_UTILS_COUNTING_MAP_HPP 27 | 28 | #include 29 | 30 | namespace utils { 31 | 32 | 33 | /** 34 | * Structure used to count elements. 35 | * It's like multiset + count but is more efficient. 36 | */ 37 | template 38 | class CountingMap { 39 | public: 40 | size_t increment(const Key& key) 41 | { 42 | auto res = mMap.insert(typename Map::value_type(key, 1)); 43 | if (!res.second) { 44 | ++res.first->second; 45 | } 46 | return res.first->second; 47 | } 48 | 49 | size_t decrement(const Key& key) 50 | { 51 | auto it = mMap.find(key); 52 | if (it == mMap.end()) { 53 | return 0; 54 | } 55 | if (--it->second == 0) { 56 | mMap.erase(it); 57 | return 0; 58 | } 59 | return it->second; 60 | } 61 | 62 | void clear() 63 | { 64 | mMap.clear(); 65 | } 66 | 67 | size_t get(const Key& key) const 68 | { 69 | auto it = mMap.find(key); 70 | return it == mMap.end() ? 0 : it->second; 71 | } 72 | 73 | bool empty() const 74 | { 75 | return mMap.empty(); 76 | } 77 | private: 78 | typedef std::unordered_map Map; 79 | Map mMap; 80 | }; 81 | 82 | 83 | } // namespace utils 84 | 85 | 86 | #endif // COMMON_UTILS_COUNTING_MAP_HPP 87 | -------------------------------------------------------------------------------- /common/utils/credentials.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Maciej Karpiuk (m.karpiuk2@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Maciej Karpiuk (m.karpiuk2@samsung.com) 22 | * @brief Credential management related functions 23 | */ 24 | 25 | #include "utils/credentials.hpp" 26 | #include "utils/exception.hpp" 27 | #include "logger/logger.hpp" 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | namespace utils { 34 | 35 | // ------------------- syscall wrappers ------------------- 36 | void setgroups(const std::vector& gids) 37 | { 38 | if (::setgroups(gids.size(), gids.data()) == -1) { 39 | THROW_EXCEPTION(UtilsException, "Error in setgroups()", errno); 40 | } 41 | } 42 | 43 | void setregid(const gid_t rgid, const gid_t egid) 44 | { 45 | if (::setregid(rgid, egid) == -1) { 46 | THROW_EXCEPTION(UtilsException, "Error in setregid()", errno); 47 | } 48 | } 49 | 50 | void setreuid(const uid_t ruid, const uid_t euid) 51 | { 52 | if (::setreuid(ruid, euid) == -1) { 53 | THROW_EXCEPTION(UtilsException, "Error in setreuid()", errno); 54 | } 55 | } 56 | 57 | pid_t setsid() 58 | { 59 | pid_t pid = ::setsid(); 60 | if (pid == -1) { 61 | THROW_EXCEPTION(UtilsException, "Error in setsid()", errno); 62 | } 63 | return pid; 64 | } 65 | 66 | } // namespace utils 67 | -------------------------------------------------------------------------------- /common/utils/credentials.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Maciej Karpiuk (m.karpiuk2@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Maciej Karpiuk (m.karpiuk2@samsung.com) 22 | * @brief Credential management related functions 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | namespace utils { 29 | 30 | // ------------------- syscall wrappers ------------------- 31 | // Throw exception on error 32 | void setgroups(const std::vector& groups); 33 | void setregid(const gid_t rgid, const gid_t egid); 34 | void setreuid(const uid_t ruid, const uid_t euid); 35 | pid_t setsid(); 36 | 37 | 38 | } // namespace utils 39 | -------------------------------------------------------------------------------- /common/utils/daemon.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Dariusz Michaluk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Dariusz Michaluk 22 | * @brief Run a process as a daemon 23 | */ 24 | 25 | #ifndef COMMON_UTILS_DAEMON_HPP 26 | #define COMMON_UTILS_DAEMON_HPP 27 | 28 | namespace utils { 29 | 30 | bool daemonize(); 31 | 32 | } // namespace utils 33 | 34 | #endif // COMMON_UTILS_DAEMON_HPP 35 | -------------------------------------------------------------------------------- /common/utils/environment.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Michal Witanowski 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Michal Witanowski (m.witanowski@samsung.com) 22 | * @brief Declaration of environment setup routines that requires root privileges 23 | */ 24 | 25 | #ifndef COMMON_UTILS_ENVIRONMENT_HPP 26 | #define COMMON_UTILS_ENVIRONMENT_HPP 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | namespace utils { 35 | 36 | 37 | /** 38 | * Set supplementary groups to the current process. 39 | */ 40 | bool setSuppGroups(const std::vector& groups); 41 | 42 | /** 43 | * Set effective and permitted capabilities on the current process and drop root privileges. 44 | */ 45 | bool dropRoot(uid_t uid, gid_t gid, const std::vector& caps); 46 | 47 | /** 48 | * Launch binary as root user 49 | * 50 | * This function forks, sets UID 0 to child process and calls binary. 51 | */ 52 | bool launchAsRoot(const std::vector& argv); 53 | 54 | /** 55 | * Join to namespace 56 | */ 57 | bool joinToNs(int nsPid, int ns); 58 | 59 | /** 60 | * Pass file descriptor from namespace of some process 61 | */ 62 | int passNamespacedFd(int nsPid, int ns, const std::function& fdFactory); 63 | 64 | } // namespace utils 65 | 66 | 67 | #endif // COMMON_UTILS_ENVIRONMENT_HPP 68 | -------------------------------------------------------------------------------- /common/utils/eventfd.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Linux socket wrapper 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/eventfd.hpp" 28 | #include "utils/exception.hpp" 29 | #include "utils/fd-utils.hpp" 30 | #include "logger/logger.hpp" 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | namespace utils { 38 | 39 | EventFD::EventFD() 40 | { 41 | mFD = ::eventfd(0, EFD_SEMAPHORE | EFD_CLOEXEC); 42 | if (mFD == -1) { 43 | THROW_EXCEPTION(EventFDException, "Error in eventfd", errno); 44 | } 45 | } 46 | 47 | EventFD::~EventFD() 48 | { 49 | utils::close(mFD); 50 | } 51 | 52 | int EventFD::getFD() const 53 | { 54 | return mFD; 55 | } 56 | 57 | void EventFD::send() 58 | { 59 | const std::uint64_t toSend = 1; 60 | utils::write(mFD, &toSend, sizeof(toSend)); 61 | } 62 | 63 | void EventFD::receive() 64 | { 65 | std::uint64_t readBuffer; 66 | utils::read(mFD, &readBuffer, sizeof(readBuffer)); 67 | } 68 | 69 | 70 | } // namespace utils 71 | -------------------------------------------------------------------------------- /common/utils/eventfd.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Eventfd wrapper 23 | */ 24 | 25 | #ifndef COMMON_UTILS_EVENTFD_HPP 26 | #define COMMON_UTILS_EVENTFD_HPP 27 | 28 | namespace utils { 29 | 30 | class EventFD { 31 | public: 32 | 33 | EventFD(); 34 | virtual ~EventFD(); 35 | 36 | EventFD(const EventFD& eventfd) = delete; 37 | EventFD& operator=(const EventFD&) = delete; 38 | 39 | /** 40 | * @return event's file descriptor. 41 | */ 42 | int getFD() const; 43 | 44 | /** 45 | * Send an event of a given value 46 | */ 47 | void send(); 48 | 49 | /** 50 | * Receives the signal. 51 | * Blocks if there is no event. 52 | */ 53 | void receive(); 54 | 55 | private: 56 | int mFD; 57 | }; 58 | 59 | } // namespace utils 60 | 61 | #endif // COMMON_UTILS_EVENTFD_HPP 62 | -------------------------------------------------------------------------------- /common/utils/execute.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Execute utils 23 | */ 24 | 25 | #ifndef COMMON_UTILS_EXECUTE_HPP 26 | #define COMMON_UTILS_EXECUTE_HPP 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | namespace utils { 33 | 34 | /** 35 | * Execute function - deprecated 36 | */ 37 | bool executeAndWait(const std::function& func, int& status); 38 | 39 | bool executeAndWait(const std::function& func); 40 | 41 | 42 | /** 43 | * Execute binary 44 | */ 45 | ///@{ 46 | bool executeAndWait(uid_t uid, const char* fname, const char* const* argv, int& status); 47 | 48 | bool executeAndWait(const char* fname, const char* const* argv); 49 | 50 | bool executeAndWait(const char* fname, const char* const* argv, int& status); 51 | 52 | bool executeAndWait(uid_t uid, const std::vector& argv, int& status); 53 | 54 | bool executeAndWait(uid_t uid, const std::vector& argv); 55 | 56 | bool executeAndWait(const std::vector& argv); 57 | ///@} 58 | 59 | /** 60 | * Wait until child processes ends 61 | */ 62 | bool waitPid(pid_t pid, int& status); 63 | 64 | } // namespace utils 65 | 66 | 67 | #endif // COMMON_UTILS_EXECUTE_HPP 68 | -------------------------------------------------------------------------------- /common/utils/file-wait.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Wait for file utility function 23 | */ 24 | 25 | #ifndef COMMON_UTILS_FILE_WAIT_HPP 26 | #define COMMON_UTILS_FILE_WAIT_HPP 27 | 28 | #include 29 | 30 | 31 | namespace utils { 32 | 33 | 34 | //TODO It is used in unit tests now, but it is unclear 35 | // whether the same solution will be used in daemon. 36 | void waitForFile(const std::string& filename, const unsigned int timeoutMs); 37 | 38 | 39 | } // namespace utils 40 | 41 | 42 | #endif // COMMON_UTILS_FILE_WAIT_HPP 43 | -------------------------------------------------------------------------------- /common/utils/glib-loop.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief C++ wrapper of glib main loop 23 | */ 24 | 25 | #ifndef COMMON_UTILS_GLIB_LOOP_HPP 26 | #define COMMON_UTILS_GLIB_LOOP_HPP 27 | 28 | #include "utils/callback-guard.hpp" 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | 35 | namespace utils { 36 | 37 | 38 | /** 39 | * Glib loop controller. Loop is running in separate thread. 40 | */ 41 | class ScopedGlibLoop { 42 | public: 43 | /** 44 | * Starts a loop in separate thread. 45 | */ 46 | ScopedGlibLoop(); 47 | 48 | /** 49 | * Stops loop and waits for a thread. 50 | */ 51 | ~ScopedGlibLoop(); 52 | 53 | private: 54 | std::unique_ptr mLoop; 55 | std::thread mLoopThread; 56 | }; 57 | 58 | /** 59 | * Miscellaneous helpers for the Glib library 60 | */ 61 | class Glib { 62 | public: 63 | /** 64 | * A user provided function that will be called succesively after an interval has passed. 65 | * 66 | * Return true if the callback is supposed to be called further, 67 | * false if the callback is not to be called anymore and be destroyed. 68 | */ 69 | typedef std::function OnTimerEventCallback; 70 | 71 | /** 72 | * Adds a timer event to the glib main loop. 73 | */ 74 | static void addTimerEvent(const unsigned int intervalMs, 75 | const OnTimerEventCallback& callback, 76 | const CallbackGuard& guard); 77 | 78 | private: 79 | static gboolean onTimerEvent(gpointer data); 80 | }; 81 | 82 | } // namespace utils 83 | 84 | 85 | #endif // COMMON_UTILS_GLIB_LOOP_HPP 86 | -------------------------------------------------------------------------------- /common/utils/glib-utils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief C++ wrapper of glib main loop 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/glib-utils.hpp" 28 | #include "utils/callback-wrapper.hpp" 29 | 30 | #include 31 | 32 | namespace utils { 33 | 34 | namespace { 35 | 36 | gboolean onIddle(gpointer data) 37 | { 38 | const VoidCallback& callback = getCallbackFromPointer(data); 39 | callback(); 40 | return FALSE; 41 | } 42 | 43 | } // namespace 44 | 45 | void executeInGlibThread(const VoidCallback& callback, const CallbackGuard& guard) 46 | { 47 | if (!callback) { 48 | return; 49 | } 50 | g_idle_add_full(G_PRIORITY_DEFAULT, 51 | &onIddle, 52 | utils::createCallbackWrapper(callback, guard.spawn()), 53 | &utils::deleteCallbackWrapper); 54 | 55 | } 56 | 57 | 58 | } // namespace utils 59 | -------------------------------------------------------------------------------- /common/utils/glib-utils.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Miscellaneous helpers for the Glib library 23 | */ 24 | 25 | #ifndef COMMON_UTILS_GLIB_UTILS_HPP 26 | #define COMMON_UTILS_GLIB_UTILS_HPP 27 | 28 | #include "utils/callback-guard.hpp" 29 | 30 | namespace utils { 31 | 32 | typedef std::function VoidCallback; 33 | 34 | /** 35 | * Executes a callback in glib thread (adds an iddle event to glib) 36 | */ 37 | void executeInGlibThread(const VoidCallback& callback, const CallbackGuard& guard); 38 | 39 | 40 | } // namespace utils 41 | 42 | #endif // COMMON_UTILS_GLIB_UTILS_HPP 43 | -------------------------------------------------------------------------------- /common/utils/img.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Kostyra 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Kostyra (l.kostyra@samsung.com) 22 | * @brief Image utility functions declaration 23 | */ 24 | 25 | #ifndef COMMON_UTILS_IMG_HPP 26 | #define COMMON_UTILS_IMG_HPP 27 | 28 | namespace utils { 29 | 30 | /** 31 | * Returns string with first free loop device. 32 | */ 33 | bool getFreeLoopDevice(std::string& ret); 34 | 35 | /** 36 | * Mount an ext4 image from file on a given path by using a loop device. 37 | */ 38 | bool mountImage(const std::string& image, const std::string& loopdev, const std::string& path); 39 | 40 | /** 41 | * Umounts previously mounted image. 42 | * This call will also free loop device used to mount specified path. 43 | */ 44 | bool umountImage(const std::string& path, const std::string& loopdev); 45 | 46 | /** 47 | * Mounts an image and copies its contents to dst directory. 48 | */ 49 | bool copyImageContents(const std::string& img, const std::string& dst); 50 | 51 | } // namespace utils 52 | 53 | #endif // COMMON_UTILS_IMG_HPP 54 | -------------------------------------------------------------------------------- /common/utils/initctl.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Api for talking to init via initctl 23 | */ 24 | 25 | #ifndef COMMON_UTILS_INITCTL_HPP 26 | #define COMMON_UTILS_INITCTL_HPP 27 | 28 | 29 | namespace utils { 30 | 31 | enum RunLevel : int { 32 | RUNLEVEL_POWEROFF = 0, 33 | RUNLEVEL_REBOOT = 6 34 | }; 35 | 36 | bool setRunLevel(RunLevel runLevel); 37 | 38 | 39 | } // namespace utils 40 | 41 | 42 | #endif // COMMON_UTILS_INITCTL_HPP 43 | -------------------------------------------------------------------------------- /common/utils/inotify.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Inotify wrapper 23 | */ 24 | 25 | #ifndef COMMON_UTILS_INOTIFY_HPP 26 | #define COMMON_UTILS_INOTIFY_HPP 27 | 28 | #include "cargo-ipc/epoll/event-poll.hpp" 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | 36 | 37 | namespace utils { 38 | 39 | /** 40 | * Inotify monitors a directory and when a specified file or folder 41 | * is created or deleted it calls a corresponding handler. 42 | */ 43 | class Inotify { 44 | public: 45 | typedef std::function Callback; 46 | 47 | Inotify(cargo::ipc::epoll::EventPoll& eventPoll); 48 | virtual ~Inotify(); 49 | 50 | Inotify(const Inotify&) = delete; 51 | Inotify& operator=(const Inotify&) = delete; 52 | 53 | /** 54 | * Add a callback for a specified path 55 | */ 56 | void setHandler(const std::string& path, const uint32_t eventMask, const Callback&& callback); 57 | 58 | /** 59 | * Stop watching the path 60 | */ 61 | void removeHandler(const std::string& path); 62 | 63 | /** 64 | * @return inotify file descriptor 65 | */ 66 | int getFD() const; 67 | 68 | private: 69 | struct Handler { 70 | std::string path; 71 | int watchID; 72 | Callback call; 73 | }; 74 | 75 | typedef std::lock_guard Lock; 76 | std::recursive_mutex mMutex; 77 | 78 | int mFD; 79 | cargo::ipc::epoll::EventPoll& mEventPoll; 80 | std::vector mHandlers; 81 | 82 | void handleInternal(); 83 | void removeHandlerInternal(const std::string& path); 84 | }; 85 | 86 | } // namespace utils 87 | 88 | #endif // COMMON_UTILS_INOTIFY_HPP 89 | -------------------------------------------------------------------------------- /common/utils/latch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Synchronization latch 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/latch.hpp" 28 | 29 | #include 30 | 31 | 32 | namespace utils { 33 | 34 | 35 | Latch::Latch() 36 | : mCount(0) 37 | { 38 | } 39 | 40 | void Latch::set() 41 | { 42 | std::unique_lock lock(mMutex); 43 | ++mCount; 44 | mCondition.notify_one(); 45 | } 46 | 47 | void Latch::wait() 48 | { 49 | waitForN(1); 50 | } 51 | 52 | bool Latch::wait(const unsigned int timeoutMs) 53 | { 54 | return waitForN(1, timeoutMs); 55 | } 56 | 57 | void Latch::waitForN(const unsigned int n) 58 | { 59 | std::unique_lock lock(mMutex); 60 | mCondition.wait(lock, [this, &n] {return mCount >= n;}); 61 | mCount -= n; 62 | } 63 | 64 | bool Latch::waitForN(const unsigned int n, const unsigned int timeoutMs) 65 | { 66 | std::unique_lock lock(mMutex); 67 | if (!mCondition.wait_for(lock, std::chrono::milliseconds(timeoutMs), 68 | [this, &n] {return mCount >= n;})) { 69 | return false; 70 | } 71 | mCount -= n; 72 | return true; 73 | } 74 | 75 | 76 | bool Latch::empty() 77 | { 78 | std::unique_lock lock(mMutex); 79 | return mCount == 0; 80 | } 81 | 82 | 83 | } // namespace utils 84 | -------------------------------------------------------------------------------- /common/utils/make-clean.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Mateusz Malicki 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Mateusz Malicki (m.malicki2@samsung.com) 22 | * @brief Function used to initialize C structures 23 | */ 24 | 25 | #ifndef COMMON_UTILS_MAKE_CLEAN_HPP 26 | #define COMMON_UTILS_MAKE_CLEAN_HPP 27 | 28 | #include 29 | #include 30 | 31 | namespace utils { 32 | 33 | template 34 | void make_clean(T& value) 35 | { 36 | static_assert(std::is_pod::value, "make_clean require trivial and standard-layout"); 37 | std::fill_n(reinterpret_cast(&value), sizeof(value), 0); 38 | } 39 | 40 | template 41 | T make_clean() 42 | { 43 | T value; 44 | make_clean(value); 45 | return value; 46 | } 47 | 48 | } // namespace utils 49 | 50 | 51 | #endif // COMMON_UTILS_MAKE_CLEAN_HPP 52 | -------------------------------------------------------------------------------- /common/utils/same-thread-guard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Same thread guard 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/same-thread-guard.hpp" 28 | 29 | #ifdef ENABLE_SAME_THREAD_GUARD 30 | 31 | #include "logger/logger.hpp" 32 | #include "logger/formatter.hpp" 33 | 34 | namespace utils { 35 | 36 | namespace { 37 | 38 | typedef decltype(logger::LogFormatter::getCurrentThread()) ThreadId; 39 | const ThreadId NOT_SET = 0; 40 | 41 | ThreadId getCurrentThreadId() { 42 | // use the same thread id numbering mechanism as in logger 43 | // to allow analyse accesses in log 44 | return logger::LogFormatter::getCurrentThread(); 45 | } 46 | 47 | } // namespace 48 | 49 | SameThreadGuard::SameThreadGuard() : mThreadId(NOT_SET) 50 | { 51 | static_assert(std::is_same::value, 52 | "thread id type mismatch"); 53 | } 54 | 55 | bool SameThreadGuard::check() 56 | { 57 | const ThreadId thisThreadId = getCurrentThreadId(); 58 | 59 | ThreadId saved = NOT_SET; 60 | if (!mThreadId.compare_exchange_strong(saved, thisThreadId) && saved != thisThreadId) { 61 | LOGE("Detected thread id mismatch; saved: " << saved << "; current: " << thisThreadId); 62 | return false; 63 | } 64 | return true; 65 | } 66 | 67 | void SameThreadGuard::reset() 68 | { 69 | mThreadId.store(NOT_SET); 70 | } 71 | 72 | } // namespace utils 73 | 74 | #endif // ENABLE_SAME_THREAD_GUARD 75 | -------------------------------------------------------------------------------- /common/utils/same-thread-guard.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Same thread guard 23 | */ 24 | 25 | #ifndef COMMON_UTILS_SAME_THREAD_GUARD_HPP 26 | #define COMMON_UTILS_SAME_THREAD_GUARD_HPP 27 | 28 | #ifndef NDEBUG 29 | #define ENABLE_SAME_THREAD_GUARD 30 | #endif 31 | 32 | #ifdef ENABLE_SAME_THREAD_GUARD 33 | #include 34 | #include 35 | #endif 36 | 37 | namespace utils { 38 | 39 | /** 40 | * Same thread guard. 41 | * There are two purposes of this guard: 42 | * - reports invalid assumptions about synchronization needs (only in debug builds) 43 | * - acts as an annotation in the source code about the thread safety 44 | * 45 | * Usage example: 46 | * ASSERT_SAME_THREAD(workerThreadGuard); 47 | */ 48 | class SameThreadGuard { 49 | public: 50 | #ifdef ENABLE_SAME_THREAD_GUARD 51 | # define ASSERT_SAME_THREAD(g) assert(g.check()) 52 | SameThreadGuard(); 53 | 54 | /** 55 | * On the first call it remembers the current thread id. 56 | * On the next call it verifies that current thread is the same as before. 57 | */ 58 | bool check(); 59 | 60 | /** 61 | * Reset thread id 62 | */ 63 | void reset(); 64 | 65 | private: 66 | std::atomic mThreadId; 67 | 68 | #else // ENABLE_SAME_THREAD_GUARD 69 | # define ASSERT_SAME_THREAD(g) 70 | static bool check() {return true;} 71 | static void reset() {} 72 | #endif // ENABLE_SAME_THREAD_GUARD 73 | }; 74 | 75 | } // namespace utils 76 | 77 | 78 | #endif // COMMON_UTILS_SAME_THREAD_GUARD_HPP 79 | -------------------------------------------------------------------------------- /common/utils/scoped-daemon.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Starts external daemon in constructor, stops it in destructor 23 | */ 24 | 25 | #ifndef UNIT_TESTS_UTILS_SCOPED_DAEMON_HPP 26 | #define UNIT_TESTS_UTILS_SCOPED_DAEMON_HPP 27 | 28 | #include 29 | 30 | 31 | namespace utils { 32 | 33 | 34 | /** 35 | * External daemon launcher helper. 36 | */ 37 | class ScopedDaemon { 38 | public: 39 | ScopedDaemon(); 40 | 41 | /** 42 | * Stops a daemon if it is not stopped already. 43 | */ 44 | ~ScopedDaemon(); 45 | 46 | /** 47 | * Starts a daemon. 48 | * @param path daemon path 49 | * @param argv arguments passed to the daemon 50 | * @param useLauncher use additional launcher process 51 | */ 52 | void start(const char* path, const char* const argv[], const bool useLauncher = false); 53 | 54 | /** 55 | * Stops a daemon by sending SIGTERM and waits for a process. 56 | */ 57 | void stop(); 58 | private: 59 | pid_t mPid; 60 | }; 61 | 62 | 63 | } // namespace utils 64 | 65 | 66 | #endif // UNIT_TESTS_UTILS_SCOPED_DAEMON_HPP 67 | -------------------------------------------------------------------------------- /common/utils/scoped-dir.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Create directory in constructor, delete it in destructor 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/scoped-dir.hpp" 28 | #include "utils/fs.hpp" 29 | #include "utils/exception.hpp" 30 | #include "logger/logger.hpp" 31 | 32 | namespace utils { 33 | 34 | ScopedDir::ScopedDir() 35 | { 36 | } 37 | 38 | ScopedDir::ScopedDir(const std::string& path) 39 | { 40 | create(path); 41 | } 42 | 43 | ScopedDir::~ScopedDir() 44 | { 45 | remove(); 46 | } 47 | 48 | void ScopedDir::create(const std::string& path) 49 | { 50 | remove(); 51 | if (!path.empty()) { 52 | mPath = path; 53 | remove(); 54 | utils::createDirs(path); 55 | } 56 | } 57 | 58 | void ScopedDir::remove() 59 | { 60 | if (mPath.empty()) { 61 | return ; 62 | } 63 | try { 64 | utils::removeDir(mPath); 65 | } catch (const UtilsException&) { 66 | LOGE("ScopedDir: can't remove " + mPath); 67 | } 68 | } 69 | 70 | } // namespace utils 71 | -------------------------------------------------------------------------------- /common/utils/scoped-dir.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Create directory in constructor, delete it in destructor 23 | */ 24 | 25 | #ifndef UNIT_TESTS_UTILS_SCOPED_DIR_HPP 26 | #define UNIT_TESTS_UTILS_SCOPED_DIR_HPP 27 | 28 | #include 29 | 30 | 31 | namespace utils { 32 | 33 | 34 | /** 35 | * Scoped directory 36 | * To be used in tests only 37 | */ 38 | class ScopedDir { 39 | public: 40 | ScopedDir(); 41 | ScopedDir(const std::string& path); 42 | ~ScopedDir(); 43 | 44 | /** 45 | * Creates a dir or if exists ensures it is empty 46 | */ 47 | void create(const std::string& path); 48 | 49 | /** 50 | * Deletes this dir with all content 51 | */ 52 | void remove(); 53 | 54 | private: 55 | std::string mPath; 56 | }; 57 | 58 | 59 | } // namespace utils 60 | 61 | 62 | #endif // UNIT_TESTS_UTILS_SCOPED_DIR_HPP 63 | -------------------------------------------------------------------------------- /common/utils/scoped-gerror.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Implementation of the wrapper for GError 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/scoped-gerror.hpp" 28 | 29 | namespace utils { 30 | 31 | ScopedGError::ScopedGError() 32 | : mError(NULL) 33 | { 34 | } 35 | 36 | ScopedGError::ScopedGError(ScopedGError&& other) 37 | : mError(other.mError) 38 | { 39 | other.mError = NULL; 40 | } 41 | 42 | ScopedGError::~ScopedGError() 43 | { 44 | if (mError) { 45 | g_error_free(mError); 46 | } 47 | } 48 | 49 | bool ScopedGError::strip() 50 | { 51 | return g_dbus_error_strip_remote_error(mError); 52 | } 53 | 54 | ScopedGError::operator bool () const 55 | { 56 | return mError != nullptr; 57 | } 58 | 59 | GError** ScopedGError::operator& () 60 | { 61 | return &mError; 62 | } 63 | 64 | const GError* ScopedGError::operator->() const 65 | { 66 | return mError; 67 | } 68 | 69 | std::ostream& operator<<(std::ostream& os, const ScopedGError& e) 70 | { 71 | os << e->message; 72 | return os; 73 | } 74 | 75 | } // namespace utils 76 | -------------------------------------------------------------------------------- /common/utils/scoped-gerror.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Declaration of the wrapper for GError 23 | */ 24 | 25 | #ifndef COMMON_SCOPED_GERROR_HPP 26 | #define COMMON_SCOPED_GERROR_HPP 27 | 28 | #include 29 | #include 30 | 31 | namespace utils { 32 | 33 | class ScopedGError { 34 | public: 35 | ScopedGError(); 36 | ScopedGError(ScopedGError&&); 37 | ~ScopedGError(); 38 | 39 | ScopedGError(const ScopedGError&) = delete; 40 | ScopedGError& operator=(const ScopedGError&) = delete; 41 | 42 | /** 43 | * Strip the error 44 | */ 45 | bool strip(); 46 | 47 | /** 48 | * Is error pointer NULL? 49 | */ 50 | operator bool () const; 51 | 52 | /** 53 | * @return pointer to the GError 54 | */ 55 | GError** operator& (); 56 | 57 | /** 58 | * @return the GError 59 | */ 60 | const GError* operator->() const; 61 | 62 | /** 63 | * Writes out the error message 64 | * @param os the output stream 65 | * @param e error to write out 66 | */ 67 | friend std::ostream& operator<<(std::ostream& os, const ScopedGError& e); 68 | 69 | private: 70 | GError* mError; 71 | 72 | }; 73 | 74 | } // namespace utils 75 | 76 | #endif // COMMON_SCOPED_GERROR_HPP 77 | -------------------------------------------------------------------------------- /common/utils/signal.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Signal related functions 23 | */ 24 | 25 | #ifndef COMMON_UTILS_SIGNAL_HPP 26 | #define COMMON_UTILS_SIGNAL_HPP 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | namespace utils { 33 | 34 | // ------------------- syscall wrappers ------------------- 35 | // Throw exception on error 36 | void pthread_sigmask(int how, const ::sigset_t *set, ::sigset_t *get); 37 | void sigemptyset(::sigset_t *set); 38 | void sigfillset(::sigset_t *set); 39 | void sigaddset(::sigset_t *set, int signum); 40 | void sigpending(::sigset_t *set); 41 | bool sigismember(const ::sigset_t *set, int signum); 42 | int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout); 43 | void sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); 44 | 45 | 46 | // ------------------- higher level functions ------------------- 47 | ::sigset_t getSignalMask(); 48 | bool isSignalPending(const int sigNum); 49 | bool waitForSignal(const int sigNum, int timeoutMs); 50 | bool isSignalBlocked(const int sigNum); 51 | void signalBlockAllExcept(const std::initializer_list& signals); 52 | void signalBlock(const int sigNum); 53 | void signalUnblock(const int sigNum); 54 | std::vector> signalIgnore(const std::initializer_list& signals); 55 | struct ::sigaction signalSet(const int sigNum, const struct ::sigaction *sigAct); 56 | void sendSignal(const pid_t pid, const int sigNum); 57 | 58 | 59 | } // namespace utils 60 | 61 | 62 | #endif // COMMON_UTILS_SIGNAL_HPP 63 | -------------------------------------------------------------------------------- /common/utils/spin-wait-for.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Simple spin lock implementation 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | namespace utils { 29 | 30 | template 31 | bool spinWaitFor(int timeoutMs, Predicate pred) 32 | { 33 | auto until = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs); 34 | while (!pred()) { 35 | if (std::chrono::steady_clock::now() >= until) { 36 | return false; 37 | } 38 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); 39 | } 40 | return true; 41 | } 42 | 43 | } // namespace utils -------------------------------------------------------------------------------- /common/utils/text.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Krzysztof Dynowski (k.dynowski@samsumg.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Krzysztof Dynowski (k.dynowski@samsumg.com) 22 | * @brief Text related utility 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/text.hpp" 28 | 29 | namespace utils { 30 | namespace { 31 | const char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', 32 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 33 | } 34 | 35 | std::string toHexString(const void *data, unsigned len) 36 | { 37 | const unsigned char *d = static_cast(data); 38 | std::string s(len * 2, ' '); 39 | for (unsigned i = 0; i < len; ++i) { 40 | s[2 * i] = hexmap[(d[i] >> 4) & 0x0F]; 41 | s[2 * i + 1] = hexmap[d[i] & 0x0F]; 42 | } 43 | return s; 44 | } 45 | 46 | std::vector split(const std::string& str, const std::string& delim) 47 | { 48 | std::vector tokens; 49 | if (str.empty()) { 50 | return tokens; 51 | } 52 | 53 | for (std::string::size_type startPos = 0; ; ) { 54 | std::string::size_type endPos = str.find_first_of(delim, startPos); 55 | 56 | if (endPos == std::string::npos) { 57 | tokens.push_back(str.substr(startPos, endPos)); 58 | break; 59 | } 60 | tokens.push_back(str.substr(startPos, endPos - startPos)); 61 | 62 | startPos = endPos + 1; 63 | } 64 | return tokens; 65 | } 66 | 67 | } // namespace utils 68 | -------------------------------------------------------------------------------- /common/utils/text.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Krzysztof Dynowski (k.dynowski@samsumg.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Krzysztof Dynowski (k.dynowski@samsumg.com) 22 | * @brief Text related utils 23 | */ 24 | 25 | #ifndef COMMON_UTILS_TEXT_HPP 26 | #define COMMON_UTILS_TEXT_HPP 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace utils { 34 | 35 | inline bool beginsWith(std::string const &value, std::string const &part) 36 | { 37 | if (part.size() > value.size()) { 38 | return false; 39 | } 40 | return std::equal(part.begin(), part.end(), value.begin()); 41 | } 42 | 43 | inline bool endsWith(std::string const &value, std::string const &part) 44 | { 45 | if (part.size() > value.size()) { 46 | return false; 47 | } 48 | return std::equal(part.rbegin(), part.rend(), value.rbegin()); 49 | } 50 | 51 | /** 52 | * Convert binary bytes array to hex string representation 53 | */ 54 | std::string toHexString(const void *data, unsigned len); 55 | 56 | template 57 | std::string join(const std::vector& vec, const char *delim) 58 | { 59 | std::stringstream res; 60 | for (const auto& s : vec) { 61 | if (res.tellp()>0) { 62 | res << delim; 63 | } 64 | res << s; 65 | } 66 | return res.str(); 67 | } 68 | 69 | template 70 | std::string join(const std::set& vec, const char *delim) 71 | { 72 | std::stringstream res; 73 | for (const auto& s : vec) { 74 | if (res.tellp()>0) { 75 | res << delim; 76 | } 77 | res << s; 78 | } 79 | return res.str(); 80 | } 81 | 82 | std::vector split(const std::string& str, const std::string& delim); 83 | 84 | } // namespace utils 85 | 86 | #endif // COMMON_UTILS_TEXT_HPP 87 | -------------------------------------------------------------------------------- /common/utils/typeinfo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak (j.olszak@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Synchronization latch 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/typeinfo.hpp" 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | namespace utils { 34 | 35 | std::string getTypeName(const std::type_info& ti) 36 | { 37 | int status; 38 | char* demangled = abi::__cxa_demangle(ti.name() , 0, 0, &status); 39 | if (status) { 40 | std::string message = "abi::__cxa_demangle failed with ret code: " + std::to_string(status); 41 | throw std::runtime_error(message); 42 | } 43 | 44 | std::string ret(demangled); 45 | free(demangled); 46 | return ret; 47 | } 48 | 49 | } // namespace utils 50 | -------------------------------------------------------------------------------- /common/utils/typeinfo.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak (j.olszak@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Synchronization latch 23 | */ 24 | 25 | #ifndef COMMON_TYPE_INFO_HPP 26 | #define COMMON_TYPE_INFO_HPP 27 | 28 | #include 29 | #include 30 | 31 | namespace utils { 32 | 33 | std::string getTypeName(const std::type_info& ti); 34 | 35 | template std::string getTypeName(const T& t) 36 | { 37 | return getTypeName(typeid(t)); 38 | } 39 | 40 | } // namespace utils 41 | 42 | 43 | #endif // COMMON_TYPE_INFO_HPP 44 | -------------------------------------------------------------------------------- /common/utils/vt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Kostyra 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Kostyra (l.kostyra@samsung.com) 22 | * @brief VT-related utility functions 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "utils/vt.hpp" 28 | #include "logger/logger.hpp" 29 | #include "utils/exception.hpp" 30 | #include "utils/fd-utils.hpp" 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | namespace { 40 | 41 | const std::string TTY_DEV = "/dev/tty0"; 42 | 43 | } // namespace 44 | 45 | namespace utils { 46 | 47 | bool activateVT(const int& vt) 48 | { 49 | int consoleFD = -1; 50 | 51 | try { 52 | consoleFD = utils::open(TTY_DEV, O_WRONLY); 53 | 54 | struct vt_stat vtstat; 55 | vtstat.v_active = 0; 56 | utils::ioctl(consoleFD, VT_GETSTATE, &vtstat); 57 | 58 | if (vtstat.v_active == vt) { 59 | LOGW("vt" << vt << " is already active."); 60 | } 61 | else { 62 | // activate vt 63 | utils::ioctl(consoleFD, VT_ACTIVATE, reinterpret_cast(vt)); 64 | 65 | // wait until activation is finished 66 | utils::ioctl(consoleFD, VT_WAITACTIVE, reinterpret_cast(vt)); 67 | } 68 | 69 | utils::close(consoleFD); 70 | return true; 71 | } catch(const UtilsException & e) { 72 | LOGE("Failed to activate vt" << vt << ": " << e.what() << " (" << getSystemErrorMessage(e.mErrno) << ")"); 73 | utils::close(consoleFD); 74 | return false; 75 | } 76 | } 77 | 78 | } // namespace utils 79 | -------------------------------------------------------------------------------- /common/utils/vt.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Kostyra 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Kostyra (l.kostyra@samsung.com) 22 | * @brief VT-related utility functions 23 | */ 24 | 25 | #ifndef COMMON_UTILS_VT_HPP 26 | #define COMMON_UTILS_VT_HPP 27 | 28 | namespace utils { 29 | 30 | bool activateVT(const int& vt); 31 | 32 | } // namespace utils 33 | 34 | #endif // COMMON_UTILS_VT_HPP 35 | -------------------------------------------------------------------------------- /common/utils/worker.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief A worker thread that executes tasks 23 | */ 24 | 25 | #ifndef COMMON_UTILS_WORKER_HPP 26 | #define COMMON_UTILS_WORKER_HPP 27 | 28 | #include 29 | #include 30 | 31 | namespace utils { 32 | 33 | /** 34 | * A queue with tasks executed in a dedicated thread. 35 | * Current implementation creates a thread on the first use. 36 | */ 37 | class Worker { 38 | public: 39 | typedef std::shared_ptr Pointer; 40 | typedef std::function Task; 41 | 42 | ~Worker(); 43 | 44 | /** 45 | * Creates a worker with its own thread 46 | */ 47 | static Pointer create(); 48 | 49 | /** 50 | * Creates a worker that share a thread with its parent 51 | */ 52 | Pointer createSubWorker(); 53 | 54 | /** 55 | * Adds a task to the queue. 56 | */ 57 | void addTask(const Task& task); 58 | void addTaskAndWait(const Task& task); 59 | 60 | private: 61 | typedef unsigned int GroupID; 62 | class WorkerQueue; 63 | 64 | const std::shared_ptr mWorkerQueue; 65 | const GroupID mGroupID; 66 | 67 | Worker(const std::shared_ptr& workerQueue); 68 | }; 69 | 70 | } // namespace utils 71 | 72 | 73 | #endif // COMMON_UTILS_WORKER_HPP 74 | -------------------------------------------------------------------------------- /doc/cargo_usage.md: -------------------------------------------------------------------------------- 1 | cargo libraries usage example {#mainpage} 2 | ============================= 3 | 4 | @ingroup libcargo 5 | 6 | @code 7 | #include "cargo/fields.hpp" 8 | #include "cargo-gvariant/cargo-gvariant.hpp" 9 | #include "cargo-json/cargo-json.hpp" 10 | #include "cargo-sqlite/cargo-sqlite.hpp" 11 | #include "cargo-sqlite-json/cargo-sqlite-json.hpp" 12 | #include "cargo-fd/cargo-fd.hpp" 13 | #include 14 | #include 15 | 16 | struct Foo 17 | { 18 | std::string bar = "plain-text"; 19 | std::vector tab = std::vector{1, 2, 4, 8}; 20 | double number = 3.14; 21 | 22 | CARGO_REGISTER 23 | ( 24 | bar, 25 | tab, 26 | number 27 | ) 28 | }; 29 | 30 | int main() 31 | { 32 | Foo foo; 33 | 34 | const std::string jsonString = cargo::saveToJsonString(foo); 35 | cargo::loadFromJsonString(jsonString, foo); 36 | 37 | const GVariant* gVariantPointer = cargo::saveToGVariant(foo); 38 | cargo::loadFromGVariant(gVariantPointer, foo); 39 | g_variant_unref(gVariantPointer); 40 | 41 | constexpr std::string jsonFile = "foo.json"; 42 | cargo::saveToJsonFile(jsonFile, foo); 43 | cargo::loadFromJsonFile(jsonFile, foo); 44 | 45 | constexpr std::string kvDBPath = "kvdb"; 46 | constexpr std::string key = "foo"; 47 | cargo::saveToKVStore(kvDBPath, foo, key); 48 | cargo::loadFromKVStore(kvDBPath, foo, key); 49 | 50 | cargo::loadFromKVStoreWithJson(kvDBPath, jsonString, foo, key); 51 | cargo::loadFromKVStoreWithJsonFile(kvDBPath, jsonFile, foo, key); 52 | 53 | FILE* file = fopen("blob", "wb"); 54 | if (!file) 55 | { 56 | return EXIT_FAILURE; 57 | } 58 | const int fd = ::fileno(file); 59 | cargo::saveToFD(fd, foo); 60 | ::fclose(file); 61 | file = ::fopen("blob", "rb"); 62 | if(!file) { 63 | return EXIT_FAILURE; 64 | } 65 | cargo::loadFromFD(fd, foo); 66 | ::fclose(file); 67 | 68 | return 0; 69 | } 70 | @endcode 71 | -------------------------------------------------------------------------------- /doc/footer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /doc/generate_documentation.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | DOC_CFG="doxygen.cfg" 4 | 5 | # Check if doxy is visible 6 | echo -n "Checking for doxygen... " 7 | EXE_PATH=$(which doxygen) 8 | if [ ! -x "$EXE_PATH" ] ; then 9 | echo "NOT FOUND, EXITING" 10 | else 11 | echo "FOUND" 12 | fi 13 | 14 | # Change pwd to script dir, to keep the paths consistent 15 | pushd . > /dev/null 16 | cd "$(dirname "${BASH_SOURCE[0]}" )" 17 | 18 | doxygen "$DOC_CFG" 19 | 20 | popd > /dev/null 21 | -------------------------------------------------------------------------------- /doc/header.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | $projectname: $title 9 | $title 10 | 11 | 12 | 13 | 14 | $treeview 15 | $search 16 | $mathjax 17 | 18 | $extrastylesheet 19 | 20 | 21 |
22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
30 |
$projectbrief
31 |
$searchbox
41 |
42 | 43 | 44 | -------------------------------------------------------------------------------- /libs/cargo-fd/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Pawel Kubik (p.kubik@samsung.com) 18 | # 19 | 20 | PROJECT(cargo-fd) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the lib${PROJECT_NAME}...") 24 | 25 | FILE(GLOB SRCS internals/*.cpp) 26 | 27 | SET(_LIB_VERSION_ "${VERSION}") 28 | SET(_LIB_SOVERSION_ "0") 29 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 30 | 31 | ## Setup target ################################################################ 32 | ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) 33 | SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES 34 | SOVERSION ${_LIB_SOVERSION_} 35 | VERSION ${_LIB_VERSION_} 36 | ) 37 | 38 | ADD_DEPENDENCIES(${PROJECT_NAME} cargo-utils) 39 | 40 | ## Link libraries ############################################################## 41 | INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${LIBS_FOLDER}) 42 | 43 | ## Generate the pc file ######################################################## 44 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 45 | 46 | ## Install ##################################################################### 47 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 48 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 49 | 50 | INSTALL(TARGETS ${PROJECT_NAME} 51 | DESTINATION ${LIB_INSTALL_DIR} 52 | COMPONENT RuntimeLibraries) 53 | 54 | INSTALL(DIRECTORY . DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME} 55 | FILES_MATCHING PATTERN "*.hpp" 56 | PATTERN "CMakeFiles" EXCLUDE) 57 | 58 | INSTALL(FILES ${COMMON_FOLDER}/config.hpp 59 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) 60 | -------------------------------------------------------------------------------- /libs/cargo-fd/internals/from-fdstore-visitor.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Kubik (p.kubik@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Kubik (p.kubik@samsung.com) 22 | * @brief Default visitor for reading from a file descriptor 23 | */ 24 | 25 | #ifndef CARGO_FD_INTERNALS_FROM_FDSTORE_VISITOR_HPP 26 | #define CARGO_FD_INTERNALS_FROM_FDSTORE_VISITOR_HPP 27 | 28 | #include "cargo-fd/internals/from-fdstore-visitor-base.hpp" 29 | #include "cargo/types.hpp" 30 | 31 | namespace cargo { 32 | 33 | namespace internals { 34 | 35 | /** 36 | * Default file descriptor reading visitor. 37 | */ 38 | class FromFDStoreVisitor : public FromFDStoreVisitorBase { 39 | public: 40 | explicit FromFDStoreVisitor(int fd) 41 | : FromFDStoreVisitorBase(fd) 42 | { 43 | } 44 | 45 | FromFDStoreVisitor(FromFDStoreVisitorBase& visitor) 46 | : FromFDStoreVisitorBase(visitor) 47 | { 48 | } 49 | 50 | template 51 | void visitImpl(T& value) 52 | { 53 | readInternal(value); 54 | } 55 | 56 | private: 57 | void readInternal(cargo::FileDescriptor& fd) 58 | { 59 | fd = mStore.receiveFD(); 60 | } 61 | 62 | template 63 | void readInternal(T& v) 64 | { 65 | FromFDStoreVisitorBase::visitImpl(v); 66 | } 67 | }; 68 | 69 | } // namespace internals 70 | 71 | } // namespace cargo 72 | 73 | #endif // CARGO_FD_INTERNALS_FROM_FDSTORE_VISITOR_HPP 74 | -------------------------------------------------------------------------------- /libs/cargo-fd/internals/to-fdstore-visitor.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Kubik (p.kubik@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Kubik (p.kubik@samsung.com) 22 | * @brief Default visitor for writing to a file descriptor 23 | */ 24 | 25 | #ifndef CARGO_FD_INTERNALS_TO_FDSTORE_VISITOR_HPP 26 | #define CARGO_FD_INTERNALS_TO_FDSTORE_VISITOR_HPP 27 | 28 | #include "to-fdstore-visitor-base.hpp" 29 | 30 | #include "cargo/types.hpp" 31 | 32 | namespace cargo { 33 | 34 | namespace internals { 35 | 36 | /** 37 | * Default file descriptor writing visitor. 38 | */ 39 | class ToFDStoreVisitor : public ToFDStoreVisitorBase { 40 | public: 41 | explicit ToFDStoreVisitor(int fd) 42 | : ToFDStoreVisitorBase(fd) 43 | { 44 | } 45 | 46 | ToFDStoreVisitor(ToFDStoreVisitorBase& visitor) 47 | : ToFDStoreVisitorBase(visitor) 48 | { 49 | } 50 | 51 | template 52 | void visitImpl(T& value) 53 | { 54 | writeInternal(value); 55 | } 56 | 57 | private: 58 | void writeInternal(const cargo::FileDescriptor& fd) 59 | { 60 | mStore.sendFD(fd.value); 61 | } 62 | 63 | template 64 | void writeInternal(T& v) 65 | { 66 | ToFDStoreVisitorBase::visitImpl(v); 67 | } 68 | }; 69 | 70 | } // namespace internals 71 | 72 | } // namespace cargo 73 | 74 | #endif // CARGO_FD_INTERNALS_TO_FDSTORE_VISITOR_HPP 75 | -------------------------------------------------------------------------------- /libs/cargo-fd/libcargo-fd.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: cargo file descriptor library 10 | Version: @_LIB_VERSION_@ 11 | Libs: -L${libdir} -l@PROJECT_NAME@ 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ 13 | -------------------------------------------------------------------------------- /libs/cargo-gvariant/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Pawel Kubik (p.kubik@samsung.com) 18 | # 19 | 20 | PROJECT(cargo-gvariant) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the lib${PROJECT_NAME}...") 24 | 25 | FILE(GLOB HEADERS *.hpp 26 | ${COMMON_FOLDER}/config.hpp) 27 | FILE(GLOB HEADERS_INTERNALS internals/*.hpp) 28 | 29 | SET(_LIB_VERSION_ "${VERSION}") 30 | SET(_LIB_SOVERSION_ "0") 31 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 32 | 33 | ## Setup target ################################################################ 34 | 35 | ## Link libraries ############################################################## 36 | PKG_CHECK_MODULES(CARGO_GVARIANT_DEPS REQUIRED glib-2.0) 37 | 38 | INCLUDE_DIRECTORIES(${LIBS_FOLDER}) 39 | INCLUDE_DIRECTORIES(SYSTEM ${CARGO_GVARIANT_DEPS_INCLUDE_DIRS}) 40 | 41 | ## Generate the pc file ######################################################## 42 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 43 | 44 | ## Install ##################################################################### 45 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 46 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 47 | 48 | INSTALL(DIRECTORY . DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME} 49 | FILES_MATCHING PATTERN "*.hpp" 50 | PATTERN "CMakeFiles" EXCLUDE) 51 | 52 | INSTALL(FILES ${COMMON_FOLDER}/config.hpp 53 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) 54 | -------------------------------------------------------------------------------- /libs/cargo-gvariant/cargo-gvariant.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Kubik (p.kubik@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Kubik (p.kubik@samsung.com) 22 | * @defgroup libcargo-gvariant libcargo-gvariant 23 | * @brief cargo GVariant interface 24 | */ 25 | 26 | #ifndef CARGO_GVARIANT_CARGO_GVARIANT_HPP 27 | #define CARGO_GVARIANT_CARGO_GVARIANT_HPP 28 | 29 | #include "cargo-gvariant/internals/to-gvariant-visitor.hpp" 30 | #include "cargo-gvariant/internals/from-gvariant-visitor.hpp" 31 | 32 | namespace cargo { 33 | 34 | /*@{*/ 35 | 36 | /** 37 | * Fills the cargo structure with data stored in the GVariant 38 | * 39 | * @param gvariant data in GVariant type 40 | * @param visitable visitable structure to fill 41 | */ 42 | template 43 | void loadFromGVariant(GVariant* gvariant, Cargo& visitable) 44 | { 45 | static_assert(internals::isVisitable::value, "Use CARGO_REGISTER macro"); 46 | static_assert(!internals::isUnion::value, "Don't use CARGO_DECLARE_UNION in top level visitable"); 47 | 48 | internals::FromGVariantVisitor visitor(gvariant); 49 | visitable.accept(visitor); 50 | } 51 | 52 | /** 53 | * Saves the visitable in a GVariant 54 | * 55 | * @param visitable visitable structure to convert 56 | */ 57 | template 58 | GVariant* saveToGVariant(const Cargo& visitable) 59 | { 60 | static_assert(internals::isVisitable::value, "Use CARGO_REGISTER macro"); 61 | static_assert(!internals::isUnion::value, "Don't use CARGO_DECLARE_UNION in top level visitable"); 62 | 63 | internals::ToGVariantVisitor visitor; 64 | visitable.accept(visitor); 65 | return visitor.toVariant(); 66 | } 67 | 68 | } // namespace cargo 69 | 70 | /*@}*/ 71 | 72 | #endif // CARGO_GVARIANT_CARGO_GVARIANT_HPP 73 | -------------------------------------------------------------------------------- /libs/cargo-gvariant/libcargo-gvariant.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: cargo GVariant library 10 | Version: @_LIB_VERSION_@ 11 | Libs: 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ 13 | -------------------------------------------------------------------------------- /libs/cargo-ipc/epoll/events.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Epoll events 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-ipc/epoll/events.hpp" 28 | 29 | #include 30 | 31 | namespace cargo { 32 | namespace ipc { 33 | namespace epoll { 34 | 35 | namespace { 36 | 37 | std::string eventToString(Events event) 38 | { 39 | switch (event) { 40 | case EPOLLIN: return "IN"; 41 | case EPOLLOUT: return "OUT"; 42 | case EPOLLERR: return "ERR"; 43 | case EPOLLHUP: return "HUP"; 44 | case EPOLLRDHUP: return "RDHUP"; 45 | default: 46 | std::ostringstream ss; 47 | ss << "0x" << std::hex << event; 48 | return ss.str(); 49 | } 50 | } 51 | 52 | } // namespace 53 | 54 | std::string eventsToString(Events events) 55 | { 56 | if (events == 0) { 57 | return ""; 58 | } 59 | std::string ret; 60 | for (unsigned int i = 0; i<32; ++i) { 61 | Events event = 1u << i; 62 | if (events & event) { 63 | if (!ret.empty()) { 64 | ret.append(", "); 65 | } 66 | ret.append(eventToString(event)); 67 | } 68 | } 69 | return ret; 70 | } 71 | 72 | } // namespace epoll 73 | } // namespace ipc 74 | } // namespace cargo 75 | -------------------------------------------------------------------------------- /libs/cargo-ipc/epoll/events.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Epoll events 23 | */ 24 | 25 | #ifndef CARGO_IPC_EPOLL_EVENTS_HPP 26 | #define CARGO_IPC_EPOLL_EVENTS_HPP 27 | 28 | #include 29 | #include // for EPOLL* constatnts 30 | 31 | namespace cargo { 32 | namespace ipc { 33 | namespace epoll { 34 | 35 | /** 36 | * @brief bitmask of EPOLL* constants 37 | * @ingroup Types 38 | */ 39 | typedef unsigned int Events; 40 | 41 | /** 42 | * Convert event mask into readable string. 43 | * Values will be comma separated. 44 | * @param events event type mask to convert 45 | * @return string describing the mask 46 | */ 47 | std::string eventsToString(Events events); 48 | 49 | } // namespace epoll 50 | } // namespace ipc 51 | } // namespace cargo 52 | 53 | #endif // CARGO_IPC_EPOLL_EVENTS_HPP 54 | -------------------------------------------------------------------------------- /libs/cargo-ipc/epoll/glib-dispatcher.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief glib epoll dispatcher 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-ipc/epoll/glib-dispatcher.hpp" 28 | #include "utils/callback-wrapper.hpp" 29 | 30 | namespace cargo { 31 | namespace ipc { 32 | namespace epoll { 33 | 34 | GlibDispatcher::GlibDispatcher() 35 | { 36 | mChannel = g_io_channel_unix_new(mPoll.getPollFD()); 37 | 38 | auto dispatchCallback = [this]() { 39 | mPoll.dispatchIteration(0); 40 | }; 41 | 42 | auto cCallback = [](GIOChannel*, GIOCondition, gpointer data) -> gboolean { 43 | utils::getCallbackFromPointer(data)(); 44 | return TRUE; 45 | }; 46 | 47 | mWatchId = g_io_add_watch_full(mChannel, 48 | G_PRIORITY_DEFAULT, 49 | G_IO_IN, 50 | cCallback, 51 | utils::createCallbackWrapper(dispatchCallback, mGuard.spawn()), 52 | &utils::deleteCallbackWrapper); 53 | } 54 | 55 | GlibDispatcher::~GlibDispatcher() 56 | { 57 | g_source_remove(mWatchId); 58 | g_io_channel_unref(mChannel); 59 | // mGuard destructor will wait for full unregister of dispatchCallback 60 | } 61 | 62 | EventPoll& GlibDispatcher::getPoll() 63 | { 64 | return mPoll; 65 | } 66 | 67 | } // namespace epoll 68 | } // namespace ipc 69 | } // namespace cargo 70 | -------------------------------------------------------------------------------- /libs/cargo-ipc/epoll/glib-dispatcher.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief glib epoll dispatcher 23 | */ 24 | 25 | #ifndef CARGO_IPC_EPOLL_GLIB_DISPATCHER_HPP 26 | #define CARGO_IPC_EPOLL_GLIB_DISPATCHER_HPP 27 | 28 | #include "cargo-ipc/epoll/event-poll.hpp" 29 | #include "utils/callback-guard.hpp" 30 | 31 | #include 32 | 33 | namespace cargo { 34 | namespace ipc { 35 | namespace epoll { 36 | 37 | /** 38 | * Will dispatch poll events in glib thread 39 | */ 40 | class GlibDispatcher { 41 | public: 42 | GlibDispatcher(); 43 | ~GlibDispatcher(); 44 | 45 | EventPoll& getPoll(); 46 | private: 47 | EventPoll mPoll; // before mGuard! 48 | utils::CallbackGuard mGuard; 49 | GIOChannel* mChannel; 50 | guint mWatchId; 51 | }; 52 | 53 | 54 | } // namespace epoll 55 | } // namespace ipc 56 | } // namespace cargo 57 | 58 | #endif // CARGO_IPC_EPOLL_GLIB_DISPATCHER_HPP 59 | -------------------------------------------------------------------------------- /libs/cargo-ipc/epoll/thread-dispatcher.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Thread epoll dispatcher 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-ipc/epoll/thread-dispatcher.hpp" 28 | 29 | namespace cargo { 30 | namespace ipc { 31 | namespace epoll { 32 | 33 | ThreadDispatcher::ThreadDispatcher() 34 | : mStopped(false) 35 | { 36 | auto controlCallback = [this](int, Events) { 37 | mStopEvent.receive(); 38 | mStopped.store(true, std::memory_order_release); 39 | }; 40 | 41 | mPoll.addFD(mStopEvent.getFD(), EPOLLIN, std::move(controlCallback)); 42 | mThread = std::thread([this] { 43 | while (!mStopped.load(std::memory_order_acquire)) { 44 | mPoll.dispatchIteration(-1); 45 | } 46 | }); 47 | } 48 | 49 | ThreadDispatcher::~ThreadDispatcher() 50 | { 51 | mStopEvent.send(); 52 | mThread.join(); 53 | mPoll.removeFD(mStopEvent.getFD()); 54 | } 55 | 56 | EventPoll& ThreadDispatcher::getPoll() 57 | { 58 | return mPoll; 59 | } 60 | 61 | } // namespace epoll 62 | } // namespace ipc 63 | } // namespace cargo 64 | -------------------------------------------------------------------------------- /libs/cargo-ipc/epoll/thread-dispatcher.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Thread epoll dispatcher 23 | */ 24 | 25 | #ifndef CARGO_IPC_EPOLL_THREAD_DISPATCHER_HPP 26 | #define CARGO_IPC_EPOLL_THREAD_DISPATCHER_HPP 27 | 28 | #include "cargo-ipc/epoll/event-poll.hpp" 29 | #include "utils/eventfd.hpp" 30 | 31 | #include 32 | #include 33 | 34 | namespace cargo { 35 | namespace ipc { 36 | namespace epoll { 37 | 38 | /** 39 | * Will dispatch poll events in a newly created thread 40 | */ 41 | class ThreadDispatcher { 42 | public: 43 | ThreadDispatcher(); 44 | ~ThreadDispatcher(); 45 | 46 | EventPoll& getPoll(); 47 | private: 48 | EventPoll mPoll; 49 | utils::EventFD mStopEvent; 50 | std::atomic_bool mStopped; 51 | std::thread mThread; 52 | }; 53 | 54 | } // namespace epoll 55 | } // namespace ipc 56 | } // namespace cargo 57 | 58 | #endif // CARGO_IPC_EPOLL_THREAD_DISPATCHER_HPP 59 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/acceptor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Class for accepting new connections 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-ipc/internals/acceptor.hpp" 28 | #include "logger/logger.hpp" 29 | 30 | #include 31 | 32 | namespace cargo { 33 | namespace ipc { 34 | namespace internals { 35 | 36 | Acceptor::Acceptor(epoll::EventPoll& eventPoll, 37 | const std::string& socketPath, 38 | const NewConnectionCallback& newConnectionCallback) 39 | : mEventPoll(eventPoll), 40 | mNewConnectionCallback(newConnectionCallback), 41 | mSocket(Socket::createUNIX(socketPath)) 42 | { 43 | LOGT("Creating Acceptor for socket " << socketPath); 44 | mEventPoll.addFD(mSocket.getFD(), EPOLLIN, std::bind(&Acceptor::handleConnection, this)); 45 | } 46 | 47 | Acceptor::~Acceptor() 48 | { 49 | LOGT("Destroyed Acceptor"); 50 | mEventPoll.removeFD(mSocket.getFD()); 51 | } 52 | 53 | void Acceptor::handleConnection() 54 | { 55 | std::shared_ptr tmpSocket = mSocket.accept(); 56 | mNewConnectionCallback(tmpSocket); 57 | } 58 | 59 | } // namespace internals 60 | } // namespace ipc 61 | } // namespace cargo 62 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/acceptor.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Class for accepting new connections 23 | */ 24 | 25 | #ifndef CARGO_IPC_INTERNALS_ACCEPTOR_HPP 26 | #define CARGO_IPC_INTERNALS_ACCEPTOR_HPP 27 | 28 | #include "cargo-ipc/internals/socket.hpp" 29 | #include "cargo-ipc/epoll/event-poll.hpp" 30 | #include "cargo-ipc/types.hpp" 31 | 32 | #include 33 | 34 | namespace cargo { 35 | namespace ipc { 36 | namespace internals { 37 | 38 | /** 39 | * Accepts new connections and passes the new socket to a callback. 40 | */ 41 | class Acceptor { 42 | public: 43 | 44 | typedef std::function& socketPtr)> NewConnectionCallback; 45 | 46 | /** 47 | * Class for accepting new connections. 48 | * 49 | * @param eventPoll dispatcher 50 | * @param socketPath path to the socket 51 | * @param newConnectionCallback called on new connections 52 | */ 53 | Acceptor(epoll::EventPoll& eventPoll, 54 | const std::string& socketPath, 55 | const NewConnectionCallback& newConnectionCallback); 56 | ~Acceptor(); 57 | 58 | Acceptor(const Acceptor& acceptor) = delete; 59 | Acceptor& operator=(const Acceptor&) = delete; 60 | 61 | private: 62 | epoll::EventPoll& mEventPoll; 63 | NewConnectionCallback mNewConnectionCallback; 64 | Socket mSocket; 65 | 66 | /** 67 | * Handle one incoming connection. 68 | * Used with external polling 69 | */ 70 | void handleConnection(); 71 | }; 72 | 73 | } // namespace internals 74 | } // namespace ipc 75 | } // namespace cargo 76 | 77 | #endif // CARGO_IPC_INTERNALS_ACCEPTOR_HPP 78 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/add-peer-request.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Processor's request to add a peer 23 | */ 24 | 25 | #ifndef CARGO_IPC_INTERNALS_ADD_PEER_REQUEST_HPP 26 | #define CARGO_IPC_INTERNALS_ADD_PEER_REQUEST_HPP 27 | 28 | #include "cargo-ipc/types.hpp" 29 | #include "cargo-ipc/internals/socket.hpp" 30 | 31 | namespace cargo { 32 | namespace ipc { 33 | namespace internals { 34 | 35 | class AddPeerRequest { 36 | public: 37 | AddPeerRequest(const AddPeerRequest&) = delete; 38 | AddPeerRequest& operator=(const AddPeerRequest&) = delete; 39 | 40 | AddPeerRequest(const std::shared_ptr& socketPtr) 41 | : socketPtr(socketPtr), 42 | peerID(getNextPeerID()) 43 | { 44 | } 45 | 46 | std::shared_ptr socketPtr; 47 | PeerID peerID; 48 | }; 49 | 50 | } // namespace internals 51 | } // namespace ipc 52 | } // namespace cargo 53 | 54 | #endif // CARGO_IPC_INTERNALS_ADD_PEER_REQUEST_HPP 55 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/finish-request.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Managing the queue with requests 23 | */ 24 | 25 | #ifndef CARGO_IPC_INTERNALS_FINISH_REQUEST_HPP 26 | #define CARGO_IPC_INTERNALS_FINISH_REQUEST_HPP 27 | 28 | #include 29 | 30 | namespace cargo { 31 | namespace ipc { 32 | namespace internals { 33 | 34 | class FinishRequest { 35 | public: 36 | FinishRequest(const FinishRequest&) = delete; 37 | FinishRequest& operator=(const FinishRequest&) = delete; 38 | 39 | FinishRequest(const std::shared_ptr& conditionPtr) 40 | : conditionPtr(conditionPtr) 41 | {} 42 | 43 | std::shared_ptr conditionPtr; 44 | }; 45 | 46 | } // namespace internals 47 | } // namespace ipc 48 | } // namespace cargo 49 | 50 | #endif // CARGO_IPC_INTERNALS_FINISH_REQUEST_HPP 51 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/remove-method-request.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Processor's request to remove the specified method handler 23 | */ 24 | 25 | #ifndef CARGO_IPC_INTERNALS_REMOVE_METHOD_REQUEST_HPP 26 | #define CARGO_IPC_INTERNALS_REMOVE_METHOD_REQUEST_HPP 27 | 28 | #include "cargo-ipc/types.hpp" 29 | 30 | namespace cargo { 31 | namespace ipc { 32 | namespace internals { 33 | 34 | class RemoveMethodRequest { 35 | public: 36 | RemoveMethodRequest(const RemoveMethodRequest&) = delete; 37 | RemoveMethodRequest& operator=(const RemoveMethodRequest&) = delete; 38 | 39 | RemoveMethodRequest(const MethodID methodID) 40 | : methodID(methodID) 41 | {} 42 | 43 | MethodID methodID; 44 | }; 45 | 46 | } // namespace internals 47 | } // namespace ipc 48 | } // namespace cargo 49 | 50 | #endif // CARGO_IPC_INTERNALS_REMOVE_METHOD_REQUEST_HPP 51 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/remove-peer-request.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Processor's request to remove a peer 23 | */ 24 | 25 | #ifndef CARGO_IPC_INTERNALS_REMOVE_PEER_REQUEST_HPP 26 | #define CARGO_IPC_INTERNALS_REMOVE_PEER_REQUEST_HPP 27 | 28 | #include "cargo-ipc/types.hpp" 29 | #include "cargo-ipc/internals/socket.hpp" 30 | #include 31 | 32 | 33 | namespace cargo { 34 | namespace ipc { 35 | namespace internals { 36 | 37 | class RemovePeerRequest { 38 | public: 39 | RemovePeerRequest(const RemovePeerRequest&) = delete; 40 | RemovePeerRequest& operator=(const RemovePeerRequest&) = delete; 41 | 42 | RemovePeerRequest(const PeerID& peerID, 43 | const std::shared_ptr& conditionPtr) 44 | : peerID(peerID), 45 | conditionPtr(conditionPtr) 46 | { 47 | } 48 | 49 | PeerID peerID; 50 | std::shared_ptr conditionPtr; 51 | }; 52 | 53 | } // namespace internals 54 | } // namespace ipc 55 | } // namespace cargo 56 | 57 | #endif // CARGO_IPC_INTERNALS_REMOVE_PEER_REQUEST_HPP 58 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/result-builder.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Class for storing result of a method - data or exception 23 | */ 24 | 25 | #ifndef CARGO_IPC_INTERNALS_RESULT_BUILDER_HPP 26 | #define CARGO_IPC_INTERNALS_RESULT_BUILDER_HPP 27 | 28 | #include "cargo-ipc/result.hpp" 29 | #include 30 | #include 31 | #include 32 | 33 | namespace cargo { 34 | namespace ipc { 35 | namespace internals { 36 | 37 | class ResultBuilder { 38 | public: 39 | ResultBuilder() 40 | : mData(nullptr), 41 | mExceptionPtr(nullptr) 42 | {} 43 | 44 | explicit ResultBuilder(const std::exception_ptr& exceptionPtr) 45 | : mData(nullptr), 46 | mExceptionPtr(exceptionPtr) 47 | {} 48 | 49 | explicit ResultBuilder(const std::shared_ptr& data) 50 | : mData(data), 51 | mExceptionPtr(nullptr) 52 | 53 | {} 54 | 55 | template 56 | Result build() 57 | { 58 | return Result(std::move(std::static_pointer_cast(mData)), 59 | std::move(mExceptionPtr)); 60 | } 61 | 62 | private: 63 | std::shared_ptr mData; 64 | std::exception_ptr mExceptionPtr; 65 | }; 66 | 67 | typedef std::function ResultBuilderHandler; 68 | 69 | 70 | } // namespace internals 71 | } // namespace ipc 72 | } // namespace cargo 73 | 74 | #endif // CARGO_IPC_INTERNALS_RESULT_BUILDER_HPP 75 | -------------------------------------------------------------------------------- /libs/cargo-ipc/internals/send-result-request.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Processor's request to send the result of a method 23 | */ 24 | 25 | #ifndef CARGO_IPC_INTERNALS_SEND_RESULT_REQUEST_HPP 26 | #define CARGO_IPC_INTERNALS_SEND_RESULT_REQUEST_HPP 27 | 28 | #include "cargo-ipc/types.hpp" 29 | #include "logger/logger-scope.hpp" 30 | 31 | namespace cargo { 32 | namespace ipc { 33 | namespace internals { 34 | 35 | class SendResultRequest { 36 | public: 37 | SendResultRequest(const SendResultRequest&) = delete; 38 | SendResultRequest& operator=(const SendResultRequest&) = delete; 39 | 40 | SendResultRequest(const MethodID methodID, 41 | const PeerID& peerID, 42 | const MessageID& messageID, 43 | const std::shared_ptr& data) 44 | : methodID(methodID), 45 | peerID(peerID), 46 | messageID(messageID), 47 | data(data) 48 | {} 49 | 50 | MethodID methodID; 51 | PeerID peerID; 52 | MessageID messageID; 53 | std::shared_ptr data; 54 | }; 55 | 56 | } // namespace internals 57 | } // namespace ipc 58 | } // namespace cargo 59 | 60 | #endif // CARGO_IPC_INTERNALS_SEND_RESULT_REQUEST_HPP 61 | -------------------------------------------------------------------------------- /libs/cargo-ipc/libcargo-ipc.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: IPC library 10 | Version: @_LIB_VERSION_@ 11 | Libs: -L${libdir} -l@PROJECT_NAME@ 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ 13 | -------------------------------------------------------------------------------- /libs/cargo-ipc/method-result.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Class for sending the result of a method 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-ipc/method-result.hpp" 28 | #include "cargo-ipc/internals/processor.hpp" 29 | 30 | namespace cargo { 31 | namespace ipc { 32 | 33 | using namespace internals; 34 | 35 | MethodResult::MethodResult(Processor& processor, 36 | const MethodID methodID, 37 | const MessageID& messageID, 38 | const PeerID& peerID) 39 | : mProcessor(processor), 40 | mMethodID(methodID), 41 | mPeerID(peerID), 42 | mMessageID(messageID) 43 | {} 44 | 45 | void MethodResult::setInternal(const std::shared_ptr& data) 46 | { 47 | mProcessor.sendResult(mMethodID, mPeerID, mMessageID, data); 48 | } 49 | 50 | void MethodResult::setVoid() 51 | { 52 | mProcessor.sendVoid(mMethodID, mPeerID, mMessageID); 53 | } 54 | 55 | void MethodResult::setError(const int code, const std::string& message) 56 | { 57 | mProcessor.sendError(mPeerID, mMessageID, code, message); 58 | } 59 | 60 | PeerID MethodResult::getPeerID() const 61 | { 62 | return mPeerID; 63 | } 64 | 65 | } // namespace ipc 66 | } // namespace cargo 67 | -------------------------------------------------------------------------------- /libs/cargo-ipc/result.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Class for storing result of a method - data or exception 23 | */ 24 | 25 | #ifndef CARGO_IPC_RESULT_HPP 26 | #define CARGO_IPC_RESULT_HPP 27 | 28 | #include 29 | #include 30 | #include 31 | #include "cargo-ipc/exception.hpp" 32 | 33 | namespace cargo { 34 | namespace ipc { 35 | 36 | template 37 | class Result { 38 | public: 39 | Result() 40 | : mData(nullptr), 41 | mExceptionPtr(nullptr) 42 | {} 43 | 44 | Result(std::shared_ptr&& data, std::exception_ptr&& exceptionPtr) 45 | : mData(std::move(data)), 46 | mExceptionPtr(std::move(exceptionPtr)) 47 | {} 48 | 49 | void rethrow() const 50 | { 51 | if (isValid()) { 52 | return; 53 | } 54 | if (mExceptionPtr) { 55 | std::rethrow_exception(mExceptionPtr); 56 | } 57 | throw IPCInvalidResultException("Invalid result received. Details unknown."); 58 | } 59 | 60 | std::shared_ptr get() const 61 | { 62 | rethrow(); 63 | return mData; 64 | } 65 | 66 | bool isSet() const 67 | { 68 | return static_cast(mExceptionPtr) || static_cast(mData); 69 | } 70 | 71 | bool isValid() const 72 | { 73 | return static_cast(mData); 74 | } 75 | 76 | private: 77 | std::shared_ptr mData; 78 | std::exception_ptr mExceptionPtr; 79 | }; 80 | 81 | template 82 | struct ResultHandler { 83 | typedef std::function < void(Result&&) > type; 84 | }; 85 | 86 | } // namespace ipc 87 | } // namespace cargo 88 | 89 | #endif // CARGO_IPC_RESULT_HPP 90 | -------------------------------------------------------------------------------- /libs/cargo-ipc/types.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Types definitions and helper functions 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-ipc/types.hpp" 28 | #include "cargo-ipc/unique-id.hpp" 29 | #include "logger/logger.hpp" 30 | 31 | #include 32 | 33 | namespace cargo { 34 | namespace ipc { 35 | 36 | namespace { 37 | // complex types cannot be easily used with std::atomic - these require libatomic to be linked 38 | // instead, secure the ID getters with mutex 39 | MessageID gLastMessageID; 40 | PeerID gLastPeerID; 41 | 42 | std::mutex gMessageIDMutex; 43 | std::mutex gPeerIDMutex; 44 | 45 | const size_t ID_TRIM_LENGTH = 6; 46 | } // namespace 47 | 48 | MessageID getNextMessageID() 49 | { 50 | std::unique_lock lock(gMessageIDMutex); 51 | UniqueID uid; 52 | uid.generate(); 53 | gLastMessageID = uid; 54 | return gLastMessageID; 55 | } 56 | 57 | MessageID shortenMessageID(const MessageID& id) 58 | { 59 | return id.substr(0, ID_TRIM_LENGTH) + "..." + id.substr(id.length() - ID_TRIM_LENGTH); 60 | } 61 | 62 | PeerID getNextPeerID() 63 | { 64 | std::unique_lock lock(gPeerIDMutex); 65 | UniqueID uid; 66 | uid.generate(); 67 | gLastPeerID = uid; 68 | return gLastPeerID; 69 | } 70 | 71 | PeerID shortenPeerID(const PeerID& id) 72 | { 73 | return id.substr(0, ID_TRIM_LENGTH) + "..." + id.substr(id.length() - ID_TRIM_LENGTH); 74 | } 75 | 76 | 77 | } // namespace ipc 78 | } // namespace cargo 79 | -------------------------------------------------------------------------------- /libs/cargo-ipc/unique-id.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Kostyra 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Kostyra (l.kostyra@samsung.com) 22 | * @brief Unique ID type definition 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "unique-id.hpp" 28 | 29 | namespace cargo { 30 | namespace ipc { 31 | 32 | UniqueID::UniqueID() 33 | { 34 | mTime.tv_sec = 0; 35 | mTime.tv_nsec = 0; 36 | ::uuid_clear(mUUID); 37 | } 38 | 39 | void UniqueID::generate() 40 | { 41 | ::clock_gettime(CLOCK_REALTIME, &mTime); 42 | ::uuid_generate_random(mUUID); 43 | } 44 | 45 | bool UniqueID::operator==(const UniqueID& other) const 46 | { 47 | return (mTime.tv_sec == other.mTime.tv_sec) 48 | && (mTime.tv_nsec == other.mTime.tv_nsec) 49 | && (::uuid_compare(mUUID, other.mUUID) == 0); // uuid_compare works just like strcmp 50 | } 51 | 52 | UniqueID::operator std::string() const 53 | { 54 | char uuid[37]; // uuid_unparse assures that it will print 36 chars + terminating zero 55 | ::uuid_unparse(mUUID, uuid); 56 | return std::to_string(mTime.tv_sec) + '.' + std::to_string(mTime.tv_nsec) + ':' + uuid; 57 | } 58 | 59 | std::ostream& operator<<(std::ostream& str, const UniqueID& id) 60 | { 61 | str << static_cast(id); 62 | return str; 63 | } 64 | 65 | } // namespace ipc 66 | } // namespace cargo 67 | -------------------------------------------------------------------------------- /libs/cargo-json/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Pawel Kubik (p.kubik@samsung.com) 18 | # 19 | 20 | PROJECT(cargo-json) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the lib${PROJECT_NAME}...") 24 | 25 | FILE(GLOB HEADERS *.hpp) 26 | FILE(GLOB HEADERS_INTERNALS internals/*.hpp) 27 | 28 | SET(_LIB_VERSION_ "${VERSION}") 29 | SET(_LIB_SOVERSION_ "0") 30 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 31 | 32 | ## Setup target ################################################################ 33 | 34 | ## Link libraries ############################################################## 35 | PKG_SEARCH_MODULE(JSON_C REQUIRED json json-c) 36 | 37 | INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${LIBS_FOLDER}) 38 | INCLUDE_DIRECTORIES(SYSTEM ${JSON_C_INCLUDE_DIRS}) 39 | 40 | ## Generate the pc file ######################################################## 41 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 42 | 43 | ## Install ##################################################################### 44 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 45 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 46 | 47 | INSTALL(DIRECTORY . DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME} 48 | FILES_MATCHING PATTERN "*.hpp" 49 | PATTERN "CMakeFiles" EXCLUDE) 50 | 51 | INSTALL(FILES ${COMMON_FOLDER}/config.hpp 52 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) 53 | -------------------------------------------------------------------------------- /libs/cargo-json/libcargo-json.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: cargo Json library 10 | Version: @_LIB_VERSION_@ 11 | Libs: -L${libdir} -l@JSON_C_LIBRARIES@ 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ -I@JSON_C_INCLUDE_DIRS@ 13 | -------------------------------------------------------------------------------- /libs/cargo-sqlite-json/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Pawel Kubik (p.kubik@samsung.com) 18 | # 19 | 20 | PROJECT(cargo-sqlite-json) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the lib${PROJECT_NAME}...") 24 | 25 | FILE(GLOB HEADERS *.hpp ${COMMON_FOLDER}/config.hpp) 26 | 27 | SET(_LIB_VERSION_ "${VERSION}") 28 | SET(_LIB_SOVERSION_ "0") 29 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 30 | 31 | ## Setup target ################################################################ 32 | 33 | ## Link libraries ############################################################## 34 | 35 | ## Generate the pc file ######################################################## 36 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 37 | 38 | ## Install ##################################################################### 39 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 40 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 41 | 42 | INSTALL(FILES ${HEADERS} 43 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) 44 | -------------------------------------------------------------------------------- /libs/cargo-sqlite-json/libcargo-sqlite-json.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: cargo KVStore with Json extension library 10 | Version: @_LIB_VERSION_@ 11 | Libs: 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ 13 | -------------------------------------------------------------------------------- /libs/cargo-sqlite/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Pawel Kubik (p.kubik@samsung.com) 18 | # 19 | 20 | PROJECT(cargo-sqlite) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the lib${PROJECT_NAME}...") 24 | 25 | FILE(GLOB_RECURSE SRCS *.cpp) 26 | 27 | SET(_LIB_VERSION_ "${VERSION}") 28 | SET(_LIB_SOVERSION_ "0") 29 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 30 | 31 | ## Setup target ################################################################ 32 | ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) 33 | SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES 34 | SOVERSION ${_LIB_SOVERSION_} 35 | VERSION ${_LIB_VERSION_} 36 | ) 37 | 38 | ADD_DEPENDENCIES(${PROJECT_NAME} cargo-utils) 39 | 40 | ## Link libraries ############################################################## 41 | PKG_CHECK_MODULES(SQLITE3 REQUIRED sqlite3) 42 | 43 | INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${LIBS_FOLDER}) 44 | INCLUDE_DIRECTORIES(SYSTEM ${SQLITE3_INCLUDE_DIRS}) 45 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SQLITE3_LIBRARIES}) 46 | 47 | ## Generate the pc file ######################################################## 48 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 49 | 50 | ## Install ##################################################################### 51 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 52 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 53 | 54 | INSTALL(TARGETS ${PROJECT_NAME} 55 | DESTINATION ${LIB_INSTALL_DIR} 56 | COMPONENT RuntimeLibraries) 57 | 58 | INSTALL(DIRECTORY . DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME} 59 | FILES_MATCHING PATTERN "*.hpp" 60 | PATTERN "CMakeFiles" EXCLUDE) 61 | 62 | INSTALL(FILES ${COMMON_FOLDER}/config.hpp 63 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) 64 | -------------------------------------------------------------------------------- /libs/cargo-sqlite/internals/from-kvstore-visitor.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Kubik (p.kubik@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Kubik (p.kubik@samsung.com) 22 | * @brief Default visitor for loading from KVStore 23 | */ 24 | 25 | #ifndef CARGO_SQLITE_INTERNALS_FROM_KVSTORE_VISITOR_HPP 26 | #define CARGO_SQLITE_INTERNALS_FROM_KVSTORE_VISITOR_HPP 27 | 28 | #include "cargo-sqlite/internals/from-kvstore-visitor-base.hpp" 29 | 30 | 31 | namespace cargo { 32 | 33 | namespace internals { 34 | 35 | /** 36 | * Default KVStore visitor. 37 | */ 38 | class FromKVStoreVisitor : public FromKVStoreVisitorBase { 39 | public: 40 | FromKVStoreVisitor(KVStore& store, const std::string& prefix) 41 | : FromKVStoreVisitorBase(store, prefix) 42 | { 43 | } 44 | 45 | FromKVStoreVisitor(const FromKVStoreVisitorBase& visitor, 46 | const std::string& prefix) 47 | : FromKVStoreVisitorBase(visitor, prefix) 48 | { 49 | } 50 | }; 51 | 52 | } // namespace internals 53 | 54 | } // namespace cargo 55 | 56 | #endif // CARGO_SQLITE_INTERNALS_FROM_KVSTORE_VISITOR_HPP 57 | -------------------------------------------------------------------------------- /libs/cargo-sqlite/libcargo-sqlite.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: cargo SQLite library 10 | Version: @_LIB_VERSION_@ 11 | Libs: -L${libdir} -l@PROJECT_NAME@ 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ 13 | -------------------------------------------------------------------------------- /libs/cargo-sqlite/sqlite3/connection.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Definition of the class managing a sqlite3 database connection 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-sqlite/sqlite3/connection.hpp" 28 | #include "cargo/exception.hpp" 29 | 30 | namespace cargo { 31 | namespace sqlite3 { 32 | 33 | Connection::Connection(const std::string& path) 34 | { 35 | if (path.empty()) { 36 | // Sqlite creates temporary database in case of empty path 37 | // but we want to forbid this. 38 | throw CargoException("Error opening the database: empty path"); 39 | } 40 | if (::sqlite3_open_v2(path.c_str(), 41 | &mDbPtr, 42 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 43 | NULL) != SQLITE_OK) { 44 | throw CargoException("Error opening the database: " + getErrorMessage()); 45 | } 46 | 47 | if (mDbPtr == NULL) { 48 | throw CargoException("Error opening the database: Unable to allocate memory."); 49 | } 50 | } 51 | 52 | Connection::~Connection() 53 | { 54 | ::sqlite3_close(mDbPtr); 55 | } 56 | 57 | void Connection::exec(const std::string& query) 58 | { 59 | char* mess; 60 | if (::sqlite3_exec(mDbPtr, query.c_str(), 0, 0, &mess) != SQLITE_OK) { 61 | throw CargoException("Error during executing statement " + std::string(mess)); 62 | } 63 | } 64 | 65 | ::sqlite3* Connection::get() 66 | { 67 | return mDbPtr; 68 | } 69 | 70 | std::string Connection::getErrorMessage() 71 | { 72 | return std::string(sqlite3_errmsg(mDbPtr)); 73 | } 74 | 75 | } // namespace sqlite3 76 | } // namespace cargo 77 | -------------------------------------------------------------------------------- /libs/cargo-sqlite/sqlite3/connection.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Declaration of the class managing a sqlite3 database connection 23 | */ 24 | 25 | #ifndef CARGO_SQLITE_SQLITE3_CONNECTION_HPP 26 | #define CARGO_SQLITE_SQLITE3_CONNECTION_HPP 27 | 28 | #include 29 | #include 30 | 31 | namespace cargo { 32 | namespace sqlite3 { 33 | 34 | struct Connection { 35 | /** 36 | * @param path database file path 37 | */ 38 | Connection(const std::string& path); 39 | Connection(const Connection&) = delete; 40 | ~Connection(); 41 | 42 | /** 43 | * @return pointer to the corresponding sqlite3 database object 44 | */ 45 | ::sqlite3* get(); 46 | 47 | /** 48 | * @return last error message in the database 49 | */ 50 | std::string getErrorMessage(); 51 | 52 | /** 53 | * Executes the query in the database. 54 | * 55 | * @param query query to be executed 56 | */ 57 | void exec(const std::string& query); 58 | 59 | private: 60 | ::sqlite3* mDbPtr; 61 | }; 62 | 63 | } // namespace sqlite3 64 | } // namespace cargo 65 | 66 | #endif // CARGO_SQLITE_SQLITE3_CONNECTION_HPP 67 | -------------------------------------------------------------------------------- /libs/cargo-sqlite/sqlite3/statement.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Declaration of the class managing a sqlite3 statement 23 | */ 24 | 25 | #ifndef CARGO_SQLITE_SQLITE3_STATEMENT_HPP 26 | #define CARGO_SQLITE_SQLITE3_STATEMENT_HPP 27 | 28 | #include "cargo-sqlite/sqlite3/connection.hpp" 29 | 30 | #include 31 | #include 32 | 33 | namespace cargo { 34 | namespace sqlite3 { 35 | 36 | struct Statement { 37 | 38 | /** 39 | * @param connRef reference to the Connection object 40 | * @param query query to be executed 41 | */ 42 | Statement(sqlite3::Connection& connRef, const std::string& query); 43 | ~Statement(); 44 | 45 | /** 46 | * @return pointer to the sqlite3 statement 47 | */ 48 | sqlite3_stmt* get(); 49 | 50 | /** 51 | * Clears the bindings and resets the statement. 52 | * After this the statement can be executed again 53 | */ 54 | void reset(); 55 | 56 | private: 57 | ::sqlite3_stmt* mStmtPtr; 58 | sqlite3::Connection& mConnRef; 59 | }; 60 | 61 | } // namespace sqlite3 62 | } // namespace cargo 63 | 64 | #endif // CARGO_SQLITE_SQLITE3_STATEMENT_HPP 65 | -------------------------------------------------------------------------------- /libs/cargo-validator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Maciej Karpiuk (m.karpiuk2@samsung.com) 18 | # 19 | 20 | PROJECT(cargo-validator) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the lib${PROJECT_NAME}...") 24 | 25 | FILE(GLOB HEADERS *.hpp) 26 | FILE(GLOB HEADERS_INTERNALS internals/*.hpp) 27 | FILE(GLOB SRCS *.cpp) 28 | FILE(GLOB SRCS_INTERNALS internals/*.cpp) 29 | 30 | SET(_LIB_VERSION_ "${VERSION}") 31 | SET(_LIB_SOVERSION_ "0") 32 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 33 | 34 | ## Setup target ################################################################ 35 | ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS} ${SRCS_INTERNALS}) 36 | SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES 37 | SOVERSION ${_LIB_SOVERSION_} 38 | VERSION ${_LIB_VERSION_} 39 | ) 40 | 41 | ADD_DEPENDENCIES(${PROJECT_NAME} cargo-utils) 42 | 43 | ## Link libraries ############################################################## 44 | INCLUDE_DIRECTORIES(${LIBS_FOLDER} ${COMMON_FOLDER}) 45 | INCLUDE_DIRECTORIES(SYSTEM ${CARGO_DEPS_INCLUDE_DIRS}) 46 | 47 | ## Generate the pc file ######################################################## 48 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 49 | 50 | ## Install ##################################################################### 51 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 52 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 53 | 54 | INSTALL(TARGETS ${PROJECT_NAME} 55 | DESTINATION ${LIB_INSTALL_DIR} 56 | COMPONENT RuntimeLibraries) 57 | 58 | INSTALL(FILES ${HEADERS} 59 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) 60 | 61 | INSTALL(FILES ${HEADERS_INTERNALS} 62 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}/internals) 63 | 64 | -------------------------------------------------------------------------------- /libs/cargo-validator/exception.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Maciej Karpiuk (m.karpiuk2@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Maciej Karpiuk (m.karpiuk2@samsung.com) 22 | * @brief Exceptions for libcargo-validator 23 | */ 24 | 25 | #ifndef CARGO_VALIDATOR_EXCEPTION_HPP 26 | #define CARGO_VALIDATOR_EXCEPTION_HPP 27 | 28 | #include "cargo/exception.hpp" 29 | #include 30 | 31 | namespace cargo { 32 | namespace validator { 33 | /** 34 | * Structure verification exception 35 | */ 36 | struct VerificationException: public CargoException { 37 | 38 | VerificationException(const std::string& error) : CargoException(error) {} 39 | }; 40 | 41 | } // namespace validator 42 | } // namespace cargo 43 | 44 | #endif // CARGO_VALIDATOR_EXCEPTION_HPP 45 | -------------------------------------------------------------------------------- /libs/cargo-validator/internals/is-validable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Maciej Karpiuk (m.karpiuk2@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Maciej Karpiuk (m.karpiuk2@samsung.com) 22 | * @brief Internal validation helper 23 | */ 24 | 25 | #ifndef CARGO_VALIDATOR_INTERNALS_IS_VALIDABLE_HPP 26 | #define CARGO_VALIDATOR_INTERNALS_IS_VALIDABLE_HPP 27 | 28 | #include 29 | #include "cargo-validator/internals/validator-visitor.hpp" 30 | 31 | namespace cargo { 32 | 33 | template 34 | struct isValidableHelper__ { 35 | template static std::true_type 36 | test(decltype(std::declval().template accept(cargo::validator::ValidatorVisitor()))*); 37 | 38 | template static std::false_type 39 | test(...); 40 | 41 | static constexpr bool value = std::is_same(0)), std::true_type>::value; 42 | }; 43 | 44 | /** 45 | * Helper for compile-time checking against existance of method 'accept' with ValidatorVisitor arg. 46 | */ 47 | template 48 | struct isValidable : public std::integral_constant::value> {}; 49 | 50 | } // namespace cargo 51 | 52 | #endif // CARGO_VALIDATOR_INTERNALS_IS_VALIDABLE_HPP 53 | -------------------------------------------------------------------------------- /libs/cargo-validator/libcargo-validator.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: Cargo Validator library 10 | Version: @_LIB_VERSION_@ 11 | Libs: -L${libdir} -l@PROJECT_NAME@ 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ 13 | -------------------------------------------------------------------------------- /libs/cargo-validator/validator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Maciej Karpiuk (m.karpiuk2@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Maciej Karpiuk (m.karpiuk2@samsung.com) 22 | * @brief Pre-defined validation check functions 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "cargo-validator/validator.hpp" 28 | #include "utils/fs.hpp" 29 | #include "utils/exception.hpp" 30 | 31 | using namespace utils; 32 | 33 | bool cargo::validator::isNonEmptyString(const std::string &s) 34 | { 35 | return !s.empty(); 36 | } 37 | 38 | bool cargo::validator::isAbsolutePath(const std::string &s) 39 | { 40 | return utils::isAbsolute(s); 41 | } 42 | 43 | bool cargo::validator::isFilePresent(const std::string &s) 44 | { 45 | return utils::isRegularFile(s); 46 | } 47 | 48 | bool cargo::validator::isDirectoryPresent(const std::string &s) 49 | { 50 | return utils::isDir(s); 51 | } 52 | -------------------------------------------------------------------------------- /libs/cargo/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Dariusz Michaluk (d.michaluk@samsung.com) 18 | # 19 | 20 | PROJECT(cargo) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the lib${PROJECT_NAME}...") 24 | 25 | SET(_LIB_VERSION_ "${VERSION}") 26 | SET(_LIB_SOVERSION_ "0") 27 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 28 | 29 | ## Setup target ################################################################ 30 | 31 | ## Link libraries ############################################################## 32 | INCLUDE_DIRECTORIES(${LIBS_FOLDER}) 33 | 34 | ## Generate the pc file ######################################################## 35 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 36 | 37 | ## Install ##################################################################### 38 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 39 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 40 | 41 | INSTALL(DIRECTORY . DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME} 42 | FILES_MATCHING PATTERN "*.hpp" 43 | PATTERN "CMakeFiles" EXCLUDE) 44 | 45 | INSTALL(FILES ${COMMON_FOLDER}/config.hpp 46 | DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) 47 | -------------------------------------------------------------------------------- /libs/cargo/exception.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Pawelczyk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com) 22 | * @brief Exceptions for libcargo 23 | */ 24 | 25 | #ifndef CARGO_EXCEPTION_HPP 26 | #define CARGO_EXCEPTION_HPP 27 | 28 | #include 29 | 30 | namespace cargo { 31 | 32 | /** 33 | * Base class for exceptions in libcargo. 34 | */ 35 | struct CargoException: public std::runtime_error { 36 | 37 | CargoException(const std::string& error) : std::runtime_error(error) {} 38 | }; 39 | 40 | /** 41 | * No such key error. 42 | */ 43 | struct NoKeyException: public CargoException { 44 | 45 | NoKeyException(const std::string& error) : CargoException(error) {} 46 | }; 47 | 48 | /** 49 | * Invalid integral type integrity error. 50 | */ 51 | struct InternalIntegrityException: public CargoException { 52 | 53 | InternalIntegrityException(const std::string& error) : CargoException(error) {} 54 | }; 55 | 56 | /** 57 | * Container size does not match. 58 | */ 59 | struct ContainerSizeException: public CargoException { 60 | 61 | ContainerSizeException(const std::string& error): CargoException(error) {} 62 | }; 63 | 64 | } // namespace cargo 65 | 66 | #endif // CARGO_EXCEPTION_HPP 67 | -------------------------------------------------------------------------------- /libs/cargo/internals/is-like-tuple.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Kubik (p.kubik@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Kubik (p.kubik@samsung.com) 22 | * @brief Tuple or pair checker 23 | */ 24 | 25 | #ifndef CARGO_INTERNALS_IS_LIKE_TUPLE_HPP 26 | #define CARGO_INTERNALS_IS_LIKE_TUPLE_HPP 27 | 28 | #include 29 | 30 | namespace cargo { 31 | namespace internals { 32 | 33 | template 34 | struct isLikeTuple { 35 | static constexpr bool value = std::false_type::value; 36 | }; 37 | 38 | template 39 | struct isLikeTuple> { 40 | static constexpr bool value = std::true_type::value; 41 | }; 42 | 43 | template 44 | struct isLikeTuple> { 45 | static constexpr bool value = std::true_type::value; 46 | }; 47 | 48 | } // namespace internals 49 | } // namespace cargo 50 | 51 | #endif // CARGO_INTERNALS_IS_LIKE_TUPLE_HPP 52 | 53 | -------------------------------------------------------------------------------- /libs/cargo/internals/is-streamable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Kubik (p.kubik@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Kubik (p.kubik@samsung.com) 22 | * @brief Check whether type is accepted by streaming operators 23 | */ 24 | 25 | #ifndef CARGO_INTERNALS_IS_STREAMABLE_HPP 26 | #define CARGO_INTERNALS_IS_STREAMABLE_HPP 27 | 28 | #include 29 | 30 | namespace cargo { 31 | namespace internals { 32 | 33 | template 34 | struct isStreamableIn { 35 | static std::istream stream; 36 | 37 | template static std::true_type 38 | test(decltype(stream >> (*(static_cast(nullptr))))); 39 | 40 | template static std::false_type 41 | test(...); 42 | 43 | static constexpr bool value = std::is_same(stream)), std::true_type>::value; 44 | }; 45 | 46 | template 47 | struct isStreamableOut { 48 | static std::ostream stream; 49 | 50 | template static std::true_type 51 | test(decltype(stream << (*(static_cast(nullptr))))); 52 | 53 | template static std::false_type 54 | test(...); 55 | 56 | static constexpr bool value = std::is_same(stream)), std::true_type>::value; 57 | }; 58 | 59 | } // internals 60 | } // namespace cargo 61 | 62 | #endif // CARGO_INTERNALS_IS_STREAMABLE_HPP 63 | 64 | -------------------------------------------------------------------------------- /libs/cargo/internals/is-union.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Krzysztof Dynowski (k.dynowski@samsumg.com) 22 | * @brief Internal configuration helper 23 | */ 24 | 25 | #ifndef CARGO_INTERNALS_IS_UNION_HPP 26 | #define CARGO_INTERNALS_IS_UNION_HPP 27 | 28 | #include "cargo/internals/is-visitable.hpp" 29 | 30 | namespace cargo { 31 | namespace internals { 32 | 33 | // generic member checker, start 34 | template 35 | struct has_member_impl { 36 | template 37 | static std::true_type check(typename F::template checker__* =0); 38 | 39 | template 40 | static std::false_type check(...); 41 | 42 | static const bool value = std::is_same(0)), std::true_type>::value; 43 | }; 44 | 45 | template 46 | struct has_member : public std::integral_constant::value> {}; 47 | // generic member checker, end 48 | 49 | 50 | template 51 | struct check_union : isVisitable { 52 | template 58 | struct checker__ {}; 59 | }; 60 | template 61 | struct isUnion : has_member> {}; 62 | 63 | //Note: 64 | // unfortunately, above generic has_member can't be used for isVisitable 65 | // because Vistable need 'accept' OR 'accept const', while has_member make exect match 66 | // e.g accept AND accept const 67 | 68 | } // namespace internals 69 | } // namespace cargo 70 | 71 | #endif // CARGO_INTERNALS_IS_UNION_HPP 72 | 73 | -------------------------------------------------------------------------------- /libs/cargo/internals/is-visitable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Internal configuration helper 23 | */ 24 | 25 | #ifndef CARGO_INTERNALS_IS_VISITABLE_HPP 26 | #define CARGO_INTERNALS_IS_VISITABLE_HPP 27 | 28 | #include 29 | 30 | namespace cargo { 31 | namespace internals { 32 | 33 | template 34 | struct isVisitableHelper__ { 35 | struct Visitor {}; 36 | 37 | template static std::true_type 38 | test(decltype(std::declval().template accept(Visitor()))*); 39 | 40 | template static std::false_type 41 | test(...); 42 | 43 | static constexpr bool value = std::is_same(0)), std::true_type>::value; 44 | }; 45 | 46 | /** 47 | * Helper for compile-time checking against existance of template method 'accept'. 48 | */ 49 | template 50 | struct isVisitable : public std::integral_constant::value> {}; 51 | 52 | } // namespace internals 53 | } // namespace cargo 54 | 55 | #endif // CARGO_INTERNALS_IS_VISITABLE_HPP 56 | -------------------------------------------------------------------------------- /libs/cargo/internals/visit-fields.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak (j.olszak@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Helper function for iterating tuples, pairs and arrays 23 | */ 24 | 25 | 26 | #ifndef CARGO_INTERNALS_VISIT_FIELDS_HPP 27 | #define CARGO_INTERNALS_VISIT_FIELDS_HPP 28 | 29 | #include 30 | #include 31 | 32 | namespace { 33 | 34 | template 35 | struct visitImpl 36 | { 37 | static void visit(T& t, F f, A&& ... args) 38 | { 39 | visitImpl::visit(t, f, std::forward(args)...); 40 | f->visit(args..., std::get(t)); 41 | } 42 | }; 43 | 44 | template 45 | struct visitImpl<0, T, F, A...> 46 | { 47 | static void visit(T& t, F f, A&& ... args) 48 | { 49 | f->visit(args..., std::get<0>(t)); 50 | } 51 | }; 52 | 53 | } // namespace 54 | 55 | namespace cargo { 56 | namespace internals { 57 | 58 | template 59 | void visitFields(T& t, F f, A ... args) 60 | { 61 | visitImpl::value - 1, T, F, A...>::visit(t, f, std::forward(args)...); 62 | } 63 | 64 | } // namespace internals 65 | } // namespace cargo 66 | 67 | #endif // CARGO_INTERNALS_VISIT_FIELDS_HPP 68 | -------------------------------------------------------------------------------- /libs/cargo/libcargo.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: lib@PROJECT_NAME@ 9 | Description: cargo library 10 | Version: @_LIB_VERSION_@ 11 | Libs: 12 | Cflags: -I${includedir} -I${includedir}/@PROJECT_NAME@ -I${includedir}/@PROJECT_NAME@-utils 13 | -------------------------------------------------------------------------------- /libs/cargo/types.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Types declarations 23 | */ 24 | 25 | #ifndef CARGO_TYPES_HPP 26 | #define CARGO_TYPES_HPP 27 | 28 | namespace cargo { 29 | 30 | /** 31 | * Whenever possible, this type will be serialized using Linux file descriptor passing. 32 | */ 33 | struct FileDescriptor { 34 | int value; 35 | FileDescriptor(int fd = -1): value(fd) {} 36 | FileDescriptor& operator=(const int fd) { 37 | value = fd; 38 | return *this; 39 | } 40 | }; 41 | 42 | } // cargo 43 | 44 | #endif //CARGO_TYPES_HPP 45 | -------------------------------------------------------------------------------- /libs/logger/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Dariusz Michaluk (d.michaluk@samsung.com) 18 | # 19 | 20 | PROJECT(Logger) 21 | 22 | MESSAGE(STATUS "") 23 | MESSAGE(STATUS "Generating makefile for the libLogger...") 24 | FILE(GLOB HEADERS *.hpp 25 | ${COMMON_FOLDER}/config.hpp) 26 | FILE(GLOB SRCS *.cpp *.hpp) 27 | 28 | SET(_LIB_VERSION_ "${VERSION}") 29 | SET(_LIB_SOVERSION_ "0") 30 | SET(PC_FILE "lib${PROJECT_NAME}.pc") 31 | 32 | ## Setup target ################################################################ 33 | ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) 34 | SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES 35 | SOVERSION ${_LIB_SOVERSION_} 36 | VERSION ${_LIB_VERSION_} 37 | ) 38 | 39 | ADD_DEPENDENCIES(${PROJECT_NAME} cargo-utils) 40 | 41 | ## Link libraries ############################################################## 42 | IF(NOT WITHOUT_SYSTEMD) 43 | PKG_CHECK_MODULES(LOGGER_DEPS REQUIRED libsystemd-journal) 44 | ENDIF(NOT WITHOUT_SYSTEMD) 45 | 46 | INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${LIBS_FOLDER}) 47 | INCLUDE_DIRECTORIES(SYSTEM ${LOGGER_DEPS_INCLUDE_DIRS}) 48 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} cargo-utils ${LOGGER_DEPS_LIBRARIES}) 49 | 50 | ## Generate the pc file ######################################################## 51 | CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) 52 | 53 | ## Install ##################################################################### 54 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} 55 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) 56 | 57 | INSTALL(TARGETS ${PROJECT_NAME} 58 | DESTINATION ${LIB_INSTALL_DIR} 59 | COMPONENT RuntimeLibraries) 60 | 61 | INSTALL(FILES ${HEADERS} 62 | DESTINATION ${INCLUDE_INSTALL_DIR}/logger) 63 | -------------------------------------------------------------------------------- /libs/logger/backend-file.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Roman Kubiak (r.kubiak@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Roman Kubiak (r.kubiak@samsung.com) 22 | * @brief File backend for logger 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "logger/formatter.hpp" 28 | #include "logger/backend-file.hpp" 29 | 30 | #include 31 | 32 | namespace logger { 33 | 34 | void FileBackend::log(LogLevel logLevel, 35 | const std::string& file, 36 | const unsigned int& line, 37 | const std::string& func, 38 | const std::string& message) 39 | { 40 | std::ofstream out(mfilePath, std::ios::app); 41 | out << LogFormatter::getHeader(logLevel, file, line, func); 42 | out << message; 43 | out << std::endl; 44 | } 45 | 46 | 47 | } // namespace logger 48 | -------------------------------------------------------------------------------- /libs/logger/backend-file.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Roman Kubiak (r.kubiak@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Roman Kubiak (r.kubiak@samsung.com) 22 | * @brief File backend for logger 23 | */ 24 | 25 | #ifndef LOGGER_BACKEND_FILE_HPP 26 | #define LOGGER_BACKEND_FILE_HPP 27 | 28 | #include "logger/backend.hpp" 29 | 30 | namespace logger { 31 | 32 | class FileBackend : public LogBackend { 33 | public: 34 | FileBackend(const std::string& filePath) : mfilePath(filePath) {} 35 | void log(LogLevel logLevel, 36 | const std::string& file, 37 | const unsigned int& line, 38 | const std::string& func, 39 | const std::string& message) override; 40 | private: 41 | std::string mfilePath; 42 | }; 43 | 44 | } // namespace logger 45 | 46 | #endif // LOGGER_BACKEND_FILE_HPP 47 | -------------------------------------------------------------------------------- /libs/logger/backend-journal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Dariusz Michaluk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Dariusz Michaluk (d.michaluk@samsung.com) 22 | * @brief Systemd journal backend for logger 23 | */ 24 | 25 | #ifdef HAVE_SYSTEMD 26 | #include "config.hpp" 27 | 28 | #include "logger/backend-journal.hpp" 29 | 30 | #define SD_JOURNAL_SUPPRESS_LOCATION 31 | #include 32 | 33 | namespace logger { 34 | 35 | namespace { 36 | 37 | inline int toJournalPriority(LogLevel logLevel) 38 | { 39 | switch (logLevel) { 40 | case LogLevel::ERROR: 41 | return LOG_ERR; // 3 42 | case LogLevel::WARN: 43 | return LOG_WARNING; // 4 44 | case LogLevel::INFO: 45 | return LOG_INFO; // 6 46 | case LogLevel::DEBUG: 47 | return LOG_DEBUG; // 7 48 | case LogLevel::TRACE: 49 | return LOG_DEBUG; // 7 50 | case LogLevel::HELP: 51 | return LOG_DEBUG; // 7 52 | default: 53 | return LOG_DEBUG; // 7 54 | } 55 | } 56 | 57 | } // namespace 58 | 59 | void SystemdJournalBackend::log(LogLevel logLevel, 60 | const std::string& file, 61 | const unsigned int& line, 62 | const std::string& func, 63 | const std::string& message) 64 | { 65 | sd_journal_send("PRIORITY=%d", toJournalPriority(logLevel), 66 | "CODE_FILE=%s", file.c_str(), 67 | "CODE_LINE=%d", line, 68 | "CODE_FUNC=%s", func.c_str(), 69 | "MESSAGE=%s", message.c_str(), 70 | NULL); 71 | } 72 | 73 | } // namespace logger 74 | #endif // HAVE_SYSTEMD 75 | -------------------------------------------------------------------------------- /libs/logger/backend-journal.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Dariusz Michaluk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Dariusz Michaluk (d.michaluk@samsung.com) 22 | * @brief Systemd journal backend for logger 23 | */ 24 | 25 | #ifndef LOGGER_BACKEND_JOURNAL_HPP 26 | #define LOGGER_BACKEND_JOURNAL_HPP 27 | 28 | #include "logger/backend.hpp" 29 | 30 | namespace logger { 31 | 32 | /** 33 | * systemd journal logging backend 34 | */ 35 | class SystemdJournalBackend : public LogBackend { 36 | public: 37 | void log(LogLevel logLevel, 38 | const std::string& file, 39 | const unsigned int& line, 40 | const std::string& func, 41 | const std::string& message) override; 42 | }; 43 | 44 | } // namespace logger 45 | 46 | #endif // LOGGER_BACKEND_JOURNAL_HPP 47 | -------------------------------------------------------------------------------- /libs/logger/backend-null.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Broda 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Broda (p.broda@partner.samsung.com) 22 | * @brief Null backend for logger 23 | */ 24 | 25 | #ifndef LOGGER_BACKEND_NULL_HPP 26 | #define LOGGER_BACKEND_NULL_HPP 27 | 28 | #include "logger/backend.hpp" 29 | 30 | namespace logger { 31 | 32 | /** 33 | * Null logging backend 34 | */ 35 | class NullLogger : public LogBackend { 36 | public: 37 | void log(LogLevel /*logLevel*/, 38 | const std::string& /*file*/, 39 | const unsigned int& /*line*/, 40 | const std::string& /*func*/, 41 | const std::string& /*message*/) override {} 42 | }; 43 | 44 | } // namespace logger 45 | 46 | #endif // LOGGER_BACKEND_NULL_HPP 47 | -------------------------------------------------------------------------------- /libs/logger/backend-persistent-file.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Pawelczyk (l.pawelczyk@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Pawelczyk (l.pawelczyk@samsung.com) 22 | * @brief Persistent file backend for logger 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "logger/formatter.hpp" 28 | #include "logger/backend-persistent-file.hpp" 29 | 30 | #include 31 | 32 | namespace logger { 33 | 34 | void PersistentFileBackend::log(LogLevel logLevel, 35 | const std::string& file, 36 | const unsigned int& line, 37 | const std::string& func, 38 | const std::string& message) 39 | { 40 | mOut << LogFormatter::getHeader(logLevel, file, line, func); 41 | mOut << message; 42 | mOut << std::endl; 43 | mOut.flush(); 44 | } 45 | 46 | 47 | } // namespace logger 48 | -------------------------------------------------------------------------------- /libs/logger/backend-persistent-file.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Pawelczyk (l.pawelczyk@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Pawelczyk (l.pawelczyk@samsung.com) 22 | * @brief Persistent file backend for logger 23 | */ 24 | 25 | #ifndef LOGGER_BACKEND_PERSISTENT_FILE_HPP 26 | #define LOGGER_BACKEND_PERSISTENT_FILE_HPP 27 | 28 | #include "logger/backend.hpp" 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace logger { 35 | 36 | class PersistentFileBackend : public LogBackend { 37 | public: 38 | PersistentFileBackend(const std::string& filePath) : 39 | mfilePath(filePath), 40 | mOut(mfilePath, std::ios::app) 41 | { 42 | using filebufType = __gnu_cxx::stdio_filebuf; 43 | const int fd = static_cast(mOut.rdbuf())->fd(); 44 | ::fcntl(fd, F_SETFD, ::fcntl(fd, F_GETFD) | FD_CLOEXEC); 45 | } 46 | 47 | void log(LogLevel logLevel, 48 | const std::string& file, 49 | const unsigned int& line, 50 | const std::string& func, 51 | const std::string& message) override; 52 | private: 53 | std::string mfilePath; 54 | std::ofstream mOut; 55 | }; 56 | 57 | } // namespace logger 58 | 59 | #endif // LOGGER_BACKEND_PERSISTENT_FILE_HPP 60 | -------------------------------------------------------------------------------- /libs/logger/backend-stderr.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Broda 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Broda (p.broda@partner.samsung.com) 22 | * @brief Stderr backend for logger 23 | */ 24 | 25 | #ifndef LOGGER_BACKEND_STDERR_HPP 26 | #define LOGGER_BACKEND_STDERR_HPP 27 | 28 | #include "logger/backend.hpp" 29 | 30 | namespace logger { 31 | 32 | /** 33 | * Stderr logging backend 34 | */ 35 | class StderrBackend : public LogBackend { 36 | public: 37 | StderrBackend(const bool useColours = true) : mUseColours(useColours) {} 38 | void log(LogLevel logLevel, 39 | const std::string& file, 40 | const unsigned int& line, 41 | const std::string& func, 42 | const std::string& message) override; 43 | void relog(LogLevel logLevel, 44 | const std::string& file, 45 | const unsigned int& line, 46 | const std::string& func, 47 | const std::istream& stream) override; 48 | private: 49 | bool mUseColours; 50 | }; 51 | 52 | } // namespace logger 53 | 54 | #endif // LOGGER_BACKEND_STDERR_HPP 55 | -------------------------------------------------------------------------------- /libs/logger/backend-syslog.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Roman Kubiak (r.kubiak@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Roman Kubiak (r.kubiak@samsung.com) 22 | * @brief Syslog backend for logger 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "logger/formatter.hpp" 28 | #include "logger/backend-syslog.hpp" 29 | 30 | #include 31 | #include 32 | namespace logger { 33 | 34 | namespace { 35 | 36 | inline int toSyslogPriority(LogLevel logLevel) 37 | { 38 | switch (logLevel) { 39 | case LogLevel::ERROR: 40 | return LOG_ERR; // 3 41 | case LogLevel::WARN: 42 | return LOG_WARNING; // 4 43 | case LogLevel::INFO: 44 | return LOG_INFO; // 6 45 | case LogLevel::DEBUG: 46 | return LOG_DEBUG; // 7 47 | case LogLevel::TRACE: 48 | return LOG_DEBUG; // 7 49 | case LogLevel::HELP: 50 | return LOG_DEBUG; // 7 51 | default: 52 | return LOG_DEBUG; // 7 53 | } 54 | } 55 | 56 | } // namespace 57 | 58 | void SyslogBackend::log(LogLevel logLevel, 59 | const std::string& file, 60 | const unsigned int& line, 61 | const std::string& func, 62 | const std::string& message) 63 | { 64 | ::syslog(toSyslogPriority(logLevel), "%s %s", LogFormatter::getHeader(logLevel, file, line, func).c_str(), message.c_str()); 65 | } 66 | 67 | } // namespace logger 68 | -------------------------------------------------------------------------------- /libs/logger/backend-syslog.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Roman Kubiak (r.kubiak@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Roman Kubiak (r.kubiak@samsung.com) 22 | * @brief Syslog backend for logger 23 | */ 24 | 25 | #ifndef LOGGER_BACKEND_SYSLOG_HPP 26 | #define LOGGER_BACKEND_SYSLOG_HPP 27 | 28 | #include "logger/backend.hpp" 29 | 30 | namespace logger { 31 | 32 | class SyslogBackend : public LogBackend { 33 | public: 34 | void log(LogLevel logLevel, 35 | const std::string& file, 36 | const unsigned int& line, 37 | const std::string& func, 38 | const std::string& message) override; 39 | }; 40 | 41 | } // namespace logger 42 | 43 | #endif // LOGGER_BACKEND_SYSLOG_HPP 44 | -------------------------------------------------------------------------------- /libs/logger/backend.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Broda 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Pawel Broda (p.broda@partner.samsung.com) 22 | * @brief Logging backend 23 | */ 24 | 25 | #ifndef LOGGER_BACKEND_HPP 26 | #define LOGGER_BACKEND_HPP 27 | 28 | #include "logger/level.hpp" 29 | 30 | #include 31 | 32 | namespace logger { 33 | 34 | /** 35 | * @brief Abstract class for logger backends 36 | * @ingroup libLogger 37 | */ 38 | class LogBackend { 39 | public: 40 | virtual void log(LogLevel logLevel, 41 | const std::string& file, 42 | const unsigned int& line, 43 | const std::string& func, 44 | const std::string& message) = 0; 45 | virtual void relog(LogLevel /*logLevel*/, 46 | const std::string& /*file*/, 47 | const unsigned int& /*line*/, 48 | const std::string& /*func*/, 49 | const std::istream& /*stream*/) {} 50 | 51 | virtual ~LogBackend() {} 52 | }; 53 | 54 | } // namespace logger 55 | 56 | #endif // LOGGER_BACKEND_HPP 57 | -------------------------------------------------------------------------------- /libs/logger/formatter.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Dariusz Michaluk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Dariusz Michaluk (d.michaluk@samsung.com) 22 | * @brief Helper formatter for logger 23 | */ 24 | 25 | #ifndef LOGGER_FORMATTER_HPP 26 | #define LOGGER_FORMATTER_HPP 27 | 28 | #include "logger/level.hpp" 29 | 30 | #include 31 | 32 | namespace logger { 33 | 34 | class LogFormatter { 35 | public: 36 | static unsigned int getCurrentThread(void); 37 | static std::string getCurrentTime(void); 38 | static std::string getConsoleColor(LogLevel logLevel); 39 | static std::string getDefaultConsoleColor(void); 40 | static std::string stripProjectDir(const std::string& file, 41 | const std::string& rootDir); 42 | static std::string getHeader(LogLevel logLevel, 43 | const std::string& file, 44 | const unsigned int& line, 45 | const std::string& func); 46 | }; 47 | 48 | } // namespace logger 49 | 50 | #endif // LOGGER_FORMATTER_HPP 51 | -------------------------------------------------------------------------------- /libs/logger/level.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Jan Olszak (j.olszak@samsung.com) 22 | * @brief Functions to handle LogLevel 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "logger/level.hpp" 28 | 29 | #include 30 | #include 31 | 32 | namespace logger { 33 | 34 | LogLevel parseLogLevel(const std::string& level) 35 | { 36 | if (boost::iequals(level, "ERROR")) { 37 | return LogLevel::ERROR; 38 | } else if (boost::iequals(level, "WARN")) { 39 | return LogLevel::WARN; 40 | } else if (boost::iequals(level, "INFO")) { 41 | return LogLevel::INFO; 42 | } else if (boost::iequals(level, "DEBUG")) { 43 | return LogLevel::DEBUG; 44 | } else if (boost::iequals(level, "TRACE")) { 45 | return LogLevel::TRACE; 46 | } else if (boost::iequals(level, "HELP")) { 47 | return LogLevel::HELP; 48 | } else { 49 | throw std::runtime_error("Invalid LogLevel to parse"); 50 | } 51 | } 52 | 53 | std::string toString(const LogLevel logLevel) 54 | { 55 | switch (logLevel) { 56 | case LogLevel::ERROR: 57 | return "ERROR"; 58 | case LogLevel::WARN: 59 | return "WARN"; 60 | case LogLevel::INFO: 61 | return "INFO"; 62 | case LogLevel::DEBUG: 63 | return "DEBUG"; 64 | case LogLevel::TRACE: 65 | return "TRACE"; 66 | case LogLevel::HELP: 67 | return "HELP"; 68 | default: 69 | return "UNKNOWN"; 70 | } 71 | } 72 | } // namespace logger 73 | -------------------------------------------------------------------------------- /libs/logger/level.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Dariusz Michaluk (d.michaluk@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Dariusz Michaluk (d.michaluk@samsung.com) 22 | * @brief LogLevel 23 | */ 24 | 25 | #ifndef LOGGER_LEVEL_HPP 26 | #define LOGGER_LEVEL_HPP 27 | 28 | #include 29 | 30 | namespace logger { 31 | 32 | /** 33 | * @brief Available log levels 34 | * @ingroup libLogger 35 | */ 36 | enum class LogLevel : int { 37 | TRACE, ///< Most detailed log level 38 | DEBUG, ///< Debug logs 39 | INFO, ///< Information 40 | WARN, ///< Warnings 41 | ERROR, ///< Errors 42 | HELP ///< Helper logs 43 | }; 44 | 45 | /** 46 | * @param logLevel LogLevel 47 | * @return std::sting representation of the LogLevel value 48 | */ 49 | std::string toString(const LogLevel logLevel); 50 | 51 | /** 52 | * @param level string representation of log level 53 | * @return parsed LogLevel value 54 | */ 55 | LogLevel parseLogLevel(const std::string& level); 56 | 57 | } // namespace logger 58 | 59 | #endif // LOGGER_LEVEL_HPP 60 | -------------------------------------------------------------------------------- /libs/logger/libLogger.pc.in: -------------------------------------------------------------------------------- 1 | # Package Information for pkg-config 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=@LIB_INSTALL_DIR@ 6 | includedir=@INCLUDE_INSTALL_DIR@ 7 | 8 | Name: libLogger 9 | Description: Logger library 10 | Version: @_LIB_VERSION_@ 11 | Libs: -L${libdir} -l@PROJECT_NAME@ 12 | Cflags: -I${includedir}/ -I${includedir}/@PROJECT_NAME@ 13 | -------------------------------------------------------------------------------- /libs/logger/logger-scope.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Kostyra 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Kostyra (l.kostyra@samsung.com) 22 | * @brief Scope logger class implementation 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "logger/logger-scope.hpp" 28 | #include "logger/logger.hpp" 29 | 30 | namespace logger { 31 | 32 | SStreamWrapper::operator std::string() const 33 | { 34 | return mSStream.str(); 35 | } 36 | 37 | LoggerScope::LoggerScope(const std::string& file, 38 | const unsigned int line, 39 | const std::string& func, 40 | const std::string& message, 41 | const std::string& rootDir): 42 | mFile(file), 43 | mLine(line), 44 | mFunc(func), 45 | mMessage(message), 46 | mRootDir(rootDir) 47 | { 48 | if (logger::Logger::getLogLevel() <= logger::LogLevel::TRACE) { 49 | logger::Logger::logMessage(logger::LogLevel::TRACE, "Entering: " + mMessage, 50 | mFile, mLine, mFunc, mRootDir); 51 | } 52 | } 53 | 54 | LoggerScope::~LoggerScope() 55 | { 56 | if (logger::Logger::getLogLevel() <= logger::LogLevel::TRACE) { 57 | logger::Logger::logMessage(logger::LogLevel::TRACE, "Leaving: " + mMessage, 58 | mFile, mLine, mFunc, mRootDir); 59 | } 60 | } 61 | 62 | } // namespace logger 63 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Lukasz Kostyra (l.kostyra@samsung.com) 18 | # 19 | 20 | SET(UNIT_TESTS_FOLDER ${TESTS_FOLDER}/unit_tests) 21 | SET(SOCKET_TEST_FOLDER ${UNIT_TESTS_FOLDER}/socket_test_service) 22 | 23 | ADD_SUBDIRECTORY(scripts) 24 | ADD_SUBDIRECTORY(unit_tests) 25 | -------------------------------------------------------------------------------- /tests/cppcheck/cppcheck.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | OPT="" 3 | OPT+=" --enable=all" 4 | OPT+=" --std=c++11" 5 | OPT+=" --inconclusive" 6 | OPT+=" --suppressions tests/cppcheck/cppcheck.suppress" 7 | OPT+=" --suppress=missingIncludeSystem" 8 | OPT+=" --inline-suppr" 9 | OPT+=" -iCMakeFiles" 10 | OPT+=" -I common" 11 | OPT+=" -I libs" 12 | OPT+=" -I tests/unit_tests" 13 | OPT+=" -I tests/unit_tests/socket_test_service" 14 | OPT+=" -U__NR_capset" 15 | cppcheck ${OPT} --template=gcc $@ ./ 1>/dev/null 16 | -------------------------------------------------------------------------------- /tests/cppcheck/cppcheck.suppress: -------------------------------------------------------------------------------- 1 | // public API functions (or write tests to use them all) 2 | unusedFunction:libs/cargo/kvstore.cpp 3 | unusedFunction:libs/ipc/internals/processor.cpp 4 | unusedFunction:common/utils/fd-utils.cpp 5 | 6 | // complains that all constructors with 1 argument should be explicit 7 | noExplicitConstructor 8 | 9 | // required for C abstraction 10 | unusedStructMember:common/utils/initctl.cpp 11 | 12 | // functions called by external utilities 13 | unusedFunction:tests/unit_tests/ut.cpp 14 | -------------------------------------------------------------------------------- /tests/scripts/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Lukasz Kostyra (l.kostyra@samsung.com) 18 | # 19 | 20 | MESSAGE(STATUS "Installing scripts to " ${SCRIPT_INSTALL_DIR}) 21 | FILE(GLOB_RECURSE scripts *.py) 22 | 23 | 24 | ## Install ##################################################################### 25 | INSTALL(PROGRAMS ${scripts} DESTINATION ${SCRIPT_INSTALL_DIR}) 26 | -------------------------------------------------------------------------------- /tests/scripts/cargo_all_tests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import cargo_launch_test 4 | import sys 5 | 6 | # insert other test binaries to this array 7 | _testCmdTable = ["cargo-unit-tests"] 8 | 9 | for test in _testCmdTable: 10 | cargo_launch_test.launchTest([test] + sys.argv[1:]) 11 | -------------------------------------------------------------------------------- /tests/unit_tests/configs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | # 16 | # @file CMakeLists.txt 17 | # @author Dariusz Michaluk (d.michaluk@samsung.com) 18 | # 19 | 20 | MESSAGE(STATUS "Installing configs for the Unit Tests") 21 | 22 | ## Generate #################################################################### 23 | IF(NOT WITHOUT_SYSTEMD) 24 | CONFIGURE_FILE(systemd/cargo-socket-test.service.in 25 | ${CMAKE_BINARY_DIR}/systemd/cargo-socket-test.service) 26 | ENDIF(NOT WITHOUT_SYSTEMD) 27 | 28 | ## Install ##################################################################### 29 | INSTALL(FILES utils/file.txt 30 | DESTINATION ${CARGO_TEST_CONFIG_INSTALL_DIR}/utils) 31 | 32 | IF(NOT WITHOUT_SYSTEMD) 33 | INSTALL(FILES systemd/cargo-socket-test.socket 34 | ${CMAKE_BINARY_DIR}/systemd/cargo-socket-test.service 35 | DESTINATION ${SYSTEMD_UNIT_DIR}) 36 | ENDIF(NOT WITHOUT_SYSTEMD) 37 | -------------------------------------------------------------------------------- /tests/unit_tests/configs/systemd/cargo-socket-test.service.in: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Socket tests mini-service 3 | 4 | [Service] 5 | Type=simple 6 | ExecStart=${CMAKE_INSTALL_PREFIX}/bin/cargo-socket-test 7 | Sockets=cargo-socket-test.socket 8 | StartLimitInterval=0 9 | StartLimitBurst=0 10 | -------------------------------------------------------------------------------- /tests/unit_tests/configs/systemd/cargo-socket-test.socket: -------------------------------------------------------------------------------- 1 | [Socket] 2 | ListenStream=/run/cargo-socket-test.socket 3 | SocketMode=0755 4 | SmackLabelIPIn=* 5 | SmackLabelIPOut=@ 6 | Service=cargo-socket-test.service 7 | 8 | [Install] 9 | WantedBy=sockets.target 10 | -------------------------------------------------------------------------------- /tests/unit_tests/configs/utils/file.txt: -------------------------------------------------------------------------------- 1 | File content 2 | Line 1 3 | Line 2 4 | -------------------------------------------------------------------------------- /tests/unit_tests/socket_test_service/socket-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Kostyra 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Kostyra (l.kostyra@samsung.com) 22 | * @brief Mini-service for IPC Socket mechanism tests 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "socket-test.hpp" 28 | #include "logger/logger.hpp" 29 | #include "logger/backend-journal.hpp" 30 | #include "cargo-ipc/internals/socket.hpp" 31 | #include "cargo-ipc/exception.hpp" 32 | 33 | #include 34 | #include 35 | 36 | using namespace socket_test; 37 | using namespace cargo::ipc; 38 | using namespace cargo::ipc::internals; 39 | using namespace logger; 40 | 41 | /** 42 | * This is a single-usage program, only meant to test cargo::ipc::Socket module. 43 | * It's purpose is to be activated when needed by systemd socket activation mechanism. 44 | */ 45 | int main() 46 | { 47 | Logger::setLogLevel(LogLevel::TRACE); 48 | Logger::setLogBackend(new SystemdJournalBackend()); 49 | 50 | try { 51 | Socket listeningSocket(Socket::createUNIX(SOCKET_PATH)); 52 | if (listeningSocket.getFD() < 0) { 53 | LOGE("Failed to connect to socket!"); 54 | return 1; 55 | } 56 | 57 | std::shared_ptr clientSocket = listeningSocket.accept(); 58 | LOGI("Connected! Emitting message to client."); 59 | clientSocket->write(TEST_MESSAGE.c_str(), TEST_MESSAGE.size()); 60 | LOGI("Message sent through socket! Exiting."); 61 | } catch (const IPCException& e) { 62 | LOGE("IPC exception caught! " << e.what()); 63 | return 1; 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /tests/unit_tests/socket_test_service/socket-test.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Kostyra 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Lukasz Kostyra (l.kostyra@samsung.com) 22 | * @brief Mini-service for IPC Socket mechanism tests 23 | */ 24 | 25 | #ifndef TESTS_SOCKET_TEST_SERVICE_HPP 26 | #define TESTS_SOCKET_TEST_SERVICE_HPP 27 | 28 | #include 29 | 30 | namespace socket_test { 31 | 32 | const std::string SOCKET_PATH = "/run/cargo-socket-test.socket"; 33 | const std::string TEST_MESSAGE = "Some great messages, ey!"; 34 | 35 | } // namespace socket_test 36 | 37 | #endif // TESTS_SOCKET_TEST_SERVICE_HPP 38 | -------------------------------------------------------------------------------- /tests/unit_tests/ut.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Jan Olszak 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | 20 | /** 21 | * @file 22 | * @author Jan Olszak (j.olszak@samsung.com) 23 | * @brief Main file for the Cargo Daemon unit tests 24 | */ 25 | 26 | #include "config.hpp" 27 | 28 | #include "logger/logger.hpp" 29 | #include "logger/backend-stderr.hpp" 30 | 31 | #include 32 | 33 | #include "utils/signal.hpp" 34 | 35 | using namespace boost::unit_test; 36 | using namespace logger; 37 | 38 | test_suite* init_unit_test_suite(int /*argc*/, char** /*argv*/) 39 | { 40 | Logger::setLogLevel(LogLevel::TRACE); 41 | Logger::setLogBackend(new StderrBackend()); 42 | 43 | utils::signalBlock(SIGPIPE); 44 | return NULL; 45 | } 46 | -------------------------------------------------------------------------------- /tests/unit_tests/ut.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 22 | * @brief Common unit tests include file 23 | */ 24 | 25 | #ifndef UNIT_TESTS_UT_HPP 26 | #define UNIT_TESTS_UT_HPP 27 | 28 | #define BOOST_TEST_DYN_LINK 29 | #include 30 | 31 | #include 32 | 33 | #include 34 | 35 | /** 36 | * Usage example: 37 | * 38 | * MULTI_FIXTURE_TEST_CASE(Test, T, Fixture1, Fixture2, Fixture3) { 39 | * std::cout << T::i << "\n"; 40 | * } 41 | */ 42 | #define MULTI_FIXTURE_TEST_CASE(NAME, TPARAM, ...) \ 43 | typedef boost::mpl::vector<__VA_ARGS__> NAME##_fixtures; \ 44 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(NAME, TPARAM, NAME##_fixtures, TPARAM) 45 | 46 | 47 | /** 48 | * An exception message checker 49 | * 50 | * Usage example: 51 | * BOOST_CHECK_EXCEPTION(foo(), SomeException, WhatEquals("oops")) 52 | */ 53 | class WhatEquals { 54 | public: 55 | explicit WhatEquals(const std::string& message) 56 | : mMessage(message) {} 57 | 58 | template 59 | bool operator()(const T& e) 60 | { 61 | BOOST_WARN_EQUAL(e.what(), mMessage); // additional failure info 62 | return e.what() == mMessage; 63 | } 64 | private: 65 | std::string mMessage; 66 | }; 67 | 68 | #endif // UNIT_TESTS_UT_HPP 69 | -------------------------------------------------------------------------------- /tests/unit_tests/utils/ut-cargs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Krzysztof Dynowski (k.dynowski@samsumg.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Krzysztof Dynowski (k.dynowski@samsung.com) 22 | * @brief Unit tests of c-like args builder 23 | */ 24 | #include "config.hpp" 25 | #include "ut.hpp" 26 | 27 | #include "utils/c-args.hpp" 28 | 29 | BOOST_AUTO_TEST_SUITE(UtilsCArgsSuite) 30 | 31 | using namespace utils; 32 | 33 | BOOST_AUTO_TEST_CASE(CArgsBuilderTest1) 34 | { 35 | CArgsBuilder args; 36 | 37 | { 38 | std::string vx = std::to_string(20); 39 | args.add(std::to_string(10)) 40 | .add(vx); 41 | } 42 | BOOST_CHECK(std::string("10") == args[0]); 43 | BOOST_CHECK(std::string("20") == args[1]); 44 | 45 | { 46 | std::string vx = std::to_string(22); 47 | args.add(std::to_string(12)) 48 | .add(vx); 49 | } 50 | BOOST_CHECK(std::string("10") == args[0]); 51 | BOOST_CHECK(std::string("20") == args[1]); 52 | BOOST_CHECK(std::string("12") == args[2]); 53 | BOOST_CHECK(std::string("22") == args[3]); 54 | } 55 | 56 | BOOST_AUTO_TEST_CASE(CArgsBuilderTest2) 57 | { 58 | CArgsBuilder args; 59 | for (int i = 0; i < 10; ++i) { 60 | args.add(i + 10); 61 | } 62 | 63 | for (int i = 0; i < 10; ++i) { 64 | int t = std::stoi(args[i]); 65 | BOOST_CHECK(t == i + 10); 66 | } 67 | 68 | const char * const * c_array = args.c_array(); 69 | for (int i = 0; i < 10; ++i) { 70 | int t = std::stoi(std::string(c_array[i])); 71 | BOOST_CHECK(t == i + 10); 72 | } 73 | } 74 | 75 | BOOST_AUTO_TEST_SUITE_END() 76 | -------------------------------------------------------------------------------- /tests/unit_tests/utils/ut-channel.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License 15 | */ 16 | 17 | 18 | /** 19 | * @file 20 | * @author Jan Olszak (j.olszak@samsung.com) 21 | * @brief Unit tests of the channel class 22 | */ 23 | 24 | #include "config.hpp" 25 | 26 | #include "ut.hpp" 27 | 28 | #include "utils/channel.hpp" 29 | #include "utils/execute.hpp" 30 | 31 | BOOST_AUTO_TEST_SUITE(ChannelSuite) 32 | 33 | using namespace utils; 34 | 35 | BOOST_AUTO_TEST_CASE(ConstructorDestructor) 36 | { 37 | Channel c; 38 | } 39 | 40 | BOOST_AUTO_TEST_CASE(SetLeftRight) 41 | { 42 | const int TEST_PASSED = 0; 43 | const int ERROR = 1; 44 | const int DATA = 1234; 45 | 46 | Channel c; 47 | 48 | pid_t pid = ::fork(); 49 | if (pid == -1) { 50 | BOOST_REQUIRE(false); 51 | } 52 | 53 | if (pid == 0) { 54 | try { 55 | c.setLeft(); 56 | c.write(DATA); 57 | c.shutdown(); 58 | ::_exit(TEST_PASSED); 59 | } catch(...) { 60 | ::_exit(ERROR); 61 | } 62 | } 63 | 64 | c.setRight(); 65 | 66 | int recData = c.read(); 67 | 68 | BOOST_REQUIRE(recData == DATA); 69 | 70 | int status = -1; 71 | BOOST_REQUIRE(utils::waitPid(pid, status)); 72 | BOOST_REQUIRE(status == TEST_PASSED); 73 | c.shutdown(); 74 | 75 | } 76 | 77 | BOOST_AUTO_TEST_SUITE_END() 78 | -------------------------------------------------------------------------------- /tests/unit_tests/utils/ut-fd-utils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Pawel Kubik (p.kubik@samsung.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | 20 | /** 21 | * @file 22 | * @author Pawel Kubik (p.kubik@samsung.com) 23 | * @brief Unit tests of fd utils 24 | */ 25 | 26 | #include "config.hpp" 27 | 28 | #include "ut.hpp" 29 | 30 | #include "utils/fd-utils.hpp" 31 | 32 | #include "logger/logger.hpp" 33 | 34 | 35 | using namespace utils; 36 | 37 | 38 | BOOST_AUTO_TEST_SUITE(FDUtilsSuite) 39 | 40 | BOOST_AUTO_TEST_CASE(GetSetMaxFDNumber) 41 | { 42 | unsigned oldLimit = utils::getMaxFDNumber(); 43 | unsigned newLimit = 50; 44 | 45 | utils::setMaxFDNumber(newLimit); 46 | BOOST_CHECK_EQUAL(newLimit, utils::getMaxFDNumber()); 47 | 48 | utils::setMaxFDNumber(oldLimit); 49 | BOOST_CHECK_EQUAL(oldLimit, utils::getMaxFDNumber()); 50 | } 51 | 52 | BOOST_AUTO_TEST_SUITE_END() 53 | -------------------------------------------------------------------------------- /tests/unit_tests/utils/ut-glib-loop.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Lukasz Pawelczyk 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | 20 | /** 21 | * @file 22 | * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com) 23 | * @brief Unit tests of glib-loop 24 | */ 25 | 26 | #include "config.hpp" 27 | 28 | #include "ut.hpp" 29 | 30 | #include "utils/glib-loop.hpp" 31 | 32 | #include 33 | 34 | BOOST_AUTO_TEST_SUITE(GlibLoopSuite) 35 | 36 | using namespace utils; 37 | 38 | 39 | namespace { 40 | 41 | const unsigned int TIMER_INTERVAL_MS = 100; 42 | const unsigned int TIMER_NUMBER = 4; 43 | const unsigned int TIMER_WAIT_FOR = 2 * TIMER_NUMBER * TIMER_INTERVAL_MS; 44 | 45 | } // namespace 46 | 47 | BOOST_AUTO_TEST_CASE(GlibLoopTest) 48 | { 49 | ScopedGlibLoop loop; 50 | } 51 | 52 | BOOST_AUTO_TEST_CASE(GlibTimerEvent) 53 | { 54 | ScopedGlibLoop loop; 55 | std::atomic_uint counter(0); 56 | 57 | CallbackGuard guard; 58 | 59 | auto callback = [&]()-> bool { 60 | if (++counter >= TIMER_NUMBER) { 61 | return false; 62 | } 63 | return true; 64 | }; 65 | 66 | Glib::addTimerEvent(TIMER_INTERVAL_MS, callback, guard); 67 | 68 | BOOST_CHECK(counter < TIMER_NUMBER); 69 | BOOST_CHECK(guard.waitForTrackers(TIMER_WAIT_FOR)); 70 | BOOST_CHECK_EQUAL(counter, TIMER_NUMBER); 71 | } 72 | 73 | BOOST_AUTO_TEST_SUITE_END() 74 | -------------------------------------------------------------------------------- /tests/unit_tests/utils/ut-same-thread-guard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Piotr Bartosiewicz 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | 20 | /** 21 | * @file 22 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) 23 | * @brief Unit tests of same thread guard 24 | */ 25 | 26 | #include "config.hpp" 27 | 28 | #include "ut.hpp" 29 | 30 | #include "utils/same-thread-guard.hpp" 31 | #include 32 | 33 | #ifdef ENABLE_SAME_THREAD_GUARD 34 | 35 | BOOST_AUTO_TEST_SUITE(SameThreadGuardSuite) 36 | 37 | using namespace utils; 38 | 39 | BOOST_AUTO_TEST_CASE(Simple) 40 | { 41 | SameThreadGuard guard; 42 | BOOST_CHECK(guard.check()); 43 | BOOST_CHECK(guard.check()); 44 | guard.reset(); 45 | BOOST_CHECK(guard.check()); 46 | BOOST_CHECK(guard.check()); 47 | } 48 | 49 | BOOST_AUTO_TEST_CASE(Thread) 50 | { 51 | SameThreadGuard guard; 52 | 53 | std::thread([&] { 54 | BOOST_CHECK(guard.check()); 55 | }).join(); 56 | 57 | BOOST_CHECK(!guard.check()); 58 | BOOST_CHECK(!guard.check()); 59 | 60 | guard.reset(); 61 | BOOST_CHECK(guard.check()); 62 | 63 | std::thread([&] { 64 | BOOST_CHECK(!guard.check()); 65 | }).join(); 66 | } 67 | 68 | BOOST_AUTO_TEST_SUITE_END() 69 | 70 | #endif // ENABLE_SAME_THREAD_GUARD 71 | -------------------------------------------------------------------------------- /tests/unit_tests/utils/ut-text.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 3 | * 4 | * Contact: Krzysztof Dynowski (k.dynowski@samsumg.com) 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License 17 | */ 18 | 19 | /** 20 | * @file 21 | * @author Krzysztof Dynowski (k.dynowski@samsung.com) 22 | * @brief Unit tests of text helpers 23 | */ 24 | 25 | #include "config.hpp" 26 | 27 | #include "ut.hpp" 28 | 29 | #include "utils/text.hpp" 30 | 31 | BOOST_AUTO_TEST_SUITE(TextUtilsSuite) 32 | 33 | BOOST_AUTO_TEST_CASE(SplitText) 34 | { 35 | std::vector v; 36 | v = utils::split("", ","); 37 | BOOST_CHECK(v.size() == 0); 38 | v = utils::split("a", ","); 39 | BOOST_CHECK(v.size() == 1); 40 | v = utils::split(",", ","); 41 | BOOST_CHECK(v.size() == 2); 42 | v = utils::split("1,2", ","); 43 | BOOST_CHECK(v.size() == 2); 44 | } 45 | 46 | BOOST_AUTO_TEST_SUITE_END() 47 | --------------------------------------------------------------------------------