[SOLVED} dbus-send works on cli, fails in bash

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
JLC17
Level 3
Level 3
Posts: 175
Joined: Tue Feb 07, 2017 11:51 am

[SOLVED} dbus-send works on cli, fails in bash

Post by JLC17 »

Messing around with ofono-phonesim.

from cli

Code: Select all

>ofono-phonesim -p 12345 -gui /usr/share/phonesim/default.xml&
>dbus-send --print-reply --system --dest=org.ofono /phonesim org.ofono.Modem.SetProperty string:"Powered" variant:boolean:true
method return time=1612370672.056339 sender=:1.553 -> destination=:1.556 serial=35 reply_serial=2
>dbus-send --print-reply --system --dest=org.ofono /phonesim org.ofono.Modem.SetProperty string:"Online" variant:boolean:true
method return time=1612370672.056339 sender=:1.553 -> destination=:1.556 serial=35 reply_serial=2
works fine.

Put it in a bash script, and the first line executes (ofono-phonesim runs) and the next two lines give:

Code: Select all

>./myscript.sh
>Error org.ofono.Error.Failed: Operation failed
>Error org.ofono.Error.Failed: Operation failed

Now I am suspecting some permissions issue. Googling
using dbus-send inside a bash script
didn't seem to help.

Could anyone help, please ?

SOLVED

Even though the ofono-phonesim process had started, it wasn't ready to accept connections ... inserting a

Code: Select all

sleep 5
before calling dbus-send fixed the issue.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: dbus-send works on cli, fails in bash

Post by Welcome »

Interesting problem, I wonder if it could be a timing related issue?

If you place "sleep 1" between the commands in the script, does it change?

Otherwise, could there be any environment variables that are available at the cli, but not in the script? For example, is the environment variable DBUS_SESSION_BUS_ADDRESS available in both?
JLC17
Level 3
Level 3
Posts: 175
Joined: Tue Feb 07, 2017 11:51 am

Re: dbus-send works on cli, fails in bash

Post by JLC17 »

Thanks for the reply

I excluded that before i posted ...

Code: Select all

ofono-phonesim -p 12345 -gui /usr/share/phonesim/default.xml&

echo "$(pidof ofono-phonesim )"
shows a pid before the error ....
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: dbus-send works on cli, fails in bash

Post by 1000 »

2
When I try to run ./enable-modem, I get the following error: File "/usr/lib/python3/dist-packages/dbus/connection.py", line 652, in call_blocking reply_message = self.send_message_with_reply_and_block( dbus.exceptions.DBusException: org.ofono.Error.Failed: Operation failed What does it mean and how do I get around it? – Zoltán Oct 13 '20 at 21:19

My problem with ./enable-modem was that I didn't have phonesim running while enabling the modem. The thing is that the previous command - ofono-phonesim -p 12345 /usr/share/phonesim/default.xml - has to be left running, then enable-modem works. – Zoltán Dec 18 '20 at 16:00
Post from Link https://askubuntu.com/questions/831331/ ... 10_1236379

Edit
Similar answer https://askubuntu.com/questions/1283580 ... -error-fai
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: dbus-send works on cli, fails in bash

Post by Welcome »

Are you running as root in both cases?
JLC17
Level 3
Level 3
Posts: 175
Joined: Tue Feb 07, 2017 11:51 am

Re: dbus-send works on cli, fails in bash

Post by JLC17 »

No.
JLC17
Level 3
Level 3
Posts: 175
Joined: Tue Feb 07, 2017 11:51 am

Re: dbus-send works on cli, fails in bash

Post by JLC17 »

1000 wrote: Wed Feb 03, 2021 2:26 pm Similar answer https://askubuntu.com/questions/1283580 ... -error-fai
Similar maybe. But helpful, no.

As I posted previously phonesim is running (has a pid) when the call fails.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: dbus-send works on cli, fails in bash

Post by 1000 »

You wrote ofono-phonesim
but inside lines you have /phonesim
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: dbus-send works on cli, fails in bash

Post by Welcome »

JLC17 wrote: Wed Feb 03, 2021 2:57 pmNo.
I tried to review a lot of web pages regarding your problem. I think one mentioned something about needing root unless your config is set up to not need it. Yeah, it was a stab in the dark.

In the past, I've had a problem when using MPRIS (dbus) where I'd get a random failure. My solution was to create a small test and retry-on-fail loop, with a small time delay between attempts (and a drop out after x number of attempts).

Otherwise, hope you or someone else comes up with a solution. Please let us know when you find something that works.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: dbus-send works on cli, fails in bash

Post by 1000 »

Sorry my mistake, path looks the same "--dest=org.ofono /phonesim"
in https://wiki.archlinux.org/index.php/Bl ... PulseAudio
Welcome
Level 6
Level 6
Posts: 1026
Joined: Wed Aug 19, 2020 11:38 am

Re: dbus-send works on cli, fails in bash

Post by Welcome »

Welcome wrote: Wed Feb 03, 2021 1:49 pm Interesting problem, I wonder if it could be a timing related issue?

If you place "sleep 1" between the commands in the script, does it change?

Otherwise, could there be any environment variables that are available at the cli, but not in the script? For example, is the environment variable DBUS_SESSION_BUS_ADDRESS available in both?
Welcome wrote: Wed Feb 03, 2021 3:32 pm
I tried to review a lot of web pages regarding your problem. I think one mentioned something about needing root unless your config is set up to not need it. Yeah, it was a stab in the dark.

In the past, I've had a problem when using MPRIS (dbus) where I'd get a random failure. My solution was to create a small test and retry-on-fail loop, with a small time delay between attempts (and a drop out after x number of attempts).

Otherwise, hope you or someone else comes up with a solution. Please let us know when you find something that works.

Code: Select all

#!/bin/bash
ofono-phonesim -p 12345 -gui /usr/share/phonesim/default.xml&
sleep 1
# OPTION: create a small test and retry-on-fail loop, with a small time delay between attempts (and a exit with error after x number of attempts).
dbus-send --print-reply --system --dest=org.ofono /phonesim org.ofono.Modem.SetProperty string:"Powered" variant:boolean:true
sleep 1
# OPTION: create another small test and retry-on-fail loop, with a small time delay between attempts (and a exit with errort after x number of attempts).
dbus-send --print-reply --system --dest=org.ofono /phonesim org.ofono.Modem.SetProperty string:"Online" variant:boolean:true
#EOF

Code: Select all

sudo ./myscript.bash
@JLC17 - Your solution was...? :D
JLC17
Level 3
Level 3
Posts: 175
Joined: Tue Feb 07, 2017 11:51 am

Re: dbus-send works on cli, fails in bash

Post by JLC17 »

Welcome wrote: Thu Feb 04, 2021 8:23 am @JLC17 - Your solution was...? :D
A variant of your suggestion, thanks.

Still no closer to actually getting it working. The obvious solution is that Linux and Bluetooth really don't play nicely together. So I can't dump my phone just yet :(

(really I should have known. When I switched to Mint in 2017, I wanted to use a BT headset to make VOIP calles via my PC. That was a week I'll never get back).
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED} dbus-send works on cli, fails in bash

Post by 1000 »

In one above link is
2. Config pulseaudio to use ofono:

Goto /etc/pulse/default.pa find the line load-module module-bluetooth-discover and change it in load-module module-bluetooth-discover headset=ofono.
Add the user pulse to group bluetooth to grant the permission: sudo usermod -aG bluetooth pulse (probably it's already correct)
VERY IMPORTANT: To grant the permission, add this to /etc/dbus-1/system.d/ofono.conf (before </busconfig>):

Code: Select all

<policy user="pulse">
  <allow send_destination="org.ofono"/>
</policy>
Can you check ?
JLC17
Level 3
Level 3
Posts: 175
Joined: Tue Feb 07, 2017 11:51 am

Re: [SOLVED} dbus-send works on cli, fails in bash

Post by JLC17 »

I think this thread has descended into confusion :(

I have no problem now switching pulseaudio to use the "HFP" profile of my headset. The script I wrote, following the thread here

viewtopic.php?t=302364

does that fine.

And when it does, the headset appears as a headset in pavucontrol. However, the microphone - for whatever reason - refuses to work. And that's that.

I had already made the permission changes:

Code: Select all

Add the user pulse to group bluetooth to grant the permission: sudo usermod -aG bluetooth pulse (probably it's already correct)
VERY IMPORTANT: To grant the permission, add this to /etc/dbus-1/system.d/ofono.conf (before </busconfig>):
and making the change to /etc/pulse/default.pa and restart pulseaudio didn't change anything.

However the original question in this thread is now solved and closed, with many thanks to all who commented and gave me their time. Virtual beers all round :D
Locked

Return to “Scripts & Bash”