-
Notifications
You must be signed in to change notification settings - Fork 14
✨ QDMI Environment Implementation #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 39 commits
0887df6
302c416
3aab49e
52bd8d7
49cc0bf
3ba8a92
6f658f6
25abdd4
98971ba
98a8659
c2b2935
f88b60c
c1591fe
4e7b8f8
bd849ae
3858563
3929d0a
16afe77
9bbc775
7b3e8df
ba5a676
e67355f
67312db
94e4b25
20a2841
e239964
7474cbe
83a6b18
c71b2c3
d0db550
0e9bcb2
d2218d5
889b27c
a83cbf4
709ac33
4572c14
123d2e6
f562d03
64ea415
6df7dee
e3e6694
04a8218
0bda557
56ba00d
aff6f1e
10710d0
e305c46
9f38ad9
248a112
aba1df1
80cd577
ab49391
3234155
2f2e03e
4ef85b5
0202474
34718bc
5a121c5
f8eb819
d2f3321
8c747b3
3a3576d
35dc853
c7ffdc3
e3467e6
b4c416d
d61e4e7
abe1b30
891abf3
abbbbd5
c7432aa
ecc8fcd
b88ff2f
72cf9eb
ddd73a1
80628dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,10 +26,14 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <chrono> | ||
| #include <cmath> | ||
| #include <complex> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <ctime> | ||
| #include <functional> | ||
| #include <iterator> | ||
| #include <limits> | ||
|
|
@@ -77,6 +81,24 @@ struct CXX_QDMI_Operation_impl_d { | |
| std::string name; | ||
| }; | ||
|
|
||
| struct CXX_QDMI_EnvironmentSensor_impl_d { | ||
| std::string id; | ||
| std::string unit; | ||
| std::chrono::duration<int> sampling_rate{}; // in seconds | ||
| }; | ||
|
|
||
| struct CXX_QDMI_Device_EnvironmentSensor_Query_impl_d { | ||
| std::chrono::time_point<std::chrono::system_clock> start_time; | ||
| std::chrono::time_point<std::chrono::system_clock> end_time; | ||
| size_t timeout{}; | ||
| CXX_QDMI_EnvironmentSensor environment_sensor{}; | ||
| std::vector<std::chrono::time_point<std::chrono::system_clock>> | ||
| result_timestamps; | ||
| std::vector<float> result_values; | ||
|
||
| size_t result_length{}; | ||
| QDMI_EnvironmentSensor_Query_Status status{}; | ||
| }; | ||
|
|
||
| namespace { | ||
| /** | ||
| * @brief Static function to maintain the device state. | ||
|
|
@@ -159,6 +181,12 @@ constexpr CXX_QDMI_Site_impl_d SITE4{4}; | |
| constexpr std::array<const CXX_QDMI_Site_impl_d *, 5> CXX_DEVICE_SITES = { | ||
| &SITE0, &SITE1, &SITE2, &SITE3, &SITE4}; | ||
|
|
||
| const CXX_QDMI_EnvironmentSensor_impl_d ENVSEN{"t4k", "K", | ||
| std::chrono::duration<int>{60}}; | ||
|
|
||
| constexpr std::array<const CXX_QDMI_EnvironmentSensor_impl_d *, 1> | ||
| CXX_DEVICE_ENVIRONMENTSENSORS{&ENVSEN}; | ||
|
|
||
| constexpr std::array<const CXX_QDMI_Site_impl_d *, 20> | ||
| // clang-format off | ||
| DEVICE_COUPLING_MAP = { | ||
|
|
@@ -773,6 +801,9 @@ int CXX_QDMI_device_session_query_device_property( | |
| ADD_SINGLE_VALUE_PROPERTY(QDMI_DEVICE_PROPERTY_NEEDSCALIBRATION, size_t, 0, | ||
| prop, size, value, size_ret) | ||
|
|
||
| ADD_LIST_PROPERTY(QDMI_DEVICE_PROPERTY_ENVIRONMENTSENSORS, | ||
| CXX_QDMI_EnvironmentSensor, CXX_DEVICE_ENVIRONMENTSENSORS, | ||
| prop, size, value, size_ret) | ||
| ADD_SINGLE_VALUE_PROPERTY( | ||
| QDMI_DEVICE_PROPERTY_PULSESUPPORT, QDMI_Device_Pulse_Support_Level, | ||
| QDMI_DEVICE_PULSE_SUPPORT_LEVEL_NONE, prop, size, value, size_ret) | ||
|
|
@@ -872,3 +903,227 @@ int CXX_QDMI_device_session_query_operation_property( | |
| } | ||
| return QDMI_ERROR_NOTSUPPORTED; | ||
| } /// [DOXYGEN FUNCTION END] | ||
|
|
||
| int CXX_QDMI_device_session_query_environmentsensor_property( | ||
| CXX_QDMI_Device_Session session, | ||
| CXX_QDMI_EnvironmentSensor environment_sensor, | ||
| QDMI_EnvironmentSensor_Property prop, size_t size, void *value, | ||
| size_t *size_ret) { | ||
| if (session == nullptr || environment_sensor == nullptr || | ||
| (value != nullptr && size == 0) || | ||
| (prop >= QDMI_ENVIRONMENTSENSOR_PROPERTY_MAX && | ||
| prop != QDMI_ENVIRONMENTSENSOR_PROPERTY_CUSTOM1 && | ||
| prop != QDMI_ENVIRONMENTSENSOR_PROPERTY_CUSTOM2 && | ||
| prop != QDMI_ENVIRONMENTSENSOR_PROPERTY_CUSTOM3 && | ||
| prop != QDMI_ENVIRONMENTSENSOR_PROPERTY_CUSTOM4 && | ||
| prop != QDMI_ENVIRONMENTSENSOR_PROPERTY_CUSTOM5)) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
| ADD_STRING_PROPERTY(QDMI_ENVIRONMENTSENSOR_PROPERTY_ID, | ||
| environment_sensor->id.c_str(), prop, size, value, | ||
| size_ret) | ||
| ADD_STRING_PROPERTY(QDMI_ENVIRONMENTSENSOR_PROPERTY_UNIT, | ||
| environment_sensor->unit.c_str(), prop, size, value, | ||
| size_ret) | ||
| ADD_SINGLE_VALUE_PROPERTY(QDMI_ENVIRONMENTSENSOR_PROPERTY_SAMPLINGRATE, int, | ||
| environment_sensor->sampling_rate.count(), prop, | ||
| size, value, size_ret) | ||
| return QDMI_ERROR_NOTSUPPORTED; | ||
| } | ||
|
|
||
| int CXX_QDMI_device_session_create_environmentsensor_query( | ||
| CXX_QDMI_Device_Session session, | ||
| CXX_QDMI_Device_EnvironmentSensor_Query *query) { | ||
|
|
||
| if (session == nullptr || query == nullptr) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
| if (session->status != CXX_QDMI_DEVICE_SESSION_STATUS::INITIALIZED) { | ||
| return QDMI_ERROR_BADSTATE; | ||
| } | ||
| *query = new CXX_QDMI_Device_EnvironmentSensor_Query_impl_d(); | ||
| (*query)->environment_sensor = new CXX_QDMI_EnvironmentSensor_impl_d(); | ||
| (*query)->start_time = std::chrono::system_clock::now(); | ||
| (*query)->end_time = std::chrono::system_clock::now(); | ||
|
|
||
| return QDMI_SUCCESS; | ||
| } | ||
|
|
||
| int CXX_QDMI_device_environmentsensor_query_set_parameter( | ||
| CXX_QDMI_Device_EnvironmentSensor_Query query, | ||
| QDMI_Device_EnvironmentSensor_Query_Parameter param, size_t size, | ||
| const void *value) { | ||
|
|
||
| if (query == nullptr || (value != nullptr && size == 0) || | ||
| (param >= QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_MAX && | ||
| param != QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_CUSTOM1 && | ||
| param != QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_CUSTOM2 && | ||
| param != QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_CUSTOM3 && | ||
| param != QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_CUSTOM4 && | ||
| param != QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_CUSTOM5) || | ||
| value == nullptr) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
| switch (param) { | ||
|
|
||
| case QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_STARTTIME: { | ||
| query->start_time = std::chrono::system_clock::from_time_t( | ||
| *static_cast<const time_t *>(value)); | ||
| return QDMI_SUCCESS; | ||
| } | ||
| case QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_ENDTIME: { | ||
| query->end_time = std::chrono::system_clock::from_time_t( | ||
| *static_cast<const time_t *>(value)); | ||
| return QDMI_SUCCESS; | ||
| } | ||
|
|
||
| case QDMI_DEVICE_ENVIRONMENTSENSOR_QUERY_PARAMETER_ENVIRONMENTSENSOR: { | ||
| query->environment_sensor = | ||
| *(static_cast<const CXX_QDMI_EnvironmentSensor *>(value)); | ||
| return QDMI_SUCCESS; | ||
| } | ||
| default: | ||
| return QDMI_ERROR_NOTSUPPORTED; | ||
| } | ||
| } | ||
|
|
||
| int CXX_QDMI_device_environmentsensor_query_submit( | ||
| CXX_QDMI_Device_EnvironmentSensor_Query query) { | ||
|
|
||
| if (query == nullptr || query->environment_sensor == nullptr) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
|
|
||
| // here, the actual submission. | ||
|
|
||
| // for demonstration purposes | ||
| auto time_difference = | ||
| std::chrono::round<std::chrono::seconds>( // NOLINT(misc-include-cleaner) | ||
| query->end_time - query->start_time); | ||
|
|
||
| auto sampling_rate = query->environment_sensor->sampling_rate; | ||
|
|
||
| auto result_length = | ||
| static_cast<size_t>(time_difference.count() / sampling_rate.count()); | ||
|
|
||
| query->result_timestamps.clear(); | ||
| query->result_timestamps.reserve(result_length); | ||
|
|
||
| query->result_values.clear(); | ||
| query->result_values.reserve(result_length); | ||
|
|
||
| query->result_length = result_length; | ||
|
||
|
|
||
| for (unsigned int i = 0; i < result_length; i++) { | ||
| auto duration_to_add = | ||
| std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
| query->environment_sensor->sampling_rate * i); | ||
| auto next_time = query->start_time + duration_to_add; | ||
| query->result_timestamps.emplace_back(next_time); | ||
| query->result_values[i] = | ||
| static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / 100)); | ||
|
||
| } | ||
|
|
||
| return QDMI_SUCCESS; | ||
| } | ||
|
|
||
| int CXX_QDMI_device_environmentsensor_query_get_results( | ||
| CXX_QDMI_Device_EnvironmentSensor_Query query, | ||
| QDMI_EnvironmentSensor_Query_Result result, size_t size, void *data, | ||
| size_t *size_ret) { | ||
|
|
||
| if (query == nullptr || (data != nullptr && size == 0) || | ||
| (result >= QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_MAX && | ||
| result != QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_CUSTOM1 && | ||
| result != QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_CUSTOM2 && | ||
| result != QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_CUSTOM3 && | ||
| result != QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_CUSTOM4 && | ||
| result != QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_CUSTOM5)) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
| size_t req_size = query->result_length; | ||
|
||
| switch (result) { | ||
| case QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_TIMESTAMPS: | ||
| req_size *= sizeof(time_t); | ||
| if (data != nullptr) { | ||
| if (size < req_size) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
| auto *data_ptr = static_cast<time_t *>(data); | ||
| for (size_t i = 0; i < query->result_length; i++) { | ||
| data_ptr[i] = | ||
| std::chrono::system_clock::to_time_t(query->result_timestamps[i]); | ||
| } | ||
| } | ||
|
|
||
| if ((size_ret) != nullptr) { | ||
| *(size_ret) = req_size; | ||
| } | ||
| return QDMI_SUCCESS; | ||
| case QDMI_ENVIRONMENTSENSOR_QUERY_RESULT_VALUES: | ||
|
|
||
| req_size *= sizeof(float); | ||
| if (data != nullptr) { | ||
| if (size < req_size) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
| memcpy(data, query->result_values.data(), req_size); | ||
| } | ||
| if ((size_ret) != nullptr) { | ||
| *(size_ret) = req_size; | ||
| } | ||
| return QDMI_SUCCESS; | ||
|
|
||
| default: | ||
| return QDMI_ERROR_NOTSUPPORTED; | ||
| } | ||
|
|
||
| return QDMI_SUCCESS; | ||
| } | ||
|
|
||
| int CXX_QDMI_device_environmentsensor_query_check_status( | ||
| CXX_QDMI_Device_EnvironmentSensor_Query query, | ||
| QDMI_EnvironmentSensor_Query_Status *status) { | ||
| if (query == nullptr || status == nullptr) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
|
|
||
| // randomly decide whether job is done or not | ||
| if (query->status == QDMI_ENVIRONMENTSENSOR_QUERY_STATUS_RUNNING && | ||
| CXX_QDMI_generate_bit()) { | ||
| query->status = QDMI_ENVIRONMENTSENSOR_QUERY_STATUS_DONE; | ||
| } | ||
|
|
||
| *status = query->status; | ||
| return QDMI_SUCCESS; | ||
| } | ||
|
|
||
| int CXX_QDMI_device_environmentsensor_query_wait( | ||
| CXX_QDMI_Device_EnvironmentSensor_Query query, size_t timeout) { | ||
|
|
||
| if (query == nullptr) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
|
|
||
| query->status = QDMI_ENVIRONMENTSENSOR_QUERY_STATUS_DONE; | ||
| query->timeout = timeout; | ||
| return QDMI_SUCCESS; | ||
| } | ||
|
|
||
| int CXX_QDMI_device_environmentsensor_query_cancel( | ||
| CXX_QDMI_Device_EnvironmentSensor_Query query) { | ||
|
|
||
| if (query == nullptr || | ||
| query->status == QDMI_ENVIRONMENTSENSOR_QUERY_STATUS_DONE) { | ||
| return QDMI_ERROR_INVALIDARGUMENT; | ||
| } | ||
|
|
||
| query->status = QDMI_ENVIRONMENTSENSOR_QUERY_STATUS_CANCELED; | ||
|
|
||
| return QDMI_SUCCESS; | ||
| } | ||
|
|
||
| void CXX_QDMI_device_environmentsensor_query_free( | ||
| CXX_QDMI_Device_EnvironmentSensor_Query query) { | ||
| delete query; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could that be seconds, i.e.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and
size_t(same asstd::chrono::seconds) does not need a{}initialiser.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it cannot. However, we can
std::chrono::secondsinstead ofsize_t, leading to additional casting in theCXX_QDMI_device_telemetrysensor_query_wait. If you suggest that it will improve the readability of the code, we can change it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Done