From 299a06be7c7682eb852b028d2aadf62fcc252d8c Mon Sep 17 00:00:00 2001 From: Will Eccles Date: Tue, 17 Dec 2024 18:36:33 -0500 Subject: [PATCH] feat: add model ID query --- include/bci/abs/CInterface.h | 13 +++++++++++++ include/bci/abs/ScpiClient.h | 8 ++++++++ src/CInterface.cpp | 27 +++++++++++++++++++++++++++ src/ScpiClient_Modeling.cpp | 4 ++++ 4 files changed, 52 insertions(+) diff --git a/include/bci/abs/CInterface.h b/include/bci/abs/CInterface.h index 906a5f4..cb5224b 100644 --- a/include/bci/abs/CInterface.h +++ b/include/bci/abs/CInterface.h @@ -1164,6 +1164,19 @@ int AbsScpiClient_UnloadModel(AbsScpiClientHandle handle); int AbsScpiClient_GetModelInfo(AbsScpiClientHandle handle, AbsModelInfo* model_info_out); +/** + * @brief Query the ID of the currently loaded model. This ID is user-defined + * and is not used by the unit. It is intended for use by tools. + * + * @param[in] handle SCPI client + * @param[out] id_buf buffer to store the null-terminated ID + * @param[in] buf_len length of @a id_buf + * + * @return 0 on success or a negative error code. + */ +int AbsScpiClient_GetModelId(AbsScpiClientHandle handle, char id_buf[], + unsigned int buf_len); + /** * @brief Set a single global model input. Particularly useful with multicast to * address multiple units at once. diff --git a/include/bci/abs/ScpiClient.h b/include/bci/abs/ScpiClient.h index 9d8c49e..bbe4396 100644 --- a/include/bci/abs/ScpiClient.h +++ b/include/bci/abs/ScpiClient.h @@ -1343,6 +1343,14 @@ class ScpiClient { */ Result GetModelInfo() const; + /** + * @brief Query the ID of the currently loaded model. This ID is arbitrary and + * set in the config. It is primarily for tool use. + * + * @return Result containing the model ID or an error code. + */ + Result GetModelId() const; + /** * @brief Set a global model input. * diff --git a/src/CInterface.cpp b/src/CInterface.cpp index abce416..933a18b 100644 --- a/src/CInterface.cpp +++ b/src/CInterface.cpp @@ -747,6 +747,33 @@ int AbsScpiClient_GetModelInfo(AbsScpiClientHandle handle, return static_cast(ec::kUnexpectedException); } +int AbsScpiClient_GetModelId(AbsScpiClientHandle handle, char id_buf[], + unsigned int buf_len) try { + if (!handle || !id_buf || buf_len == 0) { + return static_cast(ec::kInvalidArgument); + } + + std::memset(id_buf, '\0', buf_len); + + if (auto id = GetClient(handle).GetModelId()) { + // must have enough space for the id + null terminator + if (buf_len < id->size() + 1) { + return static_cast(ec::kBufferTooSmall); + } + + id->copy(id_buf, id->size()); + id_buf[id->size()] = '\0'; + } else { + return static_cast(id.error()); + } + + return static_cast(ec::kSuccess); +} catch (const std::bad_alloc&) { + return static_cast(ec::kAllocationFailed); +} catch (...) { + return static_cast(ec::kUnexpectedException); +} + int AbsScpiClient_SetGlobalModelInput(AbsScpiClientHandle handle, unsigned int index, float value) { return WrapSet(&sc::SetGlobalModelInput, handle, index, value); diff --git a/src/ScpiClient_Modeling.cpp b/src/ScpiClient_Modeling.cpp index d3e5e39..daa96d5 100644 --- a/src/ScpiClient_Modeling.cpp +++ b/src/ScpiClient_Modeling.cpp @@ -44,6 +44,10 @@ Result ScpiClient::GetModelInfo() const { return ModelInfo{std::move(res->at(0)), std::move(res->at(1))}; } +Result ScpiClient::GetModelId() const { + return SendAndRecv("MOD:ID?\r\n").and_then(scpi::ParseStringResponse); +} + ErrorCode ScpiClient::SetGlobalModelInput(unsigned int index, float value) const { if (index >= kGlobalModelInputCount) {