diff options
author | Robin Krahl <me@robin-krahl.de> | 2019-01-13 12:05:37 +0100 |
---|---|---|
committer | Robin Krahl <me@robin-krahl.de> | 2019-01-13 13:27:38 +0100 |
commit | 5d94dece0392ce0d5486097abf8918b6922f85d2 (patch) | |
tree | d860908fa09d46aa26987e5674139d57b271ecba | |
parent | a80378e0c770a503ddaafc0c7aacb78cac667b8f (diff) | |
download | libnitrokey-5d94dece0392ce0d5486097abf8918b6922f85d2.tar.gz libnitrokey-5d94dece0392ce0d5486097abf8918b6922f85d2.tar.bz2 |
Add NK_device_info, NK_list_devices and NK_free_device_info
NK_list_devices corresponds to NitrokeyManager::list_devices. It
returns a linked list of NK_device_info, which has to be freed using the
NK_free_device_info function.
-rw-r--r-- | NK_C_API.cc | 60 | ||||
-rw-r--r-- | NK_C_API.h | 36 |
2 files changed, 96 insertions, 0 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc index f0c23a1..05457d0 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -736,6 +736,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([&]() { @@ -57,6 +57,29 @@ extern "C" { NK_STORAGE = 2 }; + /** + * The connection info for a Nitrokey device as a linked list. + */ + struct NK_device_info { + /** + * The model of the Nitrokey device. + */ + enum NK_device_model model; + /** + * The USB device path for NK_connect_with_path. + */ + char* path; + /** + * The serial number. + */ + char* serial_number; + /** + * The pointer to the next element of the linked list or null + * if this is the last element in the list. + */ + struct NK_device_info* next; + }; + /** * Stores the status of a Storage device. */ @@ -768,6 +791,19 @@ extern "C" { */ NK_C_API char* NK_list_devices_by_cpuID(); + /** + * Returns a linked list of all connected devices, or null if no devices + * are connected or an error occured. The linked list must be freed by + * calling NK_free_device_info. + * @return a linked list of all connected devices + */ + NK_C_API struct NK_device_info* NK_list_devices(); + + /** + * Free a linked list returned by NK_list_devices. + * @param the linked list to free or null + */ + NK_C_API void NK_free_device_info(struct NK_device_info* device_info); /** * Connects to the device with given ID. ID's list could be created with NK_list_devices_by_cpuID. |