How do I utilize the hidraw plug on a pi running ubuntu core 18?

I’m have snap that communicate with a custom hid device. The app contained within the snap uses nodejs and the library I’m using is ‘node-hid’. Note that ‘node-hid’ uses the hidapi internally.

My app is running as a daemon and gives this error when I try to start it:

 cannot initialize hidapi (hid_init failed)

I know that the nodejs app works on its own with no problems (as long as a specify sudo) but I cant seem to figure how to connect to hidraw plug.

I have also tried the raw-usb plug but that as not worked as well.
Running installing the snap in devmode works so that’s promising.

Here is a the yaml file:

grade: stable # must be 'stable' to release into candidate/stable channels
confinement: strict # use 'strict' once you have the right plugs and slots

apps:
  hid-demo:
    command: bin/hid-demo
    plugs: [hidraw, network, raw-usb]
    daemon: simple
    restart-condition : always

parts:
  hid-demo:
    plugin: nodejs
    stage-packages: [libusb-1.0-0]
    override-pull: | 
      echo "Configure linux dependencies"
      apt-get install build-essential git
      apt-get install gcc-4.8 g++-4.8 && export CXX=g++-4.8
      apt-get install libusb-1.0-0 libusb-1.0-0-dev
      apt-get install -y pkg-config
      snapcraftctl pull
    source: .

instead of all the apt hackery in override-pull you should use the build-packages directive and simply list all the packages required to be pulled … along with this to get the HID api into your snap you should list libhidapi-hidraw0 and probably also libhidapi-libusb0 in stage-packages

1 Like

Thanks I’ll give your suggestions a try!

I have ubuntu server 20.04 on a Pi. When I run my snap and look into journalctl I see the following

`Log: apparmor=“DENIED” operation=“open” profile=“snap.mydaemon.mydaemon” name="/dev/hidraw0" pid=13038 comm=“node” requested_mask=“wr” denied_mask=“wr” fsuid=0 ouid=0
File: /dev/hidraw0 (write)
Suggestion:

  • add ‘hidraw (with gadget or core support)’ to ‘plugs’`

and if I run sudo snap connect mydaemon:hidraw
it gives me the following error:

error: snap "snapd" has no "hidraw" interface slots

Can you help me with this please? I included my yaml for your reference.

confinement: strict
base: core20
grade: stable

parts:
mydaemon:
plugin: npm
npm-node-version: 10.19.0
source: .
stage-packages:
- libusb-1.0-0
- libasound2
- libasound2-data
- libatomic1
- libjack0
- libportaudio2
- libhidapi-hidraw0
- libhidapi-libusb0

apps:

mydaemon:
command: myexecutable
daemon: simple
plugs:
- network
- network-bind
- hardware-observe
- raw-usb
- hidraw

The classic images (server/desktop) do not come with a gadget snap which could provide this interface … you’d need to build your own classic server image with a gadget (or simply use Ubuntu Core)

1 Like