// File: 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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x4aa9
movq -0x18(%rbp), %rdi
callq 0x3540
cmpq $0x0, %rax
jne 0x4ac8
leaq 0x1d8c8(%rip), %rdi     # 0x22378
leaq 0x11a13(%rip), %rsi     # 0x164ca
callq 0x34c0
movl $0x1, -0x4(%rbp)
jmp 0x5044
movq -0x10(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x35f0
movq -0x130(%rbp), %rdx
leaq 0x11a25(%rip), %rsi     # 0x16516
leaq -0x38(%rbp), %rdi
callq 0x8b50
jmp 0x4afc
leaq -0x71(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x35f0
movq -0x140(%rbp), %rdx
leaq 0x11a02(%rip), %rsi     # 0x1651c
leaq -0x70(%rbp), %rdi
callq 0x8b50
jmp 0x4b25
movq -0x138(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x4560
movl %eax, -0x144(%rbp)
jmp 0x4b41
leaq -0x70(%rbp), %rdi
callq 0x3698
leaq -0x71(%rbp), %rdi
callq 0x33f0
leaq -0x38(%rbp), %rdi
callq 0x3698
leaq -0x39(%rbp), %rdi
callq 0x33f0
movl -0x144(%rbp), %eax
cmpl $0x0, %eax
je 0x4be0
leaq 0x1d801(%rip), %rdi     # 0x22378
leaq 0x119a8(%rip), %rsi     # 0x16526
callq 0x34c0
movl $0x1, -0x4(%rbp)
jmp 0x5044
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4bd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4bc0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3698
leaq -0x71(%rbp), %rdi
callq 0x33f0
leaq -0x38(%rbp), %rdi
callq 0x3698
leaq -0x39(%rbp), %rdi
callq 0x33f0
jmp 0x5050
leaq -0x98(%rbp), %rdi
callq 0x34e0
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x34e0
movq -0x150(%rbp), %rdi
leaq 0x11948(%rip), %rsi     # 0x16555
callq 0x34c0
jmp 0x4c14
leaq 0x1194e(%rip), %rsi     # 0x16569
leaq -0xb8(%rbp), %rdi
callq 0x3630
jmp 0x4c29
leaq 0x11960(%rip), %rsi     # 0x16590
leaq -0xb8(%rbp), %rdi
callq 0x3630
jmp 0x4c3e
movq -0x18(%rbp), %rdi
callq 0x31c0
movq %rax, %rsi
leaq -0xb8(%rbp), %rdi
callq 0x3630
jmp 0x4c58
leaq 0x11989(%rip), %rsi     # 0x165e8
leaq -0xb8(%rbp), %rdi
callq 0x3630
jmp 0x4c6d
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x35f0
movq -0x158(%rbp), %rdx
leaq 0x1197e(%rip), %rsi     # 0x1660c
leaq -0xd8(%rbp), %rdi
callq 0x8b50
jmp 0x4c9c
leaq -0xd9(%rbp), %rdi
callq 0x33f0
movl $0xffffffff, -0xe0(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x31c0
movq -0x170(%rbp), %rdi
movq %rax, -0x160(%rbp)
callq 0x3540
movq -0x168(%rbp), %rdi
movq -0x160(%rbp), %rsi
movl %eax, %edx
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
jmp 0x4cfe
leaq 0x1d6fb(%rip), %rdi     # 0x22400
movq %rdi, -0x180(%rbp)
xorl %esi, %esi
movl %esi, -0x174(%rbp)
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x3210
movq -0x180(%rbp), %rsi
movl -0x174(%rbp), %ecx
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movl $0xf4240, %edx          # imm = 0xF4240
callq *%rax
jmp 0x4d3e
leaq 0x1d6bb(%rip), %rsi     # 0x22400
leaq -0x98(%rbp), %rdi
callq 0x34c0
jmp 0x4d53
leaq -0x98(%rbp), %rdi
leaq 0x118b5(%rip), %rsi     # 0x16616
xorl %eax, %eax
movl %eax, %edx
callq 0x3670
cmpq $0x0, %rax
je 0x4de6
leaq 0x1d601(%rip), %rdi     # 0x22378
leaq 0x1189e(%rip), %rsi     # 0x1661c
callq 0x34c0
jmp 0x4d85
movl $0x1, -0x4(%rbp)
movl $0x1, -0xe4(%rbp)
jmp 0x5004
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x502a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd9(%rbp), %rdi
callq 0x33f0
jmp 0x502a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x3698
jmp 0x502a
movb $0x0, -0xe5(%rbp)
movq -0x10(%rbp), %rdi
leaq -0xe5(%rbp), %rsi
movss 0x11204(%rip), %xmm0    # 0x16004
callq 0x14e60
jmp 0x4e07
testb $0x1, -0xe5(%rbp)
jne 0x4e3e
leaq 0x117ff(%rip), %rsi     # 0x16616
leaq -0x98(%rbp), %rdi
callq 0x8cc0
movb %al, -0x181(%rbp)
jmp 0x4e2b
movb -0x181(%rbp), %al
testb $0x1, %al
jne 0x4e37
jmp 0x4e3e
movb $0x1, -0xe5(%rbp)
jmp 0x4e40
testb $0x1, -0xe5(%rbp)
je 0x4eba
leaq 0x1d5b0(%rip), %rdi     # 0x22400
movq %rdi, -0x190(%rbp)
xorl %esi, %esi
movl %esi, -0x188(%rbp)
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x3210
movq -0x190(%rbp), %rsi
movl -0x188(%rbp), %ecx
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movl $0xf4240, %edx          # imm = 0xF4240
callq *%rax
jmp 0x4e89
leaq 0x1d570(%rip), %rsi     # 0x22400
leaq -0x98(%rbp), %rdi
callq 0x3630
jmp 0x4e9e
movq -0x10(%rbp), %rdi
leaq -0xe5(%rbp), %rsi
movss 0x11153(%rip), %xmm0    # 0x16004
callq 0x14e60
jmp 0x4eb8
jmp 0x4e40
leaq -0x98(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3680
movl %eax, -0xe0(%rbp)
movslq -0xe0(%rbp), %rax
cmpq $-0x1, %rax
jne 0x4f0f
leaq 0x1d48d(%rip), %rdi     # 0x22378
leaq 0x1175c(%rip), %rsi     # 0x1664e
callq 0x34c0
jmp 0x4ef9
movl $0x1, -0x4(%rbp)
movl $0x1, -0xe4(%rbp)
jmp 0x5004
movslq -0xe0(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x3540
movq -0x198(%rbp), %rdx
addq %rax, %rdx
leaq -0x108(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movq $-0x1, %rcx
callq 0x33c0
jmp 0x4f4f
movq -0x18(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3460
leaq -0x108(%rbp), %rdi
callq 0x3698
movq -0x18(%rbp), %rdi
leaq 0x1173a(%rip), %rsi     # 0x166b0
xorl %eax, %eax
movl %eax, %edx
callq 0x3670
movl %eax, -0xe0(%rbp)
movslq -0xe0(%rbp), %rax
cmpq $-0x1, %rax
jne 0x4fba
leaq 0x1d3df(%rip), %rdi     # 0x22378
leaq 0x11722(%rip), %rsi     # 0x166c2
callq 0x34c0
jmp 0x4fa7
movl $0x1, -0x4(%rbp)
movl $0x1, -0xe4(%rbp)
jmp 0x5004
movq -0x18(%rbp), %rsi
movslq -0xe0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x128(%rbp), %rdi
callq 0x33c0
jmp 0x4fd7
movq -0x18(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x3460
leaq -0x128(%rbp), %rdi
callq 0x3698
movl $0x0, -0x4(%rbp)
movl $0x1, -0xe4(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x3698
leaq -0xb8(%rbp), %rdi
callq 0x3698
leaq -0x98(%rbp), %rdi
callq 0x3698
jmp 0x5044
leaq -0xb8(%rbp), %rdi
callq 0x3698
leaq -0x98(%rbp), %rdi
callq 0x3698
jmp 0x5050
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x35e0
nopl (%rax)