mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-01-20 14:48:54 +01:00
* basic ctb config api for register and bit names * tests for define and definelist pass. yet to implement using them for reg, setbit, clearbit and getbit * improved autocomplete for getbit,setbit, clearbit * validate autocomplete * definelist has no put * updating help * converting char array+int in runtimeerror compiles but throws at runtime.Fixed.Tested for it. Also check if string or int before using getregisterdefinitonbyvalue to see if it threw to call the other function. because both of it can throw and we should differentiate the issues for both * removed std::vector<std::pair<string,int> to std::map<string, int> for defiitions list * Dev/define cmd tie bit to reg (#1328) * strong type * moved everythign to bit_utils class * pybindings * added tests for python * removed duplicates * removed bit names in reg * changed BitPosition to BitAddress * Using define reg/bit from python (#1344) * define_bit, define_addr in python. * setBit/clearBit takes int or addr * added example using bits * split define into 2 commands define_reg and define_bit, definelist into 2: definelist_reg and definelist_bit * allow string for register and bit names in c++ api * refactor from github comments * naming refactoring (getRegisterDefnition to retunr name and address specifically * added marker for 8 cmd tests connected to define, changed macro to static constexpr * changed bitPosition from int to uint32_t * got rid of setbitposition and setaddress, instead overloaded constructor to take in strings so that the conversion from string to bit address members, takes place within the class for easy maintainance in case type changes * Removing implicit conversions: RegisterAddresss and RegisterValue: Removed the implicit conversions. RegisterAddress: Changed member name from address_ to value_ and method as well to value(). RegisterValue: Also added | operator to be able to concatenate with uint32_t. Same in python bindings (but could not find the tests to modify * Allowed concatenation with other RegisterValue, made them all constexpr * fix a ctbConfig test * Maponstack works with integration tests, but need unit tests * tests on mapstack * fixed ctb tests and FixedString being initialized with gibberish * removing parsing from string inside the class RegisterAddress, BitAddress and RegisterValue * updated python bindings * fixed bit utils test * renaming getRegisterDefintiionAddress/Name=>getRegisterAddress/Name and similary for getBitDefinitionAddress/Name * updated python bindings * fix tests (format) * a few python tests added and python bindings corrected * replaceing str with __str__ for bit.cpp * repr reimplemented for bit.cpp * removed make with registerAddress etc * starting server for tests per session and nor module * killprocess throws if no process found-> github runs fails, changed to pkill and not throw * clean shm shouldnt raise, in ci binary not found * ignoring these tests for CI, which fail on CI because simulators are not generated in CI. This is in another PR, where it should work --------- Co-authored-by: Erik Fröjdh <erik.frojdh@gmail.com> Co-authored-by: froejdh_e <erik.frojdh@psi.ch>
119 lines
3.4 KiB
C++
119 lines
3.4 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-or-other
|
|
// Copyright (C) 2021 Contributors to the SLS Detector Package
|
|
#include "catch.hpp"
|
|
#include "sls/UdpRxSocket.h"
|
|
#include "sls/sls_detector_exceptions.h"
|
|
#include <cstdint>
|
|
#include <errno.h>
|
|
#include <future>
|
|
#include <iostream>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
namespace sls {
|
|
|
|
constexpr int default_port = 50001;
|
|
|
|
int open_socket(int port) {
|
|
const char *host = nullptr; // localhost
|
|
|
|
// Create a socket for sending
|
|
struct addrinfo hints;
|
|
memset(&hints, 0, sizeof(hints));
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
hints.ai_protocol = 0;
|
|
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
|
|
struct addrinfo *res = nullptr;
|
|
|
|
const std::string portname = std::to_string(port);
|
|
if (getaddrinfo(host, portname.c_str(), &hints, &res)) {
|
|
throw RuntimeError("Failed at getaddrinfo with " + std::string(host));
|
|
}
|
|
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
if (fd == -1) {
|
|
throw RuntimeError("Failed to create UDP RX socket");
|
|
}
|
|
|
|
if (connect(fd, res->ai_addr, res->ai_addrlen)) {
|
|
throw RuntimeError("Failed to connect socket");
|
|
}
|
|
freeaddrinfo(res);
|
|
return fd;
|
|
}
|
|
|
|
TEST_CASE("Get packet size returns the packet size we set in the constructor") {
|
|
constexpr int port = 50001;
|
|
constexpr ssize_t packet_size = 8000;
|
|
UdpRxSocket s{port, packet_size};
|
|
CHECK(s.getPacketSize() == packet_size);
|
|
}
|
|
|
|
TEST_CASE("Receive data from a vector") {
|
|
constexpr int port = 50001;
|
|
std::vector<int> data_to_send{4, 5, 3, 2, 5, 7, 2, 3};
|
|
std::vector<int> data_received(data_to_send.size());
|
|
ssize_t packet_size =
|
|
sizeof(decltype(data_to_send)::value_type) * data_to_send.size();
|
|
|
|
UdpRxSocket udpsock{port, packet_size};
|
|
|
|
int fd = open_socket(port);
|
|
auto n = write(fd, data_to_send.data(), packet_size);
|
|
CHECK(n == packet_size);
|
|
|
|
CHECK(udpsock.ReceivePacket((char *)data_received.data()));
|
|
close(fd);
|
|
CHECK(data_to_send == data_received);
|
|
}
|
|
|
|
// TODO! Test blocking on apple, investigate when implementing
|
|
// receiver support in macOS
|
|
#ifndef __APPLE__
|
|
|
|
TEST_CASE("Shutdown socket without hanging when waiting for data") {
|
|
constexpr int port = 50001;
|
|
constexpr ssize_t packet_size = 8000;
|
|
UdpRxSocket s{port, packet_size};
|
|
char buff[packet_size];
|
|
|
|
// Start a thread and wait for package
|
|
// if the socket is left open we would block
|
|
std::future<bool> ret = std::async(
|
|
std::launch::async, &UdpRxSocket::ReceivePacket, &s, (char *)&buff);
|
|
|
|
s.Shutdown();
|
|
auto r = ret.get();
|
|
|
|
CHECK(r == false); // since we didn't get the packet
|
|
}
|
|
#endif
|
|
|
|
TEST_CASE("Too small packet") {
|
|
constexpr int port = 50001;
|
|
UdpRxSocket s(port, 2 * sizeof(uint32_t));
|
|
auto fd = open_socket(port);
|
|
uint32_t val = 10;
|
|
write(fd, &val, sizeof(val));
|
|
uint32_t buff[2];
|
|
CHECK(s.ReceivePacket((char *)&buff) == false);
|
|
close(fd);
|
|
}
|
|
|
|
TEST_CASE("Receive an int to an external buffer") {
|
|
int to_send = 5;
|
|
int received = -1;
|
|
auto fd = open_socket(default_port);
|
|
UdpRxSocket s(default_port, sizeof(int));
|
|
write(fd, &to_send, sizeof(to_send));
|
|
CHECK(s.ReceivePacket(reinterpret_cast<char *>(&received)));
|
|
CHECK(received == to_send);
|
|
}
|
|
|
|
} // namespace sls
|