|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "UloopHandler.h" |
| 19 | + |
| 20 | +#include <lib/support/CodeUtils.h> |
| 21 | +#include <platform/CHIPDeviceLayer.h> |
| 22 | + |
| 23 | +#include <chrono> |
| 24 | + |
| 25 | +extern "C" { |
| 26 | +#include <libubox/uloop.h> |
| 27 | +} |
| 28 | + |
| 29 | +namespace chip { |
| 30 | + |
| 31 | +namespace { |
| 32 | +System::LayerSocketsLoop & SystemLayer() |
| 33 | +{ |
| 34 | + return static_cast<System::LayerSocketsLoop &>(DeviceLayer::SystemLayer()); |
| 35 | +} |
| 36 | +} // namespace |
| 37 | + |
| 38 | +UloopHandler UloopHandler::gInstance; |
| 39 | +int UloopHandler::gRegistered = 0; |
| 40 | + |
| 41 | +CHIP_ERROR UloopHandler::Register() |
| 42 | +{ |
| 43 | + VerifyOrReturnError(gRegistered++ == 0, CHIP_NO_ERROR); |
| 44 | + |
| 45 | + if (uloop_fd_set_cb != nullptr) |
| 46 | + { |
| 47 | + gRegistered = 0; |
| 48 | + return CHIP_ERROR_INCORRECT_STATE; |
| 49 | + } |
| 50 | + |
| 51 | + ChipLogDetail(chipSystemLayer, "Registering uloop handler"); |
| 52 | + uloop_fd_set_cb = &UloopFdSet; |
| 53 | + uloop_init(); |
| 54 | + SystemLayer().AddLoopHandler(gInstance); |
| 55 | + |
| 56 | + return CHIP_NO_ERROR; |
| 57 | +} |
| 58 | + |
| 59 | +void UloopHandler::Unregister() |
| 60 | +{ |
| 61 | + auto count = gRegistered--; |
| 62 | + VerifyOrDie(count > 0); |
| 63 | + VerifyOrReturn(count == 1); |
| 64 | + |
| 65 | + ChipLogDetail(chipSystemLayer, "Unregistering uloop handler"); |
| 66 | + SystemLayer().RemoveLoopHandler(gInstance); |
| 67 | + uloop_done(); |
| 68 | + uloop_fd_set_cb = nullptr; |
| 69 | +} |
| 70 | + |
| 71 | +System::Clock::Timestamp UloopHandler::PrepareEvents(System::Clock::Timestamp now) |
| 72 | +{ |
| 73 | + std::chrono::duration<int, std::milli> timeout(uloop_get_next_timeout()); |
| 74 | + VerifyOrReturnValue(timeout.count() >= 0, System::Clock::Timestamp::max()); |
| 75 | + ChipLogDetail(chipSystemLayer, "Next uloop timeout: %d", timeout.count()); |
| 76 | + return now + std::chrono::duration_cast<System::Clock::Timestamp>(timeout); |
| 77 | +} |
| 78 | + |
| 79 | +void UloopHandler::HandleEvents() |
| 80 | +{ |
| 81 | + uloop_run_timeout(0); |
| 82 | +} |
| 83 | + |
| 84 | +void UloopHandler::UloopFdSet(struct uloop_fd * fd, unsigned int events) |
| 85 | +{ |
| 86 | + ChipLogDetail(chipSystemLayer, "Uloop fd_set(%d, %d)", fd->fd, events); |
| 87 | + |
| 88 | + auto & system = SystemLayer(); |
| 89 | + System::SocketWatchToken watch; |
| 90 | + CHIP_ERROR err = system.StartWatchingSocket(fd->fd, &watch); // or find existing watch |
| 91 | + if (events != 0) |
| 92 | + { |
| 93 | + int changed = fd->flags ^ events; |
| 94 | + if (changed & ULOOP_READ) |
| 95 | + { |
| 96 | + if (events & ULOOP_READ) |
| 97 | + { |
| 98 | + system.RequestCallbackOnPendingRead(watch); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + system.ClearCallbackOnPendingRead(watch); |
| 103 | + } |
| 104 | + } |
| 105 | + if (changed & ULOOP_WRITE) |
| 106 | + { |
| 107 | + if (events & ULOOP_WRITE) |
| 108 | + { |
| 109 | + system.RequestCallbackOnPendingWrite(watch); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + system.ClearCallbackOnPendingWrite(watch); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + VerifyOrReturn(err == CHIP_NO_ERROR); // shouldn't happen, but ignore |
| 120 | + SuccessOrExit(err = system.StopWatchingSocket(&watch)); |
| 121 | + } |
| 122 | + |
| 123 | + return; |
| 124 | +exit: |
| 125 | + ChipLogError(chipSystemLayer, "Failed to handle uloop fd_set(%d, %d): %" CHIP_ERROR_FORMAT, fd->fd, events, err.Format()); |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace chip |
0 commit comments