#include <ctype.h> 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h> 
#define LOCALHOST_IP    "127.0.0.1"
#define DEFAULT_SYS_ID  1
static const char* dest_ip;
static uint8_t my_sys_id;
static uint16_t port;
static int running = 0;
static void __print_usage(void)
{
        fprintf(stderr,"usage: rc_test_mavlink [-h] [-a dest_ip_addr] [-p port] [-s sys_id]\n");
        return;
}
static int __parse_args(int argc, char * argv[])
{
        int c,tmp;
        opterr = 0;
        while ((c = getopt(argc, argv, "ha:p:s:")) != -1)
                switch (c)
                {
                case 'h':
                        __print_usage();
                        exit(0);
                        break;
                case 'a':
                        dest_ip = optarg;
                        break;
                case 'p':
                        tmp = atoi(optarg);
                        if(tmp<0){
                                fprintf(stderr, "UDP port must be greater than 0\n");
                                exit(-1);
                        }
                        if(tmp>UINT16_MAX){
                                fprintf(stderr, "UDP port must be less than %d\n", UINT16_MAX);
                                exit(-1);
                        }
                        if(tmp<1024){
                                fprintf(stderr, "WARNING: ports less than 1024 are privaledged ports\n");
                        }
                        port=tmp;
                        break;
                case 's':
                        tmp=atoi(optarg);
                        if(tmp>UINT8_MAX||tmp<0){
                                fprintf(stderr, "sys_id must be between 0 and %d\n", UINT8_MAX);
                                exit(-1);
                        }
                        my_sys_id = tmp;
                        break;
                case '?':
                        if (optopt == 'a' || optopt=='p' || optopt=='s')
                                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
                        else if (isprint (optopt))
                                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
                        else
                                fprintf (stderr,"Unknown option `\\x%x'.\n",optopt);
                        __print_usage();
                        exit(-1);
                default:
                        __print_usage();
                        exit(-1);
                }
        return 0;
}
static void __callback_func_any(void)
{
        printf("received msg_id: %d ", msg_id);
        
        
        printf(" from sysid: %d \n", sysid);
        return;
}
static void __callback_func_connection_lost(void)
{
        fprintf(stderr,"CONNECTION LOST\n");
        return;
}
static void __signal_handler(__attribute__ ((unused)) int dummy)
{
        running=0;
        return;
}
int main(int argc, char * argv[])
{
        
        dest_ip=LOCALHOST_IP;
        my_sys_id=DEFAULT_SYS_ID;
        
        if(__parse_args(argc,argv)){
                fprintf(stderr,"failed to parse arguments\n");
                return -1;
        }
        printf("run with -h option to see usage and other options\n");
        
        printf("\n");
        printf("Initializing with the following settings:\n");
        printf("dest ip addr: %s\n", dest_ip);
        printf("my system id: %d\n", my_sys_id);
        printf("UDP port: %d\n", port);
        printf("\n");
        
        signal(SIGINT, __signal_handler);
        
                return -1;
        }
        
        running=1;
        while(running){
                sleep(1);
                        fprintf(stderr,"failed to send heartbeat\n");
                }
                else{
                        printf("sent heartbeat\n");
                }
        }
        
        printf("closing UDP port\n");
}