forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConnectivityManagerImpl.cpp
145 lines (121 loc) · 4.89 KB
/
ConnectivityManagerImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
*
* Copyright (c) 2021-2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <inet/UDPEndPointImplSockets.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/ConnectivityManager.h>
#include <platform/Zephyr/InetUtils.h>
#include <platform/internal/BLEManager.h>
#include <zephyr/net/net_if.h>
#include <platform/internal/GenericConnectivityManagerImpl_UDP.ipp>
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <platform/internal/GenericConnectivityManagerImpl_TCP.ipp>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.ipp>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/internal/GenericConnectivityManagerImpl_Thread.ipp>
#endif
using namespace ::chip::Inet;
using namespace ::chip::DeviceLayer::Internal;
namespace chip {
namespace DeviceLayer {
namespace {
CHIP_ERROR JoinLeaveMulticastGroup(net_if * iface, const Inet::IPAddress & address,
UDPEndPointImplSockets::MulticastOperation operation)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
if (net_if_l2(iface) == &NET_L2_GET_NAME(OPENTHREAD))
{
const otIp6Address otAddress = ToOpenThreadIP6Address(address);
const auto handler = operation == UDPEndPointImplSockets::MulticastOperation::kJoin ? otIp6SubscribeMulticastAddress
: otIp6UnsubscribeMulticastAddress;
otError error;
ThreadStackMgr().LockThreadStack();
error = handler(openthread_get_default_instance(), &otAddress);
ThreadStackMgr().UnlockThreadStack();
return MapOpenThreadError(error);
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
// The following code should also be valid for other interface types, such as Ethernet,
// but they are not officially supported, so for now enable it for Wi-Fi only.
const in6_addr in6Addr = InetUtils::ToZephyrAddr(address);
if (operation == UDPEndPointImplSockets::MulticastOperation::kJoin)
{
net_if_mcast_addr * maddr = net_if_ipv6_maddr_add(iface, &in6Addr);
if (maddr && !net_if_ipv6_maddr_is_joined(maddr))
{
#if defined(CONFIG_ZEPHYR_VERSION_3_3)
net_if_ipv6_maddr_join(maddr);
#else
net_if_ipv6_maddr_join(iface, maddr);
#endif
}
}
else if (operation == UDPEndPointImplSockets::MulticastOperation::kLeave)
{
VerifyOrReturnError(net_if_ipv6_maddr_rm(iface, &in6Addr), CHIP_ERROR_INVALID_ADDRESS);
}
else
{
return CHIP_ERROR_INCORRECT_STATE;
}
#endif
return CHIP_NO_ERROR;
}
} // namespace
ConnectivityManagerImpl ConnectivityManagerImpl::sInstance;
CHIP_ERROR ConnectivityManagerImpl::_Init()
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_Init();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
ReturnErrorOnFailure(InitWiFi());
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_WIFI
UDPEndPointImplSockets::SetMulticastGroupHandler(
[](InterfaceId interfaceId, const IPAddress & address, UDPEndPointImplSockets::MulticastOperation operation) {
if (interfaceId.IsPresent())
{
net_if * iface = InetUtils::GetInterface(interfaceId);
VerifyOrReturnError(iface != nullptr, INET_ERROR_UNKNOWN_INTERFACE);
return JoinLeaveMulticastGroup(iface, address, operation);
}
// If the interface is not specified, join or leave the multicast group on all interfaces.
for (int i = 1; net_if * iface = net_if_get_by_index(i); i++)
{
ReturnErrorOnFailure(JoinLeaveMulticastGroup(iface, address, operation));
}
return CHIP_NO_ERROR;
});
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_WIFI
return CHIP_NO_ERROR;
}
void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
// Forward the event to the generic base classes as needed.
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_OnPlatformEvent(event);
#endif
}
} // namespace DeviceLayer
} // namespace chip