mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-13 05:17:13 +02:00
cleaning and new functions
This commit is contained in:
@ -8,6 +8,7 @@
|
|||||||
#include "sls_detector_defs.h"
|
#include "sls_detector_defs.h"
|
||||||
#include "sls_detector_exceptions.h"
|
#include "sls_detector_exceptions.h"
|
||||||
#include "sls_detector_funcs.h"
|
#include "sls_detector_funcs.h"
|
||||||
|
#include "network_utils.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -26,7 +27,10 @@ using sls::SocketError;
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
std::cout << "size: " <<sizeof(struct in_addr) << "\n";
|
auto ip = sls::IpStringToUint("74.125.43.99");
|
||||||
|
char buffer[50];
|
||||||
|
snprintf(buffer, 50, "%x", __builtin_bswap32(ip));
|
||||||
|
std::cout << "buffer: " << buffer << "\n";
|
||||||
// std::string hostname;
|
// std::string hostname;
|
||||||
// std::cout << "Enter hostname: ";
|
// std::cout << "Enter hostname: ";
|
||||||
// std::cin >> hostname;
|
// std::cin >> hostname;
|
||||||
|
@ -318,9 +318,8 @@ void slsDetector::initializeDetectorStructure(detectorType type) {
|
|||||||
|
|
||||||
detector_shm()->detectorMAC = MacStringToUint(DEFAULT_DET_MAC);
|
detector_shm()->detectorMAC = MacStringToUint(DEFAULT_DET_MAC);
|
||||||
detector_shm()->detectorMAC2 = MacStringToUint(DEFAULT_DET_MAC2);
|
detector_shm()->detectorMAC2 = MacStringToUint(DEFAULT_DET_MAC2);
|
||||||
|
detector_shm()->detectorIP = IpStringToUint(DEFAULT_DET_MAC);
|
||||||
inet_pton(AF_INET, DEFAULT_DET_IP, &(detector_shm()->detectorIP));
|
detector_shm()->detectorIP2 = IpStringToUint(DEFAULT_DET_MAC2);
|
||||||
inet_pton(AF_INET, DEFAULT_DET_IP2, &(detector_shm()->detectorIP2));
|
|
||||||
|
|
||||||
detector_shm()->numUDPInterfaces = 1;
|
detector_shm()->numUDPInterfaces = 1;
|
||||||
detector_shm()->selectedUDPInterface = 1;
|
detector_shm()->selectedUDPInterface = 1;
|
||||||
@ -1569,42 +1568,48 @@ int slsDetector::configureMAC() {
|
|||||||
const size_t array_size = 50;
|
const size_t array_size = 50;
|
||||||
const size_t n_args = 14;
|
const size_t n_args = 14;
|
||||||
const size_t n_retvals = 2;
|
const size_t n_retvals = 2;
|
||||||
char args[n_args][array_size] = {};
|
char args[n_args][array_size]{};
|
||||||
char retvals[n_retvals][array_size] = {};
|
char retvals[n_retvals][array_size]{};
|
||||||
FILE_LOG(logDEBUG1) << "Configuring MAC";
|
FILE_LOG(logDEBUG1) << "Configuring MAC";
|
||||||
if (detector_shm()->receiverUDPIP == 0) {
|
if (detector_shm()->receiverUDPIP == 0) {
|
||||||
// Hostname could be ip try to decode otherwise look up the hostname
|
//If hostname is valid ip use that, oterwise lookup hostname
|
||||||
detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname);
|
detector_shm()->receiverUDPIP = IpStringToUint(detector_shm()->receiver_hostname);
|
||||||
if (detector_shm()->receiverUDPIP == 0) {
|
if (detector_shm()->receiverUDPIP == 0) {
|
||||||
detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname);
|
detector_shm()->receiverUDPIP = HostnameToIp(detector_shm()->receiver_hostname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// rx_udpmac is none
|
|
||||||
if (detector_shm()->receiverUDPMAC == 0) {
|
if (detector_shm()->receiverUDPMAC == 0) {
|
||||||
throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses not set");
|
throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses not set");
|
||||||
}
|
}
|
||||||
FILE_LOG(logDEBUG1) << "rx_hostname and rx_udpmac are valid ";
|
FILE_LOG(logDEBUG1) << "rx_hostname and rx_udpmac are valid ";
|
||||||
|
|
||||||
// jungfrau 2 interfaces
|
// Jungfrau second interface
|
||||||
// validate for the second interface
|
|
||||||
if (detector_shm()->numUDPInterfaces == 2) {
|
if (detector_shm()->numUDPInterfaces == 2) {
|
||||||
// copy from udpip (done here as well if udpconnection avoided (no
|
|
||||||
// slsrxr)
|
|
||||||
if (detector_shm()->receiverUDPIP2 == 0) {
|
if (detector_shm()->receiverUDPIP2 == 0) {
|
||||||
detector_shm()->receiverUDPIP2 = detector_shm()->receiverUDPIP;
|
detector_shm()->receiverUDPIP2 = detector_shm()->receiverUDPIP;
|
||||||
}
|
}
|
||||||
|
if (detector_shm()->receiverUDPMAC2 == 0) {
|
||||||
|
throw RuntimeError("configureMAC: Error. Receiver UDP MAC Addresses 2 not set");
|
||||||
|
}
|
||||||
FILE_LOG(logDEBUG1) << "rx_udpmac2 is valid ";
|
FILE_LOG(logDEBUG1) << "rx_udpmac2 is valid ";
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy to args
|
// copy to args and convert to hex
|
||||||
snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort);
|
snprintf(args[0], array_size, "%x", detector_shm()->receiverUDPPort);
|
||||||
sls::strcpy_safe(args[1], getReceiverUDPIP().c_str());
|
snprintf(args[1], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP));
|
||||||
sls::strcpy_safe(args[2], getReceiverUDPMAC().c_str());
|
sls::strcpy_safe(args[2], getReceiverUDPMAC().c_str());
|
||||||
|
sls::removeChar(args[2], ':');
|
||||||
|
snprintf(args[3], array_size, "%x", __builtin_bswap32(detector_shm()->detectorIP));
|
||||||
sls::strcpy_safe(args[4], getDetectorMAC().c_str());
|
sls::strcpy_safe(args[4], getDetectorMAC().c_str());
|
||||||
|
sls::removeChar(args[4], ':');
|
||||||
snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2);
|
snprintf(args[5], array_size, "%x", detector_shm()->receiverUDPPort2);
|
||||||
sls::strcpy_safe(args[6], getReceiverUDPIP2().c_str());
|
snprintf(args[6], array_size, "%x", __builtin_bswap32(detector_shm()->receiverUDPIP2));
|
||||||
sls::strcpy_safe(args[7], getReceiverUDPMAC2().c_str());
|
sls::strcpy_safe(args[7], getReceiverUDPMAC2().c_str());
|
||||||
|
sls::removeChar(args[7], ':');
|
||||||
|
snprintf(args[8], array_size, "%x", __builtin_bswap32(detector_shm()->detectorIP2));
|
||||||
sls::strcpy_safe(args[9], getDetectorMAC2().c_str());
|
sls::strcpy_safe(args[9], getDetectorMAC2().c_str());
|
||||||
|
sls::removeChar(args[9], ':');
|
||||||
|
|
||||||
// number of interfaces and which one
|
// number of interfaces and which one
|
||||||
snprintf(args[10], array_size, "%x", detector_shm()->numUDPInterfaces);
|
snprintf(args[10], array_size, "%x", detector_shm()->numUDPInterfaces);
|
||||||
@ -1625,40 +1630,17 @@ int slsDetector::configureMAC() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FILE_LOG(logDEBUG1) << "receiver udp port:" << std::dec << args[0] << "-";
|
FILE_LOG(logDEBUG1) << "receiver udp port:" << std::dec << args[0] << "-";
|
||||||
// converting receiverUDPIP to string hex
|
|
||||||
sls::strcpy_safe(args[1], sls::stringIpToHex(args[1]).c_str());
|
|
||||||
FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[1] << "-";
|
FILE_LOG(logDEBUG1) << "receiver udp ip:" << args[1] << "-";
|
||||||
// MAC already in hex removing :
|
|
||||||
sls::removeChar(args[2], ':');
|
|
||||||
FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[2] << "-";
|
FILE_LOG(logDEBUG1) << "receiver udp mac:" << args[2] << "-";
|
||||||
// converting detectorIP to string hex
|
|
||||||
sls::strcpy_safe(args[3], sls::stringIpToHex(getDetectorIP()).c_str());
|
|
||||||
FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[3] << "-";
|
FILE_LOG(logDEBUG1) << "detecotor udp ip:" << args[3] << "-";
|
||||||
// MAC already in hex removing :
|
|
||||||
sls::removeChar(args[4], ':');
|
|
||||||
FILE_LOG(logDEBUG1) << "detector udp mac:" << args[4] << "-";
|
FILE_LOG(logDEBUG1) << "detector udp mac:" << args[4] << "-";
|
||||||
|
|
||||||
FILE_LOG(logDEBUG1) << "receiver udp port2:" << std::dec << args[5] << "-";
|
FILE_LOG(logDEBUG1) << "receiver udp port2:" << std::dec << args[5] << "-";
|
||||||
if (!strcmp(args[6], "none")) {
|
|
||||||
sprintf(args[6], "%d", 0);
|
|
||||||
} else {
|
|
||||||
sls::strcpy_safe(args[6], sls::stringIpToHex(args[6]).c_str());
|
|
||||||
}
|
|
||||||
FILE_LOG(logDEBUG1) << "receiver udp ip2:" << args[6] << "-";
|
FILE_LOG(logDEBUG1) << "receiver udp ip2:" << args[6] << "-";
|
||||||
// MAC already in hex removing :
|
|
||||||
sls::removeChar(args[7], ':');
|
|
||||||
FILE_LOG(logDEBUG1) << "receiver udp mac2:" << args[7] << "-";
|
FILE_LOG(logDEBUG1) << "receiver udp mac2:" << args[7] << "-";
|
||||||
// converting detectorIP to string hex
|
|
||||||
sls::strcpy_safe(args[8], sls::stringIpToHex(getDetectorIP2()).c_str());
|
|
||||||
|
|
||||||
FILE_LOG(logDEBUG1) << "detecotor udp ip2:" << args[8] << "-";
|
FILE_LOG(logDEBUG1) << "detecotor udp ip2:" << args[8] << "-";
|
||||||
// MAC already in hex removing :
|
|
||||||
sls::removeChar(args[9], ':');
|
|
||||||
FILE_LOG(logDEBUG1) << "detector udp mac2:" << args[9] << "-";
|
FILE_LOG(logDEBUG1) << "detector udp mac2:" << args[9] << "-";
|
||||||
|
|
||||||
FILE_LOG(logDEBUG1) << "number of udp interfaces:" << std::dec << args[10] << "-";
|
FILE_LOG(logDEBUG1) << "number of udp interfaces:" << std::dec << args[10] << "-";
|
||||||
FILE_LOG(logDEBUG1) << "selected udp interface:" << std::dec << args[11] << "-";
|
FILE_LOG(logDEBUG1) << "selected udp interface:" << std::dec << args[11] << "-";
|
||||||
|
|
||||||
FILE_LOG(logDEBUG1) << "row:" << args[12] << "-";
|
FILE_LOG(logDEBUG1) << "row:" << args[12] << "-";
|
||||||
FILE_LOG(logDEBUG1) << "col:" << args[13] << "-";
|
FILE_LOG(logDEBUG1) << "col:" << args[13] << "-";
|
||||||
|
|
||||||
@ -2098,11 +2080,9 @@ std::string slsDetector::setDetectorMAC2(const std::string &detectorMAC) {
|
|||||||
std::string slsDetector::getDetectorMAC2() { return MacAddrToString(detector_shm()->detectorMAC2); }
|
std::string slsDetector::getDetectorMAC2() { return MacAddrToString(detector_shm()->detectorMAC2); }
|
||||||
|
|
||||||
std::string slsDetector::setDetectorIP(const std::string &detectorIP) {
|
std::string slsDetector::setDetectorIP(const std::string &detectorIP) {
|
||||||
// struct sockaddr_in sa;
|
|
||||||
uint32_t ip{0};
|
|
||||||
if (detectorIP.length() && detectorIP.length() < INET_ADDRSTRLEN) {
|
if (detectorIP.length() && detectorIP.length() < INET_ADDRSTRLEN) {
|
||||||
int result = inet_pton(AF_INET, detectorIP.c_str(), &ip);
|
auto ip = IpStringToUint(detectorIP.c_str());
|
||||||
if (result == 0) {
|
if (ip == 0) {
|
||||||
throw RuntimeError("setDetectorIP: IP Address should be VALID and "
|
throw RuntimeError("setDetectorIP: IP Address should be VALID and "
|
||||||
"in xxx.xxx.xxx.xxx format");
|
"in xxx.xxx.xxx.xxx format");
|
||||||
} else {
|
} else {
|
||||||
@ -2117,17 +2097,12 @@ std::string slsDetector::setDetectorIP(const std::string &detectorIP) {
|
|||||||
return getDetectorIP();
|
return getDetectorIP();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string slsDetector::getDetectorIP() const {
|
std::string slsDetector::getDetectorIP() const { return IpToString(detector_shm()->detectorIP); }
|
||||||
char ipstring[INET_ADDRSTRLEN];
|
|
||||||
inet_ntop(AF_INET, &detector_shm()->detectorIP, ipstring, INET_ADDRSTRLEN);
|
|
||||||
return ipstring;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string slsDetector::setDetectorIP2(const std::string &detectorIP) {
|
std::string slsDetector::setDetectorIP2(const std::string &detectorIP) {
|
||||||
uint32_t ip{0};
|
|
||||||
if (detectorIP.length() && detectorIP.length() < 16) {
|
if (detectorIP.length() && detectorIP.length() < 16) {
|
||||||
int result = inet_pton(AF_INET, detectorIP.c_str(), &ip);
|
auto ip = IpStringToUint(detectorIP.c_str());
|
||||||
if (result == 0) {
|
if (ip == 0) {
|
||||||
throw RuntimeError("setDetectorIP: IP Address 2 should be VALID "
|
throw RuntimeError("setDetectorIP: IP Address 2 should be VALID "
|
||||||
"and in xxx.xxx.xxx.xxx format");
|
"and in xxx.xxx.xxx.xxx format");
|
||||||
} else {
|
} else {
|
||||||
@ -2142,11 +2117,7 @@ std::string slsDetector::setDetectorIP2(const std::string &detectorIP) {
|
|||||||
return getDetectorIP2();
|
return getDetectorIP2();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string slsDetector::getDetectorIP2() const {
|
std::string slsDetector::getDetectorIP2() const { return IpToString(detector_shm()->detectorIP2); }
|
||||||
char ipstring[INET_ADDRSTRLEN];
|
|
||||||
inet_ntop(AF_INET, &detector_shm()->detectorIP2, ipstring, INET_ADDRSTRLEN);
|
|
||||||
return ipstring;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string slsDetector::setReceiverHostname(const std::string &receiverIP) {
|
std::string slsDetector::setReceiverHostname(const std::string &receiverIP) {
|
||||||
FILE_LOG(logDEBUG1) << "Setting up Receiver with " << receiverIP;
|
FILE_LOG(logDEBUG1) << "Setting up Receiver with " << receiverIP;
|
||||||
@ -2728,7 +2699,6 @@ int slsDetector::setUDPConnection() {
|
|||||||
if (strlen(retvals[0])) {
|
if (strlen(retvals[0])) {
|
||||||
FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0];
|
FILE_LOG(logDEBUG1) << "Receiver UDP MAC returned : " << retvals[0];
|
||||||
detector_shm()->receiverUDPMAC = MacStringToUint(retvals[0]);
|
detector_shm()->receiverUDPMAC = MacStringToUint(retvals[0]);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (strlen(retvals[1])) {
|
if (strlen(retvals[1])) {
|
||||||
FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1];
|
FILE_LOG(logDEBUG1) << "Receiver UDP MAC2 returned : " << retvals[1];
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef FILELOG_MAX_LEVEL
|
#ifndef FILELOG_MAX_LEVEL
|
||||||
#define FILELOG_MAX_LEVEL logINFO
|
#define FILELOG_MAX_LEVEL logDEBUG1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,9 +6,11 @@ namespace sls {
|
|||||||
std::string MacAddrToString(uint64_t mac);
|
std::string MacAddrToString(uint64_t mac);
|
||||||
uint64_t MacStringToUint(std::string mac);
|
uint64_t MacStringToUint(std::string mac);
|
||||||
|
|
||||||
uint32_t IpStringToUint(const char* ipstr);
|
uint32_t IpStringToUint(const char *ipstr);
|
||||||
std::string IpToString(uint32_t ip);
|
|
||||||
|
|
||||||
uint32_t HostnameToIp(const char *const hostname);
|
std::string IpToString(uint32_t ip);
|
||||||
|
std::string IpToHexString(uint32_t ip);
|
||||||
|
|
||||||
|
uint32_t HostnameToIp(const char *hostname);
|
||||||
|
|
||||||
} // namespace sls
|
} // namespace sls
|
||||||
|
@ -39,15 +39,21 @@ uint32_t IpStringToUint(const char *ipstr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string IpToString(uint32_t ip) {
|
std::string IpToString(uint32_t ip) {
|
||||||
char ipstring[INET_ADDRSTRLEN];
|
char ipstring[INET_ADDRSTRLEN]{};
|
||||||
if (inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN) == nullptr) {
|
inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN);
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
// TODO! Check return
|
|
||||||
return ipstring;
|
return ipstring;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t HostnameToIp(const char *const hostname) {
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t HostnameToIp(const char *hostname) {
|
||||||
struct addrinfo hints, *result;
|
struct addrinfo hints, *result;
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = AF_INET;
|
hints.ai_family = AF_INET;
|
||||||
@ -61,6 +67,8 @@ uint32_t HostnameToIp(const char *const hostname) {
|
|||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace sls
|
} // namespace sls
|
||||||
|
|
||||||
// char ipstring[INET_ADDRSTRLEN];
|
// char ipstring[INET_ADDRSTRLEN];
|
||||||
|
@ -21,11 +21,11 @@ TEST_CASE("Convert mac address") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Convert IP"){
|
TEST_CASE("Convert IP") {
|
||||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
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"};
|
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){
|
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||||
auto ip = vec_addr[i];
|
auto ip = vec_addr[i];
|
||||||
auto answer = vec_ans[i];
|
auto answer = vec_ans[i];
|
||||||
|
|
||||||
@ -35,10 +35,19 @@ TEST_CASE("Convert IP"){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("IP not valid"){
|
TEST_CASE("IP not valid") {
|
||||||
|
|
||||||
CHECK(IpStringToUint("hej") == 0);
|
CHECK(IpStringToUint("hej") == 0);
|
||||||
CHECK(IpStringToUint("mpc2408") == 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("Lookup ip")
|
Reference in New Issue
Block a user