1 /*
2  * Copyright (c) 2015-2018 Nitrokey UG
3  *
4  * This file is part of libnitrokey.
5  *
6  * libnitrokey is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * libnitrokey is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with libnitrokey. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * SPDX-License-Identifier: LGPL-3.0
20  */
21 
22 #ifndef LIBNITROKEY_COMMANDFAILEDEXCEPTION_H
23 #define LIBNITROKEY_COMMANDFAILEDEXCEPTION_H
24 
25 #include <exception>
26 #include <cstdint>
27 #include "log.h"
28 #include "command_id.h"
29 
30 using cs = nitrokey::proto::stick10::command_status;
31 using cs2 = nitrokey::proto::stick20::device_status;
32 
33 class CommandFailedException : public std::exception {
34 public:
35     const uint8_t last_command_id;
36     const uint8_t last_command_status;
37 
CommandFailedException(uint8_t last_command_id,uint8_t last_command_status)38     CommandFailedException(uint8_t last_command_id, uint8_t last_command_status) :
39         last_command_id(last_command_id),
40             last_command_status(last_command_status){
41       LOG(std::string("CommandFailedException, status: ")+ std::to_string(last_command_status), nitrokey::log::Loglevel::DEBUG);
42     }
43 
what()44     virtual const char *what() const throw() {
45         return "Command execution has failed on device";
46     }
47 
48 
reason_timestamp_warning()49     bool reason_timestamp_warning() const throw(){
50       return last_command_status == static_cast<uint8_t>(cs::timestamp_warning);
51     }
52 
reason_AES_not_initialized()53     bool reason_AES_not_initialized() const throw(){
54       return last_command_status == static_cast<uint8_t>(cs::AES_dec_failed);
55     }
56 
reason_not_authorized()57     bool reason_not_authorized() const throw(){
58       return last_command_status == static_cast<uint8_t>(cs::not_authorized);
59     }
60 
reason_slot_not_programmed()61     bool reason_slot_not_programmed() const throw(){
62       return last_command_status == static_cast<uint8_t>(cs::slot_not_programmed);
63     }
64 
reason_wrong_password()65     bool reason_wrong_password() const throw(){
66       return last_command_status == static_cast<uint8_t>(cs::wrong_password);
67     }
68 
reason_smartcard_busy()69     bool reason_smartcard_busy() const throw(){
70       return last_command_status == static_cast<uint8_t>(cs2::smartcard_error);
71     }
72 
73 };
74 
75 
76 #endif //LIBNITROKEY_COMMANDFAILEDEXCEPTION_H
77