From a4de292726294b7eb5237a326c038ce3978da985 Mon Sep 17 00:00:00 2001
From: Sonia Minolli <sonia.minolli@synchrotron-soleil.fr>
Date: Thu, 27 Apr 2017 14:34:13 +0000
Subject: [PATCH] Updates for threaded tasks (TANGODEVIC-1647)

---
 include/timiq/TIMIQLib.h  |  3 ++
 src/TIMIQLib.cpp          | 93 +++++++++++++++++++++++++++++++++------
 src/TIMIQTypesAndConsts.h |  4 +-
 3 files changed, 86 insertions(+), 14 deletions(-)

diff --git a/include/timiq/TIMIQLib.h b/include/timiq/TIMIQLib.h
index 8d76b7f..4e099e3 100644
--- a/include/timiq/TIMIQLib.h
+++ b/include/timiq/TIMIQLib.h
@@ -175,10 +175,13 @@ namespace TIMIQLib_ns {
 			ThreadedAction * m_timiq_task;
 			
 			void ExecuteAction(timIQConfig & cfg);
+			void end_task() throw (Exception);
+			//  soso à supprimer:
 			void iVal_end_task() throw (Exception);
 			void qVal_end_task() throw (Exception);
 			void data_end_task() throw (Exception);
 			void boardT_end_task() throw (Exception);	
+			
 
 
 	};
diff --git a/src/TIMIQLib.cpp b/src/TIMIQLib.cpp
index 7bd49b7..ef9c2c6 100644
--- a/src/TIMIQLib.cpp
+++ b/src/TIMIQLib.cpp
@@ -25,6 +25,7 @@ if (!m_timiq_hw) \
 				static_cast<const char*>("CHECK_TIMIQ_HW()")); \
 }
 // ============================================================================
+
 // ============================================================================
 // TIMIQLib::TIMIQLib
 // ============================================================================
@@ -36,6 +37,7 @@ TIMIQLib::TIMIQLib ()
 	m_internal_timiq_state = Undefined;
 	m_timiq_task = NULL;
 }
+
 // ============================================================================
 // TIMIQLib::~TIMIQLib()
 // ============================================================================
@@ -51,10 +53,11 @@ TIMIQLib::~TIMIQLib()
 	if (m_timiq_task)
 	{
 		m_timiq_task->exit();
-		delete m_timiq_task;
+		//delete m_timiq_task; // soso debug - sure needs delete ?
 		m_timiq_task = NULL;
 	}
 }
+
 // ============================================================================
 // TIMIQLib::init
 // ============================================================================
@@ -78,6 +81,7 @@ void TIMIQLib::init(const std::string& ip_address, const short& num_port)
 	
 	CHECK_TIMIQ_HW;		
 }
+
 // ============================================================================
 // TIMIQLib::set_data
 // ============================================================================		
@@ -209,6 +213,35 @@ void TIMIQLib::iVal_end_task()
 	m_internal_timiq_state = OK;
 }
 
+// soso add
+// ============================================================================
+// TIMIQLib::end_task
+// ============================================================================		
+void TIMIQLib::end_task()
+	throw (Exception)
+{
+	//- Delete task 
+	if (this->m_timiq_task)
+	{	
+		if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
+		{	
+			m_timiq_task->exit();
+			m_timiq_task = NULL;
+			
+			std::string msg_err = "Threaded task error -msg: " + m_timiq_hw->get_err_msg();
+			throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
+							static_cast<const char*>(msg_err.c_str()),
+							static_cast<const char*>("TIMIQLib::end_task"));
+		}	
+		//- destroy otherwise 
+		m_timiq_task->exit();
+		m_timiq_task = NULL;
+	}
+	
+	//- update state of TIMIQ equipment
+	//m_internal_timiq_state = OK; -- est mis à jour par get state
+}
+
 // ============================================================================
 // TIMIQLib::set_qValue
 // ============================================================================		
@@ -349,17 +382,45 @@ void TIMIQLib::set_command(E_timiq_cmd_t& cmd)
 	CHECK_TIMIQ_HW;
 	
 	//- write command on TIMIQ equipment
-	err = m_timiq_hw->write_command(cmd);
-	if (err != timiq_NO_ERROR)
+	// soso add
+	// thread PLL calibration command:
+	if (cmd == RECALIBRATE_PLL)
+	{
+      if ( m_internal_timiq_state != Busy )
+	  {
+	    timIQConfig l_ti_cfg;
+	    l_ti_cfg.id = TI_CALIB;
+		
+		//- force timiq state = Busy
+		m_internal_timiq_state = Busy;
+		
+		m_timiq_task = new ThreadedAction(static_cast<yat::Thread::IOArg>(this), l_ti_cfg);
+		if (m_timiq_task == NULL)
+		{
+			std::string msg_err = "Set Q value error -msg: " + m_timiq_hw->get_err_msg();
+			throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
+							static_cast<const char*>(msg_err.c_str()),
+							static_cast<const char*>("TIMIQLib::set_command"));
+		}
+
+		//- start the task to do the pll calibration
+		m_timiq_task->start_undetached();
+	  }
+	}
+	else
 	{
+	  err = m_timiq_hw->write_command(cmd);
+	  if (err != timiq_NO_ERROR)
+	  {
 		std::string msg_err = "Set command error -msg: " + m_timiq_hw->get_err_msg();
 		throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
 						static_cast<const char*>(msg_err.c_str()),
 						static_cast<const char*>("TIMIQLib::set_command()"));
-	}
+	  }
 	
-	//- update state of TIMIQ equipment
-	m_internal_timiq_state = OK;
+	  //- update state of TIMIQ equipment
+	  m_internal_timiq_state = OK;
+	}
 }		
 
 // ============================================================================
@@ -500,7 +561,7 @@ E_timiq_code_t TIMIQLib::get_state(std::string& status)
 	CHECK_TIMIQ_HW;
 	
 	//- get internal timiq state
-	if (m_internal_timiq_state != Busy)
+	//if (m_internal_timiq_state != Busy) -- soso debug : update qqsoit l'état
 	{ 		
 		//- read status of TIMIQ equipment
 		err = m_timiq_hw->read_state_and_status(status, m_internal_timiq_state);
@@ -571,7 +632,6 @@ void ThreadedAction::exit (void)
 	this->m_goOn = false;
 }
 
-
 // ============================================================================
 // TIMIQLib::ExecuteAction
 // ============================================================================
@@ -587,26 +647,33 @@ void TIMIQLib::ExecuteAction(timIQConfig & cfg)
 			//- write "I" value
 			case TI_IVAL:	
 					cfg.ti_err = m_timiq_hw->write_iValue(cfg.value);
-					this->iVal_end_task();
+					this->end_task(); // soso meme fonction end, non ?
 				break;
 				
 			//- write "Q" value
 			case TI_QVAL:
 					cfg.ti_err = m_timiq_hw->write_qValue(cfg.value);
-					this->qVal_end_task();					
+					this->end_task(); // soso meme fonction end, non ?		
 				break;		
 				
 			//- write "DATA" value
 			case TI_DATA:
 					cfg.ti_err = m_timiq_hw->write_data(cfg.value);	
-					this->data_end_task();
+					this->end_task(); // soso meme fonction end, non ?
 				break;
 
 			//- write "BOARD Temperature" value
 			case TI_BTEMP:
 					cfg.ti_err = m_timiq_hw->write_data(cfg.value);	
-					this->boardT_end_task();
-				break;					
+					this->end_task(); // soso meme fonction end, non ?
+				break;
+
+			//- PLL calibration -- soso add
+			case TI_CALIB:
+					cfg.ti_err = m_timiq_hw->write_command(RECALIBRATE_PLL);
+					this->end_task();
+				break;
+				
 			default:
 				break;
 		}
diff --git a/src/TIMIQTypesAndConsts.h b/src/TIMIQTypesAndConsts.h
index b73ce8d..7d1e925 100644
--- a/src/TIMIQTypesAndConsts.h
+++ b/src/TIMIQTypesAndConsts.h
@@ -105,13 +105,15 @@ typedef enum
 //------------------------------
 //- timIQ struct config 
 //------------------------------
+// threaded action type
 typedef enum
 {
 	Undef = 0,
 	TI_IVAL,
 	TI_QVAL,
 	TI_BTEMP,
-	TI_DATA
+	TI_DATA,
+	TI_CALIB // soso add
 } E_ti_t;
 
 typedef struct timIQConfig
-- 
GitLab