From 46afa99b27a1e9b6ce0753d480408c1cd9d5e5ff Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Mon, 18 May 2015 15:09:52 +0300 Subject: [PATCH] Refactored POSIX tools --- CMakeLists.txt | 1 + .../{posix_tools => posix}/include.mk | 0 .../file_event_tracer.hpp | 43 +++++++++++-------- .../file_storage_backend.hpp | 40 +++++++---------- 4 files changed, 41 insertions(+), 43 deletions(-) rename libuavcan_drivers/{posix_tools => posix}/include.mk (100%) rename libuavcan_drivers/{posix_tools/include/posix_tools => posix/include/uavcan_posix/dynamic_node_id_server}/file_event_tracer.hpp (65%) rename libuavcan_drivers/{posix_tools/include/posix_tools => posix/include/uavcan_posix/dynamic_node_id_server}/file_storage_backend.hpp (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b889336f0d..6636e188d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,5 +33,6 @@ add_subdirectory(libuavcan) # if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") message(STATUS "Adding Linux support library") + add_subdirectory(libuavcan_drivers/posix) add_subdirectory(libuavcan_drivers/linux) endif () diff --git a/libuavcan_drivers/posix_tools/include.mk b/libuavcan_drivers/posix/include.mk similarity index 100% rename from libuavcan_drivers/posix_tools/include.mk rename to libuavcan_drivers/posix/include.mk diff --git a/libuavcan_drivers/posix_tools/include/posix_tools/file_event_tracer.hpp b/libuavcan_drivers/posix/include/uavcan_posix/dynamic_node_id_server/file_event_tracer.hpp similarity index 65% rename from libuavcan_drivers/posix_tools/include/posix_tools/file_event_tracer.hpp rename to libuavcan_drivers/posix/include/uavcan_posix/dynamic_node_id_server/file_event_tracer.hpp index 719e34694d..60fe4448fb 100644 --- a/libuavcan_drivers/posix_tools/include/posix_tools/file_event_tracer.hpp +++ b/libuavcan_drivers/posix/include/uavcan_posix/dynamic_node_id_server/file_event_tracer.hpp @@ -1,11 +1,17 @@ -/* - * Copyright (C) 2015 Pavel Kirienko - */ +/**************************************************************************** +* +* Copyright (c) 2015 PX4 Development Team. All rights reserved. +* Author: Pavel Kirienko +* David Sidrane +* +****************************************************************************/ -#pragma once +#ifndef UAVCAN_POSIX_DYNAMIC_NODE_ID_SERVER_FILE_EVENT_TRACER_HPP_INCLUDED +#define UAVCAN_POSIX_DYNAMIC_NODE_ID_SERVER_FILE_EVENT_TRACER_HPP_INCLUDED #include #include +#include namespace uavcan_posix { @@ -19,9 +25,7 @@ class FileEventTracer : public uavcan::dynamic_node_id_server::IEventTracer /** * Maximum length of full path to log file */ - - enum { MaxPathLength = 128, FormatBufferLength = 64 }; - + enum { MaxPathLength = 128 }; /** * This type is used for the path @@ -29,32 +33,32 @@ class FileEventTracer : public uavcan::dynamic_node_id_server::IEventTracer typedef uavcan::Array, uavcan::ArrayModeDynamic, MaxPathLength> PathString; - PathString path_; - -public: - - FileEventTracer() { } - virtual void onEvent(uavcan::dynamic_node_id_server::TraceCode code, uavcan::int64_t argument) { + using namespace std; + struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); + int fd = open(path_.c_str(), O_WRONLY | O_CREAT | O_APPEND); if (fd >= 0 ) { + const int FormatBufferLength = 64; char buffer[FormatBufferLength + 1]; - int n = snprintf(buffer, FormatBufferLength, "%d.%ld,%d,%lld\n", ts.tv_sec, ts.tv_nsec, code, argument); + int n = snprintf(buffer, FormatBufferLength, "%d.%ld,%d,%lld\n", ts.tv_sec, ts.tv_nsec, code, argument); write(fd, buffer, n); close(fd); } } - /** - * Initializes the File based event trace - * - */ +public: + FileEventTracer() { } + + /** + * Initializes the file based event tracer. + */ int init(const PathString & path) { using namespace std; @@ -73,8 +77,9 @@ public: } return rv; } - }; } } + +#endif // Include guard diff --git a/libuavcan_drivers/posix_tools/include/posix_tools/file_storage_backend.hpp b/libuavcan_drivers/posix/include/uavcan_posix/dynamic_node_id_server/file_storage_backend.hpp similarity index 88% rename from libuavcan_drivers/posix_tools/include/posix_tools/file_storage_backend.hpp rename to libuavcan_drivers/posix/include/uavcan_posix/dynamic_node_id_server/file_storage_backend.hpp index e5233420cf..139004dccd 100644 --- a/libuavcan_drivers/posix_tools/include/posix_tools/file_storage_backend.hpp +++ b/libuavcan_drivers/posix/include/uavcan_posix/dynamic_node_id_server/file_storage_backend.hpp @@ -6,15 +6,16 @@ * ****************************************************************************/ -#pragma once +#ifndef UAVCAN_POSIX_DYNAMIC_NODE_ID_SERVER_FILE_STORAGE_BACKEND_HPP_INCLUDED +#define UAVCAN_POSIX_DYNAMIC_NODE_ID_SERVER_FILE_STORAGE_BACKEND_HPP_INCLUDED #include -#include +#include #include #include -#include #include #include +#include #include @@ -30,24 +31,16 @@ class FileStorageBackend : public uavcan::dynamic_node_id_server::IStorageBacken /** * Maximum length of full path including / and key max */ - enum { MaxPathLength = 128 }; - /** * This type is used for the path */ typedef uavcan::Array, uavcan::ArrayModeDynamic, MaxPathLength> PathString; - PathString base_path; -public: - - FileStorageBackend() { } - - virtual String get(const String& key) const { using namespace std; @@ -75,7 +68,6 @@ public: } } return value; - } virtual void set(const String& key, const String& value) @@ -91,16 +83,17 @@ public: } } - /** - * Initializes the File based back end storage by passing to a path to - * the directory where the key named files will be stored. - * This the return result should be 0 on success. - * If it is -ErrInvalidConfiguration then the the path name is too long to - * Accommodate the trailing slash and max key length; - * - */ +public: + FileStorageBackend() { } - int init(const PathString & path) + /** + * Initializes the file based backend storage by passing a path to + * the directory where the key named files will be stored. + * The return value should be 0 on success. + * If it is -ErrInvalidConfiguration then the the path name is too long to + * accommodate the trailing slash and max key length. + */ + int init(const PathString& path) { using namespace std; @@ -108,7 +101,6 @@ public: if (path.size() > 0) { - base_path = path.c_str(); if (base_path.back() == '/') @@ -132,10 +124,10 @@ public: } } return rv; - } - }; } } + +#endif // Include guard