// File: /workspace/github_cpp_makefile_star2_O1/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 $0x328, %rsp            # imm = 0x328
movq %rsi, %rbx
movl %edi, %ebp
movw $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
movb $0x1, %r14b
cmpl $0x3, %edi
jl 0x45e2
movq 0x8(%rbx), %rsi
leaq 0x78(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x4c86
leaq 0x30(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x139e8
movl %eax, %r14d
xorb $0x1, %r14b
cmpl $0x3, %ebp
jl 0x4608
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4608
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4270
testb %r14b, %r14b
je 0x4623
leaq 0x11ad9(%rip), %rdi     # 0x160ed
callq 0x4380
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x4b35
movq 0x10(%rbx), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x170(%rsp), %rdx
callq 0x4c86
cmpl $0x4, %ebp
jl 0x4644
movq 0x18(%rbx), %rsi
jmp 0x464b
leaq 0x11bfe(%rip), %rsi     # 0x16249
leaq 0x38(%rsp), %rdi
leaq 0x170(%rsp), %rdx
callq 0x4c86
leaq 0x178(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x8(%rbx)
movaps %xmm0, 0x18(%rbx)
movaps %xmm0, -0x8(%rbx)
movq %rbx, 0x10(%rbx)
movq %rbx, 0x18(%rbx)
movq $0x0, 0x20(%rbx)
leaq 0x278(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x13aac
leaq 0x170(%rsp), %r15
movq %r15, %rdi
callq 0x525e
leaq 0x18d88(%rip), %r12     # 0x1d438
movq %r12, (%r15)
leaq 0x18db6(%rip), %r13     # 0x1d470
movq %r13, 0x8(%r15)
leaq 0x18e2b(%rip), %rbp     # 0x1d4f0
movq %rbp, 0x10(%r15)
leaq 0x18e68(%rip), %r14     # 0x1d538
movq %r14, 0x18(%r15)
leaq 0x11b75(%rip), %r8      # 0x16250
leaq 0x278(%rsp), %r15
leaq 0x170(%rsp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
movl $0x3f, %ecx
callq 0x52d6
leaq 0x208(%rsp), %rax
movq %r12, -0x98(%rax)
movq %r13, -0x90(%rax)
movq %rbp, -0x88(%rax)
movq %r14, -0x80(%rax)
movb $0x0, 0x48(%rax)
movq %r15, 0x50(%rax)
leaq 0x30(%rsp), %rcx
movq %rcx, 0x58(%rax)
movq %rax, 0x60(%rax)
leaq 0x98(%rsp), %rdi
movq %r15, (%rdi)
movq %rbx, 0x8(%rdi)
movl $0xbb8, %esi            # imm = 0xBB8
callq 0x4cc4
testb %al, %al
je 0x4870
movq 0x268(%rsp), %rax
leaq 0x10(%rax), %r15
cmpl $0x2, 0x18(%rax)
movq 0x40(%rsp), %rax
jne 0x4886
testq %rax, %rax
je 0x48a5
movq 0x38(%rsp), %r12
leaq 0x20(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x118a1(%rip), %rsi     # 0x1602c
leaq 0x1189b(%rip), %rdx     # 0x1602d
leaq 0x10(%rsp), %rdi
callq 0x6476
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %r15, %rsi
callq 0x13790
leaq 0xa8(%rsp), %r14
movq -0x10(%r14), %rdx
leaq 0x1186a(%rip), %rdi     # 0x1602e
movq %r12, %rsi
xorl %eax, %eax
callq 0x4040
movq -0x10(%r14), %rdi
cmpq %r14, %rdi
je 0x47e7
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
leaq 0x38(%rsp), %r14
je 0x4803
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x268(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
leaq 0x10(%rsp), %r9
movw $0xffff, 0x4(%r9)       # imm = 0xFFFF
movl $0xffffffff, (%r9)      # imm = 0xFFFFFFFF
subq $0x8, %rsp
leaq 0xa0(%rsp), %r15
leaq 0x280(%rsp), %r12
leaq 0x60(%rsp), %r8
movq %r15, %rdi
movq %r12, %rsi
pushq $0x0
pushq $0x1388                 # imm = 0x1388
pushq %r14
callq 0x4fa4
addq $0x20, %rsp
leaq 0x10(%rsp), %rdi
movq %r12, (%rdi)
movq %r15, 0x8(%rdi)
callq 0x4f50
jmp 0x497a
leaq 0x11811(%rip), %rdi     # 0x16088
callq 0x4380
movl $0x1, %ebp
jmp 0x4aea
testq %rax, %rax
je 0x49d1
leaq 0x11802(%rip), %rdi     # 0x16098
callq 0x4380
movl $0x3, %ebp
jmp 0x4aea
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x11776(%rip), %rsi     # 0x1602c
leaq 0x11770(%rip), %rdx     # 0x1602d
leaq 0x10(%rsp), %rdi
callq 0x6476
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %r15, %rsi
callq 0x13790
leaq 0xa8(%rsp), %r14
movq -0x10(%r14), %rsi
leaq 0x11715(%rip), %rdi     # 0x16004
xorl %eax, %eax
callq 0x4040
movq -0x10(%r14), %rdi
cmpq %r14, %rdi
je 0x490f
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x4926
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x268(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
subq $0x8, %rsp
leaq 0xa0(%rsp), %r14
leaq 0x280(%rsp), %r15
leaq 0x60(%rsp), %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r14, %rdi
movq %r15, %rsi
pushq $0x0
callq 0x4dc0
addq $0x10, %rsp
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq %r14, 0x8(%rdi)
callq 0x4f50
leaq 0x11769(%rip), %rcx     # 0x160ea
leaq 0x1175b(%rip), %rdi     # 0x160e3
testb %al, %al
cmovneq %rcx, %rdi
xorb $0x1, %al
movzbl %al, %ebp
addl %ebp, %ebp
callq 0x4380
leaq 0x19257(%rip), %rax     # 0x1dbf8
leaq 0x10(%rax), %rcx
leaq 0x138(%rsp), %rdi
movq %rcx, -0xa0(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x98(%rdi)
addq $0xb0, %rax
movq %rax, -0x90(%rdi)
jmp 0x4ae5
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1164a(%rip), %rsi     # 0x1602c
leaq 0x11644(%rip), %rdx     # 0x1602d
leaq 0x10(%rsp), %rdi
callq 0x6476
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %r15, %rsi
callq 0x13790
leaq 0xa8(%rsp), %r14
movq -0x10(%r14), %rsi
leaq 0x115e9(%rip), %rdi     # 0x16004
xorl %eax, %eax
callq 0x4040
movq -0x10(%r14), %rdi
cmpq %r14, %rdi
je 0x4a3b
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x4a52
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x268(%rsp), %rdx
leaq 0x10(%rdx), %rcx
addq $0xa0, %rdx
leaq 0x98(%rsp), %r14
leaq 0x278(%rsp), %r15
leaq 0x58(%rsp), %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r14, %rdi
movq %r15, %rsi
callq 0x50fc
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq %r14, 0x8(%rdi)
callq 0x4f50
leaq 0x11647(%rip), %rcx     # 0x160ea
leaq 0x11639(%rip), %rdi     # 0x160e3
testb %al, %al
cmovneq %rcx, %rdi
xorb $0x1, %al
movzbl %al, %ebp
addl %ebp, %ebp
callq 0x4380
leaq 0x1903d(%rip), %rax     # 0x1db00
leaq 0x10(%rax), %rcx
leaq 0x110(%rsp), %rdi
movq %rcx, -0x78(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x70(%rdi)
addq $0xa8, %rax
movq %rax, -0x68(%rdi)
callq 0x5af2
movq %rbx, %rdi
callq 0xbdce
leaq 0x278(%rsp), %rdi
callq 0x13b88
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4b1a
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4270
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4b35
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4270
movl %ebp, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x18fad(%rip), %rax     # 0x1db00
leaq 0x10(%rax), %rcx
leaq 0x110(%rsp), %rdi
movq %rcx, -0x78(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x70(%rdi)
addq $0xa8, %rax
movq %rax, -0x68(%rdi)
jmp 0x4bc7
jmp 0x4b92
jmp 0x4bee
jmp 0x4bee
jmp 0x4b7f
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
jne 0x4bdd
jmp 0x4bf1
jmp 0x4bee
jmp 0x4bee
movq %rax, %r14
leaq 0x1905c(%rip), %rax     # 0x1dbf8
leaq 0x10(%rax), %rcx
leaq 0x138(%rsp), %rdi
movq %rcx, -0xa0(%rdi)
leaq 0x60(%rax), %rcx
movq %rcx, -0x98(%rdi)
addq $0xb0, %rax
movq %rax, -0x90(%rdi)
callq 0x5af2
jmp 0x4bf1
jmp 0x4bee
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x4bf1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4270
jmp 0x4bf1
jmp 0x4bee
movq %rax, %r14
movq %rbx, %rdi
callq 0xbdce
jmp 0x4bfe
movq %rax, %r14
leaq 0x278(%rsp), %rdi
callq 0x13b88
jmp 0x4c1d
movq %rax, %r14
leaq 0x170(%rsp), %rdi
callq 0x525e
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c3d
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4270
jmp 0x4c3d
movq %rax, %r14
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c7d
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4270
jmp 0x4c7d
jmp 0x4c7a
movq %rax, %r14
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c7d
movq 0x88(%rsp), %rsi
jmp 0x4c50
movq %rax, %r14
movq %r14, %rdi
callq 0x43e0
nop