working implementation

This commit is contained in:
Erik Frojdh
2022-03-24 17:40:44 +01:00
parent 0803f1bc1f
commit 589124845a
9 changed files with 263 additions and 68 deletions

View File

@ -7,6 +7,7 @@ set(SOURCES
src/CmdProxy.cpp
src/CmdParser.cpp
src/Pattern.cpp
src/ctb_named_dacs.cpp
)
add_library(slsDetectorObject OBJECT

View File

@ -7,6 +7,7 @@
#include "CmdProxy.h"
#include "DetectorImpl.h"
#include "Module.h"
#include "ctb_named_dacs.h"
#include "sls/Pattern.h"
#include "sls/container_utils.h"
#include "sls/file_utils.h"
@ -21,6 +22,9 @@
namespace sls {
void freeSharedMemory(int detectorIndex, int moduleIndex) {
//
remove_ctb_dacnames(detectorIndex);
// single module
if (moduleIndex >= 0) {
SharedMemory<sharedModule> moduleShm(detectorIndex, moduleIndex);
@ -54,7 +58,10 @@ Detector::Detector(int shm_id)
Detector::~Detector() = default;
// Configuration
void Detector::freeSharedMemory() { pimpl->freeSharedMemory(); }
void Detector::freeSharedMemory() {
remove_ctb_dacnames(pimpl->getDetectorIndex());
pimpl->freeSharedMemory();
}
void Detector::loadConfig(const std::string &fname) {
int shm_id = getShmId();
@ -2070,47 +2077,19 @@ void Detector::setLEDEnable(bool enable, Positions pos) {
void Detector::setDacNames(const std::vector<std::string> names) {
if (getDetectorType().squash() != defs::CHIPTESTBOARD)
throw RuntimeError("Named dacs only for CTB");
if (names.size() != 18)
throw RuntimeError("Need to set all 18 dacs when naming dacs");
std::ofstream ofs("/dev/shm/slsDetectorPackage_ctbdacnames");
if (!ofs)
throw RuntimeError("Failed to open dacnames file in shared memory");
std::string s = sls::ToString(names);
ofs.write(&s[0], s.size());
set_ctb_dac_names(names, pimpl->getDetectorIndex());
}
std::vector<std::string> Detector::getDacNames() const {
std::vector<std::string> names;
auto type = getDetectorType().squash();
if (type == defs::CHIPTESTBOARD) {
try {
std::ifstream ifs("/dev/shm/slsDetectorPackage_ctbdacnames");
if (!ifs)
throw RuntimeError("Could not read dacnames form shm");
std::string dacnames;
ifs.seekg(0, std::ios::end);
dacnames.resize(ifs.tellg());
ifs.seekg(0, std::ios::beg);
ifs.read(&dacnames[0], dacnames.size());
std::string chars = "[] ";
dacnames.erase(std::remove_if(dacnames.begin(), dacnames.end(),
[&chars](const char &c) {
return chars.find(c) !=
std::string::npos;
}),
dacnames.end());
auto names = sls::split(dacnames, ',');
return names;
} catch (...) {
}
names = get_ctb_dac_names(pimpl->getDetectorIndex());
}
if (names.empty()) {
for (const auto &index : getDacList())
names.push_back(ToString(index));
}
std::vector<std::string> names;
for (const auto& index : getDacList())
names.push_back(ToString(index));
return names;
}

View File

@ -0,0 +1,65 @@
#include "SharedMemory.h"
#include "ctb_named_dacs.h"
#include "sls/string_utils.h"
#include "sls/ToString.h"
#include <fstream>
#include <sstream>
#include <algorithm>
namespace sls {
std::vector<std::string> get_ctb_dac_names(int det_id) {
std::ifstream ifs(ctb_dac_fname(det_id));
if (!ifs)
return {};
std::string dacnames;
ifs.seekg(0, std::ios::end);
dacnames.resize(ifs.tellg());
ifs.seekg(0, std::ios::beg);
ifs.read(&dacnames[0], dacnames.size());
std::string chars = "[] ";
dacnames.erase(std::remove_if(dacnames.begin(), dacnames.end(),
[&chars](const char &c) {
return chars.find(c) != std::string::npos;
}),
dacnames.end());
auto names = sls::split(dacnames, ',');
return names;
}
std::string ctb_dac_fname(int det_id) {
std::string sEnvPath;
char *envpath = getenv(SHM_ENV_NAME);
if (envpath != nullptr) {
sEnvPath.assign(envpath);
sEnvPath.append("_");
}
sEnvPath.insert(0, "_");
std::stringstream oss;
oss << "/dev/shm" << SHM_DETECTOR_PREFIX << det_id << sEnvPath << "ctbdacs";
return oss.str();
}
void set_ctb_dac_names(const std::vector<std::string>& names, int det_id){
if (names.size() != 18)
throw RuntimeError("Need to set all 18 dacs when naming dacs");
std::ofstream ofs(ctb_dac_fname(det_id));
if(!ofs)
throw RuntimeError("Could not open dacnames file for writing");
std::string s = sls::ToString(names);
ofs.write(&s[0], s.size());
}
void remove_ctb_dacnames(int det_id){
unlink(ctb_dac_fname(det_id).c_str());
}
} // namespace sls

View File

@ -0,0 +1,13 @@
#pragma once
#include <string>
#include <vector>
namespace sls{
std::vector<std::string> get_ctb_dac_names(int det_id);
void set_ctb_dac_names(const std::vector<std::string>& names, int det_id);
std::string ctb_dac_fname(int det_id);
void remove_ctb_dacnames(int det_id);
}

View File

@ -18,6 +18,7 @@ target_sources(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdParser.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-Module.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-Pattern.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_ctb_named_dacs.cpp
)
target_include_directories(tests PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src>")

View File

@ -590,13 +590,13 @@ TEST_CASE("master", "[.cmd]") {
}
{
std::ostringstream oss1;
proxy.Call("master", {"0"}, 0, PUT, oss3);
REQUIRE(oss3.str() == "master 0\n");
proxy.Call("master", {"0"}, 0, PUT, oss1);
REQUIRE(oss1.str() == "master 0\n");
}
{
std::ostringstream oss1;
proxy.Call("master", {"1"}, 0, PUT, oss3);
REQUIRE(oss3.str() == "master 1\n");
proxy.Call("master", {"1"}, 0, PUT, oss1);
REQUIRE(oss1.str() == "master 1\n");
}
REQUIRE_THROWS(proxy.Call("master", {"1"}, -1, PUT));
// set all to slaves, and then master

View File

@ -0,0 +1,61 @@
#include "catch.hpp"
#include <string>
#include <stdlib.h>
#include "SharedMemory.h"
#include "ctb_named_dacs.h"
using namespace sls;
#include <fstream>
TEST_CASE("Create name for shm dac file") {
// if SLSDETNAME is already set we unset it but
// save the value
std::string old_slsdetname;
if (getenv(SHM_ENV_NAME))
old_slsdetname = getenv(SHM_ENV_NAME);
unsetenv(SHM_ENV_NAME);
int det_id = 0;
REQUIRE(ctb_dac_fname(det_id) == "/dev/shm/slsDetectorPackage_detector_0_ctbdacs");
setenv(SHM_ENV_NAME, "myprefix", 1);
REQUIRE(ctb_dac_fname(det_id) ==
"/dev/shm/slsDetectorPackage_detector_0_myprefix_ctbdacs");
// Clean up after us
if (old_slsdetname.empty())
unsetenv(SHM_ENV_NAME);
else
setenv(SHM_ENV_NAME, old_slsdetname.c_str(), 1);
}
TEST_CASE("Get ctb dac names returns an empty vector when not set"){
int large_unlikely_number = 123203;
auto vec = get_ctb_dac_names(large_unlikely_number);
REQUIRE(vec.empty());
}
TEST_CASE("Remove file fails silently when file is not present"){
int large_unlikely_number = 123203;
REQUIRE_NOTHROW(remove_ctb_dacnames(large_unlikely_number));
}
TEST_CASE("Read dacs from file then remove file"){
int large_unlikely_number = 998765;
std::ofstream out(ctb_dac_fname(large_unlikely_number));
std::string names = "[first, second, third]";
out.write(&names[0], names.size());
out.close();
auto dacnames = get_ctb_dac_names(large_unlikely_number);
REQUIRE(dacnames.size() == 3);
REQUIRE(dacnames[0] == "first");
REQUIRE(dacnames[1] == "second");
REQUIRE(dacnames[2] == "third");
remove_ctb_dacnames(large_unlikely_number);
std::ifstream in(ctb_dac_fname(large_unlikely_number));
REQUIRE_FALSE(in);
}