From c69f4875cf165c9cd71d6c6531b04bc6f4eb230b Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Tue, 9 Apr 2019 18:45:18 -0700 Subject: binding: bluetooth-pbap: use persistence binding for caching Caching of contacts moved into storing in the persistence binding Bug-AGL: SPEC-2311 Change-Id: Ie9fa7b3e01f84f17101b211204575fc77bdc9a67 Signed-off-by: Matt Ranostay --- binding/bluetooth-pbap-binding.c | 82 ++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 24 deletions(-) (limited to 'binding') diff --git a/binding/bluetooth-pbap-binding.c b/binding/bluetooth-pbap-binding.c index a45e9ec..1493c16 100644 --- a/binding/bluetooth-pbap-binding.c +++ b/binding/bluetooth-pbap-binding.c @@ -16,6 +16,7 @@ */ #define _GNU_SOURCE +#include #include #include #include @@ -39,7 +40,6 @@ static OrgBluezObexClient1 *client; static OrgBluezObexSession1 *session; static OrgBluezObexPhonebookAccess1 *phonebook; static GHashTable *xfer_queue; -static GMutex cached_mutex; static GMutex xfer_queue_mutex; static GHashTable *xfer_complete; static GMutex xfer_complete_mutex; @@ -48,8 +48,6 @@ static GMutex connected_mutex; static gboolean connected = FALSE; static afb_event_t status_event; -json_object *cached_results = NULL; - #define PBAP_UUID "0000112f-0000-1000-8000-00805f9b34fb" #define INTERNAL "int" @@ -63,6 +61,56 @@ json_object *cached_results = NULL; #define OUTGOING "och" #define MISSED "mch" + +static int update_or_insert(const char *key, const char *value) +{ + json_object *query = json_object_new_object(); + int ret; + + json_object_object_add(query, "key", json_object_new_string(key)); + json_object_object_add(query, "value", json_object_new_string(value)); + json_object_get(query); + + ret = afb_service_call_sync("persistence", "update", query, NULL, NULL, NULL); + if (!ret) { + AFB_DEBUG("Updating persistence value '%s'", key); + json_object_put(query); + return 0; + } + + ret = afb_service_call_sync("persistence", "write", query, NULL, NULL, NULL); + if (!ret) { + AFB_DEBUG("Create persistence value '%s'", key); + return 0; + } + + return ret; +} + +static int read_cached_value(const char *key, const char **data) +{ + json_object *response, *query; + int ret; + + query = json_object_new_object(); + json_object_object_add(query, "key", json_object_new_string(key)); + + ret = afb_service_call_sync("persistence", "read", query, &response, NULL, NULL); + if (!ret) { + json_object *val = NULL; + + json_object_object_get_ex(response, "value", &val); + if (!val) + return -EINVAL; + + *data = g_strdup(json_object_get_string(val)); + } + + json_object_get(response); + + return ret; +} + static void on_interface_proxy_properties_changed( GDBusObjectManagerClient *manager, GDBusObjectProxy *object_proxy, @@ -307,6 +355,7 @@ static gboolean parse_max_entries_parameter(afb_req_t request, int *max_entries) void contacts(afb_req_t request) { struct json_object *jresp; + const char *cached = NULL; int max_entries = -1; if (!connected) { @@ -317,11 +366,8 @@ void contacts(afb_req_t request) if (!parse_max_entries_parameter(request, &max_entries)) return; - g_mutex_lock(&cached_mutex); - - if (max_entries == -1 && cached_results) { - jresp = cached_results; - json_object_get(jresp); + if (max_entries == -1 && !read_cached_value("default", &cached)) { + jresp = json_tokener_parse(cached); } else { org_bluez_obex_phonebook_access1_call_select_sync( phonebook, INTERNAL, CONTACTS, NULL, NULL); @@ -329,8 +375,6 @@ void contacts(afb_req_t request) } afb_req_success(request, jresp, "contacts"); - - g_mutex_unlock(&cached_mutex); } void entry(afb_req_t request) @@ -615,16 +659,12 @@ static gboolean is_pbap_dev_and_init(struct json_object *dev) json_object_object_get_ex(dev, "device", &val1); AFB_NOTICE("PBAP device connected: %s", json_object_get_string(val1)); - g_mutex_lock(&cached_mutex); - - if (cached_results) - json_object_put(cached_results); - /* probably should be made async */ org_bluez_obex_phonebook_access1_call_select_sync( phonebook, INTERNAL, CONTACTS, NULL, NULL); - cached_results = get_vcards(-1); - g_mutex_unlock(&cached_mutex); + update_or_insert("default", + json_object_to_json_string_ext(get_vcards(-1), + JSON_C_TO_STRING_PLAIN)); return TRUE; } @@ -752,12 +792,6 @@ static void process_connection_event(afb_api_t api, struct json_object *object) afb_event_push(status_event, jresp); - g_mutex_lock(&cached_mutex); - if (cached_results) - json_object_put(cached_results); - cached_results = NULL; - g_mutex_unlock(&cached_mutex); - AFB_NOTICE("PBAP device disconnected: %s", device); } @@ -765,7 +799,7 @@ static void onevent(afb_api_t api, const char *event, struct json_object *object { if (!g_ascii_strcasecmp(event, "Bluetooth-Manager/device_changes")) process_connection_event(api, object); - else + else AFB_ERROR("Unsupported event: %s\n", event); } -- cgit 1.2.3-korg