summaryrefslogtreecommitdiff
path: root/src/components/utils/include
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/utils/include')
-rw-r--r--src/components/utils/include/utils/file_system.h224
-rw-r--r--src/components/utils/include/utils/log_message_loop_thread.h10
-rw-r--r--src/components/utils/include/utils/optional.h83
-rw-r--r--src/components/utils/include/utils/signals.h54
-rw-r--r--src/components/utils/include/utils/system_time_handler.h151
-rw-r--r--src/components/utils/include/utils/timer.h2
6 files changed, 406 insertions, 118 deletions
diff --git a/src/components/utils/include/utils/file_system.h b/src/components/utils/include/utils/file_system.h
index 22c200934f..e26fef34bb 100644
--- a/src/components/utils/include/utils/file_system.h
+++ b/src/components/utils/include/utils/file_system.h
@@ -33,17 +33,19 @@
#ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_FILE_SYSTEM_H_
#define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_FILE_SYSTEM_H_
-#include <string.h>
#include <stdint.h>
+#include <string.h>
+#include <fstream>
+#include <iostream>
#include <string>
#include <vector>
-#include <iostream>
-#include <fstream>
+#include <time.h>
namespace file_system {
/**
- * @brief Get available disc space.
+ * @brief Get available disc space of directory. Returns 0 if the directory does
+ * not exist.
*
* @param path to directory
* @return free disc space.
@@ -51,22 +53,23 @@ namespace file_system {
uint64_t GetAvailableDiskSpace(const std::string& path);
/*
- * @brief Get size of current directory
+ * @brief Get size of given directory
*
* @param path to directory
+ * @return size of directory, return 0 if dir not exist
*/
size_t DirectorySize(const std::string& path);
/*
- * @brief Get size of current file
+ * @brief Get size of given file
*
* @param path to file
* @return size of file, return 0 if file not exist
*/
-int64_t FileSize(const std::string& path);
+uint64_t FileSize(const std::string& path);
/**
- * @brief Creates directory
+ * @brief Creates directory with owner_all permissions
* @param name path to directory
* @return path to created directory.
*/
@@ -80,76 +83,75 @@ std::string CreateDirectory(const std::string& name);
bool CreateDirectoryRecursively(const std::string& path);
/**
- * @brief Checks the file to see whether the file is a directory
- * @param name path to file
- * @return returns true if file is directory.
- */
+ * @brief Checks the file to see whether the file is a directory
+ * @param name path to file
+ * @return returns true if file is directory.
+ */
bool IsDirectory(const std::string& name);
/**
- * @brief Is directory exist
- * @param name path to directory
- * @return returns true if directory is exists.
- */
+ * @brief Does directory exist
+ * @param name path to directory
+ * @return returns true if the file exists and is a directory.
+ */
bool DirectoryExists(const std::string& name);
/**
- * @brief Is file exist
- * @param name path to file
- * @return returns true if file is exists.
- */
+ * @brief Does file exist
+ * @param name path to file
+ * @return returns true if the file exists.
+ */
bool FileExists(const std::string& name);
/**
- * @brief Writes to file
- *
- * @remark - create file if it doesn't exist
- * @param name path to file
- * @param data data to write
- * @return returns true if the operation is successfully.
- */
+ * @brief Writes to file
+ *
+ * @remark - create file if it doesn't exist
+ * @param name path to file
+ * @param data data to write
+ * @return returns true if the operation is successful.
+ */
bool Write(const std::string& file_name,
const std::vector<uint8_t>& data,
std::ios_base::openmode mode = std::ios_base::out);
/**
- * @brief Opens file stream for writing
- * @param file_name path to file to write data to
- * @return returns pointer to opened stream in case of success;
- * otherwise returns NULL
- */
+ * @brief Opens file stream for writing
+ * @param file_name path to file to write data to
+ * @return returns pointer to opened stream in case of success;
+ * otherwise returns NULL
+ */
std::ofstream* Open(const std::string& file_name,
std::ios_base::openmode mode = std::ios_base::out);
/**
- * @brief Writes to file stream
- * @param file_stream file stream to be written to
- * @param data data to be written to file
- * @param data_size size of data to be written to file
- * @return returns true if the operation is successfully.
- */
+ * @brief Writes to file stream
+ * @param file_stream file stream to be written to
+ * @param data data to be written to file
+ * @param data_size size of data to be written to file
+ * @return returns true if the operation is successful.
+ */
bool Write(std::ofstream* const file_stream,
const uint8_t* data,
uint32_t data_size);
/**
- * @brief Closes file stream
- * @param file_stream file stream to be closed
- */
+ * @brief Closes file stream
+ * @param file_stream file stream to be closed
+ */
void Close(std::ofstream* file_stream);
/**
- * @brief Returns current working directory path
- * If filename begins with "/", return unchanged filename
- * @param name file name
- * @return returns full file path.
- */
+ * @brief Returns current working directory path
+ * @return returns full file path.
+ */
std::string CurrentWorkingDirectory();
/**
- * @brief Allows to obtaine absolute path for certain path.
- * @param path the file name for which absolute path have to be calculated.
- * @return absolute path for certain path.
+ * @brief Convert a path to its absolute form.
+ * @param path the file name to convert.
+ * @return corresponding absolute path for a valid path, otherwise an empty
+ * string.
*/
std::string GetAbsolutePath(const std::string& path);
@@ -162,55 +164,63 @@ std::string GetAbsolutePath(const std::string& path);
bool IsFileNameValid(const std::string& file_name);
/**
- * @brief Removes file
- *
- * @param name path to file
- * @return returns true if the file is successfully deleted.
- */
+ * @brief Removes file
+ *
+ * @param name path to file
+ * @return returns true if the file is successfully deleted.
+ */
bool DeleteFile(const std::string& name);
/**
+ * @brief Removes contents of directory but not directory itself
+ *
+ * @param directory_name path to directory.
+ */
+void remove_directory_content(const std::string& directory_name);
+
+/**
* @brief Removes directory.
*
- * @param name path to directory.
+ * @param directory_name path to directory.
* @param is_recursively true if you need delete directory recursively,
- *otherwise false.
+ * otherwise false. A non-empty directory with is_recursively == false will
+ * return false.
* @return returns true if the directory is successfully deleted.
*/
bool RemoveDirectory(const std::string& directory_name,
bool is_recursively = true);
/**
- * @brief Check access rights
- *
- * @param name path to file.
- * @param how Read/write attribute.
- * @return returns true if file has the given mode.
- */
+ * @brief Check access rights
+ *
+ * @param name path to file.
+ * @param how Read/write attribute.
+ * @return returns true if file has the given mode.
+ */
bool IsAccessible(const std::string& name, int32_t how);
/**
- * @brief Check access rights for writing
- *
- * @param name path to file or folder
- * @return returns true if has access rights.
- */
+ * @brief Check access rights for writing
+ *
+ * @param name path to file or folder
+ * @return returns true if has access rights.
+ */
bool IsWritingAllowed(const std::string& name);
/**
- * @brief Check access rights for reading
- *
- * @param name path to file.
- * @return returns true if file has access rights.
- */
+ * @brief Check access rights for reading
+ *
+ * @param name path to file.
+ * @return returns true if file has access rights.
+ */
bool IsReadingAllowed(const std::string& name);
/**
- * @brief Lists all files in given directory
- *
- * @param name path to directory.
- * @return returns list of files.
- */
+ * @brief Lists all files in given directory
+ *
+ * @param name path to directory.
+ * @return returns list of files.
+ */
std::vector<std::string> ListFiles(const std::string& directory_name);
/**
@@ -223,30 +233,30 @@ bool WriteBinaryFile(const std::string& name,
const std::vector<uint8_t>& contents);
/**
- * @brief Reads from file
- *
- * @param name path to file
- * @param result read data
- * @return returns true if the operation is successfully.
- */
+ * @brief Reads from file
+ *
+ * @param name path to file
+ * @param result read data
+ * @return returns true if the operation is successfully.
+ */
bool ReadBinaryFile(const std::string& name, std::vector<uint8_t>& result);
bool ReadFile(const std::string& name, std::string& result);
/**
- * @brief Convert special symbols in system path to percent-encoded
- *
- * @param name path to file
- * @return returns converted path.
-*/
+ * @brief Convert special symbols in system path to percent-encoded
+ *
+ * @param name path to file
+ * @return returns converted path.
+ */
const std::string ConvertPathForURL(const std::string& path);
/**
- * @brief Create empty file
- *
- * @param name path to file
- * @return if result success return true
-*/
+ * @brief Create empty file
+ *
+ * @param name path to file
+ * @return if result success return true
+ */
bool CreateFile(const std::string& path);
/**
@@ -254,28 +264,26 @@ bool CreateFile(const std::string& path);
* @param path Path to file
* @return Modification time in nanoseconds
*/
-uint64_t GetFileModificationTime(const std::string& path);
+time_t GetFileModificationTime(const std::string& path);
/**
- * @brief Copy file from source to destination
- *
- * @param src Source file path
- * @param dst Destination file path
- * @return if result success return true
-*/
+ * @brief Copy file from source to destination
+ *
+ * @param src Source file path
+ * @param dst Destination file path
+ * @return if result success return true
+ */
bool CopyFile(const std::string& src, const std::string& dst);
/**
- * @brief Move file from source to destination
- *
- * @param src Source file path
- * @param dst Destination file path
- * @return if result success return true
-*/
+ * @brief Move file from source to destination
+ *
+ * @param src Source file path
+ * @param dst Destination file path
+ * @return if result success return true
+ */
bool MoveFile(const std::string& src, const std::string& dst);
-void remove_directory_content(const std::string& directory_name);
-
} // namespace file_system
#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_FILE_SYSTEM_H_
diff --git a/src/components/utils/include/utils/log_message_loop_thread.h b/src/components/utils/include/utils/log_message_loop_thread.h
index d6a9edd072..b20b43c251 100644
--- a/src/components/utils/include/utils/log_message_loop_thread.h
+++ b/src/components/utils/include/utils/log_message_loop_thread.h
@@ -56,18 +56,14 @@ typedef std::queue<LogMessage> LogMessageQueue;
typedef threads::MessageLoopThread<LogMessageQueue>
LogMessageLoopThreadTemplate;
-class LogMessageHandler : public LogMessageLoopThreadTemplate::Handler {
- public:
- virtual void Handle(const LogMessage message) OVERRIDE;
-};
-
-class LogMessageLoopThread : public LogMessageLoopThreadTemplate {
+class LogMessageLoopThread : public LogMessageLoopThreadTemplate,
+ public LogMessageLoopThreadTemplate::Handler {
public:
LogMessageLoopThread();
~LogMessageLoopThread();
+ void Handle(const LogMessage message) OVERRIDE;
private:
- LogMessageHandler* handler_;
DISALLOW_COPY_AND_ASSIGN(LogMessageLoopThread);
};
diff --git a/src/components/utils/include/utils/optional.h b/src/components/utils/include/utils/optional.h
new file mode 100644
index 0000000000..0395945de0
--- /dev/null
+++ b/src/components/utils/include/utils/optional.h
@@ -0,0 +1,83 @@
+#ifndef ERROR_OR_H
+#define ERROR_OR_H
+#include <string>
+#include "utils/macro.h"
+
+namespace utils {
+
+/**
+ * @brief The Optional class is able to keep value, manage it it is empty and
+ * specify error code.
+ * Can be used as return value of function that is not guarantee that vaue will
+ * be returned
+ *
+ */
+template <typename ObjectType, typename ErrorType = std::string>
+class Optional {
+ public:
+ /**
+ * @brief The OptionalEmpty enum enum with one value to specify that Optional
+ * is not initialized
+ */
+ enum OptionalEmpty { EMPTY };
+
+ /**
+ * @brief Optional constructor with object initialization
+ * @param object object to initialize Optional
+ */
+ Optional(ObjectType& object)
+ : object_(&object), error_(), is_initialized_(true) {}
+
+ /**
+ * @brief Optional constructor with object and error initialization
+ * @param object object to initialize Optional
+ * @param error error code initialization
+ */
+ Optional(ObjectType& object, ErrorType error)
+ : object_(&object), error_(error), is_initialized_(true) {}
+
+ /**
+ * @brief Optional constructir without object initialization
+ * @param empty manadatory parameter for explicit specifying that Optional is
+ * empty
+ * @param error error code initialization
+ */
+ Optional(OptionalEmpty empty, ErrorType error)
+ : object_(nullptr), error_(error), is_initialized_(false) {}
+
+ /**
+ * @brief Optional empty optional initialization without specifying error code
+ * @param empty manadatory parameter for explicit specifying that Optional is
+ * empty
+ */
+ Optional(OptionalEmpty empty)
+ : object_(nullptr), error_(), is_initialized_(false) {}
+
+ /**
+ * @brief operator bool operator for checking if optional is initialized
+ */
+ operator bool() const {
+ return is_initialized_;
+ }
+
+ /**
+ * @brief operator * access to object
+ * @return
+ */
+ ObjectType& operator*() const {
+ DCHECK(is_initialized_);
+ return *object_;
+ }
+
+ ErrorType error() const {
+ return error_;
+ }
+
+ private:
+ ObjectType* object_;
+ ErrorType error_;
+ bool is_initialized_;
+};
+
+} // utils utils
+#endif // ERROR_OR_H
diff --git a/src/components/utils/include/utils/signals.h b/src/components/utils/include/utils/signals.h
index 72d29a9e28..bda83f315f 100644
--- a/src/components/utils/include/utils/signals.h
+++ b/src/components/utils/include/utils/signals.h
@@ -38,11 +38,61 @@ typedef void (*sighandler_t)(int);
#else
#include <signal.h>
#endif
+#include "appMain/low_voltage_signals_handler.h"
namespace utils {
-bool UnsibscribeFromTermination();
-bool WaitTerminationSignals(sighandler_t sig_handler);
+class Signals {
+ public:
+ /**
+ * @brief Unsubscribe thread from termination signals SIGINT and SIGTERM
+ * @return True if thread unsubscribed successfuly, otherwise false
+ */
+ static bool UnsubscribeFromTermination();
+
+ /**
+ * @brief Triggers thread to wait for termination signals SIGINT and SIGTERM
+ * @param sig_handler - handler to work with signals specidied above
+ * @return True if handler handles signals successfuly, otherwise false
+ */
+ static bool WaitTerminationSignals(sighandler_t sig_handler);
+
+ /**
+ * @brief Unsubscribe thread from low voltage signals
+ * SIGLOWVOLTAGE, SIGWAKEUP and SIGIGNOFF
+ * @return True if thread unsubscribed successfuly, otherwise false
+ */
+ static bool UnsubscribeFromLowVoltageSignals(
+ const main_namespace::LowVoltageSignalsOffset& offset_data);
+
+ /**
+ * @brief Sends signal to specified process
+ * @param signal to send
+ * @param destination process signal to be sent to
+ */
+ static void SendSignal(const int signo, const pid_t pid);
+
+ /**
+ * @brief Creates child process
+ * @return created process id or -1 in case of error
+ */
+ static pid_t Fork();
+
+ /**
+ * @brief Wait for child process to be terminated
+ * @param cpid - process to wait for termination
+ * @param status store status information in the int to which it points
+ * @param options - options for exit form function
+ * detailed options can be found here: https://linux.die.net/man/2/waitpid
+ */
+ static void WaitPid(pid_t cpid, int* status, int options);
+
+ /**
+ * @brief Exits for process
+ * @param status - exit status code
+ */
+ static void ExitProcess(const int status);
+};
} // namespace utils
diff --git a/src/components/utils/include/utils/system_time_handler.h b/src/components/utils/include/utils/system_time_handler.h
new file mode 100644
index 0000000000..15b2dd0cca
--- /dev/null
+++ b/src/components/utils/include/utils/system_time_handler.h
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2018, Ford Motor Company
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of the Ford Motor Company nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_SYSTEM_TIME_HANDLER_H_
+#define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_SYSTEM_TIME_HANDLER_H_
+
+#include <time.h>
+
+namespace utils {
+
+/**
+ * @brief The SystemTimeListener interface.
+ * This interface allows to get notifications whenever
+ * system time appears or fails to appear.
+ */
+class SystemTimeListener {
+ public:
+ /**
+ * @brief OnSystemTimeArrived Notify about system time
+ * in utc format
+ * @param utc_time current system time.
+ */
+ virtual void OnSystemTimeArrived(const time_t utc_time) = 0;
+};
+
+/**
+ * @brief SystemTimeHandler the interface which provides the necessary
+ * public API to work with system time. The class does not implement
+ * any logic it's public api forwards call to the private virtual
+ * methods which are suppose to be defined within specific implementation.
+ */
+class SystemTimeHandler {
+ public:
+ /**
+ * @brief SystemTimeHandler creates an instance
+ * for this class.
+ */
+ SystemTimeHandler();
+
+ /**
+ * @brief QuerySystemTime provides the public interface
+ * to retrieve the system time. Interface uses private implementation
+ * hence the logic will be defined within descendant class.
+ */
+ void QuerySystemTime();
+
+ /**
+ * @brief SubscribeOnSystemTime allows to subscribe listener
+ * to the certain event. This class does not provide such storage.
+ * It rather uses private pure virtual function. So the final behaviour
+ * should be defined within the descendant class
+ */
+ void SubscribeOnSystemTime(SystemTimeListener* listener);
+
+ /**
+ * @brief UnsubscribeFromSystemTime allows to unsubscribe listener
+ * from the certain event. This class does not manipulate with storage.
+ * It rather uses private pure virtual function. So the final behaviour
+ * should be defined within the descendant class
+ */
+ void UnsubscribeFromSystemTime(SystemTimeListener* listener);
+
+ /**
+ * @brief GetUTCTime allows to obtain cached result for the
+ * GetSystemTime request
+ * @return utc time.
+ */
+ time_t GetUTCTime();
+
+ /**
+ * @brief Checks if system time is ready
+ * and can be requested by GetSystemTime request
+ * @return True if HMI is ready to provide UTC time
+ * otherwise False
+ */
+ bool system_time_can_be_received() const;
+
+ /**
+ * @brief ~SystemTimeHandler destroys the object
+ */
+ virtual ~SystemTimeHandler();
+
+ private:
+ /**
+ * @brief DoSystemTimeQuery responsible for the system time querying.
+ * It is up to implementator how exactly system is going to receive this time.
+ */
+ virtual void DoSystemTimeQuery() = 0;
+
+ /**
+ * @brief DoSubscribe implements the logic which allows to handle
+ * subscription process. The handling logic should be defined within
+ * descendant class.
+ */
+ virtual void DoSubscribe(SystemTimeListener* listener) = 0;
+
+ /**
+ * @brief DoUnsubscribe implements the logic which allows to handle
+ * unsubscription process. The handling logic should be defined within
+ * descendant class.
+ */
+ virtual void DoUnsubscribe(SystemTimeListener* listener) = 0;
+
+ /**
+ * @brief FetchSystemTime allows to obtain the cached result
+ * for the GetSystemTime request.
+ * @return utc time.
+ */
+ virtual time_t FetchSystemTime() = 0;
+
+ /**
+ * @brief Checks if UTC time is ready to provided by HMI
+ * and can be requested by GetSystemTime request
+ * @return True if HMI is ready to provide UTC time
+ * otherwise False
+ */
+ virtual bool utc_time_can_be_received() const = 0;
+};
+
+} // namespace utils
+
+#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_SYSTEM_TIME_HANDLER_H_
diff --git a/src/components/utils/include/utils/timer.h b/src/components/utils/include/utils/timer.h
index ab3d48ef6f..e391db992c 100644
--- a/src/components/utils/include/utils/timer.h
+++ b/src/components/utils/include/utils/timer.h
@@ -218,7 +218,7 @@ class Timer {
mutable sync_primitives::Lock state_lock_;
- mutable std::auto_ptr<TimerDelegate> delegate_;
+ mutable std::unique_ptr<TimerDelegate> delegate_;
threads::Thread* thread_;
/**