How unique is /dev/serial/by-id - linux

So, the question is, is the content of /dev/serial/by-id unique?
Essentially the issue is I want to connect several (two or more) arduinos (potentially of different types, but they may all end up being leonardos) to the Raspberry Pi for the purposes of an automation system.
I'll be using the serial interfaces to communicate between the Raspberry Pi in Python and the Arduinos. I've run this on one of the leonardos (at present I only have one):
udevadm info -a -n /dev/ttyACM0| grep serial
0000:00:1d.0
Is this a unique serial for my serial connection to the Pi? Can I rely on this to create a UDEV rule to assign a particular mount point, or does a unique and reliable mount point get already created in /dev/serial/by-id/, which I can use instead of hacked-udev rules?

It's NOT ALWAYS unique. In my experience, if you bought a cheap arduino clone from China, they mostly didn't bother to generate unique ID for every devices. The same applies for every devices. If the manufacturer didn't bother, then the devices will be identical. I ended up just using the by-path and symlink it.

In my experience using /dev/serial/by-id with USB devices has been unique. That is true as long as the manufacturer follows "the rules" about giving each device a unique serial number.
I just make symlinks to those long names in /dev/serial/by-id and use my symlinks as the handles for my serial devices in scripts. No muss, no fuss, NO UDEV.

The rules for the naming are in
/lib/udev/rules.d/60-persistent-serial.rules

Related

c - using nl80211 without libnl or libnl-genl?

I'm hoping to just use the header in the kernel, linux/nl80211.h to get the channel my network device is on. I'm on a very restricted system where building has to happen with a minimum number of extra packages. It feels strange that SIOCGIWFREQ would be so easy to get, but I'd need a library to just get a frequency via nl80211.
Are there any examples of how to use the nl80211 interface directly in Linux? I'm just hoping to get NL80211_FREQUENCY_ATTR_FREQ
After a lot of struggling, I found out! It's actually easier to use netlink without libnl, as long as you're not doing anything complicated.
I wrote up an example here that prints all your wireless devices, what networks and channels they're connected to: https://github.com/cnlohr/netlink_without_libnl/blob/master/without_libnl.c

Identify USB port in UDev path in Linux?

I'm trying to identify USB keyboards by the physical port it's connected to, including which USB hub.
From experiments I've identified the following:
/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2.4/1-1.2.4.2/1-1.2.4.2:1.0/0003:0461:0010.0024/input/input49/event4
The first part, /devices/platform/soc/3f980000.usb/usb1/ appears to be constant given a system.
I guess it might be different on another system.
The last part, /input/input49/event4 appears to be increasing every insertion.
Each part starting with /1-1 appears to represent one hub in the USB chain, every later one starting with the full path of the previous one.
The last part in the /1-1 chain contains : which identifies sub components inside that USB device.
However only the second number in :1.0 appears to correlate with interface number, the first one is always 1 and I'm not sure how it could change or what that would mean.
My guess would be to identify the last part not containing : and use that as the physical USB path.
0003 looks like it could be USB class: HID
0461:0010 looks like VID:PID
0024 Increases by every insertion
Are my assumption correct?
Is there a reliable way to identify location in the USB Port tree from either UDev path other means?

How to find the mapping between ubertooth device number and USB path and port?

I have a system with two (or more for that matter) ubertooth sticks connected. I command them with
ubertooth-util -U devNumber ...
and
ubertooth-rxtx -U devNumber ...
with devNumber from {0, 1} respectively. I have them connected to a USB hub with per port power switching (supported by uhubctl) so that whenever one gets in a bad state I can switch them off, unbind them and switch them on again.
But which "devNumber" is connected to which port of the USB hub? For devices like USB WLAN cards this is simply done by observing their representation in the /sys-filesystem but the ubertooth sticks do not appear there since no dedicated driver touches them (at least that is what I understand).
I could switch the ports off one by one and check which device still takes a command to create the mapping.
Or I could issue "ubertooth-util -U devNumber -s" and "lsusb -v" and then map by serial number which seems even worse.
Is there a simpler way?

How does Linux determine a device class?

Linux newbie question.
Just wondering how Linux determines which device class a device is? Specifically, when I plug a barcode scanner in how does it know it is an ttyACM device? I have a scanner that works with my Linux OS but the new model isn't recognized so I'm wondering if I can alter a file somewhere in the system that tells it to recognize the scanner as ttyACM0 and use the existing drivers.
USB devices (I assume your scanner is USB) are identified by vendorId and productId (two 16bit integers), each driver fill an array with the list of supported vendor/prods id (creating a relation vendor:prod->driver), I guess at compile time all the id in the array are merged together in a list which then is used for a lookup search when a device is plugged in.
Usually you can see vendor and product id of the attached device with dmesg command right after the device is plugged in (or with lsusb).
For ttyACM see acm_ids[] in drivers/usb/class/cdc-acm.c
Careful playing around with device drivers, even being ttyACM a terminal interface only if the interface tty->hardware is implemented poorly some command may break the hardware.
Perhaps this question should be in Unix & Linux stackexchange

Change default names for USB virtual serial ports in Linux

I am developing an embedded solution using C and I am working with two USB sensors. If I connect each sensor alone they take this names:
Device 1 (I do not know why it takes 6 names...)
/dev/ttyACM0
/dev/ttyACM1
/dev/ttyACM2
/dev/ttyACM3
/dev/ttyACM4
/dev/ttyACM5
/dev/ttyACM6
Device 2
/dev/ttyACM0
So when I start as an embedded system and both sensors are connected, the fastest one takes /dev/ACM0 but it not always the same. So, when I try to read device 2 I could be reading device 1...
I think that It would be great to change the default names of the sensors. I guess that it is going to be possible but I do not find anything.
You should try using the names in /dev/serial/by-id instead, since those names include the name of the device and should not depend on the order of connection.
By the way, it is also possible to write udev rules that make symbolic links for the serial ports depending on what device they belong to. I am not sure how that would work for a composite device with 6 serial ports, but there probably is a way to make it work.

Resources