OSService.cpp
Go to the documentation of this file.
1 #include <rtt/os/fosi.h>
2 #include <rtt/Service.hpp>
4 #include <cstdlib>
5 
6 #ifndef WIN32
7 #include <sys/wait.h>
8 #endif
9 
10 // setEnvString and isEnv needed to be implemented because we
11 // can't provide a getenv() implementation on windows without leaking
12 // memory.
13 #ifdef WIN32
14 std::string getEnvString(const char *name)
15 {
16  char c[32767];
17  if (GetEnvironmentVariable(name, c, sizeof (c)) > 0) {
18  return string(c);
19  }
20  return 0;
21 }
22 
23 bool isEnv(const char* name)
24 {
25  char c;
26  // returns zero if not found and non-zero for needed buffer length.
27  if (GetEnvironmentVariable(name, &c, sizeof (c)) > 0) return true;
28  return false;
29 }
30 
31 int setenv(const char *name, const char *value, int overwrite)
32 {
33  if (overwrite == 0)
34  {
35  char c;
36  if (GetEnvironmentVariable(name, &c, sizeof (c)) > 0) return 0;
37  }
38 
39  if (SetEnvironmentVariable(name, value) != 0) return 0;
40  return -1;
41 }
42 #else
43 std::string getEnvString(const char *name)
44 {
45  std::string ret;
46  char* c = getenv(name);
47  if (c)
48  return std::string(c);
49  return ret;
50 }
51 bool isEnv(const char* name)
52 {
53  if( getenv(name) ) return true;
54  return false;
55 }
56 #endif
57 
58 #include <rtt/os/startstop.h>
59 
60 namespace OCL
61 {
62 
67  class OSService: public RTT::Service
68  {
69  public:
71  RTT::Service("os", parent)
72  {
73  doc("A service that provides access to some useful Operating System functions.");
74  // add the operations
75  addOperation("getenv", &OSService::getenv, this).doc(
76  "Returns the value of an environment variable. If it is not set, returns the empty string.").arg("name",
77  "The name of the environment variable to read.");
78  addOperation("isenv", &OSService::isenv, this).doc(
79  "Checks if an environment variable exists").arg("name",
80  "The name of the environment variable to check.");
81  addOperation("setenv", &OSService::setenv, this).doc(
82  "Sets an environment variable.").arg("name",
83  "The name of the environment variable to write.").arg("value","The text to set.");
84  addOperation("argc", &OSService::argc, this).doc("Returns the number of arguments, given to this application.");
85  addOperation("argv", &OSService::argv, this).doc("Returns the arguments as a sequence of strings, given to this application.");
86  addOperation("sleep", &OSService::sleep, this).doc("Suspend execution of the calling thread")
87  .arg("seconds", "Sleep for x seconds");
88  addOperation("millisleep", &OSService::millisleep, this).doc("Suspend execution of the calling thread")
89  .arg("milliseconds", "Sleep for x milliseconds");
90  addOperation("usleep", &OSService::usleep, this).doc("Suspend execution of the calling thread")
91  .arg("microseconds", "Sleep for x microseconds");
92  addOperation("nanosleep", &OSService::nanosleep, this).doc("Suspend execution of the calling thread")
93  .arg("seconds", "Sleep for x seconds")
94  .arg("nanoseconds", "Sleep for x nanoseconds");
95  addOperation("execute", &OSService::execute, this).doc("Execute a shell command");
96  }
97 
98  int argc(void)
99  {
100  return __os_main_argc();
101  }
102 
103  std::vector<std::string> argv(void)
104  {
105  int a = argc();
106  char** args = __os_main_argv();
107  std::vector<std::string> ret(a);
108  for (int i = 0; i != a; ++i)
109  ret[i] = std::string(args[i]);
110  return ret;
111  }
112 
113  std::string getenv(const std::string& arg)
114  {
115  return getEnvString(arg.c_str());
116  }
117 
118  bool isenv(const std::string& arg)
119  {
120  return isEnv(arg.c_str());
121  }
122 
123  bool setenv(const std::string& arg, const std::string& value)
124  {
125  return ::setenv(arg.c_str(), value.c_str(), 1) == 0;
126  }
127 
128  int sleep(unsigned int seconds)
129  {
130  TIME_SPEC rqtp, rmtp;
131  rqtp.tv_sec = seconds;
132  rqtp.tv_nsec = 0;
133  return rtos_nanosleep(&rqtp, &rmtp);
134  }
135 
136  int millisleep(unsigned int milliseconds)
137  {
138  TIME_SPEC rqtp, rmtp;
139  rqtp.tv_sec = (milliseconds / 1000u);
140  rqtp.tv_nsec = (milliseconds % 1000u) * 1000000u;
141  return rtos_nanosleep(&rqtp, &rmtp);
142  }
143 
144  int usleep(unsigned int microseconds)
145  {
146  TIME_SPEC rqtp, rmtp;
147  rqtp.tv_sec = (microseconds / 1000000u);
148  rqtp.tv_nsec = (microseconds % 1000000u) * 1000u;
149  return rtos_nanosleep(&rqtp, &rmtp);
150  }
151 
152  int nanosleep(unsigned int seconds, unsigned int nanoseconds)
153  {
154  TIME_SPEC rqtp, rmtp;
155  rqtp.tv_sec = seconds;
156  rqtp.tv_nsec = nanoseconds;
157  return rtos_nanosleep(&rqtp, &rmtp);
158  }
159 
160  int execute(const std::string& command)
161  {
162 #ifdef WIN32
163  return system(command.c_str());
164 #else
165  int status = system(command.c_str());
166  if (status < 0) {
167  return status;
168  } else {
169  if (!WIFEXITED(status)) {
170  return -1;
171  } else {
172  return WEXITSTATUS(status);
173  }
174  }
175 #endif
176  }
177  };
178 }
179 
181 
Variable opBinary s not applicable to args
Definition: rtt.cpp:757
int nanosleep(unsigned int seconds, unsigned int nanoseconds)
Definition: OSService.cpp:152
bool isEnv(const char *name)
Definition: OSService.cpp:51
int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
Operation< Signature > & addOperation(Operation< Signature > &op)
int __os_main_argc(void)
bool setenv(const std::string &arg, const std::string &value)
Definition: OSService.cpp:123
int usleep(unsigned int microseconds)
Definition: OSService.cpp:144
std::vector< std::string > argv(void)
Definition: OSService.cpp:103
int argc(void)
Definition: OSService.cpp:98
char ** __os_main_argv(void)
int millisleep(unsigned int milliseconds)
Definition: OSService.cpp:136
bool isenv(const std::string &arg)
Definition: OSService.cpp:118
const std::string & doc() const
int execute(const std::string &command)
Definition: OSService.cpp:160
std::string getenv(const std::string &arg)
Definition: OSService.cpp:113
std::string getEnvString(const char *name)
Definition: OSService.cpp:43
int setenv(const char *name, const char *value, int overwrite)
int sleep(unsigned int seconds)
Definition: OSService.cpp:128
struct timespec TIME_SPEC
OSService(RTT::TaskContext *parent)
Definition: OSService.cpp:70
shared_ptr parent


ocl
Author(s): OCL Development Team
autogenerated on Mon Mar 23 2020 04:47:19