Skip to contentSkip to conversation

Cancel pending phonebook request

PATCH In review Dmitriy Paliy 2 participants 5 messages Dec 9, 2010 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

v4 cover Cancel pending phonebook request

Hi,

Structure context_query changed to query_context in phonebook-ebook.c comparing to previous submission, and phonebook_req_cancel changed to phonebook_req_finalize.

More comments added to phonebook.h.

Br,
Dmitriy

#1 lore raw

PATCH v4 1/3 Unified ebook data structures

Query data structures are unified in phonebook-ebook.c to have a unique identifier for ebook async operation. Purpose of this modification is next commit where pending async operation can be cancelled.

---
 plugins/phonebook-ebook.c |   36 ++++++++++++++++--------------------
 1 files changed, 16 insertions(+), 20 deletions(-)
plugins/phonebook-ebook.c +16 -20
index 073ff33..80398ef 100644
@@ -45,13 +45,9 @@
 #define QUERY_NAME "(contains \"given_name\" \"%s\")"
 #define QUERY_PHONE "(contains \"phone\" \"%s\")"
 
-struct contacts_query {
+struct query_context {
 	const struct apparam_field *params;
-	phonebook_cb cb;
-	void *user_data;
-};
-
-struct cache_query {
+	phonebook_cb contacts_cb;
 	phonebook_entry_cb entry_cb;
 	phonebook_cache_ready_cb ready_cb;
 	void *user_data;
@@ -141,7 +137,7 @@ static char *evcard_to_string(EVCard *evcard, unsigned int format,
 static void ebookpull_cb(EBook *book, EBookStatus estatus, GList *contacts,
 							void *user_data)
 {
-	struct contacts_query *data = user_data;
+	struct query_context *data = user_data;
 	GString *string = g_string_new("");
 	unsigned int count = 0, maxcount;
 	GList *l;
@@ -182,7 +178,7 @@ static void ebookpull_cb(EBook *book, EBookStatus estatus, GList *contacts,
 
 
 done:
-	data->cb(string->str, string->len, count, 0, data->user_data);
+	data->contacts_cb(string->str, string->len, count, 0, data->user_data);
 
 	g_string_free(string, TRUE);
 	g_free(data);
@@ -191,14 +187,14 @@ done:
 static void ebook_entry_cb(EBook *book, EBookStatus estatus,
 			EContact *contact, void *user_data)
 {
-	struct contacts_query *data = user_data;
+	struct query_context *data = user_data;
 	EVCard *evcard;
 	char *vcard;
 	size_t len;
 
 	if (estatus != E_BOOK_ERROR_OK) {
 		error("E-Book query failed: status %d", estatus);
-		data->cb(NULL, 0, 1, 0, data->user_data);
+		data->contacts_cb(NULL, 0, 1, 0, data->user_data);
 		g_free(data);
 		return;
 	}
@@ -210,7 +206,7 @@ static void ebook_entry_cb(EBook *book, EBookStatus estatus,
 
 	len = vcard ? strlen(vcard) : 0;
 
-	data->cb(vcard, len, 1, 0, data->user_data);
+	data->contacts_cb(vcard, len, 1, 0, data->user_data);
 
 	g_free(vcard);
 	g_free(data);
@@ -249,7 +245,7 @@ static char *evcard_name_attribute_to_string(EVCard *evcard)
 static void cache_cb(EBook *book, EBookStatus estatus, GList *contacts,
 							void *user_data)
 {
-	struct cache_query *data = user_data;
+	struct query_context *data = user_data;
 	GList *l;
 
 	if (estatus != E_BOOK_ERROR_OK) {
@@ -411,13 +407,13 @@ done:
 int phonebook_pull(const char *name, const struct apparam_field *params,
 					phonebook_cb cb, void *user_data)
 {
-	struct contacts_query *data;
+	struct query_context *data;
 	EBookQuery *query;
 
 	query = e_book_query_any_field_contains("");
 
-	data = g_new0(struct contacts_query, 1);
-	data->cb = cb;
+	data = g_new0(struct query_context, 1);
+	data->contacts_cb = cb;
 	data->params = params;
 	data->user_data = user_data;
 
@@ -432,10 +428,10 @@ int phonebook_get_entry(const char *folder, const char *id,
 					const struct apparam_field *params,
 					phonebook_cb cb, void *user_data)
 {
-	struct contacts_query *data;
+	struct query_context *data;
 
-	data = g_new0(struct contacts_query, 1);
-	data->cb = cb;
+	data = g_new0(struct query_context, 1);
+	data->contacts_cb = cb;
 	data->params = params;
 	data->user_data = user_data;
 
@@ -450,7 +446,7 @@ int phonebook_get_entry(const char *folder, const char *id,
 int phonebook_create_cache(const char *name, phonebook_entry_cb entry_cb,
 			phonebook_cache_ready_cb ready_cb, void *user_data)
 {
-	struct cache_query *data;
+	struct query_context *data;
 	EBookQuery *query;
 	gboolean ret;
 
@@ -459,7 +455,7 @@ int phonebook_create_cache(const char *name, phonebook_entry_cb entry_cb,
 
 	query = e_book_query_any_field_contains("");
 
-	data = g_new0(struct cache_query, 1);
+	data = g_new0(struct query_context, 1);
 	data->entry_cb = entry_cb;
 	data->ready_cb = ready_cb;
 	data->user_data = user_data;
Signature · 1 line
1.7.0.4
#2 lore raw

PATCH v4 2/3 Add handing of backend pending request

In reply to Dmitriy Paliy

Add phonebook_req_finalize function prototype in phonebook.h that deallocates resources associated to asynchronous request, and cancels the request if it is not completed. It unrefs pointer to pending request if such is applicable.

It does the following, depending on phonebook backend:
- for phonebook-tracker.c backend driver, it cancels pending DBus call,
- for phonebook-dummy.c, it removes GSource with a given id,
- for phonebook-ebook.c, it cancels last, still running, asynchronous operation.

The following modifcations are donein the code. Phonebook request pointer is added in PBAP object and IRMC session data to provide reference for pending call. Phonebook callbacks are updated in PBAP and IRMC to unref request upon completion and clear pointers, if such is applicable.

PBAP and IRMC are updated to cancel pending request, if any, when OBEX object is closed.

Phonebook_pull, phonebook_get_entry, phonebook_create_cache functions are updated to return void pointer to backend specific request and error code. All backend drivers updated accordingly to the modified API, which are phonebook-tracker.c, phonebook-dummy.c, and phonebook-ebook.c, to be specific.

In phonebook-tracker.c, query_tracker is updated to return pending DBus call request. Allocated pending_reply and phonebook_data are destroyed after call is unrefed. For this purpose new query_free_data function is created.

Call is unrefed either if request succeeded or in
phonebook_req_finalize if terminated.

Such fix prevents obexd crash on unexpected OBEX session close. E.g., OBEX session is closed before reply comes from phonebook backend. In that case, both session and object are destroyed, and dereferencing of object in associated callback function causes crash.

---
 plugins/irmc.c              |   27 ++++++++---
 plugins/pbap.c              |   65 +++++++++++++++++++------
 plugins/phonebook-dummy.c   |   70 +++++++++++++++++++--------
 plugins/phonebook-ebook.c   |   96 +++++++++++++++++++++++++++++-------
 plugins/phonebook-tracker.c |  113 ++++++++++++++++++++++++++++++++----------
 plugins/phonebook.h         |   35 +++++++++++---
 6 files changed, 310 insertions(+), 96 deletions(-)
plugins/irmc.c +20 -7
index f7ad33b..a281341 100644
@@ -110,6 +110,7 @@ struct irmc_session {
 	char did[DID_LEN];
 	char manu[DID_LEN];
 	char model[DID_LEN];
+	void *request;
 };
 
 #define IRMC_TARGET_SIZE 9
@@ -139,6 +140,11 @@ static void phonebook_size_result(const char *buffer, size_t bufsize,
 	DBG("vcards %d", vcards);
 
 	irmc->params->maxlistcount = vcards;
+
+	if (irmc->request) {
+		phonebook_req_finalize(irmc->request);
+		irmc->request = NULL;
+	}
 }
 
 static void query_result(const char *buffer, size_t bufsize, int vcards,
@@ -149,6 +155,11 @@ static void query_result(const char *buffer, size_t bufsize, int vcards,
 
 	DBG("bufsize %zu vcards %d missed %d", bufsize, vcards, missed);
 
+	if (irmc->request) {
+		phonebook_req_finalize(irmc->request);
+		irmc->request = NULL;
+	}
+
 	/* first add a 'owner' vcard */
 	if (!irmc->buffer)
 		irmc->buffer = g_string_new(owner_vcard);
@@ -210,11 +221,8 @@ static void *irmc_connect(struct obex_session *os, int *err)
 	param->maxlistcount = 0; /* to count the number of vcards... */
 	param->filter = 0x200085; /* UID TEL N VERSION */
 	irmc->params = param;
-	phonebook_pull("telecom/pb.vcf", irmc->params, phonebook_size_result,
-									irmc);
-
-	if (err)
-		*err = 0;
+	irmc->request = phonebook_pull("telecom/pb.vcf", irmc->params,
+					phonebook_size_result, irmc, err);
 
 	return irmc;
 }
@@ -298,8 +306,8 @@ static void *irmc_open_pb(const char *name, struct irmc_session *irmc,
 
 	if (!g_strcmp0(name, ".vcf")) {
 		/* how can we tell if the vcard count call already finished? */
-		ret = phonebook_pull("telecom/pb.vcf", irmc->params,
-							query_result, irmc);
+		irmc->request = phonebook_pull("telecom/pb.vcf", irmc->params,
+						query_result, irmc, &ret);
 		if (ret < 0) {
 			DBG("phonebook_pull failed...");
 			goto fail;
@@ -435,6 +443,11 @@ static int irmc_close(void *object)
 		irmc->buffer = NULL;
 	}
 
+	if (irmc->request) {
+		phonebook_req_finalize(irmc->request);
+		irmc->request = NULL;
+	}
+
 	return 0;
 }
 
plugins/pbap.c +49 -16

158 lines not on this page · raw message

plugins/phonebook-dummy.c +49 -21

133 lines not on this page · raw message

plugins/phonebook-ebook.c +78 -18

180 lines not on this page · raw message

plugins/phonebook-tracker.c +86 -27

213 lines not on this page · raw message

plugins/phonebook.h +28 -7
index d7cfa46..bfbae0f 100644
@@ -85,25 +85,46 @@ char *phonebook_set_folder(const char *current_folder,
  * PullPhoneBook never use cached entries. PCE use this function to get all
  * entries of a given folder. The back-end MUST return only the content based
  * on the application parameters requested by the client.
+ *
+ * Return value is a pointer to asynchronous request to phonebook back-end.
+ * phonebook_req_finalize MUST always be used to free associated resources.
  */
-int phonebook_pull(const char *name, const struct apparam_field *params,
-					phonebook_cb cb, void *user_data);
+void *phonebook_pull(const char *name, const struct apparam_field *params,
+				phonebook_cb cb, void *user_data, int *err);
 
 /*
  * Function used to retrieve a contact from the backend. Only contacts
  * found in the cache are requested to the back-ends. The back-end MUST
  * return only the content based on the application parameters requested
  * by the client.
+ *
+ * Return value is a pointer to asynchronous request to phonebook back-end.
+ * phonebook_req_finalize MUST always be used to free associated resources.
  */
-int phonebook_get_entry(const char *folder, const char *id,
+void *phonebook_get_entry(const char *folder, const char *id,
 				const struct apparam_field *params,
-				phonebook_cb cb, void *user_data);
+				phonebook_cb cb, void *user_data, int *err);
 
 /*
  * PBAP core will keep the contacts cache per folder. SetPhoneBook or
  * PullvCardListing can invalidate the cache if the current folder changes.
  * Cache will store only the necessary information required to reply to
- * PullvCardListing request and verify if a given contact belongs to the source.
+ * PullvCardListing request and verify if a given contact belongs to the
+ * source.
+ *
+ * Return value is a pointer to asynchronous request to phonebook back-end.
+ * phonebook_req_finalize MUST always be used to free associated resources.
+ */
+void *phonebook_create_cache(const char *name, phonebook_entry_cb entry_cb,
+		phonebook_cache_ready_cb ready_cb, void *user_data, int *err);
+
+/*
+ * Finalizes request to phonebook back-end and deallocates associated
+ * resources. Operation is canceled if not completed. This function MUST
+ * always be used after any of phonebook_pull, phonebook_get_entry, and
+ * phonebook_create_cache invoked.
+ *
+ * request is a pointer to asynchronous operation returned by phonebook_pull,
+ * phonebook_get_entry, and phonebook_create_cache.
  */
-int phonebook_create_cache(const char *name, phonebook_entry_cb entry_cb,
-			phonebook_cache_ready_cb ready_cb, void *user_data);
+void phonebook_req_finalize(void *request);
Signature · 1 line
1.7.0.4
#3 lore raw

PATCH v4 3/3 Code clean-up: long, empty lines, indentation

In reply to Dmitriy Paliy

Lines longer 80 symbols and empty lines removed. Indentation in generate_response function corrected.

---
 plugins/pbap.c |   17 ++++++++---------
 1 files changed, 8 insertions(+), 9 deletions(-)
plugins/pbap.c +8 -9
index 820e35c..3ebfbe8 100644
@@ -273,8 +273,8 @@ static void query_result(const char *buffer, size_t bufsize, int vcards,
 	if (!pbap->obj->buffer)
 		pbap->obj->buffer = g_string_new_len(buffer, bufsize);
 	else
-		pbap->obj->buffer = g_string_append_len(pbap->obj->buffer, buffer,
-								bufsize);
+		pbap->obj->buffer = g_string_append_len(pbap->obj->buffer,
+							buffer,	bufsize);
 
 	obex_object_set_io_flags(pbap->obj, G_IO_IN, 0);
 }
@@ -418,9 +418,8 @@ static int generate_response(void *user_data)
 	 * only the reference for the "real" cache entry.
 	 */
 	sorted = sort_entries(pbap->cache.entries, pbap->params->order,
-			pbap->params->searchattrib,
-			(const char *) pbap->params->searchval);
-
+				pbap->params->searchattrib,
+				(const char *) pbap->params->searchval);
 	if (sorted == NULL)
 		return -ENOENT;
 
@@ -431,12 +430,12 @@ static int generate_response(void *user_data)
 	for (; l && max; l = l->next, max--) {
 		const struct cache_entry *entry = l->data;
 
-		g_string_append_printf(pbap->obj->buffer, VCARD_LISTING_ELEMENT,
-						entry->handle, entry->name);
+		g_string_append_printf(pbap->obj->buffer,
+			VCARD_LISTING_ELEMENT, entry->handle, entry->name);
 	}
 
-	pbap->obj->buffer = g_string_append(pbap->obj->buffer, VCARD_LISTING_END);
-
+	pbap->obj->buffer = g_string_append(pbap->obj->buffer,
+							VCARD_LISTING_END);
 	g_slist_free(sorted);
 
 	return 0;
Signature · 1 line
1.7.0.4
#4 lore raw
In reply to Dmitriy Paliy

Hi Dmitriy,

Quoted 5 lines · Dmitriy Paliy

Structure context_query changed to query_context in phonebook-ebook.c comparing to previous submission, and phonebook_req_cancel changed to phonebook_req_finalize.

More comments added to phonebook.h.

Thanks for the update. All three patches have been pushed upstream.

Johan