Skip to content

Commit 32a87df

Browse files
authored
Split signal handler code (#225)
1 parent 6fca46b commit 32a87df

File tree

5 files changed

+156
-89
lines changed

5 files changed

+156
-89
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ add_executable(ypspur-coordinator
204204
src/param.c
205205
src/ping.c
206206
src/serial.c
207+
src/signal.c
207208
src/ssm_spur_handler.c
208209
src/utility.c
209210
src/ypprotocol.c

include/ypspur/signal.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2010-2025 The YP-Spur Authors, except where otherwise indicated.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to
5+
// deal in the Software without restriction, including without limitation the
6+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
// sell copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
#ifndef YPSPUR_SIGNAL_H
22+
#define YPSPUR_SIGNAL_H
23+
24+
int ctrlc_setjmp();
25+
void enable_ctrlc_handling(const int enable);
26+
27+
#endif // YPSPUR_SIGNAL_H

include/ypspur/ssm_spur_handler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define YPSPUR_SSM_SPUR_HANDLER_H
2323

2424
#include <ypspur/command.h>
25+
#include <ypspur/odometry.h>
2526

2627
void init_ypspurSSM(int ssm_id);
2728
void end_ypspurSSM(void);

src/signal.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (c) 2010-2025 The YP-Spur Authors, except where otherwise indicated.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to
5+
// deal in the Software without restriction, including without limitation the
6+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
// sell copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
#include <setjmp.h>
22+
#include <signal.h>
23+
#include <stdio.h>
24+
25+
#ifdef __MINGW32__
26+
#include <windows.h>
27+
#endif // __MINGW32__
28+
29+
#include <ypspur/param.h>
30+
#include <ypspur/serial.h>
31+
#include <ypspur/ssm_spur_handler.h>
32+
#include <ypspur/yprintf.h>
33+
34+
#if HAVE_SIGLONGJMP
35+
sigjmp_buf ctrlc_capture;
36+
#elif HAVE_LONGJMP
37+
jmp_buf ctrlc_capture;
38+
#endif // HAVE_SIGLONGJMP
39+
40+
#if defined(__MINGW32__)
41+
BOOL WINAPI win32_ctrlc_handler(DWORD type)
42+
{
43+
fprintf(stderr, "\n");
44+
#ifdef HAVE_SSM
45+
// SSM終了処理
46+
if (!option(OPTION_WITHOUT_SSM))
47+
{
48+
end_ypspurSSM();
49+
}
50+
#endif // HAVE_SSM
51+
if (!(option(OPTION_WITHOUT_DEVICE)))
52+
{
53+
serial_close();
54+
}
55+
56+
return TRUE;
57+
}
58+
#else
59+
void emergency(int sig)
60+
{
61+
fprintf(stderr, "\n");
62+
#if HAVE_SIGLONGJMP
63+
siglongjmp(ctrlc_capture, 1);
64+
#elif HAVE_LONGJMP
65+
longjmp(ctrlc_capture, 1);
66+
#else
67+
#ifdef HAVE_SSM
68+
// SSM終了処理
69+
if (!option(OPTION_WITHOUT_SSM))
70+
{
71+
end_ypspurSSM();
72+
}
73+
#endif // HAVE_SSM
74+
if (!(option(OPTION_WITHOUT_DEVICE)))
75+
{
76+
serial_close();
77+
}
78+
79+
exit(0);
80+
#endif // HAVE_SIGLONGJMP
81+
}
82+
#endif // defined(__MINGW32__)
83+
84+
void enable_ctrlc_handling(const int enable)
85+
{
86+
#if defined(__MINGW32__)
87+
if (enable)
88+
{
89+
if (!SetConsoleCtrlHandler(win32_ctrlc_handler, TRUE))
90+
{
91+
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler registration failed.\n");
92+
}
93+
}
94+
else
95+
{
96+
if (!SetConsoleCtrlHandler(NULL, FALSE))
97+
{
98+
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler restoration failed.\n");
99+
}
100+
}
101+
#else
102+
if (enable)
103+
{
104+
signal(SIGINT, emergency);
105+
}
106+
else
107+
{
108+
signal(SIGINT, SIG_DFL);
109+
}
110+
#endif // defined(__MINGW32__)
111+
}
112+
113+
int ctrlc_setjmp()
114+
{
115+
#if HAVE_SIGLONGJMP
116+
return sigsetjmp(ctrlc_capture, 1);
117+
#elif HAVE_LONGJMP
118+
return setjmp(ctrlc_capture);
119+
#else
120+
return 0;
121+
#endif // HAVE_SIGLONGJMP
122+
}

src/ypspur-coordinator.c

Lines changed: 5 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <unistd.h>
2626

2727
#include <fcntl.h>
28-
#include <setjmp.h>
29-
#include <signal.h>
3028
#include <sys/stat.h>
3129
#include <sys/time.h>
3230
#include <sys/types.h>
@@ -40,6 +38,7 @@
4038
#include <ypspur/param.h>
4139
#include <ypspur/ping.h>
4240
#include <ypspur/serial.h>
41+
#include <ypspur/signal.h>
4342
#include <ypspur/ssm_spur_handler.h>
4443
#include <ypspur/utility.h>
4544
#include <ypspur/ypprotocol.h>
@@ -50,81 +49,6 @@
5049

5150
#include <pthread.h>
5251

53-
#if HAVE_SIGLONGJMP
54-
sigjmp_buf ctrlc_capture;
55-
#elif HAVE_LONGJMP
56-
jmp_buf ctrlc_capture;
57-
#endif // HAVE_SIGLONGJMP
58-
59-
#if defined(__MINGW32__)
60-
BOOL WINAPI win32_ctrlc_handler(DWORD type)
61-
{
62-
fprintf(stderr, "\n");
63-
#ifdef HAVE_SSM
64-
// SSM終了処理
65-
if (!option(OPTION_WITHOUT_SSM))
66-
end_ypspurSSM();
67-
#endif // HAVE_SSM
68-
if (!(option(OPTION_WITHOUT_DEVICE)))
69-
{
70-
serial_close();
71-
}
72-
73-
return TRUE;
74-
}
75-
#else
76-
void emergency(int sig)
77-
{
78-
fprintf(stderr, "\n");
79-
#if HAVE_SIGLONGJMP
80-
siglongjmp(ctrlc_capture, 1);
81-
#elif HAVE_LONGJMP
82-
longjmp(ctrlc_capture, 1);
83-
#else
84-
#ifdef HAVE_SSM
85-
// SSM終了処理
86-
if (!option(OPTION_WITHOUT_SSM))
87-
end_ypspurSSM();
88-
#endif // HAVE_SSM
89-
if (!(option(OPTION_WITHOUT_DEVICE)))
90-
{
91-
serial_close();
92-
}
93-
94-
exit(0);
95-
#endif // HAVE_SIGLONGJMP
96-
}
97-
#endif // defined(__MINGW32__)
98-
99-
void escape_road(const int enable)
100-
{
101-
#if defined(__MINGW32__)
102-
if (enable)
103-
{
104-
if (!SetConsoleCtrlHandler(win32_ctrlc_handler, TRUE))
105-
{
106-
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler registration failed.\n");
107-
}
108-
}
109-
else
110-
{
111-
if (!SetConsoleCtrlHandler(NULL, FALSE))
112-
{
113-
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler restoration failed.\n");
114-
}
115-
}
116-
#else
117-
if (enable)
118-
{
119-
signal(SIGINT, emergency);
120-
}
121-
else
122-
{
123-
signal(SIGINT, SIG_DFL);
124-
}
125-
#endif // defined(__MINGW32__)
126-
}
127-
12852
int main(int argc, char* argv[])
12953
{
13054
pthread_t command_thread;
@@ -497,22 +421,14 @@ int main(int argc, char* argv[])
497421
update_thread_en = 0;
498422
}
499423

500-
// オドメトリ受信ループ
501-
#if HAVE_SIGLONGJMP
502-
if (sigsetjmp(ctrlc_capture, 1) != 0)
503-
{
504-
quit = 1;
505-
}
506-
else
507-
#elif HAVE_LONGJMP
508-
if (setjmp(ctrlc_capture) != 0)
424+
// オドメトリ受信ループ
425+
if (ctrlc_setjmp() != 0)
509426
{
510427
quit = 1;
511428
}
512429
else
513-
#endif // HAVE_SIGLONGJMP
514430
{
515-
escape_road(1);
431+
enable_ctrlc_handling(1);
516432
if (!(option(OPTION_WITHOUT_DEVICE)))
517433
{
518434
odometry_receive_loop();
@@ -526,7 +442,7 @@ int main(int argc, char* argv[])
526442
}
527443
yprintf(OUTPUT_LV_INFO, "Connection to %s was closed.\n", param->device_name);
528444
}
529-
escape_road(0);
445+
enable_ctrlc_handling(0);
530446

531447
// 終了処理
532448
if (update_thread_en)

0 commit comments

Comments
 (0)