00001 /// 00002 /// \file m_mode_base.cc 00003 /// Base for mode classes 00004 /// 00005 00006 /* 00007 Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/) 00008 00009 This program is free software; you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation; either version 2 of the License, or 00012 (at your option) any later version. 00013 00014 This program is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00017 00018 See the GNU General Public License in the COPYING file at the 00019 root directory of this project for more details. 00020 */ 00021 00022 #include "m_mode_base.h" 00023 00024 namespace Barry { namespace Mode { 00025 00026 ////////////////////////////////////////////////////////////////////////////// 00027 // Mode base class 00028 00029 Mode::Mode(Controller &con, Controller::ModeType type) 00030 : m_con(con) 00031 , m_modetype(type) 00032 , m_ModeSocket(0) 00033 { 00034 } 00035 00036 Mode::~Mode() 00037 { 00038 } 00039 00040 // 00041 // Open 00042 // 00043 /// Select device mode. This is required before using any other mode-based 00044 /// operations, such as GetDBDB() and LoadDatabase(). 00045 /// 00046 /// This function opens a socket to the device for communicating in Desktop 00047 /// mode. If the device requires it, specify the password with a const char* 00048 /// string in password. The password will not be stored in memory 00049 /// inside this class, only a hash will be generated from it. After 00050 /// using the hash, the hash memory will be set to 0. The application 00051 /// is responsible for safely handling the raw password data. 00052 /// 00053 /// You can retry the password by catching Barry::BadPassword and 00054 /// calling RetryPassword() with the new password. 00055 /// 00056 /// \exception Barry::Error 00057 /// Thrown on protocol error. 00058 /// 00059 /// \exception std::logic_error() 00060 /// Thrown if unsupported mode is requested, or if socket 00061 /// already open. 00062 /// 00063 /// \exception Barry::BadPassword 00064 /// Thrown when password is invalid or if not enough retries 00065 /// left in the device. 00066 /// 00067 void Mode::Open(const char *password) 00068 { 00069 if( m_ModeSocket ) { 00070 m_socket->Close(); 00071 m_socket.reset(); 00072 m_ModeSocket = 0; 00073 } 00074 00075 m_ModeSocket = m_con.SelectMode(m_modetype); 00076 RetryPassword(password); 00077 } 00078 00079 // 00080 // RetryPassword 00081 // 00082 /// Retry a failed password attempt from the first call to Open(). 00083 /// Only call this function in response to Barry::BadPassword exceptions 00084 /// that are thrown from Open(). 00085 /// 00086 /// \exception Barry::Error 00087 /// Thrown on protocol error. 00088 /// 00089 /// \exception std::logic_error() 00090 /// Thrown if in unsupported mode, or if socket already open. 00091 /// 00092 /// \exception Barry::BadPassword 00093 /// Thrown when password is invalid or if not enough retries 00094 /// left in the device. 00095 /// 00096 void Mode::RetryPassword(const char *password) 00097 { 00098 if( m_socket.get() != 0 ) 00099 throw std::logic_error("Socket alreay open in RetryPassword"); 00100 00101 m_socket = m_con.m_zero.Open(m_ModeSocket, password); 00102 00103 // success... perform open-oriented setup 00104 OnOpen(); 00105 } 00106 00107 00108 void Mode::OnOpen() 00109 { 00110 } 00111 00112 00113 }} // namespace Barry::Mode 00114