// File: /workspace/github_cpp_makefile_star2_O2/NovaGL[P]HarmonyHubControl/HarmonyHubControl.cpp
// Function: submitCommand(csocket*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int submitCommand(csocket* commandcsocket, std::string& strAuthorizationToken, std::string strCommand, std::string strCommandParameterPrimary, std::string strCommandParameterSecondary,std::string strCommandParameterThird,std::string strCommandParameterFourth ){

  if(commandcsocket== NULL || strAuthorizationToken.length() == 0)
    {
        errorString = "submitCommand : NULL csocket or empty authorization token provided";
        return 1;
    }

    std::string lstrCommand = strCommand;
    if(lstrCommand.length() == 0)
    {
        // No command provided, return query for the current activity
        lstrCommand = "get_current_activity_id";
        return 0;
    }

    std::string strData;

    std::string sendData;
    
    sendData = "<iq type=\"get\" id=\"";
    sendData.append(CONNECTION_ID);
    sendData.append("\"><oa xmlns=\"connect.logitech.com\" mime=\"vnd.logitech.harmony/vnd.logitech.harmony.engine?");

    // Issue the provided command
    if(lstrCommand == "get_current_activity_id" || lstrCommand == "get_current_activity_id_raw")
    {
        sendData.append("getCurrentActivity\" /></iq>");
    }
    if(lstrCommand == "get_config_raw")
    {
        sendData.append("config\"></oa></iq>");        
    }
    else if (lstrCommand == "start_activity" || strCommand == "start_activity_raw")
    {
        sendData.append("startactivity\">activityId=");
        sendData.append(strCommandParameterPrimary.c_str());
        sendData.append(":timestamp=0</oa></iq>");
    }
    else if (lstrCommand == "issue_device_command")
    {
        sendData.append("holdAction\">action={\"type\"::\"IRCommand\",\"deviceId\"::\"");
        sendData.append(strCommandParameterPrimary.c_str());
        sendData.append("\",\"command\"::\"");
        sendData.append(strCommandParameterSecondary.c_str());
        sendData.append("\"}:status=press</oa></iq>");
    }

    commandcsocket->write(sendData.c_str(), static_cast<unsigned int>(sendData.length()));
    
 if (strCommandParameterThird != "") 
   {
        sendData = "<iq type=\"get\" id=\"";
        sendData.append(CONNECTION_ID);
        sendData.append("\"><oa xmlns=\"connect.logitech.com\" mime=\"vnd.logitech.harmony/vnd.logitech.harmony.engine?");

    sendData.append("holdAction\">action={\"type\"::\"IRCommand\",\"deviceId\"::\"");
        sendData.append(strCommandParameterPrimary.c_str());
        sendData.append("\",\"command\"::\"");
        sendData.append(strCommandParameterThird.c_str());
        sendData.append("\"}:status=press</oa></iq>");
    
    commandcsocket->write(sendData.c_str(), sendData.length());
    }

  if (strCommandParameterFourth != "") 
   {
        sendData = "<iq type=\"get\" id=\"";
        sendData.append(CONNECTION_ID);
        sendData.append("\"><oa xmlns=\"connect.logitech.com\" mime=\"vnd.logitech.harmony/vnd.logitech.harmony.engine?");

    sendData.append("holdAction\">action={\"type\"::\"IRCommand\",\"deviceId\"::\"");
        sendData.append(strCommandParameterPrimary.c_str());
        sendData.append("\",\"command\"::\"");
        sendData.append(strCommandParameterFourth.c_str());
        sendData.append("\"}:status=press</oa></iq>");
    
    commandcsocket->write(sendData.c_str(), sendData.length());
    }

    memset(databuffer, 0, 1000000);
    commandcsocket->read(databuffer, 1000000, false);
    strData = databuffer; /* <- Expect: strData  == <iq/> */
    
    std::string iqTag = "<iq/>";
    std::string::size_type pos = strData.find(iqTag);

    if(pos != 0)
    {
        errorString = "submitCommand: Invalid Harmony response";
        return 1;  
    }

    bool bIsDataReadable = false;
    commandcsocket->canRead(&bIsDataReadable, 0.6f);

    if(bIsDataReadable == false && strData == "<iq/>")
    {
        bIsDataReadable = true;
    }

    if(strCommand != "issue_device_command")
    {
        while(bIsDataReadable)
        {
            memset(databuffer, 0, 1000000);
            commandcsocket->read(databuffer, 1000000, false);
            strData.append(databuffer);
            commandcsocket->canRead(&bIsDataReadable, 0.3f);
        }
    }
    
    resultString = strData;

    if(strCommand == "get_current_activity_id" || strCommand == "get_current_activity_id_raw")
    {
         std::string::size_type resultStartPos = resultString.find("result=");
        std::string::size_type resultEndPos = resultString.find("]]>");

        if(resultStartPos != std::string::npos && resultEndPos != std::string::npos)
        {
            resultString = resultString.substr(resultStartPos + 7, resultEndPos - resultStartPos - 7);
	resultString.insert(0, "{\"current activity\":");
            if(strCommand == "get_current_activity_id")
            {

            }
		resultString.append("}");	
		ofstream current("current.json");
		current << ("%s\n\n", resultString.c_str());
        }

    }
    else if(strCommand == "get_config" || strCommand == "get_config_raw")
    {
        commandcsocket->canRead(&bIsDataReadable, 0.3f);

#ifndef _WIN32
        bIsDataReadable = true;
#endif

        while(bIsDataReadable)
        {
            memset(databuffer, 0, 1000000);
            commandcsocket->read(databuffer, 1000000, false);
            strData.append(databuffer);
            commandcsocket->canRead(&bIsDataReadable, 0.3f);
        }
        
        pos = strData.find("![CDATA[{");
        if(pos != std::string::npos)
        {
            size_t endpos = strData.rfind("}]]");
            if (pos != std::string::npos)
            {
                rawConfig = strData.substr(pos + 8, endpos - pos - 7);
            }
            
            resultString = "Logitech Harmony Configuration : \n" + strData.substr(pos + 9);
        }
        if (rawConfig.empty())
        {
            errorString = "Invalid Logitech Harmony Configuration : \n" + strData;
            return 1;
        }
    }
    else if (strCommand == "start_activity" || strCommand == "start_activity_raw")
    {
		
        resultString ="{\"current activity\":" + strCommandParameterPrimary + "}";
        ofstream current("current.json");
        current << ("%s\n\n", resultString.c_str());
    }
	else if (strCommand == "issue_device_command")
	{
		resultString ="";
	}	
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
testq %rdi, %rdi
je 0x4585
cmpq $0x0, 0x8(%rsi)
je 0x4585
movq %r9, %r12
movq %r8, %r13
movq %rcx, %r15
movq %rdx, %rbx
movq %rdi, %r14
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %rdx, %rsi
callq 0x3380
cmpq $0x0, 0x8(%rbp)
je 0x45a1
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x7020(%rip), %rsi      # 0xb51d
leaq 0x8(%rsp), %rdi
callq 0x34c0
leaq 0x7023(%rip), %rsi      # 0xb531
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x7216(%rip), %rsi      # 0xb735
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x9b56(%rip), %rsi      # 0xe086
leaq 0x48(%rsp), %rdi
callq 0x68fd
testb %al, %al
jne 0x4553
leaq 0x9b61(%rip), %rsi      # 0xe0a6
leaq 0x48(%rsp), %rdi
callq 0x68fd
testb %al, %al
je 0x4564
leaq 0x7236(%rip), %rsi      # 0xb790
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x9c42(%rip), %rsi      # 0xe1ad
leaq 0x48(%rsp), %rdi
callq 0x68fd
testb %al, %al
je 0x45ba
leaq 0x722c(%rip), %rsi      # 0xb7ac
jmp 0x460b
leaq 0xed2c(%rip), %rdi      # 0x132b8
leaq 0x715f(%rip), %rsi      # 0xb6f2
callq 0x34c0
pushq $0x1
popq %r14
jmp 0x4833
xorl %r14d, %r14d
leaq 0x9adb(%rip), %rsi      # 0xe086
leaq 0x48(%rsp), %rdi
callq 0x34c0
jmp 0x4829
leaq 0x71fe(%rip), %rsi      # 0xb7bf
leaq 0x48(%rsp), %rdi
callq 0x68fd
testb %al, %al
jne 0x45e6
leaq 0x71f8(%rip), %rsi      # 0xb7ce
movq %rbx, %rdi
callq 0x68fd
testb %al, %al
je 0x4a27
leaq 0x71f4(%rip), %rsi      # 0xb7e1
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%r15), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x71f1(%rip), %rsi      # 0xb7fc
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0x7949(%rip), %rsi      # 0xbf77
movq %r12, %rdi
callq 0x68f3
testb %al, %al
je 0x46d1
leaq 0x6ed8(%rip), %rsi      # 0xb51d
leaq 0x8(%rsp), %rdi
callq 0x34c0
leaq 0x6edb(%rip), %rsi      # 0xb531
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x70ce(%rip), %rsi      # 0xb735
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x71b0(%rip), %rsi      # 0xb828
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%r15), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x71c8(%rip), %rsi      # 0xb85e
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%r12), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x71b8(%rip), %rsi      # 0xb86d
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
movq 0x2e0(%rsp), %r12
leaq 0x7897(%rip), %rsi      # 0xbf77
movq %r12, %rdi
callq 0x68f3
testb %al, %al
je 0x4783
leaq 0x6e26(%rip), %rsi      # 0xb51d
leaq 0x8(%rsp), %rdi
callq 0x34c0
leaq 0x6e29(%rip), %rsi      # 0xb531
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x701c(%rip), %rsi      # 0xb735
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x70fe(%rip), %rsi      # 0xb828
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%r15), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x7116(%rip), %rsi      # 0xb85e
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%r12), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x7106(%rip), %rsi      # 0xb86d
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0xebb6(%rip), %r12      # 0x13340
movl $0xf4240, %edx          # imm = 0xF4240
movq %r12, %rdi
xorl %esi, %esi
callq 0x31b0
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
leaq 0xeb8e(%rip), %rsi      # 0x13340
leaq 0x28(%rsp), %rdi
callq 0x34c0
leaq 0x6e1b(%rip), %rsi      # 0xb5de
leaq 0x88(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x68b8
leaq 0x28(%rsp), %rdi
leaq 0x88(%rsp), %rsi
xorl %edx, %edx
callq 0x3500
testq %rax, %rax
je 0x4848
leaq 0xeac0(%rip), %rdi      # 0x132b8
leaq 0x7088(%rip), %rsi      # 0xb887
callq 0x34c0
pushq $0x1
popq %r14
leaq 0x88(%rsp), %rdi
callq 0x3518
leaq 0x8(%rsp), %rdi
callq 0x3518
leaq 0x28(%rsp), %rdi
callq 0x3518
leaq 0x48(%rsp), %rdi
callq 0x3518
movl %r14d, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7(%rsp), %rsi
movb $0x0, (%rsi)
movss 0x67b0(%rip), %xmm0     # 0xb008
movq %r14, %rdi
callq 0xa41a
cmpb $0x0, 0x7(%rsp)
jne 0x4881
leaq 0x6d70(%rip), %rsi      # 0xb5de
leaq 0x28(%rsp), %rdi
callq 0x68fd
testb %al, %al
je 0x4881
movb $0x1, 0x7(%rsp)
leaq 0x6f8b(%rip), %rsi      # 0xb813
movq %rbx, %rdi
callq 0x68f3
testb %al, %al
je 0x48ed
leaq 0xeaa5(%rip), %r12      # 0x13340
leaq 0x28(%rsp), %r13
leaq 0x7(%rsp), %rbp
cmpb $0x1, 0x7(%rsp)
jne 0x48ed
movl $0xf4240, %edx          # imm = 0xF4240
movq %r12, %rdi
xorl %esi, %esi
callq 0x31b0
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
movq %r13, %rdi
movq %r12, %rsi
callq 0x34b0
movq %r14, %rdi
movq %rbp, %rsi
movss 0x671e(%rip), %xmm0     # 0xb004
callq 0xa41a
jmp 0x48a5
leaq 0xe9e4(%rip), %rdi      # 0x132d8
leaq 0x28(%rsp), %rsi
callq 0x31a0
leaq 0x9781(%rip), %rsi      # 0xe086
movq %rbx, %rdi
callq 0x68fd
testb %al, %al
jne 0x4928
leaq 0x978e(%rip), %rsi      # 0xe0a6
movq %rbx, %rdi
callq 0x68fd
testb %al, %al
je 0x4a89
leaq 0xe9a9(%rip), %r12      # 0x132d8
leaq 0x6f79(%rip), %rsi      # 0xb8af
xorl %r14d, %r14d
movq %r12, %rdi
xorl %edx, %edx
callq 0x34f0
movq %rax, %r15
leaq 0x6f6a(%rip), %rsi      # 0xb8b7
movq %r12, %rdi
xorl %edx, %edx
callq 0x34f0
cmpq $-0x1, %r15
sete %cl
cmpq $-0x1, %rax
sete %dl
orb %cl, %dl
jne 0x4808
subq %r15, %rax
addq $0x7, %r15
addq $-0x7, %rax
leaq 0xe959(%rip), %rsi      # 0x132d8
leaq 0xa8(%rsp), %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x32d0
leaq 0xe93f(%rip), %r14      # 0x132d8
leaq 0xa8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3320
movq %r15, %rdi
callq 0x3518
leaq 0x6f00(%rip), %rdx      # 0xb8bb
movq %r14, %rdi
xorl %esi, %esi
callq 0x3100
leaq 0x96ba(%rip), %rsi      # 0xe086
movq %rbx, %rdi
callq 0x68fd
leaq 0xe8fd(%rip), %rdi      # 0x132d8
leaq 0x671c(%rip), %rsi      # 0xb0fe
callq 0x34b0
leaq 0x6ee2(%rip), %rsi      # 0xb8d0
leaq 0xa8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x32f0
movq 0xe8d3(%rip), %rsi      # 0x132d8
leaq 0xa8(%rsp), %rdi
callq 0x3270
leaq 0xa8(%rsp), %rdi
callq 0x33e0
xorl %r14d, %r14d
jmp 0x4808
leaq 0x6de5(%rip), %rsi      # 0xb813
leaq 0x48(%rsp), %rdi
callq 0x68fd
testb %al, %al
je 0x4615
leaq 0x6de1(%rip), %rsi      # 0xb828
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%r15), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x6df9(%rip), %rsi      # 0xb85e
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%r13), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x6de9(%rip), %rsi      # 0xb86d
jmp 0x460b
leaq 0x970a(%rip), %rsi      # 0xe19a
movq %rbx, %rdi
callq 0x68fd
testb %al, %al
jne 0x4ab3
leaq 0x970a(%rip), %rsi      # 0xe1ad
movq %rbx, %rdi
callq 0x68fd
testb %al, %al
je 0x4c40
leaq 0x7(%rsp), %rsi
movss 0x6544(%rip), %xmm0     # 0xb004
movq %r14, %rdi
callq 0xa41a
leaq 0x7(%rsp), %rbx
movb $0x1, (%rbx)
leaq 0xe869(%rip), %r15      # 0x13340
leaq 0x28(%rsp), %r12
cmpb $0x1, 0x7(%rsp)
jne 0x4b24
movl $0xf4240, %edx          # imm = 0xF4240
movq %r15, %rdi
xorl %esi, %esi
callq 0x31b0
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
movq %r12, %rdi
movq %r15, %rsi
callq 0x34b0
movq %r14, %rdi
movq %rbx, %rsi
movss 0x64e7(%rip), %xmm0     # 0xb004
callq 0xa41a
jmp 0x4adc
leaq 0x6db7(%rip), %rsi      # 0xb8e2
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x34f0
cmpq $-0x1, %rax
je 0x4bf5
movq %rax, %rbx
leaq 0x6d9e(%rip), %rsi      # 0xb8ec
leaq 0x28(%rsp), %rdi
pushq $-0x1
popq %rdx
callq 0x3450
leaq 0x8(%rbx), %rdx
subq %rbx, %rax
addq $-0x7, %rax
leaq 0xa8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movq %rax, %rcx
callq 0x32d0
leaq 0xe776(%rip), %rdi      # 0x132f8
leaq 0xa8(%rsp), %r15
movq %r15, %rsi
callq 0x3320
movq %r15, %rdi
callq 0x3518
addq $0x9, %rbx
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
pushq $-0x1
popq %rcx
movq %rbx, %rdx
callq 0x32d0
leaq 0x6d3e(%rip), %rsi      # 0xb8f8
leaq 0xa8(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x690a
leaq 0xe705(%rip), %rdi      # 0x132d8
leaq 0xa8(%rsp), %rbx
movq %rbx, %rsi
callq 0x3320
movq %rbx, %rdi
callq 0x3518
leaq 0x68(%rsp), %rdi
callq 0x3518
cmpq $0x0, 0xe703(%rip)      # 0x13300
jne 0x4808
leaq 0x6ce6(%rip), %rsi      # 0xb8f0
leaq 0xa8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x692e
leaq 0xe695(%rip), %rdi      # 0x132b8
leaq 0xa8(%rsp), %rbx
movq %rbx, %rsi
callq 0x3320
movq %rbx, %rdi
callq 0x3518
jmp 0x4804
leaq 0x6b78(%rip), %rsi      # 0xb7bf
movq %rbx, %rdi
callq 0x68fd
testb %al, %al
jne 0x4c6a
leaq 0x6b74(%rip), %rsi      # 0xb7ce
movq %rbx, %rdi
callq 0x68fd
testb %al, %al
je 0x4cf0
leaq 0x6c4a(%rip), %rsi      # 0xb8bb
leaq 0x68(%rsp), %rdi
movq %r15, %rdx
callq 0x692e
leaq 0x6479(%rip), %rdx      # 0xb0fe
leaq 0xa8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x69a4
leaq 0xe63a(%rip), %rdi      # 0x132d8
leaq 0xa8(%rsp), %rbx
movq %rbx, %rsi
callq 0x3320
movq %rbx, %rdi
callq 0x3518
leaq 0x68(%rsp), %rdi
callq 0x3518
leaq 0x6c09(%rip), %rsi      # 0xb8d0
leaq 0xa8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x32f0
movq 0xe5fa(%rip), %rsi      # 0x132d8
leaq 0xa8(%rsp), %rdi
callq 0x3270
jmp 0x4a12
leaq 0x6b1c(%rip), %rsi      # 0xb813
movq %rbx, %rdi
callq 0x68fd
xorl %r14d, %r14d
testb %al, %al
je 0x4808
leaq 0xe5c7(%rip), %rdi      # 0x132d8
leaq 0x725f(%rip), %rsi      # 0xbf77
callq 0x34c0
jmp 0x4808
jmp 0x4d41
jmp 0x4d66
jmp 0x4d2c
jmp 0x4d66
jmp 0x4d66
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3518
jmp 0x4d69
jmp 0x4d66
jmp 0x4d66
jmp 0x4d66
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x33e0
jmp 0x4d69
jmp 0x4d66
jmp 0x4d66
jmp 0x4d66
movq %rax, %rbx
jmp 0x4d8f
jmp 0x4d66
jmp 0x4d78
jmp 0x4d66
jmp 0x4d66
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3518
jmp 0x4d7b
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3518
leaq 0x28(%rsp), %rdi
callq 0x3518
leaq 0x48(%rsp), %rdi
callq 0x3518
movq %rbx, %rdi
callq 0x3470