blob: b45e6d01475c338a3109d7d420d809e3282091d3 [file] [log] [blame]
/*
*
* 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 <platform/PlatformManager.h>
#include <platform/internal/GenericPlatformManagerImpl_POSIX.cpp>
#include <support/CHIPMem.h>
#include <support/logging/CHIPLogging.h>
#include <thread>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <netinet/in.h>
namespace chip {
namespace DeviceLayer {
PlatformManagerImpl PlatformManagerImpl::sInstance;
#if CHIP_WITH_GIO
static void GDBus_Thread()
{
GMainLoop * loop = g_main_loop_new(nullptr, false);
g_main_loop_run(loop);
g_main_loop_unref(loop);
}
#endif
void PlatformManagerImpl::WiFIIPChangeListener()
{
int sock;
if ((sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
{
ChipLogError(DeviceLayer, "Failed to init netlink socket for ip addresses.");
return;
}
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.");
return;
}
ssize_t len;
char buffer[4096];
for (struct nlmsghdr * header = reinterpret_cast<struct nlmsghdr *>(buffer); (len = recv(sock, header, sizeof(buffer), 0)) > 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);
while (rtl && RTA_OK(routeInfo, rtl))
{
if (routeInfo->rta_type == IFA_LOCAL)
{
char name[IFNAMSIZ];
ChipDeviceEvent event;
if_indextoname(addressMessage->ifa_index, name);
if (strcmp(name, CHIP_DEVICE_CONFIG_WIFI_STATION_IF_NAME) != 0)
{
continue;
}
event.Type = DeviceEventType::kInternetConnectivityChange;
event.InternetConnectivityChange.IPv4 = kConnectivity_Established;
event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange;
inet_ntop(AF_INET, RTA_DATA(routeInfo), event.InternetConnectivityChange.address,
sizeof(event.InternetConnectivityChange.address));
ChipLogDetail(DeviceLayer, "Got IP address on interface: %s IP: %s", name,
event.InternetConnectivityChange.address);
PlatformMgr().LockChipStack();
PlatformMgr().PostEvent(&event);
PlatformMgr().UnlockChipStack();
}
routeInfo = RTA_NEXT(routeInfo, rtl);
}
}
}
}
}
CHIP_ERROR PlatformManagerImpl::_InitChipStack()
{
CHIP_ERROR err;
#if CHIP_WITH_GIO
GError * error = nullptr;
this->mpGDBusConnection = UniqueGDBusConnection(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error));
std::thread gdbusThread(GDBus_Thread);
gdbusThread.detach();
#endif
std::thread wifiIPThread(WiFIIPChangeListener);
wifiIPThread.detach();
// Initialize the configuration system.
err = Internal::PosixConfig::Init();
SuccessOrExit(err);
// Call _InitChipStack() on the generic implementation base class
// to finish the initialization process.
err = Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_InitChipStack();
SuccessOrExit(err);
exit:
return err;
}
#if CHIP_WITH_GIO
GDBusConnection * PlatformManagerImpl::GetGDBusConnection()
{
return this->mpGDBusConnection.get();
}
#endif
} // namespace DeviceLayer
} // namespace chip