// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movw $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
cmpl $0x3, %edi
jl 0x462b
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8(%rsi), %rsi
leaq 0x140(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x4c54
leaq 0x20(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x134b4
movl %eax, %r14d
leaq 0x150(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4602
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x4270
testb %r14b, %r14b
je 0x462b
movq 0x10(%rbx), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x140(%rsp), %rdx
callq 0x4c54
cmpl $0x3, %ebp
jne 0x4650
leaq 0x11c20(%rip), %rsi     # 0x16249
jmp 0x4654
leaq 0x11abb(%rip), %rdi     # 0x160ed
callq 0x4380
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %rdi
leaq 0x140(%rsp), %rdx
callq 0x4c54
leaq 0x148(%rsp), %r13
xorl %eax, %eax
movq %rax, 0x8(%r13)
xorps %xmm0, %xmm0
movaps %xmm0, -0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rax, 0x20(%r13)
leaq 0x248(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x1356c
leaq 0x140(%rsp), %r12
movq %r12, %rdi
callq 0x522c
leaq 0x18e74(%rip), %r15     # 0x1d528
movq %r15, (%r12)
leaq 0x18ea1(%rip), %rbp     # 0x1d560
movq %rbp, 0x8(%r12)
leaq 0x18f15(%rip), %r14     # 0x1d5e0
movq %r14, 0x10(%r12)
leaq 0x18f51(%rip), %rbx     # 0x1d628
movq %rbx, 0x18(%r12)
leaq 0x11b6d(%rip), %r8      # 0x16250
leaq 0x248(%rsp), %r12
leaq 0x140(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
movl $0x3f, %ecx
callq 0x52a4
leaq 0x1d8(%rsp), %rax
movq %r15, -0x98(%rax)
movq %rbp, -0x90(%rax)
movq %r14, -0x88(%rax)
movq %rbx, -0x80(%rax)
movb $0x0, 0x48(%rax)
movq %r12, 0x50(%rax)
leaq 0x20(%rsp), %rcx
movq %rcx, 0x58(%rax)
movq %rax, 0x60(%rax)
leaq 0x68(%rsp), %rdi
movq %r12, (%rdi)
movq %r13, 0x8(%rdi)
movl $0xbb8, %esi            # imm = 0xBB8
callq 0x4c92
testb %al, %al
je 0x4865
movq 0x238(%rsp), %rax
leaq 0x10(%rax), %r15
cmpl $0x2, 0x18(%rax)
movq 0x30(%rsp), %rax
jne 0x487b
testq %rax, %rax
je 0x489a
movq %r13, %rbp
movq 0x28(%rsp), %r12
leaq 0x10(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x11899(%rip), %rsi     # 0x1602c
leaq 0x11893(%rip), %rdx     # 0x1602d
movq %rsp, %rdi
callq 0x642c
leaq 0x68(%rsp), %rdi
movq %rsp, %rdx
movq %r15, %rsi
callq 0x1325c
leaq 0x78(%rsp), %rbx
movq -0x10(%rbx), %rdx
leaq 0x1186c(%rip), %rdi     # 0x1602e
movq %r12, %rsi
xorl %eax, %eax
callq 0x4040
movq -0x10(%rbx), %rdi
cmpq %rbx, %rdi
je 0x47e2
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4270
movq (%rsp), %rdi
cmpq %r13, %rdi
leaq 0x28(%rsp), %rbx
je 0x47fd
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x238(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
movq %rsp, %r9
movw $0xffff, 0x4(%r9)       # imm = 0xFFFF
movl $0xffffffff, (%r9)      # imm = 0xFFFFFFFF
subq $0x8, %rsp
leaq 0x70(%rsp), %r15
leaq 0x250(%rsp), %r12
leaq 0x50(%rsp), %r8
movq %r15, %rdi
movq %r12, %rsi
pushq $0x0
pushq $0x1388                 # imm = 0x1388
pushq %rbx
movq %rbp, %r13
callq 0x4f72
addq $0x20, %rsp
movq %rsp, %rdi
movq %r12, (%rdi)
movq %r15, 0x8(%rdi)
callq 0x4f1e
jmp 0x495c
leaq 0x1181c(%rip), %rdi     # 0x16088
callq 0x4380
movl $0x1, %ebp
jmp 0x4ab9
testq %rax, %rax
je 0x49b3
leaq 0x1180d(%rip), %rdi     # 0x16098
callq 0x4380
movl $0x3, %ebp
jmp 0x4ab9
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x11781(%rip), %rsi     # 0x1602c
leaq 0x1177b(%rip), %rdx     # 0x1602d
movq %rsp, %rdi
callq 0x642c
leaq 0x68(%rsp), %rdi
movq %rsp, %rdx
movq %r15, %rsi
callq 0x1325c
leaq 0x78(%rsp), %rbx
movq -0x10(%rbx), %rsi
leaq 0x1172a(%rip), %rdi     # 0x16004
xorl %eax, %eax
callq 0x4040
movq -0x10(%rbx), %rdi
cmpq %rbx, %rdi
je 0x48f7
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4270
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x490d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x238(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
subq $0x8, %rsp
leaq 0x70(%rsp), %r14
leaq 0x250(%rsp), %r15
leaq 0x50(%rsp), %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r14, %rdi
movq %r15, %rsi
pushq $0x0
callq 0x4d8e
addq $0x10, %rsp
movq %rsp, %rdi
movq %r15, (%rdi)
movq %r14, 0x8(%rdi)
callq 0x4f1e
leaq 0x11787(%rip), %rcx     # 0x160ea
leaq 0x11779(%rip), %rdi     # 0x160e3
testb %al, %al
cmovneq %rcx, %rdi
xorb $0x1, %al
movzbl %al, %ebp
addl %ebp, %ebp
callq 0x4380
leaq 0x19265(%rip), %rax     # 0x1dbe8
leaq 0x10(%rax), %rcx
leaq 0x108(%rsp), %rdi
movq %rcx, -0xa0(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x98(%rdi)
addq $0xb0, %rax
movq %rax, -0x90(%rdi)
jmp 0x4ab4
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x11668(%rip), %rsi     # 0x1602c
leaq 0x11662(%rip), %rdx     # 0x1602d
movq %rsp, %rdi
callq 0x642c
leaq 0x68(%rsp), %rdi
movq %rsp, %rdx
movq %r15, %rsi
callq 0x1325c
leaq 0x78(%rsp), %rbx
movq -0x10(%rbx), %rsi
leaq 0x11611(%rip), %rdi     # 0x16004
xorl %eax, %eax
callq 0x4040
movq -0x10(%rbx), %rdi
cmpq %rbx, %rdi
je 0x4a10
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4270
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x4a26
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x238(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
leaq 0x68(%rsp), %r14
leaq 0x248(%rsp), %r15
leaq 0x48(%rsp), %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r14, %rdi
movq %r15, %rsi
callq 0x50ca
movq %rsp, %rdi
movq %r15, (%rdi)
movq %r14, 0x8(%rdi)
callq 0x4f1e
leaq 0x11678(%rip), %rcx     # 0x160ea
leaq 0x1166a(%rip), %rdi     # 0x160e3
testb %al, %al
cmovneq %rcx, %rdi
xorb $0x1, %al
movzbl %al, %ebp
addl %ebp, %ebp
callq 0x4380
leaq 0x1905e(%rip), %rax     # 0x1daf0
leaq 0x10(%rax), %rcx
leaq 0xe0(%rsp), %rdi
movq %rcx, -0x78(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x70(%rdi)
addq $0xa8, %rax
movq %rax, -0x68(%rdi)
callq 0x5ab6
movq %r13, %rdi
callq 0xbb74
leaq 0x248(%rsp), %rdi
callq 0x13648
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4ae9
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4270
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x463c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4270
jmp 0x463c
movq %r13, %rbp
movq %rax, %r14
leaq 0x18fd6(%rip), %rax     # 0x1daf0
leaq 0x10(%rax), %rcx
leaq 0xe0(%rsp), %rdi
movq %rcx, -0x78(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x70(%rdi)
addq $0xa8, %rax
movq %rax, -0x68(%rdi)
jmp 0x4b93
movq %r13, %rbp
jmp 0x4b5e
jmp 0x4bb9
jmp 0x4bb9
jmp 0x4b49
movq %r13, %rbp
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r12, %rdi
jne 0x4ba8
jmp 0x4bbf
jmp 0x4bb9
jmp 0x4bb9
movq %rax, %r14
leaq 0x19080(%rip), %rax     # 0x1dbe8
leaq 0x10(%rax), %rcx
leaq 0x108(%rsp), %rdi
movq %rcx, -0xa0(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x98(%rdi)
addq $0xb0, %rax
movq %rax, -0x90(%rdi)
callq 0x5ab6
jmp 0x4bbf
jmp 0x4bbc
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r13, %rdi
je 0x4bbf
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4270
jmp 0x4bbf
jmp 0x4bbc
movq %r13, %rbp
movq %rax, %r14
movq %rbp, %rdi
callq 0xbb74
jmp 0x4bcc
movq %rax, %r14
leaq 0x248(%rsp), %rdi
callq 0x13648
jmp 0x4beb
movq %rax, %r14
leaq 0x140(%rsp), %rdi
callq 0x522c
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c0b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4270
jmp 0x4c0b
movq %rax, %r14
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c4b
movq 0x58(%rsp), %rsi
jmp 0x4c3e
jmp 0x4c48
movq %rax, %r14
leaq 0x150(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c4b
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x4270
jmp 0x4c4b
movq %rax, %r14
movq %r14, %rdi
callq 0x43e0
nop