diff --git a/Makefile b/Makefile index d587ccaf4a33609521c53658040fcd266e953e82..0fea46a8bdd71e19514ab897ba13722fece65f61 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ NAME_SRV = twin-apu-id-srv SRC_FILES = $(wildcard deps/supervisedid/src/SupervisedID*.cpp) -CXXFLAGS += -I deps/supervisedid/src -I/runtime/elettra/include -LDFLAGS += -L/runtime/elettra/lib -llpc -lnc +CXXFLAGS += -I deps/supervisedid/src include ../makefiles/Make-9.3.3.in diff --git a/deps/supervisedid b/deps/supervisedid index fd9e432b85d18a1534df37a59c24f1865c52049e..2c5fa6e4e7672d4dc84b773533062cce1d6ef969 160000 --- a/deps/supervisedid +++ b/deps/supervisedid @@ -1 +1 @@ -Subproject commit fd9e432b85d18a1534df37a59c24f1865c52049e +Subproject commit 2c5fa6e4e7672d4dc84b773533062cce1d6ef969 diff --git a/src/TwinApuId.cpp b/src/TwinApuId.cpp index 819af3b220b1c9152d7ced8de0c56952a375ea22..5fe3d3fe8cf1903d533f4cf486314615761be826 100644 --- a/src/TwinApuId.cpp +++ b/src/TwinApuId.cpp @@ -87,7 +87,7 @@ namespace TwinApuId_ns { /*----- PROTECTED REGION ID(TwinApuId::namespace_starting) ENABLED START -----*/ - +using namespace SupervisedID_ns; // static initializations /*----- PROTECTED REGION END -----*/ // TwinApuId::namespace_starting @@ -318,7 +318,6 @@ void TwinApuId::get_device_property() dev_prop.push_back(Tango::DbDatum("LowEnergyPeriod")); dev_prop.push_back(Tango::DbDatum("MaxTimeToInjectionPosition")); dev_prop.push_back(Tango::DbDatum("maxTimeToEnergySwitch")); - dev_prop.push_back(Tango::DbDatum("HostPrivileges")); dev_prop.push_back(Tango::DbDatum("ReadThreadPollTime")); // is there at least one property to be read ? @@ -439,17 +438,6 @@ void TwinApuId::get_device_property() // And try to extract maxTimeToEnergySwitch value from database if (dev_prop[i].is_empty()==false) dev_prop[i] >> maxTimeToEnergySwitch; - // Try to initialize HostPrivileges from class property - cl_prop = ds_class->get_class_property(dev_prop[++i].name); - if (cl_prop.is_empty()==false) cl_prop >> hostPrivileges; - else { - // Try to initialize HostPrivileges from default device value - def_prop = ds_class->get_default_device_property(dev_prop[i].name); - if (def_prop.is_empty()==false) def_prop >> hostPrivileges; - } - // And try to extract HostPrivileges value from database - if (dev_prop[i].is_empty()==false) dev_prop[i] >> hostPrivileges; - // Try to initialize ReadThreadPollTime from class property cl_prop = ds_class->get_class_property(dev_prop[++i].name); if (cl_prop.is_empty()==false) cl_prop >> readThreadPollTime; @@ -621,7 +609,7 @@ void TwinApuId::write_Enabled(Tango::WAttribute &attr) attr.get_write_value(w_val); /*----- PROTECTED REGION ID(TwinApuId::write_Enabled) ENABLED START -----*/ - int priv=caller_privileges(); + privilege priv=caller_privileges(); if (priv < PRIVILEGED){ INFO_STREAM << "Enabled from "<< clientAddress <<" REJECTED"<<endl; TangoSys_OMemStream o; @@ -1197,76 +1185,7 @@ void TwinApuId::add_dynamic_commands() /*----- PROTECTED REGION ID(TwinApuId::namespace_ending) ENABLED START -----*/ // Additional Methods -//------------------------------------------------------------------------------- - /** - * assign privileges elevel based on host name from which the call is generated - * PRIVILEGED : can change the enable bit and set gap/tapering - * AUTHORIZED : can set gap/tapering when enabled - * NORMAL : can only read; - */ -int TwinApuId::caller_privileges(void) -{ - //if no privileges are configured, run unimpeded - if(hostMap.size() == 0)return PRIVILEGED; - Tango::DevVarStringArray blackbox = black_box(1)[0]; - string last_msg(blackbox[0].in()); - string::size_type loc = last_msg.find( " requested from ", 0 ); - if( loc != string::npos ) - { - clientAddress = last_msg.substr(loc + strlen(" requested from ")); - loc=clientAddress.find(" ",0); - clientAddress =clientAddress.substr(0,loc); - INFO_STREAM<<"checking call from "<<clientAddress<<endl; - // std::set<std::string> client_ips; - // get_ips_from_host(clientAddress, client_ips); - // - // for (std::set<std::string>::const_iterator it = client_ips.begin(); it != client_ips.end(); ++it ) - // { - // if (std::find(allowed_ips.begin(), allowed_ips.end(), *it) != allowed_ips.end()) - // return; // found the IP in the allowed list of IPs - // } - map<string,int>::iterator it; - it=hostMap.find(clientAddress); - if (it == hostMap.end()) { - INFO_STREAM << clientAddress<< " host not foud" << endl; - return NORMAL; - } - else { - INFO_STREAM << "host: "<< clientAddress<< " privilges="<< it->second << endl; - return it->second; - } - - } - else - { - WARN_STREAM << "Cannot extract client IP or hostname from tango blackbox, denying access."; - clientAddress=""; - return NORMAL; //do not grant special privileges - } - return NORMAL; //do not grant special privilege - but we should NOT arrive at this line -} -//-------------------------------------------------------------------- -/** - * check privileges level based on host name from which the call is generated - * formats warning/ error messages and throw exception in case of vilotaions - * privilege levels: - * PRIVILEGED : can change the enable bit and set gap/tapering - * AUTHORIZED : can set gap/tapering when enabled - * NORMAL : can only read; - */ - -int TwinApuId::check_privileges(int level, string msg) -{ - int priv=caller_privileges(); - if (priv < level){ - INFO_STREAM << msg << " from "<< clientAddress <<" REJECTED"<<endl; - TangoSys_OMemStream o; - o << msg << " from "<< clientAddress <<" REJECTED"<<ends; - Tango::Except::throw_exception((const char *)"Host not allowed",o.str(),msg); - } - return priv; -} /*----- PROTECTED REGION END -----*/ // TwinApuId::namespace_ending } // namespace diff --git a/src/TwinApuId.h b/src/TwinApuId.h index 13be82c186207b98cf1b42fbf3ae962d21961ff4..7dcd4663e44f6364ab3b826bfdc7022864d3424f 100644 --- a/src/TwinApuId.h +++ b/src/TwinApuId.h @@ -41,10 +41,6 @@ #include "readthread.h" #include "switchenergythread.h" -#include <map> -#define PRIVILEGED 2 -#define AUTHORIZED 1 -#define NORMAL 0 /*----- PROTECTED REGION END -----*/ // TwinApuId.h /** @@ -79,14 +75,6 @@ public: Tango::DevState _state; bool switching_energy; -protected: - int caller_privileges(void); - bool is_privileged; //true is the caller has all privileges - must be handled in alawys_executed - string clientAddress; - map<string,int> hostMap; - int check_privileges(int, string); //check privileges , emits diagnostics and throw exception, returns actual privileges - - /*----- PROTECTED REGION END -----*/ // TwinApuId::Data Members // Device property data members @@ -115,11 +103,6 @@ public: Tango::DevUShort maxTimeToInjectionPosition; // maxTimeToEnergySwitch: maximum tim [s] to perform an energy switch of the APUs Tango::DevUShort maxTimeToEnergySwitch; - // HostPrivileges: list of host:privileges - // host:2 host is privileged can read and write and change limits and change permission - // host:1 host is authorized can read and write when permitted - // host:0 host can only read - vector<string> hostPrivileges; // ReadThreadPollTime: polling time for the read thread [ms] Tango::DevULong readThreadPollTime; diff --git a/src/TwinApuId.xmi b/src/TwinApuId.xmi index 549dd730ba488a614e7ade8b0df91d72e244948b..f2b3f8a8a7ffd5211ac3e3d8036519cb859932cc 100644 --- a/src/TwinApuId.xmi +++ b/src/TwinApuId.xmi @@ -53,10 +53,6 @@ <status abstract="false" inherited="false" concrete="true" concreteHere="true"/> <DefaultPropValue>170</DefaultPropValue> </deviceProperties> - <deviceProperties name="HostPrivileges" description="list of host:privileges
host:2 host is privileged can read and write and change limits and change permission
host:1 host is authorized can read and write when permitted
host:0 host can only read"> - <type xsi:type="pogoDsl:StringVectorType"/> - <status abstract="false" inherited="false" concrete="true" concreteHere="true"/> - </deviceProperties> <deviceProperties name="ReadThreadPollTime" description="polling time for the read thread [ms]"> <type xsi:type="pogoDsl:UIntType"/> <status abstract="false" inherited="false" concrete="true" concreteHere="true"/> diff --git a/src/TwinApuIdClass.cpp b/src/TwinApuIdClass.cpp index 6568b1bc26ce691116744687f23efde3bb9e99e1..67be78848c3f68e495acc3620cfadbd5c404ef0c 100644 --- a/src/TwinApuIdClass.cpp +++ b/src/TwinApuIdClass.cpp @@ -467,19 +467,6 @@ void TwinApuIdClass::set_default_property() dev_def_prop.push_back(data); add_wiz_dev_prop(prop_name, prop_desc, prop_def); } - else - add_wiz_dev_prop(prop_name, prop_desc); - prop_name = "HostPrivileges"; - prop_desc = "list of host:privileges\nhost:2 host is privileged can read and write and change limits and change permission\nhost:1 host is authorized can read and write when permitted\nhost:0 host can only read"; - prop_def = ""; - vect_data.clear(); - if (prop_def.length()>0) - { - Tango::DbDatum data(prop_name); - data << vect_data ; - dev_def_prop.push_back(data); - add_wiz_dev_prop(prop_name, prop_desc, prop_def); - } else add_wiz_dev_prop(prop_name, prop_desc); prop_name = "ReadThreadPollTime"; @@ -513,7 +500,7 @@ void TwinApuIdClass::write_class_property() Tango::DbData data; string classname = get_name(); string header; - string::size_type start, end; + //string::size_type start, end; // Put title Tango::DbDatum title("ProjectTitle"); diff --git a/src/switchenergythread.cpp b/src/switchenergythread.cpp index 32e46b1916df5a28d8937777a1219a62a1ff3d54..c0b9a920f1a5241ed44450d31c6b92714c3b9249 100644 --- a/src/switchenergythread.cpp +++ b/src/switchenergythread.cpp @@ -11,7 +11,7 @@ namespace TwinApuId_ns { switchenergythread::switchenergythread(Tango::DeviceImpl *devImpl, bool ene) : - Tango::LogAdapter(devImpl), device(devImpl), abortflag(false), high_energy(ene){ + Tango::LogAdapter(devImpl), device(devImpl), abortflag(false), high_energy(ene){ //DEBUG_STREAM << "switchenergythread::switchenergythread(): constructor << endl; } @@ -24,7 +24,7 @@ switchenergythread::switchenergythread(Tango::DeviceImpl *devImpl, bool ene) : // //------------------------------------------------------------------- void* switchenergythread::run_undetached(void*) { - INFO_STREAM << "switchenergythread::run(): running... high_energy=" + DEBUG_STREAM << "switchenergythread::run(): running... high_energy=" << high_energy << endl; struct timeval begin, end; @@ -60,31 +60,30 @@ void* switchenergythread::run_undetached(void*) { gettimeofday(&begin, NULL); //tweak initial time - useconds_t sleep_time = 500000; //500 ms - usleep(sleep_time); // give time for reaction of low level systems + useconds_t sleep_time = 1000 * _devicebase->readThreadPollTime; //wait one refersh cyslce for checking state + usleep(2 * sleep_time); // give time for reaction of low level systems end detecting it Tango::DevState mstate = _devicebase->_state; bool not_moving = (mstate == Tango::DevState::OFF || mstate == Tango::DevState::STANDBY) && (mstate != Tango::DevState::FAULT); - while ( !not_moving) //one second loop + while ( !not_moving ) //one second loop { - // check forced exit + // check forced exit if (abortflag){ _devicebase->switching_energy=false; return 0; } - - // bulk - wait for injection_position to stop - mstate = _devicebase->_state; - not_moving = (mstate == Tango::DevState::OFF || mstate == Tango::DevState::STANDBY) && (mstate != Tango::DevState::FAULT); + // wait for injection_position to be reached + mstate = _devicebase->_state; + not_moving = (mstate == Tango::DevState::OFF || mstate == Tango::DevState::STANDBY) && (mstate != Tango::DevState::FAULT); // timekeeping and checking - INFO_STREAM << "switchenergy wait... not_moving=" << not_moving; + INFO_STREAM << "switchenergy waiting, not_moving=" << not_moving; gettimeofday(&end, NULL); time_t diff_time = ((end.tv_sec - begin.tv_sec) + ((time_t) (end.tv_usec - begin.tv_usec)) / 1000000); if (diff_time > _devicebase->max_time_to_inj_position) { ERROR_STREAM - << "switchenergythread : injection_position not reached in time"; + << "switchenergythread : injection_position not reached in time"; _devicebase->set_state(Tango::DevState::FAULT); _devicebase->set_status("injection_position not reached in time"); _devicebase->switching_energy = false; @@ -95,13 +94,13 @@ void* switchenergythread::run_undetached(void*) { if (abortflag) { _devicebase->switching_energy = false; return 0; - } //end immediately - + } //end immediately on abort + usleep(2 * sleep_time); // give time for reaction of low level systems end detecting it // now switch energy only if ready, otherwise notify... mstate = _devicebase->_state; - not_moving = (mstate == Tango::DevState::OFF || mstate == Tango::DevState::STANDBY) && (mstate != Tango::DevState::FAULT); + not_moving = (mstate == Tango::DevState::OFF || mstate == Tango::DevState::STANDBY ) && (mstate != Tango::DevState::FAULT); INFO_STREAM << "switch energy phase2 not_moving=" << not_moving << " state "<<mstate; - if (not_moving){ + if (mstate != Tango::DevState::FAULT){ Tango::DevDouble pos; if (high_energy) pos = _devicebase->highEnergyPosition; @@ -109,21 +108,18 @@ void* switchenergythread::run_undetached(void*) { pos = _devicebase->lowEnergyPosition; Tango::DeviceAttribute da("UserPosition", pos); try { - energy_switch_device->write_attribute(da); - _devicebase->set_state(Tango::DevState::MOVING); + //_devicebase->set_state(Tango::DevState::MOVING); *_devicebase->attr_Ready_read = false; _devicebase->id_flags[0] = false; - } catch (Tango::DevFailed &e) { Tango::Except::print_exception(e); _devicebase->set_state(Tango::DevState::FAULT); _devicebase->set_status( "failed to write attribute UserPosition on energy_switch_device"); ERROR_STREAM - << "failed to write attribute UserPosition on energy_switch_device"; + << "failed to write attribute UserPosition on energy_switch_device"; } - } else { _devicebase->set_state(Tango::DevState::FAULT); _devicebase->set_status( @@ -132,11 +128,39 @@ void* switchenergythread::run_undetached(void*) { _devicebase->switching_energy=false; return 0; } + usleep(2 * sleep_time); // give time for reaction of low level systems end detecting it + // step 3: wait for energy motor to stop or max time + not_moving = ( *_devicebase->attr_EnergySwitchAxisState_read == Tango::DevState::OFF || *_devicebase->attr_EnergySwitchAxisState_read == Tango::DevState::STANDBY); + INFO_STREAM << "switch energy phase3 not_moving=" << not_moving << " state "<<mstate; + while ( !not_moving) + { + // check forced exit + if (abortflag){ + _devicebase->switching_energy=false; + return 0; + } + // wait for final position to be reached + not_moving = ( *_devicebase->attr_EnergySwitchAxisState_read == Tango::DevState::OFF || *_devicebase->attr_EnergySwitchAxisState_read == Tango::DevState::STANDBY); + // timekeeping and checking + DEBUG_STREAM << "switchenergy waiting, not_moving=" << not_moving; + gettimeofday(&end, NULL); + time_t diff_time = ((end.tv_sec - begin.tv_sec) + + ((time_t) (end.tv_usec - begin.tv_usec)) / 1000000); + DEBUG_STREAM << " times " << diff_time << " " << _devicebase->maxTimeToEnergySwitch; + if (diff_time > _devicebase->maxTimeToEnergySwitch) { + ERROR_STREAM + << "switchenergythread : injection_position not reached in time"; + _devicebase->set_state(Tango::DevState::FAULT); + _devicebase->set_status("injection_position not reached in time"); + _devicebase->switching_energy = false; + return 0; + } + usleep(sleep_time); + } // while - INFO_STREAM << "switchenergythread::run(): exit!!!" << endl; + DEBUG_STREAM << "switchenergythread::run(): exit!!!" << endl; _devicebase->switching_energy=false; return 0; - } }