xref: /btstack/platform/posix/btstack_signal.c (revision 783a8debb986dadf80e1e0260970c1aaf86b52ed)
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
23970469cfSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24970469cfSMatthias Ringwald  * RINGWALD 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 
40970469cfSMatthias Ringwald #include "btstack_signal.h"
41970469cfSMatthias Ringwald 
42970469cfSMatthias Ringwald #include <pthread.h>
43970469cfSMatthias Ringwald #include <signal.h>
44970469cfSMatthias Ringwald #include <string.h>
45970469cfSMatthias Ringwald 
46970469cfSMatthias Ringwald #include "btstack_run_loop.h"
47970469cfSMatthias Ringwald 
48970469cfSMatthias Ringwald static void signal_callback(void * arg){
49970469cfSMatthias Ringwald     void (*callback)(void) = (void (*)(void)) arg;
50970469cfSMatthias Ringwald     (*callback)();
51970469cfSMatthias Ringwald }
52970469cfSMatthias Ringwald 
53970469cfSMatthias Ringwald static void * signal_thread(void *arg) {
54*783a8debSMatthias Ringwald     // prepare registration
55*783a8debSMatthias Ringwald     btstack_context_callback_registration_t registration;
56*783a8debSMatthias Ringwald     memset(&registration, 0, sizeof(btstack_context_callback_registration_t));
57*783a8debSMatthias Ringwald     registration.callback = &signal_callback;
58*783a8debSMatthias Ringwald     registration.context  = arg;
59*783a8debSMatthias Ringwald 
60970469cfSMatthias Ringwald     while (1){
61970469cfSMatthias Ringwald         // wait for signal
62970469cfSMatthias Ringwald         sigset_t sigset;
63970469cfSMatthias Ringwald         sigemptyset(&sigset);
64970469cfSMatthias Ringwald         sigaddset(&sigset, SIGINT);
65970469cfSMatthias Ringwald         int sig = 0;
66970469cfSMatthias Ringwald         (void) sigwait(&sigset, &sig);
67970469cfSMatthias Ringwald 
68970469cfSMatthias Ringwald         // execute callback on main thread
69970469cfSMatthias Ringwald         btstack_run_loop_execute_on_main_thread(&registration);
70970469cfSMatthias Ringwald     }
71970469cfSMatthias Ringwald     return NULL;
72970469cfSMatthias Ringwald }
73970469cfSMatthias Ringwald 
74970469cfSMatthias Ringwald void btstack_signal_register_callback(int signal, void (*callback)(void)) {
75970469cfSMatthias Ringwald     // block signal
76970469cfSMatthias Ringwald     sigset_t base_mask;
77970469cfSMatthias Ringwald     sigemptyset (&base_mask);
78970469cfSMatthias Ringwald     sigaddset (&base_mask, signal);
79970469cfSMatthias Ringwald     sigprocmask (SIG_SETMASK, &base_mask, NULL);
80970469cfSMatthias Ringwald 
81970469cfSMatthias Ringwald     // start thread to receive signal
82970469cfSMatthias Ringwald     pthread_t thread;
83970469cfSMatthias Ringwald     pthread_create(&thread, NULL, signal_thread, (void*) callback);
84970469cfSMatthias Ringwald }
85