blob: 9326bd85b88ebdc28f5cdda6cc0e03fd199489f7 [file] [log] [blame]
Enrico Granata60a818d2019-05-09 09:56:09 -07001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Enrico Granata60b1cbc2019-05-20 11:06:46 -07005#include <base/files/file_path.h>
6#include <base/logging.h>
7#include <base/strings/stringprintf.h>
8#include <brillo/flag_helper.h>
9#include <brillo/syslog_logging.h>
10
11#define __STDC_FORMAT_MACROS
12#include <inttypes.h>
13
14#include "mems_setup/configuration.h"
15#include "mems_setup/delegate_impl.h"
16#include "mems_setup/iio_context_impl.h"
17#include "mems_setup/iio_device.h"
18#include "mems_setup/sensor_kind.h"
19
Enrico Granata60a818d2019-05-09 09:56:09 -070020int main(int argc, char** argv) {
Enrico Granata60b1cbc2019-05-20 11:06:46 -070021 DEFINE_string(sensor_kind, "",
22 "Kind of sensor being initialized. "
23 "One of anglvel, accel.");
24 DEFINE_string(device_name, "",
25 "The IIO device path for the sensor being "
26 "initialized, such as iio:device0.");
27
28 brillo::OpenLog("mems_setup", true /*log_pid*/);
29
30 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader |
31 brillo::kLogToStderrIfTty);
32
33 brillo::FlagHelper::Init(argc, argv, "Chromium OS MEMS Setup");
34
35 if (FLAGS_sensor_kind.empty() || FLAGS_device_name.empty()) {
36 LOG(ERROR) << "mems_setup must be called with sensor and type";
37 exit(1);
38 }
39
40 LOG(INFO) << "Starting mems_setup [name=" << FLAGS_device_name
41 << ", kind=" << FLAGS_sensor_kind << "]";
42
43 mems_setup::SensorKind kind;
44 if (auto sk = mems_setup::SensorKindFromString(FLAGS_sensor_kind)) {
45 kind = sk.value();
46 } else {
47 LOG(ERROR) << FLAGS_sensor_kind << " is not a known type of sensor";
48 exit(1);
49 }
50
51 std::unique_ptr<mems_setup::Delegate> delegate(
52 new mems_setup::DelegateImpl());
53
54 base::FilePath iio_trig_sysfs_path("/sys/bus/iio/devices/iio_sysfs_trigger");
55 if (!delegate->Exists(iio_trig_sysfs_path)) {
56 if (!delegate->ProbeKernelModule("iio_trig_sysfs")) {
57 LOG(ERROR) << "cannot load iio_trig_sysfs module";
58 exit(1);
59 }
60 if (!delegate->Exists(iio_trig_sysfs_path)) {
61 LOG(ERROR) << "cannot find iio_sysfs_trigger device";
62 exit(1);
63 }
64 }
65
66 std::unique_ptr<mems_setup::IioContext> context(
67 new mems_setup::IioContextImpl());
68 auto device = context->GetDevice(FLAGS_device_name);
69 if (device == nullptr) {
70 LOG(ERROR) << "device" << FLAGS_device_name << " not found";
71 exit(1);
72 }
73
74 std::unique_ptr<mems_setup::Configuration> config(
75 new mems_setup::Configuration(device, kind, delegate.get()));
76
77 return config->Configure() ? 0 : 1;
Enrico Granata60a818d2019-05-09 09:56:09 -070078}