blob: 4b993d3d07f4f0f24eb78ecfbc70e21cb5a44af0 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
2// Copyright (c) 2018 Intel 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
17#include <Utils.hpp>
18#include <boost/container/flat_map.hpp>
19#include <ctime>
James Feist9eb0b582018-04-27 12:15:46 -070020#include <sdbusplus/asio/connection.hpp>
21#include <sdbusplus/asio/object_server.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080022#include <dbus/properties.hpp>
23#include <fcntl.h>
24#include <fstream>
25#include <future>
James Feistb5320a72018-01-24 12:28:12 -080026#include <linux/i2c-dev-user.h>
James Feist3cb5fec2018-01-23 14:41:51 -080027#include <iostream>
28#include <sys/ioctl.h>
James Feist3f8a2782018-02-12 09:24:42 -080029#include <regex>
James Feist4131aea2018-03-09 09:47:30 -080030#include <sys/inotify.h>
James Feist2a9d6db2018-04-27 15:48:28 -070031#include <xyz/openbmc_project/Common/error.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080032
33namespace fs = std::experimental::filesystem;
34static constexpr bool DEBUG = false;
35static size_t UNKNOWN_BUS_OBJECT_COUNT = 0;
36
37const static constexpr char *BASEBOARD_FRU_LOCATION =
38 "/etc/fru/baseboard.fru.bin";
39
James Feist4131aea2018-03-09 09:47:30 -080040const static constexpr char *I2C_DEV_LOCATION = "/dev";
41
James Feist3cb5fec2018-01-23 14:41:51 -080042static constexpr std::array<const char *, 5> FRU_AREAS = {
43 "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"};
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -070044const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]");
James Feist3cb5fec2018-01-23 14:41:51 -080045using DeviceMap = boost::container::flat_map<int, std::vector<char>>;
46using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
47
James Feist6ebf9de2018-05-15 15:01:17 -070048struct FindDevicesWithCallback;
49
James Feistc95cb142018-02-26 10:41:42 -080050static bool isMuxBus(size_t bus)
51{
52 return is_symlink(std::experimental::filesystem::path(
53 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
54}
55
James Feist3cb5fec2018-01-23 14:41:51 -080056int get_bus_frus(int file, int first, int last, int bus,
57 std::shared_ptr<DeviceMap> devices)
58{
59 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> block_data;
60
61 for (int ii = first; ii <= last; ii++)
62 {
63 // Set slave address
64 if (ioctl(file, I2C_SLAVE_FORCE, ii) < 0)
65 {
66 std::cerr << "device at bus " << bus << "register " << ii
67 << "busy\n";
68 continue;
69 }
70 // probe
71 else if (i2c_smbus_read_byte(file) < 0)
72 {
73 continue;
74 }
75
76 if (DEBUG)
77 {
78 std::cout << "something at bus " << bus << "addr " << ii << "\n";
79 }
80 if (i2c_smbus_read_i2c_block_data(file, 0x0, 0x8, block_data.data()) <
81 0)
82 {
83 std::cerr << "failed to read bus " << bus << " address " << ii
84 << "\n";
85 continue;
86 }
87 size_t sum = 0;
88 for (int jj = 0; jj < 7; jj++)
89 {
90 sum += block_data[jj];
91 }
92 sum = (256 - sum) & 0xFF;
93
94 // check the header checksum
95 if (sum == block_data[7])
96 {
97 std::vector<char> device;
98 device.insert(device.end(), block_data.begin(),
99 block_data.begin() + 8);
100
101 for (int jj = 1; jj <= FRU_AREAS.size(); jj++)
102 {
103 auto area_offset = device[jj];
104 if (area_offset != 0)
105 {
106 area_offset *= 8;
107 if (i2c_smbus_read_i2c_block_data(file, area_offset, 0x8,
108 block_data.data()) < 0)
109 {
110 std::cerr << "failed to read bus " << bus << " address "
111 << ii << "\n";
112 return -1;
113 }
114 int length = block_data[1] * 8;
115 device.insert(device.end(), block_data.begin(),
116 block_data.begin() + 8);
117 length -= 8;
118 area_offset += 8;
119
120 while (length > 0)
121 {
122 auto to_get = std::min(0x20, length);
123 if (i2c_smbus_read_i2c_block_data(
124 file, area_offset, to_get, block_data.data()) <
125 0)
126 {
127 std::cerr << "failed to read bus " << bus
128 << " address " << ii << "\n";
129 return -1;
130 }
131 device.insert(device.end(), block_data.begin(),
132 block_data.begin() + to_get);
133 area_offset += to_get;
134 length -= to_get;
135 }
136 }
137 }
138 (*devices).emplace(ii, device);
139 }
140 }
141
142 return 0;
143}
144
James Feist6ebf9de2018-05-15 15:01:17 -0700145static void FindI2CDevices(const std::vector<fs::path> &i2cBuses,
146 std::shared_ptr<FindDevicesWithCallback> context,
147 boost::asio::io_service &io, BusMap &busMap)
James Feist3cb5fec2018-01-23 14:41:51 -0800148{
James Feist3cb5fec2018-01-23 14:41:51 -0800149 for (auto &i2cBus : i2cBuses)
150 {
151 auto busnum = i2cBus.string();
152 auto lastDash = busnum.rfind(std::string("-"));
153 // delete everything before dash inclusive
154 if (lastDash != std::string::npos)
155 {
156 busnum.erase(0, lastDash + 1);
157 }
158 auto bus = std::stoi(busnum);
159
160 auto file = open(i2cBus.c_str(), O_RDWR);
161 if (file < 0)
162 {
163 std::cerr << "unable to open i2c device " << i2cBus.string()
164 << "\n";
165 continue;
166 }
167 unsigned long funcs = 0;
168
169 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
170 {
171 std::cerr
172 << "Error: Could not get the adapter functionality matrix bus"
173 << bus << "\n";
174 continue;
175 }
176 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
177 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
178 {
179 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
180 << bus << "\n";
181 continue;
182 }
183 auto &device = busMap[bus];
184 device = std::make_shared<DeviceMap>();
185
James Feistc95cb142018-02-26 10:41:42 -0800186 // don't scan muxed buses async as don't want to confuse the mux
187 if (isMuxBus(bus))
188 {
189 get_bus_frus(file, 0x03, 0x77, bus, device);
190 close(file);
191 }
192 else
193 {
James Feist6ebf9de2018-05-15 15:01:17 -0700194 io.post([file, device, bus, context] {
195 // i2cdetect by default uses the range 0x03 to 0x77, as
196 // this is
197 // what we
198 // have tested with, use this range. Could be changed in
199 // future.
200 get_bus_frus(file, 0x03, 0x77, bus, device);
201 close(file);
202 });
James Feistc95cb142018-02-26 10:41:42 -0800203 }
James Feist3cb5fec2018-01-23 14:41:51 -0800204 }
James Feist3cb5fec2018-01-23 14:41:51 -0800205}
206
James Feist6ebf9de2018-05-15 15:01:17 -0700207// this class allows an async response after all i2c devices are discovered
208struct FindDevicesWithCallback
209 : std::enable_shared_from_this<FindDevicesWithCallback>
210{
211 FindDevicesWithCallback(const std::vector<fs::path> &i2cBuses,
212 boost::asio::io_service &io, BusMap &busMap,
213 std::function<void(void)> &&callback) :
214 _i2cBuses(i2cBuses),
215 _io(io), _busMap(busMap), _callback(std::move(callback))
216 {
217 }
218 ~FindDevicesWithCallback()
219 {
220 _callback();
221 }
222 void run()
223 {
224 FindI2CDevices(_i2cBuses, shared_from_this(), _io, _busMap);
225 }
226
227 const std::vector<fs::path> &_i2cBuses;
228 boost::asio::io_service &_io;
229 BusMap &_busMap;
230 std::function<void(void)> _callback;
231};
232
James Feist3cb5fec2018-01-23 14:41:51 -0800233static const std::tm intelEpoch(void)
234{
235 std::tm val = {0};
236 val.tm_year = 1996 - 1900;
237 return val;
238}
239
240bool formatFru(const std::vector<char> &fruBytes,
241 boost::container::flat_map<std::string, std::string> &result)
242{
243 static const std::vector<const char *> CHASSIS_FRU_AREAS = {
244 "PART_NUMBER", "SERIAL_NUMBER", "CHASSIS_INFO_AM1", "CHASSIS_INFO_AM2"};
245
246 static const std::vector<const char *> BOARD_FRU_AREAS = {
247 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
248 "VERSION_ID"};
249
250 static const std::vector<const char *> PRODUCT_FRU_AREAS = {
251 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
252 "PRODUCT_VERSION", "PRODUCT_SERIAL_NUMBER", "ASSET_TAG"};
253
254 size_t sum = 0;
255
256 if (fruBytes.size() < 8)
257 {
258 return false;
259 }
260 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700261 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800262 std::to_string(static_cast<int>(*fruAreaOffsetField));
263
264 const std::vector<const char *> *fieldData;
265
266 for (auto &area : FRU_AREAS)
267 {
268 fruAreaOffsetField++;
269 if (fruAreaOffsetField >= fruBytes.end())
270 {
271 return false;
272 }
273 size_t offset = (*fruAreaOffsetField) * 8;
274
275 if (offset > 1)
276 {
277 // +2 to skip format and length
278 std::vector<char>::const_iterator fruBytesIter =
279 fruBytes.begin() + offset + 2;
280
281 if (fruBytesIter >= fruBytes.end())
282 {
283 return false;
284 }
285
286 if (area == "CHASSIS")
287 {
288 result["CHASSIS_TYPE"] =
289 std::to_string(static_cast<int>(*fruBytesIter));
290 fruBytesIter += 1;
291 fieldData = &CHASSIS_FRU_AREAS;
292 }
293 else if (area == "BOARD")
294 {
295 result["BOARD_LANGUAGE_CODE"] =
296 std::to_string(static_cast<int>(*fruBytesIter));
297 fruBytesIter += 1;
298 if (fruBytesIter >= fruBytes.end())
299 {
300 return false;
301 }
302
303 unsigned int minutes = *fruBytesIter |
304 *(fruBytesIter + 1) << 8 |
305 *(fruBytesIter + 2) << 16;
306 std::tm fruTime = intelEpoch();
307 time_t timeValue = mktime(&fruTime);
308 timeValue += minutes * 60;
309 fruTime = *gmtime(&timeValue);
310
311 result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
312 result["BOARD_MANUFACTURE_DATE"]
313 .pop_back(); // remove trailing null
314 fruBytesIter += 3;
315 fieldData = &BOARD_FRU_AREAS;
316 }
317 else if (area == "PRODUCT")
318 {
319 result["PRODUCT_LANGUAGE_CODE"] =
320 std::to_string(static_cast<int>(*fruBytesIter));
321 fruBytesIter += 1;
322 fieldData = &PRODUCT_FRU_AREAS;
323 }
324 else
325 {
326 continue;
327 }
328 for (auto &field : *fieldData)
329 {
330 if (fruBytesIter >= fruBytes.end())
331 {
332 return false;
333 }
334
335 size_t length = *fruBytesIter & 0x3f;
336 fruBytesIter += 1;
337
338 if (fruBytesIter >= fruBytes.end())
339 {
340 return false;
341 }
342
343 result[std::string(area) + "_" + field] =
344 std::string(fruBytesIter, fruBytesIter + length);
345 fruBytesIter += length;
346 if (fruBytesIter >= fruBytes.end())
347 {
348 std::cerr << "Warning Fru Length Mismatch:\n ";
349 for (auto &c : fruBytes)
350 {
351 std::cerr << c;
352 }
353 std::cerr << "\n";
354 if (DEBUG)
355 {
356 for (auto &keyPair : result)
357 {
358 std::cerr << keyPair.first << " : "
359 << keyPair.second << "\n";
360 }
361 }
362 return false;
363 }
364 }
365 }
366 }
367
368 return true;
369}
370
371void AddFruObjectToDbus(
James Feist9eb0b582018-04-27 12:15:46 -0700372 std::shared_ptr<sdbusplus::asio::connection> dbusConn,
373 std::vector<char> &device, sdbusplus::asio::object_server &objServer,
James Feist3cb5fec2018-01-23 14:41:51 -0800374 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700375 std::shared_ptr<sdbusplus::asio::dbus_interface>>
376 &dbusInterfaceMap,
James Feist3cb5fec2018-01-23 14:41:51 -0800377 int bus, size_t address)
378{
379 boost::container::flat_map<std::string, std::string> formattedFru;
380 if (!formatFru(device, formattedFru))
381 {
382 std::cerr << "failed to format fru for device at bus " << std::hex
383 << bus << "address " << address << "\n";
384 return;
385 }
386 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
387 std::string productName;
388 if (productNameFind == formattedFru.end())
389 {
390 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
391 }
392 if (productNameFind != formattedFru.end())
393 {
394 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800395 std::regex illegalObject("[^A-Za-z0-9_]");
396 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800397 }
398 else
399 {
400 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
401 UNKNOWN_BUS_OBJECT_COUNT++;
402 }
403
James Feist918e18c2018-02-13 15:51:07 -0800404 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800405 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700406 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800407 {
James Feist79e9c0b2018-03-15 15:21:17 -0700408 size_t index = 0;
James Feist9eb0b582018-04-27 12:15:46 -0700409 for (auto const &busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800410 {
James Feist9eb0b582018-04-27 12:15:46 -0700411 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800412 {
James Feist9eb0b582018-04-27 12:15:46 -0700413 if (isMuxBus(bus) && address == busIface.first.second)
James Feist79e9c0b2018-03-15 15:21:17 -0700414 {
415 continue;
416 }
417 // add underscore _index for the same object path on dbus
418 std::string strIndex = std::to_string(index);
419 if (index > 0)
420 {
421 productName.substr(0, productName.size() - strIndex.size());
422 }
423 else
424 {
425 productName += "_";
426 }
427 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800428 }
429 }
430 }
James Feist3cb5fec2018-01-23 14:41:51 -0800431
James Feist9eb0b582018-04-27 12:15:46 -0700432 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
433 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
434 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
435
James Feist3cb5fec2018-01-23 14:41:51 -0800436 for (auto &property : formattedFru)
437 {
James Feist9eb0b582018-04-27 12:15:46 -0700438
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700439 std::regex_replace(property.second.begin(), property.second.begin(),
440 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700441 if (property.second.empty())
442 {
443 continue;
444 }
445 std::string key =
446 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
447 if (!iface->register_property(key, property.second + '\0'))
448 {
449 std::cerr << "illegal key: " << key << "\n";
450 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700451 if (DEBUG)
452 {
453 std::cout << property.first << ": " << property.second << "\n";
454 }
James Feist3cb5fec2018-01-23 14:41:51 -0800455 }
James Feist2a9d6db2018-04-27 15:48:28 -0700456
457 // baseboard will be 0, 0
458 std::stringstream data;
459 data << "0x" << std::hex << bus;
460 iface->register_property("BUS", data.str());
461 data.str("");
462 data << "0x" << std::hex << address;
463 iface->register_property("ADDRESS", data.str());
464
James Feist9eb0b582018-04-27 12:15:46 -0700465 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800466}
467
468static bool readBaseboardFru(std::vector<char> &baseboardFru)
469{
470 // try to read baseboard fru from file
471 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
472 if (baseboardFruFile.good())
473 {
474 baseboardFruFile.seekg(0, std::ios_base::end);
475 std::streampos fileSize = baseboardFruFile.tellg();
476 baseboardFru.resize(fileSize);
477 baseboardFruFile.seekg(0, std::ios_base::beg);
478 baseboardFruFile.read(baseboardFru.data(), fileSize);
479 }
480 else
481 {
482 return false;
483 }
484 return true;
485}
486
James Feist9eb0b582018-04-27 12:15:46 -0700487void rescanBusses(
James Feist6ebf9de2018-05-15 15:01:17 -0700488 boost::asio::io_service &io, BusMap &busMap,
James Feist9eb0b582018-04-27 12:15:46 -0700489 boost::container::flat_map<std::pair<size_t, size_t>,
490 std::shared_ptr<sdbusplus::asio::dbus_interface>>
491 &dbusInterfaceMap,
James Feist6ebf9de2018-05-15 15:01:17 -0700492 std::shared_ptr<sdbusplus::asio::connection> &systemBus,
493 sdbusplus::asio::object_server &objServer)
James Feist918e18c2018-02-13 15:51:07 -0800494{
James Feist6ebf9de2018-05-15 15:01:17 -0700495 static boost::asio::deadline_timer timer(io);
496 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800497
James Feist6ebf9de2018-05-15 15:01:17 -0700498 // setup an async wait incase we get flooded with requests
499 timer.async_wait([&](const boost::system::error_code &ec) {
James Feist4131aea2018-03-09 09:47:30 -0800500 auto devDir = fs::path("/dev/");
501 auto matchString = std::string("i2c*");
502 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800503
James Feist4131aea2018-03-09 09:47:30 -0800504 if (!find_files(devDir, matchString, i2cBuses, 0))
James Feist918e18c2018-02-13 15:51:07 -0800505 {
James Feist4131aea2018-03-09 09:47:30 -0800506 std::cerr << "unable to find i2c devices\n";
507 return;
James Feist918e18c2018-02-13 15:51:07 -0800508 }
James Feist4131aea2018-03-09 09:47:30 -0800509 // scanning muxes in reverse order seems to have adverse effects
510 // sorting this list seems to be a fix for that
511 std::sort(i2cBuses.begin(), i2cBuses.end());
James Feist4131aea2018-03-09 09:47:30 -0800512
James Feist6ebf9de2018-05-15 15:01:17 -0700513 busMap.clear();
514 auto scan = std::make_shared<FindDevicesWithCallback>(
515 i2cBuses, io, busMap, [&]() {
516 for (auto &busIface : dbusInterfaceMap)
517 {
518 objServer.remove_interface(busIface.second);
519 }
James Feist4131aea2018-03-09 09:47:30 -0800520
James Feist6ebf9de2018-05-15 15:01:17 -0700521 dbusInterfaceMap.clear();
522 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -0800523
James Feist6ebf9de2018-05-15 15:01:17 -0700524 // todo, get this from a more sensable place
525 std::vector<char> baseboardFru;
526 if (readBaseboardFru(baseboardFru))
527 {
528 boost::container::flat_map<int, std::vector<char>>
529 baseboardDev;
530 baseboardDev.emplace(0, baseboardFru);
531 busMap[0] = std::make_shared<DeviceMap>(baseboardDev);
532 }
533 for (auto &devicemap : busMap)
534 {
535 for (auto &device : *devicemap.second)
536 {
537 AddFruObjectToDbus(systemBus, device.second, objServer,
538 dbusInterfaceMap, devicemap.first,
539 device.first);
540 }
541 }
542 });
543 scan->run();
544 });
James Feist918e18c2018-02-13 15:51:07 -0800545}
546
James Feist3cb5fec2018-01-23 14:41:51 -0800547int main(int argc, char **argv)
548{
549 auto devDir = fs::path("/dev/");
550 auto matchString = std::string("i2c*");
551 std::vector<fs::path> i2cBuses;
552
553 if (!find_files(devDir, matchString, i2cBuses, 0))
554 {
555 std::cerr << "unable to find i2c devices\n";
556 return 1;
557 }
James Feist3cb5fec2018-01-23 14:41:51 -0800558
559 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -0700560 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
561 auto objServer = sdbusplus::asio::object_server(systemBus);
James Feist3cb5fec2018-01-23 14:41:51 -0800562 systemBus->request_name("com.intel.FruDevice");
563
James Feist6ebf9de2018-05-15 15:01:17 -0700564 // this is a map with keys of pair(bus number, address) and values of
565 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -0800566 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700567 std::shared_ptr<sdbusplus::asio::dbus_interface>>
568 dbusInterfaceMap;
James Feist2a9d6db2018-04-27 15:48:28 -0700569 BusMap busmap;
James Feist3cb5fec2018-01-23 14:41:51 -0800570
James Feist9eb0b582018-04-27 12:15:46 -0700571 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
572 objServer.add_interface("/xyz/openbmc_project/FruDevice",
573 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -0800574
575 iface->register_method("ReScan", [&]() {
James Feist6ebf9de2018-05-15 15:01:17 -0700576 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -0800577 });
James Feist2a9d6db2018-04-27 15:48:28 -0700578
579 iface->register_method(
580 "GetRawFru", [&](const uint8_t &bus, const uint8_t &address) {
581 auto deviceMap = busmap.find(bus);
582 if (deviceMap == busmap.end())
583 {
584 throw sdbusplus::xyz::openbmc_project::Common::Error::
585 InvalidArgument();
586 }
587 auto device = deviceMap->second->find(address);
588 if (device == deviceMap->second->end())
589 {
590 throw sdbusplus::xyz::openbmc_project::Common::Error::
591 InvalidArgument();
592 }
593 std::vector<uint8_t> &ret =
594 reinterpret_cast<std::vector<uint8_t> &>(device->second);
595 return ret;
596 });
James Feist9eb0b582018-04-27 12:15:46 -0700597 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800598
James Feist9eb0b582018-04-27 12:15:46 -0700599 std::function<void(sdbusplus::message::message & message)> eventHandler =
600 [&](sdbusplus::message::message &message) {
James Feist918e18c2018-02-13 15:51:07 -0800601 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -0700602 boost::container::flat_map<
603 std::string, sdbusplus::message::variant<
604 std::string, bool, int64_t, uint64_t, double>>
605 values;
606 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -0800607 auto findPgood = values.find("pgood");
608 if (findPgood != values.end())
609 {
James Feist6ebf9de2018-05-15 15:01:17 -0700610
611 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
612 objServer);
James Feist918e18c2018-02-13 15:51:07 -0800613 }
James Feist918e18c2018-02-13 15:51:07 -0800614 };
James Feist9eb0b582018-04-27 12:15:46 -0700615
616 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
617 static_cast<sdbusplus::bus::bus &>(*systemBus),
618 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
Kuiying Wang1dc43102018-05-09 15:09:29 +0800619 "namespace='/xyz/openbmc_project/Chassis/Control/"
620 "power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -0700621 eventHandler);
622
James Feist4131aea2018-03-09 09:47:30 -0800623 int fd = inotify_init();
624 int wd = inotify_add_watch(fd, I2C_DEV_LOCATION,
625 IN_CREATE | IN_MOVED_TO | IN_DELETE);
626 std::array<char, 4096> readBuffer;
627 std::string pendingBuffer;
628 // monitor for new i2c devices
629 boost::asio::posix::stream_descriptor dirWatch(io, fd);
630 std::function<void(const boost::system::error_code, std::size_t)>
631 watchI2cBusses = [&](const boost::system::error_code &ec,
632 std::size_t bytes_transferred) {
633 if (ec)
634 {
635 std::cout << "Callback Error " << ec << "\n";
636 return;
637 }
638 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
639 bool devChange = false;
640 while (pendingBuffer.size() > sizeof(inotify_event))
641 {
642 const inotify_event *iEvent =
643 reinterpret_cast<const inotify_event *>(
644 pendingBuffer.data());
645 switch (iEvent->mask)
646 {
James Feist9eb0b582018-04-27 12:15:46 -0700647 case IN_CREATE:
648 case IN_MOVED_TO:
649 case IN_DELETE:
650 if (boost::starts_with(std::string(iEvent->name),
651 "i2c"))
652 {
653 devChange = true;
654 }
James Feist4131aea2018-03-09 09:47:30 -0800655 }
656
657 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
658 }
James Feist6ebf9de2018-05-15 15:01:17 -0700659 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -0800660 {
James Feist6ebf9de2018-05-15 15:01:17 -0700661 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
662 objServer);
James Feist4131aea2018-03-09 09:47:30 -0800663 }
James Feist6ebf9de2018-05-15 15:01:17 -0700664
James Feist4131aea2018-03-09 09:47:30 -0800665 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
666 watchI2cBusses);
667 };
668
669 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -0500670 // run the initial scan
James Feist6ebf9de2018-05-15 15:01:17 -0700671 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800672
James Feist3cb5fec2018-01-23 14:41:51 -0800673 io.run();
674 return 0;
675}