|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 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 | + |
| 19 | +#include "CHIPCommandBridge.h" |
| 20 | + |
| 21 | +#import <CHIP/CHIPDeviceController.h> |
| 22 | +#include <core/CHIPBuildConfig.h> |
| 23 | +#include <lib/support/CodeUtils.h> |
| 24 | + |
| 25 | +const uint16_t kListenPort = 5541; |
| 26 | + |
| 27 | +CHIP_ERROR CHIPCommandBridge::Run() |
| 28 | +{ |
| 29 | + NSLog(@"Running Command"); |
| 30 | + |
| 31 | + mController = [CHIPDeviceController sharedController]; |
| 32 | + [mController setListenPort:kListenPort]; |
| 33 | + [mController startup:nil vendorId:0 nocSigner:nil]; |
| 34 | + |
| 35 | + RunCommand(); |
| 36 | + ReturnLogErrorOnFailure(StartWaiting(GetWaitDuration())); |
| 37 | + return CHIP_NO_ERROR; |
| 38 | +} |
| 39 | + |
| 40 | +CHIPDeviceController * CHIPCommandBridge::CurrentCommissioner() { return mController; } |
| 41 | + |
| 42 | +CHIP_ERROR CHIPCommandBridge::ShutdownCommissioner() |
| 43 | +{ |
| 44 | + NSLog(@"Shutting down controller"); |
| 45 | + BOOL result = [CurrentCommissioner() shutdown]; |
| 46 | + if (!result) { |
| 47 | + NSLog(@"Unable to shut down controller"); |
| 48 | + return CHIP_ERROR_INTERNAL; |
| 49 | + } |
| 50 | + return CHIP_NO_ERROR; |
| 51 | +} |
| 52 | + |
| 53 | +#if !CONFIG_USE_SEPARATE_EVENTLOOP |
| 54 | +static void OnResponseTimeout(chip::System::Layer *, void * appState) |
| 55 | +{ |
| 56 | + (reinterpret_cast<CHIPCommandBridge *>(appState))->SetCommandExitStatus(CHIP_ERROR_TIMEOUT); |
| 57 | +} |
| 58 | +#endif // !CONFIG_USE_SEPARATE_EVENTLOOP |
| 59 | + |
| 60 | +CHIP_ERROR CHIPCommandBridge::StartWaiting(chip::System::Clock::Timeout duration) |
| 61 | +{ |
| 62 | +#if CONFIG_USE_SEPARATE_EVENTLOOP |
| 63 | + chip::DeviceLayer::PlatformMgr().StartEventLoopTask(); |
| 64 | + auto waitingUntil = std::chrono::system_clock::now() + std::chrono::duration_cast<std::chrono::seconds>(duration); |
| 65 | + { |
| 66 | + std::unique_lock<std::mutex> lk(cvWaitingForResponseMutex); |
| 67 | + if (!cvWaitingForResponse.wait_until(lk, waitingUntil, [this]() { return !this->mWaitingForResponse; })) { |
| 68 | + mCommandExitStatus = CHIP_ERROR_TIMEOUT; |
| 69 | + } |
| 70 | + } |
| 71 | + LogErrorOnFailure(chip::DeviceLayer::PlatformMgr().StopEventLoopTask()); |
| 72 | +#else |
| 73 | + ReturnLogErrorOnFailure(chip::DeviceLayer::SystemLayer().StartTimer(duration, OnResponseTimeout, this)); |
| 74 | + chip::DeviceLayer::PlatformMgr().RunEventLoop(); |
| 75 | +#endif // CONFIG_USE_SEPARATE_EVENTLOOP |
| 76 | + |
| 77 | + return mCommandExitStatus; |
| 78 | +} |
| 79 | + |
| 80 | +void CHIPCommandBridge::StopWaiting() |
| 81 | +{ |
| 82 | +#if CONFIG_USE_SEPARATE_EVENTLOOP |
| 83 | + { |
| 84 | + std::lock_guard<std::mutex> lk(cvWaitingForResponseMutex); |
| 85 | + mWaitingForResponse = false; |
| 86 | + } |
| 87 | + cvWaitingForResponse.notify_all(); |
| 88 | +#else // CONFIG_USE_SEPARATE_EVENTLOOP |
| 89 | + LogErrorOnFailure(chip::DeviceLayer::PlatformMgr().StopEventLoopTask()); |
| 90 | +#endif // CONFIG_USE_SEPARATE_EVENTLOOP |
| 91 | +} |
0 commit comments