/*
 * SerialConfig.h
 *
 *  Created on: Jun 20, 2012
 *      Author: willie
 */

#include <list>
#include <map>

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

#include "SerialPort.h"

#ifndef SERIALCONFIG_H_
#define SERIALCONFIG_H_

namespace kserial
{

class SerialConfig
{
public:
	SerialConfig(const char *file, const char *owner);
	~SerialConfig();
	void getPortList(std::list<std::string> &portlist);
	int connect(std::string device);
private:
	void parsePort(xmlDocPtr doc, xmlNodePtr config);
	std::string mOwner;
	std::list<SerialPort *> mPortList;
};

class ConfigParseException : public std::exception
{
public:
	ConfigParseException(const char *msg) : mMsg(msg) { }
	~ConfigParseException() throw() { }
	virtual const char* what() const throw() { return mMsg.c_str(); }
private:
	std::string mMsg;
};

class DeviceNotFoundException : public std::exception
{
public:
	DeviceNotFoundException(const char *msg) : mMsg(msg) { }
	~DeviceNotFoundException() throw() { }
	virtual const char* what() const throw() { return mMsg.c_str(); }
private:
	std::string mMsg;
};

class DeviceOwnershipException : public std::exception
{
public:
	DeviceOwnershipException(const char *dev, const char *owner) : mDev(dev), mOwner(owner) { }
	~DeviceOwnershipException() throw() { }
	virtual const char* what() const throw()
	{
		std::string msg;
		msg += mDev;
		msg += " owned by ";
		msg += mOwner;
		return msg.c_str();
	}
private:
	std::string mDev;
	std::string mOwner;
};

} /* namespace kserial */
#endif /* SERIALCONFIG_H_ */
