3.8. Device Cache

Using inquiry to find Bluetooth devices around your host takes usually at least several seconds. To make accessing the remote Bluetooth equipment more efficient and simpler for the end user Affix implements a specific device cache. This is cache stores information about Bluetooth devices that are found around you during the device inquiry. Note that also devices that are not currently in the radio range are preserved in the device cache until it is flushed or they are explicitly removed.

The end users get the list of cached devices fast and it is also more convenient to refer to devices cache entry number that all the time write the whole Bluetooth address. A single device is presented in the following data structure.

typedef struct {
    int flags;
    int state; /* Flag indicating is the device in the range. */

    BD_ADDR bda; /* Address of the device */
    uint32_t cod;
    char name[248];

    __u8 key_type;
    __u8 link_key[16];
    __u8 pin_length;
    __u8 pin[16];
} btdev_struct;

The devices in the cache are stored as a linked list where each element contains one device in a structure described above. In addition to device entries cache structure contains the mount of the devices in the list, flag for locking and the file name where the cache will be stored etc. See the definition below.

typedef struct {
    slist_t *head;
    int count;
    char *file;
    int lock;
    time_t stamp;
} btdev_list;

In the following table are the functions that are needed to access device cache. All function definitions are in btcore.h file.

Table 3-24. Device cache API

FunctionPurpose
btdev_struct *btdev_cache_lookup(BD_ADDR *bda);Get device information of the device which address is given as a parameter. Returns btdev_struct or null if the address is not found in the cache.
int btdev_cache_del(btdev_struct *entry);Delete a device from the list of cached devices. Always returns zero. Device to remove is identified by a pointer to it's btdev_structure.
btdev_struct *btdev_cache_add(BD_ADDR *bda);Initialize a new btdev_structure and insert it to the device cache. Set the given address for the device. Returns a pointer to the new structure.
int btdev_cache_load(int locked);Load cache information from the disk. The file name is stored in btdev_list structure's file member (see above). The member is initialized in btdev_cache_init() function. If locked parameter is non zero the function leaves cache locked after it exits. On error a value below zero is returned. Returning zero indicates success.
int btdev_cache_reload(void);The same as btdev_cache_load, but leaves cache locked by default.
void btdev_cache_purge(void);Flush device cache. Note! Also the cache file on the disk is emptied.
int btdev_cache_save(void);Stores the whole device cache into a file on a disk. The file name is stored in btdev_list structure's file member (see above). Function returns zero on success and -1 on error.
void btdev_cache_free(void);Removes device cache and frees all memory allocated for it's structures.
int btdev_cache_init(void);Initializes device cache. The storage file's name is copied from global variable affix_cachefile that is defined in btcore.c.
void btdev_cache_retire(void);Retire cache by marking all devices out of the range. Set their state to DEVSTATE_GONE
void btdev_cache_print(int state);Print all the devices with a specific state in the cache to standard output. State is given as a parameter for the function. This way you can print first devices that are reachable in the radio range.
int btdev_cache_resolve(BD_ADDR *bda, int id);Resolve the device address according to the cache entry number. Address is stored in a structure pointed by bda. Memory allocation is left for the caller. Parameter id holds the cache index. Returns zero if resolving was OK or -1 on error.
int btdev_get_bda(BD_ADDR *bda, char *arg);Converts string presentation of a Bluetooth device address to binary form. Also seeks for the address from the device cache if a cache entry index was given instead of device address. Parameter bda is a pointer to structure where the resolved address is stored. Parameter arg is the address or the index number in a string form. Return zero if resolving the address was OK.
int btdev_cache_lock(void);Lock device cache.
void btdev_cache_unlock(void);Unlock device cache.
int btdev_cache_init(void)Initialize device cache. Make sure you have freed existing cache before calling this. This is done with btdev_cache_free() function.