Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <experimental/filesystem> |
| 17 | #include <phosphor-logging/log.hpp> |
| 18 | #include <regex> |
| 19 | #include "targeting.hpp" |
| 20 | |
| 21 | namespace openpower |
| 22 | { |
| 23 | namespace targeting |
| 24 | { |
| 25 | |
| 26 | using namespace phosphor::logging; |
| 27 | namespace fs = std::experimental::filesystem; |
| 28 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 29 | int Target::getCFAMFD() |
| 30 | { |
| 31 | if (cfamFD.get() == nullptr) |
| 32 | { |
| 33 | cfamFD = std::make_unique< |
| 34 | openpower::util::FileDescriptor>(getCFAMPath()); |
| 35 | } |
| 36 | |
| 37 | return cfamFD->get(); |
| 38 | } |
| 39 | |
Michael Tritz | be40716 | 2017-03-30 16:52:24 -0500 | [diff] [blame^] | 40 | std::unique_ptr<Target>& Targeting::getTarget(size_t pos) |
| 41 | { |
| 42 | auto search = [pos](const auto& t) |
| 43 | { |
| 44 | return t->getPos() == pos; |
| 45 | }; |
| 46 | |
| 47 | auto target = find_if(targets.begin(), targets.end(), search); |
| 48 | if (target == targets.end()) |
| 49 | { |
| 50 | throw std::runtime_error("Target not found: " + std::to_string(pos)); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | return *target; |
| 55 | } |
| 56 | } |
| 57 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 58 | |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 59 | Targeting::Targeting(const std::string& fsiMasterDev, |
| 60 | const std::string& fsiSlaveDir) : |
| 61 | fsiMasterPath(fsiMasterDev), |
| 62 | fsiSlaveBasePath(fsiSlaveDir) |
| 63 | { |
| 64 | //Always create P0, the FSI master. |
| 65 | targets.push_back(std::make_unique<Target>(0, fsiMasterPath)); |
| 66 | |
| 67 | //Find the the remaining P9s dynamically based on which files show up |
Matt Spinler | fabe92e | 2017-03-17 11:25:22 -0500 | [diff] [blame] | 68 | std::regex exp{"hub@00/slave@([0-9]{2}):00", std::regex::extended}; |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 69 | |
| 70 | for (auto& file : fs::directory_iterator(fsiSlaveBasePath)) |
| 71 | { |
| 72 | std::smatch match; |
| 73 | std::string path = file.path(); |
| 74 | if (std::regex_search(path, match, exp)) |
| 75 | { |
| 76 | auto pos = atoi(match[1].str().c_str()); |
| 77 | if (pos == 0) |
| 78 | { |
| 79 | log<level::ERR>("Unexpected FSI slave device name found", |
Matt Spinler | fabe92e | 2017-03-17 11:25:22 -0500 | [diff] [blame] | 80 | entry("DEVICE_NAME=%s", path.c_str())); |
Matt Spinler | 2c05aa7 | 2017-02-28 09:48:13 -0600 | [diff] [blame] | 81 | continue; |
| 82 | } |
| 83 | |
| 84 | path += "/raw"; |
| 85 | |
| 86 | targets.push_back(std::make_unique<Target>(pos, path)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | auto sortTargets = [](const std::unique_ptr<Target>& left, |
| 91 | const std::unique_ptr<Target>& right) |
| 92 | { |
| 93 | return left->getPos() < right->getPos(); |
| 94 | }; |
| 95 | std::sort(targets.begin(), targets.end(), sortTargets); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | } |