diff --git a/CMakeLists.txt b/CMakeLists.txt index 2140ade..51b9a96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.03) -project(keller_dv2ps VERSION 1.0) +project(tcpip VERSION 0.1) add_compile_options( -Wall @@ -11,22 +11,13 @@ add_compile_options( -Wno-unused-function ) -set( - INCLUDES - ${PROJECT_SOURCE_DIR} - $ENV{MIDASSYS}/include - $ENV{MIDASSYS}/mscb/include -) - set( LIBRARIES - -lz + # -lz # compression library -lpthread -lutil - -lrt - -ldl - $ENV{MIDAS_PREFIX}/lib/libmfe.a - $ENV{MIDAS_PREFIX}/lib/libmidas.a + -lrt # librt for real time guarantees (really just backwards compat) + -ldl # libdl for dynamic loading ) set( @@ -52,13 +43,18 @@ set_property( target_include_directories( tcpip + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE - ${INCLUDES} + $ENV{MIDASSYS}/include ) target_link_libraries( tcpip + PRIVATE ${LIBRARIES} + $ENV{MIDAS_PREFIX}/lib/libmfe.a + $ENV{MIDAS_PREFIX}/lib/libmidas.a ) ################################################################################ @@ -73,12 +69,13 @@ add_executable( target_include_directories( test_tcpip PRIVATE - ${INCLUDES} + $ENV{MIDASSYS}/include ) target_link_libraries( test_tcpip - PUBLIC + PRIVATE tcpip ${LIBRARIES} + $ENV{MIDAS_PREFIX}/lib/libmidas.a ) diff --git a/bus/tcpip.cxx b/bus/tcpip.cxx index fbeea1e..292ddeb 100644 --- a/bus/tcpip.cxx +++ b/bus/tcpip.cxx @@ -428,7 +428,7 @@ int tcpip_gets(TCPIP_INFO *info, char *data, int size, char *pattern, if (info == nullptr) return FE_ERR_DRIVER; - if (size == 0) + if (size <= 0) return FE_SUCCESS; n = 0; @@ -499,7 +499,7 @@ int tcpip_gets(TCPIP_INFO *info, char *data, int size, char *pattern, currtime = ss_millitime(); - } while (status <= 0); /* dont return if an alarm signal was cought */ + } while (status <= 0); /* dont return if an alarm signal was caught */ /* it may be that 0 bytes are read */ i = recv(info->fd, data + n, 1, 0); @@ -507,9 +507,13 @@ int tcpip_gets(TCPIP_INFO *info, char *data, int size, char *pattern, if (i >= 0) { n += i; - if (pattern != nullptr && pattern[0]) - if (strstr(data, pattern) != NULL) + if (pattern != nullptr && pattern[0]) { + char *loc = strstr(data, pattern); + if (loc != NULL) { + *loc = 0; break; + } + } } else { #ifdef OS_UNIX @@ -550,7 +554,7 @@ int tcpip_init(HNDLE hkey, TCPIP_INFO **pinfo) { cm_get_experiment_database(&hDB, NULL); /* create TCPIP settings record */ - status = db_create_record(hDB, hkey, "BD", TCPIP_SETTINGS_STR); + status = db_check_record(hDB, hkey, "BD", TCPIP_SETTINGS_STR, true); if (status != DB_SUCCESS) return FE_ERR_ODB; diff --git a/test/test_tcpip.cxx b/test/test_tcpip.cxx index 7a1a414..c8ae5df 100644 --- a/test/test_tcpip.cxx +++ b/test/test_tcpip.cxx @@ -2,35 +2,22 @@ #include "bus/tcpip.h" -#include +#include #define DEFAULT_FE_TIMEOUT 10000 /* 10 seconds for watchdog timeout */ -// TODO duplicated for testing -typedef struct { - char host[256]; - int port; - char ets_in_use; /* Y = logout eds/ets/epr connection */ - int debug; -} TCPIP_SETTINGS; - -#define TCPIP_SETTINGS_STR \ +#define TCPIP_TEST_SETTINGS_STR \ "\ Host = STRING : [256] localhost\n\ Port = INT : 9070\n\ TS_in_Use = CHAR : N\n\ Debug = INT : 0\n\ " -// TODO duplicated for testing char host_name[HOST_NAME_LENGTH]; char exp_name[NAME_LENGTH]; -int main() { - HNDLE hDB, hKey; - INT command_status = FE_SUCCESS; - void *bd_info = nullptr; - +INT connect_to_odb() { host_name[0] = 0; exp_name[0] = 0; @@ -38,55 +25,191 @@ int main() { sizeof(exp_name)); printf("Starting experiment: '%s' on host '%s'\n", exp_name, host_name); - command_status = - cm_connect_experiment1(host_name, exp_name, "tcpip_test", NULL, - DEFAULT_ODB_SIZE, DEFAULT_FE_TIMEOUT); + return cm_connect_experiment1(host_name, exp_name, "tcpip_test", NULL, + DEFAULT_ODB_SIZE, DEFAULT_FE_TIMEOUT); +} - if (command_status != CM_SUCCESS) - return 1; +INT configure_odb(HNDLE *hKey, int port) { + HNDLE hDB; + char *base_key = "/Equipment/test/tcpip"; + char tcpip_settings_string[256]; + + snprintf(tcpip_settings_string, 256, + "Host = STRING : [256] %s\n" + "Port = INT : %d\n" + "TS_in_Use = CHAR : %c\n" + "Debug = INT : %d\n", + "localhost", port, 'N', 0); cm_get_experiment_database(&hDB, NULL); - db_create_record(hDB, 0, "/Equipment/test/tcpip", - "Device = STRING : [8] tcpip\n"); - db_find_key(hDB, 0, "/Equipment/test/tcpip", &hKey); - db_create_record(hDB, hKey, "BD", TCPIP_SETTINGS_STR); + db_create_record(hDB, 0, base_key, "Device = STRING : [12] test_tcpip\n"); + db_find_key(hDB, 0, base_key, hKey); + + return db_create_record(hDB, *hKey, "BD", tcpip_settings_string); +} + +INT test_write_read_string(void *bd_info) { + char *test_string = "Test String!\n"; + char response[14] = {0}; + INT command_status; + + // Write string + command_status = tcpip(CMD_PUTS, bd_info, test_string); + + if (command_status != FE_SUCCESS) + return command_status; // Couldn't write string + + printf("Sent %s", test_string); + + // Read response + command_status = tcpip(CMD_GETS, bd_info, response, 13, NULL, 1000); + + if (command_status != FE_SUCCESS) + return command_status; // Couldn't read string + + printf("Received %s", response); + + if (strcmp(test_string, response) != 0) + return FE_ERR_DRIVER; + + return FE_SUCCESS; +} + +INT test_write_read_string_delim(void *bd_info) { + char *test_string = "Test String!\n"; + char response[14] = {0}; + INT command_status; + + // Write string + command_status = tcpip(CMD_PUTS, bd_info, test_string); + + if (command_status != FE_SUCCESS) + return command_status; // Couldn't write string + + printf("Sent %s", test_string); + + // Read up to delimiter ' ' + command_status = tcpip(CMD_GETS, bd_info, response, 14, " ", 1000); + + if (command_status != FE_SUCCESS) + return command_status; // Couldn't read string + + printf("Received %s\n", response); + + if (strcmp(response, "Test") != 0) + return FE_ERR_DRIVER; + + // Read up to delimiter '!\n' + command_status = tcpip(CMD_GETS, bd_info, response, 14, "!\n", 1000); + + if (command_status != FE_SUCCESS) + return command_status; // Couldn't read string + + printf("Received %s\n", response); + + if (strcmp(response, "String") != 0) + return FE_ERR_DRIVER; + + return FE_SUCCESS; +} + +void array_to_string(char *str, int str_size, char *arr, int arr_size) { + + int len = 0; + memset(str, 0, str_size); + + snprintf(str, str_size, "["); + for (int i = 0; i < arr_size - 1; ++i) { + len = strlen(str); + snprintf(str + len, str_size - len, "%d ", *(arr + i)); + } + len = strlen(str); + snprintf(str + len, str_size - len, "%d]", *(arr + arr_size - 1)); +} + +INT test_write_read_data(void *bd_info) { + + char test_data[] = {0, 1, 3, 10, 5}; + char response[256] = {0}; + char response_str[256]; + INT command_status; + + // Write string + command_status = tcpip(CMD_WRITE, bd_info, test_data, 5); + + if (command_status != FE_SUCCESS) + return command_status; // Couldn't write string + + array_to_string(response_str, 256, test_data, 5); + printf("Sent %s\n", response_str); + + // Read response + command_status = tcpip(CMD_READ, bd_info, response, 5, 1000); + + if (command_status != FE_SUCCESS) + return command_status; // Couldn't read string + + array_to_string(response_str, 256, response, 5); + printf("Received %s\n", response_str); + + if (memcmp(test_data, response, 5) != 0) + return FE_ERR_DRIVER; + + return FE_SUCCESS; +} + +INT run_tests() { + HNDLE hKey; + void *bd_info; + INT command_status; + + command_status = configure_odb(&hKey, 9070); + + if (command_status != DB_SUCCESS) + return command_status; // Couldn't configure connection settings // Setup connection command_status = tcpip(CMD_INIT, hKey, &bd_info); - if (command_status != FE_SUCCESS) { - cm_disconnect_experiment(); - return 1; - } + if (command_status != FE_SUCCESS) + return command_status; // Couldn't initialise bus driver - // Write string - tcpip(CMD_PUTS, bd_info, "Test String!\n"); + command_status = test_write_read_string(bd_info); - // Read response - char response[14] = {0}; - tcpip(CMD_GETS, bd_info, response, 13, NULL, 1000); - // TODO tcpip(CMD_GETS, bd_info, response, 20, '\n', 1000); - printf("Received %s", response); + if (command_status != FE_SUCCESS) + return command_status; - // Write data - char data[] = {0, 1, 3, 10, 5}; - tcpip(CMD_WRITE, bd_info, data, 5); + command_status = test_write_read_string_delim(bd_info); - // Read response - tcpip(CMD_READ, bd_info, response, 5, 1000); - printf("Received %d %d %d %d %d\n", response[0], response[1], response[2], - response[3], response[4]); + if (command_status != FE_SUCCESS) + return command_status; + + command_status = test_write_read_data(bd_info); + + if (command_status != FE_SUCCESS) + return command_status; // Close connection command_status = tcpip(CMD_EXIT, &bd_info); - if (command_status != FE_SUCCESS) { - cm_disconnect_experiment(); - return 1; - } + if (command_status != FE_SUCCESS) + return command_status; // Couldn't clean up bus driver + + return FE_SUCCESS; +} + +int main() { + INT command_status; + + command_status = connect_to_odb(); + + if (command_status != DB_SUCCESS) + return 1; // Couldn't connect to ODB + + command_status = run_tests(); cm_disconnect_experiment(); - return 0; + return command_status == FE_SUCCESS ? 0 : command_status; }