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) { |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 21 | DEFINE_int32(device_id, -1, |
| 22 | "The IIO device id for the sensor being " |
| 23 | "initialized, such as iio:device0."); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 24 | DEFINE_string(device_name, "", |
| 25 | "The IIO device path for the sensor being " |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 26 | "initialized, such as cros-ec-accel."); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 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 | |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 35 | if ((FLAGS_device_id == -1 && FLAGS_device_name.empty())) { |
| 36 | LOG(ERROR) << "mems_setup must be called with sensor"; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 37 | exit(1); |
| 38 | } |
| 39 | |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 40 | if (FLAGS_device_name.empty()) { |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 41 | LOG(INFO) << "Starting mems_setup [id=" << FLAGS_device_id << "]"; |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 42 | } else { |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 43 | LOG(INFO) << "Starting mems_setup [name=" << FLAGS_device_name << "]"; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | std::unique_ptr<mems_setup::Delegate> delegate( |
Tom Hughes | 09483d1 | 2020-08-27 15:55:08 -0700 | [diff] [blame] | 47 | new mems_setup::DelegateImpl()); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 48 | |
| 49 | base::FilePath iio_trig_sysfs_path("/sys/bus/iio/devices/iio_sysfs_trigger"); |
| 50 | if (!delegate->Exists(iio_trig_sysfs_path)) { |
| 51 | if (!delegate->ProbeKernelModule("iio_trig_sysfs")) { |
| 52 | LOG(ERROR) << "cannot load iio_trig_sysfs module"; |
| 53 | exit(1); |
| 54 | } |
| 55 | if (!delegate->Exists(iio_trig_sysfs_path)) { |
| 56 | LOG(ERROR) << "cannot find iio_sysfs_trigger device"; |
| 57 | exit(1); |
| 58 | } |
| 59 | } |
| 60 | |
Tom Hughes | 09483d1 | 2020-08-27 15:55:08 -0700 | [diff] [blame] | 61 | std::unique_ptr<libmems::IioContext> context(new libmems::IioContextImpl()); |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 62 | |
| 63 | libmems::IioDevice* device = nullptr; |
| 64 | if (FLAGS_device_id != -1) { |
| 65 | device = context->GetDeviceById(FLAGS_device_id); |
| 66 | |
| 67 | if (device == nullptr) { |
| 68 | LOG(ERROR) << "device with id: " << FLAGS_device_id << " not found"; |
| 69 | exit(1); |
| 70 | } |
| 71 | } else { |
| 72 | auto devices = context->GetDevicesByName(FLAGS_device_name); |
Gwendal Grignou | d4c9d65 | 2019-12-28 01:13:16 -0800 | [diff] [blame] | 73 | if (devices.size() != 1) { |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 74 | LOG(ERROR) << devices.size() << " possible devices with name " |
| 75 | << FLAGS_device_name << " found"; |
| 76 | exit(1); |
| 77 | } |
| 78 | device = devices[0]; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | std::unique_ptr<mems_setup::Configuration> config( |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 82 | new mems_setup::Configuration(context.get(), device, delegate.get())); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 83 | |
| 84 | return config->Configure() ? 0 : 1; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 85 | } |