#include "mainwindow.h"
#include "inout.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <string>
#include <iostream>
#include <QMessageBox>

//void comm_send(QString cport, QString ccommand, QString &creturn, int &anzahl)
//{
//    char outputstring[250] = "";
//    char inputstring[250] = "";
//    int j = 0;
//    char strport[100];
//    char c;
//    int sdo;
//    int sdi;
//    int read_ok;
//    int write_ok;
//    int ret;

//    QMessageBox msgBox;

//    ccommand = ccommand + "\r";
//    strcpy(strport,cport.toLatin1());
//    strcpy(outputstring,ccommand.toLatin1());


//    sdo = open ( strport ,  O_RDWR | O_NDELAY );
//    if (sdo!=0)
//    {
//        for (j=0;outputstring[j]!=0;j++)
//        {
//            anzahl = j;
//            c = outputstring[j];
//            write_ok = write(sdo,&c,1);
//            if (write_ok<1)
//            {linux smsd
//                msgBox.setText("Fehler output");
//                ret = msgBox.exec();
//                anzahl = j;
//                return;
//            }
//        }
//    }
//    close(sdo);
//return;
//    sdi = open ( strport ,  O_RDWR | O_NOCTTY | O_NDELAY );
//    if (sdi!=0)
//    {
//        j=0;
//        while (1)
//        {
//            read_ok = read(sdi, &c, 1);
//            inputstring[j] = c;
//            j++;
//            if(c==13)
//            {
//                creturn = creturn + "<CR>";
//            }
//            else if (c==10)
//            {
//                creturn = creturn + "<LF>";
//            }
//            else if (c==0)
//            {
//                creturn = creturn + "<NULL>";
//            }
//            else
//            {
//                creturn = creturn + c;
//            }
//            if (j>249)
//            {
//                inputstring[250] = '\0';
//                close(sdi);
//                //creturn=creturn+inputstring;
//                anzahl = j;
//                return;
//            }
//        }
//        return;
//    }
//    else
//    {
//        creturn = "error opening usb0";
//    }

//}

void comm_send(QString cport, QString ccommand, QString &creturn, int &anzahl)
{
    FILE * pFile;
    char strport[100];
    int j = 0;
    char outputstring[250] = "";
    char inputstring[250] = "";
    char c[1];
    QMessageBox msgBox;


    int ret;
    ccommand = ccommand + "\r";

    strcpy(strport,cport.toLatin1());
    strcpy(outputstring,ccommand.toLatin1());

    pFile = fopen ( strport , "wt" );
    if (pFile!=NULL)
    {
        //ccommand.toLatin1()
        //sizeof(ccommand.toLatin1())
        //"AT+CSCS=\"GSM\"\r\0"

        fwrite (outputstring , 1 , sizeof(outputstring) , pFile );
        fclose(pFile);
    }
    else
    {
        creturn="ERROR öffnen schreiben nicht möglich";
        return;
    }
}

void comm_read(QString cport, QString ccommand, QString &creturn, int &anzahl)
{

    char strport[100];
    int j = 0;
    int sd;
    int read_ok;
    char inputstring[250] = "";
    char c;



    strcpy(strport,cport.toLatin1());

    j=0;
    creturn = ccommand+"-";
    sd = open ( strport ,  O_RDWR | O_NOCTTY | O_NDELAY );

    if (sd!=0)
    {
        while (1)
        {
            read_ok = read(sd, &c, 1);
            inputstring[j] = c;
            j++;
            if(c==13)
            {
                creturn = creturn + "<CR>";
            }
            else if (c==10)
            {
                creturn = creturn + "<LF>";
            }
            else if (c==0)
            {
                creturn = creturn + "<NULL>";
            }
            else
            {
                creturn = creturn + c;
            }
            if (j>249 || read_ok<=0)
            {
                inputstring[250] = '\0';
                close(sd);
                creturn=creturn +"\n"+inputstring;
                anzahl = j;
                return;
            }
        }
        return;
    }
    else
    {
        creturn = "error opening usb0";
    }
}

//QMessageBox msgBox;
//msgBox.setText(creturn);anzahl

//int ret;
//ret = msgBox.exec();

/*

FILE * pFile;
char str[1024];

strcpy(str , "AT+CSCS=\"GSM\"\r\0");
pFile = fopen ( "/dev/ttyUSB0" , "wt+" );
if (pFile!=NULL)anzahl
{

    fwrite (str , 1 , sizeof(str) , pFile );
    fclose(pFile);
}
else
{
    ui->output->setPlainText("öffnen schreiben nicht möglich");
}
ui->output->setPlainText("Daten geschrieben");

int j = 0;anzahl
int sd;

char valor[250] = "";
char c;

ui->output->setPlainText("vor open");
sd = open ( "/dev/ttyUSB0" ,  O_RDWR | O_NOCTTY | O_NDELAY );

ui->output->setPlainText("nach open ");

if (sd!=0)
{anzahl

    while (c != '\n'|| j<10)
    {



        read(sd, &c, 1);


        if ((isprint(c) != 0) || (c == '\n')) {
            valor[j] = c;
            j++;
        }


        if ((c == '\n') ||(c == '\0') || (j == (250 - 1))) {
            valor[j] = '\0';
            ui->output->setPlainText("vor sscanf");

            //ret = sscanf(valor, "%s", &input[0]);
            ui->output->setPlainText("nach sscanf");
            //return;
            j = 0;
            valor[j] = '\0';
            ui->output->setPlainText(valor);
            return;
        }

    }
    return;
}
else
{
    ui->output->setPlainText("error opening usb0");
}

*/
