functionality of class Dump integrated into BusCommand.
This commit is contained in:
@@ -6,9 +6,7 @@ AM_CXXFLAGS = -fpic \
|
|||||||
|
|
||||||
bin_PROGRAMS = ebusd
|
bin_PROGRAMS = ebusd
|
||||||
|
|
||||||
ebusd_SOURCES = dump.cpp \
|
ebusd_SOURCES = busloop.cpp \
|
||||||
dump.h \
|
|
||||||
busloop.cpp \
|
|
||||||
busloop.h \
|
busloop.h \
|
||||||
network.cpp \
|
network.cpp \
|
||||||
network.h \
|
network.h \
|
||||||
|
|||||||
+29
-5
@@ -20,6 +20,7 @@
|
|||||||
#include "busloop.h"
|
#include "busloop.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "appl.h"
|
#include "appl.h"
|
||||||
|
#include <fstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
extern Logger& L;
|
extern Logger& L;
|
||||||
@@ -78,8 +79,9 @@ BusLoop::BusLoop(Commands* commands)
|
|||||||
if (m_port->isOpen() == false)
|
if (m_port->isOpen() == false)
|
||||||
L.log(bus, error, "can't open %s", A.getOptVal<const char*>("device"));
|
L.log(bus, error, "can't open %s", A.getOptVal<const char*>("device"));
|
||||||
|
|
||||||
m_dump = new Dump(A.getOptVal<const char*>("dumpfile"), A.getOptVal<long>("dumpsize"));
|
m_dumpFile = A.getOptVal<const char*>("dumpfile");
|
||||||
m_dumpState = A.getOptVal<bool>("dump");
|
m_dumpSize = A.getOptVal<long>("dumpsize");
|
||||||
|
m_dumping = A.getOptVal<bool>("dump");
|
||||||
|
|
||||||
m_logRawData = A.getOptVal<bool>("lograwdata");
|
m_logRawData = A.getOptVal<bool>("lograwdata");
|
||||||
|
|
||||||
@@ -100,7 +102,6 @@ BusLoop::~BusLoop()
|
|||||||
m_port->close();
|
m_port->close();
|
||||||
|
|
||||||
delete m_port;
|
delete m_port;
|
||||||
delete m_dump;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* BusLoop::run()
|
void* BusLoop::run()
|
||||||
@@ -234,6 +235,29 @@ void* BusLoop::run()
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int BusLoop::writeDumpFile(const char* byte)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
std::ofstream fs(m_dumpFile.c_str(), std::ios::out | std::ios::binary | std::ios::app);
|
||||||
|
|
||||||
|
if (fs == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
fs.write(byte, 1);
|
||||||
|
|
||||||
|
if (fs.tellp() >= m_dumpSize * 1024) {
|
||||||
|
std::string oldfile;
|
||||||
|
oldfile += m_dumpFile;
|
||||||
|
oldfile += ".old";
|
||||||
|
ret = rename(m_dumpFile.c_str(), oldfile.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.close();
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned char BusLoop::fetchByte()
|
unsigned char BusLoop::fetchByte()
|
||||||
{
|
{
|
||||||
unsigned char byte;
|
unsigned char byte;
|
||||||
@@ -241,8 +265,8 @@ unsigned char BusLoop::fetchByte()
|
|||||||
// fetch byte
|
// fetch byte
|
||||||
byte = m_port->byte();
|
byte = m_port->byte();
|
||||||
|
|
||||||
if (m_dumpState == true)
|
if (m_dumping == true)
|
||||||
m_dump->write((const char*) &byte);
|
writeDumpFile((const char*) &byte);
|
||||||
|
|
||||||
if (m_logRawData == true)
|
if (m_logRawData == true)
|
||||||
L.log(bus, event, "%02x", byte);
|
L.log(bus, event, "%02x", byte);
|
||||||
|
|||||||
+33
-9
@@ -22,7 +22,6 @@
|
|||||||
|
|
||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
#include "dump.h"
|
|
||||||
#include "wqueue.h"
|
#include "wqueue.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "symbol.h"
|
#include "symbol.h"
|
||||||
@@ -31,7 +30,7 @@
|
|||||||
/** the maximum time [us] allowed for retrieving a byte from an addressed slave */
|
/** the maximum time [us] allowed for retrieving a byte from an addressed slave */
|
||||||
#define RECV_TIMEOUT 10000
|
#define RECV_TIMEOUT 10000
|
||||||
|
|
||||||
enum MessageType { invalid, broadcast, masterMaster, masterSlave };
|
enum CommandType { invalid, broadcast, masterMaster, masterSlave };
|
||||||
|
|
||||||
class BusMessage
|
class BusMessage
|
||||||
{
|
{
|
||||||
@@ -40,7 +39,7 @@ public:
|
|||||||
BusMessage(const std::string command, const bool poll, const bool scan);
|
BusMessage(const std::string command, const bool poll, const bool scan);
|
||||||
~BusMessage();
|
~BusMessage();
|
||||||
|
|
||||||
MessageType getType() const { return m_type; }
|
CommandType getType() const { return m_type; }
|
||||||
bool isPoll() const { return m_poll; }
|
bool isPoll() const { return m_poll; }
|
||||||
bool isScan() const { return m_scan; }
|
bool isScan() const { return m_scan; }
|
||||||
|
|
||||||
@@ -58,7 +57,7 @@ public:
|
|||||||
void sendSignal() { pthread_cond_signal(&m_cond); }
|
void sendSignal() { pthread_cond_signal(&m_cond); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MessageType m_type;
|
CommandType m_type;
|
||||||
bool m_poll;
|
bool m_poll;
|
||||||
bool m_scan;
|
bool m_scan;
|
||||||
SymbolString m_command;
|
SymbolString m_command;
|
||||||
@@ -82,19 +81,37 @@ public:
|
|||||||
|
|
||||||
void addMessage(BusMessage* message) { m_busQueue.add(message); }
|
void addMessage(BusMessage* message) { m_busQueue.add(message); }
|
||||||
|
|
||||||
void dump() { m_dumpState == true ? m_dumpState = false : m_dumpState = true ; }
|
|
||||||
void raw() { m_logRawData == true ? m_logRawData = false : m_logRawData = true ; }
|
|
||||||
|
|
||||||
void reload(Commands* commands) { m_commands = commands; }
|
void reload(Commands* commands) { m_commands = commands; }
|
||||||
|
|
||||||
void scan(const bool full=false) { m_scan = true; m_scanFull = full; m_scanIndex = 0; }
|
void scan(const bool full=false) { m_scan = true; m_scanFull = full; m_scanIndex = 0; }
|
||||||
|
|
||||||
|
void raw() { m_logRawData == true ? m_logRawData = false : m_logRawData = true ; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief set the name of dump file.
|
||||||
|
* @param name the file name of dump file.
|
||||||
|
*/
|
||||||
|
void setDumpFile(const std::string& dumpFile) { m_dumpFile = dumpFile; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief set the max size of dump file.
|
||||||
|
* @param size the max. size of the dump file, before switching.
|
||||||
|
*/
|
||||||
|
void setDumpSize(const long dumpSize) { m_dumpSize = dumpSize; }
|
||||||
|
|
||||||
|
void dump() { m_dumping == true ? m_dumping = false : m_dumping = true ; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Commands* m_commands;
|
Commands* m_commands;
|
||||||
Port* m_port;
|
Port* m_port;
|
||||||
|
|
||||||
Dump* m_dump;
|
/** the name of dump file*/
|
||||||
bool m_dumpState;
|
std::string m_dumpFile;
|
||||||
|
|
||||||
|
/** max. size of dump file */
|
||||||
|
long m_dumpSize;
|
||||||
|
|
||||||
|
bool m_dumping;
|
||||||
|
|
||||||
bool m_logRawData;
|
bool m_logRawData;
|
||||||
|
|
||||||
@@ -118,6 +135,13 @@ private:
|
|||||||
bool m_scanFull;
|
bool m_scanFull;
|
||||||
size_t m_scanIndex;
|
size_t m_scanIndex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief write byte to dump file.
|
||||||
|
* @param byte to write
|
||||||
|
* @return -1 if dump file cannot opened or renaming of dump file failed.
|
||||||
|
*/
|
||||||
|
int writeDumpFile(const char* byte);
|
||||||
|
|
||||||
unsigned char fetchByte();
|
unsigned char fetchByte();
|
||||||
void collectCycData(const int numRecv);
|
void collectCycData(const int numRecv);
|
||||||
void analyseCycData();
|
void analyseCycData();
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
|
||||||
*
|
|
||||||
* This file is part of ebusd.
|
|
||||||
*
|
|
||||||
* ebusd is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* ebusd is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with ebusd. If not, see http://www.gnu.org/licenses/.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "dump.h"
|
|
||||||
#include <fstream>
|
|
||||||
#include <cstdio>
|
|
||||||
|
|
||||||
int Dump::write(const char* byte)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
std::ofstream fs(m_name.c_str(), std::ios::out | std::ios::binary | std::ios::app);
|
|
||||||
|
|
||||||
if (fs == 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
fs.write(byte, 1);
|
|
||||||
|
|
||||||
if (fs.tellp() >= m_size * 1024) {
|
|
||||||
std::string oldfile;
|
|
||||||
oldfile += m_name;
|
|
||||||
oldfile += ".old";
|
|
||||||
ret = rename(m_name.c_str(), oldfile.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.close();
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
|
||||||
*
|
|
||||||
* This file is part of ebusd.
|
|
||||||
*
|
|
||||||
* ebusd is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* ebusd is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with ebusd. If not, see http://www.gnu.org/licenses/.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef DUMP_H_
|
|
||||||
#define DUMP_H_
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief class for writing raw bytes to binary file.
|
|
||||||
*/
|
|
||||||
class Dump
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief create a new instance to write dump files.
|
|
||||||
* @param name the file name of dump file.
|
|
||||||
* @param size the max. size of the dump file, before switching.
|
|
||||||
*/
|
|
||||||
Dump(std::string name, long size)
|
|
||||||
: m_name(name), m_size(size) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief write byte to dump file.
|
|
||||||
* @param byte to write
|
|
||||||
* @return -1 if dump file cannot opened or renaming of dump file failed.
|
|
||||||
*/
|
|
||||||
int write(const char* byte);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief set the name of dump file.
|
|
||||||
* @param name the file name of dump file.
|
|
||||||
*/
|
|
||||||
void setName(const std::string& name) { m_name = name; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief set the max size of dump file.
|
|
||||||
* @param size the max. size of the dump file, before switching.
|
|
||||||
*/
|
|
||||||
void setSize(const long size) { m_size = size; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
/** the name of dump file*/
|
|
||||||
std::string m_name;
|
|
||||||
|
|
||||||
/** max. size of dump file */
|
|
||||||
long m_size;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // DUMP_H_
|
|
||||||
Reference in New Issue
Block a user