Merge branch 'dynamic_node_id_raft' of https://github.com/UAVCAN/uavcan into dynamic_node_id_raft

This commit is contained in:
Pavel Kirienko
2015-05-14 00:08:38 +03:00
3 changed files with 98 additions and 7 deletions
+7
View File
@@ -0,0 +1,7 @@
#
# Copyright (C) 2015 David Sidrane <david_s5@nscdg.net>
#
LIBUAVCAN_POSIX_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
LIBUAVCAN_POSIX_INC := $(LIBUAVCAN_POSIX_DIR)include/
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#pragma once
#include <uavcan/protocol/dynamic_node_id_server/event.hpp>
#include <time.h>
namespace uavcan_posix
{
namespace dynamic_node_id_server
{
/**
* This interface implements a POSIX compliant file based IEventTracer interface
*/
class FileEventTracer : public uavcan::dynamic_node_id_server::IEventTracer
{
/**
* Maximum length of full path to log file
*/
enum { MaxPathLength = 128, FormatBufferLength = 64 };
/**
* This type is used for the path
*/
typedef uavcan::Array<uavcan::IntegerSpec<8, uavcan::SignednessUnsigned, uavcan::CastModeTruncate>,
uavcan::ArrayModeDynamic, MaxPathLength> PathString;
PathString path_;
public:
FileEventTracer() { }
virtual void onEvent(uavcan::dynamic_node_id_server::TraceCode code, uavcan::int64_t argument)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
int fd = open(path_.c_str(), O_WRONLY | O_CREAT | O_APPEND);
if (fd >= 0 )
{
char buffer[FormatBufferLength + 1];
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
*
*/
int init(const PathString & path)
{
using namespace std;
int rv = -uavcan::ErrInvalidParam;
if (path.size() > 0)
{
rv = 0;
path_ = path.c_str();
int fd = open(path_.c_str(), O_RDWR | O_CREAT | O_TRUNC);
if ( fd >= 0)
{
close(fd);
}
}
return rv;
}
};
}
}
@@ -71,6 +71,7 @@ public:
break;
}
}
value = buffer;
}
}
return value;
@@ -94,16 +95,16 @@ 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 -EFBIG then the the path name is too long to
* If it is -ErrInvalidConfiguration then the the path name is too long to
* Accommodate the trailing slash and max key length;
*
*/
int init(const String & path)
int init(const PathString & path)
{
using namespace std;
int rv = -EINVAL;
int rv = -uavcan::ErrInvalidParam;
if (path.size() > 0)
{
@@ -115,16 +116,19 @@ public:
base_path.pop_back();
}
rv = 0;
struct stat sb;
if (stat(base_path.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode))
{
rv = mkdir(base_path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
}
base_path.push_back('/');
if ((base_path.size() + MaxStringLength) > MaxPathLength)
if (rv >= 0 )
{
rv = -EFBIG;
base_path.push_back('/');
if ((base_path.size() + MaxStringLength) > MaxPathLength)
{
rv = -uavcan::ErrInvalidConfiguration;
}
}
}
return rv;