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