📄 cmdbaseddevicerecovery.cpp
字号:
/* -*- c++ -*- --------------------------------------------------------------- Copyright (C) 2005, SWECO, All Rights Reserved. Implementation of CmdBasedDeviceRecovery class Author: Zsolt Molnar (Zsolt.Molnar@ieee.org) ---------------------------------------------------------------------------*/#include <fstream>#include <string>#include <memory>#include <stdlib.h>#include <iostream>#include "AppFactory.hh"#include "FileReader.hh"#include "IDeviceTypeExtractor.hh"#include "CmdBasedDeviceRecovery.hh"namespace { const char rcs_id[] = "$Id$"; }CmdBasedDeviceRecovery::CmdBasedDeviceRecovery(IDeviceManager& aDevManager) : DeviceRecoveryLogic(aDevManager){ // EMPTY}CmdBasedDeviceRecovery::_tDeviceSet CmdBasedDeviceRecovery::_inquiryForDevices(){ _tDeviceSet foundDevices; // Execute the scanning command system(HCITOOL_PATH " " HCITOOL_SCAN "> " HCITOOL_RESULT); // Parse the scan result file FileReader hcires(HCITOOL_RESULT); // Ignore the first "Inquiring..." line hcires.nextLine(); while(!hcires.isEnd()) { string line = hcires.nextLine(); // Get the device data if the actual line is valid if (line.size() > HCITOOL_IDEND) { Device device(_getDeviceID(line), _getDeviceType(line)); foundDevices.insert(device); cout << "CmdBasedDeviceRecovery: device " << device.devID << " is here." << endl; } } hcires.remove();#ifdef __TEST__ // If testing, terminate the main loop in order to be able to proceed // the next test item _isLooping = false;#endif // #ifdef __TEST__ return foundDevices;}Device::tDeviceID CmdBasedDeviceRecovery::_getDeviceID(const string& aScanLine){ return aScanLine.substr(HCITOOL_IDSTART, HCITOOL_IDEND);}Device::tDeviceType CmdBasedDeviceRecovery::_getDeviceType(const string& aScanLine){ size_t firstChar = aScanLine.rfind("0x"); // If there is no "0x" then it must be an unknown error. Safest is to // return with unknown device... if (firstChar == string::npos) { return Device::UNKNOWN; } // 8 is the length of the decice class id const size_t idsize = 8; string devTypeStr = aScanLine.substr(firstChar, idsize); // If there string length is not 8 then it must be an unknown error. // Safest is to return with unknown device... if (devTypeStr.size() != idsize) { return Device::UNKNOWN; } auto_ptr<IDeviceTypeExtractor> dte(AppFactory::newDeviceTypeExtractorC()); return dte->getDeviceType(devTypeStr);}#ifdef __TEST__#include <assert.h>#include <iostream>#include <time.h>void CmdBasedDeviceRecovery::_scanSleep(){ _isLooping = true; const int maxSleepTime = 40; static time_t lastScan = time(NULL); time_t actScan = time(NULL); time_t diff = actScan - lastScan; if (diff <= maxSleepTime) { cout << "Waiting for scan buffer ready for " << maxSleepTime - diff << " seconds... " << flush; sleep(maxSleepTime - diff); cout << "OK" << endl << endl << flush; } lastScan = time(NULL);}boolCmdBasedDeviceRecovery::test(){ string input; Device dev_0("", Device::OTHER); Device dev_1("00:0E:6D:32:0D:FF", Device::NOKIA6600); Device dev_2("00:60:57:A3:DF:28", Device::NOKIA6600); cout << endl; cout << "Scanning for the devices not participating in the testing" << endl; cout << "All the test device bluetooth must be switched off" << endl; cout << "Press enter if you are ready." << endl; getchar(); start(); cout << _presentLastTime.size() << " devices are present." << endl << endl; // 1 Nokia 6600 device appears cout << "Switch on Nokia 6600 (ID: " << dev_1.devID << ") bluetooth " << "and press enter"; getchar(); _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 1); cout << "The device is recognized. Wait..." << endl << endl; // no new device _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 1); // The device goes away cout << "Switch off Nokia 6600 (ID: " << dev_1.devID << ") bluetooth " << "and press enter"; getchar(); _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 0); cout << "The device is away." << endl << endl; // 2 Nokia 6600 device appears cout << "Switch on Nokia 6600 (ID: " << dev_1.devID << ") and " << "Nokia 6600 (ID: " << dev_2.devID << ") bluetooth on " << "and press enter"; getchar(); _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 1); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_2) == 1); cout << "The devices are recognized. Wait..." << endl << endl; // no new device _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 1); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_2) == 1); // 1 device leaves cout << "Switch off Nokia 6600 (ID: " << dev_2.devID << ") bluetooth " << "and press enter"; getchar(); _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 1); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_2) == 0); cout << "The device is away. Wait..." << endl << endl; // no new device _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 1); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_2) == 0); // 1 device leaves cout << "Switch off all the Nokia 6600 bluetooth " << "and press enter"; getchar(); _scanSleep(); start(); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_1) == 0); assert(count(_presentLastTime.begin(), _presentLastTime.end(), dev_2) == 0); cout << "CmdBasedDeviceRecovery test passed" << endl; return true;}#endif // #ifdef __TEST__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -