|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2020 Project CHIP Authors |
| 4 | + * Copyright (c) 2018 Nest Labs, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @file |
| 21 | + * Provides an implementation of the PlatformManager object |
| 22 | + * for Darwin platforms. |
| 23 | + */ |
| 24 | + |
| 25 | +#if !__has_feature(objc_arc) |
| 26 | +#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). |
| 27 | +#endif |
| 28 | + |
| 29 | +#include <platform/internal/CHIPDeviceLayerInternal.h> |
| 30 | +#include <tracing/metric_macros.h> |
| 31 | + |
| 32 | +#if !CHIP_DISABLE_PLATFORM_KVS |
| 33 | +#include <platform/Darwin/DeviceInstanceInfoProviderImpl.h> |
| 34 | +#include <platform/DeviceInstanceInfoProvider.h> |
| 35 | +#endif |
| 36 | + |
| 37 | +#include <platform/Darwin/DiagnosticDataProviderImpl.h> |
| 38 | +#include <platform/Darwin/PlatformMetricKeys.h> |
| 39 | +#include <platform/PlatformManager.h> |
| 40 | + |
| 41 | +// Include the non-inline definitions for the GenericPlatformManagerImpl<> template, |
| 42 | +#if CHIP_SYSTEM_CONFIG_USE_DISPATCH |
| 43 | +#include <platform/internal/GenericPlatformManagerImpl.ipp> |
| 44 | +#else |
| 45 | +#include <platform/internal/GenericPlatformManagerImpl_POSIX.ipp> |
| 46 | +#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH |
| 47 | + |
| 48 | +#include <CoreFoundation/CoreFoundation.h> |
| 49 | +#include <tracing/metric_event.h> |
| 50 | + |
| 51 | +using namespace chip::Tracing::DarwinPlatform; |
| 52 | + |
| 53 | +namespace chip { |
| 54 | +namespace DeviceLayer { |
| 55 | + |
| 56 | + AtomicGlobal<PlatformManagerImpl> PlatformManagerImpl::sInstance; |
| 57 | + |
| 58 | + PlatformManagerImpl::PlatformManagerImpl() |
| 59 | + : mWorkQueue(dispatch_queue_create("org.csa-iot.matter.workqueue", |
| 60 | + dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL, |
| 61 | + QOS_CLASS_USER_INITIATED, QOS_MIN_RELATIVE_PRIORITY))) |
| 62 | + { |
| 63 | + // Tag our queue for IsWorkQueueCurrentQueue() |
| 64 | + dispatch_queue_set_specific(mWorkQueue, this, this, nullptr); |
| 65 | + dispatch_suspend(mWorkQueue); |
| 66 | + } |
| 67 | + |
| 68 | + CHIP_ERROR PlatformManagerImpl::_InitChipStack() |
| 69 | + { |
| 70 | + // Initialize the configuration system. |
| 71 | +#if !CHIP_DISABLE_PLATFORM_KVS |
| 72 | + ReturnErrorOnFailure(Internal::PosixConfig::Init()); |
| 73 | +#endif // CHIP_DISABLE_PLATFORM_KVS |
| 74 | + |
| 75 | +#if CHIP_SYSTEM_CONFIG_USE_DISPATCH |
| 76 | + // Ensure there is a dispatch queue available |
| 77 | + static_cast<System::LayerSocketsLoop &>(DeviceLayer::SystemLayer()).SetDispatchQueue(GetWorkQueue()); |
| 78 | +#endif |
| 79 | + |
| 80 | + // Call _InitChipStack() on the generic implementation base class |
| 81 | + // to finish the initialization process. |
| 82 | + ReturnErrorOnFailure(Internal::GenericPlatformManagerImpl<PlatformManagerImpl>::_InitChipStack()); |
| 83 | + |
| 84 | +#if !CHIP_DISABLE_PLATFORM_KVS |
| 85 | + // Now set up our device instance info provider. We couldn't do that |
| 86 | + // earlier, because the generic implementation sets a generic one. |
| 87 | + SetDeviceInstanceInfoProvider(&DeviceInstanceInfoProviderMgrImpl()); |
| 88 | +#endif // CHIP_DISABLE_PLATFORM_KVS |
| 89 | + |
| 90 | + mStartTime = System::SystemClock().GetMonotonicTimestamp(); |
| 91 | + return CHIP_NO_ERROR; |
| 92 | + } |
| 93 | + |
| 94 | +#if CHIP_SYSTEM_CONFIG_USE_DISPATCH |
| 95 | + CHIP_ERROR PlatformManagerImpl::_StartEventLoopTask() |
| 96 | + { |
| 97 | + auto expected = WorkQueueState::kSuspended; |
| 98 | + VerifyOrReturnError(mWorkQueueState.compare_exchange_strong(expected, WorkQueueState::kRunning), CHIP_ERROR_INCORRECT_STATE); |
| 99 | + dispatch_resume(mWorkQueue); |
| 100 | + return CHIP_NO_ERROR; |
| 101 | + }; |
| 102 | + |
| 103 | + CHIP_ERROR PlatformManagerImpl::_StopEventLoopTask() |
| 104 | + { |
| 105 | + auto expected = WorkQueueState::kRunning; |
| 106 | + VerifyOrReturnError(mWorkQueueState.compare_exchange_strong(expected, WorkQueueState::kSuspensionPending), |
| 107 | + CHIP_ERROR_INCORRECT_STATE); |
| 108 | + |
| 109 | + // We need to dispatch to the work queue to ensure any currently queued jobs |
| 110 | + // finish executing. When called from outside the work queue we also need to |
| 111 | + // wait for them to complete before returning to the caller, so we use |
| 112 | + // dispatch_sync in that case. |
| 113 | + (IsWorkQueueCurrentQueue() ? dispatch_async : dispatch_sync)(mWorkQueue, ^{ |
| 114 | + dispatch_suspend(mWorkQueue); |
| 115 | + mWorkQueueState.store(WorkQueueState::kSuspended); |
| 116 | + auto * semaphore = mRunLoopSem; |
| 117 | + if (semaphore != nullptr) { |
| 118 | + dispatch_semaphore_signal(semaphore); |
| 119 | + } |
| 120 | + }); |
| 121 | + return CHIP_NO_ERROR; |
| 122 | + } |
| 123 | + |
| 124 | + void PlatformManagerImpl::_RunEventLoop() |
| 125 | + { |
| 126 | + mRunLoopSem = dispatch_semaphore_create(0); |
| 127 | + |
| 128 | + _StartEventLoopTask(); |
| 129 | + |
| 130 | + // |
| 131 | + // Block on the semaphore till we're signalled to stop by |
| 132 | + // _StopEventLoopTask() |
| 133 | + // |
| 134 | + dispatch_semaphore_wait(mRunLoopSem, DISPATCH_TIME_FOREVER); |
| 135 | + mRunLoopSem = nullptr; |
| 136 | + } |
| 137 | + |
| 138 | + CHIP_ERROR PlatformManagerImpl::_PostEvent(const ChipDeviceEvent * event) |
| 139 | + { |
| 140 | + const ChipDeviceEvent eventCopy = *event; |
| 141 | + dispatch_async(mWorkQueue, ^{ |
| 142 | + DispatchEvent(&eventCopy); |
| 143 | + }); |
| 144 | + return CHIP_NO_ERROR; |
| 145 | + } |
| 146 | +#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH |
| 147 | + |
| 148 | +#if CHIP_STACK_LOCK_TRACKING_ENABLED |
| 149 | + bool PlatformManagerImpl::_IsChipStackLockedByCurrentThread() const |
| 150 | + { |
| 151 | + // Assume our caller knows what they are doing in terms of concurrency if the work queue is suspended. |
| 152 | + return IsWorkQueueCurrentQueue() || mWorkQueueState.load() == WorkQueueState::kSuspended; |
| 153 | + }; |
| 154 | +#endif |
| 155 | + |
| 156 | + bool PlatformManagerImpl::IsWorkQueueCurrentQueue() const |
| 157 | + { |
| 158 | + return dispatch_get_specific(this) == this; |
| 159 | + } |
| 160 | + |
| 161 | + CHIP_ERROR PlatformManagerImpl::StartBleScan(BleScannerDelegate * delegate, BleScanMode mode) |
| 162 | + { |
| 163 | +#if CONFIG_NETWORK_LAYER_BLE |
| 164 | + ReturnErrorOnFailureWithMetric(kMetricBLEScan, Internal::BLEMgrImpl().StartScan(delegate, mode)); |
| 165 | + return CHIP_NO_ERROR; |
| 166 | +#else |
| 167 | + return CHIP_ERROR_NOT_IMPLEMENTED; |
| 168 | +#endif // CONFIG_NETWORK_LAYER_BLE |
| 169 | + } |
| 170 | + |
| 171 | + CHIP_ERROR PlatformManagerImpl::StopBleScan() |
| 172 | + { |
| 173 | +#if CONFIG_NETWORK_LAYER_BLE |
| 174 | + ReturnErrorOnFailureWithMetric(kMetricBLEScan, Internal::BLEMgrImpl().StopScan()); |
| 175 | + return CHIP_NO_ERROR; |
| 176 | +#else |
| 177 | + return CHIP_ERROR_NOT_IMPLEMENTED; |
| 178 | +#endif // CONFIG_NETWORK_LAYER_BLE |
| 179 | + } |
| 180 | + |
| 181 | +} // namespace DeviceLayer |
| 182 | +} // namespace chip |
0 commit comments