aboutsummaryrefslogtreecommitdiff
path: root/daemon/uart_manager.cc
blob: bc98111e0e9c92c472072e6b8d55d1cfe50ccd5e (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
/*
 * Copyright (C) 2016 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 "uart_manager.h"

#include "uart_driver_sysfs.h"

#include <base/logging.h>

namespace android {

std::unique_ptr<UartManager> g_uart_manager;

UartManager* UartManager::GetManager() {
  if (!g_uart_manager) {
    g_uart_manager.reset(new UartManager);
  }
  return g_uart_manager.get();
}

void UartManager::ResetManager() {
  g_uart_manager.reset();
}

UartManager::UartManager() {}

UartManager::~UartManager() {}

bool UartManager::RegisterDriver(
    std::unique_ptr<UartDriverInfoBase> driver_info) {
  std::string key = driver_info->Compat();
  driver_infos_[key] = std::move(driver_info);
  return true;
}

bool UartManager::RegisterUartDevice(const std::string& name,
                                     const std::string& uart_device_name) {
  if (uart_devices_.count(name)) {
    LOG(ERROR) << "Uart device " << name << " is already registered";
    return false;
  }
  uart_devices_[name].name = name;
  uart_devices_[name].path = uart_device_name;
  return true;
}

bool UartManager::SetPinMux(const std::string& name,
                            const std::string& pin_mux) {
  auto bus_it = uart_devices_.find(name);
  if (bus_it == uart_devices_.end()) {
    LOG(ERROR) << "Uart device " << name << " is not registered";
    return false;
  }

  bus_it->second.mux = pin_mux;
  return true;
}

std::vector<std::string> UartManager::GetDevicesList() {
  std::vector<std::string> list;
  for (const auto& it : uart_devices_) {
    list.push_back(it.first);
  }
  return list;
}

bool UartManager::HasUartDevice(const std::string& name) {
  return uart_devices_.count(name);
}

std::unique_ptr<UartDevice> UartManager::OpenUartDevice(
    const std::string& name) {
  // Get the Bus from the BSP.
  auto bus_it = uart_devices_.find(name);
  if (bus_it == uart_devices_.end())
    return nullptr;

  // Check its not already in use
  if (bus_it->second.driver_)
    return nullptr;

  // Find a driver.
  // Currently there is only hardcoded support for UARTSYSFS
  auto driver_info_it = driver_infos_.find(UartDriverSysfs::Compat());

  // Fail if there is no driver.
  if (driver_info_it == driver_infos_.end())
    return nullptr;

  std::unique_ptr<UartDriverInterface> driver(driver_info_it->second->Probe());

  if (!driver->Init(bus_it->second.path))
    return nullptr;

  // Set Pin muxing.
  if (!bus_it->second.mux.empty()) {
    PinMuxManager::GetPinMuxManager()->SetSource(bus_it->second.mux,
                                                 bus_it->second.mux);
  }

  bus_it->second.driver_ = std::move(driver);

  return std::unique_ptr<UartDevice>(new UartDevice(&(bus_it->second)));
}

}  // namespace android