VirtualHub improvements for Linux

VirtualHub improvements for Linux

Linux is an OS which is widely used in the IoT. Unfortunately for the users discovering this OS, it can be quite confusing. For example, by default, Linux blocks write access to the USB devices for "non-root" users. It isn't really easy to configure an application so that it starts at boot time. This week, we are going to see two small improvements which are going to make the life of Linux users easier.




We have added two options to the VirtualHub enabling us to modify the Linux configuration. One enables all users to communicate with Yoctopuce modules, the other one enables us to install the VirtualHub as a service.

UDEV rules


As written above, by default Linux prevents "standard" users to communicate with USB devices. This verification is performed by the UDEV service which is in charge of the management of the machine devices.

To authorize all the users to communicate with Yoctopuce modules, you must add a UDEV rule and restart the machine. This process is described in the Yoctopuce module documentation and we also have a post on the topic.

However, we decided to make the life of users easier by adding a startup option to the VirtualHub so that it automatically records a rule that authorizes users to communicate with Yoctopuce modules. Note that this rule modifies only access to Yoctopuce modules connected on the USB ports. The other devices are not impacted by this rule.

The VirtualHub


From now on, to authorize the VirtualHub to communicate with Yoctopuce modules, you only need to run it once with the --install_udev_rule option to create the UDEV rule. As it is a system modification, you must run it with super-user privileges with the sudo command.

yocto@laptop-linux:~$ sudo VirtualHub --install_udev_rule VirtualHub: UDEV rule added YOU NEED TO REBOOT THE SYSTEM BEFORE THIS RULE IS APPLIED. yocto@laptop-linux:~$



Warning, as the VirtualHub indicates it, you must reboot the machine for the UDEV rule to apply.

When the machine has restarted, the VirtualHub can use the modules connected on the USB ports without sudo.

yocto@laptop-linux:~$ VirtualHub [2021-02-23 12:02:20] (01) Yoctopuce VirtualHub debug (Feb 23 2021 09:17:48) [2021-02-23 12:02:20] (01) Serial is VIRTHUB0-317e8cd34f [2021-02-23 12:02:20] (01) NetworkName is VIRTHUB0-317e8c [2021-02-23 12:02:20] (01) LogicalName is not set [2021-02-23 12:02:21] (01) [1]ystream: 2314: Device YHUBETH1-96D1F plugged [2021-02-23 12:02:21] (01) HTTP server is listening port 4444 [2021-02-23 12:02:21] (01) VXI-11 server is listening port 4445 [2021-02-23 12:02:23] (01) Close open HTTP request ...



Programming libraries


As we thought that this feature could also be useful for your applications, we added a new YAPI.AddLinuxUdevRule() method to the Yoctopuce programming libraries. It contains the same code used by the --install_udev_rule option of the VirtualHub.

Specifically, this method creates a /etc/udev/rules.d/51-yoctopuce.rules file with the following content:

# udev rules to allow write access to all users for Yoctopuce USB devices SUBSYSTEM=="usb", ATTR{idVendor}=="24e0", MODE="0666"



This method uses a force parameter which allows the file to be overwritten if it was already present. When all goes well, an empty string is returned. On error, a string that starts with "error:" then the error message is returned.

Here is an example of its use in Python:

# do not forget to reboot after that
res = YAPI.AddUdevRule(True)
if res.startswith("error:"):
    print("Unable to register UDEV rules.")
    sys.exit(res)



Warning, this command only adds the UDEV rule to the system. For it to be active, you must restart the machine so that the UDEV service also restarts and so that the rule applies. Note that this function returns an error if this function is called under Windows, OSX, or Android.

Starting the VirtualHub at boot time


We also made the installation of the VirtualHub as a service easier for distributions using systemd (Debian, Ubuntu, Raspberry Pi OS, and so on). You don't need anymore to manually install the startup script. We added two options which enable you to add and remove the VirtualHub in the list of services to be started at boot time.

The -i option adds the VirtualHub to the services and starts it. Thus, the VirtualHub is always available even if the machine restarts. By default, the settings of the VirtualHub service are stored in the /etc/vhub.byn file. Note that the other options passed with this command are used when starting the service.

For example, the following command

yocto@laptop-linux:~$ sudo VirtualHub -i -c /my_configfiles/vhub.byn -p 1234


records a service which starts the VirtualHub with options -c /my_configfiles/vhub.byn -p 1234. That is, the VirtualHub saves its parameters in the /my_configfiles/vhub.byn file and will use TCP port 1234.

Note that we used the sudo command in order to temporarily provide super-user privileges to the VirtualHub so that it can save the startup script.

The -u option, on the other hand, removes the VirtualHub from the list of services.

Conclusion

These new options should ease the use of the VirtualHub, particularly if you have installed the VirtualHub with apt-get. The UDEV rule and the startup script that the VirtualHub generates should suit most scenarios. If you have more specific needs, you can always refer to our two previous posts which explain how to create a UDEV rule and how to start an application at boot time.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.