Skip to content
Snippets Groups Projects
Commit 5b5b8132 authored by ELATTAOUI's avatar ELATTAOUI
Browse files

No commit message

No commit message
parents
No related branches found
No related tags found
No related merge requests found
//=============================================================================
// TIMIQLib.h
//=============================================================================
// abstraction.......Tim IQ Application Programming Interface
// class.............TIMIQLib
// original author...J. GOUNO - NEXEYA-FRANCE
//=============================================================================
#ifndef _TIMIQ_LIB_H_
#define _TIMIQ_LIB_H_
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include <TIMIQException.h>
#include <TIMIQCurl.h>
// ==========================================================================
// DEFINITION OF API
//
// This API allows access to an electronic equipment "TimIQ".
// It allows a control software to send and receive data
// via a http web protocol.
// ===========================================================================
namespace TIMIQLib_ns {
//- low layer TimIQ curl class
//-----------------------------
class TIMIQCurl;
//------------------------------------------------------------------------
//- TIMIQProxy Class
//- Ensures the interface of the timIQ equipment with
//- a tango software control system
//------------------------------------------------------------------------
class TIMIQLib
{
public:
// Contructor
TIMIQLib ();
// Destructor
~TIMIQLib();
//- Write functions
//---------------------------------------------------------------------------
// Sets data to TimIQ equipment
// @param data float value.
void set_data(float data) throw (Exception);
// Regulates "I" tension to TimIQ equipment
// @param iValue float value.
void set_iValue(float iValue)throw (Exception);
// Regulates "Q" tension to TimIQ equipment
// @param qValue float value.
void set_qValue(float qValue)throw (Exception);
// Sets board temperature to TimIQ equipment
// @param boardTemperature float value.
void set_boardTemperature(float boardTemperature) throw (Exception);
// Sets command to TimIQ equipment
// @param cmd TimIQCmd_t value.
void set_command(E_timiq_cmd_t& cmd) throw (Exception);
//- Read functions
//---------------------------------------------------------------------------
// Gets data from TimIQ equipment
// @param [out] data float pointer.
void get_data(float &data)throw (Exception);
// Gets "I" value from TimIQ equipment
// @param [out] iValue float pointer.
void get_iValue(float& iValue)throw (Exception);
// Gets "Q" value from TimIQ equipment
// @param [out] qValue float pointer.
void get_qValue(float& qValue)throw (Exception);
// Gets the mixer cosinus output from TimIQ equipment
// @param [out] mixerCosOutput float pointer.
void get_mixerCosOutput(float& mixerCosOutput)throw (Exception);
// Gets the mixer sinus output from TimIQ equipment
// @param [out] mixerSinOutput float pointer.
void get_mixerSinOutput(float& mixerSinOutput)throw (Exception);
// Gets the board temperature from TimIQ equipment
// @param [out] boardTemperature float pointer.
void get_boardTemperature(float& boardTemperature)throw (Exception);
// Gets state of TimIQ equipment
// @param [out] status string
E_timiq_code_t get_state(std::string& status) throw (Exception);
// Initialize TimIQCurl library
// @param ip_address string, port_number string
void init (const std::string& ip_address, const short& port_number) throw (Exception);
private:
//- internal members
//--------------------------
TIMIQCurl * m_timiq_hw;
//- ip address number
std::string m_ipAddress;
//- port number
std::string m_numPort;
//- internal function
//-------------------------
// ...
};
} // namespace TIMIQLib_ns
#endif // _TIMIQ_LIB_H_
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.soleil</groupId>
<artifactId>super-pom-C-CPP</artifactId>
<version>RELEASE</version>
</parent>
<groupId>fr.soleil.lib</groupId>
<artifactId>TimIQLib-${aol}-${library}-${mode}</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>nar</packaging>
<name>TimIQLib</name>
<description>TIMIQ library</description>
<build>
<plugins>
<plugin>
<groupId>org.freehep</groupId>
<artifactId>freehep-nar-plugin</artifactId>
<configuration>
<os>Linux</os>
<cpp>
<sourceDirectory>src</sourceDirectory>
<includePaths>
<includePath>${basedir}/include</includePath>
<includePath>${basedir}/src</includePath>
</includePaths>
<options>
<!--option>-D_SIMULATION_</option-->
<option>-Wno-uninitialized</option>
<option>-Wno-unused-parameter</option>
<option>-Wno-unused-variable</option>
</options>
</cpp>
</configuration>
</plugin>
</plugins>
</build>
<scm>
<connection>${scm.connection.svn.tango-ds}/Libraries/TimIQLib/trunk</connection>
<developerConnection>${scm.connection.svn.tango-ds}/Librairies/TimIQLib/trunk</developerConnection>
<url>${scm.url.svn.tango-ds}/Libraries/TimIQLib/trunk</url>
</scm>
<dependencies>
<dependency>
<groupId>fr.soleil.lib</groupId>
<artifactId>YAT-${aol}-${library}-${mode}</artifactId>
</dependency>
<dependency>
<groupId>se.haxx.lib</groupId>
<artifactId>curl-i386-Linux-gcc-static-release</artifactId>
<version>7.37.0</version>
</dependency>
</dependencies>
<developers>
<developer>
<id>gouno</id>
<name>gouno</name>
<url>http://controle/</url>
<organization>Synchrotron Soleil</organization>
<organizationUrl>http://www.synchrotron-soleil.fr</organizationUrl>
<roles>
<role>developper</role>
</roles>
<timezone>1</timezone>
</developer>
<developer>
<id>buteau</id>
<name>buteau</name>
<url>http://controle/</url>
<organization>Synchrotron Soleil</organization>
<organizationUrl>http://www.synchrotron-soleil.fr</organizationUrl>
<roles>
<role>manager</role>
</roles>
<timezone>1</timezone>
</developer>
</developers>
</project>
//=============================================================================
// TIMIQCurl.cpp
//=============================================================================
// abstraction.......TIMIQCurl Low level access to TIMIQ hardware
// class.............TIMIQCurl
// original author...J. GOUNO - NEXEYA-FRANCE
//=============================================================================
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include "TIMIQCurl.h"
#include <iostream>
#include <string.h>
#include <yat/utils/StringTokenizer.h>
#include <yat/utils/XString.h>
namespace TIMIQLib_ns {
// ============================================================================
// TIMIQCurl::TIMIQCurl
// ============================================================================
TIMIQCurl::TIMIQCurl(const std::string& ip_address, const std::string& num_port)
{
//std::cout << "TIMIQCurl constructor" << std::endl;
m_base_addr = kHTTP_WEB_PROTOCOL + ip_address + ":"+ num_port + "/";
m_error_string = "";
m_timiq_internal_buff.clear();
m_hw_curl = NULL;
}
// ============================================================================
// TIMIQCurl::~TIMIQCurl
// ============================================================================
TIMIQCurl::~TIMIQCurl ()
{
m_hw_curl = NULL;
}
// ============================================================================
// TIMIQCurl::write_data
// ============================================================================
E_timiq_errno_t TIMIQCurl::write_data(float& data)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
char buff[kMAX_DATA_SIZE];
memset(buff, 0, sizeof(buff));
sprintf(buff, "%s=%2.3f", (char*)kTIMIQ_DATA_KEY.c_str(), data);
std::string strbuff(buff);
//- request to the web page
// http://address/set_data
std::string str_url = m_base_addr + kTIMIQ_DATA_PAGE+ "?" + strbuff;
if (this->write_i(str_url, strbuff))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::write_iValue
// ============================================================================
E_timiq_errno_t TIMIQCurl::write_iValue(float& iValue)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
char buff[kMAX_DATA_SIZE];
memset(buff, 0, sizeof(buff));
sprintf(buff, "%s=%1.6f", kTIMIQ_IVALUE_KEY.c_str() , iValue);
std::string strbuff(buff);
//- request to the web page
// http://address/set_iValue
std::string str_url = m_base_addr + kTIMIQ_IVALUE_PAGE + "?" + strbuff;
if (this->write_i(str_url, strbuff))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::write_qValue
// ============================================================================
E_timiq_errno_t TIMIQCurl::write_qValue(float& qValue)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
char buff[kMAX_DATA_SIZE];
memset(buff, 0, sizeof (buff));
sprintf(buff, "%s=%1.6f", kTIMIQ_QVALUE_KEY.c_str(), qValue);
std::string strbuff(buff);
//- request to the web page
// http://address/set_qValue
std::string str_url = m_base_addr + kTIMIQ_QVALUE_PAGE + "?" + strbuff;
if (this->write_i(str_url, strbuff))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::write_boardTemperature
// ============================================================================
E_timiq_errno_t TIMIQCurl::write_boardTemperature(float& boardTemperature)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
char buff[kMAX_DATA_SIZE];
memset(buff, 0, sizeof (buff));
sprintf(buff, "%s=%2.3f", kTIMIQ_BOARD_TEMPERATURE_KEY.c_str(), boardTemperature);
std::string strbuff(buff);
//- request to the web page
// http://address/set_boardTemperature.wsgi
std::string str_url = m_base_addr + kTIMIQ_BOARD_TEMPERATURE_PAGE + "?" + strbuff;
if (this->write_i(str_url, strbuff))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::write_command
// ============================================================================
E_timiq_errno_t TIMIQCurl::write_command(E_timiq_cmd_t& cmd)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
char buff[kMAX_DATA_SIZE];
memset(buff, 0, sizeof(buff));
sprintf(buff, "%s=%d", kTIMIQ_COMMAND_KEY.c_str(), (unsigned short)cmd);
std::string strbuff(buff);
//- request to the web page
// http://address/set_command
std::string str_url = m_base_addr + kTIMIQ_COMMAND_PAGE + "?" + strbuff;
if (this->write_i(str_url, strbuff))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::read_data
// ============================================================================
E_timiq_errno_t TIMIQCurl::read_data(float& data)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
//- request to the web page
// http://address/get_data.wsgi
std::string str_url = m_base_addr + kTIMIQ_DATA_PAGE;
if (this->read_float_i(str_url, kTIMIQ_DATA_KEY, data))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::read_iValue
// ============================================================================
E_timiq_errno_t TIMIQCurl::read_iValue(float& iValue)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
//- request to the web page
// http://address/get_feedback.wsgi
std::string str_url = m_base_addr + kTIMIQ_FEEDBACK_PAGE;
if (this->read_float_i(str_url, kTIMIQ_IVALUE_KEY, iValue))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::read_qValue
// ============================================================================
E_timiq_errno_t TIMIQCurl::read_qValue(float& qValue)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
//- request to the web page
// http://address/get_feedback.wsgi
std::string str_url = m_base_addr + kTIMIQ_FEEDBACK_PAGE;
if (this->read_float_i(str_url, kTIMIQ_QVALUE_KEY, qValue))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::read_mixerCosOutput
// ============================================================================
E_timiq_errno_t TIMIQCurl::read_mixerCosOutput(float& mixerCosOutput)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
//- request to the web page
//- http://address/get_feedback.wsgi
std::string str_url = m_base_addr + kTIMIQ_FEEDBACK_PAGE;
if (this->read_float_i(str_url, kTIMIQ_MIXER_COS_OUTPUT_KEY, mixerCosOutput))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::read_mixerSinOutput
// ============================================================================
E_timiq_errno_t TIMIQCurl::read_mixerSinOutput(float& mixerSinOutput)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
//- request to the web page
//- http://address/get_feedback.wsgi
std::string str_url = m_base_addr + kTIMIQ_FEEDBACK_PAGE;
if (this->read_float_i(str_url, kTIMIQ_MIXER_SIN_OUTPUT_KEY, mixerSinOutput))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::read_boardTemperature
// ============================================================================
E_timiq_errno_t TIMIQCurl::read_boardTemperature(float& boardTemperature)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
//- request to the web page
//- http://address/get_feedback.wsgi
std::string str_url = m_base_addr + kTIMIQ_FEEDBACK_PAGE;
if (this->read_float_i(str_url, kTIMIQ_BOARD_TEMPERATURE_KEY, boardTemperature))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::read_state_and_status
// ============================================================================
E_timiq_errno_t TIMIQCurl::read_state_and_status(std::string& status, E_timiq_code_t& codeRet)
{
E_timiq_errno_t err_num = timiq_internal_ERROR;
//- request to the web page
//- http://address/get_state.wsgi
std::string str_url = m_base_addr + kTIMIQ_STATE_PAGE;
if (this->read_state_and_status_i(str_url, status, codeRet))
err_num = timiq_NO_ERROR;
return err_num;
}
// ============================================================================
// TIMIQCurl::writeTimIQCallback
// ============================================================================
size_t TIMIQCurl::writeTimIQCallback( char *buffer, size_t size, size_t nmemb, void*)
{
//- fill the buffer with the data collected from TIMQ webserver
for (size_t idx = 0; idx < size*nmemb; idx++ )
m_timiq_internal_buff.push_back(buffer[idx]);
return size*nmemb;
}
// ============================================================================
// TIMIQCurl::connect_i
// ============================================================================
E_timiq_errno_t TIMIQCurl::connect_i()
{
// std::cout << "TIMIQCurl::connect() called..." << std::endl;
E_timiq_errno_t err_num = timiq_internal_ERROR;
m_error_string = "";
try
{
m_hw_curl = curl_easy_init();
if (m_hw_curl)
{
err_num = timiq_NO_ERROR;
}
else
{
m_error_string = "INTERNAL_ERROR\n";
m_error_string += "TIMIQ WebServer access is not available, null pointer detected!\n";
m_error_string += "TIMIQCurl::connect()";
}
}
catch (...)
{
m_error_string = "TIMIQCurl::connect: exception while initialize Webserver...";
m_hw_curl = NULL;
}
return err_num;
}
// ============================================================================
// TIMIQCurl::write_i
// ============================================================================
bool TIMIQCurl::write_i(std::string& url, std::string& strData)
{
//- CURL easy return code
CURLcode result;
m_error_string = "";
//- clean the timiq buffer data before perform
m_timiq_internal_buff.clear();
#if !defined (_SIMULATION_)
//- no connection is possible
if (this->connect_i() != timiq_NO_ERROR)
return false;
try
{
//send to webserver
curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &TIMIQCurl::writeTimIQCallback);
//- perform the query
result = curl_easy_perform(m_hw_curl);
//- clean up
curl_easy_cleanup(m_hw_curl);
}
catch (...)
{
m_error_string = "TIMIQCurl::write_i: exception while writing data on" + url;
return false;
}
//- check the result
//----------------------------
if (result != CURLE_OK)
{
//- HTTP error
m_error_string = m_timiq_internal_buff;
return false;
}
else
//- check the data returned
{
#else
m_timiq_internal_buff = "\ncommand=1\n";
m_timiq_internal_buff += "command=2\n";
m_timiq_internal_buff += "iValue=-0.000001\n";
m_timiq_internal_buff += "qValue=-0.000002";
#endif
std::size_t found = m_timiq_internal_buff.find(strData);
if (found != std::string::npos)
{
m_error_string = "OK";
return true;
}
else
{
//- otherwise error
m_error_string = m_timiq_internal_buff;
}
#if !defined (_SIMULATION_)
}
#endif
return false;
}
// ============================================================================
// TIMIQCurl::read_float_i
// ============================================================================
bool TIMIQCurl::read_float_i(const std::string& url, const std::string& strData, float& value )
{
// std::cout <<"TIMIQCurl::read_float_i"<<std::endl;
//- CURL easy return code
CURLcode result;
m_error_string = "";
//- clean the timiq buffer data before perform
m_timiq_internal_buff.clear();
#if !defined (_SIMULATION_)
//- no connection is possible
if (this->connect_i() != timiq_NO_ERROR)
{
return false;
}
try
{
//send to webserver
curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &TIMIQCurl::writeTimIQCallback);
//- perform the query
result = curl_easy_perform(m_hw_curl);
//- clean up
curl_easy_cleanup(m_hw_curl);
}
catch (...)
{
m_error_string = "TIMIQCurl::read_float_i: exception while reading data on " + url;
return false;
}
//- check the result
//----------------------------
if (result != CURLE_OK)
{
//- HTTP error
m_error_string = m_timiq_internal_buff;
return false;
}
else
//- check the data returned
{
#else
m_timiq_internal_buff = "\niValue=-0.000001\n";
m_timiq_internal_buff += "qValue=0.000002\n";
m_timiq_internal_buff += "mixerSinOutput=3.100000\n";
m_timiq_internal_buff += "mixerCosOutput=0.154600\n";
m_timiq_internal_buff += "boardTemperature=20.154";
#endif
std::size_t index = m_timiq_internal_buff.find(strData);
if (index != std::string::npos)
{
//- extract substring from index
std::string l_subStr = m_timiq_internal_buff.substr(index);
//- parse "="
yat::StringTokenizer tokfloatStr (l_subStr, "=");
try
{
//- field
std::string fieldStr = tokfloatStr.next_token();
//- value
std::string floatStr = tokfloatStr.next_token();
//- convert data to flaot
value = (float)yat::XString<float>::to_num(floatStr);
}
catch(...)
{
m_error_string = "TIMIQCurl::read_float_i: exception while converting data from " + url;
m_error_string += m_timiq_internal_buff;
return false;
}
//- success
m_error_string = "OK";
return true;
}
//- otherwise error
m_error_string = m_timiq_internal_buff;
#if !defined (_SIMULATION_)
}
#endif
return false;
}
// ============================================================================
// TIMIQCurl::read_state_and_status_i
// ============================================================================
bool TIMIQCurl::read_state_and_status_i(const std::string& url, std::string& status, E_timiq_code_t& codeRet )
{
//- CURL easy return code
CURLcode result;
m_error_string = "";
//- clean the timiq buffer data before perform
m_timiq_internal_buff.clear();
#if !defined (_SIMULATION_)
//- no connection is possible
if (this->connect_i() != timiq_NO_ERROR)
return false;
try
{
//- send to webserver
curl_easy_setopt(m_hw_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_hw_curl, CURLOPT_WRITEFUNCTION, &TIMIQCurl::writeTimIQCallback);
//- perform the query
result = curl_easy_perform(m_hw_curl);
//- clean up
curl_easy_cleanup(m_hw_curl);
}
catch (...)
{
m_error_string = "TIMIQCurl::read_status_i: exception while reading TimIQ status on " + url;
return false;
}
//- check the result
//----------------------------
if (result != CURLE_OK)
{
//- HTTP error
m_error_string = m_timiq_internal_buff;
return false;
}
else
//- check the data returned
{
#else
m_timiq_internal_buff="message=System up and running\n";
m_timiq_internal_buff+="state=OK\n";
m_timiq_internal_buff+="code=1\n";
#endif
bool b_found = true;
//- key word: message
std::size_t found = m_timiq_internal_buff.find(kTIMIQ_MESSAGE_KEY);
b_found = (found != std::string::npos);
//- key word: state
found = m_timiq_internal_buff.find(kTIMIQ_STATE_KEY);
b_found &=(found != std::string::npos);
//- key word: code
found = m_timiq_internal_buff.find(kTIMIQ_CODE_KEY);
b_found &=(found != std::string::npos);
if (b_found)
{
//- extract the code value
std::string strCode = m_timiq_internal_buff.substr(found);
//- parse "="
yat::StringTokenizer tokKeyword (strCode, "=");
try
{
//- field
std::string fieldStr = tokKeyword.next_token();
//- value
codeRet = (E_timiq_code_t) tokKeyword.next_int_token();
}
catch(...)
{
m_error_string = "TIMIQCurl::read_state_and_status_i: exception while converting code from " + url;
m_error_string += m_timiq_internal_buff;
return false;
}
status = m_timiq_internal_buff;
m_error_string = "OK";
return true;
}
//- otherwise error
m_error_string = m_timiq_internal_buff;
#if !defined (_SIMULATION_)
}
#endif
return false;
}
}
\ No newline at end of file
//=============================================================================
// TIMIQCurl.h
//=============================================================================
// abstraction.......TimIQ low level access to TIMIQ hardware
// class.............TIMIQCurl
// original author...J. GOUNO - NEXEYA-FRANCE
//=============================================================================
#ifndef _TIMIQ_CURL_H_
#define _TIMIQ_CURL_H_
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include <iostream>
#include <curl/curl.h>
#include <TIMIQTypesAndConsts.h>
namespace TIMIQLib_ns
{
class TIMIQCurl
{
public:
// Contructor
TIMIQCurl (const std::string& ip_address, const std::string& num_port);
// Destructor
~TIMIQCurl();
// Writes data on TimIQ equipment
// @param data float value.
E_timiq_errno_t write_data(float& data);
// Writes "I" tension on TimIQ equipment
// @param iValue float value.
E_timiq_errno_t write_iValue(float& iValue);
// Writes "Q" tension to TimIQ equipment
// @param qValue float value.
E_timiq_errno_t write_qValue(float& qValue);
// Writes board temperature to TimIQ equipment
// @param boardTemperature float value.
E_timiq_errno_t write_boardTemperature(float& boardTemperature);
// Writes command on TimIQ equipment
// @param cmd TimIQCmd_t value.
E_timiq_errno_t write_command(E_timiq_cmd_t& cmd);
// Reads data from TimIQ equipment
// @param [out] data float pointer.
E_timiq_errno_t read_data(float& data);
// Reads "I" value from TimIQ equipment
// @param [out] iValue float pointer.
E_timiq_errno_t read_iValue(float& iValue);
// Reads "Q" value from TimIQ equipment
// @param [out] qValue float pointer.
E_timiq_errno_t read_qValue(float& qValue);
// Reads the mixer cosinus output from TimIQ equipment
// @param [out] mixerCosOutput float pointer.
E_timiq_errno_t read_mixerCosOutput(float& mixerCosOutput);
// Reads the mixer sinus output from TimIQ equipment
// @param [out] mixerSinOutput float pointer.
E_timiq_errno_t read_mixerSinOutput(float& mixerSinOutput);
// Reads the board temperature from TimIQ equipment
// @param [out] boardTemperature float pointer.
E_timiq_errno_t read_boardTemperature(float& boardTemperature);
// Reads TimIQ state of TimIQ equipment
// @param [out] state vector of string
E_timiq_errno_t read_state_and_status(std::string& status, E_timiq_code_t& codeRet);
// Gets error messages
// @return string variable
std::string get_err_msg(void)
{
return m_error_string;
}
private:
//- internal members
//-------------------
//- IP address +":"+ port number
std::string m_base_addr;
//- CURL easy handle
CURL * m_hw_curl;
// message error
std::string m_error_string;
//- timiq internal web buffer
std::string m_timiq_internal_buff;
//- internal functions
//--------------------
// ...
protected:
//- internal functions
//--------------------
// connects to api
// connects to an easy handle using curl
// @return 0: no error, others: an internal error occurred
E_timiq_errno_t connect_i();
// write callback functions
// @param buffer data , size*nb_member buffer size, void pointer
// @return size*nb_member buffer size
size_t writeTimIQCallback( char *buffer, size_t size, size_t nb_member, void *);
// writes on timiq WebServer
// @param url of the page, strData data to send
// @return true if success, false otherwise
bool write_i(std::string& url, std::string& strData);
// reads float data from timiq WebServer
// @param url of the page, strData data value to find, [out]flaot value to extracted
// @return true if success, false otherwise
bool read_float_i(const std::string& url, const std::string& strData, float& value );
// reads timiq equipment state and status
// @param url of the page, string state
// @return true if success, false otherwise
bool read_state_and_status_i(const std::string& url, std::string& state, E_timiq_code_t& codeRet );
};
}
#endif //- _TIMIQ_CURL_H_
\ No newline at end of file
//=============================================================================
// TIMIQException.cpp
//=============================================================================
// abstraction.......Tim IQ Application Programming Interface
// class.............TIMIQ Error & Exception implementation
// original author...xxxxx
//=============================================================================
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include "TIMIQException.h"
namespace TIMIQLib_ns {
// ============================================================================
// Error::Error
// ============================================================================
Error::Error ()
: reason ("unknown"),
desc ("unknown exception"),
origin ("unknown"),
code (-1),
severity (TIMIQLib_ns::ERR)
{
}
// ============================================================================
// Error::Error
// ============================================================================
Error::Error (const char *_reason,
const char *_desc,
const char *_origin,
int _code,
TIMIQLib_ns::Severity _severity)
: reason (_reason),
desc (_desc),
origin (_origin),
code (_code),
severity (_severity)
{
}
// ============================================================================
// Error::Error
// ============================================================================
Error::Error (const std::string& _reason,
const std::string& _desc,
const std::string& _origin,
int _code,
TIMIQLib_ns::Severity _severity)
: reason (_reason),
desc (_desc),
origin (_origin),
code (_code),
severity (_severity)
{
}
// ============================================================================
// Error::Error
// ============================================================================
Error::Error (const Error& _src)
: reason (_src.reason),
desc (_src.desc),
origin (_src.origin),
code (_src.code),
severity (_src.severity)
{
}
// ============================================================================
// Error::~Error
// ============================================================================
Error::~Error ()
{
}
// ============================================================================
// Error::operator=
// ============================================================================
Error& Error::operator= (const Error& _src)
{
//- no self assign
if (this == &_src) {
return *this;
}
this->reason = _src.reason;
this->desc = _src.desc;
this->origin = _src.origin;
this->code = _src.code;
this->severity = _src.severity;
return *this;
}
// ============================================================================
// Exception::Exception
// ============================================================================
Exception::Exception ()
: errors(0)
{
this->push_error(Error());
}
// ============================================================================
// Exception::Exception
// ============================================================================
Exception::Exception (const char *_reason,
const char *_desc,
const char *_origin,
int _code,
TIMIQLib_ns::Severity _severity)
: errors(0)
{
this->push_error(Error(_reason, _desc, _origin, _code, _severity));
}
// ============================================================================
// Exception::Exception
// ============================================================================
Exception::Exception (const std::string& _reason,
const std::string& _desc,
const std::string& _origin,
int _code,
TIMIQLib_ns::Severity _severity)
: errors(0)
{
this->push_error(_reason, _desc, _origin, _code, _severity);
}
// ============================================================================
// Exception::Exception
// ============================================================================
Exception::Exception (const Exception& _src)
: errors(0)
{
for (unsigned int idx = 0; idx < _src.errors.size(); idx++)
{
this->push_error(_src.errors[idx]);
}
}
// ============================================================================
// Exception::Exception
// ============================================================================
Exception& Exception::operator= (const Exception& _src)
{
//- no self assign
if (this == &_src) {
return *this;
}
this->errors.clear();
for (unsigned int idx = 0; idx < _src.errors.size(); idx++)
{
this->push_error(_src.errors[idx]);
}
return *this;
}
// ============================================================================
// Exception::~Exception
// ============================================================================
Exception::~Exception ()
{
this->errors.clear();
}
// ============================================================================
// Exception::push_error
// ============================================================================
void Exception::push_error (const char *_reason,
const char *_desc,
const char *_origin,
int _code,
TIMIQLib_ns::Severity _severity)
{
this->errors.push_back(Error(_reason, _desc, _origin, _code, _severity));
}
// ============================================================================
// Exception::push_error
// ============================================================================
void Exception::push_error (const std::string& _reason,
const std::string& _desc,
const std::string& _origin,
int _code,
TIMIQLib_ns::Severity _severity)
{
this->errors.push_back(Error(_reason, _desc, _origin, _code, _severity));
}
// ============================================================================
// Exception::push_error
// ============================================================================
void Exception::push_error (const Error& _error)
{
this->errors.push_back(_error);
}
} // namespace TIMIQLib_ns
//=============================================================================
// TIMIQException.h
//=============================================================================
// abstraction.......Tim IQ Application Programming Interface
// class.............TIMIQ Error & Exception specification
// original author...xxxxxx
//=============================================================================
#ifndef _TIMIQ_EXCEPTION_H_
#define _TIMIQ_EXCEPTION_H_
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include <string>
#include <vector>
namespace TIMIQLib_ns {
// ============================================================================
// TIMIQ error severities
// ============================================================================
typedef enum {
WARN,
ERR,
PANIC
} Severity;
// ============================================================================
// The TIMIQ Error abstraction base class.
//
// Contains 5 fields:
// ° reason
// ° description
// ° origin
// ° error code
// ° severity
// ============================================================================
class Error
{
public:
// Constructor.
Error ();
// Constructor with parameters.
Error (const char *_reason,
const char *_desc,
const char *_origin,
int _code = -1,
TIMIQLib_ns::Severity _severity = TIMIQLib_ns::ERR);
// Constructor with parameters.
Error (const std::string& _reason,
const std::string& _desc,
const std::string& _origin,
int _code = -1,
TIMIQLib_ns::Severity _severity = TIMIQLib_ns::ERR);
// Copy constructor.
Error (const Error& _src);
// Destructor.
virtual ~Error ();
// Operator=
Error& operator= (const Error& _src);
// Error details: reason
std::string reason;
// Error details: description
std::string desc;
// Error details: origin
std::string origin;
// Error details: code
int code;
// Error details: severity
TIMIQLib_ns::Severity severity;
};
// ============================================================================
// The TIMIQ error list.
// ============================================================================
typedef std::vector<Error> ErrorList;
// ============================================================================
// The TIMIQ Exception abstraction base class.
//
// Contains a list of TIMIQ Errors.
//
// ============================================================================
class Exception
{
public:
// Constructor.
Exception ();
// Constructor with parameters.
Exception (const char *_reason,
const char *_desc,
const char *_origin,
int _code = -1,
TIMIQLib_ns::Severity _severity = TIMIQLib_ns::ERR);
// Constructor with parameters.
Exception (const std::string& _reason,
const std::string& _desc,
const std::string& _origin,
int _code = -1,
TIMIQLib_ns::Severity _severity = TIMIQLib_ns::ERR);
// Constructor from Error class.
Exception (const Error& error);
// Copy constructor.
Exception (const Exception& _src);
// Operator=
Exception& operator= (const Exception& _src);
// Destructor.
virtual ~Exception ();
// Pushes the specified error into the errors list.
void push_error (const char *_reason,
const char *_desc,
const char *_origin,
int _code = -1,
TIMIQLib_ns::Severity _severity = TIMIQLib_ns::ERR);
// Pushes the specified error into the errors list.
void push_error (const std::string& _reason,
const std::string& _desc,
const std::string& _origin,
int _code = -1,
TIMIQLib_ns::Severity _severity = TIMIQLib_ns::ERR);
// Pushes the specified error into the errors list.
void push_error (const Error& _error);
// The error list.
ErrorList errors;
};
} // namespace TIMIQLib_ns
#endif // _TIMIQ_EXCEPTION_H_
//=============================================================================
// TIMIQLib.cpp
//=============================================================================
// abstraction.......TimIQ Application Programming Interface
// class.............TIMIQLib
// original author...J. GOUNO - NEXEYA-FRANCE
//=============================================================================
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include <timiq/TIMIQLib.h>
#include "TIMIQCurl.h"
namespace TIMIQLib_ns {
// ============================================================================
//- Check low level access to TIMIQ hardware pointer:
#define CHECK_TIMIQ_HW \
if (!m_timiq_hw) \
{ \
throw Exception(static_cast<const char*>("INTERNAL_ERROR"), \
static_cast<const char*>("Low level access to TIMIQ hardware pointer is null!"), \
static_cast<const char*>("CHECK_TIMIQ_HW()")); \
}
// ============================================================================
// ============================================================================
// TIMIQLib::TIMIQLib
// ============================================================================
TIMIQLib::TIMIQLib ()
{
//std::cout << "TIMIQLib constructor" << std::endl;
m_timiq_hw = NULL;
m_ipAddress = "";
m_numPort = "";
}
// ============================================================================
// TIMIQLib::~TIMIQLib()
// ============================================================================
TIMIQLib::~TIMIQLib()
{
//std::cout << "TIMIQLib destructor" << std::endl;
if (m_timiq_hw)
{
delete m_timiq_hw;
m_timiq_hw = NULL;
}
}
// ============================================================================
// TIMIQLib::init
// ============================================================================
void TIMIQLib::init(const std::string& ip_address, const short& num_port)
throw (Exception)
{
//- chek Ip Address/ num port
if (ip_address.empty() || num_port == 0)
throw Exception(static_cast<const char*>("INTERNAL_ERROR"),
static_cast<const char*>("Ip address or port number not properly defined!"),
static_cast<const char*>("TIMIQLib::init"));
//-
m_ipAddress = ip_address;
char buffer[20];
sprintf(buffer, "%d", num_port);
m_numPort = buffer;
// instantiate TIMIQCurl pointer
m_timiq_hw = new TIMIQCurl(m_ipAddress, m_numPort);
CHECK_TIMIQ_HW;
}
// ============================================================================
// TIMIQLib::set_data
// ============================================================================
void TIMIQLib::set_data(float data)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- write data value on TIMIQ equipment
err = m_timiq_hw->write_data(data);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Set data 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_data()"));
}
}
// ============================================================================
// TIMIQLib::set_iValue
// ============================================================================
void TIMIQLib::set_iValue(float iValue)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- write "I" tension value on TIMIQ equipment
err = m_timiq_hw->write_iValue(iValue);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Set I 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_iValue()"));
}
}
// ============================================================================
// TIMIQLib::set_qValue
// ============================================================================
void TIMIQLib::set_qValue(float qValue)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- write "Q" tension value on TIMIQ equipment
err = m_timiq_hw->write_qValue(qValue);
if (err != timiq_NO_ERROR)
{
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_qValue()"));
}
}
// ============================================================================
// TIMIQLib::set_boardTemperature
// ============================================================================
void TIMIQLib::set_boardTemperature(float boardTemperature)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- write board temperature on TIMIQ equipment
err = m_timiq_hw->write_boardTemperature(boardTemperature);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Set board Temperature 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_boardTemperature()"));
}
}
// ============================================================================
// TIMIQLib::set_command
// ============================================================================
void TIMIQLib::set_command(E_timiq_cmd_t& cmd)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- write command on TIMIQ equipment
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()"));
}
}
// ============================================================================
// TIMIQLib::get_data
// ============================================================================
void TIMIQLib::get_data(float &data)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read data from TIMIQ equipment
err = m_timiq_hw->read_data(data);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get data 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::get_data()"));
}
}
// ============================================================================
// TIMIQLib::get_iValue
// ============================================================================
void TIMIQLib::get_iValue(float& iValue)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read "I" tension value from TIMIQ equipment
err = m_timiq_hw->read_iValue(iValue);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get I 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::get_iValue()"));
}
}
// ============================================================================
// TIMIQLib::get_qValue
// ============================================================================
void TIMIQLib::get_qValue(float& qValue)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read "Q" tension value from TIMIQ equipment
err = m_timiq_hw->read_qValue(qValue);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get 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::get_qValue()"));
}
}
// ============================================================================
// TIMIQLib::get_mixerCosOutput
// ============================================================================
void TIMIQLib::get_mixerCosOutput(float& mixerCosOutput)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read mixer cos output value from TIMIQ equipment
err = m_timiq_hw->read_mixerCosOutput(mixerCosOutput);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get mixer cosinus output 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::get_mixerCosOutput()"));
}
}
// ============================================================================
// TIMIQLib::get_mixerSinOutput
// ============================================================================
void TIMIQLib::get_mixerSinOutput(float& mixerSinOutput)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read mixer sin output value from TIMIQ equipment
err = m_timiq_hw->read_mixerSinOutput(mixerSinOutput);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get mixer sinus output 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::get_mixerSinOutput()"));
}
}
// ============================================================================
// TIMIQLib::get_boardTemperature
// ============================================================================
void TIMIQLib::get_boardTemperature(float& boardTemperature)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read board temperature value from TIMIQ equipment
err = m_timiq_hw->read_boardTemperature(boardTemperature);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get board temperature 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::get_boardTemperature()"));
}
}
// ============================================================================
// TIMIQLib::get_state
// ============================================================================
E_timiq_code_t TIMIQLib::get_state(std::string& status)
throw (Exception)
{
E_timiq_errno_t err;
E_timiq_code_t retCode;
CHECK_TIMIQ_HW;
//- read status of TIMIQ equipment
err = m_timiq_hw->read_state_and_status(status, retCode);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get status 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::get_status()"));
}
return retCode;
}
} // namespace timiqlib
//=============================================================================
// TimIQTypesAndConsts.h
//=============================================================================
// abstraction.......Basic types and Constants for TimIQ library
// class.............Basic structures
// original author...J. GOUNO - NEXEYA-FRANCE
//=============================================================================
#ifndef _TIMIQ_TYPES_AND_CONSTS_H_
#define _TIMIQ_TYPES_AND_CONSTS_H_
//=============================================================================
// DEPENDENCIES
//=============================================================================
namespace TIMIQLib_ns
{
// ============================================================================
// CONSTANTS
// ============================================================================
//- web protocol: http
const std::string kHTTP_WEB_PROTOCOL = "http://";
//- Web page: data
//----------------------
const std::string kTIMIQ_DATA_PAGE= "/get_data.wsgi";
const std::string kTIMIQ_DATA_KEY= "data";
//- Web page: IValue
//----------------------
const std::string kTIMIQ_IVALUE_PAGE= "/set_iValue.wsgi";
const std::string kTIMIQ_IVALUE_KEY= "iValue";
//- Web page: QValue
//----------------------
const std::string kTIMIQ_QVALUE_PAGE= "/set_qValue.wsgi";
const std::string kTIMIQ_QVALUE_KEY= "qValue";
//- Web page: boardTemperature
//------------------------------
const std::string kTIMIQ_BOARD_TEMPERATURE_PAGE= "/set_boardTemperature.wsgi";
const std::string kTIMIQ_BOARD_TEMPERATURE_KEY= "boardTemperature";
//- Web page: command
//----------------------
const std::string kTIMIQ_COMMAND_PAGE= "/set_command.wsgi";
const std::string kTIMIQ_COMMAND_KEY= "command";
//- Web page: feedback
//-------------------------
const std::string kTIMIQ_FEEDBACK_PAGE= "/get_feedback.wsgi";
//- mixerCosOutput key
const std::string kTIMIQ_MIXER_COS_OUTPUT_KEY= "mixerCosOutput";
//- mixerSinOutput key
const std::string kTIMIQ_MIXER_SIN_OUTPUT_KEY= "mixerSinOutput";
//- Web page: state
//-------------------------
const std::string kTIMIQ_STATE_PAGE= "/get_feedback.wsgi";
//- message key
const std::string kTIMIQ_MESSAGE_KEY= "message=";
//- state key
const std::string kTIMIQ_STATE_KEY= "state=";
//- state code
const std::string kTIMIQ_CODE_KEY= "code=";
//- buffer key size
const size_t kMAX_DATA_SIZE = 35;
// ============================================================================
// TYPES
// ============================================================================
//- TimIQ library error numbers
typedef enum {
// No error occurred
timiq_NO_ERROR = 0,
// timIQ errors
timiq_internal_ERROR
}E_timiq_errno_t;
//- TimIQsSystem codes
typedef enum
{
Error = 0,
OK,
Busy,
Warning
} E_timiq_code_t;
//- TimIQsSystem commands
typedef enum
{
RESET_SYSTEM = 1,
RECALIBRATE_PLL
} E_timiq_cmd_t;
} //- namespace timiqlib
#endif //- _TIMIQ_TYPES_AND_CONSTS_H_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment