Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 1 | // 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 Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 5 | #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 Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 14 | #include <libmems/iio_context_impl.h> |
| 15 | #include <libmems/iio_device.h> |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 16 | #include "mems_setup/configuration.h" |
| 17 | #include "mems_setup/delegate_impl.h" |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 18 | #include "mems_setup/sensor_kind.h" |
| 19 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 20 | int main(int argc, char** argv) { |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 21 | DEFINE_string(sensor_kind, "", |
| 22 | "Kind of sensor being initialized. " |
| 23 | "One of anglvel, accel."); |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 24 | DEFINE_int32(device_id, -1, |
| 25 | "The IIO device id for the sensor being " |
| 26 | "initialized, such as iio:device0."); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 27 | DEFINE_string(device_name, "", |
| 28 | "The IIO device path for the sensor being " |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 29 | "initialized, such as cros-ec-accel."); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 30 | |
| 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 Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 38 | if (FLAGS_sensor_kind.empty() || |
| 39 | (FLAGS_device_id == -1 && FLAGS_device_name.empty())) { |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 40 | LOG(ERROR) << "mems_setup must be called with sensor and type"; |
| 41 | exit(1); |
| 42 | } |
| 43 | |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 44 | 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 Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 51 | |
| 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 Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 75 | std::unique_ptr<libmems::IioContext> context( |
| 76 | new libmems::IioContextImpl()); |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 77 | |
| 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 Grignou | d4c9d65 | 2019-12-28 01:13:16 -0800 | [diff] [blame^] | 88 | if (devices.size() != 1) { |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 89 | LOG(ERROR) << devices.size() << " possible devices with name " |
| 90 | << FLAGS_device_name << " found"; |
| 91 | exit(1); |
| 92 | } |
| 93 | device = devices[0]; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 94 | } |
| 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 Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 100 | } |