PiPower  ac0ff3c (Mon Feb 11 21:44:30 2019 -0500)
Data Structures | Macros | Functions | Variables
pipowerd.c File Reference

Monitor GPIO for the SHUTDOWN signal. More...

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <linux/gpio.h>

Data Structures

struct  config
 Holds our global configuration. More...
 

Macros

#define _POSIX_C_SOURCE   200809L
 
#define DEFAULT_GPIO_DEV   "/dev/gpiochip0"
 On which gpio device is our pin of interest?
 
#define DEFAULT_PIN   17
 GPIO pin to monitor for the shutdown signal.
 
#define DEFAULT_SHUTDOWN_COMMAND   "/bin/systemctl poweroff"
 Command to run when a shutdown signal is received.
 
#define OPT_GPIO_DEV   'd'
 --device|-d <device>
 
#define OPT_PIN   'p'
 --pin|-p <pin>
 
#define OPT_SHUTDOWN_COMMAND   'c'
 --shutdown-command|-c <command>
 
#define OPT_VERBOSE   'v'
 --verbose|-v (may be specified multiple times)
 
#define OPT_IGNORE_INITIAL_STATE   'i'
 --ignore-initial-state|-i
 
#define OPT_HELP   'h'
 --help|-h
 
#define OPTSTRING   "d:p:c:vih"
 Valid single character options.
 

Functions

void init_config ()
 Initialize global configuration with default values.
 
void usage (FILE *out)
 Display a usage message.
 
void monitor_shutdown_pin ()
 Loop until we detect a shutdown request. More...
 
void parse_args (int argc, char *argv[])
 Handle command line options.
 
int main (int argc, char *argv[])
 

Variables

const struct option longopts []
 Configure options handling. More...
 
struct config config
 

Detailed Description

Monitor GPIO for the SHUTDOWN signal.

Function Documentation

void monitor_shutdown_pin ( )

Loop until we detect a shutdown request.

First check the initial state of the shutdown pin. If a shutdown request has been asserted, exit immediately unless --ignore-initial-state was provided. If there is no active shutdown request, monitor the pin for rising edge events and exit when one is received.

Variable Documentation

const struct option longopts[]
Initial value:
= {
{"gpio-device", required_argument, 0, OPT_GPIO_DEV},
{"gpio-pin", required_argument, 0, OPT_PIN},
{"shutdown-command", required_argument, 0, OPT_SHUTDOWN_COMMAND},
{"ignore-initial-state", required_argument, 0, OPT_IGNORE_INITIAL_STATE},
{"verbose", no_argument, 0, OPT_VERBOSE},
{"help", no_argument, 0, OPT_HELP},
}
#define OPT_PIN
--pin|-p <pin>
Definition: pipowerd.c:39
#define OPT_IGNORE_INITIAL_STATE
--ignore-initial-state|-i
Definition: pipowerd.c:42
#define OPT_SHUTDOWN_COMMAND
--shutdown-command|-c <command>
Definition: pipowerd.c:40
#define OPT_VERBOSE
--verbose|-v (may be specified multiple times)
Definition: pipowerd.c:41
#define OPT_HELP
--help|-h
Definition: pipowerd.c:43
#define OPT_GPIO_DEV
--device|-d <device>
Definition: pipowerd.c:38

Configure options handling.