ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
timeout-glib.c
Go to the documentation of this file.
1 
9 /*
10  *
11  * BlueZ - Bluetooth protocol stack for Linux
12  *
13  * Copyright (C) 2014 Intel Corporation. All rights reserved.
14  *
15  *
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  * Lesser General Public License for more details.
25  *
26  */
27 
28 #include "timeout.h"
29 
30 #include <glib.h>
31 
32 struct timeout_data {
35  void *user_data;
36 };
37 
38 static gboolean timeout_callback(gpointer user_data)
39 {
40  struct timeout_data *data = user_data;
41 
42  if (data->func(data->user_data))
43  return TRUE;
44 
45  return FALSE;
46 }
47 
48 static void timeout_destroy(gpointer user_data)
49 {
50  struct timeout_data *data = user_data;
51 
52  if (data->destroy)
53  data->destroy(data->user_data);
54 
55  g_free(data);
56 }
57 
58 unsigned int timeout_add(unsigned int timeout, timeout_func_t func,
60 {
61  struct timeout_data *data;
62  guint id;
63 
64  data = g_try_new0(struct timeout_data, 1);
65  if (!data)
66  return 0;
67 
68  data->func = func;
69  data->destroy = destroy;
70  data->user_data = user_data;
71 
72  id = g_timeout_add_full(G_PRIORITY_DEFAULT, timeout, timeout_callback,
73  data, timeout_destroy);
74  if (!id)
75  g_free(data);
76 
77  return id;
78 }
79 
80 void timeout_remove(unsigned int id)
81 {
82  GSource *source = g_main_context_find_source_by_id(NULL, id);
83 
84  if (source)
85  g_source_destroy(source);
86 }
timeout_destroy_func_t destroy
Definition: timeout-glib.c:34
command_func_t func
timeout_func_t func
Definition: timeout-glib.c:33
bool(* timeout_func_t)(void *user_data)
Definition: timeout.h:22
void * user_data
Definition: mainloop.c:83
static gboolean timeout_callback(gpointer user_data)
Definition: timeout-glib.c:38
mainloop_destroy_func destroy
Definition: mainloop.c:82
unsigned int timeout_add(unsigned int timeout, timeout_func_t func, void *user_data, timeout_destroy_func_t destroy)
Definition: timeout-glib.c:58
unsigned int id
Definition: att.c:435
void timeout_remove(unsigned int id)
Definition: timeout-glib.c:80
void(* timeout_destroy_func_t)(void *user_data)
Definition: timeout.h:23
static void timeout_destroy(gpointer user_data)
Definition: timeout-glib.c:48