// File: /workspace/github_cpp_makefile_star2_O2/NovaGL[P]HarmonyHubControl/HarmonyHubControl.cpp
// Function: swapAuthorizationToken(csocket*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
int swapAuthorizationToken(csocket* authorizationcsocket, std::string& strAuthorizationToken)
{
    if(authorizationcsocket == NULL || strAuthorizationToken.length() == 0)
    {
        errorString = "swapAuthorizationToken : NULL csocket or empty authorization token provided";
        return 1;
    }

    if(startCommunication(authorizationcsocket, "guest", "gatorade.") != 0)
    {
        errorString = "swapAuthorizationToken : Communication failure";
        return 1;
    }

    std::string strData;
    std::string sendData;
    
    // GENERATE A LOGIN ID REQUEST USING THE HARMONY ID AND LOGIN AUTHORIZATION TOKEN 
    sendData = "<iq type=\"get\" id=\"";
    sendData.append(CONNECTION_ID);
    sendData.append("\"><oa xmlns=\"connect.logitech.com\" mime=\"vnd.logitech.connect/vnd.logitech.pair\">token=");
    sendData.append(strAuthorizationToken.c_str());
    sendData.append(":name=foo#iOS6.0.1#iPhone</oa></iq>");

    std::string strIdentityTokenTag = "identity=";
    int pos = std::string::npos;
    
    authorizationcsocket->write(sendData.c_str(), static_cast<unsigned int>(sendData.length()));
    
    memset(databuffer, 0, 1000000);
    authorizationcsocket->read(databuffer, 1000000, false);
        
    strData = databuffer; /* <- Expect: <iq/> ... <success xmlns= ... identity=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:status=succeeded ... */

    if(strData.find("<iq/>") != 0)
    {
         errorString = "swapAuthorizationToken : Invalid Harmony response";
         return 1;  
    }

    bool bIsDataReadable = false;
    authorizationcsocket->canRead(&bIsDataReadable, 0.3f);
    if(!bIsDataReadable && strData == "<iq/>")
    {
        bIsDataReadable = true;
    }

    while(bIsDataReadable)
    {
        memset(databuffer, 0, 1000000);
        authorizationcsocket->read(databuffer, 1000000, false);
        strData.append(databuffer);
        authorizationcsocket->canRead(&bIsDataReadable, 0.3f);
    };

    // Parse the session authorization token from the response
    pos = (int)strData.find(strIdentityTokenTag);
    if(pos == std::string::npos)
    {
        errorString = "swapAuthorizationToken : Logitech Harmony response does not contain a session authorization token";
        return 1;  
    }
    
    strAuthorizationToken = strData.substr(pos + strIdentityTokenTag.length());

    pos = (int)strAuthorizationToken.find(":status=succeeded");
    if(pos == std::string::npos)
    {
        errorString = "swapAuthorizationToken : Logitech Harmony response does not contain a valid session authorization token";
        return 1;  
    }
    strAuthorizationToken = strAuthorizationToken.substr(0, pos);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xc8, %rsp
testq %rdi, %rdi
je 0x4183
movq %rsi, %rbx
cmpq $0x0, 0x8(%rsi)
je 0x4183
movq %rdi, %r14
leaq 0x73ce(%rip), %rsi      # 0xb4de
leaq 0xa8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x68b8
leaq 0x73bb(%rip), %rsi      # 0xb4e4
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x68b8
leaq 0xa8(%rsp), %rsi
leaq 0x88(%rsp), %rdx
movq %r14, %rdi
callq 0x3e61
movl %eax, %ebp
leaq 0x88(%rsp), %rdi
callq 0x3518
leaq 0xa8(%rsp), %rdi
callq 0x3518
testl %ebp, %ebp
je 0x41ab
leaq 0xf13e(%rip), %rdi      # 0x132b8
leaq 0x736d(%rip), %rsi      # 0xb4ee
jmp 0x4191
leaq 0xf12e(%rip), %rdi      # 0x132b8
leaq 0x7301(%rip), %rsi      # 0xb492
callq 0x34c0
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
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 0x7349(%rip), %rsi      # 0xb51d
leaq 0x8(%rsp), %rdi
callq 0x34c0
leaq 0x734c(%rip), %rsi      # 0xb531
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x7362(%rip), %rsi      # 0xb558
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq (%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x739c(%rip), %rsi      # 0xb5b0
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x73af(%rip), %rsi      # 0xb5d4
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x68b8
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0xf0f3(%rip), %r15      # 0x13340
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)
leaq 0xf0cb(%rip), %rsi      # 0x13340
leaq 0x28(%rsp), %rdi
callq 0x34c0
leaq 0x7358(%rip), %rsi      # 0xb5de
leaq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x34f0
testq %rax, %rax
je 0x42af
leaq 0xf01a(%rip), %rdi      # 0x132b8
leaq 0x733f(%rip), %rsi      # 0xb5e4
callq 0x34c0
jmp 0x43cf
leaq 0x7(%rsp), %rsi
movb $0x0, (%rsi)
movss 0x6d45(%rip), %xmm0     # 0xb004
movq %r14, %rdi
callq 0xa41a
cmpb $0x1, 0x7(%rsp)
je 0x42e8
leaq 0x7309(%rip), %rsi      # 0xb5de
leaq 0x28(%rsp), %rdi
callq 0x68fd
testb %al, %al
je 0x42e8
movb $0x1, 0x7(%rsp)
cmpb $0x1, 0x7(%rsp)
jne 0x433f
leaq 0xf04a(%rip), %r15      # 0x13340
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)
leaq 0xf022(%rip), %rsi      # 0x13340
leaq 0x28(%rsp), %rdi
callq 0x34b0
leaq 0x7(%rsp), %rsi
movss 0x6ccf(%rip), %xmm0     # 0xb004
movq %r14, %rdi
callq 0xa41a
jmp 0x42e8
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
leaq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rsi
xorl %edx, %edx
callq 0x3500
shlq $0x20, %rax
cmpq %r15, %rax
jne 0x436c
leaq 0x72ac(%rip), %rsi      # 0xb616
jmp 0x43c3
sarq $0x20, %rax
addq 0x70(%rsp), %rax
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
pushq $-0x1
popq %rcx
movq %rax, %rdx
callq 0x32d0
leaq 0x48(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3320
movq %r14, %rdi
callq 0x3518
leaq 0x72cf(%rip), %rsi      # 0xb678
movq %rbx, %rdi
xorl %edx, %edx
callq 0x34f0
shlq $0x20, %rax
cmpq %r15, %rax
jne 0x43f6
leaq 0x72c7(%rip), %rsi      # 0xb68a
leaq 0xeeee(%rip), %rdi      # 0x132b8
callq 0x34c0
pushq $0x1
popq %r14
leaq 0x68(%rsp), %rdi
callq 0x3518
leaq 0x8(%rsp), %rdi
callq 0x3518
leaq 0x28(%rsp), %rdi
callq 0x3518
jmp 0x419a
sarq $0x20, %rax
xorl %r14d, %r14d
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x32d0
leaq 0x48(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3320
movq %r15, %rdi
callq 0x3518
jmp 0x43d3
jmp 0x445c
jmp 0x445c
jmp 0x445c
jmp 0x4457
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3518
jmp 0x4446
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x4478
movq %rax, %rbx
jmp 0x447d
jmp 0x445c
movq %rax, %rbx
jmp 0x4469
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3518
leaq 0x8(%rsp), %rdi
callq 0x3518
leaq 0x28(%rsp), %rdi
callq 0x3518
movq %rbx, %rdi
callq 0x3470