YDLIDAR SDK  V1.4.5
v8stdint.h
1 #ifndef V8STDINT_H_
2 #define V8STDINT_H_
3 
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string>
8 #include <string.h>
9 #include <signal.h>
10 #include <cerrno>
11 #include <stdexcept>
12 #include <csignal>
13 #include <sys/stat.h>
14 #if defined(_MSC_VER)
15 #include <io.h>
16 #endif
17 
18 #if !defined(_MSC_VER)
19 #include <unistd.h>
20 #endif
21 
22 #define UNUSED(x) (void)x
23 
24 #if !defined(_MSC_VER)
25 # define _access access
26 #endif
27 
28 #if defined(_WIN32) && !defined(__MINGW32__)
29 typedef signed char int8_t;
30 typedef unsigned char uint8_t;
31 typedef short int16_t;
32 typedef unsigned short uint16_t;
33 typedef int int32_t;
34 typedef unsigned int uint32_t;
35 typedef __int64 int64_t;
36 typedef unsigned __int64 uint64_t;
37 #else
38 
39 #include <stdint.h>
40 
41 #endif
42 
43 #define __small_endian
44 
45 #ifndef __GNUC__
46 #define __attribute__(x)
47 #endif
48 
49 
50 #ifdef _AVR_
51 typedef uint8_t _size_t;
52 #define THREAD_PROC
53 #elif defined (WIN64)
54 typedef uint64_t _size_t;
55 #define THREAD_PROC __stdcall
56 #elif defined (WIN32)
57 typedef uint32_t _size_t;
58 #define THREAD_PROC __stdcall
59 #elif defined (_M_X64)
60 typedef uint64_t _size_t;
61 #define THREAD_PROC __stdcall
62 #elif defined (__GNUC__)
63 typedef unsigned long _size_t;
64 #define THREAD_PROC
65 #elif defined (__ICCARM__)
66 typedef uint32_t _size_t;
67 #define THREAD_PROC
68 #endif
69 
70 typedef _size_t (THREAD_PROC *thread_proc_t)(void *);
71 
72 typedef int32_t result_t;
73 
74 #define RESULT_OK 0
75 #define RESULT_TIMEOUT -1
76 #define RESULT_FAIL -2
77 
78 #define INVALID_TIMESTAMP (0)
79 
80 enum {
81  DEVICE_DRIVER_TYPE_SERIALPORT = 0x0,
82  DEVICE_DRIVER_TYPE_TCP = 0x1,
83 };
84 
85 
86 #define IS_OK(x) ( (x) == RESULT_OK )
87 #define IS_TIMEOUT(x) ( (x) == RESULT_TIMEOUT )
88 #define IS_FAIL(x) ( (x) == RESULT_FAIL )
89 
90 
91 // Determine if sigaction is available
92 #if __APPLE__ || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
93 #define HAS_SIGACTION
94 #endif
95 
96 
97 static volatile sig_atomic_t g_signal_status = 0;
98 
99 #ifdef HAS_SIGACTION
100 static struct sigaction old_action;
101 #else
102 typedef void (* signal_handler_t)(int);
103 static signal_handler_t old_signal_handler = 0;
104 #endif
105 
106 #ifdef HAS_SIGACTION
107 inline struct sigaction
108 set_sigaction(int signal_value, const struct sigaction &action)
109 #else
110 inline signal_handler_t
111 set_signal_handler(int signal_value, signal_handler_t signal_handler)
112 #endif
113 {
114 #ifdef HAS_SIGACTION
115  struct sigaction old_action;
116  ssize_t ret = sigaction(signal_value, &action, &old_action);
117 
118  if (ret == -1)
119 #else
120  signal_handler_t old_signal_handler = std::signal(signal_value, signal_handler);
121 
122  // NOLINTNEXTLINE(readability/braces)
123  if (old_signal_handler == SIG_ERR)
124 #endif
125  {
126  const size_t error_length = 1024;
127  // NOLINTNEXTLINE(runtime/arrays)
128  char error_string[error_length];
129 #ifndef _WIN32
130 #if (defined(_GNU_SOURCE) && !defined(ANDROID) &&(_POSIX_C_SOURCE >= 200112L))
131  char *msg = strerror_r(errno, error_string, error_length);
132 
133  if (msg != error_string) {
134  strncpy(error_string, msg, error_length);
135  msg[error_length - 1] = '\0';
136  }
137 
138 #else
139  int error_status = strerror_r(errno, error_string, error_length);
140 
141  if (error_status != 0) {
142  throw std::runtime_error("Failed to get error string for errno: " +
143  std::to_string(errno));
144  }
145 
146 #endif
147 #else
148  strerror_s(error_string, error_length, errno);
149 #endif
150  // *INDENT-OFF* (prevent uncrustify from making unnecessary indents here)
151  throw std::runtime_error(
152  std::string("Failed to set SIGINT signal handler: (" + std::to_string(errno) + ")") +
153  error_string);
154  // *INDENT-ON*
155  }
156 
157 #ifdef HAS_SIGACTION
158  return old_action;
159 #else
160  return old_signal_handler;
161 #endif
162 }
163 
164 inline void trigger_interrupt_guard_condition(int signal_value) {
165  g_signal_status = signal_value;
166  signal(signal_value, SIG_DFL);
167 }
168 
169 inline void
170 #ifdef HAS_SIGACTION
171 signal_handler(int signal_value, siginfo_t *siginfo, void *context)
172 #else
173 signal_handler(int signal_value)
174 #endif
175 {
176  // TODO(wjwwood): remove? move to console logging at some point?
177  printf("signal_handler(%d)\n", signal_value);
178 
179 #ifdef HAS_SIGACTION
180 
181  if (old_action.sa_flags & SA_SIGINFO) {
182  if (old_action.sa_sigaction != NULL) {
183  old_action.sa_sigaction(signal_value, siginfo, context);
184  }
185  } else {
186  if (
187  old_action.sa_handler != NULL && // Is set
188  old_action.sa_handler != SIG_DFL && // Is not default
189  old_action.sa_handler != SIG_IGN) { // Is not ignored
190  old_action.sa_handler(signal_value);
191  }
192  }
193 
194 #else
195 
196  if (old_signal_handler) {
197  old_signal_handler(signal_value);
198  }
199 
200 #endif
201 
202  trigger_interrupt_guard_condition(signal_value);
203 }
204 
205 namespace ydlidar {
206 
207 inline void init(int argc, char *argv[]) {
208  UNUSED(argc);
209  UNUSED(argv);
210 #ifdef HAS_SIGACTION
211  struct sigaction action;
212  memset(&action, 0, sizeof(action));
213  sigemptyset(&action.sa_mask);
214  action.sa_sigaction = ::signal_handler;
215  action.sa_flags = SA_SIGINFO;
216  ::old_action = set_sigaction(SIGINT, action);
217  set_sigaction(SIGTERM, action);
218 
219 #else
220  ::old_signal_handler = set_signal_handler(SIGINT, ::signal_handler);
221  // Register an on_shutdown hook to restore the old signal handler.
222 #endif
223 }
224 inline bool ok() {
225  return g_signal_status == 0;
226 }
227 inline void shutdownNow() {
228  trigger_interrupt_guard_condition(SIGINT);
229 }
230 
231 //inline bool fileExists(const std::string filename) {
232 // return 0 == _access(filename.c_str(), 0x00 ); // 0x00 = Check for existence only!
233 //}
234 
235 inline bool fileExists(const std::string filename) {
236 #ifdef _WIN32
237  struct _stat info = {0};
238  int ret = _stat(filename.c_str(), &info);
239 #else
240  struct stat info = {0};
241  int ret = stat(filename.c_str(), &info);
242 #endif
243  return (ret == 0);
244  /*return 0 == _access(filename.c_str(), 0x00 ); // 0x00 = Check for existence only!*/
245 }
246 
247 
248 }// namespace ydlidar
249 
250 
251 #endif // V8STDINT_H_
Definition: help_info.h:39