blob: e03eaf44015200b2da7a8002f2ee3afa7ef6ea06 [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
Enrico Granata51cdb942019-06-18 16:40:17 -070014#include <libmems/iio_context_impl.h>
15#include <libmems/iio_device.h>
Enrico Granata60b1cbc2019-05-20 11:06:46 -070016#include "mems_setup/configuration.h"
17#include "mems_setup/delegate_impl.h"
Enrico Granata60b1cbc2019-05-20 11:06:46 -070018#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.");
Harvey Yang0c0dab52019-11-18 12:04:30 +080024 DEFINE_int32(device_id, -1,
25 "The IIO device id for the sensor being "
26 "initialized, such as iio:device0.");
Enrico Granata60b1cbc2019-05-20 11:06:46 -070027 DEFINE_string(device_name, "",
28 "The IIO device path for the sensor being "
Harvey Yang0c0dab52019-11-18 12:04:30 +080029 "initialized, such as cros-ec-accel.");
Enrico Granata60b1cbc2019-05-20 11:06:46 -070030
31 brillo::OpenLog("mems_setup", true /*log_pid*/);
32
33 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader |
34 brillo::kLogToStderrIfTty);
35
36 brillo::FlagHelper::Init(argc, argv, "Chromium OS MEMS Setup");
37
Harvey Yang0c0dab52019-11-18 12:04:30 +080038 if (FLAGS_sensor_kind.empty() ||
39 (FLAGS_device_id == -1 && FLAGS_device_name.empty())) {
Enrico Granata60b1cbc2019-05-20 11:06:46 -070040 LOG(ERROR) << "mems_setup must be called with sensor and type";
41 exit(1);
42 }
43
Harvey Yang0c0dab52019-11-18 12:04:30 +080044 if (FLAGS_device_name.empty()) {
45 LOG(INFO) << "Starting mems_setup [id=" << FLAGS_device_id
46 << ", kind=" << FLAGS_sensor_kind << "]";
47 } else {
48 LOG(INFO) << "Starting mems_setup [name=" << FLAGS_device_name
49 << ", kind=" << FLAGS_sensor_kind << "]";
50 }
Enrico Granata60b1cbc2019-05-20 11:06:46 -070051
52 mems_setup::SensorKind kind;
53 if (auto sk = mems_setup::SensorKindFromString(FLAGS_sensor_kind)) {
54 kind = sk.value();
55 } else {
56 LOG(ERROR) << FLAGS_sensor_kind << " is not a known type of sensor";
57 exit(1);
58 }
59
60 std::unique_ptr<mems_setup::Delegate> delegate(
Tom Hughes09483d12020-08-27 15:55:08 -070061 new mems_setup::DelegateImpl());
Enrico Granata60b1cbc2019-05-20 11:06:46 -070062
63 base::FilePath iio_trig_sysfs_path("/sys/bus/iio/devices/iio_sysfs_trigger");
64 if (!delegate->Exists(iio_trig_sysfs_path)) {
65 if (!delegate->ProbeKernelModule("iio_trig_sysfs")) {
66 LOG(ERROR) << "cannot load iio_trig_sysfs module";
67 exit(1);
68 }
69 if (!delegate->Exists(iio_trig_sysfs_path)) {
70 LOG(ERROR) << "cannot find iio_sysfs_trigger device";
71 exit(1);
72 }
73 }
74
Tom Hughes09483d12020-08-27 15:55:08 -070075 std::unique_ptr<libmems::IioContext> context(new libmems::IioContextImpl());
Harvey Yang0c0dab52019-11-18 12:04:30 +080076
77 libmems::IioDevice* device = nullptr;
78 if (FLAGS_device_id != -1) {
79 device = context->GetDeviceById(FLAGS_device_id);
80
81 if (device == nullptr) {
82 LOG(ERROR) << "device with id: " << FLAGS_device_id << " not found";
83 exit(1);
84 }
85 } else {
86 auto devices = context->GetDevicesByName(FLAGS_device_name);
Gwendal Grignoud4c9d652019-12-28 01:13:16 -080087 if (devices.size() != 1) {
Harvey Yang0c0dab52019-11-18 12:04:30 +080088 LOG(ERROR) << devices.size() << " possible devices with name "
89 << FLAGS_device_name << " found";
90 exit(1);
91 }
92 device = devices[0];
Enrico Granata60b1cbc2019-05-20 11:06:46 -070093 }
94
95 std::unique_ptr<mems_setup::Configuration> config(
Gwendal Grignouba911fe2019-12-08 17:14:53 -080096 new mems_setup::Configuration(context.get(), device, kind,
97 delegate.get()));
Enrico Granata60b1cbc2019-05-20 11:06:46 -070098
99 return config->Configure() ? 0 : 1;
Enrico Granata60a818d2019-05-09 09:56:09 -0700100}