device: Create the Device D-Bus interface only when the DEP link is up
authorSamuel Ortiz <sameo@linux.intel.com>
Sun, 10 Nov 2013 23:55:07 +0000 (00:55 +0100)
committerSamuel Ortiz <sameo@linux.intel.com>
Sun, 10 Nov 2013 23:55:07 +0000 (00:55 +0100)
In Initiator mode, this is when we get a DEP_LINK_UP event.
In target mode, DEP link is up as soon as we see a new device.
This fix avoids racy situations where a Device interface is available
although the DEP link is still not up.

src/adapter.c
src/device.c
src/near.h

index 1e1fd20..52f11a6 100644 (file)
@@ -618,6 +618,9 @@ int __near_adapter_set_dep_state(uint32_t idx, bool dep)
        } else {
                if (adapter->dep_timer > 0)
                        g_source_remove(adapter->dep_timer);
+
+               if (!__near_device_register_interface(adapter->device_link))
+                       return -ENODEV;
        }
 
        return 0;
@@ -759,6 +762,8 @@ static int adapter_add_device(struct near_adapter *adapter,
        struct near_device *device;
        int err;
 
+       DBG();
+
        device = __near_device_add(adapter->idx, target_idx, nfcid, nfcid_len);
        if (!device)
                return -ENODEV;
@@ -775,8 +780,12 @@ static int adapter_add_device(struct near_adapter *adapter,
 
        adapter->device_link = device;
 
-       if (adapter->dep_up)
+       if (adapter->dep_up) {
+               if (!__near_device_register_interface(device))
+                       return -ENODEV;
+
                return 0;
+       }
 
        err = __near_netlink_dep_link_up(adapter->idx, target_idx,
                                        NFC_COMM_ACTIVE, NFC_RF_INITIATOR);
index 9a3e48c..5e67443 100644 (file)
@@ -338,14 +338,17 @@ struct near_device *__near_device_add(uint32_t adapter_idx, uint32_t target_idx,
 
        g_hash_table_insert(device_hash, path, device);
 
+       return device;
+}
+
+bool __near_device_register_interface(struct near_device *device)
+{
        DBG("connection %p", connection);
 
-       g_dbus_register_interface(connection, device->path,
-                                       NFC_DEVICE_INTERFACE,
-                                       device_methods, NULL, NULL,
+       return g_dbus_register_interface(connection, device->path,
+                                               NFC_DEVICE_INTERFACE,
+                                               device_methods, NULL, NULL,
                                                        device, NULL);
-
-       return device;
 }
 
 int __near_device_listen(struct near_device *device, near_device_io_cb cb)
index 5d09b4b..66e34ee 100644 (file)
@@ -144,6 +144,7 @@ uint32_t __neard_device_get_idx(struct near_device *device);
 struct near_device *__near_device_add(uint32_t idx, uint32_t target_idx,
                                        uint8_t *nfcid, uint8_t nfcid_len);
 void __near_device_remove(struct near_device *device);
+bool __near_device_register_interface(struct near_device *device);
 int __near_device_listen(struct near_device *device, near_device_io_cb cb);
 int __near_device_push(struct near_device *device,
                        struct near_ndef_message *ndef, char *service_name,