stevensonmt

stevensonmt

How to interact with D-Bus from an Elixir app?

Anyone have experience interacting with DBus from within an elixir app? I’m trying to think of the best way to make the app’s internal state available to other services and setting a DBus property seems like one way to do so. I just can’t find much information about doing so from within elixir/erlang.

Marked As Solved

cblavier

cblavier

We use dbus from elixir on our Elixir Nerves project (running on raspberry pi3).
We use it to control different omxplayer processes.

No special library involved, we have a DBusServer genserver managing the state of our daemon. We use the Elixir Port API to monitor its state

We start the daemon like this:

def start_daemon do
  cmd = "dbus-daemon --session --nofork --print-pid --print-address"
  port = Port.open({:spawn, cmd}, [:binary])

  receive do
    {^port, {:data, output}} -> {:ok, port, output}
  after
    @start_timeout -> :error
  end
end

we parse the command output with this code to extract its pid

defp parse_command_output(output) do
  pid_match = Regex.named_captures(~r/^(?<pid>\d+)$/m, output)
  address_match = Regex.named_captures(~r/^(?<address>unix:(path|abstract).*)$/m, output)
  pid = pid_match["pid"]
  address = address_match["address"]

  cond do
    pid && address -> {:ok, %{bus_pid: pid, bus_address: address}}
    address -> :pid_not_found
    true -> :error
  end
end

and interact with it like this

System.cmd(
  "dbus-send",
  [
    "--session",
    "--print-reply=literal",
    "--dest=#{dest}",
    object_name,
    action,
    message
  ],
  env: [
    {"DBUS_SESSION_BUS_ADDRESS", bus_address},
    {"DBUS_SESSION_BUS_PID", bus_pid}
  ]
)

Also Liked

stevensonmt

stevensonmt

Plan to update this thread with a more thorough breakdown when I wrap up the project but I just wanted to mention that in order to get the functionality out of dbus that I really wanted I had to break down and use Rustler to wrap dbus-rs bindings. The interaction can then be handled with busctl from scripts or from within the app using the dbus-rs calls. I’m sure there are other ways but this is the only workable solution I could find.

To anyone interested, I wanted to use the zbus crate instead of dbus-rs because the documentation on that crate is so good, but zbus is async first, so much that even the blocking API requires use of async. Async functions with Rustler were a layer of complexity beyond my current capacity.

tj0

tj0

BTW, regarding the comment from @cblavier , it looks like that they are starting their own dbus session on Nerves and then interacting with dbus-send. I think this is probably because they bring the system up from scratch.

If you are planning to run on Ubuntu for example, you can just use the already existing
DBUS_SESSION_BUS_ADDRESS in your environment.

Anyway, I’m not really sure what you’re trying to build, so just remember that if the session/user dbus environmental variable already exists, that is the one you probably want.

tj0

tj0

I haven’t done this in Erlang/Elixir, but I got smacked real bad once because there’s a difference between the system and session dbus.

Here are some notes in case you are unfamiliar.

There are two different dbus types. There is a “system” dbus which usually runs under /run/dbus/system_bus_socket (or some close by location) as user dbus and is typically not the source of problems.

However, there is generally confusion when a program doesn’t work. That’s becaues the [system] dbus is running, but the program can’t find [user] dbus.

The user dbus runs as your regular user account and does not have a well-known location to listen. Usually, programs needing the user-dbus will read the location from environmental variable DBUS_SESSION_BUS_ADDRESS.

Note you might think that launching your program with dbus-launch is a good idea because it will then easily find dbus. This thinking will get you in a world of hurt because now there will be three separate dbus interfaces running, none of which speak to the other.

Good luck in your quest. :crossed_fingers:

Where Next?

Popular in Questions Top

New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement