// File: Eba-M[P]E3DC-Control/RscpExampleMain.cpp
// Function: mainLoop()
static void mainLoop(void)
{
    RscpProtocol protocol;
    bool bStopExecution = false;
    bool bWBRequest = false;
    while(!bStopExecution&&!e3dc_config.stop)
    {
        
        //--------------------------------------------------------------------------------------------------------------
        // RSCP Transmit Frame Block Data
        //--------------------------------------------------------------------------------------------------------------
        SRscpFrameBuffer frameBuffer;
        memset(&frameBuffer, 0, sizeof(frameBuffer));

        // create an RSCP frame with requests to some example data
        if(iAuthenticated == 1) {
            int sunrise = sunriseAt;
            if (e3dc_config.debug) printf("M1\n");
            if (e3dc_config.aWATTar||e3dc_config.openmeteo)
            aWATTar(ch,w,wetter,e3dc_config,fBatt_SOC, fNotstromreserve, sunrise,iDayStat);
            
//            test;
            if (e3dc_config.debug) printf("M2\n");
            float zulufttemp = -99;
            if (e3dc_config.WPWolf&&wolf.size()>0)
                zulufttemp = wolf[wpzl].wert;
            if (fBatt_SOC >= 0)
            mewp(w,wetter,fatemp,fcop,sunriseAt,sunsetAt,e3dc_config,fBatt_SOC,ireq_Heistab,zulufttemp,fNotstromreserve,iHeatStat[1]);       // Ermitteln Wetterdaten
            if (e3dc_config.debug) printf("M3\n");

            if (strcmp(e3dc_config.heizung_ip,"0.0.0.0") >  0)
              iModbusTCP();
            if (e3dc_config.debug) printf("M3a\n");

            if((frameBuffer.dataLength == 0)&&(e3dc_config.wallbox>=0)&&(bWBRequest))
            {
                if (e3dc_config.debug) printf("M3b\n");
                WBProcess(&frameBuffer);
            }
            if (e3dc_config.debug) printf("M4\n");

            if(frameBuffer.dataLength == 0)
                 bWBRequest = true;
            else
                 bWBRequest = false;
if (e3dc_config.debug) printf("M5\n");

        if(frameBuffer.dataLength == 0)
            LoadDataProcess();
            
//            sleep(1);
if (e3dc_config.debug) printf("M6");

        }
        // check that frame data was created
        
        if(frameBuffer.dataLength == 0)
        {
            createRequestExample(&frameBuffer);
//            sleep(1);
        }
        // check that frame data was created

        if(frameBuffer.dataLength > 0)
        {
            // resize temporary encryption buffer to a multiple of AES_BLOCK_SIZE
            std::vector<uint8_t> encryptionBuffer;
            encryptionBuffer.resize(ROUNDUP(frameBuffer.dataLength, AES_BLOCK_SIZE));
            // zero padding for data above the desired length
            memset(&encryptionBuffer[0] + frameBuffer.dataLength, 0, encryptionBuffer.size() - frameBuffer.dataLength);
            // copy desired data length
            memcpy(&encryptionBuffer[0], frameBuffer.data, frameBuffer.dataLength);
            // set continues encryption IV
            aesEncrypter.SetIV(ucEncryptionIV, AES_BLOCK_SIZE);
            // start encryption from encryptionBuffer to encryptionBuffer, blocks = encryptionBuffer.size() / AES_BLOCK_SIZE
            aesEncrypter.Encrypt(&encryptionBuffer[0], &encryptionBuffer[0], encryptionBuffer.size() / AES_BLOCK_SIZE);
            // save new IV for next encryption block
            memcpy(ucEncryptionIV, &encryptionBuffer[0] + encryptionBuffer.size() - AES_BLOCK_SIZE, AES_BLOCK_SIZE);

            // send data on socket
            int iResult = SocketSendData(iSocket, &encryptionBuffer[0], encryptionBuffer.size());
            if(iResult < 0) {
                printf("Socket send error %i. errno %i\n", iResult, errno);
                bStopExecution = true;
            }
            else {
                // go into receive loop and wait for response
                sleep(1);
                    printf("%c[H", 27 );
                if (e3dc_config.debug)
                    printf("%c[2J", 27 );
//                printf("Request cyclic example data done %s
//                printf("Request data done %s %2ld:%2ld:%2ld",VERSION,tm_CONF_dt%(24*3600)/3600,tm_CONF_dt%3600/60,tm_CONF_dt%60);
                printf("%s %2ld:%2ld:%2ld",VERSION,tm_CONF_dt%(24*3600)/3600,tm_CONF_dt%3600/60,tm_CONF_dt%60);
                printf(" %0.02f %0.02f %0.02f %0.02f %0.02fkWh", fPVcharge,fPVtoday*e3dc_config.speichergroesse/100,fPVnextday*e3dc_config.speichergroesse/100,fPVSoll*e3dc_config.speichergroesse/100,fPVdirect*e3dc_config.speichergroesse/100); // erwartete PV Ertrag in % des Speichers
                int x2 = (t%(24*4*900))/900+1;
                if (x2 > 0&&e3dc_config.statistik)
                {
// Ausgabe Soll/Ist/ %  -15min, akt Soll Ist
//                    float f2 = iDayStat[x2]/100.0;
                    float f3 = iDayStat[DayStat-3]/3600000.0;
                    float f4 = 0;
                    if (wetter.size()>0)
                        f4 = wetter[0].solar;
                    float f5 = iDayStat[DayStat]/(e3dc_config.speichergroesse*10*3600);

//                    if (f2>0)
//                    printf(" %0.02f%% %0.02f%% %0.02f %0.02f%% %0.04f%%", f2,f3,f3/f2,f4,f5);
                    printf(" %0.02f %0.02f %0.04fkWh", f3,f4*e3dc_config.speichergroesse/100,iDayStat[DayStat]/3600000.0);
                    // erwartete PV Ertrag
                }
                printf("%c[K\n", 27 );


//                if (e3dc_config.debug) printf ("start receiveLoop");
                receiveLoop(bStopExecution);
//                if (e3dc_config.debug) printf ("end receiveLoop");
            }
        }
        // free frame buffer memory
        protocol.destroyFrameData(&frameBuffer);

        // main loop sleep / cycle time before next request
    }
    printf("mainloop beendet");
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
leaq 0x110(%rsp), %rdi
callq 0x200b0
movb $0x0, 0x10f(%rsp)
movb $0x0, 0x10e(%rsp)
xorl %eax, %eax
testb $0x1, 0x10f(%rsp)
movb %al, 0xb3(%rsp)
jne 0x1b79a
cmpb $0x0, 0x536e8(%rip)     # 0x6ee76
setne %al
xorb $-0x1, %al
movb %al, 0xb3(%rsp)
movb 0xb3(%rsp), %al
testb $0x1, %al
jne 0x1b7aa
jmp 0x1c021
leaq 0xf8(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2170
cmpl $0x1, 0x5a30b(%rip)     # 0x75ad0
jne 0x1ba67
movl 0x54433(%rip), %eax     # 0x6fc04
movl %eax, 0xf4(%rsp)
testb $0x1, 0x53682(%rip)     # 0x6ee61
je 0x1b80c
leaq 0x463fa(%rip), %rdi     # 0x61be2
xorl %eax, %eax
callq 0x2040
jmp 0x1b7f1
jmp 0x1b80c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x1c046
cmpb $0x0, 0x5365f(%rip)     # 0x6ee72
jne 0x1b81e
testb $0x1, 0x5364f(%rip)     # 0x6ee6b
je 0x1b862
movss 0x52b2a(%rip), %xmm0    # 0x6e350
movss 0x5a14a(%rip), %xmm1    # 0x75978
movl 0xf4(%rsp), %r8d
leaq 0x5306b(%rip), %rdi     # 0x6e8a8
leaq 0x52bc4(%rip), %rsi     # 0x6e408
leaq 0x52bd5(%rip), %rdx     # 0x6e420
leaq 0x530e2(%rip), %rcx     # 0x6e934
leaq 0x59d47(%rip), %r9      # 0x755a0
callq 0x48160
jmp 0x1b860
jmp 0x1b862
testb $0x1, 0x535f8(%rip)     # 0x6ee61
je 0x1b87d
leaq 0x46374(%rip), %rdi     # 0x61be6
xorl %eax, %eax
callq 0x2040
jmp 0x1b87b
jmp 0x1b87d
movss 0x44787(%rip), %xmm0    # 0x6000c
movss %xmm0, 0xe0(%rsp)
testb $0x1, 0x535d3(%rip)     # 0x6ee68
je 0x1b8ca
leaq 0x52b9a(%rip), %rdi     # 0x6e438
callq 0x1cc40
cmpq $0x0, %rax
jbe 0x1b8ca
movslq 0x5437c(%rip), %rsi     # 0x6fc2c
leaq 0x52b81(%rip), %rdi     # 0x6e438
callq 0x1cc60
movss 0x68(%rax), %xmm0
movss %xmm0, 0xe0(%rsp)
movss 0x52a7e(%rip), %xmm0    # 0x6e350
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x1b945
movl 0x54323(%rip), %r8d     # 0x6fc04
movl 0x54320(%rip), %r9d     # 0x6fc08
movss 0x52a60(%rip), %xmm0    # 0x6e350
movl 0x5562a(%rip), %ecx     # 0x70f20
movss 0xe0(%rsp), %xmm1
movss 0x5a071(%rip), %xmm2    # 0x75978
movl 0x59b07(%rip), %edx     # 0x75414
movq %rsp, %rax
movl %edx, 0x10(%rax)
movl %ecx, 0x8(%rax)
leaq 0x53017(%rip), %rcx     # 0x6e934
movq %rcx, (%rax)
leaq 0x52ae1(%rip), %rdi     # 0x6e408
leaq 0x52af2(%rip), %rsi     # 0x6e420
leaq 0x5a02b(%rip), %rdx     # 0x75960
leaq 0x5a054(%rip), %rcx     # 0x75990
callq 0x36e20
jmp 0x1b943
jmp 0x1b945
testb $0x1, 0x53515(%rip)     # 0x6ee61
je 0x1b960
leaq 0x46295(%rip), %rdi     # 0x61bea
xorl %eax, %eax
callq 0x2040
jmp 0x1b95e
jmp 0x1b960
leaq 0x52ff5(%rip), %rdi     # 0x6e95c
leaq 0x449a9(%rip), %rsi     # 0x60317
callq 0x23c0
cmpl $0x0, %eax
jle 0x1b981
callq 0x6010
jmp 0x1b97f
jmp 0x1b981
testb $0x1, 0x534d9(%rip)     # 0x6ee61
je 0x1b99c
leaq 0x4625d(%rip), %rdi     # 0x61bee
xorl %eax, %eax
callq 0x2040
jmp 0x1b99a
jmp 0x1b99c
cmpl $0x0, 0x100(%rsp)
jne 0x1b9e5
cmpl $0x0, 0x5350b(%rip)     # 0x6eeb8
jl 0x1b9e5
testb $0x1, 0x10e(%rsp)
je 0x1b9e5
testb $0x1, 0x534a1(%rip)     # 0x6ee61
je 0x1b9d4
leaq 0x4622a(%rip), %rdi     # 0x61bf3
xorl %eax, %eax
callq 0x2040
jmp 0x1b9d2
jmp 0x1b9d4
leaq 0xf8(%rsp), %rdi
callq 0x12e50
jmp 0x1b9e3
jmp 0x1b9e5
testb $0x1, 0x53475(%rip)     # 0x6ee61
je 0x1ba00
leaq 0x46203(%rip), %rdi     # 0x61bf8
xorl %eax, %eax
callq 0x2040
jmp 0x1b9fe
jmp 0x1ba00
cmpl $0x0, 0x100(%rsp)
jne 0x1ba14
movb $0x1, 0x10e(%rsp)
jmp 0x1ba1c
movb $0x0, 0x10e(%rsp)
testb $0x1, 0x5343e(%rip)     # 0x6ee61
je 0x1ba37
leaq 0x461d0(%rip), %rdi     # 0x61bfc
xorl %eax, %eax
callq 0x2040
jmp 0x1ba35
jmp 0x1ba37
cmpl $0x0, 0x100(%rsp)
jne 0x1ba4a
callq 0x8e40
jmp 0x1ba48
jmp 0x1ba4a
testb $0x1, 0x53410(%rip)     # 0x6ee61
je 0x1ba65
leaq 0x461a6(%rip), %rdi     # 0x61c00
xorl %eax, %eax
callq 0x2040
jmp 0x1ba63
jmp 0x1ba65
jmp 0x1ba67
cmpl $0x0, 0x100(%rsp)
jne 0x1ba82
leaq 0xf8(%rsp), %rdi
callq 0x14bf0
jmp 0x1ba80
jmp 0x1ba82
cmpl $0x0, 0x100(%rsp)
jbe 0x1c005
leaq 0xc8(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x1caf0
movq 0xa8(%rsp), %rdi
movl 0x100(%rsp), %eax
addl $0x1f, %eax
andl $-0x20, %eax
movl %eax, %esi
callq 0x1cb60
jmp 0x1bac3
xorl %eax, %eax
movl %eax, 0x94(%rsp)
movl %eax, %esi
movq %rsi, 0xa0(%rsp)
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0x1cc00
movq 0x98(%rsp), %rdi
movl 0x100(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x88(%rsp)
callq 0x1cc20
movq 0x88(%rsp), %rdi
movl 0x94(%rsp), %esi
movq %rax, %rdx
movl 0x100(%rsp), %eax
subq %rax, %rdx
callq 0x2170
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x1cc00
movq %rax, %rdi
movq 0xf8(%rsp), %rsi
movl 0x100(%rsp), %eax
movl %eax, %edx
callq 0x2220
leaq 0x52908(%rip), %rdi     # 0x6e468
leaq 0x59fc9(%rip), %rsi     # 0x75b30
movl $0x20, %edx
callq 0x23490
jmp 0x1bb73
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x68(%rsp)
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x1cc00
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdi
movq %rax, 0x78(%rsp)
callq 0x1cc00
movq 0x70(%rsp), %rdi
movq %rax, 0x80(%rsp)
callq 0x1cc20
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
shrq $0x5, %rax
movl %eax, %ecx
leaq 0x5289a(%rip), %rdi     # 0x6e468
movl $0x1, %r8d
callq 0x2bee0
jmp 0x1bbdb
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x38(%rsp)
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x1cc00
movq 0x48(%rsp), %rdi
movq %rax, 0x40(%rsp)
callq 0x1cc20
movq 0x38(%rsp), %rsi
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movq 0x40(%rsp), %rax
movups -0x20(%rax,%rcx), %xmm0
movups -0x10(%rax,%rcx), %xmm1
movups %xmm1, 0x59f18(%rip)    # 0x75b40
movups %xmm0, 0x59f01(%rip)    # 0x75b30
movl 0x52767(%rip), %eax     # 0x6e39c
movl %eax, 0x54(%rsp)
callq 0x1cc00
movq 0x48(%rsp), %rdi
movq %rax, 0x58(%rsp)
callq 0x1cc20
movl 0x54(%rsp), %edi
movq 0x58(%rsp), %rsi
movl %eax, %edx
callq 0x364e0
movl %eax, 0x64(%rsp)
jmp 0x1bc63
movl 0x64(%rsp), %eax
movl %eax, 0xc4(%rsp)
cmpl $0x0, 0xc4(%rsp)
jge 0x1bcd1
movl 0xc4(%rsp), %eax
movl %eax, 0x34(%rsp)
callq 0x2030
movl 0x34(%rsp), %esi
movl (%rax), %edx
leaq 0x45f6e(%rip), %rdi     # 0x61c03
xorl %eax, %eax
callq 0x2040
jmp 0x1bc9e
movb $0x1, 0x10f(%rsp)
jmp 0x1bff8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x1cb00
jmp 0x1c046
movl $0x1, %edi
callq 0x21e0
jmp 0x1bcdd
leaq 0x45f3f(%rip), %rdi     # 0x61c23
xorl %eax, %eax
movl $0x1b, %esi
callq 0x2040
jmp 0x1bcf2
testb $0x1, 0x53168(%rip)     # 0x6ee61
je 0x1bd12
leaq 0x45f26(%rip), %rdi     # 0x61c28
xorl %eax, %eax
movl $0x1b, %esi
callq 0x2040
jmp 0x1bd10
jmp 0x1bd12
movq 0x53ed7(%rip), %r8      # 0x6fbf0
movabsq $0x1845c8a0ce512957, %rcx # imm = 0x1845C8A0CE512957
movq %r8, %rax
imulq %rcx
movq %rdx, %rax
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0xd, %rax
addq %rdx, %rax
imulq $0x15180, %rax, %rcx    # imm = 0x15180
movq %r8, %rax
subq %rcx, %rax
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq %rdx, %rax
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0xa, %rax
addq %rdx, %rax
movq %rax, 0x28(%rsp)
movq %r8, %rax
imulq %rcx
movq %rdx, %rax
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0xa, %rax
addq %rdx, %rax
imulq $0xe10, %rax, %rcx      # imm = 0xE10
movq %r8, %rax
subq %rcx, %rax
movq %rax, 0x18(%rsp)
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
movq %rcx, 0x20(%rsp)
imulq %rcx
movq 0x18(%rsp), %rax
movq %rdx, %rcx
movq 0x20(%rsp), %rdx
addq %rax, %rcx
movq %rcx, %rax
shrq $0x3f, %rax
sarq $0x5, %rcx
addq %rax, %rcx
movq %r8, %rax
imulq %rdx
movq %rdx, %rax
movq 0x28(%rsp), %rdx
addq %r8, %rax
movq %rax, %rsi
shrq $0x3f, %rsi
sarq $0x5, %rax
addq %rsi, %rax
imulq $0x3c, %rax, %rax
subq %rax, %r8
leaq 0x45e3e(%rip), %rdi     # 0x61c2e
leaq 0x45cda(%rip), %rsi     # 0x61ad1
xorl %eax, %eax
callq 0x2040
jmp 0x1be00
movss 0x59b94(%rip), %xmm0    # 0x7599c
cvtss2sd %xmm0, %xmm0
movss 0x59b5c(%rip), %xmm1    # 0x75970
movss 0x530c8(%rip), %xmm6    # 0x6eee4
mulss %xmm6, %xmm1
movss 0x44228(%rip), %xmm5    # 0x60050
divss %xmm5, %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x59b6c(%rip), %xmm2    # 0x759a4
mulss %xmm6, %xmm2
divss %xmm5, %xmm2
cvtss2sd %xmm2, %xmm2
movss 0x59b28(%rip), %xmm3    # 0x75974
mulss %xmm6, %xmm3
divss %xmm5, %xmm3
cvtss2sd %xmm3, %xmm3
movss 0x59b38(%rip), %xmm4    # 0x75998
mulss %xmm6, %xmm4
divss %xmm5, %xmm4
cvtss2sd %xmm4, %xmm4
leaq 0x45dcd(%rip), %rdi     # 0x61c40
movb $0x5, %al
callq 0x2040
jmp 0x1be7c
movq 0x53d75(%rip), %rax     # 0x6fbf8
movl $0x15180, %ecx          # imm = 0x15180
cqto
idivq %rcx
movq %rdx, %rax
movl $0x384, %ecx            # imm = 0x384
cqto
idivq %rcx
addq $0x1, %rax
movl %eax, 0xc0(%rsp)
cmpl $0x0, 0xc0(%rsp)
jle 0x1bfd2
testb $0x1, 0x52fb2(%rip)     # 0x6ee6c
je 0x1bfd2
movl 0x524a6(%rip), %eax     # 0x6e36c
addl $-0x3, %eax
movslq %eax, %rcx
leaq 0x596cd(%rip), %rax     # 0x755a0
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
movsd 0x44255(%rip), %xmm1    # 0x60138
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xbc(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0xb8(%rsp)
leaq 0x52519(%rip), %rdi     # 0x6e420
callq 0x1cc80
cmpq $0x0, %rax
jbe 0x1bf30
leaq 0x52507(%rip), %rdi     # 0x6e420
xorl %eax, %eax
movl %eax, %esi
callq 0x1cca0
movss 0x1c(%rax), %xmm0
movss %xmm0, 0xb8(%rsp)
movslq 0x52435(%rip), %rcx     # 0x6e36c
leaq 0x59662(%rip), %rax     # 0x755a0
movl (%rax,%rcx,4), %ecx
cvtsi2ss %rcx, %xmm0
movss 0x52f96(%rip), %xmm1    # 0x6eee4
movss 0x440ce(%rip), %xmm2    # 0x60024
mulss %xmm2, %xmm1
movss 0x440fe(%rip), %xmm2    # 0x60060
mulss %xmm2, %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0xb4(%rsp)
movss 0xbc(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0xb8(%rsp), %xmm1
movss 0x52f53(%rip), %xmm2    # 0x6eee4
mulss %xmm2, %xmm1
movss 0x440b3(%rip), %xmm2    # 0x60050
divss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
movslq 0x523c0(%rip), %rcx     # 0x6e36c
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm2
movsd 0x4417c(%rip), %xmm3    # 0x60138
divsd %xmm3, %xmm2
leaq 0x45ca0(%rip), %rdi     # 0x61c67
movb $0x3, %al
callq 0x2040
jmp 0x1bfd0
jmp 0x1bfd2
leaq 0x45358(%rip), %rdi     # 0x61331
xorl %eax, %eax
movl $0x1b, %esi
callq 0x2040
jmp 0x1bfe7
leaq 0x10f(%rsp), %rdi
callq 0x1c060
jmp 0x1bff6
jmp 0x1bff8
leaq 0xc8(%rsp), %rdi
callq 0x1cb00
leaq 0x110(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x21df0
jmp 0x1c01c
jmp 0x1b774
leaq 0x45c58(%rip), %rdi     # 0x61c80
xorl %eax, %eax
callq 0x2040
jmp 0x1c031
leaq 0x110(%rsp), %rdi
callq 0x200d0
addq $0x118, %rsp            # imm = 0x118
retq
leaq 0x110(%rsp), %rdi
callq 0x200d0
movq 0xe8(%rsp), %rdi
callq 0x24c0