#include "sthreadwatchport.h"

#include "mainwindow.h"


#include <stdlib.h>
#include <assert.h>
#include <fcntl.h>              /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <string.h> // strerrno
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

//#include <stdexcept>
#include <sys/klog.h>
#include <linux/videodev2.h>

SThreadWatchPort::SThreadWatchPort():QSThread()
{
    m_nDefaultMicroSleep = 1000*100;
    m_nValue = 0;
    m_nValuePrev = 0;
}

SThreadWatchPort::~SThreadWatchPort()
{

}

#include <poll.h>

void SThreadWatchPort::InnerRun()
{
    m_nINotifyWatch = inotify_init();

    QString strGPIO = MainWindow::GetCommonData()->GetHandswitchGPIONumber();

#ifdef __x86_64
    usleep(m_nDefaultMicroSleep);
    return;
#else
    struct pollfd myPollfd;

    myPollfd.fd = open(strGPIO.toUtf8(), O_RDONLY); //store filedescriptor in struct, open(path, read-write-permission)

    myPollfd.events = POLLIN;

    poll(&myPollfd, 1, 1); //poll(struct pollfd, max fd, timeout), timeout=-1 --> never
    if(myPollfd.revents & POLLIN)
    {
        char buf[1024];
        memset(buf, 0, 1024);
        int len = read(myPollfd.fd, buf, 1024);  //mandatory to make system register interrupt as served

        if(len>0)
        {
            if(buf[0]=='1')
            {
                m_nValue = 1;
            }
            else
            {
                m_nValue = 0;
            }

            if(m_nValue==1 && m_nValuePrev==0)
            {
                CaptureHandSwitch();
            }

            m_nValuePrev = m_nValue;
        }
    }
    lseek(myPollfd.fd, 0, 0); //return cursor to beginning of file or next read() will return EOF

    close(myPollfd.fd);

    usleep(m_nDefaultMicroSleep);

    return;
        m_nWatchGPIO271 = inotify_add_watch(m_nINotifyWatch, "/sys/class/gpio/gpio271/value", IN_ALL_EVENTS);
#endif

    memset(m_pNotifyBuffer, 0, INOTIFY_BUF_LEN);

    int nLength = 0;
    nLength = read(m_nINotifyWatch, m_pNotifyBuffer, INOTIFY_BUF_LEN);

    if(nLength<0)
    {
        //Error
        qDebug() << "Notify Error";
        return;
    }
    struct inotify_event* pEvent = (inotify_event*)&m_pNotifyBuffer[0];
    if(pEvent->wd==1)
    {
#ifdef __x86_64
        QFile file("/home/birdhead/1.txt");
        char cData;
        if(file.open(QFile::ReadOnly)==true)
        {

            file.read(&cData, 1);
            file.close();
        }

        if(cData=='1')
        {
            CaptureHandSwitch();
        }

#else
        char cRead = 0;
        int nReadCount = 0;
        int nGPIO_HandSwitch = 0;

        //nGPIO_HandSwitch = open("/sys/class/gpio/gpio271/value", O_RDONLY);
        nGPIO_HandSwitch = open(strGPIO.toUtf8(), O_RDONLY);

        if(nGPIO_HandSwitch==-1)
        {
            qDebug() << "271 Open Failed";
        }
        else
        {
            //qDebug() << "271 Open success";

            nReadCount = read(nGPIO_HandSwitch, &cRead, sizeof(char));

            if(nReadCount>0)
            {
                //qDebug() << cRead;
                if(cRead=='1')
                {
                    CaptureHandSwitch();
                }
            }

            close(nGPIO_HandSwitch);
        }
#endif
    }

    close(m_nWatchGPIO271);
    close(m_nINotifyWatch);
}


void SThreadWatchPort::Init()
{




}

void SThreadWatchPort::ExitThread()
{

    m_bStop = true;

    while(m_bRunning==true)
    {
        usleep(1000*10);
    }


}
