rendered paste body#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/gpio.h>
#include <net/if.h>
#include <net/bpf.h>
#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sysexits.h>
#include <util.h>
#include <unistd.h>
#define SNAPLEN 64
//
static int devfd = -1;
static int led = 0;
static int led_on_val = GPIO_PIN_HIGH;
static int led_off_val = GPIO_PIN_LOW;
static char *device = "/dev/gpio0";
extern char *__progname;
static void usage(char *device, int to_ms)
{
fprintf(stderr, "\nUsage: %s [options]\n"
" Options:\n"
" -f foreground [off]\n"
" -d device gpio device [%s]\n"
" -t milliseconds minimum period to keep LED active [%d]\n"
" -n don't monopolize gpio device\n"
" -r invert LED behavior\n"
" -P write pidfile [off]\n"
" -B basename pidfile basename (implies -P) [%s]\n"
" Version: 0.1\n",
__progname, device, to_ms, __progname);
exit(EX_USAGE);
}
#define LED_STD 1
#define LED_SIG 2
static int (*gpio_ioctl)(int, unsigned long, ...) = NULL;
static int my_ioctl(int d, unsigned long request, ...)
{
int res = d = open(device, O_RDWR);
if (d > -1)
{
va_list ap;
va_start(ap, request);
res = ioctl(d, request, va_arg(ap, struct gpio_pin_op *));
close (d);
va_end(ap);
}
return res;
}
static void mod_led(gpio_pin_op led, int led_state)
{
if(led_state == 0) {
led.gp_value = led_on_val;
}
else {
led.gp_value = led_off_val;
}
gpio_ioctl(devfd, GPIOPINWRITE, &led);
}
static void bye(int unused)
{
exit(-1);
}
int main(int argc, char **argv)
{
char *pid_base = __progname;
char buf[SNAPLEN];
int ch, r;
int foreground = 0;
int opt_ms = 100;
int opt_pidfile = 0;
int initial = -1;
struct gpio_pin_op op[8];
gpio_ioctl = ioctl;
memset(&op, 0, sizeof(op));
op[0].gp_pin = 9; /* suitable for Soekris Net4501 */
while ((ch = getopt(argc, argv, "d:fhi:p:t:PB:I:rn")) != -1) {
switch (ch) {
case 'd':
device = optarg;
break;
case 'f':
foreground = 1;
break;
//case 'p':
//op.gp_pin = atoi(optarg);
//break;
case 't':
opt_ms = atoi(optarg);
break;
//case 'I':
//initial = atoi(optarg);
//break;
case 'B':
pid_base = optarg;
case 'P':
opt_pidfile = 1;
break;
case 'n':
gpio_ioctl = my_ioctl;
break;
case 'r':
led_on_val = GPIO_PIN_LOW;
led_off_val = GPIO_PIN_HIGH;
break;
default:
usage(device, opt_ms);
}
}
if (argc > optind)
usage(device, opt_ms);
if ((gpio_ioctl == ioctl) && (devfd = open(device, O_RDWR)) < 0)
err(EX_IOERR, "%s", device);
if (!foreground)
daemon(1, 0);
if (opt_pidfile)
pidfile(pid_base);
signal(SIGQUIT, bye);
signal(SIGHUP, bye);
signal(SIGTERM, bye);
while(1) {
}
if (devfd > -1)
close(devfd);
exit(EX_IOERR);
}