Gadget providing serial-port slot

We are currently experimenting ubuntu core on a Dell Edge 5000 , the device comes with 4 serial ports (3 RS485 and 1 RS232) and 3 USB ports .

The snap needs access to these serial ports (including USB) , From the interface documentation , it is mentioned that serial-port interface needs to be used but there is also a mention that a gadget needs to be available providing the slot. I could not get any gadgets for DELL 5000 that provides these slots (may be they are private) . The documentation for creating gadgets looks to be a “too much” work for a snap developer (which of-course would be a last resort ) .But just to get the things rolling , are there any ready to use generic gadgets which would provide access to the available serial ports on any device ?

I really do not know the rationale behind not having this feature supported by the core snap.

Thanks in advance.

The core snap is a generic root filesystem that provides generic interfaces. Typically they are more service focused ones, i.e. network-manager, browser-support, timeserver-control or process-control. Or the ones that are rather generically using userspace libs and binaries from the core rootfs but combined with some device like opengl, joystick, ppp or log-observe.

The gadget snap defines everything hardware specific about an image, the partition table, on arm systems the device tree to use (that defines all kernel devices and drivers for the hardware), the type of bootloader used and basic hardware configuration to bring up the system devices. As such, it also defines all interfaces that are specific to that hardware, examples are i2c, gpio mappings, ioports-control, hidraw or serial-port.

If your device came preinstalled with Ubuntu Core 16 then snap interfaces should definitely list serial interfaces provided by the gadget, does it not ?

1 Like

@ogra thanks for the reply. The device came with a different OS and I installed a generic amd64 ubuntu core image from (http://releases.ubuntu.com/ubuntu-core/16/) . I am sure the devices which come factory installed with ubuntu core from DELL should have some snap providing the serial-interface but in this case (generic ubuntu core image) looks like we hit a dead end.

Well, rolling your own gadget with additional serial interfaces is rather trivial:

  • install snapcraft:
snap install snapcraft --classic
  • clone the gadget source tree
git clone https://github.com/snapcore/pc-amd64-gadget.git
cd pc-amd64-gadget
  • now edit snapcraft.yaml and append something like (this would be for four RS232 ports, i’m not sure how RS485 shows up in /dev, you would have to adjust accordingly):
slots:
    serial0:
      interface: serial-port
      path: /dev/ttyS0
    serial1:
      interface: serial-port
      path: /dev/ttyS1
    serial2:
      interface: serial-port
      path: /dev/ttyS2
    serial3:
      interface: serial-port
      path: /dev/ttyS3
  • and now you just call snapcraft in the source dir and a gadget snap gets produced.

To build an image with this gadget snap you do the following:

  • install ubuntu-image:
snap install ubuntu-image --beta --classic
  • get the model assertion (image description and signature):
snap known --remote model series=16 model=pc-amd64 brand-id=canonical >pc.model
  • now tell ubuntu-image to build a new image that includes the new gadget snap:
sudo ubuntu-image --channel stable --extra-snaps /path/to/your/gadget.snap /path/to/pc.model

this produces a new “pc.img” file, that you can write to the HDD of your device.

4 Likes

awesome, will give a try and update. Thanks for the help.

Also, is it also required to add usb ports as well ? Or that is taken care by raw-usb interface ?

raw-usb will help you to talk directly to usb devices on the lowest level, you might or might not need to set up additional interfaces for whatever usb device you have attached…

i.e. if you have a usb camera raw-usb would not give you access to /dev/video but only to the raw usb device the camera creates on the bus, you would rather use the camera interface here (which will give you /dev/video and raw access to the very specific device that /dev/video uses in the back) … or disks, raw-usb would not allow you to mount the disk but give you access to the low level device only, here you would instead use the removable-media interface (and perhaps also mount-observe).

Read: for your USB ports you should decide on a case-by-case base which interface is the best suited one for the specific use-case … most of the time the high level ones are likely enough and you will not need anything in the gadget.

In case you did not find it yet, the interface reference list is at:

As far as I understand the current situation, you have to pre plan how many USB to serial converters you want to use. They typically appear as /dev/ttyUSB<x> or /dev/ttyACM<x> and every single one has to be exported in the gadgets snapcraft.yaml. Plugging in new devices or using wild cards is not supported (please correct me if I’m wrong).

you can do something like:

slots:
  foobar:
    interface: serial-port
    usb-vendor: 0xabcd
    usb-product: 0x1234
    usb-interface-number: 0
    path: /dev/serial-port-myserial

if you know the vendor and product ID’s if the to be attached device. After you connect the interface your application can then talk to it on /dev/serial-port-myserial which the interface creates for you …

(i think there was also an option to use serial numbers in the above block, to make the setup even closer bound to a particular device)

1 Like

@thymythos We have hotplug coming next in our roadmap, as soon as we finish the on-going implementation of interface hooks, which are a requirement for making good use of the dynamic connections.

1 Like

Thanks @ogra for the gadget creation overview! I’m doing something similar to OP in the context of a Raspberry Pi with an expansion board. I created the gadget, and was able to create the image also (with some slight modifications to the instructions; see below). That said, is there a way to install a gadget snap - e.g. for quick testing - without going through the image build and flash process? Specifically, when trying to install my gadget snap I get the following error:

$ snap install strato-pi3_16.04-0.5_armhf.snap --dangerous
error: cannot perform the following tasks:
- Mount snap "strato-pi3" (unset) (cannot replace signed gadget snap with an unasserted one)

That seems sensible enough - but what’s the right way to add a valid assertion to the gadget snap? If I try to push it to the store I get an error about not being allowed to push gadget snaps.

Aside: building the image
I ran into two issues with the instructions in your post, and indeed those on https://docs.ubuntu.com/core/en/guides/build-device/board-enablement; am posting them here for the benefit of others:

  • This isn’t actually an issue for the pc-amd64 gadget, but for the Raspberry Pi. When trying to build a gadget snap using the current master of https://github.com/snapcore/pi3-gadget, when trying to pull the v2017.05 branch of uboot the build fails with fatal: ambiguous argument 'refs/heads/v2017.05': unknown revision or path not in the working tree. That’s because v2017.05 is now a tag in the uboot repo, not a branch. Can be fixed by changing the source-branch: v2017.05 line in snapcraft.yaml to source-tag: v2017.05.
  • snap install ubuntu-image --beta --classic does not work (with various combinations of switches): the snap ubuntu-image does not appear to exist. What am I missing? That said, sudo apt install ubuntu-image seems to do the job.

@svet I could download ubunut-image with snap install command , I did that on a ubuntu 16.04 machine (not core). I agree with you on the installation part , I got the same error when trying to install the gadget)

uboot v2017.11 should also work. From v2018.01 uboot needs GCC 6 which is not available on Ubuntu 16.04.

On second thought, I’m assuming it’s because I was trying to run this on an armhf and not amd64 - and I guess the snap is just not available for the arch. But also yes - I was running this on a Ubuntu Core device (in classic mode), rather than than regular Ubuntu, so that may have been a problem also.

sadly not … but … if you change files that end up to live in the system-boot partition you can simply replace them directly in /boot/uboot/ from your gadget build (just scp them to your home on the pi and then sudo cp them to the dir) …

that way you only need to test the snap as a last step and only have to flash once … specifically for the pi3 i think the majority of files are actually in that dir somewhere …

if you change contents of uboot.env you can even use the shipped fw_setenv and fw_printenv commands to modify the bootloader config from the running system for testing.

Take a look at the prepare and build scriptlets in:

https://github.com/ogra1/nanopi-neo-gadget/blob/master/snapcraft.yaml

This uses a (admittedly a little gross but working) hack to download the latest linaro cross GCC to solve exactly this issue for the allwinner gadgets i maintain (they all need gcc6) …

So I followed your instructions to create an image file and wrote it to HDD of my disk. The device is failing to boot up with the following errors.

  1. error file '/system-data/var/lib/snapd/snaps/pc-kernel_102.snap' not found
  2. no server is specified
  3. alloc magic is broken

I build the gadget snap on Ubuntu 16.04.3 LTS and here is the modified snapcraft.yaml file (only the diff)

 diff --git a/snapcraft.yaml b/snapcraft.yaml
index bea8d36..89165b8 100644
--- a/snapcraft.yaml
+++ b/snapcraft.yaml
@@ -22,4 +22,16 @@ parts:
       - grub-common
     plugin: make
     after: [grub-prepare]
-
+slots:
+    com1:
+      interface: serial-port
+      path: /dev/ttyS0
+    com2:
+      interface: serial-port
+      path: /dev/ttyS4
+    com3:
+      interface: serial-port
+      path: /dev/ttyS5
+    com4:
+      interface: serial-port
+      path: /dev/ttyS2

I used xz to compress the build image file and used the below command to write tot the HDD xzcat <path to my img.xz> | sudo dd of=/dev/sda bs=32M; sync

I will give another try and see if that works.

Some more updates. I could build and flash the image but the pc snap I built is not appearing on the device. Also the behavior of snap commands is erratic. For .eg look at the output of snap list command executed .

admin@localhost:~$ sudo snap list
[sudo] password for admin:
No snaps are installed yet. Try "snap install hello-world".
admin@localhost:~$ sudo snap list
No snaps are installed yet. Try "snap install hello-world".
admin@localhost:~$ sudo snap list
No snaps are installed yet. Try "snap install hello-world".
admin@localhost:~$ sudo bash
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
root@localhost:~# snap list
Name  Version  Rev   Developer  Notes
core  16-2.31  4017  canonical  core
root@localhost:~# snap list
Name       Version        Rev   Developer  Notes
core       16-2.31        4017  canonical  core
pc-kernel  4.4.0-112.135  102   canonical  kernel
root@localhost:~# snap list
Name       Version        Rev   Developer  Notes
core       16-2.31        4017  canonical  core
pc-kernel  4.4.0-112.135  102   canonical  kernel
root@localhost:~# snap list
Name       Version        Rev   Developer  Notes
core       16-2.31        4017  canonical  core
pc         16.04-0.8      9     canonical  gadget
pc-kernel  4.4.0-112.135  102   canonical  kernel
root@localhost:~# snap list
Name       Version        Rev   Developer  Notes
core       16-2.31        4017  canonical  core
pc         16.04-0.8      9     canonical  gadget
pc-kernel  4.4.0-112.135  102   canonical  kernel
root@localhost:~# snap list
Name       Version        Rev   Developer  Notes
core       16-2.31        4017  canonical  core
pc-kernel  4.4.0-112.135  102   canonical  kernel
root@localhost:~# snap list
Name       Version        Rev   Developer  Notes
core       16-2.31        4017  canonical  core
pc-kernel  4.4.0-112.135  102   canonical  kernel

Note the pc snap version is still 16.01.0.8 where as the snap I built is pc_16.04-0.9_amd64.snap. Also snap interfaces is not showing the serial slots I have created.

If you seee it dis/enabled in snap list all the time, I suggest to take a look at snap changes (and if you find an error there, use: snap change <id of the change>

That should have some more details about what is going on.

You are correct, there are lot of errors wrt Initialize system state. Here is the summary of one such task.

Status  Spawn                 Ready                 Summary
Done    2018-02-20T14:10:14Z  2018-02-20T14:13:12Z  Ensure prerequisites for "core" are available
Undone  2018-02-20T14:10:14Z  2018-02-20T14:13:12Z  Prepare snap "/var/lib/snapd/seed/snaps/core_4017.snap" (4017)
Undone  2018-02-20T14:10:14Z  2018-02-20T14:13:09Z  Mount snap "core" (4017)
Undone  2018-02-20T14:10:14Z  2018-02-20T14:13:06Z  Copy snap "core" data
Undone  2018-02-20T14:10:14Z  2018-02-20T14:13:04Z  Setup snap "core" (4017) security profiles
Undone  2018-02-20T14:10:14Z  2018-02-20T14:13:02Z  Make snap "core" (4017) available to the system
Undone  2018-02-20T14:10:14Z  2018-02-20T14:13:00Z  Setup snap "core" (4017) security profiles (phase 2)
Undone  2018-02-20T14:10:14Z  2018-02-20T14:12:59Z  Set automatic aliases for snap "core"
Undone  2018-02-20T14:10:14Z  2018-02-20T14:12:56Z  Setup snap "core" aliases
Done    2018-02-20T14:10:14Z  2018-02-20T14:12:53Z  Run install hook of "core" snap if present
Undone  2018-02-20T14:10:14Z  2018-02-20T14:12:52Z  Start snap "core" (4017) services
Done    2018-02-20T14:10:18Z  2018-02-20T14:12:50Z  Ensure prerequisites for "pc-kernel" are available
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:49Z  Prepare snap "/var/lib/snapd/seed/snaps/pc-kernel_102.snap" (102)
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:46Z  Mount snap "pc-kernel" (102)
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:44Z  Copy snap "pc-kernel" data
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:42Z  Setup snap "pc-kernel" (102) security profiles
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:40Z  Make snap "pc-kernel" (102) available to the system
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:38Z  Setup snap "pc-kernel" (102) security profiles (phase 2)
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:36Z  Set automatic aliases for snap "pc-kernel"
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:33Z  Setup snap "pc-kernel" aliases
Done    2018-02-20T14:10:18Z  2018-02-20T14:12:30Z  Run install hook of "pc-kernel" snap if present
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:29Z  Start snap "pc-kernel" (102) services
Done    2018-02-20T14:10:18Z  2018-02-20T14:12:28Z  Ensure prerequisites for "pc" are available
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:27Z  Prepare snap "/var/lib/snapd/seed/snaps/pc_9.snap" (9)
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:24Z  Mount snap "pc" (9)
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:21Z  Copy snap "pc" data
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:19Z  Setup snap "pc" (9) security profiles
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:17Z  Make snap "pc" (9) available to the system
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:15Z  Setup snap "pc" (9) security profiles (phase 2)
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:13Z  Set automatic aliases for snap "pc"
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:10Z  Setup snap "pc" aliases
Done    2018-02-20T14:10:18Z  2018-02-20T14:12:08Z  Run install hook of "pc" snap if present
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:07Z  Start snap "pc" (9) services
Done    2018-02-20T14:10:14Z  2018-02-20T14:12:05Z  Run configure hook of "core" snap if present
Done    2018-02-20T14:10:18Z  2018-02-20T14:12:05Z  Run configure hook of "pc-kernel" snap if present
Done    2018-02-20T14:10:18Z  2018-02-20T14:12:04Z  Run configure hook of "pc" snap if present
Done    2018-02-20T14:10:18Z  2018-02-20T14:12:03Z  Ensure prerequisites for "dell5k" are available
Undone  2018-02-20T14:10:18Z  2018-02-20T14:12:02Z  Prepare snap "/var/lib/snapd/seed/snaps/dell5k_x1.snap" (unset)
Error   2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Mount snap "dell5k" (unset)
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Copy snap "dell5k" data
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Setup snap "dell5k" (unset) security profiles
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Make snap "dell5k" (unset) available to the system
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Setup snap "dell5k" (unset) security profiles (phase 2)
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Set automatic aliases for snap "dell5k"
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Setup snap "dell5k" aliases
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Run install hook of "dell5k" snap if present
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Start snap "dell5k" (unset) services
Hold    2018-02-20T14:10:18Z  2018-02-20T14:11:59Z  Run configure hook of "dell5k" snap if present
Hold    2018-02-20T14:10:11Z  2018-02-20T14:11:59Z  Mark system seeded

......................................................................
Mount snap "dell5k" (unset)

2018-02-20T14:11:59Z ERROR cannot replace signed gadget snap with an unasserted one

Any hints on how to resolve this error ?