blob: e2ef3179c7cd9b29103db5fd8f7ab8b82be3ab9e [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
James Feist24bae7a2019-04-03 09:50:56 -0700213 if (validateHeader(block_data))
James Feist26c27ad2018-07-25 15:09:40 -0700214 {
215 std::vector<char> device;
216 device.insert(device.end(), block_data.begin(),
217 block_data.begin() + 8);
218
James Feist0eb40352019-04-09 14:44:04 -0700219 for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++)
James Feist26c27ad2018-07-25 15:09:40 -0700220 {
221 auto area_offset = device[jj];
222 if (area_offset != 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800223 {
James Feist98132792019-07-09 13:29:09 -0700224 area_offset = static_cast<char>(area_offset * 8);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800225 if (read_block_data(flag, file, area_offset, 0x8,
226 block_data.data()) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800227 {
228 std::cerr << "failed to read bus " << bus
229 << " address " << ii << "\n";
230 return -1;
231 }
James Feist26c27ad2018-07-25 15:09:40 -0700232 int length = block_data[1] * 8;
James Feist3cb5fec2018-01-23 14:41:51 -0800233 device.insert(device.end(), block_data.begin(),
James Feist26c27ad2018-07-25 15:09:40 -0700234 block_data.begin() + 8);
235 length -= 8;
James Feist98132792019-07-09 13:29:09 -0700236 area_offset = static_cast<char>(area_offset + 8);
James Feist26c27ad2018-07-25 15:09:40 -0700237
238 while (length > 0)
239 {
240 auto to_get = std::min(0x20, length);
James Feist98132792019-07-09 13:29:09 -0700241
242 if (read_block_data(flag, file, area_offset,
243 static_cast<uint8_t>(to_get),
Vijay Khemka2d681f62018-11-06 15:51:00 -0800244 block_data.data()) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700245 {
246 std::cerr << "failed to read bus " << bus
247 << " address " << ii << "\n";
248 return -1;
249 }
250 device.insert(device.end(), block_data.begin(),
251 block_data.begin() + to_get);
James Feist98132792019-07-09 13:29:09 -0700252 area_offset =
253 static_cast<char>(area_offset + to_get);
James Feist26c27ad2018-07-25 15:09:40 -0700254 length -= to_get;
255 }
James Feist3cb5fec2018-01-23 14:41:51 -0800256 }
257 }
James Feist26c27ad2018-07-25 15:09:40 -0700258 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800259 }
James Feist24bae7a2019-04-03 09:50:56 -0700260 else if (DEBUG)
James Feist9945ddf2019-03-07 13:57:36 -0800261 {
James Feist24bae7a2019-04-03 09:50:56 -0700262 std::cerr << "Illegal header at bus " << bus << " address "
263 << ii << "\n";
James Feist9945ddf2019-03-07 13:57:36 -0800264 }
James Feist3cb5fec2018-01-23 14:41:51 -0800265 }
James Feist26c27ad2018-07-25 15:09:40 -0700266 return 1;
267 });
268 std::future_status status =
269 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
270 if (status == std::future_status::timeout)
271 {
272 std::cerr << "Error reading bus " << bus << "\n";
James Feist444830e2019-04-05 08:38:16 -0700273 busBlacklist.insert(bus);
274 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700275 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800276 }
277
James Feist444830e2019-04-05 08:38:16 -0700278 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700279 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800280}
281
Patrick Venture11f1ff42019-08-01 10:42:12 -0700282void loadBlacklist(const char* path)
283{
284 std::ifstream blacklistStream(path);
285 if (!blacklistStream.good())
286 {
287 // File is optional.
288 std::cerr << "Cannot open blacklist file.\n\n";
289 return;
290 }
291
292 nlohmann::json data =
293 nlohmann::json::parse(blacklistStream, nullptr, false);
294 if (data.is_discarded())
295 {
296 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
297 "exiting\n";
298 std::exit(EXIT_FAILURE);
299 return;
300 }
301
302 // It's expected to have at least one field, "buses" that is an array of the
303 // buses by integer. Allow for future options to exclude further aspects,
304 // such as specific addresses or ranges.
305 if (data.type() != nlohmann::json::value_t::object)
306 {
307 std::cerr << "Illegal blacklist, expected to read dictionary\n";
308 std::exit(EXIT_FAILURE);
309 return;
310 }
311
312 // If buses field is missing, that's fine.
313 if (data.count("buses") == 1)
314 {
315 // Parse the buses array after a little validation.
316 auto buses = data.at("buses");
317 if (buses.type() != nlohmann::json::value_t::array)
318 {
319 // Buses field present but invalid, therefore this is an error.
320 std::cerr << "Invalid contents for blacklist buses field\n";
321 std::exit(EXIT_FAILURE);
322 return;
323 }
324
325 // Catch exception here for type mis-match.
326 try
327 {
328 for (const auto& bus : buses)
329 {
330 busBlacklist.insert(bus.get<size_t>());
331 }
332 }
333 catch (const nlohmann::detail::type_error& e)
334 {
335 // Type mis-match is a critical error.
336 std::cerr << "Invalid bus type: " << e.what() << "\n";
337 std::exit(EXIT_FAILURE);
338 return;
339 }
340 }
341
342 return;
343}
344
James Feista465ccc2019-02-08 12:51:01 -0800345static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700346 BusMap& busmap)
James Feist3cb5fec2018-01-23 14:41:51 -0800347{
James Feista465ccc2019-02-08 12:51:01 -0800348 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800349 {
350 auto busnum = i2cBus.string();
351 auto lastDash = busnum.rfind(std::string("-"));
352 // delete everything before dash inclusive
353 if (lastDash != std::string::npos)
354 {
355 busnum.erase(0, lastDash + 1);
356 }
357 auto bus = std::stoi(busnum);
James Feist444830e2019-04-05 08:38:16 -0700358 if (busBlacklist.find(bus) != busBlacklist.end())
359 {
360 continue; // skip previously failed busses
361 }
James Feist3cb5fec2018-01-23 14:41:51 -0800362
363 auto file = open(i2cBus.c_str(), O_RDWR);
364 if (file < 0)
365 {
366 std::cerr << "unable to open i2c device " << i2cBus.string()
367 << "\n";
368 continue;
369 }
370 unsigned long funcs = 0;
371
372 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
373 {
374 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700375 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800376 << bus << "\n";
377 continue;
378 }
379 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
380 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
381 {
382 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
383 << bus << "\n";
384 continue;
385 }
James Feist98132792019-07-09 13:29:09 -0700386 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800387 device = std::make_shared<DeviceMap>();
388
Nikhil Potaded8884f12019-03-27 13:27:13 -0700389 // i2cdetect by default uses the range 0x03 to 0x77, as
390 // this is what we have tested with, use this range. Could be
391 // changed in future.
392 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800393 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700394 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800395 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700396
397 // fd is closed in this function in case the bus locks up
398 get_bus_frus(file, 0x03, 0x77, bus, device);
399
400 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800401 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700402 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800403 }
James Feist3cb5fec2018-01-23 14:41:51 -0800404 }
James Feist3cb5fec2018-01-23 14:41:51 -0800405}
406
James Feist6ebf9de2018-05-15 15:01:17 -0700407// this class allows an async response after all i2c devices are discovered
408struct FindDevicesWithCallback
409 : std::enable_shared_from_this<FindDevicesWithCallback>
410{
James Feista465ccc2019-02-08 12:51:01 -0800411 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700412 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800413 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700414 _i2cBuses(i2cBuses),
James Feist98132792019-07-09 13:29:09 -0700415 _io(io), _busMap(busmap), _callback(std::move(callback))
James Feist6ebf9de2018-05-15 15:01:17 -0700416 {
417 }
418 ~FindDevicesWithCallback()
419 {
420 _callback();
421 }
422 void run()
423 {
James Feist98132792019-07-09 13:29:09 -0700424 FindI2CDevices(_i2cBuses, _busMap);
James Feist6ebf9de2018-05-15 15:01:17 -0700425 }
426
James Feista465ccc2019-02-08 12:51:01 -0800427 const std::vector<fs::path>& _i2cBuses;
428 boost::asio::io_service& _io;
429 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700430 std::function<void(void)> _callback;
431};
432
James Feist3cb5fec2018-01-23 14:41:51 -0800433static const std::tm intelEpoch(void)
434{
James Feist98132792019-07-09 13:29:09 -0700435 std::tm val = {};
James Feist3cb5fec2018-01-23 14:41:51 -0800436 val.tm_year = 1996 - 1900;
437 return val;
438}
439
James Feista465ccc2019-02-08 12:51:01 -0800440bool formatFru(const std::vector<char>& fruBytes,
441 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800442{
James Feista465ccc2019-02-08 12:51:01 -0800443 static const std::vector<const char*> CHASSIS_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800444 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800445
James Feista465ccc2019-02-08 12:51:01 -0800446 static const std::vector<const char*> BOARD_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800447 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
448 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800449
James Feista465ccc2019-02-08 12:51:01 -0800450 static const std::vector<const char*> PRODUCT_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800451 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
452 "VERSION", "SERIAL_NUMBER", "ASSET_TAG",
453 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800454
James Feistd068e932018-09-20 10:53:07 -0700455 if (fruBytes.size() <= 8)
James Feist3cb5fec2018-01-23 14:41:51 -0800456 {
457 return false;
458 }
459 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700460 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800461 std::to_string(static_cast<int>(*fruAreaOffsetField));
462
James Feista465ccc2019-02-08 12:51:01 -0800463 const std::vector<const char*>* fieldData;
James Feist3cb5fec2018-01-23 14:41:51 -0800464
James Feist0eb40352019-04-09 14:44:04 -0700465 for (const std::string& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800466 {
467 fruAreaOffsetField++;
468 if (fruAreaOffsetField >= fruBytes.end())
469 {
470 return false;
471 }
472 size_t offset = (*fruAreaOffsetField) * 8;
473
474 if (offset > 1)
475 {
476 // +2 to skip format and length
477 std::vector<char>::const_iterator fruBytesIter =
478 fruBytes.begin() + offset + 2;
479
480 if (fruBytesIter >= fruBytes.end())
481 {
482 return false;
483 }
484
485 if (area == "CHASSIS")
486 {
487 result["CHASSIS_TYPE"] =
488 std::to_string(static_cast<int>(*fruBytesIter));
489 fruBytesIter += 1;
490 fieldData = &CHASSIS_FRU_AREAS;
491 }
492 else if (area == "BOARD")
493 {
494 result["BOARD_LANGUAGE_CODE"] =
495 std::to_string(static_cast<int>(*fruBytesIter));
496 fruBytesIter += 1;
497 if (fruBytesIter >= fruBytes.end())
498 {
499 return false;
500 }
501
502 unsigned int minutes = *fruBytesIter |
503 *(fruBytesIter + 1) << 8 |
504 *(fruBytesIter + 2) << 16;
505 std::tm fruTime = intelEpoch();
506 time_t timeValue = mktime(&fruTime);
507 timeValue += minutes * 60;
508 fruTime = *gmtime(&timeValue);
509
510 result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
511 result["BOARD_MANUFACTURE_DATE"]
512 .pop_back(); // remove trailing null
513 fruBytesIter += 3;
514 fieldData = &BOARD_FRU_AREAS;
515 }
516 else if (area == "PRODUCT")
517 {
518 result["PRODUCT_LANGUAGE_CODE"] =
519 std::to_string(static_cast<int>(*fruBytesIter));
520 fruBytesIter += 1;
521 fieldData = &PRODUCT_FRU_AREAS;
522 }
523 else
524 {
525 continue;
526 }
James Feista465ccc2019-02-08 12:51:01 -0800527 for (auto& field : *fieldData)
James Feist3cb5fec2018-01-23 14:41:51 -0800528 {
529 if (fruBytesIter >= fruBytes.end())
530 {
531 return false;
532 }
533
Vijay Khemka5d5de442018-11-07 10:51:25 -0800534 /* Checking for last byte C1 to indicate that no more
535 * field to be read */
James Feist98132792019-07-09 13:29:09 -0700536 if (static_cast<uint8_t>(*fruBytesIter) == 0xC1)
Vijay Khemka5d5de442018-11-07 10:51:25 -0800537 {
538 break;
539 }
540
Ed Tanous2147e672019-02-27 13:59:56 -0800541 size_t length = *fruBytesIter & 0x3f;
542 fruBytesIter += 1;
543
James Feist3cb5fec2018-01-23 14:41:51 -0800544 if (fruBytesIter >= fruBytes.end())
545 {
546 return false;
547 }
Ed Tanous2147e672019-02-27 13:59:56 -0800548 std::string value(fruBytesIter, fruBytesIter + length);
James Feist3cb5fec2018-01-23 14:41:51 -0800549
Ed Tanous2147e672019-02-27 13:59:56 -0800550 // Strip non null characters from the end
551 value.erase(std::find_if(value.rbegin(), value.rend(),
552 [](char ch) { return ch != 0; })
553 .base(),
554 value.end());
555
James Feist0eb40352019-04-09 14:44:04 -0700556 result[area + "_" + field] = std::move(value);
Ed Tanous2147e672019-02-27 13:59:56 -0800557
James Feist3cb5fec2018-01-23 14:41:51 -0800558 fruBytesIter += length;
559 if (fruBytesIter >= fruBytes.end())
560 {
561 std::cerr << "Warning Fru Length Mismatch:\n ";
James Feista465ccc2019-02-08 12:51:01 -0800562 for (auto& c : fruBytes)
James Feist3cb5fec2018-01-23 14:41:51 -0800563 {
564 std::cerr << c;
565 }
566 std::cerr << "\n";
567 if (DEBUG)
568 {
James Feista465ccc2019-02-08 12:51:01 -0800569 for (auto& keyPair : result)
James Feist3cb5fec2018-01-23 14:41:51 -0800570 {
571 std::cerr << keyPair.first << " : "
572 << keyPair.second << "\n";
573 }
574 }
575 return false;
576 }
577 }
578 }
579 }
580
581 return true;
582}
583
Nikhil Potaded8884f12019-03-27 13:27:13 -0700584std::vector<uint8_t>& getFruInfo(const uint8_t& bus, const uint8_t& address)
585{
586 auto deviceMap = busMap.find(bus);
587 if (deviceMap == busMap.end())
588 {
589 throw std::invalid_argument("Invalid Bus.");
590 }
591 auto device = deviceMap->second->find(address);
592 if (device == deviceMap->second->end())
593 {
594 throw std::invalid_argument("Invalid Address.");
595 }
596 std::vector<uint8_t>& ret =
597 reinterpret_cast<std::vector<uint8_t>&>(device->second);
598
599 return ret;
600}
601
James Feist3cb5fec2018-01-23 14:41:51 -0800602void AddFruObjectToDbus(
James Feista465ccc2019-02-08 12:51:01 -0800603 std::vector<char>& device, sdbusplus::asio::object_server& objServer,
604 boost::container::flat_map<
605 std::pair<size_t, size_t>,
606 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700607 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -0800608{
609 boost::container::flat_map<std::string, std::string> formattedFru;
610 if (!formatFru(device, formattedFru))
611 {
612 std::cerr << "failed to format fru for device at bus " << std::hex
Nikhil Potaded8884f12019-03-27 13:27:13 -0700613 << bus << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800614 return;
615 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700616
James Feist3cb5fec2018-01-23 14:41:51 -0800617 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
618 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700619 // Not found under Board section or an empty string.
620 if (productNameFind == formattedFru.end() ||
621 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800622 {
623 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
624 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700625 // Found under Product section and not an empty string.
626 if (productNameFind != formattedFru.end() &&
627 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800628 {
629 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800630 std::regex illegalObject("[^A-Za-z0-9_]");
631 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800632 }
633 else
634 {
635 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
636 UNKNOWN_BUS_OBJECT_COUNT++;
637 }
638
James Feist918e18c2018-02-13 15:51:07 -0800639 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800640 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700641 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800642 {
James Feist79e9c0b2018-03-15 15:21:17 -0700643 size_t index = 0;
James Feista465ccc2019-02-08 12:51:01 -0800644 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800645 {
James Feist9eb0b582018-04-27 12:15:46 -0700646 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800647 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700648 if (isMuxBus(bus) && address == busIface.first.second &&
James Feist98132792019-07-09 13:29:09 -0700649 (getFruInfo(static_cast<uint8_t>(busIface.first.first),
650 static_cast<uint8_t>(busIface.first.second)) ==
651 getFruInfo(static_cast<uint8_t>(bus),
652 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700653 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700654 // This device is already added to the lower numbered bus,
655 // do not replicate it.
656 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700657 }
658 // add underscore _index for the same object path on dbus
659 std::string strIndex = std::to_string(index);
660 if (index > 0)
661 {
662 productName.substr(0, productName.size() - strIndex.size());
663 }
664 else
665 {
666 productName += "_";
667 }
668 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800669 }
670 }
671 }
James Feist3cb5fec2018-01-23 14:41:51 -0800672
James Feist9eb0b582018-04-27 12:15:46 -0700673 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
674 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
675 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
676
James Feista465ccc2019-02-08 12:51:01 -0800677 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800678 {
James Feist9eb0b582018-04-27 12:15:46 -0700679
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700680 std::regex_replace(property.second.begin(), property.second.begin(),
681 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700682 if (property.second.empty())
683 {
684 continue;
685 }
686 std::string key =
687 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
688 if (!iface->register_property(key, property.second + '\0'))
689 {
690 std::cerr << "illegal key: " << key << "\n";
691 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700692 if (DEBUG)
693 {
694 std::cout << property.first << ": " << property.second << "\n";
695 }
James Feist3cb5fec2018-01-23 14:41:51 -0800696 }
James Feist2a9d6db2018-04-27 15:48:28 -0700697
698 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700699 iface->register_property("BUS", bus);
700 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700701
James Feist9eb0b582018-04-27 12:15:46 -0700702 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800703}
704
James Feista465ccc2019-02-08 12:51:01 -0800705static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800706{
707 // try to read baseboard fru from file
708 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
709 if (baseboardFruFile.good())
710 {
711 baseboardFruFile.seekg(0, std::ios_base::end);
James Feist98132792019-07-09 13:29:09 -0700712 size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg());
James Feist3cb5fec2018-01-23 14:41:51 -0800713 baseboardFru.resize(fileSize);
714 baseboardFruFile.seekg(0, std::ios_base::beg);
715 baseboardFruFile.read(baseboardFru.data(), fileSize);
716 }
717 else
718 {
719 return false;
720 }
721 return true;
722}
723
James Feista465ccc2019-02-08 12:51:01 -0800724bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700725{
726 boost::container::flat_map<std::string, std::string> tmp;
727 if (fru.size() > MAX_FRU_SIZE)
728 {
729 std::cerr << "Invalid fru.size() during writeFru\n";
730 return false;
731 }
732 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -0800733 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -0700734 {
735 std::cerr << "Invalid fru format during writeFru\n";
736 return false;
737 }
738 // baseboard fru
739 if (bus == 0 && address == 0)
740 {
741 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
742 if (!file.good())
743 {
744 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
745 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700746 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700747 return false;
748 }
James Feista465ccc2019-02-08 12:51:01 -0800749 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700750 return file.good();
751 }
752 else
753 {
754 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
755
756 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
757 if (file < 0)
758 {
759 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -0700760 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700761 return false;
762 }
763 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
764 {
765 std::cerr << "unable to set device address\n";
766 close(file);
James Feistddb78302018-09-06 11:45:42 -0700767 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700768 return false;
769 }
770
771 constexpr const size_t RETRY_MAX = 2;
772 uint16_t index = 0;
773 size_t retries = RETRY_MAX;
774 while (index < fru.size())
775 {
776 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
777 (retries == RETRY_MAX))
778 {
779 // The 4K EEPROM only uses the A2 and A1 device address bits
780 // with the third bit being a memory page address bit.
781 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
782 {
783 std::cerr << "unable to set device address\n";
784 close(file);
James Feistddb78302018-09-06 11:45:42 -0700785 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700786 return false;
787 }
788 }
789
James Feist98132792019-07-09 13:29:09 -0700790 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
791 fru[index]) < 0)
James Feistb49ffc32018-05-02 11:10:43 -0700792 {
793 if (!retries--)
794 {
795 std::cerr << "error writing fru: " << strerror(errno)
796 << "\n";
797 close(file);
James Feistddb78302018-09-06 11:45:42 -0700798 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700799 return false;
800 }
801 }
802 else
803 {
804 retries = RETRY_MAX;
805 index++;
806 }
807 // most eeproms require 5-10ms between writes
808 std::this_thread::sleep_for(std::chrono::milliseconds(10));
809 }
810 close(file);
811 return true;
812 }
813}
814
James Feist9eb0b582018-04-27 12:15:46 -0700815void rescanBusses(
James Feist98132792019-07-09 13:29:09 -0700816 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800817 boost::container::flat_map<
818 std::pair<size_t, size_t>,
819 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feista465ccc2019-02-08 12:51:01 -0800820 sdbusplus::asio::object_server& objServer)
James Feist918e18c2018-02-13 15:51:07 -0800821{
James Feist6ebf9de2018-05-15 15:01:17 -0700822 static boost::asio::deadline_timer timer(io);
823 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800824
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500825 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -0700826 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -0800827 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800828 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800829
Nikhil Potaded8884f12019-03-27 13:27:13 -0700830 boost::container::flat_map<size_t, fs::path> busPaths;
831 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -0800832 {
James Feist4131aea2018-03-09 09:47:30 -0800833 std::cerr << "unable to find i2c devices\n";
834 return;
James Feist918e18c2018-02-13 15:51:07 -0800835 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700836
837 for (auto busPath : busPaths)
838 {
839 i2cBuses.emplace_back(busPath.second);
840 }
James Feist4131aea2018-03-09 09:47:30 -0800841
James Feist98132792019-07-09 13:29:09 -0700842 busmap.clear();
James Feist6ebf9de2018-05-15 15:01:17 -0700843 auto scan = std::make_shared<FindDevicesWithCallback>(
James Feist98132792019-07-09 13:29:09 -0700844 i2cBuses, io, busmap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -0800845 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700846 {
847 objServer.remove_interface(busIface.second);
848 }
James Feist4131aea2018-03-09 09:47:30 -0800849
James Feist6ebf9de2018-05-15 15:01:17 -0700850 dbusInterfaceMap.clear();
851 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -0800852
James Feist6ebf9de2018-05-15 15:01:17 -0700853 // todo, get this from a more sensable place
854 std::vector<char> baseboardFru;
855 if (readBaseboardFru(baseboardFru))
856 {
857 boost::container::flat_map<int, std::vector<char>>
858 baseboardDev;
859 baseboardDev.emplace(0, baseboardFru);
James Feist98132792019-07-09 13:29:09 -0700860 busmap[0] = std::make_shared<DeviceMap>(baseboardDev);
James Feist6ebf9de2018-05-15 15:01:17 -0700861 }
James Feist98132792019-07-09 13:29:09 -0700862 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -0700863 {
James Feista465ccc2019-02-08 12:51:01 -0800864 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -0700865 {
James Feist98132792019-07-09 13:29:09 -0700866 AddFruObjectToDbus(device.second, objServer,
James Feist6ebf9de2018-05-15 15:01:17 -0700867 dbusInterfaceMap, devicemap.first,
868 device.first);
869 }
870 }
871 });
872 scan->run();
873 });
James Feist918e18c2018-02-13 15:51:07 -0800874}
875
James Feist98132792019-07-09 13:29:09 -0700876int main()
James Feist3cb5fec2018-01-23 14:41:51 -0800877{
878 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -0800879 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -0800880 std::vector<fs::path> i2cBuses;
881
James Feista3c180a2018-08-09 16:06:04 -0700882 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -0800883 {
884 std::cerr << "unable to find i2c devices\n";
885 return 1;
886 }
James Feist3cb5fec2018-01-23 14:41:51 -0800887
Patrick Venture11f1ff42019-08-01 10:42:12 -0700888 // check for and load blacklist with initial buses.
889 loadBlacklist(blacklistPath);
890
James Feist3cb5fec2018-01-23 14:41:51 -0800891 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -0700892 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
893 auto objServer = sdbusplus::asio::object_server(systemBus);
Vijay Khemka065f6d92018-12-18 10:37:47 -0800894 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -0800895
James Feist6ebf9de2018-05-15 15:01:17 -0700896 // this is a map with keys of pair(bus number, address) and values of
897 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -0800898 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700899 std::shared_ptr<sdbusplus::asio::dbus_interface>>
900 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -0800901
James Feist9eb0b582018-04-27 12:15:46 -0700902 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
903 objServer.add_interface("/xyz/openbmc_project/FruDevice",
904 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -0800905
906 iface->register_method("ReScan", [&]() {
James Feist98132792019-07-09 13:29:09 -0700907 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -0800908 });
James Feist2a9d6db2018-04-27 15:48:28 -0700909
Nikhil Potaded8884f12019-03-27 13:27:13 -0700910 iface->register_method("GetRawFru", getFruInfo);
James Feistb49ffc32018-05-02 11:10:43 -0700911
912 iface->register_method("WriteFru", [&](const uint8_t bus,
913 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -0800914 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -0700915 if (!writeFru(bus, address, data))
916 {
James Feistddb78302018-09-06 11:45:42 -0700917 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -0700918 return;
919 }
920 // schedule rescan on success
James Feist98132792019-07-09 13:29:09 -0700921 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feistb49ffc32018-05-02 11:10:43 -0700922 });
James Feist9eb0b582018-04-27 12:15:46 -0700923 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800924
James Feist9eb0b582018-04-27 12:15:46 -0700925 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -0800926 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -0800927 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -0700928 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800929 std::string,
930 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -0700931 values;
932 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -0800933 auto findPgood = values.find("pgood");
934 if (findPgood != values.end())
935 {
James Feist6ebf9de2018-05-15 15:01:17 -0700936
James Feist98132792019-07-09 13:29:09 -0700937 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800938 }
James Feist918e18c2018-02-13 15:51:07 -0800939 };
James Feist9eb0b582018-04-27 12:15:46 -0700940
941 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -0800942 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -0700943 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
944 "openbmc_project/Chassis/Control/"
945 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -0700946 eventHandler);
947
James Feist4131aea2018-03-09 09:47:30 -0800948 int fd = inotify_init();
James Feist0eb40352019-04-09 14:44:04 -0700949 inotify_add_watch(fd, I2C_DEV_LOCATION,
950 IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -0800951 std::array<char, 4096> readBuffer;
952 std::string pendingBuffer;
953 // monitor for new i2c devices
954 boost::asio::posix::stream_descriptor dirWatch(io, fd);
955 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -0800956 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -0800957 std::size_t bytes_transferred) {
958 if (ec)
959 {
960 std::cout << "Callback Error " << ec << "\n";
961 return;
962 }
963 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
964 bool devChange = false;
965 while (pendingBuffer.size() > sizeof(inotify_event))
966 {
James Feista465ccc2019-02-08 12:51:01 -0800967 const inotify_event* iEvent =
968 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -0800969 pendingBuffer.data());
970 switch (iEvent->mask)
971 {
James Feist9eb0b582018-04-27 12:15:46 -0700972 case IN_CREATE:
973 case IN_MOVED_TO:
974 case IN_DELETE:
975 if (boost::starts_with(std::string(iEvent->name),
976 "i2c"))
977 {
978 devChange = true;
979 }
James Feist4131aea2018-03-09 09:47:30 -0800980 }
981
982 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
983 }
James Feist6ebf9de2018-05-15 15:01:17 -0700984 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -0800985 {
James Feist98132792019-07-09 13:29:09 -0700986 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist4131aea2018-03-09 09:47:30 -0800987 }
James Feist6ebf9de2018-05-15 15:01:17 -0700988
James Feist4131aea2018-03-09 09:47:30 -0800989 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
990 watchI2cBusses);
991 };
992
993 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -0500994 // run the initial scan
James Feist98132792019-07-09 13:29:09 -0700995 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800996
James Feist3cb5fec2018-01-23 14:41:51 -0800997 io.run();
998 return 0;
999}