Commit 449da513 authored by nshehzad's avatar nshehzad
Browse files

changed: adapted to deviceaccess.

git-svn-id: https://svnsrv.desy.de/desy/mtca4u_applications/tmcb/trunk@17 a36f209a-b8d0-4e10-b481-4fe451885e5b
parent 02d10539
include /usr/share/mtca4u/MTCA4U.CONFIG
#include /usr/share/mtca4u/MTCA4U.CONFIG
CXXFLAGS = -std=c++0x -O0 -g -Wall -fmessage-length=0 $(MtcaMappedDevice_INCLUDE_FLAGS)
CXXFLAGS = -std=c++0x -O0 -g -Wall -fmessage-length=0 $(mtca4u-deviceaccess_INCLUDE_FLAGS)
#CXXFLAGS = -std=c++0x -fPIC -O0 -g -Wall -fmessage-length=0
LDFLAGS = -Wl,-rpath,$(CURDIR)/TMCBeth
OBJS = TMCBethTest.o
LIBS = -lpthread -lboost_system $(MtcaMappedDevice_LIB_FLAGS) $(MtcaMappedDevice_RUNPATH_FLAGS) -lTMCBeth -L$(CURDIR)/TMCBeth
LIBS = -lpthread -lboost_system $(mtca4u-deviceaccess_LIB_FLAGS) $(mtca4u-deviceaccess_RUNPATH_FLAGS) -lTMCBeth -L$(CURDIR)/TMCBeth
#LIBS = -lTMCBeth -L./TMCBeth
TARGET = TMCBethTest
......
......@@ -14,7 +14,8 @@ include_directories(inc)
set(CMAKE_CXX_FLAGS "-std=c++0x -fPIC -pthread")
#verbose debugging
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -pedantic -Wuninitialized -Weffc++")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -pedantic -Wuninitialized -Weffc++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -pedantic -Wuninitialized")
#use -DCMAKE_BUILD_TYPE=Debug in your cmake command to turn on the coverage option
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 --coverage")
......@@ -25,15 +26,18 @@ set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
find_package(MtcaMappedDevice REQUIRED)
include_directories(SYSTEM ${MtcaMappedDevice_INCLUDE_DIRS})
#find_package(MtcaMappedDevice REQUIRED)
#include_directories(SYSTEM ${MtcaMappedDevice_INCLUDE_DIRS})
find_package(mtca4u-deviceaccess REQUIRED)
include_directories(${mtca4u-deviceaccess_INCLUDE_DIRS})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "./lib")
add_library(${PROJECT_NAME} SHARED ${SOURCEDIR}/RebotDevice.cpp ${SOURCEDIR}/TcpCtrl.cpp ${SOURCEDIR}/exTcpCtrl.cpp)
#add_executable(${PROJECT_NAME} TMCBeth.cpp TcpCtrl.cpp exTcpCtrl.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${${PROJECT_NAME}_VERSION} SOVERSION ${${PROJECT_NAME}_SOVERSION})
target_link_libraries(${PROJECT_NAME} ${MtcaMappedDevice_LIBRARIES} ${Boost_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${deviceaccess_LIBRARIES} ${Boost_LIBRARIES})
find_package(Doxygen)
if(DOXYGEN_FOUND)
......
#include <MtcaMappedDevice/devBase.h>
#include <MtcaMappedDevice/devConfigBase.h>
#include <MtcaMappedDevice/devBaseImpl.h>
//#include <mtca4u/DeviceBackend.h>
//#include <mtca4u/devConfigBase.h>
#include <mtca4u/DeviceBackendImpl.h>
#include <iostream>
#include <iomanip>
......@@ -20,7 +20,7 @@
namespace mtca4u {
class RebotDevice : public devBaseImpl {
class RebotDevice : public DeviceBackendImpl {
private:
std::string _boardAddr;
......@@ -28,26 +28,19 @@ private:
boost::shared_ptr<TcpCtrl> _tcpObject;
public:
RebotDevice(void);
~RebotDevice(void);
RebotDevice(std::string boardAddr, int port);
~RebotDevice();
///The function opens the connection to the device
/// @param devName device name string passed by reference in format <ip_address>:<port>
/// @param perm NOT USED
/// @param pConfig NOT USED
void openDev(const std::string &devName, int perm=O_RDWR, devConfigBase *pConfig=NULL);
void closeDev();
void readReg(uint32_t regOffset, int32_t *data, uint8_t bar);
void writeReg(uint32_t regOffset, int32_t data, uint8_t bar);
void readArea(uint32_t regOffset, int32_t *data, size_t size, uint8_t bar);
void writeArea(uint32_t regOffset, int32_t const *data, size_t size, uint8_t bar);
virtual void open();
virtual void close();
virtual void read(uint8_t bar, uint32_t address, int32_t* data, size_t sizeInBytes);
virtual void write(uint8_t bar, uint32_t address, int32_t const* data, size_t sizeInBytes);
///Not implemented
void readDeviceInfo (std::string *devInfo) {}
virtual std::string readDeviceInfo () {return std::string("RebotDevice");}
///Not implemented
void readDMA(uint32_t regOffset, int32_t *data, size_t size, uint8_t bar) {}
virtual void readDMA(uint8_t /*bar*/, uint32_t /*address*/, int32_t* /*data*/, size_t /*sizeInBytes*/) {};
///Not implemented
void writeDMA(uint32_t regOffset, int32_t const *data, size_t size, uint8_t bar) {}
virtual void writeDMA(uint8_t /*bar*/, uint32_t /*address*/, int32_t const* /*data*/, size_t /*sizeInBytes*/) {};
};
} //namespace mtca4u
......@@ -8,13 +8,13 @@
#ifndef EXTCPCTRL_H_
#define EXTCPCTRL_H_
#include <MtcaMappedDevice/exBase.h>
#include <mtca4u/Exception.h>
#include <string>
namespace mtca4u {
///Provides class for exceptions related to RebotDevice
class exTcpCtrl : public exBase {
class exTcpCtrl : public Exception {
public:
enum { EX_OPENSOCK, EX_CONNECT, EX_CLOSESOCK, EX_WRSOCK, EX_RDSOCK, EX_DEVICE_CLOSED, EX_SETIP, EX_SETPORT, EX_SIZEMULT};
exTcpCtrl(const std::string &_exMessage, unsigned int _exID);
......
......@@ -5,11 +5,9 @@ using boost::asio::ip::tcp;
namespace mtca4u {
RebotDevice::RebotDevice() {
_boardAddr = "";
_port = 0;
opened = false;
_tcpObject = boost::make_shared<TcpCtrl>(_boardAddr, _port);
RebotDevice::RebotDevice(std::string boardAddr, int port)
:_boardAddr(boardAddr) , _port(port) , _tcpObject(boost::make_shared<TcpCtrl>(_boardAddr, _port))
{
}
RebotDevice::~RebotDevice() {
......@@ -18,63 +16,16 @@ RebotDevice::~RebotDevice() {
}
}
void RebotDevice::openDev(const std::string &devName, int perm, devConfigBase *pConfig) {
std::vector<std::string> deviceName;
boost::split(deviceName, devName, boost::is_any_of(":"));
_boardAddr = deviceName[0];
_port = std::stoi(deviceName[1]);
_tcpObject->setAddress(_boardAddr);
_tcpObject->setPort(_port);
void RebotDevice::open() {
_tcpObject->openConnection();
opened = true;
_opened = true;
}
void RebotDevice::readReg(uint32_t regOffset, int32_t *data, uint8_t bar) {
this->readArea(regOffset, data, 4, bar);
//temporary solution, first approach, works against dummy, doesn't work on TMCB,
}
void RebotDevice::writeReg(uint32_t regOffset, int32_t data, uint8_t bar) {
this->writeArea(regOffset, &data, 4, bar);
//temporary solution
}
void RebotDevice::writeArea(uint32_t regOffset, int32_t const *data, size_t size, uint8_t bar) {
if (!isOpen()) {
throw exTcpCtrl("Device is closed", exTcpCtrl::EX_DEVICE_CLOSED);
}
if (size % 4 != 0) {
throw exTcpCtrl("\"size\" argument must be a multiplicity of 4", exTcpCtrl::EX_SIZEMULT);
}
int mode = 1;
int packetsize = size/4;
const unsigned int datasendSize = 3*sizeof(int);
boost::array<char, 4> receivedData;
for (unsigned int i=0; i<packetsize; ++i) {
std::vector<char> datasend(datasendSize);
char result[datasendSize];
datasend[0] = mode;
for (int j=1; j<4; ++j) {
datasend[j] = 0;
}
for (int j=4; j<8; ++j) {
datasend[j] = ((regOffset+i) >> (8*(j-4))) & 0xFF;
}
for (int j=8; j<12; ++j) {
datasend[j] = (data[i] >> (8*(j-8))) & 0xFF;
}
_tcpObject->sendData(datasend);
_tcpObject->receiveData(receivedData);
}
}
void RebotDevice::readArea(uint32_t regOffset, int32_t *data, size_t size, uint8_t bar) {
void RebotDevice::read(uint8_t /*bar*/, uint32_t address, int32_t* data, size_t sizeInBytes){
if(!isOpen()) {
throw exTcpCtrl("Device is closed", exTcpCtrl::EX_DEVICE_CLOSED);
}
if (size % 4 != 0) {
if (sizeInBytes % 4 != 0) {
throw exTcpCtrl("\"size\" argument must be a multiplicity of 4", exTcpCtrl::EX_SIZEMULT);
}
......@@ -84,18 +35,18 @@ void RebotDevice::readArea(uint32_t regOffset, int32_t *data, size_t size, uint8
unsigned int datasendSize = 3*sizeof(int);
std::vector<char> datasend(datasendSize);
datasend[0] = mode;
int packetsize = size/4;
unsigned int packetsize = sizeInBytes/4;
for (int j=1; j<4; ++j) {
datasend[j] = 0;
}
for (int j=4; j<8; ++j) {
datasend[j] = (regOffset >> (8*(j-4))) & 0xFF;
datasend[j] = (address >> (8*(j-4))) & 0xFF;
}
for (int j=8; j<12; ++j) {
datasend[j] = ((packetsize) >> (8*(j-8))) & 0xFF;
}
_tcpObject->sendData(datasend);
_tcpObject->receiveData(receivedData);
......@@ -109,8 +60,39 @@ void RebotDevice::readArea(uint32_t regOffset, int32_t *data, size_t size, uint8
}
}
void RebotDevice::closeDev() {
opened = false;
void RebotDevice::write(uint8_t /*bar*/, uint32_t address, int32_t const* data, size_t sizeInBytes){
if (!isOpen()) {
throw exTcpCtrl("Device is closed", exTcpCtrl::EX_DEVICE_CLOSED);
}
if (sizeInBytes % 4 != 0) {
throw exTcpCtrl("\"size\" argument must be a multiplicity of 4", exTcpCtrl::EX_SIZEMULT);
}
int mode = 1;
unsigned int packetsize = sizeInBytes/4;
const unsigned int datasendSize = 3*sizeof(int);
boost::array<char, 4> receivedData;
for (unsigned int i=0; i<packetsize; ++i) {
std::vector<char> datasend(datasendSize);
datasend[0] = mode;
for (int j=1; j<4; ++j) {
datasend[j] = 0;
}
for (int j=4; j<8; ++j) {
datasend[j] = ((address+i) >> (8*(j-4))) & 0xFF;
}
for (int j=8; j<12; ++j) {
datasend[j] = (data[i] >> (8*(j-8))) & 0xFF;
}
_tcpObject->sendData(datasend);
_tcpObject->receiveData(receivedData);
}
}
void RebotDevice::close() {
_opened = false;
_tcpObject->closeConnection();
}
......
......@@ -9,6 +9,6 @@
namespace mtca4u {
exTcpCtrl::exTcpCtrl(const std::string &_exMessage, unsigned int _exID) : exBase(_exMessage, _exID) { }
exTcpCtrl::exTcpCtrl(const std::string &_exMessage, unsigned int _exID) : Exception(_exMessage, _exID) { }
} //namespace mtca4u
include /usr/share/mtca4u/MTCA4U.CONFIG
#include /usr/share/mtca4u/MTCA4U.CONFIG
CXXFLAGS = -std=c++0x -O0 -g -Wall -fmessage-length=0 $(MtcaMappedDevice_INCLUDE_FLAGS) -I../RebotDevice/inc
CXXFLAGS = -std=c++0x -O0 -g -Wall -fmessage-length=0 $(mtca4u-deviceaccess_INCLUDE_FLAGS) -I/space/nshehzad/work/deviceaccess/build/mtca_local_install/include -I../RebotDevice/inc
#CXXFLAGS = -std=c++0x -fPIC -O0 -g -Wall -fmessage-length=0
LDFLAGS = -Wl,-rpath,$(CURDIR)/../RebotDevice/lib
LDFLAGS = -Wl,-rpath,$(CURDIR)/../rebot_build/lib
OBJS = RebotDeviceTests.o
LIBS = -lpthread -lboost_system $(MtcaMappedDevice_LIB_FLAGS) $(MtcaMappedDevice_RUNPATH_FLAGS) -lRebotDevice -L$(CURDIR)/../RebotDevice/lib -lboost_unit_test_framework
#LIBS = -lpthread -lboost_system $(MtcaMappedDevice_LIB_FLAGS) $(MtcaMappedDevice_RUNPATH_FLAGS) -lRebotDevice -L$(CURDIR)/../RebotDevice/lib -lboost_unit_test_framework
LIBS = -lpthread -lboost_system -lRebotDevice -lmtca4u-deviceaccess -L$(CURDIR)/../rebot_build/lib $(mtca4u-deviceaccess_LIB_FLAGS) -L/space/nshehzad/work/deviceaccess/build/mtca_local_install/lib -lboost_unit_test_framework
TARGET = RebotDeviceTests
......
......@@ -13,53 +13,64 @@ using namespace boost::unit_test;
void rw_multiple_test_function(std::vector<int32_t> testArray) {
const static std::string devname = boost::unit_test::framework::master_test_suite().argv[1];
const static std::string devName = boost::unit_test::framework::master_test_suite().argv[1];
std::vector<std::string> deviceName;
boost::split(deviceName, devName, boost::is_any_of(":"));
std::string boardAddr = deviceName[0];
int port = std::stoi(deviceName[1]);
int32_t *writeData = &testArray[0];
int32_t readData[testArray.size()];
int regAddr = atoi(boost::unit_test::framework::master_test_suite().argv[2]);
mtca4u::devBaseImpl *device = new mtca4u::RebotDevice();
device->openDev(devname);
device->writeArea(regAddr, writeData, 4*testArray.size(), 0);
device->readArea(regAddr, readData, 4*testArray.size(), 0);
mtca4u::DeviceBackendImpl *device = new mtca4u::RebotDevice(boardAddr,port);
device->open();
device->write(0, regAddr, writeData, 4*testArray.size());
device->read(0, regAddr, readData, 4*testArray.size());
for(unsigned int i=0; i<testArray.size(); ++i) {
BOOST_TEST_MESSAGE("Testing value " << i << ": " << testArray[i] );
BOOST_TEST_MESSAGE("\tWRITE:" << std::hex << writeData[i] << ":::READ:" << readData[i]);
BOOST_CHECK(writeData[i]==readData[i]);
}
device->closeDev();
device->close();
delete device;
}
void rw_single_test_function(std::vector<int32_t> testArray) {
BOOST_TEST_MESSAGE("TESTS OF writeReg/readReg");
const static std::string devname = boost::unit_test::framework::master_test_suite().argv[1];
const static std::string devName = boost::unit_test::framework::master_test_suite().argv[1];
std::vector<std::string> deviceName;
boost::split(deviceName, devName, boost::is_any_of(":"));
std::string boardAddr = deviceName[0];
int port = std::stoi(deviceName[1]);
int32_t readData;
int regAddr = atoi(boost::unit_test::framework::master_test_suite().argv[2]);
mtca4u::devBaseImpl *device = new mtca4u::RebotDevice();
device->openDev(devname);
mtca4u::DeviceBackendImpl *device = new mtca4u::RebotDevice(boardAddr,port);
device->open();
for (unsigned int i=0; i<testArray.size(); ++i) {
int32_t writeData = testArray.at(i);
device->writeReg(regAddr, writeData, 0);
device->readReg(regAddr, &readData, 0);
device->write(0, regAddr, &writeData, 4);
device->read(0, regAddr, &readData, 4 );
BOOST_TEST_MESSAGE("Testing value " << i << ": " << writeData );
BOOST_TEST_MESSAGE("\tWRITE:" << std::hex << writeData << ":::READ:" << readData);
BOOST_CHECK(writeData==readData);
}
device->closeDev();
device->close();
delete device;
}
void ex_connect_test() {
BOOST_TEST_MESSAGE("EX_CONNECT exception test:");
const static std::string devname = "127.0.0.1:5432";
const static std::string devName = "127.0.0.1:5432";
std::vector<std::string> deviceName;
boost::split(deviceName, devName, boost::is_any_of(":"));
std::string boardAddr = deviceName[0];
int port = std::stoi(deviceName[1]);
bool exceptionIndicator = false;
mtca4u::devBaseImpl *device = new mtca4u::RebotDevice();
mtca4u::DeviceBackendImpl *device = new mtca4u::RebotDevice(boardAddr,port);
try {
device->openDev(devname);
device->open();
}
catch (mtca4u::exTcpCtrl errorException) {
if (errorException.getID() == mtca4u::exTcpCtrl::EX_CONNECT) {
......@@ -75,17 +86,21 @@ void ex_connect_test() {
void ex_sizemult_test() {
BOOST_TEST_MESSAGE("EX_SIZEMULT exception test:");
const static std::string devname = boost::unit_test::framework::master_test_suite().argv[1];
const static std::string devName = boost::unit_test::framework::master_test_suite().argv[1];
std::vector<std::string> deviceName;
boost::split(deviceName, devName, boost::is_any_of(":"));
std::string boardAddr = deviceName[0];
int port = std::stoi(deviceName[1]);
int regAddr = 0;
bool exceptionIndicator = false;
mtca4u::devBaseImpl *device = new mtca4u::RebotDevice();
mtca4u::DeviceBackendImpl *device = new mtca4u::RebotDevice(boardAddr,port);
int testSize = 3;
int32_t writeData[] = {0,0,0};
int32_t readData[testSize];
try {
device->openDev(devname);
device->writeArea(regAddr, writeData, 3*testSize, 0);
device->open();
device->write(0, regAddr, writeData, 3*testSize);
}
catch (mtca4u::exTcpCtrl errorException) {
if (errorException.getID() == mtca4u::exTcpCtrl::EX_SIZEMULT) {
......@@ -98,7 +113,7 @@ void ex_sizemult_test() {
exceptionIndicator = false;
try {
device->readArea(regAddr, readData, 3*testSize, 0);
device->read(0, regAddr, readData, 3*testSize);
}
catch (mtca4u::exTcpCtrl errorException) {
if (errorException.getID() == mtca4u::exTcpCtrl::EX_SIZEMULT) {
......@@ -108,7 +123,7 @@ void ex_sizemult_test() {
BOOST_MESSAGE(" EX_SIZEMULT by readArea thrown: " << exceptionIndicator);
BOOST_CHECK(exceptionIndicator == true);
device->closeDev();
device->close();
delete device;
}
......@@ -116,16 +131,20 @@ void ex_sizemult_test() {
void ex_deviceclosed_test() {
BOOST_TEST_MESSAGE("EX_DEVICE_CLOSED exception test:");
const static std::string devname = boost::unit_test::framework::master_test_suite().argv[1];
const static std::string devName = boost::unit_test::framework::master_test_suite().argv[1];
std::vector<std::string> deviceName;
boost::split(deviceName, devName, boost::is_any_of(":"));
std::string boardAddr = deviceName[0];
int port = std::stoi(deviceName[1]);
int regAddr = 0;
bool exceptionIndicator = false;
mtca4u::devBaseImpl *device = new mtca4u::RebotDevice();
mtca4u::DeviceBackendImpl *device = new mtca4u::RebotDevice(boardAddr,port);
int testSize = 3;
int32_t writeData[] = {0,0,0};
int32_t readData[testSize];
try {
device->writeArea(regAddr, writeData, 4*testSize, 0);
device->write(0, regAddr, writeData, 4*testSize);
}
catch (mtca4u::exTcpCtrl errorException) {
if (errorException.getID() == mtca4u::exTcpCtrl::EX_DEVICE_CLOSED) {
......@@ -138,7 +157,7 @@ void ex_deviceclosed_test() {
exceptionIndicator = false;
try {
device->readArea(regAddr, readData, 4*testSize, 0);
device->read(0, regAddr, readData, 4*testSize);
}
catch (mtca4u::exTcpCtrl errorException) {
if (errorException.getID() == mtca4u::exTcpCtrl::EX_DEVICE_CLOSED) {
......@@ -155,7 +174,7 @@ void ex_deviceclosed_test() {
void ex_getset_ipport_test() {
BOOST_TEST_MESSAGE("EX_SETIP and EX_SETPORT exception test:");
const static std::string devname = boost::unit_test::framework::master_test_suite().argv[1];
const static std::string devName = boost::unit_test::framework::master_test_suite().argv[1];
bool exceptionIndicator = false;
std::string devIP = "1.2.3.4";
......@@ -167,7 +186,7 @@ void ex_getset_ipport_test() {
BOOST_CHECK(tcpCtrlTest.getPort() == devPort);
std::vector<std::string> devNameParts;
boost::split(devNameParts, devname, boost::is_any_of(":"));
boost::split(devNameParts, devName, boost::is_any_of(":"));
devIP = devNameParts[0];
devPort = std::stoi(devNameParts[1]);
tcpCtrlTest.setAddress(devIP);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment