aboutsummaryrefslogtreecommitdiff
path: root/daemon/led_manager.cc
blob: 949a995215781181aedc5ea4f6740d25a2cc96ac (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
/*
 * 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 "led_manager.h"

#include <base/logging.h>

namespace android {

std::unique_ptr<LedManager> g_led_manager;

LedManager::LedManager() {}

LedManager::~LedManager() {}

// static
LedManager* LedManager::GetLedManager() {
  if (!g_led_manager) {
    g_led_manager.reset(new LedManager());
  }
  return g_led_manager.get();
}

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

bool LedManager::RegisterLedSysfs(const std::string& name, const std::string& led_name) {
  if (leds_.count(name))
    return false;
  leds_[name].name = led_name;
  return true;
}

std::unique_ptr<Led> LedManager::OpenLed(const std::string& name) {
  auto led_it = leds_.find(name);
  if (led_it == leds_.end()) {
    LOG(WARNING) << "LedManager: Led not found. " << name;
    return nullptr;
  }

  // Check its not alread in use
  if (led_it->second.driver_) {
    LOG(WARNING) << "LedManager: Led in use. " << name;
    return nullptr;
  }

  // Find a driver.
  auto driver_info_it = driver_infos_.find("LEDSYSFS");

  // Fail if there is no driver.
  if (driver_info_it == driver_infos_.end()) {
    LOG(WARNING) << "LedManager: Failed to find driver " << name;
    return nullptr;
  }

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

  // Set pin mux
  if (!led_it->second.mux.empty()) {
    //TODO(leecam): Enable pin muxing
  }

  if (!driver->Init(led_it->second.name)) {
    LOG(WARNING) << "LedManager: Failed to init driver " << name;
    return nullptr;
  }

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

  return std::unique_ptr<Led>(new Led(&(led_it->second)));
}

std::vector<std::string> LedManager::GetLeds() {
  std::vector<std::string> leds;
  for (auto& i : leds_)
    leds.push_back(i.first);
  return leds;
}

bool LedManager::HasLed(const std::string& name) {
  return leds_.count(name);
}

}  // namespace android