blob: a6fef4095a94ec3785c31632b8490ee5b6eca67e [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
Harvey Yang30ec26b2020-12-29 17:10:11 +080014#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 Granata51cdb942019-06-18 16:40:17 -070019#include <libmems/iio_context_impl.h>
20#include <libmems/iio_device.h>
Enrico Granata60b1cbc2019-05-20 11:06:46 -070021#include "mems_setup/configuration.h"
22#include "mems_setup/delegate_impl.h"
Enrico Granata60b1cbc2019-05-20 11:06:46 -070023#include "mems_setup/sensor_kind.h"
24
Enrico Granata60a818d2019-05-09 09:56:09 -070025int main(int argc, char** argv) {
Harvey Yang0c0dab52019-11-18 12:04:30 +080026 DEFINE_int32(device_id, -1,
27 "The IIO device id for the sensor being "
28 "initialized, such as iio:device0.");
Enrico Granata60b1cbc2019-05-20 11:06:46 -070029 DEFINE_string(device_name, "",
30 "The IIO device path for the sensor being "
Harvey Yang0c0dab52019-11-18 12:04:30 +080031 "initialized, such as cros-ec-accel.");
Enrico Granata60b1cbc2019-05-20 11:06:46 -070032
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 Yang73394622020-02-12 10:57:57 +080040 if ((FLAGS_device_id == -1 && FLAGS_device_name.empty())) {
41 LOG(ERROR) << "mems_setup must be called with sensor";
Enrico Granata60b1cbc2019-05-20 11:06:46 -070042 exit(1);
43 }
44
Harvey Yang0c0dab52019-11-18 12:04:30 +080045 if (FLAGS_device_name.empty()) {
Harvey Yang73394622020-02-12 10:57:57 +080046 LOG(INFO) << "Starting mems_setup [id=" << FLAGS_device_id << "]";
Harvey Yang0c0dab52019-11-18 12:04:30 +080047 } else {
Harvey Yang73394622020-02-12 10:57:57 +080048 LOG(INFO) << "Starting mems_setup [name=" << FLAGS_device_name << "]";
Enrico Granata60b1cbc2019-05-20 11:06:46 -070049 }
50
51 std::unique_ptr<mems_setup::Delegate> delegate(
Tom Hughes09483d12020-08-27 15:55:08 -070052 new mems_setup::DelegateImpl());
Enrico Granata60b1cbc2019-05-20 11:06:46 -070053
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 Hughes09483d12020-08-27 15:55:08 -070066 std::unique_ptr<libmems::IioContext> context(new libmems::IioContextImpl());
Harvey Yang0c0dab52019-11-18 12:04:30 +080067
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 Grignoud4c9d652019-12-28 01:13:16 -080078 if (devices.size() != 1) {
Harvey Yang0c0dab52019-11-18 12:04:30 +080079 LOG(ERROR) << devices.size() << " possible devices with name "
80 << FLAGS_device_name << " found";
81 exit(1);
82 }
83 device = devices[0];
Enrico Granata60b1cbc2019-05-20 11:06:46 -070084 }
85
86 std::unique_ptr<mems_setup::Configuration> config(
Harvey Yang73394622020-02-12 10:57:57 +080087 new mems_setup::Configuration(context.get(), device, delegate.get()));
Enrico Granata60b1cbc2019-05-20 11:06:46 -070088
Harvey Yang30ec26b2020-12-29 17:10:11 +080089 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 Granata60a818d2019-05-09 09:56:09 -0700117}