1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
/*!
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright(c) 2009 Apogee Instruments, Inc.
* \class AspenEthernetIo
* \brief Class for talking with apsen generation cameras with libcurl
*
*/
#include "AspenEthernetIo.h"
#include "libCurlWrap.h"
#include "apgHelper.h"
#include "helpers.h"
#include "ApgLogger.h"
#include "CamHelpers.h"
#include <sstream>
#include <ctime>
#include <algorithm>
// TODO figure out what to do
// with the pretty formatting. g++ doesn't
// seem to have full regex support
//#ifdef WIN_OS
//#include <regex>
//#else
//#include <tr1/regex>
//#endif
#include <cstring> //for memcpy
namespace
{
bool IsResultOk(const std::string & response)
{
if( std::string::npos != response.find("OK") )
{
return true;
}
else
{
return false;
}
}
bool IsSessionOk(const std::string & response, const std::string & key )
{
size_t found = response.find("=");
if( std::string::npos != found )
{
if( 0 == response.compare( found+1, key.size(), key) )
{
return true;
}
}
return false;
}
const uint32_t MAX_STRDB_READ_BYTES = 1280;
}
////////////////////////////
// CTOR
AspenEthernetIo::AspenEthernetIo( const std::string url )
: m_url( url )
, m_fileName( __BASE_FILE__ )
, m_sessionKey("")
, m_sessionKeyUrlStr("")
, m_libcurl( new CLibCurlWrap )
{
//make the guid for the session key from
//the date/time
time_t rawtime;
time( &rawtime );
struct tm * timeinfo = localtime( &rawtime );
char buffer [80];
strftime(buffer,80,"%Y%m%d%H%M%S", timeinfo);
m_sessionKey.append( buffer );
m_sessionKeyUrlStr.append("&keyval=");
m_sessionKeyUrlStr.append( m_sessionKey );
StartSession();
m_lastExposureTimeRegHigh = 0;
m_lastExposureTimeRegLow = 0;
}
////////////////////////////
// DTOR
AspenEthernetIo::~AspenEthernetIo()
{
EndSession();
}
////////////////////////////
// START SESSION
void AspenEthernetIo::StartSession()
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=Start_Session" + m_sessionKeyUrlStr;
std::string result;
m_libcurl->HttpGet( fullUrl, result );
if( !IsSessionOk( result, m_sessionKey ) )
{
std::string errMsg = "ERROR - command " + fullUrl + " failed.";
apgHelper::throwRuntimeException( m_fileName, errMsg,
__LINE__, Apg::ErrorType_Critical );
}
//log device we are connected to
const std::string msg = "Connected to device " + m_url;
ApgLogger::Instance().Write(ApgLogger::LEVEL_RELEASE,"info",
msg );
}
////////////////////////////
// END SESSION
void AspenEthernetIo::EndSession()
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=End_Session" + m_sessionKeyUrlStr;
std::string result;
m_libcurl->HttpGet( fullUrl, result );
if( !IsSessionOk( result, m_sessionKey ) )
{
std::string errMsg = "ERROR - command " + fullUrl + " failed.";
std::string msg2Log = apgHelper::mkMsg( m_fileName, errMsg, __LINE__ );
//since this is called by the dtor all I can do is
//log that something bad happened
ApgLogger::Instance().Write(ApgLogger::LEVEL_RELEASE, "error", msg2Log);
}
//log what device we have disconnected from
const std::string msg = "Connection to device " + m_url + " is closed.";
ApgLogger::Instance().Write(ApgLogger::LEVEL_RELEASE,"info",
msg );
}
////////////////////////////
// READ REG
uint16_t AspenEthernetIo::ReadReg( uint16_t reg ) const
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=CC_Reg_Rd&wIndex=" +
help::uShort2Str(reg) + "&wValue=1" + m_sessionKeyUrlStr;
std::string result;
m_libcurl->HttpGet(fullUrl, result );
uint16_t FpgaData = 0;
std::stringstream is( result );
is >> std::hex >> FpgaData;
return FpgaData;
}
////////////////////////////
// WRITE REG
void AspenEthernetIo::WriteReg( uint16_t reg, uint16_t val )
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=CC_Reg_Wr&wIndex=" +
help::uShort2Str(reg) + "&wValue=1¶m=" +
help::uShort2Str(val) + m_sessionKeyUrlStr;
std::string result;
m_libcurl->HttpGet( fullUrl, result );
if( !IsResultOk( result ) )
{
std::string errMsg = "ERROR - command " + fullUrl + " failed.";
apgHelper::throwRuntimeException( m_fileName, errMsg,
__LINE__, Apg::ErrorType_Critical );
}
if (CameraRegs::TIMER_UPPER == reg)
{
m_lastExposureTimeRegHigh = val;
}
if (CameraRegs::TIMER_LOWER == reg)
{
m_lastExposureTimeRegLow = val;
}
}
////////////////////////////
// GET IMAGE DATA
void AspenEthernetIo::GetImageData(std::vector<uint16_t> & ImageData)
{
const int32_t NumBytesExpected = apgHelper::SizeT2Uint32(ImageData.size())*sizeof(uint16_t);
//grab the data
std::string fullUrl = m_url + "/aspen.bin?keyval=" + m_sessionKey;
std::vector<uint8_t> result;
// set container capacity so it doesn't waste time
// dynamically allocating space
result.reserve( NumBytesExpected );
m_libcurl->setTimeout( 60 + getLastExposureTime() ); // set extended timeout
m_libcurl->HttpGet( fullUrl, result );
m_libcurl->setTimeout( -1 ); // restore default timeout
if( NumBytesExpected != apgHelper::SizeT2Int32( result.size() ) )
{
std::stringstream msg;
msg << fullUrl.c_str() << " error - requested ";
msg << NumBytesExpected << " bytes, but received ";
msg << result.size() << " bytes.";
apgHelper::throwRuntimeException( m_fileName, msg.str() ,
__LINE__, Apg::ErrorType_Critical );
}
//faster than for loop
memcpy(&(*ImageData.begin()), &(*result.begin()), NumBytesExpected);
}
////////////////////////////
// SETUP IMG XFER
void AspenEthernetIo::SetupImgXfer(uint16_t Rows,
uint16_t Cols,
uint16_t NumOfImages, bool IsBulkSeq)
{
std::string fullUrl;
std::stringstream ss;
std::stringstream FrameCount;
if ( IsBulkSeq || NumOfImages == 1)
{
// download all as 1 image
const uint32_t numPixels = Rows * Cols * NumOfImages;
ss << numPixels;
fullUrl = m_url + "/camcmd.cgi?req=Start_Image&imgSize=" +
ss.str() + "¶m=0" + m_sessionKeyUrlStr;
}
else
{
// download as a set of individual images
const uint32_t numPixels = Rows * Cols;
ss << numPixels;
FrameCount << NumOfImages;
fullUrl = m_url + "/camcmd.cgi?req=Start_Image&imgSize=" +
ss.str() + "&frmCount=" + FrameCount.str() +
"¶m=0" + m_sessionKeyUrlStr;
}
std::string result;
m_libcurl->HttpGet( fullUrl, result );
if( !IsResultOk( result ) )
{
std::string errMsg = "ERROR - command " + fullUrl + " failed.";
apgHelper::throwRuntimeException( m_fileName, errMsg,
__LINE__, Apg::ErrorType_Critical );
}
}
////////////////////////////
// CANCEL IMG XFER
void AspenEthernetIo::CancelImgXfer()
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=Stop_Image" +
m_sessionKeyUrlStr;
std::string result;
m_libcurl->HttpGet( fullUrl, result );
if( !IsResultOk( result ) )
{
std::string errMsg = "ERROR - command " + fullUrl + " failed.";
apgHelper::throwRuntimeException( m_fileName, errMsg,
__LINE__, Apg::ErrorType_Critical );
}
}
////////////////////////////
// GET STATUS
void AspenEthernetIo::GetStatus(CameraStatusRegs::BasicStatus & status)
{
CameraStatusRegs::AdvStatus advStatus;
GetStatus( advStatus );
status.CoolerDrive = advStatus.CoolerDrive;
status.DataAvailFlag = advStatus.DataAvailFlag;
status.FetchCount = advStatus.FetchCount;
status.InputVoltage = advStatus.InputVoltage;
status.SequenceCounter = advStatus.SequenceCounter;
status.Status = advStatus.Status;
status.TdiCounter = advStatus.TdiCounter;
status.TempCcd = advStatus.TempCcd;
status.TempHeatSink = advStatus.TempHeatSink;
status.uFrame = advStatus.uFrame;
}
////////////////////////////
// GET STATUS
void AspenEthernetIo::GetStatus(CameraStatusRegs::AdvStatus & status)
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=Get_Status" + m_sessionKeyUrlStr;
std::vector<uint8_t> result;
m_libcurl->HttpGet( fullUrl, result );
status.TempHeatSink = ((result.at(0) << 8) | result.at(1));
status.TempCcd = ((result.at(2) << 8) | result.at(3));
status.CoolerDrive = ((result.at(4) << 8) | result.at(5));
status.InputVoltage = ((result.at(6) << 8) | result.at(7));
status.TdiCounter = ((result.at(8) << 8) | result.at(9));
status.SequenceCounter = ((result.at(10) << 8) | result.at(11));
status.Status = ((result.at(12) << 8) | result.at(13));
status.uFrame = ((result.at(14) << 8) | result.at(15));
status.MostRecentFrame = ((result.at(16) << 8) | result.at(17));
status.ReadyFrame = ((result.at(18) << 8) | result.at(19));
status.CurrentFrame = ((result.at(20) << 8) | result.at(21));
status.FetchCount = ((result.at(22) << 24) | (result.at(23) << 16) | (result.at(24) << 8) | result.at(25));
status.DataAvailFlag = result.at(26);
}
////////////////////////////
// GET MAC ADDRESS
void AspenEthernetIo::GetMacAddress( std::string & Mac )
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=Get_Mac" + m_sessionKeyUrlStr;
m_libcurl->HttpGet( fullUrl, Mac );
}
////////////////////////////
// WRITE SRMD
void AspenEthernetIo::WriteSRMD(const uint16_t reg, const std::vector<uint16_t> & data )
{
std::vector<uint16_t>::const_iterator iter;
for( iter = data.begin(); iter != data.end(); ++iter )
{
WriteReg( reg, *iter );
}
}
////////////////////////////
// WRITE MRMD
void AspenEthernetIo::WriteMRMD(const uint16_t reg, const std::vector<uint16_t> & data )
{
std::vector<uint16_t>::const_iterator iter;
uint16_t offset = 0;
for( iter = data.begin(); iter != data.end(); ++iter, ++offset )
{
WriteReg( reg+offset, *iter );
}
}
////////////////////////////
// GET NETWORK SETTINGS
std::string AspenEthernetIo::GetNetworkSettings()
{
const std::string fullUrl = m_url + "/camcmd.cgi?req=Net_Param_Rd" + m_sessionKeyUrlStr;
std::string info;
m_libcurl->HttpGet( fullUrl, info );
// TODO figure out what to do
// with the pretty formatting. g++ doesn't
// seem to have full regex support
//make the format pretty
// std::regex rx("[\"\\}\\{ ]");
// std::string replacement("");
// info = std::regex_replace(info, rx, replacement);
return info;
}
////////////////////////////
// GET LAST EXPOSURE TIME
unsigned int AspenEthernetIo::getLastExposureTime()
{
uint32_t LastExpTime = ((uint32_t)m_lastExposureTimeRegHigh << 16) | (uint32_t)m_lastExposureTimeRegLow;
return LastExpTime * 0.00000133;
}
////////////////////////////
// GET DRIVER VERSION
std::string AspenEthernetIo::GetDriverVersion()
{
CLibCurlWrap theCurl;
return theCurl.GetVerison();
}
////////////////////////////
// GET FIRMWARE REV
uint16_t AspenEthernetIo::GetFirmwareRev()
{
return ReadReg( CameraRegs::FIRMWARE_REV );
}
////////////////////////////
// GET INFO
std::string AspenEthernetIo::GetInfo()
{
std::stringstream result;
result << "Interface: Ethernet\n";
result << "Camera Firmware: " << GetFirmwareRev() << "\n";
result << GetNetworkSettings().c_str() << "\n";
return result.str();
}
////////////////////////////
// READ STR DATABASE
std::vector<std::string> AspenEthernetIo::ReadStrDatabase()
{
// read the first 1280 bytes (the maximum that can be read from
// the flash via ethernet see if there is anything in the str db
// if so how big is it
std::vector<uint8_t> out;
if( !ReadStrDatabase( 0, MAX_STRDB_READ_BYTES, out ) )
{
std::string errMsg = "Initial read of str db via ethernet failed.";
apgHelper::throwRuntimeException( m_fileName, errMsg,
__LINE__, Apg::ErrorType_Critical );
}
const uint16_t totalSize = out.at(1) << 8 | out.at(0);
if( CamStrDb::MAX_STR_DB_BYTES < totalSize )
{
//log error and return an empty vector
std::stringstream ss;
ss << "Read of Aspen string db returned an in valid size (" << totalSize << " ). ";
apgHelper::LogErrorMsg( __FILE__, ss.str(), __LINE__ );
std::vector<std::string> out;
return out;
}
// now read the full db
std::vector<uint8_t> result( totalSize );
uint32_t NumBytesExpected = totalSize;
uint32_t offset = 0;
while( NumBytesExpected > 0 )
{
uint32_t SizeToRead = std::min<uint32_t>(NumBytesExpected,
MAX_STRDB_READ_BYTES );
std::vector<uint8_t> strDb;
if( !ReadStrDatabase( offset, SizeToRead, strDb ) )
{
std::stringstream ss;
ss << "Read string db failed at offset " << offset << " with a read of ";
ss << SizeToRead << " bytes.";
apgHelper::throwRuntimeException( m_fileName, ss.str(),
__LINE__, Apg::ErrorType_Critical );
}
std::vector<uint8_t>::iterator copyStart = result.begin()+offset;
std::copy( strDb.begin(), strDb.end(), copyStart );
NumBytesExpected -= SizeToRead;
offset += SizeToRead;
}
return CamStrDb::UnpackStrings( result );
}
////////////////////////////
// READ STR DATABASE
bool AspenEthernetIo::ReadStrDatabase( const uint32_t offset, const uint32_t size2Read,
std::vector<uint8_t> & out )
{
std::stringstream offsetSs;
offsetSs << "offset=" << offset;
std::stringstream bytesSs;
bytesSs << "bytes=" << size2Read;
std::string fullUrl = m_url + "/camcmd.cgi?req=get_str_db&" + offsetSs.str() + "&" + bytesSs.str() + m_sessionKeyUrlStr;
m_libcurl->HttpGet( fullUrl, out );
if( 0 == out.size() )
{
apgHelper::LogErrorMsg( __FILE__,"string db cmd return a zero size response", __LINE__ );
return false;
}
//check if ERR 2 is in the response
std::string testStr( reinterpret_cast<const char*>( &out.at(0) ), out.size() );
if( std::string::npos != testStr.find("ERR 2") )
{
std::string errMsg( "Error returned with str db cmd: ");
errMsg.append( testStr );
apgHelper::LogErrorMsg( __FILE__, errMsg, __LINE__ );
return false;
}
return true;
}
|