Uit Hack42
Ga naar: navigatie, zoeken
k
k (Lurwah heeft pagina Gebuiker:BugBlue/ESpaceState hernoemd naar Gebruiker:BugBlue/ESpaceState)
 
(15 tussenliggende versies door 2 gebruikers niet weergegeven)
Regel 6: Regel 6:
 
|Samenvatting=GPIO switches misbruiken om status van de space te detecten
 
|Samenvatting=GPIO switches misbruiken om status van de space te detecten
 
}}
 
}}
 +
 
De Alix.1C barPC heeft mplayer, aplay, en 21 GPIO switches pins. Deze pins kun je misbruiken om hier  
 
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.
 
en daar wat uit te lezen als je er een switch tussen zet.
Regel 15: Regel 16:
 
=== Code or it didn't happen ===
 
=== Code or it didn't happen ===
 
<pre>
 
<pre>
/* ALIX 1C GPIO reader and handler. Contains bugs. Guaranteed! */
+
#include <stdint.h>
/* TODO:
 
  * stupid output from scripts (or failure to start) to /dev/null
 
  * Get rid of BugBlue signatures, they are ugly
 
*/
 
 
 
 
#include <sys/io.h>
 
#include <sys/io.h>
 
#include <stdio.h>
 
#include <stdio.h>
Regel 27: Regel 23:
 
#include <unistd.h>
 
#include <unistd.h>
 
#include <signal.h>
 
#include <signal.h>
 +
#include <sys/wait.h>
  
#define PORT_INDEX 0x2e
+
#define         PORT_INDEX     0x2e
#define PORT_DATA 0x2f
+
#define         PORT_DATA       0x2f
#define DEBUG 0
+
#define         DEBUG           0
  
 
typedef unsigned char BYTE;
 
typedef unsigned char BYTE;
Regel 37: Regel 34:
 
const char* toBin(const BYTE c)
 
const char* toBin(const BYTE c)
 
{
 
{
static char buff[9]="";
+
        static char buff[9]="       "; // auto 0 termination++
char *pBuff = buff;
+
        BYTE bit;                       // always wanted to do that.
unsigned mask = 128;
+
        for(bit=0;bit<8;bit++)
 
+
                buff[7-bit]=(c>>bit) & 1 ? '1' : '0'; // eigtdoc
for(;mask;mask>>=1) {
+
        return buff;
*pBuff++ = mask & c ? "1" : "0";
 
}
 
return buff;
 
 
}
 
}
  
 
void init()
 
void init()
 
{
 
{
outb_p(0x87, PORT_INDEX);
+
        outb_p(0x87, PORT_INDEX);
outb_p(0x87, PORT_INDEX);
+
        outb_p(0x87, PORT_INDEX);
 
}
 
}
  
 
void done()
 
void done()
 
{
 
{
outb_p(0xAA, PORT_INDEX);
+
        outb_p(0xAA, PORT_INDEX);
 
}
 
}
  
 
void set(BYTE idx, BYTE val)
 
void set(BYTE idx, BYTE val)
 
{
 
{
outb_p(idx, PORT_INDEX);
+
        outb_p(idx, PORT_INDEX);
outb_p(val, PORT_DATA);
+
        outb_p(val, PORT_DATA);
 
}
 
}
  
 
BYTE get(BYTE idx)
 
BYTE get(BYTE idx)
 
{
 
{
outb_p(idx, PORT_INDEX);
+
        outb_p(idx, PORT_INDEX);
return inb_p(PORT_DATA);
+
        return inb_p(PORT_DATA);
 
}
 
}
  
 
/* Time to move away */
 
/* Time to move away */
void leave(int sig) {
+
void leave(int sig)  
done();
+
{
exit(sig);
+
        done();
 +
        exit(sig);
 
}
 
}
  
 
// now do the damn thing
 
// now do the damn thing
BYTE action(BYTE ot,BYTE byte,int bit,int pin,char *desc) {
+
BYTE action(BYTE ot,BYTE byte,int bit,int pin,char *desc)  
 +
{
 
         char cmd[50];
 
         char cmd[50];
int tmp = bit & byte;
+
        int tmp = bit & byte;
  if(tmp==0 && ot==0) {
+
        int docmd=0;
printf("%s: %s is open\n",asctime(local),desc);
+
        if(tmp==0 && ot==0) {
sprintf(cmd,"./%d-open.sh",pin);
+
                printf("%s: %s is open\n",asctime(local),desc);
} else if(tmp!=0 && ot==1) {
+
                sprintf(cmd,"./%d-open.sh",pin);
printf("%s: %s is closed\n",asctime(local),desc);
+
                ot=1;
sprintf(cmd,"./%d-close.sh",pin);
+
                docmd=1;
 +
        } else if(tmp!=0 && ot==1) {
 +
                printf("%s: %s is closed\n",asctime(local),desc);
 +
                sprintf(cmd,"./%d-close.sh",pin);
 +
                ot=0;
 +
                docmd=1;
 
         }
 
         }
         cmd[49]=0;
+
         if(docmd) {
pid_t childpid = fork();
+
                cmd[49]=0;
if (childpid >= 0) { /* we created a child (or is it called smurf) */
+
                pid_t childpid = fork();
if (childpid == 0) { /* I'm tha smurf */
+
                if (childpid >= 0) { /* we created a child (or is it called smurf) */
iopl(0); /* still running as root, but don't need the privs anymore */
+
                        if (childpid == 0) { /* I'm tha smurf */
exit(system(cmd));  // do your stuff and die
+
                                iopl(0); /* still running as root, but don't need the privs anymore */
} else { /* I'm your daddy */
+
                                exit(system(cmd));  // do your stuff and die
/* totally useless structure, but I wanted to type the above line */
+
                        } else { /* I'm your daddy */
}
+
                                /* totally useless structure, but I wanted to type the above line */
}
+
                        }
return ot;
+
                }
 +
        }
 +
        return ot;
 
}
 
}
  
 
int main(int nargs, char **argv)
 
int main(int nargs, char **argv)
 
{
 
{
BYTE byte = 0;
+
        BYTE byte = 0;
time_t t;
+
        time_t t;
t = time(NULL);
+
        t = time(NULL);
 +
        pid_t w;
 +
        int status;
  
(void) signal(SIGKILL,leave);
+
        (void) signal(SIGKILL,leave);
  
iopl(3);
+
        iopl(3);
+
 
init();
+
        init();
+
 
// check if OK - must be 0x52
+
        if(DEBUG) {
byte = get(0x20);
+
                // check if OK - must be 0x52
if(DEBUG) printf("0x52 = [0x%02x] (%s)\n", byte, toBin(byte));
+
                byte = get(0x20);
+
                printf("0x52 = [0x%02x] (%s)\n", byte, toBin(byte));
// check if GPIO11 is active
+
 
byte = get(0x2a);
+
                // check if GPIO11 is active
if(DEBUG) printf("CR2A = [0x%02x] (%s)\n", byte, toBin(byte));
+
                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)
+
        // set (and get) logical device to #7 - GPIO1 (GP10-17)
// GPIO next = #9 (GP30-35)
+
        // GPIO next = #8 (GP20-26)
const BYTE devIdx = 0x07;
+
        // GPIO next = #9 (GP30-35)
set(devIdx, 0x07);
+
        const BYTE devIdx = 0x07;
byte = get(devIdx);
+
        set(devIdx, 0x07);
if(DEBUG) printf("LOGICAL DEVICE# = [0x%02x] (%s)\n", byte, toBin(byte));
+
        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;
+
        // check GPIO1 functions [1:input, 0:output]
byte = get(funcIdx);
+
        const BYTE funcIdx = 0xf0;
if(DEBUG) printf("FUNCTION = [0x%02x] (%s)\n", byte, toBin(byte));
+
        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);
 +
                w = waitpid(-1, &status, WUNTRACED | WCONTINUED);
 +
        }
  
// GPIO1 data
+
        exit(1); // WTF do we do here?
// 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?
 
 
}
 
}
 
</pre>
 
</pre>

Huidige versie van 16 mei 2024 om 17:02

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

#include <stdint.h>
#include <sys/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.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]="        "; // auto 0 termination++
        BYTE bit;                       // always wanted to do that.
        for(bit=0;bit<8;bit++)
                buff[7-bit]=(c>>bit) & 1 ? '1' : '0'; // eigtdoc
        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;
        int docmd=0;
        if(tmp==0 && ot==0) {
                printf("%s: %s is open\n",asctime(local),desc);
                sprintf(cmd,"./%d-open.sh",pin);
                ot=1;
                docmd=1;
        } else if(tmp!=0 && ot==1) {
                printf("%s: %s is closed\n",asctime(local),desc);
                sprintf(cmd,"./%d-close.sh",pin);
                ot=0;
                docmd=1;
        }
        if(docmd) {
                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);
        pid_t w;
        int status;

        (void) signal(SIGKILL,leave);

        iopl(3);

        init();

        if(DEBUG) {
                // check if OK - must be 0x52
                byte = get(0x20);
                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);
                w = waitpid(-1, &status, WUNTRACED | WCONTINUED);
        }

        exit(1); // WTF do we do here?
}