target: Target hash table key should be the target path
authorSamuel Ortiz <sameo@linux.intel.com>
Sat, 2 Jul 2011 00:00:09 +0000 (02:00 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 21 Oct 2011 06:54:05 +0000 (23:54 -0700)
include/target.h
plugins/nfctype2.c
src/near.h
src/target.c

index 66ea225..de8cc2b 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <near/tag.h>
 
-struct near_tag *near_target_get_tag(uint32_t target_idx, size_t data_length);
+struct near_tag *near_target_add_tag(uint32_t adapter_idx, uint32_t target_idx,
+                                       size_t data_length);
 
 #endif
index aa69024..961c2ec 100644 (file)
@@ -174,7 +174,8 @@ static int meta_recv(uint8_t *resp, int length, void *data)
                goto out;
        }
 
-       tag = near_target_get_tag(cookie->target_idx, TAG_DATA_LENGTH(cc));
+       tag = near_target_add_tag(cookie->adapter_idx, cookie->target_idx,
+                                       TAG_DATA_LENGTH(cc));
        if (tag == NULL) {
                err = -ENOMEM;
                goto out;
index 32a382d..62b4cb0 100644 (file)
@@ -98,7 +98,7 @@ uint32_t __near_target_get_protocols(struct near_target *target);
 int __near_target_add(uint32_t adapter_idx, uint32_t target_idx,
                uint32_t protocols, enum near_target_type type,
                uint16_t sens_res, uint8_t sel_res);
-void __near_target_remove(uint32_t target_idx);
+void __near_target_remove(struct near_target *target);
 int __near_target_init(void);
 void __near_target_cleanup(void);
 
index ccaff88..d561ba8 100644 (file)
@@ -329,29 +329,29 @@ int __near_target_add(uint32_t adapter_idx, uint32_t target_idx,
                        uint16_t sens_res, uint8_t sel_res)
 {
        struct near_target *target;
+       char *path;
+
+       path = g_strdup_printf("%s/nfc%d/target%d", NFC_PATH,
+                                       adapter_idx, target_idx);
+
+       if (path == NULL)
+               return -ENOMEM;
 
-       if (g_hash_table_lookup(target_hash,
-                       GINT_TO_POINTER(target_idx)) != NULL)
+       if (g_hash_table_lookup(target_hash, path) != NULL)
                return -EEXIST;
 
        target = g_try_malloc0(sizeof(struct near_target));
        if (target == NULL)
                return -ENOMEM;
 
-       target->path = g_strdup_printf("%s/nfc%d/target%d", NFC_PATH,
-                                       adapter_idx, target_idx);
-       if (target->path == NULL) {
-               g_free(target);
-               return -ENOMEM;
-       }
-
+       target->path = path;
        target->idx = target_idx;
        target->adapter_idx = adapter_idx;
        target->protocols = protocols;
        target->type = type;
        find_tag_type(target, sens_res, sel_res);
 
-       g_hash_table_insert(target_hash, GINT_TO_POINTER(target_idx), target);
+       g_hash_table_insert(target_hash, path, target);
 
        DBG("connection %p", connection);
 
@@ -363,12 +363,9 @@ int __near_target_add(uint32_t adapter_idx, uint32_t target_idx,
        return __near_adapter_add_target(adapter_idx, target);
 }
 
-void __near_target_remove(uint32_t target_idx)
+void __near_target_remove(struct near_target *target)
 {
-       struct near_target *target;
-
-       target = g_hash_table_lookup(target_hash, GINT_TO_POINTER(target_idx));
-       if (target == NULL)
+       if (g_hash_table_lookup(target_hash, target->path) == NULL)
                return;
 
        __near_adapter_remove_target(target->adapter_idx, target);
@@ -376,14 +373,22 @@ void __near_target_remove(uint32_t target_idx)
        g_dbus_unregister_interface(connection, target->path,
                                                NFC_TARGET_INTERFACE);
 
-       g_hash_table_remove(target_hash, GINT_TO_POINTER(target_idx));
+       g_hash_table_remove(target_hash, target->path);
 }
 
-struct near_tag *near_target_get_tag(uint32_t target_idx, size_t data_length)
+struct near_tag *near_target_add_tag(uint32_t adapter_idx, uint32_t target_idx,
+                                               size_t data_length)
 {
        struct near_target *target;
+       char *path;
+
+       path = g_strdup_printf("%s/nfc%d/target%d", NFC_PATH,
+                                       adapter_idx, target_idx);
+       if (path == NULL)
+               return NULL;
 
-       target = g_hash_table_lookup(target_hash, GINT_TO_POINTER(target_idx));
+       target = g_hash_table_lookup(target_hash, path);
+       g_free(path);
        if (target == NULL)
                return NULL;
 
@@ -404,8 +409,8 @@ int __near_target_init(void)
 
        connection = near_dbus_get_connection();
 
-       target_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
-                                                       NULL, free_target);
+       target_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                               g_free, free_target);
 
        return 0;
 }