device: ObjectManager conversion
authorSamuel Ortiz <sameo@linux.intel.com>
Wed, 23 Oct 2013 01:00:09 +0000 (03:00 +0200)
committerSamuel Ortiz <sameo@linux.intel.com>
Sun, 10 Nov 2013 16:57:09 +0000 (17:57 +0100)
No need to export the Record property. GetProperties and SetProperties
are also removed.
We also centralize all device related test scripts into test-device, a
la BlueZ.

Makefile.am
doc/device-api.txt
src/device.c
test/dump-device [deleted file]
test/neardutils.py
test/push-device [deleted file]

index 4186069..739f5f3 100644 (file)
@@ -78,10 +78,10 @@ EXTRA_DIST = src/genbuiltin $(doc_files)
 
 dist_man_MANS = doc/neard.8 doc/neard.conf.5 doc/nfctool.1
 
-test_scripts = test/test-adapter \
-               test/dump-device test/dump-tag test/dump-record \
+test_scripts = test/test-adapter test/test-device \
+               test/dump-tag test/dump-record \
                test/monitor-near test/write-tag \
-               test/push-device test/bt-handover test/handover-agent \
+               test/bt-handover test/handover-agent \
                test/phdc-simple-manager test/neardutils.py
 
 if TEST
index 3430bf0..0852368 100644 (file)
@@ -5,14 +5,7 @@ Service                org.neard
 Interface      org.neard.Device
 Object path    [variable prefix]/{nfc0}/{device0, device1...}
 
-Method         dict GetProperties()
-
-                       Returns all properties for the device. See the
-                       properties section for available properties.
-
-                       Possible Errors: org.neard.Error.DoesNotExist
-
-               void Push(dict attributes)
+Method         void Push(dict attributes)
 
                        Creates an NDEF record from the attributes dictionary.
 
@@ -40,13 +33,3 @@ Method               dict GetProperties()
                                         org.neard.Error.InvalidArguments
                                         org.neard.Error.InProgress
 
-
-Signals                PropertyChanged(string name, variant value)
-
-                       This signal indicates a changed value of the given
-                       property.
-
-
-Properties     array{object} Records [readonly]
-
-                       List of NDEF records object paths.
index cac1ed3..dba0efe 100644 (file)
@@ -101,59 +101,6 @@ uint32_t __neard_device_get_idx(struct near_device *device)
        return device->target_idx;
 }
 
-static void append_records(DBusMessageIter *iter, void *user_data)
-{
-       struct near_device *device = user_data;
-       GList *list;
-
-       DBG("");
-
-       for (list = device->records; list; list = list->next) {
-               struct near_ndef_record *record = list->data;
-               char *path;
-
-               path = __near_ndef_record_get_path(record);
-               if (!path)
-                       continue;
-
-               dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
-                                                       &path);
-       }
-}
-
-static DBusMessage *get_properties(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
-{
-       struct near_device *device = data;
-       DBusMessage *reply;
-       DBusMessageIter array, dict;
-
-       DBG("conn %p", conn);
-
-       reply = dbus_message_new_method_return(msg);
-       if (!reply)
-               return NULL;
-
-       dbus_message_iter_init_append(reply, &array);
-
-       near_dbus_dict_open(&array, &dict);
-
-       near_dbus_dict_append_array(&dict, "Records",
-                               DBUS_TYPE_OBJECT_PATH, append_records, device);
-
-       near_dbus_dict_close(&array, &dict);
-
-       return reply;
-}
-
-static DBusMessage *set_property(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
-{
-       DBG("conn %p", conn);
-
-       return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
-}
-
 static void push_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
 {
        struct near_device *device;
@@ -276,23 +223,11 @@ error:
 }
 
 static const GDBusMethodTable device_methods[] = {
-       { GDBUS_METHOD("GetProperties",
-                               NULL, GDBUS_ARGS({"properties", "a{sv}"}),
-                               get_properties) },
-       { GDBUS_METHOD("SetProperty",
-                               GDBUS_ARGS({"name", "s"}, {"value", "v"}),
-                               NULL, set_property) },
        { GDBUS_ASYNC_METHOD("Push", GDBUS_ARGS({"attributes", "a{sv}"}),
                                                        NULL, push_ndef) },
        { },
 };
 
-static const GDBusSignalTable device_signals[] = {
-       { GDBUS_SIGNAL("PropertyChanged",
-                               GDBUS_ARGS({"name", "s"}, {"value", "v"})) },
-       { }
-};
-
 void __near_device_remove(struct near_device *device)
 {
        char *path = device->path;
@@ -361,11 +296,6 @@ int near_device_add_records(struct near_device *device, GList *records,
 
        __near_agent_ndef_parse_records(device->records);
 
-       near_dbus_property_changed_array(device->path,
-                                       NFC_DEVICE_INTERFACE, "Records",
-                                       DBUS_TYPE_OBJECT_PATH, append_records,
-                                       device);
-
        if (cb)
                cb(device->adapter_idx, device->target_idx, status);
 
@@ -415,8 +345,8 @@ struct near_device *__near_device_add(uint32_t adapter_idx, uint32_t target_idx,
 
        g_dbus_register_interface(connection, device->path,
                                        NFC_DEVICE_INTERFACE,
-                                       device_methods, device_signals,
-                                                       NULL, device, NULL);
+                                       device_methods, NULL, NULL,
+                                                       device, NULL);
 
        return device;
 }
diff --git a/test/dump-device b/test/dump-device
deleted file mode 100755 (executable)
index 6eead93..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/usr/bin/python
-
-import sys
-import dbus
-
-if len(sys.argv) < 2:
-       print "Usage: %s /org/neard/nfcX" % (sys.argv[0])
-       sys.exit(1)
-
-bus = dbus.SystemBus()
-
-def extract_record(key, list):
-       for i in list:
-               record = dbus.Interface(bus.get_object("org.neard", i),
-                                               "org.neard.Record")
-
-               properties = record.GetProperties()
-               print "        Record = [ %s ]" % (str(i))
-
-               for key in properties.keys():
-                       val = str(properties[key])
-                       print "              %s = %s" % (key, val)
-
-device = dbus.Interface(bus.get_object("org.neard", sys.argv[1]),
-                                               "org.neard.Device")
-
-properties = device.GetProperties()
-
-print "[ %s ]" % (sys.argv[1])
-
-for key in properties.keys():
-    if key in ["Records"]:
-        extract_record(key, properties[key])
-
index 770fb45..a7b2946 100644 (file)
@@ -2,6 +2,8 @@ import dbus
 
 SERVICE_NAME = "org.neard"
 ADAPTER_INTERFACE = SERVICE_NAME + ".Adapter"
+DEVICE_INTERFACE = SERVICE_NAME + ".Device"
+RECORD_INTERFACE = SERVICE_NAME + ".Record"
 
 def get_managed_objects():
        bus = dbus.SystemBus()
@@ -22,3 +24,31 @@ def find_adapter_in_objects(objects, pattern=None):
                        obj = bus.get_object(SERVICE_NAME, path)
                        return dbus.Interface(obj, ADAPTER_INTERFACE)
        raise Exception("NFC adapter not found")
+
+def find_device(pattern=None):
+       return find_device_in_objects(get_managed_objects(), pattern)
+
+def find_device_in_objects(objects, pattern=None):
+       bus = dbus.SystemBus()
+       for path, ifaces in objects.iteritems():
+               device = ifaces.get(DEVICE_INTERFACE)
+               if device is None:
+                       continue
+               if not pattern or path.endswith(pattern):
+                       obj = bus.get_object(SERVICE_NAME, path)
+                       return dbus.Interface(obj, DEVICE_INTERFACE)
+       raise Exception("NFC adapter not found")
+
+def find_record(pattern=None):
+       return find_record_in_objects(get_managed_objects(), pattern)
+
+def find_record_in_objects(objects, pattern=None):
+       bus = dbus.SystemBus()
+       for path, ifaces in objects.iteritems():
+               record = ifaces.get(RECORD_INTERFACE)
+               if record is None:
+                       continue
+               if not pattern or path.endswith(pattern):
+                       obj = bus.get_object(SERVICE_NAME, path)
+                       return dbus.Interface(obj, RECORD_INTERFACE)
+       raise Exception("NFC adapter not found")
diff --git a/test/push-device b/test/push-device
deleted file mode 100755 (executable)
index 1354e6c..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/usr/bin/python
-
-import sys
-import dbus
-
-def help_text():
-       print "Usage: %s <device-path> <record-type> <...>" % (sys.argv[0])
-       print "         If type is Text, parameters are <encoding> <language> <representation>"
-       print "         If type is URI, parameters are <uri>"
-       print "         If type is SmartPoster, parameters are <uri>"
-       print "         If type is Handover, parameters are <carrier>"
-       print "         If type is StaticHandover, parameters are <carrier>"
-       print "         If type is MIME, parameters are <mime_type> (only wifi_wsc and raw)"
-       print "                    raw is for sending raw payload, parameters are <mime_type> <payload>"
-       print "e.g. < %s /org/neard/nfc0/device0 Text UTF-8 en-US hello,Type2! >" % (sys.argv[0])
-       print "e.g. < %s /org/neard/nfc0/device0 URI http://www.nfc-forum.com >" % (sys.argv[0])
-       print "e.g. < %s /org/neard/nfc0/device0 SmartPoster http://www.nfc-forum.com >" % (sys.argv[0])
-       print "e.g. < %s /org/neard/nfc0/device0 Handover bluetooth,wifi >" % (sys.argv[0])
-       print "e.g. < %s /org/neard/nfc0/device0 StaticHandover bluetooth,wifi >" % (sys.argv[0])
-       print "e.g. < %s /org/neard/nfc0/device0 MIME wifi_wsc>" % (sys.argv[0])
-       print "e.g. < %s /org/neard/nfc0/device0 MIME raw application/xml '<your><xml tags></your>' >" % (sys.argv[0])
-       sys.exit(1)
-
-if len(sys.argv) < 2:
-       help_text()
-
-bus = dbus.SystemBus()
-
-device = dbus.Interface(bus.get_object("org.neard", sys.argv[1]),
-                                               "org.neard.Device")
-
-if len(sys.argv) == 6:
-       if sys.argv[2] in ["Text"]:
-               device.Push(({ "Type" : "Text",
-                               "Encoding" : sys.argv[3],
-                               "Language" : sys.argv[4],
-                               "Representation" : sys.argv[5] }))
-       elif sys.argv[2] in ["MIME"]:
-               if sys.argv[3] in ["raw"]:
-                       device.Push(({ "Type" : "MIME",
-                                      "MIME" : sys.argv[4],
-                                      "Payload" : dbus.ByteArray(sys.argv[5]) }))
-               else:
-                       help_text()
-       else:
-               help_text()
-
-elif len(sys.argv) == 4:
-       if sys.argv[2] in ["URI"]:
-               device.Push(({ "Type" : "URI",
-                               "URI" : sys.argv[3] }))
-       elif sys.argv[2] in ["SmartPoster"]:
-               device.Push(({ "Type" : "SmartPoster",
-                               "URI" : sys.argv[3] }))
-       elif sys.argv[2] in ["Handover"]:
-               device.Push(({ "Type" : "Handover",
-                               "Carrier" : sys.argv[3] }))
-       elif sys.argv[2] in ["StaticHandover"]:
-               device.Push(({ "Type" : "StaticHandover",
-                               "Carrier" : sys.argv[3] }))
-       elif sys.argv[2] in ["MIME"]:
-               if sys.argv[3] in ["wifi_wsc"]:
-                       device.Push(({ "Type" : "MIME",
-                               "MIME" : "application/vnd.wfa.wsc"}))
-               else:
-                       help_text()
-       else:
-               help_text()
-else:
-       help_text()