Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <fcntl.h> |
| 18 | #include <phosphor-logging/log.hpp> |
| 19 | #include "monitor.hpp" |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace gpio |
| 23 | { |
| 24 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame^] | 25 | using namespace phosphor::logging; |
| 26 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 27 | // Populate the file descriptor for passed in device |
| 28 | int Monitor::openDevice() |
| 29 | { |
| 30 | using namespace phosphor::logging; |
| 31 | |
| 32 | int fd = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
| 33 | if (fd < 0) |
| 34 | { |
| 35 | log<level::ERR>("Failed to open device", |
| 36 | entry("PATH=%s", path.c_str())); |
| 37 | throw std::runtime_error("Failed to open device"); |
| 38 | } |
| 39 | return fd; |
| 40 | } |
| 41 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame^] | 42 | // Attaches the FD to event loop and registers the callback handler |
| 43 | void Monitor::registerCallback() |
| 44 | { |
| 45 | decltype(eventSource.get()) sourcePtr = nullptr; |
| 46 | auto r = sd_event_add_io(event.get(), &sourcePtr, (fd)(), |
| 47 | EPOLLIN, callbackHandler, this); |
| 48 | eventSource.reset(sourcePtr); |
| 49 | |
| 50 | if (r < 0) |
| 51 | { |
| 52 | log<level::ERR>("Failed to register callback handler", |
| 53 | entry("ERROR=%s", strerror(-r))); |
| 54 | throw std::runtime_error("Failed to register callback handler"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Callback handler when there is an activity on the FD |
| 59 | int Monitor::processEvents(sd_event_source* es, int fd, |
| 60 | uint32_t revents, void* userData) |
| 61 | { |
| 62 | // TODO. This calls into starting configured target |
| 63 | log<level::INFO>("Callback handler called"); |
| 64 | return 0; |
| 65 | } |
| 66 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 67 | } // namespace gpio |
| 68 | } // namespace phosphor |