Is it possible to Socket Can with Android?

Hello, today when I was looking for the “Realdash Can” option in the Can connection options, I saw that there was a Socket Can option. I have a custom kernel and Android that I compiled myself. My Android has socketcan support. The applications I wrote with Qt can manage virtual and physical Can devices via socketcan. I tried to establish a vcan0 connection with Realdash but I was not successful. Here is the question. Does the Android version of Realdash have socketcan support?

Yes it has, but it honestly I have never tested that on Android. RealDash attempts to set the CAN speed with system commands that require sudo. The two commands that are executed are:

sudo -n ip link set CAN0 down
sudo -n ip link set CAN0 up type can bitrate 500000 restart-ms 100

That being said, if these commands fail, the SocketCAN initialization should continue and any settings set to CAN ip link will be used. After that its just plain Linux SocketCAN code, which to my understanding should work the same on Android.

@realdashdev
I took a screenshot on my Android device. This screenshot shows the output of the “ip link” command. At the bottom, in the 29th row, there is a virtual can(vcan0) device. The applications I wrote for this Android can access vcan0, and while it is accessing, I can see the access attempts in the “dmesg” output. After selecting the socketcan setting on Realdash, I type vcan0 in the can interface name section. I don’t select any can descriptions xml and than i press the can monitor button. It just writes connecting and waits. Also, there is no information in the dmesg output. Is there something I’m doing wrong?
I added the dmesg outputs for those who are curious. :slight_smile:


You were saying that QT SocketCAN implementation does work in your environment? We can’t use QT as its licensing model is crazy expensive, but I can take a look at QT source code if they are doing something different on SocketCAN connection. What QT version you are working at?

Yes, it’s works perfect.
Well, i am using QT’s open source license.
I am using qt6. I tried it in qt 6.5.2 and 6.4.3 and it works.
Qt engineers have disabled the socketcan library for Android by default. I downloaded the qtserialbus library from Github. “if Not-andoid” conditions are written in the Cmake files, especially for socketcan. I changed Android conditions inside 3-4 cmake files. I compiled the edited library and installed it in the Qt directory and gained socketcan support for Android. It works in real time without lag. If you’re interested I can share how I did it.
My Android is in default SELinux enforcing mode. I put SELinux into permissive mode for Qt to work.
I’m running Android 12 on a Oneplus 5 phone. :slight_smile:

qtserialbus lib : GitHub - qt/qtserialbus at 6.5.2

1 Like

I will take a closer look at the source, but at quick glance I do not see anything drastically different than what we do in RealDash.

Meanwhile, you could send me a debug log, maybe I can see something from that:

How to send a debug log - General / Frequently Asked Questions - RealDash Forum

Sure!
I waited for a while, but the log file did not reach 5kb. I am copy the log file here.

Blockquote
21.9.2023, 11:49:53.677: RealDash version 2.3.1 (PRODUCTION) - Log session opened
21.9.2023, 11:49:53.677: Platform: Android 32. Lang: en. User: RealDasher 012017. Vehicle: My Supercar
21.9.2023, 11:49:57.669: HandleSelectionById - ChangeState(garage)
21.9.2023, 11:50:02.158: ValueInput::DisconnectDataSources
21.9.2023, 11:50:02.158: ValueInput::StopSerialConnections
21.9.2023, 11:50:02.158: Stopping: vcan0
21.9.2023, 11:50:02.158: SerialComms::Gone - entering
21.9.2023, 11:50:02.158: SerialComms(Android)::CloseComms
21.9.2023, 11:50:02.158: SerialComms::Gone - complete
21.9.2023, 11:50:03.509: SerialComms::Create: adapter vcan0
21.9.2023, 11:50:03.509: SerialComms::Starting task: Task_Open
21.9.2023, 11:50:05.472: SerialComms::Gone - entering
21.9.2023, 11:50:05.473: SerialComms(Android)::CloseComms
21.9.2023, 11:50:05.473: SerialComms::Gone - complete
21.9.2023, 11:50:07.401: SerialComms::Create: adapter vcan0
21.9.2023, 11:50:07.401: SerialComms::Starting task: Task_Open
21.9.2023, 11:52:00.510: SerialComms::Gone - entering
21.9.2023, 11:52:00.511: SerialComms(Android)::CloseComms
21.9.2023, 11:52:00.511: SerialComms::Gone - complete
21.9.2023, 11:52:04.808: ValueInput::StopSerialConnections
21.9.2023, 11:52:04.808: ValueInput::StartSerialConnections
21.9.2023, 11:52:04.809: SerialComms::Create: adapter vcan0
21.9.2023, 11:52:04.809: SerialComms::Starting task: Task_Open
21.9.2023, 11:52:13.193: Log session closed

Just wondering, any news? :slight_smile:

I did go thru QT source code on Socket CAN and I did not find anything major being different. Your log just shows that it thinks it is connected, waits for data for 3 seconds and then attempts to reconnect.

Send me an email to contact@realdash.net and I will make a special version that has a lot more logging.

Ok. I sent an email.

Got it, give me couple of days.

I have verified that v2.3.3 works fine with SocketCan on Android.

Also, a little addition for enthusiasts. If you have an android in Enforcing mode you can use these SELinux policies for full access to SocketCan. If you have an android in Permissive mode, nevermind.

# Allow untrusted_app to perform various operations on sysfs files related to can_socket and can_socket devices
allow untrusted_app sysfs_net:dir { read open search };
allow untrusted_app sysfs_net:file { read open getattr };
allow untrusted_app sysfs:file { read open getattr };

# Allow untrusted_app to perform all operations on can_socket objects
allow untrusted_app untrusted_app:can_socket create_stream_socket_perms;

# Allow untrusted_app to perform various operations on netlink_route_socket objects
allow untrusted_app untrusted_app:netlink_route_socket { bind nlmsg_write };

File name : untrusted_app.te

Thanks.

1 Like