forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformManagerImpl.cpp
321 lines (258 loc) · 11.2 KB
/
PlatformManagerImpl.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2018 Nest Labs, Inc.
*
* 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.
*/
/**
* @file
* Provides an implementation of the PlatformManager object
* for Linux platforms.
*/
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netpacket/netlink.h>
#include <unistd.h>
#include <mutex>
#include <app-common/zap-generated/ids/Events.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/DeviceControlServer.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <platform/NuttX/DeviceInstanceInfoProviderImpl.h>
#include <platform/NuttX/DiagnosticDataProviderImpl.h>
#include <platform/PlatformManager.h>
#include <platform/internal/GenericPlatformManagerImpl_POSIX.ipp>
using namespace ::chip::app::Clusters;
namespace chip {
namespace DeviceLayer {
PlatformManagerImpl PlatformManagerImpl::sInstance;
namespace {
#if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP
void * GLibMainLoopThread(void * userData)
{
GMainLoop * loop = static_cast<GMainLoop *>(userData);
GMainContext * context = g_main_loop_get_context(loop);
g_main_context_push_thread_default(context);
g_main_loop_run(loop);
return nullptr;
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
gboolean WiFiIPChangeListener(GIOChannel * ch, GIOCondition /* condition */, void * /* userData */)
{
char buffer[4096];
auto * header = reinterpret_cast<struct nlmsghdr *>(buffer);
ssize_t len;
if ((len = recv(g_io_channel_unix_get_fd(ch), buffer, sizeof(buffer), 0)) == -1)
{
if (errno == EINTR || errno == EAGAIN)
return G_SOURCE_CONTINUE;
ChipLogError(DeviceLayer, "Error reading from netlink socket: %d", errno);
return G_SOURCE_CONTINUE;
}
if (len > 0)
{
for (struct nlmsghdr * messageHeader = header;
(NLMSG_OK(messageHeader, static_cast<uint32_t>(len))) && (messageHeader->nlmsg_type != NLMSG_DONE);
messageHeader = NLMSG_NEXT(messageHeader, len))
{
if (header->nlmsg_type == RTM_NEWADDR)
{
struct ifaddrmsg * addressMessage = (struct ifaddrmsg *) NLMSG_DATA(header);
struct rtattr * routeInfo = IFA_RTA(addressMessage);
size_t rtl = IFA_PAYLOAD(header);
for (; rtl && RTA_OK(routeInfo, rtl); routeInfo = RTA_NEXT(routeInfo, rtl))
{
if (routeInfo->rta_type == IFA_LOCAL)
{
char name[Inet::InterfaceId::kMaxIfNameLength];
if (if_indextoname(addressMessage->ifa_index, name) == nullptr)
{
ChipLogError(DeviceLayer, "Error %d when getting the interface name at index: %d", errno,
addressMessage->ifa_index);
continue;
}
if (ConnectivityMgrImpl().GetWiFiIfName() == nullptr)
{
ChipLogDetail(DeviceLayer, "No wifi interface name. Ignoring IP update event.");
continue;
}
if (strcmp(name, ConnectivityMgrImpl().GetWiFiIfName()) != 0)
{
continue;
}
char ipStrBuf[chip::Inet::IPAddress::kMaxStringLength] = { 0 };
inet_ntop(AF_INET, RTA_DATA(routeInfo), ipStrBuf, sizeof(ipStrBuf));
ChipLogDetail(DeviceLayer, "Got IP address on interface: %s IP: %s", name, ipStrBuf);
ChipDeviceEvent event;
event.Type = DeviceEventType::kInternetConnectivityChange;
event.InternetConnectivityChange.IPv4 = kConnectivity_Established;
event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange;
if (!chip::Inet::IPAddress::FromString(ipStrBuf, event.InternetConnectivityChange.ipAddress))
{
ChipLogDetail(DeviceLayer, "Failed to report IP address - ip address parsing failed");
continue;
}
CHIP_ERROR status = PlatformMgr().PostEvent(&event);
if (status != CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "Failed to report IP address: %" CHIP_ERROR_FORMAT, status.Format());
}
}
}
}
}
}
else
{
ChipLogError(DeviceLayer, "EOF on netlink socket");
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
// The temporary hack for getting IP address change on linux for network provisioning in the rendezvous session.
// This should be removed or find a better place once we deprecate the rendezvous session.
CHIP_ERROR RunWiFiIPChangeListener()
{
int sock;
if ((sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
{
ChipLogError(DeviceLayer, "Failed to init netlink socket for IP addresses: %d", errno);
return CHIP_ERROR_INTERNAL;
}
struct sockaddr_nl addr;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = RTMGRP_IPV4_IFADDR;
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1)
{
ChipLogError(DeviceLayer, "Failed to bind netlink socket for IP addresses: %d", errno);
close(sock);
return CHIP_ERROR_INTERNAL;
}
GIOChannel * ch = g_io_channel_unix_new(sock);
GSource * watchSource = g_io_create_watch(ch, G_IO_IN);
g_source_set_callback(watchSource, G_SOURCE_FUNC(WiFiIPChangeListener), nullptr, nullptr);
g_io_channel_set_close_on_unref(ch, TRUE);
g_io_channel_set_encoding(ch, nullptr, nullptr);
PlatformMgrImpl().GLibMatterContextAttachSource(watchSource);
g_source_unref(watchSource);
g_io_channel_unref(ch);
return CHIP_NO_ERROR;
}
#endif // #if CHIP_DEVICE_CONFIG_ENABLE_WIFI
} // namespace
CHIP_ERROR PlatformManagerImpl::_InitChipStack()
{
#if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP
auto * context = g_main_context_new();
mGLibMainLoop = g_main_loop_new(context, FALSE);
mGLibMainLoopThread = g_thread_new("gmain-matter", GLibMainLoopThread, mGLibMainLoop);
g_main_context_unref(context);
{
// Wait for the GLib main loop to start. It is required that the context used
// by the main loop is acquired before any other GLib functions are called. Otherwise,
// the GLibMatterContextInvokeSync() might run functions on the wrong thread.
std::unique_lock<std::mutex> lock(mGLibMainLoopCallbackIndirectionMutex);
GLibMatterContextInvokeData invokeData{};
auto * idleSource = g_idle_source_new();
g_source_set_callback(
idleSource,
[](void * userData_) {
auto * data = reinterpret_cast<GLibMatterContextInvokeData *>(userData_);
std::unique_lock<std::mutex> lock_(PlatformMgrImpl().mGLibMainLoopCallbackIndirectionMutex);
data->mDone = true;
data->mDoneCond.notify_one();
return G_SOURCE_REMOVE;
},
&invokeData, nullptr);
GLibMatterContextAttachSource(idleSource);
g_source_unref(idleSource);
invokeData.mDoneCond.wait(lock, [&invokeData]() { return invokeData.mDone; });
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
ReturnErrorOnFailure(RunWiFiIPChangeListener());
#endif
// Initialize the configuration system.
ReturnErrorOnFailure(Internal::PosixConfig::Init());
// Call _InitChipStack() on the generic implementation base class
// to finish the initialization process.
ReturnErrorOnFailure(Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_InitChipStack());
// Now set up our device instance info provider. We couldn't do that
// earlier, because the generic implementation sets a generic one.
SetDeviceInstanceInfoProvider(&DeviceInstanceInfoProviderMgrImpl());
mStartTime = System::SystemClock().GetMonotonicTimestamp();
return CHIP_NO_ERROR;
}
void PlatformManagerImpl::_Shutdown()
{
uint64_t upTime = 0;
if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR)
{
uint32_t totalOperationalHours = 0;
if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR)
{
ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + static_cast<uint32_t>(upTime / 3600));
}
else
{
ChipLogError(DeviceLayer, "Failed to get total operational hours of the Node");
}
}
else
{
ChipLogError(DeviceLayer, "Failed to get current uptime since the Node’s last reboot");
}
Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_Shutdown();
#if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP
g_main_loop_quit(mGLibMainLoop);
g_thread_join(mGLibMainLoopThread);
g_main_loop_unref(mGLibMainLoop);
#endif
}
#if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP
void PlatformManagerImpl::_GLibMatterContextInvokeSync(LambdaBridge && bridge)
{
// Because of TSAN false positives, we need to use a mutex to synchronize access to all members of
// the GLibMatterContextInvokeData object (including constructor and destructor). This is a temporary
// workaround until TSAN-enabled GLib will be used in our CI.
std::unique_lock<std::mutex> lock(mGLibMainLoopCallbackIndirectionMutex);
GLibMatterContextInvokeData invokeData{ std::move(bridge) };
lock.unlock();
g_main_context_invoke_full(
g_main_loop_get_context(mGLibMainLoop), G_PRIORITY_HIGH_IDLE,
[](void * userData_) {
auto * data = reinterpret_cast<GLibMatterContextInvokeData *>(userData_);
// XXX: Temporary workaround for TSAN false positives.
std::unique_lock<std::mutex> lock_(PlatformMgrImpl().mGLibMainLoopCallbackIndirectionMutex);
lock_.unlock();
data->bridge();
lock_.lock();
data->mDone = true;
data->mDoneCond.notify_one();
return G_SOURCE_REMOVE;
},
&invokeData, nullptr);
lock.lock();
invokeData.mDoneCond.wait(lock, [&invokeData]() { return invokeData.mDone; });
}
#endif // CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP
} // namespace DeviceLayer
} // namespace chip