simplified initialization and cleanup.
parent
b514a55749
commit
2a63bbf13f
|
|
@ -15,14 +15,34 @@ using namespace icsneo;
|
||||||
|
|
||||||
typedef struct icsneo_device_t {
|
typedef struct icsneo_device_t {
|
||||||
std::shared_ptr<Device> device;
|
std::shared_ptr<Device> device;
|
||||||
|
// Received messages from the device, we can automatically free them without the user.
|
||||||
std::vector<std::shared_ptr<icsneo_message_t>> messages;
|
std::vector<std::shared_ptr<icsneo_message_t>> messages;
|
||||||
|
// Seperate buffer for transmit messages for simplicity. User is responsible for freeing.
|
||||||
|
std::vector<std::shared_ptr<icsneo_message_t>> tx_messages;
|
||||||
std::vector<icsneo_event_t> events;
|
std::vector<icsneo_event_t> events;
|
||||||
|
|
||||||
icsneo_open_options_t options;
|
icsneo_open_options_t options;
|
||||||
|
|
||||||
|
icsneo_device_t(std::shared_ptr<Device>& device, icsneo_open_options_t options) : device(device), options(options) {}
|
||||||
|
|
||||||
|
// Take care of cleaning up the device buffers like when closing the device
|
||||||
|
void clean_up() {
|
||||||
|
messages.clear();
|
||||||
|
messages.shrink_to_fit();
|
||||||
|
events.clear();
|
||||||
|
events.shrink_to_fit();
|
||||||
|
tx_messages.clear();
|
||||||
|
tx_messages.shrink_to_fit();
|
||||||
|
}
|
||||||
} icsneo_device_t;
|
} icsneo_device_t;
|
||||||
|
|
||||||
|
// Any new members to this struct should be initialized in icsneo_device_get_messages()
|
||||||
typedef struct icsneo_message_t {
|
typedef struct icsneo_message_t {
|
||||||
std::shared_ptr<Message> message;
|
std::shared_ptr<Message> message;
|
||||||
|
// Indicates this message is a transmit message and should be in the tx_messages vector
|
||||||
|
bool is_tx;
|
||||||
|
|
||||||
|
icsneo_message_t(std::shared_ptr<Message>& message, bool is_tx) : message(message), is_tx(is_tx) {}
|
||||||
} icsneo_message_t;
|
} icsneo_message_t;
|
||||||
|
|
||||||
typedef struct icsneo_event_t {
|
typedef struct icsneo_event_t {
|
||||||
|
|
@ -113,10 +133,9 @@ ICSNEO_API icsneo_error_t icsneo_device_find_all(icsneo_device_t** devices, uint
|
||||||
[&](const auto& device) {
|
[&](const auto& device) {
|
||||||
return device->device == found_device;
|
return device->device == found_device;
|
||||||
})) {
|
})) {
|
||||||
auto device = std::make_shared<icsneo_device_t>();
|
auto default_options = icsneo_open_options_go_online | icsneo_open_options_enable_message_polling | icsneo_open_options_sync_rtc | icsneo_open_options_enable_auto_update;
|
||||||
device->device = found_device;
|
auto device = std::make_shared<icsneo_device_t>(found_device, default_options);
|
||||||
device->options = icsneo_open_options_go_online | icsneo_open_options_enable_message_polling | icsneo_open_options_sync_rtc | icsneo_open_options_enable_auto_update;
|
g_devices.push_back(device);
|
||||||
g_devices.push_back(device);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Determine how many we can return to the caller
|
// Determine how many we can return to the caller
|
||||||
|
|
@ -201,10 +220,7 @@ ICSNEO_API icsneo_error_t icsneo_device_close(icsneo_device_t* device) {
|
||||||
}
|
}
|
||||||
dev->close();
|
dev->close();
|
||||||
// Clear out old messages and events
|
// Clear out old messages and events
|
||||||
device->messages.clear();
|
device->clean_up();
|
||||||
device->messages.shrink_to_fit();
|
|
||||||
device->events.clear();
|
|
||||||
device->events.shrink_to_fit();
|
|
||||||
return icsneo_error_success;
|
return icsneo_error_success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -381,8 +397,7 @@ ICSNEO_API icsneo_error_t icsneo_device_get_messages(icsneo_device_t* device, ic
|
||||||
// Copy the messages into our device message container
|
// Copy the messages into our device message container
|
||||||
device->messages.clear();
|
device->messages.clear();
|
||||||
for (auto& message : queried_messages) {
|
for (auto& message : queried_messages) {
|
||||||
auto message_t = std::make_shared<icsneo_message_t>();
|
auto message_t = std::make_shared<icsneo_message_t>(message, false);
|
||||||
message_t->message = message;
|
|
||||||
device->messages.push_back(message_t);
|
device->messages.push_back(message_t);
|
||||||
}
|
}
|
||||||
device->messages.shrink_to_fit();
|
device->messages.shrink_to_fit();
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ ICSNEO_API icsneo_error_t icsneo_device_open(icsneo_device_t* device);
|
||||||
/** @brief Close a connection to a previously opened device.
|
/** @brief Close a connection to a previously opened device.
|
||||||
*
|
*
|
||||||
* After a successful call to icsneo_device_open(), this function must be called to close the device.
|
* After a successful call to icsneo_device_open(), this function must be called to close the device.
|
||||||
* An already closed device will still succeed.
|
* An already closed device will still succeed. All messages and events related to the device will be freed.
|
||||||
*
|
*
|
||||||
* @param[in] icsneo_device_t device The device to close.
|
* @param[in] icsneo_device_t device The device to close.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue