From 1f8cffdbbb4068e1d810ac32b57b8c3e62431e18 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 8 Sep 2016 11:30:12 +0200 Subject: [PATCH 1/2] Added operations sleep, usleep, nanosleep and execute to the os service Signed-off-by: Johannes Meyer --- ocl/OSService.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/ocl/OSService.cpp b/ocl/OSService.cpp index 31c971eb..4a2f2186 100644 --- a/ocl/OSService.cpp +++ b/ocl/OSService.cpp @@ -1,8 +1,11 @@ - +#include #include #include #include +#ifndef WIN32 +#include +#endif // setEnvString and isEnv needed to be implemented because we // can't provide a getenv() implementation on windows without leaking @@ -80,6 +83,14 @@ namespace OCL "The name of the environment variable to write.").arg("value","The text to set."); addOperation("argc", &OSService::argc, this).doc("Returns the number of arguments, given to this application."); addOperation("argv", &OSService::argv, this).doc("Returns the arguments as a sequence of strings, given to this application."); + addOperation("sleep", &OSService::sleep, this).doc("Suspend execution of the calling thread") + .arg("seconds", "Sleep for x seconds"); + addOperation("usleep", &OSService::usleep, this).doc("Suspend execution of the calling thread") + .arg("microseconds", "Sleep for x microseconds"); + addOperation("nanosleep", &OSService::nanosleep, this).doc("Suspend execution of the calling thread") + .arg("seconds", "Sleep for x seconds") + .arg("nanoseconds", "Sleep for x nanoseconds"); + addOperation("execute", &OSService::execute, this).doc("Execute a shell command"); } int argc(void) @@ -101,14 +112,58 @@ namespace OCL { return getEnvString(arg.c_str()); } + bool isenv(const std::string& arg) { return isEnv(arg.c_str()); } + bool setenv(const std::string& arg, const std::string& value) { return ::setenv(arg.c_str(), value.c_str(), 1) == 0; } + + int sleep(unsigned int seconds) + { + TIME_SPEC rqtp, rmtp; + rqtp.tv_sec = seconds; + rqtp.tv_nsec = 0; + return rtos_nanosleep(&rqtp, &rmtp); + } + + int usleep(unsigned int microseconds) + { + TIME_SPEC rqtp, rmtp; + rqtp.tv_sec = (microseconds / 1000000u); + rqtp.tv_nsec = (microseconds % 1000000u) * 1000u; + return rtos_nanosleep(&rqtp, &rmtp); + } + + int nanosleep(unsigned int seconds, unsigned int nanoseconds) + { + TIME_SPEC rqtp, rmtp; + rqtp.tv_sec = seconds; + rqtp.tv_nsec = nanoseconds; + return rtos_nanosleep(&rqtp, &rmtp); + } + + int execute(const std::string& command) + { +#ifdef WIN32 + return system(command.c_str()); +#else + int status = system(command.c_str()); + if (status < 0) { + return status; + } else { + if (!WIFEXITED(status)) { + return -1; + } else { + return WEXITSTATUS(status); + } + } +#endif + } }; } From 0eaa53c96f62841fc9ddcf38a71fbbc44c5d65bc Mon Sep 17 00:00:00 2001 From: Ruben Smits Date: Thu, 2 Feb 2017 11:29:19 +0100 Subject: [PATCH 2/2] Use orocos macro for helloworld executable Signed-off-by: Ruben Smits --- helloworld/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helloworld/CMakeLists.txt b/helloworld/CMakeLists.txt index edac4078..dce334a8 100644 --- a/helloworld/CMakeLists.txt +++ b/helloworld/CMakeLists.txt @@ -6,7 +6,7 @@ IF ( BUILD_HELLOWORLD ) FILE( GLOB SRCS [^.]*.cpp ) FILE( GLOB HPPS [^.]*.hpp ) - ADD_EXECUTABLE( helloworld ${SRCS} ) + orocos_executable( helloworld ${SRCS} ) orocos_component( orocos-ocl-helloworld ${SRCS} ) SET_TARGET_PROPERTIES( orocos-ocl-helloworld PROPERTIES SOVERSION ${OCL_SOVERSION}