#!/usr/bin/perl
use v5.14;
use warnings;
use UAV::Pilot::Driver::ARDrone;
use Getopt::Long;

my $HOST         = '192.168.1.1';
my $PORT         = UAV::Pilot::Driver::ARDrone->ARDRONE_PORT_COMMAND;
my $SSID         = undef;
my $MODE_AP      = 0;
my $MODE_JOIN    = 0;
my $MODE_STATION = 0;
my $OWNER_MAC    = undef;
GetOptions(
    'host=s'      => \$HOST,
    'port=i'      => \$PORT,
    'ssid=s'      => \$SSID,
    'join'        => \$MODE_JOIN,
    'ap'          => \$MODE_AP,
    'station'     => \$MODE_STATION,
    'owner-mac=s' => \$OWNER_MAC,
);


my $ardrone = UAV::Pilot::Driver::ARDrone->new({
    host => $HOST,
    ($PORT ? (port => $PORT) : ()),
});
$ardrone->connect;

$ardrone->at_config(
    $ardrone->ARDRONE_CONFIG_NETWORK_SSID_SINGLE_PLAYER,
    $SSID,
) if defined $SSID;

my $mode_setting =
    $MODE_JOIN    ? $ardrone->ARDRONE_CONFIG_NETWORK_WIFI_MODE_JOIN    :
    $MODE_AP      ? $ardrone->ARDRONE_CONFIG_NETWORK_WIFI_MODE_AP      :
    $MODE_STATION ? $ardrone->ARDRONE_CONFIG_NETWORK_WIFI_MODE_STATION :
    undef;
$ardrone->at_config(
    $ardrone->ARDRONE_CONFIG_NETWORK_WIFI_MODE,
    $mode_setting,
) if defined $mode_setting;

$ardrone->at_config(
    $ardrone->ARDRONE_CONFIG_NETWORK_OWNER_MAC,
    $OWNER_MAC,
) if defined $OWNER_MAC;


__END__

=head1 SYNOPSIS

   uav_set_ssid \
       --ssid 'bane_of_cats'
       --host 192.168.1.1 \
       --port 5557 \
       --join    \  # Join a network in Ad-Hoc mode (default)
       --ap      \  # UAV is the access point
       --station \  # Join the network as a station
       --owner-mac <00:00:00:00:00:00>

=head1 DESCRIPTION

Configure the network settings for the AR Parrot UAV.

B<NOTE>: The AR Parrot must be restarted before the changes take effect.

If none of the settings C<--join>, C<--ap>, or C<--station> are set, the UAV is left 
with its current setting.

You can set C<--owner-mac> to C<00:00:00:00:00:00> to unpair the UAV.

=cut
