mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-06 18:10:40 +02:00
clean up
This commit is contained in:
parent
45fd35b243
commit
24f28f14f4
@ -1573,7 +1573,7 @@ int slsDetector::configureMAC() {
|
||||
FILE_LOG(logDEBUG1) << "Configuring MAC";
|
||||
if (detector_shm()->receiverUDPIP == 0) {
|
||||
// If hostname is valid ip use that, oterwise lookup hostname
|
||||
detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname);
|
||||
detector_shm()->receiverUDPIP = detector_shm()->receiver_hostname;
|
||||
if (detector_shm()->receiverUDPIP == 0) {
|
||||
detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname);
|
||||
}
|
||||
@ -1597,23 +1597,15 @@ int slsDetector::configureMAC() {
|
||||
|
||||
// copy to args and convert to hex
|
||||
snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort);
|
||||
// snprintf(args[1], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP));
|
||||
sls::strcpy_safe(args[1], getReceiverUDPIP().str());
|
||||
sls::strcpy_safe(args[2], getReceiverUDPMAC().hex().c_str());
|
||||
// sls::removeChar(args[2], ':');
|
||||
sls::strcpy_safe(args[3], getDetectorIP().hex().c_str());
|
||||
sls::strcpy_safe(args[4], getDetectorMAC().hex().c_str());
|
||||
// sls::removeChar(args[4], ':');
|
||||
sls::strcpy_safe(args[1], getReceiverUDPIP().str()); //TODO! Why not hex?
|
||||
sls::strcpy_safe(args[2], getReceiverUDPMAC().hex());
|
||||
sls::strcpy_safe(args[3], getDetectorIP().hex());
|
||||
sls::strcpy_safe(args[4], getDetectorMAC().hex());
|
||||
snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2);
|
||||
// snprintf(args[6], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP2));
|
||||
sls::strcpy_safe(args[6], getReceiverUDPIP2().str());
|
||||
sls::strcpy_safe(args[7], getReceiverUDPMAC2().hex().c_str());
|
||||
// sls::removeChar(args[7], ':');
|
||||
sls::strcpy_safe(args[8], getDetectorIP2().hex().c_str());
|
||||
sls::strcpy_safe(args[9], getDetectorMAC2().hex().c_str());
|
||||
// sls::removeChar(args[9], ':');
|
||||
|
||||
// number of interfaces and which one
|
||||
sls::strcpy_safe(args[7], getReceiverUDPMAC2().hex());
|
||||
sls::strcpy_safe(args[8], getDetectorIP2().hex());
|
||||
sls::strcpy_safe(args[9], getDetectorMAC2().hex());
|
||||
snprintf(args[10], array_size, "%x", detector_shm()->numUDPInterfaces);
|
||||
snprintf(args[11], array_size, "%x", detector_shm()->selectedUDPInterface);
|
||||
|
||||
@ -2237,48 +2229,43 @@ std::string slsDetector::getReceiverHostname() const {
|
||||
}
|
||||
|
||||
std::string slsDetector::setReceiverUDPIP(const std::string &udpip) {
|
||||
|
||||
if (udpip.length() && udpip.length() < 16) {
|
||||
auto ip = IpStringToUint(udpip.c_str());
|
||||
if (ip == 0) {
|
||||
throw ReceiverError("setReceiverUDPIP: UDP IP Address should be "
|
||||
"VALID and in xxx.xxx.xxx.xxx format");
|
||||
} else {
|
||||
detector_shm()->receiverUDPIP = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
auto ip = IpAddr(udpip);
|
||||
if (ip == 0) {
|
||||
throw ReceiverError("setReceiverUDPIP: UDP IP Address should be "
|
||||
"VALID and in xxx.xxx.xxx.xxx format");
|
||||
} else {
|
||||
detector_shm()->receiverUDPIP = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
return getReceiverUDPIP().str();
|
||||
}
|
||||
return getReceiverUDPIP().str();
|
||||
}
|
||||
|
||||
sls::IpAddr slsDetector::getReceiverUDPIP() const { return detector_shm()->receiverUDPIP; }
|
||||
|
||||
std::string slsDetector::setReceiverUDPIP2(const std::string &udpip) {
|
||||
if (udpip.length() && udpip.length() < 16) {
|
||||
auto ip = IpStringToUint(udpip.c_str());
|
||||
if (ip == 0) {
|
||||
throw ReceiverError("setReceiverUDPIP: UDP IP Address 2 should be "
|
||||
"VALID and in xxx.xxx.xxx.xxx format");
|
||||
} else {
|
||||
detector_shm()->receiverUDPIP2 = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
auto ip = IpAddr(udpip);
|
||||
if (ip == 0) {
|
||||
throw ReceiverError("setReceiverUDPIP: UDP IP Address 2 should be "
|
||||
"VALID and in xxx.xxx.xxx.xxx format");
|
||||
} else {
|
||||
detector_shm()->receiverUDPIP2 = ip;
|
||||
if (!strcmp(detector_shm()->receiver_hostname, "none")) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver hostname not set yet";
|
||||
} else if (setUDPConnection() == FAIL) {
|
||||
FILE_LOG(logWARNING) << "UDP connection set up failed";
|
||||
}
|
||||
return getReceiverUDPIP2().str();
|
||||
}
|
||||
return getReceiverUDPIP2().str();
|
||||
}
|
||||
|
||||
sls::IpAddr slsDetector::getReceiverUDPIP2() const { return detector_shm()->receiverUDPIP2; }
|
||||
|
||||
std::string slsDetector::setReceiverUDPMAC(const std::string &udpmac) {
|
||||
auto mac = MacStringToUint(udpmac);
|
||||
auto mac = MacAddr(udpmac);
|
||||
if (mac == 0) {
|
||||
throw ReceiverError("Could not decode UDPMAC from: " + udpmac);
|
||||
}
|
||||
@ -2289,7 +2276,7 @@ std::string slsDetector::setReceiverUDPMAC(const std::string &udpmac) {
|
||||
MacAddr slsDetector::getReceiverUDPMAC() const { return detector_shm()->receiverUDPMAC; }
|
||||
|
||||
std::string slsDetector::setReceiverUDPMAC2(const std::string &udpmac) {
|
||||
auto mac = MacStringToUint(udpmac);
|
||||
auto mac = MacAddr(udpmac);
|
||||
if (mac == 0) {
|
||||
throw ReceiverError("Could not decode UDPMA2C from: " + udpmac);
|
||||
}
|
||||
@ -2647,7 +2634,7 @@ int slsDetector::setUDPConnection() {
|
||||
|
||||
if (detector_shm()->receiverUDPIP == 0) {
|
||||
// Hostname could be ip try to decode otherwise look up the hostname
|
||||
detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname);
|
||||
detector_shm()->receiverUDPIP = detector_shm()->receiver_hostname;
|
||||
if (detector_shm()->receiverUDPIP == 0) {
|
||||
detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname);
|
||||
}
|
||||
@ -2681,11 +2668,11 @@ int slsDetector::setUDPConnection() {
|
||||
ret = receiver.sendCommandThenRead(fnum, args, sizeof(args), retvals, sizeof(retvals));
|
||||
if (strlen(retvals[0])) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0];
|
||||
detector_shm()->receiverUDPMAC = MacStringToUint(retvals[0]);
|
||||
detector_shm()->receiverUDPMAC = retvals[0];
|
||||
}
|
||||
if (strlen(retvals[1])) {
|
||||
FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1];
|
||||
detector_shm()->receiverUDPMAC2 = MacStringToUint(retvals[1]);
|
||||
detector_shm()->receiverUDPMAC2 = retvals[1];
|
||||
}
|
||||
if (ret == FORCE_UPDATE) {
|
||||
receiver.close();
|
||||
|
@ -4,11 +4,6 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
std::string MacAddrToString(uint64_t mac);
|
||||
uint64_t MacStringToUint(std::string mac);
|
||||
uint32_t IpStringToUint(const char *ipstr);
|
||||
std::string IpToString(uint32_t ip);
|
||||
std::string IpToHexString(uint32_t ip);
|
||||
uint32_t HostnameToIp(const char *hostname);
|
||||
|
||||
class IpAddr {
|
||||
@ -16,7 +11,7 @@ class IpAddr {
|
||||
uint32_t addr_{0};
|
||||
|
||||
public:
|
||||
IpAddr(uint32_t address);
|
||||
constexpr IpAddr(uint32_t address) noexcept: addr_{address} {}
|
||||
IpAddr(const std::string &address);
|
||||
IpAddr(const char *address);
|
||||
std::string str() const;
|
||||
@ -33,7 +28,7 @@ class MacAddr {
|
||||
std::string to_hex(const char delimiter = 0) const;
|
||||
|
||||
public:
|
||||
MacAddr(uint64_t mac);
|
||||
constexpr MacAddr(uint64_t mac) noexcept : addr_{mac} {}
|
||||
MacAddr(std::string mac);
|
||||
MacAddr(const char *address);
|
||||
std::string str() const { return to_hex(':'); }
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
@ -15,7 +16,7 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
IpAddr::IpAddr(uint32_t address) : addr_{address} {}
|
||||
|
||||
IpAddr::IpAddr(const std::string &address) { inet_pton(AF_INET, address.c_str(), &addr_); }
|
||||
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
|
||||
std::string IpAddr::str() const {
|
||||
@ -32,18 +33,18 @@ std::string IpAddr::hex() const {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
MacAddr::MacAddr(uint64_t mac) : addr_{mac} {}
|
||||
MacAddr::MacAddr(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
||||
(mac[11] != ':') || (mac[14] != ':')) {
|
||||
addr_ = 0;
|
||||
} else {
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
addr_ = std::strtoul(mac.c_str(), nullptr, 16);
|
||||
}
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
addr_ = std::stol(mac, nullptr, 16);
|
||||
}
|
||||
MacAddr::MacAddr(const char *address) : MacAddr(std::string(address)) {}
|
||||
|
||||
std::string MacAddr::to_hex(const char delimiter) const{
|
||||
std::string MacAddr::to_hex(const char delimiter) const {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
ss << ((addr_ >> 40) & 0xFF);
|
||||
@ -55,53 +56,9 @@ std::string MacAddr::to_hex(const char delimiter) const{
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr) { return out << addr.str(); }
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
|
||||
std::string MacAddrToString(uint64_t mac) {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
ss << ((mac >> 40) & 0xFF);
|
||||
for (int i = 32; i >= 0; i -= 8) {
|
||||
ss << ':' << ((mac >> i) & 0xFF);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
uint64_t MacStringToUint(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
||||
(mac[11] != ':') || (mac[14] != ':')) {
|
||||
return 0;
|
||||
}
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
return std::stol(mac, nullptr, 16);
|
||||
}
|
||||
|
||||
uint32_t IpStringToUint(const char *ipstr) {
|
||||
uint32_t ip{0};
|
||||
inet_pton(AF_INET, ipstr, &ip);
|
||||
return ip;
|
||||
}
|
||||
|
||||
std::string IpToString(uint32_t ip) {
|
||||
char ipstring[INET_ADDRSTRLEN]{};
|
||||
inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN);
|
||||
return ipstring;
|
||||
}
|
||||
|
||||
std::string IpToHexString(uint32_t ip) {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
for (int i = 0; i != 4; ++i) {
|
||||
ss << ((ip >> i * 8) & 0xFF);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr) { return out << addr.str(); }
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname) {
|
||||
struct addrinfo hints, *result;
|
||||
|
@ -7,19 +7,7 @@
|
||||
#include "string_utils.h"
|
||||
|
||||
using namespace sls;
|
||||
TEST_CASE("Convert mac address") {
|
||||
|
||||
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028};
|
||||
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4", "ee:50:22:46:d9:f4"};
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto mac = vec_addr[i];
|
||||
auto answer = vec_ans[i];
|
||||
|
||||
std::string string_addr = MacAddrToString(mac);
|
||||
CHECK(string_addr == answer);
|
||||
CHECK(MacStringToUint(string_addr) == mac);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert mac address using classes") {
|
||||
|
||||
@ -38,19 +26,6 @@ TEST_CASE("Convert mac address using classes") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert IP") {
|
||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
||||
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"};
|
||||
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto ip = vec_addr[i];
|
||||
auto answer = vec_ans[i];
|
||||
|
||||
auto string_addr = IpToString(ip);
|
||||
CHECK(string_addr == answer);
|
||||
CHECK(IpStringToUint(string_addr.c_str()) == ip);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert IP using classes ") {
|
||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
||||
@ -70,19 +45,7 @@ TEST_CASE("Convert IP using classes ") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("IP not valid") {
|
||||
|
||||
CHECK(IpStringToUint("hej") == 0);
|
||||
CHECK(IpStringToUint("mpc2408") == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Convert ip to hex") {
|
||||
std::vector<std::string> ipstrings{"74.125.43.99", "129.129.202.217"};
|
||||
std::vector<std::string> vec_ans{"4a7d2b63", "8181cad9"};
|
||||
for (int i = 0; i != ipstrings.size(); ++i) {
|
||||
uint32_t ip = IpStringToUint(ipstrings[i].c_str());
|
||||
CHECK(IpToHexString(ip) == vec_ans[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// TEST_CASE("Lookup ip")
|
||||
TEST_CASE("Strange input gives 0"){
|
||||
CHECK(IpAddr("hej")== 0);
|
||||
CHECK(MacAddr("hej")== 0);
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
#include "MySocketTCP.h"
|
||||
#include "catch.hpp"
|
||||
// #include "multiSlsDetector.h"
|
||||
#include "logger.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
@ -23,6 +22,9 @@ TEST_CASE("copy a string") {
|
||||
|
||||
}
|
||||
|
||||
|
||||
#ifdef NDEBUG
|
||||
//This test can only run in release since we assert on the length of the string
|
||||
TEST_CASE("copy a long string"){
|
||||
auto src = "some very very long sting that does not fit";
|
||||
char dst[3];
|
||||
@ -32,7 +34,7 @@ TEST_CASE("copy a long string"){
|
||||
REQUIRE(dst[2]=='\0');
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
TEST_CASE("Concat") {
|
||||
std::vector<std::string> v{"one", "one", "one"};
|
||||
std::vector<std::string> v2{"one", "one", "one"};
|
||||
|
Loading…
x
Reference in New Issue
Block a user