[fpc-pascal] DBus interface needs an update

Matthias Klumpp matthias at nlinux.org
Mon May 18 20:12:23 CEST 2009


Well, okay. Because I changed the code frequently, I dont't know if this is
the right solution to do the task, but it does not work like all other
trials.
I use the following function: (Include unit "dbus"):

function CallDBus: Boolean;
var
  err: DBusError;
  conn: PDBusConnection;
  ret: cint;

  msg: PDBusMessage;
  args: DBusMessageIter;
  pending: PDBusPendingCall;
  stat: Boolean;
  level: dbus_uint32_t;
  a: Boolean;
begin

  { Initializes the errors }
  dbus_error_init(@err);
  
  { Connection }
  conn := dbus_bus_get(DBUS_BUS_SESSION, @err);

  if dbus_error_is_set(@err) <> 0 then
  begin
    WriteLn('Connection Error: ' + err.message);
    dbus_error_free(@err);
  end;
  
  if conn = nil then Exit;

  { Request the name of the bus }
  ret := dbus_bus_request_name(conn, 'dbus.test.call',
DBUS_NAME_FLAG_REPLACE_EXISTING, @err);

  if dbus_error_is_set(@err) <> 0 then
  begin
    WriteLn('Name Error: ' + err.message);
    dbus_error_free(@err);
  end;

  if ret <> DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER then Exit;

  // create a new method call and check for errors
  msg := dbus_message_new_method_call('org.freedesktop.PackageKit', //
target for the method call
                                      '/org/freedesktop/PackageKit', //
object to call on
                                      'org.freedesktop.PackageKit', //
interface to call on
                                      'RefreshCache'); // method name
  if (msg = nil) then
  begin
    WriteLn('Message Null');
    Exit;
  end;

  // append arguments
  a:=true;
  dbus_message_iter_init_append(msg, @args);
  if (dbus_message_iter_append_basic(@args, DBUS_TYPE_BOOLEAN, @a) = 0)
then
  begin
    WriteLn('Out Of Memory!');
    Exit;
  end;

  // send message and get a handle for a reply
  if (dbus_connection_send_with_reply(conn, msg, @pending, -1) = 0) then //
-1 is default timeout
  begin
    WriteLn('Out Of Memory!');
    Exit;
  end;
  if (pending = nil) then
  begin
    WriteLn('Pending Call Null');
    Exit;
  end;
  dbus_connection_flush(conn);

  WriteLn('Request Sent');

  // free message
  dbus_message_unref(msg);

  // block until we recieve a reply
  dbus_pending_call_block(pending);

  // get the reply message
  msg := dbus_pending_call_steal_reply(pending);
  if (msg = nil) then
  begin
    WriteLn('Reply Null');
    Exit;
  end;
  // free the pending message handle
  dbus_pending_call_unref(pending);

  // read the parameters
  if (dbus_message_iter_init(msg, @args) = 0) then
     WriteLn('Message has no arguments!')
  else if (DBUS_TYPE_BOOLEAN <> dbus_message_iter_get_arg_type(@args)) then
     WriteLn('Argument is not boolean!')
  else
     dbus_message_iter_get_basic(@args, @stat);

  if (dbus_message_iter_next(@args) = 0) then
     WriteLn('Message has too few arguments!')
  else if (DBUS_TYPE_UINT32 <> dbus_message_iter_get_arg_type(@args)) then
     WriteLn('Argument is not int!')
  else
     dbus_message_iter_get_basic(@args, @level);

  WriteLn('Got Reply: ', stat, ', ', level);

  // free reply
  dbus_message_unref(msg);

  { Finalization }
 //Uncommented because I got the advice NOT to free the dbus connection
 // dbus_connection_close(conn);
end;

If I execute this, I get the following messages:
Request Sent
Argument is not boolean!
Message has too few arguments!
Got Reply: FALSE, 6708224
process 7121: Applications must not close shared connections - see
dbus_connection_close() docs. This is a bug in the application.

I really dont't know why it does not work. I call a method from the
PackageKit DBus service, which works great with all my C++-Programs. (But
there I use a DBus Proxy over GLib)
Maybe I'm too stupid to get this working? I already looked at the new
DBus-Headers and saw that there are a large amount of new methods, also
older ones are missing.
I hope this is enough information.
Regards
    Matthias

P.S: I use Linux 2.6.30-5 (but the problem appears on my Linux 2.6.28-11
machine too) on Ubuntu 9.10 (but I also tried Fedora 10)


On Mon, 18 May 2009 10:06:52 -0500, Prince Riley <wmarketing3 at gmail.com>
wrote:
> Matthias,
> 
> No doubt if even the example code will not work for you (won't compile?),
> then the DBus package code needs to be you taken in hand and fixed.
> 
> I'm not sure how helpful I can be to you on this but I agree with you
about
> how important, if not essential, DBus is. So I 'm willing to assist your
> effort to update the Dbus support in FPC, if in fact that's what need to
be
> done.
> 
> Can you post back any specifics on the trouble with the DBus headers
you've
> had and a few more details of the kernel version and distro you are
using.
> 
> Prince
> 




More information about the fpc-pascal mailing list