/*
GPIO HALT: monitors a single GPIO pin, initiates orderly shutdown.
Similar functionality to the rpi_power_switch kernel module from the
fbtft project, but easier to compile (no kernel headers needed).

Connect button between any GND pin (there are several on the GPIO header)
and the GPIO pin of interest.  Internal pullup is used; no resistors needed.
By default GPIO21 is used; this and GND are the last pins on the Model B+
GPIO (40 pin) header, so it's very easy to plug in a button quick-connect.
Different pin can be specified on the command line or by editing the code.
Avoid pins 8 and 10; these are configured as a serial port by default on
most systems (this can be disabled but takes some doing).

To run automatically at startup, move the executable to /usr/local/bin and
edit /etc/rc.local, inserting this one line before the final 'exit 0':

/usr/local/bin/gpio-halt &

An alternate pin number can optionally be specified before the '&'

This is mostly just a pared-down 'retrogame' from the Cupcade project.

Written by Phil Burgess for Adafruit Industries, distributed under BSD
License.  Adafruit invests time and resources providing this open source
code, please support Adafruit and open-source hardware by purchasing
products from Adafruit!


Copyright (c) 2014 Adafruit Industries.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <sys/mman.h>
#include <bcm_host.h>

// A few globals ---------------------------------------------------------

char
  *progName,                         // Program name (for error reporting)
   sysfs_root[] = "/sys/class/gpio", // Location of Sysfs GPIO files
   running      = 1;                 // Signal handler will set to 0 (exit)
int
   pin          = 21;                // Shutdown pin # (override w/argv)
volatile unsigned int
  *gpio;                             // GPIO register table
int
   debounceTime = 20;                // 20 ms for button debouncing


// Some utility functions ------------------------------------------------

// Set one GPIO pin attribute through the Sysfs interface.
int pinConfig(char *attr, char *value) {
	char filename[50];
	int  fd, w, len = strlen(value);
	sprintf(filename, "%s/gpio%d/%s", sysfs_root, pin, attr);
	if((fd = open(filename, O_WRONLY)) < 0) return -1;
	w = write(fd, value, len);
	close(fd);
	return (w != len); // 0 = success
}

// Un-export Sysfs pins; don't leave filesystem cruft.  Write errors are
// ignored as pins may be in a partially-initialized state.
void cleanup() {
	char buf[50];
	int  fd;
	sprintf(buf, "%s/unexport", sysfs_root);
	if((fd = open(buf, O_WRONLY)) >= 0) {
		sprintf(buf, "%d", pin);
		write(fd, buf, strlen(buf));
		close(fd);
	}
}

// Quick-n-dirty error reporter; print message, clean up and exit.
void err(char *msg) {
	printf("%s: %s.  Try 'sudo %s'.\n", progName, msg, progName);
	cleanup();
	exit(1);
}

// Interrupt handler -- set global flag to abort main loop.
void signalHandler(int n) {
	running = 0;
}

// Detect Pi board type.  Not detailed, just enough for GPIO compatibilty:
// true  = Pi 1 Model B revision 1 (some GPIO pin numbers were different)
// false = All other board types
static bool earlyPiDetect(void) {
	FILE *fp;
	char  buf[1024], *ptr;
	int   n;
	bool  isEarly = false; // Assume "modern" Pi by default

	// Relies on info in /proc/cmdline.  If this becomes unreliable
	// in the future, alt code below uses /proc/cpuinfo if any better.
#if 1
	if((fp = fopen("/proc/cmdline", "r"))) {
		while(fgets(buf, sizeof(buf), fp)) {
			if((ptr = strstr(buf, "boardrev=")) &&
			          (sscanf(&ptr[9], "%x", &n) == 1) &&
			          ((n == 0x02) || (n == 0x03))) {
				isEarly = true; // Appears to be an early Pi
				break;
			}
		}
		fclose(fp);
	}
#else
	char s[8];
	if((fp = fopen("/proc/cpuinfo", "r"))) {
		while(fgets(buf, sizeof(buf), fp)) {
			if((ptr = strstr(buf, "Revision")) &&
			          (sscanf(&ptr[8], " : %x", &n) == 1) &&
			          ((n == 0x02) || (n == 0x03))) {
				isEarly = true; // Appears to be an early Pi
				break;
			}
		}
		fclose(fp);
	}
#endif

	return isEarly;
}

// Main stuff ------------------------------------------------------------

#define GPIO_BASE  0x200000
#define BLOCK_SIZE (4*1024)
#define GPPUD      (0x94 / 4)
#define GPPUDCLK0  (0x98 / 4)

int main(int argc, char *argv[]) {

	char                   buf[50],      // For sundry filenames
	                       c;            // Pin input value ('0'/'1')
	int                    fd,           // For mmap, sysfs, uinput
	                       timeout = -1, // poll() timeout
	                       pressed;      // Last-read pin state
	volatile unsigned char shortWait;    // Delay counter
	struct pollfd          p;            // GPIO file descriptor

	progName = argv[0];             // For error reporting
	signal(SIGINT , signalHandler); // Trap basic signals (exit cleanly)
	signal(SIGKILL, signalHandler);

	if(argc > 1) pin = atoi(argv[1]);
	// Second argument is the time (in milliseconds) that the button
    	// should be kept pressed for shutdown to start
    	if(argc > 2) debounceTime = debounceTime + atoi(argv[2]);

	// If this is a "Revision 1" Pi board (no mounting holes),
	// remap certain pin numbers for compatibility.
	if(earlyPiDetect()) {
		if(     pin ==  2) pin = 0;
		else if(pin ==  3) pin = 1;
		else if(pin == 27) pin = 21;
	}

	// ----------------------------------------------------------------
	// Although Sysfs provides solid GPIO interrupt handling, there's
	// no interface to the internal pull-up resistors (this is by
	// design, being a hardware-dependent feature).  It's necessary to
	// grapple with the GPIO configuration registers directly to enable
	// the pull-ups.  Based on GPIO example code by Dom and Gert van
	// Loo on elinux.org

	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0)
		err("Can't open /dev/mem");
	gpio = mmap(            // Memory-mapped I/O
	  NULL,                 // Any adddress will do
	  BLOCK_SIZE,           // Mapped block length
	  PROT_READ|PROT_WRITE, // Enable read+write
	  MAP_SHARED,           // Shared with other processes
	  fd,                   // File to map
          bcm_host_get_peripheral_address() + GPIO_BASE); // -> GPIO registers
	close(fd);              // Not needed after mmap()
	if(gpio == MAP_FAILED) err("Can't mmap()");
	gpio[GPPUD]     = 2;                    // Enable pullup
	for(shortWait=150;--shortWait;);        // Min 150 cycle wait
	gpio[GPPUDCLK0] = 1 << pin;             // Set pullup mask
	for(shortWait=150;--shortWait;);        // Wait again
	gpio[GPPUD]     = 0;                    // Reset pullup registers
	gpio[GPPUDCLK0] = 0;
	(void)munmap((void *)gpio, BLOCK_SIZE); // Done with GPIO mmap()

	// ----------------------------------------------------------------
	// All other GPIO config is handled through the sysfs interface.

	sprintf(buf, "%s/export", sysfs_root);
	if((fd = open(buf, O_WRONLY)) < 0) // Open Sysfs export file
		err("Can't open GPIO export file");
	sprintf(buf, "%d", pin);
	write(fd, buf, strlen(buf));  // Export pin
	pinConfig("active_low", "0"); // Don't invert
	// Set pin to input, detect rise+fall events
	if(pinConfig("direction", "in") ||
	   pinConfig("edge"     , "both"))
		err("Pin config failed");
	// Get initial pin value
	sprintf(buf, "%s/gpio%d/value", sysfs_root, pin);
	if((p.fd = open(buf, O_RDONLY)) < 0)
		err("Can't access pin value");
	pressed = 0;
	if((read(p.fd, &c, 1) == 1) && (c == '0')) pressed = 1;
	p.events  = POLLPRI; // Set up poll() events
	p.revents = 0;
	close(fd); // Done exporting

	// ----------------------------------------------------------------
	// Monitor GPIO file descriptor for button events.  The poll()
	// function watches for GPIO IRQs in this case; it is NOT
	// continually polling the pins!  Processor load is near zero.

	while(running) { // Signal handler can set this to 0 to exit
		// Wait for IRQ on pin (or timeout for button debounce)
		if(poll(&p, 1, timeout) > 0) { // If IRQ...
			if(p.revents) { // Event received?
				// Read current pin state, store in
				// 'pressed' state flag, but don't halt
				// yet -- must wait for debounce!
				lseek(p.fd, 0, SEEK_SET);
				read(p.fd, &c, 1);
				if(c == '0')      pressed = 1;
				else if(c == '1') pressed = 0;
				p.revents = 0; // Clear flag
			}
			timeout = debounceTime; // Set timeout for debounce
			// Else timeout occurred
		} else if(timeout == debounceTime) { // Button debounce timeout
			if(pressed) {
				(void)system("shutdown -h now");
				running = 0;
			}
		}
	}

	// ----------------------------------------------------------------
	// Clean up

	cleanup(); // Un-export pins

	puts("Done.");

	return 0;
}
