88
99#include "gattlib_internal.h"
1010
11+
1112int gattlib_register_notification (gattlib_connection_t * connection , gattlib_event_handler_t notification_handler , void * user_data ) {
1213 GError * error = NULL ;
14+ int ret = GATTLIB_SUCCESS ;
15+
16+ g_rec_mutex_lock (& m_gattlib_mutex );
1317
1418 if (connection == NULL ) {
15- return GATTLIB_INVALID_PARAMETER ;
19+ ret = GATTLIB_INVALID_PARAMETER ;
20+ goto EXIT ;
21+ }
22+
23+ if (!gattlib_device_is_valid (connection -> device )) {
24+ ret = GATTLIB_INVALID_PARAMETER ;
25+ goto EXIT ;
1626 }
1727
1828 connection -> notification .callback .notification_handler = notification_handler ;
@@ -25,19 +35,33 @@ int gattlib_register_notification(gattlib_connection_t* connection, gattlib_even
2535 if (error != NULL ) {
2636 GATTLIB_LOG (GATTLIB_ERROR , "gattlib_register_notification: Failed to create thread pool: %s" , error -> message );
2737 g_error_free (error );
28- return GATTLIB_ERROR_INTERNAL ;
38+ ret = GATTLIB_ERROR_INTERNAL ;
39+ goto EXIT ;
2940 } else {
3041 assert (connection -> notification .thread_pool != NULL );
31- return GATTLIB_SUCCESS ;
3242 }
43+
44+ EXIT :
45+ g_rec_mutex_unlock (& m_gattlib_mutex );
46+ return ret ;
3347}
3448
3549int gattlib_register_indication (gattlib_connection_t * connection , gattlib_event_handler_t indication_handler , void * user_data ) {
3650 GError * error = NULL ;
51+ int ret = GATTLIB_SUCCESS ;
52+
53+ g_rec_mutex_lock (& m_gattlib_mutex );
3754
3855 if (connection == NULL ) {
39- return GATTLIB_INVALID_PARAMETER ;
56+ ret = GATTLIB_INVALID_PARAMETER ;
57+ goto EXIT ;
4058 }
59+
60+ if (!gattlib_device_is_valid (connection -> device )) {
61+ ret = GATTLIB_INVALID_PARAMETER ;
62+ goto EXIT ;
63+ }
64+
4165 connection -> indication .callback .notification_handler = indication_handler ;
4266 connection -> indication .user_data = user_data ;
4367
@@ -48,19 +72,36 @@ int gattlib_register_indication(gattlib_connection_t* connection, gattlib_event_
4872 if (error != NULL ) {
4973 GATTLIB_LOG (GATTLIB_ERROR , "gattlib_register_indication: Failed to create thread pool: %s" , error -> message );
5074 g_error_free (error );
51- return GATTLIB_ERROR_INTERNAL ;
52- } else {
53- return GATTLIB_SUCCESS ;
75+ ret = GATTLIB_ERROR_INTERNAL ;
76+ goto EXIT ;
5477 }
78+
79+ EXIT :
80+ g_rec_mutex_unlock (& m_gattlib_mutex );
81+ return ret ;
5582}
5683
5784int gattlib_register_on_disconnect (gattlib_connection_t * connection , gattlib_disconnection_handler_t handler , void * user_data ) {
85+ int ret = GATTLIB_SUCCESS ;
86+
87+ g_rec_mutex_lock (& m_gattlib_mutex );
88+
5889 if (connection == NULL ) {
59- return GATTLIB_INVALID_PARAMETER ;
90+ ret = GATTLIB_INVALID_PARAMETER ;
91+ goto EXIT ;
92+ }
93+
94+ if (!gattlib_device_is_valid (connection -> device )) {
95+ ret = GATTLIB_INVALID_PARAMETER ;
96+ goto EXIT ;
6097 }
98+
6199 connection -> on_disconnection .callback .disconnection_handler = handler ;
62100 connection -> on_disconnection .user_data = user_data ;
63- return GATTLIB_SUCCESS ;
101+
102+ EXIT :
103+ g_rec_mutex_unlock (& m_gattlib_mutex );
104+ return ret ;
64105}
65106
66107void bt_uuid_to_uuid (bt_uuid_t * bt_uuid , uuid_t * uuid ) {
@@ -144,10 +185,8 @@ int gattlib_uuid_cmp(const uuid_t *uuid1, const uuid_t *uuid2) {
144185}
145186
146187void gattlib_handler_free (struct gattlib_handler * handler ) {
147- g_rec_mutex_lock (& handler -> mutex );
148-
149188 if (!gattlib_has_valid_handler (handler )) {
150- goto EXIT ;
189+ return ;
151190 }
152191
153192 // Reset callback to stop calling it after we stopped
@@ -172,9 +211,6 @@ void gattlib_handler_free(struct gattlib_handler* handler) {
172211 g_thread_pool_free (handler -> thread_pool , FALSE /* immediate */ , TRUE /* wait */ );
173212 handler -> thread_pool = NULL ;
174213 }
175-
176- EXIT :
177- g_rec_mutex_unlock (& handler -> mutex );
178214}
179215
180216bool gattlib_has_valid_handler (struct gattlib_handler * handler ) {
@@ -185,8 +221,11 @@ void gattlib_handler_dispatch_to_thread(struct gattlib_handler* handler, void (*
185221 GThreadFunc thread_func , const char * thread_name , void * (* thread_args_allocator )(va_list args ), ...) {
186222 GError * error = NULL ;
187223
224+ g_rec_mutex_lock (& m_gattlib_mutex );
225+
188226 if (!gattlib_has_valid_handler (handler )) {
189227 // We do not have (anymore) a callback, nothing to do
228+ g_rec_mutex_unlock (& m_gattlib_mutex );
190229 return ;
191230 }
192231
@@ -198,6 +237,8 @@ void gattlib_handler_dispatch_to_thread(struct gattlib_handler* handler, void (*
198237 }
199238#endif
200239
240+ g_rec_mutex_unlock (& m_gattlib_mutex );
241+
201242 // We create a thread to ensure the callback is not blocking the mainloop
202243 va_list args ;
203244 va_start (args , thread_args_allocator );
@@ -218,3 +259,10 @@ void gattlib_free_mem(void *ptr) {
218259 free (ptr );
219260 }
220261}
262+
263+ int gattlib_device_ref (gattlib_device_t * device ) {
264+ g_rec_mutex_lock (& m_gattlib_mutex );
265+ device -> reference_counter ++ ;
266+ g_rec_mutex_unlock (& m_gattlib_mutex );
267+ return GATTLIB_SUCCESS ;
268+ }
0 commit comments