/*************************************************************************\
* "$RCSfile: CXml.hpp,v $"                                                *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2011-10-20 07:08:57 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.6 $                                  *
\*************************************************************************/
/*! \file CXml.hpp
*	\brief Header CXml Class
**************************************************************************/

#include "ftp.h"
#include "tinyxml.h"

/*************************************************************************/
/*! @brief Handles external commands.

	This class creates an own task which opens a TCP/IP socket at port
	CPSApp::GetXmlPort(). The task listens for XML commands.\n
	The \p Get keyword reads the value of camera properties and images.
	Images are sent via a separate socket with port XML+1. A small
	'header' of 4 bytes consists of the file size followed by 4 bytes
	describing the file type (\p 'RAW', \p 'BMP' or \p 'PNG').
	@par Example:
	Question to camera:
@verbatim
<?xml version="1.0" encoding="utf-8"?>
<VisionCam>
  <!-- Get data from camera -->
  <Get>
    <!-- Camera Features -->
    <CFeature>
      <ExposureTime />
      <FlipImage />
      ... // More Features
    </CFeature>
    <!-- Image data -->
    <Image>
      <BMP />
    </Image>
  </Get>
</VisionCam>
@endverbatim
	Answer from camera:
@verbatim
<?xml version="1.0" encoding="utf-8" ?>
<VisionCam>
  <Get>
    <CFeature>
      <ExposureTime State="OK">20000</ExposureTime>
      <FlipImage State="OK">0</FlipImage>
      ...
    </CFeature>
    <Image>
      <BMP State="OK">
        <Picture Width="1024" Height="1024" Size="1049654"/>
      </BMP>
    </Image>
  </Get>
</VisionCam>
@endverbatim
	@par Example:
	The \p Set keyword allows changing the camera properties.
@verbatim
<?xml version="1.0" encoding="utf-8"?>
<VisionCam>
  <!-- Set data in camera -->
  <Set>
    <CFeature>
      <ExposureTime>5000</ExposureTime>
      <FlipImage>1</FlipImage>
    </CFeature>
  </Set>
</VisionCam>
@endverbatim
**************************************************************************/
class CXml
{
private:
	CXml();
	~CXml();
	static CXml *m_pThis;

public:
	static CXml *CreateInstance();
	//! Gets CXml instance.
	static CXml *GetInstance() { return m_pThis; }
	static void DisposeInstance();
	int Dispatcher();
	int SendMessage(TiXmlDocument *pXML);

	//! Returns if XML port is connected.
	bool IsXMLConnected() const { return m_bXConnected; }
	//! Returns if data port is connected.
	bool IsDataConnected() const { return m_bDConnected; }

protected:
	//! Constants
	enum eCONST
	{
		C_UNKOWN = -1,			//!< Unknown command
		XMLBUFFER = 16384,		//!< Size of XML receive buffer
		UDP_PORT_IN = 49152,	//!< Port for UDP broadcast
		UDP_PORT_OUT = 49153,	//!< Port for UDP broadcast
		TCP_UPDATE_PORT = 49154	//!< Port for firmware update via TCP
	};

	static void TaskDispatcher();
	static void TaskUDPBroadcast();
	static void TaskFirmwareUpdate();

	int Parser();
	int GetCValue(const char *pCFeatureName, char *pValStr);
	int SetCValue(const char *pCFeatureName, const char *pValStr);
	int GetPValue(const char *pPropertyName, char *pValStr);
	int SetPValue(const char *pPropertyName, const char *pValStr);
	int GetCamRegValue(const char *pSizeStr, char *pValStr);
	int SetCamRegValue(const char *pSizeStr, const char *pValStr);
	int SendImage(TiXmlDocument *pXML, TiXmlElement *pElem, int Type);
	
	CPSApp *m_pA;				//!< Pointer to application
	SOCKET m_XSocket;			//!< XML socket
	SOCKET m_XListenSocket;		//!< XML listen socket
	SOCKET m_DSocket;			//!< Data socket
	SOCKET m_DListenSocket;		//!< Data listen socket;
	sockaddr m_XSoIn;			//!< XML socket address
	sockaddr m_DSoIn;			//!< Data socket address
	bool m_bXConnected;			//!< XML port is connected
	bool m_bDConnected;			//!< Data port is connected
	int m_XMLPos;				//!< Position in XML string
	int m_Counter;				//!< Timeout counter
	int m_Timeout;				//!< Timeout value for automatic disconnect
	char *m_XMLString;			//!< Received XML string
};

/*************************************************************************/
