Page MenuHomec4science

main.cpp
No OneTemporary

File Metadata

Created
Sat, Nov 30, 07:39

main.cpp

/**
* \file main.cpp
*
* \date Sep 25, 2012
* \author Alexandre Tuleu
*/
#include <iostream>
#include <libsbcp/utils/HexaByte.h>
#include <libsbcp-tool/argstream.h>
#include <libsbcp/utils/Config.h>
#include <libsbcp-tool/UIElements.h>
#include <biorob-cpp/log/Logger.h>
int main(int argc, char ** argv){
sbcp::HexaByte klass,oldId ,newId;
bool nonInteractive(false);
argstream as(argc,argv);
std::string busName(sbcp::BusesConfigSection::DEFAULT_BUS_NAME);
as >> parameter('c',"class",klass,"class to scan for",false)
>> parameter('b',"bus",busName,"name of the bus to use",false)
>> parameter('o',"old_id",oldId,"id to modify",false)
>> parameter('n',"new_id",newId,"new id",false)
>> option('s',"non-interactive",nonInteractive,"run in non interactif mode")
>> help();
as.defaultErrorHandling();
sbcp::Config config;
config.LoadAllFiles();
std::tr1::shared_ptr<sbcp::Bus> bus = config.OpenBusWithFrame(busName,1,16);
FillHexaByteIfNeeded("class",nonInteractive,klass);
FillHexaByteIfNeeded("id of device",nonInteractive,oldId);
FillHexaByteIfNeeded("new id",nonInteractive,newId);
sbcp::Device::Ptr dev = bus->OpenGenericDevice(klass,oldId);
if(! dev){
std::ostringstream os;
os << "Could not find device " << klass << ":" << oldId << std::endl;
throw std::runtime_error(os.str());
}
if(!nonInteractive){
std::ostringstream os;
os << "Really change device " << klass << ":" << oldId << " to "
<< klass;
if(!AskYesNoQuestion(os.str())){
return 0;
}
}
loggers::debug.SetCerr();
log(debug,"Unlocking device ", klass, ":", oldId , " register " , sbcp::Device::SBCP_ID);
dev->UnlockRegister(sbcp::Device::SBCP_ID);
log(debug,"Unlocking device ", klass, ":", oldId , " register " , sbcp::Device::SBCP_ID , " to " , newId);
try {
dev->SetRegister(sbcp::Device::SBCP_ID,newId);
} catch (const sbcp::TransferError & e) {
// a transfer error will be issued, since the ID changed !!!!
//check if it does have changed !!!
if(e.EmettorID() != newId.AsByte() || e.EmettorClass() != klass.AsByte()){
throw;
}
}
return 0;
}

Event Timeline