I have a blink program. Compiling this source into a binary and the program blinks the LED successfully.
However, when packaged as a snap and installation in strict mode, with the gpio plug enabled, the program fails to blink the LED
Commands ran:
snap install blink_X_armhf.snap --dangerous
# This connect commands does not work at time of writing. This is just for reference
snap connect blink:gpio pi3:bcm-gpio-17
snap connect blink:gpio pi3:bcm-gpio-22
sudo blink 22 17
snapcraft.yml
name: blink
version: '1.0'
summary: Reads and writes to GPIO based on specified pins.
description: One read and one write pin are specified via the commandline arguments then read from or written to the pin.
grade: devel
confinement: strict
apps:
blink:
command: blink
plugs: [ gpio ]
parts:
blink:
source: .
plugin: make
Source for blink program
//Modified from from http://elinux.org/RPi_GPIO_Code_Samples#sysfs
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
static int
GPIOExport(int pin)
{
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int
GPIOUnexport(int pin)
{
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open unexport for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int
GPIODirection(int pin, int dir)
{
static const char s_directions_str[] = "in\0out";
#define DIRECTION_MAX 35
char path[DIRECTION_MAX];
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio direction for writing!\n");
return(-1);
}
if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) {
fprintf(stderr, "Failed to set direction!\n");
return(-1);
}
close(fd);
return(0);
}
static int
GPIORead(int pin)
{
#define VALUE_MAX 30
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for reading!\n");
return(-1);
}
if (-1 == read(fd, value_str, 3)) {
fprintf(stderr, "Failed to read value!\n");
return(-1);
}
close(fd);
return(atoi(value_str));
}
static int
GPIOWrite(int pin, int value)
{
static const char s_values_str[] = "01";
char path[VALUE_MAX];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for writing!\n");
return(-1);
}
if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
fprintf(stderr, "Failed to write value!\n");
return(-1);
}
close(fd);
return(0);
}
int
main(int argc, char *argv[])
{
if(argc < 3){
printf("Need 2 arguments, read pin and write pin like sudo ./blink 22 17\n");
return 1;
}
char * readPinStr = argv[1];
char * writePinStr = argv[2];
int readPin = strtol(readPinStr, NULL, 10);
if(readPin == 0){
printf("Read Pin cannot be derived\n");
return 1;
}
int writePin = strtol(writePinStr, NULL, 10);
if(writePin == 0){
printf("Write Pin cannot be derived\n");
return 1;
}
printf("Will read from Pin %d and write to Pin %d\n", readPin, writePin);
int repeat = 20;
/*
* Enable GPIO pins
*/
if (-1 == GPIOExport(writePin) || -1 == GPIOExport(readPin))
return(1);
/*
* Set GPIO directions
*/
if (-1 == GPIODirection(writePin, OUT) || -1 == GPIODirection(readPin, IN))
return(2);
do {
/*
* Write GPIO value
*/
if (-1 == GPIOWrite(writePin, repeat % 2))
return(3);
/*
* Read GPIO value
*/
printf("I'm reading %d in GPIO %d\n", GPIORead(readPin), readPin);
usleep(500 * 1000);
}
while (repeat--);
/*
* Disable GPIO pins
*/
if (-1 == GPIOUnexport(writePin) || -1 == GPIOUnexport(readPin))
return(4);
return(0);
}