blob: e66735b1de68f2a5a0bf673497069d1b6f8f28a0 [file] [log] [blame]
Andrii Bilynskyi7c2e7db2023-04-05 15:44:29 +03001/*
2 *
3 * Copyright (c) 2023 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 * Provides an implementation of the ThreadStackManager object
21 * for Telink platform.
22 *
23 */
24/* this file behaves like a config.h, comes first */
25#include <platform/internal/CHIPDeviceLayerInternal.h>
26
Sergei Lissianoi56518752023-05-05 09:19:52 -040027#include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp>
Andrii Bilynskyi7c2e7db2023-04-05 15:44:29 +030028#include <platform/telink/ThreadStackManagerImpl.h>
29
30#include <inet/UDPEndPointImpl.h>
31#include <lib/support/CodeUtils.h>
32#include <platform/OpenThread/OpenThreadUtils.h>
33#include <platform/ThreadStackManager.h>
34
35namespace chip {
36namespace DeviceLayer {
37
38using namespace ::chip::DeviceLayer::Internal;
39using namespace ::chip::Inet;
40
41ThreadStackManagerImpl ThreadStackManagerImpl::sInstance;
42
43CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack()
44{
45 mRadioBlocked = false;
46 mReadyToAttach = false;
47 otInstance * const instance = openthread_get_default_instance();
48
49 ReturnErrorOnFailure(GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::DoInit(instance));
Andrii Bilynskyi7c2e7db2023-04-05 15:44:29 +030050
51 UDPEndPointImplSockets::SetJoinMulticastGroupHandler([](InterfaceId, const IPAddress & address) {
52 const otIp6Address otAddress = ToOpenThreadIP6Address(address);
53
54 ThreadStackMgr().LockThreadStack();
55 const auto otError = otIp6SubscribeMulticastAddress(openthread_get_default_instance(), &otAddress);
56 ThreadStackMgr().UnlockThreadStack();
57
58 return MapOpenThreadError(otError);
59 });
60
61 UDPEndPointImplSockets::SetLeaveMulticastGroupHandler([](InterfaceId, const IPAddress & address) {
62 const otIp6Address otAddress = ToOpenThreadIP6Address(address);
63
64 ThreadStackMgr().LockThreadStack();
65 const auto otError = otIp6UnsubscribeMulticastAddress(openthread_get_default_instance(), &otAddress);
66 ThreadStackMgr().UnlockThreadStack();
67
68 return MapOpenThreadError(otError);
69 });
70
71 return CHIP_NO_ERROR;
72}
73
74void ThreadStackManagerImpl::_LockThreadStack()
75{
76 openthread_api_mutex_lock(openthread_get_default_context());
77}
78
79bool ThreadStackManagerImpl::_TryLockThreadStack()
80{
81 // There's no openthread_api_mutex_try_lock() in Zephyr, so until it's contributed we must use the low-level API
82 return k_mutex_lock(&openthread_get_default_context()->api_lock, K_NO_WAIT) == 0;
83}
84
85void ThreadStackManagerImpl::_UnlockThreadStack()
86{
87 openthread_api_mutex_unlock(openthread_get_default_context());
88}
89
90CHIP_ERROR
91ThreadStackManagerImpl::_AttachToThreadNetwork(const Thread::OperationalDataset & dataset,
92 NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * callback)
93{
94 CHIP_ERROR result = CHIP_NO_ERROR;
95
96 if (mRadioBlocked)
97 {
98 /* On Telink platform it's not possible to rise Thread network when its used by BLE,
99 so just mark that it's provisioned and rise Thread after BLE disconnect */
100 result = SetThreadProvision(dataset.AsByteSpan());
101 if (result == CHIP_NO_ERROR)
102 {
103 mReadyToAttach = true;
104 callback->OnResult(NetworkCommissioning::Status::kSuccess, CharSpan(), 0);
105 }
106 }
107 else
108 {
109 result =
110 Internal::GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::_AttachToThreadNetwork(dataset, callback);
111 }
112 return result;
113}
114
Alex Tsitsiurabc0d4222023-05-25 17:26:21 +0300115CHIP_ERROR ThreadStackManagerImpl::_StartThreadScan(NetworkCommissioning::ThreadDriver::ScanCallback * callback)
116{
117 mpScanCallback = callback;
118
119 /* On Telink platform it's not possible to rise Thread network when its used by BLE,
120 so Thread networks scanning performed before start BLE and also available after switch into Thread */
121 if (mRadioBlocked)
122 {
123 if (mpScanCallback != nullptr)
124 {
125 DeviceLayer::SystemLayer().ScheduleLambda([this]() {
126 mpScanCallback->OnFinished(NetworkCommissioning::Status::kSuccess, CharSpan(), &mScanResponseIter);
127 mpScanCallback = nullptr;
128 });
129 }
130 }
131 else
132 {
133 return Internal::GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>::_StartThreadScan(mpScanCallback);
134 }
135
136 return CHIP_NO_ERROR;
137}
138
Alex Tsitsiura76baa662023-05-29 19:19:24 +0300139void ThreadStackManagerImpl::Finalize(void)
140{
141 otInstanceFinalize(openthread_get_default_instance());
142}
143
Andrii Bilynskyi7c2e7db2023-04-05 15:44:29 +0300144} // namespace DeviceLayer
145} // namespace chip