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 23 #ifndef LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H 24 #define LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H 25 26 #include <atomic> 27 #include <exception> 28 #include <stdexcept> 29 #include <string> 30 31 32 class DeviceCommunicationException: public std::runtime_error 33 { 34 std::string message; 35 static std::atomic_int occurred; 36 public: DeviceCommunicationException(std::string _msg)37 DeviceCommunicationException(std::string _msg): std::runtime_error(_msg), message(_msg){ 38 ++occurred; 39 } getType()40 uint8_t getType() const {return 1;}; 41 // virtual const char* what() const throw() override { 42 // return message.c_str(); 43 // } has_occurred()44 static bool has_occurred(){ return occurred > 0; }; reset_occurred_flag()45 static void reset_occurred_flag(){ occurred = 0; }; 46 }; 47 48 class DeviceNotConnected: public DeviceCommunicationException { 49 public: DeviceNotConnected(std::string msg)50 DeviceNotConnected(std::string msg) : DeviceCommunicationException(msg){} getType()51 uint8_t getType() const {return 2;}; 52 }; 53 54 class DeviceSendingFailure: public DeviceCommunicationException { 55 public: DeviceSendingFailure(std::string msg)56 DeviceSendingFailure(std::string msg) : DeviceCommunicationException(msg){} getType()57 uint8_t getType() const {return 3;}; 58 }; 59 60 class DeviceReceivingFailure: public DeviceCommunicationException { 61 public: DeviceReceivingFailure(std::string msg)62 DeviceReceivingFailure(std::string msg) : DeviceCommunicationException(msg){} getType()63 uint8_t getType() const {return 4;}; 64 }; 65 66 class InvalidCRCReceived: public DeviceReceivingFailure { 67 public: InvalidCRCReceived(std::string msg)68 InvalidCRCReceived(std::string msg) : DeviceReceivingFailure(msg){} getType()69 uint8_t getType() const {return 5;}; 70 }; 71 72 73 #endif //LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H 74