// File: /workspace/github_cpp_makefile_star2_O2/dguglielmi[P]dlan-linux-software/tools/dlanpasswd.cpp
// Function: main
int main(int argc, char** argv)
{
  int result = 0;
  CMACAddress addrDevice;
  
  if((argc < 3) || !addrDevice.FromString(argv[1]))
  {
    printf("Usage: dlanpasswd MAC-ADDRESS PASSWORD\n"
           "   or: dlanpasswd MAC-ADDRESS PASSWORD SECURITY-ID\n"
           "\n"
           "In the 1st form, assign PASSWORD to the local dLAN adapter MAC-ADDRESS.\n"
           "\n"    
           "In the 2nd form, assign PASSWORD to the remote dLAN 200 AV adapter identified \n"
           "by SECURITY-ID, using the local dLAN 200 AV adapter MAC-ADDRESS to forward the\n"
           "request to the PowerLine.\n"
           "\n");
           
    result = -1;
  }
  else
  {
    std::string strPassword = argv[2];
    std::string strSecurityID = (argc > 3) ? argv[3] : "";

    CPacketInterfaceLinux pktIfc;
    CFindLocalDevice finder(pktIfc, addrDevice);
    if(CPacketTaskRunner(pktIfc, finder).Run(3000, MSCounter()))
    {
      if(finder.itDevice->GetType() == CDlanDevice::eThunderbolt)
      {
        if(strSecurityID.empty())
        {
          printf("assigning password to local device %s: ", 
            finder.itDevice->GetAddress().ToString().c_str());
            
          HomePlugAvSecurity::CSetKeyTask setKey(pktIfc,
            finder.itDevice->GetNICAddress(), finder.itDevice->GetAddress(), strPassword);

          if(CPacketTaskRunner(pktIfc, setKey).Run())
          {
            printf("OK\n");
          }
          else
          {
            printf("failed\n");
            result = 2;          
          }
        }
        else
        {
          printf("assigning password to remote device %s via %s: ", 
            strSecurityID.c_str(), finder.itDevice->GetAddress().ToString().c_str());
            
          HomePlugAvSecurity::CSetKeyTask setKey(pktIfc, 
            finder.itDevice->GetNICAddress(), finder.itDevice->GetAddress(), strPassword, 
            CMACAddress::Broadcast(), strSecurityID);

          if(CPacketTaskRunner(pktIfc, setKey).Run())
          {
            printf("OK\n");
          }
          else
          {
            printf("failed\n");
            result = 2;          
          }
        }
      }
      else
      {
        if(strSecurityID.empty())
        {
          printf("assigning password to local device %s: ", 
            finder.itDevice->GetAddress().ToString().c_str());
            
          HomePlugSecurity::CSetKeyTask setKey(pktIfc, 
            finder.itDevice->GetNICAddress(), finder.itDevice->GetAddress(), strPassword);

          if(CPacketTaskRunner(pktIfc, setKey).Run())
          {
            printf("OK\n");
          }
          else
          {
            printf("failed\n");
            result = 2;          
          }
        }
        else
        {
          printf("error: assigning remote password is only supported for dLAN 200 AV devices\n");
          result = 3;
        }
      }
    }
    else
    {
      printf("no device found\n");
      result = 1;
    }
  }

  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x300, %rsp            # imm = 0x300
andl $0x0, 0x10(%rsp)
andw $0x0, 0x14(%rsp)
cmpl $0x3, %edi
jl 0x4631
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8(%rsi), %rsi
leaq 0x150(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x4ab0
leaq 0x10(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x11946
movl %eax, %r14d
leaq 0x150(%rsp), %rdi
callq 0x4170
testb %r14b, %r14b
je 0x4631
movq 0x10(%rbx), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x4ab0
cmpl $0x3, %ebp
jne 0x4645
leaq 0xfc1a(%rip), %rsi      # 0x14249
jmp 0x4649
leaq 0xfab5(%rip), %rdi      # 0x140ed
callq 0x43a0
pushq $-0x1
popq %rbp
jmp 0x49ac
movq 0x18(%rbx), %rsi
leaq 0x58(%rsp), %r14
leaq 0x150(%rsp), %rdx
movq %r14, %rdi
callq 0x4ab0
leaq 0x158(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x8(%rbx)
andq $0x0, 0x8(%rbx)
movq %rbx, 0x10(%rbx)
movq %rbx, 0x18(%rbx)
andq $0x0, 0x20(%rbx)
leaq 0x250(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x119fc
leaq 0x150(%rsp), %rdi
callq 0x4ffc
leaq 0x150(%rsp), %rdi
leaq 0x250(%rsp), %r15
leaq 0x10(%rsp), %rdx
movq %r15, %rsi
callq 0x4aec
leaq 0x78(%rsp), %rdi
movq %r15, (%rdi)
movq %rbx, 0x8(%rdi)
movl $0xbb8, %esi            # imm = 0xBB8
callq 0x4b8a
testb %al, %al
je 0x47c4
movq 0x248(%rsp), %rax
leaq 0x10(%rax), %r15
cmpl $0x2, 0x18(%rax)
movq 0x60(%rsp), %rax
jne 0x47d4
testq %rax, %rax
je 0x47f1
movq 0x58(%rsp), %r12
leaq 0xf920(%rip), %rsi      # 0x1402c
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4ab0
leaq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %r15, %rsi
callq 0x117a8
leaq 0x78(%rsp), %r15
movq (%r15), %rdx
leaq 0xf8f2(%rip), %rdi      # 0x1402e
movq %r12, %rsi
xorl %eax, %eax
callq 0x4040
movq %r15, %rdi
callq 0x4170
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x4170
movq 0x248(%rsp), %rdx
leaq 0x10(%rdx), %rcx
orw $-0x1, 0x4(%r15)
addq $0xa0, %rdx
orl $-0x1, (%r15)
subq $0x8, %rsp
leaq 0x80(%rsp), %r15
leaq 0x258(%rsp), %r12
leaq 0x40(%rsp), %r8
leaq 0x20(%rsp), %r9
movq %r15, %rdi
movq %r12, %rsi
pushq $0x0
pushq $0x1388                 # imm = 0x1388
pushq %r14
callq 0x4da0
addq $0x20, %rsp
leaq 0x18(%rsp), %rdi
movq %r12, (%rdi)
movq %r15, 0x8(%rdi)
callq 0x4d2c
jmp 0x4893
leaq 0xf8bd(%rip), %rdi      # 0x14088
callq 0x43a0
pushq $0x1
jmp 0x47eb
testq %rax, %rax
je 0x48c2
leaq 0xf8b4(%rip), %rdi      # 0x14098
callq 0x43a0
pushq $0x3
popq %rbp
jmp 0x4983
leaq 0xf834(%rip), %rsi      # 0x1402c
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4ab0
leaq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %r15, %rsi
callq 0x117a8
leaq 0x78(%rsp), %r14
movq (%r14), %rsi
leaq 0xf7dc(%rip), %rdi      # 0x14004
xorl %eax, %eax
callq 0x4040
movq %r14, %rdi
callq 0x4170
leaq 0x18(%rsp), %rdi
callq 0x4170
movq 0x248(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
subq $0x8, %rsp
leaq 0x80(%rsp), %r14
leaq 0x258(%rsp), %r15
leaq 0x40(%rsp), %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r14, %rdi
movq %r15, %rsi
pushq $0x0
callq 0x4c16
popq %rax
popq %rcx
leaq 0x18(%rsp), %rdi
movq %r15, (%rdi)
movq %r14, 0x8(%rdi)
callq 0x4d2c
leaq 0xf850(%rip), %rcx      # 0x140ea
leaq 0xf842(%rip), %rdi      # 0x140e3
testb %al, %al
cmovneq %rcx, %rdi
xorb $0x1, %al
movzbl %al, %ebp
addl %ebp, %ebp
callq 0x43a0
leaq 0x78(%rsp), %rdi
callq 0x4d74
jmp 0x4983
leaq 0xf763(%rip), %rsi      # 0x1402c
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4ab0
leaq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movq %r15, %rsi
callq 0x117a8
leaq 0x78(%rsp), %r14
movq (%r14), %rsi
leaq 0xf70b(%rip), %rdi      # 0x14004
xorl %eax, %eax
callq 0x4040
movq %r14, %rdi
callq 0x4170
leaq 0x18(%rsp), %rdi
callq 0x4170
movq 0x248(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
leaq 0x78(%rsp), %r14
leaq 0x250(%rsp), %r15
leaq 0x38(%rsp), %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r14, %rdi
movq %r15, %rsi
callq 0x4ece
leaq 0x18(%rsp), %rdi
movq %r15, (%rdi)
movq %r14, 0x8(%rdi)
callq 0x4d2c
leaq 0xf78a(%rip), %rcx      # 0x140ea
leaq 0xf77c(%rip), %rdi      # 0x140e3
testb %al, %al
cmovneq %rcx, %rdi
xorb $0x1, %al
movzbl %al, %ebp
addl %ebp, %ebp
callq 0x43a0
leaq 0x78(%rsp), %rdi
callq 0x4fbe
movq %rbx, %rdi
callq 0xa768
leaq 0x250(%rsp), %rdi
callq 0x11ade
leaq 0x58(%rsp), %rdi
callq 0x4170
leaq 0x38(%rsp), %rdi
callq 0x4170
movl %ebp, %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x4fbe
jmp 0x4a00
jmp 0x49db
jmp 0x49fd
jmp 0x49fd
jmp 0x49ec
jmp 0x49ec
jmp 0x49fd
jmp 0x49fd
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x4d74
jmp 0x4a00
jmp 0x49fd
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x4170
jmp 0x4a00
jmp 0x49fd
movq %rax, %r14
movq %rbx, %rdi
callq 0xa768
jmp 0x4a0d
movq %rax, %r14
leaq 0x250(%rsp), %rdi
callq 0x11ade
jmp 0x4a2c
movq %rax, %r14
leaq 0x150(%rsp), %rdi
callq 0x4ffc
leaq 0x58(%rsp), %rdi
callq 0x4170
leaq 0x38(%rsp), %rdi
jmp 0x4a4f
movq %rax, %r14
jmp 0x4a36
jmp 0x4a56
movq %rax, %r14
leaq 0x150(%rsp), %rdi
callq 0x4170
jmp 0x4a59
movq %rax, %r14
movq %r14, %rdi
callq 0x4410