trying to clean up to factor out tcp connection
Test And Build / Format (push) Has been cancelled
Test And Build / Lint (push) Has been cancelled

This commit is contained in:
2025-09-05 16:02:11 +02:00
parent 5f31eee67f
commit bfae17547c
2 changed files with 172 additions and 172 deletions
+33 -20
View File
@@ -12,12 +12,10 @@ add_compile_options(
)
set(
LIBS
# -lz # compression library
-lpthread
-lutil
-lrt # librt for real time guarantees (really just backwards compat)
-ldl # libdl for dynamic loading
TCPIP_DRIVER_DIR
$ENV{MIDASSYS}/drivers/bus/tcpip.cxx
CACHE STRING
"path to tcpip driver that should be used"
)
set(
@@ -26,17 +24,35 @@ set(
)
set(
TCPIP_DRIVER_DIR
./tcpip
CACHE STRING
"path to tcpip driver that should be used"
LIBS
# -lz # compression library
-lpthread
-lutil
-lrt # librt for real time guarantees (really just backwards compat)
-ldl # libdl for dynamic loading
$ENV{MIDAS_PREFIX}/lib/libmfe.a
$ENV{MIDAS_PREFIX}/lib/libmidas.a
)
add_subdirectory(
# TODO not sure, should it be a submodule? does it really make sense to
# separate everything?
${TCPIP_DRIVER_DIR} ./bus/tcpip
)
if(IS_DIRECTORY ${TCPIP_DRIVER_DIR})
add_subdirectory(
# TODO not sure, should it be a submodule? does it really make sense to
# separate everything?
${TCPIP_DRIVER_DIR} ./bus/tcpip
)
set(
LIBS
${LIBS}
tcpip
)
else()
set(
DRIVERS
${DRIVERS}
${TCPIP_DRIVER_DIR}
)
endif()
add_executable(
strayfield
@@ -53,7 +69,7 @@ set_property(
)
target_include_directories(
strayfield
strayfield
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
$ENV{MIDASSYS}/drivers
@@ -62,9 +78,6 @@ target_include_directories(
)
target_link_libraries(
strayfield
$ENV{MIDAS_PREFIX}/lib/libmfe.a
$ENV{MIDAS_PREFIX}/lib/libmidas.a
tcpip
strayfield
${LIBS}
)
+139 -152
View File
@@ -30,12 +30,14 @@ typedef struct {
INT err_timeout; //!< error report interval in sec
} STRAYFIELD_SETTINGS;
//! Initializing string for the struct <pre>COBRA_SETTINGS</pre>
//! These entries are stored in the database at
//! /Equipment/COBRA/Settings/Devices/IN/DD/
// TODO seems like the ODB code is broken. It doesn't seem to correctly parse
// the strings in an array... Seem below where I even try extracting via their
// logic the string, and it matches this format, but doesn't work again when
// importing.
// So you need to still specify the input names manually in the odb
#define STRAYFIELD_SETTINGS_STR \
"\
Input Name = STRING[MAX_INPUT_CHANNELS] : \n\
Input Name = STRING[4] : \n\
[32] Cobra Current\n\
[32] Cobra Comp Coil\n\
[32] Comet 1\n\
@@ -51,10 +53,6 @@ Error Timeout = INT : 3600\n\
typedef struct {
STRAYFIELD_SETTINGS strayfield_settings;
HNDLE hKey;
HNDLE MCAST_GROUP_COBRAhkey;
HNDLE MCAST_GROUP_COMEThkey;
HNDLE max_update_duration_hkey;
HNDLE err_timeout_hkey;
float Strayfield[MAX_INPUT_CHANNELS]; //!< [0] Cobra Current, [1] Cobra Comp
//!< Coil Current, [2] Comet1 current,
//!< [3] Comet2 current
@@ -72,6 +70,30 @@ typedef struct {
int num_channels; //!< number of channels when init is called
} STRAYFIELD_INFO;
INT get_string_data(HNDLE hDB, HNDLE hKey, char *path, void *result, int size,
INT dtype) {
HNDLE dKey;
INT status;
// get key
status = db_find_key(hDB, hKey, path, &dKey);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "", "db_find_key() ERROR %d finding %s key", status,
path);
return FE_ERR_ODB;
}
// read data
status = db_get_data(hDB, dKey, result, &size, dtype);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "", "db_get_data() ERROR %d reading %s data", status,
path);
return FE_ERR_ODB;
}
return FE_SUCCESS;
}
INT strayfield_in_init(HNDLE hKey, STRAYFIELD_INFO **pinfo,
INT channels) // NOTE: no bus driver!
{
@@ -83,7 +105,6 @@ INT strayfield_in_init(HNDLE hKey, STRAYFIELD_INFO **pinfo,
info = new STRAYFIELD_INFO();
*pinfo = info;
info->num_channels = channels;
if (channels > MAX_INPUT_CHANNELS) {
cm_msg(MERROR, "strafield_in_init",
"Warning number of input channels is %d, "
@@ -101,6 +122,11 @@ INT strayfield_in_init(HNDLE hKey, STRAYFIELD_INFO **pinfo,
info->hKey = hKey;
char bufferstr[10000];
size = sizeof(bufferstr);
db_copy(hDB, hKey, bufferstr, &size, "DD");
printf(bufferstr);
// create settings record
status = db_create_record(hDB, hKey, "DD", STRAYFIELD_SETTINGS_STR);
if (status != DB_SUCCESS) {
@@ -109,83 +135,37 @@ INT strayfield_in_init(HNDLE hKey, STRAYFIELD_INFO **pinfo,
return FE_ERR_ODB;
}
// get MCAST_GROUP_COBRA key
status = db_find_key(hDB, hKey, "DD/MCAST_GROUP_COBRA",
&info->MCAST_GROUP_COBRAhkey);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_find_key() ERROR %d finding DD/MCAST_GROUP_COBRA key",
status);
}
// read MCAST_GROUP_COBRA to variable
status = get_string_data(
hDB, info->hKey, "DD/MCAST_GROUP_COBRA",
&info->strayfield_settings.MCAST_GROUP_COBRA,
sizeof(info->strayfield_settings.MCAST_GROUP_COBRA), TID_STRING);
if (status != FE_SUCCESS)
return status;
// read MCAST_GROUP_COBRA to private variables
size = sizeof(info->strayfield_settings.MCAST_GROUP_COBRA);
status = db_get_data(hDB, info->MCAST_GROUP_COBRAhkey,
&info->strayfield_settings.MCAST_GROUP_COBRA, &size,
TID_STRING);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_get_data() ERROR %d reading DD/MCAST_GROUP_COBRA data",
status);
}
// read MCAST_GROUP_COMET to variable
status = get_string_data(
hDB, info->hKey, "DD/MCAST_GROUP_COMET",
&info->strayfield_settings.MCAST_GROUP_COMET,
sizeof(info->strayfield_settings.MCAST_GROUP_COMET), TID_STRING);
if (status != FE_SUCCESS)
return status;
// get MCAST_GROUP_COMET key
status = db_find_key(hDB, hKey, "DD/MCAST_GROUP_COMET",
&info->MCAST_GROUP_COMEThkey);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_find_key() ERROR %d finding DD/MCAST_GROUP_COMET key",
status);
}
// read "Max Update Duration" to variable
status = get_string_data(
hDB, info->hKey, "DD/Max Update Duration",
&info->strayfield_settings.max_update_duration,
sizeof(info->strayfield_settings.max_update_duration), TID_INT32);
if (status != FE_SUCCESS)
return status;
// read MCAST_GROUP_COMET to private variables
size = sizeof(info->strayfield_settings.MCAST_GROUP_COMET);
status = db_get_data(hDB, info->MCAST_GROUP_COMEThkey,
&info->strayfield_settings.MCAST_GROUP_COMET, &size,
TID_STRING);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_get_data() ERROR %d reading DD/MCAST_GROUP_COMET data",
status);
}
// get "Max Update Duration" key
status = db_find_key(hDB, hKey, "DD/Max Update Duration",
&info->max_update_duration_hkey);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_find_key() ERROR %d finding key DD/Max Update Duration",
status);
}
// read "Max Update Duration" to private variables
size = sizeof(info->strayfield_settings.max_update_duration);
status = db_get_data(hDB, info->max_update_duration_hkey,
&info->strayfield_settings.max_update_duration, &size,
TID_INT);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_get_data() ERROR %d reading key DD/Max Update Duration",
status);
}
// get "Error Timeout" key
status =
db_find_key(hDB, hKey, "DD/Error Timeout", &info->err_timeout_hkey);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_find_key() ERROR %d finding key DD/Error Timeout", status);
}
// read "Error Timeout" to private variables
size = sizeof(info->strayfield_settings.err_timeout);
status =
db_get_data(hDB, info->err_timeout_hkey,
&info->strayfield_settings.err_timeout, &size, TID_INT);
if (status != DB_SUCCESS) {
cm_msg(MERROR, "",
"db_get_data() ERROR %d reading key DD/Error Timeout", status);
}
// read "Error Timeout" to variable
status = get_string_data(hDB, info->hKey, "DD/Error Timeout",
&info->strayfield_settings.err_timeout,
sizeof(info->strayfield_settings.err_timeout),
TID_INT32);
if (status != FE_SUCCESS)
return status;
info->last_update_COBRA = ss_time();
info->last_update_COMET = ss_time();
@@ -210,25 +190,26 @@ INT strayfield_in_init(HNDLE hKey, STRAYFIELD_INFO **pinfo,
return FE_SUCCESS;
}
INT strayfield_exit(STRAYFIELD_INFO **info) {
STRAYFIELD_INFO *pinfo;
INT strayfield_exit(STRAYFIELD_INFO **pinfo) {
STRAYFIELD_INFO *info;
pinfo = (STRAYFIELD_INFO *)info;
if (pinfo == nullptr || *pinfo == nullptr)
return FE_SUCCESS;
if (pinfo) {
if (pinfo->sock_COBRA != -1) {
close(pinfo->sock_COBRA);
pinfo->sock_COBRA = -1;
}
info = *pinfo;
if (pinfo->sock_COMET != -1) {
close(pinfo->sock_COMET);
pinfo->sock_COMET = -1;
}
free(pinfo);
if (info->sock_COBRA != -1) {
close(info->sock_COBRA);
info->sock_COBRA = -1;
}
if (info->sock_COMET != -1) {
close(info->sock_COMET);
info->sock_COMET = -1;
}
free(info);
#if defined(_MSC_VER)
WSACleanup();
#endif
@@ -451,8 +432,8 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
status = cm_yield(1);
if ((status == SS_ABORT) || (status == SS_EXIT) ||
(status == RPC_SHUTDOWN)) {
//TODO must be an Andrea thing
//should_exit = TRUE;
// TODO must be an Andrea thing
// should_exit = TRUE;
return status;
}
size = sizeof(addr);
@@ -465,7 +446,7 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
// receive data from socket
#ifdef MIDEBUG
//TODO this should use the inbuilt debug option
// TODO this should use the inbuilt debug option
r if (debug) cm_msg(MLOG, "", "Reading COBRA Broadcast");
#endif
@@ -495,7 +476,7 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
cm_set_watchdog_params(wflag, wtimeout);
#ifdef MIDEBUG
//TODO this should use the inbuilt debug option
// TODO this should use the inbuilt debug option
if (debug) {
if (n == -1)
cm_msg(MLOG, "",
@@ -507,23 +488,26 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
str);
}
#endif
//TODO must be an Andrea thing
// if (verbose) {
// if (n == -1) {
// #ifdef _MSC_VER
// if (terrno != WSAEWOULDBLOCK)
// #else
// if (terrno != EAGAIN)
// #endif
// printf("Got status %d ERROR %d receiving COBRA "
// "Broadcast\n",
// n, terrno);
// else
// printf("**Buffer empty - no more COBRA Broadcasts\n");
// } else if (n > 0)
// printf("Got status %d receiving COBRA Broadcast \"%s\"\n",
// n, str);
// }
// TODO must be an Andrea thing
// if (verbose) {
// if (n == -1) {
// #ifdef _MSC_VER
// if (terrno != WSAEWOULDBLOCK)
// #else
// if (terrno != EAGAIN)
// #endif
// printf("Got status %d ERROR %d receiving
// COBRA "
// "Broadcast\n",
// n, terrno);
// else
// printf("**Buffer empty - no more COBRA
// Broadcasts\n");
// } else if (n > 0)
// printf("Got status %d receiving COBRA
// Broadcast \"%s\"\n",
// n, str);
// }
// got broadcast and broadcast starts with COBRA
if ((n > 0) && strstr(str, "COBRA")) {
@@ -665,8 +649,8 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
status = cm_yield(1);
if ((status == SS_ABORT) || (status == SS_EXIT) ||
(status == RPC_SHUTDOWN)) {
//TODO must be an Andrea thing
//should_exit = TRUE;
// TODO must be an Andrea thing
// should_exit = TRUE;
return status;
}
@@ -680,7 +664,7 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
// receive data from socket
#ifdef MIDEBUG
//TODO this should use the inbuilt debug option
// TODO this should use the inbuilt debug option
if (debug)
cm_msg(MLOG, "", "Reading COMET Broadcast");
#endif
@@ -710,7 +694,7 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
cm_set_watchdog_params(wflag, wtimeout);
#ifdef MIDEBUG
//TODO this should use the inbuilt debug option
// TODO this should use the inbuilt debug option
if (debug) {
if (n == -1)
cm_msg(MLOG, "",
@@ -722,23 +706,26 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
str);
}
#endif
//TODO must be an Andrea thing
// if (verbose) {
// if (n == -1) {
// #ifdef _MSC_VER
// if (terrno != WSAEWOULDBLOCK)
// #else
// if (terrno != EAGAIN)
// #endif
// printf("Got status %d ERROR %d receiving COMET "
// "Broadcast\n",
// n, terrno);
// else
// printf("**Buffer empty - no more COMET Broadcasts\n");
// } else if (n > 0)
// printf("Got status %d receiving COMET Broadcast \"%s\"\n",
// n, str);
// }
// TODO must be an Andrea thing
// if (verbose) {
// if (n == -1) {
// #ifdef _MSC_VER
// if (terrno != WSAEWOULDBLOCK)
// #else
// if (terrno != EAGAIN)
// #endif
// printf("Got status %d ERROR %d receiving
// COMET "
// "Broadcast\n",
// n, terrno);
// else
// printf("**Buffer empty - no more COMET
// Broadcasts\n");
// } else if (n > 0)
// printf("Got status %d receiving COMET
// Broadcast \"%s\"\n",
// n, str);
// }
// got broadcast and broadcast starts with COMET
if ((n > 0) && strstr(str, "COMET")) {
@@ -796,7 +783,7 @@ INT strayfield_get(STRAYFIELD_INFO *info, INT channel, float *pvalue) {
printf("Broadcast: %.2f %.2f %.2f %.2f\n", info->Strayfield[0],
info->Strayfield[1], info->Strayfield[2], info->Strayfield[3]);
//TODO this should use the inbuilt debug option
// TODO this should use the inbuilt debug option
if (debug) {
cm_msg(MLOG, "", "Broadcast: %.2f %.2f %.2f %.2f", info->Strayfield[0],
info->Strayfield[1], info->Strayfield[2], info->Strayfield[3]);
@@ -834,19 +821,19 @@ INT strayfield_in(INT cmd, ...) {
status = strayfield_exit(pinfo);
break;
case CMD_GET:
info = va_arg(argptr, STRAYFIELD_INFO *);
channel = va_arg(argptr, INT);
pvalue = va_arg(argptr, float *);
status = strayfield_get(info, channel, pvalue);
break;
// case CMD_GET:
// info = va_arg(argptr, STRAYFIELD_INFO *);
// channel = va_arg(argptr, INT);
// pvalue = va_arg(argptr, float *);
// status = strayfield_get(info, channel, pvalue);
// break;
case CMD_GET_LABEL:
info = va_arg(argptr, STRAYFIELD_INFO *);
channel = va_arg(argptr, INT);
name = va_arg(argptr, char *);
status = strayfield_get_in_default_name(info, channel, name);
break;
// case CMD_GET_LABEL:
// info = va_arg(argptr, STRAYFIELD_INFO *);
// channel = va_arg(argptr, INT);
// name = va_arg(argptr, char *);
// status = strayfield_get_in_default_name(info, channel, name);
// break;
default:
break;