-
Notifications
You must be signed in to change notification settings - Fork 1
/
iio_device.cc
133 lines (105 loc) · 3.88 KB
/
iio_device.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "libmems/iio_device.h"
#include <stdlib.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/string_split.h>
#include "libmems/common_types.h"
#include "libmems/iio_channel.h"
namespace libmems {
IioDevice::~IioDevice() = default;
bool IioDevice::IsSingleSensor() const {
return ReadStringAttribute("location").has_value();
}
// static
base::Optional<int> IioDevice::GetIdAfterPrefix(const char* id_str,
const char* prefix) {
size_t id_len = strlen(id_str);
size_t prefix_len = strlen(prefix);
if (id_len <= prefix_len || strncmp(id_str, prefix, prefix_len) != 0) {
return base::nullopt;
}
int value = 0;
bool success = base::StringToInt(std::string(id_str + prefix_len), &value);
if (success)
return value;
return base::nullopt;
}
std::vector<IioChannel*> IioDevice::GetAllChannels() {
std::vector<IioChannel*> channels;
for (const auto& channel_data : channels_)
channels.push_back(channel_data.chn.get());
return channels;
}
void IioDevice::EnableAllChannels() {
for (IioChannel* chn : GetAllChannels()) {
if (!chn->SetEnabledAndCheck(true))
LOG(ERROR) << "Failed to enable channel: " << chn->GetId();
}
}
IioChannel* IioDevice::GetChannel(int32_t index) {
if (index < 0 || index >= channels_.size())
return nullptr;
return channels_[index].chn.get();
}
IioChannel* IioDevice::GetChannel(const std::string& name) {
for (size_t i = 0; i < channels_.size(); ++i) {
if (channels_[i].chn_id == name)
return channels_[i].chn.get();
}
return nullptr;
}
bool IioDevice::GetMinMaxFrequency(double* min_freq, double* max_freq) {
auto available_opt = ReadStringAttribute(kSamplingFrequencyAvailable);
if (!available_opt.has_value()) {
LOG(ERROR) << "Failed to read attribute: " << kSamplingFrequencyAvailable;
return false;
}
std::string sampling_frequency_available = available_opt.value();
// Remove trailing '\0' for parsing
auto pos = available_opt->find_first_of('\0');
if (pos != std::string::npos)
sampling_frequency_available = available_opt->substr(0, pos);
std::vector<std::string> sampling_frequencies =
base::SplitString(sampling_frequency_available, " ",
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
switch (sampling_frequencies.size()) {
case 0:
LOG(ERROR) << "Invalid format of " << kSamplingFrequencyAvailable << ": "
<< sampling_frequency_available;
return false;
case 1:
if (!base::StringToDouble(sampling_frequencies.front(), min_freq) ||
*min_freq < 0.0 || *min_freq < kFrequencyEpsilon) {
LOG(ERROR) << "Failed to parse min max sampling_frequency: "
<< sampling_frequency_available;
return false;
}
*max_freq = *min_freq;
return true;
default:
if (!base::StringToDouble(sampling_frequencies.back(), max_freq) ||
*max_freq < kFrequencyEpsilon) {
LOG(ERROR) << "Failed to parse max sampling_frequency: "
<< sampling_frequency_available;
return false;
}
if (!base::StringToDouble(sampling_frequencies.front(), min_freq) ||
*min_freq < 0.0) {
LOG(ERROR) << "Failed to parse the first sampling_frequency: "
<< sampling_frequency_available;
return false;
}
if (*min_freq == 0.0) {
if (!base::StringToDouble(sampling_frequencies[1], min_freq) ||
*min_freq < 0.0 || *max_freq < *min_freq) {
LOG(ERROR) << "Failed to parse min sampling_frequency: "
<< sampling_frequency_available;
return false;
}
}
return true;
}
}
} // namespace libmems