Hey there, Im currently trying to create a device that turns off the operating system through a parrallel port device connected to the usb port on the motherboard.

Basically I have 2 programs, a readusbdong.cpp file which reads the dongle value and returns this value as nDongleValue. I also have another file named poweroff.c which basically works with the original parrallel port on the linux machine and switches off the operating system when the user clicks the switch connected to the parrallel port. The readusbdong.cpp file originally works on Windows which is why I have a readusbdong.cpp file. I guess my main problem is editing this program to work with Linux.

The reason I need a combination of the above is because my new machine doesnt have an active parrallel port, which is why I have created a usb adapter to link the parrallel input to the usb port on my motherboard. I've attached the 2 files below. I am also using the GCC Complier for my work.

Please help as I have never done any work with Linux before and I feel I am so close to coming up with an answer, just need a little help to get me over the finish line! Thanks

readusbdong.cpp:

Code:
// readusbdong.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <windows.h>
#include "ftd2xx.h" // Needs to go after others

using namespace std;

// Function Prototypes
void showUsage();

// Some global variables
bool bMask(false);            // Should we return all 8 bits?
bool bEcho(false);            // Should we also echo to screen?

int main(int argc, char* argv[])
{
#define READ_COMMAND 0x80
#define READ_LENGTH  4
#define WRITE_LENGTH 1


    // First see if we have any switches
    int i=1;
    while (i < argc)
    {
        if (strcmp("/mask", argv[i]) == 0)
        {
            // Mask specified
            bMask = true;
            i++;
        }
        else if (strcmp("/echo", argv[i]) == 0)
        {
            // Echo specified
            bEcho = true;
            i++;
        }
        else
        {
            // Don't understand the parameter
            showUsage();
            return 0;
        }
    } // Wend

    // Now try to read dongle

    FT_STATUS ftStatus;
    FT_HANDLE ftHandle;

    ftStatus = FT_OpenEx (
                "FT245R USB FIFO",
                FT_OPEN_BY_DESCRIPTION,
                &ftHandle);

    if (ftStatus != FT_OK)
    {
        cout << "Failed to obtain file handle to device FT245R USB FIFO" << endl;
        return 255; // As we use this as a legit return, need to return illegal
                    // value
    }


    DWORD BytesWritten;

    char WriteBuffer [WRITE_LENGTH];


    WriteBuffer [0] = (char) READ_COMMAND; // 1st byte, read command

    ftStatus = FT_Write (
                ftHandle,
                &WriteBuffer,
                WRITE_LENGTH, // Bytes to write
                &BytesWritten);

    if ( (ftStatus != FT_OK) || (BytesWritten != WRITE_LENGTH) )
    {
        cout << "Failed to write" << endl;
        return 255; // As we use this as a legit return, need to return illegal
                    // value
    }


    Sleep (1) ; // Delay 1 ms


    DWORD BytesRead;

    char ReadBuffer [READ_LENGTH];

    ftStatus = FT_Read (
                ftHandle,
                &ReadBuffer,
                READ_LENGTH, // Bytes to read
                &BytesRead);

    if ( (ftStatus != FT_OK) || (BytesRead != READ_LENGTH) )
    {
        cout << "Failed to read" << endl;
        return 255; // As we use this as a legit return, need to return illegal
                    // value
    }


    char XOR_Result = ReadBuffer [0];

    XOR_Result ^= ReadBuffer [1];
    XOR_Result ^= ReadBuffer [2];
    XOR_Result ^= ReadBuffer [3];

    if (XOR_Result != 0)
    {
        cout << "Failed checksum" << endl;
        return 255; // As we use this as a legit return, need to return illegal
                    // value
    }


    ftStatus = FT_Close (
                ftHandle);

    if (ftStatus != FT_OK)
    {
        cout << "Failed to close" << endl;
        return 255; // As we use this as a legit return, need to return illegal
                    // value
    }



    unsigned char nDongleValue = (unsigned char) ReadBuffer [1]; // 2nd byte


    // OK, if we've arrived here, we have legit values
    if(bMask)
    {
        // Mask out top two bits
        nDongleValue &= 63;
    }

    if(bEcho)
    {
        // Echo specified, so print to console
        cout << "Dongle id is " << (int)nDongleValue << endl;
    }

    // All done, just return the value
    return nDongleValue;
}

void showUsage()
{
    cout << "readusbdong: Sets %ERROR_LEVEL% to value of USB Dongle" << endl;
    cout << "   usage: readusbdong [/full] [/echo]" << endl;
    cout << "   where /mask returns all 8 bits, default is lower 6" << endl;
    cout << "         /echo also prints value to console" << endl;
    return;
}
poweroff.c:

Code:
// This is the source code for the dras_poweroff program.
// To compile it, use the command:
// gcc -o dras_poweroff dras_poweroff.c
// It checks bit 1 on the parallel port to see if it is set
// every 10s. If so, the poweroff command is run in /bin/sh,
// forcing a clean shutdown of the machine.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/io.h>

int main()
{
    const unsigned int PARALLEL_BASE_PORT = 0x378;

    //Acquire permissions on the parallel port
    if(ioperm(PARALLEL_BASE_PORT, 3, 1))
        _exit(1);

    // Set the read bit
    outb(32, PARALLEL_BASE_PORT+2);

    while(1)
    {
        // Check if the number 1 bit is set
        if(inb(PARALLEL_BASE_PORT) & 0x01 == 0x01)
        {
            //It's time to poweroff
            system("poweroff");
            _exit(0);
        }

        //Give it 10s until we poll again
        sleep(10);
    }
    // Should never get here
    return 1;
}
Jag