Uit Hack42
Ga naar: navigatie, zoeken
Project: ESpaceState
Schroefje24.png
Schroefje24.png
Schroefje24.png
Schroefje24.png
ESpaceState Picture.jpg

ESpaceState

Naam ESpaceState
Door BugBlue
Status Uitvoer
Madskillz solderen, C coden, GPIO, docs lezen, schroeven, spijkeren, debuggen
Doel / Omschrijving
GPIO switches misbruiken om status van de space te detecten
Alle Projecten - Project Toevoegen
File:ESpaceState_Picture.jpg noez

De Alix.1C barPC heeft mplayer, aplay, en 21 GPIO switches pins. Deze pins kun je misbruiken om hier en daar wat uit te lezen als je er een switch tussen zet.

Op dit moment zijn er 2 deuren voorzien van microswitches en 1 deur van bekabeling hiernaartoe.

De code draait als root op de barPC omdat het iets van privs nodig heeft op I/O ports. Chip docs op http://www.itox.com/pages/support/wdt/W83627HF.pdf

Code or it didn't happen

/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
/* TODO:
   * stupid output from scripts (or failure to start) to /dev/null
   * Get rid of BugBlue signatures, they are ugly
*/
#include <stdint.h>
#include <sys/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

#define		PORT_INDEX	0x2e
#define		PORT_DATA	0x2f
#define		DEBUG		0

typedef unsigned char BYTE;
struct tm *local;

const char* toBin(const BYTE c)
{
	static char buff[9]="";
	char *pBuff = buff;
	BYTE mask=128;

	for(;mask;mask>>=1) 
		*pBuff++ = (int) (mask & c ? "1" : "0");
	return buff;
}

void init()
{
	outb_p(0x87, PORT_INDEX);
	outb_p(0x87, PORT_INDEX);
}

void done()
{
	outb_p(0xAA, PORT_INDEX);
}

void set(BYTE idx, BYTE val)
{
	outb_p(idx, PORT_INDEX);
	outb_p(val, PORT_DATA);
}

BYTE get(BYTE idx)
{
	outb_p(idx, PORT_INDEX);
	return inb_p(PORT_DATA);
}

/* Time to move away */
void leave(int sig) 
{
	done();
	exit(sig);
}

// now do the damn thing
BYTE action(BYTE ot,BYTE byte,int bit,int pin,char *desc) 
{
        char cmd[50];
	int tmp = bit & byte;
  	if(tmp==0 && ot==0) {
		printf("%s: %s is open\n",asctime(local),desc);
		sprintf(cmd,"./%d-open.sh",pin);
		ot=1;
	} else if(tmp!=0 && ot==1) {
		printf("%s: %s is closed\n",asctime(local),desc);
		sprintf(cmd,"./%d-close.sh",pin);
		ot=0;
        }
        cmd[49]=0;
	pid_t childpid = fork();
	if (childpid >= 0) { /* we created a child (or is it called smurf) */
		if (childpid == 0) { /* I'm tha smurf */
			iopl(0); /* still running as root, but don't need the privs anymore */
			exit(system(cmd));  // do your stuff and die
		} else { /* I'm your daddy */
			/* totally useless structure, but I wanted to type the above line */
		}
	}
	return ot;
}

int main(int nargs, char **argv)
{
	BYTE byte = 0;
	time_t t;
	t = time(NULL);

	(void) signal(SIGKILL,leave);

	iopl(3);
	
	init();
	
	// check if OK - must be 0x52
	byte = get(0x20);
	if(DEBUG) printf("0x52 = [0x%02x] (%s)\n", byte, toBin(byte));
	
	// check if GPIO11 is active
	byte = get(0x2a);
	if(DEBUG) printf("CR2A = [0x%02x] (%s)\n", byte, toBin(byte));
	
	// set (and get) logical device to #7 - GPIO1 (GP10-17)
	// GPIO next = #8 (GP20-26)
	// GPIO next = #9 (GP30-35)
	const BYTE devIdx = 0x07;
	set(devIdx, 0x07);
	byte = get(devIdx);
	if(DEBUG) printf("LOGICAL DEVICE# = [0x%02x] (%s)\n", byte, toBin(byte));
	
	// check GPIO1 functions [1:input, 0:output]
	const BYTE funcIdx = 0xf0;
	byte = get(funcIdx);
	if(DEBUG) printf("FUNCTION = [0x%02x] (%s)\n", byte, toBin(byte));

	// GPIO1 data
	// Natuurlijk is GP10-17 (1 = open) GP20-26 (0=open) en GP30-35 (0=open)
	const BYTE dataIdx = 0xf1;
	BYTE ot1=0,ot2=0,ot3=0,ot4=0,ot5=0,ot6=0,ot7=0,ot8=0;
	while(1) {
		byte = get(dataIdx);
		local = localtime(&t);
		if(DEBUG) printf("DATA = [0x%02x] (%s)\n", byte, toBin(byte));	
		ot1=action(ot1,byte,1,1,"Voor-tussendeur");
		ot2=action(ot2,byte,2,2,"Achter-tussendeur");
		ot3=action(ot3,byte,4,3,"GPIO12");
		ot4=action(ot4,byte,8,4,"GPIO13");
		ot5=action(ot5,byte,16,5,"GPIO14");
		ot6=action(ot6,byte,32,6,"GPIO15");
		ot7=action(ot7,byte,64,7,"GPIO16");
		ot8=action(ot8,byte,128,8,"GPIO17");
		usleep(200000);
	}
	
	exit(1); // WTF do we do here?
}