busexample.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. program busexample;
  2. {$ifdef fpc}
  3. {$mode objfpc}{$H+}
  4. {$endif}
  5. uses
  6. {$IFDEF UNIX}{$IFDEF UseCThreads}
  7. cthreads,
  8. {$ENDIF}{$ENDIF}
  9. SysUtils,
  10. ctypes,
  11. dbus;
  12. const
  13. SINTAX_TEXT = 'Syntax: dbus-example [send|receive|listen|query] [<param>]';
  14. var
  15. err: DBusError;
  16. conn: PDBusConnection;
  17. ret: cint;
  18. {
  19. * Send a broadcast signal
  20. }
  21. procedure BusSend(sigvalue: PAnsiChar);
  22. var
  23. msg: PDBusMessage;
  24. args: DBusMessageIter;
  25. serial: dbus_uint32_t = 0;
  26. begin
  27. WriteLn('Sending signal with value ', string(sigvalue));
  28. { Request the name of the bus }
  29. ret := dbus_bus_request_name(conn, 'test.signal.source', DBUS_NAME_FLAG_REPLACE_EXISTING, @err);
  30. if dbus_error_is_set(@err) <> 0 then
  31. begin
  32. WriteLn('Name Error: ' + err.message);
  33. dbus_error_free(@err);
  34. end;
  35. if ret <> DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER then Exit;
  36. // create a signal & check for errors
  37. msg := dbus_message_new_signal('/test/signal/Object', // object name of the signal
  38. 'test.signal.Type', // interface name of the signal
  39. 'Test'); // name of the signal
  40. if (msg = nil) then
  41. begin
  42. WriteLn('Message Null');
  43. Exit;
  44. end;
  45. // append arguments onto signal
  46. dbus_message_iter_init_append(msg, @args);
  47. if (dbus_message_iter_append_basic(@args, DBUS_TYPE_STRING, @sigvalue) = 0) then
  48. begin
  49. WriteLn('Out Of Memory!');
  50. Exit;
  51. end;
  52. // send the message and flush the connection
  53. if (dbus_connection_send(conn, msg, @serial) = 0) then
  54. begin
  55. WriteLn('Out Of Memory!');
  56. Exit;
  57. end;
  58. dbus_connection_flush(conn);
  59. WriteLn('Signal Sent');
  60. // free the message and close the connection
  61. dbus_message_unref(msg);
  62. end;
  63. {
  64. * Listens for signals on the bus
  65. }
  66. procedure BusReceive;
  67. var
  68. msg: PDBusMessage;
  69. args: DBusMessageIter;
  70. sigvalue: PAnsiChar;
  71. begin
  72. WriteLn('Listening for signals');
  73. { Request the name of the bus }
  74. ret := dbus_bus_request_name(conn, 'test.signal.sink', DBUS_NAME_FLAG_REPLACE_EXISTING, @err);
  75. if dbus_error_is_set(@err) <> 0 then
  76. begin
  77. WriteLn('Name Error: ' + err.message);
  78. dbus_error_free(@err);
  79. end;
  80. // add a rule for which messages we want to see
  81. dbus_bus_add_match(conn, 'type=''signal'',interface=''test.signal.Type''', @err); // see signals from the given interface
  82. dbus_connection_flush(conn);
  83. if (dbus_error_is_set(@err) <> 0) then
  84. begin
  85. WriteLn('Match Error (', err.message, ')');
  86. Exit;
  87. end;
  88. WriteLn('Match rule sent');
  89. // loop listening for signals being emmitted
  90. while (true) do
  91. begin
  92. // non blocking read of the next available message
  93. dbus_connection_read_write(conn, 0);
  94. msg := dbus_connection_pop_message(conn);
  95. // loop again if we haven't read a message
  96. if (msg = nil) then
  97. begin
  98. sleep(1);
  99. Continue;
  100. end;
  101. // check if the message is a signal from the correct interface and with the correct name
  102. if (dbus_message_is_signal(msg, 'test.signal.Type', 'Test') <> 0) then
  103. begin
  104. // read the parameters
  105. if (dbus_message_iter_init(msg, @args) = 0) then
  106. WriteLn('Message Has No Parameters')
  107. else if (DBUS_TYPE_STRING <> dbus_message_iter_get_arg_type(@args)) then
  108. WriteLn('Argument is not string!')
  109. else
  110. dbus_message_iter_get_basic(@args, @sigvalue);
  111. WriteLn('Got Signal with value ', sigvalue);
  112. end;
  113. // free the message
  114. dbus_message_unref(msg);
  115. end;
  116. end;
  117. procedure reply_to_method_call(msg: PDBusMessage; conn: PDBusConnection);
  118. var
  119. reply: PDBusMessage;
  120. args: DBusMessageIter;
  121. stat: Boolean = true;
  122. level: dbus_uint32_t = 21614;
  123. serial: dbus_uint32_t = 0;
  124. param: PAnsiChar = '';
  125. begin
  126. // read the arguments
  127. if (dbus_message_iter_init(msg, @args) = 0) then
  128. WriteLn('Message has no arguments!')
  129. else if (DBUS_TYPE_STRING <> dbus_message_iter_get_arg_type(@args)) then
  130. WriteLn('Argument is not string!')
  131. else
  132. dbus_message_iter_get_basic(@args, @param);
  133. WriteLn('Method called with ', param);
  134. // create a reply from the message
  135. reply := dbus_message_new_method_return(msg);
  136. // add the arguments to the reply
  137. dbus_message_iter_init_append(reply, @args);
  138. if (dbus_message_iter_append_basic(@args, DBUS_TYPE_BOOLEAN, @stat) = 0) then
  139. begin
  140. WriteLn('Out Of Memory!');
  141. Exit;
  142. end;
  143. if (dbus_message_iter_append_basic(@args, DBUS_TYPE_UINT32, @level) = 0) then
  144. begin
  145. WriteLn('Out Of Memory!');
  146. Exit;
  147. end;
  148. // send the reply && flush the connection
  149. if (dbus_connection_send(conn, reply, @serial) = 0) then
  150. begin
  151. WriteLn('Out Of Memory!');
  152. Exit;
  153. end;
  154. dbus_connection_flush(conn);
  155. // free the reply
  156. dbus_message_unref(reply);
  157. end;
  158. {
  159. * Server that exposes a method call and waits for it to be called
  160. }
  161. procedure BusListen;
  162. var
  163. msg, reply: PDBusMessage;
  164. args: DBusMessageIter;
  165. param: PAnsiChar;
  166. begin
  167. WriteLn('Listening for method calls');
  168. { Request the name of the bus }
  169. ret := dbus_bus_request_name(conn, 'test.method.server', DBUS_NAME_FLAG_REPLACE_EXISTING, @err);
  170. if dbus_error_is_set(@err) <> 0 then
  171. begin
  172. WriteLn('Name Error: ' + err.message);
  173. dbus_error_free(@err);
  174. end;
  175. if ret <> DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER then Exit;
  176. // loop, testing for new messages
  177. while (true) do
  178. begin
  179. // non blocking read of the next available message
  180. dbus_connection_read_write(conn, 0);
  181. msg := dbus_connection_pop_message(conn);
  182. // loop again if we haven't got a message
  183. if (msg = nil) then
  184. begin
  185. sleep(1);
  186. Continue;
  187. end;
  188. // check this is a method call for the right interface & method
  189. if (dbus_message_is_method_call(msg, 'test.method.Type', 'Method') <> 0) then
  190. reply_to_method_call(msg, conn);
  191. // free the message
  192. dbus_message_unref(msg);
  193. end;
  194. end;
  195. {
  196. * Call a method on a remote object
  197. }
  198. procedure BusCall(param: PAnsiChar);
  199. var
  200. msg: PDBusMessage;
  201. args: DBusMessageIter;
  202. pending: PDBusPendingCall;
  203. stat: Boolean;
  204. level: dbus_uint32_t;
  205. begin
  206. WriteLn('Calling remote method with ', param);
  207. { Request the name of the bus }
  208. ret := dbus_bus_request_name(conn, 'test.method.caller', DBUS_NAME_FLAG_REPLACE_EXISTING, @err);
  209. if dbus_error_is_set(@err) <> 0 then
  210. begin
  211. WriteLn('Name Error: ' + err.message);
  212. dbus_error_free(@err);
  213. end;
  214. if ret <> DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER then Exit;
  215. // create a new method call and check for errors
  216. msg := dbus_message_new_method_call('test.method.server', // target for the method call
  217. '/test/method/Object', // object to call on
  218. 'test.method.Type', // interface to call on
  219. 'Method'); // method name
  220. if (msg = nil) then
  221. begin
  222. WriteLn('Message Null');
  223. Exit;
  224. end;
  225. // append arguments
  226. dbus_message_iter_init_append(msg, @args);
  227. if (dbus_message_iter_append_basic(@args, DBUS_TYPE_STRING, @param) = 0) then
  228. begin
  229. WriteLn('Out Of Memory!');
  230. Exit;
  231. end;
  232. // send message and get a handle for a reply
  233. if (dbus_connection_send_with_reply(conn, msg, @pending, -1) = 0) then // -1 is default timeout
  234. begin
  235. WriteLn('Out Of Memory!');
  236. Exit;
  237. end;
  238. if (pending = nil) then
  239. begin
  240. WriteLn('Pending Call Null');
  241. Exit;
  242. end;
  243. dbus_connection_flush(conn);
  244. WriteLn('Request Sent');
  245. // free message
  246. dbus_message_unref(msg);
  247. // block until we recieve a reply
  248. dbus_pending_call_block(pending);
  249. // get the reply message
  250. msg := dbus_pending_call_steal_reply(pending);
  251. if (msg = nil) then
  252. begin
  253. WriteLn('Reply Null');
  254. Exit;
  255. end;
  256. // free the pending message handle
  257. dbus_pending_call_unref(pending);
  258. // read the parameters
  259. if (dbus_message_iter_init(msg, @args) = 0) then
  260. WriteLn('Message has no arguments!')
  261. else if (DBUS_TYPE_BOOLEAN <> dbus_message_iter_get_arg_type(@args)) then
  262. WriteLn('Argument is not boolean!')
  263. else
  264. dbus_message_iter_get_basic(@args, @stat);
  265. if (dbus_message_iter_next(@args) = 0) then
  266. WriteLn('Message has too few arguments!')
  267. else if (DBUS_TYPE_UINT32 <> dbus_message_iter_get_arg_type(@args)) then
  268. WriteLn('Argument is not int!')
  269. else
  270. dbus_message_iter_get_basic(@args, @level);
  271. WriteLn('Got Reply: ', stat, ', ', level);
  272. // free reply
  273. dbus_message_unref(msg);
  274. end;
  275. begin
  276. { Initializes the errors }
  277. dbus_error_init(@err);
  278. { Connection }
  279. conn := dbus_bus_get(DBUS_BUS_SESSION, @err);
  280. if dbus_error_is_set(@err) <> 0 then
  281. begin
  282. WriteLn('Connection Error: ' + err.message);
  283. dbus_error_free(@err);
  284. end;
  285. if conn = nil then Exit;
  286. { Parses parameters }
  287. if (ParamCount <> 1) and (ParamCount <> 2) then WriteLn(SINTAX_TEXT)
  288. else
  289. begin
  290. if ParamStr(1) = 'send' then BusSend(PAnsiChar(ParamStr(2)))
  291. else if ParamStr(1) = 'receive' then BusReceive()
  292. else if ParamStr(1) = 'listen' then BusListen()
  293. else if ParamStr(1) = 'call' then BusCall(PAnsiChar(ParamStr(2)))
  294. else WriteLn(SINTAX_TEXT);
  295. end;
  296. { Finalization }
  297. dbus_connection_close(conn);
  298. end.