blob: b09b8da52ece971852e307557b61d5a1bfedaf65 [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];
230 if (area_offset != 0)
231 {
232 area_offset = static_cast<char>(area_offset * 8);
233 if (read_block_data(flag, file, area_offset, 0x8,
234 block_data.data()) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800235 {
Patrick Venture786f1792019-08-05 16:33:44 -0700236 std::cerr << "failed to read bus " << bus << " address "
237 << ii << "\n";
238 return -1;
239 }
240 int length = block_data[1] * 8;
241 device.insert(device.end(), block_data.begin(),
242 block_data.begin() + 8);
243 length -= 8;
244 area_offset = static_cast<char>(area_offset + 8);
245
246 while (length > 0)
247 {
248 auto to_get = std::min(0x20, length);
249
250 if (read_block_data(flag, file, area_offset,
251 static_cast<uint8_t>(to_get),
Vijay Khemka2d681f62018-11-06 15:51:00 -0800252 block_data.data()) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800253 {
254 std::cerr << "failed to read bus " << bus
255 << " address " << ii << "\n";
256 return -1;
257 }
258 device.insert(device.end(), block_data.begin(),
Patrick Venture786f1792019-08-05 16:33:44 -0700259 block_data.begin() + to_get);
260 area_offset = static_cast<char>(area_offset + to_get);
261 length -= to_get;
James Feist3cb5fec2018-01-23 14:41:51 -0800262 }
263 }
264 }
Patrick Venture786f1792019-08-05 16:33:44 -0700265 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800266 }
James Feist26c27ad2018-07-25 15:09:40 -0700267 return 1;
268 });
269 std::future_status status =
270 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
271 if (status == std::future_status::timeout)
272 {
273 std::cerr << "Error reading bus " << bus << "\n";
James Feist444830e2019-04-05 08:38:16 -0700274 busBlacklist.insert(bus);
275 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700276 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800277 }
278
James Feist444830e2019-04-05 08:38:16 -0700279 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700280 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800281}
282
Patrick Venture11f1ff42019-08-01 10:42:12 -0700283void loadBlacklist(const char* path)
284{
285 std::ifstream blacklistStream(path);
286 if (!blacklistStream.good())
287 {
288 // File is optional.
289 std::cerr << "Cannot open blacklist file.\n\n";
290 return;
291 }
292
293 nlohmann::json data =
294 nlohmann::json::parse(blacklistStream, nullptr, false);
295 if (data.is_discarded())
296 {
297 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
298 "exiting\n";
299 std::exit(EXIT_FAILURE);
300 return;
301 }
302
303 // It's expected to have at least one field, "buses" that is an array of the
304 // buses by integer. Allow for future options to exclude further aspects,
305 // such as specific addresses or ranges.
306 if (data.type() != nlohmann::json::value_t::object)
307 {
308 std::cerr << "Illegal blacklist, expected to read dictionary\n";
309 std::exit(EXIT_FAILURE);
310 return;
311 }
312
313 // If buses field is missing, that's fine.
314 if (data.count("buses") == 1)
315 {
316 // Parse the buses array after a little validation.
317 auto buses = data.at("buses");
318 if (buses.type() != nlohmann::json::value_t::array)
319 {
320 // Buses field present but invalid, therefore this is an error.
321 std::cerr << "Invalid contents for blacklist buses field\n";
322 std::exit(EXIT_FAILURE);
323 return;
324 }
325
326 // Catch exception here for type mis-match.
327 try
328 {
329 for (const auto& bus : buses)
330 {
331 busBlacklist.insert(bus.get<size_t>());
332 }
333 }
334 catch (const nlohmann::detail::type_error& e)
335 {
336 // Type mis-match is a critical error.
337 std::cerr << "Invalid bus type: " << e.what() << "\n";
338 std::exit(EXIT_FAILURE);
339 return;
340 }
341 }
342
343 return;
344}
345
James Feista465ccc2019-02-08 12:51:01 -0800346static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700347 BusMap& busmap)
James Feist3cb5fec2018-01-23 14:41:51 -0800348{
James Feista465ccc2019-02-08 12:51:01 -0800349 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800350 {
351 auto busnum = i2cBus.string();
352 auto lastDash = busnum.rfind(std::string("-"));
353 // delete everything before dash inclusive
354 if (lastDash != std::string::npos)
355 {
356 busnum.erase(0, lastDash + 1);
357 }
358 auto bus = std::stoi(busnum);
James Feist444830e2019-04-05 08:38:16 -0700359 if (busBlacklist.find(bus) != busBlacklist.end())
360 {
361 continue; // skip previously failed busses
362 }
James Feist3cb5fec2018-01-23 14:41:51 -0800363
364 auto file = open(i2cBus.c_str(), O_RDWR);
365 if (file < 0)
366 {
367 std::cerr << "unable to open i2c device " << i2cBus.string()
368 << "\n";
369 continue;
370 }
371 unsigned long funcs = 0;
372
373 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
374 {
375 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700376 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800377 << bus << "\n";
378 continue;
379 }
380 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
381 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
382 {
383 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
384 << bus << "\n";
385 continue;
386 }
James Feist98132792019-07-09 13:29:09 -0700387 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800388 device = std::make_shared<DeviceMap>();
389
Nikhil Potaded8884f12019-03-27 13:27:13 -0700390 // i2cdetect by default uses the range 0x03 to 0x77, as
391 // this is what we have tested with, use this range. Could be
392 // changed in future.
393 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800394 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700395 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800396 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700397
398 // fd is closed in this function in case the bus locks up
399 get_bus_frus(file, 0x03, 0x77, bus, device);
400
401 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800402 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700403 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800404 }
James Feist3cb5fec2018-01-23 14:41:51 -0800405 }
James Feist3cb5fec2018-01-23 14:41:51 -0800406}
407
James Feist6ebf9de2018-05-15 15:01:17 -0700408// this class allows an async response after all i2c devices are discovered
409struct FindDevicesWithCallback
410 : std::enable_shared_from_this<FindDevicesWithCallback>
411{
James Feista465ccc2019-02-08 12:51:01 -0800412 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700413 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800414 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700415 _i2cBuses(i2cBuses),
James Feist98132792019-07-09 13:29:09 -0700416 _io(io), _busMap(busmap), _callback(std::move(callback))
James Feist6ebf9de2018-05-15 15:01:17 -0700417 {
418 }
419 ~FindDevicesWithCallback()
420 {
421 _callback();
422 }
423 void run()
424 {
James Feist98132792019-07-09 13:29:09 -0700425 FindI2CDevices(_i2cBuses, _busMap);
James Feist6ebf9de2018-05-15 15:01:17 -0700426 }
427
James Feista465ccc2019-02-08 12:51:01 -0800428 const std::vector<fs::path>& _i2cBuses;
429 boost::asio::io_service& _io;
430 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700431 std::function<void(void)> _callback;
432};
433
James Feist3cb5fec2018-01-23 14:41:51 -0800434static const std::tm intelEpoch(void)
435{
James Feist98132792019-07-09 13:29:09 -0700436 std::tm val = {};
James Feist3cb5fec2018-01-23 14:41:51 -0800437 val.tm_year = 1996 - 1900;
438 return val;
439}
440
James Feista465ccc2019-02-08 12:51:01 -0800441bool formatFru(const std::vector<char>& fruBytes,
442 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800443{
James Feista465ccc2019-02-08 12:51:01 -0800444 static const std::vector<const char*> CHASSIS_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800445 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800446
James Feista465ccc2019-02-08 12:51:01 -0800447 static const std::vector<const char*> BOARD_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800448 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
449 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800450
James Feista465ccc2019-02-08 12:51:01 -0800451 static const std::vector<const char*> PRODUCT_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800452 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
453 "VERSION", "SERIAL_NUMBER", "ASSET_TAG",
454 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800455
James Feistd068e932018-09-20 10:53:07 -0700456 if (fruBytes.size() <= 8)
James Feist3cb5fec2018-01-23 14:41:51 -0800457 {
458 return false;
459 }
460 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700461 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800462 std::to_string(static_cast<int>(*fruAreaOffsetField));
463
James Feista465ccc2019-02-08 12:51:01 -0800464 const std::vector<const char*>* fieldData;
James Feist3cb5fec2018-01-23 14:41:51 -0800465
James Feist0eb40352019-04-09 14:44:04 -0700466 for (const std::string& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800467 {
468 fruAreaOffsetField++;
469 if (fruAreaOffsetField >= fruBytes.end())
470 {
471 return false;
472 }
473 size_t offset = (*fruAreaOffsetField) * 8;
474
475 if (offset > 1)
476 {
477 // +2 to skip format and length
478 std::vector<char>::const_iterator fruBytesIter =
479 fruBytes.begin() + offset + 2;
480
481 if (fruBytesIter >= fruBytes.end())
482 {
483 return false;
484 }
485
486 if (area == "CHASSIS")
487 {
488 result["CHASSIS_TYPE"] =
489 std::to_string(static_cast<int>(*fruBytesIter));
490 fruBytesIter += 1;
491 fieldData = &CHASSIS_FRU_AREAS;
492 }
493 else if (area == "BOARD")
494 {
495 result["BOARD_LANGUAGE_CODE"] =
496 std::to_string(static_cast<int>(*fruBytesIter));
497 fruBytesIter += 1;
498 if (fruBytesIter >= fruBytes.end())
499 {
500 return false;
501 }
502
503 unsigned int minutes = *fruBytesIter |
504 *(fruBytesIter + 1) << 8 |
505 *(fruBytesIter + 2) << 16;
506 std::tm fruTime = intelEpoch();
507 time_t timeValue = mktime(&fruTime);
508 timeValue += minutes * 60;
509 fruTime = *gmtime(&timeValue);
510
511 result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
512 result["BOARD_MANUFACTURE_DATE"]
513 .pop_back(); // remove trailing null
514 fruBytesIter += 3;
515 fieldData = &BOARD_FRU_AREAS;
516 }
517 else if (area == "PRODUCT")
518 {
519 result["PRODUCT_LANGUAGE_CODE"] =
520 std::to_string(static_cast<int>(*fruBytesIter));
521 fruBytesIter += 1;
522 fieldData = &PRODUCT_FRU_AREAS;
523 }
524 else
525 {
526 continue;
527 }
James Feista465ccc2019-02-08 12:51:01 -0800528 for (auto& field : *fieldData)
James Feist3cb5fec2018-01-23 14:41:51 -0800529 {
530 if (fruBytesIter >= fruBytes.end())
531 {
532 return false;
533 }
534
Vijay Khemka5d5de442018-11-07 10:51:25 -0800535 /* Checking for last byte C1 to indicate that no more
536 * field to be read */
James Feist98132792019-07-09 13:29:09 -0700537 if (static_cast<uint8_t>(*fruBytesIter) == 0xC1)
Vijay Khemka5d5de442018-11-07 10:51:25 -0800538 {
539 break;
540 }
541
Ed Tanous2147e672019-02-27 13:59:56 -0800542 size_t length = *fruBytesIter & 0x3f;
543 fruBytesIter += 1;
544
James Feist3cb5fec2018-01-23 14:41:51 -0800545 if (fruBytesIter >= fruBytes.end())
546 {
547 return false;
548 }
Ed Tanous2147e672019-02-27 13:59:56 -0800549 std::string value(fruBytesIter, fruBytesIter + length);
James Feist3cb5fec2018-01-23 14:41:51 -0800550
Ed Tanous2147e672019-02-27 13:59:56 -0800551 // Strip non null characters from the end
552 value.erase(std::find_if(value.rbegin(), value.rend(),
553 [](char ch) { return ch != 0; })
554 .base(),
555 value.end());
556
James Feist0eb40352019-04-09 14:44:04 -0700557 result[area + "_" + field] = std::move(value);
Ed Tanous2147e672019-02-27 13:59:56 -0800558
James Feist3cb5fec2018-01-23 14:41:51 -0800559 fruBytesIter += length;
560 if (fruBytesIter >= fruBytes.end())
561 {
562 std::cerr << "Warning Fru Length Mismatch:\n ";
James Feista465ccc2019-02-08 12:51:01 -0800563 for (auto& c : fruBytes)
James Feist3cb5fec2018-01-23 14:41:51 -0800564 {
565 std::cerr << c;
566 }
567 std::cerr << "\n";
568 if (DEBUG)
569 {
James Feista465ccc2019-02-08 12:51:01 -0800570 for (auto& keyPair : result)
James Feist3cb5fec2018-01-23 14:41:51 -0800571 {
572 std::cerr << keyPair.first << " : "
573 << keyPair.second << "\n";
574 }
575 }
576 return false;
577 }
578 }
579 }
580 }
581
582 return true;
583}
584
Nikhil Potaded8884f12019-03-27 13:27:13 -0700585std::vector<uint8_t>& getFruInfo(const uint8_t& bus, const uint8_t& address)
586{
587 auto deviceMap = busMap.find(bus);
588 if (deviceMap == busMap.end())
589 {
590 throw std::invalid_argument("Invalid Bus.");
591 }
592 auto device = deviceMap->second->find(address);
593 if (device == deviceMap->second->end())
594 {
595 throw std::invalid_argument("Invalid Address.");
596 }
597 std::vector<uint8_t>& ret =
598 reinterpret_cast<std::vector<uint8_t>&>(device->second);
599
600 return ret;
601}
602
James Feist3cb5fec2018-01-23 14:41:51 -0800603void AddFruObjectToDbus(
James Feista465ccc2019-02-08 12:51:01 -0800604 std::vector<char>& device, sdbusplus::asio::object_server& objServer,
605 boost::container::flat_map<
606 std::pair<size_t, size_t>,
607 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700608 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -0800609{
610 boost::container::flat_map<std::string, std::string> formattedFru;
611 if (!formatFru(device, formattedFru))
612 {
613 std::cerr << "failed to format fru for device at bus " << std::hex
Nikhil Potaded8884f12019-03-27 13:27:13 -0700614 << bus << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800615 return;
616 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700617
James Feist3cb5fec2018-01-23 14:41:51 -0800618 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
619 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700620 // Not found under Board section or an empty string.
621 if (productNameFind == formattedFru.end() ||
622 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800623 {
624 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
625 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700626 // Found under Product section and not an empty string.
627 if (productNameFind != formattedFru.end() &&
628 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800629 {
630 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800631 std::regex illegalObject("[^A-Za-z0-9_]");
632 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800633 }
634 else
635 {
636 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
637 UNKNOWN_BUS_OBJECT_COUNT++;
638 }
639
James Feist918e18c2018-02-13 15:51:07 -0800640 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800641 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700642 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800643 {
James Feist79e9c0b2018-03-15 15:21:17 -0700644 size_t index = 0;
James Feista465ccc2019-02-08 12:51:01 -0800645 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800646 {
James Feist9eb0b582018-04-27 12:15:46 -0700647 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800648 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700649 if (isMuxBus(bus) && address == busIface.first.second &&
James Feist98132792019-07-09 13:29:09 -0700650 (getFruInfo(static_cast<uint8_t>(busIface.first.first),
651 static_cast<uint8_t>(busIface.first.second)) ==
652 getFruInfo(static_cast<uint8_t>(bus),
653 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700654 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700655 // This device is already added to the lower numbered bus,
656 // do not replicate it.
657 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700658 }
659 // add underscore _index for the same object path on dbus
660 std::string strIndex = std::to_string(index);
661 if (index > 0)
662 {
663 productName.substr(0, productName.size() - strIndex.size());
664 }
665 else
666 {
667 productName += "_";
668 }
669 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800670 }
671 }
672 }
James Feist3cb5fec2018-01-23 14:41:51 -0800673
James Feist9eb0b582018-04-27 12:15:46 -0700674 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
675 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
676 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
677
James Feista465ccc2019-02-08 12:51:01 -0800678 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800679 {
James Feist9eb0b582018-04-27 12:15:46 -0700680
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700681 std::regex_replace(property.second.begin(), property.second.begin(),
682 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700683 if (property.second.empty())
684 {
685 continue;
686 }
687 std::string key =
688 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
689 if (!iface->register_property(key, property.second + '\0'))
690 {
691 std::cerr << "illegal key: " << key << "\n";
692 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700693 if (DEBUG)
694 {
695 std::cout << property.first << ": " << property.second << "\n";
696 }
James Feist3cb5fec2018-01-23 14:41:51 -0800697 }
James Feist2a9d6db2018-04-27 15:48:28 -0700698
699 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700700 iface->register_property("BUS", bus);
701 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700702
James Feist9eb0b582018-04-27 12:15:46 -0700703 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800704}
705
James Feista465ccc2019-02-08 12:51:01 -0800706static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800707{
708 // try to read baseboard fru from file
709 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
710 if (baseboardFruFile.good())
711 {
712 baseboardFruFile.seekg(0, std::ios_base::end);
James Feist98132792019-07-09 13:29:09 -0700713 size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg());
James Feist3cb5fec2018-01-23 14:41:51 -0800714 baseboardFru.resize(fileSize);
715 baseboardFruFile.seekg(0, std::ios_base::beg);
716 baseboardFruFile.read(baseboardFru.data(), fileSize);
717 }
718 else
719 {
720 return false;
721 }
722 return true;
723}
724
James Feista465ccc2019-02-08 12:51:01 -0800725bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700726{
727 boost::container::flat_map<std::string, std::string> tmp;
728 if (fru.size() > MAX_FRU_SIZE)
729 {
730 std::cerr << "Invalid fru.size() during writeFru\n";
731 return false;
732 }
733 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -0800734 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -0700735 {
736 std::cerr << "Invalid fru format during writeFru\n";
737 return false;
738 }
739 // baseboard fru
740 if (bus == 0 && address == 0)
741 {
742 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
743 if (!file.good())
744 {
745 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
746 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700747 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700748 return false;
749 }
James Feista465ccc2019-02-08 12:51:01 -0800750 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700751 return file.good();
752 }
753 else
754 {
755 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
756
757 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
758 if (file < 0)
759 {
760 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -0700761 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700762 return false;
763 }
764 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
765 {
766 std::cerr << "unable to set device address\n";
767 close(file);
James Feistddb78302018-09-06 11:45:42 -0700768 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700769 return false;
770 }
771
772 constexpr const size_t RETRY_MAX = 2;
773 uint16_t index = 0;
774 size_t retries = RETRY_MAX;
775 while (index < fru.size())
776 {
777 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
778 (retries == RETRY_MAX))
779 {
780 // The 4K EEPROM only uses the A2 and A1 device address bits
781 // with the third bit being a memory page address bit.
782 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
783 {
784 std::cerr << "unable to set device address\n";
785 close(file);
James Feistddb78302018-09-06 11:45:42 -0700786 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700787 return false;
788 }
789 }
790
James Feist98132792019-07-09 13:29:09 -0700791 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
792 fru[index]) < 0)
James Feistb49ffc32018-05-02 11:10:43 -0700793 {
794 if (!retries--)
795 {
796 std::cerr << "error writing fru: " << strerror(errno)
797 << "\n";
798 close(file);
James Feistddb78302018-09-06 11:45:42 -0700799 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700800 return false;
801 }
802 }
803 else
804 {
805 retries = RETRY_MAX;
806 index++;
807 }
808 // most eeproms require 5-10ms between writes
809 std::this_thread::sleep_for(std::chrono::milliseconds(10));
810 }
811 close(file);
812 return true;
813 }
814}
815
James Feist9eb0b582018-04-27 12:15:46 -0700816void rescanBusses(
James Feist98132792019-07-09 13:29:09 -0700817 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800818 boost::container::flat_map<
819 std::pair<size_t, size_t>,
820 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feista465ccc2019-02-08 12:51:01 -0800821 sdbusplus::asio::object_server& objServer)
James Feist918e18c2018-02-13 15:51:07 -0800822{
James Feist6ebf9de2018-05-15 15:01:17 -0700823 static boost::asio::deadline_timer timer(io);
824 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800825
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500826 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -0700827 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -0800828 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800829 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800830
Nikhil Potaded8884f12019-03-27 13:27:13 -0700831 boost::container::flat_map<size_t, fs::path> busPaths;
832 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -0800833 {
James Feist4131aea2018-03-09 09:47:30 -0800834 std::cerr << "unable to find i2c devices\n";
835 return;
James Feist918e18c2018-02-13 15:51:07 -0800836 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700837
838 for (auto busPath : busPaths)
839 {
840 i2cBuses.emplace_back(busPath.second);
841 }
James Feist4131aea2018-03-09 09:47:30 -0800842
James Feist98132792019-07-09 13:29:09 -0700843 busmap.clear();
James Feist6ebf9de2018-05-15 15:01:17 -0700844 auto scan = std::make_shared<FindDevicesWithCallback>(
James Feist98132792019-07-09 13:29:09 -0700845 i2cBuses, io, busmap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -0800846 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700847 {
848 objServer.remove_interface(busIface.second);
849 }
James Feist4131aea2018-03-09 09:47:30 -0800850
James Feist6ebf9de2018-05-15 15:01:17 -0700851 dbusInterfaceMap.clear();
852 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -0800853
James Feist6ebf9de2018-05-15 15:01:17 -0700854 // todo, get this from a more sensable place
855 std::vector<char> baseboardFru;
856 if (readBaseboardFru(baseboardFru))
857 {
858 boost::container::flat_map<int, std::vector<char>>
859 baseboardDev;
860 baseboardDev.emplace(0, baseboardFru);
James Feist98132792019-07-09 13:29:09 -0700861 busmap[0] = std::make_shared<DeviceMap>(baseboardDev);
James Feist6ebf9de2018-05-15 15:01:17 -0700862 }
James Feist98132792019-07-09 13:29:09 -0700863 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -0700864 {
James Feista465ccc2019-02-08 12:51:01 -0800865 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -0700866 {
James Feist98132792019-07-09 13:29:09 -0700867 AddFruObjectToDbus(device.second, objServer,
James Feist6ebf9de2018-05-15 15:01:17 -0700868 dbusInterfaceMap, devicemap.first,
869 device.first);
870 }
871 }
872 });
873 scan->run();
874 });
James Feist918e18c2018-02-13 15:51:07 -0800875}
876
James Feist98132792019-07-09 13:29:09 -0700877int main()
James Feist3cb5fec2018-01-23 14:41:51 -0800878{
879 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -0800880 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -0800881 std::vector<fs::path> i2cBuses;
882
James Feista3c180a2018-08-09 16:06:04 -0700883 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -0800884 {
885 std::cerr << "unable to find i2c devices\n";
886 return 1;
887 }
James Feist3cb5fec2018-01-23 14:41:51 -0800888
Patrick Venture11f1ff42019-08-01 10:42:12 -0700889 // check for and load blacklist with initial buses.
890 loadBlacklist(blacklistPath);
891
James Feist3cb5fec2018-01-23 14:41:51 -0800892 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -0700893 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
894 auto objServer = sdbusplus::asio::object_server(systemBus);
Vijay Khemka065f6d92018-12-18 10:37:47 -0800895 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -0800896
James Feist6ebf9de2018-05-15 15:01:17 -0700897 // this is a map with keys of pair(bus number, address) and values of
898 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -0800899 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700900 std::shared_ptr<sdbusplus::asio::dbus_interface>>
901 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -0800902
James Feist9eb0b582018-04-27 12:15:46 -0700903 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
904 objServer.add_interface("/xyz/openbmc_project/FruDevice",
905 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -0800906
907 iface->register_method("ReScan", [&]() {
James Feist98132792019-07-09 13:29:09 -0700908 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -0800909 });
James Feist2a9d6db2018-04-27 15:48:28 -0700910
Nikhil Potaded8884f12019-03-27 13:27:13 -0700911 iface->register_method("GetRawFru", getFruInfo);
James Feistb49ffc32018-05-02 11:10:43 -0700912
913 iface->register_method("WriteFru", [&](const uint8_t bus,
914 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -0800915 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -0700916 if (!writeFru(bus, address, data))
917 {
James Feistddb78302018-09-06 11:45:42 -0700918 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -0700919 return;
920 }
921 // schedule rescan on success
James Feist98132792019-07-09 13:29:09 -0700922 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feistb49ffc32018-05-02 11:10:43 -0700923 });
James Feist9eb0b582018-04-27 12:15:46 -0700924 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800925
James Feist9eb0b582018-04-27 12:15:46 -0700926 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -0800927 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -0800928 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -0700929 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800930 std::string,
931 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -0700932 values;
933 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -0800934 auto findPgood = values.find("pgood");
935 if (findPgood != values.end())
936 {
James Feist6ebf9de2018-05-15 15:01:17 -0700937
James Feist98132792019-07-09 13:29:09 -0700938 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800939 }
James Feist918e18c2018-02-13 15:51:07 -0800940 };
James Feist9eb0b582018-04-27 12:15:46 -0700941
942 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -0800943 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -0700944 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
945 "openbmc_project/Chassis/Control/"
946 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -0700947 eventHandler);
948
James Feist4131aea2018-03-09 09:47:30 -0800949 int fd = inotify_init();
James Feist0eb40352019-04-09 14:44:04 -0700950 inotify_add_watch(fd, I2C_DEV_LOCATION,
951 IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -0800952 std::array<char, 4096> readBuffer;
953 std::string pendingBuffer;
954 // monitor for new i2c devices
955 boost::asio::posix::stream_descriptor dirWatch(io, fd);
956 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -0800957 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -0800958 std::size_t bytes_transferred) {
959 if (ec)
960 {
961 std::cout << "Callback Error " << ec << "\n";
962 return;
963 }
964 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
965 bool devChange = false;
966 while (pendingBuffer.size() > sizeof(inotify_event))
967 {
James Feista465ccc2019-02-08 12:51:01 -0800968 const inotify_event* iEvent =
969 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -0800970 pendingBuffer.data());
971 switch (iEvent->mask)
972 {
James Feist9eb0b582018-04-27 12:15:46 -0700973 case IN_CREATE:
974 case IN_MOVED_TO:
975 case IN_DELETE:
976 if (boost::starts_with(std::string(iEvent->name),
977 "i2c"))
978 {
979 devChange = true;
980 }
James Feist4131aea2018-03-09 09:47:30 -0800981 }
982
983 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
984 }
James Feist6ebf9de2018-05-15 15:01:17 -0700985 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -0800986 {
James Feist98132792019-07-09 13:29:09 -0700987 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist4131aea2018-03-09 09:47:30 -0800988 }
James Feist6ebf9de2018-05-15 15:01:17 -0700989
James Feist4131aea2018-03-09 09:47:30 -0800990 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
991 watchI2cBusses);
992 };
993
994 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -0500995 // run the initial scan
James Feist98132792019-07-09 13:29:09 -0700996 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800997
James Feist3cb5fec2018-01-23 14:41:51 -0800998 io.run();
999 return 0;
1000}