'How do we emulate virtual CAN devices from host to Android emulator?

I have virtual CAN (vcan0, vcan1) in my host Linux. I want to make them available in an Android Virtual Device (AVD). I am creating the AVD using AVD manager in Androi Studio. However, I cannot seem to make this work.

I create the Virtual CAN devices in my host Linux by following commands:

modprobe can
modprobe can_raw
modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
ip link show vcan0

The above commands are from SocketCAN wiki.

I tried emulating vcan0 inside emulated android by the following command, but it did not work:

~/Android/Sdk/tools/emulator -avd Soham_Device_API_25 -qemu -device can_pci,canbus=canbus0,host=vcan0

or

 ~/Android/Sdk/tools/emulator -avd Soham_Device_API_25 -qemu -device -device mioe3680_pci,canbus1=canbus0,host1=vcan0

and many others. But none of them work and throw error messages like 'can_pci' is not a valid device model name and Device 'vcan0' not found. However, I have checked with test application that vcan0 and vcan1 work in my host Linux. Does anyone know how to enable this vcan0 inside emulated Android?



Solution 1:[1]

If you want to access host system real or virtual CAN bus from the guest systems then you CAN use QEMU SJA1000 CAN controller emulation support.

The support has been mainlinen into 2.12 QEMU version. I have no experience with Android official development environment, so I do not know if it uses recent enough QEMU version.

Basic use instructions: https://www.qemu.org/docs/master/system/devices/can.html

It is required that given target system supports PCI bus which allows to map some of supported board with SJA1000 controller into target. Support for platform bus mapped SJA1000 or other controllers emulation can be implemented when resources are available.

Based on the example in your question, I would expect that

 ~/Android/Sdk/tools/emulator -avd Soham_Device_API_25 -qemu \
      -object can-bus,id=canbus0 \
      -object can-host-socketcan,id=canhost0,if=vcan0,canbus=canbus0 \
      -device kvaser_pci,canbus=canbus0 \

could work, but I am not sure what ~/Android/Sdk/tools/emulator is and how it passes parameters to real QEMU. But it probably passes that because it reports that parameters are incorrect. You attempt to use old dummy can_pci board model used only for development and syntax of the QEMU parameters have changed heavily after change to QEMU object model required for accepting into mainline.

The support to emulate CAN FD bus and controllers based on open-source open-hardware CTU CAN FD core emulation has been included in QEMU mainline as well. Drivers are on the way to the Linux kernel mainline. Look for actual code and state at the CTU FEE CAN related project page http://canbus.pages.fel.cvut.cz/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1