diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2019-01-15 16:02:31 +0100 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2019-01-15 16:02:31 +0100 |
commit | e853275784e48823be0f39f0855efbe85c0f905c (patch) | |
tree | 2b428a23bddbd87c08ee47351bc31768f54b63fe /NK_C_API.cc | |
parent | 3f3fdebbc795dc3805cd5be105ce994286598f16 (diff) | |
parent | cd1cfdbfc4113186f80dbadf5eb76543b22e34bd (diff) | |
download | libnitrokey-e853275784e48823be0f39f0855efbe85c0f905c.tar.gz libnitrokey-e853275784e48823be0f39f0855efbe85c0f905c.tar.bz2 |
Merge branch 'pr_138' into contributions
Improve support for multiple devices
Fixes #138
Diffstat (limited to 'NK_C_API.cc')
-rw-r--r-- | NK_C_API.cc | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc index a780acf..794673a 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -753,6 +753,66 @@ NK_C_API char* NK_get_SD_usage_data_as_string() { }); } + bool copy_device_info(const DeviceInfo& source, NK_device_info* target) { + switch (source.m_deviceModel) { + case DeviceModel::PRO: + target->model = NK_PRO; + break; + case DeviceModel::STORAGE: + target->model = NK_STORAGE; + break; + default: + return false; + } + + target->path = strndup(source.m_path.c_str(), MAXIMUM_STR_REPLY_LENGTH); + target->serial_number = strndup(source.m_serialNumber.c_str(), MAXIMUM_STR_REPLY_LENGTH); + target->next = nullptr; + + return target->path && target->serial_number; + } + + NK_C_API struct NK_device_info* NK_list_devices() { + auto nm = NitrokeyManager::instance(); + return get_with_result([&]() -> NK_device_info* { + auto v = nm->list_devices(); + if (v.empty()) + return nullptr; + + auto result = new NK_device_info(); + auto ptr = result; + auto first = v.begin(); + if (!copy_device_info(*first, ptr)) { + NK_free_device_info(result); + return nullptr; + } + v.erase(first); + + for (auto& info : v) { + ptr->next = new NK_device_info(); + ptr = ptr->next; + + if (!copy_device_info(info, ptr)) { + NK_free_device_info(result); + return nullptr; + } + } + return result; + }); + } + + NK_C_API void NK_free_device_info(struct NK_device_info* device_info) { + if (!device_info) + return; + + if (device_info->next) + NK_free_device_info(device_info->next); + + free(device_info->path); + free(device_info->serial_number); + delete device_info; + } + NK_C_API int NK_connect_with_ID(const char* id) { auto m = NitrokeyManager::instance(); return get_with_result([&]() { @@ -760,6 +820,14 @@ NK_C_API char* NK_get_SD_usage_data_as_string() { }); } + NK_C_API int NK_connect_with_path(const char* path) { + auto m = NitrokeyManager::instance(); + return get_with_result([&]() { + return m->connect_with_path(path) ? 1 : 0; + }); + } + + NK_C_API int NK_wink() { auto m = NitrokeyManager::instance(); return get_without_result([&]() { |