blob: 5a459c9770dc15762727db3fd3dc531656ee6aed [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(
61 new mems_setup::DelegateImpl());
62
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
Enrico Granata51cdb942019-06-18 16:40:17 -070075 std::unique_ptr<libmems::IioContext> context(
76 new libmems::IioContextImpl());
Harvey Yang0c0dab52019-11-18 12:04:30 +080077
78 libmems::IioDevice* device = nullptr;
79 if (FLAGS_device_id != -1) {
80 device = context->GetDeviceById(FLAGS_device_id);
81
82 if (device == nullptr) {
83 LOG(ERROR) << "device with id: " << FLAGS_device_id << " not found";
84 exit(1);
85 }
86 } else {
87 auto devices = context->GetDevicesByName(FLAGS_device_name);
Gwendal Grignoud4c9d652019-12-28 01:13:16 -080088 if (devices.size() != 1) {
Harvey Yang0c0dab52019-11-18 12:04:30 +080089 LOG(ERROR) << devices.size() << " possible devices with name "
90 << FLAGS_device_name << " found";
91 exit(1);
92 }
93 device = devices[0];
Enrico Granata60b1cbc2019-05-20 11:06:46 -070094 }
95
96 std::unique_ptr<mems_setup::Configuration> config(
97 new mems_setup::Configuration(device, kind, delegate.get()));
98
99 return config->Configure() ? 0 : 1;
Enrico Granata60a818d2019-05-09 09:56:09 -0700100}