/*
    Robot server - 2005 Jason Hunt
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    Contact: huntjas2@msu.edu
*/


#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "version.h"
#include "server.h"
#include "camera.h"
#include "failsafe.h"
#include "controller.h"
#include "network.h"
#include "gun.h"


int program_done = 0;
          

/* get a string ftom the console */
void get_console_str(char *buffer, unsigned int max_len)
{
    int line_done = 0, i = 0;
    char tmp;

    while (!line_done)
    {
        tmp = getchar();

        if (tmp == '\n' || i == max_len -1) 
        {
            line_done = 1;
            buffer[i] = 0;
        }else
            buffer[i++] = tmp;

    }
}
/* end of get_console */


/* display the help menu */
void show_help(void)
{
    printf("Commands:\n");
    printf(" exit - exit program\n");
    printf(" help - show this menu\n");
    printf("\n");
}    
/* end of show help */
    

/* parse console command */
void parse_command(char *line)
{
    if (!strcmp(line, "stop")) send_serial(0x50); else
    if (!strcmp(line, "up")) send_serial(0x10); else
    if (!strcmp(line, "down")) send_serial(0x20); else
    if (!strcmp(line, "left")) send_serial(0x30); else
    if (!strcmp(line, "right")) send_serial(0x40); else
    if (!strcmp(line, "fire")) send_serial(0x60); else
    
    if (!strcmp(line, "exit")) program_done = 1; else
    if (!strcmp(line, "help")) show_help(); else
        printf("%s: unknown command\n", line);
}
/* end of parse_command */


/* main */
int main (int argc, char *argv[]) 
{
    printf("\n%s - Jason Hunt\n", VERSION);
    
    pthread_t network_t, camera_t, failsafe_t, gun_t;
 
    // reset controller, first thing to do
    reset_controller();
 
    if (setup_network())
    {
        printf("Network setup failed\n");
        return(1);
    }
    
    if (setup_camera_server())
    {
        printf("Camera server setup failed\n");
        return(1);       
    }

    // start the failsafe
    pthread_create(&failsafe_t, NULL, failsafe, NULL);
    
    // take care of network connections
    pthread_create(&network_t, NULL, control_network, NULL);

    // take care of camera connections
    pthread_create(&camera_t, NULL, camera_server, NULL);

    // take care of camera connections
    pthread_create(&gun_t, NULL, gun_control, NULL);


//    sleep(1);
    
    // command loop
    char buffer[100];

    //while(!program_done)
    
    
    while(1)
    {

      sleep(1);
      }
    
    
    /*{
        printf("> ");
        get_console_str(buffer, 100);
            

    if (strcmp(buffer, ""))
            parse_command(buffer);
    }*/

    printf("Program shutdown\n");

    close(server_sd);
    close(camera_server_sd);

    reset_controller();


    // wait for threads    
    sleep(1);
}
/* end of main */




