|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Project CHIP Authors |
| 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 | +/** |
| 19 | + * @file |
| 20 | + * This file declares an implementation of System::Layer using select(). |
| 21 | + */ |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include "system/SystemConfig.h" |
| 26 | + |
| 27 | +#include <lib/support/ObjectLifeCycle.h> |
| 28 | +#include <system/SystemLayer.h> |
| 29 | +#include <system/SystemTimer.h> |
| 30 | + |
| 31 | +namespace chip { |
| 32 | +namespace System { |
| 33 | + |
| 34 | +class LayerImplDispatch : public LayerDispatch |
| 35 | +{ |
| 36 | +public: |
| 37 | + LayerImplDispatch() = default; |
| 38 | + ~LayerImplDispatch() override { VerifyOrDie(mLayerState.Destroy()); } |
| 39 | + |
| 40 | + // Layer overrides. |
| 41 | + CHIP_ERROR Init() override; |
| 42 | + void Shutdown() override; |
| 43 | + bool IsInitialized() const override { return mLayerState.IsInitialized(); } |
| 44 | + CHIP_ERROR StartTimer(Clock::Timeout delay, TimerCompleteCallback onComplete, void * appState) override; |
| 45 | + CHIP_ERROR ExtendTimerTo(Clock::Timeout delay, TimerCompleteCallback onComplete, void * appState) override; |
| 46 | + bool IsTimerActive(TimerCompleteCallback onComplete, void * appState) override; |
| 47 | + Clock::Timeout GetRemainingTime(TimerCompleteCallback onComplete, void * appState) override; |
| 48 | + void CancelTimer(TimerCompleteCallback onComplete, void * appState) override; |
| 49 | + CHIP_ERROR ScheduleWork(TimerCompleteCallback onComplete, void * appState) override; |
| 50 | + |
| 51 | + // LayerDispatch overrides. |
| 52 | + void SetDispatchQueue(dispatch_queue_t dispatchQueue) override { mDispatchQueue = dispatchQueue; }; |
| 53 | + dispatch_queue_t GetDispatchQueue() override { return mDispatchQueue; }; |
| 54 | + void HandleDispatchQueueEvents(Clock::Timeout timeout) override; |
| 55 | + CHIP_ERROR ScheduleWorkWithBlock(dispatch_block_t block) override; |
| 56 | + |
| 57 | +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS |
| 58 | + // LayerSocket overrides. |
| 59 | + CHIP_ERROR StartWatchingSocket(int fd, SocketWatchToken * tokenOut) override; |
| 60 | + CHIP_ERROR SetCallback(SocketWatchToken token, SocketWatchCallback callback, intptr_t data) override; |
| 61 | + CHIP_ERROR RequestCallbackOnPendingRead(SocketWatchToken token) override; |
| 62 | + CHIP_ERROR RequestCallbackOnPendingWrite(SocketWatchToken token) override; |
| 63 | + CHIP_ERROR ClearCallbackOnPendingRead(SocketWatchToken token) override; |
| 64 | + CHIP_ERROR ClearCallbackOnPendingWrite(SocketWatchToken token) override; |
| 65 | + CHIP_ERROR StopWatchingSocket(SocketWatchToken * tokenInOut) override; |
| 66 | + SocketWatchToken InvalidSocketWatchToken() override { return reinterpret_cast<SocketWatchToken>(nullptr); } |
| 67 | +#endif |
| 68 | + |
| 69 | +protected: |
| 70 | +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS |
| 71 | + struct SelectSets |
| 72 | + { |
| 73 | + fd_set mReadSet; |
| 74 | + fd_set mWriteSet; |
| 75 | + fd_set mErrorSet; |
| 76 | + }; |
| 77 | + |
| 78 | + static constexpr int kSocketWatchMax = (INET_CONFIG_ENABLE_TCP_ENDPOINT ? INET_CONFIG_NUM_TCP_ENDPOINTS : 0) + |
| 79 | + (INET_CONFIG_ENABLE_UDP_ENDPOINT ? INET_CONFIG_NUM_UDP_ENDPOINTS : 0); |
| 80 | + |
| 81 | + struct SocketWatch |
| 82 | + { |
| 83 | + int mFD; |
| 84 | + SocketEvents mPendingIO; |
| 85 | + SocketWatchCallback mCallback; |
| 86 | + intptr_t mCallbackData; |
| 87 | + |
| 88 | + dispatch_source_t mRdSource = nullptr; |
| 89 | + dispatch_source_t mWrSource = nullptr; |
| 90 | + |
| 91 | + bool IsActive() const { return mFD != kInvalidFd; } |
| 92 | + void PrepareEvents(SelectSets & sets, int & maxFd) const; |
| 93 | + void HandleEvents(const SelectSets & sets) const; |
| 94 | + void Clear(); |
| 95 | + }; |
| 96 | + SocketWatch mSocketWatchPool[kSocketWatchMax]; |
| 97 | + void ClearSocketWatchPool(); |
| 98 | + void HandleSocketsAndTimerEvents(Clock::Timeout timeout); |
| 99 | + CHIP_ERROR RequestCallback(SocketWatchToken token, SocketEventFlags flag); |
| 100 | + CHIP_ERROR ClearCallback(SocketWatchToken token, SocketEventFlags flag); |
| 101 | +#endif |
| 102 | + |
| 103 | + TimerPool<TimerList::Node> mTimerPool; |
| 104 | + TimerList mTimerList; |
| 105 | + // List of expired timers being processed right now. Stored in a member so |
| 106 | + // we can cancel them. |
| 107 | + TimerList mExpiredTimers; |
| 108 | + timeval mNextTimeout; |
| 109 | + void EnableTimer(const char * source, TimerList::Node *); |
| 110 | + void DisableTimer(const char * source, TimerList::Node *); |
| 111 | + CHIP_ERROR StartTimer(Clock::Timeout delay, TimerCompleteCallback onComplete, void * appState, bool shouldCancel); |
| 112 | + void HandleTimerEvents(Clock::Timeout timeout); |
| 113 | + |
| 114 | + ObjectLifeCycle mLayerState; |
| 115 | + |
| 116 | + dispatch_queue_t mDispatchQueue = nullptr; |
| 117 | +}; |
| 118 | + |
| 119 | +using LayerImpl = LayerImplDispatch; |
| 120 | + |
| 121 | +} // namespace System |
| 122 | +} // namespace chip |
0 commit comments