/* jawsd - connects to AWS datalogger over serial connection and provides
 *         weather information to the network
 * Copyright (C) 2003-2004 Jeffrey Grafton <jgrafton@tjhsst.edu>
 *
 * 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. 
 */

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <string.h>
#include <pthread.h>
#include <syslog.h>

#include "config.h"
#include "data_types.h"
#include "datalogger.h"
#include "process.h"

int
open_serial_device (const char * serial_device)
{
	struct termios term_settings;
	int fd;

	if ((fd = open(serial_device, O_RDWR | O_NOCTTY)) == -1)
	{
		perror("datalogger");
		exit(1);
	}

	if (lockf(fd, F_TLOCK, 0) == -1)
	{
		perror("lockf");
		exit(1);
	}
	
	memset(&term_settings, 0, sizeof(term_settings));
	
	term_settings.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
	term_settings.c_iflag = IGNPAR;
	term_settings.c_oflag = 0;
	term_settings.c_lflag = 0;
	term_settings.c_cc[VMIN] = 255;
	term_settings.c_cc[VTIME] = 20;

	tcflush(fd, TCIFLUSH);
	tcsetattr(fd, TCSANOW, &term_settings);

	return fd;
}

void *
receive_and_process_data (void *voidptr)
{
    int data_logger, size, i;
    char buf[255];
    int *ptrs = (int *)voidptr;
    data_processed *processed = (data_processed *)ptrs[0];
    curobs_raw *curobs = (curobs_raw *)ptrs[1];
    hilo_raw *hilo = (hilo_raw *)ptrs[2];
    char *serial = (char *)ptrs[3];
    unsigned short *history_id = (unsigned short *)ptrs[4];
    char *history = (char *)ptrs[5];
    int *history_length = (int *)ptrs[6];
    time_t last_history = 0, current_time;
    
    syslog(LOG_INFO, "receive_and_process_data: running.");

    if (setuid(UID) == -1)
    {
	perror("setuid");
    }
    else
    {
        printf("receive_and_process_data dropped to uid %d\n", getuid());
    }
   
    data_logger = open_serial_device(DATA_LOGGER);

    memset(serial, 0, SERIAL_LENGTH);

    buf[0] = 0x06;
    write(data_logger, buf, 1);
    size = read(data_logger, serial, SERIAL_LENGTH);

    while (1)
    {
        buf[0] = 0x00;
        buf[1] = 0x01;
        write(data_logger, buf, 2);
	
        size = read(data_logger, curobs, sizeof(curobs_raw));

        size = read(data_logger, hilo, sizeof(hilo_raw));

        process_data(curobs, hilo, processed);
	
	current_time = time(NULL);
	
	if ((curobs->minute == 0 && (current_time - last_history > 61)) || last_history == 0)
	{
		last_history = current_time;
		(*history_id)++;
		printf("history id: %hd\n", *history_id);
		memcpy(history, curobs, sizeof(curobs_raw));
	}

        usleep(500000);
    }
    
    buf[0] = 0xff;
    buf[1] = 0xff;
    write(data_logger, buf, 2);
    
    close(data_logger);

    pthread_exit(0);
}


