Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ add_executable(ypspur-coordinator
src/param.c
src/ping.c
src/serial.c
src/signal.c
src/ssm_spur_handler.c
src/utility.c
src/ypprotocol.c
Expand Down
27 changes: 27 additions & 0 deletions include/ypspur/signal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2010-2025 The YP-Spur Authors, except where otherwise indicated.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef YPSPUR_SIGNAL_H
#define YPSPUR_SIGNAL_H

int ctrlc_setjmp();
void enable_ctrlc_handling(const int enable);

#endif // YPSPUR_SIGNAL_H
1 change: 1 addition & 0 deletions include/ypspur/ssm_spur_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define YPSPUR_SSM_SPUR_HANDLER_H

#include <ypspur/command.h>
#include <ypspur/odometry.h>

void init_ypspurSSM(int ssm_id);
void end_ypspurSSM(void);
Expand Down
122 changes: 122 additions & 0 deletions src/signal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) 2010-2025 The YP-Spur Authors, except where otherwise indicated.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <setjmp.h>
#include <signal.h>
#include <stdio.h>

#ifdef __MINGW32__
#include <windows.h>
#endif // __MINGW32__

#include <ypspur/param.h>
#include <ypspur/serial.h>
#include <ypspur/ssm_spur_handler.h>
#include <ypspur/yprintf.h>

#if HAVE_SIGLONGJMP
sigjmp_buf ctrlc_capture;
#elif HAVE_LONGJMP
jmp_buf ctrlc_capture;
#endif // HAVE_SIGLONGJMP

#if defined(__MINGW32__)
BOOL WINAPI win32_ctrlc_handler(DWORD type)
{
fprintf(stderr, "\n");
#ifdef HAVE_SSM
// SSM終了処理
if (!option(OPTION_WITHOUT_SSM))
{
end_ypspurSSM();
}
#endif // HAVE_SSM
if (!(option(OPTION_WITHOUT_DEVICE)))
{
serial_close();
}

return TRUE;
}
#else
void emergency(int sig)
{
fprintf(stderr, "\n");
#if HAVE_SIGLONGJMP
siglongjmp(ctrlc_capture, 1);
#elif HAVE_LONGJMP
longjmp(ctrlc_capture, 1);
#else
#ifdef HAVE_SSM
// SSM終了処理
if (!option(OPTION_WITHOUT_SSM))
{
end_ypspurSSM();
}
#endif // HAVE_SSM
if (!(option(OPTION_WITHOUT_DEVICE)))
{
serial_close();
}

exit(0);
#endif // HAVE_SIGLONGJMP
}
#endif // defined(__MINGW32__)

void enable_ctrlc_handling(const int enable)
{
#if defined(__MINGW32__)
if (enable)
{
if (!SetConsoleCtrlHandler(win32_ctrlc_handler, TRUE))
{
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler registration failed.\n");
}
}
else
{
if (!SetConsoleCtrlHandler(NULL, FALSE))
{
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler restoration failed.\n");
}
}
#else
if (enable)
{
signal(SIGINT, emergency);
}
else
{
signal(SIGINT, SIG_DFL);
}
#endif // defined(__MINGW32__)
}

int ctrlc_setjmp()
{
#if HAVE_SIGLONGJMP
return sigsetjmp(ctrlc_capture, 1);
#elif HAVE_LONGJMP
return setjmp(ctrlc_capture);
#else
return 0;
#endif // HAVE_SIGLONGJMP
}
94 changes: 5 additions & 89 deletions src/ypspur-coordinator.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <unistd.h>

#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
Expand All @@ -40,6 +38,7 @@
#include <ypspur/param.h>
#include <ypspur/ping.h>
#include <ypspur/serial.h>
#include <ypspur/signal.h>
#include <ypspur/ssm_spur_handler.h>
#include <ypspur/utility.h>
#include <ypspur/ypprotocol.h>
Expand All @@ -50,81 +49,6 @@

#include <pthread.h>

#if HAVE_SIGLONGJMP
sigjmp_buf ctrlc_capture;
#elif HAVE_LONGJMP
jmp_buf ctrlc_capture;
#endif // HAVE_SIGLONGJMP

#if defined(__MINGW32__)
BOOL WINAPI win32_ctrlc_handler(DWORD type)
{
fprintf(stderr, "\n");
#ifdef HAVE_SSM
// SSM終了処理
if (!option(OPTION_WITHOUT_SSM))
end_ypspurSSM();
#endif // HAVE_SSM
if (!(option(OPTION_WITHOUT_DEVICE)))
{
serial_close();
}

return TRUE;
}
#else
void emergency(int sig)
{
fprintf(stderr, "\n");
#if HAVE_SIGLONGJMP
siglongjmp(ctrlc_capture, 1);
#elif HAVE_LONGJMP
longjmp(ctrlc_capture, 1);
#else
#ifdef HAVE_SSM
// SSM終了処理
if (!option(OPTION_WITHOUT_SSM))
end_ypspurSSM();
#endif // HAVE_SSM
if (!(option(OPTION_WITHOUT_DEVICE)))
{
serial_close();
}

exit(0);
#endif // HAVE_SIGLONGJMP
}
#endif // defined(__MINGW32__)

void escape_road(const int enable)
{
#if defined(__MINGW32__)
if (enable)
{
if (!SetConsoleCtrlHandler(win32_ctrlc_handler, TRUE))
{
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler registration failed.\n");
}
}
else
{
if (!SetConsoleCtrlHandler(NULL, FALSE))
{
yprintf(OUTPUT_LV_ERROR, "Error: Win32 Ctrl+C handler restoration failed.\n");
}
}
#else
if (enable)
{
signal(SIGINT, emergency);
}
else
{
signal(SIGINT, SIG_DFL);
}
#endif // defined(__MINGW32__)
}

int main(int argc, char* argv[])
{
pthread_t command_thread;
Expand Down Expand Up @@ -497,22 +421,14 @@ int main(int argc, char* argv[])
update_thread_en = 0;
}

// オドメトリ受信ループ
#if HAVE_SIGLONGJMP
if (sigsetjmp(ctrlc_capture, 1) != 0)
{
quit = 1;
}
else
#elif HAVE_LONGJMP
if (setjmp(ctrlc_capture) != 0)
// オドメトリ受信ループ
if (ctrlc_setjmp() != 0)
{
quit = 1;
}
else
#endif // HAVE_SIGLONGJMP
{
escape_road(1);
enable_ctrlc_handling(1);
if (!(option(OPTION_WITHOUT_DEVICE)))
{
odometry_receive_loop();
Expand All @@ -526,7 +442,7 @@ int main(int argc, char* argv[])
}
yprintf(OUTPUT_LV_INFO, "Connection to %s was closed.\n", param->device_name);
}
escape_road(0);
enable_ctrlc_handling(0);

// 終了処理
if (update_thread_en)
Expand Down
Loading