Page MenuHomec4science

main.cpp
No OneTemporary

File Metadata

Created
Mon, Dec 2, 05:16

main.cpp

/**
* \file main.cpp
*
* \date Sep 25, 2012
* \author Alexandre Tuleu
*/
#include <iostream>
const static unsigned int UnitializedByte = 0x0f00;
class HexaInt {
public :
friend std::ostream & operator << (std::ostream & out, const HexaInt & h);
friend std::istream & operator >> (std::istream & in, HexaInt & h);
HexaInt() : d_value(UnitializedByte){};
unsigned int Value() {
return d_value;
}
private :
unsigned int d_value;
};
std::ostream & operator << (std::ostream & out, const HexaInt & h){
std::ios_base::fmtflags oldflags = out.flags();
out << "0x" << std::hex << h.d_value;
out.flags(oldflags);
return out;
}
std::istream & operator >> (std::istream & in, HexaInt & h){
std::ios_base::fmtflags oldflags = in.flags();
in >> std::hex >> h.d_value;
in.flags(oldflags);
return in;
}
#include <libsbcp-tool/argstream.h>
#include <libsbcp-tool/Config.h>
#include <libatuleu/log/Logger.h>
void FillDataIfNeeded(const std::string & name, bool nonInteractif, HexaInt & data){
std::ostringstream os;
if(data.Value() == UnitializedByte){
if(nonInteractif){
os << name << " should be provided.";
throw std::runtime_error(os.str());
}
std::cout << "Please enter a " << name << " : " << std::flush;
std::cin >> data;
}
if(data.Value() > 0xff){
os << name << "should be lower or equal to 0xff (here : " << data << ").";
throw std::runtime_error(os.str());
}
}
int main(int argc, char ** argv){
HexaInt klass,oldId ,newId;
bool nonInteractive(false);
argstream as(argc,argv);
as >> parameter('c',"class",klass,"class to scan for",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();
std::tr1::shared_ptr<sbcp::Bus> bus = sbcp::Config::Instance().CreateBusFromStdFile();
FillDataIfNeeded("class",nonInteractive,klass);
FillDataIfNeeded("id of device",nonInteractive,oldId);
FillDataIfNeeded("new id",nonInteractive,newId);
sbcp::Device::Ptr dev = bus->OpenGenericDevice(klass.Value(),oldId.Value());
if(! dev){
std::ostringstream os;
os << "Could not find device " << klass << ":" << oldId << std::endl;
throw std::runtime_error(os.str());
}
if(!nonInteractive){
std::string foo;
while (foo != "yes" && foo != "no"){
std::cout << "Really change device "<< klass
<< ":" << oldId << " to " << klass << ":" << newId << " [yes/no]?"
<< std::flush;
std::cin >> foo;
if(foo =="no"){
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);
dev->SetRegister(sbcp::Device::SBCP_ID,newId.Value());
return 0;
}

Event Timeline