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 | |
Harvey Yang | 30ec26b | 2020-12-29 17:10:11 +0800 | [diff] [blame] | 14 | #include <base/task/single_thread_task_executor.h> |
| 15 | #include <dbus/bus.h> |
| 16 | #include <dbus/message.h> |
| 17 | #include <dbus/object_proxy.h> |
| 18 | #include <iioservice/include/dbus-constants.h> |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 19 | #include <libmems/iio_context_impl.h> |
| 20 | #include <libmems/iio_device.h> |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 21 | #include "mems_setup/configuration.h" |
| 22 | #include "mems_setup/delegate_impl.h" |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 23 | #include "mems_setup/sensor_kind.h" |
| 24 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 25 | int main(int argc, char** argv) { |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 26 | DEFINE_int32(device_id, -1, |
| 27 | "The IIO device id for the sensor being " |
| 28 | "initialized, such as iio:device0."); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 29 | DEFINE_string(device_name, "", |
| 30 | "The IIO device path for the sensor being " |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 31 | "initialized, such as cros-ec-accel."); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 32 | |
| 33 | brillo::OpenLog("mems_setup", true /*log_pid*/); |
| 34 | |
| 35 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader | |
| 36 | brillo::kLogToStderrIfTty); |
| 37 | |
| 38 | brillo::FlagHelper::Init(argc, argv, "Chromium OS MEMS Setup"); |
| 39 | |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 40 | if ((FLAGS_device_id == -1 && FLAGS_device_name.empty())) { |
| 41 | LOG(ERROR) << "mems_setup must be called with sensor"; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 42 | exit(1); |
| 43 | } |
| 44 | |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 45 | if (FLAGS_device_name.empty()) { |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 46 | LOG(INFO) << "Starting mems_setup [id=" << FLAGS_device_id << "]"; |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 47 | } else { |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 48 | LOG(INFO) << "Starting mems_setup [name=" << FLAGS_device_name << "]"; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | std::unique_ptr<mems_setup::Delegate> delegate( |
Tom Hughes | 09483d1 | 2020-08-27 15:55:08 -0700 | [diff] [blame] | 52 | new mems_setup::DelegateImpl()); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 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 | |
Tom Hughes | 09483d1 | 2020-08-27 15:55:08 -0700 | [diff] [blame] | 66 | std::unique_ptr<libmems::IioContext> context(new libmems::IioContextImpl()); |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 67 | |
| 68 | libmems::IioDevice* device = nullptr; |
| 69 | if (FLAGS_device_id != -1) { |
| 70 | device = context->GetDeviceById(FLAGS_device_id); |
| 71 | |
| 72 | if (device == nullptr) { |
| 73 | LOG(ERROR) << "device with id: " << FLAGS_device_id << " not found"; |
| 74 | exit(1); |
| 75 | } |
| 76 | } else { |
| 77 | auto devices = context->GetDevicesByName(FLAGS_device_name); |
Gwendal Grignou | d4c9d65 | 2019-12-28 01:13:16 -0800 | [diff] [blame] | 78 | if (devices.size() != 1) { |
Harvey Yang | 0c0dab5 | 2019-11-18 12:04:30 +0800 | [diff] [blame] | 79 | LOG(ERROR) << devices.size() << " possible devices with name " |
| 80 | << FLAGS_device_name << " found"; |
| 81 | exit(1); |
| 82 | } |
| 83 | device = devices[0]; |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | std::unique_ptr<mems_setup::Configuration> config( |
Harvey Yang | 7339462 | 2020-02-12 10:57:57 +0800 | [diff] [blame] | 87 | new mems_setup::Configuration(context.get(), device, delegate.get())); |
Enrico Granata | 60b1cbc | 2019-05-20 11:06:46 -0700 | [diff] [blame] | 88 | |
Harvey Yang | 30ec26b | 2020-12-29 17:10:11 +0800 | [diff] [blame] | 89 | if (config->Configure()) { |
| 90 | #if USE_IIOSERVICE |
| 91 | base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO); |
| 92 | |
| 93 | dbus::Bus::Options options; |
| 94 | options.bus_type = dbus::Bus::SYSTEM; |
| 95 | scoped_refptr<dbus::Bus> bus(new dbus::Bus(options)); |
| 96 | if (!bus->Connect()) { |
| 97 | LOG(ERROR) << "mems_setup: Cannot connect to D-Bus."; |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | dbus::ObjectProxy* proxy = bus->GetObjectProxy( |
| 102 | ::iioservice::kIioserviceServiceName, |
| 103 | dbus::ObjectPath(::iioservice::kIioserviceServicePath)); |
| 104 | |
| 105 | dbus::MethodCall method_call(::iioservice::kIioserviceInterface, |
| 106 | ::iioservice::kMemsSetupDoneMethod); |
| 107 | dbus::MessageWriter writer(&method_call); |
| 108 | writer.AppendInt32(device->GetId()); |
| 109 | |
| 110 | proxy->CallMethodAndBlock(&method_call, |
| 111 | dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); |
| 112 | #endif // USE_IIOSERVICE |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | return 1; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 117 | } |