Example #1
0
File: main.c Project: f4exb/tnc1101
// ------------------------------------------------------------------------------------------------
// Terminator
static void terminate(const int signal_) {
// ------------------------------------------------------------------------------------------------
    printf("PICC: Terminating with signal %d\n", signal_);
    close_serial(&serial_parms_usb);
    close_serial(&serial_parms_ax25);
    delete_args(&arguments);
    exit(1);
}
Example #2
0
int main(int argc,char *argv[])
{
	int i;
	for(i=1;i<argc;i++)
	{
		if (!strcmp(argv[i], "-f"))
		{
			strcpy(input_file, argv[i+1]);
			printf("Input file: %s\n", input_file);
			i++;
		}
		else if (!strcmp(argv[i], "-o"))
		{
			strcpy(output_device, argv[i+1]);
			printf("Output device: %s\n", output_device);
			i++;
		}
		else if (!strcmp(argv[i], "-p"))
		{
			pause_between_block = atoi(argv[i+1]);
			printf("Pause between blocks: %u\n", pause_between_block);
			i++;
		}
		else if (!strcmp(argv[i], "-b"))
		{
			baud_rate = atoi(argv[i+1]);
			printf("Baud rate: %u\n", baud_rate);
			i++;
		}
		else if (!strcmp(argv[i], "-v"))
		{
			verbose = 1;
			printf("Verbose mode on\n");
		}
		else
		{
			printf("-f: Absolute path file input\n-o: Serial output device\n-p: Pause between sysex blocks(100ms)\n-b: Baud rate(115200)\n-v: Verbose mode\n");
			exit(EXIT_FAILURE);
		}
	}
	file_descriptor = open_serial(output_device, baud_rate);
	if (file_descriptor > -1)
	{
		upload_file(input_file, output_device, pause_between_block);
		if (close_serial(file_descriptor) > -1)
			exit(EXIT_SUCCESS);
		else
		{
			printf("Error while closing the serial line...\n");
			exit(EXIT_FAILURE);
		}
	}
	else
	{
		printf("Aborting...\n");
		exit(EXIT_FAILURE);
	}
}
Example #3
0
File: run.c Project: Yunaik/asp
int main(int argc, char argv[])
{
//Serial Configs

    initialize_defaults();
    open_serial();

//Write some message
    /*sleep(1);*/
    /*set_servo(2000);*/
    /*sleep(1);*/
    /*set_servo(1000);*/
    /*sleep(1);*/
    /*set_servo(1500);*/
    /*sleep(1);*/
    /*release_channel();*/
    /*sleep(1);*/

//Reading messages
    /*while (!current_messages.sysid){*/
    /*read_messages(&current_messages);*/
    /*}*/
    Mavlink_Messages current_messages;
    Mavlink_Messages output;
    int i ;
    for(i = 0; i < 1000; i++)
    {
    read_messages(&output, &output);
    }
    mavlink_servo_output_raw_t servo = output.servo_output_raw;
    printf("Printing Servo output raw\n");
    printf("Printing Servo 1:  %u \n", (unsigned int)servo.servo1_raw);
    printf("Printing Servo 2:  %u \n", (unsigned int)servo.servo2_raw);
    printf("Printing Servo 3:  %u \n", (unsigned int)servo.servo3_raw);
    printf("Printing Servo 4:  %u \n", (unsigned int)servo.servo4_raw);
    printf("Printing Servo 5:  %u \n", (unsigned int)servo.servo5_raw);
    printf("Printing Servo 6:  %u \n", (unsigned int)servo.servo6_raw);
    printf("Printing Servo 7:  %u \n", (unsigned int)servo.servo7_raw);
    printf("Printing Servo 8:  %u \n", (unsigned int)servo.servo8_raw);
    mavlink_global_position_int_t global_position_int = output.global_position_int;
    printf("LAT: %u \n", (unsigned int)global_position_int.lat);

    /*mavlink_global_position_int_t gposint = messages.global_position_int;*/
    /*printf("Lat: %u \n", (unsigned int)gposint.lat);*/
    /*printf("Lat: %u \n", (unsigned int)gposint.lon);*/

    /*mavlink_local_position_ned_t locpos = messages.local_position_ned;*/
    /*printf("x: %u \n", (unsigned int)locpos.x);*/
    /*printf("y: %u \n", (unsigned int)locpos.y);*/
    /*printf("z: %u \n", (unsigned int)locpos.z);*/

//Serial Port stuff
    close_serial();
    sleep(1);
    return 0;
}    
Example #4
0
/**
 * Set up termination handlers when receiving signals.
 *
 * @param signum The number of the signal received by the daemon.
 *
 */
static void termination_handler(int signum)
{
	switch (signum) {
	case SIGTERM:
		close_serial();
		exit(EXIT_SUCCESS);
	default:
		break;
	}
}
Example #5
0
File: run.c Project: Yunaik/asp
int main(int argc, char argv[])
{
//Serial Configs

    initialize_defaults();
    open_serial();

    message_interval(100000, 33);
//Serial Port stuff
    close_serial();
    sleep(1);
    return 0;
}    
/**
 * This function properly closes all subsystems and communication
 * channels before terminating the BaseStation program.
 */
void
quit_basestation ()
{
    print_log ("Exiting...", Log_Level_High);

    close_ctrl ();

    if (m_comm_mode == Comm_Mode_Offline)
    {
        close_serial ();
    }

	exit (0);
}
int main (int argc, char const * argv [])

{
	static char const * optv [] =
	{
		"cl:qv",
		"",
		"Atheros UART Device Detector",
		"c\tassume device is in command mode",
		"l f\tserial port is (f) [" SERIAL_PORT "]",
		"q\tquiet mode",
		"v\tverbose mode",
		(char const *) (0)
	};
	signed c;
	char const *line = SERIAL_PORT;
	struct serial serial;
	struct serial_mode serial_mode;
	flag_t flags = 0;
	optind = 1;
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch ((char) (c))
		{
		case 'c':
			_setbits (flags, INT6KDETECT_CMD_MODE);
			break;
		case 'l':
			line = optarg;
			break;
		case 'q':
			_setbits (flags, INT6KDETECT_QUIET);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (open_serial (line, &serial) == -1) error (1, errno, "could not open %s", line);
	if (detect (&serial, &serial_mode, flags) == -1) error (1, 0, "could not detect device");
	printf ("Detected the following serial mode:\n");
	dump_serial_mode (&serial_mode);
	close_serial (&serial);
	return (0);
}
Example #8
0
int main(int argc, char* argv[])
{
    initialize_defaults();
    open_serial();



    uart_name = "/dev/ttyAMA0";
    baudrate = 57600;
    uint16_t    u = 0xFFFF;

    sleep(1);
    mavlink_rc_channels_override_t rco;

    rco.chan1_raw = 1500; 
    rco.chan2_raw = 1500; 
    rco.chan3_raw = 1500; 
    rco.chan4_raw = 2000; 
    rco.chan5_raw = 1500; 
    rco.chan6_raw = 1500; 
    rco.chan7_raw = 1500; 
    rco.chan8_raw = 1500; 
    rco.target_system =0;
    rco.target_component = 0;
    mavlink_message_t message;
    /*int i;*/
    /*for(i = 0; i<1; i++){*/
        mavlink_msg_rc_channels_override_encode(0xff,0,&message, &rco);
        int len = write_message(message);
    /*}*/
        sleep(1);
        mavlink_msg_rc_channels_override_pack(0xff,0, &message,0,0,u,u,u,1000,u,u,u,u );
        len = write_message(message);
        sleep(1);
        mavlink_msg_rc_channels_override_pack(0xff,0, &message,0,0,u,u,u,1500,u,u,u,u );
        len = write_message(message);
        sleep(1);
        mavlink_msg_rc_channels_override_pack(0xff,0, &message,0,0,0,0,0,0,0,0,0,0 );
        len = write_message(message);
    
    sleep(1);
    close_serial();
    sleep(1);
	pthread_mutex_destroy(&lock);
    return 0;
}
Example #9
0
File: main.c Project: f4exb/tnc1101
// ------------------------------------------------------------------------------------------------
int main (int argc, char **argv)
// ------------------------------------------------------------------------------------------------
{
    int i, nbytes;

    // unsolicited termination handling
    struct sigaction sa;
    // Catch all signals possible on process exit!
    for (i = 1; i < 64; i++) 
    {
        // skip SIGUSR2 for Wiring Pi
        if (i == 17)
            continue; 

        // These are uncatchable or harmless or we want a core dump (SEGV) 
        if (i != SIGKILL
            && i != SIGSEGV
            && i != SIGSTOP
            && i != SIGVTALRM
            && i != SIGWINCH
            && i != SIGPROF) 
        {
            memset(&sa, 0, sizeof(sa));
            sa.sa_handler = terminate;
            sigaction(i, &sa, NULL);
        }
    }

    // Set argument defaults
    init_args(&arguments); 

    // Parse arguments 
    argp_parse (&argp, argc, argv, 0, 0, &arguments);

    if (arguments.print_long_help)
    {
        print_long_help();
        return 0;
    }
    
    if (!arguments.usbacm_device)
    {
        arguments.usbacm_device = strdup("/dev/ttyACM2");
    }
    
    if (!arguments.serial_device)
    {
        arguments.serial_device = strdup("/var/ax25/axp2");
    }
    
    if (!arguments.bulk_filename)
    {
        arguments.bulk_filename = strdup("-");
    }

    set_serial_parameters(&serial_parms_ax25, arguments.serial_device, get_serial_speed(arguments.serial_speed, &arguments.serial_speed_n));
    set_serial_parameters(&serial_parms_usb,  arguments.usbacm_device, get_serial_speed(115200, &arguments.usb_speed_n));
    init_radio_parms(&radio_parms, &arguments);

    if (arguments.verbose_level > 0)
    {
        print_args(&arguments);
        print_radio_parms(&radio_parms);
        fprintf(stderr, "\n");
    }

    if (arguments.tnc_mode == TNC_KISS)
    {
        kiss_init(&arguments);
        kiss_run(&serial_parms_ax25, &serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_SLIP)
    {
        arguments.slip = 1;
        kiss_init(&arguments);
        kiss_run(&serial_parms_ax25, &serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_TEST_USB_ECHO) // This one does not need any access to the radio
    {
        usb_test_echo(&serial_parms_usb, &arguments);
    }
    else if (arguments.tnc_mode == TNC_RADIO_STATUS) 
    {
        print_radio_status(&serial_parms_usb, &arguments);
    }
    else if (arguments.tnc_mode == TNC_RADIO_INIT)
    {
        init_radio(&serial_parms_usb, &radio_parms, &arguments);

        if (nbytes < 0)
        {
            fprintf(stderr, "Error\n");
        }
    }
    else if (arguments.tnc_mode == TNC_TEST_TX)
    {
        radio_transmit_test(&serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_TEST_RX)
    {
        radio_receive_test(&serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_TEST_ECHO_TX)
    {
        radio_echo_test(&serial_parms_usb, &radio_parms, &arguments, 1);
    } 
    else if (arguments.tnc_mode == TNC_TEST_ECHO_RX)
    {
        radio_echo_test(&serial_parms_usb, &radio_parms, &arguments, 0);
    }
    else if (arguments.tnc_mode == TNC_TEST_TX_PACKET)
    {
        radio_packet_transmit_test(&serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_TEST_RX_PACKET)
    {
        radio_packet_receive_test(&serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_TEST_RX_PACKET_NON_BLOCKING)
    {
        radio_packet_receive_nb_test(&serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_BULK_TX)
    {
        file_bulk_transmit(&serial_parms_usb, &radio_parms, &arguments);
    }
    else if (arguments.tnc_mode == TNC_BULK_RX)
    {
        file_bulk_receive(&serial_parms_usb, &radio_parms, &arguments);
    }

    close_serial(&serial_parms_usb);
    close_serial(&serial_parms_ax25);
    delete_args(&arguments);
    return 0;
}
Example #10
0
int main(int argc, char* argv[]){
  int x, y, result;

  int ch;
  while ((ch = getopt(argc, argv, "bpthro:123T?")) != -1) {
    switch (ch) {
    case 'b': show_board_status = FALSE;  break;
    case 'p': show_placed_tile = TRUE; break;
    case 't': show_available_tiles = FALSE; break;
    case 'h': show_hint = TRUE; break;
    case 'r': first_player = 2; break; // player 2 moves first
    case '1': on_serial = 1; break; // player 1 on serial
    case '2': on_serial = 2; break; // player 2 on serial
    case '3': on_serial = 3; break; // both player on serial
    case 'T': use_tcp = TRUE; break; // use TCP
    case 'o': move_timeout = atoi(optarg); break; // set timeout
    case '?':
    default:
      usage();
      return 0;
    }
  }

  init_serial();

  printf(">> %s %s vs %s %s \n", 
	 team_ids[0], ((first_player==1) ? "*" : ""),
	 team_ids[1], ((first_player==2) ? "*" : ""));

  // ------------------------------
  // clear board & available pieces

  for(y=0; y<16; y++)
    for(x=0; x<16; x++)
      board[y][x] = 0;

  for(y=0; y<2; y++)
    for(x=0; x<21; x++)
      available[y][x] = 1;

  // setup board: border is already filled
  for(x=0; x<16; x++){
    board[ 0][ x] = BORDER;
    board[15][ x] = BORDER;
    board[ x][ 0] = BORDER;
    board[ x][15] = BORDER;
  }

#ifdef DEBUG_FILL
  // for test (some grids already occupied on start)
  for(y=7; y<=14; y++)
    for(x=12; x<=14; x++)
      board[y][x] = 2;

  for(y=12; y<=14; y++)
    for(x=7; x<=14; x++)
      board[y][x] = 2;
#endif

#ifdef DEBUG_LESS_TILES
  for(x=5; x<21; x++){
    available[0][x] = 0;
    available[1][x] = 0;
  }
#endif


  switch(result = do_game()){
    int a1, a2;
    
  case TERMINATE_NORMAL:
    a1 = remaining_size(1);
    a2 = remaining_size(2);
    
    printf("** Total remaining size: %d / %d. ", a1, a2);
    if(a1 != a2)
      printf("Player %d won the game!\n", ( (a1<a2) ? 1 : 2 ) );
    else
      printf("Draw game.\n");
    break;

  case 1:
  case 2:
    printf("** Player %d lost the game by violation.\n", result);
    break;

  default:
  case TERMINATE_WRONG:
    printf("** Game terminated unexpectedly.\n");
    break;
  }
  
  close_serial();
  return 0;
}
Example #11
0
File: run.c Project: Yunaik/asp
int main(int argc, char argv[]){

    //Serial Configs
    initialize_defaults();
    open_serial();

    //Frequency of linux/raspi to check at the port for new messages
    int f_linux = 10;
    
    int wert;
    int check=0;
    int unique_counter =0;

    int counter = 0;
    int unique_list[100];
    int liste[100];
    int time_start;
    int time_stop;
    int error_counter = 0;
    float t_sample_linux = 1.0/(float)f_linux;
    int sleep_time = t_sample_linux*1e6;
    float t_diff_max = 0;
    float sum_t_diff = 0;
    /*int fails = 0;*/
        mavlink_message_t message; 

//..........
    time_start = get_time_usec();
    
    // Read out all the messages in a for loop that has 1000 iterations (about 0.25s) and storing them into a list (unsorted and not filtered)
    while((get_time_usec()-time_start)/1000000.0 < 2)
    {
        /*int success = read_messages(&all_counter, &wert);*/
        int success = read_message(&message, &all_counter, &wert);
        float t_px = messages.global_position_int.time_boot_ms/1000.0 - t_px_start;
        float t_linux= (get_time_usec() -time_start)/1e6;
        float t_diff_now = t_linux - t_px;
        printf("Pixhawk Time: %0.3f , Linux Time: %0.3f, Time difference: %0.4f \n", t_px, t_linux, t_diff_now);
        sum_t_diff += t_diff_now;
        if (t_diff_now > t_diff_max)
        {
            t_diff_max = t_diff_now;
        }
        liste[counter] = wert;
        if(success == 1)
        {
            counter++;
        }
        else
        {
            error_counter++;    
        }
        time_stop = get_time_usec();
        
        usleep(sleep_time);
    }

    float t_diff = (time_stop - time_start)/1000000.0;
    int int_t_diff = t_diff*1e6;
//..........
    float scale  = 1/t_diff;
    float frequency = scale*all_counter;
    float t_sample = 1/frequency;
    printf("\n -----------------------------\n");
    printf("Received %i messages in %f secs (Real time)\n", all_counter, t_diff);
    printf("Time past according to Pixhawk: %f \n -----------------\n", t_px_stop - t_px_start);
    printf("Sample time of Linux: %0.3f , Frequency of Linux: %i \n", t_sample_linux, f_linux);
    printf("Sample time of Pixhawk: %0.3f and Frequency: %0.1f \n -------------\n", t_sample, frequency);
    int times_called = error_counter + all_counter;
    printf("Error counter: %i, Times called: %i \n", error_counter, times_called);
    /*printf("Time for a while loop: %i us \n", (int_t_diff - times_called*sleep_time)/times_called);*/
    /*printf("int_t_diff %i, times_called %i, sleep_time %i \n", int_t_diff, times_called, sleep_time);*/
    printf("Maximum time difference between linux and pixhawk clock: %f and mean time difference: %f \n", t_diff_max, sum_t_diff/times_called);

    printf("---------------------\n");

    printf("Packets dropped: %i \n", packets_dropped);


/*// Generating a list with unique values*/
    /*for(i=0; i<all_counter; i++){*/
    /*wert = liste[i];*/
        /*check = 0;*/
        /*int a;*/
        /*for(a=0;a<unique_counter;a++){*/
            /*if(wert ==unique_list[a]){check =1;}*/
        /*}*/
    
        /*if (check == 0){*/
        /*unique_list[unique_counter] = wert;*/
        /*unique_counter++;*/
        /*}*/
    /*}*/

    /*qsort(unique_list, unique_counter, sizeof(int), cmpfcn);*/

    /*int unique_count_list[100] = {0}; //variable size cannot be initialized*/
    /*[>for (i=0; i < unique_counter; i++){<]*/
    /*[>printf("value of ucl %i \n", unique_count_list[i]);<]*/
    /*[>}<]*/
    
    /*for(i=0; i < unique_counter; i++){*/
        /*int unique_value = unique_list[i];*/
        /*[>printf("\n ----------\n unique_value %i \n", unique_value);<]*/
        /*int a;*/
        /*for(a=0; a < all_counter; a++){*/
            /*[>printf("liste[a] %i \n", liste[a]);<]*/
            /*if(unique_value == liste[a]){*/
                /*unique_count_list[i] += 1;*/
                /*[>printf("unique_coutn_value %i \n",unique_count_list[i]);<]*/
            /*}*/
        /*}*/
    /*}*/

/*//Printing the results*/
    /*printf("Got %i unique values: \n", unique_counter);*/
    

    /*for (i = 0; i < unique_counter; i++){*/
        /*printf("%i: %i times\n", unique_list[i], unique_count_list[i]);*/
    /*}*/
    /*printf("..............................\n");*/



//Serial Port stuff
    close_serial();
    sleep(1);
    return 0;
}    
Example #12
0
File: run.c Project: Yunaik/asp
int main(int argc, char argv[]) {
//Serial Configs

    initialize_defaults();
    open_serial();

//Reading messages
    //FREQUENCY OF READ MSG
    int f_linux = 10;

    int i ;
    int wert;
    int check=0;
    int unique_counter =0;

    int counter = 0;
    int unique_list[100];
    int liste[100];
    int time_start;
    int time_stop;
    int error_counter = 0;
    float t_sample_linux = 1.0/(float)f_linux;
    int sleep_time = t_sample_linux*1e6;
    /*int fails = 0;*/
//..........
    time_start = get_time_usec();

    // Read out all the messages in a for loop that has 1000 iterations (about 0.25s) and storing them into a list (unsorted and not filtered)
    while((get_time_usec()-time_start)/1000000.0 < 1)
    {
        int success = read_messages(&all_counter, &wert);
        float t = messages.global_position_int.time_boot_ms;
        /*printf("Time since start %0.3f", t/1000.0 - t_px_start);*/
        /*printf("\n ---------------- \nLinux Time: %0.3f \n", (get_time_usec()-time_start)/1e6);*/
        liste[counter] = wert;
        if(success == 1) {
            counter++;
        }
        else {
            error_counter++;
        }
        /*printf("i %i \n", i);*/
        /*if(get_time_usec()-time_start > 5000000){break;}*/
        time_stop = get_time_usec();

        usleep(sleep_time);
    }

    float t_diff = (time_stop - time_start)/1000000.0;
    int int_t_diff = t_diff*1e6;
//..........
    float scale  = 1/t_diff;
    float frequency = scale*all_counter;
    float t_sample = 1/frequency;
    printf("Received %i messages in %f secs (Real time)\n", all_counter, t_diff);
    printf("Pixhawk time difference: %f \n -----------------\n", t_px_stop - t_px_start);
    printf("Sample Rate of Linux: %0.4f , Frequency of Linux: %i \n", t_sample_linux, f_linux);
    printf("Sample time: %f and Frequency: %f \n -------------\n", t_sample, frequency);
    int times_called = error_counter + all_counter;
    printf("Error counter: %i, Times called: %i \n", error_counter, times_called);
    printf("Time for a while loop: %i us \n", (int_t_diff - times_called*sleep_time)/times_called);
    printf("int_t_diff %i, times_called %i, sleep_time %i \n", int_t_diff, times_called, sleep_time);

    printf("---------------------\n");

    printf("Packets dropped: %i \n", packets_dropped);


// Generating a list with unique values
    for(i=0; i<all_counter; i++) {
        wert = liste[i];
        check = 0;
        int a;
        for(a=0; a<unique_counter; a++) {
            if(wert ==unique_list[a]) {
                check =1;
            }
        }

        if (check == 0) {
            unique_list[unique_counter] = wert;
            unique_counter++;
        }
    }

    qsort(unique_list, unique_counter, sizeof(int), cmpfcn);

    int unique_count_list[100] = {0}; //variable size cannot be initialized
    /*for (i=0; i < unique_counter; i++){*/
    /*printf("value of ucl %i \n", unique_count_list[i]);*/
    /*}*/

    for(i=0; i < unique_counter; i++) {
        int unique_value = unique_list[i];
        /*printf("\n ----------\n unique_value %i \n", unique_value);*/
        int a;
        for(a=0; a < all_counter; a++) {
            /*printf("liste[a] %i \n", liste[a]);*/
            if(unique_value == liste[a]) {
                unique_count_list[i] += 1;
                /*printf("unique_coutn_value %i \n",unique_count_list[i]);*/
            }
        }
    }

//Printing the results
    printf("Got %i unique values: \n", unique_counter);


    for (i = 0; i < unique_counter; i++) {
        printf("%i: %i times\n", unique_list[i], unique_count_list[i]);
    }
    printf("..............................\n");



//Serial Port stuff
    close_serial();
    sleep(1);
    return 0;
}
Example #13
0
/**
 * Our main entry, decode requests and monitor activity
 */
static void avr_evtd_main(void)
{
	char buf[17];
	char cmd;
	char pushed_power = 0;
	char pushed_reset = 0;
	char pressed_power_flag = 0;
	char pressed_reset_flag = 0;
	char current_status = 0;
	time_t idle = time(NULL);
	time_t power_press = idle;
	time_t fault_time;
	time_t last_shutdown_ping;
	fd_set serialfd_set;
	struct timeval timeout_poll;
	int fan_fault = 0;
	long time_diff;
	char extraTime = 0;
	char disk_full = 0;

	/* Update the shutdown timer */
	fault_time = 0;
	last_shutdown_ping = time(NULL);

	/* Loop whilst port is valid */
	while (serialfd) {
		timeout_poll.tv_usec = 0;
		int res = refresh_rate;
		/* After file change or startup, update the time within 20 secs as the
		 * user may have pushed the refresh time out. */
		if (check_state > 0) {
			res = 2;
		} else {
			/* Change our timer to check for a power/reset request need a
			 * faster poll rate here to see the double press event
			 * properly. */
			if (pushed_power || pushed_reset || first_time_flag > 1) {
				timeout_poll.tv_usec = 250;
				res = 0;
				check_state = -2;
				/* Hold off any configuration file updates */
			}
		}

		if (check_state != -2) {
			/* Ensure we shutdown on the nail if the timer is enabled will
			 * be off slightly as timer reads are different */
			if (timer_flag == 1) {
				if (shutdown_timer < res)
					res = shutdown_timer;
			}

			/* If we have a fan failure report, then ping frequently */
			if (fan_fault > 0)
				res = fan_fault == 6 ? fan_fault_seize : 2;
		}

		timeout_poll.tv_sec = res;

		FD_ZERO(&serialfd_set);
		FD_SET(serialfd, &serialfd_set);

		/* Wait for AVR message or time-out? */
		res = select(serialfd + 1, &serialfd_set, NULL, NULL, &timeout_poll);

		time_t time_now = time(NULL);

		/* catch input? */
		if (res > 0) {
			/* Read AVR message */
			res = read(serialfd, buf, 16);
			/* AVR command detected so force to ping only */
			check_state = -2;

			switch (buf[0]) {
				/* power button release */
			case 0x20:	/* ' ' */
				if (pressed_power_flag == 0) {
					cmd = POWER_RELEASE;

					if ((time_now - power_press) <= HOLD_TIME && first_time_flag < 2) {
						cmd = USER_RESET;
					} else if (shutdown_timer < FIVE_MINUTES || first_time_flag > 1) {
						if (first_time_flag == 0)
							first_time_flag = 10;

						shutdown_timer += FIVE_MINUTES;
						first_time_flag--;
						extraTime = 1;
					}

					exec_simple_cmd(cmd);
					power_press = time_now;
				}

				pushed_power = pressed_power_flag = 0;
				break;

				/* power button push */
			case 0x21:	/* '!' */
				exec_simple_cmd(POWER_PRESS);

				pressed_power_flag = 0;
				pushed_power = 1;
				break;

				/* reset button release */
			case 0x22:	/* '"' */
				if (pressed_reset_flag == 0) {
					cmd = RESET_RELEASE;
					res = 0;

					/* Launch our telnet daemon */
					if ((time_now - power_press) <= HOLD_TIME) {
						cmd = SPECIAL_RESET;
						res = reset_presses;
						reset_presses++;
					}

					exec_cmd(cmd, res);
					power_press = time_now;
				}

				pushed_reset = pressed_reset_flag = 0;
				break;

				/* reset button push */
			case 0x23:	/* '#' */
				exec_simple_cmd(RESET_PRESS);

				pressed_reset_flag = 0;
				pushed_reset = 1;
				break;

				/* Fan on high speed */
			case 0x24:	/* '$' */
				fan_fault = 6;
				fault_time = time_now;
				break;

				/* Fan fault */
			case 0x25:	/* '%' */
				/* Flag the EventScript */
				exec_cmd(FAN_FAULT, fan_fault);

				if (fan_fault_seize > 0) {
					fan_fault = 2;
					fault_time = time_now;
				} else
					fan_fault = -1;

				break;

				/* Acknowledge */
			case 0x30:	/* '0' */
				break;

				/* AVR halt requested */
			case 0x31:	/* '1' */
				close_serial();
				exec_simple_cmd(AVR_HALT);
				break;

				/* AVR initialization complete */
			case 0x33:	/* '3' */
				break;
			default:
				syslog(LOG_INFO, "unknown message %X[%d]", buf[0], res);
				break;
			}

			/* Get time for use later */
			time(&idle);
		} else {	/* Time-out event */
			/* Check if button(s) are still held after holdcyle seconds */
			if ((idle + hold_cycle) < time_now) {
				/* Power down selected */
				if (pushed_power == 1) {
					/* Re-validate our time wake-up; do not perform if in extra time */
					if (!extraTime)
						set_avr_timer(1);

					exec_simple_cmd(USER_POWER_DOWN);

					pushed_power = 0;
					pressed_power_flag = 1;
				}

			}

			/* Has user held the reset button long enough to request EM-Mode? */
			if ((idle + EM_MODE_TIME) < time_now) {
				if (pushed_reset == 1 && in_em_mode) {
					/* Send EM-Mode request to script.  The script handles the
					 * flash device decoding and writes the HDD no-good flag
					 * NGNGNG into the flash status.  It then flags a reboot
					 * which causes the box to boot from ram-disk backup to
					 * recover the HDD.
					 */
					exec_simple_cmd(EM_MODE);

					pushed_reset = 0;
					pressed_reset_flag = 1;
				}
			}

			/* Skip this processing during power/reset scan */
			if (!pushed_reset && !pushed_power && first_time_flag < 2) {
				/* shutdown timer event? */
				if (timer_flag == 1) {
					/* Decrement our powerdown timer */
					if (shutdown_timer > 0) {
						time_diff = (time_now - last_shutdown_ping);

						/* If time difference is more than a minute,
						 * force a re-calculation of shutdown time */
						if (refresh_rate + 60 > labs(time_diff)) {
							shutdown_timer -= time_diff;

							/* Within five minutes of shutdown? */
							if (shutdown_timer < FIVE_MINUTES) {
								if (first_time_flag) {
									first_time_flag = 0;

									/* Inform the EventScript */
									exec_cmd(FIVE_SHUTDOWN, shutdown_timer);

									/* Re-validate out time wake-up; do not
									 * perform if in extra time */
									if (!extraTime)
										set_avr_timer(1);
								}
							}
						}
						/* Large clock drift, either user set time
						 * or an ntp update, handle accordingly. */
						else {
							check_timer(2);
						}
					} else {
						/* Prevent re-entry and execute command */
						pushed_power = pressed_reset_flag = 2;
						exec_simple_cmd(TIMED_SHUTDOWN);
					}
				}

				/* Keep track of shutdown time remaining */
				last_shutdown_ping = time(NULL);

				/* Split loading, handle disk checks
				 * over a number of cycles, reduce CPU hog */
				switch (check_state) {
					/* Kick state machine */
				case 0:
					check_state = 1;
					break;

					/* Check for timer change through configuration file */
				case 1:
					check_timer(0);
					check_state = 2;
					break;

					/* Check the disk and ping AVR accordingly */
				case -2:

					/* Check the disk to see if full and output appropriate
					 * AVR command? */
				case 2:
					cmd = keep_alive;

					if ((current_status = check_disk())) {
						/* Execute some user code on disk full */
						if (first_warning) {
							first_warning = pester_message;
							exec_cmd(DISK_FULL, pct_used);
						}
					}

					/* Only update DISK LED on disk full change */
					if (disk_full != current_status) {
						/* LED status */
						cmd = 0x56;	/* 'V' */
						if (current_status)
							cmd++;
						else {
							first_warning = 0;
							exec_cmd(DISK_FULL, 0);
						}

						disk_full = current_status;
					}

					/* Ping AVR */
					write_to_uart(cmd);

					check_state = 3;
					break;

					/* Wait for next refresh kick */
				case 3:
					check_state = 0;
					break;
				}
			}

			/* Try and catch spurious fan fault messages */
			switch (fan_fault) {
			case -1:
				break;
			case 1:
				fan_fault = 0;
				break;
				/* Check how long we have been operating with a fan failure */
			case 2:
			case 3:
			case 4:
				if ((fault_time + fan_fault_seize) < time_now) {
					/* Run some user script on no fan restart message after
					 * FAN_FAULT_SEIZE time */
					exec_cmd(FAN_FAULT, 4);
					fan_fault = 5;
				}

				break;
				/* Fan sped up message received */
			case 6:
				/* Attempt to slow fan down again after 5 minutes */
				if ((fault_time + FIVE_MINUTES) < time_now) {
					write_to_uart(0x5C);	/* '\\' */
					fan_fault = 1;
				}

				break;
			}

			/* Check that the shutdown pause function (if activated) is still
			 * available, no then ping the delayed time */
			if ((power_press + SP_MONITOR_TIME) < time_now && first_time_flag > 1) {
				/* Inform the EventScript */
				exec_cmd(FIVE_SHUTDOWN, shutdown_timer/60);
				first_time_flag = 1;
				power_press = 0;
			}
		}
	}
}
Example #14
0
void serial_exit(void)
{
	close_serial(fd);	
	fd = -1;
}
Example #15
0
void xmos_dev_close(int xmos_d)
{
	close_serial(xmos_d);
}
Example #16
0
File: close.c Project: Yunaik/asp
void close()
{
    close_serial();
}
Example #17
0
void
Serial_Port::
stop()
{
	close_serial();
}
Example #18
0
/**
 * Close SDP and all asociated ports.
 * @param sdp   Pointer to sdp_t structure, initialized by sdp_open.
 */
void sdp_close(sdp_t *sdp)
{
        close_serial(sdp->f_in);
        if (sdp->f_in != sdp->f_out)
                close_serial(sdp->f_out);
}