/***************************************************************************
 *                                                                         *
 *   powerplug v1.0.0                                                      *
 *   for information see http://kurt.das.cc/powerplug                      *
 *                                                                         *
 *   Copyright (C) 2005 by Kurt Schlager                                   *
 *   software@das.cc                                                       *
 *                                                                         *
 *   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 <iostream>
#include <cstdlib>
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
using namespace std;

#define DELAY_POWER_OFF   5

class PowerPlug
{
	private:
		char* device_;
		int fd_;
	
	public:
		PowerPlug();
		~PowerPlug();
		bool setDevice(char* device);
		bool powerOff(int time);
};

//--------------------------------------------------
PowerPlug::PowerPlug()
{
}

//--------------------------------------------------
PowerPlug::~PowerPlug()
{
	close(fd_);
}

//--------------------------------------------------
bool PowerPlug::setDevice(char* device)
{
	device_ = device;
	if (device_ == "")
		return false;
		
	fd_ = open(device_, O_RDWR|O_NDELAY);
	if (fd_ < 0)
		return false;

	//ioctl(fd_, TIOCMSET, TIOCM_RTS);
	return true;
}

//--------------------------------------------------
bool PowerPlug::powerOff(int time)
{
	ioctl(fd_, TIOCMBIS, TIOCM_DTR);
	sleep(time);
	ioctl(fd_, TIOCMBIS, 0);
	return true;
}



//--------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
	if (argc < 2 || argc > 3)
	{
		cout << "Error starting modemwd!" << endl
		<< "Usage: modemwd device [time]" << endl
		<< "device\tport the powerplug is connected to (e.g. /dev/ttyS0)" << endl
		<< "time  \tswitch off power for time seconds (standart is 5)" << endl
		<< "example: modemwd /dev/ttyS0 7" << endl << endl;
		return -1;
	}

	PowerPlug* plug = new PowerPlug();
	if (plug->setDevice(argv[1]) == false)
	{
		cout << "Error on opening device!" << endl << endl;
		delete plug;
		return -1;
	}

	int time = DELAY_POWER_OFF;
	if (argc == 3)
	{
		time = atoi(argv[2]);
	}

	plug->powerOff(time);

	return 0;
}

