// File: bummelbaerchen[P]e3dc/RscpExampleMain.cpp
// Function: mainLoop()
static void mainLoop(void)
{
    RscpProtocol protocol;
    bool bStopExecution = false;

    while(!bStopExecution)
    {
        
        //--------------------------------------------------------------------------------------------------------------
        // 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) {
            if((frameBuffer.dataLength == 0)&&(e3dc_config.wallbox))
            WBProcess(&frameBuffer);
        if(frameBuffer.dataLength == 0)
            LoadDataProcess(&frameBuffer);
//            sleep(1);
        }
        // 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
                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 $0xd8, %rsp
leaq 0xd0(%rsp), %rdi
callq 0xadf0
movb $0x0, 0xcf(%rsp)
movb 0xcf(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x83de
jmp 0x8717
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2110
cmpl $0x1, 0x1e63b(%rip)     # 0x26a34
jne 0x8455
cmpl $0x0, 0xc0(%rsp)
jne 0x8438
testb $0x1, 0x1e394(%rip)     # 0x267a0
je 0x8438
leaq 0xb8(%rsp), %rdi
callq 0x3650
jmp 0x841d
jmp 0x8438
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x873c
cmpl $0x0, 0xc0(%rsp)
jne 0x8453
leaq 0xb8(%rsp), %rdi
callq 0x2a40
jmp 0x8451
jmp 0x8453
jmp 0x8455
cmpl $0x0, 0xc0(%rsp)
jne 0x847c
leaq 0xb8(%rsp), %rdi
callq 0x4230
jmp 0x846e
movl $0x1, %edi
callq 0x2140
jmp 0x847a
jmp 0x847c
cmpl $0x0, 0xc0(%rsp)
jbe 0x86fb
leaq 0x90(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0xa090
movq 0x80(%rsp), %rdi
movl 0xc0(%rsp), %eax
addl $0x1f, %eax
andl $-0x20, %eax
movl %eax, %esi
callq 0xa0a0
jmp 0x84bd
xorl %eax, %eax
movl %eax, 0x6c(%rsp)
movl %eax, %esi
movq %rsi, 0x78(%rsp)
leaq 0x90(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0xa140
movq 0x70(%rsp), %rdi
movl 0xc0(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
callq 0xa160
movq 0x60(%rsp), %rdi
movl 0x6c(%rsp), %esi
movq %rax, %rdx
movl 0xc0(%rsp), %eax
subq %rax, %rdx
callq 0x2110
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0xa140
movq %rax, %rdi
movq 0xb8(%rsp), %rsi
movl 0xc0(%rsp), %eax
movl %eax, %edx
callq 0x2160
leaq 0x1dcb1(%rip), %rdi     # 0x261f0
leaq 0x1e52a(%rip), %rsi     # 0x26a70
movl $0x20, %edx
callq 0xe1d0
jmp 0x8552
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x40(%rsp)
leaq 0x90(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0xa140
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdi
movq %rax, 0x50(%rsp)
callq 0xa140
movq 0x48(%rsp), %rdi
movq %rax, 0x58(%rsp)
callq 0xa160
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
shrq $0x5, %rax
movl %eax, %ecx
leaq 0x1dc49(%rip), %rdi     # 0x261f0
movl $0x1, %r8d
callq 0x16c20
jmp 0x85b4
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x10(%rsp)
leaq 0x90(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xa140
movq 0x20(%rsp), %rdi
movq %rax, 0x18(%rsp)
callq 0xa160
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movq 0x18(%rsp), %rax
movups -0x20(%rax,%rcx), %xmm0
movups -0x10(%rax,%rcx), %xmm1
movups %xmm1, 0x1e47f(%rip)    # 0x26a80
movups %xmm0, 0x1e468(%rip)    # 0x26a70
movl 0x1dbba(%rip), %eax     # 0x261c8
movl %eax, 0x2c(%rsp)
callq 0xa140
movq 0x20(%rsp), %rdi
movq %rax, 0x30(%rsp)
callq 0xa160
movl 0x2c(%rsp), %edi
movq 0x30(%rsp), %rsi
movl %eax, %edx
callq 0x20e00
movl %eax, 0x3c(%rsp)
jmp 0x863c
movl 0x3c(%rsp), %eax
movl %eax, 0x8c(%rsp)
cmpl $0x0, 0x8c(%rsp)
jge 0x86a7
movl 0x8c(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x2030
movl 0xc(%rsp), %esi
movl (%rax), %edx
leaq 0x192c9(%rip), %rdi     # 0x21937
xorl %eax, %eax
callq 0x2040
jmp 0x8677
movb $0x1, 0xcf(%rsp)
jmp 0x86ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xa180
jmp 0x873c
testb $0x1, 0x1e0f7(%rip)     # 0x267a5
je 0x86c2
leaq 0x192a0(%rip), %rdi     # 0x21957
xorl %eax, %eax
callq 0x2040
jmp 0x86c0
jmp 0x86c2
leaq 0xcf(%rsp), %rdi
callq 0x8760
jmp 0x86d1
testb $0x1, 0x1e0cd(%rip)     # 0x267a5
je 0x86ec
leaq 0x19288(%rip), %rdi     # 0x21969
xorl %eax, %eax
callq 0x2040
jmp 0x86ea
jmp 0x86ec
jmp 0x86ee
leaq 0x90(%rsp), %rdi
callq 0xa180
leaq 0xd0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xcb30
jmp 0x8712
jmp 0x83cc
leaq 0x1925b(%rip), %rdi     # 0x21979
xorl %eax, %eax
callq 0x2040
jmp 0x8727
leaq 0xd0(%rsp), %rdi
callq 0xae10
addq $0xd8, %rsp
retq
leaq 0xd0(%rsp), %rdi
callq 0xae10
movq 0xb0(%rsp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)