Skip to content

Commit

Permalink
Add checks
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed May 19, 2019
1 parent f70f0bb commit d1bc954
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions PlutoSDR_Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,27 @@ bool SoapyPlutoSDR::is_sensor_channel(struct iio_channel *chn) const
double SoapyPlutoSDR::get_sensor_value(struct iio_channel *chn) const
{
char *old_locale;
char buf[1024];
double val;
char buf[32];
double val = 0.0;

old_locale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");

if (iio_channel_find_attr(chn, "input")) {
iio_channel_attr_read(chn, "input", buf, sizeof(buf));
val = strtod(buf, NULL);
if (iio_channel_attr_read(chn, "input", buf, sizeof(buf)) > 0)
val = strtod(buf, NULL);
} else {
iio_channel_attr_read(chn, "raw", buf, sizeof(buf));
val = strtod(buf, NULL);
if (iio_channel_attr_read(chn, "raw", buf, sizeof(buf)) > 0)
val = strtod(buf, NULL);

if (iio_channel_find_attr(chn, "offset")) {
iio_channel_attr_read(chn, "offset", buf, sizeof(buf));
val += strtod(buf, NULL);
if (iio_channel_attr_read(chn, "offset", buf, sizeof(buf)) > 0)
val += strtod(buf, NULL);
}

if (iio_channel_find_attr(chn, "scale")) {
iio_channel_attr_read(chn, "scale", buf, sizeof(buf));
val *= strtod(buf, NULL);
if (iio_channel_attr_read(chn, "scale", buf, sizeof(buf)) > 0)
val *= strtod(buf, NULL);
}
}

Expand Down Expand Up @@ -241,13 +241,16 @@ SoapySDR::ArgInfo SoapyPlutoSDR::getSensorInfo(const std::string &key) const
std::string channelStr = key.substr(dash + 1);

iio_device *dev = iio_context_find_device(ctx, deviceStr.c_str());
//if (!dev) ...
if (!dev)
return info;
iio_channel *chn = iio_device_find_channel(dev, channelStr.c_str(), false);
//if (!chn) ...
if (!chn)
return info;

const char *name = iio_channel_get_name(chn);
info.key = key;
if (name) info.name = name;
if (name)
info.name = name;
info.type = SoapySDR::ArgInfo::FLOAT;
info.value = "0.0";
info.units = id_to_unit(channelStr.c_str());
Expand All @@ -267,9 +270,11 @@ std::string SoapyPlutoSDR::readSensor(const std::string &key) const
std::string channelStr = key.substr(dash + 1);

iio_device *dev = iio_context_find_device(ctx, deviceStr.c_str());
//if (!dev) ...
if (!dev)
return sensorValue;
iio_channel *chn = iio_device_find_channel(dev, channelStr.c_str(), false);
//if (!chn) ...
if (!chn)
return sensorValue;

double value = get_sensor_value(chn);
sensorValue.assign(std::to_string(value));
Expand Down

0 comments on commit d1bc954

Please sign in to comment.