summaryrefslogtreecommitdiffstats
path: root/hcid/storage.c
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2005-04-17 00:53:32 +0000
committerMarcel Holtmann <marcel@holtmann.org>2005-04-17 00:53:32 +0000
commit04e3dafbeb0b78f247140314de18c9099a1b2bae (patch)
treea0c50dbdd68a6efb5fe5b4c359b59cdb7a63f087 /hcid/storage.c
parent18ebe398a14f4f26025db652c312dadeec8fa555 (diff)
Use cached device name for PIN request
Diffstat (limited to 'hcid/storage.c')
-rw-r--r--hcid/storage.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/hcid/storage.c b/hcid/storage.c
index da34b416..31f7eb62 100644
--- a/hcid/storage.c
+++ b/hcid/storage.c
@@ -242,6 +242,67 @@ close:
return err;
}
+int read_device_name(const bdaddr_t *local, const bdaddr_t *peer, char *name)
+{
+ char filename[PATH_MAX + 1], addr[18], str[249], *buf, *ptr;
+ bdaddr_t bdaddr;
+ struct stat st;
+ int fd, pos, err = -ENOENT;
+
+ ba2str(local, addr);
+ snprintf(filename, PATH_MAX, "%s/%s/names", DEVPATH, addr);
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return -errno;
+
+ if (flock(fd, LOCK_SH) < 0) {
+ err = -errno;
+ goto close;
+ }
+
+ if (fstat(fd, &st) < 0) {
+ err = -errno;
+ goto unlock;
+ }
+
+ buf = malloc(st.st_size);
+ if (!buf) {
+ err = -ENOMEM;
+ goto unlock;
+ }
+
+ if (st.st_size > 0) {
+ read(fd, buf, st.st_size);
+
+ ptr = buf;
+
+ memset(str, 0, sizeof(str));
+ while (sscanf(ptr, "%17s %[^\n]\n%n", addr, str, &pos) != EOF) {
+ str2ba(addr, &bdaddr);
+ str[sizeof(str) - 1] = '\0';
+
+ if (!bacmp(&bdaddr, peer)) {
+ snprintf(name, 249, "%s", str);
+ err = 0;
+ break;
+ }
+
+ memset(str, 0, sizeof(str));
+ ptr += pos;
+ if (ptr - buf >= st.st_size)
+ break;
+ };
+ }
+
+unlock:
+ flock(fd, LOCK_UN);
+
+close:
+ close(fd);
+ return err;
+}
+
int write_link_key(const bdaddr_t *local, const bdaddr_t *peer, const unsigned char *key, const int type)
{
struct list *temp, *list = NULL;