Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2021 Project CHIP Authors
4 : * Copyright (c) 2019 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 generic implementation of ConnectivityManager features
22 : * for use on platforms that support Thread.
23 : */
24 :
25 : #ifndef GENERIC_CONNECTIVITY_MANAGER_IMPL_THREAD_CPP
26 : #define GENERIC_CONNECTIVITY_MANAGER_IMPL_THREAD_CPP
27 :
28 : #include <lib/support/CodeUtils.h>
29 : #include <platform/internal/CHIPDeviceLayerInternal.h>
30 : #include <platform/internal/GenericConnectivityManagerImpl_Thread.h>
31 :
32 : namespace chip {
33 : namespace DeviceLayer {
34 : namespace Internal {
35 :
36 : template <class ImplClass>
37 19 : void GenericConnectivityManagerImpl_Thread<ImplClass>::_OnPlatformEvent(const ChipDeviceEvent * event)
38 : {
39 : // Define some short-hands for various interesting event conditions.
40 19 : const bool threadConnChanged = (event->Type == DeviceEventType::kThreadConnectivityChange &&
41 0 : event->ThreadConnectivityChange.Result != kConnectivity_NoChange);
42 19 : const bool threadAddrChanged = (event->Type == DeviceEventType::kThreadStateChange && event->ThreadStateChange.AddressChanged);
43 19 : const bool threadNetDataChanged =
44 19 : (event->Type == DeviceEventType::kThreadStateChange && event->ThreadStateChange.NetDataChanged);
45 :
46 19 : if (threadConnChanged && event->ThreadConnectivityChange.Result == kConnectivity_Established)
47 : {
48 0 : ThreadStackMgrImpl().OnThreadAttachFinished();
49 : }
50 :
51 : // If any of the above events has occurred, assess whether there's been a change in
52 : // service connectivity via Thread.
53 19 : if (threadConnChanged || threadAddrChanged || threadNetDataChanged)
54 : {
55 0 : UpdateServiceConnectivity();
56 : }
57 19 : }
58 :
59 : template <class ImplClass>
60 0 : void GenericConnectivityManagerImpl_Thread<ImplClass>::UpdateServiceConnectivity()
61 : {
62 0 : constexpr bool haveServiceConnectivity = false;
63 :
64 : // If service connectivity via Thread has changed, post an event signaling the change.
65 0 : if (mFlags.Has(Flags::kHaveServiceConnectivity) != haveServiceConnectivity)
66 : {
67 0 : ChipLogProgress(DeviceLayer, "ConnectivityManager: Service connectivity via Thread %s",
68 : (haveServiceConnectivity) ? "ESTABLISHED" : "LOST");
69 :
70 0 : mFlags.Set(Flags::kHaveServiceConnectivity, haveServiceConnectivity);
71 :
72 : {
73 0 : ChipDeviceEvent event{ .Type = DeviceEventType::kServiceConnectivityChange,
74 : .ServiceConnectivityChange = { .ViaThread = { .Result = (haveServiceConnectivity)
75 : ? kConnectivity_Established
76 : : kConnectivity_Lost } } };
77 0 : event.ServiceConnectivityChange.Overall.Result = event.ServiceConnectivityChange.ViaThread.Result;
78 0 : CHIP_ERROR status = PlatformMgr().PostEvent(&event);
79 0 : if (status != CHIP_NO_ERROR)
80 : {
81 0 : ChipLogError(DeviceLayer, "Failed to post thread connectivity change: %" CHIP_ERROR_FORMAT, status.Format());
82 : }
83 : }
84 : }
85 0 : }
86 :
87 : } // namespace Internal
88 : } // namespace DeviceLayer
89 : } // namespace chip
90 :
91 : #endif // GENERIC_CONNECTIVITY_MANAGER_IMPL_THREAD_CPP
|