#define SETTINGS_FILE ANDROID_STORAGEDIR"/settings"
#define DEVICES_FILE ANDROID_STORAGEDIR"/devices"
+#define CACHE_FILE ANDROID_STORAGEDIR"/cache"
#define DEVICE_ID_SOURCE 0x0002 /* USB */
#define DEVICE_ID_VENDOR 0x1d6b /* Linux Foundation */
/* Default discoverable timeout 120sec as in Android */
#define DEFAULT_DISCOVERABLE_TIMEOUT 120
+#define DEVICES_CACHE_MAX 300
+
#define BASELEN_PROP_CHANGED (sizeof(struct hal_ev_adapter_props_changed) \
+ (sizeof(struct hal_property)))
g_key_file_free(key_file);
}
-static void store_device_info(struct device *dev)
+static void store_device_info(struct device *dev, const char *path)
{
GKeyFile *key_file;
char addr[18];
char **uuids = NULL;
char *str;
- /* We only store bonded devices and need to modify the storage
- * if the state is either NONE or BONDED.
- */
- if (dev->bond_state != HAL_BOND_STATE_BONDED &&
- dev->bond_state != HAL_BOND_STATE_NONE)
- return;
-
ba2str(&dev->bdaddr, addr);
key_file = g_key_file_new();
- g_key_file_load_from_file(key_file, DEVICES_FILE, 0, NULL);
-
- if (dev->bond_state == HAL_BOND_STATE_NONE) {
- g_key_file_remove_group(key_file, addr, NULL);
- goto done;
- }
+ g_key_file_load_from_file(key_file, path, 0, NULL);
g_key_file_set_integer(key_file, addr, "Type", dev->bdaddr_type);
g_key_file_remove_key(key_file, addr, "Services", NULL);
}
-done:
str = g_key_file_to_data(key_file, &length, NULL);
- g_file_set_contents(DEVICES_FILE, str, length, NULL);
+ g_file_set_contents(path, str, length, NULL);
g_free(str);
g_key_file_free(key_file);
g_strfreev(uuids);
}
+static void remove_device_info(struct device *dev, const char *path)
+{
+ GKeyFile *key_file;
+ gsize length = 0;
+ char addr[18];
+ char *str;
+
+ ba2str(&dev->bdaddr, addr);
+
+ key_file = g_key_file_new();
+ g_key_file_load_from_file(key_file, path, 0, NULL);
+
+ g_key_file_remove_group(key_file, addr, NULL);
+
+ str = g_key_file_to_data(key_file, &length, NULL);
+ g_file_set_contents(path, str, length, NULL);
+ g_free(str);
+
+ g_key_file_free(key_file);
+}
+
static int device_match(gconstpointer a, gconstpointer b)
{
const struct device *dev = a;
return NULL;
}
+static void free_device(struct device *dev)
+{
+ g_free(dev->name);
+ g_free(dev->friendly_name);
+ g_slist_free_full(dev->uuids, g_free);
+ g_free(dev);
+}
+
+static void cache_device(struct device *new_dev)
+{
+ struct device *dev;
+ GSList *l;
+
+ l = g_slist_find(devices, new_dev);
+ if (l) {
+ devices = g_slist_remove(devices, new_dev);
+ goto cache;
+ }
+
+ if (g_slist_length(devices) < DEVICES_CACHE_MAX)
+ goto cache;
+
+ l = g_slist_last(devices);
+ dev = l->data;
+
+ devices = g_slist_remove(devices, dev);
+ remove_device_info(dev, CACHE_FILE);
+ free_device(dev);
+
+cache:
+ devices = g_slist_prepend(devices, new_dev);
+ new_dev->timestamp = time(NULL);
+ store_device_info(new_dev, CACHE_FILE);
+}
+
static struct device *create_device(const bdaddr_t *bdaddr, uint8_t type)
{
struct device *dev;
/* use address for name, will be change if one is present
* eg. in EIR or set by set_property. */
dev->name = g_strdup(addr);
- devices = g_slist_prepend(devices, dev);
return dev;
}
-static void free_device(struct device *dev)
-{
- g_free(dev->name);
- g_free(dev->friendly_name);
- g_slist_free_full(dev->uuids, g_free);
- g_free(dev);
-}
-
static struct device *get_device(const bdaddr_t *bdaddr, uint8_t type)
{
struct device *dev;
if (dev)
return dev;
- return create_device(bdaddr, type);
+ dev = create_device(bdaddr, type);
+
+ cache_device(dev);
+
+ return dev;
}
static void send_adapter_property(uint8_t type, uint16_t len, const void *val)
case HAL_BOND_STATE_NONE:
if (dev->bond_state == HAL_BOND_STATE_BONDED) {
bonded_devices = g_slist_remove(bonded_devices, dev);
- devices = g_slist_prepend(devices, dev);
+ remove_device_info(dev, DEVICES_FILE);
+ cache_device(dev);
}
break;
case HAL_BOND_STATE_BONDED:
devices = g_slist_remove(devices, dev);
bonded_devices = g_slist_prepend(bonded_devices, dev);
+ remove_device_info(dev, CACHE_FILE);
+ store_device_info(dev, DEVICES_FILE);
break;
case HAL_BOND_STATE_BONDING:
default:
dev->bond_state = state;
- store_device_info(dev);
-
send_bond_state_change(&dev->bdaddr, status, state);
}
g_slist_free_full(dev->uuids, g_free);
dev->uuids = uuids;
- store_device_info(dev);
+ if (dev->bond_state == HAL_BOND_STATE_BONDED)
+ store_device_info(dev, DEVICES_FILE);
+ else
+ store_device_info(dev, CACHE_FILE);
send_device_uuids_notif(dev);
}
ev->status = HAL_STATUS_SUCCESS;
bdaddr2android(bdaddr, ev->bdaddr);
-
- dev->timestamp = time(NULL);
}
if (eir.class) {
(*num_prop)++;
}
+ if (dev->bond_state != HAL_BOND_STATE_BONDED)
+ cache_device(dev);
+
if (*num_prop)
ipc_send_notif(HAL_SERVICE_ID_BLUETOOTH, opcode, size, buf);
g_free(dev->friendly_name);
dev->friendly_name = g_strndup((const char *) val, len);
- store_device_info(dev);
+ if (dev->bond_state == HAL_BOND_STATE_BONDED)
+ store_device_info(dev, DEVICES_FILE);
+ else
+ store_device_info(dev, CACHE_FILE);
send_device_property(&dev->bdaddr, HAL_PROP_DEVICE_FRIENDLY_NAME,
strlen(dev->friendly_name), dev->friendly_name);