diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2016-07-23 17:18:20 +0200 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2016-08-01 13:54:57 +0200 |
commit | 93803ac591d0e0e0d05d5a552cd2d5fb4a62fb19 (patch) | |
tree | 65c4f57618fd078d2a5de084deb6bac5eec0594b /NK_C_API.cc | |
parent | 1b03ab0377bfd106225e16bbd2baee3048b266b4 (diff) | |
download | libnitrokey-93803ac591d0e0e0d05d5a552cd2d5fb4a62fb19.tar.gz libnitrokey-93803ac591d0e0e0d05d5a552cd2d5fb4a62fb19.tar.bz2 |
Handle errors with exceptions
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
Diffstat (limited to 'NK_C_API.cc')
-rw-r--r-- | NK_C_API.cc | 120 |
1 files changed, 106 insertions, 14 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc index 1695c92..440e824 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -3,41 +3,98 @@ using namespace nitrokey; +static uint8_t NK_last_command_status = 0; + extern "C" { +extern uint8_t NK_get_last_command_status(){ + auto _copy = NK_last_command_status; + NK_last_command_status = 0; + return _copy; +} + extern int NK_login(const char *pin, const char *temporary_password) { auto m = NitrokeyManager::instance(); - return m->connect() && m->authorize(pin, temporary_password); + try { + m->connect(); + m->authorize(pin, temporary_password); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } extern int NK_logout() { auto m = NitrokeyManager::instance(); - return m->disconnect(); + try { + m->disconnect(); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } + extern const char * NK_status() { auto m = NitrokeyManager::instance(); - string s = m->get_status(); - return strdup(s.c_str()); + try { + string s = m->get_status(); + return strdup(s.c_str()); //FIXME leak? + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + return ""; } extern uint32_t NK_get_hotp_code(uint8_t slot_number){ auto m = NitrokeyManager::instance(); - return m->get_HOTP_code(slot_number); + try { + return m->get_HOTP_code(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + return 0; } extern uint32_t NK_get_totp_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time, uint8_t last_interval){ auto m = NitrokeyManager::instance(); - return m->get_TOTP_code(slot_number, challenge, last_totp_time, last_interval); + try { + return m->get_TOTP_code(slot_number, challenge, last_totp_time, last_interval); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + return 0; } extern int NK_erase_hotp_slot(uint8_t slot_number) { auto m = NitrokeyManager::instance(); - return m->erase_hotp_slot(slot_number); + try { + m->erase_hotp_slot(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } + extern int NK_erase_totp_slot(uint8_t slot_number) { auto m = NitrokeyManager::instance(); - return m->erase_totp_slot(slot_number); + try { + m->erase_totp_slot(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } extern int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint8_t hotp_counter, @@ -47,6 +104,7 @@ extern int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const m->write_HOTP_slot(slot_number, slot_name, secret, hotp_counter, temporary_password); } catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; return commandFailedException.last_command_status; } return 0; @@ -55,16 +113,35 @@ extern int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const extern int NK_write_totp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint16_t time_window, bool use_8_digits, const char *temporary_password) { auto m = NitrokeyManager::instance(); - return m->write_TOTP_slot(slot_number, slot_name, secret, time_window, use_8_digits, temporary_password); + try { + m->write_TOTP_slot(slot_number, slot_name, secret, time_window, use_8_digits, temporary_password); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } extern const char* NK_get_totp_slot_name(uint8_t slot_number){ auto m = NitrokeyManager::instance(); - return m->get_totp_slot_name(slot_number); + try { + return m->get_totp_slot_name(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return ""; + } } extern const char* NK_get_hotp_slot_name(uint8_t slot_number){ auto m = NitrokeyManager::instance(); - return m->get_hotp_slot_name(slot_number); + try { + return m->get_hotp_slot_name(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return ""; + } } extern void NK_set_debug(bool state){ @@ -74,11 +151,26 @@ extern void NK_set_debug(bool state){ extern int NK_totp_set_time(uint64_t time){ auto m = NitrokeyManager::instance(); - return m->set_time(time); + try { + m->set_time(time); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } -extern int NK_totp_mark_time(){ + +extern int NK_totp_get_time(){ auto m = NitrokeyManager::instance(); - return m->get_time(); + try { + m->get_time(); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } }
\ No newline at end of file |