blob: 72c2a957d374a71d22dd2d0010af0bcc48f80c5d [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
James Feist3b860982018-10-02 14:34:07 -070017#include <errno.h>
James Feist3cb5fec2018-01-23 14:41:51 -080018#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070019#include <sys/inotify.h>
20#include <sys/ioctl.h>
21
22#include <Utils.hpp>
23#include <boost/algorithm/string/predicate.hpp>
24#include <boost/container/flat_map.hpp>
25#include <chrono>
26#include <ctime>
James Feist3cb5fec2018-01-23 14:41:51 -080027#include <fstream>
28#include <future>
James Feist3cb5fec2018-01-23 14:41:51 -080029#include <iostream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070030#include <nlohmann/json.hpp>
James Feist3f8a2782018-02-12 09:24:42 -080031#include <regex>
James Feist3b860982018-10-02 14:34:07 -070032#include <sdbusplus/asio/connection.hpp>
33#include <sdbusplus/asio/object_server.hpp>
Patrick Venture11f1ff42019-08-01 10:42:12 -070034#include <string>
James Feist3b860982018-10-02 14:34:07 -070035#include <thread>
James Feista465ccc2019-02-08 12:51:01 -080036#include <variant>
James Feist3b860982018-10-02 14:34:07 -070037
38extern "C" {
39#include <i2c/smbus.h>
40#include <linux/i2c-dev.h>
41}
James Feist3cb5fec2018-01-23 14:41:51 -080042
Ed Tanous072e25d2018-12-16 21:45:20 -080043namespace fs = std::filesystem;
James Feist3cb5fec2018-01-23 14:41:51 -080044static constexpr bool DEBUG = false;
45static size_t UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feistb49ffc32018-05-02 11:10:43 -070046constexpr size_t MAX_FRU_SIZE = 512;
47constexpr size_t MAX_EEPROM_PAGE_INDEX = 255;
James Feist26c27ad2018-07-25 15:09:40 -070048constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080049
Patrick Venture11f1ff42019-08-01 10:42:12 -070050constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
51
James Feista465ccc2019-02-08 12:51:01 -080052const static constexpr char* BASEBOARD_FRU_LOCATION =
James Feist3cb5fec2018-01-23 14:41:51 -080053 "/etc/fru/baseboard.fru.bin";
54
James Feista465ccc2019-02-08 12:51:01 -080055const static constexpr char* I2C_DEV_LOCATION = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080056
James Feista465ccc2019-02-08 12:51:01 -080057static constexpr std::array<const char*, 5> FRU_AREAS = {
James Feist3cb5fec2018-01-23 14:41:51 -080058 "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"};
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -070059const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]");
James Feist3cb5fec2018-01-23 14:41:51 -080060using DeviceMap = boost::container::flat_map<int, std::vector<char>>;
61using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
62
James Feist444830e2019-04-05 08:38:16 -070063static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070064struct FindDevicesWithCallback;
65
Nikhil Potaded8884f12019-03-27 13:27:13 -070066static BusMap busMap;
67
James Feistc95cb142018-02-26 10:41:42 -080068static bool isMuxBus(size_t bus)
69{
Ed Tanous072e25d2018-12-16 21:45:20 -080070 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -080071 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
72}
73
Vijay Khemka2d681f62018-11-06 15:51:00 -080074static int isDevice16Bit(int file)
75{
76 /* Get first byte */
77 int byte1 = i2c_smbus_read_byte_data(file, 0);
78 if (byte1 < 0)
79 {
80 return byte1;
81 }
82 /* Read 7 more bytes, it will read same first byte in case of
83 * 8 bit but it will read next byte in case of 16 bit
84 */
85 for (int i = 0; i < 7; i++)
86 {
87 int byte2 = i2c_smbus_read_byte_data(file, 0);
88 if (byte2 < 0)
89 {
90 return byte2;
91 }
92 if (byte2 != byte1)
93 {
94 return 1;
95 }
96 }
97 return 0;
98}
99
100static int read_block_data(int flag, int file, uint16_t offset, uint8_t len,
James Feista465ccc2019-02-08 12:51:01 -0800101 uint8_t* buf)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800102{
James Feist98132792019-07-09 13:29:09 -0700103 uint8_t low_addr = static_cast<uint8_t>(offset);
104 uint8_t high_addr = static_cast<uint8_t>(offset >> 8);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800105
106 if (flag == 0)
107 {
108 return i2c_smbus_read_i2c_block_data(file, low_addr, len, buf);
109 }
110
111 /* This is for 16 bit addressing EEPROM device. First an offset
112 * needs to be written before read data from a offset
113 */
114 int ret = i2c_smbus_write_byte_data(file, 0, low_addr);
115 if (ret < 0)
116 {
117 return ret;
118 }
119
120 return i2c_smbus_read_i2c_block_data(file, high_addr, len, buf);
121}
122
James Feist24bae7a2019-04-03 09:50:56 -0700123bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData)
124{
125 // ipmi spec format version number is currently at 1, verify it
126 if (blockData[0] != 0x1)
127 {
128 return false;
129 }
130
131 // verify pad is set to 0
132 if (blockData[6] != 0x0)
133 {
134 return false;
135 }
136
137 // verify offsets are 0, or don't point to another offset
138 std::set<uint8_t> foundOffsets;
139 for (int ii = 1; ii < 6; ii++)
140 {
141 if (blockData[ii] == 0)
142 {
143 continue;
144 }
James Feist0eb40352019-04-09 14:44:04 -0700145 auto inserted = foundOffsets.insert(blockData[ii]);
146 if (!inserted.second)
James Feist24bae7a2019-04-03 09:50:56 -0700147 {
148 return false;
149 }
150 }
151
152 // validate checksum
153 size_t sum = 0;
154 for (int jj = 0; jj < 7; jj++)
155 {
156 sum += blockData[jj];
157 }
158 sum = (256 - sum) & 0xFF;
159
160 if (sum != blockData[7])
161 {
162 return false;
163 }
164 return true;
165}
166
James Feist3cb5fec2018-01-23 14:41:51 -0800167int get_bus_frus(int file, int first, int last, int bus,
168 std::shared_ptr<DeviceMap> devices)
169{
James Feist3cb5fec2018-01-23 14:41:51 -0800170
James Feist26c27ad2018-07-25 15:09:40 -0700171 std::future<int> future = std::async(std::launch::async, [&]() {
172 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> block_data;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800173
James Feist26c27ad2018-07-25 15:09:40 -0700174 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800175 {
James Feist3cb5fec2018-01-23 14:41:51 -0800176
James Feist26c27ad2018-07-25 15:09:40 -0700177 // Set slave address
178 if (ioctl(file, I2C_SLAVE_FORCE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800179 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700180 std::cerr << "device at bus " << bus << " register " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700181 << "busy\n";
182 continue;
183 }
184 // probe
185 else if (i2c_smbus_read_byte(file) < 0)
186 {
187 continue;
188 }
James Feist3cb5fec2018-01-23 14:41:51 -0800189
James Feist26c27ad2018-07-25 15:09:40 -0700190 if (DEBUG)
191 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700192 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700193 << "\n";
194 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800195
196 /* Check for Device type if it is 8 bit or 16 bit */
197 int flag = isDevice16Bit(file);
198 if (flag < 0)
199 {
200 std::cerr << "failed to read bus " << bus << " address " << ii
201 << "\n";
202 continue;
203 }
204
205 if (read_block_data(flag, file, 0x0, 0x8, block_data.data()) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700206 {
207 std::cerr << "failed to read bus " << bus << " address " << ii
208 << "\n";
209 continue;
210 }
James Feist26c27ad2018-07-25 15:09:40 -0700211
212 // check the header checksum
Patrick Venture786f1792019-08-05 16:33:44 -0700213 if (!validateHeader(block_data))
James Feist26c27ad2018-07-25 15:09:40 -0700214 {
Patrick Venture786f1792019-08-05 16:33:44 -0700215 if (DEBUG)
James Feist26c27ad2018-07-25 15:09:40 -0700216 {
Patrick Venture786f1792019-08-05 16:33:44 -0700217 std::cerr << "Illegal header at bus " << bus << " address "
218 << ii << "\n";
219 }
220 continue;
221 }
222
223 std::vector<char> device;
224 device.insert(device.end(), block_data.begin(),
225 block_data.begin() + 8);
226
227 for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++)
228 {
229 auto area_offset = device[jj];
Patrick Venture64fd7e22019-08-05 16:38:20 -0700230 if (area_offset == 0)
Patrick Venture786f1792019-08-05 16:33:44 -0700231 {
Patrick Venture64fd7e22019-08-05 16:38:20 -0700232 continue;
233 }
234
235 area_offset = static_cast<char>(area_offset * 8);
236 if (read_block_data(flag, file, area_offset, 0x8,
237 block_data.data()) < 0)
238 {
239 std::cerr << "failed to read bus " << bus << " address "
240 << ii << "\n";
241 return -1;
242 }
243 int length = block_data[1] * 8;
244 device.insert(device.end(), block_data.begin(),
245 block_data.begin() + 8);
246 length -= 8;
247 area_offset = static_cast<char>(area_offset + 8);
248
249 while (length > 0)
250 {
251 auto to_get = std::min(0x20, length);
252
253 if (read_block_data(flag, file, area_offset,
254 static_cast<uint8_t>(to_get),
Patrick Venture786f1792019-08-05 16:33:44 -0700255 block_data.data()) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800256 {
Patrick Venture786f1792019-08-05 16:33:44 -0700257 std::cerr << "failed to read bus " << bus << " address "
258 << ii << "\n";
259 return -1;
260 }
Patrick Venture786f1792019-08-05 16:33:44 -0700261 device.insert(device.end(), block_data.begin(),
Patrick Venture64fd7e22019-08-05 16:38:20 -0700262 block_data.begin() + to_get);
263 area_offset = static_cast<char>(area_offset + to_get);
264 length -= to_get;
James Feist3cb5fec2018-01-23 14:41:51 -0800265 }
266 }
Patrick Venture786f1792019-08-05 16:33:44 -0700267 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800268 }
James Feist26c27ad2018-07-25 15:09:40 -0700269 return 1;
270 });
271 std::future_status status =
272 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
273 if (status == std::future_status::timeout)
274 {
275 std::cerr << "Error reading bus " << bus << "\n";
James Feist444830e2019-04-05 08:38:16 -0700276 busBlacklist.insert(bus);
277 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700278 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800279 }
280
James Feist444830e2019-04-05 08:38:16 -0700281 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700282 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800283}
284
Patrick Venture11f1ff42019-08-01 10:42:12 -0700285void loadBlacklist(const char* path)
286{
287 std::ifstream blacklistStream(path);
288 if (!blacklistStream.good())
289 {
290 // File is optional.
291 std::cerr << "Cannot open blacklist file.\n\n";
292 return;
293 }
294
295 nlohmann::json data =
296 nlohmann::json::parse(blacklistStream, nullptr, false);
297 if (data.is_discarded())
298 {
299 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
300 "exiting\n";
301 std::exit(EXIT_FAILURE);
302 return;
303 }
304
305 // It's expected to have at least one field, "buses" that is an array of the
306 // buses by integer. Allow for future options to exclude further aspects,
307 // such as specific addresses or ranges.
308 if (data.type() != nlohmann::json::value_t::object)
309 {
310 std::cerr << "Illegal blacklist, expected to read dictionary\n";
311 std::exit(EXIT_FAILURE);
312 return;
313 }
314
315 // If buses field is missing, that's fine.
316 if (data.count("buses") == 1)
317 {
318 // Parse the buses array after a little validation.
319 auto buses = data.at("buses");
320 if (buses.type() != nlohmann::json::value_t::array)
321 {
322 // Buses field present but invalid, therefore this is an error.
323 std::cerr << "Invalid contents for blacklist buses field\n";
324 std::exit(EXIT_FAILURE);
325 return;
326 }
327
328 // Catch exception here for type mis-match.
329 try
330 {
331 for (const auto& bus : buses)
332 {
333 busBlacklist.insert(bus.get<size_t>());
334 }
335 }
336 catch (const nlohmann::detail::type_error& e)
337 {
338 // Type mis-match is a critical error.
339 std::cerr << "Invalid bus type: " << e.what() << "\n";
340 std::exit(EXIT_FAILURE);
341 return;
342 }
343 }
344
345 return;
346}
347
James Feista465ccc2019-02-08 12:51:01 -0800348static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700349 BusMap& busmap)
James Feist3cb5fec2018-01-23 14:41:51 -0800350{
James Feista465ccc2019-02-08 12:51:01 -0800351 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800352 {
353 auto busnum = i2cBus.string();
354 auto lastDash = busnum.rfind(std::string("-"));
355 // delete everything before dash inclusive
356 if (lastDash != std::string::npos)
357 {
358 busnum.erase(0, lastDash + 1);
359 }
360 auto bus = std::stoi(busnum);
James Feist444830e2019-04-05 08:38:16 -0700361 if (busBlacklist.find(bus) != busBlacklist.end())
362 {
363 continue; // skip previously failed busses
364 }
James Feist3cb5fec2018-01-23 14:41:51 -0800365
366 auto file = open(i2cBus.c_str(), O_RDWR);
367 if (file < 0)
368 {
369 std::cerr << "unable to open i2c device " << i2cBus.string()
370 << "\n";
371 continue;
372 }
373 unsigned long funcs = 0;
374
375 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
376 {
377 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700378 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800379 << bus << "\n";
380 continue;
381 }
382 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
383 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
384 {
385 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
386 << bus << "\n";
387 continue;
388 }
James Feist98132792019-07-09 13:29:09 -0700389 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800390 device = std::make_shared<DeviceMap>();
391
Nikhil Potaded8884f12019-03-27 13:27:13 -0700392 // i2cdetect by default uses the range 0x03 to 0x77, as
393 // this is what we have tested with, use this range. Could be
394 // changed in future.
395 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800396 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700397 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800398 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700399
400 // fd is closed in this function in case the bus locks up
401 get_bus_frus(file, 0x03, 0x77, bus, device);
402
403 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800404 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700405 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800406 }
James Feist3cb5fec2018-01-23 14:41:51 -0800407 }
James Feist3cb5fec2018-01-23 14:41:51 -0800408}
409
James Feist6ebf9de2018-05-15 15:01:17 -0700410// this class allows an async response after all i2c devices are discovered
411struct FindDevicesWithCallback
412 : std::enable_shared_from_this<FindDevicesWithCallback>
413{
James Feista465ccc2019-02-08 12:51:01 -0800414 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700415 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800416 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700417 _i2cBuses(i2cBuses),
James Feist98132792019-07-09 13:29:09 -0700418 _io(io), _busMap(busmap), _callback(std::move(callback))
James Feist6ebf9de2018-05-15 15:01:17 -0700419 {
420 }
421 ~FindDevicesWithCallback()
422 {
423 _callback();
424 }
425 void run()
426 {
James Feist98132792019-07-09 13:29:09 -0700427 FindI2CDevices(_i2cBuses, _busMap);
James Feist6ebf9de2018-05-15 15:01:17 -0700428 }
429
James Feista465ccc2019-02-08 12:51:01 -0800430 const std::vector<fs::path>& _i2cBuses;
431 boost::asio::io_service& _io;
432 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700433 std::function<void(void)> _callback;
434};
435
James Feist3cb5fec2018-01-23 14:41:51 -0800436static const std::tm intelEpoch(void)
437{
James Feist98132792019-07-09 13:29:09 -0700438 std::tm val = {};
James Feist3cb5fec2018-01-23 14:41:51 -0800439 val.tm_year = 1996 - 1900;
440 return val;
441}
442
James Feista465ccc2019-02-08 12:51:01 -0800443bool formatFru(const std::vector<char>& fruBytes,
444 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800445{
James Feista465ccc2019-02-08 12:51:01 -0800446 static const std::vector<const char*> CHASSIS_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800447 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800448
James Feista465ccc2019-02-08 12:51:01 -0800449 static const std::vector<const char*> BOARD_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800450 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
451 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800452
James Feista465ccc2019-02-08 12:51:01 -0800453 static const std::vector<const char*> PRODUCT_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800454 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
455 "VERSION", "SERIAL_NUMBER", "ASSET_TAG",
456 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800457
James Feistd068e932018-09-20 10:53:07 -0700458 if (fruBytes.size() <= 8)
James Feist3cb5fec2018-01-23 14:41:51 -0800459 {
460 return false;
461 }
462 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700463 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800464 std::to_string(static_cast<int>(*fruAreaOffsetField));
465
James Feista465ccc2019-02-08 12:51:01 -0800466 const std::vector<const char*>* fieldData;
James Feist3cb5fec2018-01-23 14:41:51 -0800467
James Feist0eb40352019-04-09 14:44:04 -0700468 for (const std::string& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800469 {
470 fruAreaOffsetField++;
471 if (fruAreaOffsetField >= fruBytes.end())
472 {
473 return false;
474 }
475 size_t offset = (*fruAreaOffsetField) * 8;
476
477 if (offset > 1)
478 {
479 // +2 to skip format and length
480 std::vector<char>::const_iterator fruBytesIter =
481 fruBytes.begin() + offset + 2;
482
483 if (fruBytesIter >= fruBytes.end())
484 {
485 return false;
486 }
487
488 if (area == "CHASSIS")
489 {
490 result["CHASSIS_TYPE"] =
491 std::to_string(static_cast<int>(*fruBytesIter));
492 fruBytesIter += 1;
493 fieldData = &CHASSIS_FRU_AREAS;
494 }
495 else if (area == "BOARD")
496 {
497 result["BOARD_LANGUAGE_CODE"] =
498 std::to_string(static_cast<int>(*fruBytesIter));
499 fruBytesIter += 1;
500 if (fruBytesIter >= fruBytes.end())
501 {
502 return false;
503 }
504
505 unsigned int minutes = *fruBytesIter |
506 *(fruBytesIter + 1) << 8 |
507 *(fruBytesIter + 2) << 16;
508 std::tm fruTime = intelEpoch();
509 time_t timeValue = mktime(&fruTime);
510 timeValue += minutes * 60;
511 fruTime = *gmtime(&timeValue);
512
513 result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
514 result["BOARD_MANUFACTURE_DATE"]
515 .pop_back(); // remove trailing null
516 fruBytesIter += 3;
517 fieldData = &BOARD_FRU_AREAS;
518 }
519 else if (area == "PRODUCT")
520 {
521 result["PRODUCT_LANGUAGE_CODE"] =
522 std::to_string(static_cast<int>(*fruBytesIter));
523 fruBytesIter += 1;
524 fieldData = &PRODUCT_FRU_AREAS;
525 }
526 else
527 {
528 continue;
529 }
James Feista465ccc2019-02-08 12:51:01 -0800530 for (auto& field : *fieldData)
James Feist3cb5fec2018-01-23 14:41:51 -0800531 {
532 if (fruBytesIter >= fruBytes.end())
533 {
534 return false;
535 }
536
Vijay Khemka5d5de442018-11-07 10:51:25 -0800537 /* Checking for last byte C1 to indicate that no more
538 * field to be read */
James Feist98132792019-07-09 13:29:09 -0700539 if (static_cast<uint8_t>(*fruBytesIter) == 0xC1)
Vijay Khemka5d5de442018-11-07 10:51:25 -0800540 {
541 break;
542 }
543
Ed Tanous2147e672019-02-27 13:59:56 -0800544 size_t length = *fruBytesIter & 0x3f;
545 fruBytesIter += 1;
546
James Feist3cb5fec2018-01-23 14:41:51 -0800547 if (fruBytesIter >= fruBytes.end())
548 {
549 return false;
550 }
Ed Tanous2147e672019-02-27 13:59:56 -0800551 std::string value(fruBytesIter, fruBytesIter + length);
James Feist3cb5fec2018-01-23 14:41:51 -0800552
Ed Tanous2147e672019-02-27 13:59:56 -0800553 // Strip non null characters from the end
554 value.erase(std::find_if(value.rbegin(), value.rend(),
555 [](char ch) { return ch != 0; })
556 .base(),
557 value.end());
558
James Feist0eb40352019-04-09 14:44:04 -0700559 result[area + "_" + field] = std::move(value);
Ed Tanous2147e672019-02-27 13:59:56 -0800560
James Feist3cb5fec2018-01-23 14:41:51 -0800561 fruBytesIter += length;
562 if (fruBytesIter >= fruBytes.end())
563 {
564 std::cerr << "Warning Fru Length Mismatch:\n ";
James Feista465ccc2019-02-08 12:51:01 -0800565 for (auto& c : fruBytes)
James Feist3cb5fec2018-01-23 14:41:51 -0800566 {
567 std::cerr << c;
568 }
569 std::cerr << "\n";
570 if (DEBUG)
571 {
James Feista465ccc2019-02-08 12:51:01 -0800572 for (auto& keyPair : result)
James Feist3cb5fec2018-01-23 14:41:51 -0800573 {
574 std::cerr << keyPair.first << " : "
575 << keyPair.second << "\n";
576 }
577 }
578 return false;
579 }
580 }
581 }
582 }
583
584 return true;
585}
586
Nikhil Potaded8884f12019-03-27 13:27:13 -0700587std::vector<uint8_t>& getFruInfo(const uint8_t& bus, const uint8_t& address)
588{
589 auto deviceMap = busMap.find(bus);
590 if (deviceMap == busMap.end())
591 {
592 throw std::invalid_argument("Invalid Bus.");
593 }
594 auto device = deviceMap->second->find(address);
595 if (device == deviceMap->second->end())
596 {
597 throw std::invalid_argument("Invalid Address.");
598 }
599 std::vector<uint8_t>& ret =
600 reinterpret_cast<std::vector<uint8_t>&>(device->second);
601
602 return ret;
603}
604
James Feist3cb5fec2018-01-23 14:41:51 -0800605void AddFruObjectToDbus(
James Feista465ccc2019-02-08 12:51:01 -0800606 std::vector<char>& device, sdbusplus::asio::object_server& objServer,
607 boost::container::flat_map<
608 std::pair<size_t, size_t>,
609 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700610 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -0800611{
612 boost::container::flat_map<std::string, std::string> formattedFru;
613 if (!formatFru(device, formattedFru))
614 {
615 std::cerr << "failed to format fru for device at bus " << std::hex
Nikhil Potaded8884f12019-03-27 13:27:13 -0700616 << bus << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800617 return;
618 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700619
James Feist3cb5fec2018-01-23 14:41:51 -0800620 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
621 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700622 // Not found under Board section or an empty string.
623 if (productNameFind == formattedFru.end() ||
624 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800625 {
626 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
627 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700628 // Found under Product section and not an empty string.
629 if (productNameFind != formattedFru.end() &&
630 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800631 {
632 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800633 std::regex illegalObject("[^A-Za-z0-9_]");
634 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800635 }
636 else
637 {
638 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
639 UNKNOWN_BUS_OBJECT_COUNT++;
640 }
641
James Feist918e18c2018-02-13 15:51:07 -0800642 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800643 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700644 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800645 {
James Feist79e9c0b2018-03-15 15:21:17 -0700646 size_t index = 0;
James Feista465ccc2019-02-08 12:51:01 -0800647 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800648 {
James Feist9eb0b582018-04-27 12:15:46 -0700649 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800650 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700651 if (isMuxBus(bus) && address == busIface.first.second &&
James Feist98132792019-07-09 13:29:09 -0700652 (getFruInfo(static_cast<uint8_t>(busIface.first.first),
653 static_cast<uint8_t>(busIface.first.second)) ==
654 getFruInfo(static_cast<uint8_t>(bus),
655 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700656 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700657 // This device is already added to the lower numbered bus,
658 // do not replicate it.
659 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700660 }
661 // add underscore _index for the same object path on dbus
662 std::string strIndex = std::to_string(index);
663 if (index > 0)
664 {
665 productName.substr(0, productName.size() - strIndex.size());
666 }
667 else
668 {
669 productName += "_";
670 }
671 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800672 }
673 }
674 }
James Feist3cb5fec2018-01-23 14:41:51 -0800675
James Feist9eb0b582018-04-27 12:15:46 -0700676 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
677 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
678 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
679
James Feista465ccc2019-02-08 12:51:01 -0800680 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800681 {
James Feist9eb0b582018-04-27 12:15:46 -0700682
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700683 std::regex_replace(property.second.begin(), property.second.begin(),
684 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700685 if (property.second.empty())
686 {
687 continue;
688 }
689 std::string key =
690 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
691 if (!iface->register_property(key, property.second + '\0'))
692 {
693 std::cerr << "illegal key: " << key << "\n";
694 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700695 if (DEBUG)
696 {
697 std::cout << property.first << ": " << property.second << "\n";
698 }
James Feist3cb5fec2018-01-23 14:41:51 -0800699 }
James Feist2a9d6db2018-04-27 15:48:28 -0700700
701 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700702 iface->register_property("BUS", bus);
703 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700704
James Feist9eb0b582018-04-27 12:15:46 -0700705 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800706}
707
James Feista465ccc2019-02-08 12:51:01 -0800708static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800709{
710 // try to read baseboard fru from file
711 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
712 if (baseboardFruFile.good())
713 {
714 baseboardFruFile.seekg(0, std::ios_base::end);
James Feist98132792019-07-09 13:29:09 -0700715 size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg());
James Feist3cb5fec2018-01-23 14:41:51 -0800716 baseboardFru.resize(fileSize);
717 baseboardFruFile.seekg(0, std::ios_base::beg);
718 baseboardFruFile.read(baseboardFru.data(), fileSize);
719 }
720 else
721 {
722 return false;
723 }
724 return true;
725}
726
James Feista465ccc2019-02-08 12:51:01 -0800727bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700728{
729 boost::container::flat_map<std::string, std::string> tmp;
730 if (fru.size() > MAX_FRU_SIZE)
731 {
732 std::cerr << "Invalid fru.size() during writeFru\n";
733 return false;
734 }
735 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -0800736 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -0700737 {
738 std::cerr << "Invalid fru format during writeFru\n";
739 return false;
740 }
741 // baseboard fru
742 if (bus == 0 && address == 0)
743 {
744 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
745 if (!file.good())
746 {
747 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
748 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700749 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700750 return false;
751 }
James Feista465ccc2019-02-08 12:51:01 -0800752 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700753 return file.good();
754 }
755 else
756 {
757 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
758
759 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
760 if (file < 0)
761 {
762 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -0700763 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700764 return false;
765 }
766 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
767 {
768 std::cerr << "unable to set device address\n";
769 close(file);
James Feistddb78302018-09-06 11:45:42 -0700770 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700771 return false;
772 }
773
774 constexpr const size_t RETRY_MAX = 2;
775 uint16_t index = 0;
776 size_t retries = RETRY_MAX;
777 while (index < fru.size())
778 {
779 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
780 (retries == RETRY_MAX))
781 {
782 // The 4K EEPROM only uses the A2 and A1 device address bits
783 // with the third bit being a memory page address bit.
784 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
785 {
786 std::cerr << "unable to set device address\n";
787 close(file);
James Feistddb78302018-09-06 11:45:42 -0700788 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700789 return false;
790 }
791 }
792
James Feist98132792019-07-09 13:29:09 -0700793 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
794 fru[index]) < 0)
James Feistb49ffc32018-05-02 11:10:43 -0700795 {
796 if (!retries--)
797 {
798 std::cerr << "error writing fru: " << strerror(errno)
799 << "\n";
800 close(file);
James Feistddb78302018-09-06 11:45:42 -0700801 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700802 return false;
803 }
804 }
805 else
806 {
807 retries = RETRY_MAX;
808 index++;
809 }
810 // most eeproms require 5-10ms between writes
811 std::this_thread::sleep_for(std::chrono::milliseconds(10));
812 }
813 close(file);
814 return true;
815 }
816}
817
James Feist9eb0b582018-04-27 12:15:46 -0700818void rescanBusses(
James Feist98132792019-07-09 13:29:09 -0700819 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800820 boost::container::flat_map<
821 std::pair<size_t, size_t>,
822 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feista465ccc2019-02-08 12:51:01 -0800823 sdbusplus::asio::object_server& objServer)
James Feist918e18c2018-02-13 15:51:07 -0800824{
James Feist6ebf9de2018-05-15 15:01:17 -0700825 static boost::asio::deadline_timer timer(io);
826 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800827
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500828 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -0700829 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -0800830 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800831 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800832
Nikhil Potaded8884f12019-03-27 13:27:13 -0700833 boost::container::flat_map<size_t, fs::path> busPaths;
834 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -0800835 {
James Feist4131aea2018-03-09 09:47:30 -0800836 std::cerr << "unable to find i2c devices\n";
837 return;
James Feist918e18c2018-02-13 15:51:07 -0800838 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700839
840 for (auto busPath : busPaths)
841 {
842 i2cBuses.emplace_back(busPath.second);
843 }
James Feist4131aea2018-03-09 09:47:30 -0800844
James Feist98132792019-07-09 13:29:09 -0700845 busmap.clear();
James Feist6ebf9de2018-05-15 15:01:17 -0700846 auto scan = std::make_shared<FindDevicesWithCallback>(
James Feist98132792019-07-09 13:29:09 -0700847 i2cBuses, io, busmap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -0800848 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700849 {
850 objServer.remove_interface(busIface.second);
851 }
James Feist4131aea2018-03-09 09:47:30 -0800852
James Feist6ebf9de2018-05-15 15:01:17 -0700853 dbusInterfaceMap.clear();
854 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -0800855
James Feist6ebf9de2018-05-15 15:01:17 -0700856 // todo, get this from a more sensable place
857 std::vector<char> baseboardFru;
858 if (readBaseboardFru(baseboardFru))
859 {
860 boost::container::flat_map<int, std::vector<char>>
861 baseboardDev;
862 baseboardDev.emplace(0, baseboardFru);
James Feist98132792019-07-09 13:29:09 -0700863 busmap[0] = std::make_shared<DeviceMap>(baseboardDev);
James Feist6ebf9de2018-05-15 15:01:17 -0700864 }
James Feist98132792019-07-09 13:29:09 -0700865 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -0700866 {
James Feista465ccc2019-02-08 12:51:01 -0800867 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -0700868 {
James Feist98132792019-07-09 13:29:09 -0700869 AddFruObjectToDbus(device.second, objServer,
James Feist6ebf9de2018-05-15 15:01:17 -0700870 dbusInterfaceMap, devicemap.first,
871 device.first);
872 }
873 }
874 });
875 scan->run();
876 });
James Feist918e18c2018-02-13 15:51:07 -0800877}
878
James Feist98132792019-07-09 13:29:09 -0700879int main()
James Feist3cb5fec2018-01-23 14:41:51 -0800880{
881 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -0800882 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -0800883 std::vector<fs::path> i2cBuses;
884
James Feista3c180a2018-08-09 16:06:04 -0700885 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -0800886 {
887 std::cerr << "unable to find i2c devices\n";
888 return 1;
889 }
James Feist3cb5fec2018-01-23 14:41:51 -0800890
Patrick Venture11f1ff42019-08-01 10:42:12 -0700891 // check for and load blacklist with initial buses.
892 loadBlacklist(blacklistPath);
893
James Feist3cb5fec2018-01-23 14:41:51 -0800894 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -0700895 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
896 auto objServer = sdbusplus::asio::object_server(systemBus);
Vijay Khemka065f6d92018-12-18 10:37:47 -0800897 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -0800898
James Feist6ebf9de2018-05-15 15:01:17 -0700899 // this is a map with keys of pair(bus number, address) and values of
900 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -0800901 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700902 std::shared_ptr<sdbusplus::asio::dbus_interface>>
903 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -0800904
James Feist9eb0b582018-04-27 12:15:46 -0700905 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
906 objServer.add_interface("/xyz/openbmc_project/FruDevice",
907 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -0800908
909 iface->register_method("ReScan", [&]() {
James Feist98132792019-07-09 13:29:09 -0700910 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -0800911 });
James Feist2a9d6db2018-04-27 15:48:28 -0700912
Nikhil Potaded8884f12019-03-27 13:27:13 -0700913 iface->register_method("GetRawFru", getFruInfo);
James Feistb49ffc32018-05-02 11:10:43 -0700914
915 iface->register_method("WriteFru", [&](const uint8_t bus,
916 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -0800917 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -0700918 if (!writeFru(bus, address, data))
919 {
James Feistddb78302018-09-06 11:45:42 -0700920 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -0700921 return;
922 }
923 // schedule rescan on success
James Feist98132792019-07-09 13:29:09 -0700924 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feistb49ffc32018-05-02 11:10:43 -0700925 });
James Feist9eb0b582018-04-27 12:15:46 -0700926 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800927
James Feist9eb0b582018-04-27 12:15:46 -0700928 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -0800929 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -0800930 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -0700931 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800932 std::string,
933 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -0700934 values;
935 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -0800936 auto findPgood = values.find("pgood");
937 if (findPgood != values.end())
938 {
James Feist6ebf9de2018-05-15 15:01:17 -0700939
James Feist98132792019-07-09 13:29:09 -0700940 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800941 }
James Feist918e18c2018-02-13 15:51:07 -0800942 };
James Feist9eb0b582018-04-27 12:15:46 -0700943
944 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -0800945 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -0700946 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
947 "openbmc_project/Chassis/Control/"
948 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -0700949 eventHandler);
950
James Feist4131aea2018-03-09 09:47:30 -0800951 int fd = inotify_init();
James Feist0eb40352019-04-09 14:44:04 -0700952 inotify_add_watch(fd, I2C_DEV_LOCATION,
953 IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -0800954 std::array<char, 4096> readBuffer;
955 std::string pendingBuffer;
956 // monitor for new i2c devices
957 boost::asio::posix::stream_descriptor dirWatch(io, fd);
958 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -0800959 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -0800960 std::size_t bytes_transferred) {
961 if (ec)
962 {
963 std::cout << "Callback Error " << ec << "\n";
964 return;
965 }
966 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
967 bool devChange = false;
968 while (pendingBuffer.size() > sizeof(inotify_event))
969 {
James Feista465ccc2019-02-08 12:51:01 -0800970 const inotify_event* iEvent =
971 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -0800972 pendingBuffer.data());
973 switch (iEvent->mask)
974 {
James Feist9eb0b582018-04-27 12:15:46 -0700975 case IN_CREATE:
976 case IN_MOVED_TO:
977 case IN_DELETE:
978 if (boost::starts_with(std::string(iEvent->name),
979 "i2c"))
980 {
981 devChange = true;
982 }
James Feist4131aea2018-03-09 09:47:30 -0800983 }
984
985 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
986 }
James Feist6ebf9de2018-05-15 15:01:17 -0700987 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -0800988 {
James Feist98132792019-07-09 13:29:09 -0700989 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist4131aea2018-03-09 09:47:30 -0800990 }
James Feist6ebf9de2018-05-15 15:01:17 -0700991
James Feist4131aea2018-03-09 09:47:30 -0800992 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
993 watchI2cBusses);
994 };
995
996 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -0500997 // run the initial scan
James Feist98132792019-07-09 13:29:09 -0700998 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800999
James Feist3cb5fec2018-01-23 14:41:51 -08001000 io.run();
1001 return 0;
1002}