How can I start and use the D-Bus service?

Created by Alexander Knaub
Keywords:
usage D-Bus dbus instructions start run service pasori readerwriter
Last updated by:
Alexander Knaub

After you downloaded the source code and configured your system like described in https://answers.launchpad.net/nfcpy/+faq/1508 , you make your PaSoRi available via D-Bus by following this steps:

1. cd into the downloaded dbus_examples folder
2. Start the D-Bus service:

  ./dbus_nfc_service.py

  You can skip this step if you configure the service "org.nfcpy.NFC" to start automatically (For instuction see https://answers.launchpad.net/nfcpy/+faq/1511)

3. For every connected PaSoRi that you want to be connected, start the ReaderWriter object in another shell/tab:

  sudo ./dbus_readerwriter.py

Now the PaSori is available via D-Bus and can be used by your application.
Note: This reserves the PaSori exclusively for D-Bus applications (1 at a time instance) and makes it unavailable for direct use via NFCpy python code until the dbus_readerwriter.py module is exited, e.g. by pressing Ctrl+C

Note: The files dbus_nfc_service.py and dbus_readerwriter.py have to be marked as an executable to be started in this way. If they aren't, you have to start them in the way "python dbus_nfc_service.py"

4.1. To use NFC in a user app, connect to D-Bus service (examples are in python, you can use your preferred language's D-Bus binding):

  sys_bus = dbus.SystemBus()
  service = sys_bus.get_object("org.nfcpy.NFC", "/")
  (connection, path) = service.GetReader()
  reader = sys_bus.get_object(connection, path)

  Now you can use methods and signals that are exposed to D-Bus by the ReaderWriter object referenced by reader

4.2. After using the reader, you should release it. If you don't, this reader will not be available for next user application. To release, simply call:

  service.ReleaseReader(connection)

Note: This example code demonstrates only the usage and does not include possibly necessary operations with the mainloop or threading initialisation.

Note: When your user application releases the used ReaderWriter object, it is still running and connected to the D-Bus. So, next application will be able to connect to this ReaderWriter.
If you want to shut down any particular ReaderWriter object, i.e. disconnect corresponding PaSoRi from D-Bus and make it available to be used by applications that use NFCpy directly instead of over D-Bus, press Ctrl+C in the shell where the corresponding dbus_readerwriter.py program is running.

To exit the entire D-Bus service including all ReaderWriter objects connected to D-Bus, call the D-Bus method Exit() on the Service object.
You can do this in the interactive python shell (type "python" to start it):

>>> import dbus
>>> dbus.SystemBus().get_object("org.nfcpy.NFC", "/").Exit()

or you use dbus-send in the linux shell:
dbus-send --system --dest="org.nfcpy.NFC" --type=method_call --print-reply "/" org.nfcpy.NFC.Exit