ttyUSB connection inside snap

Hi, I am new to snapcraft.
I have a GPS that is connected to the host on ttyUSB2.
On the host, I can cat /dev/ttyUSB2 and see the traffic flow.

I would like my application to get the same information from ttyUSB2 inside the snap.
I have read the post auto-connected-serial-port-interface

I would like to confirm,

  1. for serial ports to be exposed inside the snap I need a gadget snap that will expose the ports to my snap via slots?
  2. Do I have to build this gadget snap?
  3. Can I use the “code” in the post auto-connected-serial-port-interface after the ========serialports======== part as type of base?

Help would be appreciated, thank you in advance.

Hi, you can try out the new hotplug serial-port interface for this purpose. See https://snapcraft.io/docs/hotplug-support.

Basically, after enabling hotplug (it’s still experimental), you will see new individual serial-port interfaces appear for all matching serial ports (i.e. ones that define enough udev attributes to uniquely identify the device), and you can connect your snap’s serial-port plug to these slots. This works on both Ubuntu Core and classic Ubuntu and will not require a new gadget snap.

Hi, thank you for your reply.
I went through the process to enable hotplug support.
Before I enabled hotplug support it looked like this in my /val/log/syslog

Jul 31 15:04:51 OptiPlex-9020 kernel: [199647.867119] usb 3-8: USB disconnect, device number 12
Jul 31 15:04:51 OptiPlex-9020 kernel: [199647.867549] mxuport ttyUSB0: MOXA UPort converter now disconnected from ttyUSB0
Jul 31 15:04:51 OptiPlex-9020 kernel: [199647.867732] mxuport ttyUSB1: MOXA UPort converter now disconnected from ttyUSB1
Jul 31 15:04:51 OptiPlex-9020 kernel: [199647.867776] mxuport 3-8:1.0: device disconnected
Jul 31 15:04:58 OptiPlex-9020 kernel: [199654.751203] usb 3-8: new high-speed USB device number 13 using xhci_hcd
Jul 31 15:04:58 OptiPlex-9020 kernel: [199654.880049] usb 3-8: New USB device found, idVendor=110a, idProduct=1250
Jul 31 15:04:58 OptiPlex-9020 kernel: [199654.880052] usb 3-8: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Jul 31 15:04:58 OptiPlex-9020 kernel: [199654.880053] usb 3-8: Product: MOXA UPort 1250
Jul 31 15:04:58 OptiPlex-9020 kernel: [199654.880054] usb 3-8: Manufacturer: MOXA Technologies
Jul 31 15:04:58 OptiPlex-9020 kernel: [199654.880215] usb 3-8: ep 0x83 - rounding interval to 8 microframes, ep desc says 10 microframes
Jul 31 15:05:01 OptiPlex-9020 kernel: [199658.003469] mxuport 3-8:1.0: Using device firmware version v1.4.1
Jul 31 15:05:01 OptiPlex-9020 kernel: [199658.003495] mxuport 3-8:1.0: MOXA UPort converter detected
Jul 31 15:05:01 OptiPlex-9020 kernel: [199658.004122] usb 3-8: MOXA UPort converter now attached to ttyUSB0
Jul 31 15:05:01 OptiPlex-9020 kernel: [199658.004500] usb 3-8: MOXA UPort converter now attached to ttyUSB1
Jul 31 15:05:01 OptiPlex-9020 mtp-probe: checking bus 3, device 13: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-8"
Jul 31 15:05:01 OptiPlex-9020 mtp-probe: bus: 3, device: 13 was not an MTP device
Jul 31 15:05:01 OptiPlex-9020 snapd[1172]: hotplug.go:193: hotplug device add event ignored, enable experimental.hotplug
Jul 31 15:05:01 OptiPlex-9020 snapd[1172]: hotplug.go:193: hotplug device add event ignored, enable experimental.hotplug

the text snapd[1172]: hotplug.go:193: hotplug device add event ignored, enable experimental.hotplug was present in the log.

After enabling the hotplug support, the text was not in the log anymore, so something happened there.
When executing the command snap connections bms2 with the usb device connected the result is

Interface    Plug              Slot         Notes
home         bms2:home         :home        -
network      bms2:network      :network     -
opengl       bms2:opengl       :opengl      -
pulseaudio   bms2:pulseaudio   :pulseaudio  -
raw-usb      bms2:raw-usb      -            -
serial-port  bms2:serial-port  -            -
x11          bms2:x11          :x11         -

I thought the raw-usb or the serial-port plug would have been connected to something?

Here is a extract of the yaml file

apps:
  bms2:
    command: bin/start_bms.sh
    plugs: [opengl, network, serial-port, x11, home, raw-usb, pulseaudio]
    environment: 
      LD_LIBRARY_PATH: $SNAP/opt/Qt/5.12.2/gcc_64/lib:$SNAP/usr/local/lib/x86_64-linux-gnu/malt:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/snap/bms/x45/usr/lib/x86_64-linux-gnu/gstreamer1.0/gstreamer-1.0:$LD_LIBRARY_PATH
      QT_PLUGIN_PATH: $QT_PLUGIN_PATH:$SNAP/opt/Qt/5.12.2/gcc_64/plugins
      QML2_IMPORT_PATH: $QML2_IMPORT_PATH:$SNAP/opt/Qt/5.12.2/gcc_64/qml
      XDG_RUNTIME_DIR: /run/user/1000
      LIBGL_DRIVERS_PATH: $SNAP/usr/lib/x86_64-linux-gnu/dri

parts:
  bms2:
    plugin: dump
    source: .
    stage-packages: [libfreetype6, libpng16-16, mesa-utils, libgl1-mesa-dri, libgl1-mesa-glx, libfontconfig1, fonts-freefont-ttf, ttf-ubuntu-font-family, libxrender1, libxkbcommon-x11-0, libxkbcommon0, libxcb-xfixes0, libpq5, libpulse0, libpulse-mainloop-glib0, libasound2]
    override-build: |
      snapcraftctl build 
      chmod +x $SNAPCRAFT_PART_INSTALL/bin/start_bms.sh

Is there something missing in the yaml file perhaps?
I do have the serial-port plug in the apps: section.
Do I need something for the port connection the parts: section?

you still need to connect the serial-port interface. This interface is not automatically connected so it must be done manually:

snap connect bms2:serial-port core:ft232rusbuart

Here, I’ve used a slot called ft232rusbuart, but the actual slot name for your system must be discovered once you’ve plugged in your device with:

snap interface serial-port

which, if everything is set-up correctly, will output something similar to this:

name:    serial-port
summary: allows accessing a specific serial port
slots:
  - core:ft232rusbuart

Where the slot is the one you want to use in your snap connect command.

1 Like

HI
Thanks @lucyllewy and @ijohnson this works for me.
I installed the core from the --edge chanel and flowed @lucyllewy 's advice, and it works thank you.

1 Like

Hi @lucyllewy and @ijohnson
I have a new scenario where the GPS is now connected on the serial port on /dev/ttyS5
When executing the snap interface serial-port command I can see there is no slot that provides the interface, although /dev/ttyS5 does exist.

How will I go about providing this serial port interface to the snap?

Regards
Theuns Heydenrych

Is this the same physical device or a different device? If it’s the same physical device this sounds like a bug with hotplug interface, if it’s different it may be that there are not enough udev attributes defined for the device when it is plugged in.

Hi, thank you for the reply
It is the same physical device. The port is always available even if the device is not attached.

It does not “disappear” like usb devices when plugged in or out.

Regards

I see, sorry I misread your statement, this is a different device from what you were using above (which was on /dev/ttyUSB2). In this case, it seems that /dev/ttyS5 is probably a static path for the same physical device, so this device is probably a better match for defining it with slots from a gadget snap. However, it may still be possible to use hotplug here. Can you run the following command to see what udev attributes are defined for the device?

$ udevadm info /dev/ttyS5

Currently, to be usable with the serial-port hotplug interface, you need to have at least the ID_VENDOR_ID and ID_MODEL_ID attributes defined as well as be a member of the tty, usb subsystem or match a specific device path regex (which /dev/ttyS5 does).

HI, thank you for the reply.
I will be back at the office on Monday, to send you the info of the udevadm command.

Regards

Theuns Heydenrych

HI @ijohnson,
here is the output for the /dev/ttyS5 according to

$ udevadm info /dev/ttyS5

P: /devices/pci0000:00/0000:00:18.1/dw-apb-uart.5/tty/ttyS5
N: ttyS5
E: DEVNAME=/dev/ttyS5
E: DEVPATH=/devices/pci0000:00/0000:00:18.1/dw-apb-uart.5/tty/ttyS5
E: ID_BUS=pci
E: ID_MM_CANDIDATE=1
E: ID_MODEL_FROM_DATABASE=Celeron N3350/Pentium N4200/Atom E3900 Series HSUART Controller
E: ID_MODEL_ID=0x5abe
E: ID_PCI_CLASS_FROM_DATABASE=Signal processing controller
E: ID_PCI_SUBCLASS_FROM_DATABASE=Signal processing controller
E: ID_VENDOR_FROM_DATABASE=Intel Corporation
E: ID_VENDOR_ID=0x8086
E: MAJOR=4
E: MINOR=69
E: SUBSYSTEM=tty
E: TAGS=:systemd:
E: USEC_INITIALIZED=7929696

Is there any of this information that I can use to get the serial-port exposed to the snap?

Regards
Theuns Heydenrych

I think for now there is nothing you can do to access that serial port specifically, because it doesn’t have the required udev attributes to be usable with the hotplug serial-port interface.

@pstolowski maybe you can shed some light on this? Do we have plans to use hotplug to enumerate static serial port paths like this (that don’t have the same attributes as say a USB serial-port)?

I think the reason the above /dev/ttyS5 is ignored by hotplug for serial-port is because inside serial_port hotplug handler we filter out ports if ID_BUS is not “usb” (yours is “pci”).

Regarding @ijohnson 's question: there were very vague ideas about using hotplug to enumerate static devices (not just for serial ports, but in general where it make sense with other interfaces, I think gpio was an example), but no concrete plan. I think PCI would fall into this category? Perhaps it is time to revisit this idea.

EDIT: gpio was a bad example, I got corrected on this.

1 Like

Thank you for the replies
@ijohnson , @pstolowski will I maybe get access to the static serial port via a gadget snap?