From 6c6361bd6a1f63ec4301d0c810e6f13842443732 Mon Sep 17 00:00:00 2001 From: Teo Perisanu Date: Mon, 23 Aug 2021 18:00:28 +0300 Subject: [PATCH] GenericAnalogIn: Check if 'sampling_frequency' is a channel attribute. An iio device samples using only one samplerate. Sometimes the 'sampling_frequency' attribute is located inside all channels, these sharing it. Signed-off-by: Teo Perisanu --- src/analog/generic/genericanalogin_impl.cpp | 8 +++++++- src/utils/channel.cpp | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/analog/generic/genericanalogin_impl.cpp b/src/analog/generic/genericanalogin_impl.cpp index 621ca462..cc182c12 100644 --- a/src/analog/generic/genericanalogin_impl.cpp +++ b/src/analog/generic/genericanalogin_impl.cpp @@ -23,6 +23,7 @@ #include "genericanalogin_impl.hpp" #include #include "libm2k/utils/utils.hpp" +#include "utils/channel.hpp" #include #include #include @@ -91,7 +92,12 @@ std::vector GenericAnalogInImpl::getAvailableSampleRates() std::vector stringValues; std::vector values; - stringValues = getAdcDevice(0)->getAvailableAttributeValues("sampling_frequency"); + if (getAdcDevice(0)->hasGlobalAttribute("sampling_frequency")) { + stringValues = getAdcDevice(0)->getAvailableAttributeValues("sampling_frequency"); + } else { + stringValues = getAdcDevice(0)->getChannel(0, false)->getAvailableAttributeValues("sampling_frequency"); + } + std::transform(stringValues.begin(), stringValues.end(), std::back_inserter(values), [] (std::string &s) -> double { try { diff --git a/src/utils/channel.cpp b/src/utils/channel.cpp index 8bbdc845..514f46f4 100644 --- a/src/utils/channel.cpp +++ b/src/utils/channel.cpp @@ -378,6 +378,11 @@ std::vector Channel::getAvailableAttributeValues(const std::string std::vector values; std::string valuesAsString; + if (!hasAttribute(attr)) { + THROW_M2K_EXCEPTION(std::string(m_channel_id) + " has no " + attr + " attribute", libm2k::EXC_INVALID_PARAMETER); + return std::vector(); + } + __try { valuesAsString = getStringValue(std::string(attr + "_available")); std::istringstream iss(valuesAsString);