// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
cmpq $0x0, 0x8(%rdi)
je 0x3c8d
movq %rsi, %r14
cmpq $0x0, 0x8(%rsi)
je 0x3c8d
movq %rdx, %rbx
movq %rdi, %r15
leaq 0x7659(%rip), %rsi      # 0xb0e3
leaq 0x28(%rsp), %r12
leaq 0xa8(%rsp), %rdx
movq %r12, %rdi
callq 0x68b8
movq (%r15), %rsi
movq %r12, %rdi
callq 0x34b0
leaq 0x763d(%rip), %rsi      # 0xb0ee
leaq 0x28(%rsp), %rdi
callq 0x34b0
movq (%r14), %rsi
leaq 0x28(%rsp), %rdi
callq 0x34b0
leaq 0x762e(%rip), %rsi      # 0xb0fd
leaq 0x28(%rsp), %rdi
callq 0x34b0
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xa8(%rsp), %rdi
callq 0xa1e8
leaq 0x7602(%rip), %rsi      # 0xb100
leaq 0xa8(%rsp), %rdi
pushq $0x50
popq %rdx
callq 0xa29e
leaq 0xa8(%rsp), %rdi
callq 0xa5d6
cmpl $0x1, %eax
jne 0x3ca8
movq 0x30(%rsp), %rdx
leaq 0x7621(%rip), %rsi      # 0xb151
leaq 0x100(%rsp), %rdi
xorl %eax, %eax
callq 0x3090
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x75fd(%rip), %rsi      # 0xb154
leaq 0x8(%rsp), %rdi
callq 0x34c0
leaq 0x75f2(%rip), %rsi      # 0xb15a
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x7639(%rip), %rsi      # 0xb1b2
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x7576(%rip), %rsi      # 0xb100
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x7629(%rip), %rsi      # 0xb1c4
leaq 0x8(%rsp), %rdi
callq 0x34b0
leaq 0x8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x34b0
leaq 0x7634(%rip), %rsi      # 0xb1f2
leaq 0x8(%rsp), %rdi
callq 0x34b0
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
leaq 0xa8(%rsp), %rdi
callq 0xa580
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %edx
leaq 0xa8(%rsp), %rdi
callq 0xa580
leaq 0xf745(%rip), %r14      # 0x13340
movl $0xf4240, %edx          # imm = 0xF4240
movq %r14, %rdi
xorl %esi, %esi
callq 0x31b0
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq 0xa4e8
leaq 0xf718(%rip), %rsi      # 0x13340
leaq 0x68(%rsp), %rdi
callq 0x34c0
leaq 0x75ec(%rip), %rsi      # 0xb225
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x68b8
leaq 0x68(%rsp), %rdi
leaq 0x88(%rsp), %rsi
xorl %edx, %edx
callq 0x3500
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jne 0x3cc3
leaq 0xf63f(%rip), %rdi      # 0x132b8
leaq 0x75b6(%rip), %rsi      # 0xb236
callq 0x34c0
pushq $0x1
popq %rbx
jmp 0x3d63
leaq 0xf624(%rip), %rdi      # 0x132b8
leaq 0x740e(%rip), %rsi      # 0xb0a9
callq 0x34c0
pushq $0x1
popq %rbx
jmp 0x3d9b
leaq 0xf609(%rip), %rdi      # 0x132b8
leaq 0x745d(%rip), %rsi      # 0xb113
callq 0x34c0
pushq $0x1
popq %rbx
jmp 0x3d7a
sarq $0x20, %rax
addq 0x90(%rsp), %rax
leaq 0x48(%rsp), %rdi
leaq 0x68(%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 0x7597(%rip), %rsi      # 0xb29a
movq %rbx, %rdi
xorl %edx, %edx
callq 0x34f0
movslq %eax, %rcx
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x32d0
leaq 0x48(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3320
movq %r14, %rdi
callq 0x3518
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
addq %rdi, %rsi
movb $0x5c, (%r14)
leaq 0x48(%rsp), %rdx
callq 0x7215
movq (%rbx), %rdx
addq 0x8(%rbx), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x30c0
xorl %ebx, %ebx
leaq 0x88(%rsp), %rdi
callq 0x3518
leaq 0x8(%rsp), %rdi
callq 0x3518
leaq 0xa8(%rsp), %rdi
callq 0xa210
leaq 0x68(%rsp), %rdi
callq 0x3518
leaq 0x28(%rsp), %rdi
callq 0x3518
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x3db4
jmp 0x3db4
jmp 0x3db4
jmp 0x3db4
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3518
jmp 0x3dda
jmp 0x3dd7
movq %rax, %rbx
jmp 0x3df1
movq %rax, %rbx
jmp 0x3de4
movq %rax, %rbx
jmp 0x3dfb
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3518
leaq 0xa8(%rsp), %rdi
callq 0xa210
leaq 0x68(%rsp), %rdi
callq 0x3518
leaq 0x28(%rsp), %rdi
callq 0x3518
movq %rbx, %rdi
callq 0x3470