aboutsummaryrefslogtreecommitdiff
path: root/host/commands/run_cvd/launch/mcu.cpp
blob: 8076abebdfd7cd63da2d1199905753b42574aecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <unordered_set>
#include <vector>

#include <android-base/logging.h>
#include <fruit/fruit.h>
#include <json/json.h>

#include "common/libs/utils/files.h"
#include "common/libs/utils/result.h"
#include "host/commands/run_cvd/launch/launch.h"
#include "host/commands/run_cvd/launch/log_tee_creator.h"
#include "host/libs/config/command_source.h"
#include "host/libs/config/cuttlefish_config.h"
#include "host/libs/config/known_paths.h"


// timeout for the MCU channels to be created after the start command is issued
#define MCU_START_TIMEOUT 30

namespace cuttlefish {
namespace {

class Mcu : public vm_manager::VmmDependencyCommand {
 public:
  INJECT(Mcu(const CuttlefishConfig::InstanceSpecific& instance,
             LogTeeCreator& log_tee))
      :instance_(instance), log_tee_(log_tee) {}

  Result<void> ResultSetup() override {
    if (!Enabled()) {
      return {};
    }

    mcu_dir_ = instance_.PerInstanceInternalPath("/mcu/");
    CF_EXPECT(EnsureDirectoryExists(mcu_dir_),
              "MCU directory cannot be created.");
    return {};
  }

  // CommandSource
  Result<std::vector<MonitorCommand>> Commands() override {
    if (!Enabled()) {
      return {};
    }

    auto start = instance_.mcu()["start-cmd"];
    CF_EXPECT(start.type() == Json::arrayValue,
              "mcu: config: start-cmd: array expected");
    CF_EXPECT(start.size() > 0, "mcu: config: empty start-cmd");
    Command command(android::base::StringReplace(start[0].asString(), "${bin}",
                                                 HostBinaryPath(""), true));

    for (unsigned int i = 1; i < start.size(); i++) {
      auto param = start[i].asString();
      param = android::base::StringReplace(param, "${wdir}", mcu_dir_, true);
      param = android::base::StringReplace(param, "${bin}", HostBinaryPath(""),
                                           true);
      command.AddParameter(param);
    }

    std::vector<MonitorCommand> commands;
    commands.emplace_back(CF_EXPECT(log_tee_.CreateLogTee(command, "mcu")));
    commands.emplace_back(std::move(command));
    return commands;
  }

  // SetupFeature
  std::string Name() const override { return "MCU"; }
  bool Enabled() const override {
    return instance_.mcu().type() != Json::nullValue;
  }

  // StatusCheckCommandSource
  Result<void> WaitForAvailability() const {
    if (!Enabled()) {
      return {};
    }

    auto control = instance_.mcu()["control"]["path"];
    if (control.type() == Json::stringValue) {
      CF_EXPECT(WaitForFile(mcu_dir_ + "/" + control.asString(),
                            MCU_START_TIMEOUT));
    }
    auto uart0 = instance_.mcu()["uart0"]["path"];
    if (uart0.type() == Json::stringValue) {
      CF_EXPECT(WaitForFile(mcu_dir_ + "/" + uart0.asString(),
                            MCU_START_TIMEOUT));
    }
    return {};
  }

 private:
  std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }

  std::string mcu_dir_;
  const CuttlefishConfig::InstanceSpecific& instance_;
  LogTeeCreator& log_tee_;
};

}  // namespace

fruit::Component<
    fruit::Required<const CuttlefishConfig,
                    const CuttlefishConfig::InstanceSpecific, LogTeeCreator>>
McuComponent() {
  return fruit::createComponent()
      .addMultibinding<vm_manager::VmmDependencyCommand, Mcu>()
      .addMultibinding<CommandSource, Mcu>()
      .addMultibinding<SetupFeature, Mcu>();
}

}  // namespace cuttlefish