The serial-port interface

Hey there,

A customer had some questions regarding how serial-port interfaces (and generally other IO-oriented ones) auto-connect. I wrote this. I’m pasting it here in case some of this information is useful to someone else; and it’d be awesome if some of this seems generically useful and could be added to the official documentation for this and/or other interfaces.


For IO interfaces like serial, gpio and i2c, our convention (in a brand store context) is to drive them from the gadget snap instead of from the connecting snap. This is more robust and allows the gadget snap (the one providing the slot) to centralize and arbitrate the conditions under which other snaps (identified by snap ID) can connect to the slots the gadget offers.

For these interfaces, then, usually no change is required in the plugging snap’s snap declaration. The only thing the plugging snap needs to do is declare and use an appropriately-configured plug in snap.yaml.

All the changes in the snap declaration are done in the slot-offering snap (usually a gadget snap). You’ll notice that the snap declaration indicates which snaps are allowed to auto-connect, assuming the corresponding plug matches the indicated attributes:


serial-port:
    allow-auto-connection:
      -
        on-store:
          - (whatever)
        plug-names:
          - serial-foo
        plug-snap-id:
          - foooVbn5YriRw2sRVw7Cuj5PbjJjwnFb
        slot-attributes:
          path: /dev/whatever
        slot-names:
          - serial-foo

Note that all attributes must match for an auto-connection attempt to be successful. In this case we are scoping by plug-snap-id, indicating the attributes of the candidate slot, and also the name of the plug that will be connected to this slot.

A possible reason if this is not auto-connecting is because the plugging snap is using the “serial-port” plug name which implicitly uses the “serial-port” interface, whereas it should declare instead a specific plug that contains the attributes that can match the slot rule above. You would need to apply these changes to the snap.yaml file (you typically make these changes in the snapcraft.yaml file and then rebuild the snap).

For example, this won’t work:

apps:
  whatever:
     plugs:
        serial-port

But this will:

apps:
  whatever:
     plugs:
        serial-foo
plugs:
   serial-foo:
      interface: serial-port

As for when a snap-declaration-driven auto-connection is processed: All auto-connections that are configured in snap-declarations, be it on the slot side or the plug side, are always processed when the snap is installed or refreshed. Strictly speaking, they are processed when a just-installed or refreshed snap-declaration is obtained by the device (even if the snap revision itself did not change). This means that they do not require a reboot to be connected. You should only need to "snap refresh " the gadget and application snap for the changes to apply.

The counterpart to this are auto-connections driven from the gadget snap’s connections section as described here: Gadget snaps. Those only auto-connect on first boot, but that is not what we are doing here.

  • Daniel
1 Like