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

#include <list>
#include <map>
#include <string>

#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);
	// Added during bug #3344
	SerialConfig(const char *owner);
	void createPort(std::string device, std::string owner, std::string baud, std::string data, std::string stop, std::string mode, std::string parity);
	void setNonBlock(std::string device, std::string nonBlock);
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_ */
