//------------------------------------------------------------------------------
// <copyright file="LqrSMACSerialListener.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Declarations for the SerialListener class, which spawns a thread and reads
//   incoming data, firing events from the background thread for received data
//   packets.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __SERIALLISTENER_H__
#define __SERIALLISTENER_H__

#include "LqrSMACByteArray.h"
#include "LqrSMACDeclareException.h"
#include "LqrSMACLockableConcept.h"
#include "LqrSMACSerialPort.h"
#include "LqrSMACSharedLib.h"
#include <stdexcept>
#include <string>
#include <boost/signals2.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>

namespace LiquidRobotics {
namespace SMACFramework {

class SerialListenerSettings;

class SHARED_LIB_PUBLIC SerialListener
{
public:

	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef LiquidRobotics::SMACFramework::ILockableConcept ILockableConcept;
	typedef LiquidRobotics::SMACFramework::ISerialPort ISerialPort;
	typedef boost::shared_ptr<SerialListener> Ptr;
	typedef boost::signals2::scoped_connection ScopedConnection;
	typedef boost::shared_ptr<ScopedConnection> ScopedConnectionPtr;
	typedef ByteArray::size_type size_type;
	typedef ISerialPort::TimeSpan TimeSpan;
	typedef boost::signals2::signal<void (SerialListener& sender, const ByteArray& receivedData)> T_HereIsSerialData_Signal;

	DECLARE_EXCEPTION(AlreadyInitialized, std::logic_error);
	DECLARE_EXCEPTION(NotInitialized, std::logic_error);

	SerialListener(ISerialPort& _sp, size_type _maxRead, TimeSpan _readTimeout);
	SerialListener(ISerialPort& _sp, const SerialListenerSettings& settings);
	virtual ~SerialListener();

	void Initialize();
	void Shutdown();

	ILockableConcept& FiringMutex();
	ScopedConnectionPtr Subscribe(T_HereIsSerialData_Signal::slot_type slot);

	T_HereIsSerialData_Signal hereIsSerialData_Signal;

private:

	friend class boost::detail::thread_data<boost::reference_wrapper<SerialListener> >;
	typedef LiquidRobotics::SMACFramework::LockableConceptMutex LockableConceptMutex;

	void operator()();

	ISerialPort& sp;
	const size_type maxRead;
	const TimeSpan readTimeout;

	LockableConceptMutex firingMutex;

	boost::thread* pThread;
	bool signalStop;
	bool shutDown;
};

}
}

#endif
