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 
29 Targeting::Targeting(const std::string& fsiMasterDev,
30                      const std::string& fsiSlaveDir) :
31     fsiMasterPath(fsiMasterDev),
32     fsiSlaveBasePath(fsiSlaveDir)
33 {
34     //Always create P0, the FSI master.
35     targets.push_back(std::make_unique<Target>(0, fsiMasterPath));
36 
37     //Find the the remaining P9s dynamically based on which files show up
38     std::regex exp{"slave@([0-9]{2}):00", std::regex::extended};
39 
40     for (auto& file : fs::directory_iterator(fsiSlaveBasePath))
41     {
42         std::smatch match;
43         std::string path = file.path();
44         if (std::regex_search(path, match, exp))
45         {
46             auto pos = atoi(match[1].str().c_str());
47             if (pos == 0)
48             {
49                 log<level::ERR>("Unexpected FSI slave device name found",
50                                 entry("DEVICE_NAME=%d", path.c_str()));
51                 continue;
52             }
53 
54             path += "/raw";
55 
56             targets.push_back(std::make_unique<Target>(pos, path));
57         }
58     }
59 
60     auto sortTargets = [](const std::unique_ptr<Target>& left,
61                           const std::unique_ptr<Target>& right)
62     {
63         return left->getPos() < right->getPos();
64     };
65     std::sort(targets.begin(), targets.end(), sortTargets);
66 }
67 
68 }
69 }
70