@@ -133,19 +133,22 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
133
133
134
134
// even after init_default_adapter() - the adapter can be NULL
135
135
if (gattlib_adapter == NULL ) {
136
+ GATTLIB_LOG (GATTLIB_DEBUG , "gattlib_connect: No adapter" );
136
137
return NULL ;
137
138
}
138
139
139
140
get_device_path_from_mac (adapter_name , dst , object_path , sizeof (object_path ));
140
141
141
142
gattlib_context_t * conn_context = calloc (sizeof (gattlib_context_t ), 1 );
142
143
if (conn_context == NULL ) {
144
+ GATTLIB_LOG (GATTLIB_DEBUG , "gattlib_connect: Cannot allocate context" );
143
145
return NULL ;
144
146
}
145
147
conn_context -> adapter = gattlib_adapter ;
146
148
147
149
gatt_connection_t * connection = calloc (sizeof (gatt_connection_t ), 1 );
148
150
if (connection == NULL ) {
151
+ GATTLIB_LOG (GATTLIB_DEBUG , "gattlib_connect: Cannot allocate connection" );
149
152
goto FREE_CONN_CONTEXT ;
150
153
} else {
151
154
connection -> context = conn_context ;
@@ -162,6 +165,8 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
162
165
if (error ) {
163
166
GATTLIB_LOG (GATTLIB_ERROR , "Failed to connect to DBus Bluez Device: %s" , error -> message );
164
167
g_error_free (error );
168
+ } else {
169
+ GATTLIB_LOG (GATTLIB_DEBUG , "gattlib_connect: Failed to connect to DBus Bluez Device" );
165
170
}
166
171
goto FREE_CONNECTION ;
167
172
} else {
@@ -206,6 +211,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
206
211
// Get list of objects belonging to Device Manager
207
212
device_manager = get_device_manager_from_adapter (conn_context -> adapter );
208
213
if (device_manager == NULL ) {
214
+ GATTLIB_LOG (GATTLIB_DEBUG , "gattlib_connect: Failed to get device manager from adapter" );
209
215
goto FREE_DEVICE ;
210
216
}
211
217
conn_context -> dbus_objects = g_dbus_object_manager_get_objects (device_manager );
@@ -227,8 +233,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
227
233
free (conn_context );
228
234
229
235
// destroy default adapter
230
- if (adapter == NULL )
231
- {
236
+ if (adapter == NULL ) {
232
237
gattlib_adapter_close (gattlib_adapter );
233
238
}
234
239
@@ -250,9 +255,21 @@ gatt_connection_t *gattlib_connect_async(void *adapter, const char *dst,
250
255
}
251
256
252
257
int gattlib_disconnect (gatt_connection_t * connection ) {
253
- gattlib_context_t * conn_context = connection -> context ;
258
+ gattlib_context_t * conn_context ;
254
259
GError * error = NULL ;
255
260
261
+ if (connection == NULL ) {
262
+ GATTLIB_LOG (GATTLIB_ERROR , "Cannot disconnect - connection parameter is not valid." );
263
+ return GATTLIB_INVALID_PARAMETER ;
264
+ }
265
+
266
+ conn_context = connection -> context ;
267
+
268
+ if (conn_context == NULL ) {
269
+ GATTLIB_LOG (GATTLIB_ERROR , "Cannot disconnect - connection context is not valid." );
270
+ return GATTLIB_NOT_SUPPORTED ;
271
+ }
272
+
256
273
org_bluez_device1_call_disconnect_sync (conn_context -> device , NULL , & error );
257
274
if (error ) {
258
275
GATTLIB_LOG (GATTLIB_ERROR , "Failed to disconnect DBus Bluez Device: %s" , error -> message );
@@ -266,7 +283,7 @@ int gattlib_disconnect(gatt_connection_t* connection) {
266
283
pthread_join (conn_context -> event_thread , NULL );
267
284
g_main_loop_unref (conn_context -> connection_loop );
268
285
disconnect_all_notifications (conn_context );
269
-
286
+
270
287
free (conn_context -> adapter -> adapter_name );
271
288
free (conn_context -> adapter );
272
289
@@ -790,7 +807,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
790
807
interface = g_dbus_object_manager_get_interface (device_manager , object_path , "org.bluez.Battery1" );
791
808
if (interface ) {
792
809
g_object_unref (interface );
793
-
810
+
794
811
characteristic_list [count ].handle = 0 ;
795
812
characteristic_list [count ].value_handle = 0 ;
796
813
characteristic_list [count ].properties = GATTLIB_CHARACTERISTIC_READ | GATTLIB_CHARACTERISTIC_NOTIFY ;
0 commit comments