From bcac05a811ca3520c98444f415ec36ac7eb5bb7c Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Fri, 3 May 2019 09:47:18 +0200 Subject: [PATCH] examples/notification: Catch CTRL-C to exit properly --- examples/notification/notification.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/examples/notification/notification.c b/examples/notification/notification.c index 1b663e6..9c6307a 100644 --- a/examples/notification/notification.c +++ b/examples/notification/notification.c @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -31,6 +32,8 @@ // Battery Level UUID const uuid_t g_battery_level_uuid = CREATE_UUID16(0x2A19); +static GMainLoop *m_main_loop; + void notification_handler(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data) { int i; @@ -42,6 +45,10 @@ void notification_handler(const uuid_t* uuid, const uint8_t* data, size_t data_l printf("\n"); } +static void on_user_abort(int arg) { + g_main_loop_quit(m_main_loop); +} + static void usage(char *argv[]) { printf("%s \n", argv[0]); } @@ -69,12 +76,15 @@ int main(int argc, char *argv[]) { goto DISCONNECT; } - GMainLoop *loop = g_main_loop_new(NULL, 0); - g_main_loop_run(loop); + // Catch CTRL-C + signal(SIGINT, on_user_abort); + + m_main_loop = g_main_loop_new(NULL, 0); + g_main_loop_run(m_main_loop); // In case we quit the main loop, clean the connection gattlib_notification_stop(connection, &g_battery_level_uuid); - g_main_loop_unref(loop); + g_main_loop_unref(m_main_loop); DISCONNECT: gattlib_disconnect(connection);