// File: NovaGL[P]HarmonyHubControl/HarmonyHubControl.cpp
// Function: harmonyWebServiceLogin(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 harmonyWebServiceLogin(std::string strUserEmail, std::string strPassword, std::string& strAuthorizationToken )
{   
    if(strUserEmail.length() == 0 || strPassword.length() == 0)
    {
        errorString = "harmonyWebServiceLogin : Empty email or password provided";
        return 1;
    } 


    // Build JSON request
    std::string strJSONText = "{\"email\":\"";
    strJSONText.append(strUserEmail.c_str());
    strJSONText.append("\",\"password\":\"");
    strJSONText.append(strPassword.c_str());
    strJSONText.append("\"}");

    std::string strHttpPayloadText;

    csocket authcsocket;
    authcsocket.connect("svcs.myharmony.com", 80);

    if (authcsocket.getState() != csocket::CONNECTED)
    {
        errorString = "harmonyWebServiceLogin : Unable to connect to Logitech server";
        return 1;
    }

    char contentLength[32];
    sprintf( contentLength, "%d", strJSONText.length() );

    std::string strHttpRequestText;

    strHttpRequestText = "POST ";
    strHttpRequestText.append(LOGITECH_AUTH_URL);
    strHttpRequestText.append(" HTTP/1.1\r\nHost: ");
    strHttpRequestText.append(LOGITECH_AUTH_HOSTNAME);
    strHttpRequestText.append("\r\nAccept-Encoding: identity\r\nContent-Length: ");
    strHttpRequestText.append(contentLength);
    strHttpRequestText.append("\r\ncontent-type: application/json;charset=utf-8\r\n\r\n");

    authcsocket.write(strHttpRequestText.c_str(), strHttpRequestText.size());
    authcsocket.write(strJSONText.c_str(), strJSONText.length());

    memset(databuffer, 0, 1000000);
    authcsocket.read(databuffer, 1000000, false);
    strHttpPayloadText = databuffer;/* <- Expect: 0x00def280 "HTTP/1.1 200 OK Server: nginx/1.2.4 Date: Wed, 05 Feb 2014 17:52:13 GMT Content-Type: application/json; charset=utf-8 Content-Length: 127 Connection: keep-alive Cache-Control: private X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET  {"GetUserAuthTokenResult":{"AccountId":0,"UserAuthToken":"KsRE6VVA3xrhtbqFbh0jWn8YTiweDeB\/b94Qeqf3ofWGM79zLSr62XQh8geJxw\/V"}}"*/

    // Parse the login authorization token from the response
    std::string strAuthTokenTag = "UserAuthToken\":\"";
    int pos = (int)strHttpPayloadText.find(strAuthTokenTag);
    if(pos == std::string::npos)
    {
        errorString = "harmonyWebServiceLogin : Logitech web service response does not contain a login authorization token";
        return 1;  
    }

    strAuthorizationToken = strHttpPayloadText.substr(pos + strAuthTokenTag.length());
    pos = (int)strAuthorizationToken.find("\"}}");
    strAuthorizationToken = strAuthorizationToken.substr(0, pos);

    // Remove forward slashes
    strAuthorizationToken.erase(std::remove(strAuthorizationToken.begin(), strAuthorizationToken.end(), '\\'), strAuthorizationToken.end());
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movq %rdi, -0x1d8(%rbp)
movq %rsi, -0x1d0(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
callq 0x3540
cmpq $0x0, %rax
je 0x3eb2
movq -0x1d0(%rbp), %rdi
callq 0x3540
cmpq $0x0, %rax
jne 0x3ed1
leaq 0x1e4bf(%rip), %rdi     # 0x22378
leaq 0x12221(%rip), %rsi     # 0x160e1
callq 0x34c0
movl $0x1, -0x4(%rbp)
jmp 0x448e
leaq -0x41(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x35f0
movq -0x1e0(%rbp), %rdx
leaq 0x1222c(%rip), %rsi     # 0x1611b
leaq -0x40(%rbp), %rdi
callq 0x8b50
jmp 0x3efa
leaq -0x41(%rbp), %rdi
callq 0x33f0
movq -0x1d8(%rbp), %rdi
callq 0x31c0
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x3630
jmp 0x3f1d
leaq 0x12202(%rip), %rsi     # 0x16126
leaq -0x40(%rbp), %rdi
callq 0x3630
jmp 0x3f2f
movq -0x1d0(%rbp), %rdi
callq 0x31c0
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x3630
jmp 0x3f49
leaq 0x121e5(%rip), %rsi     # 0x16135
leaq -0x40(%rbp), %rdi
callq 0x3630
jmp 0x3f5b
leaq -0x78(%rbp), %rdi
callq 0x34e0
leaq -0xd0(%rbp), %rdi
callq 0x14a70
jmp 0x3f72
leaq 0x121bf(%rip), %rsi     # 0x16138
leaq -0xd0(%rbp), %rdi
movl $0x50, %edx
callq 0x14bf0
jmp 0x3f8c
leaq -0xd0(%rbp), %rdi
callq 0x15180
movl %eax, -0x1e4(%rbp)
jmp 0x3fa0
movl -0x1e4(%rbp), %eax
cmpl $0x1, %eax
je 0x4023
leaq 0x1e3c6(%rip), %rdi     # 0x22378
leaq 0x12192(%rip), %rsi     # 0x1614b
callq 0x34c0
jmp 0x3fc0
movl $0x1, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
jmp 0x444e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x33f0
jmp 0x449a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4483
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x447a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x446e
leaq -0x40(%rbp), %rdi
callq 0x3540
movq %rax, %rdx
leaq 0x12153(%rip), %rsi     # 0x16189
xorl %eax, %eax
leaq -0x100(%rbp), %rdi
callq 0x30d0
leaq -0x120(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x34e0
movq -0x1f0(%rbp), %rdi
leaq 0x12127(%rip), %rsi     # 0x1618c
callq 0x34c0
jmp 0x406c
leaq 0x1211f(%rip), %rsi     # 0x16192
leaq -0x120(%rbp), %rdi
callq 0x3630
jmp 0x4081
leaq 0x12162(%rip), %rsi     # 0x161ea
leaq -0x120(%rbp), %rdi
callq 0x3630
jmp 0x4096
leaq 0x1209b(%rip), %rsi     # 0x16138
leaq -0x120(%rbp), %rdi
callq 0x3630
jmp 0x40ab
leaq 0x1214a(%rip), %rsi     # 0x161fc
leaq -0x120(%rbp), %rdi
callq 0x3630
jmp 0x40c0
leaq -0x120(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x3630
jmp 0x40d5
leaq 0x1214e(%rip), %rsi     # 0x1622a
leaq -0x120(%rbp), %rdi
callq 0x3630
jmp 0x40ea
leaq -0x120(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x31c0
movq -0x200(%rbp), %rdi
movq %rax, -0x1f8(%rbp)
callq 0x3290
movq -0x1f8(%rbp), %rsi
movl %eax, %edx
leaq -0xd0(%rbp), %rdi
callq 0x150e0
jmp 0x4127
leaq -0x40(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x31c0
movq -0x210(%rbp), %rdi
movq %rax, -0x208(%rbp)
callq 0x3540
movq -0x208(%rbp), %rsi
movl %eax, %edx
leaq -0xd0(%rbp), %rdi
callq 0x150e0
jmp 0x4161
leaq 0x1e298(%rip), %rdi     # 0x22400
movq %rdi, -0x220(%rbp)
xorl %esi, %esi
movl %esi, -0x214(%rbp)
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x3210
movq -0x220(%rbp), %rsi
movl -0x214(%rbp), %ecx
leaq -0xd0(%rbp), %rdi
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x15010
jmp 0x41a1
leaq 0x1e258(%rip), %rsi     # 0x22400
leaq -0x78(%rbp), %rdi
callq 0x34c0
jmp 0x41b3
leaq -0x141(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x35f0
movq -0x228(%rbp), %rdx
leaq 0x12089(%rip), %rsi     # 0x1625d
leaq -0x140(%rbp), %rdi
callq 0x8b50
jmp 0x41e2
leaq -0x141(%rbp), %rdi
callq 0x33f0
leaq -0x78(%rbp), %rdi
leaq -0x140(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3680
movl %eax, -0x148(%rbp)
movslq -0x148(%rbp), %rax
cmpq $-0x1, %rax
jne 0x428b
leaq 0x1e15c(%rip), %rdi     # 0x22378
leaq 0x1204b(%rip), %rsi     # 0x1626e
callq 0x34c0
jmp 0x422a
movl $0x1, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
jmp 0x4426
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4440
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x141(%rbp), %rdi
callq 0x33f0
jmp 0x4440
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x3698
jmp 0x4440
movslq -0x148(%rbp), %rax
movq %rax, -0x230(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x3540
movq -0x230(%rbp), %rdx
addq %rax, %rdx
leaq -0x168(%rbp), %rdi
leaq -0x78(%rbp), %rsi
movq $-0x1, %rcx
callq 0x33c0
jmp 0x42c8
movq -0x20(%rbp), %rdi
leaq -0x168(%rbp), %rsi
movq %rsi, -0x240(%rbp)
callq 0x3460
movq -0x240(%rbp), %rdi
callq 0x3698
movq -0x20(%rbp), %rdi
leaq 0x11fdc(%rip), %rsi     # 0x162d2
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x238(%rbp)
callq 0x3670
movq -0x238(%rbp), %rdx
movl %eax, -0x148(%rbp)
movq -0x20(%rbp), %rsi
movslq -0x148(%rbp), %rcx
leaq -0x188(%rbp), %rdi
callq 0x33c0
jmp 0x432c
movq -0x20(%rbp), %rdi
leaq -0x188(%rbp), %rsi
movq %rsi, -0x258(%rbp)
callq 0x3460
movq -0x258(%rbp), %rdi
callq 0x3698
movq -0x20(%rbp), %rdi
movq %rdi, -0x250(%rbp)
callq 0x3320
movq %rax, -0x1a0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3230
movq %rax, -0x1a8(%rbp)
movb $0x5c, -0x1a9(%rbp)
movq -0x1a0(%rbp), %rdi
movq -0x1a8(%rbp), %rsi
leaq -0x1a9(%rbp), %rdx
callq 0x8c00
movq %rax, -0x248(%rbp)
jmp 0x43a0
movq -0x248(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x8c50
movq -0x20(%rbp), %rdi
callq 0x3230
movq %rax, -0x1c0(%rbp)
leaq -0x1b8(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x8c50
movq -0x250(%rbp), %rdi
movq -0x190(%rbp), %rsi
movq -0x1b8(%rbp), %rdx
callq 0x3100
movq %rax, -0x260(%rbp)
jmp 0x4407
movq -0x260(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x3698
leaq -0x120(%rbp), %rdi
callq 0x3698
jmp 0x444e
leaq -0x120(%rbp), %rdi
callq 0x3698
jmp 0x446e
leaq -0xd0(%rbp), %rdi
callq 0x14ad0
leaq -0x78(%rbp), %rdi
callq 0x3698
leaq -0x40(%rbp), %rdi
callq 0x3698
jmp 0x448e
leaq -0xd0(%rbp), %rdi
callq 0x14ad0
leaq -0x78(%rbp), %rdi
callq 0x3698
leaq -0x40(%rbp), %rdi
callq 0x3698
jmp 0x449a
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x35e0
nopw %cs:(%rax,%rax)