-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiio_device_trigger_impl.cc
146 lines (121 loc) · 4.24 KB
/
iio_device_trigger_impl.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
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 <string>
#include <base/files/file_util.h>
#include <base/logging.h>
#include <base/strings/stringprintf.h>
#include "libmems/common_types.h"
#include "libmems/iio_channel.h"
#include "libmems/iio_context_impl.h"
#include "libmems/iio_device_trigger_impl.h"
namespace {
constexpr char kAddTrigger[] = "add_trigger";
} // namespace
namespace libmems {
base::Optional<int> IioDeviceTriggerImpl::GetIdFromString(const char* id_str) {
size_t id_len = strlen(id_str);
if (id_len == strlen(kIioSysfsTrigger) &&
strncmp(id_str, kIioSysfsTrigger, id_len) == 0) {
return -1;
}
return IioDevice::GetIdAfterPrefix(id_str, kTriggerIdPrefix);
}
std::string IioDeviceTriggerImpl::GetStringFromId(int id) {
if (id == -1)
return std::string(kIioSysfsTrigger);
return base::StringPrintf("trigger%d", id);
}
IioDeviceTriggerImpl::IioDeviceTriggerImpl(IioContextImpl* ctx, iio_device* dev)
: IioDevice(), context_(ctx), trigger_(dev) {
CHECK(context_);
CHECK(trigger_);
log_prefix_ = base::StringPrintf("Trigger with id: %d and name: %s. ",
GetId(), (GetName() ? GetName() : "null"));
}
IioContext* IioDeviceTriggerImpl::GetContext() const {
return context_;
}
const char* IioDeviceTriggerImpl::GetName() const {
return iio_device_get_name(trigger_);
}
int IioDeviceTriggerImpl::GetId() const {
const char* id_str = iio_device_get_id(trigger_);
auto id = GetIdFromString(id_str);
DCHECK(id.has_value());
return id.value();
}
base::FilePath IioDeviceTriggerImpl::GetPath() const {
int id = GetId();
std::string id_str = std::string(kIioSysfsTrigger);
if (id >= 0)
id_str = GetStringFromId(id);
auto path = base::FilePath(kSysDevString).Append(id_str);
CHECK(base::DirectoryExists(path));
return path;
}
base::Optional<std::string> IioDeviceTriggerImpl::ReadStringAttribute(
const std::string& name) const {
char data[kReadAttrBufferSize] = {0};
ssize_t len =
iio_device_attr_read(trigger_, name.c_str(), data, sizeof(data));
if (len < 0) {
LOG(WARNING) << log_prefix_ << "Attempting to read string attribute "
<< name << " failed: " << len;
return base::nullopt;
}
return std::string(data, len);
}
base::Optional<int64_t> IioDeviceTriggerImpl::ReadNumberAttribute(
const std::string& name) const {
long long val = 0; // NOLINT(runtime/int)
int error = iio_device_attr_read_longlong(trigger_, name.c_str(), &val);
if (error) {
LOG(WARNING) << log_prefix_ << "Attempting to read number attribute "
<< name << " failed: " << error;
return base::nullopt;
}
return val;
}
base::Optional<double> IioDeviceTriggerImpl::ReadDoubleAttribute(
const std::string& name) const {
double val = 0;
int error = iio_device_attr_read_double(trigger_, name.c_str(), &val);
if (error) {
LOG(WARNING) << log_prefix_ << "Attempting to read double attribute "
<< name << " failed: " << error;
return base::nullopt;
}
return val;
}
bool IioDeviceTriggerImpl::WriteNumberAttribute(const std::string& name,
int64_t value) {
int id = GetId();
if ((id == -1 && name.compare(kAddTrigger) != 0) ||
(id != -1 && name.compare(kSamplingFrequencyAttr) != 0)) {
return false;
}
int error = iio_device_attr_write_longlong(trigger_, name.c_str(), value);
if (error) {
LOG(WARNING) << log_prefix_ << "Attempting to write number attribute "
<< name << " failed: " << error;
return false;
}
return true;
}
bool IioDeviceTriggerImpl::WriteDoubleAttribute(const std::string& name,
double value) {
int id = GetId();
if ((id == -1 && name.compare(kAddTrigger) != 0) ||
(id != -1 && name.compare(kSamplingFrequencyAttr) != 0)) {
return false;
}
int error = iio_device_attr_write_double(trigger_, name.c_str(), value);
if (error) {
LOG(WARNING) << log_prefix_ << "Attempting to write double attribute "
<< name << " failed: " << error;
return false;
}
return true;
}
} // namespace libmems