xref: /btstack/platform/posix/btstack_signal.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
1970469cfSMatthias Ringwald /*
2970469cfSMatthias Ringwald  * Copyright (C) 2021 BlueKitchen GmbH
3970469cfSMatthias Ringwald  *
4970469cfSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5970469cfSMatthias Ringwald  * modification, are permitted provided that the following conditions
6970469cfSMatthias Ringwald  * are met:
7970469cfSMatthias Ringwald  *
8970469cfSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9970469cfSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10970469cfSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11970469cfSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12970469cfSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13970469cfSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14970469cfSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15970469cfSMatthias Ringwald  *    from this software without specific prior written permission.
16970469cfSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17970469cfSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18970469cfSMatthias Ringwald  *    monetary gain.
19970469cfSMatthias Ringwald  *
20970469cfSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21970469cfSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22970469cfSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25970469cfSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26970469cfSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27970469cfSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28970469cfSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29970469cfSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30970469cfSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31970469cfSMatthias Ringwald  * SUCH DAMAGE.
32970469cfSMatthias Ringwald  *
33970469cfSMatthias Ringwald  * Please inquire about commercial licensing options at
34970469cfSMatthias Ringwald  * [email protected]
35970469cfSMatthias Ringwald  *
36970469cfSMatthias Ringwald  */
37970469cfSMatthias Ringwald 
38970469cfSMatthias Ringwald #define BTSTACK_FILE__ "btstack_signal.c"
39970469cfSMatthias Ringwald 
409e6ecc42SMatthias Ringwald // enable System V signal API
419e6ecc42SMatthias Ringwald #define _POSIX_C_SOURCE 200809
429e6ecc42SMatthias Ringwald 
43970469cfSMatthias Ringwald #include "btstack_signal.h"
44970469cfSMatthias Ringwald 
45970469cfSMatthias Ringwald #include <pthread.h>
46970469cfSMatthias Ringwald #include <signal.h>
47970469cfSMatthias Ringwald #include <string.h>
48970469cfSMatthias Ringwald 
49970469cfSMatthias Ringwald #include "btstack_run_loop.h"
50970469cfSMatthias Ringwald 
signal_callback(void * arg)51970469cfSMatthias Ringwald static void signal_callback(void * arg){
52970469cfSMatthias Ringwald     void (*callback)(void) = (void (*)(void)) arg;
53970469cfSMatthias Ringwald     (*callback)();
54970469cfSMatthias Ringwald }
55970469cfSMatthias Ringwald 
signal_thread(void * arg)56970469cfSMatthias Ringwald static void * signal_thread(void *arg) {
57783a8debSMatthias Ringwald     // prepare registration
58783a8debSMatthias Ringwald     btstack_context_callback_registration_t registration;
59783a8debSMatthias Ringwald     memset(&registration, 0, sizeof(btstack_context_callback_registration_t));
60783a8debSMatthias Ringwald     registration.callback = &signal_callback;
61783a8debSMatthias Ringwald     registration.context  = arg;
62783a8debSMatthias Ringwald 
63970469cfSMatthias Ringwald     while (1){
64970469cfSMatthias Ringwald         // wait for signal
65970469cfSMatthias Ringwald         sigset_t sigset;
66970469cfSMatthias Ringwald         sigemptyset(&sigset);
67970469cfSMatthias Ringwald         sigaddset(&sigset, SIGINT);
68970469cfSMatthias Ringwald         int sig = 0;
69970469cfSMatthias Ringwald         (void) sigwait(&sigset, &sig);
70970469cfSMatthias Ringwald 
71970469cfSMatthias Ringwald         // execute callback on main thread
72970469cfSMatthias Ringwald         btstack_run_loop_execute_on_main_thread(&registration);
73970469cfSMatthias Ringwald     }
74970469cfSMatthias Ringwald     return NULL;
75970469cfSMatthias Ringwald }
76970469cfSMatthias Ringwald 
btstack_signal_register_callback(int signal,void (* callback)(void))77970469cfSMatthias Ringwald void btstack_signal_register_callback(int signal, void (*callback)(void)) {
78970469cfSMatthias Ringwald     // block signal
79970469cfSMatthias Ringwald     sigset_t base_mask;
80970469cfSMatthias Ringwald     sigemptyset (&base_mask);
81970469cfSMatthias Ringwald     sigaddset (&base_mask, signal);
82970469cfSMatthias Ringwald     sigprocmask (SIG_SETMASK, &base_mask, NULL);
83970469cfSMatthias Ringwald 
84970469cfSMatthias Ringwald     // start thread to receive signal
85970469cfSMatthias Ringwald     pthread_t thread;
86970469cfSMatthias Ringwald     pthread_create(&thread, NULL, signal_thread, (void*) callback);
87970469cfSMatthias Ringwald }
88