diff options
author | Matt Ranostay <matt.ranostay@konsulko.com> | 2017-07-27 22:18:43 -0700 |
---|---|---|
committer | Matt Ranostay <matt.ranostay@konsulko.com> | 2017-08-04 15:31:03 -0700 |
commit | 4e226ba5ad81c09dc62cd1c4295bd20fbd1d7711 (patch) | |
tree | e74fd9b48180c2fb1031e9eda44f8a293885aa95 /binding-bluetooth/bluetooth-manager.c | |
parent | dabd2b715a9104dd2e990fddf06b06f0d4d7bd62 (diff) |
Store list of the order of devices paired to deduce priority of which
should be autoconnected to first.
Change-Id: I0db62e93dfae92a63cbd6f45515093344322f2ca
Bug-AGL: SPEC-722
Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'binding-bluetooth/bluetooth-manager.c')
-rw-r--r-- | binding-bluetooth/bluetooth-manager.c | 154 |
1 files changed, 140 insertions, 14 deletions
diff --git a/binding-bluetooth/bluetooth-manager.c b/binding-bluetooth/bluetooth-manager.c index b708439..407518f 100644 --- a/binding-bluetooth/bluetooth-manager.c +++ b/binding-bluetooth/bluetooth-manager.c @@ -68,6 +68,128 @@ void devices_list_unlock(void) g_mutex_unlock(&(BluetoothManage.m)); } +gchar *bt_priority_path() +{ + gchar *path = g_build_filename(g_get_user_config_dir(), "bluetooth", NULL); + gchar *file; + + g_mkdir(path, 0664); + + file = g_build_filename(path, "priorities.lst", NULL); + g_free(path); + + return file; +} + +void bt_remove_priority(gchar *bdaddr) +{ + LOGD("\n"); + FILE *fp; + GSList *item, *tmp; + gchar *path; + + LOGD("bd_addr: %s\n", bdaddr); + + devices_list_lock(); + + item = g_slist_find_custom(BluetoothManage.priorities, bdaddr, g_strcmp0); + if (item == NULL) + { + devices_list_unlock(); + return; + } + g_free(item->data); + tmp = BluetoothManage.priorities = g_slist_remove_link(BluetoothManage.priorities, item); + + path = bt_priority_path(); + fp = g_fopen(path, "w"); + g_free(path); + + if (fp == NULL) + { + devices_list_unlock(); + return; + } + + while (tmp && tmp->data) + { + fputs(tmp->data, fp); + fputs("\n", fp); + tmp = tmp->next; + } + + fclose(fp); + + devices_list_unlock(); +} + +void bt_save_priority(gchar *bdaddr) +{ + LOGD("\n"); + FILE *fp; + gchar *path; + + LOGD("bd_addr: %s\n", bdaddr); + + devices_list_lock(); + + if (g_slist_find(BluetoothManage.priorities, bdaddr)) + { + devices_list_unlock(); + return; + } + + path = bt_priority_path(); + fp = g_fopen(path, "a+"); + g_free(path); + + if (fp == NULL) + { + devices_list_unlock(); + return; + } + + fputs(bdaddr, fp); + fputs("\n", fp); + fclose(fp); + + BluetoothManage.priorities = g_slist_append(BluetoothManage.priorities, g_strdup(bdaddr)); + + devices_list_unlock(); +} + +GSList *bt_priority_list() +{ + LOGD("\n"); + FILE *fp; + GSList *list = NULL; + gchar *path; + gchar bdaddr[18]; + + devices_list_lock(); + + path = bt_priority_path(); + + fp = g_fopen(path, "r"); + g_free(path); + + if (fp == NULL) + { + devices_list_unlock(); + return NULL; + } + + while (fscanf(fp, "%17s\n", &bdaddr) == 1) + { + LOGD("Found priority device %s\n", bdaddr); + list = g_slist_append(list, g_strdup(bdaddr)); + } + + devices_list_unlock(); + + return list; +} + static int device_path_cmp(struct btd_device * device, const gchar* pPath ) { return !g_str_has_prefix (pPath, device->path); @@ -943,6 +1065,8 @@ static int bt_manager_app_init(void) AgentRegCallback.agent_RequestConfirmation = agent_requset_confirm; agent_API_register(&AgentRegCallback); + BluetoothManage.priorities = bt_priority_list(); + ret = BluezManagerInit(); if (0 != ret ) { @@ -1070,7 +1194,7 @@ gboolean bt_autoconnect(gpointer ptr) { LOGD("\n"); gboolean ret = TRUE; - GSList *list, *tmp, *addr_list = NULL; + GSList *list, *tmp; devices_list_lock(); @@ -1082,15 +1206,9 @@ gboolean bt_autoconnect(gpointer ptr) struct bt_device *BDdevice = tmp->data; tmp = tmp->next; - if (BDdevice->paired) - { - addr_list = g_slist_append(addr_list, g_strdup(BDdevice->bdaddr)); - } - if (BDdevice->connected) { g_slist_free_full(list, g_free); - g_slist_free_full(addr_list, g_free); devices_list_unlock(); return FALSE; } @@ -1099,13 +1217,13 @@ gboolean bt_autoconnect(gpointer ptr) g_slist_free_full(list, g_free); devices_list_unlock(); - if (addr_list == NULL) + tmp = BluetoothManage.priorities; + + if (tmp == NULL) { - return FALSE; + return TRUE; } - tmp = addr_list; - while (tmp) { LOGD("Autoconnect to %s\n", tmp->data); @@ -1118,8 +1236,6 @@ gboolean bt_autoconnect(gpointer ptr) } } - g_slist_free_full(addr_list, g_free); - return ret; } @@ -1459,6 +1575,8 @@ int adapter_remove_device(const gchar* bdaddr) g_variant_new("(o)", path), NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &error); + bt_remove_priority(bdaddr); + g_free(path); if (NULL == value) { @@ -1504,6 +1622,14 @@ void device_pair_done_cb(GDBusConnection *source_object, gpointer user_data) { LOGD("\n"); + gchar *bdaddr = user_data; + + if (g_task_had_error(G_TASK(res)) == FALSE) + { + bt_save_priority(bdaddr); + } + g_free(bdaddr); + g_dbus_connection_call_finish (source_object, res, NULL); } @@ -1559,7 +1685,7 @@ int device_pair(const gchar * bdaddr) path, DEVICE_INTERFACE, "Pair", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, - (GAsyncReadyCallback)device_pair_done_cb, NULL); + (GAsyncReadyCallback)device_pair_done_cb, g_strdup(bdaddr)); g_free(path); #endif |