2 -- D_Bus/Ada - An Ada binding to D-Bus
4 -- Copyright (C) 2011 Reto Buerki <reet@codelabs.ch>
6 -- This program is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU General Public License
8 -- as published by the Free Software Foundation; either version 2
9 -- of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU General Public License for more details.
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 -- As a special exception, if other files instantiate generics from this
22 -- unit, or you link this unit with other files to produce an
23 -- executable this unit does not by itself cause the resulting
24 -- executable to be covered by the GNU General Public License. This
25 -- exception does not however invalidate any other reasons why the
26 -- executable file might be covered by the GNU Public License.
29 with Interfaces.C.Strings;
33 package body D_Bus.Messages is
36 use type Interfaces.C.Strings.chars_ptr;
38 package C renames Interfaces.C;
40 function Value_Or_Empty (Ptr : C.Strings.chars_ptr) return String;
41 -- Returns empty string if the given pointer is a null pointer, if not the
42 -- function returns the corresponding string value.
44 -------------------------------------------------------------------------
46 procedure Add_Arguments
47 (Msg : in out Message_Type;
48 Args : Arguments.Argument_List_Type)
50 D_Args : aliased DBusMessageIter;
52 dbus_message_iter_init_append
53 (message => Msg.Thin_Msg,
54 iter => D_Args'Access);
55 Arguments.Serialize (Args => Args,
56 D_Args => D_Args'Access);
59 -------------------------------------------------------------------------
62 (D_Msg : access dbus_message_h.DBusMessage)
66 return M : Message_Type do
71 -------------------------------------------------------------------------
73 function Get_Arguments
75 return Arguments.Argument_List_Type
77 use type dbus_types_h.dbus_bool_t;
79 D_Args : aliased dbus_message_h.DBusMessageIter;
80 Args : Arguments.Argument_List_Type;
82 if dbus_message_h.dbus_message_iter_init
83 (message => Msg.Thin_Msg,
84 iter => D_Args'Access) = 1
86 Args := Arguments.Deserialize (D_Args'Access);
92 -------------------------------------------------------------------------
94 function Get_Destination (Msg : Message_Type) return String
96 C_Dest : constant C.Strings.chars_ptr := dbus_message_get_destination
97 (message => Msg.Thin_Msg);
99 return Value_Or_Empty (Ptr => C_Dest);
102 -------------------------------------------------------------------------
104 function Get_Interface (Msg : Message_Type) return String
106 C_Iface : constant C.Strings.chars_ptr := dbus_message_get_interface
107 (message => Msg.Thin_Msg);
109 return Value_Or_Empty (Ptr => C_Iface);
112 -------------------------------------------------------------------------
114 function Get_Member (Msg : Message_Type) return String
116 C_Member : constant C.Strings.chars_ptr := dbus_message_get_member
117 (message => Msg.Thin_Msg);
119 return Value_Or_Empty (Ptr => C_Member);
122 -------------------------------------------------------------------------
124 function Get_Path (Msg : Message_Type) return String
126 C_Path : constant C.Strings.chars_ptr := dbus_message_get_path
127 (message => Msg.Thin_Msg);
129 return Value_Or_Empty (Ptr => C_Path);
132 -------------------------------------------------------------------------
134 function Get_Sender (Msg : Message_Type) return String
136 C_Sender : constant C.Strings.chars_ptr := dbus_message_get_sender
137 (message => Msg.Thin_Msg);
139 return Value_Or_Empty (Ptr => C_Sender);
142 ------------------------------------------------------------------------
144 function Get_Serial (Msg : Message_Type) return Positive
147 return Positive (dbus_message_get_serial (message => Msg.Thin_Msg));
150 when Constraint_Error =>
151 raise D_Bus_Error with "Message has an invalid serial number";
154 -------------------------------------------------------------------------
156 function Get_Type (Msg : Message_Type) return Message_Variant
159 return Message_Variant'Val
160 (dbus_message_get_type (message => Msg.Thin_Msg));
163 when Constraint_Error =>
164 raise D_Bus_Error with "Message has an invalid type";
167 -------------------------------------------------------------------------
169 function Is_Method_Call
177 C_Iface : C.Strings.chars_ptr := C.Strings.New_String
179 C_Member : C.Strings.chars_ptr := C.Strings.New_String
183 Res := dbus_message_is_method_call
184 (message => Msg.Thin_Msg,
187 C.Strings.Free (Item => C_Iface);
188 C.Strings.Free (Item => C_Member);
192 -------------------------------------------------------------------------
195 (Reply_To : Message_Type;
197 Error_Message : String)
200 Error : Message_Type;
201 C_Name : C.Strings.chars_ptr
202 := C.Strings.New_String (Str => Error_Name);
203 C_Msg : C.Strings.chars_ptr
204 := C.Strings.New_String (Str => Error_Message);
206 Error.Thin_Msg := dbus_message_new_error
207 (reply_to => Reply_To.Thin_Msg,
208 error_name => C_Name,
209 error_message => C_Msg);
210 C.Strings.Free (Item => C_Name);
211 C.Strings.Free (Item => C_Msg);
213 if Error.Thin_Msg = null then
214 raise D_Bus_Error with "Could not create error reply";
220 -------------------------------------------------------------------------
222 function New_Method_Return (Method_Call : Message_Type) return Message_Type
224 Reply : Message_Type;
226 Reply.Thin_Msg := dbus_message_new_method_return
227 (method_call => Method_Call.Thin_Msg);
229 if Reply.Thin_Msg = null then
230 raise D_Bus_Error with "Could not create method reply message";
234 end New_Method_Return;
236 -------------------------------------------------------------------------
240 return access dbus_message_h.DBusMessage
246 -------------------------------------------------------------------------
248 procedure Unref (Msg : in out Message_Type)
251 if Msg /= Null_Message then
252 dbus_message_unref (message => Msg.Thin_Msg);
253 Msg.Thin_Msg := null;
257 -------------------------------------------------------------------------
259 function Value_Or_Empty (Ptr : C.Strings.chars_ptr) return String
262 if Ptr = C.Strings.Null_Ptr then
266 return C.Strings.Value (Item => Ptr);