dbus-cxx logo

hal_proxy.cpp

This is an example of an ObjectProxy generated by dbus-cxx-xml2cpp from a modified dbus introspection XML document.

dbus-cxx-xml2cpp
The command used to generate hal_manager_proxy.h from hal_manager_introspection.xml is:
 dbus-cxx-xml2cpp --xml hal_manager_introspection.xml --proxy -f --prefix="hal_"
Modifying the DBus introspected XML
dbus-cxx-xml2cpp uses a modified XML document obtained via introspection. In this example several attributes were added to the node tag and the introspection interface tag.
The node tag was changed from this:
 <node>
to this:
 <node gen-namespace="DBus::Hal" cppname="Manager" dest="org.freedesktop.Hal" path="/org/freedesktop/Hal/Manager" >
The introspection interface tag had the ignored attribute added to prevent the interface from being built changing it from this:
 <interface name="org.freedesktop.DBus.Introspectable">
to this:
 <interface name="org.freedesktop.DBus.Introspectable" ignored="1">
Source xml
Here is the modified xml document:
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node gen-namespace="DBus::Hal" cppname="Manager" dest="org.freedesktop.Hal" path="/org/freedesktop/Hal/Manager" >
  <interface name="org.freedesktop.DBus.Introspectable" ignored="1">
    <method name="Introspect">
      <arg name="data" direction="out" type="s"/>
    </method>
  </interface>
  <interface name="org.freedesktop.Hal.Manager">
    <method name="GetAllDevices">
      <arg name="devices" direction="out" type="as"/>
    </method>
    <method name="GetAllDevicesWithProperties">
      <arg name="devices_with_props" direction="out" type="a(sa{sv})"/>
    </method>
    <method name="DeviceExists">
      <arg name="does_it_exist" direction="out" type="b"/>
      <arg name="udi" direction="in" type="s"/>
    </method>
    <method name="FindDeviceStringMatch">
      <arg name="devices" direction="out" type="as"/>
      <arg name="key" direction="in" type="s"/>
      <arg name="value" direction="in" type="s"/>
    </method>
    <method name="FindDeviceByCapability">
      <arg name="devices" direction="out" type="as"/>
      <arg name="capability" direction="in" type="s"/>
    </method>
    <method name="NewDevice">
      <arg name="temporary_udi" direction="out" type="s"/>
    </method>
    <method name="Remove">
      <arg name="udi" direction="in" type="s"/>
    </method>
    <method name="CommitToGdl">
      <arg name="temporary_udi" direction="in" type="s"/>
      <arg name="global_udi" direction="in" type="s"/>
    </method>
    <method name="AcquireGlobalInterfaceLock">
      <arg name="interface_name" direction="in" type="s"/>
      <arg name="exclusive" direction="in" type="b"/>
    </method>
    <method name="ReleaseGlobalInterfaceLock">
      <arg name="interface_name" direction="in" type="s"/>
    </method>
    <method name="SingletonAddonIsReady">
      <arg name="command_line" direction="in" type="s"/>
    </method>
    <signal name="DeviceAdded">
      <arg name="udi" type="s"/>
    </signal>
    <signal name="DeviceRemoved">
      <arg name="udi" type="s"/>
    </signal>
    <signal name="NewCapability">
      <arg name="udi" type="s"/>
      <arg name="cap_name" type="s"/>
    </signal>
    <signal name="GlobalInterfaceLockAcquired">
      <arg name="interface_name" type="s"/>
      <arg name="lock_holder" type="s"/>
      <arg name="num_locks" type="i"/>
    </signal>
    <signal name="GlobalInterfaceLockReleased">
      <arg name="interface_name" type="s"/>
      <arg name="lock_holder" type="s"/>
      <arg name="num_locks" type="i"/>
    </signal>
  </interface>
</node>

Generated Proxy Object
After running dbus-cxx-xml2cpp results in a .h file containing the definition of the proxy class. Here is the resulting hal_manager_proxy.h:
#ifndef __DBUS_PROXY_DBUS_HAL_MANAGER_H
#define __DBUS_PROXY_DBUS_HAL_MANAGER_H

#include <dbus-cxx.h>


namespace DBus {
  namespace Hal {

    class ManagerProxy : public ::DBus::ObjectProxy
    {
      protected:
        ManagerProxy( ::DBus::Connection::pointer conn, const std::string& dest="org.freedesktop.Hal", const std::string& path="/org/freedesktop/Hal/Manager"):
          ::DBus::ObjectProxy(conn, dest, path)
        {
          ::DBus::Path child_path;

          m_method_DeviceExists_bs = this->create_method< bool,std::string >( "org.freedesktop.Hal.Manager", "DeviceExists" );
          m_method_NewDevice_s = this->create_method< std::string >( "org.freedesktop.Hal.Manager", "NewDevice" );
          m_method_Remove_vs = this->create_method< void,std::string >( "org.freedesktop.Hal.Manager", "Remove" );
          m_method_CommitToGdl_vss = this->create_method< void,std::string,std::string >( "org.freedesktop.Hal.Manager", "CommitToGdl" );
          m_method_AcquireGlobalInterfaceLock_vsb = this->create_method< void,std::string,bool >( "org.freedesktop.Hal.Manager", "AcquireGlobalInterfaceLock" );
          m_method_ReleaseGlobalInterfaceLock_vs = this->create_method< void,std::string >( "org.freedesktop.Hal.Manager", "ReleaseGlobalInterfaceLock" );
          m_method_SingletonAddonIsReady_vs = this->create_method< void,std::string >( "org.freedesktop.Hal.Manager", "SingletonAddonIsReady" );
          m_signal_DeviceAdded = this->create_signal<void,std::string>( "org.freedesktop.Hal.Manager", "DeviceAdded" );
          m_signal_DeviceRemoved = this->create_signal<void,std::string>( "org.freedesktop.Hal.Manager", "DeviceRemoved" );
          m_signal_NewCapability = this->create_signal<void,std::string,std::string>( "org.freedesktop.Hal.Manager", "NewCapability" );
          m_signal_GlobalInterfaceLockAcquired = this->create_signal<void,std::string,std::string,int32_t>( "org.freedesktop.Hal.Manager", "GlobalInterfaceLockAcquired" );
          m_signal_GlobalInterfaceLockReleased = this->create_signal<void,std::string,std::string,int32_t>( "org.freedesktop.Hal.Manager", "GlobalInterfaceLockReleased" );
        }

      public:

        typedef DBusCxxPointer<ManagerProxy> pointer;

        static pointer create( ::DBus::Connection::pointer conn,
                               const std::string& dest="org.freedesktop.Hal",
                               const std::string& path="/org/freedesktop/Hal/Manager"
                             )
        { return pointer( new ManagerProxy(conn, dest, path)); }


        /* can't create proxy for method GetAllDevices */

        /* can't create proxy for method GetAllDevicesWithProperties */

        bool DeviceExists( std::string udi ) { return (*m_method_DeviceExists_bs)( udi); }

        /* can't create proxy for method FindDeviceStringMatch */

        /* can't create proxy for method FindDeviceByCapability */

        std::string NewDevice( ) { return (*m_method_NewDevice_s)(); }

        void Remove( std::string udi ) { return (*m_method_Remove_vs)( udi); }

        void CommitToGdl( std::string temporary_udi, std::string global_udi ) { return (*m_method_CommitToGdl_vss)( temporary_udi, global_udi); }

        void AcquireGlobalInterfaceLock( std::string interface_name, bool exclusive ) { return (*m_method_AcquireGlobalInterfaceLock_vsb)( interface_name, exclusive); }

        void ReleaseGlobalInterfaceLock( std::string interface_name ) { return (*m_method_ReleaseGlobalInterfaceLock_vs)( interface_name); }

        void SingletonAddonIsReady( std::string command_line ) { return (*m_method_SingletonAddonIsReady_vs)( command_line); }

        ::DBus::signal_proxy<void,std::string >& signal_DeviceAdded() { return *m_signal_DeviceAdded; }

        ::DBus::signal_proxy<void,std::string >& signal_DeviceRemoved() { return *m_signal_DeviceRemoved; }

        ::DBus::signal_proxy<void,std::string,std::string >& signal_NewCapability() { return *m_signal_NewCapability; }

        ::DBus::signal_proxy<void,std::string,std::string,int32_t >& signal_GlobalInterfaceLockAcquired() { return *m_signal_GlobalInterfaceLockAcquired; }

        ::DBus::signal_proxy<void,std::string,std::string,int32_t >& signal_GlobalInterfaceLockReleased() { return *m_signal_GlobalInterfaceLockReleased; }


      protected:


        ::DBus::MethodProxy<bool,std::string>::pointer m_method_DeviceExists_bs;
        ::DBus::MethodProxy<std::string>::pointer m_method_NewDevice_s;
        ::DBus::MethodProxy<void,std::string>::pointer m_method_Remove_vs;
        ::DBus::MethodProxy<void,std::string,std::string>::pointer m_method_CommitToGdl_vss;
        ::DBus::MethodProxy<void,std::string,bool>::pointer m_method_AcquireGlobalInterfaceLock_vsb;
        ::DBus::MethodProxy<void,std::string>::pointer m_method_ReleaseGlobalInterfaceLock_vs;
        ::DBus::MethodProxy<void,std::string>::pointer m_method_SingletonAddonIsReady_vs;
        ::DBus::signal_proxy<void,std::string>::pointer m_signal_DeviceAdded;
        ::DBus::signal_proxy<void,std::string>::pointer m_signal_DeviceRemoved;
        ::DBus::signal_proxy<void,std::string,std::string>::pointer m_signal_NewCapability;
        ::DBus::signal_proxy<void,std::string,std::string,int32_t>::pointer m_signal_GlobalInterfaceLockAcquired;
        ::DBus::signal_proxy<void,std::string,std::string,int32_t>::pointer m_signal_GlobalInterfaceLockReleased;


    };

  }
}

#endif

And finally, here is the example application that uses the proxy to watch for device added/removed signals.

/***************************************************************************
 *   Copyright (C) 2007,2008,2009 by Rick L. Vinyard, Jr.                  *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This file is part of the dbus-cxx library.                            *
 *                                                                         *
 *   The dbus-cxx library is free software; you can redistribute it and/or *
 *   modify it under the terms of the GNU General Public License           *
 *   version 3 as published by the Free Software Foundation.               *
 *                                                                         *
 *   The dbus-cxx library is distributed in the hope that it will be       *
 *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
 *   General Public License for more details.                              *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this software. If not see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/
#include <dbus-cxx.h>
#include <hal_manager_proxy.h>
#include <iostream>

DBus::Hal::ManagerProxy::pointer manager;

void on_device_added(std::string device);
void on_device_removed(std::string device);

int main()
{
  DBus::init();

  DBus::Dispatcher dispatcher;

  DBus::Connection::pointer connection = dispatcher.create_connection( DBus::BUS_SYSTEM );

  manager = DBus::Hal::ManagerProxy::create(connection);
  manager->signal_DeviceAdded().connect( sigc::ptr_fun(on_device_added) );
  manager->signal_DeviceRemoved().connect( sigc::ptr_fun(on_device_removed) );

  std::cout << "Running" << std::flush;
  
  for (int i=0; i < 30; i++)
  {
    std::cout << "." << std::flush;
    sleep(1);
  }
  
  std::cout << std::endl;

  return 0;
}

void exists(const std::string& device)
{
  bool exist = manager->DeviceExists( device );
  std::cout << "!!!!! Device " << device << (exist?" exists":" doesn't exist") << std::endl;
}

void on_device_added(std::string device)
{
  std::cout << "+++++ Device Added: " << device << std::endl;
  exists(device);
}

void on_device_removed(std::string device)
{
  std::cout << "----- Device Removed: " << device << std::endl;
  exists(device);
}

Generated on Mon Sep 21 10:59:27 2009 for dbus-cxx by doxygen 1.6.1